blob: fcf4f3b72923350e3838afcf9020516662b1b3a2 [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;
Matthew Wilcox (Oracle)39f985c2021-03-20 05:40:38 +000027 struct wait_page_key *key = _key;
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -050028 struct folio *folio = wait->private;
David Howells9ae326a2009-04-03 16:42:41 +010029
30 ASSERT(key);
31
32 _enter("{%lu},%u,%d,{%p,%u}",
33 monitor->netfs_page->index, mode, sync,
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -050034 key->folio, key->bit_nr);
David Howells9ae326a2009-04-03 16:42:41 +010035
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -050036 if (key->folio != folio || key->bit_nr != PG_locked)
David Howells9ae326a2009-04-03 16:42:41 +010037 return 0;
38
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -050039 _debug("--- monitor %p %lx ---", folio, folio->flags);
David Howells9ae326a2009-04-03 16:42:41 +010040
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -050041 if (!folio_test_uptodate(folio) && !folio_test_error(folio)) {
David Howells5e929b32009-11-19 18:11:55 +000042 /* unlocked, not uptodate and not erronous? */
43 _debug("page probably truncated");
44 }
David Howells9ae326a2009-04-03 16:42:41 +010045
46 /* remove from the waitqueue */
Ingo Molnar2055da92017-06-20 12:06:46 +020047 list_del(&wait->entry);
David Howells9ae326a2009-04-03 16:42:41 +010048
49 /* move onto the action list and queue for FS-Cache thread pool */
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070050 ASSERT(op);
David Howells9ae326a2009-04-03 16:42:41 +010051
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070052 /* We need to temporarily bump the usage count as we don't own a ref
53 * here otherwise cachefiles_read_copier() may free the op between the
54 * monitor being enqueued on the op->to_do list and the op getting
55 * enqueued on the work queue.
56 */
57 fscache_get_retrieval(op);
David Howells9ae326a2009-04-03 16:42:41 +010058
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070059 object = container_of(op->op.object, struct cachefiles_object, fscache);
David Howells9ae326a2009-04-03 16:42:41 +010060 spin_lock(&object->work_lock);
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070061 list_add_tail(&monitor->op_link, &op->to_do);
Lei Xue7bb0c532020-05-07 08:50:22 -040062 fscache_enqueue_retrieval(op);
David Howells9ae326a2009-04-03 16:42:41 +010063 spin_unlock(&object->work_lock);
64
Kiran Kumar Modukuri934140a2017-07-18 16:25:49 -070065 fscache_put_retrieval(op);
David Howells9ae326a2009-04-03 16:42:41 +010066 return 0;
67}
68
69/*
David Howells5e929b32009-11-19 18:11:55 +000070 * handle a probably truncated page
71 * - check to see if the page is still relevant and reissue the read if
72 * possible
73 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
74 * must wait again and 0 if successful
75 */
76static int cachefiles_read_reissue(struct cachefiles_object *object,
77 struct cachefiles_one_read *monitor)
78{
David Howells466b77b2015-03-17 22:26:21 +000079 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
David Howells5e929b32009-11-19 18:11:55 +000080 struct page *backpage = monitor->back_page, *backpage2;
81 int ret;
82
David Howells37491a12012-12-20 21:52:34 +000083 _enter("{ino=%lx},{%lx,%lx}",
David Howells466b77b2015-03-17 22:26:21 +000084 d_backing_inode(object->backer)->i_ino,
David Howells5e929b32009-11-19 18:11:55 +000085 backpage->index, backpage->flags);
86
87 /* skip if the page was truncated away completely */
88 if (backpage->mapping != bmapping) {
David Howells37491a12012-12-20 21:52:34 +000089 _leave(" = -ENODATA [mapping]");
David Howells5e929b32009-11-19 18:11:55 +000090 return -ENODATA;
91 }
92
93 backpage2 = find_get_page(bmapping, backpage->index);
94 if (!backpage2) {
David Howells37491a12012-12-20 21:52:34 +000095 _leave(" = -ENODATA [gone]");
David Howells5e929b32009-11-19 18:11:55 +000096 return -ENODATA;
97 }
98
99 if (backpage != backpage2) {
100 put_page(backpage2);
David Howells37491a12012-12-20 21:52:34 +0000101 _leave(" = -ENODATA [different]");
David Howells5e929b32009-11-19 18:11:55 +0000102 return -ENODATA;
103 }
104
105 /* the page is still there and we already have a ref on it, so we don't
106 * need a second */
107 put_page(backpage2);
108
109 INIT_LIST_HEAD(&monitor->op_link);
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -0500110 folio_add_wait_queue(page_folio(backpage), &monitor->monitor);
David Howells5e929b32009-11-19 18:11:55 +0000111
112 if (trylock_page(backpage)) {
113 ret = -EIO;
114 if (PageError(backpage))
115 goto unlock_discard;
116 ret = 0;
117 if (PageUptodate(backpage))
118 goto unlock_discard;
119
David Howells37491a12012-12-20 21:52:34 +0000120 _debug("reissue read");
David Howells5e929b32009-11-19 18:11:55 +0000121 ret = bmapping->a_ops->readpage(NULL, backpage);
122 if (ret < 0)
Matthew Wilcox (Oracle)9480b4e2020-10-26 09:12:10 +0000123 goto discard;
David Howells5e929b32009-11-19 18:11:55 +0000124 }
125
126 /* but the page may have been read before the monitor was installed, so
127 * the monitor may miss the event - so we have to ensure that we do get
128 * one in such a case */
129 if (trylock_page(backpage)) {
130 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
131 unlock_page(backpage);
132 }
133
134 /* it'll reappear on the todo list */
David Howells37491a12012-12-20 21:52:34 +0000135 _leave(" = -EINPROGRESS");
David Howells5e929b32009-11-19 18:11:55 +0000136 return -EINPROGRESS;
137
138unlock_discard:
139 unlock_page(backpage);
Matthew Wilcox (Oracle)9480b4e2020-10-26 09:12:10 +0000140discard:
David Howells5e929b32009-11-19 18:11:55 +0000141 spin_lock_irq(&object->work_lock);
142 list_del(&monitor->op_link);
143 spin_unlock_irq(&object->work_lock);
David Howells37491a12012-12-20 21:52:34 +0000144 _leave(" = %d", ret);
David Howells5e929b32009-11-19 18:11:55 +0000145 return ret;
146}
147
148/*
David Howells9ae326a2009-04-03 16:42:41 +0100149 * copy data from backing pages to netfs pages to complete a read operation
150 * - driven by FS-Cache's thread pool
151 */
152static void cachefiles_read_copier(struct fscache_operation *_op)
153{
154 struct cachefiles_one_read *monitor;
155 struct cachefiles_object *object;
156 struct fscache_retrieval *op;
David Howells9ae326a2009-04-03 16:42:41 +0100157 int error, max;
158
159 op = container_of(_op, struct fscache_retrieval, op);
160 object = container_of(op->op.object,
161 struct cachefiles_object, fscache);
162
David Howells466b77b2015-03-17 22:26:21 +0000163 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100164
David Howells9ae326a2009-04-03 16:42:41 +0100165 max = 8;
166 spin_lock_irq(&object->work_lock);
167
168 while (!list_empty(&op->to_do)) {
169 monitor = list_entry(op->to_do.next,
170 struct cachefiles_one_read, op_link);
171 list_del(&monitor->op_link);
172
173 spin_unlock_irq(&object->work_lock);
174
175 _debug("- copy {%lu}", monitor->back_page->index);
176
David Howells5e929b32009-11-19 18:11:55 +0000177 recheck:
David Howells9dc8d9b2012-12-20 21:52:36 +0000178 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
179 &object->fscache.cookie->flags)) {
180 error = -ESTALE;
181 } else if (PageUptodate(monitor->back_page)) {
David Howells9ae326a2009-04-03 16:42:41 +0100182 copy_highpage(monitor->netfs_page, monitor->back_page);
David Howellsc4d6d8d2012-12-20 21:52:32 +0000183 fscache_mark_page_cached(monitor->op,
184 monitor->netfs_page);
David Howells9ae326a2009-04-03 16:42:41 +0100185 error = 0;
David Howells5e929b32009-11-19 18:11:55 +0000186 } else if (!PageError(monitor->back_page)) {
187 /* the page has probably been truncated */
188 error = cachefiles_read_reissue(object, monitor);
189 if (error == -EINPROGRESS)
190 goto next;
191 goto recheck;
192 } else {
David Howells9ae326a2009-04-03 16:42:41 +0100193 cachefiles_io_error_obj(
194 object,
195 "Readpage failed on backing file %lx",
196 (unsigned long) monitor->back_page->flags);
David Howells5e929b32009-11-19 18:11:55 +0000197 error = -EIO;
198 }
David Howells9ae326a2009-04-03 16:42:41 +0100199
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300200 put_page(monitor->back_page);
David Howells9ae326a2009-04-03 16:42:41 +0100201
202 fscache_end_io(op, monitor->netfs_page, error);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300203 put_page(monitor->netfs_page);
David Howells9f105232012-12-20 21:52:35 +0000204 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100205 fscache_put_retrieval(op);
206 kfree(monitor);
207
David Howells5e929b32009-11-19 18:11:55 +0000208 next:
David Howells9ae326a2009-04-03 16:42:41 +0100209 /* let the thread pool have some air occasionally */
210 max--;
211 if (max < 0 || need_resched()) {
212 if (!list_empty(&op->to_do))
213 fscache_enqueue_retrieval(op);
214 _leave(" [maxed out]");
215 return;
216 }
217
218 spin_lock_irq(&object->work_lock);
219 }
220
221 spin_unlock_irq(&object->work_lock);
222 _leave("");
223}
224
225/*
226 * read the corresponding page to the given set from the backing file
227 * - an uncertain page is simply discarded, to be tried again another time
228 */
229static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
230 struct fscache_retrieval *op,
Mel Gormana0b8cab32013-07-03 15:02:32 -0700231 struct page *netpage)
David Howells9ae326a2009-04-03 16:42:41 +0100232{
233 struct cachefiles_one_read *monitor;
234 struct address_space *bmapping;
235 struct page *newpage, *backpage;
236 int ret;
237
238 _enter("");
239
David Howells9ae326a2009-04-03 16:42:41 +0100240 _debug("read back %p{%lu,%d}",
241 netpage, netpage->index, page_count(netpage));
242
David Howells5f4f9f42012-12-20 21:52:33 +0000243 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100244 if (!monitor)
245 goto nomem;
246
247 monitor->netfs_page = netpage;
248 monitor->op = fscache_get_retrieval(op);
249
250 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
251
252 /* attempt to get hold of the backing page */
David Howells466b77b2015-03-17 22:26:21 +0000253 bmapping = d_backing_inode(object->backer)->i_mapping;
David Howells9ae326a2009-04-03 16:42:41 +0100254 newpage = NULL;
255
256 for (;;) {
257 backpage = find_get_page(bmapping, netpage->index);
258 if (backpage)
259 goto backing_page_already_present;
260
261 if (!newpage) {
Mel Gorman453f85d2017-11-15 17:38:03 -0800262 newpage = __page_cache_alloc(cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100263 if (!newpage)
264 goto nomem_monitor;
265 }
266
Johannes Weiner55881bc2014-04-03 14:47:36 -0700267 ret = add_to_page_cache_lru(newpage, bmapping,
268 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100269 if (ret == 0)
270 goto installed_new_backing_page;
271 if (ret != -EEXIST)
272 goto nomem_page;
273 }
274
Johannes Weiner55881bc2014-04-03 14:47:36 -0700275 /* we've installed a new backing page, so now we need to start
276 * it reading */
David Howells9ae326a2009-04-03 16:42:41 +0100277installed_new_backing_page:
278 _debug("- new %p", newpage);
279
280 backpage = newpage;
281 newpage = NULL;
282
David Howells9ae326a2009-04-03 16:42:41 +0100283read_backing_page:
284 ret = bmapping->a_ops->readpage(NULL, backpage);
285 if (ret < 0)
286 goto read_error;
287
288 /* set the monitor to transfer the data across */
289monitor_backing_page:
290 _debug("- monitor add");
291
292 /* install the monitor */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300293 get_page(monitor->netfs_page);
294 get_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100295 monitor->back_page = backpage;
296 monitor->monitor.private = backpage;
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -0500297 folio_add_wait_queue(page_folio(backpage), &monitor->monitor);
David Howells9ae326a2009-04-03 16:42:41 +0100298 monitor = NULL;
299
300 /* but the page may have been read before the monitor was installed, so
301 * the monitor may miss the event - so we have to ensure that we do get
302 * one in such a case */
303 if (trylock_page(backpage)) {
304 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
305 unlock_page(backpage);
306 }
307 goto success;
308
309 /* if the backing page is already present, it can be in one of
310 * three states: read in progress, read failed or read okay */
311backing_page_already_present:
312 _debug("- present");
313
314 if (newpage) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300315 put_page(newpage);
David Howells9ae326a2009-04-03 16:42:41 +0100316 newpage = NULL;
317 }
318
319 if (PageError(backpage))
320 goto io_error;
321
322 if (PageUptodate(backpage))
323 goto backing_page_already_uptodate;
324
325 if (!trylock_page(backpage))
326 goto monitor_backing_page;
327 _debug("read %p {%lx}", backpage, backpage->flags);
328 goto read_backing_page;
329
330 /* the backing page is already up to date, attach the netfs
331 * page to the pagecache and LRU and copy the data across */
332backing_page_already_uptodate:
333 _debug("- uptodate");
334
David Howellsc4d6d8d2012-12-20 21:52:32 +0000335 fscache_mark_page_cached(op, netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100336
337 copy_highpage(netpage, backpage);
338 fscache_end_io(op, netpage, 0);
David Howells9f105232012-12-20 21:52:35 +0000339 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100340
341success:
342 _debug("success");
343 ret = 0;
344
345out:
346 if (backpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300347 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100348 if (monitor) {
349 fscache_put_retrieval(monitor->op);
350 kfree(monitor);
351 }
352 _leave(" = %d", ret);
353 return ret;
354
355read_error:
356 _debug("read error %d", ret);
David Howellsb4cf1e02012-12-05 13:34:45 +0000357 if (ret == -ENOMEM) {
358 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100359 goto out;
David Howellsb4cf1e02012-12-05 13:34:45 +0000360 }
David Howells9ae326a2009-04-03 16:42:41 +0100361io_error:
362 cachefiles_io_error_obj(object, "Page read error on backing file");
David Howells9f105232012-12-20 21:52:35 +0000363 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100364 ret = -ENOBUFS;
365 goto out;
366
367nomem_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300368 put_page(newpage);
David Howells9ae326a2009-04-03 16:42:41 +0100369nomem_monitor:
370 fscache_put_retrieval(monitor->op);
371 kfree(monitor);
372nomem:
David Howells9f105232012-12-20 21:52:35 +0000373 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100374 _leave(" = -ENOMEM");
375 return -ENOMEM;
376}
377
378/*
379 * read a page from the cache or allocate a block in which to store it
380 * - cache withdrawal is prevented by the caller
381 * - returns -EINTR if interrupted
382 * - returns -ENOMEM if ran out of memory
383 * - returns -ENOBUFS if no buffers can be made available
384 * - returns -ENOBUFS if page is beyond EOF
385 * - if the page is backed by a block in the cache:
386 * - a read will be started which will call the callback on completion
387 * - 0 will be returned
388 * - else if the page is unbacked:
389 * - the metadata will be retained
390 * - -ENODATA will be returned
391 */
392int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
393 struct page *page,
394 gfp_t gfp)
395{
396 struct cachefiles_object *object;
397 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100398 struct inode *inode;
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100399 sector_t block;
David Howells9ae326a2009-04-03 16:42:41 +0100400 unsigned shift;
David Howellsc5f9d9d2020-05-04 16:12:55 +0100401 int ret, ret2;
David Howells9ae326a2009-04-03 16:42:41 +0100402
403 object = container_of(op->op.object,
404 struct cachefiles_object, fscache);
405 cache = container_of(object->fscache.cache,
406 struct cachefiles_cache, cache);
407
408 _enter("{%p},{%lx},,,", object, page->index);
409
410 if (!object->backer)
David Howells9f105232012-12-20 21:52:35 +0000411 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100412
David Howells466b77b2015-03-17 22:26:21 +0000413 inode = d_backing_inode(object->backer);
David Howells9ae326a2009-04-03 16:42:41 +0100414 ASSERT(S_ISREG(inode->i_mode));
David Howells9ae326a2009-04-03 16:42:41 +0100415
416 /* calculate the shift required to use bmap */
David Howells9ae326a2009-04-03 16:42:41 +0100417 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
418
David Howells4fbf4292009-11-19 18:11:04 +0000419 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
Tejun Heo8af7c122010-07-20 22:09:01 +0200420 op->op.flags |= FSCACHE_OP_ASYNC;
David Howells9ae326a2009-04-03 16:42:41 +0100421 op->op.processor = cachefiles_read_copier;
422
David Howells9ae326a2009-04-03 16:42:41 +0100423 /* we assume the absence or presence of the first block is a good
424 * enough indication for the page as a whole
425 * - TODO: don't use bmap() for this as it is _not_ actually good
426 * enough for this as it doesn't indicate errors, but it's all we've
427 * got for the moment
428 */
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100429 block = page->index;
430 block <<= shift;
David Howells9ae326a2009-04-03 16:42:41 +0100431
David Howellsc5f9d9d2020-05-04 16:12:55 +0100432 ret2 = bmap(inode, &block);
433 ASSERT(ret2 == 0);
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100434
David Howells9ae326a2009-04-03 16:42:41 +0100435 _debug("%llx -> %llx",
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100436 (unsigned long long) (page->index << shift),
David Howells9ae326a2009-04-03 16:42:41 +0100437 (unsigned long long) block);
438
439 if (block) {
440 /* submit the apparently valid page to the backing fs to be
441 * read from disk */
Mel Gormana0b8cab32013-07-03 15:02:32 -0700442 ret = cachefiles_read_backing_file_one(object, op, page);
David Howells9ae326a2009-04-03 16:42:41 +0100443 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
444 /* there's space in the cache we can use */
David Howellsc4d6d8d2012-12-20 21:52:32 +0000445 fscache_mark_page_cached(op, page);
David Howells9f105232012-12-20 21:52:35 +0000446 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100447 ret = -ENODATA;
448 } else {
David Howells9f105232012-12-20 21:52:35 +0000449 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100450 }
451
452 _leave(" = %d", ret);
453 return ret;
David Howells9f105232012-12-20 21:52:35 +0000454
455enobufs:
456 fscache_retrieval_complete(op, 1);
457 _leave(" = -ENOBUFS");
458 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100459}
460
461/*
462 * read the corresponding pages to the given set from the backing file
463 * - any uncertain pages are simply discarded, to be tried again another time
464 */
465static int cachefiles_read_backing_file(struct cachefiles_object *object,
466 struct fscache_retrieval *op,
David Howellsc4d6d8d2012-12-20 21:52:32 +0000467 struct list_head *list)
David Howells9ae326a2009-04-03 16:42:41 +0100468{
469 struct cachefiles_one_read *monitor = NULL;
David Howells466b77b2015-03-17 22:26:21 +0000470 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
David Howells9ae326a2009-04-03 16:42:41 +0100471 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
472 int ret = 0;
473
474 _enter("");
475
David Howells9ae326a2009-04-03 16:42:41 +0100476 list_for_each_entry_safe(netpage, _n, list, lru) {
477 list_del(&netpage->lru);
478
479 _debug("read back %p{%lu,%d}",
480 netpage, netpage->index, page_count(netpage));
481
482 if (!monitor) {
David Howells5f4f9f42012-12-20 21:52:33 +0000483 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100484 if (!monitor)
485 goto nomem;
486
487 monitor->op = fscache_get_retrieval(op);
488 init_waitqueue_func_entry(&monitor->monitor,
489 cachefiles_read_waiter);
490 }
491
492 for (;;) {
493 backpage = find_get_page(bmapping, netpage->index);
494 if (backpage)
495 goto backing_page_already_present;
496
497 if (!newpage) {
Mel Gorman453f85d2017-11-15 17:38:03 -0800498 newpage = __page_cache_alloc(cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100499 if (!newpage)
500 goto nomem;
501 }
502
Johannes Weiner55881bc2014-04-03 14:47:36 -0700503 ret = add_to_page_cache_lru(newpage, bmapping,
504 netpage->index,
505 cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100506 if (ret == 0)
507 goto installed_new_backing_page;
508 if (ret != -EEXIST)
509 goto nomem;
510 }
511
Johannes Weiner55881bc2014-04-03 14:47:36 -0700512 /* we've installed a new backing page, so now we need
513 * to start it reading */
David Howells9ae326a2009-04-03 16:42:41 +0100514 installed_new_backing_page:
515 _debug("- new %p", newpage);
516
517 backpage = newpage;
518 newpage = NULL;
519
David Howells9ae326a2009-04-03 16:42:41 +0100520 reread_backing_page:
521 ret = bmapping->a_ops->readpage(NULL, backpage);
522 if (ret < 0)
523 goto read_error;
524
525 /* add the netfs page to the pagecache and LRU, and set the
526 * monitor to transfer the data across */
527 monitor_backing_page:
528 _debug("- monitor add");
529
Johannes Weiner55881bc2014-04-03 14:47:36 -0700530 ret = add_to_page_cache_lru(netpage, op->mapping,
531 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100532 if (ret < 0) {
533 if (ret == -EEXIST) {
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000534 put_page(backpage);
535 backpage = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300536 put_page(netpage);
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000537 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000538 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100539 continue;
540 }
541 goto nomem;
542 }
543
David Howells9ae326a2009-04-03 16:42:41 +0100544 /* install a monitor */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300545 get_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100546 monitor->netfs_page = netpage;
547
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300548 get_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100549 monitor->back_page = backpage;
550 monitor->monitor.private = backpage;
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -0500551 folio_add_wait_queue(page_folio(backpage), &monitor->monitor);
David Howells9ae326a2009-04-03 16:42:41 +0100552 monitor = NULL;
553
554 /* but the page may have been read before the monitor was
555 * installed, so the monitor may miss the event - so we have to
556 * ensure that we do get one in such a case */
557 if (trylock_page(backpage)) {
558 _debug("2unlock %p {%lx}", backpage, backpage->flags);
559 unlock_page(backpage);
560 }
561
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300562 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100563 backpage = NULL;
564
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300565 put_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100566 netpage = NULL;
567 continue;
568
569 /* if the backing page is already present, it can be in one of
570 * three states: read in progress, read failed or read okay */
571 backing_page_already_present:
572 _debug("- present %p", backpage);
573
574 if (PageError(backpage))
575 goto io_error;
576
577 if (PageUptodate(backpage))
578 goto backing_page_already_uptodate;
579
580 _debug("- not ready %p{%lx}", backpage, backpage->flags);
581
582 if (!trylock_page(backpage))
583 goto monitor_backing_page;
584
585 if (PageError(backpage)) {
586 _debug("error %lx", backpage->flags);
587 unlock_page(backpage);
588 goto io_error;
589 }
590
591 if (PageUptodate(backpage))
592 goto backing_page_already_uptodate_unlock;
593
594 /* we've locked a page that's neither up to date nor erroneous,
595 * so we need to attempt to read it again */
596 goto reread_backing_page;
597
598 /* the backing page is already up to date, attach the netfs
599 * page to the pagecache and LRU and copy the data across */
600 backing_page_already_uptodate_unlock:
601 _debug("uptodate %lx", backpage->flags);
602 unlock_page(backpage);
603 backing_page_already_uptodate:
604 _debug("- uptodate");
605
Johannes Weiner55881bc2014-04-03 14:47:36 -0700606 ret = add_to_page_cache_lru(netpage, op->mapping,
607 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100608 if (ret < 0) {
609 if (ret == -EEXIST) {
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000610 put_page(backpage);
611 backpage = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300612 put_page(netpage);
Kiran Kumar Modukuri9a24ce52018-09-24 12:02:39 +1000613 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000614 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100615 continue;
616 }
617 goto nomem;
618 }
619
620 copy_highpage(netpage, backpage);
621
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300622 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100623 backpage = NULL;
624
David Howellsc4d6d8d2012-12-20 21:52:32 +0000625 fscache_mark_page_cached(op, netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100626
David Howellsc4d6d8d2012-12-20 21:52:32 +0000627 /* the netpage is unlocked and marked up to date here */
David Howells9ae326a2009-04-03 16:42:41 +0100628 fscache_end_io(op, netpage, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300629 put_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100630 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000631 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100632 continue;
633 }
634
635 netpage = NULL;
636
637 _debug("out");
638
639out:
640 /* tidy up */
David Howells9ae326a2009-04-03 16:42:41 +0100641 if (newpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300642 put_page(newpage);
David Howells9ae326a2009-04-03 16:42:41 +0100643 if (netpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300644 put_page(netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100645 if (backpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300646 put_page(backpage);
David Howells9ae326a2009-04-03 16:42:41 +0100647 if (monitor) {
648 fscache_put_retrieval(op);
649 kfree(monitor);
650 }
651
652 list_for_each_entry_safe(netpage, _n, list, lru) {
653 list_del(&netpage->lru);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300654 put_page(netpage);
David Howells9f105232012-12-20 21:52:35 +0000655 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100656 }
657
658 _leave(" = %d", ret);
659 return ret;
660
661nomem:
662 _debug("nomem");
663 ret = -ENOMEM;
David Howellsb4cf1e02012-12-05 13:34:45 +0000664 goto record_page_complete;
David Howells9ae326a2009-04-03 16:42:41 +0100665
666read_error:
667 _debug("read error %d", ret);
668 if (ret == -ENOMEM)
David Howellsb4cf1e02012-12-05 13:34:45 +0000669 goto record_page_complete;
David Howells9ae326a2009-04-03 16:42:41 +0100670io_error:
671 cachefiles_io_error_obj(object, "Page read error on backing file");
672 ret = -ENOBUFS;
David Howellsb4cf1e02012-12-05 13:34:45 +0000673record_page_complete:
674 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100675 goto out;
676}
677
678/*
679 * read a list of pages from the cache or allocate blocks in which to store
680 * them
681 */
682int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
683 struct list_head *pages,
684 unsigned *nr_pages,
685 gfp_t gfp)
686{
687 struct cachefiles_object *object;
688 struct cachefiles_cache *cache;
689 struct list_head backpages;
690 struct pagevec pagevec;
691 struct inode *inode;
692 struct page *page, *_n;
693 unsigned shift, nrbackpages;
694 int ret, ret2, space;
695
696 object = container_of(op->op.object,
697 struct cachefiles_object, fscache);
698 cache = container_of(object->fscache.cache,
699 struct cachefiles_cache, cache);
700
701 _enter("{OBJ%x,%d},,%d,,",
702 object->fscache.debug_id, atomic_read(&op->op.usage),
703 *nr_pages);
704
705 if (!object->backer)
David Howells9f105232012-12-20 21:52:35 +0000706 goto all_enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100707
708 space = 1;
709 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
710 space = 0;
711
David Howells466b77b2015-03-17 22:26:21 +0000712 inode = d_backing_inode(object->backer);
David Howells9ae326a2009-04-03 16:42:41 +0100713 ASSERT(S_ISREG(inode->i_mode));
David Howells9ae326a2009-04-03 16:42:41 +0100714
715 /* calculate the shift required to use bmap */
David Howells9ae326a2009-04-03 16:42:41 +0100716 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
717
Mel Gorman86679822017-11-15 17:37:52 -0800718 pagevec_init(&pagevec);
David Howells9ae326a2009-04-03 16:42:41 +0100719
David Howells4fbf4292009-11-19 18:11:04 +0000720 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
Tejun Heo8af7c122010-07-20 22:09:01 +0200721 op->op.flags |= FSCACHE_OP_ASYNC;
David Howells9ae326a2009-04-03 16:42:41 +0100722 op->op.processor = cachefiles_read_copier;
723
724 INIT_LIST_HEAD(&backpages);
725 nrbackpages = 0;
726
727 ret = space ? -ENODATA : -ENOBUFS;
728 list_for_each_entry_safe(page, _n, pages, lru) {
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100729 sector_t block;
David Howells9ae326a2009-04-03 16:42:41 +0100730
731 /* we assume the absence or presence of the first block is a
732 * good enough indication for the page as a whole
733 * - TODO: don't use bmap() for this as it is _not_ actually
734 * good enough for this as it doesn't indicate errors, but
735 * it's all we've got for the moment
736 */
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100737 block = page->index;
738 block <<= shift;
David Howells9ae326a2009-04-03 16:42:41 +0100739
David Howellsc5f9d9d2020-05-04 16:12:55 +0100740 ret2 = bmap(inode, &block);
741 ASSERT(ret2 == 0);
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100742
David Howells9ae326a2009-04-03 16:42:41 +0100743 _debug("%llx -> %llx",
Carlos Maiolino10d83e112020-01-09 14:30:42 +0100744 (unsigned long long) (page->index << shift),
David Howells9ae326a2009-04-03 16:42:41 +0100745 (unsigned long long) block);
746
747 if (block) {
748 /* we have data - add it to the list to give to the
749 * backing fs */
750 list_move(&page->lru, &backpages);
751 (*nr_pages)--;
752 nrbackpages++;
753 } else if (space && pagevec_add(&pagevec, page) == 0) {
754 fscache_mark_pages_cached(op, &pagevec);
David Howells9f105232012-12-20 21:52:35 +0000755 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100756 ret = -ENODATA;
David Howells9f105232012-12-20 21:52:35 +0000757 } else {
758 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100759 }
760 }
761
762 if (pagevec_count(&pagevec) > 0)
763 fscache_mark_pages_cached(op, &pagevec);
764
765 if (list_empty(pages))
766 ret = 0;
767
768 /* submit the apparently valid pages to the backing fs to be read from
769 * disk */
770 if (nrbackpages > 0) {
David Howellsc4d6d8d2012-12-20 21:52:32 +0000771 ret2 = cachefiles_read_backing_file(object, op, &backpages);
David Howells9ae326a2009-04-03 16:42:41 +0100772 if (ret2 == -ENOMEM || ret2 == -EINTR)
773 ret = ret2;
774 }
775
David Howells9ae326a2009-04-03 16:42:41 +0100776 _leave(" = %d [nr=%u%s]",
777 ret, *nr_pages, list_empty(pages) ? " empty" : "");
778 return ret;
David Howells9f105232012-12-20 21:52:35 +0000779
780all_enobufs:
781 fscache_retrieval_complete(op, *nr_pages);
782 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100783}
784
785/*
786 * allocate a block in the cache in which to store a page
787 * - cache withdrawal is prevented by the caller
788 * - returns -EINTR if interrupted
789 * - returns -ENOMEM if ran out of memory
790 * - returns -ENOBUFS if no buffers can be made available
791 * - returns -ENOBUFS if page is beyond EOF
792 * - otherwise:
793 * - the metadata will be retained
794 * - 0 will be returned
795 */
796int cachefiles_allocate_page(struct fscache_retrieval *op,
797 struct page *page,
798 gfp_t gfp)
799{
800 struct cachefiles_object *object;
801 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100802 int ret;
803
804 object = container_of(op->op.object,
805 struct cachefiles_object, fscache);
806 cache = container_of(object->fscache.cache,
807 struct cachefiles_cache, cache);
808
809 _enter("%p,{%lx},", object, page->index);
810
811 ret = cachefiles_has_space(cache, 0, 1);
David Howellsc4d6d8d2012-12-20 21:52:32 +0000812 if (ret == 0)
813 fscache_mark_page_cached(op, page);
814 else
David Howells9ae326a2009-04-03 16:42:41 +0100815 ret = -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100816
David Howells9f105232012-12-20 21:52:35 +0000817 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100818 _leave(" = %d", ret);
819 return ret;
820}
821
822/*
823 * allocate blocks in the cache in which to store a set of pages
824 * - cache withdrawal is prevented by the caller
825 * - returns -EINTR if interrupted
826 * - returns -ENOMEM if ran out of memory
827 * - returns -ENOBUFS if some buffers couldn't be made available
828 * - returns -ENOBUFS if some pages are beyond EOF
829 * - otherwise:
830 * - -ENODATA will be returned
831 * - metadata will be retained for any page marked
832 */
833int cachefiles_allocate_pages(struct fscache_retrieval *op,
834 struct list_head *pages,
835 unsigned *nr_pages,
836 gfp_t gfp)
837{
838 struct cachefiles_object *object;
839 struct cachefiles_cache *cache;
840 struct pagevec pagevec;
841 struct page *page;
842 int ret;
843
844 object = container_of(op->op.object,
845 struct cachefiles_object, fscache);
846 cache = container_of(object->fscache.cache,
847 struct cachefiles_cache, cache);
848
849 _enter("%p,,,%d,", object, *nr_pages);
850
851 ret = cachefiles_has_space(cache, 0, *nr_pages);
852 if (ret == 0) {
Mel Gorman86679822017-11-15 17:37:52 -0800853 pagevec_init(&pagevec);
David Howells9ae326a2009-04-03 16:42:41 +0100854
855 list_for_each_entry(page, pages, lru) {
856 if (pagevec_add(&pagevec, page) == 0)
857 fscache_mark_pages_cached(op, &pagevec);
858 }
859
860 if (pagevec_count(&pagevec) > 0)
861 fscache_mark_pages_cached(op, &pagevec);
862 ret = -ENODATA;
863 } else {
864 ret = -ENOBUFS;
865 }
866
David Howells9f105232012-12-20 21:52:35 +0000867 fscache_retrieval_complete(op, *nr_pages);
David Howells9ae326a2009-04-03 16:42:41 +0100868 _leave(" = %d", ret);
869 return ret;
870}
871
872/*
873 * request a page be stored in the cache
874 * - cache withdrawal is prevented by the caller
875 * - this request may be ignored if there's no cache block available, in which
876 * case -ENOBUFS will be returned
877 * - if the op is in progress, 0 will be returned
878 */
879int cachefiles_write_page(struct fscache_storage *op, struct page *page)
880{
881 struct cachefiles_object *object;
882 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100883 struct file *file;
Al Viro765927b2012-06-26 21:58:53 +0400884 struct path path;
David Howellsa17754f2009-11-19 18:11:52 +0000885 loff_t pos, eof;
886 size_t len;
David Howells9ae326a2009-04-03 16:42:41 +0100887 void *data;
Geert Uytterhoevencf897522015-11-12 11:46:23 +0000888 int ret = -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100889
890 ASSERT(op != NULL);
891 ASSERT(page != NULL);
892
893 object = container_of(op->op.object,
894 struct cachefiles_object, fscache);
895
896 _enter("%p,%p{%lx},,,", object, page, page->index);
897
898 if (!object->backer) {
899 _leave(" = -ENOBUFS");
900 return -ENOBUFS;
901 }
902
David Howellsce40fa72015-01-29 12:02:36 +0000903 ASSERT(d_is_reg(object->backer));
David Howells9ae326a2009-04-03 16:42:41 +0100904
905 cache = container_of(object->fscache.cache,
906 struct cachefiles_cache, cache);
907
David Howells102f4d92015-11-04 15:20:42 +0000908 pos = (loff_t)page->index << PAGE_SHIFT;
909
910 /* We mustn't write more data than we have, so we have to beware of a
911 * partial page at EOF.
912 */
913 eof = object->fscache.store_limit_l;
914 if (pos >= eof)
915 goto error;
916
David Howells9ae326a2009-04-03 16:42:41 +0100917 /* write the page to the backing filesystem and let it store it in its
918 * own time */
Al Viro765927b2012-06-26 21:58:53 +0400919 path.mnt = cache->mnt;
920 path.dentry = object->backer;
Justin Lecher98c350c2012-07-30 14:42:53 -0700921 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
David Howells9ae326a2009-04-03 16:42:41 +0100922 if (IS_ERR(file)) {
923 ret = PTR_ERR(file);
David Howells102f4d92015-11-04 15:20:42 +0000924 goto error_2;
925 }
David Howellsa17754f2009-11-19 18:11:52 +0000926
David Howells102f4d92015-11-04 15:20:42 +0000927 len = PAGE_SIZE;
928 if (eof & ~PAGE_MASK) {
929 if (eof - pos < PAGE_SIZE) {
930 _debug("cut short %llx to %llx",
931 pos, eof);
932 len = eof - pos;
933 ASSERTCMP(pos + len, ==, eof);
David Howells9ae326a2009-04-03 16:42:41 +0100934 }
David Howells9ae326a2009-04-03 16:42:41 +0100935 }
936
David Howells102f4d92015-11-04 15:20:42 +0000937 data = kmap(page);
Christoph Hellwig97c79902020-05-13 08:42:36 +0200938 ret = kernel_write(file, data, len, &pos);
David Howells102f4d92015-11-04 15:20:42 +0000939 kunmap(page);
940 fput(file);
941 if (ret != len)
942 goto error_eio;
David Howells9ae326a2009-04-03 16:42:41 +0100943
David Howells102f4d92015-11-04 15:20:42 +0000944 _leave(" = 0");
945 return 0;
946
947error_eio:
948 ret = -EIO;
949error_2:
950 if (ret == -EIO)
951 cachefiles_io_error_obj(object,
952 "Write page to backing file failed");
953error:
954 _leave(" = -ENOBUFS [%d]", ret);
955 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100956}
957
958/*
959 * detach a backing block from a page
960 * - cache withdrawal is prevented by the caller
961 */
962void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
David Howellsbfa38372018-04-04 13:41:26 +0100963 __releases(&object->fscache.cookie->lock)
David Howells9ae326a2009-04-03 16:42:41 +0100964{
965 struct cachefiles_object *object;
David Howells9ae326a2009-04-03 16:42:41 +0100966
967 object = container_of(_object, struct cachefiles_object, fscache);
David Howells9ae326a2009-04-03 16:42:41 +0100968
969 _enter("%p,{%lu}", object, page->index);
970
971 spin_unlock(&object->fscache.cookie->lock);
972}