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 (wbk && 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);
223 static void afs_fetch_data_success(struct afs_operation *op)
225 struct afs_vnode *vnode = op->file[0].vnode;
227 _enter("op=%08x", op->debug_id);
228 afs_vnode_commit_status(op, &op->file[0]);
229 afs_stat_v(vnode, n_fetches);
230 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
233 static void afs_fetch_data_put(struct afs_operation *op)
235 afs_put_read(op->fetch.req);
238 static const struct afs_operation_ops afs_fetch_data_operation = {
239 .issue_afs_rpc = afs_fs_fetch_data,
240 .issue_yfs_rpc = yfs_fs_fetch_data,
241 .success = afs_fetch_data_success,
242 .aborted = afs_check_for_remote_deletion,
243 .put = afs_fetch_data_put,
247 * Fetch file data from the volume.
249 int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
251 struct afs_operation *op;
253 _enter("%s{%llx:%llu.%u},%x,,,",
260 op = afs_alloc_operation(key, vnode->volume);
264 afs_op_set_vnode(op, 0, vnode);
266 op->fetch.req = afs_get_read(req);
267 op->ops = &afs_fetch_data_operation;
268 return afs_do_sync_operation(op);
272 * read page from file, directory or symlink, given a key to use
274 int afs_page_filler(void *data, struct page *page)
276 struct inode *inode = page->mapping->host;
277 struct afs_vnode *vnode = AFS_FS_I(inode);
278 struct afs_read *req;
279 struct key *key = data;
282 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
284 BUG_ON(!PageLocked(page));
287 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
291 #ifdef CONFIG_AFS_FSCACHE
292 ret = fscache_read_or_alloc_page(vnode->cache,
294 afs_file_readpage_read_complete,
301 /* read BIO submitted (page in cache) */
305 /* page not yet cached */
307 _debug("cache said ENODATA");
310 /* page will not be cached */
312 _debug("cache said ENOBUFS");
317 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
321 /* We request a full page. If the page is a partial one at the
322 * end of the file, the server will return a short read and the
323 * unmarshalling code will clear the unfilled space.
325 refcount_set(&req->usage, 1);
326 req->pos = (loff_t)page->index << PAGE_SHIFT;
327 req->len = PAGE_SIZE;
329 req->pages = req->array;
330 req->pages[0] = page;
333 /* read the contents of the file from the server into the
335 ret = afs_fetch_data(vnode, key, req);
339 if (ret == -ENOENT) {
340 _debug("got NOENT from server"
341 " - marking file deleted and stale");
342 set_bit(AFS_VNODE_DELETED, &vnode->flags);
346 #ifdef CONFIG_AFS_FSCACHE
347 fscache_uncache_page(vnode->cache, page);
349 BUG_ON(PageFsCache(page));
353 ret == -ERESTARTSYS ||
359 SetPageUptodate(page);
361 /* send the page to the cache */
362 #ifdef CONFIG_AFS_FSCACHE
363 if (PageFsCache(page) &&
364 fscache_write_page(vnode->cache, page, vnode->status.size,
366 fscache_uncache_page(vnode->cache, page);
367 BUG_ON(PageFsCache(page));
383 _leave(" = %d", ret);
388 * read page from file, directory or symlink, given a file to nominate the key
391 static int afs_readpage(struct file *file, struct page *page)
397 key = afs_file_key(file);
399 ret = afs_page_filler(key, page);
401 struct inode *inode = page->mapping->host;
402 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
406 ret = afs_page_filler(key, page);
414 * Make pages available as they're filled.
416 static void afs_readpages_page_done(struct afs_read *req)
418 #ifdef CONFIG_AFS_FSCACHE
419 struct afs_vnode *vnode = req->vnode;
421 struct page *page = req->pages[req->index];
423 req->pages[req->index] = NULL;
424 SetPageUptodate(page);
426 /* send the page to the cache */
427 #ifdef CONFIG_AFS_FSCACHE
428 if (PageFsCache(page) &&
429 fscache_write_page(vnode->cache, page, vnode->status.size,
431 fscache_uncache_page(vnode->cache, page);
432 BUG_ON(PageFsCache(page));
440 * Read a contiguous set of pages.
442 static int afs_readpages_one(struct file *file, struct address_space *mapping,
443 struct list_head *pages)
445 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
446 struct afs_read *req;
448 struct page *first, *page;
449 struct key *key = afs_file_key(file);
453 /* Count the number of contiguous pages at the front of the list. Note
454 * that the list goes prev-wards rather than next-wards.
456 first = lru_to_page(pages);
457 index = first->index + 1;
459 for (p = first->lru.prev; p != pages; p = p->prev) {
460 page = list_entry(p, struct page, lru);
461 if (page->index != index)
467 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
471 refcount_set(&req->usage, 1);
473 req->page_done = afs_readpages_page_done;
474 req->pos = first->index;
475 req->pos <<= PAGE_SHIFT;
476 req->pages = req->array;
478 /* Transfer the pages to the request. We add them in until one fails
479 * to add to the LRU and then we stop (as that'll make a hole in the
482 * Note that it's possible for the file size to change whilst we're
483 * doing this, but we rely on the server returning less than we asked
484 * for if the file shrank. We also rely on this to deal with a partial
485 * page at the end of the file.
488 page = lru_to_page(pages);
489 list_del(&page->lru);
491 if (add_to_page_cache_lru(page, mapping, index,
492 readahead_gfp_mask(mapping))) {
493 #ifdef CONFIG_AFS_FSCACHE
494 fscache_uncache_page(vnode->cache, page);
500 req->pages[req->nr_pages++] = page;
501 req->len += PAGE_SIZE;
502 } while (req->nr_pages < n);
504 if (req->nr_pages == 0) {
509 ret = afs_fetch_data(vnode, key, req);
513 task_io_account_read(PAGE_SIZE * req->nr_pages);
518 if (ret == -ENOENT) {
519 _debug("got NOENT from server"
520 " - marking file deleted and stale");
521 set_bit(AFS_VNODE_DELETED, &vnode->flags);
525 for (i = 0; i < req->nr_pages; i++) {
526 page = req->pages[i];
528 #ifdef CONFIG_AFS_FSCACHE
529 fscache_uncache_page(vnode->cache, page);
541 * read a set of pages
543 static int afs_readpages(struct file *file, struct address_space *mapping,
544 struct list_head *pages, unsigned nr_pages)
546 struct key *key = afs_file_key(file);
547 struct afs_vnode *vnode;
550 _enter("{%d},{%lu},,%d",
551 key_serial(key), mapping->host->i_ino, nr_pages);
555 vnode = AFS_FS_I(mapping->host);
556 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
557 _leave(" = -ESTALE");
561 /* attempt to read as many of the pages as possible */
562 #ifdef CONFIG_AFS_FSCACHE
563 ret = fscache_read_or_alloc_pages(vnode->cache,
567 afs_file_readpage_read_complete,
569 mapping_gfp_mask(mapping));
575 /* all pages are being read from the cache */
577 BUG_ON(!list_empty(pages));
578 BUG_ON(nr_pages != 0);
579 _leave(" = 0 [reading all]");
582 /* there were pages that couldn't be read from the cache */
589 _leave(" = %d", ret);
593 while (!list_empty(pages)) {
594 ret = afs_readpages_one(file, mapping, pages);
599 _leave(" = %d [netting]", ret);
604 * invalidate part or all of a page
605 * - release a page and clean up its private data if offset is 0 (indicating
608 static void afs_invalidatepage(struct page *page, unsigned int offset,
611 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
614 _enter("{%lu},%u,%u", page->index, offset, length);
616 BUG_ON(!PageLocked(page));
618 /* we clean up only if the entire page is being invalidated */
619 if (offset == 0 && length == PAGE_SIZE) {
620 #ifdef CONFIG_AFS_FSCACHE
621 if (PageFsCache(page)) {
622 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
623 fscache_wait_on_page_write(vnode->cache, page);
624 fscache_uncache_page(vnode->cache, page);
628 if (PagePrivate(page)) {
629 priv = page_private(page);
630 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
632 set_page_private(page, 0);
633 ClearPagePrivate(page);
641 * release a page and clean up its private state if it's not busy
642 * - return true if the page can now be released, false if not
644 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
646 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
649 _enter("{{%llx:%llu}[%lu],%lx},%x",
650 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
653 /* deny if page is being written to the cache and the caller hasn't
655 #ifdef CONFIG_AFS_FSCACHE
656 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
657 _leave(" = F [cache busy]");
662 if (PagePrivate(page)) {
663 priv = page_private(page);
664 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
666 set_page_private(page, 0);
667 ClearPagePrivate(page);
670 /* indicate that the page can be released */
676 * Handle setting up a memory mapping on an AFS file.
678 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
682 ret = generic_file_mmap(file, vma);
684 vma->vm_ops = &afs_vm_ops;