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);
293 fscache_put_context(op
->cookie
, op
->context
);
299 * allocate a retrieval op
301 struct fscache_retrieval
*fscache_alloc_retrieval(
302 struct fscache_cookie
*cookie
,
303 struct address_space
*mapping
,
304 fscache_rw_complete_t end_io_func
,
307 struct fscache_retrieval
*op
;
309 /* allocate a retrieval operation and attempt to submit it */
310 op
= kzalloc(sizeof(*op
), GFP_NOIO
);
312 fscache_stat(&fscache_n_retrievals_nomem
);
316 fscache_operation_init(cookie
, &op
->op
, NULL
,
317 fscache_do_cancel_retrieval
,
318 fscache_release_retrieval_op
);
319 op
->op
.flags
= FSCACHE_OP_MYTHREAD
|
320 (1UL << FSCACHE_OP_WAITING
) |
321 (1UL << FSCACHE_OP_UNUSE_COOKIE
);
323 op
->mapping
= mapping
;
324 op
->end_io_func
= end_io_func
;
325 op
->context
= context
;
326 INIT_LIST_HEAD(&op
->to_do
);
328 /* Pin the netfs read context in case we need to do the actual netfs
329 * read because we've encountered a cache read failure.
332 fscache_get_context(op
->cookie
, context
);
337 * wait for a deferred lookup to complete
339 int fscache_wait_for_deferred_lookup(struct fscache_cookie
*cookie
)
343 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
)) {
344 _leave(" = 0 [imm]");
348 fscache_stat(&fscache_n_retrievals_wait
);
350 if (wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
351 TASK_INTERRUPTIBLE
) != 0) {
352 fscache_stat(&fscache_n_retrievals_intr
);
353 _leave(" = -ERESTARTSYS");
357 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
));
360 _leave(" = 0 [dly]");
365 * wait for an object to become active (or dead)
367 int fscache_wait_for_operation_activation(struct fscache_object
*object
,
368 struct fscache_operation
*op
,
369 atomic_t
*stat_op_waits
,
370 atomic_t
*stat_object_dead
)
374 if (!test_bit(FSCACHE_OP_WAITING
, &op
->flags
))
379 fscache_stat(stat_op_waits
);
380 if (wait_on_bit(&op
->flags
, FSCACHE_OP_WAITING
,
381 TASK_INTERRUPTIBLE
) != 0) {
382 trace_fscache_op(object
->cookie
, op
, fscache_op_signal
);
383 ret
= fscache_cancel_op(op
, false);
387 /* it's been removed from the pending queue by another party,
388 * so we should get to run shortly */
389 wait_on_bit(&op
->flags
, FSCACHE_OP_WAITING
,
390 TASK_UNINTERRUPTIBLE
);
395 if (op
->state
== FSCACHE_OP_ST_CANCELLED
) {
396 if (stat_object_dead
)
397 fscache_stat(stat_object_dead
);
398 _leave(" = -ENOBUFS [cancelled]");
401 if (unlikely(fscache_object_is_dying(object
) ||
402 fscache_cache_is_broken(object
))) {
403 enum fscache_operation_state state
= op
->state
;
404 trace_fscache_op(object
->cookie
, op
, fscache_op_signal
);
405 fscache_cancel_op(op
, true);
406 if (stat_object_dead
)
407 fscache_stat(stat_object_dead
);
408 _leave(" = -ENOBUFS [obj dead %d]", state
);
415 * read a page from the cache or allocate a block in which to store it
417 * -ENOMEM - out of memory, nothing done
418 * -ERESTARTSYS - interrupted
419 * -ENOBUFS - no backing object available in which to cache the block
420 * -ENODATA - no data available in the backing object for this block
421 * 0 - dispatched a read - it'll call end_io_func() when finished
423 int __fscache_read_or_alloc_page(struct fscache_cookie
*cookie
,
425 fscache_rw_complete_t end_io_func
,
429 struct fscache_retrieval
*op
;
430 struct fscache_object
*object
;
431 bool wake_cookie
= false;
434 _enter("%p,%p,,,", cookie
, page
);
436 fscache_stat(&fscache_n_retrievals
);
438 if (hlist_empty(&cookie
->backing_objects
))
441 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
442 _leave(" = -ENOBUFS [invalidating]");
446 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
447 ASSERTCMP(page
, !=, NULL
);
449 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
452 op
= fscache_alloc_retrieval(cookie
, page
->mapping
,
453 end_io_func
, context
);
455 _leave(" = -ENOMEM");
458 atomic_set(&op
->n_pages
, 1);
459 trace_fscache_page_op(cookie
, page
, &op
->op
, fscache_page_op_retr_one
);
461 spin_lock(&cookie
->lock
);
463 if (!fscache_cookie_enabled(cookie
) ||
464 hlist_empty(&cookie
->backing_objects
))
466 object
= hlist_entry(cookie
->backing_objects
.first
,
467 struct fscache_object
, cookie_link
);
469 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP
, &object
->flags
));
471 __fscache_use_cookie(cookie
);
472 atomic_inc(&object
->n_reads
);
473 __set_bit(FSCACHE_OP_DEC_READ_CNT
, &op
->op
.flags
);
475 if (fscache_submit_op(object
, &op
->op
) < 0)
476 goto nobufs_unlock_dec
;
477 spin_unlock(&cookie
->lock
);
479 fscache_stat(&fscache_n_retrieval_ops
);
481 /* we wait for the operation to become active, and then process it
482 * *here*, in this thread, and not in the thread pool */
483 ret
= fscache_wait_for_operation_activation(
485 __fscache_stat(&fscache_n_retrieval_op_waits
),
486 __fscache_stat(&fscache_n_retrievals_object_dead
));
490 /* ask the cache to honour the operation */
491 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
492 fscache_stat(&fscache_n_cop_allocate_page
);
493 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
494 fscache_stat_d(&fscache_n_cop_allocate_page
);
498 fscache_stat(&fscache_n_cop_read_or_alloc_page
);
499 ret
= object
->cache
->ops
->read_or_alloc_page(op
, page
, gfp
);
500 fscache_stat_d(&fscache_n_cop_read_or_alloc_page
);
505 fscache_stat(&fscache_n_retrievals_nomem
);
506 else if (ret
== -ERESTARTSYS
)
507 fscache_stat(&fscache_n_retrievals_intr
);
508 else if (ret
== -ENODATA
)
509 fscache_stat(&fscache_n_retrievals_nodata
);
511 fscache_stat(&fscache_n_retrievals_nobufs
);
513 fscache_stat(&fscache_n_retrievals_ok
);
515 fscache_put_retrieval(op
);
516 _leave(" = %d", ret
);
520 atomic_dec(&object
->n_reads
);
521 wake_cookie
= __fscache_unuse_cookie(cookie
);
523 spin_unlock(&cookie
->lock
);
525 __fscache_wake_unused_cookie(cookie
);
526 fscache_put_retrieval(op
);
528 fscache_stat(&fscache_n_retrievals_nobufs
);
529 _leave(" = -ENOBUFS");
532 EXPORT_SYMBOL(__fscache_read_or_alloc_page
);
535 * read a list of page from the cache or allocate a block in which to store
538 * -ENOMEM - out of memory, some pages may be being read
539 * -ERESTARTSYS - interrupted, some pages may be being read
540 * -ENOBUFS - no backing object or space available in which to cache any
541 * pages not being read
542 * -ENODATA - no data available in the backing object for some or all of
544 * 0 - dispatched a read on all pages
546 * end_io_func() will be called for each page read from the cache as it is
547 * finishes being read
549 * any pages for which a read is dispatched will be removed from pages and
552 int __fscache_read_or_alloc_pages(struct fscache_cookie
*cookie
,
553 struct address_space
*mapping
,
554 struct list_head
*pages
,
556 fscache_rw_complete_t end_io_func
,
560 struct fscache_retrieval
*op
;
561 struct fscache_object
*object
;
562 bool wake_cookie
= false;
565 _enter("%p,,%d,,,", cookie
, *nr_pages
);
567 fscache_stat(&fscache_n_retrievals
);
569 if (hlist_empty(&cookie
->backing_objects
))
572 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
573 _leave(" = -ENOBUFS [invalidating]");
577 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
578 ASSERTCMP(*nr_pages
, >, 0);
579 ASSERT(!list_empty(pages
));
581 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
584 op
= fscache_alloc_retrieval(cookie
, mapping
, end_io_func
, context
);
587 atomic_set(&op
->n_pages
, *nr_pages
);
588 trace_fscache_page_op(cookie
, NULL
, &op
->op
, fscache_page_op_retr_multi
);
590 spin_lock(&cookie
->lock
);
592 if (!fscache_cookie_enabled(cookie
) ||
593 hlist_empty(&cookie
->backing_objects
))
595 object
= hlist_entry(cookie
->backing_objects
.first
,
596 struct fscache_object
, cookie_link
);
598 __fscache_use_cookie(cookie
);
599 atomic_inc(&object
->n_reads
);
600 __set_bit(FSCACHE_OP_DEC_READ_CNT
, &op
->op
.flags
);
602 if (fscache_submit_op(object
, &op
->op
) < 0)
603 goto nobufs_unlock_dec
;
604 spin_unlock(&cookie
->lock
);
606 fscache_stat(&fscache_n_retrieval_ops
);
608 /* we wait for the operation to become active, and then process it
609 * *here*, in this thread, and not in the thread pool */
610 ret
= fscache_wait_for_operation_activation(
612 __fscache_stat(&fscache_n_retrieval_op_waits
),
613 __fscache_stat(&fscache_n_retrievals_object_dead
));
617 /* ask the cache to honour the operation */
618 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
619 fscache_stat(&fscache_n_cop_allocate_pages
);
620 ret
= object
->cache
->ops
->allocate_pages(
621 op
, pages
, nr_pages
, gfp
);
622 fscache_stat_d(&fscache_n_cop_allocate_pages
);
624 fscache_stat(&fscache_n_cop_read_or_alloc_pages
);
625 ret
= object
->cache
->ops
->read_or_alloc_pages(
626 op
, pages
, nr_pages
, gfp
);
627 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages
);
632 fscache_stat(&fscache_n_retrievals_nomem
);
633 else if (ret
== -ERESTARTSYS
)
634 fscache_stat(&fscache_n_retrievals_intr
);
635 else if (ret
== -ENODATA
)
636 fscache_stat(&fscache_n_retrievals_nodata
);
638 fscache_stat(&fscache_n_retrievals_nobufs
);
640 fscache_stat(&fscache_n_retrievals_ok
);
642 fscache_put_retrieval(op
);
643 _leave(" = %d", ret
);
647 atomic_dec(&object
->n_reads
);
648 wake_cookie
= __fscache_unuse_cookie(cookie
);
650 spin_unlock(&cookie
->lock
);
651 fscache_put_retrieval(op
);
653 __fscache_wake_unused_cookie(cookie
);
655 fscache_stat(&fscache_n_retrievals_nobufs
);
656 _leave(" = -ENOBUFS");
659 EXPORT_SYMBOL(__fscache_read_or_alloc_pages
);
662 * allocate a block in the cache on which to store a page
664 * -ENOMEM - out of memory, nothing done
665 * -ERESTARTSYS - interrupted
666 * -ENOBUFS - no backing object available in which to cache the block
667 * 0 - block allocated
669 int __fscache_alloc_page(struct fscache_cookie
*cookie
,
673 struct fscache_retrieval
*op
;
674 struct fscache_object
*object
;
675 bool wake_cookie
= false;
678 _enter("%p,%p,,,", cookie
, page
);
680 fscache_stat(&fscache_n_allocs
);
682 if (hlist_empty(&cookie
->backing_objects
))
685 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
686 ASSERTCMP(page
, !=, NULL
);
688 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
689 _leave(" = -ENOBUFS [invalidating]");
693 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
696 op
= fscache_alloc_retrieval(cookie
, page
->mapping
, NULL
, NULL
);
699 atomic_set(&op
->n_pages
, 1);
700 trace_fscache_page_op(cookie
, page
, &op
->op
, fscache_page_op_alloc_one
);
702 spin_lock(&cookie
->lock
);
704 if (!fscache_cookie_enabled(cookie
) ||
705 hlist_empty(&cookie
->backing_objects
))
707 object
= hlist_entry(cookie
->backing_objects
.first
,
708 struct fscache_object
, cookie_link
);
710 __fscache_use_cookie(cookie
);
711 if (fscache_submit_op(object
, &op
->op
) < 0)
712 goto nobufs_unlock_dec
;
713 spin_unlock(&cookie
->lock
);
715 fscache_stat(&fscache_n_alloc_ops
);
717 ret
= fscache_wait_for_operation_activation(
719 __fscache_stat(&fscache_n_alloc_op_waits
),
720 __fscache_stat(&fscache_n_allocs_object_dead
));
724 /* ask the cache to honour the operation */
725 fscache_stat(&fscache_n_cop_allocate_page
);
726 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
727 fscache_stat_d(&fscache_n_cop_allocate_page
);
730 if (ret
== -ERESTARTSYS
)
731 fscache_stat(&fscache_n_allocs_intr
);
733 fscache_stat(&fscache_n_allocs_nobufs
);
735 fscache_stat(&fscache_n_allocs_ok
);
737 fscache_put_retrieval(op
);
738 _leave(" = %d", ret
);
742 wake_cookie
= __fscache_unuse_cookie(cookie
);
744 spin_unlock(&cookie
->lock
);
745 fscache_put_retrieval(op
);
747 __fscache_wake_unused_cookie(cookie
);
749 fscache_stat(&fscache_n_allocs_nobufs
);
750 _leave(" = -ENOBUFS");
753 EXPORT_SYMBOL(__fscache_alloc_page
);
756 * Unmark pages allocate in the readahead code path (via:
757 * fscache_readpages_or_alloc) after delegating to the base filesystem
759 void __fscache_readpages_cancel(struct fscache_cookie
*cookie
,
760 struct list_head
*pages
)
764 list_for_each_entry(page
, pages
, lru
) {
765 if (PageFsCache(page
))
766 __fscache_uncache_page(cookie
, page
);
769 EXPORT_SYMBOL(__fscache_readpages_cancel
);
772 * release a write op reference
774 static void fscache_release_write_op(struct fscache_operation
*_op
)
776 _enter("{OP%x}", _op
->debug_id
);
780 * perform the background storage of a page into the cache
782 static void fscache_write_op(struct fscache_operation
*_op
)
784 struct fscache_storage
*op
=
785 container_of(_op
, struct fscache_storage
, op
);
786 struct fscache_object
*object
= op
->op
.object
;
787 struct fscache_cookie
*cookie
;
793 _enter("{OP%x,%d}", op
->op
.debug_id
, atomic_read(&op
->op
.usage
));
796 spin_lock(&object
->lock
);
797 cookie
= object
->cookie
;
799 if (!fscache_object_is_active(object
)) {
800 /* If we get here, then the on-disk cache object likely no
801 * longer exists, so we should just cancel this write
804 spin_unlock(&object
->lock
);
805 fscache_op_complete(&op
->op
, true);
806 _leave(" [inactive]");
811 /* If we get here, then the cookie belonging to the object was
812 * detached, probably by the cookie being withdrawn due to
813 * memory pressure, which means that the pages we might write
814 * to the cache from no longer exist - therefore, we can just
815 * cancel this write operation.
817 spin_unlock(&object
->lock
);
818 fscache_op_complete(&op
->op
, true);
819 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
820 _op
->flags
, _op
->state
, object
->state
->short_name
,
825 spin_lock(&cookie
->stores_lock
);
827 fscache_stat(&fscache_n_store_calls
);
829 /* find a page to store */
832 n
= radix_tree_gang_lookup_tag(&cookie
->stores
, results
, 0, 1,
833 FSCACHE_COOKIE_PENDING_TAG
);
834 trace_fscache_gang_lookup(cookie
, &op
->op
, results
, n
, op
->store_limit
);
838 _debug("gang %d [%lx]", n
, page
->index
);
840 radix_tree_tag_set(&cookie
->stores
, page
->index
,
841 FSCACHE_COOKIE_STORING_TAG
);
842 radix_tree_tag_clear(&cookie
->stores
, page
->index
,
843 FSCACHE_COOKIE_PENDING_TAG
);
844 trace_fscache_page(cookie
, page
, fscache_page_radix_pend2store
);
846 spin_unlock(&cookie
->stores_lock
);
847 spin_unlock(&object
->lock
);
849 if (page
->index
>= op
->store_limit
)
852 fscache_stat(&fscache_n_store_pages
);
853 fscache_stat(&fscache_n_cop_write_page
);
854 ret
= object
->cache
->ops
->write_page(op
, page
);
855 fscache_stat_d(&fscache_n_cop_write_page
);
856 trace_fscache_wrote_page(cookie
, page
, &op
->op
, ret
);
857 fscache_end_page_write(object
, page
);
859 fscache_abort_object(object
);
860 fscache_op_complete(&op
->op
, true);
862 fscache_enqueue_operation(&op
->op
);
869 fscache_stat(&fscache_n_store_pages_over_limit
);
870 trace_fscache_wrote_page(cookie
, page
, &op
->op
, -ENOBUFS
);
871 fscache_end_page_write(object
, page
);
875 /* this writer is going away and there aren't any more things to
878 spin_unlock(&cookie
->stores_lock
);
879 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
880 spin_unlock(&object
->lock
);
881 fscache_op_complete(&op
->op
, false);
886 * Clear the pages pending writing for invalidation
888 void fscache_invalidate_writes(struct fscache_cookie
*cookie
)
897 spin_lock(&cookie
->stores_lock
);
898 n
= radix_tree_gang_lookup_tag(&cookie
->stores
, results
, 0,
900 FSCACHE_COOKIE_PENDING_TAG
);
902 spin_unlock(&cookie
->stores_lock
);
906 for (i
= n
- 1; i
>= 0; i
--) {
908 radix_tree_delete(&cookie
->stores
, page
->index
);
909 trace_fscache_page(cookie
, page
, fscache_page_radix_delete
);
910 trace_fscache_page(cookie
, page
, fscache_page_inval
);
913 spin_unlock(&cookie
->stores_lock
);
915 for (i
= n
- 1; i
>= 0; i
--)
916 put_page(results
[i
]);
919 wake_up_bit(&cookie
->flags
, 0);
920 trace_fscache_wake_cookie(cookie
);
926 * request a page be stored in the cache
928 * -ENOMEM - out of memory, nothing done
929 * -ENOBUFS - no backing object available in which to cache the page
930 * 0 - dispatched a write - it'll call end_io_func() when finished
932 * if the cookie still has a backing object at this point, that object can be
933 * in one of a few states with respect to storage processing:
935 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
940 * (b) writes deferred till post-creation (mark page for writing and
941 * return immediately)
943 * (2) negative lookup, object created, initial fill being made from netfs
945 * (a) fill point not yet reached this page (mark page for writing and
948 * (b) fill point passed this page (queue op to store this page)
950 * (3) object extant (queue op to store this page)
952 * any other state is invalid
954 int __fscache_write_page(struct fscache_cookie
*cookie
,
959 struct fscache_storage
*op
;
960 struct fscache_object
*object
;
961 bool wake_cookie
= false;
964 _enter("%p,%x,", cookie
, (u32
) page
->flags
);
966 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
967 ASSERT(PageFsCache(page
));
969 fscache_stat(&fscache_n_stores
);
971 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
972 _leave(" = -ENOBUFS [invalidating]");
976 op
= kzalloc(sizeof(*op
), GFP_NOIO
| __GFP_NOMEMALLOC
| __GFP_NORETRY
);
980 fscache_operation_init(cookie
, &op
->op
, fscache_write_op
, NULL
,
981 fscache_release_write_op
);
982 op
->op
.flags
= FSCACHE_OP_ASYNC
|
983 (1 << FSCACHE_OP_WAITING
) |
984 (1 << FSCACHE_OP_UNUSE_COOKIE
);
986 ret
= radix_tree_maybe_preload(gfp
& ~__GFP_HIGHMEM
);
990 trace_fscache_page_op(cookie
, page
, &op
->op
, fscache_page_op_write_one
);
993 spin_lock(&cookie
->lock
);
995 if (!fscache_cookie_enabled(cookie
) ||
996 hlist_empty(&cookie
->backing_objects
))
998 object
= hlist_entry(cookie
->backing_objects
.first
,
999 struct fscache_object
, cookie_link
);
1000 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
1003 trace_fscache_page(cookie
, page
, fscache_page_write
);
1005 /* add the page to the pending-storage radix tree on the backing
1007 spin_lock(&object
->lock
);
1009 if (object
->store_limit_l
!= object_size
)
1010 fscache_set_store_limit(object
, object_size
);
1012 spin_lock(&cookie
->stores_lock
);
1014 _debug("store limit %llx", (unsigned long long) object
->store_limit
);
1016 ret
= radix_tree_insert(&cookie
->stores
, page
->index
, page
);
1019 goto already_queued
;
1020 _debug("insert failed %d", ret
);
1021 goto nobufs_unlock_obj
;
1024 trace_fscache_page(cookie
, page
, fscache_page_radix_insert
);
1025 radix_tree_tag_set(&cookie
->stores
, page
->index
,
1026 FSCACHE_COOKIE_PENDING_TAG
);
1027 trace_fscache_page(cookie
, page
, fscache_page_radix_set_pend
);
1030 /* we only want one writer at a time, but we do need to queue new
1031 * writers after exclusive ops */
1032 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
))
1033 goto already_pending
;
1035 spin_unlock(&cookie
->stores_lock
);
1036 spin_unlock(&object
->lock
);
1038 op
->op
.debug_id
= atomic_inc_return(&fscache_op_debug_id
);
1039 op
->store_limit
= object
->store_limit
;
1041 __fscache_use_cookie(cookie
);
1042 if (fscache_submit_op(object
, &op
->op
) < 0)
1045 spin_unlock(&cookie
->lock
);
1046 radix_tree_preload_end();
1047 fscache_stat(&fscache_n_store_ops
);
1048 fscache_stat(&fscache_n_stores_ok
);
1050 /* the work queue now carries its own ref on the object */
1051 fscache_put_operation(&op
->op
);
1056 fscache_stat(&fscache_n_stores_again
);
1058 spin_unlock(&cookie
->stores_lock
);
1059 spin_unlock(&object
->lock
);
1060 spin_unlock(&cookie
->lock
);
1061 radix_tree_preload_end();
1062 fscache_put_operation(&op
->op
);
1063 fscache_stat(&fscache_n_stores_ok
);
1068 spin_lock(&cookie
->stores_lock
);
1069 radix_tree_delete(&cookie
->stores
, page
->index
);
1070 trace_fscache_page(cookie
, page
, fscache_page_radix_delete
);
1071 spin_unlock(&cookie
->stores_lock
);
1072 wake_cookie
= __fscache_unuse_cookie(cookie
);
1078 spin_unlock(&cookie
->stores_lock
);
1079 spin_unlock(&object
->lock
);
1081 spin_unlock(&cookie
->lock
);
1082 radix_tree_preload_end();
1083 fscache_put_operation(&op
->op
);
1085 __fscache_wake_unused_cookie(cookie
);
1086 fscache_stat(&fscache_n_stores_nobufs
);
1087 _leave(" = -ENOBUFS");
1091 fscache_put_operation(&op
->op
);
1093 fscache_stat(&fscache_n_stores_oom
);
1094 _leave(" = -ENOMEM");
1097 EXPORT_SYMBOL(__fscache_write_page
);
1100 * remove a page from the cache
1102 void __fscache_uncache_page(struct fscache_cookie
*cookie
, struct page
*page
)
1104 struct fscache_object
*object
;
1106 _enter(",%p", page
);
1108 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
1109 ASSERTCMP(page
, !=, NULL
);
1111 fscache_stat(&fscache_n_uncaches
);
1113 /* cache withdrawal may beat us to it */
1114 if (!PageFsCache(page
))
1117 trace_fscache_page(cookie
, page
, fscache_page_uncache
);
1119 /* get the object */
1120 spin_lock(&cookie
->lock
);
1122 if (hlist_empty(&cookie
->backing_objects
)) {
1123 ClearPageFsCache(page
);
1127 object
= hlist_entry(cookie
->backing_objects
.first
,
1128 struct fscache_object
, cookie_link
);
1130 /* there might now be stuff on disk we could read */
1131 clear_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
1133 /* only invoke the cache backend if we managed to mark the page
1134 * uncached here; this deals with synchronisation vs withdrawal */
1135 if (TestClearPageFsCache(page
) &&
1136 object
->cache
->ops
->uncache_page
) {
1137 /* the cache backend releases the cookie lock */
1138 fscache_stat(&fscache_n_cop_uncache_page
);
1139 object
->cache
->ops
->uncache_page(object
, page
);
1140 fscache_stat_d(&fscache_n_cop_uncache_page
);
1145 spin_unlock(&cookie
->lock
);
1149 EXPORT_SYMBOL(__fscache_uncache_page
);
1152 * fscache_mark_page_cached - Mark a page as being cached
1153 * @op: The retrieval op pages are being marked for
1154 * @page: The page to be marked
1156 * Mark a netfs page as being cached. After this is called, the netfs
1157 * must call fscache_uncache_page() to remove the mark.
1159 void fscache_mark_page_cached(struct fscache_retrieval
*op
, struct page
*page
)
1161 struct fscache_cookie
*cookie
= op
->op
.object
->cookie
;
1163 #ifdef CONFIG_FSCACHE_STATS
1164 atomic_inc(&fscache_n_marks
);
1167 trace_fscache_page(cookie
, page
, fscache_page_cached
);
1169 _debug("- mark %p{%lx}", page
, page
->index
);
1170 if (TestSetPageFsCache(page
)) {
1171 static bool once_only
;
1174 pr_warn("Cookie type %s marked page %lx multiple times\n",
1175 cookie
->def
->name
, page
->index
);
1179 if (cookie
->def
->mark_page_cached
)
1180 cookie
->def
->mark_page_cached(cookie
->netfs_data
,
1183 EXPORT_SYMBOL(fscache_mark_page_cached
);
1186 * fscache_mark_pages_cached - Mark pages as being cached
1187 * @op: The retrieval op pages are being marked for
1188 * @pagevec: The pages to be marked
1190 * Mark a bunch of netfs pages as being cached. After this is called,
1191 * the netfs must call fscache_uncache_page() to remove the mark.
1193 void fscache_mark_pages_cached(struct fscache_retrieval
*op
,
1194 struct pagevec
*pagevec
)
1198 for (loop
= 0; loop
< pagevec
->nr
; loop
++)
1199 fscache_mark_page_cached(op
, pagevec
->pages
[loop
]);
1201 pagevec_reinit(pagevec
);
1203 EXPORT_SYMBOL(fscache_mark_pages_cached
);
1206 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1207 * to be associated with the given cookie.
1209 void __fscache_uncache_all_inode_pages(struct fscache_cookie
*cookie
,
1210 struct inode
*inode
)
1212 struct address_space
*mapping
= inode
->i_mapping
;
1213 struct pagevec pvec
;
1217 _enter("%p,%p", cookie
, inode
);
1219 if (!mapping
|| mapping
->nrpages
== 0) {
1220 _leave(" [no pages]");
1224 pagevec_init(&pvec
);
1227 if (!pagevec_lookup(&pvec
, mapping
, &next
))
1229 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
1230 struct page
*page
= pvec
.pages
[i
];
1231 if (PageFsCache(page
)) {
1232 __fscache_wait_on_page_write(cookie
, page
);
1233 __fscache_uncache_page(cookie
, page
);
1236 pagevec_release(&pvec
);
1242 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages
);