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 * wait for a page to finish being written to the cache. Put a timeout here
48 * since we might be called recursively via parent fs.
51 bool release_page_wait_timeout(struct fscache_cookie
*cookie
, struct page
*page
)
53 wait_queue_head_t
*wq
= bit_waitqueue(&cookie
->flags
, 0);
55 return wait_event_timeout(*wq
, !__fscache_check_page_write(cookie
, page
),
60 * decide whether a page can be released, possibly by cancelling a store to it
61 * - we're allowed to sleep if __GFP_DIRECT_RECLAIM is flagged
63 bool __fscache_maybe_release_page(struct fscache_cookie
*cookie
,
70 _enter("%p,%p,%x", cookie
, page
, gfp
);
74 val
= radix_tree_lookup(&cookie
->stores
, page
->index
);
77 fscache_stat(&fscache_n_store_vmscan_not_storing
);
78 __fscache_uncache_page(cookie
, page
);
82 /* see if the page is actually undergoing storage - if so we can't get
83 * rid of it till the cache has finished with it */
84 if (radix_tree_tag_get(&cookie
->stores
, page
->index
,
85 FSCACHE_COOKIE_STORING_TAG
)) {
90 /* the page is pending storage, so we attempt to cancel the store and
91 * discard the store request so that the page can be reclaimed */
92 spin_lock(&cookie
->stores_lock
);
95 if (radix_tree_tag_get(&cookie
->stores
, page
->index
,
96 FSCACHE_COOKIE_STORING_TAG
)) {
97 /* the page started to undergo storage whilst we were looking,
98 * so now we can only wait or return */
99 spin_unlock(&cookie
->stores_lock
);
103 xpage
= radix_tree_delete(&cookie
->stores
, page
->index
);
104 spin_unlock(&cookie
->stores_lock
);
107 fscache_stat(&fscache_n_store_vmscan_cancelled
);
108 fscache_stat(&fscache_n_store_radix_deletes
);
109 ASSERTCMP(xpage
, ==, page
);
111 fscache_stat(&fscache_n_store_vmscan_gone
);
114 wake_up_bit(&cookie
->flags
, 0);
116 page_cache_release(xpage
);
117 __fscache_uncache_page(cookie
, page
);
121 /* We will wait here if we're allowed to, but that could deadlock the
122 * allocator as the work threads writing to the cache may all end up
123 * sleeping on memory allocation, so we may need to impose a timeout
125 if (!(gfp
& __GFP_DIRECT_RECLAIM
) || !(gfp
& __GFP_FS
)) {
126 fscache_stat(&fscache_n_store_vmscan_busy
);
130 fscache_stat(&fscache_n_store_vmscan_wait
);
131 if (!release_page_wait_timeout(cookie
, page
))
132 _debug("fscache writeout timeout page: %p{%lx}",
135 gfp
&= ~__GFP_DIRECT_RECLAIM
;
138 EXPORT_SYMBOL(__fscache_maybe_release_page
);
141 * note that a page has finished being written to the cache
143 static void fscache_end_page_write(struct fscache_object
*object
,
146 struct fscache_cookie
*cookie
;
147 struct page
*xpage
= NULL
;
149 spin_lock(&object
->lock
);
150 cookie
= object
->cookie
;
152 /* delete the page from the tree if it is now no longer
154 spin_lock(&cookie
->stores_lock
);
155 radix_tree_tag_clear(&cookie
->stores
, page
->index
,
156 FSCACHE_COOKIE_STORING_TAG
);
157 if (!radix_tree_tag_get(&cookie
->stores
, page
->index
,
158 FSCACHE_COOKIE_PENDING_TAG
)) {
159 fscache_stat(&fscache_n_store_radix_deletes
);
160 xpage
= radix_tree_delete(&cookie
->stores
, page
->index
);
162 spin_unlock(&cookie
->stores_lock
);
163 wake_up_bit(&cookie
->flags
, 0);
165 spin_unlock(&object
->lock
);
167 page_cache_release(xpage
);
171 * actually apply the changed attributes to a cache object
173 static void fscache_attr_changed_op(struct fscache_operation
*op
)
175 struct fscache_object
*object
= op
->object
;
178 _enter("{OBJ%x OP%x}", object
->debug_id
, op
->debug_id
);
180 fscache_stat(&fscache_n_attr_changed_calls
);
182 if (fscache_object_is_active(object
)) {
183 fscache_stat(&fscache_n_cop_attr_changed
);
184 ret
= object
->cache
->ops
->attr_changed(object
);
185 fscache_stat_d(&fscache_n_cop_attr_changed
);
187 fscache_abort_object(object
);
190 fscache_op_complete(op
, true);
195 * notification that the attributes on an object have changed
197 int __fscache_attr_changed(struct fscache_cookie
*cookie
)
199 struct fscache_operation
*op
;
200 struct fscache_object
*object
;
201 bool wake_cookie
= false;
203 _enter("%p", cookie
);
205 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
207 fscache_stat(&fscache_n_attr_changed
);
209 op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
211 fscache_stat(&fscache_n_attr_changed_nomem
);
212 _leave(" = -ENOMEM");
216 fscache_operation_init(op
, fscache_attr_changed_op
, NULL
, NULL
);
217 op
->flags
= FSCACHE_OP_ASYNC
|
218 (1 << FSCACHE_OP_EXCLUSIVE
) |
219 (1 << FSCACHE_OP_UNUSE_COOKIE
);
221 spin_lock(&cookie
->lock
);
223 if (!fscache_cookie_enabled(cookie
) ||
224 hlist_empty(&cookie
->backing_objects
))
226 object
= hlist_entry(cookie
->backing_objects
.first
,
227 struct fscache_object
, cookie_link
);
229 __fscache_use_cookie(cookie
);
230 if (fscache_submit_exclusive_op(object
, op
) < 0)
232 spin_unlock(&cookie
->lock
);
233 fscache_stat(&fscache_n_attr_changed_ok
);
234 fscache_put_operation(op
);
239 wake_cookie
= __fscache_unuse_cookie(cookie
);
241 spin_unlock(&cookie
->lock
);
242 fscache_put_operation(op
);
244 __fscache_wake_unused_cookie(cookie
);
245 fscache_stat(&fscache_n_attr_changed_nobufs
);
246 _leave(" = %d", -ENOBUFS
);
249 EXPORT_SYMBOL(__fscache_attr_changed
);
252 * Handle cancellation of a pending retrieval op
254 static void fscache_do_cancel_retrieval(struct fscache_operation
*_op
)
256 struct fscache_retrieval
*op
=
257 container_of(_op
, struct fscache_retrieval
, op
);
259 atomic_set(&op
->n_pages
, 0);
263 * release a retrieval op reference
265 static void fscache_release_retrieval_op(struct fscache_operation
*_op
)
267 struct fscache_retrieval
*op
=
268 container_of(_op
, struct fscache_retrieval
, op
);
270 _enter("{OP%x}", op
->op
.debug_id
);
272 ASSERTIFCMP(op
->op
.state
!= FSCACHE_OP_ST_INITIALISED
,
273 atomic_read(&op
->n_pages
), ==, 0);
275 fscache_hist(fscache_retrieval_histogram
, op
->start_time
);
277 fscache_put_context(op
->cookie
, op
->context
);
283 * allocate a retrieval op
285 static struct fscache_retrieval
*fscache_alloc_retrieval(
286 struct fscache_cookie
*cookie
,
287 struct address_space
*mapping
,
288 fscache_rw_complete_t end_io_func
,
291 struct fscache_retrieval
*op
;
293 /* allocate a retrieval operation and attempt to submit it */
294 op
= kzalloc(sizeof(*op
), GFP_NOIO
);
296 fscache_stat(&fscache_n_retrievals_nomem
);
300 fscache_operation_init(&op
->op
, NULL
,
301 fscache_do_cancel_retrieval
,
302 fscache_release_retrieval_op
);
303 op
->op
.flags
= FSCACHE_OP_MYTHREAD
|
304 (1UL << FSCACHE_OP_WAITING
) |
305 (1UL << FSCACHE_OP_UNUSE_COOKIE
);
307 op
->mapping
= mapping
;
308 op
->end_io_func
= end_io_func
;
309 op
->context
= context
;
310 op
->start_time
= jiffies
;
311 INIT_LIST_HEAD(&op
->to_do
);
313 /* Pin the netfs read context in case we need to do the actual netfs
314 * read because we've encountered a cache read failure.
317 fscache_get_context(op
->cookie
, context
);
322 * wait for a deferred lookup to complete
324 int fscache_wait_for_deferred_lookup(struct fscache_cookie
*cookie
)
330 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
)) {
331 _leave(" = 0 [imm]");
335 fscache_stat(&fscache_n_retrievals_wait
);
338 if (wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
339 TASK_INTERRUPTIBLE
) != 0) {
340 fscache_stat(&fscache_n_retrievals_intr
);
341 _leave(" = -ERESTARTSYS");
345 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
));
348 fscache_hist(fscache_retrieval_delay_histogram
, jif
);
349 _leave(" = 0 [dly]");
354 * wait for an object to become active (or dead)
356 int fscache_wait_for_operation_activation(struct fscache_object
*object
,
357 struct fscache_operation
*op
,
358 atomic_t
*stat_op_waits
,
359 atomic_t
*stat_object_dead
)
363 if (!test_bit(FSCACHE_OP_WAITING
, &op
->flags
))
368 fscache_stat(stat_op_waits
);
369 if (wait_on_bit(&op
->flags
, FSCACHE_OP_WAITING
,
370 TASK_INTERRUPTIBLE
) != 0) {
371 ret
= fscache_cancel_op(op
, false);
375 /* it's been removed from the pending queue by another party,
376 * so we should get to run shortly */
377 wait_on_bit(&op
->flags
, FSCACHE_OP_WAITING
,
378 TASK_UNINTERRUPTIBLE
);
383 if (op
->state
== FSCACHE_OP_ST_CANCELLED
) {
384 if (stat_object_dead
)
385 fscache_stat(stat_object_dead
);
386 _leave(" = -ENOBUFS [cancelled]");
389 if (unlikely(fscache_object_is_dying(object
) ||
390 fscache_cache_is_broken(object
))) {
391 enum fscache_operation_state state
= op
->state
;
392 fscache_cancel_op(op
, true);
393 if (stat_object_dead
)
394 fscache_stat(stat_object_dead
);
395 _leave(" = -ENOBUFS [obj dead %d]", state
);
402 * read a page from the cache or allocate a block in which to store it
404 * -ENOMEM - out of memory, nothing done
405 * -ERESTARTSYS - interrupted
406 * -ENOBUFS - no backing object available in which to cache the block
407 * -ENODATA - no data available in the backing object for this block
408 * 0 - dispatched a read - it'll call end_io_func() when finished
410 int __fscache_read_or_alloc_page(struct fscache_cookie
*cookie
,
412 fscache_rw_complete_t end_io_func
,
416 struct fscache_retrieval
*op
;
417 struct fscache_object
*object
;
418 bool wake_cookie
= false;
421 _enter("%p,%p,,,", cookie
, page
);
423 fscache_stat(&fscache_n_retrievals
);
425 if (hlist_empty(&cookie
->backing_objects
))
428 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
429 _leave(" = -ENOBUFS [invalidating]");
433 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
434 ASSERTCMP(page
, !=, NULL
);
436 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
439 op
= fscache_alloc_retrieval(cookie
, page
->mapping
,
440 end_io_func
, context
);
442 _leave(" = -ENOMEM");
445 atomic_set(&op
->n_pages
, 1);
447 spin_lock(&cookie
->lock
);
449 if (!fscache_cookie_enabled(cookie
) ||
450 hlist_empty(&cookie
->backing_objects
))
452 object
= hlist_entry(cookie
->backing_objects
.first
,
453 struct fscache_object
, cookie_link
);
455 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP
, &object
->flags
));
457 __fscache_use_cookie(cookie
);
458 atomic_inc(&object
->n_reads
);
459 __set_bit(FSCACHE_OP_DEC_READ_CNT
, &op
->op
.flags
);
461 if (fscache_submit_op(object
, &op
->op
) < 0)
462 goto nobufs_unlock_dec
;
463 spin_unlock(&cookie
->lock
);
465 fscache_stat(&fscache_n_retrieval_ops
);
467 /* we wait for the operation to become active, and then process it
468 * *here*, in this thread, and not in the thread pool */
469 ret
= fscache_wait_for_operation_activation(
471 __fscache_stat(&fscache_n_retrieval_op_waits
),
472 __fscache_stat(&fscache_n_retrievals_object_dead
));
476 /* ask the cache to honour the operation */
477 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
478 fscache_stat(&fscache_n_cop_allocate_page
);
479 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
480 fscache_stat_d(&fscache_n_cop_allocate_page
);
484 fscache_stat(&fscache_n_cop_read_or_alloc_page
);
485 ret
= object
->cache
->ops
->read_or_alloc_page(op
, page
, gfp
);
486 fscache_stat_d(&fscache_n_cop_read_or_alloc_page
);
491 fscache_stat(&fscache_n_retrievals_nomem
);
492 else if (ret
== -ERESTARTSYS
)
493 fscache_stat(&fscache_n_retrievals_intr
);
494 else if (ret
== -ENODATA
)
495 fscache_stat(&fscache_n_retrievals_nodata
);
497 fscache_stat(&fscache_n_retrievals_nobufs
);
499 fscache_stat(&fscache_n_retrievals_ok
);
501 fscache_put_retrieval(op
);
502 _leave(" = %d", ret
);
506 atomic_dec(&object
->n_reads
);
507 wake_cookie
= __fscache_unuse_cookie(cookie
);
509 spin_unlock(&cookie
->lock
);
511 __fscache_wake_unused_cookie(cookie
);
512 fscache_put_retrieval(op
);
514 fscache_stat(&fscache_n_retrievals_nobufs
);
515 _leave(" = -ENOBUFS");
518 EXPORT_SYMBOL(__fscache_read_or_alloc_page
);
521 * read a list of page from the cache or allocate a block in which to store
524 * -ENOMEM - out of memory, some pages may be being read
525 * -ERESTARTSYS - interrupted, some pages may be being read
526 * -ENOBUFS - no backing object or space available in which to cache any
527 * pages not being read
528 * -ENODATA - no data available in the backing object for some or all of
530 * 0 - dispatched a read on all pages
532 * end_io_func() will be called for each page read from the cache as it is
533 * finishes being read
535 * any pages for which a read is dispatched will be removed from pages and
538 int __fscache_read_or_alloc_pages(struct fscache_cookie
*cookie
,
539 struct address_space
*mapping
,
540 struct list_head
*pages
,
542 fscache_rw_complete_t end_io_func
,
546 struct fscache_retrieval
*op
;
547 struct fscache_object
*object
;
548 bool wake_cookie
= false;
551 _enter("%p,,%d,,,", cookie
, *nr_pages
);
553 fscache_stat(&fscache_n_retrievals
);
555 if (hlist_empty(&cookie
->backing_objects
))
558 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
559 _leave(" = -ENOBUFS [invalidating]");
563 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
564 ASSERTCMP(*nr_pages
, >, 0);
565 ASSERT(!list_empty(pages
));
567 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
570 op
= fscache_alloc_retrieval(cookie
, mapping
, end_io_func
, context
);
573 atomic_set(&op
->n_pages
, *nr_pages
);
575 spin_lock(&cookie
->lock
);
577 if (!fscache_cookie_enabled(cookie
) ||
578 hlist_empty(&cookie
->backing_objects
))
580 object
= hlist_entry(cookie
->backing_objects
.first
,
581 struct fscache_object
, cookie_link
);
583 __fscache_use_cookie(cookie
);
584 atomic_inc(&object
->n_reads
);
585 __set_bit(FSCACHE_OP_DEC_READ_CNT
, &op
->op
.flags
);
587 if (fscache_submit_op(object
, &op
->op
) < 0)
588 goto nobufs_unlock_dec
;
589 spin_unlock(&cookie
->lock
);
591 fscache_stat(&fscache_n_retrieval_ops
);
593 /* we wait for the operation to become active, and then process it
594 * *here*, in this thread, and not in the thread pool */
595 ret
= fscache_wait_for_operation_activation(
597 __fscache_stat(&fscache_n_retrieval_op_waits
),
598 __fscache_stat(&fscache_n_retrievals_object_dead
));
602 /* ask the cache to honour the operation */
603 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
604 fscache_stat(&fscache_n_cop_allocate_pages
);
605 ret
= object
->cache
->ops
->allocate_pages(
606 op
, pages
, nr_pages
, gfp
);
607 fscache_stat_d(&fscache_n_cop_allocate_pages
);
609 fscache_stat(&fscache_n_cop_read_or_alloc_pages
);
610 ret
= object
->cache
->ops
->read_or_alloc_pages(
611 op
, pages
, nr_pages
, gfp
);
612 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages
);
617 fscache_stat(&fscache_n_retrievals_nomem
);
618 else if (ret
== -ERESTARTSYS
)
619 fscache_stat(&fscache_n_retrievals_intr
);
620 else if (ret
== -ENODATA
)
621 fscache_stat(&fscache_n_retrievals_nodata
);
623 fscache_stat(&fscache_n_retrievals_nobufs
);
625 fscache_stat(&fscache_n_retrievals_ok
);
627 fscache_put_retrieval(op
);
628 _leave(" = %d", ret
);
632 atomic_dec(&object
->n_reads
);
633 wake_cookie
= __fscache_unuse_cookie(cookie
);
635 spin_unlock(&cookie
->lock
);
636 fscache_put_retrieval(op
);
638 __fscache_wake_unused_cookie(cookie
);
640 fscache_stat(&fscache_n_retrievals_nobufs
);
641 _leave(" = -ENOBUFS");
644 EXPORT_SYMBOL(__fscache_read_or_alloc_pages
);
647 * allocate a block in the cache on which to store a page
649 * -ENOMEM - out of memory, nothing done
650 * -ERESTARTSYS - interrupted
651 * -ENOBUFS - no backing object available in which to cache the block
652 * 0 - block allocated
654 int __fscache_alloc_page(struct fscache_cookie
*cookie
,
658 struct fscache_retrieval
*op
;
659 struct fscache_object
*object
;
660 bool wake_cookie
= false;
663 _enter("%p,%p,,,", cookie
, page
);
665 fscache_stat(&fscache_n_allocs
);
667 if (hlist_empty(&cookie
->backing_objects
))
670 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
671 ASSERTCMP(page
, !=, NULL
);
673 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
674 _leave(" = -ENOBUFS [invalidating]");
678 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
681 op
= fscache_alloc_retrieval(cookie
, page
->mapping
, NULL
, NULL
);
684 atomic_set(&op
->n_pages
, 1);
686 spin_lock(&cookie
->lock
);
688 if (!fscache_cookie_enabled(cookie
) ||
689 hlist_empty(&cookie
->backing_objects
))
691 object
= hlist_entry(cookie
->backing_objects
.first
,
692 struct fscache_object
, cookie_link
);
694 __fscache_use_cookie(cookie
);
695 if (fscache_submit_op(object
, &op
->op
) < 0)
696 goto nobufs_unlock_dec
;
697 spin_unlock(&cookie
->lock
);
699 fscache_stat(&fscache_n_alloc_ops
);
701 ret
= fscache_wait_for_operation_activation(
703 __fscache_stat(&fscache_n_alloc_op_waits
),
704 __fscache_stat(&fscache_n_allocs_object_dead
));
708 /* ask the cache to honour the operation */
709 fscache_stat(&fscache_n_cop_allocate_page
);
710 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
711 fscache_stat_d(&fscache_n_cop_allocate_page
);
714 if (ret
== -ERESTARTSYS
)
715 fscache_stat(&fscache_n_allocs_intr
);
717 fscache_stat(&fscache_n_allocs_nobufs
);
719 fscache_stat(&fscache_n_allocs_ok
);
721 fscache_put_retrieval(op
);
722 _leave(" = %d", ret
);
726 wake_cookie
= __fscache_unuse_cookie(cookie
);
728 spin_unlock(&cookie
->lock
);
729 fscache_put_retrieval(op
);
731 __fscache_wake_unused_cookie(cookie
);
733 fscache_stat(&fscache_n_allocs_nobufs
);
734 _leave(" = -ENOBUFS");
737 EXPORT_SYMBOL(__fscache_alloc_page
);
740 * Unmark pages allocate in the readahead code path (via:
741 * fscache_readpages_or_alloc) after delegating to the base filesystem
743 void __fscache_readpages_cancel(struct fscache_cookie
*cookie
,
744 struct list_head
*pages
)
748 list_for_each_entry(page
, pages
, lru
) {
749 if (PageFsCache(page
))
750 __fscache_uncache_page(cookie
, page
);
753 EXPORT_SYMBOL(__fscache_readpages_cancel
);
756 * release a write op reference
758 static void fscache_release_write_op(struct fscache_operation
*_op
)
760 _enter("{OP%x}", _op
->debug_id
);
764 * perform the background storage of a page into the cache
766 static void fscache_write_op(struct fscache_operation
*_op
)
768 struct fscache_storage
*op
=
769 container_of(_op
, struct fscache_storage
, op
);
770 struct fscache_object
*object
= op
->op
.object
;
771 struct fscache_cookie
*cookie
;
777 _enter("{OP%x,%d}", op
->op
.debug_id
, atomic_read(&op
->op
.usage
));
780 spin_lock(&object
->lock
);
781 cookie
= object
->cookie
;
783 if (!fscache_object_is_active(object
)) {
784 /* If we get here, then the on-disk cache object likely longer
785 * exists, so we should just cancel this write operation.
787 spin_unlock(&object
->lock
);
788 fscache_op_complete(&op
->op
, false);
789 _leave(" [inactive]");
794 /* If we get here, then the cookie belonging to the object was
795 * detached, probably by the cookie being withdrawn due to
796 * memory pressure, which means that the pages we might write
797 * to the cache from no longer exist - therefore, we can just
798 * cancel this write operation.
800 spin_unlock(&object
->lock
);
801 fscache_op_complete(&op
->op
, false);
802 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
803 _op
->flags
, _op
->state
, object
->state
->short_name
,
808 spin_lock(&cookie
->stores_lock
);
810 fscache_stat(&fscache_n_store_calls
);
812 /* find a page to store */
814 n
= radix_tree_gang_lookup_tag(&cookie
->stores
, results
, 0, 1,
815 FSCACHE_COOKIE_PENDING_TAG
);
819 _debug("gang %d [%lx]", n
, page
->index
);
821 radix_tree_tag_set(&cookie
->stores
, page
->index
,
822 FSCACHE_COOKIE_STORING_TAG
);
823 radix_tree_tag_clear(&cookie
->stores
, page
->index
,
824 FSCACHE_COOKIE_PENDING_TAG
);
826 spin_unlock(&cookie
->stores_lock
);
827 spin_unlock(&object
->lock
);
829 if (page
->index
>= op
->store_limit
)
832 fscache_stat(&fscache_n_store_pages
);
833 fscache_stat(&fscache_n_cop_write_page
);
834 ret
= object
->cache
->ops
->write_page(op
, page
);
835 fscache_stat_d(&fscache_n_cop_write_page
);
836 fscache_end_page_write(object
, page
);
838 fscache_abort_object(object
);
839 fscache_op_complete(&op
->op
, true);
841 fscache_enqueue_operation(&op
->op
);
848 fscache_stat(&fscache_n_store_pages_over_limit
);
849 fscache_end_page_write(object
, page
);
853 /* this writer is going away and there aren't any more things to
856 spin_unlock(&cookie
->stores_lock
);
857 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
858 spin_unlock(&object
->lock
);
859 fscache_op_complete(&op
->op
, true);
864 * Clear the pages pending writing for invalidation
866 void fscache_invalidate_writes(struct fscache_cookie
*cookie
)
875 spin_lock(&cookie
->stores_lock
);
876 n
= radix_tree_gang_lookup_tag(&cookie
->stores
, results
, 0,
878 FSCACHE_COOKIE_PENDING_TAG
);
880 spin_unlock(&cookie
->stores_lock
);
884 for (i
= n
- 1; i
>= 0; i
--) {
886 radix_tree_delete(&cookie
->stores
, page
->index
);
889 spin_unlock(&cookie
->stores_lock
);
891 for (i
= n
- 1; i
>= 0; i
--)
892 page_cache_release(results
[i
]);
899 * request a page be stored in the cache
901 * -ENOMEM - out of memory, nothing done
902 * -ENOBUFS - no backing object available in which to cache the page
903 * 0 - dispatched a write - it'll call end_io_func() when finished
905 * if the cookie still has a backing object at this point, that object can be
906 * in one of a few states with respect to storage processing:
908 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
913 * (b) writes deferred till post-creation (mark page for writing and
914 * return immediately)
916 * (2) negative lookup, object created, initial fill being made from netfs
918 * (a) fill point not yet reached this page (mark page for writing and
921 * (b) fill point passed this page (queue op to store this page)
923 * (3) object extant (queue op to store this page)
925 * any other state is invalid
927 int __fscache_write_page(struct fscache_cookie
*cookie
,
931 struct fscache_storage
*op
;
932 struct fscache_object
*object
;
933 bool wake_cookie
= false;
936 _enter("%p,%x,", cookie
, (u32
) page
->flags
);
938 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
939 ASSERT(PageFsCache(page
));
941 fscache_stat(&fscache_n_stores
);
943 if (test_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
)) {
944 _leave(" = -ENOBUFS [invalidating]");
948 op
= kzalloc(sizeof(*op
), GFP_NOIO
| __GFP_NOMEMALLOC
| __GFP_NORETRY
);
952 fscache_operation_init(&op
->op
, fscache_write_op
, NULL
,
953 fscache_release_write_op
);
954 op
->op
.flags
= FSCACHE_OP_ASYNC
|
955 (1 << FSCACHE_OP_WAITING
) |
956 (1 << FSCACHE_OP_UNUSE_COOKIE
);
958 ret
= radix_tree_maybe_preload(gfp
& ~__GFP_HIGHMEM
);
963 spin_lock(&cookie
->lock
);
965 if (!fscache_cookie_enabled(cookie
) ||
966 hlist_empty(&cookie
->backing_objects
))
968 object
= hlist_entry(cookie
->backing_objects
.first
,
969 struct fscache_object
, cookie_link
);
970 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
973 /* add the page to the pending-storage radix tree on the backing
975 spin_lock(&object
->lock
);
976 spin_lock(&cookie
->stores_lock
);
978 _debug("store limit %llx", (unsigned long long) object
->store_limit
);
980 ret
= radix_tree_insert(&cookie
->stores
, page
->index
, page
);
984 _debug("insert failed %d", ret
);
985 goto nobufs_unlock_obj
;
988 radix_tree_tag_set(&cookie
->stores
, page
->index
,
989 FSCACHE_COOKIE_PENDING_TAG
);
990 page_cache_get(page
);
992 /* we only want one writer at a time, but we do need to queue new
993 * writers after exclusive ops */
994 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
))
995 goto already_pending
;
997 spin_unlock(&cookie
->stores_lock
);
998 spin_unlock(&object
->lock
);
1000 op
->op
.debug_id
= atomic_inc_return(&fscache_op_debug_id
);
1001 op
->store_limit
= object
->store_limit
;
1003 __fscache_use_cookie(cookie
);
1004 if (fscache_submit_op(object
, &op
->op
) < 0)
1007 spin_unlock(&cookie
->lock
);
1008 radix_tree_preload_end();
1009 fscache_stat(&fscache_n_store_ops
);
1010 fscache_stat(&fscache_n_stores_ok
);
1012 /* the work queue now carries its own ref on the object */
1013 fscache_put_operation(&op
->op
);
1018 fscache_stat(&fscache_n_stores_again
);
1020 spin_unlock(&cookie
->stores_lock
);
1021 spin_unlock(&object
->lock
);
1022 spin_unlock(&cookie
->lock
);
1023 radix_tree_preload_end();
1024 fscache_put_operation(&op
->op
);
1025 fscache_stat(&fscache_n_stores_ok
);
1030 spin_lock(&cookie
->stores_lock
);
1031 radix_tree_delete(&cookie
->stores
, page
->index
);
1032 spin_unlock(&cookie
->stores_lock
);
1033 wake_cookie
= __fscache_unuse_cookie(cookie
);
1034 page_cache_release(page
);
1039 spin_unlock(&cookie
->stores_lock
);
1040 spin_unlock(&object
->lock
);
1042 spin_unlock(&cookie
->lock
);
1043 radix_tree_preload_end();
1044 fscache_put_operation(&op
->op
);
1046 __fscache_wake_unused_cookie(cookie
);
1047 fscache_stat(&fscache_n_stores_nobufs
);
1048 _leave(" = -ENOBUFS");
1052 fscache_put_operation(&op
->op
);
1054 fscache_stat(&fscache_n_stores_oom
);
1055 _leave(" = -ENOMEM");
1058 EXPORT_SYMBOL(__fscache_write_page
);
1061 * remove a page from the cache
1063 void __fscache_uncache_page(struct fscache_cookie
*cookie
, struct page
*page
)
1065 struct fscache_object
*object
;
1067 _enter(",%p", page
);
1069 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
1070 ASSERTCMP(page
, !=, NULL
);
1072 fscache_stat(&fscache_n_uncaches
);
1074 /* cache withdrawal may beat us to it */
1075 if (!PageFsCache(page
))
1078 /* get the object */
1079 spin_lock(&cookie
->lock
);
1081 if (hlist_empty(&cookie
->backing_objects
)) {
1082 ClearPageFsCache(page
);
1086 object
= hlist_entry(cookie
->backing_objects
.first
,
1087 struct fscache_object
, cookie_link
);
1089 /* there might now be stuff on disk we could read */
1090 clear_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
1092 /* only invoke the cache backend if we managed to mark the page
1093 * uncached here; this deals with synchronisation vs withdrawal */
1094 if (TestClearPageFsCache(page
) &&
1095 object
->cache
->ops
->uncache_page
) {
1096 /* the cache backend releases the cookie lock */
1097 fscache_stat(&fscache_n_cop_uncache_page
);
1098 object
->cache
->ops
->uncache_page(object
, page
);
1099 fscache_stat_d(&fscache_n_cop_uncache_page
);
1104 spin_unlock(&cookie
->lock
);
1108 EXPORT_SYMBOL(__fscache_uncache_page
);
1111 * fscache_mark_page_cached - Mark a page as being cached
1112 * @op: The retrieval op pages are being marked for
1113 * @page: The page to be marked
1115 * Mark a netfs page as being cached. After this is called, the netfs
1116 * must call fscache_uncache_page() to remove the mark.
1118 void fscache_mark_page_cached(struct fscache_retrieval
*op
, struct page
*page
)
1120 struct fscache_cookie
*cookie
= op
->op
.object
->cookie
;
1122 #ifdef CONFIG_FSCACHE_STATS
1123 atomic_inc(&fscache_n_marks
);
1126 _debug("- mark %p{%lx}", page
, page
->index
);
1127 if (TestSetPageFsCache(page
)) {
1128 static bool once_only
;
1131 pr_warn("Cookie type %s marked page %lx multiple times\n",
1132 cookie
->def
->name
, page
->index
);
1136 if (cookie
->def
->mark_page_cached
)
1137 cookie
->def
->mark_page_cached(cookie
->netfs_data
,
1140 EXPORT_SYMBOL(fscache_mark_page_cached
);
1143 * fscache_mark_pages_cached - Mark pages as being cached
1144 * @op: The retrieval op pages are being marked for
1145 * @pagevec: The pages to be marked
1147 * Mark a bunch of netfs pages as being cached. After this is called,
1148 * the netfs must call fscache_uncache_page() to remove the mark.
1150 void fscache_mark_pages_cached(struct fscache_retrieval
*op
,
1151 struct pagevec
*pagevec
)
1155 for (loop
= 0; loop
< pagevec
->nr
; loop
++)
1156 fscache_mark_page_cached(op
, pagevec
->pages
[loop
]);
1158 pagevec_reinit(pagevec
);
1160 EXPORT_SYMBOL(fscache_mark_pages_cached
);
1163 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1164 * to be associated with the given cookie.
1166 void __fscache_uncache_all_inode_pages(struct fscache_cookie
*cookie
,
1167 struct inode
*inode
)
1169 struct address_space
*mapping
= inode
->i_mapping
;
1170 struct pagevec pvec
;
1174 _enter("%p,%p", cookie
, inode
);
1176 if (!mapping
|| mapping
->nrpages
== 0) {
1177 _leave(" [no pages]");
1181 pagevec_init(&pvec
, 0);
1184 if (!pagevec_lookup(&pvec
, mapping
, next
, PAGEVEC_SIZE
))
1186 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
1187 struct page
*page
= pvec
.pages
[i
];
1189 if (PageFsCache(page
)) {
1190 __fscache_wait_on_page_write(cookie
, page
);
1191 __fscache_uncache_page(cookie
, page
);
1194 pagevec_release(&pvec
);
1200 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages
);