Linux 3.12.5
[linux/fpc-iii.git] / fs / fscache / page.c
blob73899c1c34494555d73dd5714ecb21eb74c0296d
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>
18 #include "internal.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)
25 void *val;
27 rcu_read_lock();
28 val = radix_tree_lookup(&cookie->stores, page->index);
29 rcu_read_unlock();
31 return val != NULL;
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 * decide whether a page can be released, possibly by cancelling a store to it
48 * - we're allowed to sleep if __GFP_WAIT is flagged
50 bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
51 struct page *page,
52 gfp_t gfp)
54 struct page *xpage;
55 void *val;
57 _enter("%p,%p,%x", cookie, page, gfp);
59 try_again:
60 rcu_read_lock();
61 val = radix_tree_lookup(&cookie->stores, page->index);
62 if (!val) {
63 rcu_read_unlock();
64 fscache_stat(&fscache_n_store_vmscan_not_storing);
65 __fscache_uncache_page(cookie, page);
66 return true;
69 /* see if the page is actually undergoing storage - if so we can't get
70 * rid of it till the cache has finished with it */
71 if (radix_tree_tag_get(&cookie->stores, page->index,
72 FSCACHE_COOKIE_STORING_TAG)) {
73 rcu_read_unlock();
74 goto page_busy;
77 /* the page is pending storage, so we attempt to cancel the store and
78 * discard the store request so that the page can be reclaimed */
79 spin_lock(&cookie->stores_lock);
80 rcu_read_unlock();
82 if (radix_tree_tag_get(&cookie->stores, page->index,
83 FSCACHE_COOKIE_STORING_TAG)) {
84 /* the page started to undergo storage whilst we were looking,
85 * so now we can only wait or return */
86 spin_unlock(&cookie->stores_lock);
87 goto page_busy;
90 xpage = radix_tree_delete(&cookie->stores, page->index);
91 spin_unlock(&cookie->stores_lock);
93 if (xpage) {
94 fscache_stat(&fscache_n_store_vmscan_cancelled);
95 fscache_stat(&fscache_n_store_radix_deletes);
96 ASSERTCMP(xpage, ==, page);
97 } else {
98 fscache_stat(&fscache_n_store_vmscan_gone);
101 wake_up_bit(&cookie->flags, 0);
102 if (xpage)
103 page_cache_release(xpage);
104 __fscache_uncache_page(cookie, page);
105 return true;
107 page_busy:
108 /* We will wait here if we're allowed to, but that could deadlock the
109 * allocator as the work threads writing to the cache may all end up
110 * sleeping on memory allocation, so we may need to impose a timeout
111 * too. */
112 if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
113 fscache_stat(&fscache_n_store_vmscan_busy);
114 return false;
117 fscache_stat(&fscache_n_store_vmscan_wait);
118 __fscache_wait_on_page_write(cookie, page);
119 gfp &= ~__GFP_WAIT;
120 goto try_again;
122 EXPORT_SYMBOL(__fscache_maybe_release_page);
125 * note that a page has finished being written to the cache
127 static void fscache_end_page_write(struct fscache_object *object,
128 struct page *page)
130 struct fscache_cookie *cookie;
131 struct page *xpage = NULL;
133 spin_lock(&object->lock);
134 cookie = object->cookie;
135 if (cookie) {
136 /* delete the page from the tree if it is now no longer
137 * pending */
138 spin_lock(&cookie->stores_lock);
139 radix_tree_tag_clear(&cookie->stores, page->index,
140 FSCACHE_COOKIE_STORING_TAG);
141 if (!radix_tree_tag_get(&cookie->stores, page->index,
142 FSCACHE_COOKIE_PENDING_TAG)) {
143 fscache_stat(&fscache_n_store_radix_deletes);
144 xpage = radix_tree_delete(&cookie->stores, page->index);
146 spin_unlock(&cookie->stores_lock);
147 wake_up_bit(&cookie->flags, 0);
149 spin_unlock(&object->lock);
150 if (xpage)
151 page_cache_release(xpage);
155 * actually apply the changed attributes to a cache object
157 static void fscache_attr_changed_op(struct fscache_operation *op)
159 struct fscache_object *object = op->object;
160 int ret;
162 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
164 fscache_stat(&fscache_n_attr_changed_calls);
166 if (fscache_object_is_active(object) &&
167 fscache_use_cookie(object)) {
168 fscache_stat(&fscache_n_cop_attr_changed);
169 ret = object->cache->ops->attr_changed(object);
170 fscache_stat_d(&fscache_n_cop_attr_changed);
171 fscache_unuse_cookie(object);
172 if (ret < 0)
173 fscache_abort_object(object);
176 fscache_op_complete(op, true);
177 _leave("");
181 * notification that the attributes on an object have changed
183 int __fscache_attr_changed(struct fscache_cookie *cookie)
185 struct fscache_operation *op;
186 struct fscache_object *object;
188 _enter("%p", cookie);
190 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
192 fscache_stat(&fscache_n_attr_changed);
194 op = kzalloc(sizeof(*op), GFP_KERNEL);
195 if (!op) {
196 fscache_stat(&fscache_n_attr_changed_nomem);
197 _leave(" = -ENOMEM");
198 return -ENOMEM;
201 fscache_operation_init(op, fscache_attr_changed_op, NULL);
202 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
204 spin_lock(&cookie->lock);
206 if (hlist_empty(&cookie->backing_objects))
207 goto nobufs;
208 object = hlist_entry(cookie->backing_objects.first,
209 struct fscache_object, cookie_link);
211 if (fscache_submit_exclusive_op(object, op) < 0)
212 goto nobufs;
213 spin_unlock(&cookie->lock);
214 fscache_stat(&fscache_n_attr_changed_ok);
215 fscache_put_operation(op);
216 _leave(" = 0");
217 return 0;
219 nobufs:
220 spin_unlock(&cookie->lock);
221 kfree(op);
222 fscache_stat(&fscache_n_attr_changed_nobufs);
223 _leave(" = %d", -ENOBUFS);
224 return -ENOBUFS;
226 EXPORT_SYMBOL(__fscache_attr_changed);
229 * release a retrieval op reference
231 static void fscache_release_retrieval_op(struct fscache_operation *_op)
233 struct fscache_retrieval *op =
234 container_of(_op, struct fscache_retrieval, op);
236 _enter("{OP%x}", op->op.debug_id);
238 ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
240 fscache_hist(fscache_retrieval_histogram, op->start_time);
241 if (op->context)
242 fscache_put_context(op->op.object->cookie, op->context);
244 _leave("");
248 * allocate a retrieval op
250 static struct fscache_retrieval *fscache_alloc_retrieval(
251 struct fscache_cookie *cookie,
252 struct address_space *mapping,
253 fscache_rw_complete_t end_io_func,
254 void *context)
256 struct fscache_retrieval *op;
258 /* allocate a retrieval operation and attempt to submit it */
259 op = kzalloc(sizeof(*op), GFP_NOIO);
260 if (!op) {
261 fscache_stat(&fscache_n_retrievals_nomem);
262 return NULL;
265 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
266 atomic_inc(&cookie->n_active);
267 op->op.flags = FSCACHE_OP_MYTHREAD |
268 (1UL << FSCACHE_OP_WAITING) |
269 (1UL << FSCACHE_OP_UNUSE_COOKIE);
270 op->mapping = mapping;
271 op->end_io_func = end_io_func;
272 op->context = context;
273 op->start_time = jiffies;
274 INIT_LIST_HEAD(&op->to_do);
275 return op;
279 * wait for a deferred lookup to complete
281 int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
283 unsigned long jif;
285 _enter("");
287 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
288 _leave(" = 0 [imm]");
289 return 0;
292 fscache_stat(&fscache_n_retrievals_wait);
294 jif = jiffies;
295 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
296 fscache_wait_bit_interruptible,
297 TASK_INTERRUPTIBLE) != 0) {
298 fscache_stat(&fscache_n_retrievals_intr);
299 _leave(" = -ERESTARTSYS");
300 return -ERESTARTSYS;
303 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
305 smp_rmb();
306 fscache_hist(fscache_retrieval_delay_histogram, jif);
307 _leave(" = 0 [dly]");
308 return 0;
312 * Handle cancellation of a pending retrieval op
314 static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
316 struct fscache_retrieval *op =
317 container_of(_op, struct fscache_retrieval, op);
319 atomic_set(&op->n_pages, 0);
323 * wait for an object to become active (or dead)
325 int fscache_wait_for_operation_activation(struct fscache_object *object,
326 struct fscache_operation *op,
327 atomic_t *stat_op_waits,
328 atomic_t *stat_object_dead,
329 void (*do_cancel)(struct fscache_operation *))
331 int ret;
333 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
334 goto check_if_dead;
336 _debug(">>> WT");
337 if (stat_op_waits)
338 fscache_stat(stat_op_waits);
339 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
340 fscache_wait_bit_interruptible,
341 TASK_INTERRUPTIBLE) != 0) {
342 ret = fscache_cancel_op(op, do_cancel);
343 if (ret == 0)
344 return -ERESTARTSYS;
346 /* it's been removed from the pending queue by another party,
347 * so we should get to run shortly */
348 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
349 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
351 _debug("<<< GO");
353 check_if_dead:
354 if (op->state == FSCACHE_OP_ST_CANCELLED) {
355 if (stat_object_dead)
356 fscache_stat(stat_object_dead);
357 _leave(" = -ENOBUFS [cancelled]");
358 return -ENOBUFS;
360 if (unlikely(fscache_object_is_dead(object))) {
361 pr_err("%s() = -ENOBUFS [obj dead %d]\n", __func__, op->state);
362 fscache_cancel_op(op, do_cancel);
363 if (stat_object_dead)
364 fscache_stat(stat_object_dead);
365 return -ENOBUFS;
367 return 0;
371 * read a page from the cache or allocate a block in which to store it
372 * - we return:
373 * -ENOMEM - out of memory, nothing done
374 * -ERESTARTSYS - interrupted
375 * -ENOBUFS - no backing object available in which to cache the block
376 * -ENODATA - no data available in the backing object for this block
377 * 0 - dispatched a read - it'll call end_io_func() when finished
379 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
380 struct page *page,
381 fscache_rw_complete_t end_io_func,
382 void *context,
383 gfp_t gfp)
385 struct fscache_retrieval *op;
386 struct fscache_object *object;
387 int ret;
389 _enter("%p,%p,,,", cookie, page);
391 fscache_stat(&fscache_n_retrievals);
393 if (hlist_empty(&cookie->backing_objects))
394 goto nobufs;
396 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
397 _leave(" = -ENOBUFS [invalidating]");
398 return -ENOBUFS;
401 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
402 ASSERTCMP(page, !=, NULL);
404 if (fscache_wait_for_deferred_lookup(cookie) < 0)
405 return -ERESTARTSYS;
407 op = fscache_alloc_retrieval(cookie, page->mapping,
408 end_io_func,context);
409 if (!op) {
410 _leave(" = -ENOMEM");
411 return -ENOMEM;
413 atomic_set(&op->n_pages, 1);
415 spin_lock(&cookie->lock);
417 if (hlist_empty(&cookie->backing_objects))
418 goto nobufs_unlock;
419 object = hlist_entry(cookie->backing_objects.first,
420 struct fscache_object, cookie_link);
422 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
424 atomic_inc(&object->n_reads);
425 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
427 if (fscache_submit_op(object, &op->op) < 0)
428 goto nobufs_unlock_dec;
429 spin_unlock(&cookie->lock);
431 fscache_stat(&fscache_n_retrieval_ops);
433 /* pin the netfs read context in case we need to do the actual netfs
434 * read because we've encountered a cache read failure */
435 fscache_get_context(object->cookie, op->context);
437 /* we wait for the operation to become active, and then process it
438 * *here*, in this thread, and not in the thread pool */
439 ret = fscache_wait_for_operation_activation(
440 object, &op->op,
441 __fscache_stat(&fscache_n_retrieval_op_waits),
442 __fscache_stat(&fscache_n_retrievals_object_dead),
443 fscache_do_cancel_retrieval);
444 if (ret < 0)
445 goto error;
447 /* ask the cache to honour the operation */
448 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
449 fscache_stat(&fscache_n_cop_allocate_page);
450 ret = object->cache->ops->allocate_page(op, page, gfp);
451 fscache_stat_d(&fscache_n_cop_allocate_page);
452 if (ret == 0)
453 ret = -ENODATA;
454 } else {
455 fscache_stat(&fscache_n_cop_read_or_alloc_page);
456 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
457 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
460 error:
461 if (ret == -ENOMEM)
462 fscache_stat(&fscache_n_retrievals_nomem);
463 else if (ret == -ERESTARTSYS)
464 fscache_stat(&fscache_n_retrievals_intr);
465 else if (ret == -ENODATA)
466 fscache_stat(&fscache_n_retrievals_nodata);
467 else if (ret < 0)
468 fscache_stat(&fscache_n_retrievals_nobufs);
469 else
470 fscache_stat(&fscache_n_retrievals_ok);
472 fscache_put_retrieval(op);
473 _leave(" = %d", ret);
474 return ret;
476 nobufs_unlock_dec:
477 atomic_dec(&object->n_reads);
478 nobufs_unlock:
479 spin_unlock(&cookie->lock);
480 atomic_dec(&cookie->n_active);
481 kfree(op);
482 nobufs:
483 fscache_stat(&fscache_n_retrievals_nobufs);
484 _leave(" = -ENOBUFS");
485 return -ENOBUFS;
487 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
490 * read a list of page from the cache or allocate a block in which to store
491 * them
492 * - we return:
493 * -ENOMEM - out of memory, some pages may be being read
494 * -ERESTARTSYS - interrupted, some pages may be being read
495 * -ENOBUFS - no backing object or space available in which to cache any
496 * pages not being read
497 * -ENODATA - no data available in the backing object for some or all of
498 * the pages
499 * 0 - dispatched a read on all pages
501 * end_io_func() will be called for each page read from the cache as it is
502 * finishes being read
504 * any pages for which a read is dispatched will be removed from pages and
505 * nr_pages
507 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
508 struct address_space *mapping,
509 struct list_head *pages,
510 unsigned *nr_pages,
511 fscache_rw_complete_t end_io_func,
512 void *context,
513 gfp_t gfp)
515 struct fscache_retrieval *op;
516 struct fscache_object *object;
517 int ret;
519 _enter("%p,,%d,,,", cookie, *nr_pages);
521 fscache_stat(&fscache_n_retrievals);
523 if (hlist_empty(&cookie->backing_objects))
524 goto nobufs;
526 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
527 _leave(" = -ENOBUFS [invalidating]");
528 return -ENOBUFS;
531 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
532 ASSERTCMP(*nr_pages, >, 0);
533 ASSERT(!list_empty(pages));
535 if (fscache_wait_for_deferred_lookup(cookie) < 0)
536 return -ERESTARTSYS;
538 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
539 if (!op)
540 return -ENOMEM;
541 atomic_set(&op->n_pages, *nr_pages);
543 spin_lock(&cookie->lock);
545 if (hlist_empty(&cookie->backing_objects))
546 goto nobufs_unlock;
547 object = hlist_entry(cookie->backing_objects.first,
548 struct fscache_object, cookie_link);
550 atomic_inc(&object->n_reads);
551 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
553 if (fscache_submit_op(object, &op->op) < 0)
554 goto nobufs_unlock_dec;
555 spin_unlock(&cookie->lock);
557 fscache_stat(&fscache_n_retrieval_ops);
559 /* pin the netfs read context in case we need to do the actual netfs
560 * read because we've encountered a cache read failure */
561 fscache_get_context(object->cookie, op->context);
563 /* we wait for the operation to become active, and then process it
564 * *here*, in this thread, and not in the thread pool */
565 ret = fscache_wait_for_operation_activation(
566 object, &op->op,
567 __fscache_stat(&fscache_n_retrieval_op_waits),
568 __fscache_stat(&fscache_n_retrievals_object_dead),
569 fscache_do_cancel_retrieval);
570 if (ret < 0)
571 goto error;
573 /* ask the cache to honour the operation */
574 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
575 fscache_stat(&fscache_n_cop_allocate_pages);
576 ret = object->cache->ops->allocate_pages(
577 op, pages, nr_pages, gfp);
578 fscache_stat_d(&fscache_n_cop_allocate_pages);
579 } else {
580 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
581 ret = object->cache->ops->read_or_alloc_pages(
582 op, pages, nr_pages, gfp);
583 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
586 error:
587 if (ret == -ENOMEM)
588 fscache_stat(&fscache_n_retrievals_nomem);
589 else if (ret == -ERESTARTSYS)
590 fscache_stat(&fscache_n_retrievals_intr);
591 else if (ret == -ENODATA)
592 fscache_stat(&fscache_n_retrievals_nodata);
593 else if (ret < 0)
594 fscache_stat(&fscache_n_retrievals_nobufs);
595 else
596 fscache_stat(&fscache_n_retrievals_ok);
598 fscache_put_retrieval(op);
599 _leave(" = %d", ret);
600 return ret;
602 nobufs_unlock_dec:
603 atomic_dec(&object->n_reads);
604 nobufs_unlock:
605 spin_unlock(&cookie->lock);
606 atomic_dec(&cookie->n_active);
607 kfree(op);
608 nobufs:
609 fscache_stat(&fscache_n_retrievals_nobufs);
610 _leave(" = -ENOBUFS");
611 return -ENOBUFS;
613 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
616 * allocate a block in the cache on which to store a page
617 * - we return:
618 * -ENOMEM - out of memory, nothing done
619 * -ERESTARTSYS - interrupted
620 * -ENOBUFS - no backing object available in which to cache the block
621 * 0 - block allocated
623 int __fscache_alloc_page(struct fscache_cookie *cookie,
624 struct page *page,
625 gfp_t gfp)
627 struct fscache_retrieval *op;
628 struct fscache_object *object;
629 int ret;
631 _enter("%p,%p,,,", cookie, page);
633 fscache_stat(&fscache_n_allocs);
635 if (hlist_empty(&cookie->backing_objects))
636 goto nobufs;
638 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
639 ASSERTCMP(page, !=, NULL);
641 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
642 _leave(" = -ENOBUFS [invalidating]");
643 return -ENOBUFS;
646 if (fscache_wait_for_deferred_lookup(cookie) < 0)
647 return -ERESTARTSYS;
649 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
650 if (!op)
651 return -ENOMEM;
652 atomic_set(&op->n_pages, 1);
654 spin_lock(&cookie->lock);
656 if (hlist_empty(&cookie->backing_objects))
657 goto nobufs_unlock;
658 object = hlist_entry(cookie->backing_objects.first,
659 struct fscache_object, cookie_link);
661 if (fscache_submit_op(object, &op->op) < 0)
662 goto nobufs_unlock;
663 spin_unlock(&cookie->lock);
665 fscache_stat(&fscache_n_alloc_ops);
667 ret = fscache_wait_for_operation_activation(
668 object, &op->op,
669 __fscache_stat(&fscache_n_alloc_op_waits),
670 __fscache_stat(&fscache_n_allocs_object_dead),
671 fscache_do_cancel_retrieval);
672 if (ret < 0)
673 goto error;
675 /* ask the cache to honour the operation */
676 fscache_stat(&fscache_n_cop_allocate_page);
677 ret = object->cache->ops->allocate_page(op, page, gfp);
678 fscache_stat_d(&fscache_n_cop_allocate_page);
680 error:
681 if (ret == -ERESTARTSYS)
682 fscache_stat(&fscache_n_allocs_intr);
683 else if (ret < 0)
684 fscache_stat(&fscache_n_allocs_nobufs);
685 else
686 fscache_stat(&fscache_n_allocs_ok);
688 fscache_put_retrieval(op);
689 _leave(" = %d", ret);
690 return ret;
692 nobufs_unlock:
693 spin_unlock(&cookie->lock);
694 atomic_dec(&cookie->n_active);
695 kfree(op);
696 nobufs:
697 fscache_stat(&fscache_n_allocs_nobufs);
698 _leave(" = -ENOBUFS");
699 return -ENOBUFS;
701 EXPORT_SYMBOL(__fscache_alloc_page);
704 * Unmark pages allocate in the readahead code path (via:
705 * fscache_readpages_or_alloc) after delegating to the base filesystem
707 void __fscache_readpages_cancel(struct fscache_cookie *cookie,
708 struct list_head *pages)
710 struct page *page;
712 list_for_each_entry(page, pages, lru) {
713 if (PageFsCache(page))
714 __fscache_uncache_page(cookie, page);
717 EXPORT_SYMBOL(__fscache_readpages_cancel);
720 * release a write op reference
722 static void fscache_release_write_op(struct fscache_operation *_op)
724 _enter("{OP%x}", _op->debug_id);
728 * perform the background storage of a page into the cache
730 static void fscache_write_op(struct fscache_operation *_op)
732 struct fscache_storage *op =
733 container_of(_op, struct fscache_storage, op);
734 struct fscache_object *object = op->op.object;
735 struct fscache_cookie *cookie;
736 struct page *page;
737 unsigned n;
738 void *results[1];
739 int ret;
741 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
743 spin_lock(&object->lock);
744 cookie = object->cookie;
746 if (!fscache_object_is_active(object)) {
747 /* If we get here, then the on-disk cache object likely longer
748 * exists, so we should just cancel this write operation.
750 spin_unlock(&object->lock);
751 fscache_op_complete(&op->op, false);
752 _leave(" [inactive]");
753 return;
756 if (!cookie) {
757 /* If we get here, then the cookie belonging to the object was
758 * detached, probably by the cookie being withdrawn due to
759 * memory pressure, which means that the pages we might write
760 * to the cache from no longer exist - therefore, we can just
761 * cancel this write operation.
763 spin_unlock(&object->lock);
764 fscache_op_complete(&op->op, false);
765 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
766 _op->flags, _op->state, object->state->short_name,
767 object->flags);
768 return;
771 spin_lock(&cookie->stores_lock);
773 fscache_stat(&fscache_n_store_calls);
775 /* find a page to store */
776 page = NULL;
777 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
778 FSCACHE_COOKIE_PENDING_TAG);
779 if (n != 1)
780 goto superseded;
781 page = results[0];
782 _debug("gang %d [%lx]", n, page->index);
783 if (page->index > op->store_limit) {
784 fscache_stat(&fscache_n_store_pages_over_limit);
785 goto superseded;
788 radix_tree_tag_set(&cookie->stores, page->index,
789 FSCACHE_COOKIE_STORING_TAG);
790 radix_tree_tag_clear(&cookie->stores, page->index,
791 FSCACHE_COOKIE_PENDING_TAG);
793 spin_unlock(&cookie->stores_lock);
794 spin_unlock(&object->lock);
796 fscache_stat(&fscache_n_store_pages);
797 fscache_stat(&fscache_n_cop_write_page);
798 ret = object->cache->ops->write_page(op, page);
799 fscache_stat_d(&fscache_n_cop_write_page);
800 fscache_end_page_write(object, page);
801 if (ret < 0) {
802 fscache_abort_object(object);
803 fscache_op_complete(&op->op, true);
804 } else {
805 fscache_enqueue_operation(&op->op);
808 _leave("");
809 return;
811 superseded:
812 /* this writer is going away and there aren't any more things to
813 * write */
814 _debug("cease");
815 spin_unlock(&cookie->stores_lock);
816 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
817 spin_unlock(&object->lock);
818 fscache_op_complete(&op->op, true);
819 _leave("");
823 * Clear the pages pending writing for invalidation
825 void fscache_invalidate_writes(struct fscache_cookie *cookie)
827 struct page *page;
828 void *results[16];
829 int n, i;
831 _enter("");
833 for (;;) {
834 spin_lock(&cookie->stores_lock);
835 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
836 ARRAY_SIZE(results),
837 FSCACHE_COOKIE_PENDING_TAG);
838 if (n == 0) {
839 spin_unlock(&cookie->stores_lock);
840 break;
843 for (i = n - 1; i >= 0; i--) {
844 page = results[i];
845 radix_tree_delete(&cookie->stores, page->index);
848 spin_unlock(&cookie->stores_lock);
850 for (i = n - 1; i >= 0; i--)
851 page_cache_release(results[i]);
854 _leave("");
858 * request a page be stored in the cache
859 * - returns:
860 * -ENOMEM - out of memory, nothing done
861 * -ENOBUFS - no backing object available in which to cache the page
862 * 0 - dispatched a write - it'll call end_io_func() when finished
864 * if the cookie still has a backing object at this point, that object can be
865 * in one of a few states with respect to storage processing:
867 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
868 * set)
870 * (a) no writes yet
872 * (b) writes deferred till post-creation (mark page for writing and
873 * return immediately)
875 * (2) negative lookup, object created, initial fill being made from netfs
877 * (a) fill point not yet reached this page (mark page for writing and
878 * return)
880 * (b) fill point passed this page (queue op to store this page)
882 * (3) object extant (queue op to store this page)
884 * any other state is invalid
886 int __fscache_write_page(struct fscache_cookie *cookie,
887 struct page *page,
888 gfp_t gfp)
890 struct fscache_storage *op;
891 struct fscache_object *object;
892 int ret;
894 _enter("%p,%x,", cookie, (u32) page->flags);
896 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
897 ASSERT(PageFsCache(page));
899 fscache_stat(&fscache_n_stores);
901 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
902 _leave(" = -ENOBUFS [invalidating]");
903 return -ENOBUFS;
906 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
907 if (!op)
908 goto nomem;
910 fscache_operation_init(&op->op, fscache_write_op,
911 fscache_release_write_op);
912 op->op.flags = FSCACHE_OP_ASYNC |
913 (1 << FSCACHE_OP_WAITING) |
914 (1 << FSCACHE_OP_UNUSE_COOKIE);
916 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
917 if (ret < 0)
918 goto nomem_free;
920 ret = -ENOBUFS;
921 spin_lock(&cookie->lock);
923 if (hlist_empty(&cookie->backing_objects))
924 goto nobufs;
925 object = hlist_entry(cookie->backing_objects.first,
926 struct fscache_object, cookie_link);
927 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
928 goto nobufs;
930 /* add the page to the pending-storage radix tree on the backing
931 * object */
932 spin_lock(&object->lock);
933 spin_lock(&cookie->stores_lock);
935 _debug("store limit %llx", (unsigned long long) object->store_limit);
937 ret = radix_tree_insert(&cookie->stores, page->index, page);
938 if (ret < 0) {
939 if (ret == -EEXIST)
940 goto already_queued;
941 _debug("insert failed %d", ret);
942 goto nobufs_unlock_obj;
945 radix_tree_tag_set(&cookie->stores, page->index,
946 FSCACHE_COOKIE_PENDING_TAG);
947 page_cache_get(page);
949 /* we only want one writer at a time, but we do need to queue new
950 * writers after exclusive ops */
951 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
952 goto already_pending;
954 spin_unlock(&cookie->stores_lock);
955 spin_unlock(&object->lock);
957 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
958 op->store_limit = object->store_limit;
960 atomic_inc(&cookie->n_active);
961 if (fscache_submit_op(object, &op->op) < 0)
962 goto submit_failed;
964 spin_unlock(&cookie->lock);
965 radix_tree_preload_end();
966 fscache_stat(&fscache_n_store_ops);
967 fscache_stat(&fscache_n_stores_ok);
969 /* the work queue now carries its own ref on the object */
970 fscache_put_operation(&op->op);
971 _leave(" = 0");
972 return 0;
974 already_queued:
975 fscache_stat(&fscache_n_stores_again);
976 already_pending:
977 spin_unlock(&cookie->stores_lock);
978 spin_unlock(&object->lock);
979 spin_unlock(&cookie->lock);
980 radix_tree_preload_end();
981 kfree(op);
982 fscache_stat(&fscache_n_stores_ok);
983 _leave(" = 0");
984 return 0;
986 submit_failed:
987 atomic_dec(&cookie->n_active);
988 spin_lock(&cookie->stores_lock);
989 radix_tree_delete(&cookie->stores, page->index);
990 spin_unlock(&cookie->stores_lock);
991 page_cache_release(page);
992 ret = -ENOBUFS;
993 goto nobufs;
995 nobufs_unlock_obj:
996 spin_unlock(&cookie->stores_lock);
997 spin_unlock(&object->lock);
998 nobufs:
999 spin_unlock(&cookie->lock);
1000 radix_tree_preload_end();
1001 kfree(op);
1002 fscache_stat(&fscache_n_stores_nobufs);
1003 _leave(" = -ENOBUFS");
1004 return -ENOBUFS;
1006 nomem_free:
1007 kfree(op);
1008 nomem:
1009 fscache_stat(&fscache_n_stores_oom);
1010 _leave(" = -ENOMEM");
1011 return -ENOMEM;
1013 EXPORT_SYMBOL(__fscache_write_page);
1016 * remove a page from the cache
1018 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1020 struct fscache_object *object;
1022 _enter(",%p", page);
1024 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1025 ASSERTCMP(page, !=, NULL);
1027 fscache_stat(&fscache_n_uncaches);
1029 /* cache withdrawal may beat us to it */
1030 if (!PageFsCache(page))
1031 goto done;
1033 /* get the object */
1034 spin_lock(&cookie->lock);
1036 if (hlist_empty(&cookie->backing_objects)) {
1037 ClearPageFsCache(page);
1038 goto done_unlock;
1041 object = hlist_entry(cookie->backing_objects.first,
1042 struct fscache_object, cookie_link);
1044 /* there might now be stuff on disk we could read */
1045 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1047 /* only invoke the cache backend if we managed to mark the page
1048 * uncached here; this deals with synchronisation vs withdrawal */
1049 if (TestClearPageFsCache(page) &&
1050 object->cache->ops->uncache_page) {
1051 /* the cache backend releases the cookie lock */
1052 fscache_stat(&fscache_n_cop_uncache_page);
1053 object->cache->ops->uncache_page(object, page);
1054 fscache_stat_d(&fscache_n_cop_uncache_page);
1055 goto done;
1058 done_unlock:
1059 spin_unlock(&cookie->lock);
1060 done:
1061 _leave("");
1063 EXPORT_SYMBOL(__fscache_uncache_page);
1066 * fscache_mark_page_cached - Mark a page as being cached
1067 * @op: The retrieval op pages are being marked for
1068 * @page: The page to be marked
1070 * Mark a netfs page as being cached. After this is called, the netfs
1071 * must call fscache_uncache_page() to remove the mark.
1073 void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1075 struct fscache_cookie *cookie = op->op.object->cookie;
1077 #ifdef CONFIG_FSCACHE_STATS
1078 atomic_inc(&fscache_n_marks);
1079 #endif
1081 _debug("- mark %p{%lx}", page, page->index);
1082 if (TestSetPageFsCache(page)) {
1083 static bool once_only;
1084 if (!once_only) {
1085 once_only = true;
1086 printk(KERN_WARNING "FS-Cache:"
1087 " Cookie type %s marked page %lx"
1088 " multiple times\n",
1089 cookie->def->name, page->index);
1093 if (cookie->def->mark_page_cached)
1094 cookie->def->mark_page_cached(cookie->netfs_data,
1095 op->mapping, page);
1097 EXPORT_SYMBOL(fscache_mark_page_cached);
1100 * fscache_mark_pages_cached - Mark pages as being cached
1101 * @op: The retrieval op pages are being marked for
1102 * @pagevec: The pages to be marked
1104 * Mark a bunch of netfs pages as being cached. After this is called,
1105 * the netfs must call fscache_uncache_page() to remove the mark.
1107 void fscache_mark_pages_cached(struct fscache_retrieval *op,
1108 struct pagevec *pagevec)
1110 unsigned long loop;
1112 for (loop = 0; loop < pagevec->nr; loop++)
1113 fscache_mark_page_cached(op, pagevec->pages[loop]);
1115 pagevec_reinit(pagevec);
1117 EXPORT_SYMBOL(fscache_mark_pages_cached);
1120 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1121 * to be associated with the given cookie.
1123 void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1124 struct inode *inode)
1126 struct address_space *mapping = inode->i_mapping;
1127 struct pagevec pvec;
1128 pgoff_t next;
1129 int i;
1131 _enter("%p,%p", cookie, inode);
1133 if (!mapping || mapping->nrpages == 0) {
1134 _leave(" [no pages]");
1135 return;
1138 pagevec_init(&pvec, 0);
1139 next = 0;
1140 do {
1141 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1142 break;
1143 for (i = 0; i < pagevec_count(&pvec); i++) {
1144 struct page *page = pvec.pages[i];
1145 next = page->index;
1146 if (PageFsCache(page)) {
1147 __fscache_wait_on_page_write(cookie, page);
1148 __fscache_uncache_page(cookie, page);
1151 pagevec_release(&pvec);
1152 cond_resched();
1153 } while (++next);
1155 _leave("");
1157 EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);