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/file.h>
17 * detect wake up events generated by the unlocking of pages in which we're
19 * - we use this to detect read completion of backing pages
20 * - the caller holds the waitqueue lock
22 static int cachefiles_read_waiter(wait_queue_t *wait, unsigned mode,
25 struct cachefiles_one_read *monitor =
26 container_of(wait, struct cachefiles_one_read, monitor);
27 struct cachefiles_object *object;
28 struct wait_bit_key *key = _key;
29 struct page *page = wait->private;
33 _enter("{%lu},%u,%d,{%p,%u}",
34 monitor->netfs_page->index, mode, sync,
35 key->flags, key->bit_nr);
37 if (key->flags != &page->flags ||
38 key->bit_nr != PG_locked)
41 _debug("--- monitor %p %lx ---", page, page->flags);
43 if (!PageUptodate(page) && !PageError(page))
46 /* remove from the waitqueue */
47 list_del(&wait->task_list);
49 /* move onto the action list and queue for FS-Cache thread pool */
52 object = container_of(monitor->op->op.object,
53 struct cachefiles_object, fscache);
55 spin_lock(&object->work_lock);
56 list_add_tail(&monitor->op_link, &monitor->op->to_do);
57 spin_unlock(&object->work_lock);
59 fscache_enqueue_retrieval(monitor->op);
64 * copy data from backing pages to netfs pages to complete a read operation
65 * - driven by FS-Cache's thread pool
67 static void cachefiles_read_copier(struct fscache_operation *_op)
69 struct cachefiles_one_read *monitor;
70 struct cachefiles_object *object;
71 struct fscache_retrieval *op;
72 struct pagevec pagevec;
75 op = container_of(_op, struct fscache_retrieval, op);
76 object = container_of(op->op.object,
77 struct cachefiles_object, fscache);
79 _enter("{ino=%lu}", object->backer->d_inode->i_ino);
81 pagevec_init(&pagevec, 0);
84 spin_lock_irq(&object->work_lock);
86 while (!list_empty(&op->to_do)) {
87 monitor = list_entry(op->to_do.next,
88 struct cachefiles_one_read, op_link);
89 list_del(&monitor->op_link);
91 spin_unlock_irq(&object->work_lock);
93 _debug("- copy {%lu}", monitor->back_page->index);
96 if (PageUptodate(monitor->back_page)) {
97 copy_highpage(monitor->netfs_page, monitor->back_page);
99 pagevec_add(&pagevec, monitor->netfs_page);
100 fscache_mark_pages_cached(monitor->op, &pagevec);
105 cachefiles_io_error_obj(
107 "Readpage failed on backing file %lx",
108 (unsigned long) monitor->back_page->flags);
110 page_cache_release(monitor->back_page);
112 fscache_end_io(op, monitor->netfs_page, error);
113 page_cache_release(monitor->netfs_page);
114 fscache_put_retrieval(op);
117 /* let the thread pool have some air occasionally */
119 if (max < 0 || need_resched()) {
120 if (!list_empty(&op->to_do))
121 fscache_enqueue_retrieval(op);
122 _leave(" [maxed out]");
126 spin_lock_irq(&object->work_lock);
129 spin_unlock_irq(&object->work_lock);
134 * read the corresponding page to the given set from the backing file
135 * - an uncertain page is simply discarded, to be tried again another time
137 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
138 struct fscache_retrieval *op,
139 struct page *netpage,
140 struct pagevec *pagevec)
142 struct cachefiles_one_read *monitor;
143 struct address_space *bmapping;
144 struct page *newpage, *backpage;
149 pagevec_reinit(pagevec);
151 _debug("read back %p{%lu,%d}",
152 netpage, netpage->index, page_count(netpage));
154 monitor = kzalloc(sizeof(*monitor), GFP_KERNEL);
158 monitor->netfs_page = netpage;
159 monitor->op = fscache_get_retrieval(op);
161 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
163 /* attempt to get hold of the backing page */
164 bmapping = object->backer->d_inode->i_mapping;
168 backpage = find_get_page(bmapping, netpage->index);
170 goto backing_page_already_present;
173 newpage = page_cache_alloc_cold(bmapping);
178 ret = add_to_page_cache(newpage, bmapping,
179 netpage->index, GFP_KERNEL);
181 goto installed_new_backing_page;
186 /* we've installed a new backing page, so now we need to add it
187 * to the LRU list and start it reading */
188 installed_new_backing_page:
189 _debug("- new %p", newpage);
194 page_cache_get(backpage);
195 pagevec_add(pagevec, backpage);
196 __pagevec_lru_add_file(pagevec);
199 ret = bmapping->a_ops->readpage(NULL, backpage);
203 /* set the monitor to transfer the data across */
204 monitor_backing_page:
205 _debug("- monitor add");
207 /* install the monitor */
208 page_cache_get(monitor->netfs_page);
209 page_cache_get(backpage);
210 monitor->back_page = backpage;
211 monitor->monitor.private = backpage;
212 add_page_wait_queue(backpage, &monitor->monitor);
215 /* but the page may have been read before the monitor was installed, so
216 * the monitor may miss the event - so we have to ensure that we do get
217 * one in such a case */
218 if (trylock_page(backpage)) {
219 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
220 unlock_page(backpage);
224 /* if the backing page is already present, it can be in one of
225 * three states: read in progress, read failed or read okay */
226 backing_page_already_present:
230 page_cache_release(newpage);
234 if (PageError(backpage))
237 if (PageUptodate(backpage))
238 goto backing_page_already_uptodate;
240 if (!trylock_page(backpage))
241 goto monitor_backing_page;
242 _debug("read %p {%lx}", backpage, backpage->flags);
243 goto read_backing_page;
245 /* the backing page is already up to date, attach the netfs
246 * page to the pagecache and LRU and copy the data across */
247 backing_page_already_uptodate:
248 _debug("- uptodate");
250 pagevec_add(pagevec, netpage);
251 fscache_mark_pages_cached(op, pagevec);
253 copy_highpage(netpage, backpage);
254 fscache_end_io(op, netpage, 0);
262 page_cache_release(backpage);
264 fscache_put_retrieval(monitor->op);
267 _leave(" = %d", ret);
271 _debug("read error %d", ret);
275 cachefiles_io_error_obj(object, "Page read error on backing file");
280 page_cache_release(newpage);
282 fscache_put_retrieval(monitor->op);
285 _leave(" = -ENOMEM");
290 * read a page from the cache or allocate a block in which to store it
291 * - cache withdrawal is prevented by the caller
292 * - returns -EINTR if interrupted
293 * - returns -ENOMEM if ran out of memory
294 * - returns -ENOBUFS if no buffers can be made available
295 * - returns -ENOBUFS if page is beyond EOF
296 * - if the page is backed by a block in the cache:
297 * - a read will be started which will call the callback on completion
298 * - 0 will be returned
299 * - else if the page is unbacked:
300 * - the metadata will be retained
301 * - -ENODATA will be returned
303 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
307 struct cachefiles_object *object;
308 struct cachefiles_cache *cache;
309 struct pagevec pagevec;
311 sector_t block0, block;
315 object = container_of(op->op.object,
316 struct cachefiles_object, fscache);
317 cache = container_of(object->fscache.cache,
318 struct cachefiles_cache, cache);
320 _enter("{%p},{%lx},,,", object, page->index);
325 inode = object->backer->d_inode;
326 ASSERT(S_ISREG(inode->i_mode));
327 ASSERT(inode->i_mapping->a_ops->bmap);
328 ASSERT(inode->i_mapping->a_ops->readpages);
330 /* calculate the shift required to use bmap */
331 if (inode->i_sb->s_blocksize > PAGE_SIZE)
334 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
336 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
337 op->op.flags |= FSCACHE_OP_FAST;
338 op->op.processor = cachefiles_read_copier;
340 pagevec_init(&pagevec, 0);
342 /* we assume the absence or presence of the first block is a good
343 * enough indication for the page as a whole
344 * - TODO: don't use bmap() for this as it is _not_ actually good
345 * enough for this as it doesn't indicate errors, but it's all we've
348 block0 = page->index;
351 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
352 _debug("%llx -> %llx",
353 (unsigned long long) block0,
354 (unsigned long long) block);
357 /* submit the apparently valid page to the backing fs to be
359 ret = cachefiles_read_backing_file_one(object, op, page,
361 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
362 /* there's space in the cache we can use */
363 pagevec_add(&pagevec, page);
364 fscache_mark_pages_cached(op, &pagevec);
370 _leave(" = %d", ret);
375 * read the corresponding pages to the given set from the backing file
376 * - any uncertain pages are simply discarded, to be tried again another time
378 static int cachefiles_read_backing_file(struct cachefiles_object *object,
379 struct fscache_retrieval *op,
380 struct list_head *list,
381 struct pagevec *mark_pvec)
383 struct cachefiles_one_read *monitor = NULL;
384 struct address_space *bmapping = object->backer->d_inode->i_mapping;
385 struct pagevec lru_pvec;
386 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
391 pagevec_init(&lru_pvec, 0);
393 list_for_each_entry_safe(netpage, _n, list, lru) {
394 list_del(&netpage->lru);
396 _debug("read back %p{%lu,%d}",
397 netpage, netpage->index, page_count(netpage));
400 monitor = kzalloc(sizeof(*monitor), GFP_KERNEL);
404 monitor->op = fscache_get_retrieval(op);
405 init_waitqueue_func_entry(&monitor->monitor,
406 cachefiles_read_waiter);
410 backpage = find_get_page(bmapping, netpage->index);
412 goto backing_page_already_present;
415 newpage = page_cache_alloc_cold(bmapping);
420 ret = add_to_page_cache(newpage, bmapping,
421 netpage->index, GFP_KERNEL);
423 goto installed_new_backing_page;
428 /* we've installed a new backing page, so now we need to add it
429 * to the LRU list and start it reading */
430 installed_new_backing_page:
431 _debug("- new %p", newpage);
436 page_cache_get(backpage);
437 if (!pagevec_add(&lru_pvec, backpage))
438 __pagevec_lru_add_file(&lru_pvec);
441 ret = bmapping->a_ops->readpage(NULL, backpage);
445 /* add the netfs page to the pagecache and LRU, and set the
446 * monitor to transfer the data across */
447 monitor_backing_page:
448 _debug("- monitor add");
450 ret = add_to_page_cache(netpage, op->mapping, netpage->index,
453 if (ret == -EEXIST) {
454 page_cache_release(netpage);
460 page_cache_get(netpage);
461 if (!pagevec_add(&lru_pvec, netpage))
462 __pagevec_lru_add_file(&lru_pvec);
464 /* install a monitor */
465 page_cache_get(netpage);
466 monitor->netfs_page = netpage;
468 page_cache_get(backpage);
469 monitor->back_page = backpage;
470 monitor->monitor.private = backpage;
471 add_page_wait_queue(backpage, &monitor->monitor);
474 /* but the page may have been read before the monitor was
475 * installed, so the monitor may miss the event - so we have to
476 * ensure that we do get one in such a case */
477 if (trylock_page(backpage)) {
478 _debug("2unlock %p {%lx}", backpage, backpage->flags);
479 unlock_page(backpage);
482 page_cache_release(backpage);
485 page_cache_release(netpage);
489 /* if the backing page is already present, it can be in one of
490 * three states: read in progress, read failed or read okay */
491 backing_page_already_present:
492 _debug("- present %p", backpage);
494 if (PageError(backpage))
497 if (PageUptodate(backpage))
498 goto backing_page_already_uptodate;
500 _debug("- not ready %p{%lx}", backpage, backpage->flags);
502 if (!trylock_page(backpage))
503 goto monitor_backing_page;
505 if (PageError(backpage)) {
506 _debug("error %lx", backpage->flags);
507 unlock_page(backpage);
511 if (PageUptodate(backpage))
512 goto backing_page_already_uptodate_unlock;
514 /* we've locked a page that's neither up to date nor erroneous,
515 * so we need to attempt to read it again */
516 goto reread_backing_page;
518 /* the backing page is already up to date, attach the netfs
519 * page to the pagecache and LRU and copy the data across */
520 backing_page_already_uptodate_unlock:
521 _debug("uptodate %lx", backpage->flags);
522 unlock_page(backpage);
523 backing_page_already_uptodate:
524 _debug("- uptodate");
526 ret = add_to_page_cache(netpage, op->mapping, netpage->index,
529 if (ret == -EEXIST) {
530 page_cache_release(netpage);
536 copy_highpage(netpage, backpage);
538 page_cache_release(backpage);
541 if (!pagevec_add(mark_pvec, netpage))
542 fscache_mark_pages_cached(op, mark_pvec);
544 page_cache_get(netpage);
545 if (!pagevec_add(&lru_pvec, netpage))
546 __pagevec_lru_add_file(&lru_pvec);
548 fscache_end_io(op, netpage, 0);
549 page_cache_release(netpage);
560 pagevec_lru_add_file(&lru_pvec);
563 page_cache_release(newpage);
565 page_cache_release(netpage);
567 page_cache_release(backpage);
569 fscache_put_retrieval(op);
573 list_for_each_entry_safe(netpage, _n, list, lru) {
574 list_del(&netpage->lru);
575 page_cache_release(netpage);
578 _leave(" = %d", ret);
587 _debug("read error %d", ret);
591 cachefiles_io_error_obj(object, "Page read error on backing file");
597 * read a list of pages from the cache or allocate blocks in which to store
600 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
601 struct list_head *pages,
605 struct cachefiles_object *object;
606 struct cachefiles_cache *cache;
607 struct list_head backpages;
608 struct pagevec pagevec;
610 struct page *page, *_n;
611 unsigned shift, nrbackpages;
612 int ret, ret2, space;
614 object = container_of(op->op.object,
615 struct cachefiles_object, fscache);
616 cache = container_of(object->fscache.cache,
617 struct cachefiles_cache, cache);
619 _enter("{OBJ%x,%d},,%d,,",
620 object->fscache.debug_id, atomic_read(&op->op.usage),
627 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
630 inode = object->backer->d_inode;
631 ASSERT(S_ISREG(inode->i_mode));
632 ASSERT(inode->i_mapping->a_ops->bmap);
633 ASSERT(inode->i_mapping->a_ops->readpages);
635 /* calculate the shift required to use bmap */
636 if (inode->i_sb->s_blocksize > PAGE_SIZE)
639 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
641 pagevec_init(&pagevec, 0);
643 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
644 op->op.flags |= FSCACHE_OP_FAST;
645 op->op.processor = cachefiles_read_copier;
647 INIT_LIST_HEAD(&backpages);
650 ret = space ? -ENODATA : -ENOBUFS;
651 list_for_each_entry_safe(page, _n, pages, lru) {
652 sector_t block0, block;
654 /* we assume the absence or presence of the first block is a
655 * good enough indication for the page as a whole
656 * - TODO: don't use bmap() for this as it is _not_ actually
657 * good enough for this as it doesn't indicate errors, but
658 * it's all we've got for the moment
660 block0 = page->index;
663 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
665 _debug("%llx -> %llx",
666 (unsigned long long) block0,
667 (unsigned long long) block);
670 /* we have data - add it to the list to give to the
672 list_move(&page->lru, &backpages);
675 } else if (space && pagevec_add(&pagevec, page) == 0) {
676 fscache_mark_pages_cached(op, &pagevec);
681 if (pagevec_count(&pagevec) > 0)
682 fscache_mark_pages_cached(op, &pagevec);
684 if (list_empty(pages))
687 /* submit the apparently valid pages to the backing fs to be read from
689 if (nrbackpages > 0) {
690 ret2 = cachefiles_read_backing_file(object, op, &backpages,
692 if (ret2 == -ENOMEM || ret2 == -EINTR)
696 if (pagevec_count(&pagevec) > 0)
697 fscache_mark_pages_cached(op, &pagevec);
699 _leave(" = %d [nr=%u%s]",
700 ret, *nr_pages, list_empty(pages) ? " empty" : "");
705 * allocate a block in the cache in which to store a page
706 * - cache withdrawal is prevented by the caller
707 * - returns -EINTR if interrupted
708 * - returns -ENOMEM if ran out of memory
709 * - returns -ENOBUFS if no buffers can be made available
710 * - returns -ENOBUFS if page is beyond EOF
712 * - the metadata will be retained
713 * - 0 will be returned
715 int cachefiles_allocate_page(struct fscache_retrieval *op,
719 struct cachefiles_object *object;
720 struct cachefiles_cache *cache;
721 struct pagevec pagevec;
724 object = container_of(op->op.object,
725 struct cachefiles_object, fscache);
726 cache = container_of(object->fscache.cache,
727 struct cachefiles_cache, cache);
729 _enter("%p,{%lx},", object, page->index);
731 ret = cachefiles_has_space(cache, 0, 1);
733 pagevec_init(&pagevec, 0);
734 pagevec_add(&pagevec, page);
735 fscache_mark_pages_cached(op, &pagevec);
740 _leave(" = %d", ret);
745 * allocate blocks in the cache in which to store a set of pages
746 * - cache withdrawal is prevented by the caller
747 * - returns -EINTR if interrupted
748 * - returns -ENOMEM if ran out of memory
749 * - returns -ENOBUFS if some buffers couldn't be made available
750 * - returns -ENOBUFS if some pages are beyond EOF
752 * - -ENODATA will be returned
753 * - metadata will be retained for any page marked
755 int cachefiles_allocate_pages(struct fscache_retrieval *op,
756 struct list_head *pages,
760 struct cachefiles_object *object;
761 struct cachefiles_cache *cache;
762 struct pagevec pagevec;
766 object = container_of(op->op.object,
767 struct cachefiles_object, fscache);
768 cache = container_of(object->fscache.cache,
769 struct cachefiles_cache, cache);
771 _enter("%p,,,%d,", object, *nr_pages);
773 ret = cachefiles_has_space(cache, 0, *nr_pages);
775 pagevec_init(&pagevec, 0);
777 list_for_each_entry(page, pages, lru) {
778 if (pagevec_add(&pagevec, page) == 0)
779 fscache_mark_pages_cached(op, &pagevec);
782 if (pagevec_count(&pagevec) > 0)
783 fscache_mark_pages_cached(op, &pagevec);
789 _leave(" = %d", ret);
794 * request a page be stored in the cache
795 * - cache withdrawal is prevented by the caller
796 * - this request may be ignored if there's no cache block available, in which
797 * case -ENOBUFS will be returned
798 * - if the op is in progress, 0 will be returned
800 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
802 struct cachefiles_object *object;
803 struct cachefiles_cache *cache;
811 ASSERT(page != NULL);
813 object = container_of(op->op.object,
814 struct cachefiles_object, fscache);
816 _enter("%p,%p{%lx},,,", object, page, page->index);
818 if (!object->backer) {
819 _leave(" = -ENOBUFS");
823 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
825 cache = container_of(object->fscache.cache,
826 struct cachefiles_cache, cache);
828 /* write the page to the backing filesystem and let it store it in its
830 dget(object->backer);
832 file = dentry_open(object->backer, cache->mnt, O_RDWR,
838 if (file->f_op->write) {
839 pos = (loff_t) page->index << PAGE_SHIFT;
843 ret = file->f_op->write(
844 file, (const void __user *) data, PAGE_SIZE,
848 if (ret != PAGE_SIZE)
856 cachefiles_io_error_obj(
857 object, "Write page to backing file failed");
861 _leave(" = %d", ret);
866 * detach a backing block from a page
867 * - cache withdrawal is prevented by the caller
869 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
871 struct cachefiles_object *object;
872 struct cachefiles_cache *cache;
874 object = container_of(_object, struct cachefiles_object, fscache);
875 cache = container_of(object->fscache.cache,
876 struct cachefiles_cache, cache);
878 _enter("%p,{%lu}", object, page->index);
880 spin_unlock(&object->fscache.cookie->lock);