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 spin_unlock(&object->work_lock);
69 fscache_enqueue_retrieval(op);
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);
145 spin_lock_irq(&object->work_lock);
146 list_del(&monitor->op_link);
147 spin_unlock_irq(&object->work_lock);
148 _leave(" = %d", 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;
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);
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);
182 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
183 &object->fscache.cookie->flags)) {
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);
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)
197 cachefiles_io_error_obj(
199 "Readpage failed on backing file %lx",
200 (unsigned long) monitor->back_page->flags);
204 put_page(monitor->back_page);
206 fscache_end_io(op, monitor->netfs_page, error);
207 put_page(monitor->netfs_page);
208 fscache_retrieval_complete(op, 1);
209 fscache_put_retrieval(op);
213 /* let the thread pool have some air occasionally */
215 if (max < 0 || need_resched()) {
216 if (!list_empty(&op->to_do))
217 fscache_enqueue_retrieval(op);
218 _leave(" [maxed out]");
222 spin_lock_irq(&object->work_lock);
225 spin_unlock_irq(&object->work_lock);
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;
244 _debug("read back %p{%lu,%d}",
245 netpage, netpage->index, page_count(netpage));
247 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
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;
261 backpage = find_get_page(bmapping, netpage->index);
263 goto backing_page_already_present;
266 newpage = __page_cache_alloc(cachefiles_gfp |
272 ret = add_to_page_cache_lru(newpage, bmapping,
273 netpage->index, cachefiles_gfp);
275 goto installed_new_backing_page;
280 /* we've installed a new backing page, so now we need to start
282 installed_new_backing_page:
283 _debug("- new %p", newpage);
289 ret = bmapping->a_ops->readpage(NULL, backpage);
293 /* set the monitor to transfer the data across */
294 monitor_backing_page:
295 _debug("- monitor add");
297 /* install the monitor */
298 get_page(monitor->netfs_page);
300 monitor->back_page = backpage;
301 monitor->monitor.private = backpage;
302 add_page_wait_queue(backpage, &monitor->monitor);
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);
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:
324 if (PageError(backpage))
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);
354 fscache_put_retrieval(monitor->op);
357 _leave(" = %d", ret);
361 _debug("read error %d", ret);
362 if (ret == -ENOMEM) {
363 fscache_retrieval_complete(op, 1);
367 cachefiles_io_error_obj(object, "Page read error on backing file");
368 fscache_retrieval_complete(op, 1);
375 fscache_put_retrieval(monitor->op);
378 fscache_retrieval_complete(op, 1);
379 _leave(" = -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,
401 struct cachefiles_object *object;
402 struct cachefiles_cache *cache;
404 sector_t block0, block;
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);
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
436 block0 = page->index;
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);
445 /* submit the apparently valid page to the backing fs to be
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);
457 _leave(" = %d", ret);
461 fscache_retrieval_complete(op, 1);
462 _leave(" = -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;
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));
488 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
492 monitor->op = fscache_get_retrieval(op);
493 init_waitqueue_func_entry(&monitor->monitor,
494 cachefiles_read_waiter);
498 backpage = find_get_page(bmapping, netpage->index);
500 goto backing_page_already_present;
503 newpage = __page_cache_alloc(cachefiles_gfp |
509 ret = add_to_page_cache_lru(newpage, bmapping,
513 goto installed_new_backing_page;
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);
527 ret = bmapping->a_ops->readpage(NULL, backpage);
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);
539 if (ret == -EEXIST) {
544 fscache_retrieval_complete(op, 1);
550 /* install a monitor */
552 monitor->netfs_page = netpage;
555 monitor->back_page = backpage;
556 monitor->monitor.private = backpage;
557 add_page_wait_queue(backpage, &monitor->monitor);
560 /* but the page may have been read before the monitor was
561 * installed, so the monitor may miss the event - so we have to
562 * ensure that we do get one in such a case */
563 if (trylock_page(backpage)) {
564 _debug("2unlock %p {%lx}", backpage, backpage->flags);
565 unlock_page(backpage);
575 /* if the backing page is already present, it can be in one of
576 * three states: read in progress, read failed or read okay */
577 backing_page_already_present:
578 _debug("- present %p", backpage);
580 if (PageError(backpage))
583 if (PageUptodate(backpage))
584 goto backing_page_already_uptodate;
586 _debug("- not ready %p{%lx}", backpage, backpage->flags);
588 if (!trylock_page(backpage))
589 goto monitor_backing_page;
591 if (PageError(backpage)) {
592 _debug("error %lx", backpage->flags);
593 unlock_page(backpage);
597 if (PageUptodate(backpage))
598 goto backing_page_already_uptodate_unlock;
600 /* we've locked a page that's neither up to date nor erroneous,
601 * so we need to attempt to read it again */
602 goto reread_backing_page;
604 /* the backing page is already up to date, attach the netfs
605 * page to the pagecache and LRU and copy the data across */
606 backing_page_already_uptodate_unlock:
607 _debug("uptodate %lx", backpage->flags);
608 unlock_page(backpage);
609 backing_page_already_uptodate:
610 _debug("- uptodate");
612 ret = add_to_page_cache_lru(netpage, op->mapping,
613 netpage->index, cachefiles_gfp);
615 if (ret == -EEXIST) {
620 fscache_retrieval_complete(op, 1);
626 copy_highpage(netpage, backpage);
631 fscache_mark_page_cached(op, netpage);
633 /* the netpage is unlocked and marked up to date here */
634 fscache_end_io(op, netpage, 0);
637 fscache_retrieval_complete(op, 1);
654 fscache_put_retrieval(op);
658 list_for_each_entry_safe(netpage, _n, list, lru) {
659 list_del(&netpage->lru);
661 fscache_retrieval_complete(op, 1);
664 _leave(" = %d", ret);
670 goto record_page_complete;
673 _debug("read error %d", ret);
675 goto record_page_complete;
677 cachefiles_io_error_obj(object, "Page read error on backing file");
679 record_page_complete:
680 fscache_retrieval_complete(op, 1);
685 * read a list of pages from the cache or allocate blocks in which to store
688 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
689 struct list_head *pages,
693 struct cachefiles_object *object;
694 struct cachefiles_cache *cache;
695 struct list_head backpages;
696 struct pagevec pagevec;
698 struct page *page, *_n;
699 unsigned shift, nrbackpages;
700 int ret, ret2, space;
702 object = container_of(op->op.object,
703 struct cachefiles_object, fscache);
704 cache = container_of(object->fscache.cache,
705 struct cachefiles_cache, cache);
707 _enter("{OBJ%x,%d},,%d,,",
708 object->fscache.debug_id, atomic_read(&op->op.usage),
715 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
718 inode = d_backing_inode(object->backer);
719 ASSERT(S_ISREG(inode->i_mode));
720 ASSERT(inode->i_mapping->a_ops->bmap);
721 ASSERT(inode->i_mapping->a_ops->readpages);
723 /* calculate the shift required to use bmap */
724 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
726 pagevec_init(&pagevec, 0);
728 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
729 op->op.flags |= FSCACHE_OP_ASYNC;
730 op->op.processor = cachefiles_read_copier;
732 INIT_LIST_HEAD(&backpages);
735 ret = space ? -ENODATA : -ENOBUFS;
736 list_for_each_entry_safe(page, _n, pages, lru) {
737 sector_t block0, block;
739 /* we assume the absence or presence of the first block is a
740 * good enough indication for the page as a whole
741 * - TODO: don't use bmap() for this as it is _not_ actually
742 * good enough for this as it doesn't indicate errors, but
743 * it's all we've got for the moment
745 block0 = page->index;
748 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
750 _debug("%llx -> %llx",
751 (unsigned long long) block0,
752 (unsigned long long) block);
755 /* we have data - add it to the list to give to the
757 list_move(&page->lru, &backpages);
760 } else if (space && pagevec_add(&pagevec, page) == 0) {
761 fscache_mark_pages_cached(op, &pagevec);
762 fscache_retrieval_complete(op, 1);
765 fscache_retrieval_complete(op, 1);
769 if (pagevec_count(&pagevec) > 0)
770 fscache_mark_pages_cached(op, &pagevec);
772 if (list_empty(pages))
775 /* submit the apparently valid pages to the backing fs to be read from
777 if (nrbackpages > 0) {
778 ret2 = cachefiles_read_backing_file(object, op, &backpages);
779 if (ret2 == -ENOMEM || ret2 == -EINTR)
783 _leave(" = %d [nr=%u%s]",
784 ret, *nr_pages, list_empty(pages) ? " empty" : "");
788 fscache_retrieval_complete(op, *nr_pages);
793 * allocate a block in the cache in which to store a page
794 * - cache withdrawal is prevented by the caller
795 * - returns -EINTR if interrupted
796 * - returns -ENOMEM if ran out of memory
797 * - returns -ENOBUFS if no buffers can be made available
798 * - returns -ENOBUFS if page is beyond EOF
800 * - the metadata will be retained
801 * - 0 will be returned
803 int cachefiles_allocate_page(struct fscache_retrieval *op,
807 struct cachefiles_object *object;
808 struct cachefiles_cache *cache;
811 object = container_of(op->op.object,
812 struct cachefiles_object, fscache);
813 cache = container_of(object->fscache.cache,
814 struct cachefiles_cache, cache);
816 _enter("%p,{%lx},", object, page->index);
818 ret = cachefiles_has_space(cache, 0, 1);
820 fscache_mark_page_cached(op, page);
824 fscache_retrieval_complete(op, 1);
825 _leave(" = %d", ret);
830 * allocate blocks in the cache in which to store a set of pages
831 * - cache withdrawal is prevented by the caller
832 * - returns -EINTR if interrupted
833 * - returns -ENOMEM if ran out of memory
834 * - returns -ENOBUFS if some buffers couldn't be made available
835 * - returns -ENOBUFS if some pages are beyond EOF
837 * - -ENODATA will be returned
838 * - metadata will be retained for any page marked
840 int cachefiles_allocate_pages(struct fscache_retrieval *op,
841 struct list_head *pages,
845 struct cachefiles_object *object;
846 struct cachefiles_cache *cache;
847 struct pagevec pagevec;
851 object = container_of(op->op.object,
852 struct cachefiles_object, fscache);
853 cache = container_of(object->fscache.cache,
854 struct cachefiles_cache, cache);
856 _enter("%p,,,%d,", object, *nr_pages);
858 ret = cachefiles_has_space(cache, 0, *nr_pages);
860 pagevec_init(&pagevec, 0);
862 list_for_each_entry(page, pages, lru) {
863 if (pagevec_add(&pagevec, page) == 0)
864 fscache_mark_pages_cached(op, &pagevec);
867 if (pagevec_count(&pagevec) > 0)
868 fscache_mark_pages_cached(op, &pagevec);
874 fscache_retrieval_complete(op, *nr_pages);
875 _leave(" = %d", ret);
880 * request a page be stored in the cache
881 * - cache withdrawal is prevented by the caller
882 * - this request may be ignored if there's no cache block available, in which
883 * case -ENOBUFS will be returned
884 * - if the op is in progress, 0 will be returned
886 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
888 struct cachefiles_object *object;
889 struct cachefiles_cache *cache;
898 ASSERT(page != NULL);
900 object = container_of(op->op.object,
901 struct cachefiles_object, fscache);
903 _enter("%p,%p{%lx},,,", object, page, page->index);
905 if (!object->backer) {
906 _leave(" = -ENOBUFS");
910 ASSERT(d_is_reg(object->backer));
912 cache = container_of(object->fscache.cache,
913 struct cachefiles_cache, cache);
915 pos = (loff_t)page->index << PAGE_SHIFT;
917 /* We mustn't write more data than we have, so we have to beware of a
918 * partial page at EOF.
920 eof = object->fscache.store_limit_l;
924 /* write the page to the backing filesystem and let it store it in its
926 path.mnt = cache->mnt;
927 path.dentry = object->backer;
928 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
935 if (eof & ~PAGE_MASK) {
936 if (eof - pos < PAGE_SIZE) {
937 _debug("cut short %llx to %llx",
940 ASSERTCMP(pos + len, ==, eof);
945 ret = __kernel_write(file, data, len, &pos);
958 cachefiles_io_error_obj(object,
959 "Write page to backing file failed");
961 _leave(" = -ENOBUFS [%d]", ret);
966 * detach a backing block from a page
967 * - cache withdrawal is prevented by the caller
969 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
971 struct cachefiles_object *object;
973 object = container_of(_object, struct cachefiles_object, fscache);
975 _enter("%p,{%lu}", object, page->index);
977 spin_unlock(&object->fscache.cookie->lock);