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>
20 * check to see if a page is being written to the cache
22 bool __fscache_check_page_write(struct fscache_cookie
*cookie
, struct page
*page
)
27 val
= radix_tree_lookup(&cookie
->stores
, page
->index
);
32 EXPORT_SYMBOL(__fscache_check_page_write
);
35 * wait for a page to finish being written to the cache
37 void __fscache_wait_on_page_write(struct fscache_cookie
*cookie
, struct page
*page
)
39 wait_queue_head_t
*wq
= bit_waitqueue(&cookie
->flags
, 0);
41 wait_event(*wq
, !__fscache_check_page_write(cookie
, page
));
43 EXPORT_SYMBOL(__fscache_wait_on_page_write
);
46 * decide whether a page can be released, possibly by cancelling a store to it
47 * - we're allowed to sleep if __GFP_WAIT is flagged
49 bool __fscache_maybe_release_page(struct fscache_cookie
*cookie
,
56 _enter("%p,%p,%x", cookie
, page
, gfp
);
59 val
= radix_tree_lookup(&cookie
->stores
, page
->index
);
62 fscache_stat(&fscache_n_store_vmscan_not_storing
);
63 __fscache_uncache_page(cookie
, page
);
67 /* see if the page is actually undergoing storage - if so we can't get
68 * rid of it till the cache has finished with it */
69 if (radix_tree_tag_get(&cookie
->stores
, page
->index
,
70 FSCACHE_COOKIE_STORING_TAG
)) {
75 /* the page is pending storage, so we attempt to cancel the store and
76 * discard the store request so that the page can be reclaimed */
77 spin_lock(&cookie
->stores_lock
);
80 if (radix_tree_tag_get(&cookie
->stores
, page
->index
,
81 FSCACHE_COOKIE_STORING_TAG
)) {
82 /* the page started to undergo storage whilst we were looking,
83 * so now we can only wait or return */
84 spin_unlock(&cookie
->stores_lock
);
88 xpage
= radix_tree_delete(&cookie
->stores
, page
->index
);
89 spin_unlock(&cookie
->stores_lock
);
92 fscache_stat(&fscache_n_store_vmscan_cancelled
);
93 fscache_stat(&fscache_n_store_radix_deletes
);
94 ASSERTCMP(xpage
, ==, page
);
96 fscache_stat(&fscache_n_store_vmscan_gone
);
99 wake_up_bit(&cookie
->flags
, 0);
101 page_cache_release(xpage
);
102 __fscache_uncache_page(cookie
, page
);
106 /* we might want to wait here, but that could deadlock the allocator as
107 * the slow-work threads writing to the cache may all end up sleeping
108 * on memory allocation */
109 fscache_stat(&fscache_n_store_vmscan_busy
);
112 EXPORT_SYMBOL(__fscache_maybe_release_page
);
115 * note that a page has finished being written to the cache
117 static void fscache_end_page_write(struct fscache_object
*object
,
120 struct fscache_cookie
*cookie
;
121 struct page
*xpage
= NULL
;
123 spin_lock(&object
->lock
);
124 cookie
= object
->cookie
;
126 /* delete the page from the tree if it is now no longer
128 spin_lock(&cookie
->stores_lock
);
129 radix_tree_tag_clear(&cookie
->stores
, page
->index
,
130 FSCACHE_COOKIE_STORING_TAG
);
131 if (!radix_tree_tag_get(&cookie
->stores
, page
->index
,
132 FSCACHE_COOKIE_PENDING_TAG
)) {
133 fscache_stat(&fscache_n_store_radix_deletes
);
134 xpage
= radix_tree_delete(&cookie
->stores
, page
->index
);
136 spin_unlock(&cookie
->stores_lock
);
137 wake_up_bit(&cookie
->flags
, 0);
139 spin_unlock(&object
->lock
);
141 page_cache_release(xpage
);
145 * actually apply the changed attributes to a cache object
147 static void fscache_attr_changed_op(struct fscache_operation
*op
)
149 struct fscache_object
*object
= op
->object
;
152 _enter("{OBJ%x OP%x}", object
->debug_id
, op
->debug_id
);
154 fscache_stat(&fscache_n_attr_changed_calls
);
156 if (fscache_object_is_active(object
)) {
157 fscache_set_op_state(op
, "CallFS");
158 fscache_stat(&fscache_n_cop_attr_changed
);
159 ret
= object
->cache
->ops
->attr_changed(object
);
160 fscache_stat_d(&fscache_n_cop_attr_changed
);
161 fscache_set_op_state(op
, "Done");
163 fscache_abort_object(object
);
170 * notification that the attributes on an object have changed
172 int __fscache_attr_changed(struct fscache_cookie
*cookie
)
174 struct fscache_operation
*op
;
175 struct fscache_object
*object
;
177 _enter("%p", cookie
);
179 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
181 fscache_stat(&fscache_n_attr_changed
);
183 op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
185 fscache_stat(&fscache_n_attr_changed_nomem
);
186 _leave(" = -ENOMEM");
190 fscache_operation_init(op
, NULL
);
191 fscache_operation_init_slow(op
, fscache_attr_changed_op
);
192 op
->flags
= FSCACHE_OP_SLOW
| (1 << FSCACHE_OP_EXCLUSIVE
);
193 fscache_set_op_name(op
, "Attr");
195 spin_lock(&cookie
->lock
);
197 if (hlist_empty(&cookie
->backing_objects
))
199 object
= hlist_entry(cookie
->backing_objects
.first
,
200 struct fscache_object
, cookie_link
);
202 if (fscache_submit_exclusive_op(object
, op
) < 0)
204 spin_unlock(&cookie
->lock
);
205 fscache_stat(&fscache_n_attr_changed_ok
);
206 fscache_put_operation(op
);
211 spin_unlock(&cookie
->lock
);
213 fscache_stat(&fscache_n_attr_changed_nobufs
);
214 _leave(" = %d", -ENOBUFS
);
217 EXPORT_SYMBOL(__fscache_attr_changed
);
220 * handle secondary execution given to a retrieval op on behalf of the
223 static void fscache_retrieval_work(struct work_struct
*work
)
225 struct fscache_retrieval
*op
=
226 container_of(work
, struct fscache_retrieval
, op
.fast_work
);
229 _enter("{OP%x}", op
->op
.debug_id
);
232 op
->op
.processor(&op
->op
);
233 fscache_hist(fscache_ops_histogram
, start
);
234 fscache_put_operation(&op
->op
);
238 * release a retrieval op reference
240 static void fscache_release_retrieval_op(struct fscache_operation
*_op
)
242 struct fscache_retrieval
*op
=
243 container_of(_op
, struct fscache_retrieval
, op
);
245 _enter("{OP%x}", op
->op
.debug_id
);
247 fscache_hist(fscache_retrieval_histogram
, op
->start_time
);
249 fscache_put_context(op
->op
.object
->cookie
, op
->context
);
255 * allocate a retrieval op
257 static struct fscache_retrieval
*fscache_alloc_retrieval(
258 struct address_space
*mapping
,
259 fscache_rw_complete_t end_io_func
,
262 struct fscache_retrieval
*op
;
264 /* allocate a retrieval operation and attempt to submit it */
265 op
= kzalloc(sizeof(*op
), GFP_NOIO
);
267 fscache_stat(&fscache_n_retrievals_nomem
);
271 fscache_operation_init(&op
->op
, fscache_release_retrieval_op
);
272 op
->op
.flags
= FSCACHE_OP_MYTHREAD
| (1 << FSCACHE_OP_WAITING
);
273 op
->mapping
= mapping
;
274 op
->end_io_func
= end_io_func
;
275 op
->context
= context
;
276 op
->start_time
= jiffies
;
277 INIT_WORK(&op
->op
.fast_work
, fscache_retrieval_work
);
278 INIT_LIST_HEAD(&op
->to_do
);
279 fscache_set_op_name(&op
->op
, "Retr");
284 * wait for a deferred lookup to complete
286 static int fscache_wait_for_deferred_lookup(struct fscache_cookie
*cookie
)
292 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
)) {
293 _leave(" = 0 [imm]");
297 fscache_stat(&fscache_n_retrievals_wait
);
300 if (wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
301 fscache_wait_bit_interruptible
,
302 TASK_INTERRUPTIBLE
) != 0) {
303 fscache_stat(&fscache_n_retrievals_intr
);
304 _leave(" = -ERESTARTSYS");
308 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
));
311 fscache_hist(fscache_retrieval_delay_histogram
, jif
);
312 _leave(" = 0 [dly]");
317 * read a page from the cache or allocate a block in which to store it
319 * -ENOMEM - out of memory, nothing done
320 * -ERESTARTSYS - interrupted
321 * -ENOBUFS - no backing object available in which to cache the block
322 * -ENODATA - no data available in the backing object for this block
323 * 0 - dispatched a read - it'll call end_io_func() when finished
325 int __fscache_read_or_alloc_page(struct fscache_cookie
*cookie
,
327 fscache_rw_complete_t end_io_func
,
331 struct fscache_retrieval
*op
;
332 struct fscache_object
*object
;
335 _enter("%p,%p,,,", cookie
, page
);
337 fscache_stat(&fscache_n_retrievals
);
339 if (hlist_empty(&cookie
->backing_objects
))
342 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
343 ASSERTCMP(page
, !=, NULL
);
345 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
348 op
= fscache_alloc_retrieval(page
->mapping
, end_io_func
, context
);
350 _leave(" = -ENOMEM");
353 fscache_set_op_name(&op
->op
, "RetrRA1");
355 spin_lock(&cookie
->lock
);
357 if (hlist_empty(&cookie
->backing_objects
))
359 object
= hlist_entry(cookie
->backing_objects
.first
,
360 struct fscache_object
, cookie_link
);
362 ASSERTCMP(object
->state
, >, FSCACHE_OBJECT_LOOKING_UP
);
364 atomic_inc(&object
->n_reads
);
365 set_bit(FSCACHE_OP_DEC_READ_CNT
, &op
->op
.flags
);
367 if (fscache_submit_op(object
, &op
->op
) < 0)
369 spin_unlock(&cookie
->lock
);
371 fscache_stat(&fscache_n_retrieval_ops
);
373 /* pin the netfs read context in case we need to do the actual netfs
374 * read because we've encountered a cache read failure */
375 fscache_get_context(object
->cookie
, op
->context
);
377 /* we wait for the operation to become active, and then process it
378 * *here*, in this thread, and not in the thread pool */
379 if (test_bit(FSCACHE_OP_WAITING
, &op
->op
.flags
)) {
381 fscache_stat(&fscache_n_retrieval_op_waits
);
382 if (wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
383 fscache_wait_bit_interruptible
,
384 TASK_INTERRUPTIBLE
) < 0) {
385 ret
= fscache_cancel_op(&op
->op
);
391 /* it's been removed from the pending queue by another
392 * party, so we should get to run shortly */
393 wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
394 fscache_wait_bit
, TASK_UNINTERRUPTIBLE
);
399 /* ask the cache to honour the operation */
400 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
401 fscache_stat(&fscache_n_cop_allocate_page
);
402 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
403 fscache_stat_d(&fscache_n_cop_allocate_page
);
407 fscache_stat(&fscache_n_cop_read_or_alloc_page
);
408 ret
= object
->cache
->ops
->read_or_alloc_page(op
, page
, gfp
);
409 fscache_stat_d(&fscache_n_cop_read_or_alloc_page
);
414 fscache_stat(&fscache_n_retrievals_nomem
);
415 else if (ret
== -ERESTARTSYS
)
416 fscache_stat(&fscache_n_retrievals_intr
);
417 else if (ret
== -ENODATA
)
418 fscache_stat(&fscache_n_retrievals_nodata
);
420 fscache_stat(&fscache_n_retrievals_nobufs
);
422 fscache_stat(&fscache_n_retrievals_ok
);
424 fscache_put_retrieval(op
);
425 _leave(" = %d", ret
);
429 spin_unlock(&cookie
->lock
);
432 fscache_stat(&fscache_n_retrievals_nobufs
);
433 _leave(" = -ENOBUFS");
436 EXPORT_SYMBOL(__fscache_read_or_alloc_page
);
439 * read a list of page from the cache or allocate a block in which to store
442 * -ENOMEM - out of memory, some pages may be being read
443 * -ERESTARTSYS - interrupted, some pages may be being read
444 * -ENOBUFS - no backing object or space available in which to cache any
445 * pages not being read
446 * -ENODATA - no data available in the backing object for some or all of
448 * 0 - dispatched a read on all pages
450 * end_io_func() will be called for each page read from the cache as it is
451 * finishes being read
453 * any pages for which a read is dispatched will be removed from pages and
456 int __fscache_read_or_alloc_pages(struct fscache_cookie
*cookie
,
457 struct address_space
*mapping
,
458 struct list_head
*pages
,
460 fscache_rw_complete_t end_io_func
,
464 struct fscache_retrieval
*op
;
465 struct fscache_object
*object
;
468 _enter("%p,,%d,,,", cookie
, *nr_pages
);
470 fscache_stat(&fscache_n_retrievals
);
472 if (hlist_empty(&cookie
->backing_objects
))
475 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
476 ASSERTCMP(*nr_pages
, >, 0);
477 ASSERT(!list_empty(pages
));
479 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
482 op
= fscache_alloc_retrieval(mapping
, end_io_func
, context
);
485 fscache_set_op_name(&op
->op
, "RetrRAN");
487 spin_lock(&cookie
->lock
);
489 if (hlist_empty(&cookie
->backing_objects
))
491 object
= hlist_entry(cookie
->backing_objects
.first
,
492 struct fscache_object
, cookie_link
);
494 atomic_inc(&object
->n_reads
);
495 set_bit(FSCACHE_OP_DEC_READ_CNT
, &op
->op
.flags
);
497 if (fscache_submit_op(object
, &op
->op
) < 0)
499 spin_unlock(&cookie
->lock
);
501 fscache_stat(&fscache_n_retrieval_ops
);
503 /* pin the netfs read context in case we need to do the actual netfs
504 * read because we've encountered a cache read failure */
505 fscache_get_context(object
->cookie
, op
->context
);
507 /* we wait for the operation to become active, and then process it
508 * *here*, in this thread, and not in the thread pool */
509 if (test_bit(FSCACHE_OP_WAITING
, &op
->op
.flags
)) {
511 fscache_stat(&fscache_n_retrieval_op_waits
);
512 if (wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
513 fscache_wait_bit_interruptible
,
514 TASK_INTERRUPTIBLE
) < 0) {
515 ret
= fscache_cancel_op(&op
->op
);
521 /* it's been removed from the pending queue by another
522 * party, so we should get to run shortly */
523 wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
524 fscache_wait_bit
, TASK_UNINTERRUPTIBLE
);
529 /* ask the cache to honour the operation */
530 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
531 fscache_stat(&fscache_n_cop_allocate_pages
);
532 ret
= object
->cache
->ops
->allocate_pages(
533 op
, pages
, nr_pages
, gfp
);
534 fscache_stat_d(&fscache_n_cop_allocate_pages
);
536 fscache_stat(&fscache_n_cop_read_or_alloc_pages
);
537 ret
= object
->cache
->ops
->read_or_alloc_pages(
538 op
, pages
, nr_pages
, gfp
);
539 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages
);
544 fscache_stat(&fscache_n_retrievals_nomem
);
545 else if (ret
== -ERESTARTSYS
)
546 fscache_stat(&fscache_n_retrievals_intr
);
547 else if (ret
== -ENODATA
)
548 fscache_stat(&fscache_n_retrievals_nodata
);
550 fscache_stat(&fscache_n_retrievals_nobufs
);
552 fscache_stat(&fscache_n_retrievals_ok
);
554 fscache_put_retrieval(op
);
555 _leave(" = %d", ret
);
559 spin_unlock(&cookie
->lock
);
562 fscache_stat(&fscache_n_retrievals_nobufs
);
563 _leave(" = -ENOBUFS");
566 EXPORT_SYMBOL(__fscache_read_or_alloc_pages
);
569 * allocate a block in the cache on which to store a page
571 * -ENOMEM - out of memory, nothing done
572 * -ERESTARTSYS - interrupted
573 * -ENOBUFS - no backing object available in which to cache the block
574 * 0 - block allocated
576 int __fscache_alloc_page(struct fscache_cookie
*cookie
,
580 struct fscache_retrieval
*op
;
581 struct fscache_object
*object
;
584 _enter("%p,%p,,,", cookie
, page
);
586 fscache_stat(&fscache_n_allocs
);
588 if (hlist_empty(&cookie
->backing_objects
))
591 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
592 ASSERTCMP(page
, !=, NULL
);
594 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
597 op
= fscache_alloc_retrieval(page
->mapping
, NULL
, NULL
);
600 fscache_set_op_name(&op
->op
, "RetrAL1");
602 spin_lock(&cookie
->lock
);
604 if (hlist_empty(&cookie
->backing_objects
))
606 object
= hlist_entry(cookie
->backing_objects
.first
,
607 struct fscache_object
, cookie_link
);
609 if (fscache_submit_op(object
, &op
->op
) < 0)
611 spin_unlock(&cookie
->lock
);
613 fscache_stat(&fscache_n_alloc_ops
);
615 if (test_bit(FSCACHE_OP_WAITING
, &op
->op
.flags
)) {
617 fscache_stat(&fscache_n_alloc_op_waits
);
618 if (wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
619 fscache_wait_bit_interruptible
,
620 TASK_INTERRUPTIBLE
) < 0) {
621 ret
= fscache_cancel_op(&op
->op
);
627 /* it's been removed from the pending queue by another
628 * party, so we should get to run shortly */
629 wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
630 fscache_wait_bit
, TASK_UNINTERRUPTIBLE
);
635 /* ask the cache to honour the operation */
636 fscache_stat(&fscache_n_cop_allocate_page
);
637 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
638 fscache_stat_d(&fscache_n_cop_allocate_page
);
641 if (ret
== -ERESTARTSYS
)
642 fscache_stat(&fscache_n_allocs_intr
);
644 fscache_stat(&fscache_n_allocs_nobufs
);
646 fscache_stat(&fscache_n_allocs_ok
);
648 fscache_put_retrieval(op
);
649 _leave(" = %d", ret
);
653 spin_unlock(&cookie
->lock
);
656 fscache_stat(&fscache_n_allocs_nobufs
);
657 _leave(" = -ENOBUFS");
660 EXPORT_SYMBOL(__fscache_alloc_page
);
663 * release a write op reference
665 static void fscache_release_write_op(struct fscache_operation
*_op
)
667 _enter("{OP%x}", _op
->debug_id
);
671 * perform the background storage of a page into the cache
673 static void fscache_write_op(struct fscache_operation
*_op
)
675 struct fscache_storage
*op
=
676 container_of(_op
, struct fscache_storage
, op
);
677 struct fscache_object
*object
= op
->op
.object
;
678 struct fscache_cookie
*cookie
;
684 _enter("{OP%x,%d}", op
->op
.debug_id
, atomic_read(&op
->op
.usage
));
686 fscache_set_op_state(&op
->op
, "GetPage");
688 spin_lock(&object
->lock
);
689 cookie
= object
->cookie
;
691 if (!fscache_object_is_active(object
) || !cookie
) {
692 spin_unlock(&object
->lock
);
697 spin_lock(&cookie
->stores_lock
);
699 fscache_stat(&fscache_n_store_calls
);
701 /* find a page to store */
703 n
= radix_tree_gang_lookup_tag(&cookie
->stores
, results
, 0, 1,
704 FSCACHE_COOKIE_PENDING_TAG
);
708 _debug("gang %d [%lx]", n
, page
->index
);
709 if (page
->index
> op
->store_limit
) {
710 fscache_stat(&fscache_n_store_pages_over_limit
);
715 radix_tree_tag_set(&cookie
->stores
, page
->index
,
716 FSCACHE_COOKIE_STORING_TAG
);
717 radix_tree_tag_clear(&cookie
->stores
, page
->index
,
718 FSCACHE_COOKIE_PENDING_TAG
);
721 spin_unlock(&cookie
->stores_lock
);
722 spin_unlock(&object
->lock
);
725 fscache_set_op_state(&op
->op
, "Store");
726 fscache_stat(&fscache_n_store_pages
);
727 fscache_stat(&fscache_n_cop_write_page
);
728 ret
= object
->cache
->ops
->write_page(op
, page
);
729 fscache_stat_d(&fscache_n_cop_write_page
);
730 fscache_set_op_state(&op
->op
, "EndWrite");
731 fscache_end_page_write(object
, page
);
733 fscache_set_op_state(&op
->op
, "Abort");
734 fscache_abort_object(object
);
736 fscache_enqueue_operation(&op
->op
);
744 /* this writer is going away and there aren't any more things to
747 spin_unlock(&cookie
->stores_lock
);
748 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
749 spin_unlock(&object
->lock
);
754 * request a page be stored in the cache
756 * -ENOMEM - out of memory, nothing done
757 * -ENOBUFS - no backing object available in which to cache the page
758 * 0 - dispatched a write - it'll call end_io_func() when finished
760 * if the cookie still has a backing object at this point, that object can be
761 * in one of a few states with respect to storage processing:
763 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
766 * (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
769 * (b) writes deferred till post-creation (mark page for writing and
770 * return immediately)
772 * (2) negative lookup, object created, initial fill being made from netfs
773 * (FSCACHE_COOKIE_INITIAL_FILL is set)
775 * (a) fill point not yet reached this page (mark page for writing and
778 * (b) fill point passed this page (queue op to store this page)
780 * (3) object extant (queue op to store this page)
782 * any other state is invalid
784 int __fscache_write_page(struct fscache_cookie
*cookie
,
788 struct fscache_storage
*op
;
789 struct fscache_object
*object
;
792 _enter("%p,%x,", cookie
, (u32
) page
->flags
);
794 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
795 ASSERT(PageFsCache(page
));
797 fscache_stat(&fscache_n_stores
);
799 op
= kzalloc(sizeof(*op
), GFP_NOIO
);
803 fscache_operation_init(&op
->op
, fscache_release_write_op
);
804 fscache_operation_init_slow(&op
->op
, fscache_write_op
);
805 op
->op
.flags
= FSCACHE_OP_SLOW
| (1 << FSCACHE_OP_WAITING
);
806 fscache_set_op_name(&op
->op
, "Write1");
808 ret
= radix_tree_preload(gfp
& ~__GFP_HIGHMEM
);
813 spin_lock(&cookie
->lock
);
815 if (hlist_empty(&cookie
->backing_objects
))
817 object
= hlist_entry(cookie
->backing_objects
.first
,
818 struct fscache_object
, cookie_link
);
819 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
822 /* add the page to the pending-storage radix tree on the backing
824 spin_lock(&object
->lock
);
825 spin_lock(&cookie
->stores_lock
);
827 _debug("store limit %llx", (unsigned long long) object
->store_limit
);
829 ret
= radix_tree_insert(&cookie
->stores
, page
->index
, page
);
833 _debug("insert failed %d", ret
);
834 goto nobufs_unlock_obj
;
837 radix_tree_tag_set(&cookie
->stores
, page
->index
,
838 FSCACHE_COOKIE_PENDING_TAG
);
839 page_cache_get(page
);
841 /* we only want one writer at a time, but we do need to queue new
842 * writers after exclusive ops */
843 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
))
844 goto already_pending
;
846 spin_unlock(&cookie
->stores_lock
);
847 spin_unlock(&object
->lock
);
849 op
->op
.debug_id
= atomic_inc_return(&fscache_op_debug_id
);
850 op
->store_limit
= object
->store_limit
;
852 if (fscache_submit_op(object
, &op
->op
) < 0)
855 spin_unlock(&cookie
->lock
);
856 radix_tree_preload_end();
857 fscache_stat(&fscache_n_store_ops
);
858 fscache_stat(&fscache_n_stores_ok
);
860 /* the slow work queue now carries its own ref on the object */
861 fscache_put_operation(&op
->op
);
866 fscache_stat(&fscache_n_stores_again
);
868 spin_unlock(&cookie
->stores_lock
);
869 spin_unlock(&object
->lock
);
870 spin_unlock(&cookie
->lock
);
871 radix_tree_preload_end();
873 fscache_stat(&fscache_n_stores_ok
);
878 spin_lock(&cookie
->stores_lock
);
879 radix_tree_delete(&cookie
->stores
, page
->index
);
880 spin_unlock(&cookie
->stores_lock
);
881 page_cache_release(page
);
886 spin_unlock(&object
->lock
);
888 spin_unlock(&cookie
->lock
);
889 radix_tree_preload_end();
891 fscache_stat(&fscache_n_stores_nobufs
);
892 _leave(" = -ENOBUFS");
898 fscache_stat(&fscache_n_stores_oom
);
899 _leave(" = -ENOMEM");
902 EXPORT_SYMBOL(__fscache_write_page
);
905 * remove a page from the cache
907 void __fscache_uncache_page(struct fscache_cookie
*cookie
, struct page
*page
)
909 struct fscache_object
*object
;
913 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
914 ASSERTCMP(page
, !=, NULL
);
916 fscache_stat(&fscache_n_uncaches
);
918 /* cache withdrawal may beat us to it */
919 if (!PageFsCache(page
))
923 spin_lock(&cookie
->lock
);
925 if (hlist_empty(&cookie
->backing_objects
)) {
926 ClearPageFsCache(page
);
930 object
= hlist_entry(cookie
->backing_objects
.first
,
931 struct fscache_object
, cookie_link
);
933 /* there might now be stuff on disk we could read */
934 clear_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
936 /* only invoke the cache backend if we managed to mark the page
937 * uncached here; this deals with synchronisation vs withdrawal */
938 if (TestClearPageFsCache(page
) &&
939 object
->cache
->ops
->uncache_page
) {
940 /* the cache backend releases the cookie lock */
941 fscache_stat(&fscache_n_cop_uncache_page
);
942 object
->cache
->ops
->uncache_page(object
, page
);
943 fscache_stat_d(&fscache_n_cop_uncache_page
);
948 spin_unlock(&cookie
->lock
);
952 EXPORT_SYMBOL(__fscache_uncache_page
);
955 * fscache_mark_pages_cached - Mark pages as being cached
956 * @op: The retrieval op pages are being marked for
957 * @pagevec: The pages to be marked
959 * Mark a bunch of netfs pages as being cached. After this is called,
960 * the netfs must call fscache_uncache_page() to remove the mark.
962 void fscache_mark_pages_cached(struct fscache_retrieval
*op
,
963 struct pagevec
*pagevec
)
965 struct fscache_cookie
*cookie
= op
->op
.object
->cookie
;
968 #ifdef CONFIG_FSCACHE_STATS
969 atomic_add(pagevec
->nr
, &fscache_n_marks
);
972 for (loop
= 0; loop
< pagevec
->nr
; loop
++) {
973 struct page
*page
= pagevec
->pages
[loop
];
975 _debug("- mark %p{%lx}", page
, page
->index
);
976 if (TestSetPageFsCache(page
)) {
977 static bool once_only
;
980 printk(KERN_WARNING
"FS-Cache:"
981 " Cookie type %s marked page %lx"
983 cookie
->def
->name
, page
->index
);
988 if (cookie
->def
->mark_pages_cached
)
989 cookie
->def
->mark_pages_cached(cookie
->netfs_data
,
990 op
->mapping
, pagevec
);
991 pagevec_reinit(pagevec
);
993 EXPORT_SYMBOL(fscache_mark_pages_cached
);