blob: 8bda092e60c5a00118ec8d5159f8a30bdcd38a04 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells9ae326a2009-04-03 16:42:41 +01002/* Storage object read/write
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells9ae326a2009-04-03 16:42:41 +01006 */
7
8#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
David Howells9ae326a2009-04-03 16:42:41 +010010#include <linux/file.h>
Mel Gormana0b8cab32013-07-03 15:02:32 -070011#include <linux/swap.h>
David Howells9ae326a2009-04-03 16:42:41 +010012#include "internal.h"
13
14/*
15 * detect wake up events generated by the unlocking of pages in which we're
16 * interested
17 * - we use this to detect read completion of backing pages
18 * - the caller holds the waitqueue lock
19 */
Ingo Molnarac6424b2017-06-20 12:06:13 +020020static int cachefiles_read_waiter(wait_queue_entry_t *wait, unsigned mode,
David Howells9ae326a2009-04-03 16:42:41 +010021 int sync, void *_key)
22{
23 struct cachefiles_one_read *monitor =
24 container_of(wait, struct cachefiles_one_read, monitor);
25 struct cachefiles_object *object;
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070026 struct fscache_retrieval *op = monitor->op;
David Howells9ae326a2009-04-03 16:42:41 +010027 struct wait_bit_key *key = _key;
28 struct page *page = wait->private;
29
30 ASSERT(key);
31
32 _enter("{%lu},%u,%d,{%p,%u}",
33 monitor->netfs_page->index, mode, sync,
34 key->flags, key->bit_nr);
35
36 if (key->flags != &page->flags ||
37 key->bit_nr != PG_locked)
38 return 0;
39
40 _debug("--- monitor %p %lx ---", page, page->flags);
41
David Howells5e929b32009-11-19 18:11:55 +000042 if (!PageUptodate(page) && !PageError(page)) {
43 /* unlocked, not uptodate and not erronous? */
44 _debug("page probably truncated");
45 }
David Howells9ae326a2009-04-03 16:42:41 +010046
47 /* remove from the waitqueue */
Ingo Molnar2055da92017-06-20 12:06:46 +020048 list_del(&wait->entry);
David Howells9ae326a2009-04-03 16:42:41 +010049
50 /* move onto the action list and queue for FS-Cache thread pool */
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070051 ASSERT(op);
David Howells9ae326a2009-04-03 16:42:41 +010052
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070053 /* We need to temporarily bump the usage count as we don't own a ref
54 * here otherwise cachefiles_read_copier() may free the op between the
55 * monitor being enqueued on the op->to_do list and the op getting
56 * enqueued on the work queue.
57 */
58 fscache_get_retrieval(op);
David Howells9ae326a2009-04-03 16:42:41 +010059
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070060 object = container_of(op->op.object, struct cachefiles_object, fscache);
David Howells9ae326a2009-04-03 16:42:41 +010061 spin_lock(&object->work_lock);
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070062 list_add_tail(&monitor->op_link, &op->to_do);
Lei Xue7bb0c532020-05-07 08:50:22 -040063 fscache_enqueue_retrieval(op);
David Howells9ae326a2009-04-03 16:42:41 +010064 spin_unlock(&object->work_lock);
65
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070066 fscache_put_retrieval(op);
David Howells9ae326a2009-04-03 16:42:41 +010067 return 0;
68}
69
70/*
David Howells5e929b32009-11-19 18:11:55 +000071 * handle a probably truncated page
72 * - check to see if the page is still relevant and reissue the read if
73 * possible
74 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
75 * must wait again and 0 if successful
76 */
77static int cachefiles_read_reissue(struct cachefiles_object *object,
78 struct cachefiles_one_read *monitor)
79{
David Howells466b77b2015-03-17 22:26:21 +000080 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
David Howells5e929b32009-11-19 18:11:55 +000081 struct page *backpage = monitor->back_page, *backpage2;
82 int ret;
83
David Howells37491a12012-12-20 21:52:34 +000084 _enter("{ino=%lx},{%lx,%lx}",
David Howells466b77b2015-03-17 22:26:21 +000085 d_backing_inode(object->backer)->i_ino,
David Howells5e929b32009-11-19 18:11:55 +000086 backpage->index, backpage->flags);
87
88 /* skip if the page was truncated away completely */
89 if (backpage->mapping != bmapping) {
David Howells37491a12012-12-20 21:52:34 +000090 _leave(" = -ENODATA [mapping]");
David Howells5e929b32009-11-19 18:11:55 +000091 return -ENODATA;
92 }
93
94 backpage2 = find_get_page(bmapping, backpage->index);
95 if (!backpage2) {
David Howells37491a12012-12-20 21:52:34 +000096 _leave(" = -ENODATA [gone]");
David Howells5e929b32009-11-19 18:11:55 +000097 return -ENODATA;
98 }
99
100 if (backpage != backpage2) {
101 put_page(backpage2);
David Howells37491a12012-12-20 21:52:34 +0000102 _leave(" = -ENODATA [different]");
David Howells5e929b32009-11-19 18:11:55 +0000103 return -ENODATA;
104 }
105
106 /* the page is still there and we already have a ref on it, so we don't
107 * need a second */
108 put_page(backpage2);
109
110 INIT_LIST_HEAD(&monitor->op_link);
111 add_page_wait_queue(backpage, &monitor->monitor);
112
113 if (trylock_page(backpage)) {
114 ret = -EIO;
115 if (PageError(backpage))
116 goto unlock_discard;
117 ret = 0;
118 if (PageUptodate(backpage))
119 goto unlock_discard;
120
David Howells37491a12012-12-20 21:52:34 +0000121 _debug("reissue read");
David Howells5e929b32009-11-19 18:11:55 +0000122 ret = bmapping->a_ops->readpage(NULL, backpage);
123 if (ret < 0)
Matthew Wilcox (Oracle)9480b4e2020-10-26 09:12:10 +0000124 goto discard;
David Howells5e929b32009-11-19 18:11:55 +0000125 }
126
127 /* but the page may have been read before the monitor was installed, so
128 * the monitor may miss the event - so we have to ensure that we do get
129 * one in such a case */
130 if (trylock_page(backpage)) {
131 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
132 unlock_page(backpage);
133 }
134
135 /* it'll reappear on the todo list */
David Howells37491a12012-12-20 21:52:34 +0000136 _leave(" = -EINPROGRESS");
David Howells5e929b32009-11-19 18:11:55 +0000137 return -EINPROGRESS;
138
139unlock_discard:
140 unlock_page(backpage);
Matthew Wilcox (Oracle)9480b4e2020-10-26 09:12:10 +0000141discard:
David Howells5e929b32009-11-19 18:11:55 +0000142 spin_lock_irq(&object->work_lock);
143 list_del(&monitor->op_link);
144 spin_unlock_irq(&object->work_lock);
David Howells37491a12012-12-20 21:52:34 +0000145 _leave(" = %d", ret);
David Howells5e929b32009-11-19 18:11:55 +0000146 return ret;
147}
148
149/*
David Howells9ae326a2009-04-03 16:42:41 +0100150 * copy data from backing pages to netfs pages to complete a read operation
151 * - driven by FS-Cache's thread pool
152 */
153static void cachefiles_read_copier(struct fscache_operation *_op)
154{
155 struct cachefiles_one_read *monitor;
156 struct cachefiles_object *object;
157 struct fscache_retrieval *op;
David Howells9ae326a2009-04-03 16:42:41 +0100158 int error, max;
159
160 op = container_of(_op, struct fscache_retrieval, op);
161 object = container_of(op->op.object,
162 struct cachefiles_object, fscache);
163
David Howells466b77b2015-03-17 22:26:21 +0000164 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100165
David Howells9ae326a2009-04-03 16:42:41 +0100166 max = 8;
167 spin_lock_irq(&object->work_lock);
168
169 while (!list_empty(&op->to_do)) {
170 monitor = list_entry(op->to_do.next,
171 struct cachefiles_one_read, op_link);
172 list_del(&monitor->op_link);
173
174 spin_unlock_irq(&object->work_lock);
175
176 _debug("- copy {%lu}", monitor->back_page->index);
177
David Howells5e929b32009-11-19 18:11:55 +0000178 recheck:
David Howells9dc8d9b2012-12-20 21:52:36 +0000179 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
180 &object->fscache.cookie->flags)) {
181 error = -ESTALE;
182 } else if (PageUptodate(monitor->back_page)) {
David Howells9ae326a2009-04-03 16:42:41 +0100183 copy_highpage(monitor->netfs_page, monitor->back_page);
David Howellsc4d6d8d2012-12-20 21:52:32 +0000184 fscache_mark_page_cached(monitor->op,
185 monitor->netfs_page);
David Howells9ae326a2009-04-03 16:42:41 +0100186 error = 0;
David Howells5e929b32009-11-19 18:11:55 +0000187 } else if (!PageError(monitor->back_page)) {
188 /* the page has probably been truncated */
189 error = cachefiles_read_reissue(object, monitor);
190 if (error == -EINPROGRESS)
191 goto next;
192 goto recheck;
193 } else {
David Howells9ae326a2009-04-03 16:42:41 +0100194 cachefiles_io_error_obj(
195 object,
196 "Readpage failed on backing file %lx",
197 (unsigned long) monitor->back_page->flags);
David Howells5e929b32009-11-19 18:11:55 +0000198 error = -EIO;
199 }
David Howells9ae326a2009-04-03 16:42:41 +0100200
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300201 put_page(monitor->back_page);
David Howells9ae326a2009-04-03 16:42:41 +0100202
203 fscache_end_io(op, monitor->netfs_page, error);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(monitor->netfs_page);
David Howells9f105232012-12-20 21:52:35 +0000205 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100206 fscache_put_retrieval(op);
207 kfree(monitor);
208
David Howells5e929b32009-11-19 18:11:55 +0000209 next:
David Howells9ae326a2009-04-03 16:42:41 +0100210 /* let the thread pool have some air occasionally */
211 max--;
212 if (max < 0 || need_resched()) {
213 if (!list_empty(&op->to_do))
214 fscache_enqueue_retrieval(op);
215 _leave(" [maxed out]");
216 return;
217 }
218
219 spin_lock_irq(&object->work_lock);
220 }
221
222 spin_unlock_irq(&object->work_lock);
223 _leave("");
224}
225
226/*
227 * read the corresponding page to the given set from the backing file
228 * - an uncertain page is simply discarded, to be tried again another time
229 */
230static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
231 struct fscache_retrieval *op,
Mel Gormana0b8cab32013-07-03 15:02:32 -0700232 struct page *netpage)
David Howells9ae326a2009-04-03 16:42:41 +0100233{
234 struct cachefiles_one_read *monitor;
235 struct address_space *bmapping;
236 struct page *newpage, *backpage;
237 int ret;
238
239 _enter("");
240
David Howells9ae326a2009-04-03 16:42:41 +0100241 _debug("read back %p{%lu,%d}",
242 netpage, netpage->index, page_count(netpage));
243
David Howells5f4f9f42012-12-20 21:52:33 +0000244 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100245 if (!monitor)
246 goto nomem;
247
248 monitor->netfs_page = netpage;
249 monitor->op = fscache_get_retrieval(op);
250
251 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
252
253 /* attempt to get hold of the backing page */
David Howells466b77b2015-03-17 22:26:21 +0000254 bmapping = d_backing_inode(object->backer)->i_mapping;
David Howells9ae326a2009-04-03 16:42:41 +0100255 newpage = NULL;
256
257 for (;;) {
258 backpage = find_get_page(bmapping, netpage->index);
259 if (backpage)
260 goto backing_page_already_present;
261
262 if (!newpage) {
Mel Gorman453f85d2017-11-15 17:38:03 -0800263 newpage = __page_cache_alloc(cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100264 if (!newpage)
265 goto nomem_monitor;
266 }
267
Johannes Weiner55881bc2014-04-03 14:47:36 -0700268 ret = add_to_page_cache_lru(newpage, bmapping,
269 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100270 if (ret == 0)
271 goto installed_new_backing_page;
272 if (ret != -EEXIST)
273 goto nomem_page;
274 }
275
Johannes Weiner55881bc2014-04-03 14:47:36 -0700276 /* we've installed a new backing page, so now we need to start
277 * it reading */
David Howells9ae326a2009-04-03 16:42:41 +0100278installed_new_backing_page:
279 _debug("- new %p", newpage);
280
281 backpage = newpage;
282 newpage = NULL;
283
David Howells9ae326a2009-04-03 16:42:41 +0100284read_backing_page:
285 ret = bmapping->a_ops->readpage(NULL, backpage);
286 if (ret < 0)
287 goto read_error;
288
289 /* set the monitor to transfer the data across */
290monitor_backing_page:
291 _debug("- monitor add");
292
293 /* install the monitor */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 get_page(monitor->netfs_page);
295 get_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100296 monitor->back_page = backpage;
297 monitor->monitor.private = backpage;
298 add_page_wait_queue(backpage, &monitor->monitor);
299 monitor = NULL;
300
301 /* but the page may have been read before the monitor was installed, so
302 * the monitor may miss the event - so we have to ensure that we do get
303 * one in such a case */
304 if (trylock_page(backpage)) {
305 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
306 unlock_page(backpage);
307 }
308 goto success;
309
310 /* if the backing page is already present, it can be in one of
311 * three states: read in progress, read failed or read okay */
312backing_page_already_present:
313 _debug("- present");
314
315 if (newpage) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300316 put_page(newpage);
David Howells9ae326a2009-04-03 16:42:41 +0100317 newpage = NULL;
318 }
319
320 if (PageError(backpage))
321 goto io_error;
322
323 if (PageUptodate(backpage))
324 goto backing_page_already_uptodate;
325
326 if (!trylock_page(backpage))
327 goto monitor_backing_page;
328 _debug("read %p {%lx}", backpage, backpage->flags);
329 goto read_backing_page;
330
331 /* the backing page is already up to date, attach the netfs
332 * page to the pagecache and LRU and copy the data across */
333backing_page_already_uptodate:
334 _debug("- uptodate");
335
David Howellsc4d6d8d2012-12-20 21:52:32 +0000336 fscache_mark_page_cached(op, netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100337
338 copy_highpage(netpage, backpage);
339 fscache_end_io(op, netpage, 0);
David Howells9f105232012-12-20 21:52:35 +0000340 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100341
342success:
343 _debug("success");
344 ret = 0;
345
346out:
347 if (backpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300348 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100349 if (monitor) {
350 fscache_put_retrieval(monitor->op);
351 kfree(monitor);
352 }
353 _leave(" = %d", ret);
354 return ret;
355
356read_error:
357 _debug("read error %d", ret);
David Howellsb4cf1e02012-12-05 13:34:45 +0000358 if (ret == -ENOMEM) {
359 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100360 goto out;
David Howellsb4cf1e02012-12-05 13:34:45 +0000361 }
David Howells9ae326a2009-04-03 16:42:41 +0100362io_error:
363 cachefiles_io_error_obj(object, "Page read error on backing file");
David Howells9f105232012-12-20 21:52:35 +0000364 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100365 ret = -ENOBUFS;
366 goto out;
367
368nomem_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300369 put_page(newpage);
David Howells9ae326a2009-04-03 16:42:41 +0100370nomem_monitor:
371 fscache_put_retrieval(monitor->op);
372 kfree(monitor);
373nomem:
David Howells9f105232012-12-20 21:52:35 +0000374 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100375 _leave(" = -ENOMEM");
376 return -ENOMEM;
377}
378
379/*
380 * read a page from the cache or allocate a block in which to store it
381 * - cache withdrawal is prevented by the caller
382 * - returns -EINTR if interrupted
383 * - returns -ENOMEM if ran out of memory
384 * - returns -ENOBUFS if no buffers can be made available
385 * - returns -ENOBUFS if page is beyond EOF
386 * - if the page is backed by a block in the cache:
387 * - a read will be started which will call the callback on completion
388 * - 0 will be returned
389 * - else if the page is unbacked:
390 * - the metadata will be retained
391 * - -ENODATA will be returned
392 */
393int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
394 struct page *page,
395 gfp_t gfp)
396{
397 struct cachefiles_object *object;
398 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100399 struct inode *inode;
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100400 sector_t block;
David Howells9ae326a2009-04-03 16:42:41 +0100401 unsigned shift;
David Howellsc5f9d9d2020-05-04 16:12:55 +0100402 int ret, ret2;
David Howells9ae326a2009-04-03 16:42:41 +0100403
404 object = container_of(op->op.object,
405 struct cachefiles_object, fscache);
406 cache = container_of(object->fscache.cache,
407 struct cachefiles_cache, cache);
408
409 _enter("{%p},{%lx},,,", object, page->index);
410
411 if (!object->backer)
David Howells9f105232012-12-20 21:52:35 +0000412 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100413
David Howells466b77b2015-03-17 22:26:21 +0000414 inode = d_backing_inode(object->backer);
David Howells9ae326a2009-04-03 16:42:41 +0100415 ASSERT(S_ISREG(inode->i_mode));
David Howells9ae326a2009-04-03 16:42:41 +0100416 ASSERT(inode->i_mapping->a_ops->readpages);
417
418 /* calculate the shift required to use bmap */
David Howells9ae326a2009-04-03 16:42:41 +0100419 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
420
David Howells4fbf4292009-11-19 18:11:04 +0000421 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
Tejun Heo8af7c122010-07-20 22:09:01 +0200422 op->op.flags |= FSCACHE_OP_ASYNC;
David Howells9ae326a2009-04-03 16:42:41 +0100423 op->op.processor = cachefiles_read_copier;
424
David Howells9ae326a2009-04-03 16:42:41 +0100425 /* we assume the absence or presence of the first block is a good
426 * enough indication for the page as a whole
427 * - TODO: don't use bmap() for this as it is _not_ actually good
428 * enough for this as it doesn't indicate errors, but it's all we've
429 * got for the moment
430 */
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100431 block = page->index;
432 block <<= shift;
David Howells9ae326a2009-04-03 16:42:41 +0100433
David Howellsc5f9d9d2020-05-04 16:12:55 +0100434 ret2 = bmap(inode, &block);
435 ASSERT(ret2 == 0);
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100436
David Howells9ae326a2009-04-03 16:42:41 +0100437 _debug("%llx -> %llx",
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100438 (unsigned long long) (page->index << shift),
David Howells9ae326a2009-04-03 16:42:41 +0100439 (unsigned long long) block);
440
441 if (block) {
442 /* submit the apparently valid page to the backing fs to be
443 * read from disk */
Mel Gormana0b8cab32013-07-03 15:02:32 -0700444 ret = cachefiles_read_backing_file_one(object, op, page);
David Howells9ae326a2009-04-03 16:42:41 +0100445 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
446 /* there's space in the cache we can use */
David Howellsc4d6d8d2012-12-20 21:52:32 +0000447 fscache_mark_page_cached(op, page);
David Howells9f105232012-12-20 21:52:35 +0000448 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100449 ret = -ENODATA;
450 } else {
David Howells9f105232012-12-20 21:52:35 +0000451 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100452 }
453
454 _leave(" = %d", ret);
455 return ret;
David Howells9f105232012-12-20 21:52:35 +0000456
457enobufs:
458 fscache_retrieval_complete(op, 1);
459 _leave(" = -ENOBUFS");
460 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100461}
462
463/*
464 * read the corresponding pages to the given set from the backing file
465 * - any uncertain pages are simply discarded, to be tried again another time
466 */
467static int cachefiles_read_backing_file(struct cachefiles_object *object,
468 struct fscache_retrieval *op,
David Howellsc4d6d8d2012-12-20 21:52:32 +0000469 struct list_head *list)
David Howells9ae326a2009-04-03 16:42:41 +0100470{
471 struct cachefiles_one_read *monitor = NULL;
David Howells466b77b2015-03-17 22:26:21 +0000472 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
David Howells9ae326a2009-04-03 16:42:41 +0100473 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
474 int ret = 0;
475
476 _enter("");
477
David Howells9ae326a2009-04-03 16:42:41 +0100478 list_for_each_entry_safe(netpage, _n, list, lru) {
479 list_del(&netpage->lru);
480
481 _debug("read back %p{%lu,%d}",
482 netpage, netpage->index, page_count(netpage));
483
484 if (!monitor) {
David Howells5f4f9f42012-12-20 21:52:33 +0000485 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100486 if (!monitor)
487 goto nomem;
488
489 monitor->op = fscache_get_retrieval(op);
490 init_waitqueue_func_entry(&monitor->monitor,
491 cachefiles_read_waiter);
492 }
493
494 for (;;) {
495 backpage = find_get_page(bmapping, netpage->index);
496 if (backpage)
497 goto backing_page_already_present;
498
499 if (!newpage) {
Mel Gorman453f85d2017-11-15 17:38:03 -0800500 newpage = __page_cache_alloc(cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100501 if (!newpage)
502 goto nomem;
503 }
504
Johannes Weiner55881bc2014-04-03 14:47:36 -0700505 ret = add_to_page_cache_lru(newpage, bmapping,
506 netpage->index,
507 cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100508 if (ret == 0)
509 goto installed_new_backing_page;
510 if (ret != -EEXIST)
511 goto nomem;
512 }
513
Johannes Weiner55881bc2014-04-03 14:47:36 -0700514 /* we've installed a new backing page, so now we need
515 * to start it reading */
David Howells9ae326a2009-04-03 16:42:41 +0100516 installed_new_backing_page:
517 _debug("- new %p", newpage);
518
519 backpage = newpage;
520 newpage = NULL;
521
David Howells9ae326a2009-04-03 16:42:41 +0100522 reread_backing_page:
523 ret = bmapping->a_ops->readpage(NULL, backpage);
524 if (ret < 0)
525 goto read_error;
526
527 /* add the netfs page to the pagecache and LRU, and set the
528 * monitor to transfer the data across */
529 monitor_backing_page:
530 _debug("- monitor add");
531
Johannes Weiner55881bc2014-04-03 14:47:36 -0700532 ret = add_to_page_cache_lru(netpage, op->mapping,
533 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100534 if (ret < 0) {
535 if (ret == -EEXIST) {
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000536 put_page(backpage);
537 backpage = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300538 put_page(netpage);
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000539 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000540 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100541 continue;
542 }
543 goto nomem;
544 }
545
David Howells9ae326a2009-04-03 16:42:41 +0100546 /* install a monitor */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300547 get_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100548 monitor->netfs_page = netpage;
549
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300550 get_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100551 monitor->back_page = backpage;
552 monitor->monitor.private = backpage;
553 add_page_wait_queue(backpage, &monitor->monitor);
554 monitor = NULL;
555
556 /* but the page may have been read before the monitor was
557 * installed, so the monitor may miss the event - so we have to
558 * ensure that we do get one in such a case */
559 if (trylock_page(backpage)) {
560 _debug("2unlock %p {%lx}", backpage, backpage->flags);
561 unlock_page(backpage);
562 }
563
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100565 backpage = NULL;
566
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300567 put_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100568 netpage = NULL;
569 continue;
570
571 /* if the backing page is already present, it can be in one of
572 * three states: read in progress, read failed or read okay */
573 backing_page_already_present:
574 _debug("- present %p", backpage);
575
576 if (PageError(backpage))
577 goto io_error;
578
579 if (PageUptodate(backpage))
580 goto backing_page_already_uptodate;
581
582 _debug("- not ready %p{%lx}", backpage, backpage->flags);
583
584 if (!trylock_page(backpage))
585 goto monitor_backing_page;
586
587 if (PageError(backpage)) {
588 _debug("error %lx", backpage->flags);
589 unlock_page(backpage);
590 goto io_error;
591 }
592
593 if (PageUptodate(backpage))
594 goto backing_page_already_uptodate_unlock;
595
596 /* we've locked a page that's neither up to date nor erroneous,
597 * so we need to attempt to read it again */
598 goto reread_backing_page;
599
600 /* the backing page is already up to date, attach the netfs
601 * page to the pagecache and LRU and copy the data across */
602 backing_page_already_uptodate_unlock:
603 _debug("uptodate %lx", backpage->flags);
604 unlock_page(backpage);
605 backing_page_already_uptodate:
606 _debug("- uptodate");
607
Johannes Weiner55881bc2014-04-03 14:47:36 -0700608 ret = add_to_page_cache_lru(netpage, op->mapping,
609 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100610 if (ret < 0) {
611 if (ret == -EEXIST) {
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000612 put_page(backpage);
613 backpage = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300614 put_page(netpage);
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000615 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000616 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100617 continue;
618 }
619 goto nomem;
620 }
621
622 copy_highpage(netpage, backpage);
623
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300624 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100625 backpage = NULL;
626
David Howellsc4d6d8d2012-12-20 21:52:32 +0000627 fscache_mark_page_cached(op, netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100628
David Howellsc4d6d8d2012-12-20 21:52:32 +0000629 /* the netpage is unlocked and marked up to date here */
David Howells9ae326a2009-04-03 16:42:41 +0100630 fscache_end_io(op, netpage, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300631 put_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100632 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000633 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100634 continue;
635 }
636
637 netpage = NULL;
638
639 _debug("out");
640
641out:
642 /* tidy up */
David Howells9ae326a2009-04-03 16:42:41 +0100643 if (newpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300644 put_page(newpage);
David Howells9ae326a2009-04-03 16:42:41 +0100645 if (netpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300646 put_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100647 if (backpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300648 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100649 if (monitor) {
650 fscache_put_retrieval(op);
651 kfree(monitor);
652 }
653
654 list_for_each_entry_safe(netpage, _n, list, lru) {
655 list_del(&netpage->lru);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300656 put_page(netpage);
David Howells9f105232012-12-20 21:52:35 +0000657 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100658 }
659
660 _leave(" = %d", ret);
661 return ret;
662
663nomem:
664 _debug("nomem");
665 ret = -ENOMEM;
David Howellsb4cf1e02012-12-05 13:34:45 +0000666 goto record_page_complete;
David Howells9ae326a2009-04-03 16:42:41 +0100667
668read_error:
669 _debug("read error %d", ret);
670 if (ret == -ENOMEM)
David Howellsb4cf1e02012-12-05 13:34:45 +0000671 goto record_page_complete;
David Howells9ae326a2009-04-03 16:42:41 +0100672io_error:
673 cachefiles_io_error_obj(object, "Page read error on backing file");
674 ret = -ENOBUFS;
David Howellsb4cf1e02012-12-05 13:34:45 +0000675record_page_complete:
676 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100677 goto out;
678}
679
680/*
681 * read a list of pages from the cache or allocate blocks in which to store
682 * them
683 */
684int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
685 struct list_head *pages,
686 unsigned *nr_pages,
687 gfp_t gfp)
688{
689 struct cachefiles_object *object;
690 struct cachefiles_cache *cache;
691 struct list_head backpages;
692 struct pagevec pagevec;
693 struct inode *inode;
694 struct page *page, *_n;
695 unsigned shift, nrbackpages;
696 int ret, ret2, space;
697
698 object = container_of(op->op.object,
699 struct cachefiles_object, fscache);
700 cache = container_of(object->fscache.cache,
701 struct cachefiles_cache, cache);
702
703 _enter("{OBJ%x,%d},,%d,,",
704 object->fscache.debug_id, atomic_read(&op->op.usage),
705 *nr_pages);
706
707 if (!object->backer)
David Howells9f105232012-12-20 21:52:35 +0000708 goto all_enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100709
710 space = 1;
711 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
712 space = 0;
713
David Howells466b77b2015-03-17 22:26:21 +0000714 inode = d_backing_inode(object->backer);
David Howells9ae326a2009-04-03 16:42:41 +0100715 ASSERT(S_ISREG(inode->i_mode));
David Howells9ae326a2009-04-03 16:42:41 +0100716 ASSERT(inode->i_mapping->a_ops->readpages);
717
718 /* calculate the shift required to use bmap */
David Howells9ae326a2009-04-03 16:42:41 +0100719 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
720
Mel Gorman86679822017-11-15 17:37:52 -0800721 pagevec_init(&pagevec);
David Howells9ae326a2009-04-03 16:42:41 +0100722
David Howells4fbf4292009-11-19 18:11:04 +0000723 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
Tejun Heo8af7c122010-07-20 22:09:01 +0200724 op->op.flags |= FSCACHE_OP_ASYNC;
David Howells9ae326a2009-04-03 16:42:41 +0100725 op->op.processor = cachefiles_read_copier;
726
727 INIT_LIST_HEAD(&backpages);
728 nrbackpages = 0;
729
730 ret = space ? -ENODATA : -ENOBUFS;
731 list_for_each_entry_safe(page, _n, pages, lru) {
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100732 sector_t block;
David Howells9ae326a2009-04-03 16:42:41 +0100733
734 /* we assume the absence or presence of the first block is a
735 * good enough indication for the page as a whole
736 * - TODO: don't use bmap() for this as it is _not_ actually
737 * good enough for this as it doesn't indicate errors, but
738 * it's all we've got for the moment
739 */
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100740 block = page->index;
741 block <<= shift;
David Howells9ae326a2009-04-03 16:42:41 +0100742
David Howellsc5f9d9d2020-05-04 16:12:55 +0100743 ret2 = bmap(inode, &block);
744 ASSERT(ret2 == 0);
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100745
David Howells9ae326a2009-04-03 16:42:41 +0100746 _debug("%llx -> %llx",
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100747 (unsigned long long) (page->index << shift),
David Howells9ae326a2009-04-03 16:42:41 +0100748 (unsigned long long) block);
749
750 if (block) {
751 /* we have data - add it to the list to give to the
752 * backing fs */
753 list_move(&page->lru, &backpages);
754 (*nr_pages)--;
755 nrbackpages++;
756 } else if (space && pagevec_add(&pagevec, page) == 0) {
757 fscache_mark_pages_cached(op, &pagevec);
David Howells9f105232012-12-20 21:52:35 +0000758 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100759 ret = -ENODATA;
David Howells9f105232012-12-20 21:52:35 +0000760 } else {
761 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100762 }
763 }
764
765 if (pagevec_count(&pagevec) > 0)
766 fscache_mark_pages_cached(op, &pagevec);
767
768 if (list_empty(pages))
769 ret = 0;
770
771 /* submit the apparently valid pages to the backing fs to be read from
772 * disk */
773 if (nrbackpages > 0) {
David Howellsc4d6d8d2012-12-20 21:52:32 +0000774 ret2 = cachefiles_read_backing_file(object, op, &backpages);
David Howells9ae326a2009-04-03 16:42:41 +0100775 if (ret2 == -ENOMEM || ret2 == -EINTR)
776 ret = ret2;
777 }
778
David Howells9ae326a2009-04-03 16:42:41 +0100779 _leave(" = %d [nr=%u%s]",
780 ret, *nr_pages, list_empty(pages) ? " empty" : "");
781 return ret;
David Howells9f105232012-12-20 21:52:35 +0000782
783all_enobufs:
784 fscache_retrieval_complete(op, *nr_pages);
785 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100786}
787
788/*
789 * allocate a block in the cache in which to store a page
790 * - cache withdrawal is prevented by the caller
791 * - returns -EINTR if interrupted
792 * - returns -ENOMEM if ran out of memory
793 * - returns -ENOBUFS if no buffers can be made available
794 * - returns -ENOBUFS if page is beyond EOF
795 * - otherwise:
796 * - the metadata will be retained
797 * - 0 will be returned
798 */
799int cachefiles_allocate_page(struct fscache_retrieval *op,
800 struct page *page,
801 gfp_t gfp)
802{
803 struct cachefiles_object *object;
804 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100805 int ret;
806
807 object = container_of(op->op.object,
808 struct cachefiles_object, fscache);
809 cache = container_of(object->fscache.cache,
810 struct cachefiles_cache, cache);
811
812 _enter("%p,{%lx},", object, page->index);
813
814 ret = cachefiles_has_space(cache, 0, 1);
David Howellsc4d6d8d2012-12-20 21:52:32 +0000815 if (ret == 0)
816 fscache_mark_page_cached(op, page);
817 else
David Howells9ae326a2009-04-03 16:42:41 +0100818 ret = -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100819
David Howells9f105232012-12-20 21:52:35 +0000820 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100821 _leave(" = %d", ret);
822 return ret;
823}
824
825/*
826 * allocate blocks in the cache in which to store a set of pages
827 * - cache withdrawal is prevented by the caller
828 * - returns -EINTR if interrupted
829 * - returns -ENOMEM if ran out of memory
830 * - returns -ENOBUFS if some buffers couldn't be made available
831 * - returns -ENOBUFS if some pages are beyond EOF
832 * - otherwise:
833 * - -ENODATA will be returned
834 * - metadata will be retained for any page marked
835 */
836int cachefiles_allocate_pages(struct fscache_retrieval *op,
837 struct list_head *pages,
838 unsigned *nr_pages,
839 gfp_t gfp)
840{
841 struct cachefiles_object *object;
842 struct cachefiles_cache *cache;
843 struct pagevec pagevec;
844 struct page *page;
845 int ret;
846
847 object = container_of(op->op.object,
848 struct cachefiles_object, fscache);
849 cache = container_of(object->fscache.cache,
850 struct cachefiles_cache, cache);
851
852 _enter("%p,,,%d,", object, *nr_pages);
853
854 ret = cachefiles_has_space(cache, 0, *nr_pages);
855 if (ret == 0) {
Mel Gorman86679822017-11-15 17:37:52 -0800856 pagevec_init(&pagevec);
David Howells9ae326a2009-04-03 16:42:41 +0100857
858 list_for_each_entry(page, pages, lru) {
859 if (pagevec_add(&pagevec, page) == 0)
860 fscache_mark_pages_cached(op, &pagevec);
861 }
862
863 if (pagevec_count(&pagevec) > 0)
864 fscache_mark_pages_cached(op, &pagevec);
865 ret = -ENODATA;
866 } else {
867 ret = -ENOBUFS;
868 }
869
David Howells9f105232012-12-20 21:52:35 +0000870 fscache_retrieval_complete(op, *nr_pages);
David Howells9ae326a2009-04-03 16:42:41 +0100871 _leave(" = %d", ret);
872 return ret;
873}
874
875/*
876 * request a page be stored in the cache
877 * - cache withdrawal is prevented by the caller
878 * - this request may be ignored if there's no cache block available, in which
879 * case -ENOBUFS will be returned
880 * - if the op is in progress, 0 will be returned
881 */
882int cachefiles_write_page(struct fscache_storage *op, struct page *page)
883{
884 struct cachefiles_object *object;
885 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100886 struct file *file;
Al Viro765927b2012-06-26 21:58:53 +0400887 struct path path;
David Howellsa17754f2009-11-19 18:11:52 +0000888 loff_t pos, eof;
889 size_t len;
David Howells9ae326a2009-04-03 16:42:41 +0100890 void *data;
Geert Uytterhoevencf897522015-11-12 11:46:23 +0000891 int ret = -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100892
893 ASSERT(op != NULL);
894 ASSERT(page != NULL);
895
896 object = container_of(op->op.object,
897 struct cachefiles_object, fscache);
898
899 _enter("%p,%p{%lx},,,", object, page, page->index);
900
901 if (!object->backer) {
902 _leave(" = -ENOBUFS");
903 return -ENOBUFS;
904 }
905
David Howellsce40fa72015-01-29 12:02:36 +0000906 ASSERT(d_is_reg(object->backer));
David Howells9ae326a2009-04-03 16:42:41 +0100907
908 cache = container_of(object->fscache.cache,
909 struct cachefiles_cache, cache);
910
David Howells102f4d92015-11-04 15:20:42 +0000911 pos = (loff_t)page->index << PAGE_SHIFT;
912
913 /* We mustn't write more data than we have, so we have to beware of a
914 * partial page at EOF.
915 */
916 eof = object->fscache.store_limit_l;
917 if (pos >= eof)
918 goto error;
919
David Howells9ae326a2009-04-03 16:42:41 +0100920 /* write the page to the backing filesystem and let it store it in its
921 * own time */
Al Viro765927b2012-06-26 21:58:53 +0400922 path.mnt = cache->mnt;
923 path.dentry = object->backer;
Justin Lecher98c350c2012-07-30 14:42:53 -0700924 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
David Howells9ae326a2009-04-03 16:42:41 +0100925 if (IS_ERR(file)) {
926 ret = PTR_ERR(file);
David Howells102f4d92015-11-04 15:20:42 +0000927 goto error_2;
928 }
David Howellsa17754f2009-11-19 18:11:52 +0000929
David Howells102f4d92015-11-04 15:20:42 +0000930 len = PAGE_SIZE;
931 if (eof & ~PAGE_MASK) {
932 if (eof - pos < PAGE_SIZE) {
933 _debug("cut short %llx to %llx",
934 pos, eof);
935 len = eof - pos;
936 ASSERTCMP(pos + len, ==, eof);
David Howells9ae326a2009-04-03 16:42:41 +0100937 }
David Howells9ae326a2009-04-03 16:42:41 +0100938 }
939
David Howells102f4d92015-11-04 15:20:42 +0000940 data = kmap(page);
Christoph Hellwig97c79902020-05-13 08:42:36 +0200941 ret = kernel_write(file, data, len, &pos);
David Howells102f4d92015-11-04 15:20:42 +0000942 kunmap(page);
943 fput(file);
944 if (ret != len)
945 goto error_eio;
David Howells9ae326a2009-04-03 16:42:41 +0100946
David Howells102f4d92015-11-04 15:20:42 +0000947 _leave(" = 0");
948 return 0;
949
950error_eio:
951 ret = -EIO;
952error_2:
953 if (ret == -EIO)
954 cachefiles_io_error_obj(object,
955 "Write page to backing file failed");
956error:
957 _leave(" = -ENOBUFS [%d]", ret);
958 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100959}
960
961/*
962 * detach a backing block from a page
963 * - cache withdrawal is prevented by the caller
964 */
965void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
David Howellsbfa38372018-04-04 13:41:26 +0100966 __releases(&object->fscache.cookie->lock)
David Howells9ae326a2009-04-03 16:42:41 +0100967{
968 struct cachefiles_object *object;
David Howells9ae326a2009-04-03 16:42:41 +0100969
970 object = container_of(_object, struct cachefiles_object, fscache);
David Howells9ae326a2009-04-03 16:42:41 +0100971
972 _enter("%p,{%lu}", object, page->index);
973
974 spin_unlock(&object->fscache.cookie->lock);
975}