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>
19 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
20 static int afs_readpage(struct file *file, struct page *page);
21 static void afs_invalidatepage(struct page *page, unsigned int offset,
23 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
25 static int afs_readpages(struct file *filp, struct address_space *mapping,
26 struct list_head *pages, unsigned nr_pages);
28 const struct file_operations afs_file_operations = {
30 .release = afs_release,
31 .llseek = generic_file_llseek,
32 .read_iter = generic_file_read_iter,
33 .write_iter = afs_file_write,
34 .mmap = afs_file_mmap,
35 .splice_read = generic_file_splice_read,
36 .splice_write = iter_file_splice_write,
42 const struct inode_operations afs_file_inode_operations = {
43 .getattr = afs_getattr,
44 .setattr = afs_setattr,
45 .permission = afs_permission,
46 .listxattr = afs_listxattr,
49 const struct address_space_operations afs_fs_aops = {
50 .readpage = afs_readpage,
51 .readpages = afs_readpages,
52 .set_page_dirty = afs_set_page_dirty,
53 .launder_page = afs_launder_page,
54 .releasepage = afs_releasepage,
55 .invalidatepage = afs_invalidatepage,
56 .write_begin = afs_write_begin,
57 .write_end = afs_write_end,
58 .writepage = afs_writepage,
59 .writepages = afs_writepages,
62 static const struct vm_operations_struct afs_vm_ops = {
63 .fault = filemap_fault,
64 .map_pages = filemap_map_pages,
65 .page_mkwrite = afs_page_mkwrite,
69 * Discard a pin on a writeback key.
71 void afs_put_wb_key(struct afs_wb_key *wbk)
73 if (wbk && refcount_dec_and_test(&wbk->usage)) {
80 * Cache key for writeback.
82 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
84 struct afs_wb_key *wbk, *p;
86 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
89 refcount_set(&wbk->usage, 2);
92 spin_lock(&vnode->wb_lock);
93 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
94 if (p->key == wbk->key)
99 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
100 spin_unlock(&vnode->wb_lock);
105 refcount_inc(&p->usage);
106 spin_unlock(&vnode->wb_lock);
113 * open an AFS file or directory and attach a key to it
115 int afs_open(struct inode *inode, struct file *file)
117 struct afs_vnode *vnode = AFS_FS_I(inode);
122 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
124 key = afs_request_key(vnode->volume->cell);
130 af = kzalloc(sizeof(*af), GFP_KERNEL);
137 ret = afs_validate(vnode, key);
141 if (file->f_mode & FMODE_WRITE) {
142 ret = afs_cache_wb_key(vnode, af);
147 if (file->f_flags & O_TRUNC)
148 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
150 file->private_data = af;
159 _leave(" = %d", ret);
164 * release an AFS file or directory and discard its key
166 int afs_release(struct inode *inode, struct file *file)
168 struct afs_vnode *vnode = AFS_FS_I(inode);
169 struct afs_file *af = file->private_data;
172 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
174 if ((file->f_mode & FMODE_WRITE))
175 ret = vfs_fsync(file, 0);
177 file->private_data = NULL;
179 afs_put_wb_key(af->wb);
182 afs_prune_wb_keys(vnode);
183 _leave(" = %d", ret);
188 * Dispose of a ref to a read record.
190 void afs_put_read(struct afs_read *req)
194 if (refcount_dec_and_test(&req->usage)) {
196 for (i = 0; i < req->nr_pages; i++)
198 put_page(req->pages[i]);
199 if (req->pages != req->array)
206 #ifdef CONFIG_AFS_FSCACHE
208 * deal with notification that a page was read from the cache
210 static void afs_file_readpage_read_complete(struct page *page,
214 _enter("%p,%p,%d", page, data, error);
216 /* if the read completes with an error, we just unlock the page and let
217 * the VM reissue the readpage */
219 SetPageUptodate(page);
224 static void afs_fetch_data_success(struct afs_operation *op)
226 struct afs_vnode *vnode = op->file[0].vnode;
228 _enter("op=%08x", op->debug_id);
229 afs_vnode_commit_status(op, &op->file[0]);
230 afs_stat_v(vnode, n_fetches);
231 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
234 static void afs_fetch_data_put(struct afs_operation *op)
236 afs_put_read(op->fetch.req);
239 static const struct afs_operation_ops afs_fetch_data_operation = {
240 .issue_afs_rpc = afs_fs_fetch_data,
241 .issue_yfs_rpc = yfs_fs_fetch_data,
242 .success = afs_fetch_data_success,
243 .aborted = afs_check_for_remote_deletion,
244 .put = afs_fetch_data_put,
248 * Fetch file data from the volume.
250 int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
252 struct afs_operation *op;
254 _enter("%s{%llx:%llu.%u},%x,,,",
261 op = afs_alloc_operation(key, vnode->volume);
265 afs_op_set_vnode(op, 0, vnode);
267 op->fetch.req = afs_get_read(req);
268 op->ops = &afs_fetch_data_operation;
269 return afs_do_sync_operation(op);
273 * read page from file, directory or symlink, given a key to use
275 int afs_page_filler(void *data, struct page *page)
277 struct inode *inode = page->mapping->host;
278 struct afs_vnode *vnode = AFS_FS_I(inode);
279 struct afs_read *req;
280 struct key *key = data;
283 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
285 BUG_ON(!PageLocked(page));
288 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
292 #ifdef CONFIG_AFS_FSCACHE
293 ret = fscache_read_or_alloc_page(vnode->cache,
295 afs_file_readpage_read_complete,
302 /* read BIO submitted (page in cache) */
306 /* page not yet cached */
308 _debug("cache said ENODATA");
311 /* page will not be cached */
313 _debug("cache said ENOBUFS");
318 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
322 /* We request a full page. If the page is a partial one at the
323 * end of the file, the server will return a short read and the
324 * unmarshalling code will clear the unfilled space.
326 refcount_set(&req->usage, 1);
327 req->pos = (loff_t)page->index << PAGE_SHIFT;
328 req->len = PAGE_SIZE;
330 req->pages = req->array;
331 req->pages[0] = page;
334 /* read the contents of the file from the server into the
336 ret = afs_fetch_data(vnode, key, req);
340 if (ret == -ENOENT) {
341 _debug("got NOENT from server"
342 " - marking file deleted and stale");
343 set_bit(AFS_VNODE_DELETED, &vnode->flags);
347 #ifdef CONFIG_AFS_FSCACHE
348 fscache_uncache_page(vnode->cache, page);
350 BUG_ON(PageFsCache(page));
354 ret == -ERESTARTSYS ||
360 SetPageUptodate(page);
362 /* send the page to the cache */
363 #ifdef CONFIG_AFS_FSCACHE
364 if (PageFsCache(page) &&
365 fscache_write_page(vnode->cache, page, vnode->status.size,
367 fscache_uncache_page(vnode->cache, page);
368 BUG_ON(PageFsCache(page));
384 _leave(" = %d", ret);
389 * read page from file, directory or symlink, given a file to nominate the key
392 static int afs_readpage(struct file *file, struct page *page)
398 key = afs_file_key(file);
400 ret = afs_page_filler(key, page);
402 struct inode *inode = page->mapping->host;
403 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
407 ret = afs_page_filler(key, page);
415 * Make pages available as they're filled.
417 static void afs_readpages_page_done(struct afs_read *req)
419 #ifdef CONFIG_AFS_FSCACHE
420 struct afs_vnode *vnode = req->vnode;
422 struct page *page = req->pages[req->index];
424 req->pages[req->index] = NULL;
425 SetPageUptodate(page);
427 /* send the page to the cache */
428 #ifdef CONFIG_AFS_FSCACHE
429 if (PageFsCache(page) &&
430 fscache_write_page(vnode->cache, page, vnode->status.size,
432 fscache_uncache_page(vnode->cache, page);
433 BUG_ON(PageFsCache(page));
441 * Read a contiguous set of pages.
443 static int afs_readpages_one(struct file *file, struct address_space *mapping,
444 struct list_head *pages)
446 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
447 struct afs_read *req;
449 struct page *first, *page;
450 struct key *key = afs_file_key(file);
454 /* Count the number of contiguous pages at the front of the list. Note
455 * that the list goes prev-wards rather than next-wards.
457 first = lru_to_page(pages);
458 index = first->index + 1;
460 for (p = first->lru.prev; p != pages; p = p->prev) {
461 page = list_entry(p, struct page, lru);
462 if (page->index != index)
468 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
472 refcount_set(&req->usage, 1);
474 req->page_done = afs_readpages_page_done;
475 req->pos = first->index;
476 req->pos <<= PAGE_SHIFT;
477 req->pages = req->array;
479 /* Transfer the pages to the request. We add them in until one fails
480 * to add to the LRU and then we stop (as that'll make a hole in the
483 * Note that it's possible for the file size to change whilst we're
484 * doing this, but we rely on the server returning less than we asked
485 * for if the file shrank. We also rely on this to deal with a partial
486 * page at the end of the file.
489 page = lru_to_page(pages);
490 list_del(&page->lru);
492 if (add_to_page_cache_lru(page, mapping, index,
493 readahead_gfp_mask(mapping))) {
494 #ifdef CONFIG_AFS_FSCACHE
495 fscache_uncache_page(vnode->cache, page);
501 req->pages[req->nr_pages++] = page;
502 req->len += PAGE_SIZE;
503 } while (req->nr_pages < n);
505 if (req->nr_pages == 0) {
510 ret = afs_fetch_data(vnode, key, req);
514 task_io_account_read(PAGE_SIZE * req->nr_pages);
519 if (ret == -ENOENT) {
520 _debug("got NOENT from server"
521 " - marking file deleted and stale");
522 set_bit(AFS_VNODE_DELETED, &vnode->flags);
526 for (i = 0; i < req->nr_pages; i++) {
527 page = req->pages[i];
529 #ifdef CONFIG_AFS_FSCACHE
530 fscache_uncache_page(vnode->cache, page);
542 * read a set of pages
544 static int afs_readpages(struct file *file, struct address_space *mapping,
545 struct list_head *pages, unsigned nr_pages)
547 struct key *key = afs_file_key(file);
548 struct afs_vnode *vnode;
551 _enter("{%d},{%lu},,%d",
552 key_serial(key), mapping->host->i_ino, nr_pages);
556 vnode = AFS_FS_I(mapping->host);
557 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
558 _leave(" = -ESTALE");
562 /* attempt to read as many of the pages as possible */
563 #ifdef CONFIG_AFS_FSCACHE
564 ret = fscache_read_or_alloc_pages(vnode->cache,
568 afs_file_readpage_read_complete,
570 mapping_gfp_mask(mapping));
576 /* all pages are being read from the cache */
578 BUG_ON(!list_empty(pages));
579 BUG_ON(nr_pages != 0);
580 _leave(" = 0 [reading all]");
583 /* there were pages that couldn't be read from the cache */
590 _leave(" = %d", ret);
594 while (!list_empty(pages)) {
595 ret = afs_readpages_one(file, mapping, pages);
600 _leave(" = %d [netting]", ret);
605 * Adjust the dirty region of the page on truncation or full invalidation,
606 * getting rid of the markers altogether if the region is entirely invalidated.
608 static void afs_invalidate_dirty(struct page *page, unsigned int offset,
611 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
613 unsigned int f, t, end = offset + length;
615 priv = page_private(page);
617 /* we clean up only if the entire page is being invalidated */
618 if (offset == 0 && length == thp_size(page))
619 goto full_invalidate;
621 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
622 * and we don't get another notification to tell us to expand it
625 if (afs_is_page_dirty_mmapped(priv))
628 /* We may need to shorten the dirty region */
629 f = afs_page_dirty_from(priv);
630 t = afs_page_dirty_to(priv);
632 if (t <= offset || f >= end)
633 return; /* Doesn't overlap */
635 if (f < offset && t > end)
636 return; /* Splits the dirty region - just absorb it */
638 if (f >= offset && t <= end)
648 priv = afs_page_dirty(f, t);
649 set_page_private(page, priv);
650 trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page->index, priv);
654 trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page->index, priv);
655 clear_page_dirty_for_io(page);
657 priv = (unsigned long)detach_page_private(page);
658 trace_afs_page_dirty(vnode, tracepoint_string("inval"), page->index, priv);
662 * invalidate part or all of a page
663 * - release a page and clean up its private data if offset is 0 (indicating
666 static void afs_invalidatepage(struct page *page, unsigned int offset,
669 _enter("{%lu},%u,%u", page->index, offset, length);
671 BUG_ON(!PageLocked(page));
673 #ifdef CONFIG_AFS_FSCACHE
674 /* we clean up only if the entire page is being invalidated */
675 if (offset == 0 && length == PAGE_SIZE) {
676 if (PageFsCache(page)) {
677 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
678 fscache_wait_on_page_write(vnode->cache, page);
679 fscache_uncache_page(vnode->cache, page);
684 if (PagePrivate(page))
685 afs_invalidate_dirty(page, offset, length);
691 * release a page and clean up its private state if it's not busy
692 * - return true if the page can now be released, false if not
694 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
696 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
699 _enter("{{%llx:%llu}[%lu],%lx},%x",
700 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
703 /* deny if page is being written to the cache and the caller hasn't
705 #ifdef CONFIG_AFS_FSCACHE
706 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
707 _leave(" = F [cache busy]");
712 if (PagePrivate(page)) {
713 priv = (unsigned long)detach_page_private(page);
714 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
718 /* indicate that the page can be released */
724 * Handle setting up a memory mapping on an AFS file.
726 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
730 ret = generic_file_mmap(file, vma);
732 vma->vm_ops = &afs_vm_ops;