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>
17 #include <linux/slab.h>
21 * check to see if a page is being written to the cache
23 bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
28 val = radix_tree_lookup(&cookie->stores, page->index);
33 EXPORT_SYMBOL(__fscache_check_page_write);
36 * wait for a page to finish being written to the cache
38 void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
40 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
42 wait_event(*wq, !__fscache_check_page_write(cookie, page));
44 EXPORT_SYMBOL(__fscache_wait_on_page_write);
47 * wait for a page to finish being written to the cache. Put a timeout here
48 * since we might be called recursively via parent fs.
51 bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
53 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
55 return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
60 * decide whether a page can be released, possibly by cancelling a store to it
61 * - we're allowed to sleep if __GFP_WAIT is flagged
63 bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
70 _enter("%p,%p,%x", cookie, page, gfp);
74 val = radix_tree_lookup(&cookie->stores, page->index);
77 fscache_stat(&fscache_n_store_vmscan_not_storing);
78 __fscache_uncache_page(cookie, page);
82 /* see if the page is actually undergoing storage - if so we can't get
83 * rid of it till the cache has finished with it */
84 if (radix_tree_tag_get(&cookie->stores, page->index,
85 FSCACHE_COOKIE_STORING_TAG)) {
90 /* the page is pending storage, so we attempt to cancel the store and
91 * discard the store request so that the page can be reclaimed */
92 spin_lock(&cookie->stores_lock);
95 if (radix_tree_tag_get(&cookie->stores, page->index,
96 FSCACHE_COOKIE_STORING_TAG)) {
97 /* the page started to undergo storage whilst we were looking,
98 * so now we can only wait or return */
99 spin_unlock(&cookie->stores_lock);
103 xpage = radix_tree_delete(&cookie->stores, page->index);
104 spin_unlock(&cookie->stores_lock);
107 fscache_stat(&fscache_n_store_vmscan_cancelled);
108 fscache_stat(&fscache_n_store_radix_deletes);
109 ASSERTCMP(xpage, ==, page);
111 fscache_stat(&fscache_n_store_vmscan_gone);
114 wake_up_bit(&cookie->flags, 0);
116 page_cache_release(xpage);
117 __fscache_uncache_page(cookie, page);
121 /* We will wait here if we're allowed to, but that could deadlock the
122 * allocator as the work threads writing to the cache may all end up
123 * sleeping on memory allocation, so we may need to impose a timeout
125 if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
126 fscache_stat(&fscache_n_store_vmscan_busy);
130 fscache_stat(&fscache_n_store_vmscan_wait);
131 if (!release_page_wait_timeout(cookie, page))
132 _debug("fscache writeout timeout page: %p{%lx}",
138 EXPORT_SYMBOL(__fscache_maybe_release_page);
141 * note that a page has finished being written to the cache
143 static void fscache_end_page_write(struct fscache_object *object,
146 struct fscache_cookie *cookie;
147 struct page *xpage = NULL;
149 spin_lock(&object->lock);
150 cookie = object->cookie;
152 /* delete the page from the tree if it is now no longer
154 spin_lock(&cookie->stores_lock);
155 radix_tree_tag_clear(&cookie->stores, page->index,
156 FSCACHE_COOKIE_STORING_TAG);
157 if (!radix_tree_tag_get(&cookie->stores, page->index,
158 FSCACHE_COOKIE_PENDING_TAG)) {
159 fscache_stat(&fscache_n_store_radix_deletes);
160 xpage = radix_tree_delete(&cookie->stores, page->index);
162 spin_unlock(&cookie->stores_lock);
163 wake_up_bit(&cookie->flags, 0);
165 spin_unlock(&object->lock);
167 page_cache_release(xpage);
171 * actually apply the changed attributes to a cache object
173 static void fscache_attr_changed_op(struct fscache_operation *op)
175 struct fscache_object *object = op->object;
178 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
180 fscache_stat(&fscache_n_attr_changed_calls);
182 if (fscache_object_is_active(object)) {
183 fscache_stat(&fscache_n_cop_attr_changed);
184 ret = object->cache->ops->attr_changed(object);
185 fscache_stat_d(&fscache_n_cop_attr_changed);
187 fscache_abort_object(object);
190 fscache_op_complete(op, true);
195 * notification that the attributes on an object have changed
197 int __fscache_attr_changed(struct fscache_cookie *cookie)
199 struct fscache_operation *op;
200 struct fscache_object *object;
203 _enter("%p", cookie);
205 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
207 fscache_stat(&fscache_n_attr_changed);
209 op = kzalloc(sizeof(*op), GFP_KERNEL);
211 fscache_stat(&fscache_n_attr_changed_nomem);
212 _leave(" = -ENOMEM");
216 fscache_operation_init(op, fscache_attr_changed_op, NULL);
217 op->flags = FSCACHE_OP_ASYNC |
218 (1 << FSCACHE_OP_EXCLUSIVE) |
219 (1 << FSCACHE_OP_UNUSE_COOKIE);
221 spin_lock(&cookie->lock);
223 if (!fscache_cookie_enabled(cookie) ||
224 hlist_empty(&cookie->backing_objects))
226 object = hlist_entry(cookie->backing_objects.first,
227 struct fscache_object, cookie_link);
229 __fscache_use_cookie(cookie);
230 if (fscache_submit_exclusive_op(object, op) < 0)
232 spin_unlock(&cookie->lock);
233 fscache_stat(&fscache_n_attr_changed_ok);
234 fscache_put_operation(op);
239 wake_cookie = __fscache_unuse_cookie(cookie);
240 spin_unlock(&cookie->lock);
243 __fscache_wake_unused_cookie(cookie);
244 fscache_stat(&fscache_n_attr_changed_nobufs);
245 _leave(" = %d", -ENOBUFS);
248 EXPORT_SYMBOL(__fscache_attr_changed);
251 * release a retrieval op reference
253 static void fscache_release_retrieval_op(struct fscache_operation *_op)
255 struct fscache_retrieval *op =
256 container_of(_op, struct fscache_retrieval, op);
258 _enter("{OP%x}", op->op.debug_id);
260 ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
262 fscache_hist(fscache_retrieval_histogram, op->start_time);
264 fscache_put_context(op->op.object->cookie, op->context);
270 * allocate a retrieval op
272 static struct fscache_retrieval *fscache_alloc_retrieval(
273 struct fscache_cookie *cookie,
274 struct address_space *mapping,
275 fscache_rw_complete_t end_io_func,
278 struct fscache_retrieval *op;
280 /* allocate a retrieval operation and attempt to submit it */
281 op = kzalloc(sizeof(*op), GFP_NOIO);
283 fscache_stat(&fscache_n_retrievals_nomem);
287 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
288 op->op.flags = FSCACHE_OP_MYTHREAD |
289 (1UL << FSCACHE_OP_WAITING) |
290 (1UL << FSCACHE_OP_UNUSE_COOKIE);
291 op->mapping = mapping;
292 op->end_io_func = end_io_func;
293 op->context = context;
294 op->start_time = jiffies;
295 INIT_LIST_HEAD(&op->to_do);
300 * wait for a deferred lookup to complete
302 int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
308 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
309 _leave(" = 0 [imm]");
313 fscache_stat(&fscache_n_retrievals_wait);
316 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
317 TASK_INTERRUPTIBLE) != 0) {
318 fscache_stat(&fscache_n_retrievals_intr);
319 _leave(" = -ERESTARTSYS");
323 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
326 fscache_hist(fscache_retrieval_delay_histogram, jif);
327 _leave(" = 0 [dly]");
332 * Handle cancellation of a pending retrieval op
334 static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
336 struct fscache_retrieval *op =
337 container_of(_op, struct fscache_retrieval, op);
339 atomic_set(&op->n_pages, 0);
343 * wait for an object to become active (or dead)
345 int fscache_wait_for_operation_activation(struct fscache_object *object,
346 struct fscache_operation *op,
347 atomic_t *stat_op_waits,
348 atomic_t *stat_object_dead,
349 void (*do_cancel)(struct fscache_operation *))
353 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
358 fscache_stat(stat_op_waits);
359 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
360 TASK_INTERRUPTIBLE) != 0) {
361 ret = fscache_cancel_op(op, do_cancel);
365 /* it's been removed from the pending queue by another party,
366 * so we should get to run shortly */
367 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
368 TASK_UNINTERRUPTIBLE);
373 if (op->state == FSCACHE_OP_ST_CANCELLED) {
374 if (stat_object_dead)
375 fscache_stat(stat_object_dead);
376 _leave(" = -ENOBUFS [cancelled]");
379 if (unlikely(fscache_object_is_dead(object))) {
380 pr_err("%s() = -ENOBUFS [obj dead %d]\n", __func__, op->state);
381 fscache_cancel_op(op, do_cancel);
382 if (stat_object_dead)
383 fscache_stat(stat_object_dead);
390 * read a page from the cache or allocate a block in which to store it
392 * -ENOMEM - out of memory, nothing done
393 * -ERESTARTSYS - interrupted
394 * -ENOBUFS - no backing object available in which to cache the block
395 * -ENODATA - no data available in the backing object for this block
396 * 0 - dispatched a read - it'll call end_io_func() when finished
398 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
400 fscache_rw_complete_t end_io_func,
404 struct fscache_retrieval *op;
405 struct fscache_object *object;
406 bool wake_cookie = false;
409 _enter("%p,%p,,,", cookie, page);
411 fscache_stat(&fscache_n_retrievals);
413 if (hlist_empty(&cookie->backing_objects))
416 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
417 _leave(" = -ENOBUFS [invalidating]");
421 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
422 ASSERTCMP(page, !=, NULL);
424 if (fscache_wait_for_deferred_lookup(cookie) < 0)
427 op = fscache_alloc_retrieval(cookie, page->mapping,
428 end_io_func, context);
430 _leave(" = -ENOMEM");
433 atomic_set(&op->n_pages, 1);
435 spin_lock(&cookie->lock);
437 if (!fscache_cookie_enabled(cookie) ||
438 hlist_empty(&cookie->backing_objects))
440 object = hlist_entry(cookie->backing_objects.first,
441 struct fscache_object, cookie_link);
443 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
445 __fscache_use_cookie(cookie);
446 atomic_inc(&object->n_reads);
447 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
449 if (fscache_submit_op(object, &op->op) < 0)
450 goto nobufs_unlock_dec;
451 spin_unlock(&cookie->lock);
453 fscache_stat(&fscache_n_retrieval_ops);
455 /* pin the netfs read context in case we need to do the actual netfs
456 * read because we've encountered a cache read failure */
457 fscache_get_context(object->cookie, op->context);
459 /* we wait for the operation to become active, and then process it
460 * *here*, in this thread, and not in the thread pool */
461 ret = fscache_wait_for_operation_activation(
463 __fscache_stat(&fscache_n_retrieval_op_waits),
464 __fscache_stat(&fscache_n_retrievals_object_dead),
465 fscache_do_cancel_retrieval);
469 /* ask the cache to honour the operation */
470 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
471 fscache_stat(&fscache_n_cop_allocate_page);
472 ret = object->cache->ops->allocate_page(op, page, gfp);
473 fscache_stat_d(&fscache_n_cop_allocate_page);
477 fscache_stat(&fscache_n_cop_read_or_alloc_page);
478 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
479 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
484 fscache_stat(&fscache_n_retrievals_nomem);
485 else if (ret == -ERESTARTSYS)
486 fscache_stat(&fscache_n_retrievals_intr);
487 else if (ret == -ENODATA)
488 fscache_stat(&fscache_n_retrievals_nodata);
490 fscache_stat(&fscache_n_retrievals_nobufs);
492 fscache_stat(&fscache_n_retrievals_ok);
494 fscache_put_retrieval(op);
495 _leave(" = %d", ret);
499 atomic_dec(&object->n_reads);
500 wake_cookie = __fscache_unuse_cookie(cookie);
502 spin_unlock(&cookie->lock);
504 __fscache_wake_unused_cookie(cookie);
507 fscache_stat(&fscache_n_retrievals_nobufs);
508 _leave(" = -ENOBUFS");
511 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
514 * read a list of page from the cache or allocate a block in which to store
517 * -ENOMEM - out of memory, some pages may be being read
518 * -ERESTARTSYS - interrupted, some pages may be being read
519 * -ENOBUFS - no backing object or space available in which to cache any
520 * pages not being read
521 * -ENODATA - no data available in the backing object for some or all of
523 * 0 - dispatched a read on all pages
525 * end_io_func() will be called for each page read from the cache as it is
526 * finishes being read
528 * any pages for which a read is dispatched will be removed from pages and
531 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
532 struct address_space *mapping,
533 struct list_head *pages,
535 fscache_rw_complete_t end_io_func,
539 struct fscache_retrieval *op;
540 struct fscache_object *object;
541 bool wake_cookie = false;
544 _enter("%p,,%d,,,", cookie, *nr_pages);
546 fscache_stat(&fscache_n_retrievals);
548 if (hlist_empty(&cookie->backing_objects))
551 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
552 _leave(" = -ENOBUFS [invalidating]");
556 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
557 ASSERTCMP(*nr_pages, >, 0);
558 ASSERT(!list_empty(pages));
560 if (fscache_wait_for_deferred_lookup(cookie) < 0)
563 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
566 atomic_set(&op->n_pages, *nr_pages);
568 spin_lock(&cookie->lock);
570 if (!fscache_cookie_enabled(cookie) ||
571 hlist_empty(&cookie->backing_objects))
573 object = hlist_entry(cookie->backing_objects.first,
574 struct fscache_object, cookie_link);
576 __fscache_use_cookie(cookie);
577 atomic_inc(&object->n_reads);
578 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
580 if (fscache_submit_op(object, &op->op) < 0)
581 goto nobufs_unlock_dec;
582 spin_unlock(&cookie->lock);
584 fscache_stat(&fscache_n_retrieval_ops);
586 /* pin the netfs read context in case we need to do the actual netfs
587 * read because we've encountered a cache read failure */
588 fscache_get_context(object->cookie, op->context);
590 /* we wait for the operation to become active, and then process it
591 * *here*, in this thread, and not in the thread pool */
592 ret = fscache_wait_for_operation_activation(
594 __fscache_stat(&fscache_n_retrieval_op_waits),
595 __fscache_stat(&fscache_n_retrievals_object_dead),
596 fscache_do_cancel_retrieval);
600 /* ask the cache to honour the operation */
601 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
602 fscache_stat(&fscache_n_cop_allocate_pages);
603 ret = object->cache->ops->allocate_pages(
604 op, pages, nr_pages, gfp);
605 fscache_stat_d(&fscache_n_cop_allocate_pages);
607 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
608 ret = object->cache->ops->read_or_alloc_pages(
609 op, pages, nr_pages, gfp);
610 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
615 fscache_stat(&fscache_n_retrievals_nomem);
616 else if (ret == -ERESTARTSYS)
617 fscache_stat(&fscache_n_retrievals_intr);
618 else if (ret == -ENODATA)
619 fscache_stat(&fscache_n_retrievals_nodata);
621 fscache_stat(&fscache_n_retrievals_nobufs);
623 fscache_stat(&fscache_n_retrievals_ok);
625 fscache_put_retrieval(op);
626 _leave(" = %d", ret);
630 atomic_dec(&object->n_reads);
631 wake_cookie = __fscache_unuse_cookie(cookie);
633 spin_unlock(&cookie->lock);
636 __fscache_wake_unused_cookie(cookie);
638 fscache_stat(&fscache_n_retrievals_nobufs);
639 _leave(" = -ENOBUFS");
642 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
645 * allocate a block in the cache on which to store a page
647 * -ENOMEM - out of memory, nothing done
648 * -ERESTARTSYS - interrupted
649 * -ENOBUFS - no backing object available in which to cache the block
650 * 0 - block allocated
652 int __fscache_alloc_page(struct fscache_cookie *cookie,
656 struct fscache_retrieval *op;
657 struct fscache_object *object;
658 bool wake_cookie = false;
661 _enter("%p,%p,,,", cookie, page);
663 fscache_stat(&fscache_n_allocs);
665 if (hlist_empty(&cookie->backing_objects))
668 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
669 ASSERTCMP(page, !=, NULL);
671 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
672 _leave(" = -ENOBUFS [invalidating]");
676 if (fscache_wait_for_deferred_lookup(cookie) < 0)
679 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
682 atomic_set(&op->n_pages, 1);
684 spin_lock(&cookie->lock);
686 if (!fscache_cookie_enabled(cookie) ||
687 hlist_empty(&cookie->backing_objects))
689 object = hlist_entry(cookie->backing_objects.first,
690 struct fscache_object, cookie_link);
692 __fscache_use_cookie(cookie);
693 if (fscache_submit_op(object, &op->op) < 0)
694 goto nobufs_unlock_dec;
695 spin_unlock(&cookie->lock);
697 fscache_stat(&fscache_n_alloc_ops);
699 ret = fscache_wait_for_operation_activation(
701 __fscache_stat(&fscache_n_alloc_op_waits),
702 __fscache_stat(&fscache_n_allocs_object_dead),
703 fscache_do_cancel_retrieval);
707 /* ask the cache to honour the operation */
708 fscache_stat(&fscache_n_cop_allocate_page);
709 ret = object->cache->ops->allocate_page(op, page, gfp);
710 fscache_stat_d(&fscache_n_cop_allocate_page);
713 if (ret == -ERESTARTSYS)
714 fscache_stat(&fscache_n_allocs_intr);
716 fscache_stat(&fscache_n_allocs_nobufs);
718 fscache_stat(&fscache_n_allocs_ok);
720 fscache_put_retrieval(op);
721 _leave(" = %d", ret);
725 wake_cookie = __fscache_unuse_cookie(cookie);
727 spin_unlock(&cookie->lock);
730 __fscache_wake_unused_cookie(cookie);
732 fscache_stat(&fscache_n_allocs_nobufs);
733 _leave(" = -ENOBUFS");
736 EXPORT_SYMBOL(__fscache_alloc_page);
739 * Unmark pages allocate in the readahead code path (via:
740 * fscache_readpages_or_alloc) after delegating to the base filesystem
742 void __fscache_readpages_cancel(struct fscache_cookie *cookie,
743 struct list_head *pages)
747 list_for_each_entry(page, pages, lru) {
748 if (PageFsCache(page))
749 __fscache_uncache_page(cookie, page);
752 EXPORT_SYMBOL(__fscache_readpages_cancel);
755 * release a write op reference
757 static void fscache_release_write_op(struct fscache_operation *_op)
759 _enter("{OP%x}", _op->debug_id);
763 * perform the background storage of a page into the cache
765 static void fscache_write_op(struct fscache_operation *_op)
767 struct fscache_storage *op =
768 container_of(_op, struct fscache_storage, op);
769 struct fscache_object *object = op->op.object;
770 struct fscache_cookie *cookie;
776 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
778 spin_lock(&object->lock);
779 cookie = object->cookie;
781 if (!fscache_object_is_active(object)) {
782 /* If we get here, then the on-disk cache object likely longer
783 * exists, so we should just cancel this write operation.
785 spin_unlock(&object->lock);
786 fscache_op_complete(&op->op, false);
787 _leave(" [inactive]");
792 /* If we get here, then the cookie belonging to the object was
793 * detached, probably by the cookie being withdrawn due to
794 * memory pressure, which means that the pages we might write
795 * to the cache from no longer exist - therefore, we can just
796 * cancel this write operation.
798 spin_unlock(&object->lock);
799 fscache_op_complete(&op->op, false);
800 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
801 _op->flags, _op->state, object->state->short_name,
806 spin_lock(&cookie->stores_lock);
808 fscache_stat(&fscache_n_store_calls);
810 /* find a page to store */
812 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
813 FSCACHE_COOKIE_PENDING_TAG);
817 _debug("gang %d [%lx]", n, page->index);
818 if (page->index > op->store_limit) {
819 fscache_stat(&fscache_n_store_pages_over_limit);
823 radix_tree_tag_set(&cookie->stores, page->index,
824 FSCACHE_COOKIE_STORING_TAG);
825 radix_tree_tag_clear(&cookie->stores, page->index,
826 FSCACHE_COOKIE_PENDING_TAG);
828 spin_unlock(&cookie->stores_lock);
829 spin_unlock(&object->lock);
831 fscache_stat(&fscache_n_store_pages);
832 fscache_stat(&fscache_n_cop_write_page);
833 ret = object->cache->ops->write_page(op, page);
834 fscache_stat_d(&fscache_n_cop_write_page);
835 fscache_end_page_write(object, page);
837 fscache_abort_object(object);
838 fscache_op_complete(&op->op, true);
840 fscache_enqueue_operation(&op->op);
847 /* this writer is going away and there aren't any more things to
850 spin_unlock(&cookie->stores_lock);
851 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
852 spin_unlock(&object->lock);
853 fscache_op_complete(&op->op, true);
858 * Clear the pages pending writing for invalidation
860 void fscache_invalidate_writes(struct fscache_cookie *cookie)
869 spin_lock(&cookie->stores_lock);
870 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
872 FSCACHE_COOKIE_PENDING_TAG);
874 spin_unlock(&cookie->stores_lock);
878 for (i = n - 1; i >= 0; i--) {
880 radix_tree_delete(&cookie->stores, page->index);
883 spin_unlock(&cookie->stores_lock);
885 for (i = n - 1; i >= 0; i--)
886 page_cache_release(results[i]);
893 * request a page be stored in the cache
895 * -ENOMEM - out of memory, nothing done
896 * -ENOBUFS - no backing object available in which to cache the page
897 * 0 - dispatched a write - it'll call end_io_func() when finished
899 * if the cookie still has a backing object at this point, that object can be
900 * in one of a few states with respect to storage processing:
902 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
907 * (b) writes deferred till post-creation (mark page for writing and
908 * return immediately)
910 * (2) negative lookup, object created, initial fill being made from netfs
912 * (a) fill point not yet reached this page (mark page for writing and
915 * (b) fill point passed this page (queue op to store this page)
917 * (3) object extant (queue op to store this page)
919 * any other state is invalid
921 int __fscache_write_page(struct fscache_cookie *cookie,
925 struct fscache_storage *op;
926 struct fscache_object *object;
927 bool wake_cookie = false;
930 _enter("%p,%x,", cookie, (u32) page->flags);
932 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
933 ASSERT(PageFsCache(page));
935 fscache_stat(&fscache_n_stores);
937 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
938 _leave(" = -ENOBUFS [invalidating]");
942 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
946 fscache_operation_init(&op->op, fscache_write_op,
947 fscache_release_write_op);
948 op->op.flags = FSCACHE_OP_ASYNC |
949 (1 << FSCACHE_OP_WAITING) |
950 (1 << FSCACHE_OP_UNUSE_COOKIE);
952 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
957 spin_lock(&cookie->lock);
959 if (!fscache_cookie_enabled(cookie) ||
960 hlist_empty(&cookie->backing_objects))
962 object = hlist_entry(cookie->backing_objects.first,
963 struct fscache_object, cookie_link);
964 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
967 /* add the page to the pending-storage radix tree on the backing
969 spin_lock(&object->lock);
970 spin_lock(&cookie->stores_lock);
972 _debug("store limit %llx", (unsigned long long) object->store_limit);
974 ret = radix_tree_insert(&cookie->stores, page->index, page);
978 _debug("insert failed %d", ret);
979 goto nobufs_unlock_obj;
982 radix_tree_tag_set(&cookie->stores, page->index,
983 FSCACHE_COOKIE_PENDING_TAG);
984 page_cache_get(page);
986 /* we only want one writer at a time, but we do need to queue new
987 * writers after exclusive ops */
988 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
989 goto already_pending;
991 spin_unlock(&cookie->stores_lock);
992 spin_unlock(&object->lock);
994 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
995 op->store_limit = object->store_limit;
997 __fscache_use_cookie(cookie);
998 if (fscache_submit_op(object, &op->op) < 0)
1001 spin_unlock(&cookie->lock);
1002 radix_tree_preload_end();
1003 fscache_stat(&fscache_n_store_ops);
1004 fscache_stat(&fscache_n_stores_ok);
1006 /* the work queue now carries its own ref on the object */
1007 fscache_put_operation(&op->op);
1012 fscache_stat(&fscache_n_stores_again);
1014 spin_unlock(&cookie->stores_lock);
1015 spin_unlock(&object->lock);
1016 spin_unlock(&cookie->lock);
1017 radix_tree_preload_end();
1019 fscache_stat(&fscache_n_stores_ok);
1024 spin_lock(&cookie->stores_lock);
1025 radix_tree_delete(&cookie->stores, page->index);
1026 spin_unlock(&cookie->stores_lock);
1027 wake_cookie = __fscache_unuse_cookie(cookie);
1028 page_cache_release(page);
1033 spin_unlock(&cookie->stores_lock);
1034 spin_unlock(&object->lock);
1036 spin_unlock(&cookie->lock);
1037 radix_tree_preload_end();
1040 __fscache_wake_unused_cookie(cookie);
1041 fscache_stat(&fscache_n_stores_nobufs);
1042 _leave(" = -ENOBUFS");
1048 fscache_stat(&fscache_n_stores_oom);
1049 _leave(" = -ENOMEM");
1052 EXPORT_SYMBOL(__fscache_write_page);
1055 * remove a page from the cache
1057 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1059 struct fscache_object *object;
1061 _enter(",%p", page);
1063 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1064 ASSERTCMP(page, !=, NULL);
1066 fscache_stat(&fscache_n_uncaches);
1068 /* cache withdrawal may beat us to it */
1069 if (!PageFsCache(page))
1072 /* get the object */
1073 spin_lock(&cookie->lock);
1075 if (hlist_empty(&cookie->backing_objects)) {
1076 ClearPageFsCache(page);
1080 object = hlist_entry(cookie->backing_objects.first,
1081 struct fscache_object, cookie_link);
1083 /* there might now be stuff on disk we could read */
1084 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1086 /* only invoke the cache backend if we managed to mark the page
1087 * uncached here; this deals with synchronisation vs withdrawal */
1088 if (TestClearPageFsCache(page) &&
1089 object->cache->ops->uncache_page) {
1090 /* the cache backend releases the cookie lock */
1091 fscache_stat(&fscache_n_cop_uncache_page);
1092 object->cache->ops->uncache_page(object, page);
1093 fscache_stat_d(&fscache_n_cop_uncache_page);
1098 spin_unlock(&cookie->lock);
1102 EXPORT_SYMBOL(__fscache_uncache_page);
1105 * fscache_mark_page_cached - Mark a page as being cached
1106 * @op: The retrieval op pages are being marked for
1107 * @page: The page to be marked
1109 * Mark a netfs page as being cached. After this is called, the netfs
1110 * must call fscache_uncache_page() to remove the mark.
1112 void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1114 struct fscache_cookie *cookie = op->op.object->cookie;
1116 #ifdef CONFIG_FSCACHE_STATS
1117 atomic_inc(&fscache_n_marks);
1120 _debug("- mark %p{%lx}", page, page->index);
1121 if (TestSetPageFsCache(page)) {
1122 static bool once_only;
1125 pr_warn("Cookie type %s marked page %lx multiple times\n",
1126 cookie->def->name, page->index);
1130 if (cookie->def->mark_page_cached)
1131 cookie->def->mark_page_cached(cookie->netfs_data,
1134 EXPORT_SYMBOL(fscache_mark_page_cached);
1137 * fscache_mark_pages_cached - Mark pages as being cached
1138 * @op: The retrieval op pages are being marked for
1139 * @pagevec: The pages to be marked
1141 * Mark a bunch of netfs pages as being cached. After this is called,
1142 * the netfs must call fscache_uncache_page() to remove the mark.
1144 void fscache_mark_pages_cached(struct fscache_retrieval *op,
1145 struct pagevec *pagevec)
1149 for (loop = 0; loop < pagevec->nr; loop++)
1150 fscache_mark_page_cached(op, pagevec->pages[loop]);
1152 pagevec_reinit(pagevec);
1154 EXPORT_SYMBOL(fscache_mark_pages_cached);
1157 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1158 * to be associated with the given cookie.
1160 void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1161 struct inode *inode)
1163 struct address_space *mapping = inode->i_mapping;
1164 struct pagevec pvec;
1168 _enter("%p,%p", cookie, inode);
1170 if (!mapping || mapping->nrpages == 0) {
1171 _leave(" [no pages]");
1175 pagevec_init(&pvec, 0);
1178 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1180 for (i = 0; i < pagevec_count(&pvec); i++) {
1181 struct page *page = pvec.pages[i];
1183 if (PageFsCache(page)) {
1184 __fscache_wait_on_page_write(cookie, page);
1185 __fscache_uncache_page(cookie, page);
1188 pagevec_release(&pvec);
1194 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);