1 /* AFS filesystem file handling
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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_file_mmap(struct file
*file
, struct vm_area_struct
*vma
);
23 static int afs_readpage(struct file
*file
, struct page
*page
);
24 static void afs_invalidatepage(struct page
*page
, unsigned int offset
,
26 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
);
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
= {
34 .release
= afs_release
,
35 .llseek
= generic_file_llseek
,
36 .read_iter
= generic_file_read_iter
,
37 .write_iter
= afs_file_write
,
38 .mmap
= afs_file_mmap
,
39 .splice_read
= generic_file_splice_read
,
45 const struct inode_operations afs_file_inode_operations
= {
46 .getattr
= afs_getattr
,
47 .setattr
= afs_setattr
,
48 .permission
= afs_permission
,
49 .listxattr
= afs_listxattr
,
52 const struct address_space_operations afs_fs_aops
= {
53 .readpage
= afs_readpage
,
54 .readpages
= afs_readpages
,
55 .set_page_dirty
= afs_set_page_dirty
,
56 .launder_page
= afs_launder_page
,
57 .releasepage
= afs_releasepage
,
58 .invalidatepage
= afs_invalidatepage
,
59 .write_begin
= afs_write_begin
,
60 .write_end
= afs_write_end
,
61 .writepage
= afs_writepage
,
62 .writepages
= afs_writepages
,
65 static const struct vm_operations_struct afs_vm_ops
= {
66 .fault
= filemap_fault
,
67 .map_pages
= filemap_map_pages
,
68 .page_mkwrite
= afs_page_mkwrite
,
72 * Discard a pin on a writeback key.
74 void afs_put_wb_key(struct afs_wb_key
*wbk
)
76 if (refcount_dec_and_test(&wbk
->usage
)) {
83 * Cache key for writeback.
85 int afs_cache_wb_key(struct afs_vnode
*vnode
, struct afs_file
*af
)
87 struct afs_wb_key
*wbk
, *p
;
89 wbk
= kzalloc(sizeof(struct afs_wb_key
), GFP_KERNEL
);
92 refcount_set(&wbk
->usage
, 2);
95 spin_lock(&vnode
->wb_lock
);
96 list_for_each_entry(p
, &vnode
->wb_keys
, vnode_link
) {
97 if (p
->key
== wbk
->key
)
102 list_add_tail(&wbk
->vnode_link
, &vnode
->wb_keys
);
103 spin_unlock(&vnode
->wb_lock
);
108 refcount_inc(&p
->usage
);
109 spin_unlock(&vnode
->wb_lock
);
116 * open an AFS file or directory and attach a key to it
118 int afs_open(struct inode
*inode
, struct file
*file
)
120 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
125 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
127 key
= afs_request_key(vnode
->volume
->cell
);
133 af
= kzalloc(sizeof(*af
), GFP_KERNEL
);
140 ret
= afs_validate(vnode
, key
);
144 if (file
->f_mode
& FMODE_WRITE
) {
145 ret
= afs_cache_wb_key(vnode
, af
);
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
;
171 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
173 file
->private_data
= NULL
;
175 afs_put_wb_key(af
->wb
);
178 afs_prune_wb_keys(vnode
);
184 * Dispose of a ref to a read record.
186 void afs_put_read(struct afs_read
*req
)
190 if (atomic_dec_and_test(&req
->usage
)) {
191 for (i
= 0; i
< req
->nr_pages
; i
++)
193 put_page(req
->pages
[i
]);
198 #ifdef CONFIG_AFS_FSCACHE
200 * deal with notification that a page was read from the cache
202 static void afs_file_readpage_read_complete(struct page
*page
,
206 _enter("%p,%p,%d", page
, data
, error
);
208 /* if the read completes with an error, we just unlock the page and let
209 * the VM reissue the readpage */
211 SetPageUptodate(page
);
217 * Fetch file data from the volume.
219 int afs_fetch_data(struct afs_vnode
*vnode
, struct key
*key
, struct afs_read
*desc
)
221 struct afs_fs_cursor fc
;
224 _enter("%s{%x:%u.%u},%x,,,",
232 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
233 while (afs_select_fileserver(&fc
)) {
234 fc
.cb_break
= vnode
->cb_break
+ vnode
->cb_s_break
;
235 afs_fs_fetch_data(&fc
, desc
);
238 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
239 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
240 ret
= afs_end_vnode_operation(&fc
);
243 _leave(" = %d", ret
);
248 * read page from file, directory or symlink, given a key to use
250 int afs_page_filler(void *data
, struct page
*page
)
252 struct inode
*inode
= page
->mapping
->host
;
253 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
254 struct afs_read
*req
;
255 struct key
*key
= data
;
258 _enter("{%x},{%lu},{%lu}", key_serial(key
), inode
->i_ino
, page
->index
);
260 BUG_ON(!PageLocked(page
));
263 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
267 #ifdef CONFIG_AFS_FSCACHE
268 ret
= fscache_read_or_alloc_page(vnode
->cache
,
270 afs_file_readpage_read_complete
,
277 /* read BIO submitted (page in cache) */
281 /* page not yet cached */
283 _debug("cache said ENODATA");
286 /* page will not be cached */
288 _debug("cache said ENOBUFS");
291 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*),
296 /* We request a full page. If the page is a partial one at the
297 * end of the file, the server will return a short read and the
298 * unmarshalling code will clear the unfilled space.
300 atomic_set(&req
->usage
, 1);
301 req
->pos
= (loff_t
)page
->index
<< PAGE_SHIFT
;
302 req
->len
= PAGE_SIZE
;
304 req
->pages
[0] = page
;
307 /* read the contents of the file from the server into the
309 ret
= afs_fetch_data(vnode
, key
, req
);
312 if (ret
>= 0 && S_ISDIR(inode
->i_mode
) &&
313 !afs_dir_check_page(inode
, page
))
317 if (ret
== -ENOENT
) {
318 _debug("got NOENT from server"
319 " - marking file deleted and stale");
320 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
324 #ifdef CONFIG_AFS_FSCACHE
325 fscache_uncache_page(vnode
->cache
, page
);
327 BUG_ON(PageFsCache(page
));
331 ret
== -ERESTARTSYS
||
337 SetPageUptodate(page
);
339 /* send the page to the cache */
340 #ifdef CONFIG_AFS_FSCACHE
341 if (PageFsCache(page
) &&
342 fscache_write_page(vnode
->cache
, page
, vnode
->status
.size
,
344 fscache_uncache_page(vnode
->cache
, page
);
345 BUG_ON(PageFsCache(page
));
361 _leave(" = %d", ret
);
366 * read page from file, directory or symlink, given a file to nominate the key
369 static int afs_readpage(struct file
*file
, struct page
*page
)
375 key
= afs_file_key(file
);
377 ret
= afs_page_filler(key
, page
);
379 struct inode
*inode
= page
->mapping
->host
;
380 key
= afs_request_key(AFS_FS_S(inode
->i_sb
)->cell
);
384 ret
= afs_page_filler(key
, page
);
392 * Make pages available as they're filled.
394 static void afs_readpages_page_done(struct afs_call
*call
, struct afs_read
*req
)
396 #ifdef CONFIG_AFS_FSCACHE
397 struct afs_vnode
*vnode
= call
->reply
[0];
399 struct page
*page
= req
->pages
[req
->index
];
401 req
->pages
[req
->index
] = NULL
;
402 SetPageUptodate(page
);
404 /* send the page to the cache */
405 #ifdef CONFIG_AFS_FSCACHE
406 if (PageFsCache(page
) &&
407 fscache_write_page(vnode
->cache
, page
, vnode
->status
.size
,
409 fscache_uncache_page(vnode
->cache
, page
);
410 BUG_ON(PageFsCache(page
));
418 * Read a contiguous set of pages.
420 static int afs_readpages_one(struct file
*file
, struct address_space
*mapping
,
421 struct list_head
*pages
)
423 struct afs_vnode
*vnode
= AFS_FS_I(mapping
->host
);
424 struct afs_read
*req
;
426 struct page
*first
, *page
;
427 struct key
*key
= afs_file_key(file
);
431 /* Count the number of contiguous pages at the front of the list. Note
432 * that the list goes prev-wards rather than next-wards.
434 first
= list_entry(pages
->prev
, struct page
, lru
);
435 index
= first
->index
+ 1;
437 for (p
= first
->lru
.prev
; p
!= pages
; p
= p
->prev
) {
438 page
= list_entry(p
, struct page
, lru
);
439 if (page
->index
!= index
)
445 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*) * n
,
450 atomic_set(&req
->usage
, 1);
451 req
->page_done
= afs_readpages_page_done
;
452 req
->pos
= first
->index
;
453 req
->pos
<<= PAGE_SHIFT
;
455 /* Transfer the pages to the request. We add them in until one fails
456 * to add to the LRU and then we stop (as that'll make a hole in the
459 * Note that it's possible for the file size to change whilst we're
460 * doing this, but we rely on the server returning less than we asked
461 * for if the file shrank. We also rely on this to deal with a partial
462 * page at the end of the file.
465 page
= list_entry(pages
->prev
, struct page
, lru
);
466 list_del(&page
->lru
);
468 if (add_to_page_cache_lru(page
, mapping
, index
,
469 readahead_gfp_mask(mapping
))) {
470 #ifdef CONFIG_AFS_FSCACHE
471 fscache_uncache_page(vnode
->cache
, page
);
477 req
->pages
[req
->nr_pages
++] = page
;
478 req
->len
+= PAGE_SIZE
;
479 } while (req
->nr_pages
< n
);
481 if (req
->nr_pages
== 0) {
486 ret
= afs_fetch_data(vnode
, key
, req
);
490 task_io_account_read(PAGE_SIZE
* req
->nr_pages
);
495 if (ret
== -ENOENT
) {
496 _debug("got NOENT from server"
497 " - marking file deleted and stale");
498 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
502 for (i
= 0; i
< req
->nr_pages
; i
++) {
503 page
= req
->pages
[i
];
505 #ifdef CONFIG_AFS_FSCACHE
506 fscache_uncache_page(vnode
->cache
, page
);
518 * read a set of pages
520 static int afs_readpages(struct file
*file
, struct address_space
*mapping
,
521 struct list_head
*pages
, unsigned nr_pages
)
523 struct key
*key
= afs_file_key(file
);
524 struct afs_vnode
*vnode
;
527 _enter("{%d},{%lu},,%d",
528 key_serial(key
), mapping
->host
->i_ino
, nr_pages
);
532 vnode
= AFS_FS_I(mapping
->host
);
533 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
534 _leave(" = -ESTALE");
538 /* attempt to read as many of the pages as possible */
539 #ifdef CONFIG_AFS_FSCACHE
540 ret
= fscache_read_or_alloc_pages(vnode
->cache
,
544 afs_file_readpage_read_complete
,
546 mapping_gfp_mask(mapping
));
552 /* all pages are being read from the cache */
554 BUG_ON(!list_empty(pages
));
555 BUG_ON(nr_pages
!= 0);
556 _leave(" = 0 [reading all]");
559 /* there were pages that couldn't be read from the cache */
566 _leave(" = %d", ret
);
570 while (!list_empty(pages
)) {
571 ret
= afs_readpages_one(file
, mapping
, pages
);
576 _leave(" = %d [netting]", ret
);
581 * invalidate part or all of a page
582 * - release a page and clean up its private data if offset is 0 (indicating
585 static void afs_invalidatepage(struct page
*page
, unsigned int offset
,
588 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
591 _enter("{%lu},%u,%u", page
->index
, offset
, length
);
593 BUG_ON(!PageLocked(page
));
595 /* we clean up only if the entire page is being invalidated */
596 if (offset
== 0 && length
== PAGE_SIZE
) {
597 #ifdef CONFIG_AFS_FSCACHE
598 if (PageFsCache(page
)) {
599 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
600 fscache_wait_on_page_write(vnode
->cache
, page
);
601 fscache_uncache_page(vnode
->cache
, page
);
605 if (PagePrivate(page
)) {
606 priv
= page_private(page
);
607 trace_afs_page_dirty(vnode
, tracepoint_string("inval"),
609 set_page_private(page
, 0);
610 ClearPagePrivate(page
);
618 * release a page and clean up its private state if it's not busy
619 * - return true if the page can now be released, false if not
621 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
)
623 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
626 _enter("{{%x:%u}[%lu],%lx},%x",
627 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, page
->flags
,
630 /* deny if page is being written to the cache and the caller hasn't
632 #ifdef CONFIG_AFS_FSCACHE
633 if (!fscache_maybe_release_page(vnode
->cache
, page
, gfp_flags
)) {
634 _leave(" = F [cache busy]");
639 if (PagePrivate(page
)) {
640 priv
= page_private(page
);
641 trace_afs_page_dirty(vnode
, tracepoint_string("rel"),
643 set_page_private(page
, 0);
644 ClearPagePrivate(page
);
647 /* indicate that the page can be released */
653 * Handle setting up a memory mapping on an AFS file.
655 static int afs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
659 ret
= generic_file_mmap(file
, vma
);
661 vma
->vm_ops
= &afs_vm_ops
;