1 /* AFS filesystem file handling
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
16 #include <linux/pagemap.h>
17 #include <linux/writeback.h>
18 #include <linux/gfp.h>
19 #include <linux/task_io_accounting_ops.h>
22 static int afs_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);
26 static int afs_launder_page(struct page *page);
28 static int afs_readpages(struct file *filp, struct address_space *mapping,
29 struct list_head *pages, unsigned nr_pages);
31 const struct file_operations afs_file_operations = {
33 .release = afs_release,
34 .llseek = generic_file_llseek,
35 .read_iter = generic_file_read_iter,
36 .write_iter = afs_file_write,
37 .mmap = generic_file_readonly_mmap,
38 .splice_read = generic_file_splice_read,
44 const struct inode_operations afs_file_inode_operations = {
45 .getattr = afs_getattr,
46 .setattr = afs_setattr,
47 .permission = afs_permission,
50 const struct address_space_operations afs_fs_aops = {
51 .readpage = afs_readpage,
52 .readpages = afs_readpages,
53 .set_page_dirty = afs_set_page_dirty,
54 .launder_page = afs_launder_page,
55 .releasepage = afs_releasepage,
56 .invalidatepage = afs_invalidatepage,
57 .write_begin = afs_write_begin,
58 .write_end = afs_write_end,
59 .writepage = afs_writepage,
60 .writepages = afs_writepages,
64 * open an AFS file or directory and attach a key to it
66 int afs_open(struct inode *inode, struct file *file)
68 struct afs_vnode *vnode = AFS_FS_I(inode);
72 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
74 key = afs_request_key(vnode->volume->cell);
76 _leave(" = %ld [key]", PTR_ERR(key));
80 ret = afs_validate(vnode, key);
82 _leave(" = %d [val]", ret);
86 file->private_data = key;
92 * release an AFS file or directory and discard its key
94 int afs_release(struct inode *inode, struct file *file)
96 struct afs_vnode *vnode = AFS_FS_I(inode);
98 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
100 key_put(file->private_data);
106 * Dispose of a ref to a read record.
108 void afs_put_read(struct afs_read *req)
112 if (atomic_dec_and_test(&req->usage)) {
113 for (i = 0; i < req->nr_pages; i++)
115 put_page(req->pages[i]);
120 #ifdef CONFIG_AFS_FSCACHE
122 * deal with notification that a page was read from the cache
124 static void afs_file_readpage_read_complete(struct page *page,
128 _enter("%p,%p,%d", page, data, error);
130 /* if the read completes with an error, we just unlock the page and let
131 * the VM reissue the readpage */
133 SetPageUptodate(page);
139 * read page from file, directory or symlink, given a key to use
141 int afs_page_filler(void *data, struct page *page)
143 struct inode *inode = page->mapping->host;
144 struct afs_vnode *vnode = AFS_FS_I(inode);
145 struct afs_read *req;
146 struct key *key = data;
149 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
151 BUG_ON(!PageLocked(page));
154 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
158 #ifdef CONFIG_AFS_FSCACHE
159 ret = fscache_read_or_alloc_page(vnode->cache,
161 afs_file_readpage_read_complete,
168 /* read BIO submitted (page in cache) */
172 /* page not yet cached */
174 _debug("cache said ENODATA");
177 /* page will not be cached */
179 _debug("cache said ENOBUFS");
182 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
187 atomic_set(&req->usage, 1);
188 req->pos = (loff_t)page->index << PAGE_SHIFT;
189 req->len = min_t(size_t, i_size_read(inode) - req->pos,
192 req->pages[0] = page;
195 /* read the contents of the file from the server into the
197 ret = afs_vnode_fetch_data(vnode, key, req);
200 if (ret == -ENOENT) {
201 _debug("got NOENT from server"
202 " - marking file deleted and stale");
203 set_bit(AFS_VNODE_DELETED, &vnode->flags);
207 #ifdef CONFIG_AFS_FSCACHE
208 fscache_uncache_page(vnode->cache, page);
210 BUG_ON(PageFsCache(page));
214 SetPageUptodate(page);
216 /* send the page to the cache */
217 #ifdef CONFIG_AFS_FSCACHE
218 if (PageFsCache(page) &&
219 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
220 fscache_uncache_page(vnode->cache, page);
221 BUG_ON(PageFsCache(page));
235 _leave(" = %d", ret);
240 * read page from file, directory or symlink, given a file to nominate the key
243 static int afs_readpage(struct file *file, struct page *page)
249 key = file->private_data;
251 ret = afs_page_filler(key, page);
253 struct inode *inode = page->mapping->host;
254 key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
258 ret = afs_page_filler(key, page);
266 * Make pages available as they're filled.
268 static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
270 #ifdef CONFIG_AFS_FSCACHE
271 struct afs_vnode *vnode = call->reply;
273 struct page *page = req->pages[req->index];
275 req->pages[req->index] = NULL;
276 SetPageUptodate(page);
278 /* send the page to the cache */
279 #ifdef CONFIG_AFS_FSCACHE
280 if (PageFsCache(page) &&
281 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
282 fscache_uncache_page(vnode->cache, page);
283 BUG_ON(PageFsCache(page));
291 * Read a contiguous set of pages.
293 static int afs_readpages_one(struct file *file, struct address_space *mapping,
294 struct list_head *pages)
296 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
297 struct afs_read *req;
299 struct page *first, *page;
300 struct key *key = file->private_data;
304 /* Count the number of contiguous pages at the front of the list. Note
305 * that the list goes prev-wards rather than next-wards.
307 first = list_entry(pages->prev, struct page, lru);
308 index = first->index + 1;
310 for (p = first->lru.prev; p != pages; p = p->prev) {
311 page = list_entry(p, struct page, lru);
312 if (page->index != index)
318 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
323 atomic_set(&req->usage, 1);
324 req->page_done = afs_readpages_page_done;
325 req->pos = first->index;
326 req->pos <<= PAGE_SHIFT;
328 /* Transfer the pages to the request. We add them in until one fails
329 * to add to the LRU and then we stop (as that'll make a hole in the
332 * Note that it's possible for the file size to change whilst we're
333 * doing this, but we rely on the server returning less than we asked
334 * for if the file shrank. We also rely on this to deal with a partial
335 * page at the end of the file.
338 page = list_entry(pages->prev, struct page, lru);
339 list_del(&page->lru);
341 if (add_to_page_cache_lru(page, mapping, index,
342 readahead_gfp_mask(mapping))) {
343 #ifdef CONFIG_AFS_FSCACHE
344 fscache_uncache_page(vnode->cache, page);
350 req->pages[req->nr_pages++] = page;
351 req->len += PAGE_SIZE;
352 } while (req->nr_pages < n);
354 if (req->nr_pages == 0) {
359 ret = afs_vnode_fetch_data(vnode, key, req);
363 task_io_account_read(PAGE_SIZE * req->nr_pages);
368 if (ret == -ENOENT) {
369 _debug("got NOENT from server"
370 " - marking file deleted and stale");
371 set_bit(AFS_VNODE_DELETED, &vnode->flags);
375 for (i = 0; i < req->nr_pages; i++) {
376 page = req->pages[i];
378 #ifdef CONFIG_AFS_FSCACHE
379 fscache_uncache_page(vnode->cache, page);
391 * read a set of pages
393 static int afs_readpages(struct file *file, struct address_space *mapping,
394 struct list_head *pages, unsigned nr_pages)
396 struct key *key = file->private_data;
397 struct afs_vnode *vnode;
400 _enter("{%d},{%lu},,%d",
401 key_serial(key), mapping->host->i_ino, nr_pages);
405 vnode = AFS_FS_I(mapping->host);
406 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
407 _leave(" = -ESTALE");
411 /* attempt to read as many of the pages as possible */
412 #ifdef CONFIG_AFS_FSCACHE
413 ret = fscache_read_or_alloc_pages(vnode->cache,
417 afs_file_readpage_read_complete,
419 mapping_gfp_mask(mapping));
425 /* all pages are being read from the cache */
427 BUG_ON(!list_empty(pages));
428 BUG_ON(nr_pages != 0);
429 _leave(" = 0 [reading all]");
432 /* there were pages that couldn't be read from the cache */
439 _leave(" = %d", ret);
443 while (!list_empty(pages)) {
444 ret = afs_readpages_one(file, mapping, pages);
449 _leave(" = %d [netting]", ret);
454 * write back a dirty page
456 static int afs_launder_page(struct page *page)
458 _enter("{%lu}", page->index);
464 * invalidate part or all of a page
465 * - release a page and clean up its private data if offset is 0 (indicating
468 static void afs_invalidatepage(struct page *page, unsigned int offset,
471 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
473 _enter("{%lu},%u,%u", page->index, offset, length);
475 BUG_ON(!PageLocked(page));
477 /* we clean up only if the entire page is being invalidated */
478 if (offset == 0 && length == PAGE_SIZE) {
479 #ifdef CONFIG_AFS_FSCACHE
480 if (PageFsCache(page)) {
481 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
482 fscache_wait_on_page_write(vnode->cache, page);
483 fscache_uncache_page(vnode->cache, page);
487 if (PagePrivate(page)) {
488 if (wb && !PageWriteback(page)) {
489 set_page_private(page, 0);
490 afs_put_writeback(wb);
493 if (!page_private(page))
494 ClearPagePrivate(page);
502 * release a page and clean up its private state if it's not busy
503 * - return true if the page can now be released, false if not
505 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
507 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
508 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
510 _enter("{{%x:%u}[%lu],%lx},%x",
511 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
514 /* deny if page is being written to the cache and the caller hasn't
516 #ifdef CONFIG_AFS_FSCACHE
517 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
518 _leave(" = F [cache busy]");
523 if (PagePrivate(page)) {
525 set_page_private(page, 0);
526 afs_put_writeback(wb);
528 ClearPagePrivate(page);
531 /* indicate that the page can be released */