HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / cachefiles / rdwr.c
blob5df898fd0a0aadadbd0aeb208578ed8a6aafe949
1 /* Storage object read/write
3 * Copyright (C) 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/mount.h>
13 #include <linux/slab.h>
14 #include <linux/file.h>
15 #include <linux/swap.h>
16 #include "internal.h"
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
24 static int cachefiles_read_waiter(wait_queue_t *wait, unsigned mode,
25 int sync, void *_key)
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;
34 ASSERT(key);
36 _enter("{%lu},%u,%d,{%p,%u}",
37 monitor->netfs_page->index, mode, sync,
38 key->flags, key->bit_nr);
40 if (key->flags != &page->flags ||
41 key->bit_nr != PG_locked)
42 return 0;
44 _debug("--- monitor %p %lx ---", page, page->flags);
46 if (!PageUptodate(page) && !PageError(page)) {
47 /* unlocked, not uptodate and not erronous? */
48 _debug("page probably truncated");
51 /* remove from the waitqueue */
52 list_del(&wait->task_list);
54 /* move onto the action list and queue for FS-Cache thread pool */
55 ASSERT(op);
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.
62 fscache_get_retrieval(op);
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);
70 fscache_put_retrieval(op);
71 return 0;
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
81 static int cachefiles_read_reissue(struct cachefiles_object *object,
82 struct cachefiles_one_read *monitor)
84 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
85 struct page *backpage = monitor->back_page, *backpage2;
86 int ret;
88 _enter("{ino=%lx},{%lx,%lx}",
89 d_backing_inode(object->backer)->i_ino,
90 backpage->index, backpage->flags);
92 /* skip if the page was truncated away completely */
93 if (backpage->mapping != bmapping) {
94 _leave(" = -ENODATA [mapping]");
95 return -ENODATA;
98 backpage2 = find_get_page(bmapping, backpage->index);
99 if (!backpage2) {
100 _leave(" = -ENODATA [gone]");
101 return -ENODATA;
104 if (backpage != backpage2) {
105 put_page(backpage2);
106 _leave(" = -ENODATA [different]");
107 return -ENODATA;
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);
114 INIT_LIST_HEAD(&monitor->op_link);
115 add_page_wait_queue(backpage, &monitor->monitor);
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;
125 _debug("reissue read");
126 ret = bmapping->a_ops->readpage(NULL, backpage);
127 if (ret < 0)
128 goto unlock_discard;
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);
139 /* it'll reappear on the todo list */
140 _leave(" = -EINPROGRESS");
141 return -EINPROGRESS;
143 unlock_discard:
144 unlock_page(backpage);
145 spin_lock_irq(&object->work_lock);
146 list_del(&monitor->op_link);
147 spin_unlock_irq(&object->work_lock);
148 _leave(" = %d", ret);
149 return ret;
153 * copy data from backing pages to netfs pages to complete a read operation
154 * - driven by FS-Cache's thread pool
156 static void cachefiles_read_copier(struct fscache_operation *_op)
158 struct cachefiles_one_read *monitor;
159 struct cachefiles_object *object;
160 struct fscache_retrieval *op;
161 int error, max;
163 op = container_of(_op, struct fscache_retrieval, op);
164 object = container_of(op->op.object,
165 struct cachefiles_object, fscache);
167 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
169 max = 8;
170 spin_lock_irq(&object->work_lock);
172 while (!list_empty(&op->to_do)) {
173 monitor = list_entry(op->to_do.next,
174 struct cachefiles_one_read, op_link);
175 list_del(&monitor->op_link);
177 spin_unlock_irq(&object->work_lock);
179 _debug("- copy {%lu}", monitor->back_page->index);
181 recheck:
182 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
183 &object->fscache.cookie->flags)) {
184 error = -ESTALE;
185 } else if (PageUptodate(monitor->back_page)) {
186 copy_highpage(monitor->netfs_page, monitor->back_page);
187 fscache_mark_page_cached(monitor->op,
188 monitor->netfs_page);
189 error = 0;
190 } else if (!PageError(monitor->back_page)) {
191 /* the page has probably been truncated */
192 error = cachefiles_read_reissue(object, monitor);
193 if (error == -EINPROGRESS)
194 goto next;
195 goto recheck;
196 } else {
197 cachefiles_io_error_obj(
198 object,
199 "Readpage failed on backing file %lx",
200 (unsigned long) monitor->back_page->flags);
201 error = -EIO;
204 page_cache_release(monitor->back_page);
206 fscache_end_io(op, monitor->netfs_page, error);
207 page_cache_release(monitor->netfs_page);
208 fscache_retrieval_complete(op, 1);
209 fscache_put_retrieval(op);
210 kfree(monitor);
212 next:
213 /* let the thread pool have some air occasionally */
214 max--;
215 if (max < 0 || need_resched()) {
216 if (!list_empty(&op->to_do))
217 fscache_enqueue_retrieval(op);
218 _leave(" [maxed out]");
219 return;
222 spin_lock_irq(&object->work_lock);
225 spin_unlock_irq(&object->work_lock);
226 _leave("");
230 * read the corresponding page to the given set from the backing file
231 * - an uncertain page is simply discarded, to be tried again another time
233 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
234 struct fscache_retrieval *op,
235 struct page *netpage)
237 struct cachefiles_one_read *monitor;
238 struct address_space *bmapping;
239 struct page *newpage, *backpage;
240 int ret;
242 _enter("");
244 _debug("read back %p{%lu,%d}",
245 netpage, netpage->index, page_count(netpage));
247 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
248 if (!monitor)
249 goto nomem;
251 monitor->netfs_page = netpage;
252 monitor->op = fscache_get_retrieval(op);
254 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
256 /* attempt to get hold of the backing page */
257 bmapping = d_backing_inode(object->backer)->i_mapping;
258 newpage = NULL;
260 for (;;) {
261 backpage = find_get_page(bmapping, netpage->index);
262 if (backpage)
263 goto backing_page_already_present;
265 if (!newpage) {
266 newpage = __page_cache_alloc(cachefiles_gfp |
267 __GFP_COLD);
268 if (!newpage)
269 goto nomem_monitor;
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;
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);
285 backpage = newpage;
286 newpage = NULL;
288 read_backing_page:
289 ret = bmapping->a_ops->readpage(NULL, backpage);
290 if (ret < 0)
291 goto read_error;
293 /* set the monitor to transfer the data across */
294 monitor_backing_page:
295 _debug("- monitor add");
297 /* install the monitor */
298 page_cache_get(monitor->netfs_page);
299 page_cache_get(backpage);
300 monitor->back_page = backpage;
301 monitor->monitor.private = backpage;
302 add_page_wait_queue(backpage, &monitor->monitor);
303 monitor = NULL;
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);
312 goto success;
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");
319 if (newpage) {
320 page_cache_release(newpage);
321 newpage = NULL;
324 if (PageError(backpage))
325 goto io_error;
327 if (PageUptodate(backpage))
328 goto backing_page_already_uptodate;
330 if (!trylock_page(backpage))
331 goto monitor_backing_page;
332 _debug("read %p {%lx}", backpage, backpage->flags);
333 goto read_backing_page;
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");
340 fscache_mark_page_cached(op, netpage);
342 copy_highpage(netpage, backpage);
343 fscache_end_io(op, netpage, 0);
344 fscache_retrieval_complete(op, 1);
346 success:
347 _debug("success");
348 ret = 0;
350 out:
351 if (backpage)
352 page_cache_release(backpage);
353 if (monitor) {
354 fscache_put_retrieval(monitor->op);
355 kfree(monitor);
357 _leave(" = %d", ret);
358 return ret;
360 read_error:
361 _debug("read error %d", ret);
362 if (ret == -ENOMEM) {
363 fscache_retrieval_complete(op, 1);
364 goto out;
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;
372 nomem_page:
373 page_cache_release(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;
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
397 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
398 struct page *page,
399 gfp_t gfp)
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;
408 object = container_of(op->op.object,
409 struct cachefiles_object, fscache);
410 cache = container_of(object->fscache.cache,
411 struct cachefiles_cache, cache);
413 _enter("{%p},{%lx},,,", object, page->index);
415 if (!object->backer)
416 goto enobufs;
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);
423 /* calculate the shift required to use bmap */
424 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
426 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
427 op->op.flags |= FSCACHE_OP_ASYNC;
428 op->op.processor = cachefiles_read_copier;
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
436 block0 = page->index;
437 block0 <<= shift;
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);
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;
457 _leave(" = %d", ret);
458 return ret;
460 enobufs:
461 fscache_retrieval_complete(op, 1);
462 _leave(" = -ENOBUFS");
463 return -ENOBUFS;
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
470 static int cachefiles_read_backing_file(struct cachefiles_object *object,
471 struct fscache_retrieval *op,
472 struct list_head *list)
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;
479 _enter("");
481 list_for_each_entry_safe(netpage, _n, list, lru) {
482 list_del(&netpage->lru);
484 _debug("read back %p{%lu,%d}",
485 netpage, netpage->index, page_count(netpage));
487 if (!monitor) {
488 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
489 if (!monitor)
490 goto nomem;
492 monitor->op = fscache_get_retrieval(op);
493 init_waitqueue_func_entry(&monitor->monitor,
494 cachefiles_read_waiter);
497 for (;;) {
498 backpage = find_get_page(bmapping, netpage->index);
499 if (backpage)
500 goto backing_page_already_present;
502 if (!newpage) {
503 newpage = __page_cache_alloc(cachefiles_gfp |
504 __GFP_COLD);
505 if (!newpage)
506 goto nomem;
509 ret = add_to_page_cache_lru(newpage, bmapping,
510 netpage->index,
511 cachefiles_gfp);
512 if (ret == 0)
513 goto installed_new_backing_page;
514 if (ret != -EEXIST)
515 goto nomem;
518 /* we've installed a new backing page, so now we need
519 * to start it reading */
520 installed_new_backing_page:
521 _debug("- new %p", newpage);
523 backpage = newpage;
524 newpage = NULL;
526 reread_backing_page:
527 ret = bmapping->a_ops->readpage(NULL, backpage);
528 if (ret < 0)
529 goto read_error;
531 /* add the netfs page to the pagecache and LRU, and set the
532 * monitor to transfer the data across */
533 monitor_backing_page:
534 _debug("- monitor add");
536 ret = add_to_page_cache_lru(netpage, op->mapping,
537 netpage->index, cachefiles_gfp);
538 if (ret < 0) {
539 if (ret == -EEXIST) {
540 page_cache_release(netpage);
541 fscache_retrieval_complete(op, 1);
542 continue;
544 goto nomem;
547 /* install a monitor */
548 page_cache_get(netpage);
549 monitor->netfs_page = netpage;
551 page_cache_get(backpage);
552 monitor->back_page = backpage;
553 monitor->monitor.private = backpage;
554 add_page_wait_queue(backpage, &monitor->monitor);
555 monitor = NULL;
557 /* but the page may have been read before the monitor was
558 * installed, so the monitor may miss the event - so we have to
559 * ensure that we do get one in such a case */
560 if (trylock_page(backpage)) {
561 _debug("2unlock %p {%lx}", backpage, backpage->flags);
562 unlock_page(backpage);
565 page_cache_release(backpage);
566 backpage = NULL;
568 page_cache_release(netpage);
569 netpage = NULL;
570 continue;
572 /* if the backing page is already present, it can be in one of
573 * three states: read in progress, read failed or read okay */
574 backing_page_already_present:
575 _debug("- present %p", backpage);
577 if (PageError(backpage))
578 goto io_error;
580 if (PageUptodate(backpage))
581 goto backing_page_already_uptodate;
583 _debug("- not ready %p{%lx}", backpage, backpage->flags);
585 if (!trylock_page(backpage))
586 goto monitor_backing_page;
588 if (PageError(backpage)) {
589 _debug("error %lx", backpage->flags);
590 unlock_page(backpage);
591 goto io_error;
594 if (PageUptodate(backpage))
595 goto backing_page_already_uptodate_unlock;
597 /* we've locked a page that's neither up to date nor erroneous,
598 * so we need to attempt to read it again */
599 goto reread_backing_page;
601 /* the backing page is already up to date, attach the netfs
602 * page to the pagecache and LRU and copy the data across */
603 backing_page_already_uptodate_unlock:
604 _debug("uptodate %lx", backpage->flags);
605 unlock_page(backpage);
606 backing_page_already_uptodate:
607 _debug("- uptodate");
609 ret = add_to_page_cache_lru(netpage, op->mapping,
610 netpage->index, cachefiles_gfp);
611 if (ret < 0) {
612 if (ret == -EEXIST) {
613 page_cache_release(netpage);
614 fscache_retrieval_complete(op, 1);
615 continue;
617 goto nomem;
620 copy_highpage(netpage, backpage);
622 page_cache_release(backpage);
623 backpage = NULL;
625 fscache_mark_page_cached(op, netpage);
627 /* the netpage is unlocked and marked up to date here */
628 fscache_end_io(op, netpage, 0);
629 page_cache_release(netpage);
630 netpage = NULL;
631 fscache_retrieval_complete(op, 1);
632 continue;
635 netpage = NULL;
637 _debug("out");
639 out:
640 /* tidy up */
641 if (newpage)
642 page_cache_release(newpage);
643 if (netpage)
644 page_cache_release(netpage);
645 if (backpage)
646 page_cache_release(backpage);
647 if (monitor) {
648 fscache_put_retrieval(op);
649 kfree(monitor);
652 list_for_each_entry_safe(netpage, _n, list, lru) {
653 list_del(&netpage->lru);
654 page_cache_release(netpage);
655 fscache_retrieval_complete(op, 1);
658 _leave(" = %d", ret);
659 return ret;
661 nomem:
662 _debug("nomem");
663 ret = -ENOMEM;
664 goto record_page_complete;
666 read_error:
667 _debug("read error %d", ret);
668 if (ret == -ENOMEM)
669 goto record_page_complete;
670 io_error:
671 cachefiles_io_error_obj(object, "Page read error on backing file");
672 ret = -ENOBUFS;
673 record_page_complete:
674 fscache_retrieval_complete(op, 1);
675 goto out;
679 * read a list of pages from the cache or allocate blocks in which to store
680 * them
682 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
683 struct list_head *pages,
684 unsigned *nr_pages,
685 gfp_t gfp)
687 struct cachefiles_object *object;
688 struct cachefiles_cache *cache;
689 struct list_head backpages;
690 struct pagevec pagevec;
691 struct inode *inode;
692 struct page *page, *_n;
693 unsigned shift, nrbackpages;
694 int ret, ret2, space;
696 object = container_of(op->op.object,
697 struct cachefiles_object, fscache);
698 cache = container_of(object->fscache.cache,
699 struct cachefiles_cache, cache);
701 _enter("{OBJ%x,%d},,%d,,",
702 object->fscache.debug_id, atomic_read(&op->op.usage),
703 *nr_pages);
705 if (!object->backer)
706 goto all_enobufs;
708 space = 1;
709 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
710 space = 0;
712 inode = d_backing_inode(object->backer);
713 ASSERT(S_ISREG(inode->i_mode));
714 ASSERT(inode->i_mapping->a_ops->bmap);
715 ASSERT(inode->i_mapping->a_ops->readpages);
717 /* calculate the shift required to use bmap */
718 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
720 pagevec_init(&pagevec, 0);
722 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
723 op->op.flags |= FSCACHE_OP_ASYNC;
724 op->op.processor = cachefiles_read_copier;
726 INIT_LIST_HEAD(&backpages);
727 nrbackpages = 0;
729 ret = space ? -ENODATA : -ENOBUFS;
730 list_for_each_entry_safe(page, _n, pages, lru) {
731 sector_t block0, block;
733 /* we assume the absence or presence of the first block is a
734 * good enough indication for the page as a whole
735 * - TODO: don't use bmap() for this as it is _not_ actually
736 * good enough for this as it doesn't indicate errors, but
737 * it's all we've got for the moment
739 block0 = page->index;
740 block0 <<= shift;
742 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
743 block0);
744 _debug("%llx -> %llx",
745 (unsigned long long) block0,
746 (unsigned long long) block);
748 if (block) {
749 /* we have data - add it to the list to give to the
750 * backing fs */
751 list_move(&page->lru, &backpages);
752 (*nr_pages)--;
753 nrbackpages++;
754 } else if (space && pagevec_add(&pagevec, page) == 0) {
755 fscache_mark_pages_cached(op, &pagevec);
756 fscache_retrieval_complete(op, 1);
757 ret = -ENODATA;
758 } else {
759 fscache_retrieval_complete(op, 1);
763 if (pagevec_count(&pagevec) > 0)
764 fscache_mark_pages_cached(op, &pagevec);
766 if (list_empty(pages))
767 ret = 0;
769 /* submit the apparently valid pages to the backing fs to be read from
770 * disk */
771 if (nrbackpages > 0) {
772 ret2 = cachefiles_read_backing_file(object, op, &backpages);
773 if (ret2 == -ENOMEM || ret2 == -EINTR)
774 ret = ret2;
777 _leave(" = %d [nr=%u%s]",
778 ret, *nr_pages, list_empty(pages) ? " empty" : "");
779 return ret;
781 all_enobufs:
782 fscache_retrieval_complete(op, *nr_pages);
783 return -ENOBUFS;
787 * allocate a block in the cache in which to store a page
788 * - cache withdrawal is prevented by the caller
789 * - returns -EINTR if interrupted
790 * - returns -ENOMEM if ran out of memory
791 * - returns -ENOBUFS if no buffers can be made available
792 * - returns -ENOBUFS if page is beyond EOF
793 * - otherwise:
794 * - the metadata will be retained
795 * - 0 will be returned
797 int cachefiles_allocate_page(struct fscache_retrieval *op,
798 struct page *page,
799 gfp_t gfp)
801 struct cachefiles_object *object;
802 struct cachefiles_cache *cache;
803 int ret;
805 object = container_of(op->op.object,
806 struct cachefiles_object, fscache);
807 cache = container_of(object->fscache.cache,
808 struct cachefiles_cache, cache);
810 _enter("%p,{%lx},", object, page->index);
812 ret = cachefiles_has_space(cache, 0, 1);
813 if (ret == 0)
814 fscache_mark_page_cached(op, page);
815 else
816 ret = -ENOBUFS;
818 fscache_retrieval_complete(op, 1);
819 _leave(" = %d", ret);
820 return ret;
824 * allocate blocks in the cache in which to store a set of pages
825 * - cache withdrawal is prevented by the caller
826 * - returns -EINTR if interrupted
827 * - returns -ENOMEM if ran out of memory
828 * - returns -ENOBUFS if some buffers couldn't be made available
829 * - returns -ENOBUFS if some pages are beyond EOF
830 * - otherwise:
831 * - -ENODATA will be returned
832 * - metadata will be retained for any page marked
834 int cachefiles_allocate_pages(struct fscache_retrieval *op,
835 struct list_head *pages,
836 unsigned *nr_pages,
837 gfp_t gfp)
839 struct cachefiles_object *object;
840 struct cachefiles_cache *cache;
841 struct pagevec pagevec;
842 struct page *page;
843 int ret;
845 object = container_of(op->op.object,
846 struct cachefiles_object, fscache);
847 cache = container_of(object->fscache.cache,
848 struct cachefiles_cache, cache);
850 _enter("%p,,,%d,", object, *nr_pages);
852 ret = cachefiles_has_space(cache, 0, *nr_pages);
853 if (ret == 0) {
854 pagevec_init(&pagevec, 0);
856 list_for_each_entry(page, pages, lru) {
857 if (pagevec_add(&pagevec, page) == 0)
858 fscache_mark_pages_cached(op, &pagevec);
861 if (pagevec_count(&pagevec) > 0)
862 fscache_mark_pages_cached(op, &pagevec);
863 ret = -ENODATA;
864 } else {
865 ret = -ENOBUFS;
868 fscache_retrieval_complete(op, *nr_pages);
869 _leave(" = %d", ret);
870 return ret;
874 * request a page be stored in the cache
875 * - cache withdrawal is prevented by the caller
876 * - this request may be ignored if there's no cache block available, in which
877 * case -ENOBUFS will be returned
878 * - if the op is in progress, 0 will be returned
880 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
882 struct cachefiles_object *object;
883 struct cachefiles_cache *cache;
884 struct file *file;
885 struct path path;
886 loff_t pos, eof;
887 size_t len;
888 void *data;
889 int ret = -ENOBUFS;
891 ASSERT(op != NULL);
892 ASSERT(page != NULL);
894 object = container_of(op->op.object,
895 struct cachefiles_object, fscache);
897 _enter("%p,%p{%lx},,,", object, page, page->index);
899 if (!object->backer) {
900 _leave(" = -ENOBUFS");
901 return -ENOBUFS;
904 ASSERT(d_is_reg(object->backer));
906 cache = container_of(object->fscache.cache,
907 struct cachefiles_cache, cache);
909 pos = (loff_t)page->index << PAGE_SHIFT;
911 /* We mustn't write more data than we have, so we have to beware of a
912 * partial page at EOF.
914 eof = object->fscache.store_limit_l;
915 if (pos >= eof)
916 goto error;
918 /* write the page to the backing filesystem and let it store it in its
919 * own time */
920 path.mnt = cache->mnt;
921 path.dentry = object->backer;
922 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
923 if (IS_ERR(file)) {
924 ret = PTR_ERR(file);
925 goto error_2;
928 len = PAGE_SIZE;
929 if (eof & ~PAGE_MASK) {
930 if (eof - pos < PAGE_SIZE) {
931 _debug("cut short %llx to %llx",
932 pos, eof);
933 len = eof - pos;
934 ASSERTCMP(pos + len, ==, eof);
938 data = kmap(page);
939 ret = __kernel_write(file, data, len, &pos);
940 kunmap(page);
941 fput(file);
942 if (ret != len)
943 goto error_eio;
945 _leave(" = 0");
946 return 0;
948 error_eio:
949 ret = -EIO;
950 error_2:
951 if (ret == -EIO)
952 cachefiles_io_error_obj(object,
953 "Write page to backing file failed");
954 error:
955 _leave(" = -ENOBUFS [%d]", ret);
956 return -ENOBUFS;
960 * detach a backing block from a page
961 * - cache withdrawal is prevented by the caller
963 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
965 struct cachefiles_object *object;
967 object = container_of(_object, struct cachefiles_object, fscache);
969 _enter("%p,{%lu}", object, page->index);
971 spin_unlock(&object->fscache.cookie->lock);