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)) {
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_stat(&fscache_n_cop_attr_changed);
168 ret = object->cache->ops->attr_changed(object);
169 fscache_stat_d(&fscache_n_cop_attr_changed);
171 fscache_abort_object(object);
174 fscache_op_complete(op);
179 * notification that the attributes on an object have changed
181 int __fscache_attr_changed(struct fscache_cookie *cookie)
183 struct fscache_operation *op;
184 struct fscache_object *object;
186 _enter("%p", cookie);
188 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
190 fscache_stat(&fscache_n_attr_changed);
192 op = kzalloc(sizeof(*op), GFP_KERNEL);
194 fscache_stat(&fscache_n_attr_changed_nomem);
195 _leave(" = -ENOMEM");
199 fscache_operation_init(op, fscache_attr_changed_op, NULL);
200 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
202 spin_lock(&cookie->lock);
204 if (hlist_empty(&cookie->backing_objects))
206 object = hlist_entry(cookie->backing_objects.first,
207 struct fscache_object, cookie_link);
209 if (fscache_submit_exclusive_op(object, op) < 0)
211 spin_unlock(&cookie->lock);
212 fscache_stat(&fscache_n_attr_changed_ok);
213 fscache_put_operation(op);
218 spin_unlock(&cookie->lock);
220 fscache_stat(&fscache_n_attr_changed_nobufs);
221 _leave(" = %d", -ENOBUFS);
224 EXPORT_SYMBOL(__fscache_attr_changed);
227 * release a retrieval op reference
229 static void fscache_release_retrieval_op(struct fscache_operation *_op)
231 struct fscache_retrieval *op =
232 container_of(_op, struct fscache_retrieval, op);
234 _enter("{OP%x}", op->op.debug_id);
236 ASSERTCMP(op->n_pages, ==, 0);
238 fscache_hist(fscache_retrieval_histogram, op->start_time);
240 fscache_put_context(op->op.object->cookie, op->context);
246 * allocate a retrieval op
248 static struct fscache_retrieval *fscache_alloc_retrieval(
249 struct address_space *mapping,
250 fscache_rw_complete_t end_io_func,
253 struct fscache_retrieval *op;
255 /* allocate a retrieval operation and attempt to submit it */
256 op = kzalloc(sizeof(*op), GFP_NOIO);
258 fscache_stat(&fscache_n_retrievals_nomem);
262 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
263 op->op.flags = FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING);
264 op->mapping = mapping;
265 op->end_io_func = end_io_func;
266 op->context = context;
267 op->start_time = jiffies;
268 INIT_LIST_HEAD(&op->to_do);
273 * wait for a deferred lookup to complete
275 static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
281 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
282 _leave(" = 0 [imm]");
286 fscache_stat(&fscache_n_retrievals_wait);
289 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
290 fscache_wait_bit_interruptible,
291 TASK_INTERRUPTIBLE) != 0) {
292 fscache_stat(&fscache_n_retrievals_intr);
293 _leave(" = -ERESTARTSYS");
297 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
300 fscache_hist(fscache_retrieval_delay_histogram, jif);
301 _leave(" = 0 [dly]");
306 * wait for an object to become active (or dead)
308 static int fscache_wait_for_retrieval_activation(struct fscache_object *object,
309 struct fscache_retrieval *op,
310 atomic_t *stat_op_waits,
311 atomic_t *stat_object_dead)
315 if (!test_bit(FSCACHE_OP_WAITING, &op->op.flags))
319 fscache_stat(stat_op_waits);
320 if (wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
321 fscache_wait_bit_interruptible,
322 TASK_INTERRUPTIBLE) != 0) {
323 ret = fscache_cancel_op(&op->op);
327 /* it's been removed from the pending queue by another party,
328 * so we should get to run shortly */
329 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
330 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
335 if (op->op.state == FSCACHE_OP_ST_CANCELLED) {
336 fscache_stat(stat_object_dead);
337 _leave(" = -ENOBUFS [cancelled]");
340 if (unlikely(fscache_object_is_dead(object))) {
341 pr_err("%s() = -ENOBUFS [obj dead %d]", __func__, op->op.state);
342 fscache_cancel_op(&op->op);
343 fscache_stat(stat_object_dead);
350 * read a page from the cache or allocate a block in which to store it
352 * -ENOMEM - out of memory, nothing done
353 * -ERESTARTSYS - interrupted
354 * -ENOBUFS - no backing object available in which to cache the block
355 * -ENODATA - no data available in the backing object for this block
356 * 0 - dispatched a read - it'll call end_io_func() when finished
358 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
360 fscache_rw_complete_t end_io_func,
364 struct fscache_retrieval *op;
365 struct fscache_object *object;
368 _enter("%p,%p,,,", cookie, page);
370 fscache_stat(&fscache_n_retrievals);
372 if (hlist_empty(&cookie->backing_objects))
375 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
376 _leave(" = -ENOBUFS [invalidating]");
380 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
381 ASSERTCMP(page, !=, NULL);
383 if (fscache_wait_for_deferred_lookup(cookie) < 0)
386 op = fscache_alloc_retrieval(page->mapping, end_io_func, context);
388 _leave(" = -ENOMEM");
393 spin_lock(&cookie->lock);
395 if (hlist_empty(&cookie->backing_objects))
397 object = hlist_entry(cookie->backing_objects.first,
398 struct fscache_object, cookie_link);
400 ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP);
402 atomic_inc(&object->n_reads);
403 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
405 if (fscache_submit_op(object, &op->op) < 0)
406 goto nobufs_unlock_dec;
407 spin_unlock(&cookie->lock);
409 fscache_stat(&fscache_n_retrieval_ops);
411 /* pin the netfs read context in case we need to do the actual netfs
412 * read because we've encountered a cache read failure */
413 fscache_get_context(object->cookie, op->context);
415 /* we wait for the operation to become active, and then process it
416 * *here*, in this thread, and not in the thread pool */
417 ret = fscache_wait_for_retrieval_activation(
419 __fscache_stat(&fscache_n_retrieval_op_waits),
420 __fscache_stat(&fscache_n_retrievals_object_dead));
424 /* ask the cache to honour the operation */
425 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
426 fscache_stat(&fscache_n_cop_allocate_page);
427 ret = object->cache->ops->allocate_page(op, page, gfp);
428 fscache_stat_d(&fscache_n_cop_allocate_page);
432 fscache_stat(&fscache_n_cop_read_or_alloc_page);
433 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
434 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
439 fscache_stat(&fscache_n_retrievals_nomem);
440 else if (ret == -ERESTARTSYS)
441 fscache_stat(&fscache_n_retrievals_intr);
442 else if (ret == -ENODATA)
443 fscache_stat(&fscache_n_retrievals_nodata);
445 fscache_stat(&fscache_n_retrievals_nobufs);
447 fscache_stat(&fscache_n_retrievals_ok);
449 fscache_put_retrieval(op);
450 _leave(" = %d", ret);
454 atomic_dec(&object->n_reads);
456 spin_unlock(&cookie->lock);
459 fscache_stat(&fscache_n_retrievals_nobufs);
460 _leave(" = -ENOBUFS");
463 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
466 * read a list of page from the cache or allocate a block in which to store
469 * -ENOMEM - out of memory, some pages may be being read
470 * -ERESTARTSYS - interrupted, some pages may be being read
471 * -ENOBUFS - no backing object or space available in which to cache any
472 * pages not being read
473 * -ENODATA - no data available in the backing object for some or all of
475 * 0 - dispatched a read on all pages
477 * end_io_func() will be called for each page read from the cache as it is
478 * finishes being read
480 * any pages for which a read is dispatched will be removed from pages and
483 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
484 struct address_space *mapping,
485 struct list_head *pages,
487 fscache_rw_complete_t end_io_func,
491 struct fscache_retrieval *op;
492 struct fscache_object *object;
495 _enter("%p,,%d,,,", cookie, *nr_pages);
497 fscache_stat(&fscache_n_retrievals);
499 if (hlist_empty(&cookie->backing_objects))
502 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
503 _leave(" = -ENOBUFS [invalidating]");
507 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
508 ASSERTCMP(*nr_pages, >, 0);
509 ASSERT(!list_empty(pages));
511 if (fscache_wait_for_deferred_lookup(cookie) < 0)
514 op = fscache_alloc_retrieval(mapping, end_io_func, context);
517 op->n_pages = *nr_pages;
519 spin_lock(&cookie->lock);
521 if (hlist_empty(&cookie->backing_objects))
523 object = hlist_entry(cookie->backing_objects.first,
524 struct fscache_object, cookie_link);
526 atomic_inc(&object->n_reads);
527 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
529 if (fscache_submit_op(object, &op->op) < 0)
530 goto nobufs_unlock_dec;
531 spin_unlock(&cookie->lock);
533 fscache_stat(&fscache_n_retrieval_ops);
535 /* pin the netfs read context in case we need to do the actual netfs
536 * read because we've encountered a cache read failure */
537 fscache_get_context(object->cookie, op->context);
539 /* we wait for the operation to become active, and then process it
540 * *here*, in this thread, and not in the thread pool */
541 ret = fscache_wait_for_retrieval_activation(
543 __fscache_stat(&fscache_n_retrieval_op_waits),
544 __fscache_stat(&fscache_n_retrievals_object_dead));
548 /* ask the cache to honour the operation */
549 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
550 fscache_stat(&fscache_n_cop_allocate_pages);
551 ret = object->cache->ops->allocate_pages(
552 op, pages, nr_pages, gfp);
553 fscache_stat_d(&fscache_n_cop_allocate_pages);
555 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
556 ret = object->cache->ops->read_or_alloc_pages(
557 op, pages, nr_pages, gfp);
558 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
563 fscache_stat(&fscache_n_retrievals_nomem);
564 else if (ret == -ERESTARTSYS)
565 fscache_stat(&fscache_n_retrievals_intr);
566 else if (ret == -ENODATA)
567 fscache_stat(&fscache_n_retrievals_nodata);
569 fscache_stat(&fscache_n_retrievals_nobufs);
571 fscache_stat(&fscache_n_retrievals_ok);
573 fscache_put_retrieval(op);
574 _leave(" = %d", ret);
578 atomic_dec(&object->n_reads);
580 spin_unlock(&cookie->lock);
583 fscache_stat(&fscache_n_retrievals_nobufs);
584 _leave(" = -ENOBUFS");
587 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
590 * allocate a block in the cache on which to store a page
592 * -ENOMEM - out of memory, nothing done
593 * -ERESTARTSYS - interrupted
594 * -ENOBUFS - no backing object available in which to cache the block
595 * 0 - block allocated
597 int __fscache_alloc_page(struct fscache_cookie *cookie,
601 struct fscache_retrieval *op;
602 struct fscache_object *object;
605 _enter("%p,%p,,,", cookie, page);
607 fscache_stat(&fscache_n_allocs);
609 if (hlist_empty(&cookie->backing_objects))
612 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
613 ASSERTCMP(page, !=, NULL);
615 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
616 _leave(" = -ENOBUFS [invalidating]");
620 if (fscache_wait_for_deferred_lookup(cookie) < 0)
623 op = fscache_alloc_retrieval(page->mapping, NULL, NULL);
628 spin_lock(&cookie->lock);
630 if (hlist_empty(&cookie->backing_objects))
632 object = hlist_entry(cookie->backing_objects.first,
633 struct fscache_object, cookie_link);
635 if (fscache_submit_op(object, &op->op) < 0)
637 spin_unlock(&cookie->lock);
639 fscache_stat(&fscache_n_alloc_ops);
641 ret = fscache_wait_for_retrieval_activation(
643 __fscache_stat(&fscache_n_alloc_op_waits),
644 __fscache_stat(&fscache_n_allocs_object_dead));
648 /* ask the cache to honour the operation */
649 fscache_stat(&fscache_n_cop_allocate_page);
650 ret = object->cache->ops->allocate_page(op, page, gfp);
651 fscache_stat_d(&fscache_n_cop_allocate_page);
654 if (ret == -ERESTARTSYS)
655 fscache_stat(&fscache_n_allocs_intr);
657 fscache_stat(&fscache_n_allocs_nobufs);
659 fscache_stat(&fscache_n_allocs_ok);
661 fscache_put_retrieval(op);
662 _leave(" = %d", ret);
666 spin_unlock(&cookie->lock);
669 fscache_stat(&fscache_n_allocs_nobufs);
670 _leave(" = -ENOBUFS");
673 EXPORT_SYMBOL(__fscache_alloc_page);
676 * release a write op reference
678 static void fscache_release_write_op(struct fscache_operation *_op)
680 _enter("{OP%x}", _op->debug_id);
684 * perform the background storage of a page into the cache
686 static void fscache_write_op(struct fscache_operation *_op)
688 struct fscache_storage *op =
689 container_of(_op, struct fscache_storage, op);
690 struct fscache_object *object = op->op.object;
691 struct fscache_cookie *cookie;
697 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
699 spin_lock(&object->lock);
700 cookie = object->cookie;
702 if (!fscache_object_is_active(object) || !cookie) {
703 spin_unlock(&object->lock);
708 spin_lock(&cookie->stores_lock);
710 fscache_stat(&fscache_n_store_calls);
712 /* find a page to store */
714 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
715 FSCACHE_COOKIE_PENDING_TAG);
719 _debug("gang %d [%lx]", n, page->index);
720 if (page->index > op->store_limit) {
721 fscache_stat(&fscache_n_store_pages_over_limit);
725 radix_tree_tag_set(&cookie->stores, page->index,
726 FSCACHE_COOKIE_STORING_TAG);
727 radix_tree_tag_clear(&cookie->stores, page->index,
728 FSCACHE_COOKIE_PENDING_TAG);
730 spin_unlock(&cookie->stores_lock);
731 spin_unlock(&object->lock);
733 fscache_stat(&fscache_n_store_pages);
734 fscache_stat(&fscache_n_cop_write_page);
735 ret = object->cache->ops->write_page(op, page);
736 fscache_stat_d(&fscache_n_cop_write_page);
737 fscache_end_page_write(object, page);
739 fscache_abort_object(object);
740 fscache_op_complete(&op->op);
742 fscache_enqueue_operation(&op->op);
749 /* this writer is going away and there aren't any more things to
752 spin_unlock(&cookie->stores_lock);
753 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
754 spin_unlock(&object->lock);
755 fscache_op_complete(&op->op);
760 * Clear the pages pending writing for invalidation
762 void fscache_invalidate_writes(struct fscache_cookie *cookie)
770 while (spin_lock(&cookie->stores_lock),
771 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
773 FSCACHE_COOKIE_PENDING_TAG),
775 for (i = n - 1; i >= 0; i--) {
777 radix_tree_delete(&cookie->stores, page->index);
780 spin_unlock(&cookie->stores_lock);
782 for (i = n - 1; i >= 0; i--)
783 page_cache_release(results[i]);
786 spin_unlock(&cookie->stores_lock);
791 * request a page be stored in the cache
793 * -ENOMEM - out of memory, nothing done
794 * -ENOBUFS - no backing object available in which to cache the page
795 * 0 - dispatched a write - it'll call end_io_func() when finished
797 * if the cookie still has a backing object at this point, that object can be
798 * in one of a few states with respect to storage processing:
800 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
803 * (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
806 * (b) writes deferred till post-creation (mark page for writing and
807 * return immediately)
809 * (2) negative lookup, object created, initial fill being made from netfs
810 * (FSCACHE_COOKIE_INITIAL_FILL is set)
812 * (a) fill point not yet reached this page (mark page for writing and
815 * (b) fill point passed this page (queue op to store this page)
817 * (3) object extant (queue op to store this page)
819 * any other state is invalid
821 int __fscache_write_page(struct fscache_cookie *cookie,
825 struct fscache_storage *op;
826 struct fscache_object *object;
829 _enter("%p,%x,", cookie, (u32) page->flags);
831 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
832 ASSERT(PageFsCache(page));
834 fscache_stat(&fscache_n_stores);
836 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
837 _leave(" = -ENOBUFS [invalidating]");
841 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
845 fscache_operation_init(&op->op, fscache_write_op,
846 fscache_release_write_op);
847 op->op.flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_WAITING);
849 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
854 spin_lock(&cookie->lock);
856 if (hlist_empty(&cookie->backing_objects))
858 object = hlist_entry(cookie->backing_objects.first,
859 struct fscache_object, cookie_link);
860 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
863 /* add the page to the pending-storage radix tree on the backing
865 spin_lock(&object->lock);
866 spin_lock(&cookie->stores_lock);
868 _debug("store limit %llx", (unsigned long long) object->store_limit);
870 ret = radix_tree_insert(&cookie->stores, page->index, page);
874 _debug("insert failed %d", ret);
875 goto nobufs_unlock_obj;
878 radix_tree_tag_set(&cookie->stores, page->index,
879 FSCACHE_COOKIE_PENDING_TAG);
880 page_cache_get(page);
882 /* we only want one writer at a time, but we do need to queue new
883 * writers after exclusive ops */
884 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
885 goto already_pending;
887 spin_unlock(&cookie->stores_lock);
888 spin_unlock(&object->lock);
890 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
891 op->store_limit = object->store_limit;
893 if (fscache_submit_op(object, &op->op) < 0)
896 spin_unlock(&cookie->lock);
897 radix_tree_preload_end();
898 fscache_stat(&fscache_n_store_ops);
899 fscache_stat(&fscache_n_stores_ok);
901 /* the work queue now carries its own ref on the object */
902 fscache_put_operation(&op->op);
907 fscache_stat(&fscache_n_stores_again);
909 spin_unlock(&cookie->stores_lock);
910 spin_unlock(&object->lock);
911 spin_unlock(&cookie->lock);
912 radix_tree_preload_end();
914 fscache_stat(&fscache_n_stores_ok);
919 spin_lock(&cookie->stores_lock);
920 radix_tree_delete(&cookie->stores, page->index);
921 spin_unlock(&cookie->stores_lock);
922 page_cache_release(page);
927 spin_unlock(&cookie->stores_lock);
928 spin_unlock(&object->lock);
930 spin_unlock(&cookie->lock);
931 radix_tree_preload_end();
933 fscache_stat(&fscache_n_stores_nobufs);
934 _leave(" = -ENOBUFS");
940 fscache_stat(&fscache_n_stores_oom);
941 _leave(" = -ENOMEM");
944 EXPORT_SYMBOL(__fscache_write_page);
947 * remove a page from the cache
949 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
951 struct fscache_object *object;
955 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
956 ASSERTCMP(page, !=, NULL);
958 fscache_stat(&fscache_n_uncaches);
960 /* cache withdrawal may beat us to it */
961 if (!PageFsCache(page))
965 spin_lock(&cookie->lock);
967 if (hlist_empty(&cookie->backing_objects)) {
968 ClearPageFsCache(page);
972 object = hlist_entry(cookie->backing_objects.first,
973 struct fscache_object, cookie_link);
975 /* there might now be stuff on disk we could read */
976 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
978 /* only invoke the cache backend if we managed to mark the page
979 * uncached here; this deals with synchronisation vs withdrawal */
980 if (TestClearPageFsCache(page) &&
981 object->cache->ops->uncache_page) {
982 /* the cache backend releases the cookie lock */
983 fscache_stat(&fscache_n_cop_uncache_page);
984 object->cache->ops->uncache_page(object, page);
985 fscache_stat_d(&fscache_n_cop_uncache_page);
990 spin_unlock(&cookie->lock);
994 EXPORT_SYMBOL(__fscache_uncache_page);
997 * fscache_mark_page_cached - Mark a page as being cached
998 * @op: The retrieval op pages are being marked for
999 * @page: The page to be marked
1001 * Mark a netfs page as being cached. After this is called, the netfs
1002 * must call fscache_uncache_page() to remove the mark.
1004 void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1006 struct fscache_cookie *cookie = op->op.object->cookie;
1008 #ifdef CONFIG_FSCACHE_STATS
1009 atomic_inc(&fscache_n_marks);
1012 _debug("- mark %p{%lx}", page, page->index);
1013 if (TestSetPageFsCache(page)) {
1014 static bool once_only;
1017 printk(KERN_WARNING "FS-Cache:"
1018 " Cookie type %s marked page %lx"
1019 " multiple times\n",
1020 cookie->def->name, page->index);
1024 if (cookie->def->mark_page_cached)
1025 cookie->def->mark_page_cached(cookie->netfs_data,
1028 EXPORT_SYMBOL(fscache_mark_page_cached);
1031 * fscache_mark_pages_cached - Mark pages as being cached
1032 * @op: The retrieval op pages are being marked for
1033 * @pagevec: The pages to be marked
1035 * Mark a bunch of netfs pages as being cached. After this is called,
1036 * the netfs must call fscache_uncache_page() to remove the mark.
1038 void fscache_mark_pages_cached(struct fscache_retrieval *op,
1039 struct pagevec *pagevec)
1043 for (loop = 0; loop < pagevec->nr; loop++)
1044 fscache_mark_page_cached(op, pagevec->pages[loop]);
1046 pagevec_reinit(pagevec);
1048 EXPORT_SYMBOL(fscache_mark_pages_cached);
1051 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1052 * to be associated with the given cookie.
1054 void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1055 struct inode *inode)
1057 struct address_space *mapping = inode->i_mapping;
1058 struct pagevec pvec;
1062 _enter("%p,%p", cookie, inode);
1064 if (!mapping || mapping->nrpages == 0) {
1065 _leave(" [no pages]");
1069 pagevec_init(&pvec, 0);
1072 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1074 for (i = 0; i < pagevec_count(&pvec); i++) {
1075 struct page *page = pvec.pages[i];
1077 if (PageFsCache(page)) {
1078 __fscache_wait_on_page_write(cookie, page);
1079 __fscache_uncache_page(cookie, page);
1082 pagevec_release(&pvec);
1088 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);