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.
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/netfs.h>
20 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
21 static int afs_readpage(struct file *file, struct page *page);
22 static int afs_symlink_readpage(struct file *file, struct page *page);
23 static void afs_invalidatepage(struct page *page, unsigned int offset,
25 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
27 static void afs_readahead(struct readahead_control *ractl);
28 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
29 static void afs_vm_open(struct vm_area_struct *area);
30 static void afs_vm_close(struct vm_area_struct *area);
31 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
33 const struct file_operations afs_file_operations = {
35 .release = afs_release,
36 .llseek = generic_file_llseek,
37 .read_iter = afs_file_read_iter,
38 .write_iter = afs_file_write,
39 .mmap = afs_file_mmap,
40 .splice_read = generic_file_splice_read,
41 .splice_write = iter_file_splice_write,
47 const struct inode_operations afs_file_inode_operations = {
48 .getattr = afs_getattr,
49 .setattr = afs_setattr,
50 .permission = afs_permission,
53 const struct address_space_operations afs_file_aops = {
54 .readpage = afs_readpage,
55 .readahead = afs_readahead,
56 .set_page_dirty = afs_set_page_dirty,
57 .launder_page = afs_launder_page,
58 .releasepage = afs_releasepage,
59 .invalidatepage = afs_invalidatepage,
60 .write_begin = afs_write_begin,
61 .write_end = afs_write_end,
62 .writepage = afs_writepage,
63 .writepages = afs_writepages,
66 const struct address_space_operations afs_symlink_aops = {
67 .readpage = afs_symlink_readpage,
68 .releasepage = afs_releasepage,
69 .invalidatepage = afs_invalidatepage,
72 static const struct vm_operations_struct afs_vm_ops = {
74 .close = afs_vm_close,
75 .fault = filemap_fault,
76 .map_pages = afs_vm_map_pages,
77 .page_mkwrite = afs_page_mkwrite,
81 * Discard a pin on a writeback key.
83 void afs_put_wb_key(struct afs_wb_key *wbk)
85 if (wbk && refcount_dec_and_test(&wbk->usage)) {
92 * Cache key for writeback.
94 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
96 struct afs_wb_key *wbk, *p;
98 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
101 refcount_set(&wbk->usage, 2);
104 spin_lock(&vnode->wb_lock);
105 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
106 if (p->key == wbk->key)
111 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
112 spin_unlock(&vnode->wb_lock);
117 refcount_inc(&p->usage);
118 spin_unlock(&vnode->wb_lock);
125 * open an AFS file or directory and attach a key to it
127 int afs_open(struct inode *inode, struct file *file)
129 struct afs_vnode *vnode = AFS_FS_I(inode);
134 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
136 key = afs_request_key(vnode->volume->cell);
142 af = kzalloc(sizeof(*af), GFP_KERNEL);
149 ret = afs_validate(vnode, key);
153 if (file->f_mode & FMODE_WRITE) {
154 ret = afs_cache_wb_key(vnode, af);
159 if (file->f_flags & O_TRUNC)
160 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
162 file->private_data = af;
171 _leave(" = %d", ret);
176 * release an AFS file or directory and discard its key
178 int afs_release(struct inode *inode, struct file *file)
180 struct afs_vnode *vnode = AFS_FS_I(inode);
181 struct afs_file *af = file->private_data;
184 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
186 if ((file->f_mode & FMODE_WRITE))
187 ret = vfs_fsync(file, 0);
189 file->private_data = NULL;
191 afs_put_wb_key(af->wb);
194 afs_prune_wb_keys(vnode);
195 _leave(" = %d", ret);
200 * Allocate a new read record.
202 struct afs_read *afs_alloc_read(gfp_t gfp)
204 struct afs_read *req;
206 req = kzalloc(sizeof(struct afs_read), gfp);
208 refcount_set(&req->usage, 1);
214 * Dispose of a ref to a read record.
216 void afs_put_read(struct afs_read *req)
218 if (refcount_dec_and_test(&req->usage)) {
226 static void afs_fetch_data_notify(struct afs_operation *op)
228 struct afs_read *req = op->fetch.req;
229 struct netfs_read_subrequest *subreq = req->subreq;
230 int error = op->error;
232 if (error == -ECONNABORTED)
233 error = afs_abort_to_error(op->ac.abort_code);
237 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
238 netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
240 } else if (req->done) {
245 static void afs_fetch_data_success(struct afs_operation *op)
247 struct afs_vnode *vnode = op->file[0].vnode;
249 _enter("op=%08x", op->debug_id);
250 afs_vnode_commit_status(op, &op->file[0]);
251 afs_stat_v(vnode, n_fetches);
252 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
253 afs_fetch_data_notify(op);
256 static void afs_fetch_data_put(struct afs_operation *op)
258 op->fetch.req->error = op->error;
259 afs_put_read(op->fetch.req);
262 static const struct afs_operation_ops afs_fetch_data_operation = {
263 .issue_afs_rpc = afs_fs_fetch_data,
264 .issue_yfs_rpc = yfs_fs_fetch_data,
265 .success = afs_fetch_data_success,
266 .aborted = afs_check_for_remote_deletion,
267 .failed = afs_fetch_data_notify,
268 .put = afs_fetch_data_put,
272 * Fetch file data from the volume.
274 int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
276 struct afs_operation *op;
278 _enter("%s{%llx:%llu.%u},%x,,,",
283 key_serial(req->key));
285 op = afs_alloc_operation(req->key, vnode->volume);
288 netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
292 afs_op_set_vnode(op, 0, vnode);
294 op->fetch.req = afs_get_read(req);
295 op->ops = &afs_fetch_data_operation;
296 return afs_do_sync_operation(op);
299 static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
301 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
302 struct afs_read *fsreq;
304 fsreq = afs_alloc_read(GFP_NOFS);
306 return netfs_subreq_terminated(subreq, -ENOMEM, false);
308 fsreq->subreq = subreq;
309 fsreq->pos = subreq->start + subreq->transferred;
310 fsreq->len = subreq->len - subreq->transferred;
311 fsreq->key = key_get(subreq->rreq->netfs_priv);
312 fsreq->vnode = vnode;
313 fsreq->iter = &fsreq->def_iter;
315 iov_iter_xarray(&fsreq->def_iter, READ,
316 &fsreq->vnode->vfs_inode.i_mapping->i_pages,
317 fsreq->pos, fsreq->len);
319 afs_fetch_data(fsreq->vnode, fsreq);
323 static int afs_symlink_readpage(struct file *file, struct page *page)
325 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
326 struct afs_read *fsreq;
327 struct folio *folio = page_folio(page);
330 fsreq = afs_alloc_read(GFP_NOFS);
334 fsreq->pos = folio_pos(folio);
335 fsreq->len = folio_size(folio);
336 fsreq->vnode = vnode;
337 fsreq->iter = &fsreq->def_iter;
338 iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
339 fsreq->pos, fsreq->len);
341 ret = afs_fetch_data(fsreq->vnode, fsreq);
343 SetPageUptodate(page);
348 static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
350 rreq->netfs_priv = key_get(afs_file_key(file));
353 static bool afs_is_cache_enabled(struct inode *inode)
355 struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
357 return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects);
360 static int afs_begin_cache_operation(struct netfs_read_request *rreq)
362 struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
364 return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode));
367 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
368 struct folio *folio, void **_fsdata)
370 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
372 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
375 static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
380 const struct netfs_read_request_ops afs_req_ops = {
381 .init_rreq = afs_init_rreq,
382 .is_cache_enabled = afs_is_cache_enabled,
383 .begin_cache_operation = afs_begin_cache_operation,
384 .check_write_begin = afs_check_write_begin,
385 .issue_op = afs_req_issue_op,
386 .cleanup = afs_priv_cleanup,
389 static int afs_readpage(struct file *file, struct page *page)
391 struct folio *folio = page_folio(page);
393 return netfs_readpage(file, folio, &afs_req_ops, NULL);
396 static void afs_readahead(struct readahead_control *ractl)
398 netfs_readahead(ractl, &afs_req_ops, NULL);
402 * Adjust the dirty region of the page on truncation or full invalidation,
403 * getting rid of the markers altogether if the region is entirely invalidated.
405 static void afs_invalidate_dirty(struct folio *folio, unsigned int offset,
408 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
410 unsigned int f, t, end = offset + length;
412 priv = (unsigned long)folio_get_private(folio);
414 /* we clean up only if the entire page is being invalidated */
415 if (offset == 0 && length == folio_size(folio))
416 goto full_invalidate;
418 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
419 * and we don't get another notification to tell us to expand it
422 if (afs_is_folio_dirty_mmapped(priv))
425 /* We may need to shorten the dirty region */
426 f = afs_folio_dirty_from(folio, priv);
427 t = afs_folio_dirty_to(folio, priv);
429 if (t <= offset || f >= end)
430 return; /* Doesn't overlap */
432 if (f < offset && t > end)
433 return; /* Splits the dirty region - just absorb it */
435 if (f >= offset && t <= end)
445 priv = afs_folio_dirty(folio, f, t);
446 folio_change_private(folio, (void *)priv);
447 trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
451 trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
452 folio_clear_dirty_for_io(folio);
454 trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
455 folio_detach_private(folio);
459 * invalidate part or all of a page
460 * - release a page and clean up its private data if offset is 0 (indicating
463 static void afs_invalidatepage(struct page *page, unsigned int offset,
466 struct folio *folio = page_folio(page);
468 _enter("{%lu},%u,%u", folio_index(folio), offset, length);
470 BUG_ON(!PageLocked(page));
472 if (PagePrivate(page))
473 afs_invalidate_dirty(folio, offset, length);
475 folio_wait_fscache(folio);
480 * release a page and clean up its private state if it's not busy
481 * - return true if the page can now be released, false if not
483 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
485 struct folio *folio = page_folio(page);
486 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
488 _enter("{{%llx:%llu}[%lu],%lx},%x",
489 vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
492 /* deny if page is being written to the cache and the caller hasn't
494 #ifdef CONFIG_AFS_FSCACHE
495 if (folio_test_fscache(folio)) {
496 if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS))
498 folio_wait_fscache(folio);
502 if (folio_test_private(folio)) {
503 trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
504 folio_detach_private(folio);
507 /* Indicate that the folio can be released */
512 static void afs_add_open_mmap(struct afs_vnode *vnode)
514 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
515 down_write(&vnode->volume->cell->fs_open_mmaps_lock);
517 list_add_tail(&vnode->cb_mmap_link,
518 &vnode->volume->cell->fs_open_mmaps);
520 up_write(&vnode->volume->cell->fs_open_mmaps_lock);
524 static void afs_drop_open_mmap(struct afs_vnode *vnode)
526 if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
529 down_write(&vnode->volume->cell->fs_open_mmaps_lock);
531 if (atomic_read(&vnode->cb_nr_mmap) == 0)
532 list_del_init(&vnode->cb_mmap_link);
534 up_write(&vnode->volume->cell->fs_open_mmaps_lock);
535 flush_work(&vnode->cb_work);
539 * Handle setting up a memory mapping on an AFS file.
541 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
543 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
546 afs_add_open_mmap(vnode);
548 ret = generic_file_mmap(file, vma);
550 vma->vm_ops = &afs_vm_ops;
552 afs_drop_open_mmap(vnode);
556 static void afs_vm_open(struct vm_area_struct *vma)
558 afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
561 static void afs_vm_close(struct vm_area_struct *vma)
563 afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
566 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
568 struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
569 struct afs_file *af = vmf->vma->vm_file->private_data;
571 switch (afs_validate(vnode, af->key)) {
573 return filemap_map_pages(vmf, start_pgoff, end_pgoff);
578 return VM_FAULT_RETRY;
581 return VM_FAULT_SIGBUS;
585 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
587 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
588 struct afs_file *af = iocb->ki_filp->private_data;
591 ret = afs_validate(vnode, af->key);
595 return generic_file_read_iter(iocb, iter);