1 /* Storage object read/write
2 *
3 * Copyright (C) 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12 #include <linux/mount.h>
13 #include <linux/slab.h>
14 #include <linux/file.h>
15 #include <linux/swap.h>
16 #include "internal.h"
17
18 /*
19 * detect wake up events generated by the unlocking of pages in which we're
20 * interested
21 * - we use this to detect read completion of backing pages
22 * - the caller holds the waitqueue lock
23 */
cachefiles_read_waiter(wait_queue_entry_t * wait,unsigned mode,int sync,void * _key)24 static int cachefiles_read_waiter(wait_queue_entry_t *wait, unsigned mode,
25 int sync, void *_key)
26 {
27 struct cachefiles_one_read *monitor =
28 container_of(wait, struct cachefiles_one_read, monitor);
29 struct cachefiles_object *object;
30 struct fscache_retrieval *op = monitor->op;
31 struct wait_bit_key *key = _key;
32 struct page *page = wait->private;
33
34 ASSERT(key);
35
36 _enter("{%lu},%u,%d,{%p,%u}",
37 monitor->netfs_page->index, mode, sync,
38 key->flags, key->bit_nr);
39
40 if (key->flags != &page->flags ||
41 key->bit_nr != PG_locked)
42 return 0;
43
44 _debug("--- monitor %p %lx ---", page, page->flags);
45
46 if (!PageUptodate(page) && !PageError(page)) {
47 /* unlocked, not uptodate and not erronous? */
48 _debug("page probably truncated");
49 }
50
51 /* remove from the waitqueue */
52 list_del(&wait->entry);
53
54 /* move onto the action list and queue for FS-Cache thread pool */
55 ASSERT(op);
56
57 /* We need to temporarily bump the usage count as we don't own a ref
58 * here otherwise cachefiles_read_copier() may free the op between the
59 * monitor being enqueued on the op->to_do list and the op getting
60 * enqueued on the work queue.
61 */
62 fscache_get_retrieval(op);
63
64 object = container_of(op->op.object, struct cachefiles_object, fscache);
65 spin_lock(&object->work_lock);
66 list_add_tail(&monitor->op_link, &op->to_do);
67 fscache_enqueue_retrieval(op);
68 spin_unlock(&object->work_lock);
69
70 fscache_put_retrieval(op);
71 return 0;
72 }
73
74 /*
75 * handle a probably truncated page
76 * - check to see if the page is still relevant and reissue the read if
77 * possible
78 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
79 * must wait again and 0 if successful
80 */
cachefiles_read_reissue(struct cachefiles_object * object,struct cachefiles_one_read * monitor)81 static int cachefiles_read_reissue(struct cachefiles_object *object,
82 struct cachefiles_one_read *monitor)
83 {
84 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
85 struct page *backpage = monitor->back_page, *backpage2;
86 int ret;
87
88 _enter("{ino=%lx},{%lx,%lx}",
89 d_backing_inode(object->backer)->i_ino,
90 backpage->index, backpage->flags);
91
92 /* skip if the page was truncated away completely */
93 if (backpage->mapping != bmapping) {
94 _leave(" = -ENODATA [mapping]");
95 return -ENODATA;
96 }
97
98 backpage2 = find_get_page(bmapping, backpage->index);
99 if (!backpage2) {
100 _leave(" = -ENODATA [gone]");
101 return -ENODATA;
102 }
103
104 if (backpage != backpage2) {
105 put_page(backpage2);
106 _leave(" = -ENODATA [different]");
107 return -ENODATA;
108 }
109
110 /* the page is still there and we already have a ref on it, so we don't
111 * need a second */
112 put_page(backpage2);
113
114 INIT_LIST_HEAD(&monitor->op_link);
115 add_page_wait_queue(backpage, &monitor->monitor);
116
117 if (trylock_page(backpage)) {
118 ret = -EIO;
119 if (PageError(backpage))
120 goto unlock_discard;
121 ret = 0;
122 if (PageUptodate(backpage))
123 goto unlock_discard;
124
125 _debug("reissue read");
126 ret = bmapping->a_ops->readpage(NULL, backpage);
127 if (ret < 0)
128 goto discard;
129 }
130
131 /* but the page may have been read before the monitor was installed, so
132 * the monitor may miss the event - so we have to ensure that we do get
133 * one in such a case */
134 if (trylock_page(backpage)) {
135 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
136 unlock_page(backpage);
137 }
138
139 /* it'll reappear on the todo list */
140 _leave(" = -EINPROGRESS");
141 return -EINPROGRESS;
142
143 unlock_discard:
144 unlock_page(backpage);
145 discard:
146 spin_lock_irq(&object->work_lock);
147 list_del(&monitor->op_link);
148 spin_unlock_irq(&object->work_lock);
149 _leave(" = %d", ret);
150 return ret;
151 }
152
153 /*
154 * copy data from backing pages to netfs pages to complete a read operation
155 * - driven by FS-Cache's thread pool
156 */
cachefiles_read_copier(struct fscache_operation * _op)157 static void cachefiles_read_copier(struct fscache_operation *_op)
158 {
159 struct cachefiles_one_read *monitor;
160 struct cachefiles_object *object;
161 struct fscache_retrieval *op;
162 int error, max;
163
164 op = container_of(_op, struct fscache_retrieval, op);
165 object = container_of(op->op.object,
166 struct cachefiles_object, fscache);
167
168 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
169
170 max = 8;
171 spin_lock_irq(&object->work_lock);
172
173 while (!list_empty(&op->to_do)) {
174 monitor = list_entry(op->to_do.next,
175 struct cachefiles_one_read, op_link);
176 list_del(&monitor->op_link);
177
178 spin_unlock_irq(&object->work_lock);
179
180 _debug("- copy {%lu}", monitor->back_page->index);
181
182 recheck:
183 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
184 &object->fscache.cookie->flags)) {
185 error = -ESTALE;
186 } else if (PageUptodate(monitor->back_page)) {
187 copy_highpage(monitor->netfs_page, monitor->back_page);
188 fscache_mark_page_cached(monitor->op,
189 monitor->netfs_page);
190 error = 0;
191 } else if (!PageError(monitor->back_page)) {
192 /* the page has probably been truncated */
193 error = cachefiles_read_reissue(object, monitor);
194 if (error == -EINPROGRESS)
195 goto next;
196 goto recheck;
197 } else {
198 cachefiles_io_error_obj(
199 object,
200 "Readpage failed on backing file %lx",
201 (unsigned long) monitor->back_page->flags);
202 error = -EIO;
203 }
204
205 put_page(monitor->back_page);
206
207 fscache_end_io(op, monitor->netfs_page, error);
208 put_page(monitor->netfs_page);
209 fscache_retrieval_complete(op, 1);
210 fscache_put_retrieval(op);
211 kfree(monitor);
212
213 next:
214 /* let the thread pool have some air occasionally */
215 max--;
216 if (max < 0 || need_resched()) {
217 if (!list_empty(&op->to_do))
218 fscache_enqueue_retrieval(op);
219 _leave(" [maxed out]");
220 return;
221 }
222
223 spin_lock_irq(&object->work_lock);
224 }
225
226 spin_unlock_irq(&object->work_lock);
227 _leave("");
228 }
229
230 /*
231 * read the corresponding page to the given set from the backing file
232 * - an uncertain page is simply discarded, to be tried again another time
233 */
cachefiles_read_backing_file_one(struct cachefiles_object * object,struct fscache_retrieval * op,struct page * netpage)234 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
235 struct fscache_retrieval *op,
236 struct page *netpage)
237 {
238 struct cachefiles_one_read *monitor;
239 struct address_space *bmapping;
240 struct page *newpage, *backpage;
241 int ret;
242
243 _enter("");
244
245 _debug("read back %p{%lu,%d}",
246 netpage, netpage->index, page_count(netpage));
247
248 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
249 if (!monitor)
250 goto nomem;
251
252 monitor->netfs_page = netpage;
253 monitor->op = fscache_get_retrieval(op);
254
255 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
256
257 /* attempt to get hold of the backing page */
258 bmapping = d_backing_inode(object->backer)->i_mapping;
259 newpage = NULL;
260
261 for (;;) {
262 backpage = find_get_page(bmapping, netpage->index);
263 if (backpage)
264 goto backing_page_already_present;
265
266 if (!newpage) {
267 newpage = __page_cache_alloc(cachefiles_gfp);
268 if (!newpage)
269 goto nomem_monitor;
270 }
271
272 ret = add_to_page_cache_lru(newpage, bmapping,
273 netpage->index, cachefiles_gfp);
274 if (ret == 0)
275 goto installed_new_backing_page;
276 if (ret != -EEXIST)
277 goto nomem_page;
278 }
279
280 /* we've installed a new backing page, so now we need to start
281 * it reading */
282 installed_new_backing_page:
283 _debug("- new %p", newpage);
284
285 backpage = newpage;
286 newpage = NULL;
287
288 read_backing_page:
289 ret = bmapping->a_ops->readpage(NULL, backpage);
290 if (ret < 0)
291 goto read_error;
292
293 /* set the monitor to transfer the data across */
294 monitor_backing_page:
295 _debug("- monitor add");
296
297 /* install the monitor */
298 get_page(monitor->netfs_page);
299 get_page(backpage);
300 monitor->back_page = backpage;
301 monitor->monitor.private = backpage;
302 add_page_wait_queue(backpage, &monitor->monitor);
303 monitor = NULL;
304
305 /* but the page may have been read before the monitor was installed, so
306 * the monitor may miss the event - so we have to ensure that we do get
307 * one in such a case */
308 if (trylock_page(backpage)) {
309 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
310 unlock_page(backpage);
311 }
312 goto success;
313
314 /* if the backing page is already present, it can be in one of
315 * three states: read in progress, read failed or read okay */
316 backing_page_already_present:
317 _debug("- present");
318
319 if (newpage) {
320 put_page(newpage);
321 newpage = NULL;
322 }
323
324 if (PageError(backpage))
325 goto io_error;
326
327 if (PageUptodate(backpage))
328 goto backing_page_already_uptodate;
329
330 if (!trylock_page(backpage))
331 goto monitor_backing_page;
332 _debug("read %p {%lx}", backpage, backpage->flags);
333 goto read_backing_page;
334
335 /* the backing page is already up to date, attach the netfs
336 * page to the pagecache and LRU and copy the data across */
337 backing_page_already_uptodate:
338 _debug("- uptodate");
339
340 fscache_mark_page_cached(op, netpage);
341
342 copy_highpage(netpage, backpage);
343 fscache_end_io(op, netpage, 0);
344 fscache_retrieval_complete(op, 1);
345
346 success:
347 _debug("success");
348 ret = 0;
349
350 out:
351 if (backpage)
352 put_page(backpage);
353 if (monitor) {
354 fscache_put_retrieval(monitor->op);
355 kfree(monitor);
356 }
357 _leave(" = %d", ret);
358 return ret;
359
360 read_error:
361 _debug("read error %d", ret);
362 if (ret == -ENOMEM) {
363 fscache_retrieval_complete(op, 1);
364 goto out;
365 }
366 io_error:
367 cachefiles_io_error_obj(object, "Page read error on backing file");
368 fscache_retrieval_complete(op, 1);
369 ret = -ENOBUFS;
370 goto out;
371
372 nomem_page:
373 put_page(newpage);
374 nomem_monitor:
375 fscache_put_retrieval(monitor->op);
376 kfree(monitor);
377 nomem:
378 fscache_retrieval_complete(op, 1);
379 _leave(" = -ENOMEM");
380 return -ENOMEM;
381 }
382
383 /*
384 * read a page from the cache or allocate a block in which to store it
385 * - cache withdrawal is prevented by the caller
386 * - returns -EINTR if interrupted
387 * - returns -ENOMEM if ran out of memory
388 * - returns -ENOBUFS if no buffers can be made available
389 * - returns -ENOBUFS if page is beyond EOF
390 * - if the page is backed by a block in the cache:
391 * - a read will be started which will call the callback on completion
392 * - 0 will be returned
393 * - else if the page is unbacked:
394 * - the metadata will be retained
395 * - -ENODATA will be returned
396 */
cachefiles_read_or_alloc_page(struct fscache_retrieval * op,struct page * page,gfp_t gfp)397 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
398 struct page *page,
399 gfp_t gfp)
400 {
401 struct cachefiles_object *object;
402 struct cachefiles_cache *cache;
403 struct inode *inode;
404 sector_t block0, block;
405 unsigned shift;
406 int ret;
407
408 object = container_of(op->op.object,
409 struct cachefiles_object, fscache);
410 cache = container_of(object->fscache.cache,
411 struct cachefiles_cache, cache);
412
413 _enter("{%p},{%lx},,,", object, page->index);
414
415 if (!object->backer)
416 goto enobufs;
417
418 inode = d_backing_inode(object->backer);
419 ASSERT(S_ISREG(inode->i_mode));
420 ASSERT(inode->i_mapping->a_ops->bmap);
421 ASSERT(inode->i_mapping->a_ops->readpages);
422
423 /* calculate the shift required to use bmap */
424 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
425
426 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
427 op->op.flags |= FSCACHE_OP_ASYNC;
428 op->op.processor = cachefiles_read_copier;
429
430 /* we assume the absence or presence of the first block is a good
431 * enough indication for the page as a whole
432 * - TODO: don't use bmap() for this as it is _not_ actually good
433 * enough for this as it doesn't indicate errors, but it's all we've
434 * got for the moment
435 */
436 block0 = page->index;
437 block0 <<= shift;
438
439 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
440 _debug("%llx -> %llx",
441 (unsigned long long) block0,
442 (unsigned long long) block);
443
444 if (block) {
445 /* submit the apparently valid page to the backing fs to be
446 * read from disk */
447 ret = cachefiles_read_backing_file_one(object, op, page);
448 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
449 /* there's space in the cache we can use */
450 fscache_mark_page_cached(op, page);
451 fscache_retrieval_complete(op, 1);
452 ret = -ENODATA;
453 } else {
454 goto enobufs;
455 }
456
457 _leave(" = %d", ret);
458 return ret;
459
460 enobufs:
461 fscache_retrieval_complete(op, 1);
462 _leave(" = -ENOBUFS");
463 return -ENOBUFS;
464 }
465
466 /*
467 * read the corresponding pages to the given set from the backing file
468 * - any uncertain pages are simply discarded, to be tried again another time
469 */
cachefiles_read_backing_file(struct cachefiles_object * object,struct fscache_retrieval * op,struct list_head * list)470 static int cachefiles_read_backing_file(struct cachefiles_object *object,
471 struct fscache_retrieval *op,
472 struct list_head *list)
473 {
474 struct cachefiles_one_read *monitor = NULL;
475 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
476 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
477 int ret = 0;
478
479 _enter("");
480
481 list_for_each_entry_safe(netpage, _n, list, lru) {
482 list_del(&netpage->lru);
483
484 _debug("read back %p{%lu,%d}",
485 netpage, netpage->index, page_count(netpage));
486
487 if (!monitor) {
488 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
489 if (!monitor)
490 goto nomem;
491
492 monitor->op = fscache_get_retrieval(op);
493 init_waitqueue_func_entry(&monitor->monitor,
494 cachefiles_read_waiter);
495 }
496
497 for (;;) {
498 backpage = find_get_page(bmapping, netpage->index);
499 if (backpage)
500 goto backing_page_already_present;
501
502 if (!newpage) {
503 newpage = __page_cache_alloc(cachefiles_gfp);
504 if (!newpage)
505 goto nomem;
506 }
507
508 ret = add_to_page_cache_lru(newpage, bmapping,
509 netpage->index,
510 cachefiles_gfp);
511 if (ret == 0)
512 goto installed_new_backing_page;
513 if (ret != -EEXIST)
514 goto nomem;
515 }
516
517 /* we've installed a new backing page, so now we need
518 * to start it reading */
519 installed_new_backing_page:
520 _debug("- new %p", newpage);
521
522 backpage = newpage;
523 newpage = NULL;
524
525 reread_backing_page:
526 ret = bmapping->a_ops->readpage(NULL, backpage);
527 if (ret < 0)
528 goto read_error;
529
530 /* add the netfs page to the pagecache and LRU, and set the
531 * monitor to transfer the data across */
532 monitor_backing_page:
533 _debug("- monitor add");
534
535 ret = add_to_page_cache_lru(netpage, op->mapping,
536 netpage->index, cachefiles_gfp);
537 if (ret < 0) {
538 if (ret == -EEXIST) {
539 put_page(backpage);
540 backpage = NULL;
541 put_page(netpage);
542 netpage = NULL;
543 fscache_retrieval_complete(op, 1);
544 continue;
545 }
546 goto nomem;
547 }
548
549 /* install a monitor */
550 get_page(netpage);
551 monitor->netfs_page = netpage;
552
553 get_page(backpage);
554 monitor->back_page = backpage;
555 monitor->monitor.private = backpage;
556 add_page_wait_queue(backpage, &monitor->monitor);
557 monitor = NULL;
558
559 /* but the page may have been read before the monitor was
560 * installed, so the monitor may miss the event - so we have to
561 * ensure that we do get one in such a case */
562 if (trylock_page(backpage)) {
563 _debug("2unlock %p {%lx}", backpage, backpage->flags);
564 unlock_page(backpage);
565 }
566
567 put_page(backpage);
568 backpage = NULL;
569
570 put_page(netpage);
571 netpage = NULL;
572 continue;
573
574 /* if the backing page is already present, it can be in one of
575 * three states: read in progress, read failed or read okay */
576 backing_page_already_present:
577 _debug("- present %p", backpage);
578
579 if (PageError(backpage))
580 goto io_error;
581
582 if (PageUptodate(backpage))
583 goto backing_page_already_uptodate;
584
585 _debug("- not ready %p{%lx}", backpage, backpage->flags);
586
587 if (!trylock_page(backpage))
588 goto monitor_backing_page;
589
590 if (PageError(backpage)) {
591 _debug("error %lx", backpage->flags);
592 unlock_page(backpage);
593 goto io_error;
594 }
595
596 if (PageUptodate(backpage))
597 goto backing_page_already_uptodate_unlock;
598
599 /* we've locked a page that's neither up to date nor erroneous,
600 * so we need to attempt to read it again */
601 goto reread_backing_page;
602
603 /* the backing page is already up to date, attach the netfs
604 * page to the pagecache and LRU and copy the data across */
605 backing_page_already_uptodate_unlock:
606 _debug("uptodate %lx", backpage->flags);
607 unlock_page(backpage);
608 backing_page_already_uptodate:
609 _debug("- uptodate");
610
611 ret = add_to_page_cache_lru(netpage, op->mapping,
612 netpage->index, cachefiles_gfp);
613 if (ret < 0) {
614 if (ret == -EEXIST) {
615 put_page(backpage);
616 backpage = NULL;
617 put_page(netpage);
618 netpage = NULL;
619 fscache_retrieval_complete(op, 1);
620 continue;
621 }
622 goto nomem;
623 }
624
625 copy_highpage(netpage, backpage);
626
627 put_page(backpage);
628 backpage = NULL;
629
630 fscache_mark_page_cached(op, netpage);
631
632 /* the netpage is unlocked and marked up to date here */
633 fscache_end_io(op, netpage, 0);
634 put_page(netpage);
635 netpage = NULL;
636 fscache_retrieval_complete(op, 1);
637 continue;
638 }
639
640 netpage = NULL;
641
642 _debug("out");
643
644 out:
645 /* tidy up */
646 if (newpage)
647 put_page(newpage);
648 if (netpage)
649 put_page(netpage);
650 if (backpage)
651 put_page(backpage);
652 if (monitor) {
653 fscache_put_retrieval(op);
654 kfree(monitor);
655 }
656
657 list_for_each_entry_safe(netpage, _n, list, lru) {
658 list_del(&netpage->lru);
659 put_page(netpage);
660 fscache_retrieval_complete(op, 1);
661 }
662
663 _leave(" = %d", ret);
664 return ret;
665
666 nomem:
667 _debug("nomem");
668 ret = -ENOMEM;
669 goto record_page_complete;
670
671 read_error:
672 _debug("read error %d", ret);
673 if (ret == -ENOMEM)
674 goto record_page_complete;
675 io_error:
676 cachefiles_io_error_obj(object, "Page read error on backing file");
677 ret = -ENOBUFS;
678 record_page_complete:
679 fscache_retrieval_complete(op, 1);
680 goto out;
681 }
682
683 /*
684 * read a list of pages from the cache or allocate blocks in which to store
685 * them
686 */
cachefiles_read_or_alloc_pages(struct fscache_retrieval * op,struct list_head * pages,unsigned * nr_pages,gfp_t gfp)687 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
688 struct list_head *pages,
689 unsigned *nr_pages,
690 gfp_t gfp)
691 {
692 struct cachefiles_object *object;
693 struct cachefiles_cache *cache;
694 struct list_head backpages;
695 struct pagevec pagevec;
696 struct inode *inode;
697 struct page *page, *_n;
698 unsigned shift, nrbackpages;
699 int ret, ret2, space;
700
701 object = container_of(op->op.object,
702 struct cachefiles_object, fscache);
703 cache = container_of(object->fscache.cache,
704 struct cachefiles_cache, cache);
705
706 _enter("{OBJ%x,%d},,%d,,",
707 object->fscache.debug_id, atomic_read(&op->op.usage),
708 *nr_pages);
709
710 if (!object->backer)
711 goto all_enobufs;
712
713 space = 1;
714 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
715 space = 0;
716
717 inode = d_backing_inode(object->backer);
718 ASSERT(S_ISREG(inode->i_mode));
719 ASSERT(inode->i_mapping->a_ops->bmap);
720 ASSERT(inode->i_mapping->a_ops->readpages);
721
722 /* calculate the shift required to use bmap */
723 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
724
725 pagevec_init(&pagevec);
726
727 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
728 op->op.flags |= FSCACHE_OP_ASYNC;
729 op->op.processor = cachefiles_read_copier;
730
731 INIT_LIST_HEAD(&backpages);
732 nrbackpages = 0;
733
734 ret = space ? -ENODATA : -ENOBUFS;
735 list_for_each_entry_safe(page, _n, pages, lru) {
736 sector_t block0, block;
737
738 /* we assume the absence or presence of the first block is a
739 * good enough indication for the page as a whole
740 * - TODO: don't use bmap() for this as it is _not_ actually
741 * good enough for this as it doesn't indicate errors, but
742 * it's all we've got for the moment
743 */
744 block0 = page->index;
745 block0 <<= shift;
746
747 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
748 block0);
749 _debug("%llx -> %llx",
750 (unsigned long long) block0,
751 (unsigned long long) block);
752
753 if (block) {
754 /* we have data - add it to the list to give to the
755 * backing fs */
756 list_move(&page->lru, &backpages);
757 (*nr_pages)--;
758 nrbackpages++;
759 } else if (space && pagevec_add(&pagevec, page) == 0) {
760 fscache_mark_pages_cached(op, &pagevec);
761 fscache_retrieval_complete(op, 1);
762 ret = -ENODATA;
763 } else {
764 fscache_retrieval_complete(op, 1);
765 }
766 }
767
768 if (pagevec_count(&pagevec) > 0)
769 fscache_mark_pages_cached(op, &pagevec);
770
771 if (list_empty(pages))
772 ret = 0;
773
774 /* submit the apparently valid pages to the backing fs to be read from
775 * disk */
776 if (nrbackpages > 0) {
777 ret2 = cachefiles_read_backing_file(object, op, &backpages);
778 if (ret2 == -ENOMEM || ret2 == -EINTR)
779 ret = ret2;
780 }
781
782 _leave(" = %d [nr=%u%s]",
783 ret, *nr_pages, list_empty(pages) ? " empty" : "");
784 return ret;
785
786 all_enobufs:
787 fscache_retrieval_complete(op, *nr_pages);
788 return -ENOBUFS;
789 }
790
791 /*
792 * allocate a block in the cache in which to store a page
793 * - cache withdrawal is prevented by the caller
794 * - returns -EINTR if interrupted
795 * - returns -ENOMEM if ran out of memory
796 * - returns -ENOBUFS if no buffers can be made available
797 * - returns -ENOBUFS if page is beyond EOF
798 * - otherwise:
799 * - the metadata will be retained
800 * - 0 will be returned
801 */
cachefiles_allocate_page(struct fscache_retrieval * op,struct page * page,gfp_t gfp)802 int cachefiles_allocate_page(struct fscache_retrieval *op,
803 struct page *page,
804 gfp_t gfp)
805 {
806 struct cachefiles_object *object;
807 struct cachefiles_cache *cache;
808 int ret;
809
810 object = container_of(op->op.object,
811 struct cachefiles_object, fscache);
812 cache = container_of(object->fscache.cache,
813 struct cachefiles_cache, cache);
814
815 _enter("%p,{%lx},", object, page->index);
816
817 ret = cachefiles_has_space(cache, 0, 1);
818 if (ret == 0)
819 fscache_mark_page_cached(op, page);
820 else
821 ret = -ENOBUFS;
822
823 fscache_retrieval_complete(op, 1);
824 _leave(" = %d", ret);
825 return ret;
826 }
827
828 /*
829 * allocate blocks in the cache in which to store a set of pages
830 * - cache withdrawal is prevented by the caller
831 * - returns -EINTR if interrupted
832 * - returns -ENOMEM if ran out of memory
833 * - returns -ENOBUFS if some buffers couldn't be made available
834 * - returns -ENOBUFS if some pages are beyond EOF
835 * - otherwise:
836 * - -ENODATA will be returned
837 * - metadata will be retained for any page marked
838 */
cachefiles_allocate_pages(struct fscache_retrieval * op,struct list_head * pages,unsigned * nr_pages,gfp_t gfp)839 int cachefiles_allocate_pages(struct fscache_retrieval *op,
840 struct list_head *pages,
841 unsigned *nr_pages,
842 gfp_t gfp)
843 {
844 struct cachefiles_object *object;
845 struct cachefiles_cache *cache;
846 struct pagevec pagevec;
847 struct page *page;
848 int ret;
849
850 object = container_of(op->op.object,
851 struct cachefiles_object, fscache);
852 cache = container_of(object->fscache.cache,
853 struct cachefiles_cache, cache);
854
855 _enter("%p,,,%d,", object, *nr_pages);
856
857 ret = cachefiles_has_space(cache, 0, *nr_pages);
858 if (ret == 0) {
859 pagevec_init(&pagevec);
860
861 list_for_each_entry(page, pages, lru) {
862 if (pagevec_add(&pagevec, page) == 0)
863 fscache_mark_pages_cached(op, &pagevec);
864 }
865
866 if (pagevec_count(&pagevec) > 0)
867 fscache_mark_pages_cached(op, &pagevec);
868 ret = -ENODATA;
869 } else {
870 ret = -ENOBUFS;
871 }
872
873 fscache_retrieval_complete(op, *nr_pages);
874 _leave(" = %d", ret);
875 return ret;
876 }
877
878 /*
879 * request a page be stored in the cache
880 * - cache withdrawal is prevented by the caller
881 * - this request may be ignored if there's no cache block available, in which
882 * case -ENOBUFS will be returned
883 * - if the op is in progress, 0 will be returned
884 */
cachefiles_write_page(struct fscache_storage * op,struct page * page)885 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
886 {
887 struct cachefiles_object *object;
888 struct cachefiles_cache *cache;
889 struct file *file;
890 struct path path;
891 loff_t pos, eof;
892 size_t len;
893 void *data;
894 int ret = -ENOBUFS;
895
896 ASSERT(op != NULL);
897 ASSERT(page != NULL);
898
899 object = container_of(op->op.object,
900 struct cachefiles_object, fscache);
901
902 _enter("%p,%p{%lx},,,", object, page, page->index);
903
904 if (!object->backer) {
905 _leave(" = -ENOBUFS");
906 return -ENOBUFS;
907 }
908
909 ASSERT(d_is_reg(object->backer));
910
911 cache = container_of(object->fscache.cache,
912 struct cachefiles_cache, cache);
913
914 pos = (loff_t)page->index << PAGE_SHIFT;
915
916 /* We mustn't write more data than we have, so we have to beware of a
917 * partial page at EOF.
918 */
919 eof = object->fscache.store_limit_l;
920 if (pos >= eof)
921 goto error;
922
923 /* write the page to the backing filesystem and let it store it in its
924 * own time */
925 path.mnt = cache->mnt;
926 path.dentry = object->backer;
927 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
928 if (IS_ERR(file)) {
929 ret = PTR_ERR(file);
930 goto error_2;
931 }
932
933 len = PAGE_SIZE;
934 if (eof & ~PAGE_MASK) {
935 if (eof - pos < PAGE_SIZE) {
936 _debug("cut short %llx to %llx",
937 pos, eof);
938 len = eof - pos;
939 ASSERTCMP(pos + len, ==, eof);
940 }
941 }
942
943 data = kmap(page);
944 ret = __kernel_write(file, data, len, &pos);
945 kunmap(page);
946 fput(file);
947 if (ret != len)
948 goto error_eio;
949
950 _leave(" = 0");
951 return 0;
952
953 error_eio:
954 ret = -EIO;
955 error_2:
956 if (ret == -EIO)
957 cachefiles_io_error_obj(object,
958 "Write page to backing file failed");
959 error:
960 _leave(" = -ENOBUFS [%d]", ret);
961 return -ENOBUFS;
962 }
963
964 /*
965 * detach a backing block from a page
966 * - cache withdrawal is prevented by the caller
967 */
cachefiles_uncache_page(struct fscache_object * _object,struct page * page)968 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
969 __releases(&object->fscache.cookie->lock)
970 {
971 struct cachefiles_object *object;
972
973 object = container_of(_object, struct cachefiles_object, fscache);
974
975 _enter("%p,{%lu}", object, page->index);
976
977 spin_unlock(&object->fscache.cookie->lock);
978 }
979