]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/cachefiles/rdwr.c
Merge tag 'net-5.11-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[mirror_ubuntu-hirsute-kernel.git] / fs / cachefiles / rdwr.c
CommitLineData
b4d0d230 1// SPDX-License-Identifier: GPL-2.0-or-later
9ae326a6
DH
2/* Storage object read/write
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9ae326a6
DH
6 */
7
8#include <linux/mount.h>
5a0e3ad6 9#include <linux/slab.h>
9ae326a6 10#include <linux/file.h>
a0b8cab3 11#include <linux/swap.h>
9ae326a6
DH
12#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 */
ac6424b9 20static int cachefiles_read_waiter(wait_queue_entry_t *wait, unsigned mode,
9ae326a6
DH
21 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;
934140ab 26 struct fscache_retrieval *op = monitor->op;
9ae326a6
DH
27 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
5e929b33
DH
42 if (!PageUptodate(page) && !PageError(page)) {
43 /* unlocked, not uptodate and not erronous? */
44 _debug("page probably truncated");
45 }
9ae326a6
DH
46
47 /* remove from the waitqueue */
2055da97 48 list_del(&wait->entry);
9ae326a6
DH
49
50 /* move onto the action list and queue for FS-Cache thread pool */
934140ab 51 ASSERT(op);
9ae326a6 52
934140ab
KKM
53 /* 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);
9ae326a6 59
934140ab 60 object = container_of(op->op.object, struct cachefiles_object, fscache);
9ae326a6 61 spin_lock(&object->work_lock);
934140ab 62 list_add_tail(&monitor->op_link, &op->to_do);
7bb0c533 63 fscache_enqueue_retrieval(op);
9ae326a6
DH
64 spin_unlock(&object->work_lock);
65
934140ab 66 fscache_put_retrieval(op);
9ae326a6
DH
67 return 0;
68}
69
5e929b33
DH
70/*
71 * 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{
466b77bc 80 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
5e929b33
DH
81 struct page *backpage = monitor->back_page, *backpage2;
82 int ret;
83
37491a13 84 _enter("{ino=%lx},{%lx,%lx}",
466b77bc 85 d_backing_inode(object->backer)->i_ino,
5e929b33
DH
86 backpage->index, backpage->flags);
87
88 /* skip if the page was truncated away completely */
89 if (backpage->mapping != bmapping) {
37491a13 90 _leave(" = -ENODATA [mapping]");
5e929b33
DH
91 return -ENODATA;
92 }
93
94 backpage2 = find_get_page(bmapping, backpage->index);
95 if (!backpage2) {
37491a13 96 _leave(" = -ENODATA [gone]");
5e929b33
DH
97 return -ENODATA;
98 }
99
100 if (backpage != backpage2) {
101 put_page(backpage2);
37491a13 102 _leave(" = -ENODATA [different]");
5e929b33
DH
103 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
37491a13 121 _debug("reissue read");
5e929b33
DH
122 ret = bmapping->a_ops->readpage(NULL, backpage);
123 if (ret < 0)
9480b4e7 124 goto discard;
5e929b33
DH
125 }
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 */
37491a13 136 _leave(" = -EINPROGRESS");
5e929b33
DH
137 return -EINPROGRESS;
138
139unlock_discard:
140 unlock_page(backpage);
9480b4e7 141discard:
5e929b33
DH
142 spin_lock_irq(&object->work_lock);
143 list_del(&monitor->op_link);
144 spin_unlock_irq(&object->work_lock);
37491a13 145 _leave(" = %d", ret);
5e929b33
DH
146 return ret;
147}
148
9ae326a6
DH
149/*
150 * 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;
9ae326a6
DH
158 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
466b77bc 164 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
9ae326a6 165
9ae326a6
DH
166 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
5e929b33 178 recheck:
9dc8d9bf
DH
179 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
180 &object->fscache.cookie->flags)) {
181 error = -ESTALE;
182 } else if (PageUptodate(monitor->back_page)) {
9ae326a6 183 copy_highpage(monitor->netfs_page, monitor->back_page);
c4d6d8db
DH
184 fscache_mark_page_cached(monitor->op,
185 monitor->netfs_page);
9ae326a6 186 error = 0;
5e929b33
DH
187 } 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 {
9ae326a6
DH
194 cachefiles_io_error_obj(
195 object,
196 "Readpage failed on backing file %lx",
197 (unsigned long) monitor->back_page->flags);
5e929b33
DH
198 error = -EIO;
199 }
9ae326a6 200
09cbfeaf 201 put_page(monitor->back_page);
9ae326a6
DH
202
203 fscache_end_io(op, monitor->netfs_page, error);
09cbfeaf 204 put_page(monitor->netfs_page);
9f10523f 205 fscache_retrieval_complete(op, 1);
9ae326a6
DH
206 fscache_put_retrieval(op);
207 kfree(monitor);
208
5e929b33 209 next:
9ae326a6
DH
210 /* 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,
a0b8cab3 232 struct page *netpage)
9ae326a6
DH
233{
234 struct cachefiles_one_read *monitor;
235 struct address_space *bmapping;
236 struct page *newpage, *backpage;
237 int ret;
238
239 _enter("");
240
9ae326a6
DH
241 _debug("read back %p{%lu,%d}",
242 netpage, netpage->index, page_count(netpage));
243
5f4f9f4a 244 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
9ae326a6
DH
245 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 */
466b77bc 254 bmapping = d_backing_inode(object->backer)->i_mapping;
9ae326a6
DH
255 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) {
453f85d4 263 newpage = __page_cache_alloc(cachefiles_gfp);
9ae326a6
DH
264 if (!newpage)
265 goto nomem_monitor;
266 }
267
55881bc7
JW
268 ret = add_to_page_cache_lru(newpage, bmapping,
269 netpage->index, cachefiles_gfp);
9ae326a6
DH
270 if (ret == 0)
271 goto installed_new_backing_page;
272 if (ret != -EEXIST)
273 goto nomem_page;
274 }
275
55881bc7
JW
276 /* we've installed a new backing page, so now we need to start
277 * it reading */
9ae326a6
DH
278installed_new_backing_page:
279 _debug("- new %p", newpage);
280
281 backpage = newpage;
282 newpage = NULL;
283
9ae326a6
DH
284read_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 */
09cbfeaf
KS
294 get_page(monitor->netfs_page);
295 get_page(backpage);
9ae326a6
DH
296 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) {
09cbfeaf 316 put_page(newpage);
9ae326a6
DH
317 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
c4d6d8db 336 fscache_mark_page_cached(op, netpage);
9ae326a6
DH
337
338 copy_highpage(netpage, backpage);
339 fscache_end_io(op, netpage, 0);
9f10523f 340 fscache_retrieval_complete(op, 1);
9ae326a6
DH
341
342success:
343 _debug("success");
344 ret = 0;
345
346out:
347 if (backpage)
09cbfeaf 348 put_page(backpage);
9ae326a6
DH
349 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);
b4cf1e08
DH
358 if (ret == -ENOMEM) {
359 fscache_retrieval_complete(op, 1);
9ae326a6 360 goto out;
b4cf1e08 361 }
9ae326a6
DH
362io_error:
363 cachefiles_io_error_obj(object, "Page read error on backing file");
9f10523f 364 fscache_retrieval_complete(op, 1);
9ae326a6
DH
365 ret = -ENOBUFS;
366 goto out;
367
368nomem_page:
09cbfeaf 369 put_page(newpage);
9ae326a6
DH
370nomem_monitor:
371 fscache_put_retrieval(monitor->op);
372 kfree(monitor);
373nomem:
9f10523f 374 fscache_retrieval_complete(op, 1);
9ae326a6
DH
375 _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;
9ae326a6 399 struct inode *inode;
10d83e11 400 sector_t block;
9ae326a6 401 unsigned shift;
c5f9d9db 402 int ret, ret2;
9ae326a6
DH
403
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)
9f10523f 412 goto enobufs;
9ae326a6 413
466b77bc 414 inode = d_backing_inode(object->backer);
9ae326a6 415 ASSERT(S_ISREG(inode->i_mode));
9ae326a6
DH
416
417 /* calculate the shift required to use bmap */
9ae326a6
DH
418 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
419
4fbf4291 420 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
8af7c124 421 op->op.flags |= FSCACHE_OP_ASYNC;
9ae326a6
DH
422 op->op.processor = cachefiles_read_copier;
423
9ae326a6
DH
424 /* we assume the absence or presence of the first block is a good
425 * enough indication for the page as a whole
426 * - TODO: don't use bmap() for this as it is _not_ actually good
427 * enough for this as it doesn't indicate errors, but it's all we've
428 * got for the moment
429 */
10d83e11
CM
430 block = page->index;
431 block <<= shift;
432
c5f9d9db
DH
433 ret2 = bmap(inode, &block);
434 ASSERT(ret2 == 0);
9ae326a6 435
9ae326a6 436 _debug("%llx -> %llx",
10d83e11 437 (unsigned long long) (page->index << shift),
9ae326a6
DH
438 (unsigned long long) block);
439
440 if (block) {
441 /* submit the apparently valid page to the backing fs to be
442 * read from disk */
a0b8cab3 443 ret = cachefiles_read_backing_file_one(object, op, page);
9ae326a6
DH
444 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
445 /* there's space in the cache we can use */
c4d6d8db 446 fscache_mark_page_cached(op, page);
9f10523f 447 fscache_retrieval_complete(op, 1);
9ae326a6
DH
448 ret = -ENODATA;
449 } else {
9f10523f 450 goto enobufs;
9ae326a6
DH
451 }
452
453 _leave(" = %d", ret);
454 return ret;
9f10523f
DH
455
456enobufs:
457 fscache_retrieval_complete(op, 1);
458 _leave(" = -ENOBUFS");
459 return -ENOBUFS;
9ae326a6
DH
460}
461
462/*
463 * read the corresponding pages to the given set from the backing file
464 * - any uncertain pages are simply discarded, to be tried again another time
465 */
466static int cachefiles_read_backing_file(struct cachefiles_object *object,
467 struct fscache_retrieval *op,
c4d6d8db 468 struct list_head *list)
9ae326a6
DH
469{
470 struct cachefiles_one_read *monitor = NULL;
466b77bc 471 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
9ae326a6
DH
472 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
473 int ret = 0;
474
475 _enter("");
476
9ae326a6
DH
477 list_for_each_entry_safe(netpage, _n, list, lru) {
478 list_del(&netpage->lru);
479
480 _debug("read back %p{%lu,%d}",
481 netpage, netpage->index, page_count(netpage));
482
483 if (!monitor) {
5f4f9f4a 484 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
9ae326a6
DH
485 if (!monitor)
486 goto nomem;
487
488 monitor->op = fscache_get_retrieval(op);
489 init_waitqueue_func_entry(&monitor->monitor,
490 cachefiles_read_waiter);
491 }
492
493 for (;;) {
494 backpage = find_get_page(bmapping, netpage->index);
495 if (backpage)
496 goto backing_page_already_present;
497
498 if (!newpage) {
453f85d4 499 newpage = __page_cache_alloc(cachefiles_gfp);
9ae326a6
DH
500 if (!newpage)
501 goto nomem;
502 }
503
55881bc7
JW
504 ret = add_to_page_cache_lru(newpage, bmapping,
505 netpage->index,
506 cachefiles_gfp);
9ae326a6
DH
507 if (ret == 0)
508 goto installed_new_backing_page;
509 if (ret != -EEXIST)
510 goto nomem;
511 }
512
55881bc7
JW
513 /* we've installed a new backing page, so now we need
514 * to start it reading */
9ae326a6
DH
515 installed_new_backing_page:
516 _debug("- new %p", newpage);
517
518 backpage = newpage;
519 newpage = NULL;
520
9ae326a6
DH
521 reread_backing_page:
522 ret = bmapping->a_ops->readpage(NULL, backpage);
523 if (ret < 0)
524 goto read_error;
525
526 /* add the netfs page to the pagecache and LRU, and set the
527 * monitor to transfer the data across */
528 monitor_backing_page:
529 _debug("- monitor add");
530
55881bc7
JW
531 ret = add_to_page_cache_lru(netpage, op->mapping,
532 netpage->index, cachefiles_gfp);
9ae326a6
DH
533 if (ret < 0) {
534 if (ret == -EEXIST) {
9a24ce5b
KKM
535 put_page(backpage);
536 backpage = NULL;
09cbfeaf 537 put_page(netpage);
9a24ce5b 538 netpage = NULL;
b4cf1e08 539 fscache_retrieval_complete(op, 1);
9ae326a6
DH
540 continue;
541 }
542 goto nomem;
543 }
544
9ae326a6 545 /* install a monitor */
09cbfeaf 546 get_page(netpage);
9ae326a6
DH
547 monitor->netfs_page = netpage;
548
09cbfeaf 549 get_page(backpage);
9ae326a6
DH
550 monitor->back_page = backpage;
551 monitor->monitor.private = backpage;
552 add_page_wait_queue(backpage, &monitor->monitor);
553 monitor = NULL;
554
555 /* but the page may have been read before the monitor was
556 * installed, so the monitor may miss the event - so we have to
557 * ensure that we do get one in such a case */
558 if (trylock_page(backpage)) {
559 _debug("2unlock %p {%lx}", backpage, backpage->flags);
560 unlock_page(backpage);
561 }
562
09cbfeaf 563 put_page(backpage);
9ae326a6
DH
564 backpage = NULL;
565
09cbfeaf 566 put_page(netpage);
9ae326a6
DH
567 netpage = NULL;
568 continue;
569
570 /* if the backing page is already present, it can be in one of
571 * three states: read in progress, read failed or read okay */
572 backing_page_already_present:
573 _debug("- present %p", backpage);
574
575 if (PageError(backpage))
576 goto io_error;
577
578 if (PageUptodate(backpage))
579 goto backing_page_already_uptodate;
580
581 _debug("- not ready %p{%lx}", backpage, backpage->flags);
582
583 if (!trylock_page(backpage))
584 goto monitor_backing_page;
585
586 if (PageError(backpage)) {
587 _debug("error %lx", backpage->flags);
588 unlock_page(backpage);
589 goto io_error;
590 }
591
592 if (PageUptodate(backpage))
593 goto backing_page_already_uptodate_unlock;
594
595 /* we've locked a page that's neither up to date nor erroneous,
596 * so we need to attempt to read it again */
597 goto reread_backing_page;
598
599 /* the backing page is already up to date, attach the netfs
600 * page to the pagecache and LRU and copy the data across */
601 backing_page_already_uptodate_unlock:
602 _debug("uptodate %lx", backpage->flags);
603 unlock_page(backpage);
604 backing_page_already_uptodate:
605 _debug("- uptodate");
606
55881bc7
JW
607 ret = add_to_page_cache_lru(netpage, op->mapping,
608 netpage->index, cachefiles_gfp);
9ae326a6
DH
609 if (ret < 0) {
610 if (ret == -EEXIST) {
9a24ce5b
KKM
611 put_page(backpage);
612 backpage = NULL;
09cbfeaf 613 put_page(netpage);
9a24ce5b 614 netpage = NULL;
b4cf1e08 615 fscache_retrieval_complete(op, 1);
9ae326a6
DH
616 continue;
617 }
618 goto nomem;
619 }
620
621 copy_highpage(netpage, backpage);
622
09cbfeaf 623 put_page(backpage);
9ae326a6
DH
624 backpage = NULL;
625
c4d6d8db 626 fscache_mark_page_cached(op, netpage);
9ae326a6 627
c4d6d8db 628 /* the netpage is unlocked and marked up to date here */
9ae326a6 629 fscache_end_io(op, netpage, 0);
09cbfeaf 630 put_page(netpage);
9ae326a6 631 netpage = NULL;
b4cf1e08 632 fscache_retrieval_complete(op, 1);
9ae326a6
DH
633 continue;
634 }
635
636 netpage = NULL;
637
638 _debug("out");
639
640out:
641 /* tidy up */
9ae326a6 642 if (newpage)
09cbfeaf 643 put_page(newpage);
9ae326a6 644 if (netpage)
09cbfeaf 645 put_page(netpage);
9ae326a6 646 if (backpage)
09cbfeaf 647 put_page(backpage);
9ae326a6
DH
648 if (monitor) {
649 fscache_put_retrieval(op);
650 kfree(monitor);
651 }
652
653 list_for_each_entry_safe(netpage, _n, list, lru) {
654 list_del(&netpage->lru);
09cbfeaf 655 put_page(netpage);
9f10523f 656 fscache_retrieval_complete(op, 1);
9ae326a6
DH
657 }
658
659 _leave(" = %d", ret);
660 return ret;
661
662nomem:
663 _debug("nomem");
664 ret = -ENOMEM;
b4cf1e08 665 goto record_page_complete;
9ae326a6
DH
666
667read_error:
668 _debug("read error %d", ret);
669 if (ret == -ENOMEM)
b4cf1e08 670 goto record_page_complete;
9ae326a6
DH
671io_error:
672 cachefiles_io_error_obj(object, "Page read error on backing file");
673 ret = -ENOBUFS;
b4cf1e08
DH
674record_page_complete:
675 fscache_retrieval_complete(op, 1);
9ae326a6
DH
676 goto out;
677}
678
679/*
680 * read a list of pages from the cache or allocate blocks in which to store
681 * them
682 */
683int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
684 struct list_head *pages,
685 unsigned *nr_pages,
686 gfp_t gfp)
687{
688 struct cachefiles_object *object;
689 struct cachefiles_cache *cache;
690 struct list_head backpages;
691 struct pagevec pagevec;
692 struct inode *inode;
693 struct page *page, *_n;
694 unsigned shift, nrbackpages;
695 int ret, ret2, space;
696
697 object = container_of(op->op.object,
698 struct cachefiles_object, fscache);
699 cache = container_of(object->fscache.cache,
700 struct cachefiles_cache, cache);
701
702 _enter("{OBJ%x,%d},,%d,,",
703 object->fscache.debug_id, atomic_read(&op->op.usage),
704 *nr_pages);
705
706 if (!object->backer)
9f10523f 707 goto all_enobufs;
9ae326a6
DH
708
709 space = 1;
710 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
711 space = 0;
712
466b77bc 713 inode = d_backing_inode(object->backer);
9ae326a6 714 ASSERT(S_ISREG(inode->i_mode));
9ae326a6
DH
715
716 /* calculate the shift required to use bmap */
9ae326a6
DH
717 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
718
86679820 719 pagevec_init(&pagevec);
9ae326a6 720
4fbf4291 721 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
8af7c124 722 op->op.flags |= FSCACHE_OP_ASYNC;
9ae326a6
DH
723 op->op.processor = cachefiles_read_copier;
724
725 INIT_LIST_HEAD(&backpages);
726 nrbackpages = 0;
727
728 ret = space ? -ENODATA : -ENOBUFS;
729 list_for_each_entry_safe(page, _n, pages, lru) {
10d83e11 730 sector_t block;
9ae326a6
DH
731
732 /* we assume the absence or presence of the first block is a
733 * good enough indication for the page as a whole
734 * - TODO: don't use bmap() for this as it is _not_ actually
735 * good enough for this as it doesn't indicate errors, but
736 * it's all we've got for the moment
737 */
10d83e11
CM
738 block = page->index;
739 block <<= shift;
740
c5f9d9db
DH
741 ret2 = bmap(inode, &block);
742 ASSERT(ret2 == 0);
9ae326a6 743
9ae326a6 744 _debug("%llx -> %llx",
10d83e11 745 (unsigned long long) (page->index << shift),
9ae326a6
DH
746 (unsigned long long) block);
747
748 if (block) {
749 /* we have data - add it to the list to give to the
750 * backing fs */
751 list_move(&page->lru, &backpages);
752 (*nr_pages)--;
753 nrbackpages++;
754 } else if (space && pagevec_add(&pagevec, page) == 0) {
755 fscache_mark_pages_cached(op, &pagevec);
9f10523f 756 fscache_retrieval_complete(op, 1);
9ae326a6 757 ret = -ENODATA;
9f10523f
DH
758 } else {
759 fscache_retrieval_complete(op, 1);
9ae326a6
DH
760 }
761 }
762
763 if (pagevec_count(&pagevec) > 0)
764 fscache_mark_pages_cached(op, &pagevec);
765
766 if (list_empty(pages))
767 ret = 0;
768
769 /* submit the apparently valid pages to the backing fs to be read from
770 * disk */
771 if (nrbackpages > 0) {
c4d6d8db 772 ret2 = cachefiles_read_backing_file(object, op, &backpages);
9ae326a6
DH
773 if (ret2 == -ENOMEM || ret2 == -EINTR)
774 ret = ret2;
775 }
776
9ae326a6
DH
777 _leave(" = %d [nr=%u%s]",
778 ret, *nr_pages, list_empty(pages) ? " empty" : "");
779 return ret;
9f10523f
DH
780
781all_enobufs:
782 fscache_retrieval_complete(op, *nr_pages);
783 return -ENOBUFS;
9ae326a6
DH
784}
785
786/*
787 * allocate a block in the cache in which to store a page
788 * - cache withdrawal is prevented by the caller
789 * - returns -EINTR if interrupted
790 * - returns -ENOMEM if ran out of memory
791 * - returns -ENOBUFS if no buffers can be made available
792 * - returns -ENOBUFS if page is beyond EOF
793 * - otherwise:
794 * - the metadata will be retained
795 * - 0 will be returned
796 */
797int cachefiles_allocate_page(struct fscache_retrieval *op,
798 struct page *page,
799 gfp_t gfp)
800{
801 struct cachefiles_object *object;
802 struct cachefiles_cache *cache;
9ae326a6
DH
803 int ret;
804
805 object = container_of(op->op.object,
806 struct cachefiles_object, fscache);
807 cache = container_of(object->fscache.cache,
808 struct cachefiles_cache, cache);
809
810 _enter("%p,{%lx},", object, page->index);
811
812 ret = cachefiles_has_space(cache, 0, 1);
c4d6d8db
DH
813 if (ret == 0)
814 fscache_mark_page_cached(op, page);
815 else
9ae326a6 816 ret = -ENOBUFS;
9ae326a6 817
9f10523f 818 fscache_retrieval_complete(op, 1);
9ae326a6
DH
819 _leave(" = %d", ret);
820 return ret;
821}
822
823/*
824 * allocate blocks in the cache in which to store a set of pages
825 * - cache withdrawal is prevented by the caller
826 * - returns -EINTR if interrupted
827 * - returns -ENOMEM if ran out of memory
828 * - returns -ENOBUFS if some buffers couldn't be made available
829 * - returns -ENOBUFS if some pages are beyond EOF
830 * - otherwise:
831 * - -ENODATA will be returned
832 * - metadata will be retained for any page marked
833 */
834int cachefiles_allocate_pages(struct fscache_retrieval *op,
835 struct list_head *pages,
836 unsigned *nr_pages,
837 gfp_t gfp)
838{
839 struct cachefiles_object *object;
840 struct cachefiles_cache *cache;
841 struct pagevec pagevec;
842 struct page *page;
843 int ret;
844
845 object = container_of(op->op.object,
846 struct cachefiles_object, fscache);
847 cache = container_of(object->fscache.cache,
848 struct cachefiles_cache, cache);
849
850 _enter("%p,,,%d,", object, *nr_pages);
851
852 ret = cachefiles_has_space(cache, 0, *nr_pages);
853 if (ret == 0) {
86679820 854 pagevec_init(&pagevec);
9ae326a6
DH
855
856 list_for_each_entry(page, pages, lru) {
857 if (pagevec_add(&pagevec, page) == 0)
858 fscache_mark_pages_cached(op, &pagevec);
859 }
860
861 if (pagevec_count(&pagevec) > 0)
862 fscache_mark_pages_cached(op, &pagevec);
863 ret = -ENODATA;
864 } else {
865 ret = -ENOBUFS;
866 }
867
9f10523f 868 fscache_retrieval_complete(op, *nr_pages);
9ae326a6
DH
869 _leave(" = %d", ret);
870 return ret;
871}
872
873/*
874 * request a page be stored in the cache
875 * - cache withdrawal is prevented by the caller
876 * - this request may be ignored if there's no cache block available, in which
877 * case -ENOBUFS will be returned
878 * - if the op is in progress, 0 will be returned
879 */
880int cachefiles_write_page(struct fscache_storage *op, struct page *page)
881{
882 struct cachefiles_object *object;
883 struct cachefiles_cache *cache;
9ae326a6 884 struct file *file;
765927b2 885 struct path path;
a17754fb
DH
886 loff_t pos, eof;
887 size_t len;
9ae326a6 888 void *data;
cf897526 889 int ret = -ENOBUFS;
9ae326a6
DH
890
891 ASSERT(op != NULL);
892 ASSERT(page != NULL);
893
894 object = container_of(op->op.object,
895 struct cachefiles_object, fscache);
896
897 _enter("%p,%p{%lx},,,", object, page, page->index);
898
899 if (!object->backer) {
900 _leave(" = -ENOBUFS");
901 return -ENOBUFS;
902 }
903
ce40fa78 904 ASSERT(d_is_reg(object->backer));
9ae326a6
DH
905
906 cache = container_of(object->fscache.cache,
907 struct cachefiles_cache, cache);
908
102f4d90
DH
909 pos = (loff_t)page->index << PAGE_SHIFT;
910
911 /* We mustn't write more data than we have, so we have to beware of a
912 * partial page at EOF.
913 */
914 eof = object->fscache.store_limit_l;
915 if (pos >= eof)
916 goto error;
917
9ae326a6
DH
918 /* write the page to the backing filesystem and let it store it in its
919 * own time */
765927b2
AV
920 path.mnt = cache->mnt;
921 path.dentry = object->backer;
98c350cd 922 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
9ae326a6
DH
923 if (IS_ERR(file)) {
924 ret = PTR_ERR(file);
102f4d90 925 goto error_2;
9ae326a6
DH
926 }
927
102f4d90
DH
928 len = PAGE_SIZE;
929 if (eof & ~PAGE_MASK) {
930 if (eof - pos < PAGE_SIZE) {
931 _debug("cut short %llx to %llx",
932 pos, eof);
933 len = eof - pos;
934 ASSERTCMP(pos + len, ==, eof);
935 }
9ae326a6
DH
936 }
937
102f4d90 938 data = kmap(page);
97c7990c 939 ret = kernel_write(file, data, len, &pos);
102f4d90
DH
940 kunmap(page);
941 fput(file);
942 if (ret != len)
943 goto error_eio;
944
945 _leave(" = 0");
946 return 0;
947
948error_eio:
949 ret = -EIO;
950error_2:
951 if (ret == -EIO)
952 cachefiles_io_error_obj(object,
953 "Write page to backing file failed");
954error:
955 _leave(" = -ENOBUFS [%d]", ret);
956 return -ENOBUFS;
9ae326a6
DH
957}
958
959/*
960 * detach a backing block from a page
961 * - cache withdrawal is prevented by the caller
962 */
963void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
bfa3837e 964 __releases(&object->fscache.cookie->lock)
9ae326a6
DH
965{
966 struct cachefiles_object *object;
9ae326a6
DH
967
968 object = container_of(_object, struct cachefiles_object, fscache);
9ae326a6
DH
969
970 _enter("%p,{%lu}", object, page->index);
971
972 spin_unlock(&object->fscache.cookie->lock);
973}