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_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
= {
34 .release
= afs_release
,
35 .llseek
= generic_file_llseek
,
36 .read_iter
= generic_file_read_iter
,
37 .write_iter
= afs_file_write
,
38 .mmap
= generic_file_readonly_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
,
66 * open an AFS file or directory and attach a key to it
68 int afs_open(struct inode
*inode
, struct file
*file
)
70 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
74 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
76 key
= afs_request_key(vnode
->volume
->cell
);
78 _leave(" = %ld [key]", PTR_ERR(key
));
82 ret
= afs_validate(vnode
, key
);
84 _leave(" = %d [val]", ret
);
88 file
->private_data
= key
;
94 * release an AFS file or directory and discard its key
96 int afs_release(struct inode
*inode
, struct file
*file
)
98 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
100 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
102 key_put(file
->private_data
);
108 * Dispose of a ref to a read record.
110 void afs_put_read(struct afs_read
*req
)
114 if (atomic_dec_and_test(&req
->usage
)) {
115 for (i
= 0; i
< req
->nr_pages
; i
++)
117 put_page(req
->pages
[i
]);
122 #ifdef CONFIG_AFS_FSCACHE
124 * deal with notification that a page was read from the cache
126 static void afs_file_readpage_read_complete(struct page
*page
,
130 _enter("%p,%p,%d", page
, data
, error
);
132 /* if the read completes with an error, we just unlock the page and let
133 * the VM reissue the readpage */
135 SetPageUptodate(page
);
141 * Fetch file data from the volume.
143 int afs_fetch_data(struct afs_vnode
*vnode
, struct key
*key
, struct afs_read
*desc
)
145 struct afs_fs_cursor fc
;
148 _enter("%s{%x:%u.%u},%x,,,",
156 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
157 while (afs_select_fileserver(&fc
)) {
158 fc
.cb_break
= vnode
->cb_break
+ vnode
->cb_s_break
;
159 afs_fs_fetch_data(&fc
, desc
);
162 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
163 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
164 ret
= afs_end_vnode_operation(&fc
);
167 _leave(" = %d", ret
);
172 * read page from file, directory or symlink, given a key to use
174 int afs_page_filler(void *data
, struct page
*page
)
176 struct inode
*inode
= page
->mapping
->host
;
177 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
178 struct afs_read
*req
;
179 struct key
*key
= data
;
182 _enter("{%x},{%lu},{%lu}", key_serial(key
), inode
->i_ino
, page
->index
);
184 BUG_ON(!PageLocked(page
));
187 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
191 #ifdef CONFIG_AFS_FSCACHE
192 ret
= fscache_read_or_alloc_page(vnode
->cache
,
194 afs_file_readpage_read_complete
,
201 /* read BIO submitted (page in cache) */
205 /* page not yet cached */
207 _debug("cache said ENODATA");
210 /* page will not be cached */
212 _debug("cache said ENOBUFS");
215 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*),
220 /* We request a full page. If the page is a partial one at the
221 * end of the file, the server will return a short read and the
222 * unmarshalling code will clear the unfilled space.
224 atomic_set(&req
->usage
, 1);
225 req
->pos
= (loff_t
)page
->index
<< PAGE_SHIFT
;
226 req
->len
= PAGE_SIZE
;
228 req
->pages
[0] = page
;
231 /* read the contents of the file from the server into the
233 ret
= afs_fetch_data(vnode
, key
, req
);
236 if (ret
== -ENOENT
) {
237 _debug("got NOENT from server"
238 " - marking file deleted and stale");
239 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
243 #ifdef CONFIG_AFS_FSCACHE
244 fscache_uncache_page(vnode
->cache
, page
);
246 BUG_ON(PageFsCache(page
));
250 ret
== -ERESTARTSYS
||
256 SetPageUptodate(page
);
258 /* send the page to the cache */
259 #ifdef CONFIG_AFS_FSCACHE
260 if (PageFsCache(page
) &&
261 fscache_write_page(vnode
->cache
, page
, GFP_KERNEL
) != 0) {
262 fscache_uncache_page(vnode
->cache
, page
);
263 BUG_ON(PageFsCache(page
));
279 _leave(" = %d", ret
);
284 * read page from file, directory or symlink, given a file to nominate the key
287 static int afs_readpage(struct file
*file
, struct page
*page
)
293 key
= file
->private_data
;
295 ret
= afs_page_filler(key
, page
);
297 struct inode
*inode
= page
->mapping
->host
;
298 key
= afs_request_key(AFS_FS_S(inode
->i_sb
)->cell
);
302 ret
= afs_page_filler(key
, page
);
310 * Make pages available as they're filled.
312 static void afs_readpages_page_done(struct afs_call
*call
, struct afs_read
*req
)
314 #ifdef CONFIG_AFS_FSCACHE
315 struct afs_vnode
*vnode
= call
->reply
[0];
317 struct page
*page
= req
->pages
[req
->index
];
319 req
->pages
[req
->index
] = NULL
;
320 SetPageUptodate(page
);
322 /* send the page to the cache */
323 #ifdef CONFIG_AFS_FSCACHE
324 if (PageFsCache(page
) &&
325 fscache_write_page(vnode
->cache
, page
, GFP_KERNEL
) != 0) {
326 fscache_uncache_page(vnode
->cache
, page
);
327 BUG_ON(PageFsCache(page
));
335 * Read a contiguous set of pages.
337 static int afs_readpages_one(struct file
*file
, struct address_space
*mapping
,
338 struct list_head
*pages
)
340 struct afs_vnode
*vnode
= AFS_FS_I(mapping
->host
);
341 struct afs_read
*req
;
343 struct page
*first
, *page
;
344 struct key
*key
= file
->private_data
;
348 /* Count the number of contiguous pages at the front of the list. Note
349 * that the list goes prev-wards rather than next-wards.
351 first
= list_entry(pages
->prev
, struct page
, lru
);
352 index
= first
->index
+ 1;
354 for (p
= first
->lru
.prev
; p
!= pages
; p
= p
->prev
) {
355 page
= list_entry(p
, struct page
, lru
);
356 if (page
->index
!= index
)
362 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*) * n
,
367 atomic_set(&req
->usage
, 1);
368 req
->page_done
= afs_readpages_page_done
;
369 req
->pos
= first
->index
;
370 req
->pos
<<= PAGE_SHIFT
;
372 /* Transfer the pages to the request. We add them in until one fails
373 * to add to the LRU and then we stop (as that'll make a hole in the
376 * Note that it's possible for the file size to change whilst we're
377 * doing this, but we rely on the server returning less than we asked
378 * for if the file shrank. We also rely on this to deal with a partial
379 * page at the end of the file.
382 page
= list_entry(pages
->prev
, struct page
, lru
);
383 list_del(&page
->lru
);
385 if (add_to_page_cache_lru(page
, mapping
, index
,
386 readahead_gfp_mask(mapping
))) {
387 #ifdef CONFIG_AFS_FSCACHE
388 fscache_uncache_page(vnode
->cache
, page
);
394 req
->pages
[req
->nr_pages
++] = page
;
395 req
->len
+= PAGE_SIZE
;
396 } while (req
->nr_pages
< n
);
398 if (req
->nr_pages
== 0) {
403 ret
= afs_fetch_data(vnode
, key
, req
);
407 task_io_account_read(PAGE_SIZE
* req
->nr_pages
);
412 if (ret
== -ENOENT
) {
413 _debug("got NOENT from server"
414 " - marking file deleted and stale");
415 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
419 for (i
= 0; i
< req
->nr_pages
; i
++) {
420 page
= req
->pages
[i
];
422 #ifdef CONFIG_AFS_FSCACHE
423 fscache_uncache_page(vnode
->cache
, page
);
435 * read a set of pages
437 static int afs_readpages(struct file
*file
, struct address_space
*mapping
,
438 struct list_head
*pages
, unsigned nr_pages
)
440 struct key
*key
= file
->private_data
;
441 struct afs_vnode
*vnode
;
444 _enter("{%d},{%lu},,%d",
445 key_serial(key
), mapping
->host
->i_ino
, nr_pages
);
449 vnode
= AFS_FS_I(mapping
->host
);
450 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
451 _leave(" = -ESTALE");
455 /* attempt to read as many of the pages as possible */
456 #ifdef CONFIG_AFS_FSCACHE
457 ret
= fscache_read_or_alloc_pages(vnode
->cache
,
461 afs_file_readpage_read_complete
,
463 mapping_gfp_mask(mapping
));
469 /* all pages are being read from the cache */
471 BUG_ON(!list_empty(pages
));
472 BUG_ON(nr_pages
!= 0);
473 _leave(" = 0 [reading all]");
476 /* there were pages that couldn't be read from the cache */
483 _leave(" = %d", ret
);
487 while (!list_empty(pages
)) {
488 ret
= afs_readpages_one(file
, mapping
, pages
);
493 _leave(" = %d [netting]", ret
);
498 * write back a dirty page
500 static int afs_launder_page(struct page
*page
)
502 _enter("{%lu}", page
->index
);
508 * invalidate part or all of a page
509 * - release a page and clean up its private data if offset is 0 (indicating
512 static void afs_invalidatepage(struct page
*page
, unsigned int offset
,
515 struct afs_writeback
*wb
= (struct afs_writeback
*) page_private(page
);
517 _enter("{%lu},%u,%u", page
->index
, offset
, length
);
519 BUG_ON(!PageLocked(page
));
521 /* we clean up only if the entire page is being invalidated */
522 if (offset
== 0 && length
== PAGE_SIZE
) {
523 #ifdef CONFIG_AFS_FSCACHE
524 if (PageFsCache(page
)) {
525 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
526 fscache_wait_on_page_write(vnode
->cache
, page
);
527 fscache_uncache_page(vnode
->cache
, page
);
531 if (PagePrivate(page
)) {
532 if (wb
&& !PageWriteback(page
)) {
533 set_page_private(page
, 0);
534 afs_put_writeback(wb
);
537 if (!page_private(page
))
538 ClearPagePrivate(page
);
546 * release a page and clean up its private state if it's not busy
547 * - return true if the page can now be released, false if not
549 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
)
551 struct afs_writeback
*wb
= (struct afs_writeback
*) page_private(page
);
552 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
554 _enter("{{%x:%u}[%lu],%lx},%x",
555 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, page
->flags
,
558 /* deny if page is being written to the cache and the caller hasn't
560 #ifdef CONFIG_AFS_FSCACHE
561 if (!fscache_maybe_release_page(vnode
->cache
, page
, gfp_flags
)) {
562 _leave(" = F [cache busy]");
567 if (PagePrivate(page
)) {
569 set_page_private(page
, 0);
570 afs_put_writeback(wb
);
572 ClearPagePrivate(page
);
575 /* indicate that the page can be released */