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 int offset,
23 unsigned int length);
24static int afs_releasepage(struct page *page, gfp_t gfp_flags);
25static int afs_launder_page(struct page *page);
26
27static int afs_readpages(struct file *filp, struct address_space *mapping,
28 struct list_head *pages, unsigned nr_pages);
29
30const struct file_operations afs_file_operations = {
31 .open = afs_open,
32 .release = afs_release,
33 .llseek = generic_file_llseek,
34 .read = do_sync_read,
35 .write = do_sync_write,
36 .aio_read = generic_file_aio_read,
37 .aio_write = afs_file_write,
38 .mmap = generic_file_readonly_mmap,
39 .splice_read = generic_file_splice_read,
40 .fsync = afs_fsync,
41 .lock = afs_lock,
42 .flock = afs_flock,
43};
44
45const struct inode_operations afs_file_inode_operations = {
46 .getattr = afs_getattr,
47 .setattr = afs_setattr,
48 .permission = afs_permission,
49};
50
51const 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,
62};
63
64/*
65 * open an AFS file or directory and attach a key to it
66 */
67int afs_open(struct inode *inode, struct file *file)
68{
69 struct afs_vnode *vnode = AFS_FS_I(inode);
70 struct key *key;
71 int ret;
72
73 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
74
75 key = afs_request_key(vnode->volume->cell);
76 if (IS_ERR(key)) {
77 _leave(" = %ld [key]", PTR_ERR(key));
78 return PTR_ERR(key);
79 }
80
81 ret = afs_validate(vnode, key);
82 if (ret < 0) {
83 _leave(" = %d [val]", ret);
84 return ret;
85 }
86
87 file->private_data = key;
88 _leave(" = 0");
89 return 0;
90}
91
92/*
93 * release an AFS file or directory and discard its key
94 */
95int afs_release(struct inode *inode, struct file *file)
96{
97 struct afs_vnode *vnode = AFS_FS_I(inode);
98
99 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
100
101 key_put(file->private_data);
102 _leave(" = 0");
103 return 0;
104}
105
106#ifdef CONFIG_AFS_FSCACHE
107/*
108 * deal with notification that a page was read from the cache
109 */
110static void afs_file_readpage_read_complete(struct page *page,
111 void *data,
112 int error)
113{
114 _enter("%p,%p,%d", page, data, error);
115
116 /* if the read completes with an error, we just unlock the page and let
117 * the VM reissue the readpage */
118 if (!error)
119 SetPageUptodate(page);
120 unlock_page(page);
121}
122#endif
123
124/*
125 * read page from file, directory or symlink, given a key to use
126 */
127int afs_page_filler(void *data, struct page *page)
128{
129 struct inode *inode = page->mapping->host;
130 struct afs_vnode *vnode = AFS_FS_I(inode);
131 struct key *key = data;
132 size_t len;
133 off_t offset;
134 int ret;
135
136 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
137
138 BUG_ON(!PageLocked(page));
139
140 ret = -ESTALE;
141 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
142 goto error;
143
144 /* is it cached? */
145#ifdef CONFIG_AFS_FSCACHE
146 ret = fscache_read_or_alloc_page(vnode->cache,
147 page,
148 afs_file_readpage_read_complete,
149 NULL,
150 GFP_KERNEL);
151#else
152 ret = -ENOBUFS;
153#endif
154 switch (ret) {
155 /* read BIO submitted (page in cache) */
156 case 0:
157 break;
158
159 /* page not yet cached */
160 case -ENODATA:
161 _debug("cache said ENODATA");
162 goto go_on;
163
164 /* page will not be cached */
165 case -ENOBUFS:
166 _debug("cache said ENOBUFS");
167 default:
168 go_on:
169 offset = page->index << PAGE_CACHE_SHIFT;
170 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
171
172 /* read the contents of the file from the server into the
173 * page */
174 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
175 if (ret < 0) {
176 if (ret == -ENOENT) {
177 _debug("got NOENT from server"
178 " - marking file deleted and stale");
179 set_bit(AFS_VNODE_DELETED, &vnode->flags);
180 ret = -ESTALE;
181 }
182
183#ifdef CONFIG_AFS_FSCACHE
184 fscache_uncache_page(vnode->cache, page);
185#endif
186 BUG_ON(PageFsCache(page));
187 goto error;
188 }
189
190 SetPageUptodate(page);
191
192 /* send the page to the cache */
193#ifdef CONFIG_AFS_FSCACHE
194 if (PageFsCache(page) &&
195 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
196 fscache_uncache_page(vnode->cache, page);
197 BUG_ON(PageFsCache(page));
198 }
199#endif
200 unlock_page(page);
201 }
202
203 _leave(" = 0");
204 return 0;
205
206error:
207 SetPageError(page);
208 unlock_page(page);
209 _leave(" = %d", ret);
210 return ret;
211}
212
213/*
214 * read page from file, directory or symlink, given a file to nominate the key
215 * to be used
216 */
217static int afs_readpage(struct file *file, struct page *page)
218{
219 struct key *key;
220 int ret;
221
222 if (file) {
223 key = file->private_data;
224 ASSERT(key != NULL);
225 ret = afs_page_filler(key, page);
226 } else {
227 struct inode *inode = page->mapping->host;
228 key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
229 if (IS_ERR(key)) {
230 ret = PTR_ERR(key);
231 } else {
232 ret = afs_page_filler(key, page);
233 key_put(key);
234 }
235 }
236 return ret;
237}
238
239/*
240 * read a set of pages
241 */
242static int afs_readpages(struct file *file, struct address_space *mapping,
243 struct list_head *pages, unsigned nr_pages)
244{
245 struct key *key = file->private_data;
246 struct afs_vnode *vnode;
247 int ret = 0;
248
249 _enter("{%d},{%lu},,%d",
250 key_serial(key), mapping->host->i_ino, nr_pages);
251
252 ASSERT(key != NULL);
253
254 vnode = AFS_FS_I(mapping->host);
255 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
256 _leave(" = -ESTALE");
257 return -ESTALE;
258 }
259
260 /* attempt to read as many of the pages as possible */
261#ifdef CONFIG_AFS_FSCACHE
262 ret = fscache_read_or_alloc_pages(vnode->cache,
263 mapping,
264 pages,
265 &nr_pages,
266 afs_file_readpage_read_complete,
267 NULL,
268 mapping_gfp_mask(mapping));
269#else
270 ret = -ENOBUFS;
271#endif
272
273 switch (ret) {
274 /* all pages are being read from the cache */
275 case 0:
276 BUG_ON(!list_empty(pages));
277 BUG_ON(nr_pages != 0);
278 _leave(" = 0 [reading all]");
279 return 0;
280
281 /* there were pages that couldn't be read from the cache */
282 case -ENODATA:
283 case -ENOBUFS:
284 break;
285
286 /* other error */
287 default:
288 _leave(" = %d", ret);
289 return ret;
290 }
291
292 /* load the missing pages from the network */
293 ret = read_cache_pages(mapping, pages, afs_page_filler, key);
294
295 _leave(" = %d [netting]", ret);
296 return ret;
297}
298
299/*
300 * write back a dirty page
301 */
302static int afs_launder_page(struct page *page)
303{
304 _enter("{%lu}", page->index);
305
306 return 0;
307}
308
309/*
310 * invalidate part or all of a page
311 * - release a page and clean up its private data if offset is 0 (indicating
312 * the entire page)
313 */
314static void afs_invalidatepage(struct page *page, unsigned int offset,
315 unsigned int length)
316{
317 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
318
319 _enter("{%lu},%u,%u", page->index, offset, length);
320
321 BUG_ON(!PageLocked(page));
322
323 /* we clean up only if the entire page is being invalidated */
324 if (offset == 0 && length == PAGE_CACHE_SIZE) {
325#ifdef CONFIG_AFS_FSCACHE
326 if (PageFsCache(page)) {
327 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
328 fscache_wait_on_page_write(vnode->cache, page);
329 fscache_uncache_page(vnode->cache, page);
330 }
331#endif
332
333 if (PagePrivate(page)) {
334 if (wb && !PageWriteback(page)) {
335 set_page_private(page, 0);
336 afs_put_writeback(wb);
337 }
338
339 if (!page_private(page))
340 ClearPagePrivate(page);
341 }
342 }
343
344 _leave("");
345}
346
347/*
348 * release a page and clean up its private state if it's not busy
349 * - return true if the page can now be released, false if not
350 */
351static int afs_releasepage(struct page *page, gfp_t gfp_flags)
352{
353 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
354 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
355
356 _enter("{{%x:%u}[%lu],%lx},%x",
357 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
358 gfp_flags);
359
360 /* deny if page is being written to the cache and the caller hasn't
361 * elected to wait */
362#ifdef CONFIG_AFS_FSCACHE
363 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
364 _leave(" = F [cache busy]");
365 return 0;
366 }
367#endif
368
369 if (PagePrivate(page)) {
370 if (wb) {
371 set_page_private(page, 0);
372 afs_put_writeback(wb);
373 }
374 ClearPagePrivate(page);
375 }
376
377 /* indicate that the page can be released */
378 _leave(" = T");
379 return 1;
380}