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 * note that a page has finished being written to the cache
48 static void fscache_end_page_write(struct fscache_cookie
*cookie
, struct page
*page
)
52 spin_lock(&cookie
->lock
);
53 xpage
= radix_tree_delete(&cookie
->stores
, page
->index
);
54 spin_unlock(&cookie
->lock
);
55 ASSERT(xpage
!= NULL
);
57 wake_up_bit(&cookie
->flags
, 0);
61 * actually apply the changed attributes to a cache object
63 static void fscache_attr_changed_op(struct fscache_operation
*op
)
65 struct fscache_object
*object
= op
->object
;
67 _enter("{OBJ%x OP%x}", object
->debug_id
, op
->debug_id
);
69 fscache_stat(&fscache_n_attr_changed_calls
);
71 if (fscache_object_is_active(object
) &&
72 object
->cache
->ops
->attr_changed(object
) < 0)
73 fscache_abort_object(object
);
79 * notification that the attributes on an object have changed
81 int __fscache_attr_changed(struct fscache_cookie
*cookie
)
83 struct fscache_operation
*op
;
84 struct fscache_object
*object
;
88 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
90 fscache_stat(&fscache_n_attr_changed
);
92 op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
94 fscache_stat(&fscache_n_attr_changed_nomem
);
99 fscache_operation_init(op
, NULL
);
100 fscache_operation_init_slow(op
, fscache_attr_changed_op
);
101 op
->flags
= FSCACHE_OP_SLOW
| (1 << FSCACHE_OP_EXCLUSIVE
);
103 spin_lock(&cookie
->lock
);
105 if (hlist_empty(&cookie
->backing_objects
))
107 object
= hlist_entry(cookie
->backing_objects
.first
,
108 struct fscache_object
, cookie_link
);
110 if (fscache_submit_exclusive_op(object
, op
) < 0)
112 spin_unlock(&cookie
->lock
);
113 fscache_stat(&fscache_n_attr_changed_ok
);
114 fscache_put_operation(op
);
119 spin_unlock(&cookie
->lock
);
121 fscache_stat(&fscache_n_attr_changed_nobufs
);
122 _leave(" = %d", -ENOBUFS
);
125 EXPORT_SYMBOL(__fscache_attr_changed
);
128 * handle secondary execution given to a retrieval op on behalf of the
131 static void fscache_retrieval_work(struct work_struct
*work
)
133 struct fscache_retrieval
*op
=
134 container_of(work
, struct fscache_retrieval
, op
.fast_work
);
137 _enter("{OP%x}", op
->op
.debug_id
);
140 op
->op
.processor(&op
->op
);
141 fscache_hist(fscache_ops_histogram
, start
);
142 fscache_put_operation(&op
->op
);
146 * release a retrieval op reference
148 static void fscache_release_retrieval_op(struct fscache_operation
*_op
)
150 struct fscache_retrieval
*op
=
151 container_of(_op
, struct fscache_retrieval
, op
);
153 _enter("{OP%x}", op
->op
.debug_id
);
155 fscache_hist(fscache_retrieval_histogram
, op
->start_time
);
157 fscache_put_context(op
->op
.object
->cookie
, op
->context
);
163 * allocate a retrieval op
165 static struct fscache_retrieval
*fscache_alloc_retrieval(
166 struct address_space
*mapping
,
167 fscache_rw_complete_t end_io_func
,
170 struct fscache_retrieval
*op
;
172 /* allocate a retrieval operation and attempt to submit it */
173 op
= kzalloc(sizeof(*op
), GFP_NOIO
);
175 fscache_stat(&fscache_n_retrievals_nomem
);
179 fscache_operation_init(&op
->op
, fscache_release_retrieval_op
);
180 op
->op
.flags
= FSCACHE_OP_MYTHREAD
| (1 << FSCACHE_OP_WAITING
);
181 op
->mapping
= mapping
;
182 op
->end_io_func
= end_io_func
;
183 op
->context
= context
;
184 op
->start_time
= jiffies
;
185 INIT_WORK(&op
->op
.fast_work
, fscache_retrieval_work
);
186 INIT_LIST_HEAD(&op
->to_do
);
191 * wait for a deferred lookup to complete
193 static int fscache_wait_for_deferred_lookup(struct fscache_cookie
*cookie
)
199 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
)) {
200 _leave(" = 0 [imm]");
204 fscache_stat(&fscache_n_retrievals_wait
);
207 if (wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
208 fscache_wait_bit_interruptible
,
209 TASK_INTERRUPTIBLE
) != 0) {
210 fscache_stat(&fscache_n_retrievals_intr
);
211 _leave(" = -ERESTARTSYS");
215 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
));
218 fscache_hist(fscache_retrieval_delay_histogram
, jif
);
219 _leave(" = 0 [dly]");
224 * read a page from the cache or allocate a block in which to store it
226 * -ENOMEM - out of memory, nothing done
227 * -ERESTARTSYS - interrupted
228 * -ENOBUFS - no backing object available in which to cache the block
229 * -ENODATA - no data available in the backing object for this block
230 * 0 - dispatched a read - it'll call end_io_func() when finished
232 int __fscache_read_or_alloc_page(struct fscache_cookie
*cookie
,
234 fscache_rw_complete_t end_io_func
,
238 struct fscache_retrieval
*op
;
239 struct fscache_object
*object
;
242 _enter("%p,%p,,,", cookie
, page
);
244 fscache_stat(&fscache_n_retrievals
);
246 if (hlist_empty(&cookie
->backing_objects
))
249 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
250 ASSERTCMP(page
, !=, NULL
);
252 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
255 op
= fscache_alloc_retrieval(page
->mapping
, end_io_func
, context
);
257 _leave(" = -ENOMEM");
261 spin_lock(&cookie
->lock
);
263 if (hlist_empty(&cookie
->backing_objects
))
265 object
= hlist_entry(cookie
->backing_objects
.first
,
266 struct fscache_object
, cookie_link
);
268 ASSERTCMP(object
->state
, >, FSCACHE_OBJECT_LOOKING_UP
);
270 if (fscache_submit_op(object
, &op
->op
) < 0)
272 spin_unlock(&cookie
->lock
);
274 fscache_stat(&fscache_n_retrieval_ops
);
276 /* pin the netfs read context in case we need to do the actual netfs
277 * read because we've encountered a cache read failure */
278 fscache_get_context(object
->cookie
, op
->context
);
280 /* we wait for the operation to become active, and then process it
281 * *here*, in this thread, and not in the thread pool */
282 if (test_bit(FSCACHE_OP_WAITING
, &op
->op
.flags
)) {
284 fscache_stat(&fscache_n_retrieval_op_waits
);
285 wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
286 fscache_wait_bit
, TASK_UNINTERRUPTIBLE
);
290 /* ask the cache to honour the operation */
291 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
)) {
292 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
296 ret
= object
->cache
->ops
->read_or_alloc_page(op
, page
, gfp
);
300 fscache_stat(&fscache_n_retrievals_nomem
);
301 else if (ret
== -ERESTARTSYS
)
302 fscache_stat(&fscache_n_retrievals_intr
);
303 else if (ret
== -ENODATA
)
304 fscache_stat(&fscache_n_retrievals_nodata
);
306 fscache_stat(&fscache_n_retrievals_nobufs
);
308 fscache_stat(&fscache_n_retrievals_ok
);
310 fscache_put_retrieval(op
);
311 _leave(" = %d", ret
);
315 spin_unlock(&cookie
->lock
);
318 fscache_stat(&fscache_n_retrievals_nobufs
);
319 _leave(" = -ENOBUFS");
322 EXPORT_SYMBOL(__fscache_read_or_alloc_page
);
325 * read a list of page from the cache or allocate a block in which to store
328 * -ENOMEM - out of memory, some pages may be being read
329 * -ERESTARTSYS - interrupted, some pages may be being read
330 * -ENOBUFS - no backing object or space available in which to cache any
331 * pages not being read
332 * -ENODATA - no data available in the backing object for some or all of
334 * 0 - dispatched a read on all pages
336 * end_io_func() will be called for each page read from the cache as it is
337 * finishes being read
339 * any pages for which a read is dispatched will be removed from pages and
342 int __fscache_read_or_alloc_pages(struct fscache_cookie
*cookie
,
343 struct address_space
*mapping
,
344 struct list_head
*pages
,
346 fscache_rw_complete_t end_io_func
,
350 fscache_pages_retrieval_func_t func
;
351 struct fscache_retrieval
*op
;
352 struct fscache_object
*object
;
355 _enter("%p,,%d,,,", cookie
, *nr_pages
);
357 fscache_stat(&fscache_n_retrievals
);
359 if (hlist_empty(&cookie
->backing_objects
))
362 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
363 ASSERTCMP(*nr_pages
, >, 0);
364 ASSERT(!list_empty(pages
));
366 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
369 op
= fscache_alloc_retrieval(mapping
, end_io_func
, context
);
373 spin_lock(&cookie
->lock
);
375 if (hlist_empty(&cookie
->backing_objects
))
377 object
= hlist_entry(cookie
->backing_objects
.first
,
378 struct fscache_object
, cookie_link
);
380 if (fscache_submit_op(object
, &op
->op
) < 0)
382 spin_unlock(&cookie
->lock
);
384 fscache_stat(&fscache_n_retrieval_ops
);
386 /* pin the netfs read context in case we need to do the actual netfs
387 * read because we've encountered a cache read failure */
388 fscache_get_context(object
->cookie
, op
->context
);
390 /* we wait for the operation to become active, and then process it
391 * *here*, in this thread, and not in the thread pool */
392 if (test_bit(FSCACHE_OP_WAITING
, &op
->op
.flags
)) {
394 fscache_stat(&fscache_n_retrieval_op_waits
);
395 wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
396 fscache_wait_bit
, TASK_UNINTERRUPTIBLE
);
400 /* ask the cache to honour the operation */
401 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET
, &object
->cookie
->flags
))
402 func
= object
->cache
->ops
->allocate_pages
;
404 func
= object
->cache
->ops
->read_or_alloc_pages
;
405 ret
= func(op
, pages
, nr_pages
, gfp
);
408 fscache_stat(&fscache_n_retrievals_nomem
);
409 else if (ret
== -ERESTARTSYS
)
410 fscache_stat(&fscache_n_retrievals_intr
);
411 else if (ret
== -ENODATA
)
412 fscache_stat(&fscache_n_retrievals_nodata
);
414 fscache_stat(&fscache_n_retrievals_nobufs
);
416 fscache_stat(&fscache_n_retrievals_ok
);
418 fscache_put_retrieval(op
);
419 _leave(" = %d", ret
);
423 spin_unlock(&cookie
->lock
);
426 fscache_stat(&fscache_n_retrievals_nobufs
);
427 _leave(" = -ENOBUFS");
430 EXPORT_SYMBOL(__fscache_read_or_alloc_pages
);
433 * allocate a block in the cache on which to store a page
435 * -ENOMEM - out of memory, nothing done
436 * -ERESTARTSYS - interrupted
437 * -ENOBUFS - no backing object available in which to cache the block
438 * 0 - block allocated
440 int __fscache_alloc_page(struct fscache_cookie
*cookie
,
444 struct fscache_retrieval
*op
;
445 struct fscache_object
*object
;
448 _enter("%p,%p,,,", cookie
, page
);
450 fscache_stat(&fscache_n_allocs
);
452 if (hlist_empty(&cookie
->backing_objects
))
455 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
456 ASSERTCMP(page
, !=, NULL
);
458 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
461 op
= fscache_alloc_retrieval(page
->mapping
, NULL
, NULL
);
465 spin_lock(&cookie
->lock
);
467 if (hlist_empty(&cookie
->backing_objects
))
469 object
= hlist_entry(cookie
->backing_objects
.first
,
470 struct fscache_object
, cookie_link
);
472 if (fscache_submit_op(object
, &op
->op
) < 0)
474 spin_unlock(&cookie
->lock
);
476 fscache_stat(&fscache_n_alloc_ops
);
478 if (test_bit(FSCACHE_OP_WAITING
, &op
->op
.flags
)) {
480 fscache_stat(&fscache_n_alloc_op_waits
);
481 wait_on_bit(&op
->op
.flags
, FSCACHE_OP_WAITING
,
482 fscache_wait_bit
, TASK_UNINTERRUPTIBLE
);
486 /* ask the cache to honour the operation */
487 ret
= object
->cache
->ops
->allocate_page(op
, page
, gfp
);
490 fscache_stat(&fscache_n_allocs_nobufs
);
492 fscache_stat(&fscache_n_allocs_ok
);
494 fscache_put_retrieval(op
);
495 _leave(" = %d", ret
);
499 spin_unlock(&cookie
->lock
);
502 fscache_stat(&fscache_n_allocs_nobufs
);
503 _leave(" = -ENOBUFS");
506 EXPORT_SYMBOL(__fscache_alloc_page
);
509 * release a write op reference
511 static void fscache_release_write_op(struct fscache_operation
*_op
)
513 _enter("{OP%x}", _op
->debug_id
);
517 * perform the background storage of a page into the cache
519 static void fscache_write_op(struct fscache_operation
*_op
)
521 struct fscache_storage
*op
=
522 container_of(_op
, struct fscache_storage
, op
);
523 struct fscache_object
*object
= op
->op
.object
;
524 struct fscache_cookie
*cookie
= object
->cookie
;
530 _enter("{OP%x,%d}", op
->op
.debug_id
, atomic_read(&op
->op
.usage
));
532 spin_lock(&cookie
->lock
);
533 spin_lock(&object
->lock
);
535 if (!fscache_object_is_active(object
)) {
536 spin_unlock(&object
->lock
);
537 spin_unlock(&cookie
->lock
);
542 fscache_stat(&fscache_n_store_calls
);
544 /* find a page to store */
546 n
= radix_tree_gang_lookup_tag(&cookie
->stores
, results
, 0, 1,
547 FSCACHE_COOKIE_PENDING_TAG
);
551 _debug("gang %d [%lx]", n
, page
->index
);
552 if (page
->index
> op
->store_limit
)
555 radix_tree_tag_clear(&cookie
->stores
, page
->index
,
556 FSCACHE_COOKIE_PENDING_TAG
);
558 spin_unlock(&object
->lock
);
559 spin_unlock(&cookie
->lock
);
562 ret
= object
->cache
->ops
->write_page(op
, page
);
563 fscache_end_page_write(cookie
, page
);
564 page_cache_release(page
);
566 fscache_abort_object(object
);
568 fscache_enqueue_operation(&op
->op
);
575 /* this writer is going away and there aren't any more things to
578 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
579 spin_unlock(&object
->lock
);
580 spin_unlock(&cookie
->lock
);
585 * request a page be stored in the cache
587 * -ENOMEM - out of memory, nothing done
588 * -ENOBUFS - no backing object available in which to cache the page
589 * 0 - dispatched a write - it'll call end_io_func() when finished
591 * if the cookie still has a backing object at this point, that object can be
592 * in one of a few states with respect to storage processing:
594 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
597 * (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
600 * (b) writes deferred till post-creation (mark page for writing and
601 * return immediately)
603 * (2) negative lookup, object created, initial fill being made from netfs
604 * (FSCACHE_COOKIE_INITIAL_FILL is set)
606 * (a) fill point not yet reached this page (mark page for writing and
609 * (b) fill point passed this page (queue op to store this page)
611 * (3) object extant (queue op to store this page)
613 * any other state is invalid
615 int __fscache_write_page(struct fscache_cookie
*cookie
,
619 struct fscache_storage
*op
;
620 struct fscache_object
*object
;
623 _enter("%p,%x,", cookie
, (u32
) page
->flags
);
625 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
626 ASSERT(PageFsCache(page
));
628 fscache_stat(&fscache_n_stores
);
630 op
= kzalloc(sizeof(*op
), GFP_NOIO
);
634 fscache_operation_init(&op
->op
, fscache_release_write_op
);
635 fscache_operation_init_slow(&op
->op
, fscache_write_op
);
636 op
->op
.flags
= FSCACHE_OP_SLOW
| (1 << FSCACHE_OP_WAITING
);
638 ret
= radix_tree_preload(gfp
& ~__GFP_HIGHMEM
);
643 spin_lock(&cookie
->lock
);
645 if (hlist_empty(&cookie
->backing_objects
))
647 object
= hlist_entry(cookie
->backing_objects
.first
,
648 struct fscache_object
, cookie_link
);
649 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
652 /* add the page to the pending-storage radix tree on the backing
654 spin_lock(&object
->lock
);
656 _debug("store limit %llx", (unsigned long long) object
->store_limit
);
658 ret
= radix_tree_insert(&cookie
->stores
, page
->index
, page
);
662 _debug("insert failed %d", ret
);
663 goto nobufs_unlock_obj
;
666 radix_tree_tag_set(&cookie
->stores
, page
->index
,
667 FSCACHE_COOKIE_PENDING_TAG
);
668 page_cache_get(page
);
670 /* we only want one writer at a time, but we do need to queue new
671 * writers after exclusive ops */
672 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
))
673 goto already_pending
;
675 spin_unlock(&object
->lock
);
677 op
->op
.debug_id
= atomic_inc_return(&fscache_op_debug_id
);
678 op
->store_limit
= object
->store_limit
;
680 if (fscache_submit_op(object
, &op
->op
) < 0)
683 spin_unlock(&cookie
->lock
);
684 radix_tree_preload_end();
685 fscache_stat(&fscache_n_store_ops
);
686 fscache_stat(&fscache_n_stores_ok
);
688 /* the slow work queue now carries its own ref on the object */
689 fscache_put_operation(&op
->op
);
694 fscache_stat(&fscache_n_stores_again
);
696 spin_unlock(&object
->lock
);
697 spin_unlock(&cookie
->lock
);
698 radix_tree_preload_end();
700 fscache_stat(&fscache_n_stores_ok
);
705 radix_tree_delete(&cookie
->stores
, page
->index
);
706 page_cache_release(page
);
711 spin_unlock(&object
->lock
);
713 spin_unlock(&cookie
->lock
);
714 radix_tree_preload_end();
716 fscache_stat(&fscache_n_stores_nobufs
);
717 _leave(" = -ENOBUFS");
723 fscache_stat(&fscache_n_stores_oom
);
724 _leave(" = -ENOMEM");
727 EXPORT_SYMBOL(__fscache_write_page
);
730 * remove a page from the cache
732 void __fscache_uncache_page(struct fscache_cookie
*cookie
, struct page
*page
)
734 struct fscache_object
*object
;
738 ASSERTCMP(cookie
->def
->type
, !=, FSCACHE_COOKIE_TYPE_INDEX
);
739 ASSERTCMP(page
, !=, NULL
);
741 fscache_stat(&fscache_n_uncaches
);
743 /* cache withdrawal may beat us to it */
744 if (!PageFsCache(page
))
748 spin_lock(&cookie
->lock
);
750 if (hlist_empty(&cookie
->backing_objects
)) {
751 ClearPageFsCache(page
);
755 object
= hlist_entry(cookie
->backing_objects
.first
,
756 struct fscache_object
, cookie_link
);
758 /* there might now be stuff on disk we could read */
759 clear_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
761 /* only invoke the cache backend if we managed to mark the page
762 * uncached here; this deals with synchronisation vs withdrawal */
763 if (TestClearPageFsCache(page
) &&
764 object
->cache
->ops
->uncache_page
) {
765 /* the cache backend releases the cookie lock */
766 object
->cache
->ops
->uncache_page(object
, page
);
771 spin_unlock(&cookie
->lock
);
775 EXPORT_SYMBOL(__fscache_uncache_page
);
778 * fscache_mark_pages_cached - Mark pages as being cached
779 * @op: The retrieval op pages are being marked for
780 * @pagevec: The pages to be marked
782 * Mark a bunch of netfs pages as being cached. After this is called,
783 * the netfs must call fscache_uncache_page() to remove the mark.
785 void fscache_mark_pages_cached(struct fscache_retrieval
*op
,
786 struct pagevec
*pagevec
)
788 struct fscache_cookie
*cookie
= op
->op
.object
->cookie
;
791 #ifdef CONFIG_FSCACHE_STATS
792 atomic_add(pagevec
->nr
, &fscache_n_marks
);
795 for (loop
= 0; loop
< pagevec
->nr
; loop
++) {
796 struct page
*page
= pagevec
->pages
[loop
];
798 _debug("- mark %p{%lx}", page
, page
->index
);
799 if (TestSetPageFsCache(page
)) {
800 static bool once_only
;
803 printk(KERN_WARNING
"FS-Cache:"
804 " Cookie type %s marked page %lx"
806 cookie
->def
->name
, page
->index
);
811 if (cookie
->def
->mark_pages_cached
)
812 cookie
->def
->mark_pages_cached(cookie
->netfs_data
,
813 op
->mapping
, pagevec
);
814 pagevec_reinit(pagevec
);
816 EXPORT_SYMBOL(fscache_mark_pages_cached
);