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
,
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
,
65 * open an AFS file or directory and attach a key to it
67 int afs_open(struct inode
*inode
, struct file
*file
)
69 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
73 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
75 key
= afs_request_key(vnode
->volume
->cell
);
77 _leave(" = %ld [key]", PTR_ERR(key
));
81 ret
= afs_validate(vnode
, key
);
83 _leave(" = %d [val]", ret
);
87 file
->private_data
= key
;
93 * release an AFS file or directory and discard its key
95 int afs_release(struct inode
*inode
, struct file
*file
)
97 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
99 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
101 key_put(file
->private_data
);
107 * Dispose of a ref to a read record.
109 void afs_put_read(struct afs_read
*req
)
113 if (atomic_dec_and_test(&req
->usage
)) {
114 for (i
= 0; i
< req
->nr_pages
; i
++)
116 put_page(req
->pages
[i
]);
121 #ifdef CONFIG_AFS_FSCACHE
123 * deal with notification that a page was read from the cache
125 static void afs_file_readpage_read_complete(struct page
*page
,
129 _enter("%p,%p,%d", page
, data
, error
);
131 /* if the read completes with an error, we just unlock the page and let
132 * the VM reissue the readpage */
134 SetPageUptodate(page
);
140 * read page from file, directory or symlink, given a key to use
142 int afs_page_filler(void *data
, struct page
*page
)
144 struct inode
*inode
= page
->mapping
->host
;
145 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
146 struct afs_read
*req
;
147 struct key
*key
= data
;
150 _enter("{%x},{%lu},{%lu}", key_serial(key
), inode
->i_ino
, page
->index
);
152 BUG_ON(!PageLocked(page
));
155 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
159 #ifdef CONFIG_AFS_FSCACHE
160 ret
= fscache_read_or_alloc_page(vnode
->cache
,
162 afs_file_readpage_read_complete
,
169 /* read BIO submitted (page in cache) */
173 /* page not yet cached */
175 _debug("cache said ENODATA");
178 /* page will not be cached */
180 _debug("cache said ENOBUFS");
183 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*),
188 /* We request a full page. If the page is a partial one at the
189 * end of the file, the server will return a short read and the
190 * unmarshalling code will clear the unfilled space.
192 atomic_set(&req
->usage
, 1);
193 req
->pos
= (loff_t
)page
->index
<< PAGE_SHIFT
;
194 req
->len
= PAGE_SIZE
;
196 req
->pages
[0] = page
;
199 /* read the contents of the file from the server into the
201 ret
= afs_vnode_fetch_data(vnode
, key
, req
);
204 if (ret
== -ENOENT
) {
205 _debug("got NOENT from server"
206 " - marking file deleted and stale");
207 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
211 #ifdef CONFIG_AFS_FSCACHE
212 fscache_uncache_page(vnode
->cache
, page
);
214 BUG_ON(PageFsCache(page
));
218 ret
== -ERESTARTSYS
||
224 SetPageUptodate(page
);
226 /* send the page to the cache */
227 #ifdef CONFIG_AFS_FSCACHE
228 if (PageFsCache(page
) &&
229 fscache_write_page(vnode
->cache
, page
, GFP_KERNEL
) != 0) {
230 fscache_uncache_page(vnode
->cache
, page
);
231 BUG_ON(PageFsCache(page
));
247 _leave(" = %d", ret
);
252 * read page from file, directory or symlink, given a file to nominate the key
255 static int afs_readpage(struct file
*file
, struct page
*page
)
261 key
= file
->private_data
;
263 ret
= afs_page_filler(key
, page
);
265 struct inode
*inode
= page
->mapping
->host
;
266 key
= afs_request_key(AFS_FS_S(inode
->i_sb
)->volume
->cell
);
270 ret
= afs_page_filler(key
, page
);
278 * Make pages available as they're filled.
280 static void afs_readpages_page_done(struct afs_call
*call
, struct afs_read
*req
)
282 #ifdef CONFIG_AFS_FSCACHE
283 struct afs_vnode
*vnode
= call
->reply
;
285 struct page
*page
= req
->pages
[req
->index
];
287 req
->pages
[req
->index
] = NULL
;
288 SetPageUptodate(page
);
290 /* send the page to the cache */
291 #ifdef CONFIG_AFS_FSCACHE
292 if (PageFsCache(page
) &&
293 fscache_write_page(vnode
->cache
, page
, GFP_KERNEL
) != 0) {
294 fscache_uncache_page(vnode
->cache
, page
);
295 BUG_ON(PageFsCache(page
));
303 * Read a contiguous set of pages.
305 static int afs_readpages_one(struct file
*file
, struct address_space
*mapping
,
306 struct list_head
*pages
)
308 struct afs_vnode
*vnode
= AFS_FS_I(mapping
->host
);
309 struct afs_read
*req
;
311 struct page
*first
, *page
;
312 struct key
*key
= file
->private_data
;
316 /* Count the number of contiguous pages at the front of the list. Note
317 * that the list goes prev-wards rather than next-wards.
319 first
= list_entry(pages
->prev
, struct page
, lru
);
320 index
= first
->index
+ 1;
322 for (p
= first
->lru
.prev
; p
!= pages
; p
= p
->prev
) {
323 page
= list_entry(p
, struct page
, lru
);
324 if (page
->index
!= index
)
330 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*) * n
,
335 atomic_set(&req
->usage
, 1);
336 req
->page_done
= afs_readpages_page_done
;
337 req
->pos
= first
->index
;
338 req
->pos
<<= PAGE_SHIFT
;
340 /* Transfer the pages to the request. We add them in until one fails
341 * to add to the LRU and then we stop (as that'll make a hole in the
344 * Note that it's possible for the file size to change whilst we're
345 * doing this, but we rely on the server returning less than we asked
346 * for if the file shrank. We also rely on this to deal with a partial
347 * page at the end of the file.
350 page
= list_entry(pages
->prev
, struct page
, lru
);
351 list_del(&page
->lru
);
353 if (add_to_page_cache_lru(page
, mapping
, index
,
354 readahead_gfp_mask(mapping
))) {
355 #ifdef CONFIG_AFS_FSCACHE
356 fscache_uncache_page(vnode
->cache
, page
);
362 req
->pages
[req
->nr_pages
++] = page
;
363 req
->len
+= PAGE_SIZE
;
364 } while (req
->nr_pages
< n
);
366 if (req
->nr_pages
== 0) {
371 ret
= afs_vnode_fetch_data(vnode
, key
, req
);
375 task_io_account_read(PAGE_SIZE
* req
->nr_pages
);
380 if (ret
== -ENOENT
) {
381 _debug("got NOENT from server"
382 " - marking file deleted and stale");
383 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
387 for (i
= 0; i
< req
->nr_pages
; i
++) {
388 page
= req
->pages
[i
];
390 #ifdef CONFIG_AFS_FSCACHE
391 fscache_uncache_page(vnode
->cache
, page
);
403 * read a set of pages
405 static int afs_readpages(struct file
*file
, struct address_space
*mapping
,
406 struct list_head
*pages
, unsigned nr_pages
)
408 struct key
*key
= file
->private_data
;
409 struct afs_vnode
*vnode
;
412 _enter("{%d},{%lu},,%d",
413 key_serial(key
), mapping
->host
->i_ino
, nr_pages
);
417 vnode
= AFS_FS_I(mapping
->host
);
418 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
419 _leave(" = -ESTALE");
423 /* attempt to read as many of the pages as possible */
424 #ifdef CONFIG_AFS_FSCACHE
425 ret
= fscache_read_or_alloc_pages(vnode
->cache
,
429 afs_file_readpage_read_complete
,
431 mapping_gfp_mask(mapping
));
437 /* all pages are being read from the cache */
439 BUG_ON(!list_empty(pages
));
440 BUG_ON(nr_pages
!= 0);
441 _leave(" = 0 [reading all]");
444 /* there were pages that couldn't be read from the cache */
451 _leave(" = %d", ret
);
455 while (!list_empty(pages
)) {
456 ret
= afs_readpages_one(file
, mapping
, pages
);
461 _leave(" = %d [netting]", ret
);
466 * write back a dirty page
468 static int afs_launder_page(struct page
*page
)
470 _enter("{%lu}", page
->index
);
476 * invalidate part or all of a page
477 * - release a page and clean up its private data if offset is 0 (indicating
480 static void afs_invalidatepage(struct page
*page
, unsigned int offset
,
483 struct afs_writeback
*wb
= (struct afs_writeback
*) page_private(page
);
485 _enter("{%lu},%u,%u", page
->index
, offset
, length
);
487 BUG_ON(!PageLocked(page
));
489 /* we clean up only if the entire page is being invalidated */
490 if (offset
== 0 && length
== PAGE_SIZE
) {
491 #ifdef CONFIG_AFS_FSCACHE
492 if (PageFsCache(page
)) {
493 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
494 fscache_wait_on_page_write(vnode
->cache
, page
);
495 fscache_uncache_page(vnode
->cache
, page
);
499 if (PagePrivate(page
)) {
500 if (wb
&& !PageWriteback(page
)) {
501 set_page_private(page
, 0);
502 afs_put_writeback(wb
);
505 if (!page_private(page
))
506 ClearPagePrivate(page
);
514 * release a page and clean up its private state if it's not busy
515 * - return true if the page can now be released, false if not
517 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
)
519 struct afs_writeback
*wb
= (struct afs_writeback
*) page_private(page
);
520 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
522 _enter("{{%x:%u}[%lu],%lx},%x",
523 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, page
->flags
,
526 /* deny if page is being written to the cache and the caller hasn't
528 #ifdef CONFIG_AFS_FSCACHE
529 if (!fscache_maybe_release_page(vnode
->cache
, page
, gfp_flags
)) {
530 _leave(" = F [cache busy]");
535 if (PagePrivate(page
)) {
537 set_page_private(page
, 0);
538 afs_put_writeback(wb
);
540 ClearPagePrivate(page
);
543 /* indicate that the page can be released */