1 /* Cache page management and data I/O routines
3 * Copyright (C) 2004-2008 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 License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define FSCACHE_DEBUG_LEVEL PAGE
13 #include <linux/module.h>
14 #include <linux/fscache-cache.h>
15 #include <linux/buffer_head.h>
16 #include <linux/pagevec.h>
20 * check to see if a page is being written to the cache
22 bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
27 val = radix_tree_lookup(&cookie->stores, page->index);
32 EXPORT_SYMBOL(__fscache_check_page_write);
35 * wait for a page to finish being written to the cache
37 void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
39 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
41 wait_event(*wq, !__fscache_check_page_write(cookie, page));
43 EXPORT_SYMBOL(__fscache_wait_on_page_write);
46 * note that a page has finished being written to the cache
48 static void fscache_end_page_write(struct fscache_cookie *cookie, struct page *page)
52 spin_lock(&cookie->lock);
53 xpage = radix_tree_delete(&cookie->stores, page->index);
54 spin_unlock(&cookie->lock);
55 ASSERT(xpage != NULL);
57 wake_up_bit(&cookie->flags, 0);
61 * actually apply the changed attributes to a cache object
63 static void fscache_attr_changed_op(struct fscache_operation *op)
65 struct fscache_object *object = op->object;
68 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
70 fscache_stat(&fscache_n_attr_changed_calls);
72 if (fscache_object_is_active(object)) {
73 fscache_set_op_state(op, "CallFS");
74 ret = object->cache->ops->attr_changed(object);
75 fscache_set_op_state(op, "Done");
77 fscache_abort_object(object);
84 * notification that the attributes on an object have changed
86 int __fscache_attr_changed(struct fscache_cookie *cookie)
88 struct fscache_operation *op;
89 struct fscache_object *object;
93 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
95 fscache_stat(&fscache_n_attr_changed);
97 op = kzalloc(sizeof(*op), GFP_KERNEL);
99 fscache_stat(&fscache_n_attr_changed_nomem);
100 _leave(" = -ENOMEM");
104 fscache_operation_init(op, NULL);
105 fscache_operation_init_slow(op, fscache_attr_changed_op);
106 op->flags = FSCACHE_OP_SLOW | (1 << FSCACHE_OP_EXCLUSIVE);
107 fscache_set_op_name(op, "Attr");
109 spin_lock(&cookie->lock);
111 if (hlist_empty(&cookie->backing_objects))
113 object = hlist_entry(cookie->backing_objects.first,
114 struct fscache_object, cookie_link);
116 if (fscache_submit_exclusive_op(object, op) < 0)
118 spin_unlock(&cookie->lock);
119 fscache_stat(&fscache_n_attr_changed_ok);
120 fscache_put_operation(op);
125 spin_unlock(&cookie->lock);
127 fscache_stat(&fscache_n_attr_changed_nobufs);
128 _leave(" = %d", -ENOBUFS);
131 EXPORT_SYMBOL(__fscache_attr_changed);
134 * handle secondary execution given to a retrieval op on behalf of the
137 static void fscache_retrieval_work(struct work_struct *work)
139 struct fscache_retrieval *op =
140 container_of(work, struct fscache_retrieval, op.fast_work);
143 _enter("{OP%x}", op->op.debug_id);
146 op->op.processor(&op->op);
147 fscache_hist(fscache_ops_histogram, start);
148 fscache_put_operation(&op->op);
152 * release a retrieval op reference
154 static void fscache_release_retrieval_op(struct fscache_operation *_op)
156 struct fscache_retrieval *op =
157 container_of(_op, struct fscache_retrieval, op);
159 _enter("{OP%x}", op->op.debug_id);
161 fscache_hist(fscache_retrieval_histogram, op->start_time);
163 fscache_put_context(op->op.object->cookie, op->context);
169 * allocate a retrieval op
171 static struct fscache_retrieval *fscache_alloc_retrieval(
172 struct address_space *mapping,
173 fscache_rw_complete_t end_io_func,
176 struct fscache_retrieval *op;
178 /* allocate a retrieval operation and attempt to submit it */
179 op = kzalloc(sizeof(*op), GFP_NOIO);
181 fscache_stat(&fscache_n_retrievals_nomem);
185 fscache_operation_init(&op->op, fscache_release_retrieval_op);
186 op->op.flags = FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING);
187 op->mapping = mapping;
188 op->end_io_func = end_io_func;
189 op->context = context;
190 op->start_time = jiffies;
191 INIT_WORK(&op->op.fast_work, fscache_retrieval_work);
192 INIT_LIST_HEAD(&op->to_do);
193 fscache_set_op_name(&op->op, "Retr");
198 * wait for a deferred lookup to complete
200 static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
206 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
207 _leave(" = 0 [imm]");
211 fscache_stat(&fscache_n_retrievals_wait);
214 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
215 fscache_wait_bit_interruptible,
216 TASK_INTERRUPTIBLE) != 0) {
217 fscache_stat(&fscache_n_retrievals_intr);
218 _leave(" = -ERESTARTSYS");
222 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
225 fscache_hist(fscache_retrieval_delay_histogram, jif);
226 _leave(" = 0 [dly]");
231 * read a page from the cache or allocate a block in which to store it
233 * -ENOMEM - out of memory, nothing done
234 * -ERESTARTSYS - interrupted
235 * -ENOBUFS - no backing object available in which to cache the block
236 * -ENODATA - no data available in the backing object for this block
237 * 0 - dispatched a read - it'll call end_io_func() when finished
239 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
241 fscache_rw_complete_t end_io_func,
245 struct fscache_retrieval *op;
246 struct fscache_object *object;
249 _enter("%p,%p,,,", cookie, page);
251 fscache_stat(&fscache_n_retrievals);
253 if (hlist_empty(&cookie->backing_objects))
256 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
257 ASSERTCMP(page, !=, NULL);
259 if (fscache_wait_for_deferred_lookup(cookie) < 0)
262 op = fscache_alloc_retrieval(page->mapping, end_io_func, context);
264 _leave(" = -ENOMEM");
267 fscache_set_op_name(&op->op, "RetrRA1");
269 spin_lock(&cookie->lock);
271 if (hlist_empty(&cookie->backing_objects))
273 object = hlist_entry(cookie->backing_objects.first,
274 struct fscache_object, cookie_link);
276 ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP);
278 if (fscache_submit_op(object, &op->op) < 0)
280 spin_unlock(&cookie->lock);
282 fscache_stat(&fscache_n_retrieval_ops);
284 /* pin the netfs read context in case we need to do the actual netfs
285 * read because we've encountered a cache read failure */
286 fscache_get_context(object->cookie, op->context);
288 /* we wait for the operation to become active, and then process it
289 * *here*, in this thread, and not in the thread pool */
290 if (test_bit(FSCACHE_OP_WAITING, &op->op.flags)) {
292 fscache_stat(&fscache_n_retrieval_op_waits);
293 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
294 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
298 /* ask the cache to honour the operation */
299 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
300 ret = object->cache->ops->allocate_page(op, page, gfp);
304 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
308 fscache_stat(&fscache_n_retrievals_nomem);
309 else if (ret == -ERESTARTSYS)
310 fscache_stat(&fscache_n_retrievals_intr);
311 else if (ret == -ENODATA)
312 fscache_stat(&fscache_n_retrievals_nodata);
314 fscache_stat(&fscache_n_retrievals_nobufs);
316 fscache_stat(&fscache_n_retrievals_ok);
318 fscache_put_retrieval(op);
319 _leave(" = %d", ret);
323 spin_unlock(&cookie->lock);
326 fscache_stat(&fscache_n_retrievals_nobufs);
327 _leave(" = -ENOBUFS");
330 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
333 * read a list of page from the cache or allocate a block in which to store
336 * -ENOMEM - out of memory, some pages may be being read
337 * -ERESTARTSYS - interrupted, some pages may be being read
338 * -ENOBUFS - no backing object or space available in which to cache any
339 * pages not being read
340 * -ENODATA - no data available in the backing object for some or all of
342 * 0 - dispatched a read on all pages
344 * end_io_func() will be called for each page read from the cache as it is
345 * finishes being read
347 * any pages for which a read is dispatched will be removed from pages and
350 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
351 struct address_space *mapping,
352 struct list_head *pages,
354 fscache_rw_complete_t end_io_func,
358 fscache_pages_retrieval_func_t func;
359 struct fscache_retrieval *op;
360 struct fscache_object *object;
363 _enter("%p,,%d,,,", cookie, *nr_pages);
365 fscache_stat(&fscache_n_retrievals);
367 if (hlist_empty(&cookie->backing_objects))
370 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
371 ASSERTCMP(*nr_pages, >, 0);
372 ASSERT(!list_empty(pages));
374 if (fscache_wait_for_deferred_lookup(cookie) < 0)
377 op = fscache_alloc_retrieval(mapping, end_io_func, context);
380 fscache_set_op_name(&op->op, "RetrRAN");
382 spin_lock(&cookie->lock);
384 if (hlist_empty(&cookie->backing_objects))
386 object = hlist_entry(cookie->backing_objects.first,
387 struct fscache_object, cookie_link);
389 if (fscache_submit_op(object, &op->op) < 0)
391 spin_unlock(&cookie->lock);
393 fscache_stat(&fscache_n_retrieval_ops);
395 /* pin the netfs read context in case we need to do the actual netfs
396 * read because we've encountered a cache read failure */
397 fscache_get_context(object->cookie, op->context);
399 /* we wait for the operation to become active, and then process it
400 * *here*, in this thread, and not in the thread pool */
401 if (test_bit(FSCACHE_OP_WAITING, &op->op.flags)) {
403 fscache_stat(&fscache_n_retrieval_op_waits);
404 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
405 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
409 /* ask the cache to honour the operation */
410 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags))
411 func = object->cache->ops->allocate_pages;
413 func = object->cache->ops->read_or_alloc_pages;
414 ret = func(op, pages, nr_pages, gfp);
417 fscache_stat(&fscache_n_retrievals_nomem);
418 else if (ret == -ERESTARTSYS)
419 fscache_stat(&fscache_n_retrievals_intr);
420 else if (ret == -ENODATA)
421 fscache_stat(&fscache_n_retrievals_nodata);
423 fscache_stat(&fscache_n_retrievals_nobufs);
425 fscache_stat(&fscache_n_retrievals_ok);
427 fscache_put_retrieval(op);
428 _leave(" = %d", ret);
432 spin_unlock(&cookie->lock);
435 fscache_stat(&fscache_n_retrievals_nobufs);
436 _leave(" = -ENOBUFS");
439 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
442 * allocate a block in the cache on which to store a page
444 * -ENOMEM - out of memory, nothing done
445 * -ERESTARTSYS - interrupted
446 * -ENOBUFS - no backing object available in which to cache the block
447 * 0 - block allocated
449 int __fscache_alloc_page(struct fscache_cookie *cookie,
453 struct fscache_retrieval *op;
454 struct fscache_object *object;
457 _enter("%p,%p,,,", cookie, page);
459 fscache_stat(&fscache_n_allocs);
461 if (hlist_empty(&cookie->backing_objects))
464 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
465 ASSERTCMP(page, !=, NULL);
467 if (fscache_wait_for_deferred_lookup(cookie) < 0)
470 op = fscache_alloc_retrieval(page->mapping, NULL, NULL);
473 fscache_set_op_name(&op->op, "RetrAL1");
475 spin_lock(&cookie->lock);
477 if (hlist_empty(&cookie->backing_objects))
479 object = hlist_entry(cookie->backing_objects.first,
480 struct fscache_object, cookie_link);
482 if (fscache_submit_op(object, &op->op) < 0)
484 spin_unlock(&cookie->lock);
486 fscache_stat(&fscache_n_alloc_ops);
488 if (test_bit(FSCACHE_OP_WAITING, &op->op.flags)) {
490 fscache_stat(&fscache_n_alloc_op_waits);
491 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
492 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
496 /* ask the cache to honour the operation */
497 ret = object->cache->ops->allocate_page(op, page, gfp);
500 fscache_stat(&fscache_n_allocs_nobufs);
502 fscache_stat(&fscache_n_allocs_ok);
504 fscache_put_retrieval(op);
505 _leave(" = %d", ret);
509 spin_unlock(&cookie->lock);
512 fscache_stat(&fscache_n_allocs_nobufs);
513 _leave(" = -ENOBUFS");
516 EXPORT_SYMBOL(__fscache_alloc_page);
519 * release a write op reference
521 static void fscache_release_write_op(struct fscache_operation *_op)
523 _enter("{OP%x}", _op->debug_id);
527 * perform the background storage of a page into the cache
529 static void fscache_write_op(struct fscache_operation *_op)
531 struct fscache_storage *op =
532 container_of(_op, struct fscache_storage, op);
533 struct fscache_object *object = op->op.object;
534 struct fscache_cookie *cookie = object->cookie;
540 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
542 fscache_set_op_state(&op->op, "GetPage");
544 spin_lock(&cookie->lock);
545 spin_lock(&object->lock);
547 if (!fscache_object_is_active(object)) {
548 spin_unlock(&object->lock);
549 spin_unlock(&cookie->lock);
554 fscache_stat(&fscache_n_store_calls);
556 /* find a page to store */
558 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
559 FSCACHE_COOKIE_PENDING_TAG);
563 _debug("gang %d [%lx]", n, page->index);
564 if (page->index > op->store_limit)
567 radix_tree_tag_clear(&cookie->stores, page->index,
568 FSCACHE_COOKIE_PENDING_TAG);
570 spin_unlock(&object->lock);
571 spin_unlock(&cookie->lock);
574 fscache_set_op_state(&op->op, "Store");
575 ret = object->cache->ops->write_page(op, page);
576 fscache_set_op_state(&op->op, "EndWrite");
577 fscache_end_page_write(cookie, page);
578 page_cache_release(page);
580 fscache_set_op_state(&op->op, "Abort");
581 fscache_abort_object(object);
583 fscache_enqueue_operation(&op->op);
591 /* this writer is going away and there aren't any more things to
594 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
595 spin_unlock(&object->lock);
596 spin_unlock(&cookie->lock);
601 * request a page be stored in the cache
603 * -ENOMEM - out of memory, nothing done
604 * -ENOBUFS - no backing object available in which to cache the page
605 * 0 - dispatched a write - it'll call end_io_func() when finished
607 * if the cookie still has a backing object at this point, that object can be
608 * in one of a few states with respect to storage processing:
610 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
613 * (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
616 * (b) writes deferred till post-creation (mark page for writing and
617 * return immediately)
619 * (2) negative lookup, object created, initial fill being made from netfs
620 * (FSCACHE_COOKIE_INITIAL_FILL is set)
622 * (a) fill point not yet reached this page (mark page for writing and
625 * (b) fill point passed this page (queue op to store this page)
627 * (3) object extant (queue op to store this page)
629 * any other state is invalid
631 int __fscache_write_page(struct fscache_cookie *cookie,
635 struct fscache_storage *op;
636 struct fscache_object *object;
639 _enter("%p,%x,", cookie, (u32) page->flags);
641 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
642 ASSERT(PageFsCache(page));
644 fscache_stat(&fscache_n_stores);
646 op = kzalloc(sizeof(*op), GFP_NOIO);
650 fscache_operation_init(&op->op, fscache_release_write_op);
651 fscache_operation_init_slow(&op->op, fscache_write_op);
652 op->op.flags = FSCACHE_OP_SLOW | (1 << FSCACHE_OP_WAITING);
653 fscache_set_op_name(&op->op, "Write1");
655 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
660 spin_lock(&cookie->lock);
662 if (hlist_empty(&cookie->backing_objects))
664 object = hlist_entry(cookie->backing_objects.first,
665 struct fscache_object, cookie_link);
666 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
669 /* add the page to the pending-storage radix tree on the backing
671 spin_lock(&object->lock);
673 _debug("store limit %llx", (unsigned long long) object->store_limit);
675 ret = radix_tree_insert(&cookie->stores, page->index, page);
679 _debug("insert failed %d", ret);
680 goto nobufs_unlock_obj;
683 radix_tree_tag_set(&cookie->stores, page->index,
684 FSCACHE_COOKIE_PENDING_TAG);
685 page_cache_get(page);
687 /* we only want one writer at a time, but we do need to queue new
688 * writers after exclusive ops */
689 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
690 goto already_pending;
692 spin_unlock(&object->lock);
694 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
695 op->store_limit = object->store_limit;
697 if (fscache_submit_op(object, &op->op) < 0)
700 spin_unlock(&cookie->lock);
701 radix_tree_preload_end();
702 fscache_stat(&fscache_n_store_ops);
703 fscache_stat(&fscache_n_stores_ok);
705 /* the slow work queue now carries its own ref on the object */
706 fscache_put_operation(&op->op);
711 fscache_stat(&fscache_n_stores_again);
713 spin_unlock(&object->lock);
714 spin_unlock(&cookie->lock);
715 radix_tree_preload_end();
717 fscache_stat(&fscache_n_stores_ok);
722 radix_tree_delete(&cookie->stores, page->index);
723 page_cache_release(page);
728 spin_unlock(&object->lock);
730 spin_unlock(&cookie->lock);
731 radix_tree_preload_end();
733 fscache_stat(&fscache_n_stores_nobufs);
734 _leave(" = -ENOBUFS");
740 fscache_stat(&fscache_n_stores_oom);
741 _leave(" = -ENOMEM");
744 EXPORT_SYMBOL(__fscache_write_page);
747 * remove a page from the cache
749 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
751 struct fscache_object *object;
755 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
756 ASSERTCMP(page, !=, NULL);
758 fscache_stat(&fscache_n_uncaches);
760 /* cache withdrawal may beat us to it */
761 if (!PageFsCache(page))
765 spin_lock(&cookie->lock);
767 if (hlist_empty(&cookie->backing_objects)) {
768 ClearPageFsCache(page);
772 object = hlist_entry(cookie->backing_objects.first,
773 struct fscache_object, cookie_link);
775 /* there might now be stuff on disk we could read */
776 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
778 /* only invoke the cache backend if we managed to mark the page
779 * uncached here; this deals with synchronisation vs withdrawal */
780 if (TestClearPageFsCache(page) &&
781 object->cache->ops->uncache_page) {
782 /* the cache backend releases the cookie lock */
783 object->cache->ops->uncache_page(object, page);
788 spin_unlock(&cookie->lock);
792 EXPORT_SYMBOL(__fscache_uncache_page);
795 * fscache_mark_pages_cached - Mark pages as being cached
796 * @op: The retrieval op pages are being marked for
797 * @pagevec: The pages to be marked
799 * Mark a bunch of netfs pages as being cached. After this is called,
800 * the netfs must call fscache_uncache_page() to remove the mark.
802 void fscache_mark_pages_cached(struct fscache_retrieval *op,
803 struct pagevec *pagevec)
805 struct fscache_cookie *cookie = op->op.object->cookie;
808 #ifdef CONFIG_FSCACHE_STATS
809 atomic_add(pagevec->nr, &fscache_n_marks);
812 for (loop = 0; loop < pagevec->nr; loop++) {
813 struct page *page = pagevec->pages[loop];
815 _debug("- mark %p{%lx}", page, page->index);
816 if (TestSetPageFsCache(page)) {
817 static bool once_only;
820 printk(KERN_WARNING "FS-Cache:"
821 " Cookie type %s marked page %lx"
823 cookie->def->name, page->index);
828 if (cookie->def->mark_pages_cached)
829 cookie->def->mark_pages_cached(cookie->netfs_data,
830 op->mapping, pagevec);
831 pagevec_reinit(pagevec);
833 EXPORT_SYMBOL(fscache_mark_pages_cached);