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>
18 * detect wake up events generated by the unlocking of pages in which we're
20 * - we use this to detect read completion of backing pages
21 * - the caller holds the waitqueue lock
23 static int cachefiles_read_waiter(wait_queue_t *wait, unsigned mode,
26 struct cachefiles_one_read *monitor =
27 container_of(wait, struct cachefiles_one_read, monitor);
28 struct cachefiles_object *object;
29 struct wait_bit_key *key = _key;
30 struct page *page = wait->private;
34 _enter("{%lu},%u,%d,{%p,%u}",
35 monitor->netfs_page->index, mode, sync,
36 key->flags, key->bit_nr);
38 if (key->flags != &page->flags ||
39 key->bit_nr != PG_locked)
42 _debug("--- monitor %p %lx ---", page, page->flags);
44 if (!PageUptodate(page) && !PageError(page)) {
45 /* unlocked, not uptodate and not erronous? */
46 _debug("page probably truncated");
49 /* remove from the waitqueue */
50 list_del(&wait->task_list);
52 /* move onto the action list and queue for FS-Cache thread pool */
55 object = container_of(monitor->op->op.object,
56 struct cachefiles_object, fscache);
58 spin_lock(&object->work_lock);
59 list_add_tail(&monitor->op_link, &monitor->op->to_do);
60 spin_unlock(&object->work_lock);
62 fscache_enqueue_retrieval(monitor->op);
67 * handle a probably truncated page
68 * - check to see if the page is still relevant and reissue the read if
70 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
71 * must wait again and 0 if successful
73 static int cachefiles_read_reissue(struct cachefiles_object *object,
74 struct cachefiles_one_read *monitor)
76 struct address_space *bmapping = object->backer->d_inode->i_mapping;
77 struct page *backpage = monitor->back_page, *backpage2;
80 kenter("{ino=%lx},{%lx,%lx}",
81 object->backer->d_inode->i_ino,
82 backpage->index, backpage->flags);
84 /* skip if the page was truncated away completely */
85 if (backpage->mapping != bmapping) {
86 kleave(" = -ENODATA [mapping]");
90 backpage2 = find_get_page(bmapping, backpage->index);
92 kleave(" = -ENODATA [gone]");
96 if (backpage != backpage2) {
98 kleave(" = -ENODATA [different]");
102 /* the page is still there and we already have a ref on it, so we don't
106 INIT_LIST_HEAD(&monitor->op_link);
107 add_page_wait_queue(backpage, &monitor->monitor);
109 if (trylock_page(backpage)) {
111 if (PageError(backpage))
114 if (PageUptodate(backpage))
117 kdebug("reissue read");
118 ret = bmapping->a_ops->readpage(NULL, backpage);
123 /* but the page may have been read before the monitor was installed, so
124 * the monitor may miss the event - so we have to ensure that we do get
125 * one in such a case */
126 if (trylock_page(backpage)) {
127 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
128 unlock_page(backpage);
131 /* it'll reappear on the todo list */
132 kleave(" = -EINPROGRESS");
136 unlock_page(backpage);
137 spin_lock_irq(&object->work_lock);
138 list_del(&monitor->op_link);
139 spin_unlock_irq(&object->work_lock);
140 kleave(" = %d", ret);
145 * copy data from backing pages to netfs pages to complete a read operation
146 * - driven by FS-Cache's thread pool
148 static void cachefiles_read_copier(struct fscache_operation *_op)
150 struct cachefiles_one_read *monitor;
151 struct cachefiles_object *object;
152 struct fscache_retrieval *op;
153 struct pagevec pagevec;
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}", object->backer->d_inode->i_ino);
162 pagevec_init(&pagevec, 0);
165 spin_lock_irq(&object->work_lock);
167 while (!list_empty(&op->to_do)) {
168 monitor = list_entry(op->to_do.next,
169 struct cachefiles_one_read, op_link);
170 list_del(&monitor->op_link);
172 spin_unlock_irq(&object->work_lock);
174 _debug("- copy {%lu}", monitor->back_page->index);
177 if (PageUptodate(monitor->back_page)) {
178 copy_highpage(monitor->netfs_page, monitor->back_page);
179 fscache_mark_page_cached(monitor->op,
180 monitor->netfs_page);
182 } else if (!PageError(monitor->back_page)) {
183 /* the page has probably been truncated */
184 error = cachefiles_read_reissue(object, monitor);
185 if (error == -EINPROGRESS)
189 cachefiles_io_error_obj(
191 "Readpage failed on backing file %lx",
192 (unsigned long) monitor->back_page->flags);
196 page_cache_release(monitor->back_page);
198 fscache_end_io(op, monitor->netfs_page, error);
199 page_cache_release(monitor->netfs_page);
200 fscache_put_retrieval(op);
204 /* let the thread pool have some air occasionally */
206 if (max < 0 || need_resched()) {
207 if (!list_empty(&op->to_do))
208 fscache_enqueue_retrieval(op);
209 _leave(" [maxed out]");
213 spin_lock_irq(&object->work_lock);
216 spin_unlock_irq(&object->work_lock);
221 * read the corresponding page to the given set from the backing file
222 * - an uncertain page is simply discarded, to be tried again another time
224 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
225 struct fscache_retrieval *op,
226 struct page *netpage,
227 struct pagevec *pagevec)
229 struct cachefiles_one_read *monitor;
230 struct address_space *bmapping;
231 struct page *newpage, *backpage;
236 pagevec_reinit(pagevec);
238 _debug("read back %p{%lu,%d}",
239 netpage, netpage->index, page_count(netpage));
241 monitor = kzalloc(sizeof(*monitor), GFP_KERNEL);
245 monitor->netfs_page = netpage;
246 monitor->op = fscache_get_retrieval(op);
248 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
250 /* attempt to get hold of the backing page */
251 bmapping = object->backer->d_inode->i_mapping;
255 backpage = find_get_page(bmapping, netpage->index);
257 goto backing_page_already_present;
260 newpage = page_cache_alloc_cold(bmapping);
265 ret = add_to_page_cache(newpage, bmapping,
266 netpage->index, GFP_KERNEL);
268 goto installed_new_backing_page;
273 /* we've installed a new backing page, so now we need to add it
274 * to the LRU list and start it reading */
275 installed_new_backing_page:
276 _debug("- new %p", newpage);
281 page_cache_get(backpage);
282 pagevec_add(pagevec, backpage);
283 __pagevec_lru_add_file(pagevec);
286 ret = bmapping->a_ops->readpage(NULL, backpage);
290 /* set the monitor to transfer the data across */
291 monitor_backing_page:
292 _debug("- monitor add");
294 /* install the monitor */
295 page_cache_get(monitor->netfs_page);
296 page_cache_get(backpage);
297 monitor->back_page = backpage;
298 monitor->monitor.private = backpage;
299 add_page_wait_queue(backpage, &monitor->monitor);
302 /* but the page may have been read before the monitor was installed, so
303 * the monitor may miss the event - so we have to ensure that we do get
304 * one in such a case */
305 if (trylock_page(backpage)) {
306 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
307 unlock_page(backpage);
311 /* if the backing page is already present, it can be in one of
312 * three states: read in progress, read failed or read okay */
313 backing_page_already_present:
317 page_cache_release(newpage);
321 if (PageError(backpage))
324 if (PageUptodate(backpage))
325 goto backing_page_already_uptodate;
327 if (!trylock_page(backpage))
328 goto monitor_backing_page;
329 _debug("read %p {%lx}", backpage, backpage->flags);
330 goto read_backing_page;
332 /* the backing page is already up to date, attach the netfs
333 * page to the pagecache and LRU and copy the data across */
334 backing_page_already_uptodate:
335 _debug("- uptodate");
337 fscache_mark_page_cached(op, netpage);
339 copy_highpage(netpage, backpage);
340 fscache_end_io(op, netpage, 0);
348 page_cache_release(backpage);
350 fscache_put_retrieval(monitor->op);
353 _leave(" = %d", ret);
357 _debug("read error %d", ret);
361 cachefiles_io_error_obj(object, "Page read error on backing file");
366 page_cache_release(newpage);
368 fscache_put_retrieval(monitor->op);
371 _leave(" = -ENOMEM");
376 * read a page from the cache or allocate a block in which to store it
377 * - cache withdrawal is prevented by the caller
378 * - returns -EINTR if interrupted
379 * - returns -ENOMEM if ran out of memory
380 * - returns -ENOBUFS if no buffers can be made available
381 * - returns -ENOBUFS if page is beyond EOF
382 * - if the page is backed by a block in the cache:
383 * - a read will be started which will call the callback on completion
384 * - 0 will be returned
385 * - else if the page is unbacked:
386 * - the metadata will be retained
387 * - -ENODATA will be returned
389 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
393 struct cachefiles_object *object;
394 struct cachefiles_cache *cache;
395 struct pagevec pagevec;
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 = object->backer->d_inode;
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 if (inode->i_sb->s_blocksize > PAGE_SIZE)
420 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
422 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
423 op->op.flags |= FSCACHE_OP_ASYNC;
424 op->op.processor = cachefiles_read_copier;
426 pagevec_init(&pagevec, 0);
428 /* we assume the absence or presence of the first block is a good
429 * enough indication for the page as a whole
430 * - TODO: don't use bmap() for this as it is _not_ actually good
431 * enough for this as it doesn't indicate errors, but it's all we've
434 block0 = page->index;
437 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
438 _debug("%llx -> %llx",
439 (unsigned long long) block0,
440 (unsigned long long) block);
443 /* submit the apparently valid page to the backing fs to be
445 ret = cachefiles_read_backing_file_one(object, op, page,
447 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
448 /* there's space in the cache we can use */
449 fscache_mark_page_cached(op, page);
455 _leave(" = %d", ret);
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 = object->backer->d_inode->i_mapping;
469 struct pagevec lru_pvec;
470 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
475 pagevec_init(&lru_pvec, 0);
477 list_for_each_entry_safe(netpage, _n, list, lru) {
478 list_del(&netpage->lru);
480 _debug("read back %p{%lu,%d}",
481 netpage, netpage->index, page_count(netpage));
484 monitor = kzalloc(sizeof(*monitor), GFP_KERNEL);
488 monitor->op = fscache_get_retrieval(op);
489 init_waitqueue_func_entry(&monitor->monitor,
490 cachefiles_read_waiter);
494 backpage = find_get_page(bmapping, netpage->index);
496 goto backing_page_already_present;
499 newpage = page_cache_alloc_cold(bmapping);
504 ret = add_to_page_cache(newpage, bmapping,
505 netpage->index, GFP_KERNEL);
507 goto installed_new_backing_page;
512 /* we've installed a new backing page, so now we need to add it
513 * to the LRU list and start it reading */
514 installed_new_backing_page:
515 _debug("- new %p", newpage);
520 page_cache_get(backpage);
521 if (!pagevec_add(&lru_pvec, backpage))
522 __pagevec_lru_add_file(&lru_pvec);
525 ret = bmapping->a_ops->readpage(NULL, backpage);
529 /* add the netfs page to the pagecache and LRU, and set the
530 * monitor to transfer the data across */
531 monitor_backing_page:
532 _debug("- monitor add");
534 ret = add_to_page_cache(netpage, op->mapping, netpage->index,
537 if (ret == -EEXIST) {
538 page_cache_release(netpage);
544 page_cache_get(netpage);
545 if (!pagevec_add(&lru_pvec, netpage))
546 __pagevec_lru_add_file(&lru_pvec);
548 /* install a monitor */
549 page_cache_get(netpage);
550 monitor->netfs_page = netpage;
552 page_cache_get(backpage);
553 monitor->back_page = backpage;
554 monitor->monitor.private = backpage;
555 add_page_wait_queue(backpage, &monitor->monitor);
558 /* but the page may have been read before the monitor was
559 * installed, so the monitor may miss the event - so we have to
560 * ensure that we do get one in such a case */
561 if (trylock_page(backpage)) {
562 _debug("2unlock %p {%lx}", backpage, backpage->flags);
563 unlock_page(backpage);
566 page_cache_release(backpage);
569 page_cache_release(netpage);
573 /* if the backing page is already present, it can be in one of
574 * three states: read in progress, read failed or read okay */
575 backing_page_already_present:
576 _debug("- present %p", backpage);
578 if (PageError(backpage))
581 if (PageUptodate(backpage))
582 goto backing_page_already_uptodate;
584 _debug("- not ready %p{%lx}", backpage, backpage->flags);
586 if (!trylock_page(backpage))
587 goto monitor_backing_page;
589 if (PageError(backpage)) {
590 _debug("error %lx", backpage->flags);
591 unlock_page(backpage);
595 if (PageUptodate(backpage))
596 goto backing_page_already_uptodate_unlock;
598 /* we've locked a page that's neither up to date nor erroneous,
599 * so we need to attempt to read it again */
600 goto reread_backing_page;
602 /* the backing page is already up to date, attach the netfs
603 * page to the pagecache and LRU and copy the data across */
604 backing_page_already_uptodate_unlock:
605 _debug("uptodate %lx", backpage->flags);
606 unlock_page(backpage);
607 backing_page_already_uptodate:
608 _debug("- uptodate");
610 ret = add_to_page_cache(netpage, op->mapping, netpage->index,
613 if (ret == -EEXIST) {
614 page_cache_release(netpage);
620 copy_highpage(netpage, backpage);
622 page_cache_release(backpage);
625 fscache_mark_page_cached(op, netpage);
627 page_cache_get(netpage);
628 if (!pagevec_add(&lru_pvec, netpage))
629 __pagevec_lru_add_file(&lru_pvec);
631 /* the netpage is unlocked and marked up to date here */
632 fscache_end_io(op, netpage, 0);
633 page_cache_release(netpage);
644 pagevec_lru_add_file(&lru_pvec);
647 page_cache_release(newpage);
649 page_cache_release(netpage);
651 page_cache_release(backpage);
653 fscache_put_retrieval(op);
657 list_for_each_entry_safe(netpage, _n, list, lru) {
658 list_del(&netpage->lru);
659 page_cache_release(netpage);
662 _leave(" = %d", ret);
671 _debug("read error %d", ret);
675 cachefiles_io_error_obj(object, "Page read error on backing file");
681 * read a list of pages from the cache or allocate blocks in which to store
684 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
685 struct list_head *pages,
689 struct cachefiles_object *object;
690 struct cachefiles_cache *cache;
691 struct list_head backpages;
692 struct pagevec pagevec;
694 struct page *page, *_n;
695 unsigned shift, nrbackpages;
696 int ret, ret2, space;
698 object = container_of(op->op.object,
699 struct cachefiles_object, fscache);
700 cache = container_of(object->fscache.cache,
701 struct cachefiles_cache, cache);
703 _enter("{OBJ%x,%d},,%d,,",
704 object->fscache.debug_id, atomic_read(&op->op.usage),
711 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
714 inode = object->backer->d_inode;
715 ASSERT(S_ISREG(inode->i_mode));
716 ASSERT(inode->i_mapping->a_ops->bmap);
717 ASSERT(inode->i_mapping->a_ops->readpages);
719 /* calculate the shift required to use bmap */
720 if (inode->i_sb->s_blocksize > PAGE_SIZE)
723 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
725 pagevec_init(&pagevec, 0);
727 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
728 op->op.flags |= FSCACHE_OP_ASYNC;
729 op->op.processor = cachefiles_read_copier;
731 INIT_LIST_HEAD(&backpages);
734 ret = space ? -ENODATA : -ENOBUFS;
735 list_for_each_entry_safe(page, _n, pages, lru) {
736 sector_t block0, block;
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
744 block0 = page->index;
747 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
749 _debug("%llx -> %llx",
750 (unsigned long long) block0,
751 (unsigned long long) block);
754 /* we have data - add it to the list to give to the
756 list_move(&page->lru, &backpages);
759 } else if (space && pagevec_add(&pagevec, page) == 0) {
760 fscache_mark_pages_cached(op, &pagevec);
765 if (pagevec_count(&pagevec) > 0)
766 fscache_mark_pages_cached(op, &pagevec);
768 if (list_empty(pages))
771 /* submit the apparently valid pages to the backing fs to be read from
773 if (nrbackpages > 0) {
774 ret2 = cachefiles_read_backing_file(object, op, &backpages);
775 if (ret2 == -ENOMEM || ret2 == -EINTR)
779 _leave(" = %d [nr=%u%s]",
780 ret, *nr_pages, list_empty(pages) ? " empty" : "");
785 * allocate a block in the cache in which to store a page
786 * - cache withdrawal is prevented by the caller
787 * - returns -EINTR if interrupted
788 * - returns -ENOMEM if ran out of memory
789 * - returns -ENOBUFS if no buffers can be made available
790 * - returns -ENOBUFS if page is beyond EOF
792 * - the metadata will be retained
793 * - 0 will be returned
795 int cachefiles_allocate_page(struct fscache_retrieval *op,
799 struct cachefiles_object *object;
800 struct cachefiles_cache *cache;
803 object = container_of(op->op.object,
804 struct cachefiles_object, fscache);
805 cache = container_of(object->fscache.cache,
806 struct cachefiles_cache, cache);
808 _enter("%p,{%lx},", object, page->index);
810 ret = cachefiles_has_space(cache, 0, 1);
812 fscache_mark_page_cached(op, page);
816 _leave(" = %d", ret);
821 * allocate blocks in the cache in which to store a set of pages
822 * - cache withdrawal is prevented by the caller
823 * - returns -EINTR if interrupted
824 * - returns -ENOMEM if ran out of memory
825 * - returns -ENOBUFS if some buffers couldn't be made available
826 * - returns -ENOBUFS if some pages are beyond EOF
828 * - -ENODATA will be returned
829 * - metadata will be retained for any page marked
831 int cachefiles_allocate_pages(struct fscache_retrieval *op,
832 struct list_head *pages,
836 struct cachefiles_object *object;
837 struct cachefiles_cache *cache;
838 struct pagevec pagevec;
842 object = container_of(op->op.object,
843 struct cachefiles_object, fscache);
844 cache = container_of(object->fscache.cache,
845 struct cachefiles_cache, cache);
847 _enter("%p,,,%d,", object, *nr_pages);
849 ret = cachefiles_has_space(cache, 0, *nr_pages);
851 pagevec_init(&pagevec, 0);
853 list_for_each_entry(page, pages, lru) {
854 if (pagevec_add(&pagevec, page) == 0)
855 fscache_mark_pages_cached(op, &pagevec);
858 if (pagevec_count(&pagevec) > 0)
859 fscache_mark_pages_cached(op, &pagevec);
865 _leave(" = %d", ret);
870 * request a page be stored in the cache
871 * - cache withdrawal is prevented by the caller
872 * - this request may be ignored if there's no cache block available, in which
873 * case -ENOBUFS will be returned
874 * - if the op is in progress, 0 will be returned
876 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
878 struct cachefiles_object *object;
879 struct cachefiles_cache *cache;
889 ASSERT(page != NULL);
891 object = container_of(op->op.object,
892 struct cachefiles_object, fscache);
894 _enter("%p,%p{%lx},,,", object, page, page->index);
896 if (!object->backer) {
897 _leave(" = -ENOBUFS");
901 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
903 cache = container_of(object->fscache.cache,
904 struct cachefiles_cache, cache);
906 /* write the page to the backing filesystem and let it store it in its
908 path.mnt = cache->mnt;
909 path.dentry = object->backer;
910 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
915 if (file->f_op->write) {
916 pos = (loff_t) page->index << PAGE_SHIFT;
918 /* we mustn't write more data than we have, so we have
919 * to beware of a partial page at EOF */
920 eof = object->fscache.store_limit_l;
922 if (eof & ~PAGE_MASK) {
923 ASSERTCMP(pos, <, eof);
924 if (eof - pos < PAGE_SIZE) {
925 _debug("cut short %llx to %llx",
928 ASSERTCMP(pos + len, ==, eof);
935 ret = file->f_op->write(
936 file, (const void __user *) data, len, &pos);
947 cachefiles_io_error_obj(
948 object, "Write page to backing file failed");
952 _leave(" = %d", ret);
957 * detach a backing block from a page
958 * - cache withdrawal is prevented by the caller
960 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
962 struct cachefiles_object *object;
963 struct cachefiles_cache *cache;
965 object = container_of(_object, struct cachefiles_object, fscache);
966 cache = container_of(object->fscache.cache,
967 struct cachefiles_cache, cache);
969 _enter("%p,{%lu}", object, page->index);
971 spin_unlock(&object->fscache.cookie->lock);