1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS filesystem file handling
4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
12 #include <linux/pagemap.h>
13 #include <linux/writeback.h>
14 #include <linux/gfp.h>
15 #include <linux/task_io_accounting_ops.h>
17 #include <linux/swap.h>
18 #include <linux/netfs.h>
19 #include <trace/events/netfs.h>
22 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
24 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
25 static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
26 struct pipe_inode_info *pipe,
27 size_t len, unsigned int flags);
28 static void afs_vm_open(struct vm_area_struct *area);
29 static void afs_vm_close(struct vm_area_struct *area);
30 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
32 const struct file_operations afs_file_operations = {
34 .release = afs_release,
35 .llseek = generic_file_llseek,
36 .read_iter = afs_file_read_iter,
37 .write_iter = netfs_file_write_iter,
38 .mmap = afs_file_mmap,
39 .splice_read = afs_file_splice_read,
40 .splice_write = iter_file_splice_write,
46 const struct inode_operations afs_file_inode_operations = {
47 .getattr = afs_getattr,
48 .setattr = afs_setattr,
49 .permission = afs_permission,
52 const struct address_space_operations afs_file_aops = {
53 .direct_IO = noop_direct_IO,
54 .read_folio = netfs_read_folio,
55 .readahead = netfs_readahead,
56 .dirty_folio = netfs_dirty_folio,
57 .release_folio = netfs_release_folio,
58 .invalidate_folio = netfs_invalidate_folio,
59 .migrate_folio = filemap_migrate_folio,
60 .writepages = afs_writepages,
63 static const struct vm_operations_struct afs_vm_ops = {
65 .close = afs_vm_close,
66 .fault = filemap_fault,
67 .map_pages = afs_vm_map_pages,
68 .page_mkwrite = afs_page_mkwrite,
72 * Discard a pin on a writeback key.
74 void afs_put_wb_key(struct afs_wb_key *wbk)
76 if (wbk && refcount_dec_and_test(&wbk->usage)) {
83 * Cache key for writeback.
85 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
87 struct afs_wb_key *wbk, *p;
89 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
92 refcount_set(&wbk->usage, 2);
95 spin_lock(&vnode->wb_lock);
96 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
97 if (p->key == wbk->key)
102 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
103 spin_unlock(&vnode->wb_lock);
108 refcount_inc(&p->usage);
109 spin_unlock(&vnode->wb_lock);
116 * open an AFS file or directory and attach a key to it
118 int afs_open(struct inode *inode, struct file *file)
120 struct afs_vnode *vnode = AFS_FS_I(inode);
125 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
127 key = afs_request_key(vnode->volume->cell);
133 af = kzalloc(sizeof(*af), GFP_KERNEL);
140 ret = afs_validate(vnode, key);
144 if (file->f_mode & FMODE_WRITE) {
145 ret = afs_cache_wb_key(vnode, af);
150 if (file->f_flags & O_TRUNC)
151 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
153 fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
155 file->private_data = af;
164 _leave(" = %d", ret);
169 * release an AFS file or directory and discard its key
171 int afs_release(struct inode *inode, struct file *file)
173 struct afs_vnode_cache_aux aux;
174 struct afs_vnode *vnode = AFS_FS_I(inode);
175 struct afs_file *af = file->private_data;
179 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
181 if ((file->f_mode & FMODE_WRITE))
182 ret = vfs_fsync(file, 0);
184 file->private_data = NULL;
186 afs_put_wb_key(af->wb);
188 if ((file->f_mode & FMODE_WRITE)) {
189 i_size = i_size_read(&vnode->netfs.inode);
190 afs_set_cache_aux(vnode, &aux);
191 fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
193 fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
198 afs_prune_wb_keys(vnode);
199 _leave(" = %d", ret);
203 static void afs_fetch_data_notify(struct afs_operation *op)
205 struct netfs_io_subrequest *subreq = op->fetch.subreq;
207 subreq->error = afs_op_error(op);
208 netfs_read_subreq_terminated(subreq);
211 static void afs_fetch_data_success(struct afs_operation *op)
213 struct afs_vnode *vnode = op->file[0].vnode;
215 _enter("op=%08x", op->debug_id);
216 afs_vnode_commit_status(op, &op->file[0]);
217 afs_stat_v(vnode, n_fetches);
218 atomic_long_add(op->fetch.subreq->transferred, &op->net->n_fetch_bytes);
219 afs_fetch_data_notify(op);
222 static void afs_fetch_data_aborted(struct afs_operation *op)
224 afs_check_for_remote_deletion(op);
225 afs_fetch_data_notify(op);
228 const struct afs_operation_ops afs_fetch_data_operation = {
229 .issue_afs_rpc = afs_fs_fetch_data,
230 .issue_yfs_rpc = yfs_fs_fetch_data,
231 .success = afs_fetch_data_success,
232 .aborted = afs_fetch_data_aborted,
233 .failed = afs_fetch_data_notify,
236 static void afs_issue_read_call(struct afs_operation *op)
238 op->call_responded = false;
240 op->call_abort_code = 0;
241 if (test_bit(AFS_SERVER_FL_IS_YFS, &op->server->flags))
242 yfs_fs_fetch_data(op);
244 afs_fs_fetch_data(op);
247 static void afs_end_read(struct afs_operation *op)
249 if (op->call_responded && op->server)
250 set_bit(AFS_SERVER_FL_RESPONDING, &op->server->flags);
252 if (!afs_op_error(op))
253 afs_fetch_data_success(op);
254 else if (op->cumul_error.aborted)
255 afs_fetch_data_aborted(op);
257 afs_fetch_data_notify(op);
259 afs_end_vnode_operation(op);
260 afs_put_operation(op);
264 * Perform I/O processing on an asynchronous call. The work item carries a ref
265 * to the call struct that we either need to release or to pass on.
267 static void afs_read_receive(struct afs_call *call)
269 struct afs_operation *op = call->op;
270 enum afs_call_state state;
274 state = READ_ONCE(call->state);
275 if (state == AFS_CALL_COMPLETE)
277 trace_afs_read_recv(op, call);
279 while (state < AFS_CALL_COMPLETE && READ_ONCE(call->need_attention)) {
280 WRITE_ONCE(call->need_attention, false);
281 afs_deliver_to_call(call);
282 state = READ_ONCE(call->state);
285 if (state < AFS_CALL_COMPLETE) {
286 netfs_read_subreq_progress(op->fetch.subreq);
287 if (rxrpc_kernel_check_life(call->net->socket, call->rxcall))
289 /* rxrpc terminated the call. */
290 afs_set_call_complete(call, call->error, call->abort_code);
293 op->call_abort_code = call->abort_code;
294 op->call_error = call->error;
295 op->call_responded = call->responded;
300 /* If the call failed, then we need to crank the server rotation
301 * handle and try the next.
303 if (afs_select_fileserver(op)) {
304 afs_issue_read_call(op);
311 void afs_fetch_data_async_rx(struct work_struct *work)
313 struct afs_call *call = container_of(work, struct afs_call, async_work);
315 afs_read_receive(call);
319 void afs_fetch_data_immediate_cancel(struct afs_call *call)
322 afs_get_call(call, afs_call_trace_wake);
323 if (!queue_work(afs_async_calls, &call->async_work))
324 afs_deferred_put_call(call);
325 flush_work(&call->async_work);
330 * Fetch file data from the volume.
332 static void afs_issue_read(struct netfs_io_subrequest *subreq)
334 struct afs_operation *op;
335 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
336 struct key *key = subreq->rreq->netfs_priv;
338 _enter("%s{%llx:%llu.%u},%x,,,",
345 op = afs_alloc_operation(key, vnode->volume);
347 subreq->error = PTR_ERR(op);
348 netfs_read_subreq_terminated(subreq);
352 afs_op_set_vnode(op, 0, vnode);
354 op->fetch.subreq = subreq;
355 op->ops = &afs_fetch_data_operation;
357 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
359 if (subreq->rreq->origin == NETFS_READAHEAD ||
360 subreq->rreq->iocb) {
361 op->flags |= AFS_OPERATION_ASYNC;
363 if (!afs_begin_vnode_operation(op)) {
364 subreq->error = afs_put_operation(op);
365 netfs_read_subreq_terminated(subreq);
369 if (!afs_select_fileserver(op)) {
374 afs_issue_read_call(op);
376 afs_do_sync_operation(op);
380 static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
382 struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
385 rreq->netfs_priv = key_get(afs_file_key(file));
386 rreq->rsize = 256 * 1024;
387 rreq->wsize = 256 * 1024 * 1024;
389 switch (rreq->origin) {
390 case NETFS_READ_SINGLE:
392 struct key *key = afs_request_key(vnode->volume->cell);
396 rreq->netfs_priv = key;
399 case NETFS_WRITEBACK:
400 case NETFS_WRITETHROUGH:
401 case NETFS_UNBUFFERED_WRITE:
402 case NETFS_DIO_WRITE:
403 if (S_ISREG(rreq->inode->i_mode))
404 rreq->io_streams[0].avail = true;
406 case NETFS_WRITEBACK_SINGLE:
413 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
414 struct folio **foliop, void **_fsdata)
416 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
418 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
421 static void afs_free_request(struct netfs_io_request *rreq)
423 key_put(rreq->netfs_priv);
424 afs_put_wb_key(rreq->netfs_priv2);
427 static void afs_update_i_size(struct inode *inode, loff_t new_i_size)
429 struct afs_vnode *vnode = AFS_FS_I(inode);
432 write_seqlock(&vnode->cb_lock);
433 i_size = i_size_read(&vnode->netfs.inode);
434 if (new_i_size > i_size) {
435 i_size_write(&vnode->netfs.inode, new_i_size);
436 inode_set_bytes(&vnode->netfs.inode, new_i_size);
438 write_sequnlock(&vnode->cb_lock);
439 fscache_update_cookie(afs_vnode_cache(vnode), NULL, &new_i_size);
442 static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq)
444 struct afs_vnode *vnode = AFS_FS_I(wreq->inode);
446 afs_invalidate_cache(vnode, 0);
449 const struct netfs_request_ops afs_req_ops = {
450 .init_request = afs_init_request,
451 .free_request = afs_free_request,
452 .check_write_begin = afs_check_write_begin,
453 .issue_read = afs_issue_read,
454 .update_i_size = afs_update_i_size,
455 .invalidate_cache = afs_netfs_invalidate_cache,
456 .begin_writeback = afs_begin_writeback,
457 .prepare_write = afs_prepare_write,
458 .issue_write = afs_issue_write,
459 .retry_request = afs_retry_request,
462 static void afs_add_open_mmap(struct afs_vnode *vnode)
464 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
465 down_write(&vnode->volume->open_mmaps_lock);
467 if (list_empty(&vnode->cb_mmap_link))
468 list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps);
470 up_write(&vnode->volume->open_mmaps_lock);
474 static void afs_drop_open_mmap(struct afs_vnode *vnode)
476 if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1))
479 down_write(&vnode->volume->open_mmaps_lock);
481 read_seqlock_excl(&vnode->cb_lock);
482 // the only place where ->cb_nr_mmap may hit 0
483 // see __afs_break_callback() for the other side...
484 if (atomic_dec_and_test(&vnode->cb_nr_mmap))
485 list_del_init(&vnode->cb_mmap_link);
486 read_sequnlock_excl(&vnode->cb_lock);
488 up_write(&vnode->volume->open_mmaps_lock);
489 flush_work(&vnode->cb_work);
493 * Handle setting up a memory mapping on an AFS file.
495 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
497 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
500 afs_add_open_mmap(vnode);
502 ret = generic_file_mmap(file, vma);
504 vma->vm_ops = &afs_vm_ops;
506 afs_drop_open_mmap(vnode);
510 static void afs_vm_open(struct vm_area_struct *vma)
512 afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
515 static void afs_vm_close(struct vm_area_struct *vma)
517 afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
520 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
522 struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
524 if (afs_check_validity(vnode))
525 return filemap_map_pages(vmf, start_pgoff, end_pgoff);
529 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
531 struct inode *inode = file_inode(iocb->ki_filp);
532 struct afs_vnode *vnode = AFS_FS_I(inode);
533 struct afs_file *af = iocb->ki_filp->private_data;
536 if (iocb->ki_flags & IOCB_DIRECT)
537 return netfs_unbuffered_read_iter(iocb, iter);
539 ret = netfs_start_io_read(inode);
542 ret = afs_validate(vnode, af->key);
544 ret = filemap_read(iocb, iter, 0);
545 netfs_end_io_read(inode);
549 static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
550 struct pipe_inode_info *pipe,
551 size_t len, unsigned int flags)
553 struct inode *inode = file_inode(in);
554 struct afs_vnode *vnode = AFS_FS_I(inode);
555 struct afs_file *af = in->private_data;
558 ret = netfs_start_io_read(inode);
561 ret = afs_validate(vnode, af->key);
563 ret = filemap_splice_read(in, ppos, pipe, len, flags);
564 netfs_end_io_read(inode);