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 struct afs_vnode *vnode = call->reply;
271 struct page *page = req->pages[req->index];
273 req->pages[req->index] = NULL;
274 SetPageUptodate(page);
276 /* send the page to the cache */
277 #ifdef CONFIG_AFS_FSCACHE
278 if (PageFsCache(page) &&
279 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
280 fscache_uncache_page(vnode->cache, page);
281 BUG_ON(PageFsCache(page));
289 * Read a contiguous set of pages.
291 static int afs_readpages_one(struct file *file, struct address_space *mapping,
292 struct list_head *pages)
294 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
295 struct afs_read *req;
297 struct page *first, *page;
298 struct key *key = file->private_data;
302 /* Count the number of contiguous pages at the front of the list. Note
303 * that the list goes prev-wards rather than next-wards.
305 first = list_entry(pages->prev, struct page, lru);
306 index = first->index + 1;
308 for (p = first->lru.prev; p != pages; p = p->prev) {
309 page = list_entry(p, struct page, lru);
310 if (page->index != index)
316 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
321 atomic_set(&req->usage, 1);
322 req->page_done = afs_readpages_page_done;
323 req->pos = first->index;
324 req->pos <<= PAGE_SHIFT;
326 /* Transfer the pages to the request. We add them in until one fails
327 * to add to the LRU and then we stop (as that'll make a hole in the
330 * Note that it's possible for the file size to change whilst we're
331 * doing this, but we rely on the server returning less than we asked
332 * for if the file shrank. We also rely on this to deal with a partial
333 * page at the end of the file.
336 page = list_entry(pages->prev, struct page, lru);
337 list_del(&page->lru);
339 if (add_to_page_cache_lru(page, mapping, index,
340 readahead_gfp_mask(mapping))) {
341 #ifdef CONFIG_AFS_FSCACHE
342 fscache_uncache_page(vnode->cache, page);
348 req->pages[req->nr_pages++] = page;
349 req->len += PAGE_SIZE;
350 } while (req->nr_pages < n);
352 if (req->nr_pages == 0) {
357 ret = afs_vnode_fetch_data(vnode, key, req);
361 task_io_account_read(PAGE_SIZE * req->nr_pages);
366 if (ret == -ENOENT) {
367 _debug("got NOENT from server"
368 " - marking file deleted and stale");
369 set_bit(AFS_VNODE_DELETED, &vnode->flags);
373 for (i = 0; i < req->nr_pages; i++) {
374 page = req->pages[i];
376 #ifdef CONFIG_AFS_FSCACHE
377 fscache_uncache_page(vnode->cache, page);
389 * read a set of pages
391 static int afs_readpages(struct file *file, struct address_space *mapping,
392 struct list_head *pages, unsigned nr_pages)
394 struct key *key = file->private_data;
395 struct afs_vnode *vnode;
398 _enter("{%d},{%lu},,%d",
399 key_serial(key), mapping->host->i_ino, nr_pages);
403 vnode = AFS_FS_I(mapping->host);
404 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
405 _leave(" = -ESTALE");
409 /* attempt to read as many of the pages as possible */
410 #ifdef CONFIG_AFS_FSCACHE
411 ret = fscache_read_or_alloc_pages(vnode->cache,
415 afs_file_readpage_read_complete,
417 mapping_gfp_mask(mapping));
423 /* all pages are being read from the cache */
425 BUG_ON(!list_empty(pages));
426 BUG_ON(nr_pages != 0);
427 _leave(" = 0 [reading all]");
430 /* there were pages that couldn't be read from the cache */
437 _leave(" = %d", ret);
441 while (!list_empty(pages)) {
442 ret = afs_readpages_one(file, mapping, pages);
447 _leave(" = %d [netting]", ret);
452 * write back a dirty page
454 static int afs_launder_page(struct page *page)
456 _enter("{%lu}", page->index);
462 * invalidate part or all of a page
463 * - release a page and clean up its private data if offset is 0 (indicating
466 static void afs_invalidatepage(struct page *page, unsigned int offset,
469 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
471 _enter("{%lu},%u,%u", page->index, offset, length);
473 BUG_ON(!PageLocked(page));
475 /* we clean up only if the entire page is being invalidated */
476 if (offset == 0 && length == PAGE_SIZE) {
477 #ifdef CONFIG_AFS_FSCACHE
478 if (PageFsCache(page)) {
479 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
480 fscache_wait_on_page_write(vnode->cache, page);
481 fscache_uncache_page(vnode->cache, page);
485 if (PagePrivate(page)) {
486 if (wb && !PageWriteback(page)) {
487 set_page_private(page, 0);
488 afs_put_writeback(wb);
491 if (!page_private(page))
492 ClearPagePrivate(page);
500 * release a page and clean up its private state if it's not busy
501 * - return true if the page can now be released, false if not
503 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
505 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
506 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
508 _enter("{{%x:%u}[%lu],%lx},%x",
509 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
512 /* deny if page is being written to the cache and the caller hasn't
514 #ifdef CONFIG_AFS_FSCACHE
515 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
516 _leave(" = F [cache busy]");
521 if (PagePrivate(page)) {
523 set_page_private(page, 0);
524 afs_put_writeback(wb);
526 ClearPagePrivate(page);
529 /* indicate that the page can be released */