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
= {
33 .release
= afs_release
,
34 .llseek
= generic_file_llseek
,
35 .read_iter
= generic_file_read_iter
,
36 .write_iter
= afs_file_write
,
37 .mmap
= afs_file_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
,
48 .listxattr
= afs_listxattr
,
51 const struct address_space_operations afs_fs_aops
= {
52 .readpage
= afs_readpage
,
53 .readpages
= afs_readpages
,
54 .set_page_dirty
= afs_set_page_dirty
,
55 .launder_page
= afs_launder_page
,
56 .releasepage
= afs_releasepage
,
57 .invalidatepage
= afs_invalidatepage
,
58 .write_begin
= afs_write_begin
,
59 .write_end
= afs_write_end
,
60 .writepage
= afs_writepage
,
61 .writepages
= afs_writepages
,
64 static const struct vm_operations_struct afs_vm_ops
= {
65 .fault
= filemap_fault
,
66 .map_pages
= filemap_map_pages
,
67 .page_mkwrite
= afs_page_mkwrite
,
71 * Discard a pin on a writeback key.
73 void afs_put_wb_key(struct afs_wb_key
*wbk
)
75 if (refcount_dec_and_test(&wbk
->usage
)) {
82 * Cache key for writeback.
84 int afs_cache_wb_key(struct afs_vnode
*vnode
, struct afs_file
*af
)
86 struct afs_wb_key
*wbk
, *p
;
88 wbk
= kzalloc(sizeof(struct afs_wb_key
), GFP_KERNEL
);
91 refcount_set(&wbk
->usage
, 2);
94 spin_lock(&vnode
->wb_lock
);
95 list_for_each_entry(p
, &vnode
->wb_keys
, vnode_link
) {
96 if (p
->key
== wbk
->key
)
101 list_add_tail(&wbk
->vnode_link
, &vnode
->wb_keys
);
102 spin_unlock(&vnode
->wb_lock
);
107 refcount_inc(&p
->usage
);
108 spin_unlock(&vnode
->wb_lock
);
115 * open an AFS file or directory and attach a key to it
117 int afs_open(struct inode
*inode
, struct file
*file
)
119 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
124 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
126 key
= afs_request_key(vnode
->volume
->cell
);
132 af
= kzalloc(sizeof(*af
), GFP_KERNEL
);
139 ret
= afs_validate(vnode
, key
);
143 if (file
->f_mode
& FMODE_WRITE
) {
144 ret
= afs_cache_wb_key(vnode
, af
);
149 if (file
->f_flags
& O_TRUNC
)
150 set_bit(AFS_VNODE_NEW_CONTENT
, &vnode
->flags
);
152 file
->private_data
= af
;
161 _leave(" = %d", ret
);
166 * release an AFS file or directory and discard its key
168 int afs_release(struct inode
*inode
, struct file
*file
)
170 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
171 struct afs_file
*af
= file
->private_data
;
173 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
175 if ((file
->f_mode
& FMODE_WRITE
))
176 return vfs_fsync(file
, 0);
178 file
->private_data
= NULL
;
180 afs_put_wb_key(af
->wb
);
183 afs_prune_wb_keys(vnode
);
189 * Dispose of a ref to a read record.
191 void afs_put_read(struct afs_read
*req
)
195 if (refcount_dec_and_test(&req
->usage
)) {
196 for (i
= 0; i
< req
->nr_pages
; i
++)
198 put_page(req
->pages
[i
]);
199 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
*desc
)
228 struct afs_fs_cursor fc
;
231 _enter("%s{%x:%u.%u},%x,,,",
239 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
240 while (afs_select_fileserver(&fc
)) {
241 fc
.cb_break
= afs_calc_vnode_cb_break(vnode
);
242 afs_fs_fetch_data(&fc
, desc
);
245 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
246 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
247 ret
= afs_end_vnode_operation(&fc
);
251 afs_stat_v(vnode
, n_fetches
);
252 atomic_long_add(desc
->actual_len
,
253 &afs_v2net(vnode
)->n_fetch_bytes
);
256 _leave(" = %d", ret
);
261 * read page from file, directory or symlink, given a key to use
263 int afs_page_filler(void *data
, struct page
*page
)
265 struct inode
*inode
= page
->mapping
->host
;
266 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
267 struct afs_read
*req
;
268 struct key
*key
= data
;
271 _enter("{%x},{%lu},{%lu}", key_serial(key
), inode
->i_ino
, page
->index
);
273 BUG_ON(!PageLocked(page
));
276 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
280 #ifdef CONFIG_AFS_FSCACHE
281 ret
= fscache_read_or_alloc_page(vnode
->cache
,
283 afs_file_readpage_read_complete
,
290 /* read BIO submitted (page in cache) */
294 /* page not yet cached */
296 _debug("cache said ENODATA");
299 /* page will not be cached */
301 _debug("cache said ENOBUFS");
304 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*),
309 /* We request a full page. If the page is a partial one at the
310 * end of the file, the server will return a short read and the
311 * unmarshalling code will clear the unfilled space.
313 refcount_set(&req
->usage
, 1);
314 req
->pos
= (loff_t
)page
->index
<< PAGE_SHIFT
;
315 req
->len
= PAGE_SIZE
;
317 req
->pages
= req
->array
;
318 req
->pages
[0] = page
;
321 /* read the contents of the file from the server into the
323 ret
= afs_fetch_data(vnode
, key
, req
);
327 if (ret
== -ENOENT
) {
328 _debug("got NOENT from server"
329 " - marking file deleted and stale");
330 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
334 #ifdef CONFIG_AFS_FSCACHE
335 fscache_uncache_page(vnode
->cache
, page
);
337 BUG_ON(PageFsCache(page
));
341 ret
== -ERESTARTSYS
||
347 SetPageUptodate(page
);
349 /* send the page to the cache */
350 #ifdef CONFIG_AFS_FSCACHE
351 if (PageFsCache(page
) &&
352 fscache_write_page(vnode
->cache
, page
, vnode
->status
.size
,
354 fscache_uncache_page(vnode
->cache
, page
);
355 BUG_ON(PageFsCache(page
));
371 _leave(" = %d", ret
);
376 * read page from file, directory or symlink, given a file to nominate the key
379 static int afs_readpage(struct file
*file
, struct page
*page
)
385 key
= afs_file_key(file
);
387 ret
= afs_page_filler(key
, page
);
389 struct inode
*inode
= page
->mapping
->host
;
390 key
= afs_request_key(AFS_FS_S(inode
->i_sb
)->cell
);
394 ret
= afs_page_filler(key
, page
);
402 * Make pages available as they're filled.
404 static void afs_readpages_page_done(struct afs_call
*call
, struct afs_read
*req
)
406 #ifdef CONFIG_AFS_FSCACHE
407 struct afs_vnode
*vnode
= call
->reply
[0];
409 struct page
*page
= req
->pages
[req
->index
];
411 req
->pages
[req
->index
] = NULL
;
412 SetPageUptodate(page
);
414 /* send the page to the cache */
415 #ifdef CONFIG_AFS_FSCACHE
416 if (PageFsCache(page
) &&
417 fscache_write_page(vnode
->cache
, page
, vnode
->status
.size
,
419 fscache_uncache_page(vnode
->cache
, page
);
420 BUG_ON(PageFsCache(page
));
428 * Read a contiguous set of pages.
430 static int afs_readpages_one(struct file
*file
, struct address_space
*mapping
,
431 struct list_head
*pages
)
433 struct afs_vnode
*vnode
= AFS_FS_I(mapping
->host
);
434 struct afs_read
*req
;
436 struct page
*first
, *page
;
437 struct key
*key
= afs_file_key(file
);
441 /* Count the number of contiguous pages at the front of the list. Note
442 * that the list goes prev-wards rather than next-wards.
444 first
= list_entry(pages
->prev
, struct page
, lru
);
445 index
= first
->index
+ 1;
447 for (p
= first
->lru
.prev
; p
!= pages
; p
= p
->prev
) {
448 page
= list_entry(p
, struct page
, lru
);
449 if (page
->index
!= index
)
455 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*) * n
,
460 refcount_set(&req
->usage
, 1);
461 req
->page_done
= afs_readpages_page_done
;
462 req
->pos
= first
->index
;
463 req
->pos
<<= PAGE_SHIFT
;
464 req
->pages
= req
->array
;
466 /* Transfer the pages to the request. We add them in until one fails
467 * to add to the LRU and then we stop (as that'll make a hole in the
470 * Note that it's possible for the file size to change whilst we're
471 * doing this, but we rely on the server returning less than we asked
472 * for if the file shrank. We also rely on this to deal with a partial
473 * page at the end of the file.
476 page
= list_entry(pages
->prev
, struct page
, lru
);
477 list_del(&page
->lru
);
479 if (add_to_page_cache_lru(page
, mapping
, index
,
480 readahead_gfp_mask(mapping
))) {
481 #ifdef CONFIG_AFS_FSCACHE
482 fscache_uncache_page(vnode
->cache
, page
);
488 req
->pages
[req
->nr_pages
++] = page
;
489 req
->len
+= PAGE_SIZE
;
490 } while (req
->nr_pages
< n
);
492 if (req
->nr_pages
== 0) {
497 ret
= afs_fetch_data(vnode
, key
, req
);
501 task_io_account_read(PAGE_SIZE
* req
->nr_pages
);
506 if (ret
== -ENOENT
) {
507 _debug("got NOENT from server"
508 " - marking file deleted and stale");
509 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
513 for (i
= 0; i
< req
->nr_pages
; i
++) {
514 page
= req
->pages
[i
];
516 #ifdef CONFIG_AFS_FSCACHE
517 fscache_uncache_page(vnode
->cache
, page
);
529 * read a set of pages
531 static int afs_readpages(struct file
*file
, struct address_space
*mapping
,
532 struct list_head
*pages
, unsigned nr_pages
)
534 struct key
*key
= afs_file_key(file
);
535 struct afs_vnode
*vnode
;
538 _enter("{%d},{%lu},,%d",
539 key_serial(key
), mapping
->host
->i_ino
, nr_pages
);
543 vnode
= AFS_FS_I(mapping
->host
);
544 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
545 _leave(" = -ESTALE");
549 /* attempt to read as many of the pages as possible */
550 #ifdef CONFIG_AFS_FSCACHE
551 ret
= fscache_read_or_alloc_pages(vnode
->cache
,
555 afs_file_readpage_read_complete
,
557 mapping_gfp_mask(mapping
));
563 /* all pages are being read from the cache */
565 BUG_ON(!list_empty(pages
));
566 BUG_ON(nr_pages
!= 0);
567 _leave(" = 0 [reading all]");
570 /* there were pages that couldn't be read from the cache */
577 _leave(" = %d", ret
);
581 while (!list_empty(pages
)) {
582 ret
= afs_readpages_one(file
, mapping
, pages
);
587 _leave(" = %d [netting]", ret
);
592 * invalidate part or all of a page
593 * - release a page and clean up its private data if offset is 0 (indicating
596 static void afs_invalidatepage(struct page
*page
, unsigned int offset
,
599 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
602 _enter("{%lu},%u,%u", page
->index
, offset
, length
);
604 BUG_ON(!PageLocked(page
));
606 /* we clean up only if the entire page is being invalidated */
607 if (offset
== 0 && length
== PAGE_SIZE
) {
608 #ifdef CONFIG_AFS_FSCACHE
609 if (PageFsCache(page
)) {
610 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
611 fscache_wait_on_page_write(vnode
->cache
, page
);
612 fscache_uncache_page(vnode
->cache
, page
);
616 if (PagePrivate(page
)) {
617 priv
= page_private(page
);
618 trace_afs_page_dirty(vnode
, tracepoint_string("inval"),
620 set_page_private(page
, 0);
621 ClearPagePrivate(page
);
629 * release a page and clean up its private state if it's not busy
630 * - return true if the page can now be released, false if not
632 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
)
634 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
637 _enter("{{%x:%u}[%lu],%lx},%x",
638 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, page
->flags
,
641 /* deny if page is being written to the cache and the caller hasn't
643 #ifdef CONFIG_AFS_FSCACHE
644 if (!fscache_maybe_release_page(vnode
->cache
, page
, gfp_flags
)) {
645 _leave(" = F [cache busy]");
650 if (PagePrivate(page
)) {
651 priv
= page_private(page
);
652 trace_afs_page_dirty(vnode
, tracepoint_string("rel"),
654 set_page_private(page
, 0);
655 ClearPagePrivate(page
);
658 /* indicate that the page can be released */
664 * Handle setting up a memory mapping on an AFS file.
666 static int afs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
670 ret
= generic_file_mmap(file
, vma
);
672 vma
->vm_ops
= &afs_vm_ops
;