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>
19 * detect wake up events generated by the unlocking of pages in which we're
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,
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;
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)
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 */
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);
75 * handle a probably truncated page
76 * - check to see if the page is still relevant and reissue the read if
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;
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]");
98 backpage2 = find_get_page(bmapping, backpage->index);
100 _leave(" = -ENODATA [gone]");
104 if (backpage != backpage2) {
106 _leave(" = -ENODATA [different]");
110 /* the page is still there and we already have a ref on it, so we don't
114 INIT_LIST_HEAD(&monitor->op_link);
115 add_page_wait_queue(backpage, &monitor->monitor);
117 if (trylock_page(backpage)) {
119 if (PageError(backpage))
122 if (PageUptodate(backpage))
125 _debug("reissue read");
126 ret = bmapping->a_ops->readpage(NULL, backpage);
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");
144 unlock_page(backpage);
146 spin_lock_irq(&object->work_lock);
147 list_del(&monitor->op_link);
148 spin_unlock_irq(&object->work_lock);
149 _leave(" = %d", ret);
154 * copy data from backing pages to netfs pages to complete a read operation
155 * - driven by FS-Cache's thread pool
157 static void cachefiles_read_copier(struct fscache_operation *_op)
159 struct cachefiles_one_read *monitor;
160 struct cachefiles_object *object;
161 struct fscache_retrieval *op;
164 op = container_of(_op, struct fscache_retrieval, op);
165 object = container_of(op->op.object,
166 struct cachefiles_object, fscache);
168 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
171 spin_lock_irq(&object->work_lock);
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);
178 spin_unlock_irq(&object->work_lock);
180 _debug("- copy {%lu}", monitor->back_page->index);
183 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
184 &object->fscache.cookie->flags)) {
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);
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)
198 cachefiles_io_error_obj(
200 "Readpage failed on backing file %lx",
201 (unsigned long) monitor->back_page->flags);
205 put_page(monitor->back_page);
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);
214 /* let the thread pool have some air occasionally */
216 if (max < 0 || need_resched()) {
217 if (!list_empty(&op->to_do))
218 fscache_enqueue_retrieval(op);
219 _leave(" [maxed out]");
223 spin_lock_irq(&object->work_lock);
226 spin_unlock_irq(&object->work_lock);
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
234 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
235 struct fscache_retrieval *op,
236 struct page *netpage)
238 struct cachefiles_one_read *monitor;
239 struct address_space *bmapping;
240 struct page *newpage, *backpage;
245 _debug("read back %p{%lu,%d}",
246 netpage, netpage->index, page_count(netpage));
248 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
252 monitor->netfs_page = netpage;
253 monitor->op = fscache_get_retrieval(op);
255 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
257 /* attempt to get hold of the backing page */
258 bmapping = d_backing_inode(object->backer)->i_mapping;
262 backpage = find_get_page(bmapping, netpage->index);
264 goto backing_page_already_present;
267 newpage = __page_cache_alloc(cachefiles_gfp |
273 ret = add_to_page_cache_lru(newpage, bmapping,
274 netpage->index, cachefiles_gfp);
276 goto installed_new_backing_page;
281 /* we've installed a new backing page, so now we need to start
283 installed_new_backing_page:
284 _debug("- new %p", newpage);
290 ret = bmapping->a_ops->readpage(NULL, backpage);
294 /* set the monitor to transfer the data across */
295 monitor_backing_page:
296 _debug("- monitor add");
298 /* install the monitor */
299 get_page(monitor->netfs_page);
301 monitor->back_page = backpage;
302 monitor->monitor.private = backpage;
303 add_page_wait_queue(backpage, &monitor->monitor);
306 /* but the page may have been read before the monitor was installed, so
307 * the monitor may miss the event - so we have to ensure that we do get
308 * one in such a case */
309 if (trylock_page(backpage)) {
310 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
311 unlock_page(backpage);
315 /* if the backing page is already present, it can be in one of
316 * three states: read in progress, read failed or read okay */
317 backing_page_already_present:
325 if (PageError(backpage))
328 if (PageUptodate(backpage))
329 goto backing_page_already_uptodate;
331 if (!trylock_page(backpage))
332 goto monitor_backing_page;
333 _debug("read %p {%lx}", backpage, backpage->flags);
334 goto read_backing_page;
336 /* the backing page is already up to date, attach the netfs
337 * page to the pagecache and LRU and copy the data across */
338 backing_page_already_uptodate:
339 _debug("- uptodate");
341 fscache_mark_page_cached(op, netpage);
343 copy_highpage(netpage, backpage);
344 fscache_end_io(op, netpage, 0);
345 fscache_retrieval_complete(op, 1);
355 fscache_put_retrieval(monitor->op);
358 _leave(" = %d", ret);
362 _debug("read error %d", ret);
363 if (ret == -ENOMEM) {
364 fscache_retrieval_complete(op, 1);
368 cachefiles_io_error_obj(object, "Page read error on backing file");
369 fscache_retrieval_complete(op, 1);
376 fscache_put_retrieval(monitor->op);
379 fscache_retrieval_complete(op, 1);
380 _leave(" = -ENOMEM");
385 * read a page from the cache or allocate a block in which to store it
386 * - cache withdrawal is prevented by the caller
387 * - returns -EINTR if interrupted
388 * - returns -ENOMEM if ran out of memory
389 * - returns -ENOBUFS if no buffers can be made available
390 * - returns -ENOBUFS if page is beyond EOF
391 * - if the page is backed by a block in the cache:
392 * - a read will be started which will call the callback on completion
393 * - 0 will be returned
394 * - else if the page is unbacked:
395 * - the metadata will be retained
396 * - -ENODATA will be returned
398 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
402 struct cachefiles_object *object;
403 struct cachefiles_cache *cache;
405 sector_t block0, block;
409 object = container_of(op->op.object,
410 struct cachefiles_object, fscache);
411 cache = container_of(object->fscache.cache,
412 struct cachefiles_cache, cache);
414 _enter("{%p},{%lx},,,", object, page->index);
419 inode = d_backing_inode(object->backer);
420 ASSERT(S_ISREG(inode->i_mode));
421 ASSERT(inode->i_mapping->a_ops->bmap);
422 ASSERT(inode->i_mapping->a_ops->readpages);
424 /* calculate the shift required to use bmap */
425 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
427 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
428 op->op.flags |= FSCACHE_OP_ASYNC;
429 op->op.processor = cachefiles_read_copier;
431 /* we assume the absence or presence of the first block is a good
432 * enough indication for the page as a whole
433 * - TODO: don't use bmap() for this as it is _not_ actually good
434 * enough for this as it doesn't indicate errors, but it's all we've
437 block0 = page->index;
440 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
441 _debug("%llx -> %llx",
442 (unsigned long long) block0,
443 (unsigned long long) block);
446 /* submit the apparently valid page to the backing fs to be
448 ret = cachefiles_read_backing_file_one(object, op, page);
449 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
450 /* there's space in the cache we can use */
451 fscache_mark_page_cached(op, page);
452 fscache_retrieval_complete(op, 1);
458 _leave(" = %d", ret);
462 fscache_retrieval_complete(op, 1);
463 _leave(" = -ENOBUFS");
468 * read the corresponding pages to the given set from the backing file
469 * - any uncertain pages are simply discarded, to be tried again another time
471 static int cachefiles_read_backing_file(struct cachefiles_object *object,
472 struct fscache_retrieval *op,
473 struct list_head *list)
475 struct cachefiles_one_read *monitor = NULL;
476 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
477 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
482 list_for_each_entry_safe(netpage, _n, list, lru) {
483 list_del(&netpage->lru);
485 _debug("read back %p{%lu,%d}",
486 netpage, netpage->index, page_count(netpage));
489 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
493 monitor->op = fscache_get_retrieval(op);
494 init_waitqueue_func_entry(&monitor->monitor,
495 cachefiles_read_waiter);
499 backpage = find_get_page(bmapping, netpage->index);
501 goto backing_page_already_present;
504 newpage = __page_cache_alloc(cachefiles_gfp |
510 ret = add_to_page_cache_lru(newpage, bmapping,
514 goto installed_new_backing_page;
519 /* we've installed a new backing page, so now we need
520 * to start it reading */
521 installed_new_backing_page:
522 _debug("- new %p", newpage);
528 ret = bmapping->a_ops->readpage(NULL, backpage);
532 /* add the netfs page to the pagecache and LRU, and set the
533 * monitor to transfer the data across */
534 monitor_backing_page:
535 _debug("- monitor add");
537 ret = add_to_page_cache_lru(netpage, op->mapping,
538 netpage->index, cachefiles_gfp);
540 if (ret == -EEXIST) {
545 fscache_retrieval_complete(op, 1);
551 /* install a monitor */
553 monitor->netfs_page = netpage;
556 monitor->back_page = backpage;
557 monitor->monitor.private = backpage;
558 add_page_wait_queue(backpage, &monitor->monitor);
561 /* but the page may have been read before the monitor was
562 * installed, so the monitor may miss the event - so we have to
563 * ensure that we do get one in such a case */
564 if (trylock_page(backpage)) {
565 _debug("2unlock %p {%lx}", backpage, backpage->flags);
566 unlock_page(backpage);
576 /* if the backing page is already present, it can be in one of
577 * three states: read in progress, read failed or read okay */
578 backing_page_already_present:
579 _debug("- present %p", backpage);
581 if (PageError(backpage))
584 if (PageUptodate(backpage))
585 goto backing_page_already_uptodate;
587 _debug("- not ready %p{%lx}", backpage, backpage->flags);
589 if (!trylock_page(backpage))
590 goto monitor_backing_page;
592 if (PageError(backpage)) {
593 _debug("error %lx", backpage->flags);
594 unlock_page(backpage);
598 if (PageUptodate(backpage))
599 goto backing_page_already_uptodate_unlock;
601 /* we've locked a page that's neither up to date nor erroneous,
602 * so we need to attempt to read it again */
603 goto reread_backing_page;
605 /* the backing page is already up to date, attach the netfs
606 * page to the pagecache and LRU and copy the data across */
607 backing_page_already_uptodate_unlock:
608 _debug("uptodate %lx", backpage->flags);
609 unlock_page(backpage);
610 backing_page_already_uptodate:
611 _debug("- uptodate");
613 ret = add_to_page_cache_lru(netpage, op->mapping,
614 netpage->index, cachefiles_gfp);
616 if (ret == -EEXIST) {
621 fscache_retrieval_complete(op, 1);
627 copy_highpage(netpage, backpage);
632 fscache_mark_page_cached(op, netpage);
634 /* the netpage is unlocked and marked up to date here */
635 fscache_end_io(op, netpage, 0);
638 fscache_retrieval_complete(op, 1);
655 fscache_put_retrieval(op);
659 list_for_each_entry_safe(netpage, _n, list, lru) {
660 list_del(&netpage->lru);
662 fscache_retrieval_complete(op, 1);
665 _leave(" = %d", ret);
671 goto record_page_complete;
674 _debug("read error %d", ret);
676 goto record_page_complete;
678 cachefiles_io_error_obj(object, "Page read error on backing file");
680 record_page_complete:
681 fscache_retrieval_complete(op, 1);
686 * read a list of pages from the cache or allocate blocks in which to store
689 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
690 struct list_head *pages,
694 struct cachefiles_object *object;
695 struct cachefiles_cache *cache;
696 struct list_head backpages;
697 struct pagevec pagevec;
699 struct page *page, *_n;
700 unsigned shift, nrbackpages;
701 int ret, ret2, space;
703 object = container_of(op->op.object,
704 struct cachefiles_object, fscache);
705 cache = container_of(object->fscache.cache,
706 struct cachefiles_cache, cache);
708 _enter("{OBJ%x,%d},,%d,,",
709 object->fscache.debug_id, atomic_read(&op->op.usage),
716 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
719 inode = d_backing_inode(object->backer);
720 ASSERT(S_ISREG(inode->i_mode));
721 ASSERT(inode->i_mapping->a_ops->bmap);
722 ASSERT(inode->i_mapping->a_ops->readpages);
724 /* calculate the shift required to use bmap */
725 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
727 pagevec_init(&pagevec, 0);
729 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
730 op->op.flags |= FSCACHE_OP_ASYNC;
731 op->op.processor = cachefiles_read_copier;
733 INIT_LIST_HEAD(&backpages);
736 ret = space ? -ENODATA : -ENOBUFS;
737 list_for_each_entry_safe(page, _n, pages, lru) {
738 sector_t block0, block;
740 /* we assume the absence or presence of the first block is a
741 * good enough indication for the page as a whole
742 * - TODO: don't use bmap() for this as it is _not_ actually
743 * good enough for this as it doesn't indicate errors, but
744 * it's all we've got for the moment
746 block0 = page->index;
749 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
751 _debug("%llx -> %llx",
752 (unsigned long long) block0,
753 (unsigned long long) block);
756 /* we have data - add it to the list to give to the
758 list_move(&page->lru, &backpages);
761 } else if (space && pagevec_add(&pagevec, page) == 0) {
762 fscache_mark_pages_cached(op, &pagevec);
763 fscache_retrieval_complete(op, 1);
766 fscache_retrieval_complete(op, 1);
770 if (pagevec_count(&pagevec) > 0)
771 fscache_mark_pages_cached(op, &pagevec);
773 if (list_empty(pages))
776 /* submit the apparently valid pages to the backing fs to be read from
778 if (nrbackpages > 0) {
779 ret2 = cachefiles_read_backing_file(object, op, &backpages);
780 if (ret2 == -ENOMEM || ret2 == -EINTR)
784 _leave(" = %d [nr=%u%s]",
785 ret, *nr_pages, list_empty(pages) ? " empty" : "");
789 fscache_retrieval_complete(op, *nr_pages);
794 * allocate a block in the cache in which to store a page
795 * - cache withdrawal is prevented by the caller
796 * - returns -EINTR if interrupted
797 * - returns -ENOMEM if ran out of memory
798 * - returns -ENOBUFS if no buffers can be made available
799 * - returns -ENOBUFS if page is beyond EOF
801 * - the metadata will be retained
802 * - 0 will be returned
804 int cachefiles_allocate_page(struct fscache_retrieval *op,
808 struct cachefiles_object *object;
809 struct cachefiles_cache *cache;
812 object = container_of(op->op.object,
813 struct cachefiles_object, fscache);
814 cache = container_of(object->fscache.cache,
815 struct cachefiles_cache, cache);
817 _enter("%p,{%lx},", object, page->index);
819 ret = cachefiles_has_space(cache, 0, 1);
821 fscache_mark_page_cached(op, page);
825 fscache_retrieval_complete(op, 1);
826 _leave(" = %d", ret);
831 * allocate blocks in the cache in which to store a set of pages
832 * - cache withdrawal is prevented by the caller
833 * - returns -EINTR if interrupted
834 * - returns -ENOMEM if ran out of memory
835 * - returns -ENOBUFS if some buffers couldn't be made available
836 * - returns -ENOBUFS if some pages are beyond EOF
838 * - -ENODATA will be returned
839 * - metadata will be retained for any page marked
841 int cachefiles_allocate_pages(struct fscache_retrieval *op,
842 struct list_head *pages,
846 struct cachefiles_object *object;
847 struct cachefiles_cache *cache;
848 struct pagevec pagevec;
852 object = container_of(op->op.object,
853 struct cachefiles_object, fscache);
854 cache = container_of(object->fscache.cache,
855 struct cachefiles_cache, cache);
857 _enter("%p,,,%d,", object, *nr_pages);
859 ret = cachefiles_has_space(cache, 0, *nr_pages);
861 pagevec_init(&pagevec, 0);
863 list_for_each_entry(page, pages, lru) {
864 if (pagevec_add(&pagevec, page) == 0)
865 fscache_mark_pages_cached(op, &pagevec);
868 if (pagevec_count(&pagevec) > 0)
869 fscache_mark_pages_cached(op, &pagevec);
875 fscache_retrieval_complete(op, *nr_pages);
876 _leave(" = %d", ret);
881 * request a page be stored in the cache
882 * - cache withdrawal is prevented by the caller
883 * - this request may be ignored if there's no cache block available, in which
884 * case -ENOBUFS will be returned
885 * - if the op is in progress, 0 will be returned
887 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
889 struct cachefiles_object *object;
890 struct cachefiles_cache *cache;
899 ASSERT(page != NULL);
901 object = container_of(op->op.object,
902 struct cachefiles_object, fscache);
904 _enter("%p,%p{%lx},,,", object, page, page->index);
906 if (!object->backer) {
907 _leave(" = -ENOBUFS");
911 ASSERT(d_is_reg(object->backer));
913 cache = container_of(object->fscache.cache,
914 struct cachefiles_cache, cache);
916 pos = (loff_t)page->index << PAGE_SHIFT;
918 /* We mustn't write more data than we have, so we have to beware of a
919 * partial page at EOF.
921 eof = object->fscache.store_limit_l;
925 /* write the page to the backing filesystem and let it store it in its
927 path.mnt = cache->mnt;
928 path.dentry = object->backer;
929 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
936 if (eof & ~PAGE_MASK) {
937 if (eof - pos < PAGE_SIZE) {
938 _debug("cut short %llx to %llx",
941 ASSERTCMP(pos + len, ==, eof);
946 ret = __kernel_write(file, data, len, &pos);
959 cachefiles_io_error_obj(object,
960 "Write page to backing file failed");
962 _leave(" = -ENOBUFS [%d]", ret);
967 * detach a backing block from a page
968 * - cache withdrawal is prevented by the caller
970 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
972 struct cachefiles_object *object;
974 object = container_of(_object, struct cachefiles_object, fscache);
976 _enter("%p,{%lu}", object, page->index);
978 spin_unlock(&object->fscache.cookie->lock);