blob: b9f18bf4227d5f4f5c8984e3ccaa470624a5c43f [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/*
Milosz Tanski9776de92014-08-13 12:58:16 -040047 * wait for a page to finish being written to the cache. Put a timeout here
48 * since we might be called recursively via parent fs.
49 */
50static
51bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
52{
53 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
54
55 return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
56 HZ);
57}
58
59/*
David Howells201a1542009-11-19 18:11:35 +000060 * decide whether a page can be released, possibly by cancelling a store to it
Mel Gormand0164ad2015-11-06 16:28:21 -080061 * - we're allowed to sleep if __GFP_DIRECT_RECLAIM is flagged
David Howells201a1542009-11-19 18:11:35 +000062 */
63bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
64 struct page *page,
65 gfp_t gfp)
66{
67 struct page *xpage;
68 void *val;
69
70 _enter("%p,%p,%x", cookie, page, gfp);
71
David Howells8c209ce2012-12-05 13:34:49 +000072try_again:
David Howells201a1542009-11-19 18:11:35 +000073 rcu_read_lock();
74 val = radix_tree_lookup(&cookie->stores, page->index);
75 if (!val) {
76 rcu_read_unlock();
77 fscache_stat(&fscache_n_store_vmscan_not_storing);
78 __fscache_uncache_page(cookie, page);
79 return true;
80 }
81
82 /* see if the page is actually undergoing storage - if so we can't get
83 * rid of it till the cache has finished with it */
84 if (radix_tree_tag_get(&cookie->stores, page->index,
85 FSCACHE_COOKIE_STORING_TAG)) {
86 rcu_read_unlock();
87 goto page_busy;
88 }
89
90 /* the page is pending storage, so we attempt to cancel the store and
91 * discard the store request so that the page can be reclaimed */
92 spin_lock(&cookie->stores_lock);
93 rcu_read_unlock();
94
95 if (radix_tree_tag_get(&cookie->stores, page->index,
96 FSCACHE_COOKIE_STORING_TAG)) {
97 /* the page started to undergo storage whilst we were looking,
98 * so now we can only wait or return */
99 spin_unlock(&cookie->stores_lock);
100 goto page_busy;
101 }
102
103 xpage = radix_tree_delete(&cookie->stores, page->index);
104 spin_unlock(&cookie->stores_lock);
105
106 if (xpage) {
107 fscache_stat(&fscache_n_store_vmscan_cancelled);
108 fscache_stat(&fscache_n_store_radix_deletes);
109 ASSERTCMP(xpage, ==, page);
110 } else {
111 fscache_stat(&fscache_n_store_vmscan_gone);
112 }
113
114 wake_up_bit(&cookie->flags, 0);
115 if (xpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300116 put_page(xpage);
David Howells201a1542009-11-19 18:11:35 +0000117 __fscache_uncache_page(cookie, page);
118 return true;
119
120page_busy:
David Howells8c209ce2012-12-05 13:34:49 +0000121 /* We will wait here if we're allowed to, but that could deadlock the
122 * allocator as the work threads writing to the cache may all end up
123 * sleeping on memory allocation, so we may need to impose a timeout
124 * too. */
Mel Gormand0164ad2015-11-06 16:28:21 -0800125 if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS)) {
David Howells8c209ce2012-12-05 13:34:49 +0000126 fscache_stat(&fscache_n_store_vmscan_busy);
127 return false;
128 }
129
130 fscache_stat(&fscache_n_store_vmscan_wait);
Milosz Tanski9776de92014-08-13 12:58:16 -0400131 if (!release_page_wait_timeout(cookie, page))
132 _debug("fscache writeout timeout page: %p{%lx}",
133 page, page->index);
134
Mel Gormand0164ad2015-11-06 16:28:21 -0800135 gfp &= ~__GFP_DIRECT_RECLAIM;
David Howells8c209ce2012-12-05 13:34:49 +0000136 goto try_again;
David Howells201a1542009-11-19 18:11:35 +0000137}
138EXPORT_SYMBOL(__fscache_maybe_release_page);
139
140/*
David Howellsb5108822009-04-03 16:42:39 +0100141 * note that a page has finished being written to the cache
142 */
David Howells1bccf512009-11-19 18:11:25 +0000143static void fscache_end_page_write(struct fscache_object *object,
144 struct page *page)
David Howellsb5108822009-04-03 16:42:39 +0100145{
David Howells1bccf512009-11-19 18:11:25 +0000146 struct fscache_cookie *cookie;
147 struct page *xpage = NULL;
David Howellsb5108822009-04-03 16:42:39 +0100148
David Howells1bccf512009-11-19 18:11:25 +0000149 spin_lock(&object->lock);
150 cookie = object->cookie;
151 if (cookie) {
152 /* delete the page from the tree if it is now no longer
153 * pending */
154 spin_lock(&cookie->stores_lock);
David Howells201a1542009-11-19 18:11:35 +0000155 radix_tree_tag_clear(&cookie->stores, page->index,
156 FSCACHE_COOKIE_STORING_TAG);
David Howells285e7282009-11-19 18:11:29 +0000157 if (!radix_tree_tag_get(&cookie->stores, page->index,
158 FSCACHE_COOKIE_PENDING_TAG)) {
159 fscache_stat(&fscache_n_store_radix_deletes);
160 xpage = radix_tree_delete(&cookie->stores, page->index);
161 }
David Howells1bccf512009-11-19 18:11:25 +0000162 spin_unlock(&cookie->stores_lock);
163 wake_up_bit(&cookie->flags, 0);
164 }
165 spin_unlock(&object->lock);
166 if (xpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300167 put_page(xpage);
David Howellsb5108822009-04-03 16:42:39 +0100168}
169
170/*
171 * actually apply the changed attributes to a cache object
172 */
173static void fscache_attr_changed_op(struct fscache_operation *op)
174{
175 struct fscache_object *object = op->object;
David Howells440f0af2009-11-19 18:11:01 +0000176 int ret;
David Howellsb5108822009-04-03 16:42:39 +0100177
178 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
179
180 fscache_stat(&fscache_n_attr_changed_calls);
181
David Howells8fb883f2013-09-21 00:09:31 +0100182 if (fscache_object_is_active(object)) {
David Howells52bd75f2009-11-19 18:11:08 +0000183 fscache_stat(&fscache_n_cop_attr_changed);
David Howells440f0af2009-11-19 18:11:01 +0000184 ret = object->cache->ops->attr_changed(object);
David Howells52bd75f2009-11-19 18:11:08 +0000185 fscache_stat_d(&fscache_n_cop_attr_changed);
David Howells440f0af2009-11-19 18:11:01 +0000186 if (ret < 0)
187 fscache_abort_object(object);
David Howellsb27ddd42018-04-04 13:41:26 +0100188 fscache_op_complete(op, ret < 0);
189 } else {
190 fscache_op_complete(op, true);
David Howells440f0af2009-11-19 18:11:01 +0000191 }
David Howellsb5108822009-04-03 16:42:39 +0100192
193 _leave("");
194}
195
196/*
197 * notification that the attributes on an object have changed
198 */
199int __fscache_attr_changed(struct fscache_cookie *cookie)
200{
201 struct fscache_operation *op;
202 struct fscache_object *object;
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400203 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100204
205 _enter("%p", cookie);
206
207 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
208
209 fscache_stat(&fscache_n_attr_changed);
210
211 op = kzalloc(sizeof(*op), GFP_KERNEL);
212 if (!op) {
213 fscache_stat(&fscache_n_attr_changed_nomem);
214 _leave(" = -ENOMEM");
215 return -ENOMEM;
216 }
217
David Howellsd3b97ca2015-02-24 10:05:29 +0000218 fscache_operation_init(op, fscache_attr_changed_op, NULL, NULL);
David Howells8fb883f2013-09-21 00:09:31 +0100219 op->flags = FSCACHE_OP_ASYNC |
220 (1 << FSCACHE_OP_EXCLUSIVE) |
221 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100222
223 spin_lock(&cookie->lock);
224
David Howells94d30ae2013-09-21 00:09:31 +0100225 if (!fscache_cookie_enabled(cookie) ||
226 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100227 goto nobufs;
228 object = hlist_entry(cookie->backing_objects.first,
229 struct fscache_object, cookie_link);
230
David Howells8fb883f2013-09-21 00:09:31 +0100231 __fscache_use_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100232 if (fscache_submit_exclusive_op(object, op) < 0)
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400233 goto nobufs_dec;
David Howellsb5108822009-04-03 16:42:39 +0100234 spin_unlock(&cookie->lock);
235 fscache_stat(&fscache_n_attr_changed_ok);
236 fscache_put_operation(op);
237 _leave(" = 0");
238 return 0;
239
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400240nobufs_dec:
David Howells8fb883f2013-09-21 00:09:31 +0100241 wake_cookie = __fscache_unuse_cookie(cookie);
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400242nobufs:
David Howellsb5108822009-04-03 16:42:39 +0100243 spin_unlock(&cookie->lock);
David Howellsa39caad2015-02-25 14:22:40 +0000244 fscache_put_operation(op);
David Howells8fb883f2013-09-21 00:09:31 +0100245 if (wake_cookie)
246 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100247 fscache_stat(&fscache_n_attr_changed_nobufs);
248 _leave(" = %d", -ENOBUFS);
249 return -ENOBUFS;
250}
251EXPORT_SYMBOL(__fscache_attr_changed);
252
253/*
David Howellsd3b97ca2015-02-24 10:05:29 +0000254 * Handle cancellation of a pending retrieval op
255 */
256static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
257{
258 struct fscache_retrieval *op =
259 container_of(_op, struct fscache_retrieval, op);
260
261 atomic_set(&op->n_pages, 0);
262}
263
264/*
David Howellsb5108822009-04-03 16:42:39 +0100265 * release a retrieval op reference
266 */
267static void fscache_release_retrieval_op(struct fscache_operation *_op)
268{
269 struct fscache_retrieval *op =
270 container_of(_op, struct fscache_retrieval, op);
271
272 _enter("{OP%x}", op->op.debug_id);
273
David Howells4a471322015-02-24 10:05:29 +0000274 ASSERTIFCMP(op->op.state != FSCACHE_OP_ST_INITIALISED,
275 atomic_read(&op->n_pages), ==, 0);
David Howells9f105232012-12-20 21:52:35 +0000276
David Howellsb5108822009-04-03 16:42:39 +0100277 fscache_hist(fscache_retrieval_histogram, op->start_time);
278 if (op->context)
David Howells4a471322015-02-24 10:05:29 +0000279 fscache_put_context(op->cookie, op->context);
David Howellsb5108822009-04-03 16:42:39 +0100280
281 _leave("");
282}
283
284/*
285 * allocate a retrieval op
286 */
287static struct fscache_retrieval *fscache_alloc_retrieval(
David Howells13627292013-05-10 19:50:26 +0100288 struct fscache_cookie *cookie,
David Howellsb5108822009-04-03 16:42:39 +0100289 struct address_space *mapping,
290 fscache_rw_complete_t end_io_func,
291 void *context)
292{
293 struct fscache_retrieval *op;
294
295 /* allocate a retrieval operation and attempt to submit it */
296 op = kzalloc(sizeof(*op), GFP_NOIO);
297 if (!op) {
298 fscache_stat(&fscache_n_retrievals_nomem);
299 return NULL;
300 }
301
David Howellsd3b97ca2015-02-24 10:05:29 +0000302 fscache_operation_init(&op->op, NULL,
303 fscache_do_cancel_retrieval,
304 fscache_release_retrieval_op);
David Howells13627292013-05-10 19:50:26 +0100305 op->op.flags = FSCACHE_OP_MYTHREAD |
306 (1UL << FSCACHE_OP_WAITING) |
307 (1UL << FSCACHE_OP_UNUSE_COOKIE);
David Howells4a471322015-02-24 10:05:29 +0000308 op->cookie = cookie;
David Howellsb5108822009-04-03 16:42:39 +0100309 op->mapping = mapping;
310 op->end_io_func = end_io_func;
311 op->context = context;
312 op->start_time = jiffies;
David Howellsb5108822009-04-03 16:42:39 +0100313 INIT_LIST_HEAD(&op->to_do);
David Howells4a471322015-02-24 10:05:29 +0000314
315 /* Pin the netfs read context in case we need to do the actual netfs
316 * read because we've encountered a cache read failure.
317 */
318 if (context)
319 fscache_get_context(op->cookie, context);
David Howellsb5108822009-04-03 16:42:39 +0100320 return op;
321}
322
323/*
324 * wait for a deferred lookup to complete
325 */
David Howellsda9803b2013-08-21 17:29:38 -0400326int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
David Howellsb5108822009-04-03 16:42:39 +0100327{
328 unsigned long jif;
329
330 _enter("");
331
332 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
333 _leave(" = 0 [imm]");
334 return 0;
335 }
336
337 fscache_stat(&fscache_n_retrievals_wait);
338
339 jif = jiffies;
340 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
David Howellsb5108822009-04-03 16:42:39 +0100341 TASK_INTERRUPTIBLE) != 0) {
342 fscache_stat(&fscache_n_retrievals_intr);
343 _leave(" = -ERESTARTSYS");
344 return -ERESTARTSYS;
345 }
346
347 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
348
349 smp_rmb();
350 fscache_hist(fscache_retrieval_delay_histogram, jif);
351 _leave(" = 0 [dly]");
352 return 0;
353}
354
355/*
David Howells60d543c2009-11-19 18:11:45 +0000356 * wait for an object to become active (or dead)
357 */
David Howellsda9803b2013-08-21 17:29:38 -0400358int fscache_wait_for_operation_activation(struct fscache_object *object,
359 struct fscache_operation *op,
360 atomic_t *stat_op_waits,
David Howellsd3b97ca2015-02-24 10:05:29 +0000361 atomic_t *stat_object_dead)
David Howells60d543c2009-11-19 18:11:45 +0000362{
363 int ret;
364
David Howellsda9803b2013-08-21 17:29:38 -0400365 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
David Howells60d543c2009-11-19 18:11:45 +0000366 goto check_if_dead;
367
368 _debug(">>> WT");
David Howellsda9803b2013-08-21 17:29:38 -0400369 if (stat_op_waits)
370 fscache_stat(stat_op_waits);
371 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
David Howells9c04caa2012-12-07 18:08:02 +0000372 TASK_INTERRUPTIBLE) != 0) {
David Howellsd3b97ca2015-02-24 10:05:29 +0000373 ret = fscache_cancel_op(op, false);
David Howells60d543c2009-11-19 18:11:45 +0000374 if (ret == 0)
375 return -ERESTARTSYS;
376
377 /* it's been removed from the pending queue by another party,
378 * so we should get to run shortly */
David Howellsda9803b2013-08-21 17:29:38 -0400379 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
NeilBrown74316202014-07-07 15:16:04 +1000380 TASK_UNINTERRUPTIBLE);
David Howells60d543c2009-11-19 18:11:45 +0000381 }
382 _debug("<<< GO");
383
384check_if_dead:
David Howellsda9803b2013-08-21 17:29:38 -0400385 if (op->state == FSCACHE_OP_ST_CANCELLED) {
386 if (stat_object_dead)
387 fscache_stat(stat_object_dead);
David Howells9f105232012-12-20 21:52:35 +0000388 _leave(" = -ENOBUFS [cancelled]");
389 return -ENOBUFS;
390 }
David Howells87021522015-02-24 10:52:51 +0000391 if (unlikely(fscache_object_is_dying(object) ||
392 fscache_cache_is_broken(object))) {
393 enum fscache_operation_state state = op->state;
David Howellsd3b97ca2015-02-24 10:05:29 +0000394 fscache_cancel_op(op, true);
David Howellsda9803b2013-08-21 17:29:38 -0400395 if (stat_object_dead)
396 fscache_stat(stat_object_dead);
David Howells87021522015-02-24 10:52:51 +0000397 _leave(" = -ENOBUFS [obj dead %d]", state);
David Howells60d543c2009-11-19 18:11:45 +0000398 return -ENOBUFS;
399 }
400 return 0;
401}
402
403/*
David Howellsb5108822009-04-03 16:42:39 +0100404 * read a page from the cache or allocate a block in which to store it
405 * - we return:
406 * -ENOMEM - out of memory, nothing done
407 * -ERESTARTSYS - interrupted
408 * -ENOBUFS - no backing object available in which to cache the block
409 * -ENODATA - no data available in the backing object for this block
410 * 0 - dispatched a read - it'll call end_io_func() when finished
411 */
412int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
413 struct page *page,
414 fscache_rw_complete_t end_io_func,
415 void *context,
416 gfp_t gfp)
417{
418 struct fscache_retrieval *op;
419 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100420 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100421 int ret;
422
423 _enter("%p,%p,,,", cookie, page);
424
425 fscache_stat(&fscache_n_retrievals);
426
427 if (hlist_empty(&cookie->backing_objects))
428 goto nobufs;
429
David Howellsef778e72012-12-20 21:52:36 +0000430 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
431 _leave(" = -ENOBUFS [invalidating]");
432 return -ENOBUFS;
433 }
434
David Howellsb5108822009-04-03 16:42:39 +0100435 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
436 ASSERTCMP(page, !=, NULL);
437
438 if (fscache_wait_for_deferred_lookup(cookie) < 0)
439 return -ERESTARTSYS;
440
David Howells13627292013-05-10 19:50:26 +0100441 op = fscache_alloc_retrieval(cookie, page->mapping,
David Howells94d30ae2013-09-21 00:09:31 +0100442 end_io_func, context);
David Howellsb5108822009-04-03 16:42:39 +0100443 if (!op) {
444 _leave(" = -ENOMEM");
445 return -ENOMEM;
446 }
David Howells1bb4b7f92013-05-21 13:44:15 +0100447 atomic_set(&op->n_pages, 1);
David Howellsb5108822009-04-03 16:42:39 +0100448
449 spin_lock(&cookie->lock);
450
David Howells94d30ae2013-09-21 00:09:31 +0100451 if (!fscache_cookie_enabled(cookie) ||
452 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100453 goto nobufs_unlock;
454 object = hlist_entry(cookie->backing_objects.first,
455 struct fscache_object, cookie_link);
456
David Howellscaaef692013-05-10 19:50:26 +0100457 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
David Howellsb5108822009-04-03 16:42:39 +0100458
David Howells8fb883f2013-09-21 00:09:31 +0100459 __fscache_use_cookie(cookie);
David Howells4fbf4292009-11-19 18:11:04 +0000460 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000461 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000462
David Howellsb5108822009-04-03 16:42:39 +0100463 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000464 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100465 spin_unlock(&cookie->lock);
466
467 fscache_stat(&fscache_n_retrieval_ops);
468
David Howellsb5108822009-04-03 16:42:39 +0100469 /* we wait for the operation to become active, and then process it
470 * *here*, in this thread, and not in the thread pool */
David Howellsda9803b2013-08-21 17:29:38 -0400471 ret = fscache_wait_for_operation_activation(
472 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000473 __fscache_stat(&fscache_n_retrieval_op_waits),
David Howellsd3b97ca2015-02-24 10:05:29 +0000474 __fscache_stat(&fscache_n_retrievals_object_dead));
David Howells60d543c2009-11-19 18:11:45 +0000475 if (ret < 0)
476 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100477
478 /* ask the cache to honour the operation */
479 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
David Howells52bd75f2009-11-19 18:11:08 +0000480 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100481 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000482 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100483 if (ret == 0)
484 ret = -ENODATA;
485 } else {
David Howells52bd75f2009-11-19 18:11:08 +0000486 fscache_stat(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100487 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000488 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100489 }
490
David Howells5753c442009-11-19 18:11:19 +0000491error:
David Howellsb5108822009-04-03 16:42:39 +0100492 if (ret == -ENOMEM)
493 fscache_stat(&fscache_n_retrievals_nomem);
494 else if (ret == -ERESTARTSYS)
495 fscache_stat(&fscache_n_retrievals_intr);
496 else if (ret == -ENODATA)
497 fscache_stat(&fscache_n_retrievals_nodata);
498 else if (ret < 0)
499 fscache_stat(&fscache_n_retrievals_nobufs);
500 else
501 fscache_stat(&fscache_n_retrievals_ok);
502
503 fscache_put_retrieval(op);
504 _leave(" = %d", ret);
505 return ret;
506
David Howells9f105232012-12-20 21:52:35 +0000507nobufs_unlock_dec:
508 atomic_dec(&object->n_reads);
David Howells8fb883f2013-09-21 00:09:31 +0100509 wake_cookie = __fscache_unuse_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100510nobufs_unlock:
511 spin_unlock(&cookie->lock);
David Howells8fb883f2013-09-21 00:09:31 +0100512 if (wake_cookie)
513 __fscache_wake_unused_cookie(cookie);
David Howellsa39caad2015-02-25 14:22:40 +0000514 fscache_put_retrieval(op);
David Howellsb5108822009-04-03 16:42:39 +0100515nobufs:
516 fscache_stat(&fscache_n_retrievals_nobufs);
517 _leave(" = -ENOBUFS");
518 return -ENOBUFS;
519}
520EXPORT_SYMBOL(__fscache_read_or_alloc_page);
521
522/*
523 * read a list of page from the cache or allocate a block in which to store
524 * them
525 * - we return:
526 * -ENOMEM - out of memory, some pages may be being read
527 * -ERESTARTSYS - interrupted, some pages may be being read
528 * -ENOBUFS - no backing object or space available in which to cache any
529 * pages not being read
530 * -ENODATA - no data available in the backing object for some or all of
531 * the pages
532 * 0 - dispatched a read on all pages
533 *
534 * end_io_func() will be called for each page read from the cache as it is
535 * finishes being read
536 *
537 * any pages for which a read is dispatched will be removed from pages and
538 * nr_pages
539 */
540int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
541 struct address_space *mapping,
542 struct list_head *pages,
543 unsigned *nr_pages,
544 fscache_rw_complete_t end_io_func,
545 void *context,
546 gfp_t gfp)
547{
David Howellsb5108822009-04-03 16:42:39 +0100548 struct fscache_retrieval *op;
549 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100550 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100551 int ret;
552
553 _enter("%p,,%d,,,", cookie, *nr_pages);
554
555 fscache_stat(&fscache_n_retrievals);
556
557 if (hlist_empty(&cookie->backing_objects))
558 goto nobufs;
559
David Howellsef778e72012-12-20 21:52:36 +0000560 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
561 _leave(" = -ENOBUFS [invalidating]");
562 return -ENOBUFS;
563 }
564
David Howellsb5108822009-04-03 16:42:39 +0100565 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
566 ASSERTCMP(*nr_pages, >, 0);
567 ASSERT(!list_empty(pages));
568
569 if (fscache_wait_for_deferred_lookup(cookie) < 0)
570 return -ERESTARTSYS;
571
David Howells13627292013-05-10 19:50:26 +0100572 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
David Howellsb5108822009-04-03 16:42:39 +0100573 if (!op)
574 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100575 atomic_set(&op->n_pages, *nr_pages);
David Howellsb5108822009-04-03 16:42:39 +0100576
577 spin_lock(&cookie->lock);
578
David Howells94d30ae2013-09-21 00:09:31 +0100579 if (!fscache_cookie_enabled(cookie) ||
580 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100581 goto nobufs_unlock;
582 object = hlist_entry(cookie->backing_objects.first,
583 struct fscache_object, cookie_link);
584
David Howells8fb883f2013-09-21 00:09:31 +0100585 __fscache_use_cookie(cookie);
David Howells4fbf4292009-11-19 18:11:04 +0000586 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000587 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000588
David Howellsb5108822009-04-03 16:42:39 +0100589 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000590 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100591 spin_unlock(&cookie->lock);
592
593 fscache_stat(&fscache_n_retrieval_ops);
594
David Howellsb5108822009-04-03 16:42:39 +0100595 /* we wait for the operation to become active, and then process it
596 * *here*, in this thread, and not in the thread pool */
David Howellsda9803b2013-08-21 17:29:38 -0400597 ret = fscache_wait_for_operation_activation(
598 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000599 __fscache_stat(&fscache_n_retrieval_op_waits),
David Howellsd3b97ca2015-02-24 10:05:29 +0000600 __fscache_stat(&fscache_n_retrievals_object_dead));
David Howells60d543c2009-11-19 18:11:45 +0000601 if (ret < 0)
602 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100603
604 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000605 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
606 fscache_stat(&fscache_n_cop_allocate_pages);
607 ret = object->cache->ops->allocate_pages(
608 op, pages, nr_pages, gfp);
609 fscache_stat_d(&fscache_n_cop_allocate_pages);
610 } else {
611 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
612 ret = object->cache->ops->read_or_alloc_pages(
613 op, pages, nr_pages, gfp);
614 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
615 }
David Howellsb5108822009-04-03 16:42:39 +0100616
David Howells5753c442009-11-19 18:11:19 +0000617error:
David Howellsb5108822009-04-03 16:42:39 +0100618 if (ret == -ENOMEM)
619 fscache_stat(&fscache_n_retrievals_nomem);
620 else if (ret == -ERESTARTSYS)
621 fscache_stat(&fscache_n_retrievals_intr);
622 else if (ret == -ENODATA)
623 fscache_stat(&fscache_n_retrievals_nodata);
624 else if (ret < 0)
625 fscache_stat(&fscache_n_retrievals_nobufs);
626 else
627 fscache_stat(&fscache_n_retrievals_ok);
628
629 fscache_put_retrieval(op);
630 _leave(" = %d", ret);
631 return ret;
632
David Howells9f105232012-12-20 21:52:35 +0000633nobufs_unlock_dec:
634 atomic_dec(&object->n_reads);
David Howells8fb883f2013-09-21 00:09:31 +0100635 wake_cookie = __fscache_unuse_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100636nobufs_unlock:
637 spin_unlock(&cookie->lock);
David Howellsa39caad2015-02-25 14:22:40 +0000638 fscache_put_retrieval(op);
David Howells8fb883f2013-09-21 00:09:31 +0100639 if (wake_cookie)
640 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100641nobufs:
642 fscache_stat(&fscache_n_retrievals_nobufs);
643 _leave(" = -ENOBUFS");
644 return -ENOBUFS;
645}
646EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
647
648/*
649 * allocate a block in the cache on which to store a page
650 * - we return:
651 * -ENOMEM - out of memory, nothing done
652 * -ERESTARTSYS - interrupted
653 * -ENOBUFS - no backing object available in which to cache the block
654 * 0 - block allocated
655 */
656int __fscache_alloc_page(struct fscache_cookie *cookie,
657 struct page *page,
658 gfp_t gfp)
659{
660 struct fscache_retrieval *op;
661 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100662 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100663 int ret;
664
665 _enter("%p,%p,,,", cookie, page);
666
667 fscache_stat(&fscache_n_allocs);
668
669 if (hlist_empty(&cookie->backing_objects))
670 goto nobufs;
671
672 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
673 ASSERTCMP(page, !=, NULL);
674
David Howellsef778e72012-12-20 21:52:36 +0000675 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
676 _leave(" = -ENOBUFS [invalidating]");
677 return -ENOBUFS;
678 }
679
David Howellsb5108822009-04-03 16:42:39 +0100680 if (fscache_wait_for_deferred_lookup(cookie) < 0)
681 return -ERESTARTSYS;
682
David Howells13627292013-05-10 19:50:26 +0100683 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
David Howellsb5108822009-04-03 16:42:39 +0100684 if (!op)
685 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100686 atomic_set(&op->n_pages, 1);
David Howellsb5108822009-04-03 16:42:39 +0100687
688 spin_lock(&cookie->lock);
689
David Howells94d30ae2013-09-21 00:09:31 +0100690 if (!fscache_cookie_enabled(cookie) ||
691 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100692 goto nobufs_unlock;
693 object = hlist_entry(cookie->backing_objects.first,
694 struct fscache_object, cookie_link);
695
David Howells8fb883f2013-09-21 00:09:31 +0100696 __fscache_use_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100697 if (fscache_submit_op(object, &op->op) < 0)
David Howells8fb883f2013-09-21 00:09:31 +0100698 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100699 spin_unlock(&cookie->lock);
700
701 fscache_stat(&fscache_n_alloc_ops);
702
David Howellsda9803b2013-08-21 17:29:38 -0400703 ret = fscache_wait_for_operation_activation(
704 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000705 __fscache_stat(&fscache_n_alloc_op_waits),
David Howellsd3b97ca2015-02-24 10:05:29 +0000706 __fscache_stat(&fscache_n_allocs_object_dead));
David Howells60d543c2009-11-19 18:11:45 +0000707 if (ret < 0)
708 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100709
710 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000711 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100712 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000713 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100714
David Howells5753c442009-11-19 18:11:19 +0000715error:
716 if (ret == -ERESTARTSYS)
717 fscache_stat(&fscache_n_allocs_intr);
718 else if (ret < 0)
David Howellsb5108822009-04-03 16:42:39 +0100719 fscache_stat(&fscache_n_allocs_nobufs);
720 else
721 fscache_stat(&fscache_n_allocs_ok);
722
723 fscache_put_retrieval(op);
724 _leave(" = %d", ret);
725 return ret;
726
David Howells8fb883f2013-09-21 00:09:31 +0100727nobufs_unlock_dec:
728 wake_cookie = __fscache_unuse_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100729nobufs_unlock:
730 spin_unlock(&cookie->lock);
David Howellsa39caad2015-02-25 14:22:40 +0000731 fscache_put_retrieval(op);
David Howells8fb883f2013-09-21 00:09:31 +0100732 if (wake_cookie)
733 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100734nobufs:
735 fscache_stat(&fscache_n_allocs_nobufs);
736 _leave(" = -ENOBUFS");
737 return -ENOBUFS;
738}
739EXPORT_SYMBOL(__fscache_alloc_page);
740
741/*
Milosz Tanski5a6f2822013-08-21 17:30:11 -0400742 * Unmark pages allocate in the readahead code path (via:
743 * fscache_readpages_or_alloc) after delegating to the base filesystem
744 */
745void __fscache_readpages_cancel(struct fscache_cookie *cookie,
746 struct list_head *pages)
747{
748 struct page *page;
749
750 list_for_each_entry(page, pages, lru) {
751 if (PageFsCache(page))
752 __fscache_uncache_page(cookie, page);
753 }
754}
755EXPORT_SYMBOL(__fscache_readpages_cancel);
756
757/*
David Howellsb5108822009-04-03 16:42:39 +0100758 * release a write op reference
759 */
760static void fscache_release_write_op(struct fscache_operation *_op)
761{
762 _enter("{OP%x}", _op->debug_id);
763}
764
765/*
766 * perform the background storage of a page into the cache
767 */
768static void fscache_write_op(struct fscache_operation *_op)
769{
770 struct fscache_storage *op =
771 container_of(_op, struct fscache_storage, op);
772 struct fscache_object *object = op->op.object;
David Howells1bccf512009-11-19 18:11:25 +0000773 struct fscache_cookie *cookie;
David Howellsb5108822009-04-03 16:42:39 +0100774 struct page *page;
775 unsigned n;
776 void *results[1];
777 int ret;
778
779 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
780
David Howells2c984252018-04-04 13:41:26 +0100781again:
David Howellsb5108822009-04-03 16:42:39 +0100782 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000783 cookie = object->cookie;
David Howellsb5108822009-04-03 16:42:39 +0100784
David Howells7ef001e2012-12-07 10:41:26 +0000785 if (!fscache_object_is_active(object)) {
David Howellsb27ddd42018-04-04 13:41:26 +0100786 /* If we get here, then the on-disk cache object likely no
787 * longer exists, so we should just cancel this write
788 * operation.
David Howells7ef001e2012-12-07 10:41:26 +0000789 */
David Howellsb5108822009-04-03 16:42:39 +0100790 spin_unlock(&object->lock);
David Howellsb27ddd42018-04-04 13:41:26 +0100791 fscache_op_complete(&op->op, true);
David Howells7ef001e2012-12-07 10:41:26 +0000792 _leave(" [inactive]");
793 return;
794 }
795
796 if (!cookie) {
797 /* If we get here, then the cookie belonging to the object was
798 * detached, probably by the cookie being withdrawn due to
799 * memory pressure, which means that the pages we might write
800 * to the cache from no longer exist - therefore, we can just
801 * cancel this write operation.
802 */
803 spin_unlock(&object->lock);
David Howellsb27ddd42018-04-04 13:41:26 +0100804 fscache_op_complete(&op->op, true);
David Howellscaaef692013-05-10 19:50:26 +0100805 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
806 _op->flags, _op->state, object->state->short_name,
807 object->flags);
David Howellsb5108822009-04-03 16:42:39 +0100808 return;
809 }
810
David Howells1bccf512009-11-19 18:11:25 +0000811 spin_lock(&cookie->stores_lock);
812
David Howellsb5108822009-04-03 16:42:39 +0100813 fscache_stat(&fscache_n_store_calls);
814
815 /* find a page to store */
816 page = NULL;
817 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
818 FSCACHE_COOKIE_PENDING_TAG);
819 if (n != 1)
820 goto superseded;
821 page = results[0];
822 _debug("gang %d [%lx]", n, page->index);
David Howellsb5108822009-04-03 16:42:39 +0100823
Dan Carpenter08a66852010-06-01 20:58:22 +0100824 radix_tree_tag_set(&cookie->stores, page->index,
825 FSCACHE_COOKIE_STORING_TAG);
826 radix_tree_tag_clear(&cookie->stores, page->index,
827 FSCACHE_COOKIE_PENDING_TAG);
David Howellsb5108822009-04-03 16:42:39 +0100828
David Howells1bccf512009-11-19 18:11:25 +0000829 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100830 spin_unlock(&object->lock);
David Howellsb5108822009-04-03 16:42:39 +0100831
David Howells2c984252018-04-04 13:41:26 +0100832 if (page->index >= op->store_limit)
833 goto discard_page;
834
Dan Carpenter08a66852010-06-01 20:58:22 +0100835 fscache_stat(&fscache_n_store_pages);
836 fscache_stat(&fscache_n_cop_write_page);
837 ret = object->cache->ops->write_page(op, page);
838 fscache_stat_d(&fscache_n_cop_write_page);
Dan Carpenter08a66852010-06-01 20:58:22 +0100839 fscache_end_page_write(object, page);
840 if (ret < 0) {
Dan Carpenter08a66852010-06-01 20:58:22 +0100841 fscache_abort_object(object);
David Howells1f372df2012-12-13 20:03:13 +0000842 fscache_op_complete(&op->op, true);
Dan Carpenter08a66852010-06-01 20:58:22 +0100843 } else {
844 fscache_enqueue_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +0100845 }
846
847 _leave("");
848 return;
849
David Howells2c984252018-04-04 13:41:26 +0100850discard_page:
851 fscache_stat(&fscache_n_store_pages_over_limit);
852 fscache_end_page_write(object, page);
853 goto again;
854
David Howellsb5108822009-04-03 16:42:39 +0100855superseded:
856 /* this writer is going away and there aren't any more things to
857 * write */
858 _debug("cease");
David Howells1bccf512009-11-19 18:11:25 +0000859 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100860 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
861 spin_unlock(&object->lock);
David Howellsb27ddd42018-04-04 13:41:26 +0100862 fscache_op_complete(&op->op, false);
David Howellsb5108822009-04-03 16:42:39 +0100863 _leave("");
864}
865
866/*
David Howellsef778e72012-12-20 21:52:36 +0000867 * Clear the pages pending writing for invalidation
868 */
869void fscache_invalidate_writes(struct fscache_cookie *cookie)
870{
871 struct page *page;
872 void *results[16];
873 int n, i;
874
875 _enter("");
876
Sebastian Andrzej Siewioree8be572013-05-10 19:50:24 +0100877 for (;;) {
878 spin_lock(&cookie->stores_lock);
879 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
880 ARRAY_SIZE(results),
881 FSCACHE_COOKIE_PENDING_TAG);
882 if (n == 0) {
883 spin_unlock(&cookie->stores_lock);
884 break;
885 }
886
David Howellsef778e72012-12-20 21:52:36 +0000887 for (i = n - 1; i >= 0; i--) {
888 page = results[i];
889 radix_tree_delete(&cookie->stores, page->index);
890 }
891
892 spin_unlock(&cookie->stores_lock);
893
894 for (i = n - 1; i >= 0; i--)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300895 put_page(results[i]);
David Howellsef778e72012-12-20 21:52:36 +0000896 }
897
Yan, Zhengd2138452016-05-17 11:52:48 +0800898 wake_up_bit(&cookie->flags, 0);
899
David Howellsef778e72012-12-20 21:52:36 +0000900 _leave("");
901}
902
903/*
David Howellsb5108822009-04-03 16:42:39 +0100904 * request a page be stored in the cache
905 * - returns:
906 * -ENOMEM - out of memory, nothing done
907 * -ENOBUFS - no backing object available in which to cache the page
908 * 0 - dispatched a write - it'll call end_io_func() when finished
909 *
910 * if the cookie still has a backing object at this point, that object can be
911 * in one of a few states with respect to storage processing:
912 *
913 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
914 * set)
915 *
David Howellscaaef692013-05-10 19:50:26 +0100916 * (a) no writes yet
David Howellsb5108822009-04-03 16:42:39 +0100917 *
918 * (b) writes deferred till post-creation (mark page for writing and
919 * return immediately)
920 *
921 * (2) negative lookup, object created, initial fill being made from netfs
David Howellsb5108822009-04-03 16:42:39 +0100922 *
923 * (a) fill point not yet reached this page (mark page for writing and
924 * return)
925 *
926 * (b) fill point passed this page (queue op to store this page)
927 *
928 * (3) object extant (queue op to store this page)
929 *
930 * any other state is invalid
931 */
932int __fscache_write_page(struct fscache_cookie *cookie,
933 struct page *page,
934 gfp_t gfp)
935{
936 struct fscache_storage *op;
937 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100938 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100939 int ret;
940
941 _enter("%p,%x,", cookie, (u32) page->flags);
942
943 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
944 ASSERT(PageFsCache(page));
945
946 fscache_stat(&fscache_n_stores);
947
David Howellsef778e72012-12-20 21:52:36 +0000948 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
949 _leave(" = -ENOBUFS [invalidating]");
950 return -ENOBUFS;
951 }
952
David Howells5f4f9f42012-12-20 21:52:33 +0000953 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
David Howellsb5108822009-04-03 16:42:39 +0100954 if (!op)
955 goto nomem;
956
David Howellsd3b97ca2015-02-24 10:05:29 +0000957 fscache_operation_init(&op->op, fscache_write_op, NULL,
Tejun Heo8af7c122010-07-20 22:09:01 +0200958 fscache_release_write_op);
David Howells13627292013-05-10 19:50:26 +0100959 op->op.flags = FSCACHE_OP_ASYNC |
960 (1 << FSCACHE_OP_WAITING) |
961 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100962
Jan Kara5e4c0d972013-09-11 14:26:05 -0700963 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
David Howellsb5108822009-04-03 16:42:39 +0100964 if (ret < 0)
965 goto nomem_free;
966
967 ret = -ENOBUFS;
968 spin_lock(&cookie->lock);
969
David Howells94d30ae2013-09-21 00:09:31 +0100970 if (!fscache_cookie_enabled(cookie) ||
971 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100972 goto nobufs;
973 object = hlist_entry(cookie->backing_objects.first,
974 struct fscache_object, cookie_link);
975 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
976 goto nobufs;
977
978 /* add the page to the pending-storage radix tree on the backing
979 * object */
980 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000981 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100982
983 _debug("store limit %llx", (unsigned long long) object->store_limit);
984
985 ret = radix_tree_insert(&cookie->stores, page->index, page);
986 if (ret < 0) {
987 if (ret == -EEXIST)
988 goto already_queued;
989 _debug("insert failed %d", ret);
990 goto nobufs_unlock_obj;
991 }
992
993 radix_tree_tag_set(&cookie->stores, page->index,
994 FSCACHE_COOKIE_PENDING_TAG);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300995 get_page(page);
David Howellsb5108822009-04-03 16:42:39 +0100996
997 /* we only want one writer at a time, but we do need to queue new
998 * writers after exclusive ops */
999 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
1000 goto already_pending;
1001
David Howells1bccf512009-11-19 18:11:25 +00001002 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001003 spin_unlock(&object->lock);
1004
1005 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
1006 op->store_limit = object->store_limit;
1007
David Howells8fb883f2013-09-21 00:09:31 +01001008 __fscache_use_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +01001009 if (fscache_submit_op(object, &op->op) < 0)
1010 goto submit_failed;
1011
1012 spin_unlock(&cookie->lock);
1013 radix_tree_preload_end();
1014 fscache_stat(&fscache_n_store_ops);
1015 fscache_stat(&fscache_n_stores_ok);
1016
Tejun Heo8af7c122010-07-20 22:09:01 +02001017 /* the work queue now carries its own ref on the object */
David Howellsb5108822009-04-03 16:42:39 +01001018 fscache_put_operation(&op->op);
1019 _leave(" = 0");
1020 return 0;
1021
1022already_queued:
1023 fscache_stat(&fscache_n_stores_again);
1024already_pending:
David Howells1bccf512009-11-19 18:11:25 +00001025 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001026 spin_unlock(&object->lock);
1027 spin_unlock(&cookie->lock);
1028 radix_tree_preload_end();
David Howellsa39caad2015-02-25 14:22:40 +00001029 fscache_put_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +01001030 fscache_stat(&fscache_n_stores_ok);
1031 _leave(" = 0");
1032 return 0;
1033
1034submit_failed:
David Howells1bccf512009-11-19 18:11:25 +00001035 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001036 radix_tree_delete(&cookie->stores, page->index);
David Howells1bccf512009-11-19 18:11:25 +00001037 spin_unlock(&cookie->stores_lock);
David Howells8fb883f2013-09-21 00:09:31 +01001038 wake_cookie = __fscache_unuse_cookie(cookie);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001039 put_page(page);
David Howellsb5108822009-04-03 16:42:39 +01001040 ret = -ENOBUFS;
1041 goto nobufs;
1042
1043nobufs_unlock_obj:
Dan Carpenter1147d0f2010-03-23 14:48:37 +00001044 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001045 spin_unlock(&object->lock);
1046nobufs:
1047 spin_unlock(&cookie->lock);
1048 radix_tree_preload_end();
David Howellsa39caad2015-02-25 14:22:40 +00001049 fscache_put_operation(&op->op);
David Howells8fb883f2013-09-21 00:09:31 +01001050 if (wake_cookie)
1051 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +01001052 fscache_stat(&fscache_n_stores_nobufs);
1053 _leave(" = -ENOBUFS");
1054 return -ENOBUFS;
1055
1056nomem_free:
David Howellsa39caad2015-02-25 14:22:40 +00001057 fscache_put_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +01001058nomem:
1059 fscache_stat(&fscache_n_stores_oom);
1060 _leave(" = -ENOMEM");
1061 return -ENOMEM;
1062}
1063EXPORT_SYMBOL(__fscache_write_page);
1064
1065/*
1066 * remove a page from the cache
1067 */
1068void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1069{
1070 struct fscache_object *object;
1071
1072 _enter(",%p", page);
1073
1074 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1075 ASSERTCMP(page, !=, NULL);
1076
1077 fscache_stat(&fscache_n_uncaches);
1078
1079 /* cache withdrawal may beat us to it */
1080 if (!PageFsCache(page))
1081 goto done;
1082
1083 /* get the object */
1084 spin_lock(&cookie->lock);
1085
1086 if (hlist_empty(&cookie->backing_objects)) {
1087 ClearPageFsCache(page);
1088 goto done_unlock;
1089 }
1090
1091 object = hlist_entry(cookie->backing_objects.first,
1092 struct fscache_object, cookie_link);
1093
1094 /* there might now be stuff on disk we could read */
1095 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1096
1097 /* only invoke the cache backend if we managed to mark the page
1098 * uncached here; this deals with synchronisation vs withdrawal */
1099 if (TestClearPageFsCache(page) &&
1100 object->cache->ops->uncache_page) {
1101 /* the cache backend releases the cookie lock */
David Howells52bd75f2009-11-19 18:11:08 +00001102 fscache_stat(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001103 object->cache->ops->uncache_page(object, page);
David Howells52bd75f2009-11-19 18:11:08 +00001104 fscache_stat_d(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001105 goto done;
1106 }
1107
1108done_unlock:
1109 spin_unlock(&cookie->lock);
1110done:
1111 _leave("");
1112}
1113EXPORT_SYMBOL(__fscache_uncache_page);
1114
1115/**
David Howellsc4d6d8d2012-12-20 21:52:32 +00001116 * fscache_mark_page_cached - Mark a page as being cached
1117 * @op: The retrieval op pages are being marked for
1118 * @page: The page to be marked
1119 *
1120 * Mark a netfs page as being cached. After this is called, the netfs
1121 * must call fscache_uncache_page() to remove the mark.
1122 */
1123void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1124{
1125 struct fscache_cookie *cookie = op->op.object->cookie;
1126
1127#ifdef CONFIG_FSCACHE_STATS
1128 atomic_inc(&fscache_n_marks);
1129#endif
1130
1131 _debug("- mark %p{%lx}", page, page->index);
1132 if (TestSetPageFsCache(page)) {
1133 static bool once_only;
1134 if (!once_only) {
1135 once_only = true;
Fabian Frederick36dfd112014-06-04 16:05:38 -07001136 pr_warn("Cookie type %s marked page %lx multiple times\n",
1137 cookie->def->name, page->index);
David Howellsc4d6d8d2012-12-20 21:52:32 +00001138 }
1139 }
1140
1141 if (cookie->def->mark_page_cached)
1142 cookie->def->mark_page_cached(cookie->netfs_data,
1143 op->mapping, page);
1144}
1145EXPORT_SYMBOL(fscache_mark_page_cached);
1146
1147/**
David Howellsb5108822009-04-03 16:42:39 +01001148 * fscache_mark_pages_cached - Mark pages as being cached
1149 * @op: The retrieval op pages are being marked for
1150 * @pagevec: The pages to be marked
1151 *
1152 * Mark a bunch of netfs pages as being cached. After this is called,
1153 * the netfs must call fscache_uncache_page() to remove the mark.
1154 */
1155void fscache_mark_pages_cached(struct fscache_retrieval *op,
1156 struct pagevec *pagevec)
1157{
David Howellsb5108822009-04-03 16:42:39 +01001158 unsigned long loop;
1159
David Howellsc4d6d8d2012-12-20 21:52:32 +00001160 for (loop = 0; loop < pagevec->nr; loop++)
1161 fscache_mark_page_cached(op, pagevec->pages[loop]);
David Howellsb5108822009-04-03 16:42:39 +01001162
David Howellsb5108822009-04-03 16:42:39 +01001163 pagevec_reinit(pagevec);
1164}
1165EXPORT_SYMBOL(fscache_mark_pages_cached);
David Howellsc902ce12011-07-07 12:19:48 +01001166
1167/*
1168 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1169 * to be associated with the given cookie.
1170 */
1171void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1172 struct inode *inode)
1173{
1174 struct address_space *mapping = inode->i_mapping;
1175 struct pagevec pvec;
1176 pgoff_t next;
1177 int i;
1178
1179 _enter("%p,%p", cookie, inode);
1180
1181 if (!mapping || mapping->nrpages == 0) {
1182 _leave(" [no pages]");
1183 return;
1184 }
1185
Mel Gorman86679822017-11-15 17:37:52 -08001186 pagevec_init(&pvec);
David Howellsc902ce12011-07-07 12:19:48 +01001187 next = 0;
Jan Beulichb307d462011-07-21 15:02:43 +01001188 do {
Jan Kara397162f2017-09-06 16:21:43 -07001189 if (!pagevec_lookup(&pvec, mapping, &next))
Jan Beulichb307d462011-07-21 15:02:43 +01001190 break;
David Howellsc902ce12011-07-07 12:19:48 +01001191 for (i = 0; i < pagevec_count(&pvec); i++) {
1192 struct page *page = pvec.pages[i];
David Howellsc902ce12011-07-07 12:19:48 +01001193 if (PageFsCache(page)) {
1194 __fscache_wait_on_page_write(cookie, page);
1195 __fscache_uncache_page(cookie, page);
1196 }
1197 }
1198 pagevec_release(&pvec);
1199 cond_resched();
Jan Karad72dc8a2017-09-06 16:21:18 -07001200 } while (next);
David Howellsc902ce12011-07-07 12:19:48 +01001201
1202 _leave("");
1203}
1204EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);