blob: 73899c1c34494555d73dd5714ecb21eb74c0296d [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/*
Milosz Tanski5a6f2822013-08-21 17:30:11 -0400704 * Unmark pages allocate in the readahead code path (via:
705 * fscache_readpages_or_alloc) after delegating to the base filesystem
706 */
707void __fscache_readpages_cancel(struct fscache_cookie *cookie,
708 struct list_head *pages)
709{
710 struct page *page;
711
712 list_for_each_entry(page, pages, lru) {
713 if (PageFsCache(page))
714 __fscache_uncache_page(cookie, page);
715 }
716}
717EXPORT_SYMBOL(__fscache_readpages_cancel);
718
719/*
David Howellsb5108822009-04-03 16:42:39 +0100720 * release a write op reference
721 */
722static void fscache_release_write_op(struct fscache_operation *_op)
723{
724 _enter("{OP%x}", _op->debug_id);
725}
726
727/*
728 * perform the background storage of a page into the cache
729 */
730static void fscache_write_op(struct fscache_operation *_op)
731{
732 struct fscache_storage *op =
733 container_of(_op, struct fscache_storage, op);
734 struct fscache_object *object = op->op.object;
David Howells1bccf512009-11-19 18:11:25 +0000735 struct fscache_cookie *cookie;
David Howellsb5108822009-04-03 16:42:39 +0100736 struct page *page;
737 unsigned n;
738 void *results[1];
739 int ret;
740
741 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
742
David Howellsb5108822009-04-03 16:42:39 +0100743 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000744 cookie = object->cookie;
David Howellsb5108822009-04-03 16:42:39 +0100745
David Howells7ef001e2012-12-07 10:41:26 +0000746 if (!fscache_object_is_active(object)) {
747 /* If we get here, then the on-disk cache object likely longer
748 * exists, so we should just cancel this write operation.
749 */
David Howellsb5108822009-04-03 16:42:39 +0100750 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000751 fscache_op_complete(&op->op, false);
David Howells7ef001e2012-12-07 10:41:26 +0000752 _leave(" [inactive]");
753 return;
754 }
755
756 if (!cookie) {
757 /* If we get here, then the cookie belonging to the object was
758 * detached, probably by the cookie being withdrawn due to
759 * memory pressure, which means that the pages we might write
760 * to the cache from no longer exist - therefore, we can just
761 * cancel this write operation.
762 */
763 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000764 fscache_op_complete(&op->op, false);
David Howellscaaef692013-05-10 19:50:26 +0100765 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
766 _op->flags, _op->state, object->state->short_name,
767 object->flags);
David Howellsb5108822009-04-03 16:42:39 +0100768 return;
769 }
770
David Howells1bccf512009-11-19 18:11:25 +0000771 spin_lock(&cookie->stores_lock);
772
David Howellsb5108822009-04-03 16:42:39 +0100773 fscache_stat(&fscache_n_store_calls);
774
775 /* find a page to store */
776 page = NULL;
777 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
778 FSCACHE_COOKIE_PENDING_TAG);
779 if (n != 1)
780 goto superseded;
781 page = results[0];
782 _debug("gang %d [%lx]", n, page->index);
David Howells1bccf512009-11-19 18:11:25 +0000783 if (page->index > op->store_limit) {
784 fscache_stat(&fscache_n_store_pages_over_limit);
David Howellsb5108822009-04-03 16:42:39 +0100785 goto superseded;
David Howells1bccf512009-11-19 18:11:25 +0000786 }
David Howellsb5108822009-04-03 16:42:39 +0100787
Dan Carpenter08a66852010-06-01 20:58:22 +0100788 radix_tree_tag_set(&cookie->stores, page->index,
789 FSCACHE_COOKIE_STORING_TAG);
790 radix_tree_tag_clear(&cookie->stores, page->index,
791 FSCACHE_COOKIE_PENDING_TAG);
David Howellsb5108822009-04-03 16:42:39 +0100792
David Howells1bccf512009-11-19 18:11:25 +0000793 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100794 spin_unlock(&object->lock);
David Howellsb5108822009-04-03 16:42:39 +0100795
Dan Carpenter08a66852010-06-01 20:58:22 +0100796 fscache_stat(&fscache_n_store_pages);
797 fscache_stat(&fscache_n_cop_write_page);
798 ret = object->cache->ops->write_page(op, page);
799 fscache_stat_d(&fscache_n_cop_write_page);
Dan Carpenter08a66852010-06-01 20:58:22 +0100800 fscache_end_page_write(object, page);
801 if (ret < 0) {
Dan Carpenter08a66852010-06-01 20:58:22 +0100802 fscache_abort_object(object);
David Howells1f372df2012-12-13 20:03:13 +0000803 fscache_op_complete(&op->op, true);
Dan Carpenter08a66852010-06-01 20:58:22 +0100804 } else {
805 fscache_enqueue_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +0100806 }
807
808 _leave("");
809 return;
810
811superseded:
812 /* this writer is going away and there aren't any more things to
813 * write */
814 _debug("cease");
David Howells1bccf512009-11-19 18:11:25 +0000815 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100816 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
817 spin_unlock(&object->lock);
David Howells1f372df2012-12-13 20:03:13 +0000818 fscache_op_complete(&op->op, true);
David Howellsb5108822009-04-03 16:42:39 +0100819 _leave("");
820}
821
822/*
David Howellsef778e72012-12-20 21:52:36 +0000823 * Clear the pages pending writing for invalidation
824 */
825void fscache_invalidate_writes(struct fscache_cookie *cookie)
826{
827 struct page *page;
828 void *results[16];
829 int n, i;
830
831 _enter("");
832
Sebastian Andrzej Siewioree8be572013-05-10 19:50:24 +0100833 for (;;) {
834 spin_lock(&cookie->stores_lock);
835 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
836 ARRAY_SIZE(results),
837 FSCACHE_COOKIE_PENDING_TAG);
838 if (n == 0) {
839 spin_unlock(&cookie->stores_lock);
840 break;
841 }
842
David Howellsef778e72012-12-20 21:52:36 +0000843 for (i = n - 1; i >= 0; i--) {
844 page = results[i];
845 radix_tree_delete(&cookie->stores, page->index);
846 }
847
848 spin_unlock(&cookie->stores_lock);
849
850 for (i = n - 1; i >= 0; i--)
851 page_cache_release(results[i]);
852 }
853
David Howellsef778e72012-12-20 21:52:36 +0000854 _leave("");
855}
856
857/*
David Howellsb5108822009-04-03 16:42:39 +0100858 * request a page be stored in the cache
859 * - returns:
860 * -ENOMEM - out of memory, nothing done
861 * -ENOBUFS - no backing object available in which to cache the page
862 * 0 - dispatched a write - it'll call end_io_func() when finished
863 *
864 * if the cookie still has a backing object at this point, that object can be
865 * in one of a few states with respect to storage processing:
866 *
867 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
868 * set)
869 *
David Howellscaaef692013-05-10 19:50:26 +0100870 * (a) no writes yet
David Howellsb5108822009-04-03 16:42:39 +0100871 *
872 * (b) writes deferred till post-creation (mark page for writing and
873 * return immediately)
874 *
875 * (2) negative lookup, object created, initial fill being made from netfs
David Howellsb5108822009-04-03 16:42:39 +0100876 *
877 * (a) fill point not yet reached this page (mark page for writing and
878 * return)
879 *
880 * (b) fill point passed this page (queue op to store this page)
881 *
882 * (3) object extant (queue op to store this page)
883 *
884 * any other state is invalid
885 */
886int __fscache_write_page(struct fscache_cookie *cookie,
887 struct page *page,
888 gfp_t gfp)
889{
890 struct fscache_storage *op;
891 struct fscache_object *object;
892 int ret;
893
894 _enter("%p,%x,", cookie, (u32) page->flags);
895
896 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
897 ASSERT(PageFsCache(page));
898
899 fscache_stat(&fscache_n_stores);
900
David Howellsef778e72012-12-20 21:52:36 +0000901 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
902 _leave(" = -ENOBUFS [invalidating]");
903 return -ENOBUFS;
904 }
905
David Howells5f4f9f42012-12-20 21:52:33 +0000906 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
David Howellsb5108822009-04-03 16:42:39 +0100907 if (!op)
908 goto nomem;
909
Tejun Heo8af7c122010-07-20 22:09:01 +0200910 fscache_operation_init(&op->op, fscache_write_op,
911 fscache_release_write_op);
David Howells13627292013-05-10 19:50:26 +0100912 op->op.flags = FSCACHE_OP_ASYNC |
913 (1 << FSCACHE_OP_WAITING) |
914 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100915
Jan Kara5e4c0d972013-09-11 14:26:05 -0700916 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
David Howellsb5108822009-04-03 16:42:39 +0100917 if (ret < 0)
918 goto nomem_free;
919
920 ret = -ENOBUFS;
921 spin_lock(&cookie->lock);
922
923 if (hlist_empty(&cookie->backing_objects))
924 goto nobufs;
925 object = hlist_entry(cookie->backing_objects.first,
926 struct fscache_object, cookie_link);
927 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
928 goto nobufs;
929
930 /* add the page to the pending-storage radix tree on the backing
931 * object */
932 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000933 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100934
935 _debug("store limit %llx", (unsigned long long) object->store_limit);
936
937 ret = radix_tree_insert(&cookie->stores, page->index, page);
938 if (ret < 0) {
939 if (ret == -EEXIST)
940 goto already_queued;
941 _debug("insert failed %d", ret);
942 goto nobufs_unlock_obj;
943 }
944
945 radix_tree_tag_set(&cookie->stores, page->index,
946 FSCACHE_COOKIE_PENDING_TAG);
947 page_cache_get(page);
948
949 /* we only want one writer at a time, but we do need to queue new
950 * writers after exclusive ops */
951 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
952 goto already_pending;
953
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
957 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
958 op->store_limit = object->store_limit;
959
David Howells13627292013-05-10 19:50:26 +0100960 atomic_inc(&cookie->n_active);
David Howellsb5108822009-04-03 16:42:39 +0100961 if (fscache_submit_op(object, &op->op) < 0)
962 goto submit_failed;
963
964 spin_unlock(&cookie->lock);
965 radix_tree_preload_end();
966 fscache_stat(&fscache_n_store_ops);
967 fscache_stat(&fscache_n_stores_ok);
968
Tejun Heo8af7c122010-07-20 22:09:01 +0200969 /* the work queue now carries its own ref on the object */
David Howellsb5108822009-04-03 16:42:39 +0100970 fscache_put_operation(&op->op);
971 _leave(" = 0");
972 return 0;
973
974already_queued:
975 fscache_stat(&fscache_n_stores_again);
976already_pending:
David Howells1bccf512009-11-19 18:11:25 +0000977 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100978 spin_unlock(&object->lock);
979 spin_unlock(&cookie->lock);
980 radix_tree_preload_end();
981 kfree(op);
982 fscache_stat(&fscache_n_stores_ok);
983 _leave(" = 0");
984 return 0;
985
986submit_failed:
David Howells13627292013-05-10 19:50:26 +0100987 atomic_dec(&cookie->n_active);
David Howells1bccf512009-11-19 18:11:25 +0000988 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100989 radix_tree_delete(&cookie->stores, page->index);
David Howells1bccf512009-11-19 18:11:25 +0000990 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100991 page_cache_release(page);
992 ret = -ENOBUFS;
993 goto nobufs;
994
995nobufs_unlock_obj:
Dan Carpenter1147d0f2010-03-23 14:48:37 +0000996 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100997 spin_unlock(&object->lock);
998nobufs:
999 spin_unlock(&cookie->lock);
1000 radix_tree_preload_end();
1001 kfree(op);
1002 fscache_stat(&fscache_n_stores_nobufs);
1003 _leave(" = -ENOBUFS");
1004 return -ENOBUFS;
1005
1006nomem_free:
1007 kfree(op);
1008nomem:
1009 fscache_stat(&fscache_n_stores_oom);
1010 _leave(" = -ENOMEM");
1011 return -ENOMEM;
1012}
1013EXPORT_SYMBOL(__fscache_write_page);
1014
1015/*
1016 * remove a page from the cache
1017 */
1018void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1019{
1020 struct fscache_object *object;
1021
1022 _enter(",%p", page);
1023
1024 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1025 ASSERTCMP(page, !=, NULL);
1026
1027 fscache_stat(&fscache_n_uncaches);
1028
1029 /* cache withdrawal may beat us to it */
1030 if (!PageFsCache(page))
1031 goto done;
1032
1033 /* get the object */
1034 spin_lock(&cookie->lock);
1035
1036 if (hlist_empty(&cookie->backing_objects)) {
1037 ClearPageFsCache(page);
1038 goto done_unlock;
1039 }
1040
1041 object = hlist_entry(cookie->backing_objects.first,
1042 struct fscache_object, cookie_link);
1043
1044 /* there might now be stuff on disk we could read */
1045 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1046
1047 /* only invoke the cache backend if we managed to mark the page
1048 * uncached here; this deals with synchronisation vs withdrawal */
1049 if (TestClearPageFsCache(page) &&
1050 object->cache->ops->uncache_page) {
1051 /* the cache backend releases the cookie lock */
David Howells52bd75f2009-11-19 18:11:08 +00001052 fscache_stat(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001053 object->cache->ops->uncache_page(object, page);
David Howells52bd75f2009-11-19 18:11:08 +00001054 fscache_stat_d(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001055 goto done;
1056 }
1057
1058done_unlock:
1059 spin_unlock(&cookie->lock);
1060done:
1061 _leave("");
1062}
1063EXPORT_SYMBOL(__fscache_uncache_page);
1064
1065/**
David Howellsc4d6d8d2012-12-20 21:52:32 +00001066 * fscache_mark_page_cached - Mark a page as being cached
1067 * @op: The retrieval op pages are being marked for
1068 * @page: The page to be marked
1069 *
1070 * Mark a netfs page as being cached. After this is called, the netfs
1071 * must call fscache_uncache_page() to remove the mark.
1072 */
1073void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1074{
1075 struct fscache_cookie *cookie = op->op.object->cookie;
1076
1077#ifdef CONFIG_FSCACHE_STATS
1078 atomic_inc(&fscache_n_marks);
1079#endif
1080
1081 _debug("- mark %p{%lx}", page, page->index);
1082 if (TestSetPageFsCache(page)) {
1083 static bool once_only;
1084 if (!once_only) {
1085 once_only = true;
1086 printk(KERN_WARNING "FS-Cache:"
1087 " Cookie type %s marked page %lx"
1088 " multiple times\n",
1089 cookie->def->name, page->index);
1090 }
1091 }
1092
1093 if (cookie->def->mark_page_cached)
1094 cookie->def->mark_page_cached(cookie->netfs_data,
1095 op->mapping, page);
1096}
1097EXPORT_SYMBOL(fscache_mark_page_cached);
1098
1099/**
David Howellsb5108822009-04-03 16:42:39 +01001100 * fscache_mark_pages_cached - Mark pages as being cached
1101 * @op: The retrieval op pages are being marked for
1102 * @pagevec: The pages to be marked
1103 *
1104 * Mark a bunch of netfs pages as being cached. After this is called,
1105 * the netfs must call fscache_uncache_page() to remove the mark.
1106 */
1107void fscache_mark_pages_cached(struct fscache_retrieval *op,
1108 struct pagevec *pagevec)
1109{
David Howellsb5108822009-04-03 16:42:39 +01001110 unsigned long loop;
1111
David Howellsc4d6d8d2012-12-20 21:52:32 +00001112 for (loop = 0; loop < pagevec->nr; loop++)
1113 fscache_mark_page_cached(op, pagevec->pages[loop]);
David Howellsb5108822009-04-03 16:42:39 +01001114
David Howellsb5108822009-04-03 16:42:39 +01001115 pagevec_reinit(pagevec);
1116}
1117EXPORT_SYMBOL(fscache_mark_pages_cached);
David Howellsc902ce12011-07-07 12:19:48 +01001118
1119/*
1120 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1121 * to be associated with the given cookie.
1122 */
1123void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1124 struct inode *inode)
1125{
1126 struct address_space *mapping = inode->i_mapping;
1127 struct pagevec pvec;
1128 pgoff_t next;
1129 int i;
1130
1131 _enter("%p,%p", cookie, inode);
1132
1133 if (!mapping || mapping->nrpages == 0) {
1134 _leave(" [no pages]");
1135 return;
1136 }
1137
1138 pagevec_init(&pvec, 0);
1139 next = 0;
Jan Beulichb307d462011-07-21 15:02:43 +01001140 do {
1141 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
1142 break;
David Howellsc902ce12011-07-07 12:19:48 +01001143 for (i = 0; i < pagevec_count(&pvec); i++) {
1144 struct page *page = pvec.pages[i];
Jan Beulichb307d462011-07-21 15:02:43 +01001145 next = page->index;
David Howellsc902ce12011-07-07 12:19:48 +01001146 if (PageFsCache(page)) {
1147 __fscache_wait_on_page_write(cookie, page);
1148 __fscache_uncache_page(cookie, page);
1149 }
1150 }
1151 pagevec_release(&pvec);
1152 cond_resched();
Jan Beulichb307d462011-07-21 15:02:43 +01001153 } while (++next);
David Howellsc902ce12011-07-07 12:19:48 +01001154
1155 _leave("");
1156}
1157EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);