blob: 27df94ef0e0bdc00865fbe4ba579cf9c5c716726 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsb5108822009-04-03 16:42:39 +01002/* Cache page management and data I/O routines
3 *
4 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsb5108822009-04-03 16:42:39 +01006 */
7
8#define FSCACHE_DEBUG_LEVEL PAGE
9#include <linux/module.h>
10#include <linux/fscache-cache.h>
11#include <linux/buffer_head.h>
12#include <linux/pagevec.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Howellsb5108822009-04-03 16:42:39 +010014#include "internal.h"
15
16/*
17 * check to see if a page is being written to the cache
18 */
19bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
20{
21 void *val;
22
23 rcu_read_lock();
24 val = radix_tree_lookup(&cookie->stores, page->index);
25 rcu_read_unlock();
David Howells08c2e3d2018-04-04 13:41:27 +010026 trace_fscache_check_page(cookie, page, val, 0);
David Howellsb5108822009-04-03 16:42:39 +010027
28 return val != NULL;
29}
30EXPORT_SYMBOL(__fscache_check_page_write);
31
32/*
33 * wait for a page to finish being written to the cache
34 */
35void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
36{
37 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
38
David Howells08c2e3d2018-04-04 13:41:27 +010039 trace_fscache_page(cookie, page, fscache_page_write_wait);
40
David Howellsb5108822009-04-03 16:42:39 +010041 wait_event(*wq, !__fscache_check_page_write(cookie, page));
42}
43EXPORT_SYMBOL(__fscache_wait_on_page_write);
44
45/*
Milosz Tanski9776de92014-08-13 12:58:16 -040046 * wait for a page to finish being written to the cache. Put a timeout here
47 * since we might be called recursively via parent fs.
48 */
49static
50bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
51{
52 wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
53
54 return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
55 HZ);
56}
57
58/*
David Howells201a1542009-11-19 18:11:35 +000059 * decide whether a page can be released, possibly by cancelling a store to it
Mel Gormand0164ad2015-11-06 16:28:21 -080060 * - we're allowed to sleep if __GFP_DIRECT_RECLAIM is flagged
David Howells201a1542009-11-19 18:11:35 +000061 */
62bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
63 struct page *page,
64 gfp_t gfp)
65{
66 struct page *xpage;
67 void *val;
68
69 _enter("%p,%p,%x", cookie, page, gfp);
70
David Howells08c2e3d2018-04-04 13:41:27 +010071 trace_fscache_page(cookie, page, fscache_page_maybe_release);
72
David Howells8c209ce2012-12-05 13:34:49 +000073try_again:
David Howells201a1542009-11-19 18:11:35 +000074 rcu_read_lock();
75 val = radix_tree_lookup(&cookie->stores, page->index);
76 if (!val) {
77 rcu_read_unlock();
78 fscache_stat(&fscache_n_store_vmscan_not_storing);
79 __fscache_uncache_page(cookie, page);
80 return true;
81 }
82
83 /* see if the page is actually undergoing storage - if so we can't get
84 * rid of it till the cache has finished with it */
85 if (radix_tree_tag_get(&cookie->stores, page->index,
86 FSCACHE_COOKIE_STORING_TAG)) {
87 rcu_read_unlock();
88 goto page_busy;
89 }
90
91 /* the page is pending storage, so we attempt to cancel the store and
92 * discard the store request so that the page can be reclaimed */
93 spin_lock(&cookie->stores_lock);
94 rcu_read_unlock();
95
96 if (radix_tree_tag_get(&cookie->stores, page->index,
97 FSCACHE_COOKIE_STORING_TAG)) {
98 /* the page started to undergo storage whilst we were looking,
99 * so now we can only wait or return */
100 spin_unlock(&cookie->stores_lock);
101 goto page_busy;
102 }
103
104 xpage = radix_tree_delete(&cookie->stores, page->index);
David Howells08c2e3d2018-04-04 13:41:27 +0100105 trace_fscache_page(cookie, page, fscache_page_radix_delete);
David Howells201a1542009-11-19 18:11:35 +0000106 spin_unlock(&cookie->stores_lock);
107
108 if (xpage) {
109 fscache_stat(&fscache_n_store_vmscan_cancelled);
110 fscache_stat(&fscache_n_store_radix_deletes);
111 ASSERTCMP(xpage, ==, page);
112 } else {
113 fscache_stat(&fscache_n_store_vmscan_gone);
114 }
115
116 wake_up_bit(&cookie->flags, 0);
David Howells08c2e3d2018-04-04 13:41:27 +0100117 trace_fscache_wake_cookie(cookie);
David Howells201a1542009-11-19 18:11:35 +0000118 if (xpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300119 put_page(xpage);
David Howells201a1542009-11-19 18:11:35 +0000120 __fscache_uncache_page(cookie, page);
121 return true;
122
123page_busy:
David Howells8c209ce2012-12-05 13:34:49 +0000124 /* We will wait here if we're allowed to, but that could deadlock the
125 * allocator as the work threads writing to the cache may all end up
126 * sleeping on memory allocation, so we may need to impose a timeout
127 * too. */
Mel Gormand0164ad2015-11-06 16:28:21 -0800128 if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS)) {
David Howells8c209ce2012-12-05 13:34:49 +0000129 fscache_stat(&fscache_n_store_vmscan_busy);
130 return false;
131 }
132
133 fscache_stat(&fscache_n_store_vmscan_wait);
Milosz Tanski9776de92014-08-13 12:58:16 -0400134 if (!release_page_wait_timeout(cookie, page))
135 _debug("fscache writeout timeout page: %p{%lx}",
136 page, page->index);
137
Mel Gormand0164ad2015-11-06 16:28:21 -0800138 gfp &= ~__GFP_DIRECT_RECLAIM;
David Howells8c209ce2012-12-05 13:34:49 +0000139 goto try_again;
David Howells201a1542009-11-19 18:11:35 +0000140}
141EXPORT_SYMBOL(__fscache_maybe_release_page);
142
143/*
David Howellsb5108822009-04-03 16:42:39 +0100144 * note that a page has finished being written to the cache
145 */
David Howells1bccf512009-11-19 18:11:25 +0000146static void fscache_end_page_write(struct fscache_object *object,
147 struct page *page)
David Howellsb5108822009-04-03 16:42:39 +0100148{
David Howells1bccf512009-11-19 18:11:25 +0000149 struct fscache_cookie *cookie;
David Howells08c2e3d2018-04-04 13:41:27 +0100150 struct page *xpage = NULL, *val;
David Howellsb5108822009-04-03 16:42:39 +0100151
David Howells1bccf512009-11-19 18:11:25 +0000152 spin_lock(&object->lock);
153 cookie = object->cookie;
154 if (cookie) {
155 /* delete the page from the tree if it is now no longer
156 * pending */
157 spin_lock(&cookie->stores_lock);
David Howells201a1542009-11-19 18:11:35 +0000158 radix_tree_tag_clear(&cookie->stores, page->index,
159 FSCACHE_COOKIE_STORING_TAG);
David Howells08c2e3d2018-04-04 13:41:27 +0100160 trace_fscache_page(cookie, page, fscache_page_radix_clear_store);
David Howells285e7282009-11-19 18:11:29 +0000161 if (!radix_tree_tag_get(&cookie->stores, page->index,
162 FSCACHE_COOKIE_PENDING_TAG)) {
163 fscache_stat(&fscache_n_store_radix_deletes);
164 xpage = radix_tree_delete(&cookie->stores, page->index);
David Howells08c2e3d2018-04-04 13:41:27 +0100165 trace_fscache_page(cookie, page, fscache_page_radix_delete);
166 trace_fscache_page(cookie, page, fscache_page_write_end);
167
168 val = radix_tree_lookup(&cookie->stores, page->index);
169 trace_fscache_check_page(cookie, page, val, 1);
170 } else {
171 trace_fscache_page(cookie, page, fscache_page_write_end_pend);
David Howells285e7282009-11-19 18:11:29 +0000172 }
David Howells1bccf512009-11-19 18:11:25 +0000173 spin_unlock(&cookie->stores_lock);
174 wake_up_bit(&cookie->flags, 0);
David Howells08c2e3d2018-04-04 13:41:27 +0100175 trace_fscache_wake_cookie(cookie);
176 } else {
177 trace_fscache_page(cookie, page, fscache_page_write_end_noc);
David Howells1bccf512009-11-19 18:11:25 +0000178 }
179 spin_unlock(&object->lock);
180 if (xpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300181 put_page(xpage);
David Howellsb5108822009-04-03 16:42:39 +0100182}
183
184/*
185 * actually apply the changed attributes to a cache object
186 */
187static void fscache_attr_changed_op(struct fscache_operation *op)
188{
189 struct fscache_object *object = op->object;
David Howells440f0af2009-11-19 18:11:01 +0000190 int ret;
David Howellsb5108822009-04-03 16:42:39 +0100191
192 _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
193
194 fscache_stat(&fscache_n_attr_changed_calls);
195
David Howells8fb883f2013-09-21 00:09:31 +0100196 if (fscache_object_is_active(object)) {
David Howells52bd75f2009-11-19 18:11:08 +0000197 fscache_stat(&fscache_n_cop_attr_changed);
David Howells440f0af2009-11-19 18:11:01 +0000198 ret = object->cache->ops->attr_changed(object);
David Howells52bd75f2009-11-19 18:11:08 +0000199 fscache_stat_d(&fscache_n_cop_attr_changed);
David Howells440f0af2009-11-19 18:11:01 +0000200 if (ret < 0)
201 fscache_abort_object(object);
David Howellsb27ddd42018-04-04 13:41:26 +0100202 fscache_op_complete(op, ret < 0);
203 } else {
204 fscache_op_complete(op, true);
David Howells440f0af2009-11-19 18:11:01 +0000205 }
David Howellsb5108822009-04-03 16:42:39 +0100206
207 _leave("");
208}
209
210/*
211 * notification that the attributes on an object have changed
212 */
213int __fscache_attr_changed(struct fscache_cookie *cookie)
214{
215 struct fscache_operation *op;
216 struct fscache_object *object;
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400217 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100218
219 _enter("%p", cookie);
220
221 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
222
223 fscache_stat(&fscache_n_attr_changed);
224
225 op = kzalloc(sizeof(*op), GFP_KERNEL);
226 if (!op) {
227 fscache_stat(&fscache_n_attr_changed_nomem);
228 _leave(" = -ENOMEM");
229 return -ENOMEM;
230 }
231
David Howells08c2e3d2018-04-04 13:41:27 +0100232 fscache_operation_init(cookie, op, fscache_attr_changed_op, NULL, NULL);
233 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_attr_changed);
David Howells8fb883f2013-09-21 00:09:31 +0100234 op->flags = FSCACHE_OP_ASYNC |
235 (1 << FSCACHE_OP_EXCLUSIVE) |
236 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100237
238 spin_lock(&cookie->lock);
239
David Howells94d30ae2013-09-21 00:09:31 +0100240 if (!fscache_cookie_enabled(cookie) ||
241 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100242 goto nobufs;
243 object = hlist_entry(cookie->backing_objects.first,
244 struct fscache_object, cookie_link);
245
David Howells8fb883f2013-09-21 00:09:31 +0100246 __fscache_use_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100247 if (fscache_submit_exclusive_op(object, op) < 0)
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400248 goto nobufs_dec;
David Howellsb5108822009-04-03 16:42:39 +0100249 spin_unlock(&cookie->lock);
250 fscache_stat(&fscache_n_attr_changed_ok);
251 fscache_put_operation(op);
252 _leave(" = 0");
253 return 0;
254
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400255nobufs_dec:
David Howells8fb883f2013-09-21 00:09:31 +0100256 wake_cookie = __fscache_unuse_cookie(cookie);
Milosz Tanski3e1199d2014-08-13 12:58:26 -0400257nobufs:
David Howellsb5108822009-04-03 16:42:39 +0100258 spin_unlock(&cookie->lock);
David Howellsa39caad2015-02-25 14:22:40 +0000259 fscache_put_operation(op);
David Howells8fb883f2013-09-21 00:09:31 +0100260 if (wake_cookie)
261 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100262 fscache_stat(&fscache_n_attr_changed_nobufs);
263 _leave(" = %d", -ENOBUFS);
264 return -ENOBUFS;
265}
266EXPORT_SYMBOL(__fscache_attr_changed);
267
268/*
David Howellsd3b97ca2015-02-24 10:05:29 +0000269 * Handle cancellation of a pending retrieval op
270 */
271static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
272{
273 struct fscache_retrieval *op =
274 container_of(_op, struct fscache_retrieval, op);
275
276 atomic_set(&op->n_pages, 0);
277}
278
279/*
David Howellsb5108822009-04-03 16:42:39 +0100280 * release a retrieval op reference
281 */
282static void fscache_release_retrieval_op(struct fscache_operation *_op)
283{
284 struct fscache_retrieval *op =
285 container_of(_op, struct fscache_retrieval, op);
286
287 _enter("{OP%x}", op->op.debug_id);
288
David Howells4a471322015-02-24 10:05:29 +0000289 ASSERTIFCMP(op->op.state != FSCACHE_OP_ST_INITIALISED,
290 atomic_read(&op->n_pages), ==, 0);
David Howells9f105232012-12-20 21:52:35 +0000291
David Howellsb5108822009-04-03 16:42:39 +0100292 if (op->context)
David Howells4a471322015-02-24 10:05:29 +0000293 fscache_put_context(op->cookie, op->context);
David Howellsb5108822009-04-03 16:42:39 +0100294
295 _leave("");
296}
297
298/*
299 * allocate a retrieval op
300 */
David Howells26aaeff2021-02-22 11:39:47 +0000301struct fscache_retrieval *fscache_alloc_retrieval(
David Howells13627292013-05-10 19:50:26 +0100302 struct fscache_cookie *cookie,
David Howellsb5108822009-04-03 16:42:39 +0100303 struct address_space *mapping,
304 fscache_rw_complete_t end_io_func,
305 void *context)
306{
307 struct fscache_retrieval *op;
308
309 /* allocate a retrieval operation and attempt to submit it */
310 op = kzalloc(sizeof(*op), GFP_NOIO);
311 if (!op) {
312 fscache_stat(&fscache_n_retrievals_nomem);
313 return NULL;
314 }
315
David Howells08c2e3d2018-04-04 13:41:27 +0100316 fscache_operation_init(cookie, &op->op, NULL,
David Howellsd3b97ca2015-02-24 10:05:29 +0000317 fscache_do_cancel_retrieval,
318 fscache_release_retrieval_op);
David Howells13627292013-05-10 19:50:26 +0100319 op->op.flags = FSCACHE_OP_MYTHREAD |
320 (1UL << FSCACHE_OP_WAITING) |
321 (1UL << FSCACHE_OP_UNUSE_COOKIE);
David Howells4a471322015-02-24 10:05:29 +0000322 op->cookie = cookie;
David Howellsb5108822009-04-03 16:42:39 +0100323 op->mapping = mapping;
324 op->end_io_func = end_io_func;
325 op->context = context;
David Howellsb5108822009-04-03 16:42:39 +0100326 INIT_LIST_HEAD(&op->to_do);
David Howells4a471322015-02-24 10:05:29 +0000327
328 /* Pin the netfs read context in case we need to do the actual netfs
329 * read because we've encountered a cache read failure.
330 */
331 if (context)
332 fscache_get_context(op->cookie, context);
David Howellsb5108822009-04-03 16:42:39 +0100333 return op;
334}
335
336/*
337 * wait for a deferred lookup to complete
338 */
David Howellsda9803b2013-08-21 17:29:38 -0400339int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
David Howellsb5108822009-04-03 16:42:39 +0100340{
David Howellsb5108822009-04-03 16:42:39 +0100341 _enter("");
342
343 if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
344 _leave(" = 0 [imm]");
345 return 0;
346 }
347
348 fscache_stat(&fscache_n_retrievals_wait);
349
David Howellsb5108822009-04-03 16:42:39 +0100350 if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
David Howellsb5108822009-04-03 16:42:39 +0100351 TASK_INTERRUPTIBLE) != 0) {
352 fscache_stat(&fscache_n_retrievals_intr);
353 _leave(" = -ERESTARTSYS");
354 return -ERESTARTSYS;
355 }
356
357 ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
358
359 smp_rmb();
David Howellsb5108822009-04-03 16:42:39 +0100360 _leave(" = 0 [dly]");
361 return 0;
362}
363
364/*
David Howells60d543c2009-11-19 18:11:45 +0000365 * wait for an object to become active (or dead)
366 */
David Howellsda9803b2013-08-21 17:29:38 -0400367int fscache_wait_for_operation_activation(struct fscache_object *object,
368 struct fscache_operation *op,
369 atomic_t *stat_op_waits,
David Howellsd3b97ca2015-02-24 10:05:29 +0000370 atomic_t *stat_object_dead)
David Howells60d543c2009-11-19 18:11:45 +0000371{
372 int ret;
373
David Howellsda9803b2013-08-21 17:29:38 -0400374 if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
David Howells60d543c2009-11-19 18:11:45 +0000375 goto check_if_dead;
376
377 _debug(">>> WT");
David Howellsda9803b2013-08-21 17:29:38 -0400378 if (stat_op_waits)
379 fscache_stat(stat_op_waits);
380 if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
David Howells9c04caa2012-12-07 18:08:02 +0000381 TASK_INTERRUPTIBLE) != 0) {
David Howells08c2e3d2018-04-04 13:41:27 +0100382 trace_fscache_op(object->cookie, op, fscache_op_signal);
David Howellsd3b97ca2015-02-24 10:05:29 +0000383 ret = fscache_cancel_op(op, false);
David Howells60d543c2009-11-19 18:11:45 +0000384 if (ret == 0)
385 return -ERESTARTSYS;
386
387 /* it's been removed from the pending queue by another party,
388 * so we should get to run shortly */
David Howellsda9803b2013-08-21 17:29:38 -0400389 wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
NeilBrown74316202014-07-07 15:16:04 +1000390 TASK_UNINTERRUPTIBLE);
David Howells60d543c2009-11-19 18:11:45 +0000391 }
392 _debug("<<< GO");
393
394check_if_dead:
David Howellsda9803b2013-08-21 17:29:38 -0400395 if (op->state == FSCACHE_OP_ST_CANCELLED) {
396 if (stat_object_dead)
397 fscache_stat(stat_object_dead);
David Howells9f105232012-12-20 21:52:35 +0000398 _leave(" = -ENOBUFS [cancelled]");
399 return -ENOBUFS;
400 }
David Howells87021522015-02-24 10:52:51 +0000401 if (unlikely(fscache_object_is_dying(object) ||
402 fscache_cache_is_broken(object))) {
403 enum fscache_operation_state state = op->state;
David Howells08c2e3d2018-04-04 13:41:27 +0100404 trace_fscache_op(object->cookie, op, fscache_op_signal);
David Howellsd3b97ca2015-02-24 10:05:29 +0000405 fscache_cancel_op(op, true);
David Howellsda9803b2013-08-21 17:29:38 -0400406 if (stat_object_dead)
407 fscache_stat(stat_object_dead);
David Howells87021522015-02-24 10:52:51 +0000408 _leave(" = -ENOBUFS [obj dead %d]", state);
David Howells60d543c2009-11-19 18:11:45 +0000409 return -ENOBUFS;
410 }
411 return 0;
412}
413
414/*
David Howellsb5108822009-04-03 16:42:39 +0100415 * read a page from the cache or allocate a block in which to store it
416 * - we return:
417 * -ENOMEM - out of memory, nothing done
418 * -ERESTARTSYS - interrupted
419 * -ENOBUFS - no backing object available in which to cache the block
420 * -ENODATA - no data available in the backing object for this block
421 * 0 - dispatched a read - it'll call end_io_func() when finished
422 */
423int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
424 struct page *page,
425 fscache_rw_complete_t end_io_func,
426 void *context,
427 gfp_t gfp)
428{
429 struct fscache_retrieval *op;
430 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100431 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100432 int ret;
433
434 _enter("%p,%p,,,", cookie, page);
435
436 fscache_stat(&fscache_n_retrievals);
437
438 if (hlist_empty(&cookie->backing_objects))
439 goto nobufs;
440
David Howellsef778e72012-12-20 21:52:36 +0000441 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
442 _leave(" = -ENOBUFS [invalidating]");
443 return -ENOBUFS;
444 }
445
David Howellsb5108822009-04-03 16:42:39 +0100446 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
447 ASSERTCMP(page, !=, NULL);
448
449 if (fscache_wait_for_deferred_lookup(cookie) < 0)
450 return -ERESTARTSYS;
451
David Howells13627292013-05-10 19:50:26 +0100452 op = fscache_alloc_retrieval(cookie, page->mapping,
David Howells94d30ae2013-09-21 00:09:31 +0100453 end_io_func, context);
David Howellsb5108822009-04-03 16:42:39 +0100454 if (!op) {
455 _leave(" = -ENOMEM");
456 return -ENOMEM;
457 }
David Howells1bb4b7f92013-05-21 13:44:15 +0100458 atomic_set(&op->n_pages, 1);
David Howells08c2e3d2018-04-04 13:41:27 +0100459 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_retr_one);
David Howellsb5108822009-04-03 16:42:39 +0100460
461 spin_lock(&cookie->lock);
462
David Howells94d30ae2013-09-21 00:09:31 +0100463 if (!fscache_cookie_enabled(cookie) ||
464 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100465 goto nobufs_unlock;
466 object = hlist_entry(cookie->backing_objects.first,
467 struct fscache_object, cookie_link);
468
David Howellscaaef692013-05-10 19:50:26 +0100469 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
David Howellsb5108822009-04-03 16:42:39 +0100470
David Howells8fb883f2013-09-21 00:09:31 +0100471 __fscache_use_cookie(cookie);
David Howells4fbf4292009-11-19 18:11:04 +0000472 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000473 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000474
David Howellsb5108822009-04-03 16:42:39 +0100475 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000476 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100477 spin_unlock(&cookie->lock);
478
479 fscache_stat(&fscache_n_retrieval_ops);
480
David Howellsb5108822009-04-03 16:42:39 +0100481 /* we wait for the operation to become active, and then process it
482 * *here*, in this thread, and not in the thread pool */
David Howellsda9803b2013-08-21 17:29:38 -0400483 ret = fscache_wait_for_operation_activation(
484 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000485 __fscache_stat(&fscache_n_retrieval_op_waits),
David Howellsd3b97ca2015-02-24 10:05:29 +0000486 __fscache_stat(&fscache_n_retrievals_object_dead));
David Howells60d543c2009-11-19 18:11:45 +0000487 if (ret < 0)
488 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100489
490 /* ask the cache to honour the operation */
491 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
David Howells52bd75f2009-11-19 18:11:08 +0000492 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100493 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000494 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100495 if (ret == 0)
496 ret = -ENODATA;
497 } else {
David Howells52bd75f2009-11-19 18:11:08 +0000498 fscache_stat(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100499 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000500 fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
David Howellsb5108822009-04-03 16:42:39 +0100501 }
502
David Howells5753c442009-11-19 18:11:19 +0000503error:
David Howellsb5108822009-04-03 16:42:39 +0100504 if (ret == -ENOMEM)
505 fscache_stat(&fscache_n_retrievals_nomem);
506 else if (ret == -ERESTARTSYS)
507 fscache_stat(&fscache_n_retrievals_intr);
508 else if (ret == -ENODATA)
509 fscache_stat(&fscache_n_retrievals_nodata);
510 else if (ret < 0)
511 fscache_stat(&fscache_n_retrievals_nobufs);
512 else
513 fscache_stat(&fscache_n_retrievals_ok);
514
515 fscache_put_retrieval(op);
516 _leave(" = %d", ret);
517 return ret;
518
David Howells9f105232012-12-20 21:52:35 +0000519nobufs_unlock_dec:
520 atomic_dec(&object->n_reads);
David Howells8fb883f2013-09-21 00:09:31 +0100521 wake_cookie = __fscache_unuse_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100522nobufs_unlock:
523 spin_unlock(&cookie->lock);
David Howells8fb883f2013-09-21 00:09:31 +0100524 if (wake_cookie)
525 __fscache_wake_unused_cookie(cookie);
David Howellsa39caad2015-02-25 14:22:40 +0000526 fscache_put_retrieval(op);
David Howellsb5108822009-04-03 16:42:39 +0100527nobufs:
528 fscache_stat(&fscache_n_retrievals_nobufs);
529 _leave(" = -ENOBUFS");
530 return -ENOBUFS;
531}
532EXPORT_SYMBOL(__fscache_read_or_alloc_page);
533
534/*
535 * read a list of page from the cache or allocate a block in which to store
536 * them
537 * - we return:
538 * -ENOMEM - out of memory, some pages may be being read
539 * -ERESTARTSYS - interrupted, some pages may be being read
540 * -ENOBUFS - no backing object or space available in which to cache any
541 * pages not being read
542 * -ENODATA - no data available in the backing object for some or all of
543 * the pages
544 * 0 - dispatched a read on all pages
545 *
546 * end_io_func() will be called for each page read from the cache as it is
547 * finishes being read
548 *
549 * any pages for which a read is dispatched will be removed from pages and
550 * nr_pages
551 */
552int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
553 struct address_space *mapping,
554 struct list_head *pages,
555 unsigned *nr_pages,
556 fscache_rw_complete_t end_io_func,
557 void *context,
558 gfp_t gfp)
559{
David Howellsb5108822009-04-03 16:42:39 +0100560 struct fscache_retrieval *op;
561 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100562 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100563 int ret;
564
565 _enter("%p,,%d,,,", cookie, *nr_pages);
566
567 fscache_stat(&fscache_n_retrievals);
568
569 if (hlist_empty(&cookie->backing_objects))
570 goto nobufs;
571
David Howellsef778e72012-12-20 21:52:36 +0000572 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
573 _leave(" = -ENOBUFS [invalidating]");
574 return -ENOBUFS;
575 }
576
David Howellsb5108822009-04-03 16:42:39 +0100577 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
578 ASSERTCMP(*nr_pages, >, 0);
579 ASSERT(!list_empty(pages));
580
581 if (fscache_wait_for_deferred_lookup(cookie) < 0)
582 return -ERESTARTSYS;
583
David Howells13627292013-05-10 19:50:26 +0100584 op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
David Howellsb5108822009-04-03 16:42:39 +0100585 if (!op)
586 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100587 atomic_set(&op->n_pages, *nr_pages);
David Howells08c2e3d2018-04-04 13:41:27 +0100588 trace_fscache_page_op(cookie, NULL, &op->op, fscache_page_op_retr_multi);
David Howellsb5108822009-04-03 16:42:39 +0100589
590 spin_lock(&cookie->lock);
591
David Howells94d30ae2013-09-21 00:09:31 +0100592 if (!fscache_cookie_enabled(cookie) ||
593 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100594 goto nobufs_unlock;
595 object = hlist_entry(cookie->backing_objects.first,
596 struct fscache_object, cookie_link);
597
David Howells8fb883f2013-09-21 00:09:31 +0100598 __fscache_use_cookie(cookie);
David Howells4fbf4292009-11-19 18:11:04 +0000599 atomic_inc(&object->n_reads);
David Howells9f105232012-12-20 21:52:35 +0000600 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
David Howells4fbf4292009-11-19 18:11:04 +0000601
David Howellsb5108822009-04-03 16:42:39 +0100602 if (fscache_submit_op(object, &op->op) < 0)
David Howells9f105232012-12-20 21:52:35 +0000603 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100604 spin_unlock(&cookie->lock);
605
606 fscache_stat(&fscache_n_retrieval_ops);
607
David Howellsb5108822009-04-03 16:42:39 +0100608 /* we wait for the operation to become active, and then process it
609 * *here*, in this thread, and not in the thread pool */
David Howellsda9803b2013-08-21 17:29:38 -0400610 ret = fscache_wait_for_operation_activation(
611 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000612 __fscache_stat(&fscache_n_retrieval_op_waits),
David Howellsd3b97ca2015-02-24 10:05:29 +0000613 __fscache_stat(&fscache_n_retrievals_object_dead));
David Howells60d543c2009-11-19 18:11:45 +0000614 if (ret < 0)
615 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100616
617 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000618 if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
619 fscache_stat(&fscache_n_cop_allocate_pages);
620 ret = object->cache->ops->allocate_pages(
621 op, pages, nr_pages, gfp);
622 fscache_stat_d(&fscache_n_cop_allocate_pages);
623 } else {
624 fscache_stat(&fscache_n_cop_read_or_alloc_pages);
625 ret = object->cache->ops->read_or_alloc_pages(
626 op, pages, nr_pages, gfp);
627 fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
628 }
David Howellsb5108822009-04-03 16:42:39 +0100629
David Howells5753c442009-11-19 18:11:19 +0000630error:
David Howellsb5108822009-04-03 16:42:39 +0100631 if (ret == -ENOMEM)
632 fscache_stat(&fscache_n_retrievals_nomem);
633 else if (ret == -ERESTARTSYS)
634 fscache_stat(&fscache_n_retrievals_intr);
635 else if (ret == -ENODATA)
636 fscache_stat(&fscache_n_retrievals_nodata);
637 else if (ret < 0)
638 fscache_stat(&fscache_n_retrievals_nobufs);
639 else
640 fscache_stat(&fscache_n_retrievals_ok);
641
642 fscache_put_retrieval(op);
643 _leave(" = %d", ret);
644 return ret;
645
David Howells9f105232012-12-20 21:52:35 +0000646nobufs_unlock_dec:
647 atomic_dec(&object->n_reads);
David Howells8fb883f2013-09-21 00:09:31 +0100648 wake_cookie = __fscache_unuse_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100649nobufs_unlock:
650 spin_unlock(&cookie->lock);
David Howellsa39caad2015-02-25 14:22:40 +0000651 fscache_put_retrieval(op);
David Howells8fb883f2013-09-21 00:09:31 +0100652 if (wake_cookie)
653 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100654nobufs:
655 fscache_stat(&fscache_n_retrievals_nobufs);
656 _leave(" = -ENOBUFS");
657 return -ENOBUFS;
658}
659EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
660
661/*
662 * allocate a block in the cache on which to store a page
663 * - we return:
664 * -ENOMEM - out of memory, nothing done
665 * -ERESTARTSYS - interrupted
666 * -ENOBUFS - no backing object available in which to cache the block
667 * 0 - block allocated
668 */
669int __fscache_alloc_page(struct fscache_cookie *cookie,
670 struct page *page,
671 gfp_t gfp)
672{
673 struct fscache_retrieval *op;
674 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100675 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100676 int ret;
677
678 _enter("%p,%p,,,", cookie, page);
679
680 fscache_stat(&fscache_n_allocs);
681
682 if (hlist_empty(&cookie->backing_objects))
683 goto nobufs;
684
685 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
686 ASSERTCMP(page, !=, NULL);
687
David Howellsef778e72012-12-20 21:52:36 +0000688 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
689 _leave(" = -ENOBUFS [invalidating]");
690 return -ENOBUFS;
691 }
692
David Howellsb5108822009-04-03 16:42:39 +0100693 if (fscache_wait_for_deferred_lookup(cookie) < 0)
694 return -ERESTARTSYS;
695
David Howells13627292013-05-10 19:50:26 +0100696 op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
David Howellsb5108822009-04-03 16:42:39 +0100697 if (!op)
698 return -ENOMEM;
David Howells1bb4b7f92013-05-21 13:44:15 +0100699 atomic_set(&op->n_pages, 1);
David Howells08c2e3d2018-04-04 13:41:27 +0100700 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_alloc_one);
David Howellsb5108822009-04-03 16:42:39 +0100701
702 spin_lock(&cookie->lock);
703
David Howells94d30ae2013-09-21 00:09:31 +0100704 if (!fscache_cookie_enabled(cookie) ||
705 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100706 goto nobufs_unlock;
707 object = hlist_entry(cookie->backing_objects.first,
708 struct fscache_object, cookie_link);
709
David Howells8fb883f2013-09-21 00:09:31 +0100710 __fscache_use_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100711 if (fscache_submit_op(object, &op->op) < 0)
David Howells8fb883f2013-09-21 00:09:31 +0100712 goto nobufs_unlock_dec;
David Howellsb5108822009-04-03 16:42:39 +0100713 spin_unlock(&cookie->lock);
714
715 fscache_stat(&fscache_n_alloc_ops);
716
David Howellsda9803b2013-08-21 17:29:38 -0400717 ret = fscache_wait_for_operation_activation(
718 object, &op->op,
David Howells60d543c2009-11-19 18:11:45 +0000719 __fscache_stat(&fscache_n_alloc_op_waits),
David Howellsd3b97ca2015-02-24 10:05:29 +0000720 __fscache_stat(&fscache_n_allocs_object_dead));
David Howells60d543c2009-11-19 18:11:45 +0000721 if (ret < 0)
722 goto error;
David Howellsb5108822009-04-03 16:42:39 +0100723
724 /* ask the cache to honour the operation */
David Howells52bd75f2009-11-19 18:11:08 +0000725 fscache_stat(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100726 ret = object->cache->ops->allocate_page(op, page, gfp);
David Howells52bd75f2009-11-19 18:11:08 +0000727 fscache_stat_d(&fscache_n_cop_allocate_page);
David Howellsb5108822009-04-03 16:42:39 +0100728
David Howells5753c442009-11-19 18:11:19 +0000729error:
730 if (ret == -ERESTARTSYS)
731 fscache_stat(&fscache_n_allocs_intr);
732 else if (ret < 0)
David Howellsb5108822009-04-03 16:42:39 +0100733 fscache_stat(&fscache_n_allocs_nobufs);
734 else
735 fscache_stat(&fscache_n_allocs_ok);
736
737 fscache_put_retrieval(op);
738 _leave(" = %d", ret);
739 return ret;
740
David Howells8fb883f2013-09-21 00:09:31 +0100741nobufs_unlock_dec:
742 wake_cookie = __fscache_unuse_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100743nobufs_unlock:
744 spin_unlock(&cookie->lock);
David Howellsa39caad2015-02-25 14:22:40 +0000745 fscache_put_retrieval(op);
David Howells8fb883f2013-09-21 00:09:31 +0100746 if (wake_cookie)
747 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +0100748nobufs:
749 fscache_stat(&fscache_n_allocs_nobufs);
750 _leave(" = -ENOBUFS");
751 return -ENOBUFS;
752}
753EXPORT_SYMBOL(__fscache_alloc_page);
754
755/*
Milosz Tanski5a6f2822013-08-21 17:30:11 -0400756 * Unmark pages allocate in the readahead code path (via:
757 * fscache_readpages_or_alloc) after delegating to the base filesystem
758 */
759void __fscache_readpages_cancel(struct fscache_cookie *cookie,
760 struct list_head *pages)
761{
762 struct page *page;
763
764 list_for_each_entry(page, pages, lru) {
765 if (PageFsCache(page))
766 __fscache_uncache_page(cookie, page);
767 }
768}
769EXPORT_SYMBOL(__fscache_readpages_cancel);
770
771/*
David Howellsb5108822009-04-03 16:42:39 +0100772 * release a write op reference
773 */
774static void fscache_release_write_op(struct fscache_operation *_op)
775{
776 _enter("{OP%x}", _op->debug_id);
777}
778
779/*
780 * perform the background storage of a page into the cache
781 */
782static void fscache_write_op(struct fscache_operation *_op)
783{
784 struct fscache_storage *op =
785 container_of(_op, struct fscache_storage, op);
786 struct fscache_object *object = op->op.object;
David Howells1bccf512009-11-19 18:11:25 +0000787 struct fscache_cookie *cookie;
David Howellsb5108822009-04-03 16:42:39 +0100788 struct page *page;
789 unsigned n;
790 void *results[1];
791 int ret;
792
793 _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
794
David Howells2c984252018-04-04 13:41:26 +0100795again:
David Howellsb5108822009-04-03 16:42:39 +0100796 spin_lock(&object->lock);
David Howells1bccf512009-11-19 18:11:25 +0000797 cookie = object->cookie;
David Howellsb5108822009-04-03 16:42:39 +0100798
David Howells7ef001e2012-12-07 10:41:26 +0000799 if (!fscache_object_is_active(object)) {
David Howellsb27ddd42018-04-04 13:41:26 +0100800 /* If we get here, then the on-disk cache object likely no
801 * longer exists, so we should just cancel this write
802 * operation.
David Howells7ef001e2012-12-07 10:41:26 +0000803 */
David Howellsb5108822009-04-03 16:42:39 +0100804 spin_unlock(&object->lock);
David Howellsb27ddd42018-04-04 13:41:26 +0100805 fscache_op_complete(&op->op, true);
David Howells7ef001e2012-12-07 10:41:26 +0000806 _leave(" [inactive]");
807 return;
808 }
809
810 if (!cookie) {
811 /* If we get here, then the cookie belonging to the object was
812 * detached, probably by the cookie being withdrawn due to
813 * memory pressure, which means that the pages we might write
814 * to the cache from no longer exist - therefore, we can just
815 * cancel this write operation.
816 */
817 spin_unlock(&object->lock);
David Howellsb27ddd42018-04-04 13:41:26 +0100818 fscache_op_complete(&op->op, true);
David Howellscaaef692013-05-10 19:50:26 +0100819 _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
820 _op->flags, _op->state, object->state->short_name,
821 object->flags);
David Howellsb5108822009-04-03 16:42:39 +0100822 return;
823 }
824
David Howells1bccf512009-11-19 18:11:25 +0000825 spin_lock(&cookie->stores_lock);
826
David Howellsb5108822009-04-03 16:42:39 +0100827 fscache_stat(&fscache_n_store_calls);
828
829 /* find a page to store */
David Howells08c2e3d2018-04-04 13:41:27 +0100830 results[0] = NULL;
David Howellsb5108822009-04-03 16:42:39 +0100831 page = NULL;
832 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
833 FSCACHE_COOKIE_PENDING_TAG);
David Howells08c2e3d2018-04-04 13:41:27 +0100834 trace_fscache_gang_lookup(cookie, &op->op, results, n, op->store_limit);
David Howellsb5108822009-04-03 16:42:39 +0100835 if (n != 1)
836 goto superseded;
837 page = results[0];
838 _debug("gang %d [%lx]", n, page->index);
David Howellsb5108822009-04-03 16:42:39 +0100839
Dan Carpenter08a66852010-06-01 20:58:22 +0100840 radix_tree_tag_set(&cookie->stores, page->index,
841 FSCACHE_COOKIE_STORING_TAG);
842 radix_tree_tag_clear(&cookie->stores, page->index,
843 FSCACHE_COOKIE_PENDING_TAG);
David Howells08c2e3d2018-04-04 13:41:27 +0100844 trace_fscache_page(cookie, page, fscache_page_radix_pend2store);
David Howellsb5108822009-04-03 16:42:39 +0100845
David Howells1bccf512009-11-19 18:11:25 +0000846 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100847 spin_unlock(&object->lock);
David Howellsb5108822009-04-03 16:42:39 +0100848
David Howells2c984252018-04-04 13:41:26 +0100849 if (page->index >= op->store_limit)
850 goto discard_page;
851
Dan Carpenter08a66852010-06-01 20:58:22 +0100852 fscache_stat(&fscache_n_store_pages);
853 fscache_stat(&fscache_n_cop_write_page);
854 ret = object->cache->ops->write_page(op, page);
855 fscache_stat_d(&fscache_n_cop_write_page);
David Howells08c2e3d2018-04-04 13:41:27 +0100856 trace_fscache_wrote_page(cookie, page, &op->op, ret);
Dan Carpenter08a66852010-06-01 20:58:22 +0100857 fscache_end_page_write(object, page);
858 if (ret < 0) {
Dan Carpenter08a66852010-06-01 20:58:22 +0100859 fscache_abort_object(object);
David Howells1f372df2012-12-13 20:03:13 +0000860 fscache_op_complete(&op->op, true);
Dan Carpenter08a66852010-06-01 20:58:22 +0100861 } else {
862 fscache_enqueue_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +0100863 }
864
865 _leave("");
866 return;
867
David Howells2c984252018-04-04 13:41:26 +0100868discard_page:
869 fscache_stat(&fscache_n_store_pages_over_limit);
David Howells08c2e3d2018-04-04 13:41:27 +0100870 trace_fscache_wrote_page(cookie, page, &op->op, -ENOBUFS);
David Howells2c984252018-04-04 13:41:26 +0100871 fscache_end_page_write(object, page);
872 goto again;
873
David Howellsb5108822009-04-03 16:42:39 +0100874superseded:
875 /* this writer is going away and there aren't any more things to
876 * write */
877 _debug("cease");
David Howells1bccf512009-11-19 18:11:25 +0000878 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +0100879 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
880 spin_unlock(&object->lock);
David Howellsb27ddd42018-04-04 13:41:26 +0100881 fscache_op_complete(&op->op, false);
David Howellsb5108822009-04-03 16:42:39 +0100882 _leave("");
883}
884
885/*
David Howellsef778e72012-12-20 21:52:36 +0000886 * Clear the pages pending writing for invalidation
887 */
888void fscache_invalidate_writes(struct fscache_cookie *cookie)
889{
890 struct page *page;
891 void *results[16];
892 int n, i;
893
894 _enter("");
895
Sebastian Andrzej Siewioree8be572013-05-10 19:50:24 +0100896 for (;;) {
897 spin_lock(&cookie->stores_lock);
898 n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
899 ARRAY_SIZE(results),
900 FSCACHE_COOKIE_PENDING_TAG);
901 if (n == 0) {
902 spin_unlock(&cookie->stores_lock);
903 break;
904 }
905
David Howellsef778e72012-12-20 21:52:36 +0000906 for (i = n - 1; i >= 0; i--) {
907 page = results[i];
908 radix_tree_delete(&cookie->stores, page->index);
David Howells08c2e3d2018-04-04 13:41:27 +0100909 trace_fscache_page(cookie, page, fscache_page_radix_delete);
910 trace_fscache_page(cookie, page, fscache_page_inval);
David Howellsef778e72012-12-20 21:52:36 +0000911 }
912
913 spin_unlock(&cookie->stores_lock);
914
915 for (i = n - 1; i >= 0; i--)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300916 put_page(results[i]);
David Howellsef778e72012-12-20 21:52:36 +0000917 }
918
Yan, Zhengd2138452016-05-17 11:52:48 +0800919 wake_up_bit(&cookie->flags, 0);
David Howells08c2e3d2018-04-04 13:41:27 +0100920 trace_fscache_wake_cookie(cookie);
Yan, Zhengd2138452016-05-17 11:52:48 +0800921
David Howellsef778e72012-12-20 21:52:36 +0000922 _leave("");
923}
924
925/*
David Howellsb5108822009-04-03 16:42:39 +0100926 * request a page be stored in the cache
927 * - returns:
928 * -ENOMEM - out of memory, nothing done
929 * -ENOBUFS - no backing object available in which to cache the page
930 * 0 - dispatched a write - it'll call end_io_func() when finished
931 *
932 * if the cookie still has a backing object at this point, that object can be
933 * in one of a few states with respect to storage processing:
934 *
935 * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
936 * set)
937 *
David Howellscaaef692013-05-10 19:50:26 +0100938 * (a) no writes yet
David Howellsb5108822009-04-03 16:42:39 +0100939 *
940 * (b) writes deferred till post-creation (mark page for writing and
941 * return immediately)
942 *
943 * (2) negative lookup, object created, initial fill being made from netfs
David Howellsb5108822009-04-03 16:42:39 +0100944 *
945 * (a) fill point not yet reached this page (mark page for writing and
946 * return)
947 *
948 * (b) fill point passed this page (queue op to store this page)
949 *
950 * (3) object extant (queue op to store this page)
951 *
952 * any other state is invalid
953 */
954int __fscache_write_page(struct fscache_cookie *cookie,
955 struct page *page,
David Howellsee1235a2018-04-04 13:41:28 +0100956 loff_t object_size,
David Howellsb5108822009-04-03 16:42:39 +0100957 gfp_t gfp)
958{
959 struct fscache_storage *op;
960 struct fscache_object *object;
David Howells8fb883f2013-09-21 00:09:31 +0100961 bool wake_cookie = false;
David Howellsb5108822009-04-03 16:42:39 +0100962 int ret;
963
964 _enter("%p,%x,", cookie, (u32) page->flags);
965
966 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
967 ASSERT(PageFsCache(page));
968
969 fscache_stat(&fscache_n_stores);
970
David Howellsef778e72012-12-20 21:52:36 +0000971 if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
972 _leave(" = -ENOBUFS [invalidating]");
973 return -ENOBUFS;
974 }
975
David Howells5f4f9f42012-12-20 21:52:33 +0000976 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
David Howellsb5108822009-04-03 16:42:39 +0100977 if (!op)
978 goto nomem;
979
David Howells08c2e3d2018-04-04 13:41:27 +0100980 fscache_operation_init(cookie, &op->op, fscache_write_op, NULL,
Tejun Heo8af7c122010-07-20 22:09:01 +0200981 fscache_release_write_op);
David Howells13627292013-05-10 19:50:26 +0100982 op->op.flags = FSCACHE_OP_ASYNC |
983 (1 << FSCACHE_OP_WAITING) |
984 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsb5108822009-04-03 16:42:39 +0100985
Jan Kara5e4c0d972013-09-11 14:26:05 -0700986 ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
David Howellsb5108822009-04-03 16:42:39 +0100987 if (ret < 0)
988 goto nomem_free;
989
David Howells08c2e3d2018-04-04 13:41:27 +0100990 trace_fscache_page_op(cookie, page, &op->op, fscache_page_op_write_one);
991
David Howellsb5108822009-04-03 16:42:39 +0100992 ret = -ENOBUFS;
993 spin_lock(&cookie->lock);
994
David Howells94d30ae2013-09-21 00:09:31 +0100995 if (!fscache_cookie_enabled(cookie) ||
996 hlist_empty(&cookie->backing_objects))
David Howellsb5108822009-04-03 16:42:39 +0100997 goto nobufs;
998 object = hlist_entry(cookie->backing_objects.first,
999 struct fscache_object, cookie_link);
1000 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
1001 goto nobufs;
1002
David Howells08c2e3d2018-04-04 13:41:27 +01001003 trace_fscache_page(cookie, page, fscache_page_write);
1004
David Howellsb5108822009-04-03 16:42:39 +01001005 /* add the page to the pending-storage radix tree on the backing
1006 * object */
1007 spin_lock(&object->lock);
David Howellsee1235a2018-04-04 13:41:28 +01001008
1009 if (object->store_limit_l != object_size)
1010 fscache_set_store_limit(object, object_size);
1011
David Howells1bccf512009-11-19 18:11:25 +00001012 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001013
1014 _debug("store limit %llx", (unsigned long long) object->store_limit);
1015
1016 ret = radix_tree_insert(&cookie->stores, page->index, page);
1017 if (ret < 0) {
1018 if (ret == -EEXIST)
1019 goto already_queued;
1020 _debug("insert failed %d", ret);
1021 goto nobufs_unlock_obj;
1022 }
1023
David Howells08c2e3d2018-04-04 13:41:27 +01001024 trace_fscache_page(cookie, page, fscache_page_radix_insert);
David Howellsb5108822009-04-03 16:42:39 +01001025 radix_tree_tag_set(&cookie->stores, page->index,
1026 FSCACHE_COOKIE_PENDING_TAG);
David Howells08c2e3d2018-04-04 13:41:27 +01001027 trace_fscache_page(cookie, page, fscache_page_radix_set_pend);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 get_page(page);
David Howellsb5108822009-04-03 16:42:39 +01001029
1030 /* we only want one writer at a time, but we do need to queue new
1031 * writers after exclusive ops */
1032 if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
1033 goto already_pending;
1034
David Howells1bccf512009-11-19 18:11:25 +00001035 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001036 spin_unlock(&object->lock);
1037
1038 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
1039 op->store_limit = object->store_limit;
1040
David Howells8fb883f2013-09-21 00:09:31 +01001041 __fscache_use_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +01001042 if (fscache_submit_op(object, &op->op) < 0)
1043 goto submit_failed;
1044
1045 spin_unlock(&cookie->lock);
1046 radix_tree_preload_end();
1047 fscache_stat(&fscache_n_store_ops);
1048 fscache_stat(&fscache_n_stores_ok);
1049
Tejun Heo8af7c122010-07-20 22:09:01 +02001050 /* the work queue now carries its own ref on the object */
David Howellsb5108822009-04-03 16:42:39 +01001051 fscache_put_operation(&op->op);
1052 _leave(" = 0");
1053 return 0;
1054
1055already_queued:
1056 fscache_stat(&fscache_n_stores_again);
1057already_pending:
David Howells1bccf512009-11-19 18:11:25 +00001058 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001059 spin_unlock(&object->lock);
1060 spin_unlock(&cookie->lock);
1061 radix_tree_preload_end();
David Howellsa39caad2015-02-25 14:22:40 +00001062 fscache_put_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +01001063 fscache_stat(&fscache_n_stores_ok);
1064 _leave(" = 0");
1065 return 0;
1066
1067submit_failed:
David Howells1bccf512009-11-19 18:11:25 +00001068 spin_lock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001069 radix_tree_delete(&cookie->stores, page->index);
David Howells08c2e3d2018-04-04 13:41:27 +01001070 trace_fscache_page(cookie, page, fscache_page_radix_delete);
David Howells1bccf512009-11-19 18:11:25 +00001071 spin_unlock(&cookie->stores_lock);
David Howells8fb883f2013-09-21 00:09:31 +01001072 wake_cookie = __fscache_unuse_cookie(cookie);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 put_page(page);
David Howellsb5108822009-04-03 16:42:39 +01001074 ret = -ENOBUFS;
1075 goto nobufs;
1076
1077nobufs_unlock_obj:
Dan Carpenter1147d0f2010-03-23 14:48:37 +00001078 spin_unlock(&cookie->stores_lock);
David Howellsb5108822009-04-03 16:42:39 +01001079 spin_unlock(&object->lock);
1080nobufs:
1081 spin_unlock(&cookie->lock);
1082 radix_tree_preload_end();
David Howellsa39caad2015-02-25 14:22:40 +00001083 fscache_put_operation(&op->op);
David Howells8fb883f2013-09-21 00:09:31 +01001084 if (wake_cookie)
1085 __fscache_wake_unused_cookie(cookie);
David Howellsb5108822009-04-03 16:42:39 +01001086 fscache_stat(&fscache_n_stores_nobufs);
1087 _leave(" = -ENOBUFS");
1088 return -ENOBUFS;
1089
1090nomem_free:
David Howellsa39caad2015-02-25 14:22:40 +00001091 fscache_put_operation(&op->op);
David Howellsb5108822009-04-03 16:42:39 +01001092nomem:
1093 fscache_stat(&fscache_n_stores_oom);
1094 _leave(" = -ENOMEM");
1095 return -ENOMEM;
1096}
1097EXPORT_SYMBOL(__fscache_write_page);
1098
1099/*
1100 * remove a page from the cache
1101 */
1102void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
1103{
1104 struct fscache_object *object;
1105
1106 _enter(",%p", page);
1107
1108 ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
1109 ASSERTCMP(page, !=, NULL);
1110
1111 fscache_stat(&fscache_n_uncaches);
1112
1113 /* cache withdrawal may beat us to it */
1114 if (!PageFsCache(page))
1115 goto done;
1116
David Howells08c2e3d2018-04-04 13:41:27 +01001117 trace_fscache_page(cookie, page, fscache_page_uncache);
1118
David Howellsb5108822009-04-03 16:42:39 +01001119 /* get the object */
1120 spin_lock(&cookie->lock);
1121
1122 if (hlist_empty(&cookie->backing_objects)) {
1123 ClearPageFsCache(page);
1124 goto done_unlock;
1125 }
1126
1127 object = hlist_entry(cookie->backing_objects.first,
1128 struct fscache_object, cookie_link);
1129
1130 /* there might now be stuff on disk we could read */
1131 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1132
1133 /* only invoke the cache backend if we managed to mark the page
1134 * uncached here; this deals with synchronisation vs withdrawal */
1135 if (TestClearPageFsCache(page) &&
1136 object->cache->ops->uncache_page) {
1137 /* the cache backend releases the cookie lock */
David Howells52bd75f2009-11-19 18:11:08 +00001138 fscache_stat(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001139 object->cache->ops->uncache_page(object, page);
David Howells52bd75f2009-11-19 18:11:08 +00001140 fscache_stat_d(&fscache_n_cop_uncache_page);
David Howellsb5108822009-04-03 16:42:39 +01001141 goto done;
1142 }
1143
1144done_unlock:
1145 spin_unlock(&cookie->lock);
1146done:
1147 _leave("");
1148}
1149EXPORT_SYMBOL(__fscache_uncache_page);
1150
1151/**
David Howellsc4d6d8d2012-12-20 21:52:32 +00001152 * fscache_mark_page_cached - Mark a page as being cached
1153 * @op: The retrieval op pages are being marked for
1154 * @page: The page to be marked
1155 *
1156 * Mark a netfs page as being cached. After this is called, the netfs
1157 * must call fscache_uncache_page() to remove the mark.
1158 */
1159void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
1160{
1161 struct fscache_cookie *cookie = op->op.object->cookie;
1162
1163#ifdef CONFIG_FSCACHE_STATS
1164 atomic_inc(&fscache_n_marks);
1165#endif
1166
David Howells08c2e3d2018-04-04 13:41:27 +01001167 trace_fscache_page(cookie, page, fscache_page_cached);
1168
David Howellsc4d6d8d2012-12-20 21:52:32 +00001169 _debug("- mark %p{%lx}", page, page->index);
1170 if (TestSetPageFsCache(page)) {
1171 static bool once_only;
1172 if (!once_only) {
1173 once_only = true;
Fabian Frederick36dfd112014-06-04 16:05:38 -07001174 pr_warn("Cookie type %s marked page %lx multiple times\n",
1175 cookie->def->name, page->index);
David Howellsc4d6d8d2012-12-20 21:52:32 +00001176 }
1177 }
1178
1179 if (cookie->def->mark_page_cached)
1180 cookie->def->mark_page_cached(cookie->netfs_data,
1181 op->mapping, page);
1182}
1183EXPORT_SYMBOL(fscache_mark_page_cached);
1184
1185/**
David Howellsb5108822009-04-03 16:42:39 +01001186 * fscache_mark_pages_cached - Mark pages as being cached
1187 * @op: The retrieval op pages are being marked for
1188 * @pagevec: The pages to be marked
1189 *
1190 * Mark a bunch of netfs pages as being cached. After this is called,
1191 * the netfs must call fscache_uncache_page() to remove the mark.
1192 */
1193void fscache_mark_pages_cached(struct fscache_retrieval *op,
1194 struct pagevec *pagevec)
1195{
David Howellsb5108822009-04-03 16:42:39 +01001196 unsigned long loop;
1197
David Howellsc4d6d8d2012-12-20 21:52:32 +00001198 for (loop = 0; loop < pagevec->nr; loop++)
1199 fscache_mark_page_cached(op, pagevec->pages[loop]);
David Howellsb5108822009-04-03 16:42:39 +01001200
David Howellsb5108822009-04-03 16:42:39 +01001201 pagevec_reinit(pagevec);
1202}
1203EXPORT_SYMBOL(fscache_mark_pages_cached);
David Howellsc902ce12011-07-07 12:19:48 +01001204
1205/*
1206 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
1207 * to be associated with the given cookie.
1208 */
1209void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
1210 struct inode *inode)
1211{
1212 struct address_space *mapping = inode->i_mapping;
1213 struct pagevec pvec;
1214 pgoff_t next;
1215 int i;
1216
1217 _enter("%p,%p", cookie, inode);
1218
1219 if (!mapping || mapping->nrpages == 0) {
1220 _leave(" [no pages]");
1221 return;
1222 }
1223
Mel Gorman86679822017-11-15 17:37:52 -08001224 pagevec_init(&pvec);
David Howellsc902ce12011-07-07 12:19:48 +01001225 next = 0;
Jan Beulichb307d462011-07-21 15:02:43 +01001226 do {
Jan Kara397162f2017-09-06 16:21:43 -07001227 if (!pagevec_lookup(&pvec, mapping, &next))
Jan Beulichb307d462011-07-21 15:02:43 +01001228 break;
David Howellsc902ce12011-07-07 12:19:48 +01001229 for (i = 0; i < pagevec_count(&pvec); i++) {
1230 struct page *page = pvec.pages[i];
David Howellsc902ce12011-07-07 12:19:48 +01001231 if (PageFsCache(page)) {
1232 __fscache_wait_on_page_write(cookie, page);
1233 __fscache_uncache_page(cookie, page);
1234 }
1235 }
1236 pagevec_release(&pvec);
1237 cond_resched();
Jan Karad72dc8a2017-09-06 16:21:18 -07001238 } while (next);
David Howellsc902ce12011-07-07 12:19:48 +01001239
1240 _leave("");
1241}
1242EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);