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);
60 val = radix_tree_lookup(&cookie->stores, page->index);
63 fscache_stat(&fscache_n_store_vmscan_not_storing);
64 __fscache_uncache_page(cookie, page);
68 /* see if the page is actually undergoing storage - if so we can't get
69 * rid of it till the cache has finished with it */
70 if (radix_tree_tag_get(&cookie->stores, page->index,
71 FSCACHE_COOKIE_STORING_TAG)) {
76 /* the page is pending storage, so we attempt to cancel the store and
77 * discard the store request so that the page can be reclaimed */
78 spin_lock(&cookie->stores_lock);
81 if (radix_tree_tag_get(&cookie->stores, page->index,
82 FSCACHE_COOKIE_STORING_TAG)) {
83 /* the page started to undergo storage whilst we were looking,
84 * so now we can only wait or return */
85 spin_unlock(&cookie->stores_lock);
89 xpage = radix_tree_delete(&cookie->stores, page->index);
90 spin_unlock(&cookie->stores_lock);
93 fscache_stat(&fscache_n_store_vmscan_cancelled);
94 fscache_stat(&fscache_n_store_radix_deletes);
95 ASSERTCMP(xpage, ==, page);
97 fscache_stat(&fscache_n_store_vmscan_gone);
100 wake_up_bit(&cookie->flags, 0);
102 page_cache_release(xpage);
103 __fscache_uncache_page(cookie, page);
107 /* we might want to wait here, but that could deadlock the allocator as
108 * the work threads writing to the cache may all end up sleeping
109 * on memory allocation */
110 fscache_stat(&fscache_n_store_vmscan_busy);
113 EXPORT_SYMBOL(__fscache_maybe_release_page);
116 * note that a page has finished being written to the cache
118 static void fscache_end_page_write(struct fscache_object *object,
121 struct fscache_cookie *cookie;
122 struct page *xpage = NULL;
124 spin_lock(&object->lock);
125 cookie = object->cookie;
127 /* delete the page from the tree if it is now no longer
129 spin_lock(&cookie->stores_lock);
130 radix_tree_tag_clear(&cookie->stores, page->index,
131 FSCACHE_COOKIE_STORING_TAG);
132 if (!radix_tree_tag_get(&cookie->stores, page->index,
133 FSCACHE_COOKIE_PENDING_TAG)) {
134 fscache_stat(&fscache_n_store_radix_deletes);
135 xpage = radix_tree_delete(&cookie->stores, page->index);
137 spin_unlock(&cookie->stores_lock);
138 wake_up_bit(&cookie->flags, 0);
140 spin_unlock(&object->lock);
142 page_cache_release(xpage);
146 * actually apply the changed attributes to a cache object
148 static void fscache_attr_changed_op(struct fscache_operation *op)
150 struct fscache_object *object = op->object;
153 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
155 fscache_stat(&fscache_n_attr_changed_calls);
157 if (fscache_object_is_active(object)) {
158 fscache_stat(&fscache_n_cop_attr_changed);
159 ret = object->cache->ops->attr_changed(object);
160 fscache_stat_d(&fscache_n_cop_attr_changed);
162 fscache_abort_object(object);
165 fscache_op_complete(op);
170 * notification that the attributes on an object have changed
172 int __fscache_attr_changed(struct fscache_cookie *cookie)
174 struct fscache_operation *op;
175 struct fscache_object *object;
177 _enter("%p", cookie);
179 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
181 fscache_stat(&fscache_n_attr_changed);
183 op = kzalloc(sizeof(*op), GFP_KERNEL);
185 fscache_stat(&fscache_n_attr_changed_nomem);
186 _leave(" = -ENOMEM");
190 fscache_operation_init(op, fscache_attr_changed_op, NULL);
191 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
193 spin_lock(&cookie->lock);
195 if (hlist_empty(&cookie->backing_objects))
197 object = hlist_entry(cookie->backing_objects.first,
198 struct fscache_object, cookie_link);
200 if (fscache_submit_exclusive_op(object, op) < 0)
202 spin_unlock(&cookie->lock);
203 fscache_stat(&fscache_n_attr_changed_ok);
204 fscache_put_operation(op);
209 spin_unlock(&cookie->lock);
211 fscache_stat(&fscache_n_attr_changed_nobufs);
212 _leave(" = %d", -ENOBUFS);
215 EXPORT_SYMBOL(__fscache_attr_changed);
218 * release a retrieval op reference
220 static void fscache_release_retrieval_op(struct fscache_operation *_op)
222 struct fscache_retrieval *op =
223 container_of(_op, struct fscache_retrieval, op);
225 _enter("{OP%x}", op->op.debug_id);
227 ASSERTCMP(op->n_pages, ==, 0);
229 fscache_hist(fscache_retrieval_histogram, op->start_time);
231 fscache_put_context(op->op.object->cookie, op->context);
237 * allocate a retrieval op
239 static struct fscache_retrieval *fscache_alloc_retrieval(
240 struct address_space *mapping,
241 fscache_rw_complete_t end_io_func,
244 struct fscache_retrieval *op;
246 /* allocate a retrieval operation and attempt to submit it */
247 op = kzalloc(sizeof(*op), GFP_NOIO);
249 fscache_stat(&fscache_n_retrievals_nomem);
253 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
254 op->op.flags = FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING);
255 op->mapping = mapping;
256 op->end_io_func = end_io_func;
257 op->context = context;
258 op->start_time = jiffies;
259 INIT_LIST_HEAD(&op->to_do);
264 * wait for a deferred lookup to complete
266 static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
272 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
273 _leave(" = 0 [imm]");
277 fscache_stat(&fscache_n_retrievals_wait);
280 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
281 fscache_wait_bit_interruptible,
282 TASK_INTERRUPTIBLE) != 0) {
283 fscache_stat(&fscache_n_retrievals_intr);
284 _leave(" = -ERESTARTSYS");
288 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
291 fscache_hist(fscache_retrieval_delay_histogram, jif);
292 _leave(" = 0 [dly]");
297 * wait for an object to become active (or dead)
299 static int fscache_wait_for_retrieval_activation(struct fscache_object *object,
300 struct fscache_retrieval *op,
301 atomic_t *stat_op_waits,
302 atomic_t *stat_object_dead)
306 if (!test_bit(FSCACHE_OP_WAITING, &op->op.flags))
310 fscache_stat(stat_op_waits);
311 if (wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
312 fscache_wait_bit_interruptible,
313 TASK_INTERRUPTIBLE) < 0) {
314 ret = fscache_cancel_op(&op->op);
318 /* it's been removed from the pending queue by another party,
319 * so we should get to run shortly */
320 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
321 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
326 if (op->op.state == FSCACHE_OP_ST_CANCELLED) {
327 fscache_stat(stat_object_dead);
328 _leave(" = -ENOBUFS [cancelled]");
331 if (unlikely(fscache_object_is_dead(object))) {
332 fscache_stat(stat_object_dead);
339 * read a page from the cache or allocate a block in which to store it
341 * -ENOMEM - out of memory, nothing done
342 * -ERESTARTSYS - interrupted
343 * -ENOBUFS - no backing object available in which to cache the block
344 * -ENODATA - no data available in the backing object for this block
345 * 0 - dispatched a read - it'll call end_io_func() when finished
347 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
349 fscache_rw_complete_t end_io_func,
353 struct fscache_retrieval *op;
354 struct fscache_object *object;
357 _enter("%p,%p,,,", cookie, page);
359 fscache_stat(&fscache_n_retrievals);
361 if (hlist_empty(&cookie->backing_objects))
364 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
365 ASSERTCMP(page, !=, NULL);
367 if (fscache_wait_for_deferred_lookup(cookie) < 0)
370 op = fscache_alloc_retrieval(page->mapping, end_io_func, context);
372 _leave(" = -ENOMEM");
377 spin_lock(&cookie->lock);
379 if (hlist_empty(&cookie->backing_objects))
381 object = hlist_entry(cookie->backing_objects.first,
382 struct fscache_object, cookie_link);
384 ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP);
386 atomic_inc(&object->n_reads);
387 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
389 if (fscache_submit_op(object, &op->op) < 0)
390 goto nobufs_unlock_dec;
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 ret = fscache_wait_for_retrieval_activation(
403 __fscache_stat(&fscache_n_retrieval_op_waits),
404 __fscache_stat(&fscache_n_retrievals_object_dead));
408 /* ask the cache to honour the operation */
409 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
410 fscache_stat(&fscache_n_cop_allocate_page);
411 ret = object->cache->ops->allocate_page(op, page, gfp);
412 fscache_stat_d(&fscache_n_cop_allocate_page);
416 fscache_stat(&fscache_n_cop_read_or_alloc_page);
417 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
418 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
423 fscache_stat(&fscache_n_retrievals_nomem);
424 else if (ret == -ERESTARTSYS)
425 fscache_stat(&fscache_n_retrievals_intr);
426 else if (ret == -ENODATA)
427 fscache_stat(&fscache_n_retrievals_nodata);
429 fscache_stat(&fscache_n_retrievals_nobufs);
431 fscache_stat(&fscache_n_retrievals_ok);
433 fscache_put_retrieval(op);
434 _leave(" = %d", ret);
438 atomic_dec(&object->n_reads);
440 spin_unlock(&cookie->lock);
443 fscache_stat(&fscache_n_retrievals_nobufs);
444 _leave(" = -ENOBUFS");
447 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
450 * read a list of page from the cache or allocate a block in which to store
453 * -ENOMEM - out of memory, some pages may be being read
454 * -ERESTARTSYS - interrupted, some pages may be being read
455 * -ENOBUFS - no backing object or space available in which to cache any
456 * pages not being read
457 * -ENODATA - no data available in the backing object for some or all of
459 * 0 - dispatched a read on all pages
461 * end_io_func() will be called for each page read from the cache as it is
462 * finishes being read
464 * any pages for which a read is dispatched will be removed from pages and
467 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
468 struct address_space *mapping,
469 struct list_head *pages,
471 fscache_rw_complete_t end_io_func,
475 struct fscache_retrieval *op;
476 struct fscache_object *object;
479 _enter("%p,,%d,,,", cookie, *nr_pages);
481 fscache_stat(&fscache_n_retrievals);
483 if (hlist_empty(&cookie->backing_objects))
486 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
487 ASSERTCMP(*nr_pages, >, 0);
488 ASSERT(!list_empty(pages));
490 if (fscache_wait_for_deferred_lookup(cookie) < 0)
493 op = fscache_alloc_retrieval(mapping, end_io_func, context);
496 op->n_pages = *nr_pages;
498 spin_lock(&cookie->lock);
500 if (hlist_empty(&cookie->backing_objects))
502 object = hlist_entry(cookie->backing_objects.first,
503 struct fscache_object, cookie_link);
505 atomic_inc(&object->n_reads);
506 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
508 if (fscache_submit_op(object, &op->op) < 0)
509 goto nobufs_unlock_dec;
510 spin_unlock(&cookie->lock);
512 fscache_stat(&fscache_n_retrieval_ops);
514 /* pin the netfs read context in case we need to do the actual netfs
515 * read because we've encountered a cache read failure */
516 fscache_get_context(object->cookie, op->context);
518 /* we wait for the operation to become active, and then process it
519 * *here*, in this thread, and not in the thread pool */
520 ret = fscache_wait_for_retrieval_activation(
522 __fscache_stat(&fscache_n_retrieval_op_waits),
523 __fscache_stat(&fscache_n_retrievals_object_dead));
527 /* ask the cache to honour the operation */
528 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
529 fscache_stat(&fscache_n_cop_allocate_pages);
530 ret = object->cache->ops->allocate_pages(
531 op, pages, nr_pages, gfp);
532 fscache_stat_d(&fscache_n_cop_allocate_pages);
534 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
535 ret = object->cache->ops->read_or_alloc_pages(
536 op, pages, nr_pages, gfp);
537 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
542 fscache_stat(&fscache_n_retrievals_nomem);
543 else if (ret == -ERESTARTSYS)
544 fscache_stat(&fscache_n_retrievals_intr);
545 else if (ret == -ENODATA)
546 fscache_stat(&fscache_n_retrievals_nodata);
548 fscache_stat(&fscache_n_retrievals_nobufs);
550 fscache_stat(&fscache_n_retrievals_ok);
552 fscache_put_retrieval(op);
553 _leave(" = %d", ret);
557 atomic_dec(&object->n_reads);
559 spin_unlock(&cookie->lock);
562 fscache_stat(&fscache_n_retrievals_nobufs);
563 _leave(" = -ENOBUFS");
566 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
569 * allocate a block in the cache on which to store a page
571 * -ENOMEM - out of memory, nothing done
572 * -ERESTARTSYS - interrupted
573 * -ENOBUFS - no backing object available in which to cache the block
574 * 0 - block allocated
576 int __fscache_alloc_page(struct fscache_cookie *cookie,
580 struct fscache_retrieval *op;
581 struct fscache_object *object;
584 _enter("%p,%p,,,", cookie, page);
586 fscache_stat(&fscache_n_allocs);
588 if (hlist_empty(&cookie->backing_objects))
591 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
592 ASSERTCMP(page, !=, NULL);
594 if (fscache_wait_for_deferred_lookup(cookie) < 0)
597 op = fscache_alloc_retrieval(page->mapping, NULL, NULL);
602 spin_lock(&cookie->lock);
604 if (hlist_empty(&cookie->backing_objects))
606 object = hlist_entry(cookie->backing_objects.first,
607 struct fscache_object, cookie_link);
609 if (fscache_submit_op(object, &op->op) < 0)
611 spin_unlock(&cookie->lock);
613 fscache_stat(&fscache_n_alloc_ops);
615 ret = fscache_wait_for_retrieval_activation(
617 __fscache_stat(&fscache_n_alloc_op_waits),
618 __fscache_stat(&fscache_n_allocs_object_dead));
622 /* ask the cache to honour the operation */
623 fscache_stat(&fscache_n_cop_allocate_page);
624 ret = object->cache->ops->allocate_page(op, page, gfp);
625 fscache_stat_d(&fscache_n_cop_allocate_page);
628 if (ret == -ERESTARTSYS)
629 fscache_stat(&fscache_n_allocs_intr);
631 fscache_stat(&fscache_n_allocs_nobufs);
633 fscache_stat(&fscache_n_allocs_ok);
635 fscache_put_retrieval(op);
636 _leave(" = %d", ret);
640 spin_unlock(&cookie->lock);
643 fscache_stat(&fscache_n_allocs_nobufs);
644 _leave(" = -ENOBUFS");
647 EXPORT_SYMBOL(__fscache_alloc_page);
650 * release a write op reference
652 static void fscache_release_write_op(struct fscache_operation *_op)
654 _enter("{OP%x}", _op->debug_id);
658 * perform the background storage of a page into the cache
660 static void fscache_write_op(struct fscache_operation *_op)
662 struct fscache_storage *op =
663 container_of(_op, struct fscache_storage, op);
664 struct fscache_object *object = op->op.object;
665 struct fscache_cookie *cookie;
671 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
673 spin_lock(&object->lock);
674 cookie = object->cookie;
676 if (!fscache_object_is_active(object) || !cookie) {
677 spin_unlock(&object->lock);
682 spin_lock(&cookie->stores_lock);
684 fscache_stat(&fscache_n_store_calls);
686 /* find a page to store */
688 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
689 FSCACHE_COOKIE_PENDING_TAG);
693 _debug("gang %d [%lx]", n, page->index);
694 if (page->index > op->store_limit) {
695 fscache_stat(&fscache_n_store_pages_over_limit);
699 radix_tree_tag_set(&cookie->stores, page->index,
700 FSCACHE_COOKIE_STORING_TAG);
701 radix_tree_tag_clear(&cookie->stores, page->index,
702 FSCACHE_COOKIE_PENDING_TAG);
704 spin_unlock(&cookie->stores_lock);
705 spin_unlock(&object->lock);
707 fscache_stat(&fscache_n_store_pages);
708 fscache_stat(&fscache_n_cop_write_page);
709 ret = object->cache->ops->write_page(op, page);
710 fscache_stat_d(&fscache_n_cop_write_page);
711 fscache_end_page_write(object, page);
713 fscache_abort_object(object);
714 fscache_op_complete(&op->op);
716 fscache_enqueue_operation(&op->op);
723 /* this writer is going away and there aren't any more things to
726 spin_unlock(&cookie->stores_lock);
727 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
728 spin_unlock(&object->lock);
729 fscache_op_complete(&op->op);
734 * request a page be stored in the cache
736 * -ENOMEM - out of memory, nothing done
737 * -ENOBUFS - no backing object available in which to cache the page
738 * 0 - dispatched a write - it'll call end_io_func() when finished
740 * if the cookie still has a backing object at this point, that object can be
741 * in one of a few states with respect to storage processing:
743 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
746 * (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
749 * (b) writes deferred till post-creation (mark page for writing and
750 * return immediately)
752 * (2) negative lookup, object created, initial fill being made from netfs
753 * (FSCACHE_COOKIE_INITIAL_FILL is set)
755 * (a) fill point not yet reached this page (mark page for writing and
758 * (b) fill point passed this page (queue op to store this page)
760 * (3) object extant (queue op to store this page)
762 * any other state is invalid
764 int __fscache_write_page(struct fscache_cookie *cookie,
768 struct fscache_storage *op;
769 struct fscache_object *object;
772 _enter("%p,%x,", cookie, (u32) page->flags);
774 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
775 ASSERT(PageFsCache(page));
777 fscache_stat(&fscache_n_stores);
779 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
783 fscache_operation_init(&op->op, fscache_write_op,
784 fscache_release_write_op);
785 op->op.flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_WAITING);
787 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
792 spin_lock(&cookie->lock);
794 if (hlist_empty(&cookie->backing_objects))
796 object = hlist_entry(cookie->backing_objects.first,
797 struct fscache_object, cookie_link);
798 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
801 /* add the page to the pending-storage radix tree on the backing
803 spin_lock(&object->lock);
804 spin_lock(&cookie->stores_lock);
806 _debug("store limit %llx", (unsigned long long) object->store_limit);
808 ret = radix_tree_insert(&cookie->stores, page->index, page);
812 _debug("insert failed %d", ret);
813 goto nobufs_unlock_obj;
816 radix_tree_tag_set(&cookie->stores, page->index,
817 FSCACHE_COOKIE_PENDING_TAG);
818 page_cache_get(page);
820 /* we only want one writer at a time, but we do need to queue new
821 * writers after exclusive ops */
822 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
823 goto already_pending;
825 spin_unlock(&cookie->stores_lock);
826 spin_unlock(&object->lock);
828 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
829 op->store_limit = object->store_limit;
831 if (fscache_submit_op(object, &op->op) < 0)
834 spin_unlock(&cookie->lock);
835 radix_tree_preload_end();
836 fscache_stat(&fscache_n_store_ops);
837 fscache_stat(&fscache_n_stores_ok);
839 /* the work queue now carries its own ref on the object */
840 fscache_put_operation(&op->op);
845 fscache_stat(&fscache_n_stores_again);
847 spin_unlock(&cookie->stores_lock);
848 spin_unlock(&object->lock);
849 spin_unlock(&cookie->lock);
850 radix_tree_preload_end();
852 fscache_stat(&fscache_n_stores_ok);
857 spin_lock(&cookie->stores_lock);
858 radix_tree_delete(&cookie->stores, page->index);
859 spin_unlock(&cookie->stores_lock);
860 page_cache_release(page);
865 spin_unlock(&cookie->stores_lock);
866 spin_unlock(&object->lock);
868 spin_unlock(&cookie->lock);
869 radix_tree_preload_end();
871 fscache_stat(&fscache_n_stores_nobufs);
872 _leave(" = -ENOBUFS");
878 fscache_stat(&fscache_n_stores_oom);
879 _leave(" = -ENOMEM");
882 EXPORT_SYMBOL(__fscache_write_page);
885 * remove a page from the cache
887 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
889 struct fscache_object *object;
893 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
894 ASSERTCMP(page, !=, NULL);
896 fscache_stat(&fscache_n_uncaches);
898 /* cache withdrawal may beat us to it */
899 if (!PageFsCache(page))
903 spin_lock(&cookie->lock);
905 if (hlist_empty(&cookie->backing_objects)) {
906 ClearPageFsCache(page);
910 object = hlist_entry(cookie->backing_objects.first,
911 struct fscache_object, cookie_link);
913 /* there might now be stuff on disk we could read */
914 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
916 /* only invoke the cache backend if we managed to mark the page
917 * uncached here; this deals with synchronisation vs withdrawal */
918 if (TestClearPageFsCache(page) &&
919 object->cache->ops->uncache_page) {
920 /* the cache backend releases the cookie lock */
921 fscache_stat(&fscache_n_cop_uncache_page);
922 object->cache->ops->uncache_page(object, page);
923 fscache_stat_d(&fscache_n_cop_uncache_page);
928 spin_unlock(&cookie->lock);
932 EXPORT_SYMBOL(__fscache_uncache_page);
935 * fscache_mark_page_cached - Mark a page as being cached
936 * @op: The retrieval op pages are being marked for
937 * @page: The page to be marked
939 * Mark a netfs page as being cached. After this is called, the netfs
940 * must call fscache_uncache_page() to remove the mark.
942 void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
944 struct fscache_cookie *cookie = op->op.object->cookie;
946 #ifdef CONFIG_FSCACHE_STATS
947 atomic_inc(&fscache_n_marks);
950 _debug("- mark %p{%lx}", page, page->index);
951 if (TestSetPageFsCache(page)) {
952 static bool once_only;
955 printk(KERN_WARNING "FS-Cache:"
956 " Cookie type %s marked page %lx"
958 cookie->def->name, page->index);
962 if (cookie->def->mark_page_cached)
963 cookie->def->mark_page_cached(cookie->netfs_data,
966 EXPORT_SYMBOL(fscache_mark_page_cached);
969 * fscache_mark_pages_cached - Mark pages as being cached
970 * @op: The retrieval op pages are being marked for
971 * @pagevec: The pages to be marked
973 * Mark a bunch of netfs pages as being cached. After this is called,
974 * the netfs must call fscache_uncache_page() to remove the mark.
976 void fscache_mark_pages_cached(struct fscache_retrieval *op,
977 struct pagevec *pagevec)
981 for (loop = 0; loop < pagevec->nr; loop++)
982 fscache_mark_page_cached(op, pagevec->pages[loop]);
984 pagevec_reinit(pagevec);
986 EXPORT_SYMBOL(fscache_mark_pages_cached);
989 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
990 * to be associated with the given cookie.
992 void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
995 struct address_space *mapping = inode->i_mapping;
1000 _enter("%p,%p", cookie, inode);
1002 if (!mapping || mapping->nrpages == 0) {
1003 _leave(" [no pages]");
1007 pagevec_init(&pvec, 0);
1010 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1012 for (i = 0; i < pagevec_count(&pvec); i++) {
1013 struct page *page = pvec.pages[i];
1015 if (PageFsCache(page)) {
1016 __fscache_wait_on_page_write(cookie, page);
1017 __fscache_uncache_page(cookie, page);
1020 pagevec_release(&pvec);
1026 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);