]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/fscache/page.c
FS-Cache: Fix heading in documentation
[mirror_ubuntu-eoan-kernel.git] / fs / fscache / page.c
CommitLineData
b5108822
DH
1/* Cache page management and data I/O routines
2 *
3 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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>
5a0e3ad6 17#include <linux/slab.h>
b5108822
DH
18#include "internal.h"
19
20/*
21 * check to see if a page is being written to the cache
22 */
23bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
24{
25 void *val;
26
27 rcu_read_lock();
28 val = radix_tree_lookup(&cookie->stores, page->index);
29 rcu_read_unlock();
30
31 return val != NULL;
32}
33EXPORT_SYMBOL(__fscache_check_page_write);
34
35/*
36 * wait for a page to finish being written to the cache
37 */
38void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
39{
40 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
41
42 wait_event(*wq, !__fscache_check_page_write(cookie, page));
43}
44EXPORT_SYMBOL(__fscache_wait_on_page_write);
45
201a1542
DH
46/*
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
49 */
50bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
51 struct page *page,
52 gfp_t gfp)
53{
54 struct page *xpage;
55 void *val;
56
57 _enter("%p,%p,%x", cookie, page, gfp);
58
8c209ce7 59try_again:
201a1542
DH
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;
67 }
68
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;
75 }
76
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();
81
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;
88 }
89
90 xpage = radix_tree_delete(&cookie->stores, page->index);
91 spin_unlock(&cookie->stores_lock);
92
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);
99 }
100
101 wake_up_bit(&cookie->flags, 0);
102 if (xpage)
103 page_cache_release(xpage);
104 __fscache_uncache_page(cookie, page);
105 return true;
106
107page_busy:
8c209ce7
DH
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. */
0c59a95d 112 if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
8c209ce7
DH
113 fscache_stat(&fscache_n_store_vmscan_busy);
114 return false;
115 }
116
117 fscache_stat(&fscache_n_store_vmscan_wait);
118 __fscache_wait_on_page_write(cookie, page);
119 gfp &= ~__GFP_WAIT;
120 goto try_again;
201a1542
DH
121}
122EXPORT_SYMBOL(__fscache_maybe_release_page);
123
b5108822
DH
124/*
125 * note that a page has finished being written to the cache
126 */
1bccf513
DH
127static void fscache_end_page_write(struct fscache_object *object,
128 struct page *page)
b5108822 129{
1bccf513
DH
130 struct fscache_cookie *cookie;
131 struct page *xpage = NULL;
b5108822 132
1bccf513
DH
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);
201a1542
DH
139 radix_tree_tag_clear(&cookie->stores, page->index,
140 FSCACHE_COOKIE_STORING_TAG);
285e728b
DH
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);
145 }
1bccf513
DH
146 spin_unlock(&cookie->stores_lock);
147 wake_up_bit(&cookie->flags, 0);
148 }
149 spin_unlock(&object->lock);
150 if (xpage)
151 page_cache_release(xpage);
b5108822
DH
152}
153
154/*
155 * actually apply the changed attributes to a cache object
156 */
157static void fscache_attr_changed_op(struct fscache_operation *op)
158{
159 struct fscache_object *object = op->object;
440f0aff 160 int ret;
b5108822
DH
161
162 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
163
164 fscache_stat(&fscache_n_attr_changed_calls);
165
1362729b
DH
166 if (fscache_object_is_active(object) &&
167 fscache_use_cookie(object)) {
52bd75fd 168 fscache_stat(&fscache_n_cop_attr_changed);
440f0aff 169 ret = object->cache->ops->attr_changed(object);
52bd75fd 170 fscache_stat_d(&fscache_n_cop_attr_changed);
1362729b 171 fscache_unuse_cookie(object);
440f0aff
DH
172 if (ret < 0)
173 fscache_abort_object(object);
174 }
b5108822 175
1f372dff 176 fscache_op_complete(op, true);
b5108822
DH
177 _leave("");
178}
179
180/*
181 * notification that the attributes on an object have changed
182 */
183int __fscache_attr_changed(struct fscache_cookie *cookie)
184{
185 struct fscache_operation *op;
186 struct fscache_object *object;
187
188 _enter("%p", cookie);
189
190 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
191
192 fscache_stat(&fscache_n_attr_changed);
193
194 op = kzalloc(sizeof(*op), GFP_KERNEL);
195 if (!op) {
196 fscache_stat(&fscache_n_attr_changed_nomem);
197 _leave(" = -ENOMEM");
198 return -ENOMEM;
199 }
200
8af7c124
TH
201 fscache_operation_init(op, fscache_attr_changed_op, NULL);
202 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
b5108822
DH
203
204 spin_lock(&cookie->lock);
205
206 if (hlist_empty(&cookie->backing_objects))
207 goto nobufs;
208 object = hlist_entry(cookie->backing_objects.first,
209 struct fscache_object, cookie_link);
210
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;
218
219nobufs:
220 spin_unlock(&cookie->lock);
221 kfree(op);
222 fscache_stat(&fscache_n_attr_changed_nobufs);
223 _leave(" = %d", -ENOBUFS);
224 return -ENOBUFS;
225}
226EXPORT_SYMBOL(__fscache_attr_changed);
227
b5108822
DH
228/*
229 * release a retrieval op reference
230 */
231static void fscache_release_retrieval_op(struct fscache_operation *_op)
232{
233 struct fscache_retrieval *op =
234 container_of(_op, struct fscache_retrieval, op);
235
236 _enter("{OP%x}", op->op.debug_id);
237
1bb4b7f9 238 ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
9f10523f 239
b5108822
DH
240 fscache_hist(fscache_retrieval_histogram, op->start_time);
241 if (op->context)
242 fscache_put_context(op->op.object->cookie, op->context);
243
244 _leave("");
245}
246
247/*
248 * allocate a retrieval op
249 */
250static struct fscache_retrieval *fscache_alloc_retrieval(
1362729b 251 struct fscache_cookie *cookie,
b5108822
DH
252 struct address_space *mapping,
253 fscache_rw_complete_t end_io_func,
254 void *context)
255{
256 struct fscache_retrieval *op;
257
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;
263 }
264
8af7c124 265 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
1362729b
DH
266 atomic_inc(&cookie->n_active);
267 op->op.flags = FSCACHE_OP_MYTHREAD |
268 (1UL << FSCACHE_OP_WAITING) |
269 (1UL << FSCACHE_OP_UNUSE_COOKIE);
b5108822
DH
270 op->mapping = mapping;
271 op->end_io_func = end_io_func;
272 op->context = context;
273 op->start_time = jiffies;
b5108822
DH
274 INIT_LIST_HEAD(&op->to_do);
275 return op;
276}
277
278/*
279 * wait for a deferred lookup to complete
280 */
da9803bc 281int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
b5108822
DH
282{
283 unsigned long jif;
284
285 _enter("");
286
287 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
288 _leave(" = 0 [imm]");
289 return 0;
290 }
291
292 fscache_stat(&fscache_n_retrievals_wait);
293
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;
301 }
302
303 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
304
305 smp_rmb();
306 fscache_hist(fscache_retrieval_delay_histogram, jif);
307 _leave(" = 0 [dly]");
308 return 0;
309}
310
91c7fbbf
DH
311/*
312 * Handle cancellation of a pending retrieval op
313 */
314static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
315{
316 struct fscache_retrieval *op =
317 container_of(_op, struct fscache_retrieval, op);
318
1bb4b7f9 319 atomic_set(&op->n_pages, 0);
91c7fbbf
DH
320}
321
60d543ca
DH
322/*
323 * wait for an object to become active (or dead)
324 */
da9803bc
DH
325int 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 *))
60d543ca
DH
330{
331 int ret;
332
da9803bc 333 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
60d543ca
DH
334 goto check_if_dead;
335
336 _debug(">>> WT");
da9803bc
DH
337 if (stat_op_waits)
338 fscache_stat(stat_op_waits);
339 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
60d543ca 340 fscache_wait_bit_interruptible,
9c04caa8 341 TASK_INTERRUPTIBLE) != 0) {
da9803bc 342 ret = fscache_cancel_op(op, do_cancel);
60d543ca
DH
343 if (ret == 0)
344 return -ERESTARTSYS;
345
346 /* it's been removed from the pending queue by another party,
347 * so we should get to run shortly */
da9803bc 348 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
60d543ca
DH
349 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
350 }
351 _debug("<<< GO");
352
353check_if_dead:
da9803bc
DH
354 if (op->state == FSCACHE_OP_ST_CANCELLED) {
355 if (stat_object_dead)
356 fscache_stat(stat_object_dead);
9f10523f
DH
357 _leave(" = -ENOBUFS [cancelled]");
358 return -ENOBUFS;
359 }
60d543ca 360 if (unlikely(fscache_object_is_dead(object))) {
da9803bc
DH
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);
60d543ca
DH
365 return -ENOBUFS;
366 }
367 return 0;
368}
369
b5108822
DH
370/*
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
378 */
379int __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)
384{
385 struct fscache_retrieval *op;
386 struct fscache_object *object;
387 int ret;
388
389 _enter("%p,%p,,,", cookie, page);
390
391 fscache_stat(&fscache_n_retrievals);
392
393 if (hlist_empty(&cookie->backing_objects))
394 goto nobufs;
395
ef778e7a
DH
396 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
397 _leave(" = -ENOBUFS [invalidating]");
398 return -ENOBUFS;
399 }
400
b5108822
DH
401 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
402 ASSERTCMP(page, !=, NULL);
403
404 if (fscache_wait_for_deferred_lookup(cookie) < 0)
405 return -ERESTARTSYS;
406
1362729b
DH
407 op = fscache_alloc_retrieval(cookie, page->mapping,
408 end_io_func,context);
b5108822
DH
409 if (!op) {
410 _leave(" = -ENOMEM");
411 return -ENOMEM;
412 }
1bb4b7f9 413 atomic_set(&op->n_pages, 1);
b5108822
DH
414
415 spin_lock(&cookie->lock);
416
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);
421
caaef690 422 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
b5108822 423
4fbf4291 424 atomic_inc(&object->n_reads);
9f10523f 425 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
4fbf4291 426
b5108822 427 if (fscache_submit_op(object, &op->op) < 0)
9f10523f 428 goto nobufs_unlock_dec;
b5108822
DH
429 spin_unlock(&cookie->lock);
430
431 fscache_stat(&fscache_n_retrieval_ops);
432
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);
436
437 /* we wait for the operation to become active, and then process it
438 * *here*, in this thread, and not in the thread pool */
da9803bc
DH
439 ret = fscache_wait_for_operation_activation(
440 object, &op->op,
60d543ca 441 __fscache_stat(&fscache_n_retrieval_op_waits),
da9803bc
DH
442 __fscache_stat(&fscache_n_retrievals_object_dead),
443 fscache_do_cancel_retrieval);
60d543ca
DH
444 if (ret < 0)
445 goto error;
b5108822
DH
446
447 /* ask the cache to honour the operation */
448 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
52bd75fd 449 fscache_stat(&fscache_n_cop_allocate_page);
b5108822 450 ret = object->cache->ops->allocate_page(op, page, gfp);
52bd75fd 451 fscache_stat_d(&fscache_n_cop_allocate_page);
b5108822
DH
452 if (ret == 0)
453 ret = -ENODATA;
454 } else {
52bd75fd 455 fscache_stat(&fscache_n_cop_read_or_alloc_page);
b5108822 456 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
52bd75fd 457 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
b5108822
DH
458 }
459
5753c441 460error:
b5108822
DH
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);
471
472 fscache_put_retrieval(op);
473 _leave(" = %d", ret);
474 return ret;
475
9f10523f
DH
476nobufs_unlock_dec:
477 atomic_dec(&object->n_reads);
b5108822
DH
478nobufs_unlock:
479 spin_unlock(&cookie->lock);
1362729b 480 atomic_dec(&cookie->n_active);
b5108822
DH
481 kfree(op);
482nobufs:
483 fscache_stat(&fscache_n_retrievals_nobufs);
484 _leave(" = -ENOBUFS");
485 return -ENOBUFS;
486}
487EXPORT_SYMBOL(__fscache_read_or_alloc_page);
488
489/*
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
500 *
501 * end_io_func() will be called for each page read from the cache as it is
502 * finishes being read
503 *
504 * any pages for which a read is dispatched will be removed from pages and
505 * nr_pages
506 */
507int __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)
514{
b5108822
DH
515 struct fscache_retrieval *op;
516 struct fscache_object *object;
517 int ret;
518
519 _enter("%p,,%d,,,", cookie, *nr_pages);
520
521 fscache_stat(&fscache_n_retrievals);
522
523 if (hlist_empty(&cookie->backing_objects))
524 goto nobufs;
525
ef778e7a
DH
526 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
527 _leave(" = -ENOBUFS [invalidating]");
528 return -ENOBUFS;
529 }
530
b5108822
DH
531 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
532 ASSERTCMP(*nr_pages, >, 0);
533 ASSERT(!list_empty(pages));
534
535 if (fscache_wait_for_deferred_lookup(cookie) < 0)
536 return -ERESTARTSYS;
537
1362729b 538 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
b5108822
DH
539 if (!op)
540 return -ENOMEM;
1bb4b7f9 541 atomic_set(&op->n_pages, *nr_pages);
b5108822
DH
542
543 spin_lock(&cookie->lock);
544
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);
549
4fbf4291 550 atomic_inc(&object->n_reads);
9f10523f 551 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
4fbf4291 552
b5108822 553 if (fscache_submit_op(object, &op->op) < 0)
9f10523f 554 goto nobufs_unlock_dec;
b5108822
DH
555 spin_unlock(&cookie->lock);
556
557 fscache_stat(&fscache_n_retrieval_ops);
558
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);
562
563 /* we wait for the operation to become active, and then process it
564 * *here*, in this thread, and not in the thread pool */
da9803bc
DH
565 ret = fscache_wait_for_operation_activation(
566 object, &op->op,
60d543ca 567 __fscache_stat(&fscache_n_retrieval_op_waits),
da9803bc
DH
568 __fscache_stat(&fscache_n_retrievals_object_dead),
569 fscache_do_cancel_retrieval);
60d543ca
DH
570 if (ret < 0)
571 goto error;
b5108822
DH
572
573 /* ask the cache to honour the operation */
52bd75fd
DH
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);
584 }
b5108822 585
5753c441 586error:
b5108822
DH
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);
597
598 fscache_put_retrieval(op);
599 _leave(" = %d", ret);
600 return ret;
601
9f10523f
DH
602nobufs_unlock_dec:
603 atomic_dec(&object->n_reads);
b5108822
DH
604nobufs_unlock:
605 spin_unlock(&cookie->lock);
1362729b 606 atomic_dec(&cookie->n_active);
b5108822
DH
607 kfree(op);
608nobufs:
609 fscache_stat(&fscache_n_retrievals_nobufs);
610 _leave(" = -ENOBUFS");
611 return -ENOBUFS;
612}
613EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
614
615/*
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
622 */
623int __fscache_alloc_page(struct fscache_cookie *cookie,
624 struct page *page,
625 gfp_t gfp)
626{
627 struct fscache_retrieval *op;
628 struct fscache_object *object;
629 int ret;
630
631 _enter("%p,%p,,,", cookie, page);
632
633 fscache_stat(&fscache_n_allocs);
634
635 if (hlist_empty(&cookie->backing_objects))
636 goto nobufs;
637
638 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
639 ASSERTCMP(page, !=, NULL);
640
ef778e7a
DH
641 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
642 _leave(" = -ENOBUFS [invalidating]");
643 return -ENOBUFS;
644 }
645
b5108822
DH
646 if (fscache_wait_for_deferred_lookup(cookie) < 0)
647 return -ERESTARTSYS;
648
1362729b 649 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
b5108822
DH
650 if (!op)
651 return -ENOMEM;
1bb4b7f9 652 atomic_set(&op->n_pages, 1);
b5108822
DH
653
654 spin_lock(&cookie->lock);
655
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);
660
661 if (fscache_submit_op(object, &op->op) < 0)
662 goto nobufs_unlock;
663 spin_unlock(&cookie->lock);
664
665 fscache_stat(&fscache_n_alloc_ops);
666
da9803bc
DH
667 ret = fscache_wait_for_operation_activation(
668 object, &op->op,
60d543ca 669 __fscache_stat(&fscache_n_alloc_op_waits),
da9803bc
DH
670 __fscache_stat(&fscache_n_allocs_object_dead),
671 fscache_do_cancel_retrieval);
60d543ca
DH
672 if (ret < 0)
673 goto error;
b5108822
DH
674
675 /* ask the cache to honour the operation */
52bd75fd 676 fscache_stat(&fscache_n_cop_allocate_page);
b5108822 677 ret = object->cache->ops->allocate_page(op, page, gfp);
52bd75fd 678 fscache_stat_d(&fscache_n_cop_allocate_page);
b5108822 679
5753c441
DH
680error:
681 if (ret == -ERESTARTSYS)
682 fscache_stat(&fscache_n_allocs_intr);
683 else if (ret < 0)
b5108822
DH
684 fscache_stat(&fscache_n_allocs_nobufs);
685 else
686 fscache_stat(&fscache_n_allocs_ok);
687
688 fscache_put_retrieval(op);
689 _leave(" = %d", ret);
690 return ret;
691
692nobufs_unlock:
693 spin_unlock(&cookie->lock);
1362729b 694 atomic_dec(&cookie->n_active);
b5108822
DH
695 kfree(op);
696nobufs:
697 fscache_stat(&fscache_n_allocs_nobufs);
698 _leave(" = -ENOBUFS");
699 return -ENOBUFS;
700}
701EXPORT_SYMBOL(__fscache_alloc_page);
702
703/*
704 * release a write op reference
705 */
706static void fscache_release_write_op(struct fscache_operation *_op)
707{
708 _enter("{OP%x}", _op->debug_id);
709}
710
711/*
712 * perform the background storage of a page into the cache
713 */
714static void fscache_write_op(struct fscache_operation *_op)
715{
716 struct fscache_storage *op =
717 container_of(_op, struct fscache_storage, op);
718 struct fscache_object *object = op->op.object;
1bccf513 719 struct fscache_cookie *cookie;
b5108822
DH
720 struct page *page;
721 unsigned n;
722 void *results[1];
723 int ret;
724
725 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
726
b5108822 727 spin_lock(&object->lock);
1bccf513 728 cookie = object->cookie;
b5108822 729
7ef001e9
DH
730 if (!fscache_object_is_active(object)) {
731 /* If we get here, then the on-disk cache object likely longer
732 * exists, so we should just cancel this write operation.
733 */
b5108822 734 spin_unlock(&object->lock);
1f372dff 735 fscache_op_complete(&op->op, false);
7ef001e9
DH
736 _leave(" [inactive]");
737 return;
738 }
739
740 if (!cookie) {
741 /* If we get here, then the cookie belonging to the object was
742 * detached, probably by the cookie being withdrawn due to
743 * memory pressure, which means that the pages we might write
744 * to the cache from no longer exist - therefore, we can just
745 * cancel this write operation.
746 */
747 spin_unlock(&object->lock);
1f372dff 748 fscache_op_complete(&op->op, false);
caaef690
DH
749 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
750 _op->flags, _op->state, object->state->short_name,
751 object->flags);
b5108822
DH
752 return;
753 }
754
1bccf513
DH
755 spin_lock(&cookie->stores_lock);
756
b5108822
DH
757 fscache_stat(&fscache_n_store_calls);
758
759 /* find a page to store */
760 page = NULL;
761 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
762 FSCACHE_COOKIE_PENDING_TAG);
763 if (n != 1)
764 goto superseded;
765 page = results[0];
766 _debug("gang %d [%lx]", n, page->index);
1bccf513
DH
767 if (page->index > op->store_limit) {
768 fscache_stat(&fscache_n_store_pages_over_limit);
b5108822 769 goto superseded;
1bccf513 770 }
b5108822 771
08a66859
DC
772 radix_tree_tag_set(&cookie->stores, page->index,
773 FSCACHE_COOKIE_STORING_TAG);
774 radix_tree_tag_clear(&cookie->stores, page->index,
775 FSCACHE_COOKIE_PENDING_TAG);
b5108822 776
1bccf513 777 spin_unlock(&cookie->stores_lock);
b5108822 778 spin_unlock(&object->lock);
b5108822 779
08a66859
DC
780 fscache_stat(&fscache_n_store_pages);
781 fscache_stat(&fscache_n_cop_write_page);
782 ret = object->cache->ops->write_page(op, page);
783 fscache_stat_d(&fscache_n_cop_write_page);
08a66859
DC
784 fscache_end_page_write(object, page);
785 if (ret < 0) {
08a66859 786 fscache_abort_object(object);
1f372dff 787 fscache_op_complete(&op->op, true);
08a66859
DC
788 } else {
789 fscache_enqueue_operation(&op->op);
b5108822
DH
790 }
791
792 _leave("");
793 return;
794
795superseded:
796 /* this writer is going away and there aren't any more things to
797 * write */
798 _debug("cease");
1bccf513 799 spin_unlock(&cookie->stores_lock);
b5108822
DH
800 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
801 spin_unlock(&object->lock);
1f372dff 802 fscache_op_complete(&op->op, true);
b5108822
DH
803 _leave("");
804}
805
ef778e7a
DH
806/*
807 * Clear the pages pending writing for invalidation
808 */
809void fscache_invalidate_writes(struct fscache_cookie *cookie)
810{
811 struct page *page;
812 void *results[16];
813 int n, i;
814
815 _enter("");
816
ee8be57b
SAS
817 for (;;) {
818 spin_lock(&cookie->stores_lock);
819 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
820 ARRAY_SIZE(results),
821 FSCACHE_COOKIE_PENDING_TAG);
822 if (n == 0) {
823 spin_unlock(&cookie->stores_lock);
824 break;
825 }
826
ef778e7a
DH
827 for (i = n - 1; i >= 0; i--) {
828 page = results[i];
829 radix_tree_delete(&cookie->stores, page->index);
830 }
831
832 spin_unlock(&cookie->stores_lock);
833
834 for (i = n - 1; i >= 0; i--)
835 page_cache_release(results[i]);
836 }
837
ef778e7a
DH
838 _leave("");
839}
840
b5108822
DH
841/*
842 * request a page be stored in the cache
843 * - returns:
844 * -ENOMEM - out of memory, nothing done
845 * -ENOBUFS - no backing object available in which to cache the page
846 * 0 - dispatched a write - it'll call end_io_func() when finished
847 *
848 * if the cookie still has a backing object at this point, that object can be
849 * in one of a few states with respect to storage processing:
850 *
851 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
852 * set)
853 *
caaef690 854 * (a) no writes yet
b5108822
DH
855 *
856 * (b) writes deferred till post-creation (mark page for writing and
857 * return immediately)
858 *
859 * (2) negative lookup, object created, initial fill being made from netfs
b5108822
DH
860 *
861 * (a) fill point not yet reached this page (mark page for writing and
862 * return)
863 *
864 * (b) fill point passed this page (queue op to store this page)
865 *
866 * (3) object extant (queue op to store this page)
867 *
868 * any other state is invalid
869 */
870int __fscache_write_page(struct fscache_cookie *cookie,
871 struct page *page,
872 gfp_t gfp)
873{
874 struct fscache_storage *op;
875 struct fscache_object *object;
876 int ret;
877
878 _enter("%p,%x,", cookie, (u32) page->flags);
879
880 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
881 ASSERT(PageFsCache(page));
882
883 fscache_stat(&fscache_n_stores);
884
ef778e7a
DH
885 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
886 _leave(" = -ENOBUFS [invalidating]");
887 return -ENOBUFS;
888 }
889
5f4f9f4a 890 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
b5108822
DH
891 if (!op)
892 goto nomem;
893
8af7c124
TH
894 fscache_operation_init(&op->op, fscache_write_op,
895 fscache_release_write_op);
1362729b
DH
896 op->op.flags = FSCACHE_OP_ASYNC |
897 (1 << FSCACHE_OP_WAITING) |
898 (1 << FSCACHE_OP_UNUSE_COOKIE);
b5108822
DH
899
900 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
901 if (ret < 0)
902 goto nomem_free;
903
904 ret = -ENOBUFS;
905 spin_lock(&cookie->lock);
906
907 if (hlist_empty(&cookie->backing_objects))
908 goto nobufs;
909 object = hlist_entry(cookie->backing_objects.first,
910 struct fscache_object, cookie_link);
911 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
912 goto nobufs;
913
914 /* add the page to the pending-storage radix tree on the backing
915 * object */
916 spin_lock(&object->lock);
1bccf513 917 spin_lock(&cookie->stores_lock);
b5108822
DH
918
919 _debug("store limit %llx", (unsigned long long) object->store_limit);
920
921 ret = radix_tree_insert(&cookie->stores, page->index, page);
922 if (ret < 0) {
923 if (ret == -EEXIST)
924 goto already_queued;
925 _debug("insert failed %d", ret);
926 goto nobufs_unlock_obj;
927 }
928
929 radix_tree_tag_set(&cookie->stores, page->index,
930 FSCACHE_COOKIE_PENDING_TAG);
931 page_cache_get(page);
932
933 /* we only want one writer at a time, but we do need to queue new
934 * writers after exclusive ops */
935 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
936 goto already_pending;
937
1bccf513 938 spin_unlock(&cookie->stores_lock);
b5108822
DH
939 spin_unlock(&object->lock);
940
941 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
942 op->store_limit = object->store_limit;
943
1362729b 944 atomic_inc(&cookie->n_active);
b5108822
DH
945 if (fscache_submit_op(object, &op->op) < 0)
946 goto submit_failed;
947
948 spin_unlock(&cookie->lock);
949 radix_tree_preload_end();
950 fscache_stat(&fscache_n_store_ops);
951 fscache_stat(&fscache_n_stores_ok);
952
8af7c124 953 /* the work queue now carries its own ref on the object */
b5108822
DH
954 fscache_put_operation(&op->op);
955 _leave(" = 0");
956 return 0;
957
958already_queued:
959 fscache_stat(&fscache_n_stores_again);
960already_pending:
1bccf513 961 spin_unlock(&cookie->stores_lock);
b5108822
DH
962 spin_unlock(&object->lock);
963 spin_unlock(&cookie->lock);
964 radix_tree_preload_end();
965 kfree(op);
966 fscache_stat(&fscache_n_stores_ok);
967 _leave(" = 0");
968 return 0;
969
970submit_failed:
1362729b 971 atomic_dec(&cookie->n_active);
1bccf513 972 spin_lock(&cookie->stores_lock);
b5108822 973 radix_tree_delete(&cookie->stores, page->index);
1bccf513 974 spin_unlock(&cookie->stores_lock);
b5108822
DH
975 page_cache_release(page);
976 ret = -ENOBUFS;
977 goto nobufs;
978
979nobufs_unlock_obj:
1147d0f9 980 spin_unlock(&cookie->stores_lock);
b5108822
DH
981 spin_unlock(&object->lock);
982nobufs:
983 spin_unlock(&cookie->lock);
984 radix_tree_preload_end();
985 kfree(op);
986 fscache_stat(&fscache_n_stores_nobufs);
987 _leave(" = -ENOBUFS");
988 return -ENOBUFS;
989
990nomem_free:
991 kfree(op);
992nomem:
993 fscache_stat(&fscache_n_stores_oom);
994 _leave(" = -ENOMEM");
995 return -ENOMEM;
996}
997EXPORT_SYMBOL(__fscache_write_page);
998
999/*
1000 * remove a page from the cache
1001 */
1002void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1003{
1004 struct fscache_object *object;
1005
1006 _enter(",%p", page);
1007
1008 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1009 ASSERTCMP(page, !=, NULL);
1010
1011 fscache_stat(&fscache_n_uncaches);
1012
1013 /* cache withdrawal may beat us to it */
1014 if (!PageFsCache(page))
1015 goto done;
1016
1017 /* get the object */
1018 spin_lock(&cookie->lock);
1019
1020 if (hlist_empty(&cookie->backing_objects)) {
1021 ClearPageFsCache(page);
1022 goto done_unlock;
1023 }
1024
1025 object = hlist_entry(cookie->backing_objects.first,
1026 struct fscache_object, cookie_link);
1027
1028 /* there might now be stuff on disk we could read */
1029 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1030
1031 /* only invoke the cache backend if we managed to mark the page
1032 * uncached here; this deals with synchronisation vs withdrawal */
1033 if (TestClearPageFsCache(page) &&
1034 object->cache->ops->uncache_page) {
1035 /* the cache backend releases the cookie lock */
52bd75fd 1036 fscache_stat(&fscache_n_cop_uncache_page);
b5108822 1037 object->cache->ops->uncache_page(object, page);
52bd75fd 1038 fscache_stat_d(&fscache_n_cop_uncache_page);
b5108822
DH
1039 goto done;
1040 }
1041
1042done_unlock:
1043 spin_unlock(&cookie->lock);
1044done:
1045 _leave("");
1046}
1047EXPORT_SYMBOL(__fscache_uncache_page);
1048
c4d6d8db
DH
1049/**
1050 * fscache_mark_page_cached - Mark a page as being cached
1051 * @op: The retrieval op pages are being marked for
1052 * @page: The page to be marked
1053 *
1054 * Mark a netfs page as being cached. After this is called, the netfs
1055 * must call fscache_uncache_page() to remove the mark.
1056 */
1057void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1058{
1059 struct fscache_cookie *cookie = op->op.object->cookie;
1060
1061#ifdef CONFIG_FSCACHE_STATS
1062 atomic_inc(&fscache_n_marks);
1063#endif
1064
1065 _debug("- mark %p{%lx}", page, page->index);
1066 if (TestSetPageFsCache(page)) {
1067 static bool once_only;
1068 if (!once_only) {
1069 once_only = true;
1070 printk(KERN_WARNING "FS-Cache:"
1071 " Cookie type %s marked page %lx"
1072 " multiple times\n",
1073 cookie->def->name, page->index);
1074 }
1075 }
1076
1077 if (cookie->def->mark_page_cached)
1078 cookie->def->mark_page_cached(cookie->netfs_data,
1079 op->mapping, page);
1080}
1081EXPORT_SYMBOL(fscache_mark_page_cached);
1082
b5108822
DH
1083/**
1084 * fscache_mark_pages_cached - Mark pages as being cached
1085 * @op: The retrieval op pages are being marked for
1086 * @pagevec: The pages to be marked
1087 *
1088 * Mark a bunch of netfs pages as being cached. After this is called,
1089 * the netfs must call fscache_uncache_page() to remove the mark.
1090 */
1091void fscache_mark_pages_cached(struct fscache_retrieval *op,
1092 struct pagevec *pagevec)
1093{
b5108822
DH
1094 unsigned long loop;
1095
c4d6d8db
DH
1096 for (loop = 0; loop < pagevec->nr; loop++)
1097 fscache_mark_page_cached(op, pagevec->pages[loop]);
b5108822 1098
b5108822
DH
1099 pagevec_reinit(pagevec);
1100}
1101EXPORT_SYMBOL(fscache_mark_pages_cached);
c902ce1b
DH
1102
1103/*
1104 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1105 * to be associated with the given cookie.
1106 */
1107void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1108 struct inode *inode)
1109{
1110 struct address_space *mapping = inode->i_mapping;
1111 struct pagevec pvec;
1112 pgoff_t next;
1113 int i;
1114
1115 _enter("%p,%p", cookie, inode);
1116
1117 if (!mapping || mapping->nrpages == 0) {
1118 _leave(" [no pages]");
1119 return;
1120 }
1121
1122 pagevec_init(&pvec, 0);
1123 next = 0;
b307d465
JB
1124 do {
1125 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1126 break;
c902ce1b
DH
1127 for (i = 0; i < pagevec_count(&pvec); i++) {
1128 struct page *page = pvec.pages[i];
b307d465 1129 next = page->index;
c902ce1b
DH
1130 if (PageFsCache(page)) {
1131 __fscache_wait_on_page_write(cookie, page);
1132 __fscache_uncache_page(cookie, page);
1133 }
1134 }
1135 pagevec_release(&pvec);
1136 cond_resched();
b307d465 1137 } while (++next);
c902ce1b
DH
1138
1139 _leave("");
1140}
1141EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);