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 wait_bit_key *key = _key;
31 struct page *page = wait->private;
35 _enter("{%lu},%u,%d,{%p,%u}",
36 monitor->netfs_page->index, mode, sync,
37 key->flags, key->bit_nr);
39 if (key->flags != &page->flags ||
40 key->bit_nr != PG_locked)
43 _debug("--- monitor %p %lx ---", page, page->flags);
45 if (!PageUptodate(page) && !PageError(page)) {
46 /* unlocked, not uptodate and not erronous? */
47 _debug("page probably truncated");
50 /* remove from the waitqueue */
51 list_del(&wait->task_list);
53 /* move onto the action list and queue for FS-Cache thread pool */
56 object = container_of(monitor->op->op.object,
57 struct cachefiles_object, fscache);
59 spin_lock(&object->work_lock);
60 list_add_tail(&monitor->op_link, &monitor->op->to_do);
61 spin_unlock(&object->work_lock);
63 fscache_enqueue_retrieval(monitor->op);
68 * handle a probably truncated page
69 * - check to see if the page is still relevant and reissue the read if
71 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
72 * must wait again and 0 if successful
74 static int cachefiles_read_reissue(struct cachefiles_object *object,
75 struct cachefiles_one_read *monitor)
77 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
78 struct page *backpage = monitor->back_page, *backpage2;
81 _enter("{ino=%lx},{%lx,%lx}",
82 d_backing_inode(object->backer)->i_ino,
83 backpage->index, backpage->flags);
85 /* skip if the page was truncated away completely */
86 if (backpage->mapping != bmapping) {
87 _leave(" = -ENODATA [mapping]");
91 backpage2 = find_get_page(bmapping, backpage->index);
93 _leave(" = -ENODATA [gone]");
97 if (backpage != backpage2) {
99 _leave(" = -ENODATA [different]");
103 /* the page is still there and we already have a ref on it, so we don't
107 INIT_LIST_HEAD(&monitor->op_link);
108 add_page_wait_queue(backpage, &monitor->monitor);
110 if (trylock_page(backpage)) {
112 if (PageError(backpage))
115 if (PageUptodate(backpage))
118 _debug("reissue read");
119 ret = bmapping->a_ops->readpage(NULL, backpage);
124 /* but the page may have been read before the monitor was installed, so
125 * the monitor may miss the event - so we have to ensure that we do get
126 * one in such a case */
127 if (trylock_page(backpage)) {
128 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
129 unlock_page(backpage);
132 /* it'll reappear on the todo list */
133 _leave(" = -EINPROGRESS");
137 unlock_page(backpage);
138 spin_lock_irq(&object->work_lock);
139 list_del(&monitor->op_link);
140 spin_unlock_irq(&object->work_lock);
141 _leave(" = %d", ret);
146 * copy data from backing pages to netfs pages to complete a read operation
147 * - driven by FS-Cache's thread pool
149 static void cachefiles_read_copier(struct fscache_operation *_op)
151 struct cachefiles_one_read *monitor;
152 struct cachefiles_object *object;
153 struct fscache_retrieval *op;
156 op = container_of(_op, struct fscache_retrieval, op);
157 object = container_of(op->op.object,
158 struct cachefiles_object, fscache);
160 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
163 spin_lock_irq(&object->work_lock);
165 while (!list_empty(&op->to_do)) {
166 monitor = list_entry(op->to_do.next,
167 struct cachefiles_one_read, op_link);
168 list_del(&monitor->op_link);
170 spin_unlock_irq(&object->work_lock);
172 _debug("- copy {%lu}", monitor->back_page->index);
175 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
176 &object->fscache.cookie->flags)) {
178 } else if (PageUptodate(monitor->back_page)) {
179 copy_highpage(monitor->netfs_page, monitor->back_page);
180 fscache_mark_page_cached(monitor->op,
181 monitor->netfs_page);
183 } else if (!PageError(monitor->back_page)) {
184 /* the page has probably been truncated */
185 error = cachefiles_read_reissue(object, monitor);
186 if (error == -EINPROGRESS)
190 cachefiles_io_error_obj(
192 "Readpage failed on backing file %lx",
193 (unsigned long) monitor->back_page->flags);
197 page_cache_release(monitor->back_page);
199 fscache_end_io(op, monitor->netfs_page, error);
200 page_cache_release(monitor->netfs_page);
201 fscache_retrieval_complete(op, 1);
202 fscache_put_retrieval(op);
206 /* let the thread pool have some air occasionally */
208 if (max < 0 || need_resched()) {
209 if (!list_empty(&op->to_do))
210 fscache_enqueue_retrieval(op);
211 _leave(" [maxed out]");
215 spin_lock_irq(&object->work_lock);
218 spin_unlock_irq(&object->work_lock);
223 * read the corresponding page to the given set from the backing file
224 * - an uncertain page is simply discarded, to be tried again another time
226 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
227 struct fscache_retrieval *op,
228 struct page *netpage)
230 struct cachefiles_one_read *monitor;
231 struct address_space *bmapping;
232 struct page *newpage, *backpage;
237 _debug("read back %p{%lu,%d}",
238 netpage, netpage->index, page_count(netpage));
240 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
244 monitor->netfs_page = netpage;
245 monitor->op = fscache_get_retrieval(op);
247 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
249 /* attempt to get hold of the backing page */
250 bmapping = d_backing_inode(object->backer)->i_mapping;
254 backpage = find_get_page(bmapping, netpage->index);
256 goto backing_page_already_present;
259 newpage = __page_cache_alloc(cachefiles_gfp |
265 ret = add_to_page_cache_lru(newpage, bmapping,
266 netpage->index, cachefiles_gfp);
268 goto installed_new_backing_page;
273 /* we've installed a new backing page, so now we need to start
275 installed_new_backing_page:
276 _debug("- new %p", newpage);
282 ret = bmapping->a_ops->readpage(NULL, backpage);
286 /* set the monitor to transfer the data across */
287 monitor_backing_page:
288 _debug("- monitor add");
290 /* install the monitor */
291 page_cache_get(monitor->netfs_page);
292 page_cache_get(backpage);
293 monitor->back_page = backpage;
294 monitor->monitor.private = backpage;
295 add_page_wait_queue(backpage, &monitor->monitor);
298 /* but the page may have been read before the monitor was installed, so
299 * the monitor may miss the event - so we have to ensure that we do get
300 * one in such a case */
301 if (trylock_page(backpage)) {
302 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
303 unlock_page(backpage);
307 /* if the backing page is already present, it can be in one of
308 * three states: read in progress, read failed or read okay */
309 backing_page_already_present:
313 page_cache_release(newpage);
317 if (PageError(backpage))
320 if (PageUptodate(backpage))
321 goto backing_page_already_uptodate;
323 if (!trylock_page(backpage))
324 goto monitor_backing_page;
325 _debug("read %p {%lx}", backpage, backpage->flags);
326 goto read_backing_page;
328 /* the backing page is already up to date, attach the netfs
329 * page to the pagecache and LRU and copy the data across */
330 backing_page_already_uptodate:
331 _debug("- uptodate");
333 fscache_mark_page_cached(op, netpage);
335 copy_highpage(netpage, backpage);
336 fscache_end_io(op, netpage, 0);
337 fscache_retrieval_complete(op, 1);
345 page_cache_release(backpage);
347 fscache_put_retrieval(monitor->op);
350 _leave(" = %d", ret);
354 _debug("read error %d", ret);
355 if (ret == -ENOMEM) {
356 fscache_retrieval_complete(op, 1);
360 cachefiles_io_error_obj(object, "Page read error on backing file");
361 fscache_retrieval_complete(op, 1);
366 page_cache_release(newpage);
368 fscache_put_retrieval(monitor->op);
371 fscache_retrieval_complete(op, 1);
372 _leave(" = -ENOMEM");
377 * read a page from the cache or allocate a block in which to store it
378 * - cache withdrawal is prevented by the caller
379 * - returns -EINTR if interrupted
380 * - returns -ENOMEM if ran out of memory
381 * - returns -ENOBUFS if no buffers can be made available
382 * - returns -ENOBUFS if page is beyond EOF
383 * - if the page is backed by a block in the cache:
384 * - a read will be started which will call the callback on completion
385 * - 0 will be returned
386 * - else if the page is unbacked:
387 * - the metadata will be retained
388 * - -ENODATA will be returned
390 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
394 struct cachefiles_object *object;
395 struct cachefiles_cache *cache;
397 sector_t block0, block;
401 object = container_of(op->op.object,
402 struct cachefiles_object, fscache);
403 cache = container_of(object->fscache.cache,
404 struct cachefiles_cache, cache);
406 _enter("{%p},{%lx},,,", object, page->index);
411 inode = d_backing_inode(object->backer);
412 ASSERT(S_ISREG(inode->i_mode));
413 ASSERT(inode->i_mapping->a_ops->bmap);
414 ASSERT(inode->i_mapping->a_ops->readpages);
416 /* calculate the shift required to use bmap */
417 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
419 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
420 op->op.flags |= FSCACHE_OP_ASYNC;
421 op->op.processor = cachefiles_read_copier;
423 /* we assume the absence or presence of the first block is a good
424 * enough indication for the page as a whole
425 * - TODO: don't use bmap() for this as it is _not_ actually good
426 * enough for this as it doesn't indicate errors, but it's all we've
429 block0 = page->index;
432 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
433 _debug("%llx -> %llx",
434 (unsigned long long) block0,
435 (unsigned long long) block);
438 /* submit the apparently valid page to the backing fs to be
440 ret = cachefiles_read_backing_file_one(object, op, page);
441 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
442 /* there's space in the cache we can use */
443 fscache_mark_page_cached(op, page);
444 fscache_retrieval_complete(op, 1);
450 _leave(" = %d", ret);
454 fscache_retrieval_complete(op, 1);
455 _leave(" = -ENOBUFS");
460 * read the corresponding pages to the given set from the backing file
461 * - any uncertain pages are simply discarded, to be tried again another time
463 static int cachefiles_read_backing_file(struct cachefiles_object *object,
464 struct fscache_retrieval *op,
465 struct list_head *list)
467 struct cachefiles_one_read *monitor = NULL;
468 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
469 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
474 list_for_each_entry_safe(netpage, _n, list, lru) {
475 list_del(&netpage->lru);
477 _debug("read back %p{%lu,%d}",
478 netpage, netpage->index, page_count(netpage));
481 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
485 monitor->op = fscache_get_retrieval(op);
486 init_waitqueue_func_entry(&monitor->monitor,
487 cachefiles_read_waiter);
491 backpage = find_get_page(bmapping, netpage->index);
493 goto backing_page_already_present;
496 newpage = __page_cache_alloc(cachefiles_gfp |
502 ret = add_to_page_cache_lru(newpage, bmapping,
506 goto installed_new_backing_page;
511 /* we've installed a new backing page, so now we need
512 * to start it reading */
513 installed_new_backing_page:
514 _debug("- new %p", newpage);
520 ret = bmapping->a_ops->readpage(NULL, backpage);
524 /* add the netfs page to the pagecache and LRU, and set the
525 * monitor to transfer the data across */
526 monitor_backing_page:
527 _debug("- monitor add");
529 ret = add_to_page_cache_lru(netpage, op->mapping,
530 netpage->index, cachefiles_gfp);
532 if (ret == -EEXIST) {
533 page_cache_release(netpage);
534 fscache_retrieval_complete(op, 1);
540 /* install a monitor */
541 page_cache_get(netpage);
542 monitor->netfs_page = netpage;
544 page_cache_get(backpage);
545 monitor->back_page = backpage;
546 monitor->monitor.private = backpage;
547 add_page_wait_queue(backpage, &monitor->monitor);
550 /* but the page may have been read before the monitor was
551 * installed, so the monitor may miss the event - so we have to
552 * ensure that we do get one in such a case */
553 if (trylock_page(backpage)) {
554 _debug("2unlock %p {%lx}", backpage, backpage->flags);
555 unlock_page(backpage);
558 page_cache_release(backpage);
561 page_cache_release(netpage);
565 /* if the backing page is already present, it can be in one of
566 * three states: read in progress, read failed or read okay */
567 backing_page_already_present:
568 _debug("- present %p", backpage);
570 if (PageError(backpage))
573 if (PageUptodate(backpage))
574 goto backing_page_already_uptodate;
576 _debug("- not ready %p{%lx}", backpage, backpage->flags);
578 if (!trylock_page(backpage))
579 goto monitor_backing_page;
581 if (PageError(backpage)) {
582 _debug("error %lx", backpage->flags);
583 unlock_page(backpage);
587 if (PageUptodate(backpage))
588 goto backing_page_already_uptodate_unlock;
590 /* we've locked a page that's neither up to date nor erroneous,
591 * so we need to attempt to read it again */
592 goto reread_backing_page;
594 /* the backing page is already up to date, attach the netfs
595 * page to the pagecache and LRU and copy the data across */
596 backing_page_already_uptodate_unlock:
597 _debug("uptodate %lx", backpage->flags);
598 unlock_page(backpage);
599 backing_page_already_uptodate:
600 _debug("- uptodate");
602 ret = add_to_page_cache_lru(netpage, op->mapping,
603 netpage->index, cachefiles_gfp);
605 if (ret == -EEXIST) {
606 page_cache_release(netpage);
607 fscache_retrieval_complete(op, 1);
613 copy_highpage(netpage, backpage);
615 page_cache_release(backpage);
618 fscache_mark_page_cached(op, netpage);
620 /* the netpage is unlocked and marked up to date here */
621 fscache_end_io(op, netpage, 0);
622 page_cache_release(netpage);
624 fscache_retrieval_complete(op, 1);
635 page_cache_release(newpage);
637 page_cache_release(netpage);
639 page_cache_release(backpage);
641 fscache_put_retrieval(op);
645 list_for_each_entry_safe(netpage, _n, list, lru) {
646 list_del(&netpage->lru);
647 page_cache_release(netpage);
648 fscache_retrieval_complete(op, 1);
651 _leave(" = %d", ret);
657 goto record_page_complete;
660 _debug("read error %d", ret);
662 goto record_page_complete;
664 cachefiles_io_error_obj(object, "Page read error on backing file");
666 record_page_complete:
667 fscache_retrieval_complete(op, 1);
672 * read a list of pages from the cache or allocate blocks in which to store
675 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
676 struct list_head *pages,
680 struct cachefiles_object *object;
681 struct cachefiles_cache *cache;
682 struct list_head backpages;
683 struct pagevec pagevec;
685 struct page *page, *_n;
686 unsigned shift, nrbackpages;
687 int ret, ret2, space;
689 object = container_of(op->op.object,
690 struct cachefiles_object, fscache);
691 cache = container_of(object->fscache.cache,
692 struct cachefiles_cache, cache);
694 _enter("{OBJ%x,%d},,%d,,",
695 object->fscache.debug_id, atomic_read(&op->op.usage),
702 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
705 inode = d_backing_inode(object->backer);
706 ASSERT(S_ISREG(inode->i_mode));
707 ASSERT(inode->i_mapping->a_ops->bmap);
708 ASSERT(inode->i_mapping->a_ops->readpages);
710 /* calculate the shift required to use bmap */
711 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
713 pagevec_init(&pagevec, 0);
715 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
716 op->op.flags |= FSCACHE_OP_ASYNC;
717 op->op.processor = cachefiles_read_copier;
719 INIT_LIST_HEAD(&backpages);
722 ret = space ? -ENODATA : -ENOBUFS;
723 list_for_each_entry_safe(page, _n, pages, lru) {
724 sector_t block0, block;
726 /* we assume the absence or presence of the first block is a
727 * good enough indication for the page as a whole
728 * - TODO: don't use bmap() for this as it is _not_ actually
729 * good enough for this as it doesn't indicate errors, but
730 * it's all we've got for the moment
732 block0 = page->index;
735 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
737 _debug("%llx -> %llx",
738 (unsigned long long) block0,
739 (unsigned long long) block);
742 /* we have data - add it to the list to give to the
744 list_move(&page->lru, &backpages);
747 } else if (space && pagevec_add(&pagevec, page) == 0) {
748 fscache_mark_pages_cached(op, &pagevec);
749 fscache_retrieval_complete(op, 1);
752 fscache_retrieval_complete(op, 1);
756 if (pagevec_count(&pagevec) > 0)
757 fscache_mark_pages_cached(op, &pagevec);
759 if (list_empty(pages))
762 /* submit the apparently valid pages to the backing fs to be read from
764 if (nrbackpages > 0) {
765 ret2 = cachefiles_read_backing_file(object, op, &backpages);
766 if (ret2 == -ENOMEM || ret2 == -EINTR)
770 _leave(" = %d [nr=%u%s]",
771 ret, *nr_pages, list_empty(pages) ? " empty" : "");
775 fscache_retrieval_complete(op, *nr_pages);
780 * allocate a block in the cache in which to store a page
781 * - cache withdrawal is prevented by the caller
782 * - returns -EINTR if interrupted
783 * - returns -ENOMEM if ran out of memory
784 * - returns -ENOBUFS if no buffers can be made available
785 * - returns -ENOBUFS if page is beyond EOF
787 * - the metadata will be retained
788 * - 0 will be returned
790 int cachefiles_allocate_page(struct fscache_retrieval *op,
794 struct cachefiles_object *object;
795 struct cachefiles_cache *cache;
798 object = container_of(op->op.object,
799 struct cachefiles_object, fscache);
800 cache = container_of(object->fscache.cache,
801 struct cachefiles_cache, cache);
803 _enter("%p,{%lx},", object, page->index);
805 ret = cachefiles_has_space(cache, 0, 1);
807 fscache_mark_page_cached(op, page);
811 fscache_retrieval_complete(op, 1);
812 _leave(" = %d", ret);
817 * allocate blocks in the cache in which to store a set of pages
818 * - cache withdrawal is prevented by the caller
819 * - returns -EINTR if interrupted
820 * - returns -ENOMEM if ran out of memory
821 * - returns -ENOBUFS if some buffers couldn't be made available
822 * - returns -ENOBUFS if some pages are beyond EOF
824 * - -ENODATA will be returned
825 * - metadata will be retained for any page marked
827 int cachefiles_allocate_pages(struct fscache_retrieval *op,
828 struct list_head *pages,
832 struct cachefiles_object *object;
833 struct cachefiles_cache *cache;
834 struct pagevec pagevec;
838 object = container_of(op->op.object,
839 struct cachefiles_object, fscache);
840 cache = container_of(object->fscache.cache,
841 struct cachefiles_cache, cache);
843 _enter("%p,,,%d,", object, *nr_pages);
845 ret = cachefiles_has_space(cache, 0, *nr_pages);
847 pagevec_init(&pagevec, 0);
849 list_for_each_entry(page, pages, lru) {
850 if (pagevec_add(&pagevec, page) == 0)
851 fscache_mark_pages_cached(op, &pagevec);
854 if (pagevec_count(&pagevec) > 0)
855 fscache_mark_pages_cached(op, &pagevec);
861 fscache_retrieval_complete(op, *nr_pages);
862 _leave(" = %d", ret);
867 * request a page be stored in the cache
868 * - cache withdrawal is prevented by the caller
869 * - this request may be ignored if there's no cache block available, in which
870 * case -ENOBUFS will be returned
871 * - if the op is in progress, 0 will be returned
873 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
875 struct cachefiles_object *object;
876 struct cachefiles_cache *cache;
885 ASSERT(page != NULL);
887 object = container_of(op->op.object,
888 struct cachefiles_object, fscache);
890 _enter("%p,%p{%lx},,,", object, page, page->index);
892 if (!object->backer) {
893 _leave(" = -ENOBUFS");
897 ASSERT(d_is_reg(object->backer));
899 cache = container_of(object->fscache.cache,
900 struct cachefiles_cache, cache);
902 pos = (loff_t)page->index << PAGE_SHIFT;
904 /* We mustn't write more data than we have, so we have to beware of a
905 * partial page at EOF.
907 eof = object->fscache.store_limit_l;
911 /* write the page to the backing filesystem and let it store it in its
913 path.mnt = cache->mnt;
914 path.dentry = object->backer;
915 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
922 if (eof & ~PAGE_MASK) {
923 if (eof - pos < PAGE_SIZE) {
924 _debug("cut short %llx to %llx",
927 ASSERTCMP(pos + len, ==, eof);
932 ret = __kernel_write(file, data, len, &pos);
945 cachefiles_io_error_obj(object,
946 "Write page to backing file failed");
948 _leave(" = -ENOBUFS [%d]", ret);
953 * detach a backing block from a page
954 * - cache withdrawal is prevented by the caller
956 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
958 struct cachefiles_object *object;
959 struct cachefiles_cache *cache;
961 object = container_of(_object, struct cachefiles_object, fscache);
962 cache = container_of(object->fscache.cache,
963 struct cachefiles_cache, cache);
965 _enter("%p,{%lu}", object, page->index);
967 spin_unlock(&object->fscache.cookie->lock);