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 * read page from file, directory or symlink, given a key to use
143 int afs_page_filler(void *data
, struct page
*page
)
145 struct inode
*inode
= page
->mapping
->host
;
146 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
147 struct afs_read
*req
;
148 struct key
*key
= data
;
151 _enter("{%x},{%lu},{%lu}", key_serial(key
), inode
->i_ino
, page
->index
);
153 BUG_ON(!PageLocked(page
));
156 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
160 #ifdef CONFIG_AFS_FSCACHE
161 ret
= fscache_read_or_alloc_page(vnode
->cache
,
163 afs_file_readpage_read_complete
,
170 /* read BIO submitted (page in cache) */
174 /* page not yet cached */
176 _debug("cache said ENODATA");
179 /* page will not be cached */
181 _debug("cache said ENOBUFS");
184 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*),
189 /* We request a full page. If the page is a partial one at the
190 * end of the file, the server will return a short read and the
191 * unmarshalling code will clear the unfilled space.
193 atomic_set(&req
->usage
, 1);
194 req
->pos
= (loff_t
)page
->index
<< PAGE_SHIFT
;
195 req
->len
= PAGE_SIZE
;
197 req
->pages
[0] = page
;
200 /* read the contents of the file from the server into the
202 ret
= afs_vnode_fetch_data(vnode
, key
, req
);
205 if (ret
== -ENOENT
) {
206 _debug("got NOENT from server"
207 " - marking file deleted and stale");
208 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
212 #ifdef CONFIG_AFS_FSCACHE
213 fscache_uncache_page(vnode
->cache
, page
);
215 BUG_ON(PageFsCache(page
));
219 ret
== -ERESTARTSYS
||
225 SetPageUptodate(page
);
227 /* send the page to the cache */
228 #ifdef CONFIG_AFS_FSCACHE
229 if (PageFsCache(page
) &&
230 fscache_write_page(vnode
->cache
, page
, GFP_KERNEL
) != 0) {
231 fscache_uncache_page(vnode
->cache
, page
);
232 BUG_ON(PageFsCache(page
));
248 _leave(" = %d", ret
);
253 * read page from file, directory or symlink, given a file to nominate the key
256 static int afs_readpage(struct file
*file
, struct page
*page
)
262 key
= file
->private_data
;
264 ret
= afs_page_filler(key
, page
);
266 struct inode
*inode
= page
->mapping
->host
;
267 key
= afs_request_key(AFS_FS_S(inode
->i_sb
)->volume
->cell
);
271 ret
= afs_page_filler(key
, page
);
279 * Make pages available as they're filled.
281 static void afs_readpages_page_done(struct afs_call
*call
, struct afs_read
*req
)
283 #ifdef CONFIG_AFS_FSCACHE
284 struct afs_vnode
*vnode
= call
->reply
;
286 struct page
*page
= req
->pages
[req
->index
];
288 req
->pages
[req
->index
] = NULL
;
289 SetPageUptodate(page
);
291 /* send the page to the cache */
292 #ifdef CONFIG_AFS_FSCACHE
293 if (PageFsCache(page
) &&
294 fscache_write_page(vnode
->cache
, page
, GFP_KERNEL
) != 0) {
295 fscache_uncache_page(vnode
->cache
, page
);
296 BUG_ON(PageFsCache(page
));
304 * Read a contiguous set of pages.
306 static int afs_readpages_one(struct file
*file
, struct address_space
*mapping
,
307 struct list_head
*pages
)
309 struct afs_vnode
*vnode
= AFS_FS_I(mapping
->host
);
310 struct afs_read
*req
;
312 struct page
*first
, *page
;
313 struct key
*key
= file
->private_data
;
317 /* Count the number of contiguous pages at the front of the list. Note
318 * that the list goes prev-wards rather than next-wards.
320 first
= list_entry(pages
->prev
, struct page
, lru
);
321 index
= first
->index
+ 1;
323 for (p
= first
->lru
.prev
; p
!= pages
; p
= p
->prev
) {
324 page
= list_entry(p
, struct page
, lru
);
325 if (page
->index
!= index
)
331 req
= kzalloc(sizeof(struct afs_read
) + sizeof(struct page
*) * n
,
336 atomic_set(&req
->usage
, 1);
337 req
->page_done
= afs_readpages_page_done
;
338 req
->pos
= first
->index
;
339 req
->pos
<<= PAGE_SHIFT
;
341 /* Transfer the pages to the request. We add them in until one fails
342 * to add to the LRU and then we stop (as that'll make a hole in the
345 * Note that it's possible for the file size to change whilst we're
346 * doing this, but we rely on the server returning less than we asked
347 * for if the file shrank. We also rely on this to deal with a partial
348 * page at the end of the file.
351 page
= list_entry(pages
->prev
, struct page
, lru
);
352 list_del(&page
->lru
);
354 if (add_to_page_cache_lru(page
, mapping
, index
,
355 readahead_gfp_mask(mapping
))) {
356 #ifdef CONFIG_AFS_FSCACHE
357 fscache_uncache_page(vnode
->cache
, page
);
363 req
->pages
[req
->nr_pages
++] = page
;
364 req
->len
+= PAGE_SIZE
;
365 } while (req
->nr_pages
< n
);
367 if (req
->nr_pages
== 0) {
372 ret
= afs_vnode_fetch_data(vnode
, key
, req
);
376 task_io_account_read(PAGE_SIZE
* req
->nr_pages
);
381 if (ret
== -ENOENT
) {
382 _debug("got NOENT from server"
383 " - marking file deleted and stale");
384 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
388 for (i
= 0; i
< req
->nr_pages
; i
++) {
389 page
= req
->pages
[i
];
391 #ifdef CONFIG_AFS_FSCACHE
392 fscache_uncache_page(vnode
->cache
, page
);
404 * read a set of pages
406 static int afs_readpages(struct file
*file
, struct address_space
*mapping
,
407 struct list_head
*pages
, unsigned nr_pages
)
409 struct key
*key
= file
->private_data
;
410 struct afs_vnode
*vnode
;
413 _enter("{%d},{%lu},,%d",
414 key_serial(key
), mapping
->host
->i_ino
, nr_pages
);
418 vnode
= AFS_FS_I(mapping
->host
);
419 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
420 _leave(" = -ESTALE");
424 /* attempt to read as many of the pages as possible */
425 #ifdef CONFIG_AFS_FSCACHE
426 ret
= fscache_read_or_alloc_pages(vnode
->cache
,
430 afs_file_readpage_read_complete
,
432 mapping_gfp_mask(mapping
));
438 /* all pages are being read from the cache */
440 BUG_ON(!list_empty(pages
));
441 BUG_ON(nr_pages
!= 0);
442 _leave(" = 0 [reading all]");
445 /* there were pages that couldn't be read from the cache */
452 _leave(" = %d", ret
);
456 while (!list_empty(pages
)) {
457 ret
= afs_readpages_one(file
, mapping
, pages
);
462 _leave(" = %d [netting]", ret
);
467 * write back a dirty page
469 static int afs_launder_page(struct page
*page
)
471 _enter("{%lu}", page
->index
);
477 * invalidate part or all of a page
478 * - release a page and clean up its private data if offset is 0 (indicating
481 static void afs_invalidatepage(struct page
*page
, unsigned int offset
,
484 struct afs_writeback
*wb
= (struct afs_writeback
*) page_private(page
);
486 _enter("{%lu},%u,%u", page
->index
, offset
, length
);
488 BUG_ON(!PageLocked(page
));
490 /* we clean up only if the entire page is being invalidated */
491 if (offset
== 0 && length
== PAGE_SIZE
) {
492 #ifdef CONFIG_AFS_FSCACHE
493 if (PageFsCache(page
)) {
494 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
495 fscache_wait_on_page_write(vnode
->cache
, page
);
496 fscache_uncache_page(vnode
->cache
, page
);
500 if (PagePrivate(page
)) {
501 if (wb
&& !PageWriteback(page
)) {
502 set_page_private(page
, 0);
503 afs_put_writeback(wb
);
506 if (!page_private(page
))
507 ClearPagePrivate(page
);
515 * release a page and clean up its private state if it's not busy
516 * - return true if the page can now be released, false if not
518 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
)
520 struct afs_writeback
*wb
= (struct afs_writeback
*) page_private(page
);
521 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
523 _enter("{{%x:%u}[%lu],%lx},%x",
524 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, page
->flags
,
527 /* deny if page is being written to the cache and the caller hasn't
529 #ifdef CONFIG_AFS_FSCACHE
530 if (!fscache_maybe_release_page(vnode
->cache
, page
, gfp_flags
)) {
531 _leave(" = F [cache busy]");
536 if (PagePrivate(page
)) {
538 set_page_private(page
, 0);
539 afs_put_writeback(wb
);
541 ClearPagePrivate(page
);
544 /* indicate that the page can be released */