1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Cache page management and data I/O routines
4 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define FSCACHE_DEBUG_LEVEL PAGE
9 #include <linux/module.h>
10 #include <linux/fscache-cache.h>
11 #include <linux/buffer_head.h>
12 #include <linux/pagevec.h>
13 #include <linux/slab.h>
17 * check to see if a page is being written to the cache
19 bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
24 val = radix_tree_lookup(&cookie->stores, page->index);
26 trace_fscache_check_page(cookie, page, val, 0);
30 EXPORT_SYMBOL(__fscache_check_page_write);
33 * wait for a page to finish being written to the cache
35 void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
37 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
39 trace_fscache_page(cookie, page, fscache_page_write_wait);
41 wait_event(*wq, !__fscache_check_page_write(cookie, page));
43 EXPORT_SYMBOL(__fscache_wait_on_page_write);
46 * wait for a page to finish being written to the cache. Put a timeout here
47 * since we might be called recursively via parent fs.
50 bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
52 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
54 return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
59 * decide whether a page can be released, possibly by cancelling a store to it
60 * - we're allowed to sleep if __GFP_DIRECT_RECLAIM is flagged
62 bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
69 _enter("%p,%p,%x", cookie, page, gfp);
71 trace_fscache_page(cookie, page, fscache_page_maybe_release);
75 val = radix_tree_lookup(&cookie->stores, page->index);
78 fscache_stat(&fscache_n_store_vmscan_not_storing);
79 __fscache_uncache_page(cookie, page);
83 /* see if the page is actually undergoing storage - if so we can't get
84 * rid of it till the cache has finished with it */
85 if (radix_tree_tag_get(&cookie->stores, page->index,
86 FSCACHE_COOKIE_STORING_TAG)) {
91 /* the page is pending storage, so we attempt to cancel the store and
92 * discard the store request so that the page can be reclaimed */
93 spin_lock(&cookie->stores_lock);
96 if (radix_tree_tag_get(&cookie->stores, page->index,
97 FSCACHE_COOKIE_STORING_TAG)) {
98 /* the page started to undergo storage whilst we were looking,
99 * so now we can only wait or return */
100 spin_unlock(&cookie->stores_lock);
104 xpage = radix_tree_delete(&cookie->stores, page->index);
105 trace_fscache_page(cookie, page, fscache_page_radix_delete);
106 spin_unlock(&cookie->stores_lock);
109 fscache_stat(&fscache_n_store_vmscan_cancelled);
110 fscache_stat(&fscache_n_store_radix_deletes);
111 ASSERTCMP(xpage, ==, page);
113 fscache_stat(&fscache_n_store_vmscan_gone);
116 wake_up_bit(&cookie->flags, 0);
117 trace_fscache_wake_cookie(cookie);
120 __fscache_uncache_page(cookie, page);
124 /* We will wait here if we're allowed to, but that could deadlock the
125 * allocator as the work threads writing to the cache may all end up
126 * sleeping on memory allocation, so we may need to impose a timeout
128 if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS)) {
129 fscache_stat(&fscache_n_store_vmscan_busy);
133 fscache_stat(&fscache_n_store_vmscan_wait);
134 if (!release_page_wait_timeout(cookie, page))
135 _debug("fscache writeout timeout page: %p{%lx}",
138 gfp &= ~__GFP_DIRECT_RECLAIM;
141 EXPORT_SYMBOL(__fscache_maybe_release_page);
144 * note that a page has finished being written to the cache
146 static void fscache_end_page_write(struct fscache_object *object,
149 struct fscache_cookie *cookie;
150 struct page *xpage = NULL, *val;
152 spin_lock(&object->lock);
153 cookie = object->cookie;
155 /* delete the page from the tree if it is now no longer
157 spin_lock(&cookie->stores_lock);
158 radix_tree_tag_clear(&cookie->stores, page->index,
159 FSCACHE_COOKIE_STORING_TAG);
160 trace_fscache_page(cookie, page, fscache_page_radix_clear_store);
161 if (!radix_tree_tag_get(&cookie->stores, page->index,
162 FSCACHE_COOKIE_PENDING_TAG)) {
163 fscache_stat(&fscache_n_store_radix_deletes);
164 xpage = radix_tree_delete(&cookie->stores, page->index);
165 trace_fscache_page(cookie, page, fscache_page_radix_delete);
166 trace_fscache_page(cookie, page, fscache_page_write_end);
168 val = radix_tree_lookup(&cookie->stores, page->index);
169 trace_fscache_check_page(cookie, page, val, 1);
171 trace_fscache_page(cookie, page, fscache_page_write_end_pend);
173 spin_unlock(&cookie->stores_lock);
174 wake_up_bit(&cookie->flags, 0);
175 trace_fscache_wake_cookie(cookie);
177 trace_fscache_page(cookie, page, fscache_page_write_end_noc);
179 spin_unlock(&object->lock);
185 * actually apply the changed attributes to a cache object
187 static void fscache_attr_changed_op(struct fscache_operation *op)
189 struct fscache_object *object = op->object;
192 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
194 fscache_stat(&fscache_n_attr_changed_calls);
196 if (fscache_object_is_active(object)) {
197 fscache_stat(&fscache_n_cop_attr_changed);
198 ret = object->cache->ops->attr_changed(object);
199 fscache_stat_d(&fscache_n_cop_attr_changed);
201 fscache_abort_object(object);
202 fscache_op_complete(op, ret < 0);
204 fscache_op_complete(op, true);
211 * notification that the attributes on an object have changed
213 int __fscache_attr_changed(struct fscache_cookie *cookie)
215 struct fscache_operation *op;
216 struct fscache_object *object;
217 bool wake_cookie = false;
219 _enter("%p", cookie);
221 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
223 fscache_stat(&fscache_n_attr_changed);
225 op = kzalloc(sizeof(*op), GFP_KERNEL);
227 fscache_stat(&fscache_n_attr_changed_nomem);
228 _leave(" = -ENOMEM");
232 fscache_operation_init(cookie, op, fscache_attr_changed_op, NULL, NULL);
233 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_attr_changed);
234 op->flags = FSCACHE_OP_ASYNC |
235 (1 << FSCACHE_OP_EXCLUSIVE) |
236 (1 << FSCACHE_OP_UNUSE_COOKIE);
238 spin_lock(&cookie->lock);
240 if (!fscache_cookie_enabled(cookie) ||
241 hlist_empty(&cookie->backing_objects))
243 object = hlist_entry(cookie->backing_objects.first,
244 struct fscache_object, cookie_link);
246 __fscache_use_cookie(cookie);
247 if (fscache_submit_exclusive_op(object, op) < 0)
249 spin_unlock(&cookie->lock);
250 fscache_stat(&fscache_n_attr_changed_ok);
251 fscache_put_operation(op);
256 wake_cookie = __fscache_unuse_cookie(cookie);
258 spin_unlock(&cookie->lock);
259 fscache_put_operation(op);
261 __fscache_wake_unused_cookie(cookie);
262 fscache_stat(&fscache_n_attr_changed_nobufs);
263 _leave(" = %d", -ENOBUFS);
266 EXPORT_SYMBOL(__fscache_attr_changed);
269 * Handle cancellation of a pending retrieval op
271 static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
273 struct fscache_retrieval *op =
274 container_of(_op, struct fscache_retrieval, op);
276 atomic_set(&op->n_pages, 0);
280 * release a retrieval op reference
282 static void fscache_release_retrieval_op(struct fscache_operation *_op)
284 struct fscache_retrieval *op =
285 container_of(_op, struct fscache_retrieval, op);
287 _enter("{OP%x}", op->op.debug_id);
289 ASSERTIFCMP(op->op.state != FSCACHE_OP_ST_INITIALISED,
290 atomic_read(&op->n_pages), ==, 0);
292 fscache_hist(fscache_retrieval_histogram, op->start_time);
294 fscache_put_context(op->cookie, op->context);
300 * allocate a retrieval op
302 struct fscache_retrieval *fscache_alloc_retrieval(
303 struct fscache_cookie *cookie,
304 struct address_space *mapping,
305 fscache_rw_complete_t end_io_func,
308 struct fscache_retrieval *op;
310 /* allocate a retrieval operation and attempt to submit it */
311 op = kzalloc(sizeof(*op), GFP_NOIO);
313 fscache_stat(&fscache_n_retrievals_nomem);
317 fscache_operation_init(cookie, &op->op, NULL,
318 fscache_do_cancel_retrieval,
319 fscache_release_retrieval_op);
320 op->op.flags = FSCACHE_OP_MYTHREAD |
321 (1UL << FSCACHE_OP_WAITING) |
322 (1UL << FSCACHE_OP_UNUSE_COOKIE);
324 op->mapping = mapping;
325 op->end_io_func = end_io_func;
326 op->context = context;
327 op->start_time = jiffies;
328 INIT_LIST_HEAD(&op->to_do);
330 /* Pin the netfs read context in case we need to do the actual netfs
331 * read because we've encountered a cache read failure.
334 fscache_get_context(op->cookie, context);
339 * wait for a deferred lookup to complete
341 int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
347 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
348 _leave(" = 0 [imm]");
352 fscache_stat(&fscache_n_retrievals_wait);
355 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
356 TASK_INTERRUPTIBLE) != 0) {
357 fscache_stat(&fscache_n_retrievals_intr);
358 _leave(" = -ERESTARTSYS");
362 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
365 fscache_hist(fscache_retrieval_delay_histogram, jif);
366 _leave(" = 0 [dly]");
371 * wait for an object to become active (or dead)
373 int fscache_wait_for_operation_activation(struct fscache_object *object,
374 struct fscache_operation *op,
375 atomic_t *stat_op_waits,
376 atomic_t *stat_object_dead)
380 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
385 fscache_stat(stat_op_waits);
386 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
387 TASK_INTERRUPTIBLE) != 0) {
388 trace_fscache_op(object->cookie, op, fscache_op_signal);
389 ret = fscache_cancel_op(op, false);
393 /* it's been removed from the pending queue by another party,
394 * so we should get to run shortly */
395 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
396 TASK_UNINTERRUPTIBLE);
401 if (op->state == FSCACHE_OP_ST_CANCELLED) {
402 if (stat_object_dead)
403 fscache_stat(stat_object_dead);
404 _leave(" = -ENOBUFS [cancelled]");
407 if (unlikely(fscache_object_is_dying(object) ||
408 fscache_cache_is_broken(object))) {
409 enum fscache_operation_state state = op->state;
410 trace_fscache_op(object->cookie, op, fscache_op_signal);
411 fscache_cancel_op(op, true);
412 if (stat_object_dead)
413 fscache_stat(stat_object_dead);
414 _leave(" = -ENOBUFS [obj dead %d]", state);
421 * read a page from the cache or allocate a block in which to store it
423 * -ENOMEM - out of memory, nothing done
424 * -ERESTARTSYS - interrupted
425 * -ENOBUFS - no backing object available in which to cache the block
426 * -ENODATA - no data available in the backing object for this block
427 * 0 - dispatched a read - it'll call end_io_func() when finished
429 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
431 fscache_rw_complete_t end_io_func,
435 struct fscache_retrieval *op;
436 struct fscache_object *object;
437 bool wake_cookie = false;
440 _enter("%p,%p,,,", cookie, page);
442 fscache_stat(&fscache_n_retrievals);
444 if (hlist_empty(&cookie->backing_objects))
447 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
448 _leave(" = -ENOBUFS [invalidating]");
452 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
453 ASSERTCMP(page, !=, NULL);
455 if (fscache_wait_for_deferred_lookup(cookie) < 0)
458 op = fscache_alloc_retrieval(cookie, page->mapping,
459 end_io_func, context);
461 _leave(" = -ENOMEM");
464 atomic_set(&op->n_pages, 1);
465 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_retr_one);
467 spin_lock(&cookie->lock);
469 if (!fscache_cookie_enabled(cookie) ||
470 hlist_empty(&cookie->backing_objects))
472 object = hlist_entry(cookie->backing_objects.first,
473 struct fscache_object, cookie_link);
475 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
477 __fscache_use_cookie(cookie);
478 atomic_inc(&object->n_reads);
479 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
481 if (fscache_submit_op(object, &op->op) < 0)
482 goto nobufs_unlock_dec;
483 spin_unlock(&cookie->lock);
485 fscache_stat(&fscache_n_retrieval_ops);
487 /* we wait for the operation to become active, and then process it
488 * *here*, in this thread, and not in the thread pool */
489 ret = fscache_wait_for_operation_activation(
491 __fscache_stat(&fscache_n_retrieval_op_waits),
492 __fscache_stat(&fscache_n_retrievals_object_dead));
496 /* ask the cache to honour the operation */
497 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
498 fscache_stat(&fscache_n_cop_allocate_page);
499 ret = object->cache->ops->allocate_page(op, page, gfp);
500 fscache_stat_d(&fscache_n_cop_allocate_page);
504 fscache_stat(&fscache_n_cop_read_or_alloc_page);
505 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
506 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
511 fscache_stat(&fscache_n_retrievals_nomem);
512 else if (ret == -ERESTARTSYS)
513 fscache_stat(&fscache_n_retrievals_intr);
514 else if (ret == -ENODATA)
515 fscache_stat(&fscache_n_retrievals_nodata);
517 fscache_stat(&fscache_n_retrievals_nobufs);
519 fscache_stat(&fscache_n_retrievals_ok);
521 fscache_put_retrieval(op);
522 _leave(" = %d", ret);
526 atomic_dec(&object->n_reads);
527 wake_cookie = __fscache_unuse_cookie(cookie);
529 spin_unlock(&cookie->lock);
531 __fscache_wake_unused_cookie(cookie);
532 fscache_put_retrieval(op);
534 fscache_stat(&fscache_n_retrievals_nobufs);
535 _leave(" = -ENOBUFS");
538 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
541 * read a list of page from the cache or allocate a block in which to store
544 * -ENOMEM - out of memory, some pages may be being read
545 * -ERESTARTSYS - interrupted, some pages may be being read
546 * -ENOBUFS - no backing object or space available in which to cache any
547 * pages not being read
548 * -ENODATA - no data available in the backing object for some or all of
550 * 0 - dispatched a read on all pages
552 * end_io_func() will be called for each page read from the cache as it is
553 * finishes being read
555 * any pages for which a read is dispatched will be removed from pages and
558 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
559 struct address_space *mapping,
560 struct list_head *pages,
562 fscache_rw_complete_t end_io_func,
566 struct fscache_retrieval *op;
567 struct fscache_object *object;
568 bool wake_cookie = false;
571 _enter("%p,,%d,,,", cookie, *nr_pages);
573 fscache_stat(&fscache_n_retrievals);
575 if (hlist_empty(&cookie->backing_objects))
578 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
579 _leave(" = -ENOBUFS [invalidating]");
583 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
584 ASSERTCMP(*nr_pages, >, 0);
585 ASSERT(!list_empty(pages));
587 if (fscache_wait_for_deferred_lookup(cookie) < 0)
590 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
593 atomic_set(&op->n_pages, *nr_pages);
594 trace_fscache_page_op(cookie, NULL, &op->op, fscache_page_op_retr_multi);
596 spin_lock(&cookie->lock);
598 if (!fscache_cookie_enabled(cookie) ||
599 hlist_empty(&cookie->backing_objects))
601 object = hlist_entry(cookie->backing_objects.first,
602 struct fscache_object, cookie_link);
604 __fscache_use_cookie(cookie);
605 atomic_inc(&object->n_reads);
606 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
608 if (fscache_submit_op(object, &op->op) < 0)
609 goto nobufs_unlock_dec;
610 spin_unlock(&cookie->lock);
612 fscache_stat(&fscache_n_retrieval_ops);
614 /* we wait for the operation to become active, and then process it
615 * *here*, in this thread, and not in the thread pool */
616 ret = fscache_wait_for_operation_activation(
618 __fscache_stat(&fscache_n_retrieval_op_waits),
619 __fscache_stat(&fscache_n_retrievals_object_dead));
623 /* ask the cache to honour the operation */
624 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
625 fscache_stat(&fscache_n_cop_allocate_pages);
626 ret = object->cache->ops->allocate_pages(
627 op, pages, nr_pages, gfp);
628 fscache_stat_d(&fscache_n_cop_allocate_pages);
630 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
631 ret = object->cache->ops->read_or_alloc_pages(
632 op, pages, nr_pages, gfp);
633 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
638 fscache_stat(&fscache_n_retrievals_nomem);
639 else if (ret == -ERESTARTSYS)
640 fscache_stat(&fscache_n_retrievals_intr);
641 else if (ret == -ENODATA)
642 fscache_stat(&fscache_n_retrievals_nodata);
644 fscache_stat(&fscache_n_retrievals_nobufs);
646 fscache_stat(&fscache_n_retrievals_ok);
648 fscache_put_retrieval(op);
649 _leave(" = %d", ret);
653 atomic_dec(&object->n_reads);
654 wake_cookie = __fscache_unuse_cookie(cookie);
656 spin_unlock(&cookie->lock);
657 fscache_put_retrieval(op);
659 __fscache_wake_unused_cookie(cookie);
661 fscache_stat(&fscache_n_retrievals_nobufs);
662 _leave(" = -ENOBUFS");
665 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
668 * allocate a block in the cache on which to store a page
670 * -ENOMEM - out of memory, nothing done
671 * -ERESTARTSYS - interrupted
672 * -ENOBUFS - no backing object available in which to cache the block
673 * 0 - block allocated
675 int __fscache_alloc_page(struct fscache_cookie *cookie,
679 struct fscache_retrieval *op;
680 struct fscache_object *object;
681 bool wake_cookie = false;
684 _enter("%p,%p,,,", cookie, page);
686 fscache_stat(&fscache_n_allocs);
688 if (hlist_empty(&cookie->backing_objects))
691 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
692 ASSERTCMP(page, !=, NULL);
694 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
695 _leave(" = -ENOBUFS [invalidating]");
699 if (fscache_wait_for_deferred_lookup(cookie) < 0)
702 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
705 atomic_set(&op->n_pages, 1);
706 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_alloc_one);
708 spin_lock(&cookie->lock);
710 if (!fscache_cookie_enabled(cookie) ||
711 hlist_empty(&cookie->backing_objects))
713 object = hlist_entry(cookie->backing_objects.first,
714 struct fscache_object, cookie_link);
716 __fscache_use_cookie(cookie);
717 if (fscache_submit_op(object, &op->op) < 0)
718 goto nobufs_unlock_dec;
719 spin_unlock(&cookie->lock);
721 fscache_stat(&fscache_n_alloc_ops);
723 ret = fscache_wait_for_operation_activation(
725 __fscache_stat(&fscache_n_alloc_op_waits),
726 __fscache_stat(&fscache_n_allocs_object_dead));
730 /* ask the cache to honour the operation */
731 fscache_stat(&fscache_n_cop_allocate_page);
732 ret = object->cache->ops->allocate_page(op, page, gfp);
733 fscache_stat_d(&fscache_n_cop_allocate_page);
736 if (ret == -ERESTARTSYS)
737 fscache_stat(&fscache_n_allocs_intr);
739 fscache_stat(&fscache_n_allocs_nobufs);
741 fscache_stat(&fscache_n_allocs_ok);
743 fscache_put_retrieval(op);
744 _leave(" = %d", ret);
748 wake_cookie = __fscache_unuse_cookie(cookie);
750 spin_unlock(&cookie->lock);
751 fscache_put_retrieval(op);
753 __fscache_wake_unused_cookie(cookie);
755 fscache_stat(&fscache_n_allocs_nobufs);
756 _leave(" = -ENOBUFS");
759 EXPORT_SYMBOL(__fscache_alloc_page);
762 * Unmark pages allocate in the readahead code path (via:
763 * fscache_readpages_or_alloc) after delegating to the base filesystem
765 void __fscache_readpages_cancel(struct fscache_cookie *cookie,
766 struct list_head *pages)
770 list_for_each_entry(page, pages, lru) {
771 if (PageFsCache(page))
772 __fscache_uncache_page(cookie, page);
775 EXPORT_SYMBOL(__fscache_readpages_cancel);
778 * release a write op reference
780 static void fscache_release_write_op(struct fscache_operation *_op)
782 _enter("{OP%x}", _op->debug_id);
786 * perform the background storage of a page into the cache
788 static void fscache_write_op(struct fscache_operation *_op)
790 struct fscache_storage *op =
791 container_of(_op, struct fscache_storage, op);
792 struct fscache_object *object = op->op.object;
793 struct fscache_cookie *cookie;
799 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
802 spin_lock(&object->lock);
803 cookie = object->cookie;
805 if (!fscache_object_is_active(object)) {
806 /* If we get here, then the on-disk cache object likely no
807 * longer exists, so we should just cancel this write
810 spin_unlock(&object->lock);
811 fscache_op_complete(&op->op, true);
812 _leave(" [inactive]");
817 /* If we get here, then the cookie belonging to the object was
818 * detached, probably by the cookie being withdrawn due to
819 * memory pressure, which means that the pages we might write
820 * to the cache from no longer exist - therefore, we can just
821 * cancel this write operation.
823 spin_unlock(&object->lock);
824 fscache_op_complete(&op->op, true);
825 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
826 _op->flags, _op->state, object->state->short_name,
831 spin_lock(&cookie->stores_lock);
833 fscache_stat(&fscache_n_store_calls);
835 /* find a page to store */
838 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
839 FSCACHE_COOKIE_PENDING_TAG);
840 trace_fscache_gang_lookup(cookie, &op->op, results, n, op->store_limit);
844 _debug("gang %d [%lx]", n, page->index);
846 radix_tree_tag_set(&cookie->stores, page->index,
847 FSCACHE_COOKIE_STORING_TAG);
848 radix_tree_tag_clear(&cookie->stores, page->index,
849 FSCACHE_COOKIE_PENDING_TAG);
850 trace_fscache_page(cookie, page, fscache_page_radix_pend2store);
852 spin_unlock(&cookie->stores_lock);
853 spin_unlock(&object->lock);
855 if (page->index >= op->store_limit)
858 fscache_stat(&fscache_n_store_pages);
859 fscache_stat(&fscache_n_cop_write_page);
860 ret = object->cache->ops->write_page(op, page);
861 fscache_stat_d(&fscache_n_cop_write_page);
862 trace_fscache_wrote_page(cookie, page, &op->op, ret);
863 fscache_end_page_write(object, page);
865 fscache_abort_object(object);
866 fscache_op_complete(&op->op, true);
868 fscache_enqueue_operation(&op->op);
875 fscache_stat(&fscache_n_store_pages_over_limit);
876 trace_fscache_wrote_page(cookie, page, &op->op, -ENOBUFS);
877 fscache_end_page_write(object, page);
881 /* this writer is going away and there aren't any more things to
884 spin_unlock(&cookie->stores_lock);
885 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
886 spin_unlock(&object->lock);
887 fscache_op_complete(&op->op, false);
892 * Clear the pages pending writing for invalidation
894 void fscache_invalidate_writes(struct fscache_cookie *cookie)
903 spin_lock(&cookie->stores_lock);
904 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
906 FSCACHE_COOKIE_PENDING_TAG);
908 spin_unlock(&cookie->stores_lock);
912 for (i = n - 1; i >= 0; i--) {
914 radix_tree_delete(&cookie->stores, page->index);
915 trace_fscache_page(cookie, page, fscache_page_radix_delete);
916 trace_fscache_page(cookie, page, fscache_page_inval);
919 spin_unlock(&cookie->stores_lock);
921 for (i = n - 1; i >= 0; i--)
922 put_page(results[i]);
925 wake_up_bit(&cookie->flags, 0);
926 trace_fscache_wake_cookie(cookie);
932 * request a page be stored in the cache
934 * -ENOMEM - out of memory, nothing done
935 * -ENOBUFS - no backing object available in which to cache the page
936 * 0 - dispatched a write - it'll call end_io_func() when finished
938 * if the cookie still has a backing object at this point, that object can be
939 * in one of a few states with respect to storage processing:
941 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
946 * (b) writes deferred till post-creation (mark page for writing and
947 * return immediately)
949 * (2) negative lookup, object created, initial fill being made from netfs
951 * (a) fill point not yet reached this page (mark page for writing and
954 * (b) fill point passed this page (queue op to store this page)
956 * (3) object extant (queue op to store this page)
958 * any other state is invalid
960 int __fscache_write_page(struct fscache_cookie *cookie,
965 struct fscache_storage *op;
966 struct fscache_object *object;
967 bool wake_cookie = false;
970 _enter("%p,%x,", cookie, (u32) page->flags);
972 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
973 ASSERT(PageFsCache(page));
975 fscache_stat(&fscache_n_stores);
977 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
978 _leave(" = -ENOBUFS [invalidating]");
982 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
986 fscache_operation_init(cookie, &op->op, fscache_write_op, NULL,
987 fscache_release_write_op);
988 op->op.flags = FSCACHE_OP_ASYNC |
989 (1 << FSCACHE_OP_WAITING) |
990 (1 << FSCACHE_OP_UNUSE_COOKIE);
992 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
996 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_write_one);
999 spin_lock(&cookie->lock);
1001 if (!fscache_cookie_enabled(cookie) ||
1002 hlist_empty(&cookie->backing_objects))
1004 object = hlist_entry(cookie->backing_objects.first,
1005 struct fscache_object, cookie_link);
1006 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
1009 trace_fscache_page(cookie, page, fscache_page_write);
1011 /* add the page to the pending-storage radix tree on the backing
1013 spin_lock(&object->lock);
1015 if (object->store_limit_l != object_size)
1016 fscache_set_store_limit(object, object_size);
1018 spin_lock(&cookie->stores_lock);
1020 _debug("store limit %llx", (unsigned long long) object->store_limit);
1022 ret = radix_tree_insert(&cookie->stores, page->index, page);
1025 goto already_queued;
1026 _debug("insert failed %d", ret);
1027 goto nobufs_unlock_obj;
1030 trace_fscache_page(cookie, page, fscache_page_radix_insert);
1031 radix_tree_tag_set(&cookie->stores, page->index,
1032 FSCACHE_COOKIE_PENDING_TAG);
1033 trace_fscache_page(cookie, page, fscache_page_radix_set_pend);
1036 /* we only want one writer at a time, but we do need to queue new
1037 * writers after exclusive ops */
1038 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
1039 goto already_pending;
1041 spin_unlock(&cookie->stores_lock);
1042 spin_unlock(&object->lock);
1044 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
1045 op->store_limit = object->store_limit;
1047 __fscache_use_cookie(cookie);
1048 if (fscache_submit_op(object, &op->op) < 0)
1051 spin_unlock(&cookie->lock);
1052 radix_tree_preload_end();
1053 fscache_stat(&fscache_n_store_ops);
1054 fscache_stat(&fscache_n_stores_ok);
1056 /* the work queue now carries its own ref on the object */
1057 fscache_put_operation(&op->op);
1062 fscache_stat(&fscache_n_stores_again);
1064 spin_unlock(&cookie->stores_lock);
1065 spin_unlock(&object->lock);
1066 spin_unlock(&cookie->lock);
1067 radix_tree_preload_end();
1068 fscache_put_operation(&op->op);
1069 fscache_stat(&fscache_n_stores_ok);
1074 spin_lock(&cookie->stores_lock);
1075 radix_tree_delete(&cookie->stores, page->index);
1076 trace_fscache_page(cookie, page, fscache_page_radix_delete);
1077 spin_unlock(&cookie->stores_lock);
1078 wake_cookie = __fscache_unuse_cookie(cookie);
1084 spin_unlock(&cookie->stores_lock);
1085 spin_unlock(&object->lock);
1087 spin_unlock(&cookie->lock);
1088 radix_tree_preload_end();
1089 fscache_put_operation(&op->op);
1091 __fscache_wake_unused_cookie(cookie);
1092 fscache_stat(&fscache_n_stores_nobufs);
1093 _leave(" = -ENOBUFS");
1097 fscache_put_operation(&op->op);
1099 fscache_stat(&fscache_n_stores_oom);
1100 _leave(" = -ENOMEM");
1103 EXPORT_SYMBOL(__fscache_write_page);
1106 * remove a page from the cache
1108 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1110 struct fscache_object *object;
1112 _enter(",%p", page);
1114 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1115 ASSERTCMP(page, !=, NULL);
1117 fscache_stat(&fscache_n_uncaches);
1119 /* cache withdrawal may beat us to it */
1120 if (!PageFsCache(page))
1123 trace_fscache_page(cookie, page, fscache_page_uncache);
1125 /* get the object */
1126 spin_lock(&cookie->lock);
1128 if (hlist_empty(&cookie->backing_objects)) {
1129 ClearPageFsCache(page);
1133 object = hlist_entry(cookie->backing_objects.first,
1134 struct fscache_object, cookie_link);
1136 /* there might now be stuff on disk we could read */
1137 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1139 /* only invoke the cache backend if we managed to mark the page
1140 * uncached here; this deals with synchronisation vs withdrawal */
1141 if (TestClearPageFsCache(page) &&
1142 object->cache->ops->uncache_page) {
1143 /* the cache backend releases the cookie lock */
1144 fscache_stat(&fscache_n_cop_uncache_page);
1145 object->cache->ops->uncache_page(object, page);
1146 fscache_stat_d(&fscache_n_cop_uncache_page);
1151 spin_unlock(&cookie->lock);
1155 EXPORT_SYMBOL(__fscache_uncache_page);
1158 * fscache_mark_page_cached - Mark a page as being cached
1159 * @op: The retrieval op pages are being marked for
1160 * @page: The page to be marked
1162 * Mark a netfs page as being cached. After this is called, the netfs
1163 * must call fscache_uncache_page() to remove the mark.
1165 void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1167 struct fscache_cookie *cookie = op->op.object->cookie;
1169 #ifdef CONFIG_FSCACHE_STATS
1170 atomic_inc(&fscache_n_marks);
1173 trace_fscache_page(cookie, page, fscache_page_cached);
1175 _debug("- mark %p{%lx}", page, page->index);
1176 if (TestSetPageFsCache(page)) {
1177 static bool once_only;
1180 pr_warn("Cookie type %s marked page %lx multiple times\n",
1181 cookie->def->name, page->index);
1185 if (cookie->def->mark_page_cached)
1186 cookie->def->mark_page_cached(cookie->netfs_data,
1189 EXPORT_SYMBOL(fscache_mark_page_cached);
1192 * fscache_mark_pages_cached - Mark pages as being cached
1193 * @op: The retrieval op pages are being marked for
1194 * @pagevec: The pages to be marked
1196 * Mark a bunch of netfs pages as being cached. After this is called,
1197 * the netfs must call fscache_uncache_page() to remove the mark.
1199 void fscache_mark_pages_cached(struct fscache_retrieval *op,
1200 struct pagevec *pagevec)
1204 for (loop = 0; loop < pagevec->nr; loop++)
1205 fscache_mark_page_cached(op, pagevec->pages[loop]);
1207 pagevec_reinit(pagevec);
1209 EXPORT_SYMBOL(fscache_mark_pages_cached);
1212 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1213 * to be associated with the given cookie.
1215 void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1216 struct inode *inode)
1218 struct address_space *mapping = inode->i_mapping;
1219 struct pagevec pvec;
1223 _enter("%p,%p", cookie, inode);
1225 if (!mapping || mapping->nrpages == 0) {
1226 _leave(" [no pages]");
1230 pagevec_init(&pvec);
1233 if (!pagevec_lookup(&pvec, mapping, &next))
1235 for (i = 0; i < pagevec_count(&pvec); i++) {
1236 struct page *page = pvec.pages[i];
1237 if (PageFsCache(page)) {
1238 __fscache_wait_on_page_write(cookie, page);
1239 __fscache_uncache_page(cookie, page);
1242 pagevec_release(&pvec);
1248 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);