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 * decide whether a page can be released, possibly by cancelling a store to it
48 * - we're allowed to sleep if __GFP_WAIT is flagged
50 bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
57 _enter("%p,%p,%x", cookie, page, gfp);
61 val = radix_tree_lookup(&cookie->stores, page->index);
64 fscache_stat(&fscache_n_store_vmscan_not_storing);
65 __fscache_uncache_page(cookie, page);
69 /* see if the page is actually undergoing storage - if so we can't get
70 * rid of it till the cache has finished with it */
71 if (radix_tree_tag_get(&cookie->stores, page->index,
72 FSCACHE_COOKIE_STORING_TAG)) {
77 /* the page is pending storage, so we attempt to cancel the store and
78 * discard the store request so that the page can be reclaimed */
79 spin_lock(&cookie->stores_lock);
82 if (radix_tree_tag_get(&cookie->stores, page->index,
83 FSCACHE_COOKIE_STORING_TAG)) {
84 /* the page started to undergo storage whilst we were looking,
85 * so now we can only wait or return */
86 spin_unlock(&cookie->stores_lock);
90 xpage = radix_tree_delete(&cookie->stores, page->index);
91 spin_unlock(&cookie->stores_lock);
94 fscache_stat(&fscache_n_store_vmscan_cancelled);
95 fscache_stat(&fscache_n_store_radix_deletes);
96 ASSERTCMP(xpage, ==, page);
98 fscache_stat(&fscache_n_store_vmscan_gone);
101 wake_up_bit(&cookie->flags, 0);
103 page_cache_release(xpage);
104 __fscache_uncache_page(cookie, page);
108 /* We will wait here if we're allowed to, but that could deadlock the
109 * allocator as the work threads writing to the cache may all end up
110 * sleeping on memory allocation, so we may need to impose a timeout
112 if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
113 fscache_stat(&fscache_n_store_vmscan_busy);
117 fscache_stat(&fscache_n_store_vmscan_wait);
118 __fscache_wait_on_page_write(cookie, page);
122 EXPORT_SYMBOL(__fscache_maybe_release_page);
125 * note that a page has finished being written to the cache
127 static void fscache_end_page_write(struct fscache_object *object,
130 struct fscache_cookie *cookie;
131 struct page *xpage = NULL;
133 spin_lock(&object->lock);
134 cookie = object->cookie;
136 /* delete the page from the tree if it is now no longer
138 spin_lock(&cookie->stores_lock);
139 radix_tree_tag_clear(&cookie->stores, page->index,
140 FSCACHE_COOKIE_STORING_TAG);
141 if (!radix_tree_tag_get(&cookie->stores, page->index,
142 FSCACHE_COOKIE_PENDING_TAG)) {
143 fscache_stat(&fscache_n_store_radix_deletes);
144 xpage = radix_tree_delete(&cookie->stores, page->index);
146 spin_unlock(&cookie->stores_lock);
147 wake_up_bit(&cookie->flags, 0);
149 spin_unlock(&object->lock);
151 page_cache_release(xpage);
155 * actually apply the changed attributes to a cache object
157 static void fscache_attr_changed_op(struct fscache_operation *op)
159 struct fscache_object *object = op->object;
162 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
164 fscache_stat(&fscache_n_attr_changed_calls);
166 if (fscache_object_is_active(object) &&
167 fscache_use_cookie(object)) {
168 fscache_stat(&fscache_n_cop_attr_changed);
169 ret = object->cache->ops->attr_changed(object);
170 fscache_stat_d(&fscache_n_cop_attr_changed);
171 fscache_unuse_cookie(object);
173 fscache_abort_object(object);
176 fscache_op_complete(op, true);
181 * notification that the attributes on an object have changed
183 int __fscache_attr_changed(struct fscache_cookie *cookie)
185 struct fscache_operation *op;
186 struct fscache_object *object;
188 _enter("%p", cookie);
190 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
192 fscache_stat(&fscache_n_attr_changed);
194 op = kzalloc(sizeof(*op), GFP_KERNEL);
196 fscache_stat(&fscache_n_attr_changed_nomem);
197 _leave(" = -ENOMEM");
201 fscache_operation_init(op, fscache_attr_changed_op, NULL);
202 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
204 spin_lock(&cookie->lock);
206 if (hlist_empty(&cookie->backing_objects))
208 object = hlist_entry(cookie->backing_objects.first,
209 struct fscache_object, cookie_link);
211 if (fscache_submit_exclusive_op(object, op) < 0)
213 spin_unlock(&cookie->lock);
214 fscache_stat(&fscache_n_attr_changed_ok);
215 fscache_put_operation(op);
220 spin_unlock(&cookie->lock);
222 fscache_stat(&fscache_n_attr_changed_nobufs);
223 _leave(" = %d", -ENOBUFS);
226 EXPORT_SYMBOL(__fscache_attr_changed);
229 * release a retrieval op reference
231 static void fscache_release_retrieval_op(struct fscache_operation *_op)
233 struct fscache_retrieval *op =
234 container_of(_op, struct fscache_retrieval, op);
236 _enter("{OP%x}", op->op.debug_id);
238 ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
240 fscache_hist(fscache_retrieval_histogram, op->start_time);
242 fscache_put_context(op->op.object->cookie, op->context);
248 * allocate a retrieval op
250 static struct fscache_retrieval *fscache_alloc_retrieval(
251 struct fscache_cookie *cookie,
252 struct address_space *mapping,
253 fscache_rw_complete_t end_io_func,
256 struct fscache_retrieval *op;
258 /* allocate a retrieval operation and attempt to submit it */
259 op = kzalloc(sizeof(*op), GFP_NOIO);
261 fscache_stat(&fscache_n_retrievals_nomem);
265 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
266 atomic_inc(&cookie->n_active);
267 op->op.flags = FSCACHE_OP_MYTHREAD |
268 (1UL << FSCACHE_OP_WAITING) |
269 (1UL << FSCACHE_OP_UNUSE_COOKIE);
270 op->mapping = mapping;
271 op->end_io_func = end_io_func;
272 op->context = context;
273 op->start_time = jiffies;
274 INIT_LIST_HEAD(&op->to_do);
279 * wait for a deferred lookup to complete
281 static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
287 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
288 _leave(" = 0 [imm]");
292 fscache_stat(&fscache_n_retrievals_wait);
295 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
296 fscache_wait_bit_interruptible,
297 TASK_INTERRUPTIBLE) != 0) {
298 fscache_stat(&fscache_n_retrievals_intr);
299 _leave(" = -ERESTARTSYS");
303 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
306 fscache_hist(fscache_retrieval_delay_histogram, jif);
307 _leave(" = 0 [dly]");
312 * Handle cancellation of a pending retrieval op
314 static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
316 struct fscache_retrieval *op =
317 container_of(_op, struct fscache_retrieval, op);
319 atomic_set(&op->n_pages, 0);
323 * wait for an object to become active (or dead)
325 static int fscache_wait_for_retrieval_activation(struct fscache_object *object,
326 struct fscache_retrieval *op,
327 atomic_t *stat_op_waits,
328 atomic_t *stat_object_dead)
332 if (!test_bit(FSCACHE_OP_WAITING, &op->op.flags))
336 fscache_stat(stat_op_waits);
337 if (wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
338 fscache_wait_bit_interruptible,
339 TASK_INTERRUPTIBLE) != 0) {
340 ret = fscache_cancel_op(&op->op, fscache_do_cancel_retrieval);
344 /* it's been removed from the pending queue by another party,
345 * so we should get to run shortly */
346 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
347 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
352 if (op->op.state == FSCACHE_OP_ST_CANCELLED) {
353 fscache_stat(stat_object_dead);
354 _leave(" = -ENOBUFS [cancelled]");
357 if (unlikely(fscache_object_is_dead(object))) {
358 pr_err("%s() = -ENOBUFS [obj dead %d]\n", __func__, op->op.state);
359 fscache_cancel_op(&op->op, fscache_do_cancel_retrieval);
360 fscache_stat(stat_object_dead);
367 * read a page from the cache or allocate a block in which to store it
369 * -ENOMEM - out of memory, nothing done
370 * -ERESTARTSYS - interrupted
371 * -ENOBUFS - no backing object available in which to cache the block
372 * -ENODATA - no data available in the backing object for this block
373 * 0 - dispatched a read - it'll call end_io_func() when finished
375 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
377 fscache_rw_complete_t end_io_func,
381 struct fscache_retrieval *op;
382 struct fscache_object *object;
385 _enter("%p,%p,,,", cookie, page);
387 fscache_stat(&fscache_n_retrievals);
389 if (hlist_empty(&cookie->backing_objects))
392 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
393 _leave(" = -ENOBUFS [invalidating]");
397 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
398 ASSERTCMP(page, !=, NULL);
400 if (fscache_wait_for_deferred_lookup(cookie) < 0)
403 op = fscache_alloc_retrieval(cookie, page->mapping,
404 end_io_func,context);
406 _leave(" = -ENOMEM");
409 atomic_set(&op->n_pages, 1);
411 spin_lock(&cookie->lock);
413 if (hlist_empty(&cookie->backing_objects))
415 object = hlist_entry(cookie->backing_objects.first,
416 struct fscache_object, cookie_link);
418 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
420 atomic_inc(&object->n_reads);
421 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
423 if (fscache_submit_op(object, &op->op) < 0)
424 goto nobufs_unlock_dec;
425 spin_unlock(&cookie->lock);
427 fscache_stat(&fscache_n_retrieval_ops);
429 /* pin the netfs read context in case we need to do the actual netfs
430 * read because we've encountered a cache read failure */
431 fscache_get_context(object->cookie, op->context);
433 /* we wait for the operation to become active, and then process it
434 * *here*, in this thread, and not in the thread pool */
435 ret = fscache_wait_for_retrieval_activation(
437 __fscache_stat(&fscache_n_retrieval_op_waits),
438 __fscache_stat(&fscache_n_retrievals_object_dead));
442 /* ask the cache to honour the operation */
443 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
444 fscache_stat(&fscache_n_cop_allocate_page);
445 ret = object->cache->ops->allocate_page(op, page, gfp);
446 fscache_stat_d(&fscache_n_cop_allocate_page);
450 fscache_stat(&fscache_n_cop_read_or_alloc_page);
451 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
452 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
457 fscache_stat(&fscache_n_retrievals_nomem);
458 else if (ret == -ERESTARTSYS)
459 fscache_stat(&fscache_n_retrievals_intr);
460 else if (ret == -ENODATA)
461 fscache_stat(&fscache_n_retrievals_nodata);
463 fscache_stat(&fscache_n_retrievals_nobufs);
465 fscache_stat(&fscache_n_retrievals_ok);
467 fscache_put_retrieval(op);
468 _leave(" = %d", ret);
472 atomic_dec(&object->n_reads);
474 spin_unlock(&cookie->lock);
475 atomic_dec(&cookie->n_active);
478 fscache_stat(&fscache_n_retrievals_nobufs);
479 _leave(" = -ENOBUFS");
482 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
485 * read a list of page from the cache or allocate a block in which to store
488 * -ENOMEM - out of memory, some pages may be being read
489 * -ERESTARTSYS - interrupted, some pages may be being read
490 * -ENOBUFS - no backing object or space available in which to cache any
491 * pages not being read
492 * -ENODATA - no data available in the backing object for some or all of
494 * 0 - dispatched a read on all pages
496 * end_io_func() will be called for each page read from the cache as it is
497 * finishes being read
499 * any pages for which a read is dispatched will be removed from pages and
502 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
503 struct address_space *mapping,
504 struct list_head *pages,
506 fscache_rw_complete_t end_io_func,
510 struct fscache_retrieval *op;
511 struct fscache_object *object;
514 _enter("%p,,%d,,,", cookie, *nr_pages);
516 fscache_stat(&fscache_n_retrievals);
518 if (hlist_empty(&cookie->backing_objects))
521 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
522 _leave(" = -ENOBUFS [invalidating]");
526 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
527 ASSERTCMP(*nr_pages, >, 0);
528 ASSERT(!list_empty(pages));
530 if (fscache_wait_for_deferred_lookup(cookie) < 0)
533 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
536 atomic_set(&op->n_pages, *nr_pages);
538 spin_lock(&cookie->lock);
540 if (hlist_empty(&cookie->backing_objects))
542 object = hlist_entry(cookie->backing_objects.first,
543 struct fscache_object, cookie_link);
545 atomic_inc(&object->n_reads);
546 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
548 if (fscache_submit_op(object, &op->op) < 0)
549 goto nobufs_unlock_dec;
550 spin_unlock(&cookie->lock);
552 fscache_stat(&fscache_n_retrieval_ops);
554 /* pin the netfs read context in case we need to do the actual netfs
555 * read because we've encountered a cache read failure */
556 fscache_get_context(object->cookie, op->context);
558 /* we wait for the operation to become active, and then process it
559 * *here*, in this thread, and not in the thread pool */
560 ret = fscache_wait_for_retrieval_activation(
562 __fscache_stat(&fscache_n_retrieval_op_waits),
563 __fscache_stat(&fscache_n_retrievals_object_dead));
567 /* ask the cache to honour the operation */
568 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
569 fscache_stat(&fscache_n_cop_allocate_pages);
570 ret = object->cache->ops->allocate_pages(
571 op, pages, nr_pages, gfp);
572 fscache_stat_d(&fscache_n_cop_allocate_pages);
574 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
575 ret = object->cache->ops->read_or_alloc_pages(
576 op, pages, nr_pages, gfp);
577 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
582 fscache_stat(&fscache_n_retrievals_nomem);
583 else if (ret == -ERESTARTSYS)
584 fscache_stat(&fscache_n_retrievals_intr);
585 else if (ret == -ENODATA)
586 fscache_stat(&fscache_n_retrievals_nodata);
588 fscache_stat(&fscache_n_retrievals_nobufs);
590 fscache_stat(&fscache_n_retrievals_ok);
592 fscache_put_retrieval(op);
593 _leave(" = %d", ret);
597 atomic_dec(&object->n_reads);
599 spin_unlock(&cookie->lock);
600 atomic_dec(&cookie->n_active);
603 fscache_stat(&fscache_n_retrievals_nobufs);
604 _leave(" = -ENOBUFS");
607 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
610 * allocate a block in the cache on which to store a page
612 * -ENOMEM - out of memory, nothing done
613 * -ERESTARTSYS - interrupted
614 * -ENOBUFS - no backing object available in which to cache the block
615 * 0 - block allocated
617 int __fscache_alloc_page(struct fscache_cookie *cookie,
621 struct fscache_retrieval *op;
622 struct fscache_object *object;
625 _enter("%p,%p,,,", cookie, page);
627 fscache_stat(&fscache_n_allocs);
629 if (hlist_empty(&cookie->backing_objects))
632 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
633 ASSERTCMP(page, !=, NULL);
635 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
636 _leave(" = -ENOBUFS [invalidating]");
640 if (fscache_wait_for_deferred_lookup(cookie) < 0)
643 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
646 atomic_set(&op->n_pages, 1);
648 spin_lock(&cookie->lock);
650 if (hlist_empty(&cookie->backing_objects))
652 object = hlist_entry(cookie->backing_objects.first,
653 struct fscache_object, cookie_link);
655 if (fscache_submit_op(object, &op->op) < 0)
657 spin_unlock(&cookie->lock);
659 fscache_stat(&fscache_n_alloc_ops);
661 ret = fscache_wait_for_retrieval_activation(
663 __fscache_stat(&fscache_n_alloc_op_waits),
664 __fscache_stat(&fscache_n_allocs_object_dead));
668 /* ask the cache to honour the operation */
669 fscache_stat(&fscache_n_cop_allocate_page);
670 ret = object->cache->ops->allocate_page(op, page, gfp);
671 fscache_stat_d(&fscache_n_cop_allocate_page);
674 if (ret == -ERESTARTSYS)
675 fscache_stat(&fscache_n_allocs_intr);
677 fscache_stat(&fscache_n_allocs_nobufs);
679 fscache_stat(&fscache_n_allocs_ok);
681 fscache_put_retrieval(op);
682 _leave(" = %d", ret);
686 spin_unlock(&cookie->lock);
687 atomic_dec(&cookie->n_active);
690 fscache_stat(&fscache_n_allocs_nobufs);
691 _leave(" = -ENOBUFS");
694 EXPORT_SYMBOL(__fscache_alloc_page);
697 * release a write op reference
699 static void fscache_release_write_op(struct fscache_operation *_op)
701 _enter("{OP%x}", _op->debug_id);
705 * perform the background storage of a page into the cache
707 static void fscache_write_op(struct fscache_operation *_op)
709 struct fscache_storage *op =
710 container_of(_op, struct fscache_storage, op);
711 struct fscache_object *object = op->op.object;
712 struct fscache_cookie *cookie;
718 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
720 spin_lock(&object->lock);
721 cookie = object->cookie;
723 if (!fscache_object_is_active(object)) {
724 /* If we get here, then the on-disk cache object likely longer
725 * exists, so we should just cancel this write operation.
727 spin_unlock(&object->lock);
728 fscache_op_complete(&op->op, false);
729 _leave(" [inactive]");
734 /* If we get here, then the cookie belonging to the object was
735 * detached, probably by the cookie being withdrawn due to
736 * memory pressure, which means that the pages we might write
737 * to the cache from no longer exist - therefore, we can just
738 * cancel this write operation.
740 spin_unlock(&object->lock);
741 fscache_op_complete(&op->op, false);
742 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
743 _op->flags, _op->state, object->state->short_name,
748 spin_lock(&cookie->stores_lock);
750 fscache_stat(&fscache_n_store_calls);
752 /* find a page to store */
754 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
755 FSCACHE_COOKIE_PENDING_TAG);
759 _debug("gang %d [%lx]", n, page->index);
760 if (page->index > op->store_limit) {
761 fscache_stat(&fscache_n_store_pages_over_limit);
765 radix_tree_tag_set(&cookie->stores, page->index,
766 FSCACHE_COOKIE_STORING_TAG);
767 radix_tree_tag_clear(&cookie->stores, page->index,
768 FSCACHE_COOKIE_PENDING_TAG);
770 spin_unlock(&cookie->stores_lock);
771 spin_unlock(&object->lock);
773 fscache_stat(&fscache_n_store_pages);
774 fscache_stat(&fscache_n_cop_write_page);
775 ret = object->cache->ops->write_page(op, page);
776 fscache_stat_d(&fscache_n_cop_write_page);
777 fscache_end_page_write(object, page);
779 fscache_abort_object(object);
780 fscache_op_complete(&op->op, true);
782 fscache_enqueue_operation(&op->op);
789 /* this writer is going away and there aren't any more things to
792 spin_unlock(&cookie->stores_lock);
793 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
794 spin_unlock(&object->lock);
795 fscache_op_complete(&op->op, true);
800 * Clear the pages pending writing for invalidation
802 void fscache_invalidate_writes(struct fscache_cookie *cookie)
811 spin_lock(&cookie->stores_lock);
812 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
814 FSCACHE_COOKIE_PENDING_TAG);
816 spin_unlock(&cookie->stores_lock);
820 for (i = n - 1; i >= 0; i--) {
822 radix_tree_delete(&cookie->stores, page->index);
825 spin_unlock(&cookie->stores_lock);
827 for (i = n - 1; i >= 0; i--)
828 page_cache_release(results[i]);
835 * request a page be stored in the cache
837 * -ENOMEM - out of memory, nothing done
838 * -ENOBUFS - no backing object available in which to cache the page
839 * 0 - dispatched a write - it'll call end_io_func() when finished
841 * if the cookie still has a backing object at this point, that object can be
842 * in one of a few states with respect to storage processing:
844 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
849 * (b) writes deferred till post-creation (mark page for writing and
850 * return immediately)
852 * (2) negative lookup, object created, initial fill being made from netfs
854 * (a) fill point not yet reached this page (mark page for writing and
857 * (b) fill point passed this page (queue op to store this page)
859 * (3) object extant (queue op to store this page)
861 * any other state is invalid
863 int __fscache_write_page(struct fscache_cookie *cookie,
867 struct fscache_storage *op;
868 struct fscache_object *object;
871 _enter("%p,%x,", cookie, (u32) page->flags);
873 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
874 ASSERT(PageFsCache(page));
876 fscache_stat(&fscache_n_stores);
878 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
879 _leave(" = -ENOBUFS [invalidating]");
883 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
887 fscache_operation_init(&op->op, fscache_write_op,
888 fscache_release_write_op);
889 op->op.flags = FSCACHE_OP_ASYNC |
890 (1 << FSCACHE_OP_WAITING) |
891 (1 << FSCACHE_OP_UNUSE_COOKIE);
893 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
898 spin_lock(&cookie->lock);
900 if (hlist_empty(&cookie->backing_objects))
902 object = hlist_entry(cookie->backing_objects.first,
903 struct fscache_object, cookie_link);
904 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
907 /* add the page to the pending-storage radix tree on the backing
909 spin_lock(&object->lock);
910 spin_lock(&cookie->stores_lock);
912 _debug("store limit %llx", (unsigned long long) object->store_limit);
914 ret = radix_tree_insert(&cookie->stores, page->index, page);
918 _debug("insert failed %d", ret);
919 goto nobufs_unlock_obj;
922 radix_tree_tag_set(&cookie->stores, page->index,
923 FSCACHE_COOKIE_PENDING_TAG);
924 page_cache_get(page);
926 /* we only want one writer at a time, but we do need to queue new
927 * writers after exclusive ops */
928 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
929 goto already_pending;
931 spin_unlock(&cookie->stores_lock);
932 spin_unlock(&object->lock);
934 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
935 op->store_limit = object->store_limit;
937 atomic_inc(&cookie->n_active);
938 if (fscache_submit_op(object, &op->op) < 0)
941 spin_unlock(&cookie->lock);
942 radix_tree_preload_end();
943 fscache_stat(&fscache_n_store_ops);
944 fscache_stat(&fscache_n_stores_ok);
946 /* the work queue now carries its own ref on the object */
947 fscache_put_operation(&op->op);
952 fscache_stat(&fscache_n_stores_again);
954 spin_unlock(&cookie->stores_lock);
955 spin_unlock(&object->lock);
956 spin_unlock(&cookie->lock);
957 radix_tree_preload_end();
959 fscache_stat(&fscache_n_stores_ok);
964 atomic_dec(&cookie->n_active);
965 spin_lock(&cookie->stores_lock);
966 radix_tree_delete(&cookie->stores, page->index);
967 spin_unlock(&cookie->stores_lock);
968 page_cache_release(page);
973 spin_unlock(&cookie->stores_lock);
974 spin_unlock(&object->lock);
976 spin_unlock(&cookie->lock);
977 radix_tree_preload_end();
979 fscache_stat(&fscache_n_stores_nobufs);
980 _leave(" = -ENOBUFS");
986 fscache_stat(&fscache_n_stores_oom);
987 _leave(" = -ENOMEM");
990 EXPORT_SYMBOL(__fscache_write_page);
993 * remove a page from the cache
995 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
997 struct fscache_object *object;
1001 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1002 ASSERTCMP(page, !=, NULL);
1004 fscache_stat(&fscache_n_uncaches);
1006 /* cache withdrawal may beat us to it */
1007 if (!PageFsCache(page))
1010 /* get the object */
1011 spin_lock(&cookie->lock);
1013 if (hlist_empty(&cookie->backing_objects)) {
1014 ClearPageFsCache(page);
1018 object = hlist_entry(cookie->backing_objects.first,
1019 struct fscache_object, cookie_link);
1021 /* there might now be stuff on disk we could read */
1022 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1024 /* only invoke the cache backend if we managed to mark the page
1025 * uncached here; this deals with synchronisation vs withdrawal */
1026 if (TestClearPageFsCache(page) &&
1027 object->cache->ops->uncache_page) {
1028 /* the cache backend releases the cookie lock */
1029 fscache_stat(&fscache_n_cop_uncache_page);
1030 object->cache->ops->uncache_page(object, page);
1031 fscache_stat_d(&fscache_n_cop_uncache_page);
1036 spin_unlock(&cookie->lock);
1040 EXPORT_SYMBOL(__fscache_uncache_page);
1043 * fscache_mark_page_cached - Mark a page as being cached
1044 * @op: The retrieval op pages are being marked for
1045 * @page: The page to be marked
1047 * Mark a netfs page as being cached. After this is called, the netfs
1048 * must call fscache_uncache_page() to remove the mark.
1050 void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1052 struct fscache_cookie *cookie = op->op.object->cookie;
1054 #ifdef CONFIG_FSCACHE_STATS
1055 atomic_inc(&fscache_n_marks);
1058 _debug("- mark %p{%lx}", page, page->index);
1059 if (TestSetPageFsCache(page)) {
1060 static bool once_only;
1063 printk(KERN_WARNING "FS-Cache:"
1064 " Cookie type %s marked page %lx"
1065 " multiple times\n",
1066 cookie->def->name, page->index);
1070 if (cookie->def->mark_page_cached)
1071 cookie->def->mark_page_cached(cookie->netfs_data,
1074 EXPORT_SYMBOL(fscache_mark_page_cached);
1077 * fscache_mark_pages_cached - Mark pages as being cached
1078 * @op: The retrieval op pages are being marked for
1079 * @pagevec: The pages to be marked
1081 * Mark a bunch of netfs pages as being cached. After this is called,
1082 * the netfs must call fscache_uncache_page() to remove the mark.
1084 void fscache_mark_pages_cached(struct fscache_retrieval *op,
1085 struct pagevec *pagevec)
1089 for (loop = 0; loop < pagevec->nr; loop++)
1090 fscache_mark_page_cached(op, pagevec->pages[loop]);
1092 pagevec_reinit(pagevec);
1094 EXPORT_SYMBOL(fscache_mark_pages_cached);
1097 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1098 * to be associated with the given cookie.
1100 void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1101 struct inode *inode)
1103 struct address_space *mapping = inode->i_mapping;
1104 struct pagevec pvec;
1108 _enter("%p,%p", cookie, inode);
1110 if (!mapping || mapping->nrpages == 0) {
1111 _leave(" [no pages]");
1115 pagevec_init(&pvec, 0);
1118 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1120 for (i = 0; i < pagevec_count(&pvec); i++) {
1121 struct page *page = pvec.pages[i];
1123 if (PageFsCache(page)) {
1124 __fscache_wait_on_page_write(cookie, page);
1125 __fscache_uncache_page(cookie, page);
1128 pagevec_release(&pvec);
1134 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);