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);
180 pagevec_add(&pagevec, monitor->netfs_page);
181 fscache_mark_pages_cached(monitor->op, &pagevec);
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_put_retrieval(op);
205 /* let the thread pool have some air occasionally */
207 if (max < 0 || need_resched()) {
208 if (!list_empty(&op->to_do))
209 fscache_enqueue_retrieval(op);
210 _leave(" [maxed out]");
214 spin_lock_irq(&object->work_lock);
217 spin_unlock_irq(&object->work_lock);
222 * read the corresponding page to the given set from the backing file
223 * - an uncertain page is simply discarded, to be tried again another time
225 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
226 struct fscache_retrieval *op,
227 struct page *netpage,
228 struct pagevec *pagevec)
230 struct cachefiles_one_read *monitor;
231 struct address_space *bmapping;
232 struct page *newpage, *backpage;
237 pagevec_reinit(pagevec);
239 _debug("read back %p{%lu,%d}",
240 netpage, netpage->index, page_count(netpage));
242 monitor = kzalloc(sizeof(*monitor), GFP_KERNEL);
246 monitor->netfs_page = netpage;
247 monitor->op = fscache_get_retrieval(op);
249 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
251 /* attempt to get hold of the backing page */
252 bmapping = object->backer->d_inode->i_mapping;
256 backpage = find_get_page(bmapping, netpage->index);
258 goto backing_page_already_present;
261 newpage = page_cache_alloc_cold(bmapping);
266 ret = add_to_page_cache(newpage, bmapping,
267 netpage->index, GFP_KERNEL);
269 goto installed_new_backing_page;
274 /* we've installed a new backing page, so now we need to add it
275 * to the LRU list and start it reading */
276 installed_new_backing_page:
277 _debug("- new %p", newpage);
282 page_cache_get(backpage);
283 pagevec_add(pagevec, backpage);
284 __pagevec_lru_add_file(pagevec);
287 ret = bmapping->a_ops->readpage(NULL, backpage);
291 /* set the monitor to transfer the data across */
292 monitor_backing_page:
293 _debug("- monitor add");
295 /* install the monitor */
296 page_cache_get(monitor->netfs_page);
297 page_cache_get(backpage);
298 monitor->back_page = backpage;
299 monitor->monitor.private = backpage;
300 add_page_wait_queue(backpage, &monitor->monitor);
303 /* but the page may have been read before the monitor was installed, so
304 * the monitor may miss the event - so we have to ensure that we do get
305 * one in such a case */
306 if (trylock_page(backpage)) {
307 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
308 unlock_page(backpage);
312 /* if the backing page is already present, it can be in one of
313 * three states: read in progress, read failed or read okay */
314 backing_page_already_present:
318 page_cache_release(newpage);
322 if (PageError(backpage))
325 if (PageUptodate(backpage))
326 goto backing_page_already_uptodate;
328 if (!trylock_page(backpage))
329 goto monitor_backing_page;
330 _debug("read %p {%lx}", backpage, backpage->flags);
331 goto read_backing_page;
333 /* the backing page is already up to date, attach the netfs
334 * page to the pagecache and LRU and copy the data across */
335 backing_page_already_uptodate:
336 _debug("- uptodate");
338 pagevec_add(pagevec, netpage);
339 fscache_mark_pages_cached(op, pagevec);
341 copy_highpage(netpage, backpage);
342 fscache_end_io(op, netpage, 0);
350 page_cache_release(backpage);
352 fscache_put_retrieval(monitor->op);
355 _leave(" = %d", ret);
359 _debug("read error %d", ret);
363 cachefiles_io_error_obj(object, "Page read error on backing file");
368 page_cache_release(newpage);
370 fscache_put_retrieval(monitor->op);
373 _leave(" = -ENOMEM");
378 * read a page from the cache or allocate a block in which to store it
379 * - cache withdrawal is prevented by the caller
380 * - returns -EINTR if interrupted
381 * - returns -ENOMEM if ran out of memory
382 * - returns -ENOBUFS if no buffers can be made available
383 * - returns -ENOBUFS if page is beyond EOF
384 * - if the page is backed by a block in the cache:
385 * - a read will be started which will call the callback on completion
386 * - 0 will be returned
387 * - else if the page is unbacked:
388 * - the metadata will be retained
389 * - -ENODATA will be returned
391 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
395 struct cachefiles_object *object;
396 struct cachefiles_cache *cache;
397 struct pagevec pagevec;
399 sector_t block0, block;
403 object = container_of(op->op.object,
404 struct cachefiles_object, fscache);
405 cache = container_of(object->fscache.cache,
406 struct cachefiles_cache, cache);
408 _enter("{%p},{%lx},,,", object, page->index);
413 inode = object->backer->d_inode;
414 ASSERT(S_ISREG(inode->i_mode));
415 ASSERT(inode->i_mapping->a_ops->bmap);
416 ASSERT(inode->i_mapping->a_ops->readpages);
418 /* calculate the shift required to use bmap */
419 if (inode->i_sb->s_blocksize > PAGE_SIZE)
422 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
424 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
425 op->op.flags |= FSCACHE_OP_ASYNC;
426 op->op.processor = cachefiles_read_copier;
428 pagevec_init(&pagevec, 0);
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,
449 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
450 /* there's space in the cache we can use */
451 pagevec_add(&pagevec, page);
452 fscache_mark_pages_cached(op, &pagevec);
458 _leave(" = %d", ret);
463 * read the corresponding pages to the given set from the backing file
464 * - any uncertain pages are simply discarded, to be tried again another time
466 static int cachefiles_read_backing_file(struct cachefiles_object *object,
467 struct fscache_retrieval *op,
468 struct list_head *list,
469 struct pagevec *mark_pvec)
471 struct cachefiles_one_read *monitor = NULL;
472 struct address_space *bmapping = object->backer->d_inode->i_mapping;
473 struct pagevec lru_pvec;
474 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
479 pagevec_init(&lru_pvec, 0);
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), GFP_KERNEL);
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_cold(bmapping);
508 ret = add_to_page_cache(newpage, bmapping,
509 netpage->index, GFP_KERNEL);
511 goto installed_new_backing_page;
516 /* we've installed a new backing page, so now we need to add it
517 * to the LRU list and start it reading */
518 installed_new_backing_page:
519 _debug("- new %p", newpage);
524 page_cache_get(backpage);
525 if (!pagevec_add(&lru_pvec, backpage))
526 __pagevec_lru_add_file(&lru_pvec);
529 ret = bmapping->a_ops->readpage(NULL, backpage);
533 /* add the netfs page to the pagecache and LRU, and set the
534 * monitor to transfer the data across */
535 monitor_backing_page:
536 _debug("- monitor add");
538 ret = add_to_page_cache(netpage, op->mapping, netpage->index,
541 if (ret == -EEXIST) {
542 page_cache_release(netpage);
548 page_cache_get(netpage);
549 if (!pagevec_add(&lru_pvec, netpage))
550 __pagevec_lru_add_file(&lru_pvec);
552 /* install a monitor */
553 page_cache_get(netpage);
554 monitor->netfs_page = netpage;
556 page_cache_get(backpage);
557 monitor->back_page = backpage;
558 monitor->monitor.private = backpage;
559 add_page_wait_queue(backpage, &monitor->monitor);
562 /* but the page may have been read before the monitor was
563 * installed, so the monitor may miss the event - so we have to
564 * ensure that we do get one in such a case */
565 if (trylock_page(backpage)) {
566 _debug("2unlock %p {%lx}", backpage, backpage->flags);
567 unlock_page(backpage);
570 page_cache_release(backpage);
573 page_cache_release(netpage);
577 /* if the backing page is already present, it can be in one of
578 * three states: read in progress, read failed or read okay */
579 backing_page_already_present:
580 _debug("- present %p", backpage);
582 if (PageError(backpage))
585 if (PageUptodate(backpage))
586 goto backing_page_already_uptodate;
588 _debug("- not ready %p{%lx}", backpage, backpage->flags);
590 if (!trylock_page(backpage))
591 goto monitor_backing_page;
593 if (PageError(backpage)) {
594 _debug("error %lx", backpage->flags);
595 unlock_page(backpage);
599 if (PageUptodate(backpage))
600 goto backing_page_already_uptodate_unlock;
602 /* we've locked a page that's neither up to date nor erroneous,
603 * so we need to attempt to read it again */
604 goto reread_backing_page;
606 /* the backing page is already up to date, attach the netfs
607 * page to the pagecache and LRU and copy the data across */
608 backing_page_already_uptodate_unlock:
609 _debug("uptodate %lx", backpage->flags);
610 unlock_page(backpage);
611 backing_page_already_uptodate:
612 _debug("- uptodate");
614 ret = add_to_page_cache(netpage, op->mapping, netpage->index,
617 if (ret == -EEXIST) {
618 page_cache_release(netpage);
624 copy_highpage(netpage, backpage);
626 page_cache_release(backpage);
629 if (!pagevec_add(mark_pvec, netpage))
630 fscache_mark_pages_cached(op, mark_pvec);
632 page_cache_get(netpage);
633 if (!pagevec_add(&lru_pvec, netpage))
634 __pagevec_lru_add_file(&lru_pvec);
636 fscache_end_io(op, netpage, 0);
637 page_cache_release(netpage);
648 pagevec_lru_add_file(&lru_pvec);
651 page_cache_release(newpage);
653 page_cache_release(netpage);
655 page_cache_release(backpage);
657 fscache_put_retrieval(op);
661 list_for_each_entry_safe(netpage, _n, list, lru) {
662 list_del(&netpage->lru);
663 page_cache_release(netpage);
666 _leave(" = %d", ret);
675 _debug("read error %d", ret);
679 cachefiles_io_error_obj(object, "Page read error on backing file");
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 = object->backer->d_inode;
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 if (inode->i_sb->s_blocksize > PAGE_SIZE)
727 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
729 pagevec_init(&pagevec, 0);
731 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
732 op->op.flags |= FSCACHE_OP_ASYNC;
733 op->op.processor = cachefiles_read_copier;
735 INIT_LIST_HEAD(&backpages);
738 ret = space ? -ENODATA : -ENOBUFS;
739 list_for_each_entry_safe(page, _n, pages, lru) {
740 sector_t block0, block;
742 /* we assume the absence or presence of the first block is a
743 * good enough indication for the page as a whole
744 * - TODO: don't use bmap() for this as it is _not_ actually
745 * good enough for this as it doesn't indicate errors, but
746 * it's all we've got for the moment
748 block0 = page->index;
751 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
753 _debug("%llx -> %llx",
754 (unsigned long long) block0,
755 (unsigned long long) block);
758 /* we have data - add it to the list to give to the
760 list_move(&page->lru, &backpages);
763 } else if (space && pagevec_add(&pagevec, page) == 0) {
764 fscache_mark_pages_cached(op, &pagevec);
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,
780 if (ret2 == -ENOMEM || ret2 == -EINTR)
784 if (pagevec_count(&pagevec) > 0)
785 fscache_mark_pages_cached(op, &pagevec);
787 _leave(" = %d [nr=%u%s]",
788 ret, *nr_pages, list_empty(pages) ? " empty" : "");
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;
809 struct pagevec pagevec;
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 pagevec_init(&pagevec, 0);
822 pagevec_add(&pagevec, page);
823 fscache_mark_pages_cached(op, &pagevec);
828 _leave(" = %d", ret);
833 * allocate blocks in the cache in which to store a set of pages
834 * - cache withdrawal is prevented by the caller
835 * - returns -EINTR if interrupted
836 * - returns -ENOMEM if ran out of memory
837 * - returns -ENOBUFS if some buffers couldn't be made available
838 * - returns -ENOBUFS if some pages are beyond EOF
840 * - -ENODATA will be returned
841 * - metadata will be retained for any page marked
843 int cachefiles_allocate_pages(struct fscache_retrieval *op,
844 struct list_head *pages,
848 struct cachefiles_object *object;
849 struct cachefiles_cache *cache;
850 struct pagevec pagevec;
854 object = container_of(op->op.object,
855 struct cachefiles_object, fscache);
856 cache = container_of(object->fscache.cache,
857 struct cachefiles_cache, cache);
859 _enter("%p,,,%d,", object, *nr_pages);
861 ret = cachefiles_has_space(cache, 0, *nr_pages);
863 pagevec_init(&pagevec, 0);
865 list_for_each_entry(page, pages, lru) {
866 if (pagevec_add(&pagevec, page) == 0)
867 fscache_mark_pages_cached(op, &pagevec);
870 if (pagevec_count(&pagevec) > 0)
871 fscache_mark_pages_cached(op, &pagevec);
877 _leave(" = %d", ret);
882 * request a page be stored in the cache
883 * - cache withdrawal is prevented by the caller
884 * - this request may be ignored if there's no cache block available, in which
885 * case -ENOBUFS will be returned
886 * - if the op is in progress, 0 will be returned
888 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
890 struct cachefiles_object *object;
891 struct cachefiles_cache *cache;
900 ASSERT(page != NULL);
902 object = container_of(op->op.object,
903 struct cachefiles_object, fscache);
905 _enter("%p,%p{%lx},,,", object, page, page->index);
907 if (!object->backer) {
908 _leave(" = -ENOBUFS");
912 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
914 cache = container_of(object->fscache.cache,
915 struct cachefiles_cache, cache);
917 /* write the page to the backing filesystem and let it store it in its
919 dget(object->backer);
921 file = dentry_open(object->backer, cache->mnt, O_RDWR,
927 if (file->f_op->write) {
928 pos = (loff_t) page->index << PAGE_SHIFT;
930 /* we mustn't write more data than we have, so we have
931 * to beware of a partial page at EOF */
932 eof = object->fscache.store_limit_l;
934 if (eof & ~PAGE_MASK) {
935 ASSERTCMP(pos, <, eof);
936 if (eof - pos < PAGE_SIZE) {
937 _debug("cut short %llx to %llx",
940 ASSERTCMP(pos + len, ==, eof);
947 ret = file->f_op->write(
948 file, (const void __user *) data, len, &pos);
959 cachefiles_io_error_obj(
960 object, "Write page to backing file failed");
964 _leave(" = %d", ret);
969 * detach a backing block from a page
970 * - cache withdrawal is prevented by the caller
972 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
974 struct cachefiles_object *object;
975 struct cachefiles_cache *cache;
977 object = container_of(_object, struct cachefiles_object, fscache);
978 cache = container_of(object->fscache.cache,
979 struct cachefiles_cache, cache);
981 _enter("%p,{%lu}", object, page->index);
983 spin_unlock(&object->fscache.cookie->lock);