blob: 793e3d5ca4b58d2eb18b0fc8cc2dd52250aec9d3 [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 */
David Howellsda9803b2013-08-21 17:29:38 -0400281int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
David Howellsb5108822009-04-03 16:42:39 +0100282{
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 */
David Howellsda9803b2013-08-21 17:29:38 -0400325int fscache_wait_for_operation_activation(struct fscache_object *object,
326 struct fscache_operation *op,
327 atomic_t *stat_op_waits,
328 atomic_t *stat_object_dead,
329 void (*do_cancel)(struct fscache_operation *))
David Howells60d543c2009-11-19 18:11:45 +0000330{
331 int ret;
332
David Howellsda9803b2013-08-21 17:29:38 -0400333 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
David Howells60d543c2009-11-19 18:11:45 +0000334 goto check_if_dead;
335
336 _debug(">>> WT");
David Howellsda9803b2013-08-21 17:29:38 -0400337 if (stat_op_waits)
338 fscache_stat(stat_op_waits);
339 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
David Howells60d543c2009-11-19 18:11:45 +0000340 fscache_wait_bit_interruptible,
David Howells9c04caa2012-12-07 18:08:02 +0000341 TASK_INTERRUPTIBLE) != 0) {
David Howellsda9803b2013-08-21 17:29:38 -0400342 ret = fscache_cancel_op(op, do_cancel);
David Howells60d543c2009-11-19 18:11:45 +0000343 if (ret == 0)
344 return -ERESTARTSYS;
345
346 /* it's been removed from the pending queue by another party,
347 * so we should get to run shortly */
David Howellsda9803b2013-08-21 17:29:38 -0400348 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
David Howells60d543c2009-11-19 18:11:45 +0000349 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
350 }
351 _debug("<<< GO");
352
353check_if_dead:
David Howellsda9803b2013-08-21 17:29:38 -0400354 if (op->state == FSCACHE_OP_ST_CANCELLED) {
355 if (stat_object_dead)
356 fscache_stat(stat_object_dead);
David Howells9f105232012-12-20 21:52:35 +0000357 _leave(" = -ENOBUFS [cancelled]");
358 return -ENOBUFS;
359 }
David Howells60d543c2009-11-19 18:11:45 +0000360 if (unlikely(fscache_object_is_dead(object))) {
David Howellsda9803b2013-08-21 17:29:38 -0400361 pr_err("%s() = -ENOBUFS [obj dead %d]\n", __func__, op->state);
362 fscache_cancel_op(op, do_cancel);
363 if (stat_object_dead)
364 fscache_stat(stat_object_dead);
David Howells60d543c2009-11-19 18:11:45 +0000365 return -ENOBUFS;
366 }
367 return 0;
368}
369
370/*
David Howellsb5108822009-04-03 16:42:39 +0100371 * read a page from the cache or allocate a block in which to store it
372 * - we return:
373 * -ENOMEM - out of memory, nothing done
374 * -ERESTARTSYS - interrupted
375 * -ENOBUFS - no backing object available in which to cache the block
376 * -ENODATA - no data available in the backing object for this block
377 * 0 - dispatched a read - it'll call end_io_func() when finished
378 */
379int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
380 struct page *page,
381 fscache_rw_complete_t end_io_func,
382 void *context,
383 gfp_t gfp)
384{
385 struct fscache_retrieval *op;
386 struct fscache_object *object;
387 int ret;
388
389 _enter("%p,%p,,,", cookie, page);
390
391 fscache_stat(&fscache_n_retrievals);
392
393 if (hlist_empty(&cookie->backing_objects))
394 goto nobufs;
395
David Howellsef778e72012-12-20 21:52:36 +0000396 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
397 _leave(" = -ENOBUFS [invalidating]");
398 return -ENOBUFS;
399 }
400
David Howellsb5108822009-04-03 16:42:39 +0100401 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
402 ASSERTCMP(page, !=, NULL);
403
404 if (fscache_wait_for_deferred_lookup(cookie) < 0)
405 return -ERESTARTSYS;
406
David Howells13627292013-05-10 19:50:26 +0100407 op = fscache_alloc_retrieval(cookie, page->mapping,
408 end_io_func,context);
David Howellsb5108822009-04-03 16:42:39 +0100409 if (!op) {
410 _leave(" = -ENOMEM");
411 return -ENOMEM;
412 }
David Howells1bb4b7f92013-05-21 13:44:15 +0100413 atomic_set(&op->n_pages, 1);
David Howellsb5108822009-04-03 16:42:39 +0100414
415 spin_lock(&cookie->lock);
416
417 if (hlist_empty(&cookie->backing_objects))
418 goto nobufs_unlock;
419 object = hlist_entry(cookie->backing_objects.first,
420 struct fscache_object, cookie_link);
421
David Howellscaaef692013-05-10 19:50:26 +0100422 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
David Howellsb5108822009-04-03 16:42:39 +0100423
David Howells4fbf4292009-11-19 18:11:04 +0000424 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000425 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000426
David Howellsb5108822009-04-03 16:42:39 +0100427 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000428 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100429 spin_unlock(&cookie->lock);
430
431 fscache_stat(&fscache_n_retrieval_ops);
432
433 /* pin the netfs read context in case we need to do the actual netfs
434 * read because we've encountered a cache read failure */
435 fscache_get_context(object->cookie, op->context);
436
437 /* we wait for the operation to become active, and then process it
438 * *here*, in this thread, and not in the thread pool */
David Howellsda9803b2013-08-21 17:29:38 -0400439 ret = fscache_wait_for_operation_activation(
440 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000441 __fscache_stat(&fscache_n_retrieval_op_waits),
David Howellsda9803b2013-08-21 17:29:38 -0400442 __fscache_stat(&fscache_n_retrievals_object_dead),
443 fscache_do_cancel_retrieval);
David Howells60d543c2009-11-19 18:11:45 +0000444 if (ret < 0)
445 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100446
447 /* ask the cache to honour the operation */
448 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
David Howells52bd75f2009-11-19 18:11:08 +0000449 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100450 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000451 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100452 if (ret == 0)
453 ret = -ENODATA;
454 } else {
David Howells52bd75f2009-11-19 18:11:08 +0000455 fscache_stat(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100456 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000457 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100458 }
459
David Howells5753c442009-11-19 18:11:19 +0000460error:
David Howellsb5108822009-04-03 16:42:39 +0100461 if (ret == -ENOMEM)
462 fscache_stat(&fscache_n_retrievals_nomem);
463 else if (ret == -ERESTARTSYS)
464 fscache_stat(&fscache_n_retrievals_intr);
465 else if (ret == -ENODATA)
466 fscache_stat(&fscache_n_retrievals_nodata);
467 else if (ret < 0)
468 fscache_stat(&fscache_n_retrievals_nobufs);
469 else
470 fscache_stat(&fscache_n_retrievals_ok);
471
472 fscache_put_retrieval(op);
473 _leave(" = %d", ret);
474 return ret;
475
David Howells9f105232012-12-20 21:52:35 +0000476nobufs_unlock_dec:
477 atomic_dec(&object->n_reads);
David Howellsb5108822009-04-03 16:42:39 +0100478nobufs_unlock:
479 spin_unlock(&cookie->lock);
David Howells13627292013-05-10 19:50:26 +0100480 atomic_dec(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100481 kfree(op);
482nobufs:
483 fscache_stat(&fscache_n_retrievals_nobufs);
484 _leave(" = -ENOBUFS");
485 return -ENOBUFS;
486}
487EXPORT_SYMBOL(__fscache_read_or_alloc_page);
488
489/*
490 * read a list of page from the cache or allocate a block in which to store
491 * them
492 * - we return:
493 * -ENOMEM - out of memory, some pages may be being read
494 * -ERESTARTSYS - interrupted, some pages may be being read
495 * -ENOBUFS - no backing object or space available in which to cache any
496 * pages not being read
497 * -ENODATA - no data available in the backing object for some or all of
498 * the pages
499 * 0 - dispatched a read on all pages
500 *
501 * end_io_func() will be called for each page read from the cache as it is
502 * finishes being read
503 *
504 * any pages for which a read is dispatched will be removed from pages and
505 * nr_pages
506 */
507int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
508 struct address_space *mapping,
509 struct list_head *pages,
510 unsigned *nr_pages,
511 fscache_rw_complete_t end_io_func,
512 void *context,
513 gfp_t gfp)
514{
David Howellsb5108822009-04-03 16:42:39 +0100515 struct fscache_retrieval *op;
516 struct fscache_object *object;
517 int ret;
518
519 _enter("%p,,%d,,,", cookie, *nr_pages);
520
521 fscache_stat(&fscache_n_retrievals);
522
523 if (hlist_empty(&cookie->backing_objects))
524 goto nobufs;
525
David Howellsef778e72012-12-20 21:52:36 +0000526 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
527 _leave(" = -ENOBUFS [invalidating]");
528 return -ENOBUFS;
529 }
530
David Howellsb5108822009-04-03 16:42:39 +0100531 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
532 ASSERTCMP(*nr_pages, >, 0);
533 ASSERT(!list_empty(pages));
534
535 if (fscache_wait_for_deferred_lookup(cookie) < 0)
536 return -ERESTARTSYS;
537
David Howells13627292013-05-10 19:50:26 +0100538 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
David Howellsb5108822009-04-03 16:42:39 +0100539 if (!op)
540 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100541 atomic_set(&op->n_pages, *nr_pages);
David Howellsb5108822009-04-03 16:42:39 +0100542
543 spin_lock(&cookie->lock);
544
545 if (hlist_empty(&cookie->backing_objects))
546 goto nobufs_unlock;
547 object = hlist_entry(cookie->backing_objects.first,
548 struct fscache_object, cookie_link);
549
David Howells4fbf4292009-11-19 18:11:04 +0000550 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000551 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000552
David Howellsb5108822009-04-03 16:42:39 +0100553 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000554 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100555 spin_unlock(&cookie->lock);
556
557 fscache_stat(&fscache_n_retrieval_ops);
558
559 /* pin the netfs read context in case we need to do the actual netfs
560 * read because we've encountered a cache read failure */
561 fscache_get_context(object->cookie, op->context);
562
563 /* we wait for the operation to become active, and then process it
564 * *here*, in this thread, and not in the thread pool */
David Howellsda9803b2013-08-21 17:29:38 -0400565 ret = fscache_wait_for_operation_activation(
566 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000567 __fscache_stat(&fscache_n_retrieval_op_waits),
David Howellsda9803b2013-08-21 17:29:38 -0400568 __fscache_stat(&fscache_n_retrievals_object_dead),
569 fscache_do_cancel_retrieval);
David Howells60d543c2009-11-19 18:11:45 +0000570 if (ret < 0)
571 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100572
573 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000574 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
575 fscache_stat(&fscache_n_cop_allocate_pages);
576 ret = object->cache->ops->allocate_pages(
577 op, pages, nr_pages, gfp);
578 fscache_stat_d(&fscache_n_cop_allocate_pages);
579 } else {
580 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
581 ret = object->cache->ops->read_or_alloc_pages(
582 op, pages, nr_pages, gfp);
583 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
584 }
David Howellsb5108822009-04-03 16:42:39 +0100585
David Howells5753c442009-11-19 18:11:19 +0000586error:
David Howellsb5108822009-04-03 16:42:39 +0100587 if (ret == -ENOMEM)
588 fscache_stat(&fscache_n_retrievals_nomem);
589 else if (ret == -ERESTARTSYS)
590 fscache_stat(&fscache_n_retrievals_intr);
591 else if (ret == -ENODATA)
592 fscache_stat(&fscache_n_retrievals_nodata);
593 else if (ret < 0)
594 fscache_stat(&fscache_n_retrievals_nobufs);
595 else
596 fscache_stat(&fscache_n_retrievals_ok);
597
598 fscache_put_retrieval(op);
599 _leave(" = %d", ret);
600 return ret;
601
David Howells9f105232012-12-20 21:52:35 +0000602nobufs_unlock_dec:
603 atomic_dec(&object->n_reads);
David Howellsb5108822009-04-03 16:42:39 +0100604nobufs_unlock:
605 spin_unlock(&cookie->lock);
David Howells13627292013-05-10 19:50:26 +0100606 atomic_dec(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100607 kfree(op);
608nobufs:
609 fscache_stat(&fscache_n_retrievals_nobufs);
610 _leave(" = -ENOBUFS");
611 return -ENOBUFS;
612}
613EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
614
615/*
616 * allocate a block in the cache on which to store a page
617 * - we return:
618 * -ENOMEM - out of memory, nothing done
619 * -ERESTARTSYS - interrupted
620 * -ENOBUFS - no backing object available in which to cache the block
621 * 0 - block allocated
622 */
623int __fscache_alloc_page(struct fscache_cookie *cookie,
624 struct page *page,
625 gfp_t gfp)
626{
627 struct fscache_retrieval *op;
628 struct fscache_object *object;
629 int ret;
630
631 _enter("%p,%p,,,", cookie, page);
632
633 fscache_stat(&fscache_n_allocs);
634
635 if (hlist_empty(&cookie->backing_objects))
636 goto nobufs;
637
638 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
639 ASSERTCMP(page, !=, NULL);
640
David Howellsef778e72012-12-20 21:52:36 +0000641 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
642 _leave(" = -ENOBUFS [invalidating]");
643 return -ENOBUFS;
644 }
645
David Howellsb5108822009-04-03 16:42:39 +0100646 if (fscache_wait_for_deferred_lookup(cookie) < 0)
647 return -ERESTARTSYS;
648
David Howells13627292013-05-10 19:50:26 +0100649 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
David Howellsb5108822009-04-03 16:42:39 +0100650 if (!op)
651 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100652 atomic_set(&op->n_pages, 1);
David Howellsb5108822009-04-03 16:42:39 +0100653
654 spin_lock(&cookie->lock);
655
656 if (hlist_empty(&cookie->backing_objects))
657 goto nobufs_unlock;
658 object = hlist_entry(cookie->backing_objects.first,
659 struct fscache_object, cookie_link);
660
661 if (fscache_submit_op(object, &op->op) < 0)
662 goto nobufs_unlock;
663 spin_unlock(&cookie->lock);
664
665 fscache_stat(&fscache_n_alloc_ops);
666
David Howellsda9803b2013-08-21 17:29:38 -0400667 ret = fscache_wait_for_operation_activation(
668 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000669 __fscache_stat(&fscache_n_alloc_op_waits),
David Howellsda9803b2013-08-21 17:29:38 -0400670 __fscache_stat(&fscache_n_allocs_object_dead),
671 fscache_do_cancel_retrieval);
David Howells60d543c2009-11-19 18:11:45 +0000672 if (ret < 0)
673 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100674
675 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000676 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100677 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000678 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100679
David Howells5753c442009-11-19 18:11:19 +0000680error:
681 if (ret == -ERESTARTSYS)
682 fscache_stat(&fscache_n_allocs_intr);
683 else if (ret < 0)
David Howellsb5108822009-04-03 16:42:39 +0100684 fscache_stat(&fscache_n_allocs_nobufs);
685 else
686 fscache_stat(&fscache_n_allocs_ok);
687
688 fscache_put_retrieval(op);
689 _leave(" = %d", ret);
690 return ret;
691
692nobufs_unlock:
693 spin_unlock(&cookie->lock);
David Howells13627292013-05-10 19:50:26 +0100694 atomic_dec(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100695 kfree(op);
696nobufs:
697 fscache_stat(&fscache_n_allocs_nobufs);
698 _leave(" = -ENOBUFS");
699 return -ENOBUFS;
700}
701EXPORT_SYMBOL(__fscache_alloc_page);
702
703/*
704 * release a write op reference
705 */
706static void fscache_release_write_op(struct fscache_operation *_op)
707{
708 _enter("{OP%x}", _op->debug_id);
709}
710
711/*
712 * perform the background storage of a page into the cache
713 */
714static void fscache_write_op(struct fscache_operation *_op)
715{
716 struct fscache_storage *op =
717 container_of(_op, struct fscache_storage, op);
718 struct fscache_object *object = op->op.object;
David Howells1bccf512009-11-19 18:11:25 +0000719 struct fscache_cookie *cookie;
David Howellsb5108822009-04-03 16:42:39 +0100720 struct page *page;
721 unsigned n;
722 void *results[1];
723 int ret;
724
725 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
726
David Howellsb5108822009-04-03 16:42:39 +0100727 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000728 cookie = object->cookie;
David Howellsb5108822009-04-03 16:42:39 +0100729
David Howells7ef001e2012-12-07 10:41:26 +0000730 if (!fscache_object_is_active(object)) {
731 /* If we get here, then the on-disk cache object likely longer
732 * exists, so we should just cancel this write operation.
733 */
David Howellsb5108822009-04-03 16:42:39 +0100734 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000735 fscache_op_complete(&op->op, false);
David Howells7ef001e2012-12-07 10:41:26 +0000736 _leave(" [inactive]");
737 return;
738 }
739
740 if (!cookie) {
741 /* If we get here, then the cookie belonging to the object was
742 * detached, probably by the cookie being withdrawn due to
743 * memory pressure, which means that the pages we might write
744 * to the cache from no longer exist - therefore, we can just
745 * cancel this write operation.
746 */
747 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000748 fscache_op_complete(&op->op, false);
David Howellscaaef692013-05-10 19:50:26 +0100749 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
750 _op->flags, _op->state, object->state->short_name,
751 object->flags);
David Howellsb5108822009-04-03 16:42:39 +0100752 return;
753 }
754
David Howells1bccf512009-11-19 18:11:25 +0000755 spin_lock(&cookie->stores_lock);
756
David Howellsb5108822009-04-03 16:42:39 +0100757 fscache_stat(&fscache_n_store_calls);
758
759 /* find a page to store */
760 page = NULL;
761 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
762 FSCACHE_COOKIE_PENDING_TAG);
763 if (n != 1)
764 goto superseded;
765 page = results[0];
766 _debug("gang %d [%lx]", n, page->index);
David Howells1bccf512009-11-19 18:11:25 +0000767 if (page->index > op->store_limit) {
768 fscache_stat(&fscache_n_store_pages_over_limit);
David Howellsb5108822009-04-03 16:42:39 +0100769 goto superseded;
David Howells1bccf512009-11-19 18:11:25 +0000770 }
David Howellsb5108822009-04-03 16:42:39 +0100771
Dan Carpenter08a66852010-06-01 20:58:22 +0100772 radix_tree_tag_set(&cookie->stores, page->index,
773 FSCACHE_COOKIE_STORING_TAG);
774 radix_tree_tag_clear(&cookie->stores, page->index,
775 FSCACHE_COOKIE_PENDING_TAG);
David Howellsb5108822009-04-03 16:42:39 +0100776
David Howells1bccf512009-11-19 18:11:25 +0000777 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100778 spin_unlock(&object->lock);
David Howellsb5108822009-04-03 16:42:39 +0100779
Dan Carpenter08a66852010-06-01 20:58:22 +0100780 fscache_stat(&fscache_n_store_pages);
781 fscache_stat(&fscache_n_cop_write_page);
782 ret = object->cache->ops->write_page(op, page);
783 fscache_stat_d(&fscache_n_cop_write_page);
Dan Carpenter08a66852010-06-01 20:58:22 +0100784 fscache_end_page_write(object, page);
785 if (ret < 0) {
Dan Carpenter08a66852010-06-01 20:58:22 +0100786 fscache_abort_object(object);
David Howells1f372df2012-12-13 20:03:13 +0000787 fscache_op_complete(&op->op, true);
Dan Carpenter08a66852010-06-01 20:58:22 +0100788 } else {
789 fscache_enqueue_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +0100790 }
791
792 _leave("");
793 return;
794
795superseded:
796 /* this writer is going away and there aren't any more things to
797 * write */
798 _debug("cease");
David Howells1bccf512009-11-19 18:11:25 +0000799 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100800 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
801 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000802 fscache_op_complete(&op->op, true);
David Howellsb5108822009-04-03 16:42:39 +0100803 _leave("");
804}
805
806/*
David Howellsef778e72012-12-20 21:52:36 +0000807 * Clear the pages pending writing for invalidation
808 */
809void fscache_invalidate_writes(struct fscache_cookie *cookie)
810{
811 struct page *page;
812 void *results[16];
813 int n, i;
814
815 _enter("");
816
Sebastian Andrzej Siewioree8be572013-05-10 19:50:24 +0100817 for (;;) {
818 spin_lock(&cookie->stores_lock);
819 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
820 ARRAY_SIZE(results),
821 FSCACHE_COOKIE_PENDING_TAG);
822 if (n == 0) {
823 spin_unlock(&cookie->stores_lock);
824 break;
825 }
826
David Howellsef778e72012-12-20 21:52:36 +0000827 for (i = n - 1; i >= 0; i--) {
828 page = results[i];
829 radix_tree_delete(&cookie->stores, page->index);
830 }
831
832 spin_unlock(&cookie->stores_lock);
833
834 for (i = n - 1; i >= 0; i--)
835 page_cache_release(results[i]);
836 }
837
David Howellsef778e72012-12-20 21:52:36 +0000838 _leave("");
839}
840
841/*
David Howellsb5108822009-04-03 16:42:39 +0100842 * request a page be stored in the cache
843 * - returns:
844 * -ENOMEM - out of memory, nothing done
845 * -ENOBUFS - no backing object available in which to cache the page
846 * 0 - dispatched a write - it'll call end_io_func() when finished
847 *
848 * if the cookie still has a backing object at this point, that object can be
849 * in one of a few states with respect to storage processing:
850 *
851 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
852 * set)
853 *
David Howellscaaef692013-05-10 19:50:26 +0100854 * (a) no writes yet
David Howellsb5108822009-04-03 16:42:39 +0100855 *
856 * (b) writes deferred till post-creation (mark page for writing and
857 * return immediately)
858 *
859 * (2) negative lookup, object created, initial fill being made from netfs
David Howellsb5108822009-04-03 16:42:39 +0100860 *
861 * (a) fill point not yet reached this page (mark page for writing and
862 * return)
863 *
864 * (b) fill point passed this page (queue op to store this page)
865 *
866 * (3) object extant (queue op to store this page)
867 *
868 * any other state is invalid
869 */
870int __fscache_write_page(struct fscache_cookie *cookie,
871 struct page *page,
872 gfp_t gfp)
873{
874 struct fscache_storage *op;
875 struct fscache_object *object;
876 int ret;
877
878 _enter("%p,%x,", cookie, (u32) page->flags);
879
880 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
881 ASSERT(PageFsCache(page));
882
883 fscache_stat(&fscache_n_stores);
884
David Howellsef778e72012-12-20 21:52:36 +0000885 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
886 _leave(" = -ENOBUFS [invalidating]");
887 return -ENOBUFS;
888 }
889
David Howells5f4f9f42012-12-20 21:52:33 +0000890 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
David Howellsb5108822009-04-03 16:42:39 +0100891 if (!op)
892 goto nomem;
893
Tejun Heo8af7c122010-07-20 22:09:01 +0200894 fscache_operation_init(&op->op, fscache_write_op,
895 fscache_release_write_op);
David Howells13627292013-05-10 19:50:26 +0100896 op->op.flags = FSCACHE_OP_ASYNC |
897 (1 << FSCACHE_OP_WAITING) |
898 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100899
900 ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
901 if (ret < 0)
902 goto nomem_free;
903
904 ret = -ENOBUFS;
905 spin_lock(&cookie->lock);
906
907 if (hlist_empty(&cookie->backing_objects))
908 goto nobufs;
909 object = hlist_entry(cookie->backing_objects.first,
910 struct fscache_object, cookie_link);
911 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
912 goto nobufs;
913
914 /* add the page to the pending-storage radix tree on the backing
915 * object */
916 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000917 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100918
919 _debug("store limit %llx", (unsigned long long) object->store_limit);
920
921 ret = radix_tree_insert(&cookie->stores, page->index, page);
922 if (ret < 0) {
923 if (ret == -EEXIST)
924 goto already_queued;
925 _debug("insert failed %d", ret);
926 goto nobufs_unlock_obj;
927 }
928
929 radix_tree_tag_set(&cookie->stores, page->index,
930 FSCACHE_COOKIE_PENDING_TAG);
931 page_cache_get(page);
932
933 /* we only want one writer at a time, but we do need to queue new
934 * writers after exclusive ops */
935 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
936 goto already_pending;
937
David Howells1bccf512009-11-19 18:11:25 +0000938 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100939 spin_unlock(&object->lock);
940
941 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
942 op->store_limit = object->store_limit;
943
David Howells13627292013-05-10 19:50:26 +0100944 atomic_inc(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100945 if (fscache_submit_op(object, &op->op) < 0)
946 goto submit_failed;
947
948 spin_unlock(&cookie->lock);
949 radix_tree_preload_end();
950 fscache_stat(&fscache_n_store_ops);
951 fscache_stat(&fscache_n_stores_ok);
952
Tejun Heo8af7c122010-07-20 22:09:01 +0200953 /* the work queue now carries its own ref on the object */
David Howellsb5108822009-04-03 16:42:39 +0100954 fscache_put_operation(&op->op);
955 _leave(" = 0");
956 return 0;
957
958already_queued:
959 fscache_stat(&fscache_n_stores_again);
960already_pending:
David Howells1bccf512009-11-19 18:11:25 +0000961 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100962 spin_unlock(&object->lock);
963 spin_unlock(&cookie->lock);
964 radix_tree_preload_end();
965 kfree(op);
966 fscache_stat(&fscache_n_stores_ok);
967 _leave(" = 0");
968 return 0;
969
970submit_failed:
David Howells13627292013-05-10 19:50:26 +0100971 atomic_dec(&cookie->n_active);
David Howells1bccf512009-11-19 18:11:25 +0000972 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100973 radix_tree_delete(&cookie->stores, page->index);
David Howells1bccf512009-11-19 18:11:25 +0000974 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100975 page_cache_release(page);
976 ret = -ENOBUFS;
977 goto nobufs;
978
979nobufs_unlock_obj:
Dan Carpenter1147d0f2010-03-23 14:48:37 +0000980 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100981 spin_unlock(&object->lock);
982nobufs:
983 spin_unlock(&cookie->lock);
984 radix_tree_preload_end();
985 kfree(op);
986 fscache_stat(&fscache_n_stores_nobufs);
987 _leave(" = -ENOBUFS");
988 return -ENOBUFS;
989
990nomem_free:
991 kfree(op);
992nomem:
993 fscache_stat(&fscache_n_stores_oom);
994 _leave(" = -ENOMEM");
995 return -ENOMEM;
996}
997EXPORT_SYMBOL(__fscache_write_page);
998
999/*
1000 * remove a page from the cache
1001 */
1002void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1003{
1004 struct fscache_object *object;
1005
1006 _enter(",%p", page);
1007
1008 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1009 ASSERTCMP(page, !=, NULL);
1010
1011 fscache_stat(&fscache_n_uncaches);
1012
1013 /* cache withdrawal may beat us to it */
1014 if (!PageFsCache(page))
1015 goto done;
1016
1017 /* get the object */
1018 spin_lock(&cookie->lock);
1019
1020 if (hlist_empty(&cookie->backing_objects)) {
1021 ClearPageFsCache(page);
1022 goto done_unlock;
1023 }
1024
1025 object = hlist_entry(cookie->backing_objects.first,
1026 struct fscache_object, cookie_link);
1027
1028 /* there might now be stuff on disk we could read */
1029 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1030
1031 /* only invoke the cache backend if we managed to mark the page
1032 * uncached here; this deals with synchronisation vs withdrawal */
1033 if (TestClearPageFsCache(page) &&
1034 object->cache->ops->uncache_page) {
1035 /* the cache backend releases the cookie lock */
David Howells52bd75f2009-11-19 18:11:08 +00001036 fscache_stat(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001037 object->cache->ops->uncache_page(object, page);
David Howells52bd75f2009-11-19 18:11:08 +00001038 fscache_stat_d(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001039 goto done;
1040 }
1041
1042done_unlock:
1043 spin_unlock(&cookie->lock);
1044done:
1045 _leave("");
1046}
1047EXPORT_SYMBOL(__fscache_uncache_page);
1048
1049/**
David Howellsc4d6d8d2012-12-20 21:52:32 +00001050 * fscache_mark_page_cached - Mark a page as being cached
1051 * @op: The retrieval op pages are being marked for
1052 * @page: The page to be marked
1053 *
1054 * Mark a netfs page as being cached. After this is called, the netfs
1055 * must call fscache_uncache_page() to remove the mark.
1056 */
1057void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1058{
1059 struct fscache_cookie *cookie = op->op.object->cookie;
1060
1061#ifdef CONFIG_FSCACHE_STATS
1062 atomic_inc(&fscache_n_marks);
1063#endif
1064
1065 _debug("- mark %p{%lx}", page, page->index);
1066 if (TestSetPageFsCache(page)) {
1067 static bool once_only;
1068 if (!once_only) {
1069 once_only = true;
1070 printk(KERN_WARNING "FS-Cache:"
1071 " Cookie type %s marked page %lx"
1072 " multiple times\n",
1073 cookie->def->name, page->index);
1074 }
1075 }
1076
1077 if (cookie->def->mark_page_cached)
1078 cookie->def->mark_page_cached(cookie->netfs_data,
1079 op->mapping, page);
1080}
1081EXPORT_SYMBOL(fscache_mark_page_cached);
1082
1083/**
David Howellsb5108822009-04-03 16:42:39 +01001084 * fscache_mark_pages_cached - Mark pages as being cached
1085 * @op: The retrieval op pages are being marked for
1086 * @pagevec: The pages to be marked
1087 *
1088 * Mark a bunch of netfs pages as being cached. After this is called,
1089 * the netfs must call fscache_uncache_page() to remove the mark.
1090 */
1091void fscache_mark_pages_cached(struct fscache_retrieval *op,
1092 struct pagevec *pagevec)
1093{
David Howellsb5108822009-04-03 16:42:39 +01001094 unsigned long loop;
1095
David Howellsc4d6d8d2012-12-20 21:52:32 +00001096 for (loop = 0; loop < pagevec->nr; loop++)
1097 fscache_mark_page_cached(op, pagevec->pages[loop]);
David Howellsb5108822009-04-03 16:42:39 +01001098
David Howellsb5108822009-04-03 16:42:39 +01001099 pagevec_reinit(pagevec);
1100}
1101EXPORT_SYMBOL(fscache_mark_pages_cached);
David Howellsc902ce12011-07-07 12:19:48 +01001102
1103/*
1104 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1105 * to be associated with the given cookie.
1106 */
1107void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1108 struct inode *inode)
1109{
1110 struct address_space *mapping = inode->i_mapping;
1111 struct pagevec pvec;
1112 pgoff_t next;
1113 int i;
1114
1115 _enter("%p,%p", cookie, inode);
1116
1117 if (!mapping || mapping->nrpages == 0) {
1118 _leave(" [no pages]");
1119 return;
1120 }
1121
1122 pagevec_init(&pvec, 0);
1123 next = 0;
Jan Beulichb307d462011-07-21 15:02:43 +01001124 do {
1125 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1126 break;
David Howellsc902ce12011-07-07 12:19:48 +01001127 for (i = 0; i < pagevec_count(&pvec); i++) {
1128 struct page *page = pvec.pages[i];
Jan Beulichb307d462011-07-21 15:02:43 +01001129 next = page->index;
David Howellsc902ce12011-07-07 12:19:48 +01001130 if (PageFsCache(page)) {
1131 __fscache_wait_on_page_write(cookie, page);
1132 __fscache_uncache_page(cookie, page);
1133 }
1134 }
1135 pagevec_release(&pvec);
1136 cond_resched();
Jan Beulichb307d462011-07-21 15:02:43 +01001137 } while (++next);
David Howellsc902ce12011-07-07 12:19:48 +01001138
1139 _leave("");
1140}
1141EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);