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,
41 const struct inode_operations afs_file_inode_operations = {
42 .getattr = afs_getattr,
43 .setattr = afs_setattr,
44 .permission = afs_permission,
45 .listxattr = afs_listxattr,
48 const struct address_space_operations afs_fs_aops = {
49 .readpage = afs_readpage,
50 .readpages = afs_readpages,
51 .set_page_dirty = afs_set_page_dirty,
52 .launder_page = afs_launder_page,
53 .releasepage = afs_releasepage,
54 .invalidatepage = afs_invalidatepage,
55 .write_begin = afs_write_begin,
56 .write_end = afs_write_end,
57 .writepage = afs_writepage,
58 .writepages = afs_writepages,
61 static const struct vm_operations_struct afs_vm_ops = {
62 .fault = filemap_fault,
63 .map_pages = filemap_map_pages,
64 .page_mkwrite = afs_page_mkwrite,
68 * Discard a pin on a writeback key.
70 void afs_put_wb_key(struct afs_wb_key *wbk)
72 if (refcount_dec_and_test(&wbk->usage)) {
79 * Cache key for writeback.
81 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
83 struct afs_wb_key *wbk, *p;
85 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
88 refcount_set(&wbk->usage, 2);
91 spin_lock(&vnode->wb_lock);
92 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
93 if (p->key == wbk->key)
98 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
99 spin_unlock(&vnode->wb_lock);
104 refcount_inc(&p->usage);
105 spin_unlock(&vnode->wb_lock);
112 * open an AFS file or directory and attach a key to it
114 int afs_open(struct inode *inode, struct file *file)
116 struct afs_vnode *vnode = AFS_FS_I(inode);
121 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
123 key = afs_request_key(vnode->volume->cell);
129 af = kzalloc(sizeof(*af), GFP_KERNEL);
136 ret = afs_validate(vnode, key);
140 if (file->f_mode & FMODE_WRITE) {
141 ret = afs_cache_wb_key(vnode, af);
146 if (file->f_flags & O_TRUNC)
147 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
149 file->private_data = af;
158 _leave(" = %d", ret);
163 * release an AFS file or directory and discard its key
165 int afs_release(struct inode *inode, struct file *file)
167 struct afs_vnode *vnode = AFS_FS_I(inode);
168 struct afs_file *af = file->private_data;
171 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
173 if ((file->f_mode & FMODE_WRITE))
174 ret = vfs_fsync(file, 0);
176 file->private_data = NULL;
178 afs_put_wb_key(af->wb);
181 afs_prune_wb_keys(vnode);
182 _leave(" = %d", ret);
187 * Dispose of a ref to a read record.
189 void afs_put_read(struct afs_read *req)
193 if (refcount_dec_and_test(&req->usage)) {
195 for (i = 0; i < req->nr_pages; i++)
197 put_page(req->pages[i]);
198 if (req->pages != req->array)
205 #ifdef CONFIG_AFS_FSCACHE
207 * deal with notification that a page was read from the cache
209 static void afs_file_readpage_read_complete(struct page *page,
213 _enter("%p,%p,%d", page, data, error);
215 /* if the read completes with an error, we just unlock the page and let
216 * the VM reissue the readpage */
218 SetPageUptodate(page);
224 * Fetch file data from the volume.
226 int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
228 struct afs_fs_cursor fc;
229 struct afs_status_cb *scb;
232 _enter("%s{%llx:%llu.%u},%x,,,",
239 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
244 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
245 afs_dataversion_t data_version = vnode->status.data_version;
247 while (afs_select_fileserver(&fc)) {
248 fc.cb_break = afs_calc_vnode_cb_break(vnode);
249 afs_fs_fetch_data(&fc, scb, req);
252 afs_check_for_remote_deletion(&fc, vnode);
253 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
255 ret = afs_end_vnode_operation(&fc);
259 afs_stat_v(vnode, n_fetches);
260 atomic_long_add(req->actual_len,
261 &afs_v2net(vnode)->n_fetch_bytes);
265 _leave(" = %d", ret);
270 * read page from file, directory or symlink, given a key to use
272 int afs_page_filler(void *data, struct page *page)
274 struct inode *inode = page->mapping->host;
275 struct afs_vnode *vnode = AFS_FS_I(inode);
276 struct afs_read *req;
277 struct key *key = data;
280 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
282 BUG_ON(!PageLocked(page));
285 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
289 #ifdef CONFIG_AFS_FSCACHE
290 ret = fscache_read_or_alloc_page(vnode->cache,
292 afs_file_readpage_read_complete,
299 /* read BIO submitted (page in cache) */
303 /* page not yet cached */
305 _debug("cache said ENODATA");
308 /* page will not be cached */
310 _debug("cache said ENOBUFS");
315 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
319 /* We request a full page. If the page is a partial one at the
320 * end of the file, the server will return a short read and the
321 * unmarshalling code will clear the unfilled space.
323 refcount_set(&req->usage, 1);
324 req->pos = (loff_t)page->index << PAGE_SHIFT;
325 req->len = PAGE_SIZE;
327 req->pages = req->array;
328 req->pages[0] = page;
331 /* read the contents of the file from the server into the
333 ret = afs_fetch_data(vnode, key, req);
337 if (ret == -ENOENT) {
338 _debug("got NOENT from server"
339 " - marking file deleted and stale");
340 set_bit(AFS_VNODE_DELETED, &vnode->flags);
344 #ifdef CONFIG_AFS_FSCACHE
345 fscache_uncache_page(vnode->cache, page);
347 BUG_ON(PageFsCache(page));
351 ret == -ERESTARTSYS ||
357 SetPageUptodate(page);
359 /* send the page to the cache */
360 #ifdef CONFIG_AFS_FSCACHE
361 if (PageFsCache(page) &&
362 fscache_write_page(vnode->cache, page, vnode->status.size,
364 fscache_uncache_page(vnode->cache, page);
365 BUG_ON(PageFsCache(page));
381 _leave(" = %d", ret);
386 * read page from file, directory or symlink, given a file to nominate the key
389 static int afs_readpage(struct file *file, struct page *page)
395 key = afs_file_key(file);
397 ret = afs_page_filler(key, page);
399 struct inode *inode = page->mapping->host;
400 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
404 ret = afs_page_filler(key, page);
412 * Make pages available as they're filled.
414 static void afs_readpages_page_done(struct afs_read *req)
416 #ifdef CONFIG_AFS_FSCACHE
417 struct afs_vnode *vnode = req->vnode;
419 struct page *page = req->pages[req->index];
421 req->pages[req->index] = NULL;
422 SetPageUptodate(page);
424 /* send the page to the cache */
425 #ifdef CONFIG_AFS_FSCACHE
426 if (PageFsCache(page) &&
427 fscache_write_page(vnode->cache, page, vnode->status.size,
429 fscache_uncache_page(vnode->cache, page);
430 BUG_ON(PageFsCache(page));
438 * Read a contiguous set of pages.
440 static int afs_readpages_one(struct file *file, struct address_space *mapping,
441 struct list_head *pages)
443 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
444 struct afs_read *req;
446 struct page *first, *page;
447 struct key *key = afs_file_key(file);
451 /* Count the number of contiguous pages at the front of the list. Note
452 * that the list goes prev-wards rather than next-wards.
454 first = lru_to_page(pages);
455 index = first->index + 1;
457 for (p = first->lru.prev; p != pages; p = p->prev) {
458 page = list_entry(p, struct page, lru);
459 if (page->index != index)
465 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
469 refcount_set(&req->usage, 1);
471 req->page_done = afs_readpages_page_done;
472 req->pos = first->index;
473 req->pos <<= PAGE_SHIFT;
474 req->pages = req->array;
476 /* Transfer the pages to the request. We add them in until one fails
477 * to add to the LRU and then we stop (as that'll make a hole in the
480 * Note that it's possible for the file size to change whilst we're
481 * doing this, but we rely on the server returning less than we asked
482 * for if the file shrank. We also rely on this to deal with a partial
483 * page at the end of the file.
486 page = lru_to_page(pages);
487 list_del(&page->lru);
489 if (add_to_page_cache_lru(page, mapping, index,
490 readahead_gfp_mask(mapping))) {
491 #ifdef CONFIG_AFS_FSCACHE
492 fscache_uncache_page(vnode->cache, page);
498 req->pages[req->nr_pages++] = page;
499 req->len += PAGE_SIZE;
500 } while (req->nr_pages < n);
502 if (req->nr_pages == 0) {
507 ret = afs_fetch_data(vnode, key, req);
511 task_io_account_read(PAGE_SIZE * req->nr_pages);
516 if (ret == -ENOENT) {
517 _debug("got NOENT from server"
518 " - marking file deleted and stale");
519 set_bit(AFS_VNODE_DELETED, &vnode->flags);
523 for (i = 0; i < req->nr_pages; i++) {
524 page = req->pages[i];
526 #ifdef CONFIG_AFS_FSCACHE
527 fscache_uncache_page(vnode->cache, page);
539 * read a set of pages
541 static int afs_readpages(struct file *file, struct address_space *mapping,
542 struct list_head *pages, unsigned nr_pages)
544 struct key *key = afs_file_key(file);
545 struct afs_vnode *vnode;
548 _enter("{%d},{%lu},,%d",
549 key_serial(key), mapping->host->i_ino, nr_pages);
553 vnode = AFS_FS_I(mapping->host);
554 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
555 _leave(" = -ESTALE");
559 /* attempt to read as many of the pages as possible */
560 #ifdef CONFIG_AFS_FSCACHE
561 ret = fscache_read_or_alloc_pages(vnode->cache,
565 afs_file_readpage_read_complete,
567 mapping_gfp_mask(mapping));
573 /* all pages are being read from the cache */
575 BUG_ON(!list_empty(pages));
576 BUG_ON(nr_pages != 0);
577 _leave(" = 0 [reading all]");
580 /* there were pages that couldn't be read from the cache */
587 _leave(" = %d", ret);
591 while (!list_empty(pages)) {
592 ret = afs_readpages_one(file, mapping, pages);
597 _leave(" = %d [netting]", ret);
602 * invalidate part or all of a page
603 * - release a page and clean up its private data if offset is 0 (indicating
606 static void afs_invalidatepage(struct page *page, unsigned int offset,
609 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
612 _enter("{%lu},%u,%u", page->index, offset, length);
614 BUG_ON(!PageLocked(page));
616 /* we clean up only if the entire page is being invalidated */
617 if (offset == 0 && length == PAGE_SIZE) {
618 #ifdef CONFIG_AFS_FSCACHE
619 if (PageFsCache(page)) {
620 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
621 fscache_wait_on_page_write(vnode->cache, page);
622 fscache_uncache_page(vnode->cache, page);
626 if (PagePrivate(page)) {
627 priv = page_private(page);
628 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
630 set_page_private(page, 0);
631 ClearPagePrivate(page);
639 * release a page and clean up its private state if it's not busy
640 * - return true if the page can now be released, false if not
642 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
644 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
647 _enter("{{%llx:%llu}[%lu],%lx},%x",
648 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
651 /* deny if page is being written to the cache and the caller hasn't
653 #ifdef CONFIG_AFS_FSCACHE
654 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
655 _leave(" = F [cache busy]");
660 if (PagePrivate(page)) {
661 priv = page_private(page);
662 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
664 set_page_private(page, 0);
665 ClearPagePrivate(page);
668 /* indicate that the page can be released */
674 * Handle setting up a memory mapping on an AFS file.
676 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
680 ret = generic_file_mmap(file, vma);
682 vma->vm_ops = &afs_vm_ops;