Loading...
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>
17#include <linux/slab.h>
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
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
59 rcu_read_lock();
60 val = radix_tree_lookup(&cookie->stores, page->index);
61 if (!val) {
62 rcu_read_unlock();
63 fscache_stat(&fscache_n_store_vmscan_not_storing);
64 __fscache_uncache_page(cookie, page);
65 return true;
66 }
67
68 /* see if the page is actually undergoing storage - if so we can't get
69 * rid of it till the cache has finished with it */
70 if (radix_tree_tag_get(&cookie->stores, page->index,
71 FSCACHE_COOKIE_STORING_TAG)) {
72 rcu_read_unlock();
73 goto page_busy;
74 }
75
76 /* the page is pending storage, so we attempt to cancel the store and
77 * discard the store request so that the page can be reclaimed */
78 spin_lock(&cookie->stores_lock);
79 rcu_read_unlock();
80
81 if (radix_tree_tag_get(&cookie->stores, page->index,
82 FSCACHE_COOKIE_STORING_TAG)) {
83 /* the page started to undergo storage whilst we were looking,
84 * so now we can only wait or return */
85 spin_unlock(&cookie->stores_lock);
86 goto page_busy;
87 }
88
89 xpage = radix_tree_delete(&cookie->stores, page->index);
90 spin_unlock(&cookie->stores_lock);
91
92 if (xpage) {
93 fscache_stat(&fscache_n_store_vmscan_cancelled);
94 fscache_stat(&fscache_n_store_radix_deletes);
95 ASSERTCMP(xpage, ==, page);
96 } else {
97 fscache_stat(&fscache_n_store_vmscan_gone);
98 }
99
100 wake_up_bit(&cookie->flags, 0);
101 if (xpage)
102 page_cache_release(xpage);
103 __fscache_uncache_page(cookie, page);
104 return true;
105
106page_busy:
107 /* we might want to wait here, but that could deadlock the allocator as
108 * the work threads writing to the cache may all end up sleeping
109 * on memory allocation */
110 fscache_stat(&fscache_n_store_vmscan_busy);
111 return false;
112}
113EXPORT_SYMBOL(__fscache_maybe_release_page);
114
115/*
116 * note that a page has finished being written to the cache
117 */
118static void fscache_end_page_write(struct fscache_object *object,
119 struct page *page)
120{
121 struct fscache_cookie *cookie;
122 struct page *xpage = NULL;
123
124 spin_lock(&object->lock);
125 cookie = object->cookie;
126 if (cookie) {
127 /* delete the page from the tree if it is now no longer
128 * pending */
129 spin_lock(&cookie->stores_lock);
130 radix_tree_tag_clear(&cookie->stores, page->index,
131 FSCACHE_COOKIE_STORING_TAG);
132 if (!radix_tree_tag_get(&cookie->stores, page->index,
133 FSCACHE_COOKIE_PENDING_TAG)) {
134 fscache_stat(&fscache_n_store_radix_deletes);
135 xpage = radix_tree_delete(&cookie->stores, page->index);
136 }
137 spin_unlock(&cookie->stores_lock);
138 wake_up_bit(&cookie->flags, 0);
139 }
140 spin_unlock(&object->lock);
141 if (xpage)
142 page_cache_release(xpage);
143}
144
145/*
146 * actually apply the changed attributes to a cache object
147 */
148static void fscache_attr_changed_op(struct fscache_operation *op)
149{
150 struct fscache_object *object = op->object;
151 int ret;
152
153 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
154
155 fscache_stat(&fscache_n_attr_changed_calls);
156
157 if (fscache_object_is_active(object)) {
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 if (ret < 0)
162 fscache_abort_object(object);
163 }
164
165 _leave("");
166}
167
168/*
169 * notification that the attributes on an object have changed
170 */
171int __fscache_attr_changed(struct fscache_cookie *cookie)
172{
173 struct fscache_operation *op;
174 struct fscache_object *object;
175
176 _enter("%p", cookie);
177
178 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
179
180 fscache_stat(&fscache_n_attr_changed);
181
182 op = kzalloc(sizeof(*op), GFP_KERNEL);
183 if (!op) {
184 fscache_stat(&fscache_n_attr_changed_nomem);
185 _leave(" = -ENOMEM");
186 return -ENOMEM;
187 }
188
189 fscache_operation_init(op, fscache_attr_changed_op, NULL);
190 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
191
192 spin_lock(&cookie->lock);
193
194 if (hlist_empty(&cookie->backing_objects))
195 goto nobufs;
196 object = hlist_entry(cookie->backing_objects.first,
197 struct fscache_object, cookie_link);
198
199 if (fscache_submit_exclusive_op(object, op) < 0)
200 goto nobufs;
201 spin_unlock(&cookie->lock);
202 fscache_stat(&fscache_n_attr_changed_ok);
203 fscache_put_operation(op);
204 _leave(" = 0");
205 return 0;
206
207nobufs:
208 spin_unlock(&cookie->lock);
209 kfree(op);
210 fscache_stat(&fscache_n_attr_changed_nobufs);
211 _leave(" = %d", -ENOBUFS);
212 return -ENOBUFS;
213}
214EXPORT_SYMBOL(__fscache_attr_changed);
215
216/*
217 * release a retrieval op reference
218 */
219static void fscache_release_retrieval_op(struct fscache_operation *_op)
220{
221 struct fscache_retrieval *op =
222 container_of(_op, struct fscache_retrieval, op);
223
224 _enter("{OP%x}", op->op.debug_id);
225
226 fscache_hist(fscache_retrieval_histogram, op->start_time);
227 if (op->context)
228 fscache_put_context(op->op.object->cookie, op->context);
229
230 _leave("");
231}
232
233/*
234 * allocate a retrieval op
235 */
236static struct fscache_retrieval *fscache_alloc_retrieval(
237 struct address_space *mapping,
238 fscache_rw_complete_t end_io_func,
239 void *context)
240{
241 struct fscache_retrieval *op;
242
243 /* allocate a retrieval operation and attempt to submit it */
244 op = kzalloc(sizeof(*op), GFP_NOIO);
245 if (!op) {
246 fscache_stat(&fscache_n_retrievals_nomem);
247 return NULL;
248 }
249
250 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
251 op->op.flags = FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING);
252 op->mapping = mapping;
253 op->end_io_func = end_io_func;
254 op->context = context;
255 op->start_time = jiffies;
256 INIT_LIST_HEAD(&op->to_do);
257 return op;
258}
259
260/*
261 * wait for a deferred lookup to complete
262 */
263static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
264{
265 unsigned long jif;
266
267 _enter("");
268
269 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
270 _leave(" = 0 [imm]");
271 return 0;
272 }
273
274 fscache_stat(&fscache_n_retrievals_wait);
275
276 jif = jiffies;
277 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
278 fscache_wait_bit_interruptible,
279 TASK_INTERRUPTIBLE) != 0) {
280 fscache_stat(&fscache_n_retrievals_intr);
281 _leave(" = -ERESTARTSYS");
282 return -ERESTARTSYS;
283 }
284
285 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
286
287 smp_rmb();
288 fscache_hist(fscache_retrieval_delay_histogram, jif);
289 _leave(" = 0 [dly]");
290 return 0;
291}
292
293/*
294 * wait for an object to become active (or dead)
295 */
296static int fscache_wait_for_retrieval_activation(struct fscache_object *object,
297 struct fscache_retrieval *op,
298 atomic_t *stat_op_waits,
299 atomic_t *stat_object_dead)
300{
301 int ret;
302
303 if (!test_bit(FSCACHE_OP_WAITING, &op->op.flags))
304 goto check_if_dead;
305
306 _debug(">>> WT");
307 fscache_stat(stat_op_waits);
308 if (wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
309 fscache_wait_bit_interruptible,
310 TASK_INTERRUPTIBLE) < 0) {
311 ret = fscache_cancel_op(&op->op);
312 if (ret == 0)
313 return -ERESTARTSYS;
314
315 /* it's been removed from the pending queue by another party,
316 * so we should get to run shortly */
317 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
318 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
319 }
320 _debug("<<< GO");
321
322check_if_dead:
323 if (unlikely(fscache_object_is_dead(object))) {
324 fscache_stat(stat_object_dead);
325 return -ENOBUFS;
326 }
327 return 0;
328}
329
330/*
331 * read a page from the cache or allocate a block in which to store it
332 * - we return:
333 * -ENOMEM - out of memory, nothing done
334 * -ERESTARTSYS - interrupted
335 * -ENOBUFS - no backing object available in which to cache the block
336 * -ENODATA - no data available in the backing object for this block
337 * 0 - dispatched a read - it'll call end_io_func() when finished
338 */
339int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
340 struct page *page,
341 fscache_rw_complete_t end_io_func,
342 void *context,
343 gfp_t gfp)
344{
345 struct fscache_retrieval *op;
346 struct fscache_object *object;
347 int ret;
348
349 _enter("%p,%p,,,", cookie, page);
350
351 fscache_stat(&fscache_n_retrievals);
352
353 if (hlist_empty(&cookie->backing_objects))
354 goto nobufs;
355
356 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
357 ASSERTCMP(page, !=, NULL);
358
359 if (fscache_wait_for_deferred_lookup(cookie) < 0)
360 return -ERESTARTSYS;
361
362 op = fscache_alloc_retrieval(page->mapping, end_io_func, context);
363 if (!op) {
364 _leave(" = -ENOMEM");
365 return -ENOMEM;
366 }
367
368 spin_lock(&cookie->lock);
369
370 if (hlist_empty(&cookie->backing_objects))
371 goto nobufs_unlock;
372 object = hlist_entry(cookie->backing_objects.first,
373 struct fscache_object, cookie_link);
374
375 ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP);
376
377 atomic_inc(&object->n_reads);
378 set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
379
380 if (fscache_submit_op(object, &op->op) < 0)
381 goto nobufs_unlock;
382 spin_unlock(&cookie->lock);
383
384 fscache_stat(&fscache_n_retrieval_ops);
385
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);
389
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 ret = fscache_wait_for_retrieval_activation(
393 object, op,
394 __fscache_stat(&fscache_n_retrieval_op_waits),
395 __fscache_stat(&fscache_n_retrievals_object_dead));
396 if (ret < 0)
397 goto error;
398
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);
404 if (ret == 0)
405 ret = -ENODATA;
406 } else {
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);
410 }
411
412error:
413 if (ret == -ENOMEM)
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);
419 else if (ret < 0)
420 fscache_stat(&fscache_n_retrievals_nobufs);
421 else
422 fscache_stat(&fscache_n_retrievals_ok);
423
424 fscache_put_retrieval(op);
425 _leave(" = %d", ret);
426 return ret;
427
428nobufs_unlock:
429 spin_unlock(&cookie->lock);
430 kfree(op);
431nobufs:
432 fscache_stat(&fscache_n_retrievals_nobufs);
433 _leave(" = -ENOBUFS");
434 return -ENOBUFS;
435}
436EXPORT_SYMBOL(__fscache_read_or_alloc_page);
437
438/*
439 * read a list of page from the cache or allocate a block in which to store
440 * them
441 * - we return:
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
447 * the pages
448 * 0 - dispatched a read on all pages
449 *
450 * end_io_func() will be called for each page read from the cache as it is
451 * finishes being read
452 *
453 * any pages for which a read is dispatched will be removed from pages and
454 * nr_pages
455 */
456int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
457 struct address_space *mapping,
458 struct list_head *pages,
459 unsigned *nr_pages,
460 fscache_rw_complete_t end_io_func,
461 void *context,
462 gfp_t gfp)
463{
464 struct fscache_retrieval *op;
465 struct fscache_object *object;
466 int ret;
467
468 _enter("%p,,%d,,,", cookie, *nr_pages);
469
470 fscache_stat(&fscache_n_retrievals);
471
472 if (hlist_empty(&cookie->backing_objects))
473 goto nobufs;
474
475 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
476 ASSERTCMP(*nr_pages, >, 0);
477 ASSERT(!list_empty(pages));
478
479 if (fscache_wait_for_deferred_lookup(cookie) < 0)
480 return -ERESTARTSYS;
481
482 op = fscache_alloc_retrieval(mapping, end_io_func, context);
483 if (!op)
484 return -ENOMEM;
485
486 spin_lock(&cookie->lock);
487
488 if (hlist_empty(&cookie->backing_objects))
489 goto nobufs_unlock;
490 object = hlist_entry(cookie->backing_objects.first,
491 struct fscache_object, cookie_link);
492
493 atomic_inc(&object->n_reads);
494 set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
495
496 if (fscache_submit_op(object, &op->op) < 0)
497 goto nobufs_unlock;
498 spin_unlock(&cookie->lock);
499
500 fscache_stat(&fscache_n_retrieval_ops);
501
502 /* pin the netfs read context in case we need to do the actual netfs
503 * read because we've encountered a cache read failure */
504 fscache_get_context(object->cookie, op->context);
505
506 /* we wait for the operation to become active, and then process it
507 * *here*, in this thread, and not in the thread pool */
508 ret = fscache_wait_for_retrieval_activation(
509 object, op,
510 __fscache_stat(&fscache_n_retrieval_op_waits),
511 __fscache_stat(&fscache_n_retrievals_object_dead));
512 if (ret < 0)
513 goto error;
514
515 /* ask the cache to honour the operation */
516 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
517 fscache_stat(&fscache_n_cop_allocate_pages);
518 ret = object->cache->ops->allocate_pages(
519 op, pages, nr_pages, gfp);
520 fscache_stat_d(&fscache_n_cop_allocate_pages);
521 } else {
522 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
523 ret = object->cache->ops->read_or_alloc_pages(
524 op, pages, nr_pages, gfp);
525 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
526 }
527
528error:
529 if (ret == -ENOMEM)
530 fscache_stat(&fscache_n_retrievals_nomem);
531 else if (ret == -ERESTARTSYS)
532 fscache_stat(&fscache_n_retrievals_intr);
533 else if (ret == -ENODATA)
534 fscache_stat(&fscache_n_retrievals_nodata);
535 else if (ret < 0)
536 fscache_stat(&fscache_n_retrievals_nobufs);
537 else
538 fscache_stat(&fscache_n_retrievals_ok);
539
540 fscache_put_retrieval(op);
541 _leave(" = %d", ret);
542 return ret;
543
544nobufs_unlock:
545 spin_unlock(&cookie->lock);
546 kfree(op);
547nobufs:
548 fscache_stat(&fscache_n_retrievals_nobufs);
549 _leave(" = -ENOBUFS");
550 return -ENOBUFS;
551}
552EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
553
554/*
555 * allocate a block in the cache on which to store a page
556 * - we return:
557 * -ENOMEM - out of memory, nothing done
558 * -ERESTARTSYS - interrupted
559 * -ENOBUFS - no backing object available in which to cache the block
560 * 0 - block allocated
561 */
562int __fscache_alloc_page(struct fscache_cookie *cookie,
563 struct page *page,
564 gfp_t gfp)
565{
566 struct fscache_retrieval *op;
567 struct fscache_object *object;
568 int ret;
569
570 _enter("%p,%p,,,", cookie, page);
571
572 fscache_stat(&fscache_n_allocs);
573
574 if (hlist_empty(&cookie->backing_objects))
575 goto nobufs;
576
577 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
578 ASSERTCMP(page, !=, NULL);
579
580 if (fscache_wait_for_deferred_lookup(cookie) < 0)
581 return -ERESTARTSYS;
582
583 op = fscache_alloc_retrieval(page->mapping, NULL, NULL);
584 if (!op)
585 return -ENOMEM;
586
587 spin_lock(&cookie->lock);
588
589 if (hlist_empty(&cookie->backing_objects))
590 goto nobufs_unlock;
591 object = hlist_entry(cookie->backing_objects.first,
592 struct fscache_object, cookie_link);
593
594 if (fscache_submit_op(object, &op->op) < 0)
595 goto nobufs_unlock;
596 spin_unlock(&cookie->lock);
597
598 fscache_stat(&fscache_n_alloc_ops);
599
600 ret = fscache_wait_for_retrieval_activation(
601 object, op,
602 __fscache_stat(&fscache_n_alloc_op_waits),
603 __fscache_stat(&fscache_n_allocs_object_dead));
604 if (ret < 0)
605 goto error;
606
607 /* ask the cache to honour the operation */
608 fscache_stat(&fscache_n_cop_allocate_page);
609 ret = object->cache->ops->allocate_page(op, page, gfp);
610 fscache_stat_d(&fscache_n_cop_allocate_page);
611
612error:
613 if (ret == -ERESTARTSYS)
614 fscache_stat(&fscache_n_allocs_intr);
615 else if (ret < 0)
616 fscache_stat(&fscache_n_allocs_nobufs);
617 else
618 fscache_stat(&fscache_n_allocs_ok);
619
620 fscache_put_retrieval(op);
621 _leave(" = %d", ret);
622 return ret;
623
624nobufs_unlock:
625 spin_unlock(&cookie->lock);
626 kfree(op);
627nobufs:
628 fscache_stat(&fscache_n_allocs_nobufs);
629 _leave(" = -ENOBUFS");
630 return -ENOBUFS;
631}
632EXPORT_SYMBOL(__fscache_alloc_page);
633
634/*
635 * release a write op reference
636 */
637static void fscache_release_write_op(struct fscache_operation *_op)
638{
639 _enter("{OP%x}", _op->debug_id);
640}
641
642/*
643 * perform the background storage of a page into the cache
644 */
645static void fscache_write_op(struct fscache_operation *_op)
646{
647 struct fscache_storage *op =
648 container_of(_op, struct fscache_storage, op);
649 struct fscache_object *object = op->op.object;
650 struct fscache_cookie *cookie;
651 struct page *page;
652 unsigned n;
653 void *results[1];
654 int ret;
655
656 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
657
658 spin_lock(&object->lock);
659 cookie = object->cookie;
660
661 if (!fscache_object_is_active(object) || !cookie) {
662 spin_unlock(&object->lock);
663 _leave("");
664 return;
665 }
666
667 spin_lock(&cookie->stores_lock);
668
669 fscache_stat(&fscache_n_store_calls);
670
671 /* find a page to store */
672 page = NULL;
673 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
674 FSCACHE_COOKIE_PENDING_TAG);
675 if (n != 1)
676 goto superseded;
677 page = results[0];
678 _debug("gang %d [%lx]", n, page->index);
679 if (page->index > op->store_limit) {
680 fscache_stat(&fscache_n_store_pages_over_limit);
681 goto superseded;
682 }
683
684 radix_tree_tag_set(&cookie->stores, page->index,
685 FSCACHE_COOKIE_STORING_TAG);
686 radix_tree_tag_clear(&cookie->stores, page->index,
687 FSCACHE_COOKIE_PENDING_TAG);
688
689 spin_unlock(&cookie->stores_lock);
690 spin_unlock(&object->lock);
691
692 fscache_stat(&fscache_n_store_pages);
693 fscache_stat(&fscache_n_cop_write_page);
694 ret = object->cache->ops->write_page(op, page);
695 fscache_stat_d(&fscache_n_cop_write_page);
696 fscache_end_page_write(object, page);
697 if (ret < 0) {
698 fscache_abort_object(object);
699 } else {
700 fscache_enqueue_operation(&op->op);
701 }
702
703 _leave("");
704 return;
705
706superseded:
707 /* this writer is going away and there aren't any more things to
708 * write */
709 _debug("cease");
710 spin_unlock(&cookie->stores_lock);
711 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
712 spin_unlock(&object->lock);
713 _leave("");
714}
715
716/*
717 * request a page be stored in the cache
718 * - returns:
719 * -ENOMEM - out of memory, nothing done
720 * -ENOBUFS - no backing object available in which to cache the page
721 * 0 - dispatched a write - it'll call end_io_func() when finished
722 *
723 * if the cookie still has a backing object at this point, that object can be
724 * in one of a few states with respect to storage processing:
725 *
726 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
727 * set)
728 *
729 * (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
730 * fill op)
731 *
732 * (b) writes deferred till post-creation (mark page for writing and
733 * return immediately)
734 *
735 * (2) negative lookup, object created, initial fill being made from netfs
736 * (FSCACHE_COOKIE_INITIAL_FILL is set)
737 *
738 * (a) fill point not yet reached this page (mark page for writing and
739 * return)
740 *
741 * (b) fill point passed this page (queue op to store this page)
742 *
743 * (3) object extant (queue op to store this page)
744 *
745 * any other state is invalid
746 */
747int __fscache_write_page(struct fscache_cookie *cookie,
748 struct page *page,
749 gfp_t gfp)
750{
751 struct fscache_storage *op;
752 struct fscache_object *object;
753 int ret;
754
755 _enter("%p,%x,", cookie, (u32) page->flags);
756
757 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
758 ASSERT(PageFsCache(page));
759
760 fscache_stat(&fscache_n_stores);
761
762 op = kzalloc(sizeof(*op), GFP_NOIO);
763 if (!op)
764 goto nomem;
765
766 fscache_operation_init(&op->op, fscache_write_op,
767 fscache_release_write_op);
768 op->op.flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_WAITING);
769
770 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
771 if (ret < 0)
772 goto nomem_free;
773
774 ret = -ENOBUFS;
775 spin_lock(&cookie->lock);
776
777 if (hlist_empty(&cookie->backing_objects))
778 goto nobufs;
779 object = hlist_entry(cookie->backing_objects.first,
780 struct fscache_object, cookie_link);
781 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
782 goto nobufs;
783
784 /* add the page to the pending-storage radix tree on the backing
785 * object */
786 spin_lock(&object->lock);
787 spin_lock(&cookie->stores_lock);
788
789 _debug("store limit %llx", (unsigned long long) object->store_limit);
790
791 ret = radix_tree_insert(&cookie->stores, page->index, page);
792 if (ret < 0) {
793 if (ret == -EEXIST)
794 goto already_queued;
795 _debug("insert failed %d", ret);
796 goto nobufs_unlock_obj;
797 }
798
799 radix_tree_tag_set(&cookie->stores, page->index,
800 FSCACHE_COOKIE_PENDING_TAG);
801 page_cache_get(page);
802
803 /* we only want one writer at a time, but we do need to queue new
804 * writers after exclusive ops */
805 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
806 goto already_pending;
807
808 spin_unlock(&cookie->stores_lock);
809 spin_unlock(&object->lock);
810
811 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
812 op->store_limit = object->store_limit;
813
814 if (fscache_submit_op(object, &op->op) < 0)
815 goto submit_failed;
816
817 spin_unlock(&cookie->lock);
818 radix_tree_preload_end();
819 fscache_stat(&fscache_n_store_ops);
820 fscache_stat(&fscache_n_stores_ok);
821
822 /* the work queue now carries its own ref on the object */
823 fscache_put_operation(&op->op);
824 _leave(" = 0");
825 return 0;
826
827already_queued:
828 fscache_stat(&fscache_n_stores_again);
829already_pending:
830 spin_unlock(&cookie->stores_lock);
831 spin_unlock(&object->lock);
832 spin_unlock(&cookie->lock);
833 radix_tree_preload_end();
834 kfree(op);
835 fscache_stat(&fscache_n_stores_ok);
836 _leave(" = 0");
837 return 0;
838
839submit_failed:
840 spin_lock(&cookie->stores_lock);
841 radix_tree_delete(&cookie->stores, page->index);
842 spin_unlock(&cookie->stores_lock);
843 page_cache_release(page);
844 ret = -ENOBUFS;
845 goto nobufs;
846
847nobufs_unlock_obj:
848 spin_unlock(&cookie->stores_lock);
849 spin_unlock(&object->lock);
850nobufs:
851 spin_unlock(&cookie->lock);
852 radix_tree_preload_end();
853 kfree(op);
854 fscache_stat(&fscache_n_stores_nobufs);
855 _leave(" = -ENOBUFS");
856 return -ENOBUFS;
857
858nomem_free:
859 kfree(op);
860nomem:
861 fscache_stat(&fscache_n_stores_oom);
862 _leave(" = -ENOMEM");
863 return -ENOMEM;
864}
865EXPORT_SYMBOL(__fscache_write_page);
866
867/*
868 * remove a page from the cache
869 */
870void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
871{
872 struct fscache_object *object;
873
874 _enter(",%p", page);
875
876 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
877 ASSERTCMP(page, !=, NULL);
878
879 fscache_stat(&fscache_n_uncaches);
880
881 /* cache withdrawal may beat us to it */
882 if (!PageFsCache(page))
883 goto done;
884
885 /* get the object */
886 spin_lock(&cookie->lock);
887
888 if (hlist_empty(&cookie->backing_objects)) {
889 ClearPageFsCache(page);
890 goto done_unlock;
891 }
892
893 object = hlist_entry(cookie->backing_objects.first,
894 struct fscache_object, cookie_link);
895
896 /* there might now be stuff on disk we could read */
897 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
898
899 /* only invoke the cache backend if we managed to mark the page
900 * uncached here; this deals with synchronisation vs withdrawal */
901 if (TestClearPageFsCache(page) &&
902 object->cache->ops->uncache_page) {
903 /* the cache backend releases the cookie lock */
904 fscache_stat(&fscache_n_cop_uncache_page);
905 object->cache->ops->uncache_page(object, page);
906 fscache_stat_d(&fscache_n_cop_uncache_page);
907 goto done;
908 }
909
910done_unlock:
911 spin_unlock(&cookie->lock);
912done:
913 _leave("");
914}
915EXPORT_SYMBOL(__fscache_uncache_page);
916
917/**
918 * fscache_mark_pages_cached - Mark pages as being cached
919 * @op: The retrieval op pages are being marked for
920 * @pagevec: The pages to be marked
921 *
922 * Mark a bunch of netfs pages as being cached. After this is called,
923 * the netfs must call fscache_uncache_page() to remove the mark.
924 */
925void fscache_mark_pages_cached(struct fscache_retrieval *op,
926 struct pagevec *pagevec)
927{
928 struct fscache_cookie *cookie = op->op.object->cookie;
929 unsigned long loop;
930
931#ifdef CONFIG_FSCACHE_STATS
932 atomic_add(pagevec->nr, &fscache_n_marks);
933#endif
934
935 for (loop = 0; loop < pagevec->nr; loop++) {
936 struct page *page = pagevec->pages[loop];
937
938 _debug("- mark %p{%lx}", page, page->index);
939 if (TestSetPageFsCache(page)) {
940 static bool once_only;
941 if (!once_only) {
942 once_only = true;
943 printk(KERN_WARNING "FS-Cache:"
944 " Cookie type %s marked page %lx"
945 " multiple times\n",
946 cookie->def->name, page->index);
947 }
948 }
949 }
950
951 if (cookie->def->mark_pages_cached)
952 cookie->def->mark_pages_cached(cookie->netfs_data,
953 op->mapping, pagevec);
954 pagevec_reinit(pagevec);
955}
956EXPORT_SYMBOL(fscache_mark_pages_cached);
957
958/*
959 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
960 * to be associated with the given cookie.
961 */
962void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
963 struct inode *inode)
964{
965 struct address_space *mapping = inode->i_mapping;
966 struct pagevec pvec;
967 pgoff_t next;
968 int i;
969
970 _enter("%p,%p", cookie, inode);
971
972 if (!mapping || mapping->nrpages == 0) {
973 _leave(" [no pages]");
974 return;
975 }
976
977 pagevec_init(&pvec, 0);
978 next = 0;
979 do {
980 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
981 break;
982 for (i = 0; i < pagevec_count(&pvec); i++) {
983 struct page *page = pvec.pages[i];
984 next = page->index;
985 if (PageFsCache(page)) {
986 __fscache_wait_on_page_write(cookie, page);
987 __fscache_uncache_page(cookie, page);
988 }
989 }
990 pagevec_release(&pvec);
991 cond_resched();
992 } while (++next);
993
994 _leave("");
995}
996EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* Cache page management and data I/O routines
3 *
4 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#define FSCACHE_DEBUG_LEVEL PAGE
9#include <linux/module.h>
10#include <linux/fscache-cache.h>
11#include <linux/buffer_head.h>
12#include <linux/pagevec.h>
13#include <linux/slab.h>
14#include "internal.h"
15
16/*
17 * check to see if a page is being written to the cache
18 */
19bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
20{
21 void *val;
22
23 rcu_read_lock();
24 val = radix_tree_lookup(&cookie->stores, page->index);
25 rcu_read_unlock();
26 trace_fscache_check_page(cookie, page, val, 0);
27
28 return val != NULL;
29}
30EXPORT_SYMBOL(__fscache_check_page_write);
31
32/*
33 * wait for a page to finish being written to the cache
34 */
35void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
36{
37 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
38
39 trace_fscache_page(cookie, page, fscache_page_write_wait);
40
41 wait_event(*wq, !__fscache_check_page_write(cookie, page));
42}
43EXPORT_SYMBOL(__fscache_wait_on_page_write);
44
45/*
46 * wait for a page to finish being written to the cache. Put a timeout here
47 * since we might be called recursively via parent fs.
48 */
49static
50bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
51{
52 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
53
54 return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
55 HZ);
56}
57
58/*
59 * decide whether a page can be released, possibly by cancelling a store to it
60 * - we're allowed to sleep if __GFP_DIRECT_RECLAIM is flagged
61 */
62bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
63 struct page *page,
64 gfp_t gfp)
65{
66 struct page *xpage;
67 void *val;
68
69 _enter("%p,%p,%x", cookie, page, gfp);
70
71 trace_fscache_page(cookie, page, fscache_page_maybe_release);
72
73try_again:
74 rcu_read_lock();
75 val = radix_tree_lookup(&cookie->stores, page->index);
76 if (!val) {
77 rcu_read_unlock();
78 fscache_stat(&fscache_n_store_vmscan_not_storing);
79 __fscache_uncache_page(cookie, page);
80 return true;
81 }
82
83 /* see if the page is actually undergoing storage - if so we can't get
84 * rid of it till the cache has finished with it */
85 if (radix_tree_tag_get(&cookie->stores, page->index,
86 FSCACHE_COOKIE_STORING_TAG)) {
87 rcu_read_unlock();
88 goto page_busy;
89 }
90
91 /* the page is pending storage, so we attempt to cancel the store and
92 * discard the store request so that the page can be reclaimed */
93 spin_lock(&cookie->stores_lock);
94 rcu_read_unlock();
95
96 if (radix_tree_tag_get(&cookie->stores, page->index,
97 FSCACHE_COOKIE_STORING_TAG)) {
98 /* the page started to undergo storage whilst we were looking,
99 * so now we can only wait or return */
100 spin_unlock(&cookie->stores_lock);
101 goto page_busy;
102 }
103
104 xpage = radix_tree_delete(&cookie->stores, page->index);
105 trace_fscache_page(cookie, page, fscache_page_radix_delete);
106 spin_unlock(&cookie->stores_lock);
107
108 if (xpage) {
109 fscache_stat(&fscache_n_store_vmscan_cancelled);
110 fscache_stat(&fscache_n_store_radix_deletes);
111 ASSERTCMP(xpage, ==, page);
112 } else {
113 fscache_stat(&fscache_n_store_vmscan_gone);
114 }
115
116 wake_up_bit(&cookie->flags, 0);
117 trace_fscache_wake_cookie(cookie);
118 if (xpage)
119 put_page(xpage);
120 __fscache_uncache_page(cookie, page);
121 return true;
122
123page_busy:
124 /* We will wait here if we're allowed to, but that could deadlock the
125 * allocator as the work threads writing to the cache may all end up
126 * sleeping on memory allocation, so we may need to impose a timeout
127 * too. */
128 if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS)) {
129 fscache_stat(&fscache_n_store_vmscan_busy);
130 return false;
131 }
132
133 fscache_stat(&fscache_n_store_vmscan_wait);
134 if (!release_page_wait_timeout(cookie, page))
135 _debug("fscache writeout timeout page: %p{%lx}",
136 page, page->index);
137
138 gfp &= ~__GFP_DIRECT_RECLAIM;
139 goto try_again;
140}
141EXPORT_SYMBOL(__fscache_maybe_release_page);
142
143/*
144 * note that a page has finished being written to the cache
145 */
146static void fscache_end_page_write(struct fscache_object *object,
147 struct page *page)
148{
149 struct fscache_cookie *cookie;
150 struct page *xpage = NULL, *val;
151
152 spin_lock(&object->lock);
153 cookie = object->cookie;
154 if (cookie) {
155 /* delete the page from the tree if it is now no longer
156 * pending */
157 spin_lock(&cookie->stores_lock);
158 radix_tree_tag_clear(&cookie->stores, page->index,
159 FSCACHE_COOKIE_STORING_TAG);
160 trace_fscache_page(cookie, page, fscache_page_radix_clear_store);
161 if (!radix_tree_tag_get(&cookie->stores, page->index,
162 FSCACHE_COOKIE_PENDING_TAG)) {
163 fscache_stat(&fscache_n_store_radix_deletes);
164 xpage = radix_tree_delete(&cookie->stores, page->index);
165 trace_fscache_page(cookie, page, fscache_page_radix_delete);
166 trace_fscache_page(cookie, page, fscache_page_write_end);
167
168 val = radix_tree_lookup(&cookie->stores, page->index);
169 trace_fscache_check_page(cookie, page, val, 1);
170 } else {
171 trace_fscache_page(cookie, page, fscache_page_write_end_pend);
172 }
173 spin_unlock(&cookie->stores_lock);
174 wake_up_bit(&cookie->flags, 0);
175 trace_fscache_wake_cookie(cookie);
176 } else {
177 trace_fscache_page(cookie, page, fscache_page_write_end_noc);
178 }
179 spin_unlock(&object->lock);
180 if (xpage)
181 put_page(xpage);
182}
183
184/*
185 * actually apply the changed attributes to a cache object
186 */
187static void fscache_attr_changed_op(struct fscache_operation *op)
188{
189 struct fscache_object *object = op->object;
190 int ret;
191
192 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
193
194 fscache_stat(&fscache_n_attr_changed_calls);
195
196 if (fscache_object_is_active(object)) {
197 fscache_stat(&fscache_n_cop_attr_changed);
198 ret = object->cache->ops->attr_changed(object);
199 fscache_stat_d(&fscache_n_cop_attr_changed);
200 if (ret < 0)
201 fscache_abort_object(object);
202 fscache_op_complete(op, ret < 0);
203 } else {
204 fscache_op_complete(op, true);
205 }
206
207 _leave("");
208}
209
210/*
211 * notification that the attributes on an object have changed
212 */
213int __fscache_attr_changed(struct fscache_cookie *cookie)
214{
215 struct fscache_operation *op;
216 struct fscache_object *object;
217 bool wake_cookie = false;
218
219 _enter("%p", cookie);
220
221 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
222
223 fscache_stat(&fscache_n_attr_changed);
224
225 op = kzalloc(sizeof(*op), GFP_KERNEL);
226 if (!op) {
227 fscache_stat(&fscache_n_attr_changed_nomem);
228 _leave(" = -ENOMEM");
229 return -ENOMEM;
230 }
231
232 fscache_operation_init(cookie, op, fscache_attr_changed_op, NULL, NULL);
233 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_attr_changed);
234 op->flags = FSCACHE_OP_ASYNC |
235 (1 << FSCACHE_OP_EXCLUSIVE) |
236 (1 << FSCACHE_OP_UNUSE_COOKIE);
237
238 spin_lock(&cookie->lock);
239
240 if (!fscache_cookie_enabled(cookie) ||
241 hlist_empty(&cookie->backing_objects))
242 goto nobufs;
243 object = hlist_entry(cookie->backing_objects.first,
244 struct fscache_object, cookie_link);
245
246 __fscache_use_cookie(cookie);
247 if (fscache_submit_exclusive_op(object, op) < 0)
248 goto nobufs_dec;
249 spin_unlock(&cookie->lock);
250 fscache_stat(&fscache_n_attr_changed_ok);
251 fscache_put_operation(op);
252 _leave(" = 0");
253 return 0;
254
255nobufs_dec:
256 wake_cookie = __fscache_unuse_cookie(cookie);
257nobufs:
258 spin_unlock(&cookie->lock);
259 fscache_put_operation(op);
260 if (wake_cookie)
261 __fscache_wake_unused_cookie(cookie);
262 fscache_stat(&fscache_n_attr_changed_nobufs);
263 _leave(" = %d", -ENOBUFS);
264 return -ENOBUFS;
265}
266EXPORT_SYMBOL(__fscache_attr_changed);
267
268/*
269 * Handle cancellation of a pending retrieval op
270 */
271static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
272{
273 struct fscache_retrieval *op =
274 container_of(_op, struct fscache_retrieval, op);
275
276 atomic_set(&op->n_pages, 0);
277}
278
279/*
280 * release a retrieval op reference
281 */
282static void fscache_release_retrieval_op(struct fscache_operation *_op)
283{
284 struct fscache_retrieval *op =
285 container_of(_op, struct fscache_retrieval, op);
286
287 _enter("{OP%x}", op->op.debug_id);
288
289 ASSERTIFCMP(op->op.state != FSCACHE_OP_ST_INITIALISED,
290 atomic_read(&op->n_pages), ==, 0);
291
292 fscache_hist(fscache_retrieval_histogram, op->start_time);
293 if (op->context)
294 fscache_put_context(op->cookie, op->context);
295
296 _leave("");
297}
298
299/*
300 * allocate a retrieval op
301 */
302static struct fscache_retrieval *fscache_alloc_retrieval(
303 struct fscache_cookie *cookie,
304 struct address_space *mapping,
305 fscache_rw_complete_t end_io_func,
306 void *context)
307{
308 struct fscache_retrieval *op;
309
310 /* allocate a retrieval operation and attempt to submit it */
311 op = kzalloc(sizeof(*op), GFP_NOIO);
312 if (!op) {
313 fscache_stat(&fscache_n_retrievals_nomem);
314 return NULL;
315 }
316
317 fscache_operation_init(cookie, &op->op, NULL,
318 fscache_do_cancel_retrieval,
319 fscache_release_retrieval_op);
320 op->op.flags = FSCACHE_OP_MYTHREAD |
321 (1UL << FSCACHE_OP_WAITING) |
322 (1UL << FSCACHE_OP_UNUSE_COOKIE);
323 op->cookie = cookie;
324 op->mapping = mapping;
325 op->end_io_func = end_io_func;
326 op->context = context;
327 op->start_time = jiffies;
328 INIT_LIST_HEAD(&op->to_do);
329
330 /* Pin the netfs read context in case we need to do the actual netfs
331 * read because we've encountered a cache read failure.
332 */
333 if (context)
334 fscache_get_context(op->cookie, context);
335 return op;
336}
337
338/*
339 * wait for a deferred lookup to complete
340 */
341int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
342{
343 unsigned long jif;
344
345 _enter("");
346
347 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
348 _leave(" = 0 [imm]");
349 return 0;
350 }
351
352 fscache_stat(&fscache_n_retrievals_wait);
353
354 jif = jiffies;
355 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
356 TASK_INTERRUPTIBLE) != 0) {
357 fscache_stat(&fscache_n_retrievals_intr);
358 _leave(" = -ERESTARTSYS");
359 return -ERESTARTSYS;
360 }
361
362 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
363
364 smp_rmb();
365 fscache_hist(fscache_retrieval_delay_histogram, jif);
366 _leave(" = 0 [dly]");
367 return 0;
368}
369
370/*
371 * wait for an object to become active (or dead)
372 */
373int fscache_wait_for_operation_activation(struct fscache_object *object,
374 struct fscache_operation *op,
375 atomic_t *stat_op_waits,
376 atomic_t *stat_object_dead)
377{
378 int ret;
379
380 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
381 goto check_if_dead;
382
383 _debug(">>> WT");
384 if (stat_op_waits)
385 fscache_stat(stat_op_waits);
386 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
387 TASK_INTERRUPTIBLE) != 0) {
388 trace_fscache_op(object->cookie, op, fscache_op_signal);
389 ret = fscache_cancel_op(op, false);
390 if (ret == 0)
391 return -ERESTARTSYS;
392
393 /* it's been removed from the pending queue by another party,
394 * so we should get to run shortly */
395 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
396 TASK_UNINTERRUPTIBLE);
397 }
398 _debug("<<< GO");
399
400check_if_dead:
401 if (op->state == FSCACHE_OP_ST_CANCELLED) {
402 if (stat_object_dead)
403 fscache_stat(stat_object_dead);
404 _leave(" = -ENOBUFS [cancelled]");
405 return -ENOBUFS;
406 }
407 if (unlikely(fscache_object_is_dying(object) ||
408 fscache_cache_is_broken(object))) {
409 enum fscache_operation_state state = op->state;
410 trace_fscache_op(object->cookie, op, fscache_op_signal);
411 fscache_cancel_op(op, true);
412 if (stat_object_dead)
413 fscache_stat(stat_object_dead);
414 _leave(" = -ENOBUFS [obj dead %d]", state);
415 return -ENOBUFS;
416 }
417 return 0;
418}
419
420/*
421 * read a page from the cache or allocate a block in which to store it
422 * - we return:
423 * -ENOMEM - out of memory, nothing done
424 * -ERESTARTSYS - interrupted
425 * -ENOBUFS - no backing object available in which to cache the block
426 * -ENODATA - no data available in the backing object for this block
427 * 0 - dispatched a read - it'll call end_io_func() when finished
428 */
429int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
430 struct page *page,
431 fscache_rw_complete_t end_io_func,
432 void *context,
433 gfp_t gfp)
434{
435 struct fscache_retrieval *op;
436 struct fscache_object *object;
437 bool wake_cookie = false;
438 int ret;
439
440 _enter("%p,%p,,,", cookie, page);
441
442 fscache_stat(&fscache_n_retrievals);
443
444 if (hlist_empty(&cookie->backing_objects))
445 goto nobufs;
446
447 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
448 _leave(" = -ENOBUFS [invalidating]");
449 return -ENOBUFS;
450 }
451
452 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
453 ASSERTCMP(page, !=, NULL);
454
455 if (fscache_wait_for_deferred_lookup(cookie) < 0)
456 return -ERESTARTSYS;
457
458 op = fscache_alloc_retrieval(cookie, page->mapping,
459 end_io_func, context);
460 if (!op) {
461 _leave(" = -ENOMEM");
462 return -ENOMEM;
463 }
464 atomic_set(&op->n_pages, 1);
465 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_retr_one);
466
467 spin_lock(&cookie->lock);
468
469 if (!fscache_cookie_enabled(cookie) ||
470 hlist_empty(&cookie->backing_objects))
471 goto nobufs_unlock;
472 object = hlist_entry(cookie->backing_objects.first,
473 struct fscache_object, cookie_link);
474
475 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
476
477 __fscache_use_cookie(cookie);
478 atomic_inc(&object->n_reads);
479 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
480
481 if (fscache_submit_op(object, &op->op) < 0)
482 goto nobufs_unlock_dec;
483 spin_unlock(&cookie->lock);
484
485 fscache_stat(&fscache_n_retrieval_ops);
486
487 /* we wait for the operation to become active, and then process it
488 * *here*, in this thread, and not in the thread pool */
489 ret = fscache_wait_for_operation_activation(
490 object, &op->op,
491 __fscache_stat(&fscache_n_retrieval_op_waits),
492 __fscache_stat(&fscache_n_retrievals_object_dead));
493 if (ret < 0)
494 goto error;
495
496 /* ask the cache to honour the operation */
497 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
498 fscache_stat(&fscache_n_cop_allocate_page);
499 ret = object->cache->ops->allocate_page(op, page, gfp);
500 fscache_stat_d(&fscache_n_cop_allocate_page);
501 if (ret == 0)
502 ret = -ENODATA;
503 } else {
504 fscache_stat(&fscache_n_cop_read_or_alloc_page);
505 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
506 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
507 }
508
509error:
510 if (ret == -ENOMEM)
511 fscache_stat(&fscache_n_retrievals_nomem);
512 else if (ret == -ERESTARTSYS)
513 fscache_stat(&fscache_n_retrievals_intr);
514 else if (ret == -ENODATA)
515 fscache_stat(&fscache_n_retrievals_nodata);
516 else if (ret < 0)
517 fscache_stat(&fscache_n_retrievals_nobufs);
518 else
519 fscache_stat(&fscache_n_retrievals_ok);
520
521 fscache_put_retrieval(op);
522 _leave(" = %d", ret);
523 return ret;
524
525nobufs_unlock_dec:
526 atomic_dec(&object->n_reads);
527 wake_cookie = __fscache_unuse_cookie(cookie);
528nobufs_unlock:
529 spin_unlock(&cookie->lock);
530 if (wake_cookie)
531 __fscache_wake_unused_cookie(cookie);
532 fscache_put_retrieval(op);
533nobufs:
534 fscache_stat(&fscache_n_retrievals_nobufs);
535 _leave(" = -ENOBUFS");
536 return -ENOBUFS;
537}
538EXPORT_SYMBOL(__fscache_read_or_alloc_page);
539
540/*
541 * read a list of page from the cache or allocate a block in which to store
542 * them
543 * - we return:
544 * -ENOMEM - out of memory, some pages may be being read
545 * -ERESTARTSYS - interrupted, some pages may be being read
546 * -ENOBUFS - no backing object or space available in which to cache any
547 * pages not being read
548 * -ENODATA - no data available in the backing object for some or all of
549 * the pages
550 * 0 - dispatched a read on all pages
551 *
552 * end_io_func() will be called for each page read from the cache as it is
553 * finishes being read
554 *
555 * any pages for which a read is dispatched will be removed from pages and
556 * nr_pages
557 */
558int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
559 struct address_space *mapping,
560 struct list_head *pages,
561 unsigned *nr_pages,
562 fscache_rw_complete_t end_io_func,
563 void *context,
564 gfp_t gfp)
565{
566 struct fscache_retrieval *op;
567 struct fscache_object *object;
568 bool wake_cookie = false;
569 int ret;
570
571 _enter("%p,,%d,,,", cookie, *nr_pages);
572
573 fscache_stat(&fscache_n_retrievals);
574
575 if (hlist_empty(&cookie->backing_objects))
576 goto nobufs;
577
578 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
579 _leave(" = -ENOBUFS [invalidating]");
580 return -ENOBUFS;
581 }
582
583 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
584 ASSERTCMP(*nr_pages, >, 0);
585 ASSERT(!list_empty(pages));
586
587 if (fscache_wait_for_deferred_lookup(cookie) < 0)
588 return -ERESTARTSYS;
589
590 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
591 if (!op)
592 return -ENOMEM;
593 atomic_set(&op->n_pages, *nr_pages);
594 trace_fscache_page_op(cookie, NULL, &op->op, fscache_page_op_retr_multi);
595
596 spin_lock(&cookie->lock);
597
598 if (!fscache_cookie_enabled(cookie) ||
599 hlist_empty(&cookie->backing_objects))
600 goto nobufs_unlock;
601 object = hlist_entry(cookie->backing_objects.first,
602 struct fscache_object, cookie_link);
603
604 __fscache_use_cookie(cookie);
605 atomic_inc(&object->n_reads);
606 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
607
608 if (fscache_submit_op(object, &op->op) < 0)
609 goto nobufs_unlock_dec;
610 spin_unlock(&cookie->lock);
611
612 fscache_stat(&fscache_n_retrieval_ops);
613
614 /* we wait for the operation to become active, and then process it
615 * *here*, in this thread, and not in the thread pool */
616 ret = fscache_wait_for_operation_activation(
617 object, &op->op,
618 __fscache_stat(&fscache_n_retrieval_op_waits),
619 __fscache_stat(&fscache_n_retrievals_object_dead));
620 if (ret < 0)
621 goto error;
622
623 /* ask the cache to honour the operation */
624 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
625 fscache_stat(&fscache_n_cop_allocate_pages);
626 ret = object->cache->ops->allocate_pages(
627 op, pages, nr_pages, gfp);
628 fscache_stat_d(&fscache_n_cop_allocate_pages);
629 } else {
630 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
631 ret = object->cache->ops->read_or_alloc_pages(
632 op, pages, nr_pages, gfp);
633 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
634 }
635
636error:
637 if (ret == -ENOMEM)
638 fscache_stat(&fscache_n_retrievals_nomem);
639 else if (ret == -ERESTARTSYS)
640 fscache_stat(&fscache_n_retrievals_intr);
641 else if (ret == -ENODATA)
642 fscache_stat(&fscache_n_retrievals_nodata);
643 else if (ret < 0)
644 fscache_stat(&fscache_n_retrievals_nobufs);
645 else
646 fscache_stat(&fscache_n_retrievals_ok);
647
648 fscache_put_retrieval(op);
649 _leave(" = %d", ret);
650 return ret;
651
652nobufs_unlock_dec:
653 atomic_dec(&object->n_reads);
654 wake_cookie = __fscache_unuse_cookie(cookie);
655nobufs_unlock:
656 spin_unlock(&cookie->lock);
657 fscache_put_retrieval(op);
658 if (wake_cookie)
659 __fscache_wake_unused_cookie(cookie);
660nobufs:
661 fscache_stat(&fscache_n_retrievals_nobufs);
662 _leave(" = -ENOBUFS");
663 return -ENOBUFS;
664}
665EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
666
667/*
668 * allocate a block in the cache on which to store a page
669 * - we return:
670 * -ENOMEM - out of memory, nothing done
671 * -ERESTARTSYS - interrupted
672 * -ENOBUFS - no backing object available in which to cache the block
673 * 0 - block allocated
674 */
675int __fscache_alloc_page(struct fscache_cookie *cookie,
676 struct page *page,
677 gfp_t gfp)
678{
679 struct fscache_retrieval *op;
680 struct fscache_object *object;
681 bool wake_cookie = false;
682 int ret;
683
684 _enter("%p,%p,,,", cookie, page);
685
686 fscache_stat(&fscache_n_allocs);
687
688 if (hlist_empty(&cookie->backing_objects))
689 goto nobufs;
690
691 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
692 ASSERTCMP(page, !=, NULL);
693
694 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
695 _leave(" = -ENOBUFS [invalidating]");
696 return -ENOBUFS;
697 }
698
699 if (fscache_wait_for_deferred_lookup(cookie) < 0)
700 return -ERESTARTSYS;
701
702 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
703 if (!op)
704 return -ENOMEM;
705 atomic_set(&op->n_pages, 1);
706 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_alloc_one);
707
708 spin_lock(&cookie->lock);
709
710 if (!fscache_cookie_enabled(cookie) ||
711 hlist_empty(&cookie->backing_objects))
712 goto nobufs_unlock;
713 object = hlist_entry(cookie->backing_objects.first,
714 struct fscache_object, cookie_link);
715
716 __fscache_use_cookie(cookie);
717 if (fscache_submit_op(object, &op->op) < 0)
718 goto nobufs_unlock_dec;
719 spin_unlock(&cookie->lock);
720
721 fscache_stat(&fscache_n_alloc_ops);
722
723 ret = fscache_wait_for_operation_activation(
724 object, &op->op,
725 __fscache_stat(&fscache_n_alloc_op_waits),
726 __fscache_stat(&fscache_n_allocs_object_dead));
727 if (ret < 0)
728 goto error;
729
730 /* ask the cache to honour the operation */
731 fscache_stat(&fscache_n_cop_allocate_page);
732 ret = object->cache->ops->allocate_page(op, page, gfp);
733 fscache_stat_d(&fscache_n_cop_allocate_page);
734
735error:
736 if (ret == -ERESTARTSYS)
737 fscache_stat(&fscache_n_allocs_intr);
738 else if (ret < 0)
739 fscache_stat(&fscache_n_allocs_nobufs);
740 else
741 fscache_stat(&fscache_n_allocs_ok);
742
743 fscache_put_retrieval(op);
744 _leave(" = %d", ret);
745 return ret;
746
747nobufs_unlock_dec:
748 wake_cookie = __fscache_unuse_cookie(cookie);
749nobufs_unlock:
750 spin_unlock(&cookie->lock);
751 fscache_put_retrieval(op);
752 if (wake_cookie)
753 __fscache_wake_unused_cookie(cookie);
754nobufs:
755 fscache_stat(&fscache_n_allocs_nobufs);
756 _leave(" = -ENOBUFS");
757 return -ENOBUFS;
758}
759EXPORT_SYMBOL(__fscache_alloc_page);
760
761/*
762 * Unmark pages allocate in the readahead code path (via:
763 * fscache_readpages_or_alloc) after delegating to the base filesystem
764 */
765void __fscache_readpages_cancel(struct fscache_cookie *cookie,
766 struct list_head *pages)
767{
768 struct page *page;
769
770 list_for_each_entry(page, pages, lru) {
771 if (PageFsCache(page))
772 __fscache_uncache_page(cookie, page);
773 }
774}
775EXPORT_SYMBOL(__fscache_readpages_cancel);
776
777/*
778 * release a write op reference
779 */
780static void fscache_release_write_op(struct fscache_operation *_op)
781{
782 _enter("{OP%x}", _op->debug_id);
783}
784
785/*
786 * perform the background storage of a page into the cache
787 */
788static void fscache_write_op(struct fscache_operation *_op)
789{
790 struct fscache_storage *op =
791 container_of(_op, struct fscache_storage, op);
792 struct fscache_object *object = op->op.object;
793 struct fscache_cookie *cookie;
794 struct page *page;
795 unsigned n;
796 void *results[1];
797 int ret;
798
799 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
800
801again:
802 spin_lock(&object->lock);
803 cookie = object->cookie;
804
805 if (!fscache_object_is_active(object)) {
806 /* If we get here, then the on-disk cache object likely no
807 * longer exists, so we should just cancel this write
808 * operation.
809 */
810 spin_unlock(&object->lock);
811 fscache_op_complete(&op->op, true);
812 _leave(" [inactive]");
813 return;
814 }
815
816 if (!cookie) {
817 /* If we get here, then the cookie belonging to the object was
818 * detached, probably by the cookie being withdrawn due to
819 * memory pressure, which means that the pages we might write
820 * to the cache from no longer exist - therefore, we can just
821 * cancel this write operation.
822 */
823 spin_unlock(&object->lock);
824 fscache_op_complete(&op->op, true);
825 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
826 _op->flags, _op->state, object->state->short_name,
827 object->flags);
828 return;
829 }
830
831 spin_lock(&cookie->stores_lock);
832
833 fscache_stat(&fscache_n_store_calls);
834
835 /* find a page to store */
836 results[0] = NULL;
837 page = NULL;
838 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
839 FSCACHE_COOKIE_PENDING_TAG);
840 trace_fscache_gang_lookup(cookie, &op->op, results, n, op->store_limit);
841 if (n != 1)
842 goto superseded;
843 page = results[0];
844 _debug("gang %d [%lx]", n, page->index);
845
846 radix_tree_tag_set(&cookie->stores, page->index,
847 FSCACHE_COOKIE_STORING_TAG);
848 radix_tree_tag_clear(&cookie->stores, page->index,
849 FSCACHE_COOKIE_PENDING_TAG);
850 trace_fscache_page(cookie, page, fscache_page_radix_pend2store);
851
852 spin_unlock(&cookie->stores_lock);
853 spin_unlock(&object->lock);
854
855 if (page->index >= op->store_limit)
856 goto discard_page;
857
858 fscache_stat(&fscache_n_store_pages);
859 fscache_stat(&fscache_n_cop_write_page);
860 ret = object->cache->ops->write_page(op, page);
861 fscache_stat_d(&fscache_n_cop_write_page);
862 trace_fscache_wrote_page(cookie, page, &op->op, ret);
863 fscache_end_page_write(object, page);
864 if (ret < 0) {
865 fscache_abort_object(object);
866 fscache_op_complete(&op->op, true);
867 } else {
868 fscache_enqueue_operation(&op->op);
869 }
870
871 _leave("");
872 return;
873
874discard_page:
875 fscache_stat(&fscache_n_store_pages_over_limit);
876 trace_fscache_wrote_page(cookie, page, &op->op, -ENOBUFS);
877 fscache_end_page_write(object, page);
878 goto again;
879
880superseded:
881 /* this writer is going away and there aren't any more things to
882 * write */
883 _debug("cease");
884 spin_unlock(&cookie->stores_lock);
885 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
886 spin_unlock(&object->lock);
887 fscache_op_complete(&op->op, false);
888 _leave("");
889}
890
891/*
892 * Clear the pages pending writing for invalidation
893 */
894void fscache_invalidate_writes(struct fscache_cookie *cookie)
895{
896 struct page *page;
897 void *results[16];
898 int n, i;
899
900 _enter("");
901
902 for (;;) {
903 spin_lock(&cookie->stores_lock);
904 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
905 ARRAY_SIZE(results),
906 FSCACHE_COOKIE_PENDING_TAG);
907 if (n == 0) {
908 spin_unlock(&cookie->stores_lock);
909 break;
910 }
911
912 for (i = n - 1; i >= 0; i--) {
913 page = results[i];
914 radix_tree_delete(&cookie->stores, page->index);
915 trace_fscache_page(cookie, page, fscache_page_radix_delete);
916 trace_fscache_page(cookie, page, fscache_page_inval);
917 }
918
919 spin_unlock(&cookie->stores_lock);
920
921 for (i = n - 1; i >= 0; i--)
922 put_page(results[i]);
923 }
924
925 wake_up_bit(&cookie->flags, 0);
926 trace_fscache_wake_cookie(cookie);
927
928 _leave("");
929}
930
931/*
932 * request a page be stored in the cache
933 * - returns:
934 * -ENOMEM - out of memory, nothing done
935 * -ENOBUFS - no backing object available in which to cache the page
936 * 0 - dispatched a write - it'll call end_io_func() when finished
937 *
938 * if the cookie still has a backing object at this point, that object can be
939 * in one of a few states with respect to storage processing:
940 *
941 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
942 * set)
943 *
944 * (a) no writes yet
945 *
946 * (b) writes deferred till post-creation (mark page for writing and
947 * return immediately)
948 *
949 * (2) negative lookup, object created, initial fill being made from netfs
950 *
951 * (a) fill point not yet reached this page (mark page for writing and
952 * return)
953 *
954 * (b) fill point passed this page (queue op to store this page)
955 *
956 * (3) object extant (queue op to store this page)
957 *
958 * any other state is invalid
959 */
960int __fscache_write_page(struct fscache_cookie *cookie,
961 struct page *page,
962 loff_t object_size,
963 gfp_t gfp)
964{
965 struct fscache_storage *op;
966 struct fscache_object *object;
967 bool wake_cookie = false;
968 int ret;
969
970 _enter("%p,%x,", cookie, (u32) page->flags);
971
972 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
973 ASSERT(PageFsCache(page));
974
975 fscache_stat(&fscache_n_stores);
976
977 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
978 _leave(" = -ENOBUFS [invalidating]");
979 return -ENOBUFS;
980 }
981
982 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
983 if (!op)
984 goto nomem;
985
986 fscache_operation_init(cookie, &op->op, fscache_write_op, NULL,
987 fscache_release_write_op);
988 op->op.flags = FSCACHE_OP_ASYNC |
989 (1 << FSCACHE_OP_WAITING) |
990 (1 << FSCACHE_OP_UNUSE_COOKIE);
991
992 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
993 if (ret < 0)
994 goto nomem_free;
995
996 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_write_one);
997
998 ret = -ENOBUFS;
999 spin_lock(&cookie->lock);
1000
1001 if (!fscache_cookie_enabled(cookie) ||
1002 hlist_empty(&cookie->backing_objects))
1003 goto nobufs;
1004 object = hlist_entry(cookie->backing_objects.first,
1005 struct fscache_object, cookie_link);
1006 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
1007 goto nobufs;
1008
1009 trace_fscache_page(cookie, page, fscache_page_write);
1010
1011 /* add the page to the pending-storage radix tree on the backing
1012 * object */
1013 spin_lock(&object->lock);
1014
1015 if (object->store_limit_l != object_size)
1016 fscache_set_store_limit(object, object_size);
1017
1018 spin_lock(&cookie->stores_lock);
1019
1020 _debug("store limit %llx", (unsigned long long) object->store_limit);
1021
1022 ret = radix_tree_insert(&cookie->stores, page->index, page);
1023 if (ret < 0) {
1024 if (ret == -EEXIST)
1025 goto already_queued;
1026 _debug("insert failed %d", ret);
1027 goto nobufs_unlock_obj;
1028 }
1029
1030 trace_fscache_page(cookie, page, fscache_page_radix_insert);
1031 radix_tree_tag_set(&cookie->stores, page->index,
1032 FSCACHE_COOKIE_PENDING_TAG);
1033 trace_fscache_page(cookie, page, fscache_page_radix_set_pend);
1034 get_page(page);
1035
1036 /* we only want one writer at a time, but we do need to queue new
1037 * writers after exclusive ops */
1038 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
1039 goto already_pending;
1040
1041 spin_unlock(&cookie->stores_lock);
1042 spin_unlock(&object->lock);
1043
1044 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
1045 op->store_limit = object->store_limit;
1046
1047 __fscache_use_cookie(cookie);
1048 if (fscache_submit_op(object, &op->op) < 0)
1049 goto submit_failed;
1050
1051 spin_unlock(&cookie->lock);
1052 radix_tree_preload_end();
1053 fscache_stat(&fscache_n_store_ops);
1054 fscache_stat(&fscache_n_stores_ok);
1055
1056 /* the work queue now carries its own ref on the object */
1057 fscache_put_operation(&op->op);
1058 _leave(" = 0");
1059 return 0;
1060
1061already_queued:
1062 fscache_stat(&fscache_n_stores_again);
1063already_pending:
1064 spin_unlock(&cookie->stores_lock);
1065 spin_unlock(&object->lock);
1066 spin_unlock(&cookie->lock);
1067 radix_tree_preload_end();
1068 fscache_put_operation(&op->op);
1069 fscache_stat(&fscache_n_stores_ok);
1070 _leave(" = 0");
1071 return 0;
1072
1073submit_failed:
1074 spin_lock(&cookie->stores_lock);
1075 radix_tree_delete(&cookie->stores, page->index);
1076 trace_fscache_page(cookie, page, fscache_page_radix_delete);
1077 spin_unlock(&cookie->stores_lock);
1078 wake_cookie = __fscache_unuse_cookie(cookie);
1079 put_page(page);
1080 ret = -ENOBUFS;
1081 goto nobufs;
1082
1083nobufs_unlock_obj:
1084 spin_unlock(&cookie->stores_lock);
1085 spin_unlock(&object->lock);
1086nobufs:
1087 spin_unlock(&cookie->lock);
1088 radix_tree_preload_end();
1089 fscache_put_operation(&op->op);
1090 if (wake_cookie)
1091 __fscache_wake_unused_cookie(cookie);
1092 fscache_stat(&fscache_n_stores_nobufs);
1093 _leave(" = -ENOBUFS");
1094 return -ENOBUFS;
1095
1096nomem_free:
1097 fscache_put_operation(&op->op);
1098nomem:
1099 fscache_stat(&fscache_n_stores_oom);
1100 _leave(" = -ENOMEM");
1101 return -ENOMEM;
1102}
1103EXPORT_SYMBOL(__fscache_write_page);
1104
1105/*
1106 * remove a page from the cache
1107 */
1108void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1109{
1110 struct fscache_object *object;
1111
1112 _enter(",%p", page);
1113
1114 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1115 ASSERTCMP(page, !=, NULL);
1116
1117 fscache_stat(&fscache_n_uncaches);
1118
1119 /* cache withdrawal may beat us to it */
1120 if (!PageFsCache(page))
1121 goto done;
1122
1123 trace_fscache_page(cookie, page, fscache_page_uncache);
1124
1125 /* get the object */
1126 spin_lock(&cookie->lock);
1127
1128 if (hlist_empty(&cookie->backing_objects)) {
1129 ClearPageFsCache(page);
1130 goto done_unlock;
1131 }
1132
1133 object = hlist_entry(cookie->backing_objects.first,
1134 struct fscache_object, cookie_link);
1135
1136 /* there might now be stuff on disk we could read */
1137 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1138
1139 /* only invoke the cache backend if we managed to mark the page
1140 * uncached here; this deals with synchronisation vs withdrawal */
1141 if (TestClearPageFsCache(page) &&
1142 object->cache->ops->uncache_page) {
1143 /* the cache backend releases the cookie lock */
1144 fscache_stat(&fscache_n_cop_uncache_page);
1145 object->cache->ops->uncache_page(object, page);
1146 fscache_stat_d(&fscache_n_cop_uncache_page);
1147 goto done;
1148 }
1149
1150done_unlock:
1151 spin_unlock(&cookie->lock);
1152done:
1153 _leave("");
1154}
1155EXPORT_SYMBOL(__fscache_uncache_page);
1156
1157/**
1158 * fscache_mark_page_cached - Mark a page as being cached
1159 * @op: The retrieval op pages are being marked for
1160 * @page: The page to be marked
1161 *
1162 * Mark a netfs page as being cached. After this is called, the netfs
1163 * must call fscache_uncache_page() to remove the mark.
1164 */
1165void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1166{
1167 struct fscache_cookie *cookie = op->op.object->cookie;
1168
1169#ifdef CONFIG_FSCACHE_STATS
1170 atomic_inc(&fscache_n_marks);
1171#endif
1172
1173 trace_fscache_page(cookie, page, fscache_page_cached);
1174
1175 _debug("- mark %p{%lx}", page, page->index);
1176 if (TestSetPageFsCache(page)) {
1177 static bool once_only;
1178 if (!once_only) {
1179 once_only = true;
1180 pr_warn("Cookie type %s marked page %lx multiple times\n",
1181 cookie->def->name, page->index);
1182 }
1183 }
1184
1185 if (cookie->def->mark_page_cached)
1186 cookie->def->mark_page_cached(cookie->netfs_data,
1187 op->mapping, page);
1188}
1189EXPORT_SYMBOL(fscache_mark_page_cached);
1190
1191/**
1192 * fscache_mark_pages_cached - Mark pages as being cached
1193 * @op: The retrieval op pages are being marked for
1194 * @pagevec: The pages to be marked
1195 *
1196 * Mark a bunch of netfs pages as being cached. After this is called,
1197 * the netfs must call fscache_uncache_page() to remove the mark.
1198 */
1199void fscache_mark_pages_cached(struct fscache_retrieval *op,
1200 struct pagevec *pagevec)
1201{
1202 unsigned long loop;
1203
1204 for (loop = 0; loop < pagevec->nr; loop++)
1205 fscache_mark_page_cached(op, pagevec->pages[loop]);
1206
1207 pagevec_reinit(pagevec);
1208}
1209EXPORT_SYMBOL(fscache_mark_pages_cached);
1210
1211/*
1212 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1213 * to be associated with the given cookie.
1214 */
1215void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1216 struct inode *inode)
1217{
1218 struct address_space *mapping = inode->i_mapping;
1219 struct pagevec pvec;
1220 pgoff_t next;
1221 int i;
1222
1223 _enter("%p,%p", cookie, inode);
1224
1225 if (!mapping || mapping->nrpages == 0) {
1226 _leave(" [no pages]");
1227 return;
1228 }
1229
1230 pagevec_init(&pvec);
1231 next = 0;
1232 do {
1233 if (!pagevec_lookup(&pvec, mapping, &next))
1234 break;
1235 for (i = 0; i < pagevec_count(&pvec); i++) {
1236 struct page *page = pvec.pages[i];
1237 if (PageFsCache(page)) {
1238 __fscache_wait_on_page_write(cookie, page);
1239 __fscache_uncache_page(cookie, page);
1240 }
1241 }
1242 pagevec_release(&pvec);
1243 cond_resched();
1244 } while (next);
1245
1246 _leave("");
1247}
1248EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);