Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* AFS filesystem file handling
3 *
4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/writeback.h>
14#include <linux/gfp.h>
15#include <linux/task_io_accounting_ops.h>
16#include <linux/mm.h>
17#include "internal.h"
18
19static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
20static int afs_readpage(struct file *file, struct page *page);
21static void afs_invalidatepage(struct page *page, unsigned int offset,
22 unsigned int length);
23static int afs_releasepage(struct page *page, gfp_t gfp_flags);
24
25static int afs_readpages(struct file *filp, struct address_space *mapping,
26 struct list_head *pages, unsigned nr_pages);
27
28const struct file_operations afs_file_operations = {
29 .open = afs_open,
30 .release = afs_release,
31 .llseek = generic_file_llseek,
32 .read_iter = generic_file_read_iter,
33 .write_iter = afs_file_write,
34 .mmap = afs_file_mmap,
35 .splice_read = generic_file_splice_read,
36 .fsync = afs_fsync,
37 .lock = afs_lock,
38 .flock = afs_flock,
39};
40
41const struct inode_operations afs_file_inode_operations = {
42 .getattr = afs_getattr,
43 .setattr = afs_setattr,
44 .permission = afs_permission,
45 .listxattr = afs_listxattr,
46};
47
48const struct address_space_operations afs_fs_aops = {
49 .readpage = afs_readpage,
50 .readpages = afs_readpages,
51 .set_page_dirty = afs_set_page_dirty,
52 .launder_page = afs_launder_page,
53 .releasepage = afs_releasepage,
54 .invalidatepage = afs_invalidatepage,
55 .write_begin = afs_write_begin,
56 .write_end = afs_write_end,
57 .writepage = afs_writepage,
58 .writepages = afs_writepages,
59};
60
61static const struct vm_operations_struct afs_vm_ops = {
62 .fault = filemap_fault,
63 .map_pages = filemap_map_pages,
64 .page_mkwrite = afs_page_mkwrite,
65};
66
67/*
68 * Discard a pin on a writeback key.
69 */
70void afs_put_wb_key(struct afs_wb_key *wbk)
71{
72 if (wbk && refcount_dec_and_test(&wbk->usage)) {
73 key_put(wbk->key);
74 kfree(wbk);
75 }
76}
77
78/*
79 * Cache key for writeback.
80 */
81int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
82{
83 struct afs_wb_key *wbk, *p;
84
85 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
86 if (!wbk)
87 return -ENOMEM;
88 refcount_set(&wbk->usage, 2);
89 wbk->key = af->key;
90
91 spin_lock(&vnode->wb_lock);
92 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
93 if (p->key == wbk->key)
94 goto found;
95 }
96
97 key_get(wbk->key);
98 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
99 spin_unlock(&vnode->wb_lock);
100 af->wb = wbk;
101 return 0;
102
103found:
104 refcount_inc(&p->usage);
105 spin_unlock(&vnode->wb_lock);
106 af->wb = p;
107 kfree(wbk);
108 return 0;
109}
110
111/*
112 * open an AFS file or directory and attach a key to it
113 */
114int afs_open(struct inode *inode, struct file *file)
115{
116 struct afs_vnode *vnode = AFS_FS_I(inode);
117 struct afs_file *af;
118 struct key *key;
119 int ret;
120
121 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
122
123 key = afs_request_key(vnode->volume->cell);
124 if (IS_ERR(key)) {
125 ret = PTR_ERR(key);
126 goto error;
127 }
128
129 af = kzalloc(sizeof(*af), GFP_KERNEL);
130 if (!af) {
131 ret = -ENOMEM;
132 goto error_key;
133 }
134 af->key = key;
135
136 ret = afs_validate(vnode, key);
137 if (ret < 0)
138 goto error_af;
139
140 if (file->f_mode & FMODE_WRITE) {
141 ret = afs_cache_wb_key(vnode, af);
142 if (ret < 0)
143 goto error_af;
144 }
145
146 if (file->f_flags & O_TRUNC)
147 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
148
149 file->private_data = af;
150 _leave(" = 0");
151 return 0;
152
153error_af:
154 kfree(af);
155error_key:
156 key_put(key);
157error:
158 _leave(" = %d", ret);
159 return ret;
160}
161
162/*
163 * release an AFS file or directory and discard its key
164 */
165int afs_release(struct inode *inode, struct file *file)
166{
167 struct afs_vnode *vnode = AFS_FS_I(inode);
168 struct afs_file *af = file->private_data;
169 int ret = 0;
170
171 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
172
173 if ((file->f_mode & FMODE_WRITE))
174 ret = vfs_fsync(file, 0);
175
176 file->private_data = NULL;
177 if (af->wb)
178 afs_put_wb_key(af->wb);
179 key_put(af->key);
180 kfree(af);
181 afs_prune_wb_keys(vnode);
182 _leave(" = %d", ret);
183 return ret;
184}
185
186/*
187 * Dispose of a ref to a read record.
188 */
189void afs_put_read(struct afs_read *req)
190{
191 int i;
192
193 if (refcount_dec_and_test(&req->usage)) {
194 if (req->pages) {
195 for (i = 0; i < req->nr_pages; i++)
196 if (req->pages[i])
197 put_page(req->pages[i]);
198 if (req->pages != req->array)
199 kfree(req->pages);
200 }
201 kfree(req);
202 }
203}
204
205#ifdef CONFIG_AFS_FSCACHE
206/*
207 * deal with notification that a page was read from the cache
208 */
209static void afs_file_readpage_read_complete(struct page *page,
210 void *data,
211 int error)
212{
213 _enter("%p,%p,%d", page, data, error);
214
215 /* if the read completes with an error, we just unlock the page and let
216 * the VM reissue the readpage */
217 if (!error)
218 SetPageUptodate(page);
219 unlock_page(page);
220}
221#endif
222
223static void afs_fetch_data_success(struct afs_operation *op)
224{
225 struct afs_vnode *vnode = op->file[0].vnode;
226
227 _enter("op=%08x", op->debug_id);
228 afs_vnode_commit_status(op, &op->file[0]);
229 afs_stat_v(vnode, n_fetches);
230 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
231}
232
233static void afs_fetch_data_put(struct afs_operation *op)
234{
235 afs_put_read(op->fetch.req);
236}
237
238static const struct afs_operation_ops afs_fetch_data_operation = {
239 .issue_afs_rpc = afs_fs_fetch_data,
240 .issue_yfs_rpc = yfs_fs_fetch_data,
241 .success = afs_fetch_data_success,
242 .aborted = afs_check_for_remote_deletion,
243 .put = afs_fetch_data_put,
244};
245
246/*
247 * Fetch file data from the volume.
248 */
249int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
250{
251 struct afs_operation *op;
252
253 _enter("%s{%llx:%llu.%u},%x,,,",
254 vnode->volume->name,
255 vnode->fid.vid,
256 vnode->fid.vnode,
257 vnode->fid.unique,
258 key_serial(key));
259
260 op = afs_alloc_operation(key, vnode->volume);
261 if (IS_ERR(op))
262 return PTR_ERR(op);
263
264 afs_op_set_vnode(op, 0, vnode);
265
266 op->fetch.req = afs_get_read(req);
267 op->ops = &afs_fetch_data_operation;
268 return afs_do_sync_operation(op);
269}
270
271/*
272 * read page from file, directory or symlink, given a key to use
273 */
274int afs_page_filler(void *data, struct page *page)
275{
276 struct inode *inode = page->mapping->host;
277 struct afs_vnode *vnode = AFS_FS_I(inode);
278 struct afs_read *req;
279 struct key *key = data;
280 int ret;
281
282 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
283
284 BUG_ON(!PageLocked(page));
285
286 ret = -ESTALE;
287 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
288 goto error;
289
290 /* is it cached? */
291#ifdef CONFIG_AFS_FSCACHE
292 ret = fscache_read_or_alloc_page(vnode->cache,
293 page,
294 afs_file_readpage_read_complete,
295 NULL,
296 GFP_KERNEL);
297#else
298 ret = -ENOBUFS;
299#endif
300 switch (ret) {
301 /* read BIO submitted (page in cache) */
302 case 0:
303 break;
304
305 /* page not yet cached */
306 case -ENODATA:
307 _debug("cache said ENODATA");
308 goto go_on;
309
310 /* page will not be cached */
311 case -ENOBUFS:
312 _debug("cache said ENOBUFS");
313
314 fallthrough;
315 default:
316 go_on:
317 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
318 if (!req)
319 goto enomem;
320
321 /* We request a full page. If the page is a partial one at the
322 * end of the file, the server will return a short read and the
323 * unmarshalling code will clear the unfilled space.
324 */
325 refcount_set(&req->usage, 1);
326 req->pos = (loff_t)page->index << PAGE_SHIFT;
327 req->len = PAGE_SIZE;
328 req->nr_pages = 1;
329 req->pages = req->array;
330 req->pages[0] = page;
331 get_page(page);
332
333 /* read the contents of the file from the server into the
334 * page */
335 ret = afs_fetch_data(vnode, key, req);
336 afs_put_read(req);
337
338 if (ret < 0) {
339 if (ret == -ENOENT) {
340 _debug("got NOENT from server"
341 " - marking file deleted and stale");
342 set_bit(AFS_VNODE_DELETED, &vnode->flags);
343 ret = -ESTALE;
344 }
345
346#ifdef CONFIG_AFS_FSCACHE
347 fscache_uncache_page(vnode->cache, page);
348#endif
349 BUG_ON(PageFsCache(page));
350
351 if (ret == -EINTR ||
352 ret == -ENOMEM ||
353 ret == -ERESTARTSYS ||
354 ret == -EAGAIN)
355 goto error;
356 goto io_error;
357 }
358
359 SetPageUptodate(page);
360
361 /* send the page to the cache */
362#ifdef CONFIG_AFS_FSCACHE
363 if (PageFsCache(page) &&
364 fscache_write_page(vnode->cache, page, vnode->status.size,
365 GFP_KERNEL) != 0) {
366 fscache_uncache_page(vnode->cache, page);
367 BUG_ON(PageFsCache(page));
368 }
369#endif
370 unlock_page(page);
371 }
372
373 _leave(" = 0");
374 return 0;
375
376io_error:
377 SetPageError(page);
378 goto error;
379enomem:
380 ret = -ENOMEM;
381error:
382 unlock_page(page);
383 _leave(" = %d", ret);
384 return ret;
385}
386
387/*
388 * read page from file, directory or symlink, given a file to nominate the key
389 * to be used
390 */
391static int afs_readpage(struct file *file, struct page *page)
392{
393 struct key *key;
394 int ret;
395
396 if (file) {
397 key = afs_file_key(file);
398 ASSERT(key != NULL);
399 ret = afs_page_filler(key, page);
400 } else {
401 struct inode *inode = page->mapping->host;
402 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
403 if (IS_ERR(key)) {
404 ret = PTR_ERR(key);
405 } else {
406 ret = afs_page_filler(key, page);
407 key_put(key);
408 }
409 }
410 return ret;
411}
412
413/*
414 * Make pages available as they're filled.
415 */
416static void afs_readpages_page_done(struct afs_read *req)
417{
418#ifdef CONFIG_AFS_FSCACHE
419 struct afs_vnode *vnode = req->vnode;
420#endif
421 struct page *page = req->pages[req->index];
422
423 req->pages[req->index] = NULL;
424 SetPageUptodate(page);
425
426 /* send the page to the cache */
427#ifdef CONFIG_AFS_FSCACHE
428 if (PageFsCache(page) &&
429 fscache_write_page(vnode->cache, page, vnode->status.size,
430 GFP_KERNEL) != 0) {
431 fscache_uncache_page(vnode->cache, page);
432 BUG_ON(PageFsCache(page));
433 }
434#endif
435 unlock_page(page);
436 put_page(page);
437}
438
439/*
440 * Read a contiguous set of pages.
441 */
442static int afs_readpages_one(struct file *file, struct address_space *mapping,
443 struct list_head *pages)
444{
445 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
446 struct afs_read *req;
447 struct list_head *p;
448 struct page *first, *page;
449 struct key *key = afs_file_key(file);
450 pgoff_t index;
451 int ret, n, i;
452
453 /* Count the number of contiguous pages at the front of the list. Note
454 * that the list goes prev-wards rather than next-wards.
455 */
456 first = lru_to_page(pages);
457 index = first->index + 1;
458 n = 1;
459 for (p = first->lru.prev; p != pages; p = p->prev) {
460 page = list_entry(p, struct page, lru);
461 if (page->index != index)
462 break;
463 index++;
464 n++;
465 }
466
467 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
468 if (!req)
469 return -ENOMEM;
470
471 refcount_set(&req->usage, 1);
472 req->vnode = vnode;
473 req->page_done = afs_readpages_page_done;
474 req->pos = first->index;
475 req->pos <<= PAGE_SHIFT;
476 req->pages = req->array;
477
478 /* Transfer the pages to the request. We add them in until one fails
479 * to add to the LRU and then we stop (as that'll make a hole in the
480 * contiguous run.
481 *
482 * Note that it's possible for the file size to change whilst we're
483 * doing this, but we rely on the server returning less than we asked
484 * for if the file shrank. We also rely on this to deal with a partial
485 * page at the end of the file.
486 */
487 do {
488 page = lru_to_page(pages);
489 list_del(&page->lru);
490 index = page->index;
491 if (add_to_page_cache_lru(page, mapping, index,
492 readahead_gfp_mask(mapping))) {
493#ifdef CONFIG_AFS_FSCACHE
494 fscache_uncache_page(vnode->cache, page);
495#endif
496 put_page(page);
497 break;
498 }
499
500 req->pages[req->nr_pages++] = page;
501 req->len += PAGE_SIZE;
502 } while (req->nr_pages < n);
503
504 if (req->nr_pages == 0) {
505 kfree(req);
506 return 0;
507 }
508
509 ret = afs_fetch_data(vnode, key, req);
510 if (ret < 0)
511 goto error;
512
513 task_io_account_read(PAGE_SIZE * req->nr_pages);
514 afs_put_read(req);
515 return 0;
516
517error:
518 if (ret == -ENOENT) {
519 _debug("got NOENT from server"
520 " - marking file deleted and stale");
521 set_bit(AFS_VNODE_DELETED, &vnode->flags);
522 ret = -ESTALE;
523 }
524
525 for (i = 0; i < req->nr_pages; i++) {
526 page = req->pages[i];
527 if (page) {
528#ifdef CONFIG_AFS_FSCACHE
529 fscache_uncache_page(vnode->cache, page);
530#endif
531 SetPageError(page);
532 unlock_page(page);
533 }
534 }
535
536 afs_put_read(req);
537 return ret;
538}
539
540/*
541 * read a set of pages
542 */
543static int afs_readpages(struct file *file, struct address_space *mapping,
544 struct list_head *pages, unsigned nr_pages)
545{
546 struct key *key = afs_file_key(file);
547 struct afs_vnode *vnode;
548 int ret = 0;
549
550 _enter("{%d},{%lu},,%d",
551 key_serial(key), mapping->host->i_ino, nr_pages);
552
553 ASSERT(key != NULL);
554
555 vnode = AFS_FS_I(mapping->host);
556 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
557 _leave(" = -ESTALE");
558 return -ESTALE;
559 }
560
561 /* attempt to read as many of the pages as possible */
562#ifdef CONFIG_AFS_FSCACHE
563 ret = fscache_read_or_alloc_pages(vnode->cache,
564 mapping,
565 pages,
566 &nr_pages,
567 afs_file_readpage_read_complete,
568 NULL,
569 mapping_gfp_mask(mapping));
570#else
571 ret = -ENOBUFS;
572#endif
573
574 switch (ret) {
575 /* all pages are being read from the cache */
576 case 0:
577 BUG_ON(!list_empty(pages));
578 BUG_ON(nr_pages != 0);
579 _leave(" = 0 [reading all]");
580 return 0;
581
582 /* there were pages that couldn't be read from the cache */
583 case -ENODATA:
584 case -ENOBUFS:
585 break;
586
587 /* other error */
588 default:
589 _leave(" = %d", ret);
590 return ret;
591 }
592
593 while (!list_empty(pages)) {
594 ret = afs_readpages_one(file, mapping, pages);
595 if (ret < 0)
596 break;
597 }
598
599 _leave(" = %d [netting]", ret);
600 return ret;
601}
602
603/*
604 * invalidate part or all of a page
605 * - release a page and clean up its private data if offset is 0 (indicating
606 * the entire page)
607 */
608static void afs_invalidatepage(struct page *page, unsigned int offset,
609 unsigned int length)
610{
611 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
612 unsigned long priv;
613
614 _enter("{%lu},%u,%u", page->index, offset, length);
615
616 BUG_ON(!PageLocked(page));
617
618 /* we clean up only if the entire page is being invalidated */
619 if (offset == 0 && length == PAGE_SIZE) {
620#ifdef CONFIG_AFS_FSCACHE
621 if (PageFsCache(page)) {
622 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
623 fscache_wait_on_page_write(vnode->cache, page);
624 fscache_uncache_page(vnode->cache, page);
625 }
626#endif
627
628 if (PagePrivate(page)) {
629 priv = page_private(page);
630 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
631 page->index, priv);
632 set_page_private(page, 0);
633 ClearPagePrivate(page);
634 }
635 }
636
637 _leave("");
638}
639
640/*
641 * release a page and clean up its private state if it's not busy
642 * - return true if the page can now be released, false if not
643 */
644static int afs_releasepage(struct page *page, gfp_t gfp_flags)
645{
646 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
647 unsigned long priv;
648
649 _enter("{{%llx:%llu}[%lu],%lx},%x",
650 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
651 gfp_flags);
652
653 /* deny if page is being written to the cache and the caller hasn't
654 * elected to wait */
655#ifdef CONFIG_AFS_FSCACHE
656 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
657 _leave(" = F [cache busy]");
658 return 0;
659 }
660#endif
661
662 if (PagePrivate(page)) {
663 priv = page_private(page);
664 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
665 page->index, priv);
666 set_page_private(page, 0);
667 ClearPagePrivate(page);
668 }
669
670 /* indicate that the page can be released */
671 _leave(" = T");
672 return 1;
673}
674
675/*
676 * Handle setting up a memory mapping on an AFS file.
677 */
678static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
679{
680 int ret;
681
682 ret = generic_file_mmap(file, vma);
683 if (ret == 0)
684 vma->vm_ops = &afs_vm_ops;
685 return ret;
686}
1/* AFS filesystem file handling
2 *
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/pagemap.h>
17#include <linux/writeback.h>
18#include <linux/gfp.h>
19#include "internal.h"
20
21static int afs_readpage(struct file *file, struct page *page);
22static void afs_invalidatepage(struct page *page, unsigned long offset);
23static int afs_releasepage(struct page *page, gfp_t gfp_flags);
24static int afs_launder_page(struct page *page);
25
26static int afs_readpages(struct file *filp, struct address_space *mapping,
27 struct list_head *pages, unsigned nr_pages);
28
29const struct file_operations afs_file_operations = {
30 .open = afs_open,
31 .release = afs_release,
32 .llseek = generic_file_llseek,
33 .read = do_sync_read,
34 .write = do_sync_write,
35 .aio_read = generic_file_aio_read,
36 .aio_write = afs_file_write,
37 .mmap = generic_file_readonly_mmap,
38 .splice_read = generic_file_splice_read,
39 .fsync = afs_fsync,
40 .lock = afs_lock,
41 .flock = afs_flock,
42};
43
44const struct inode_operations afs_file_inode_operations = {
45 .getattr = afs_getattr,
46 .setattr = afs_setattr,
47 .permission = afs_permission,
48};
49
50const struct address_space_operations afs_fs_aops = {
51 .readpage = afs_readpage,
52 .readpages = afs_readpages,
53 .set_page_dirty = afs_set_page_dirty,
54 .launder_page = afs_launder_page,
55 .releasepage = afs_releasepage,
56 .invalidatepage = afs_invalidatepage,
57 .write_begin = afs_write_begin,
58 .write_end = afs_write_end,
59 .writepage = afs_writepage,
60 .writepages = afs_writepages,
61};
62
63/*
64 * open an AFS file or directory and attach a key to it
65 */
66int afs_open(struct inode *inode, struct file *file)
67{
68 struct afs_vnode *vnode = AFS_FS_I(inode);
69 struct key *key;
70 int ret;
71
72 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
73
74 key = afs_request_key(vnode->volume->cell);
75 if (IS_ERR(key)) {
76 _leave(" = %ld [key]", PTR_ERR(key));
77 return PTR_ERR(key);
78 }
79
80 ret = afs_validate(vnode, key);
81 if (ret < 0) {
82 _leave(" = %d [val]", ret);
83 return ret;
84 }
85
86 file->private_data = key;
87 _leave(" = 0");
88 return 0;
89}
90
91/*
92 * release an AFS file or directory and discard its key
93 */
94int afs_release(struct inode *inode, struct file *file)
95{
96 struct afs_vnode *vnode = AFS_FS_I(inode);
97
98 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
99
100 key_put(file->private_data);
101 _leave(" = 0");
102 return 0;
103}
104
105#ifdef CONFIG_AFS_FSCACHE
106/*
107 * deal with notification that a page was read from the cache
108 */
109static void afs_file_readpage_read_complete(struct page *page,
110 void *data,
111 int error)
112{
113 _enter("%p,%p,%d", page, data, error);
114
115 /* if the read completes with an error, we just unlock the page and let
116 * the VM reissue the readpage */
117 if (!error)
118 SetPageUptodate(page);
119 unlock_page(page);
120}
121#endif
122
123/*
124 * read page from file, directory or symlink, given a key to use
125 */
126int afs_page_filler(void *data, struct page *page)
127{
128 struct inode *inode = page->mapping->host;
129 struct afs_vnode *vnode = AFS_FS_I(inode);
130 struct key *key = data;
131 size_t len;
132 off_t offset;
133 int ret;
134
135 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
136
137 BUG_ON(!PageLocked(page));
138
139 ret = -ESTALE;
140 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
141 goto error;
142
143 /* is it cached? */
144#ifdef CONFIG_AFS_FSCACHE
145 ret = fscache_read_or_alloc_page(vnode->cache,
146 page,
147 afs_file_readpage_read_complete,
148 NULL,
149 GFP_KERNEL);
150#else
151 ret = -ENOBUFS;
152#endif
153 switch (ret) {
154 /* read BIO submitted (page in cache) */
155 case 0:
156 break;
157
158 /* page not yet cached */
159 case -ENODATA:
160 _debug("cache said ENODATA");
161 goto go_on;
162
163 /* page will not be cached */
164 case -ENOBUFS:
165 _debug("cache said ENOBUFS");
166 default:
167 go_on:
168 offset = page->index << PAGE_CACHE_SHIFT;
169 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
170
171 /* read the contents of the file from the server into the
172 * page */
173 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
174 if (ret < 0) {
175 if (ret == -ENOENT) {
176 _debug("got NOENT from server"
177 " - marking file deleted and stale");
178 set_bit(AFS_VNODE_DELETED, &vnode->flags);
179 ret = -ESTALE;
180 }
181
182#ifdef CONFIG_AFS_FSCACHE
183 fscache_uncache_page(vnode->cache, page);
184#endif
185 BUG_ON(PageFsCache(page));
186 goto error;
187 }
188
189 SetPageUptodate(page);
190
191 /* send the page to the cache */
192#ifdef CONFIG_AFS_FSCACHE
193 if (PageFsCache(page) &&
194 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
195 fscache_uncache_page(vnode->cache, page);
196 BUG_ON(PageFsCache(page));
197 }
198#endif
199 unlock_page(page);
200 }
201
202 _leave(" = 0");
203 return 0;
204
205error:
206 SetPageError(page);
207 unlock_page(page);
208 _leave(" = %d", ret);
209 return ret;
210}
211
212/*
213 * read page from file, directory or symlink, given a file to nominate the key
214 * to be used
215 */
216static int afs_readpage(struct file *file, struct page *page)
217{
218 struct key *key;
219 int ret;
220
221 if (file) {
222 key = file->private_data;
223 ASSERT(key != NULL);
224 ret = afs_page_filler(key, page);
225 } else {
226 struct inode *inode = page->mapping->host;
227 key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
228 if (IS_ERR(key)) {
229 ret = PTR_ERR(key);
230 } else {
231 ret = afs_page_filler(key, page);
232 key_put(key);
233 }
234 }
235 return ret;
236}
237
238/*
239 * read a set of pages
240 */
241static int afs_readpages(struct file *file, struct address_space *mapping,
242 struct list_head *pages, unsigned nr_pages)
243{
244 struct key *key = file->private_data;
245 struct afs_vnode *vnode;
246 int ret = 0;
247
248 _enter("{%d},{%lu},,%d",
249 key_serial(key), mapping->host->i_ino, nr_pages);
250
251 ASSERT(key != NULL);
252
253 vnode = AFS_FS_I(mapping->host);
254 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
255 _leave(" = -ESTALE");
256 return -ESTALE;
257 }
258
259 /* attempt to read as many of the pages as possible */
260#ifdef CONFIG_AFS_FSCACHE
261 ret = fscache_read_or_alloc_pages(vnode->cache,
262 mapping,
263 pages,
264 &nr_pages,
265 afs_file_readpage_read_complete,
266 NULL,
267 mapping_gfp_mask(mapping));
268#else
269 ret = -ENOBUFS;
270#endif
271
272 switch (ret) {
273 /* all pages are being read from the cache */
274 case 0:
275 BUG_ON(!list_empty(pages));
276 BUG_ON(nr_pages != 0);
277 _leave(" = 0 [reading all]");
278 return 0;
279
280 /* there were pages that couldn't be read from the cache */
281 case -ENODATA:
282 case -ENOBUFS:
283 break;
284
285 /* other error */
286 default:
287 _leave(" = %d", ret);
288 return ret;
289 }
290
291 /* load the missing pages from the network */
292 ret = read_cache_pages(mapping, pages, afs_page_filler, key);
293
294 _leave(" = %d [netting]", ret);
295 return ret;
296}
297
298/*
299 * write back a dirty page
300 */
301static int afs_launder_page(struct page *page)
302{
303 _enter("{%lu}", page->index);
304
305 return 0;
306}
307
308/*
309 * invalidate part or all of a page
310 * - release a page and clean up its private data if offset is 0 (indicating
311 * the entire page)
312 */
313static void afs_invalidatepage(struct page *page, unsigned long offset)
314{
315 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
316
317 _enter("{%lu},%lu", page->index, offset);
318
319 BUG_ON(!PageLocked(page));
320
321 /* we clean up only if the entire page is being invalidated */
322 if (offset == 0) {
323#ifdef CONFIG_AFS_FSCACHE
324 if (PageFsCache(page)) {
325 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
326 fscache_wait_on_page_write(vnode->cache, page);
327 fscache_uncache_page(vnode->cache, page);
328 }
329#endif
330
331 if (PagePrivate(page)) {
332 if (wb && !PageWriteback(page)) {
333 set_page_private(page, 0);
334 afs_put_writeback(wb);
335 }
336
337 if (!page_private(page))
338 ClearPagePrivate(page);
339 }
340 }
341
342 _leave("");
343}
344
345/*
346 * release a page and clean up its private state if it's not busy
347 * - return true if the page can now be released, false if not
348 */
349static int afs_releasepage(struct page *page, gfp_t gfp_flags)
350{
351 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
352 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
353
354 _enter("{{%x:%u}[%lu],%lx},%x",
355 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
356 gfp_flags);
357
358 /* deny if page is being written to the cache and the caller hasn't
359 * elected to wait */
360#ifdef CONFIG_AFS_FSCACHE
361 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
362 _leave(" = F [cache busy]");
363 return 0;
364 }
365#endif
366
367 if (PagePrivate(page)) {
368 if (wb) {
369 set_page_private(page, 0);
370 afs_put_writeback(wb);
371 }
372 ClearPagePrivate(page);
373 }
374
375 /* indicate that the page can be released */
376 _leave(" = T");
377 return 1;
378}