blob: d479ab3c63e487ba097ff2b865c34401a9fcbfcb [file] [log] [blame]
David Howellsb5108822009-04-03 16:42:39 +01001/* 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howellsb5108822009-04-03 16:42:39 +010018#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/*
David Howells201a1542009-11-19 18:11:35 +000047 * 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
David Howells8c209ce2012-12-05 13:34:49 +000059try_again:
David Howells201a1542009-11-19 18:11:35 +000060 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:
David Howells8c209ce2012-12-05 13:34:49 +0000108 /* 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. */
David Howells0c59a952013-05-10 19:50:25 +0100112 if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
David Howells8c209ce2012-12-05 13:34:49 +0000113 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;
David Howells201a1542009-11-19 18:11:35 +0000121}
122EXPORT_SYMBOL(__fscache_maybe_release_page);
123
124/*
David Howellsb5108822009-04-03 16:42:39 +0100125 * note that a page has finished being written to the cache
126 */
David Howells1bccf512009-11-19 18:11:25 +0000127static void fscache_end_page_write(struct fscache_object *object,
128 struct page *page)
David Howellsb5108822009-04-03 16:42:39 +0100129{
David Howells1bccf512009-11-19 18:11:25 +0000130 struct fscache_cookie *cookie;
131 struct page *xpage = NULL;
David Howellsb5108822009-04-03 16:42:39 +0100132
David Howells1bccf512009-11-19 18:11:25 +0000133 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);
David Howells201a1542009-11-19 18:11:35 +0000139 radix_tree_tag_clear(&cookie->stores, page->index,
140 FSCACHE_COOKIE_STORING_TAG);
David Howells285e7282009-11-19 18:11:29 +0000141 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 }
David Howells1bccf512009-11-19 18:11:25 +0000146 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);
David Howellsb5108822009-04-03 16:42:39 +0100152}
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;
David Howells440f0af2009-11-19 18:11:01 +0000160 int ret;
David Howellsb5108822009-04-03 16:42:39 +0100161
162 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
163
164 fscache_stat(&fscache_n_attr_changed_calls);
165
David Howells13627292013-05-10 19:50:26 +0100166 if (fscache_object_is_active(object) &&
167 fscache_use_cookie(object)) {
David Howells52bd75f2009-11-19 18:11:08 +0000168 fscache_stat(&fscache_n_cop_attr_changed);
David Howells440f0af2009-11-19 18:11:01 +0000169 ret = object->cache->ops->attr_changed(object);
David Howells52bd75f2009-11-19 18:11:08 +0000170 fscache_stat_d(&fscache_n_cop_attr_changed);
David Howells13627292013-05-10 19:50:26 +0100171 fscache_unuse_cookie(object);
David Howells440f0af2009-11-19 18:11:01 +0000172 if (ret < 0)
173 fscache_abort_object(object);
174 }
David Howellsb5108822009-04-03 16:42:39 +0100175
David Howells1f372df2012-12-13 20:03:13 +0000176 fscache_op_complete(op, true);
David Howellsb5108822009-04-03 16:42:39 +0100177 _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
Tejun Heo8af7c122010-07-20 22:09:01 +0200201 fscache_operation_init(op, fscache_attr_changed_op, NULL);
202 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
David Howellsb5108822009-04-03 16:42:39 +0100203
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
228/*
David Howellsb5108822009-04-03 16:42:39 +0100229 * 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
David Howells1bb4b7f92013-05-21 13:44:15 +0100238 ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
David Howells9f105232012-12-20 21:52:35 +0000239
David Howellsb5108822009-04-03 16:42:39 +0100240 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(
David Howells13627292013-05-10 19:50:26 +0100251 struct fscache_cookie *cookie,
David Howellsb5108822009-04-03 16:42:39 +0100252 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
Tejun Heo8af7c122010-07-20 22:09:01 +0200265 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
David Howells13627292013-05-10 19:50:26 +0100266 atomic_inc(&cookie->n_active);
267 op->op.flags = FSCACHE_OP_MYTHREAD |
268 (1UL << FSCACHE_OP_WAITING) |
269 (1UL << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100270 op->mapping = mapping;
271 op->end_io_func = end_io_func;
272 op->context = context;
273 op->start_time = jiffies;
David Howellsb5108822009-04-03 16:42:39 +0100274 INIT_LIST_HEAD(&op->to_do);
275 return op;
276}
277
278/*
279 * wait for a deferred lookup to complete
280 */
281static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
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
311/*
David Howells91c7fbb2012-12-14 11:02:22 +0000312 * 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
David Howells1bb4b7f92013-05-21 13:44:15 +0100319 atomic_set(&op->n_pages, 0);
David Howells91c7fbb2012-12-14 11:02:22 +0000320}
321
322/*
David Howells60d543c2009-11-19 18:11:45 +0000323 * wait for an object to become active (or dead)
324 */
325static int fscache_wait_for_retrieval_activation(struct fscache_object *object,
326 struct fscache_retrieval *op,
327 atomic_t *stat_op_waits,
328 atomic_t *stat_object_dead)
329{
330 int ret;
331
332 if (!test_bit(FSCACHE_OP_WAITING, &op->op.flags))
333 goto check_if_dead;
334
335 _debug(">>> WT");
336 fscache_stat(stat_op_waits);
337 if (wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
338 fscache_wait_bit_interruptible,
David Howells9c04caa2012-12-07 18:08:02 +0000339 TASK_INTERRUPTIBLE) != 0) {
David Howells91c7fbb2012-12-14 11:02:22 +0000340 ret = fscache_cancel_op(&op->op, fscache_do_cancel_retrieval);
David Howells60d543c2009-11-19 18:11:45 +0000341 if (ret == 0)
342 return -ERESTARTSYS;
343
344 /* it's been removed from the pending queue by another party,
345 * so we should get to run shortly */
346 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
347 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
348 }
349 _debug("<<< GO");
350
351check_if_dead:
David Howells9f105232012-12-20 21:52:35 +0000352 if (op->op.state == FSCACHE_OP_ST_CANCELLED) {
353 fscache_stat(stat_object_dead);
354 _leave(" = -ENOBUFS [cancelled]");
355 return -ENOBUFS;
356 }
David Howells60d543c2009-11-19 18:11:45 +0000357 if (unlikely(fscache_object_is_dead(object))) {
David Howells91c7fbb2012-12-14 11:02:22 +0000358 pr_err("%s() = -ENOBUFS [obj dead %d]\n", __func__, op->op.state);
359 fscache_cancel_op(&op->op, fscache_do_cancel_retrieval);
David Howells60d543c2009-11-19 18:11:45 +0000360 fscache_stat(stat_object_dead);
361 return -ENOBUFS;
362 }
363 return 0;
364}
365
366/*
David Howellsb5108822009-04-03 16:42:39 +0100367 * read a page from the cache or allocate a block in which to store it
368 * - we return:
369 * -ENOMEM - out of memory, nothing done
370 * -ERESTARTSYS - interrupted
371 * -ENOBUFS - no backing object available in which to cache the block
372 * -ENODATA - no data available in the backing object for this block
373 * 0 - dispatched a read - it'll call end_io_func() when finished
374 */
375int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
376 struct page *page,
377 fscache_rw_complete_t end_io_func,
378 void *context,
379 gfp_t gfp)
380{
381 struct fscache_retrieval *op;
382 struct fscache_object *object;
383 int ret;
384
385 _enter("%p,%p,,,", cookie, page);
386
387 fscache_stat(&fscache_n_retrievals);
388
389 if (hlist_empty(&cookie->backing_objects))
390 goto nobufs;
391
David Howellsef778e72012-12-20 21:52:36 +0000392 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
393 _leave(" = -ENOBUFS [invalidating]");
394 return -ENOBUFS;
395 }
396
David Howellsb5108822009-04-03 16:42:39 +0100397 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
398 ASSERTCMP(page, !=, NULL);
399
400 if (fscache_wait_for_deferred_lookup(cookie) < 0)
401 return -ERESTARTSYS;
402
David Howells13627292013-05-10 19:50:26 +0100403 op = fscache_alloc_retrieval(cookie, page->mapping,
404 end_io_func,context);
David Howellsb5108822009-04-03 16:42:39 +0100405 if (!op) {
406 _leave(" = -ENOMEM");
407 return -ENOMEM;
408 }
David Howells1bb4b7f92013-05-21 13:44:15 +0100409 atomic_set(&op->n_pages, 1);
David Howellsb5108822009-04-03 16:42:39 +0100410
411 spin_lock(&cookie->lock);
412
413 if (hlist_empty(&cookie->backing_objects))
414 goto nobufs_unlock;
415 object = hlist_entry(cookie->backing_objects.first,
416 struct fscache_object, cookie_link);
417
David Howellscaaef692013-05-10 19:50:26 +0100418 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
David Howellsb5108822009-04-03 16:42:39 +0100419
David Howells4fbf4292009-11-19 18:11:04 +0000420 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000421 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000422
David Howellsb5108822009-04-03 16:42:39 +0100423 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000424 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100425 spin_unlock(&cookie->lock);
426
427 fscache_stat(&fscache_n_retrieval_ops);
428
429 /* pin the netfs read context in case we need to do the actual netfs
430 * read because we've encountered a cache read failure */
431 fscache_get_context(object->cookie, op->context);
432
433 /* we wait for the operation to become active, and then process it
434 * *here*, in this thread, and not in the thread pool */
David Howells60d543c2009-11-19 18:11:45 +0000435 ret = fscache_wait_for_retrieval_activation(
436 object, op,
437 __fscache_stat(&fscache_n_retrieval_op_waits),
438 __fscache_stat(&fscache_n_retrievals_object_dead));
439 if (ret < 0)
440 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100441
442 /* ask the cache to honour the operation */
443 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
David Howells52bd75f2009-11-19 18:11:08 +0000444 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100445 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000446 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100447 if (ret == 0)
448 ret = -ENODATA;
449 } else {
David Howells52bd75f2009-11-19 18:11:08 +0000450 fscache_stat(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100451 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000452 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100453 }
454
David Howells5753c442009-11-19 18:11:19 +0000455error:
David Howellsb5108822009-04-03 16:42:39 +0100456 if (ret == -ENOMEM)
457 fscache_stat(&fscache_n_retrievals_nomem);
458 else if (ret == -ERESTARTSYS)
459 fscache_stat(&fscache_n_retrievals_intr);
460 else if (ret == -ENODATA)
461 fscache_stat(&fscache_n_retrievals_nodata);
462 else if (ret < 0)
463 fscache_stat(&fscache_n_retrievals_nobufs);
464 else
465 fscache_stat(&fscache_n_retrievals_ok);
466
467 fscache_put_retrieval(op);
468 _leave(" = %d", ret);
469 return ret;
470
David Howells9f105232012-12-20 21:52:35 +0000471nobufs_unlock_dec:
472 atomic_dec(&object->n_reads);
David Howellsb5108822009-04-03 16:42:39 +0100473nobufs_unlock:
474 spin_unlock(&cookie->lock);
David Howells13627292013-05-10 19:50:26 +0100475 atomic_dec(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100476 kfree(op);
477nobufs:
478 fscache_stat(&fscache_n_retrievals_nobufs);
479 _leave(" = -ENOBUFS");
480 return -ENOBUFS;
481}
482EXPORT_SYMBOL(__fscache_read_or_alloc_page);
483
484/*
485 * read a list of page from the cache or allocate a block in which to store
486 * them
487 * - we return:
488 * -ENOMEM - out of memory, some pages may be being read
489 * -ERESTARTSYS - interrupted, some pages may be being read
490 * -ENOBUFS - no backing object or space available in which to cache any
491 * pages not being read
492 * -ENODATA - no data available in the backing object for some or all of
493 * the pages
494 * 0 - dispatched a read on all pages
495 *
496 * end_io_func() will be called for each page read from the cache as it is
497 * finishes being read
498 *
499 * any pages for which a read is dispatched will be removed from pages and
500 * nr_pages
501 */
502int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
503 struct address_space *mapping,
504 struct list_head *pages,
505 unsigned *nr_pages,
506 fscache_rw_complete_t end_io_func,
507 void *context,
508 gfp_t gfp)
509{
David Howellsb5108822009-04-03 16:42:39 +0100510 struct fscache_retrieval *op;
511 struct fscache_object *object;
512 int ret;
513
514 _enter("%p,,%d,,,", cookie, *nr_pages);
515
516 fscache_stat(&fscache_n_retrievals);
517
518 if (hlist_empty(&cookie->backing_objects))
519 goto nobufs;
520
David Howellsef778e72012-12-20 21:52:36 +0000521 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
522 _leave(" = -ENOBUFS [invalidating]");
523 return -ENOBUFS;
524 }
525
David Howellsb5108822009-04-03 16:42:39 +0100526 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
527 ASSERTCMP(*nr_pages, >, 0);
528 ASSERT(!list_empty(pages));
529
530 if (fscache_wait_for_deferred_lookup(cookie) < 0)
531 return -ERESTARTSYS;
532
David Howells13627292013-05-10 19:50:26 +0100533 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
David Howellsb5108822009-04-03 16:42:39 +0100534 if (!op)
535 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100536 atomic_set(&op->n_pages, *nr_pages);
David Howellsb5108822009-04-03 16:42:39 +0100537
538 spin_lock(&cookie->lock);
539
540 if (hlist_empty(&cookie->backing_objects))
541 goto nobufs_unlock;
542 object = hlist_entry(cookie->backing_objects.first,
543 struct fscache_object, cookie_link);
544
David Howells4fbf4292009-11-19 18:11:04 +0000545 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000546 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000547
David Howellsb5108822009-04-03 16:42:39 +0100548 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000549 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100550 spin_unlock(&cookie->lock);
551
552 fscache_stat(&fscache_n_retrieval_ops);
553
554 /* pin the netfs read context in case we need to do the actual netfs
555 * read because we've encountered a cache read failure */
556 fscache_get_context(object->cookie, op->context);
557
558 /* we wait for the operation to become active, and then process it
559 * *here*, in this thread, and not in the thread pool */
David Howells60d543c2009-11-19 18:11:45 +0000560 ret = fscache_wait_for_retrieval_activation(
561 object, op,
562 __fscache_stat(&fscache_n_retrieval_op_waits),
563 __fscache_stat(&fscache_n_retrievals_object_dead));
564 if (ret < 0)
565 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100566
567 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000568 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
569 fscache_stat(&fscache_n_cop_allocate_pages);
570 ret = object->cache->ops->allocate_pages(
571 op, pages, nr_pages, gfp);
572 fscache_stat_d(&fscache_n_cop_allocate_pages);
573 } else {
574 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
575 ret = object->cache->ops->read_or_alloc_pages(
576 op, pages, nr_pages, gfp);
577 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
578 }
David Howellsb5108822009-04-03 16:42:39 +0100579
David Howells5753c442009-11-19 18:11:19 +0000580error:
David Howellsb5108822009-04-03 16:42:39 +0100581 if (ret == -ENOMEM)
582 fscache_stat(&fscache_n_retrievals_nomem);
583 else if (ret == -ERESTARTSYS)
584 fscache_stat(&fscache_n_retrievals_intr);
585 else if (ret == -ENODATA)
586 fscache_stat(&fscache_n_retrievals_nodata);
587 else if (ret < 0)
588 fscache_stat(&fscache_n_retrievals_nobufs);
589 else
590 fscache_stat(&fscache_n_retrievals_ok);
591
592 fscache_put_retrieval(op);
593 _leave(" = %d", ret);
594 return ret;
595
David Howells9f105232012-12-20 21:52:35 +0000596nobufs_unlock_dec:
597 atomic_dec(&object->n_reads);
David Howellsb5108822009-04-03 16:42:39 +0100598nobufs_unlock:
599 spin_unlock(&cookie->lock);
David Howells13627292013-05-10 19:50:26 +0100600 atomic_dec(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100601 kfree(op);
602nobufs:
603 fscache_stat(&fscache_n_retrievals_nobufs);
604 _leave(" = -ENOBUFS");
605 return -ENOBUFS;
606}
607EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
608
609/*
610 * allocate a block in the cache on which to store a page
611 * - we return:
612 * -ENOMEM - out of memory, nothing done
613 * -ERESTARTSYS - interrupted
614 * -ENOBUFS - no backing object available in which to cache the block
615 * 0 - block allocated
616 */
617int __fscache_alloc_page(struct fscache_cookie *cookie,
618 struct page *page,
619 gfp_t gfp)
620{
621 struct fscache_retrieval *op;
622 struct fscache_object *object;
623 int ret;
624
625 _enter("%p,%p,,,", cookie, page);
626
627 fscache_stat(&fscache_n_allocs);
628
629 if (hlist_empty(&cookie->backing_objects))
630 goto nobufs;
631
632 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
633 ASSERTCMP(page, !=, NULL);
634
David Howellsef778e72012-12-20 21:52:36 +0000635 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
636 _leave(" = -ENOBUFS [invalidating]");
637 return -ENOBUFS;
638 }
639
David Howellsb5108822009-04-03 16:42:39 +0100640 if (fscache_wait_for_deferred_lookup(cookie) < 0)
641 return -ERESTARTSYS;
642
David Howells13627292013-05-10 19:50:26 +0100643 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
David Howellsb5108822009-04-03 16:42:39 +0100644 if (!op)
645 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100646 atomic_set(&op->n_pages, 1);
David Howellsb5108822009-04-03 16:42:39 +0100647
648 spin_lock(&cookie->lock);
649
650 if (hlist_empty(&cookie->backing_objects))
651 goto nobufs_unlock;
652 object = hlist_entry(cookie->backing_objects.first,
653 struct fscache_object, cookie_link);
654
655 if (fscache_submit_op(object, &op->op) < 0)
656 goto nobufs_unlock;
657 spin_unlock(&cookie->lock);
658
659 fscache_stat(&fscache_n_alloc_ops);
660
David Howells60d543c2009-11-19 18:11:45 +0000661 ret = fscache_wait_for_retrieval_activation(
662 object, op,
663 __fscache_stat(&fscache_n_alloc_op_waits),
664 __fscache_stat(&fscache_n_allocs_object_dead));
665 if (ret < 0)
666 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100667
668 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000669 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100670 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000671 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100672
David Howells5753c442009-11-19 18:11:19 +0000673error:
674 if (ret == -ERESTARTSYS)
675 fscache_stat(&fscache_n_allocs_intr);
676 else if (ret < 0)
David Howellsb5108822009-04-03 16:42:39 +0100677 fscache_stat(&fscache_n_allocs_nobufs);
678 else
679 fscache_stat(&fscache_n_allocs_ok);
680
681 fscache_put_retrieval(op);
682 _leave(" = %d", ret);
683 return ret;
684
685nobufs_unlock:
686 spin_unlock(&cookie->lock);
David Howells13627292013-05-10 19:50:26 +0100687 atomic_dec(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100688 kfree(op);
689nobufs:
690 fscache_stat(&fscache_n_allocs_nobufs);
691 _leave(" = -ENOBUFS");
692 return -ENOBUFS;
693}
694EXPORT_SYMBOL(__fscache_alloc_page);
695
696/*
697 * release a write op reference
698 */
699static void fscache_release_write_op(struct fscache_operation *_op)
700{
701 _enter("{OP%x}", _op->debug_id);
702}
703
704/*
705 * perform the background storage of a page into the cache
706 */
707static void fscache_write_op(struct fscache_operation *_op)
708{
709 struct fscache_storage *op =
710 container_of(_op, struct fscache_storage, op);
711 struct fscache_object *object = op->op.object;
David Howells1bccf512009-11-19 18:11:25 +0000712 struct fscache_cookie *cookie;
David Howellsb5108822009-04-03 16:42:39 +0100713 struct page *page;
714 unsigned n;
715 void *results[1];
716 int ret;
717
718 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
719
David Howellsb5108822009-04-03 16:42:39 +0100720 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000721 cookie = object->cookie;
David Howellsb5108822009-04-03 16:42:39 +0100722
David Howells7ef001e2012-12-07 10:41:26 +0000723 if (!fscache_object_is_active(object)) {
724 /* If we get here, then the on-disk cache object likely longer
725 * exists, so we should just cancel this write operation.
726 */
David Howellsb5108822009-04-03 16:42:39 +0100727 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000728 fscache_op_complete(&op->op, false);
David Howells7ef001e2012-12-07 10:41:26 +0000729 _leave(" [inactive]");
730 return;
731 }
732
733 if (!cookie) {
734 /* If we get here, then the cookie belonging to the object was
735 * detached, probably by the cookie being withdrawn due to
736 * memory pressure, which means that the pages we might write
737 * to the cache from no longer exist - therefore, we can just
738 * cancel this write operation.
739 */
740 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000741 fscache_op_complete(&op->op, false);
David Howellscaaef692013-05-10 19:50:26 +0100742 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
743 _op->flags, _op->state, object->state->short_name,
744 object->flags);
David Howellsb5108822009-04-03 16:42:39 +0100745 return;
746 }
747
David Howells1bccf512009-11-19 18:11:25 +0000748 spin_lock(&cookie->stores_lock);
749
David Howellsb5108822009-04-03 16:42:39 +0100750 fscache_stat(&fscache_n_store_calls);
751
752 /* find a page to store */
753 page = NULL;
754 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
755 FSCACHE_COOKIE_PENDING_TAG);
756 if (n != 1)
757 goto superseded;
758 page = results[0];
759 _debug("gang %d [%lx]", n, page->index);
David Howells1bccf512009-11-19 18:11:25 +0000760 if (page->index > op->store_limit) {
761 fscache_stat(&fscache_n_store_pages_over_limit);
David Howellsb5108822009-04-03 16:42:39 +0100762 goto superseded;
David Howells1bccf512009-11-19 18:11:25 +0000763 }
David Howellsb5108822009-04-03 16:42:39 +0100764
Dan Carpenter08a66852010-06-01 20:58:22 +0100765 radix_tree_tag_set(&cookie->stores, page->index,
766 FSCACHE_COOKIE_STORING_TAG);
767 radix_tree_tag_clear(&cookie->stores, page->index,
768 FSCACHE_COOKIE_PENDING_TAG);
David Howellsb5108822009-04-03 16:42:39 +0100769
David Howells1bccf512009-11-19 18:11:25 +0000770 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100771 spin_unlock(&object->lock);
David Howellsb5108822009-04-03 16:42:39 +0100772
Dan Carpenter08a66852010-06-01 20:58:22 +0100773 fscache_stat(&fscache_n_store_pages);
774 fscache_stat(&fscache_n_cop_write_page);
775 ret = object->cache->ops->write_page(op, page);
776 fscache_stat_d(&fscache_n_cop_write_page);
Dan Carpenter08a66852010-06-01 20:58:22 +0100777 fscache_end_page_write(object, page);
778 if (ret < 0) {
Dan Carpenter08a66852010-06-01 20:58:22 +0100779 fscache_abort_object(object);
David Howells1f372df2012-12-13 20:03:13 +0000780 fscache_op_complete(&op->op, true);
Dan Carpenter08a66852010-06-01 20:58:22 +0100781 } else {
782 fscache_enqueue_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +0100783 }
784
785 _leave("");
786 return;
787
788superseded:
789 /* this writer is going away and there aren't any more things to
790 * write */
791 _debug("cease");
David Howells1bccf512009-11-19 18:11:25 +0000792 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100793 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
794 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000795 fscache_op_complete(&op->op, true);
David Howellsb5108822009-04-03 16:42:39 +0100796 _leave("");
797}
798
799/*
David Howellsef778e72012-12-20 21:52:36 +0000800 * Clear the pages pending writing for invalidation
801 */
802void fscache_invalidate_writes(struct fscache_cookie *cookie)
803{
804 struct page *page;
805 void *results[16];
806 int n, i;
807
808 _enter("");
809
Sebastian Andrzej Siewioree8be572013-05-10 19:50:24 +0100810 for (;;) {
811 spin_lock(&cookie->stores_lock);
812 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
813 ARRAY_SIZE(results),
814 FSCACHE_COOKIE_PENDING_TAG);
815 if (n == 0) {
816 spin_unlock(&cookie->stores_lock);
817 break;
818 }
819
David Howellsef778e72012-12-20 21:52:36 +0000820 for (i = n - 1; i >= 0; i--) {
821 page = results[i];
822 radix_tree_delete(&cookie->stores, page->index);
823 }
824
825 spin_unlock(&cookie->stores_lock);
826
827 for (i = n - 1; i >= 0; i--)
828 page_cache_release(results[i]);
829 }
830
David Howellsef778e72012-12-20 21:52:36 +0000831 _leave("");
832}
833
834/*
David Howellsb5108822009-04-03 16:42:39 +0100835 * request a page be stored in the cache
836 * - returns:
837 * -ENOMEM - out of memory, nothing done
838 * -ENOBUFS - no backing object available in which to cache the page
839 * 0 - dispatched a write - it'll call end_io_func() when finished
840 *
841 * if the cookie still has a backing object at this point, that object can be
842 * in one of a few states with respect to storage processing:
843 *
844 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
845 * set)
846 *
David Howellscaaef692013-05-10 19:50:26 +0100847 * (a) no writes yet
David Howellsb5108822009-04-03 16:42:39 +0100848 *
849 * (b) writes deferred till post-creation (mark page for writing and
850 * return immediately)
851 *
852 * (2) negative lookup, object created, initial fill being made from netfs
David Howellsb5108822009-04-03 16:42:39 +0100853 *
854 * (a) fill point not yet reached this page (mark page for writing and
855 * return)
856 *
857 * (b) fill point passed this page (queue op to store this page)
858 *
859 * (3) object extant (queue op to store this page)
860 *
861 * any other state is invalid
862 */
863int __fscache_write_page(struct fscache_cookie *cookie,
864 struct page *page,
865 gfp_t gfp)
866{
867 struct fscache_storage *op;
868 struct fscache_object *object;
869 int ret;
870
871 _enter("%p,%x,", cookie, (u32) page->flags);
872
873 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
874 ASSERT(PageFsCache(page));
875
876 fscache_stat(&fscache_n_stores);
877
David Howellsef778e72012-12-20 21:52:36 +0000878 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
879 _leave(" = -ENOBUFS [invalidating]");
880 return -ENOBUFS;
881 }
882
David Howells5f4f9f42012-12-20 21:52:33 +0000883 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
David Howellsb5108822009-04-03 16:42:39 +0100884 if (!op)
885 goto nomem;
886
Tejun Heo8af7c122010-07-20 22:09:01 +0200887 fscache_operation_init(&op->op, fscache_write_op,
888 fscache_release_write_op);
David Howells13627292013-05-10 19:50:26 +0100889 op->op.flags = FSCACHE_OP_ASYNC |
890 (1 << FSCACHE_OP_WAITING) |
891 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100892
893 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
894 if (ret < 0)
895 goto nomem_free;
896
897 ret = -ENOBUFS;
898 spin_lock(&cookie->lock);
899
900 if (hlist_empty(&cookie->backing_objects))
901 goto nobufs;
902 object = hlist_entry(cookie->backing_objects.first,
903 struct fscache_object, cookie_link);
904 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
905 goto nobufs;
906
907 /* add the page to the pending-storage radix tree on the backing
908 * object */
909 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000910 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100911
912 _debug("store limit %llx", (unsigned long long) object->store_limit);
913
914 ret = radix_tree_insert(&cookie->stores, page->index, page);
915 if (ret < 0) {
916 if (ret == -EEXIST)
917 goto already_queued;
918 _debug("insert failed %d", ret);
919 goto nobufs_unlock_obj;
920 }
921
922 radix_tree_tag_set(&cookie->stores, page->index,
923 FSCACHE_COOKIE_PENDING_TAG);
924 page_cache_get(page);
925
926 /* we only want one writer at a time, but we do need to queue new
927 * writers after exclusive ops */
928 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
929 goto already_pending;
930
David Howells1bccf512009-11-19 18:11:25 +0000931 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100932 spin_unlock(&object->lock);
933
934 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
935 op->store_limit = object->store_limit;
936
David Howells13627292013-05-10 19:50:26 +0100937 atomic_inc(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100938 if (fscache_submit_op(object, &op->op) < 0)
939 goto submit_failed;
940
941 spin_unlock(&cookie->lock);
942 radix_tree_preload_end();
943 fscache_stat(&fscache_n_store_ops);
944 fscache_stat(&fscache_n_stores_ok);
945
Tejun Heo8af7c122010-07-20 22:09:01 +0200946 /* the work queue now carries its own ref on the object */
David Howellsb5108822009-04-03 16:42:39 +0100947 fscache_put_operation(&op->op);
948 _leave(" = 0");
949 return 0;
950
951already_queued:
952 fscache_stat(&fscache_n_stores_again);
953already_pending:
David Howells1bccf512009-11-19 18:11:25 +0000954 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100955 spin_unlock(&object->lock);
956 spin_unlock(&cookie->lock);
957 radix_tree_preload_end();
958 kfree(op);
959 fscache_stat(&fscache_n_stores_ok);
960 _leave(" = 0");
961 return 0;
962
963submit_failed:
David Howells13627292013-05-10 19:50:26 +0100964 atomic_dec(&cookie->n_active);
David Howells1bccf512009-11-19 18:11:25 +0000965 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100966 radix_tree_delete(&cookie->stores, page->index);
David Howells1bccf512009-11-19 18:11:25 +0000967 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100968 page_cache_release(page);
969 ret = -ENOBUFS;
970 goto nobufs;
971
972nobufs_unlock_obj:
Dan Carpenter1147d0f2010-03-23 14:48:37 +0000973 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100974 spin_unlock(&object->lock);
975nobufs:
976 spin_unlock(&cookie->lock);
977 radix_tree_preload_end();
978 kfree(op);
979 fscache_stat(&fscache_n_stores_nobufs);
980 _leave(" = -ENOBUFS");
981 return -ENOBUFS;
982
983nomem_free:
984 kfree(op);
985nomem:
986 fscache_stat(&fscache_n_stores_oom);
987 _leave(" = -ENOMEM");
988 return -ENOMEM;
989}
990EXPORT_SYMBOL(__fscache_write_page);
991
992/*
993 * remove a page from the cache
994 */
995void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
996{
997 struct fscache_object *object;
998
999 _enter(",%p", page);
1000
1001 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1002 ASSERTCMP(page, !=, NULL);
1003
1004 fscache_stat(&fscache_n_uncaches);
1005
1006 /* cache withdrawal may beat us to it */
1007 if (!PageFsCache(page))
1008 goto done;
1009
1010 /* get the object */
1011 spin_lock(&cookie->lock);
1012
1013 if (hlist_empty(&cookie->backing_objects)) {
1014 ClearPageFsCache(page);
1015 goto done_unlock;
1016 }
1017
1018 object = hlist_entry(cookie->backing_objects.first,
1019 struct fscache_object, cookie_link);
1020
1021 /* there might now be stuff on disk we could read */
1022 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1023
1024 /* only invoke the cache backend if we managed to mark the page
1025 * uncached here; this deals with synchronisation vs withdrawal */
1026 if (TestClearPageFsCache(page) &&
1027 object->cache->ops->uncache_page) {
1028 /* the cache backend releases the cookie lock */
David Howells52bd75f2009-11-19 18:11:08 +00001029 fscache_stat(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001030 object->cache->ops->uncache_page(object, page);
David Howells52bd75f2009-11-19 18:11:08 +00001031 fscache_stat_d(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001032 goto done;
1033 }
1034
1035done_unlock:
1036 spin_unlock(&cookie->lock);
1037done:
1038 _leave("");
1039}
1040EXPORT_SYMBOL(__fscache_uncache_page);
1041
1042/**
David Howellsc4d6d8d2012-12-20 21:52:32 +00001043 * fscache_mark_page_cached - Mark a page as being cached
1044 * @op: The retrieval op pages are being marked for
1045 * @page: The page to be marked
1046 *
1047 * Mark a netfs page as being cached. After this is called, the netfs
1048 * must call fscache_uncache_page() to remove the mark.
1049 */
1050void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1051{
1052 struct fscache_cookie *cookie = op->op.object->cookie;
1053
1054#ifdef CONFIG_FSCACHE_STATS
1055 atomic_inc(&fscache_n_marks);
1056#endif
1057
1058 _debug("- mark %p{%lx}", page, page->index);
1059 if (TestSetPageFsCache(page)) {
1060 static bool once_only;
1061 if (!once_only) {
1062 once_only = true;
1063 printk(KERN_WARNING "FS-Cache:"
1064 " Cookie type %s marked page %lx"
1065 " multiple times\n",
1066 cookie->def->name, page->index);
1067 }
1068 }
1069
1070 if (cookie->def->mark_page_cached)
1071 cookie->def->mark_page_cached(cookie->netfs_data,
1072 op->mapping, page);
1073}
1074EXPORT_SYMBOL(fscache_mark_page_cached);
1075
1076/**
David Howellsb5108822009-04-03 16:42:39 +01001077 * fscache_mark_pages_cached - Mark pages as being cached
1078 * @op: The retrieval op pages are being marked for
1079 * @pagevec: The pages to be marked
1080 *
1081 * Mark a bunch of netfs pages as being cached. After this is called,
1082 * the netfs must call fscache_uncache_page() to remove the mark.
1083 */
1084void fscache_mark_pages_cached(struct fscache_retrieval *op,
1085 struct pagevec *pagevec)
1086{
David Howellsb5108822009-04-03 16:42:39 +01001087 unsigned long loop;
1088
David Howellsc4d6d8d2012-12-20 21:52:32 +00001089 for (loop = 0; loop < pagevec->nr; loop++)
1090 fscache_mark_page_cached(op, pagevec->pages[loop]);
David Howellsb5108822009-04-03 16:42:39 +01001091
David Howellsb5108822009-04-03 16:42:39 +01001092 pagevec_reinit(pagevec);
1093}
1094EXPORT_SYMBOL(fscache_mark_pages_cached);
David Howellsc902ce12011-07-07 12:19:48 +01001095
1096/*
1097 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1098 * to be associated with the given cookie.
1099 */
1100void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1101 struct inode *inode)
1102{
1103 struct address_space *mapping = inode->i_mapping;
1104 struct pagevec pvec;
1105 pgoff_t next;
1106 int i;
1107
1108 _enter("%p,%p", cookie, inode);
1109
1110 if (!mapping || mapping->nrpages == 0) {
1111 _leave(" [no pages]");
1112 return;
1113 }
1114
1115 pagevec_init(&pvec, 0);
1116 next = 0;
Jan Beulichb307d462011-07-21 15:02:43 +01001117 do {
1118 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1119 break;
David Howellsc902ce12011-07-07 12:19:48 +01001120 for (i = 0; i < pagevec_count(&pvec); i++) {
1121 struct page *page = pvec.pages[i];
Jan Beulichb307d462011-07-21 15:02:43 +01001122 next = page->index;
David Howellsc902ce12011-07-07 12:19:48 +01001123 if (PageFsCache(page)) {
1124 __fscache_wait_on_page_write(cookie, page);
1125 __fscache_uncache_page(cookie, page);
1126 }
1127 }
1128 pagevec_release(&pvec);
1129 cond_resched();
Jan Beulichb307d462011-07-21 15:02:43 +01001130 } while (++next);
David Howellsc902ce12011-07-07 12:19:48 +01001131
1132 _leave("");
1133}
1134EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);