]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/cachefiles/rdwr.c
ubifs: Reject unsupported ioctl flags explicitly
[mirror_ubuntu-bionic-kernel.git] / fs / cachefiles / rdwr.c
CommitLineData
9ae326a6
DH
1/* Storage object read/write
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/mount.h>
5a0e3ad6 13#include <linux/slab.h>
9ae326a6 14#include <linux/file.h>
a0b8cab3 15#include <linux/swap.h>
9ae326a6
DH
16#include "internal.h"
17
18/*
19 * detect wake up events generated by the unlocking of pages in which we're
20 * interested
21 * - we use this to detect read completion of backing pages
22 * - the caller holds the waitqueue lock
23 */
ac6424b9 24static int cachefiles_read_waiter(wait_queue_entry_t *wait, unsigned mode,
9ae326a6
DH
25 int sync, void *_key)
26{
27 struct cachefiles_one_read *monitor =
28 container_of(wait, struct cachefiles_one_read, monitor);
29 struct cachefiles_object *object;
7bf1efb0 30 struct fscache_retrieval *op = monitor->op;
9ae326a6
DH
31 struct wait_bit_key *key = _key;
32 struct page *page = wait->private;
33
34 ASSERT(key);
35
36 _enter("{%lu},%u,%d,{%p,%u}",
37 monitor->netfs_page->index, mode, sync,
38 key->flags, key->bit_nr);
39
40 if (key->flags != &page->flags ||
41 key->bit_nr != PG_locked)
42 return 0;
43
44 _debug("--- monitor %p %lx ---", page, page->flags);
45
5e929b33
DH
46 if (!PageUptodate(page) && !PageError(page)) {
47 /* unlocked, not uptodate and not erronous? */
48 _debug("page probably truncated");
49 }
9ae326a6
DH
50
51 /* remove from the waitqueue */
2055da97 52 list_del(&wait->entry);
9ae326a6
DH
53
54 /* move onto the action list and queue for FS-Cache thread pool */
7bf1efb0 55 ASSERT(op);
9ae326a6 56
7bf1efb0
KKM
57 /* We need to temporarily bump the usage count as we don't own a ref
58 * here otherwise cachefiles_read_copier() may free the op between the
59 * monitor being enqueued on the op->to_do list and the op getting
60 * enqueued on the work queue.
61 */
62 fscache_get_retrieval(op);
9ae326a6 63
7bf1efb0 64 object = container_of(op->op.object, struct cachefiles_object, fscache);
9ae326a6 65 spin_lock(&object->work_lock);
7bf1efb0 66 list_add_tail(&monitor->op_link, &op->to_do);
9ae326a6
DH
67 spin_unlock(&object->work_lock);
68
7bf1efb0
KKM
69 fscache_enqueue_retrieval(op);
70 fscache_put_retrieval(op);
9ae326a6
DH
71 return 0;
72}
73
5e929b33
DH
74/*
75 * handle a probably truncated page
76 * - check to see if the page is still relevant and reissue the read if
77 * possible
78 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
79 * must wait again and 0 if successful
80 */
81static int cachefiles_read_reissue(struct cachefiles_object *object,
82 struct cachefiles_one_read *monitor)
83{
466b77bc 84 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
5e929b33
DH
85 struct page *backpage = monitor->back_page, *backpage2;
86 int ret;
87
37491a13 88 _enter("{ino=%lx},{%lx,%lx}",
466b77bc 89 d_backing_inode(object->backer)->i_ino,
5e929b33
DH
90 backpage->index, backpage->flags);
91
92 /* skip if the page was truncated away completely */
93 if (backpage->mapping != bmapping) {
37491a13 94 _leave(" = -ENODATA [mapping]");
5e929b33
DH
95 return -ENODATA;
96 }
97
98 backpage2 = find_get_page(bmapping, backpage->index);
99 if (!backpage2) {
37491a13 100 _leave(" = -ENODATA [gone]");
5e929b33
DH
101 return -ENODATA;
102 }
103
104 if (backpage != backpage2) {
105 put_page(backpage2);
37491a13 106 _leave(" = -ENODATA [different]");
5e929b33
DH
107 return -ENODATA;
108 }
109
110 /* the page is still there and we already have a ref on it, so we don't
111 * need a second */
112 put_page(backpage2);
113
114 INIT_LIST_HEAD(&monitor->op_link);
115 add_page_wait_queue(backpage, &monitor->monitor);
116
117 if (trylock_page(backpage)) {
118 ret = -EIO;
119 if (PageError(backpage))
120 goto unlock_discard;
121 ret = 0;
122 if (PageUptodate(backpage))
123 goto unlock_discard;
124
37491a13 125 _debug("reissue read");
5e929b33
DH
126 ret = bmapping->a_ops->readpage(NULL, backpage);
127 if (ret < 0)
128 goto unlock_discard;
129 }
130
131 /* but the page may have been read before the monitor was installed, so
132 * the monitor may miss the event - so we have to ensure that we do get
133 * one in such a case */
134 if (trylock_page(backpage)) {
135 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
136 unlock_page(backpage);
137 }
138
139 /* it'll reappear on the todo list */
37491a13 140 _leave(" = -EINPROGRESS");
5e929b33
DH
141 return -EINPROGRESS;
142
143unlock_discard:
144 unlock_page(backpage);
145 spin_lock_irq(&object->work_lock);
146 list_del(&monitor->op_link);
147 spin_unlock_irq(&object->work_lock);
37491a13 148 _leave(" = %d", ret);
5e929b33
DH
149 return ret;
150}
151
9ae326a6
DH
152/*
153 * copy data from backing pages to netfs pages to complete a read operation
154 * - driven by FS-Cache's thread pool
155 */
156static void cachefiles_read_copier(struct fscache_operation *_op)
157{
158 struct cachefiles_one_read *monitor;
159 struct cachefiles_object *object;
160 struct fscache_retrieval *op;
9ae326a6
DH
161 int error, max;
162
163 op = container_of(_op, struct fscache_retrieval, op);
164 object = container_of(op->op.object,
165 struct cachefiles_object, fscache);
166
466b77bc 167 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
9ae326a6 168
9ae326a6
DH
169 max = 8;
170 spin_lock_irq(&object->work_lock);
171
172 while (!list_empty(&op->to_do)) {
173 monitor = list_entry(op->to_do.next,
174 struct cachefiles_one_read, op_link);
175 list_del(&monitor->op_link);
176
177 spin_unlock_irq(&object->work_lock);
178
179 _debug("- copy {%lu}", monitor->back_page->index);
180
5e929b33 181 recheck:
9dc8d9bf
DH
182 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
183 &object->fscache.cookie->flags)) {
184 error = -ESTALE;
185 } else if (PageUptodate(monitor->back_page)) {
9ae326a6 186 copy_highpage(monitor->netfs_page, monitor->back_page);
c4d6d8db
DH
187 fscache_mark_page_cached(monitor->op,
188 monitor->netfs_page);
9ae326a6 189 error = 0;
5e929b33
DH
190 } else if (!PageError(monitor->back_page)) {
191 /* the page has probably been truncated */
192 error = cachefiles_read_reissue(object, monitor);
193 if (error == -EINPROGRESS)
194 goto next;
195 goto recheck;
196 } else {
9ae326a6
DH
197 cachefiles_io_error_obj(
198 object,
199 "Readpage failed on backing file %lx",
200 (unsigned long) monitor->back_page->flags);
5e929b33
DH
201 error = -EIO;
202 }
9ae326a6 203
09cbfeaf 204 put_page(monitor->back_page);
9ae326a6
DH
205
206 fscache_end_io(op, monitor->netfs_page, error);
09cbfeaf 207 put_page(monitor->netfs_page);
9f10523f 208 fscache_retrieval_complete(op, 1);
9ae326a6
DH
209 fscache_put_retrieval(op);
210 kfree(monitor);
211
5e929b33 212 next:
9ae326a6
DH
213 /* let the thread pool have some air occasionally */
214 max--;
215 if (max < 0 || need_resched()) {
216 if (!list_empty(&op->to_do))
217 fscache_enqueue_retrieval(op);
218 _leave(" [maxed out]");
219 return;
220 }
221
222 spin_lock_irq(&object->work_lock);
223 }
224
225 spin_unlock_irq(&object->work_lock);
226 _leave("");
227}
228
229/*
230 * read the corresponding page to the given set from the backing file
231 * - an uncertain page is simply discarded, to be tried again another time
232 */
233static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
234 struct fscache_retrieval *op,
a0b8cab3 235 struct page *netpage)
9ae326a6
DH
236{
237 struct cachefiles_one_read *monitor;
238 struct address_space *bmapping;
239 struct page *newpage, *backpage;
240 int ret;
241
242 _enter("");
243
9ae326a6
DH
244 _debug("read back %p{%lu,%d}",
245 netpage, netpage->index, page_count(netpage));
246
5f4f9f4a 247 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
9ae326a6
DH
248 if (!monitor)
249 goto nomem;
250
251 monitor->netfs_page = netpage;
252 monitor->op = fscache_get_retrieval(op);
253
254 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
255
256 /* attempt to get hold of the backing page */
466b77bc 257 bmapping = d_backing_inode(object->backer)->i_mapping;
9ae326a6
DH
258 newpage = NULL;
259
260 for (;;) {
261 backpage = find_get_page(bmapping, netpage->index);
262 if (backpage)
263 goto backing_page_already_present;
264
265 if (!newpage) {
453f85d4 266 newpage = __page_cache_alloc(cachefiles_gfp);
9ae326a6
DH
267 if (!newpage)
268 goto nomem_monitor;
269 }
270
55881bc7
JW
271 ret = add_to_page_cache_lru(newpage, bmapping,
272 netpage->index, cachefiles_gfp);
9ae326a6
DH
273 if (ret == 0)
274 goto installed_new_backing_page;
275 if (ret != -EEXIST)
276 goto nomem_page;
277 }
278
55881bc7
JW
279 /* we've installed a new backing page, so now we need to start
280 * it reading */
9ae326a6
DH
281installed_new_backing_page:
282 _debug("- new %p", newpage);
283
284 backpage = newpage;
285 newpage = NULL;
286
9ae326a6
DH
287read_backing_page:
288 ret = bmapping->a_ops->readpage(NULL, backpage);
289 if (ret < 0)
290 goto read_error;
291
292 /* set the monitor to transfer the data across */
293monitor_backing_page:
294 _debug("- monitor add");
295
296 /* install the monitor */
09cbfeaf
KS
297 get_page(monitor->netfs_page);
298 get_page(backpage);
9ae326a6
DH
299 monitor->back_page = backpage;
300 monitor->monitor.private = backpage;
301 add_page_wait_queue(backpage, &monitor->monitor);
302 monitor = NULL;
303
304 /* but the page may have been read before the monitor was installed, so
305 * the monitor may miss the event - so we have to ensure that we do get
306 * one in such a case */
307 if (trylock_page(backpage)) {
308 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
309 unlock_page(backpage);
310 }
311 goto success;
312
313 /* if the backing page is already present, it can be in one of
314 * three states: read in progress, read failed or read okay */
315backing_page_already_present:
316 _debug("- present");
317
318 if (newpage) {
09cbfeaf 319 put_page(newpage);
9ae326a6
DH
320 newpage = NULL;
321 }
322
323 if (PageError(backpage))
324 goto io_error;
325
326 if (PageUptodate(backpage))
327 goto backing_page_already_uptodate;
328
329 if (!trylock_page(backpage))
330 goto monitor_backing_page;
331 _debug("read %p {%lx}", backpage, backpage->flags);
332 goto read_backing_page;
333
334 /* the backing page is already up to date, attach the netfs
335 * page to the pagecache and LRU and copy the data across */
336backing_page_already_uptodate:
337 _debug("- uptodate");
338
c4d6d8db 339 fscache_mark_page_cached(op, netpage);
9ae326a6
DH
340
341 copy_highpage(netpage, backpage);
342 fscache_end_io(op, netpage, 0);
9f10523f 343 fscache_retrieval_complete(op, 1);
9ae326a6
DH
344
345success:
346 _debug("success");
347 ret = 0;
348
349out:
350 if (backpage)
09cbfeaf 351 put_page(backpage);
9ae326a6
DH
352 if (monitor) {
353 fscache_put_retrieval(monitor->op);
354 kfree(monitor);
355 }
356 _leave(" = %d", ret);
357 return ret;
358
359read_error:
360 _debug("read error %d", ret);
b4cf1e08
DH
361 if (ret == -ENOMEM) {
362 fscache_retrieval_complete(op, 1);
9ae326a6 363 goto out;
b4cf1e08 364 }
9ae326a6
DH
365io_error:
366 cachefiles_io_error_obj(object, "Page read error on backing file");
9f10523f 367 fscache_retrieval_complete(op, 1);
9ae326a6
DH
368 ret = -ENOBUFS;
369 goto out;
370
371nomem_page:
09cbfeaf 372 put_page(newpage);
9ae326a6
DH
373nomem_monitor:
374 fscache_put_retrieval(monitor->op);
375 kfree(monitor);
376nomem:
9f10523f 377 fscache_retrieval_complete(op, 1);
9ae326a6
DH
378 _leave(" = -ENOMEM");
379 return -ENOMEM;
380}
381
382/*
383 * read a page from the cache or allocate a block in which to store it
384 * - cache withdrawal is prevented by the caller
385 * - returns -EINTR if interrupted
386 * - returns -ENOMEM if ran out of memory
387 * - returns -ENOBUFS if no buffers can be made available
388 * - returns -ENOBUFS if page is beyond EOF
389 * - if the page is backed by a block in the cache:
390 * - a read will be started which will call the callback on completion
391 * - 0 will be returned
392 * - else if the page is unbacked:
393 * - the metadata will be retained
394 * - -ENODATA will be returned
395 */
396int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
397 struct page *page,
398 gfp_t gfp)
399{
400 struct cachefiles_object *object;
401 struct cachefiles_cache *cache;
9ae326a6
DH
402 struct inode *inode;
403 sector_t block0, block;
404 unsigned shift;
405 int ret;
406
407 object = container_of(op->op.object,
408 struct cachefiles_object, fscache);
409 cache = container_of(object->fscache.cache,
410 struct cachefiles_cache, cache);
411
412 _enter("{%p},{%lx},,,", object, page->index);
413
414 if (!object->backer)
9f10523f 415 goto enobufs;
9ae326a6 416
466b77bc 417 inode = d_backing_inode(object->backer);
9ae326a6
DH
418 ASSERT(S_ISREG(inode->i_mode));
419 ASSERT(inode->i_mapping->a_ops->bmap);
420 ASSERT(inode->i_mapping->a_ops->readpages);
421
422 /* calculate the shift required to use bmap */
9ae326a6
DH
423 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
424
4fbf4291 425 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
8af7c124 426 op->op.flags |= FSCACHE_OP_ASYNC;
9ae326a6
DH
427 op->op.processor = cachefiles_read_copier;
428
9ae326a6
DH
429 /* we assume the absence or presence of the first block is a good
430 * enough indication for the page as a whole
431 * - TODO: don't use bmap() for this as it is _not_ actually good
432 * enough for this as it doesn't indicate errors, but it's all we've
433 * got for the moment
434 */
435 block0 = page->index;
436 block0 <<= shift;
437
438 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
439 _debug("%llx -> %llx",
440 (unsigned long long) block0,
441 (unsigned long long) block);
442
443 if (block) {
444 /* submit the apparently valid page to the backing fs to be
445 * read from disk */
a0b8cab3 446 ret = cachefiles_read_backing_file_one(object, op, page);
9ae326a6
DH
447 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
448 /* there's space in the cache we can use */
c4d6d8db 449 fscache_mark_page_cached(op, page);
9f10523f 450 fscache_retrieval_complete(op, 1);
9ae326a6
DH
451 ret = -ENODATA;
452 } else {
9f10523f 453 goto enobufs;
9ae326a6
DH
454 }
455
456 _leave(" = %d", ret);
457 return ret;
9f10523f
DH
458
459enobufs:
460 fscache_retrieval_complete(op, 1);
461 _leave(" = -ENOBUFS");
462 return -ENOBUFS;
9ae326a6
DH
463}
464
465/*
466 * read the corresponding pages to the given set from the backing file
467 * - any uncertain pages are simply discarded, to be tried again another time
468 */
469static int cachefiles_read_backing_file(struct cachefiles_object *object,
470 struct fscache_retrieval *op,
c4d6d8db 471 struct list_head *list)
9ae326a6
DH
472{
473 struct cachefiles_one_read *monitor = NULL;
466b77bc 474 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
9ae326a6
DH
475 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
476 int ret = 0;
477
478 _enter("");
479
9ae326a6
DH
480 list_for_each_entry_safe(netpage, _n, list, lru) {
481 list_del(&netpage->lru);
482
483 _debug("read back %p{%lu,%d}",
484 netpage, netpage->index, page_count(netpage));
485
486 if (!monitor) {
5f4f9f4a 487 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
9ae326a6
DH
488 if (!monitor)
489 goto nomem;
490
491 monitor->op = fscache_get_retrieval(op);
492 init_waitqueue_func_entry(&monitor->monitor,
493 cachefiles_read_waiter);
494 }
495
496 for (;;) {
497 backpage = find_get_page(bmapping, netpage->index);
498 if (backpage)
499 goto backing_page_already_present;
500
501 if (!newpage) {
453f85d4 502 newpage = __page_cache_alloc(cachefiles_gfp);
9ae326a6
DH
503 if (!newpage)
504 goto nomem;
505 }
506
55881bc7
JW
507 ret = add_to_page_cache_lru(newpage, bmapping,
508 netpage->index,
509 cachefiles_gfp);
9ae326a6
DH
510 if (ret == 0)
511 goto installed_new_backing_page;
512 if (ret != -EEXIST)
513 goto nomem;
3f467f61
KKM
514 put_page(newpage);
515 newpage = NULL;
9ae326a6
DH
516 }
517
55881bc7
JW
518 /* we've installed a new backing page, so now we need
519 * to start it reading */
9ae326a6
DH
520 installed_new_backing_page:
521 _debug("- new %p", newpage);
522
523 backpage = newpage;
524 newpage = NULL;
525
9ae326a6
DH
526 reread_backing_page:
527 ret = bmapping->a_ops->readpage(NULL, backpage);
528 if (ret < 0)
529 goto read_error;
530
531 /* add the netfs page to the pagecache and LRU, and set the
532 * monitor to transfer the data across */
533 monitor_backing_page:
534 _debug("- monitor add");
535
55881bc7
JW
536 ret = add_to_page_cache_lru(netpage, op->mapping,
537 netpage->index, cachefiles_gfp);
9ae326a6
DH
538 if (ret < 0) {
539 if (ret == -EEXIST) {
3f467f61
KKM
540 put_page(backpage);
541 backpage = NULL;
09cbfeaf 542 put_page(netpage);
3f467f61 543 netpage = NULL;
b4cf1e08 544 fscache_retrieval_complete(op, 1);
9ae326a6
DH
545 continue;
546 }
547 goto nomem;
548 }
549
9ae326a6 550 /* install a monitor */
09cbfeaf 551 get_page(netpage);
9ae326a6
DH
552 monitor->netfs_page = netpage;
553
09cbfeaf 554 get_page(backpage);
9ae326a6
DH
555 monitor->back_page = backpage;
556 monitor->monitor.private = backpage;
557 add_page_wait_queue(backpage, &monitor->monitor);
558 monitor = NULL;
559
560 /* but the page may have been read before the monitor was
561 * installed, so the monitor may miss the event - so we have to
562 * ensure that we do get one in such a case */
563 if (trylock_page(backpage)) {
564 _debug("2unlock %p {%lx}", backpage, backpage->flags);
565 unlock_page(backpage);
566 }
567
09cbfeaf 568 put_page(backpage);
9ae326a6
DH
569 backpage = NULL;
570
09cbfeaf 571 put_page(netpage);
9ae326a6
DH
572 netpage = NULL;
573 continue;
574
575 /* if the backing page is already present, it can be in one of
576 * three states: read in progress, read failed or read okay */
577 backing_page_already_present:
578 _debug("- present %p", backpage);
579
580 if (PageError(backpage))
581 goto io_error;
582
583 if (PageUptodate(backpage))
584 goto backing_page_already_uptodate;
585
586 _debug("- not ready %p{%lx}", backpage, backpage->flags);
587
588 if (!trylock_page(backpage))
589 goto monitor_backing_page;
590
591 if (PageError(backpage)) {
592 _debug("error %lx", backpage->flags);
593 unlock_page(backpage);
594 goto io_error;
595 }
596
597 if (PageUptodate(backpage))
598 goto backing_page_already_uptodate_unlock;
599
600 /* we've locked a page that's neither up to date nor erroneous,
601 * so we need to attempt to read it again */
602 goto reread_backing_page;
603
604 /* the backing page is already up to date, attach the netfs
605 * page to the pagecache and LRU and copy the data across */
606 backing_page_already_uptodate_unlock:
607 _debug("uptodate %lx", backpage->flags);
608 unlock_page(backpage);
609 backing_page_already_uptodate:
610 _debug("- uptodate");
611
55881bc7
JW
612 ret = add_to_page_cache_lru(netpage, op->mapping,
613 netpage->index, cachefiles_gfp);
9ae326a6
DH
614 if (ret < 0) {
615 if (ret == -EEXIST) {
3f467f61
KKM
616 put_page(backpage);
617 backpage = NULL;
09cbfeaf 618 put_page(netpage);
3f467f61 619 netpage = NULL;
b4cf1e08 620 fscache_retrieval_complete(op, 1);
9ae326a6
DH
621 continue;
622 }
623 goto nomem;
624 }
625
626 copy_highpage(netpage, backpage);
627
09cbfeaf 628 put_page(backpage);
9ae326a6
DH
629 backpage = NULL;
630
c4d6d8db 631 fscache_mark_page_cached(op, netpage);
9ae326a6 632
c4d6d8db 633 /* the netpage is unlocked and marked up to date here */
9ae326a6 634 fscache_end_io(op, netpage, 0);
09cbfeaf 635 put_page(netpage);
9ae326a6 636 netpage = NULL;
b4cf1e08 637 fscache_retrieval_complete(op, 1);
9ae326a6
DH
638 continue;
639 }
640
641 netpage = NULL;
642
643 _debug("out");
644
645out:
646 /* tidy up */
9ae326a6 647 if (newpage)
09cbfeaf 648 put_page(newpage);
9ae326a6 649 if (netpage)
09cbfeaf 650 put_page(netpage);
9ae326a6 651 if (backpage)
09cbfeaf 652 put_page(backpage);
9ae326a6
DH
653 if (monitor) {
654 fscache_put_retrieval(op);
655 kfree(monitor);
656 }
657
658 list_for_each_entry_safe(netpage, _n, list, lru) {
659 list_del(&netpage->lru);
09cbfeaf 660 put_page(netpage);
9f10523f 661 fscache_retrieval_complete(op, 1);
9ae326a6
DH
662 }
663
664 _leave(" = %d", ret);
665 return ret;
666
667nomem:
668 _debug("nomem");
669 ret = -ENOMEM;
b4cf1e08 670 goto record_page_complete;
9ae326a6
DH
671
672read_error:
673 _debug("read error %d", ret);
674 if (ret == -ENOMEM)
b4cf1e08 675 goto record_page_complete;
9ae326a6
DH
676io_error:
677 cachefiles_io_error_obj(object, "Page read error on backing file");
678 ret = -ENOBUFS;
b4cf1e08
DH
679record_page_complete:
680 fscache_retrieval_complete(op, 1);
9ae326a6
DH
681 goto out;
682}
683
684/*
685 * read a list of pages from the cache or allocate blocks in which to store
686 * them
687 */
688int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
689 struct list_head *pages,
690 unsigned *nr_pages,
691 gfp_t gfp)
692{
693 struct cachefiles_object *object;
694 struct cachefiles_cache *cache;
695 struct list_head backpages;
696 struct pagevec pagevec;
697 struct inode *inode;
698 struct page *page, *_n;
699 unsigned shift, nrbackpages;
700 int ret, ret2, space;
701
702 object = container_of(op->op.object,
703 struct cachefiles_object, fscache);
704 cache = container_of(object->fscache.cache,
705 struct cachefiles_cache, cache);
706
707 _enter("{OBJ%x,%d},,%d,,",
708 object->fscache.debug_id, atomic_read(&op->op.usage),
709 *nr_pages);
710
711 if (!object->backer)
9f10523f 712 goto all_enobufs;
9ae326a6
DH
713
714 space = 1;
715 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
716 space = 0;
717
466b77bc 718 inode = d_backing_inode(object->backer);
9ae326a6
DH
719 ASSERT(S_ISREG(inode->i_mode));
720 ASSERT(inode->i_mapping->a_ops->bmap);
721 ASSERT(inode->i_mapping->a_ops->readpages);
722
723 /* calculate the shift required to use bmap */
9ae326a6
DH
724 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
725
86679820 726 pagevec_init(&pagevec);
9ae326a6 727
4fbf4291 728 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
8af7c124 729 op->op.flags |= FSCACHE_OP_ASYNC;
9ae326a6
DH
730 op->op.processor = cachefiles_read_copier;
731
732 INIT_LIST_HEAD(&backpages);
733 nrbackpages = 0;
734
735 ret = space ? -ENODATA : -ENOBUFS;
736 list_for_each_entry_safe(page, _n, pages, lru) {
737 sector_t block0, block;
738
739 /* we assume the absence or presence of the first block is a
740 * good enough indication for the page as a whole
741 * - TODO: don't use bmap() for this as it is _not_ actually
742 * good enough for this as it doesn't indicate errors, but
743 * it's all we've got for the moment
744 */
745 block0 = page->index;
746 block0 <<= shift;
747
748 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
749 block0);
750 _debug("%llx -> %llx",
751 (unsigned long long) block0,
752 (unsigned long long) block);
753
754 if (block) {
755 /* we have data - add it to the list to give to the
756 * backing fs */
757 list_move(&page->lru, &backpages);
758 (*nr_pages)--;
759 nrbackpages++;
760 } else if (space && pagevec_add(&pagevec, page) == 0) {
761 fscache_mark_pages_cached(op, &pagevec);
9f10523f 762 fscache_retrieval_complete(op, 1);
9ae326a6 763 ret = -ENODATA;
9f10523f
DH
764 } else {
765 fscache_retrieval_complete(op, 1);
9ae326a6
DH
766 }
767 }
768
769 if (pagevec_count(&pagevec) > 0)
770 fscache_mark_pages_cached(op, &pagevec);
771
772 if (list_empty(pages))
773 ret = 0;
774
775 /* submit the apparently valid pages to the backing fs to be read from
776 * disk */
777 if (nrbackpages > 0) {
c4d6d8db 778 ret2 = cachefiles_read_backing_file(object, op, &backpages);
9ae326a6
DH
779 if (ret2 == -ENOMEM || ret2 == -EINTR)
780 ret = ret2;
781 }
782
9ae326a6
DH
783 _leave(" = %d [nr=%u%s]",
784 ret, *nr_pages, list_empty(pages) ? " empty" : "");
785 return ret;
9f10523f
DH
786
787all_enobufs:
788 fscache_retrieval_complete(op, *nr_pages);
789 return -ENOBUFS;
9ae326a6
DH
790}
791
792/*
793 * allocate a block in the cache in which to store a page
794 * - cache withdrawal is prevented by the caller
795 * - returns -EINTR if interrupted
796 * - returns -ENOMEM if ran out of memory
797 * - returns -ENOBUFS if no buffers can be made available
798 * - returns -ENOBUFS if page is beyond EOF
799 * - otherwise:
800 * - the metadata will be retained
801 * - 0 will be returned
802 */
803int cachefiles_allocate_page(struct fscache_retrieval *op,
804 struct page *page,
805 gfp_t gfp)
806{
807 struct cachefiles_object *object;
808 struct cachefiles_cache *cache;
9ae326a6
DH
809 int ret;
810
811 object = container_of(op->op.object,
812 struct cachefiles_object, fscache);
813 cache = container_of(object->fscache.cache,
814 struct cachefiles_cache, cache);
815
816 _enter("%p,{%lx},", object, page->index);
817
818 ret = cachefiles_has_space(cache, 0, 1);
c4d6d8db
DH
819 if (ret == 0)
820 fscache_mark_page_cached(op, page);
821 else
9ae326a6 822 ret = -ENOBUFS;
9ae326a6 823
9f10523f 824 fscache_retrieval_complete(op, 1);
9ae326a6
DH
825 _leave(" = %d", ret);
826 return ret;
827}
828
829/*
830 * allocate blocks in the cache in which to store a set of pages
831 * - cache withdrawal is prevented by the caller
832 * - returns -EINTR if interrupted
833 * - returns -ENOMEM if ran out of memory
834 * - returns -ENOBUFS if some buffers couldn't be made available
835 * - returns -ENOBUFS if some pages are beyond EOF
836 * - otherwise:
837 * - -ENODATA will be returned
838 * - metadata will be retained for any page marked
839 */
840int cachefiles_allocate_pages(struct fscache_retrieval *op,
841 struct list_head *pages,
842 unsigned *nr_pages,
843 gfp_t gfp)
844{
845 struct cachefiles_object *object;
846 struct cachefiles_cache *cache;
847 struct pagevec pagevec;
848 struct page *page;
849 int ret;
850
851 object = container_of(op->op.object,
852 struct cachefiles_object, fscache);
853 cache = container_of(object->fscache.cache,
854 struct cachefiles_cache, cache);
855
856 _enter("%p,,,%d,", object, *nr_pages);
857
858 ret = cachefiles_has_space(cache, 0, *nr_pages);
859 if (ret == 0) {
86679820 860 pagevec_init(&pagevec);
9ae326a6
DH
861
862 list_for_each_entry(page, pages, lru) {
863 if (pagevec_add(&pagevec, page) == 0)
864 fscache_mark_pages_cached(op, &pagevec);
865 }
866
867 if (pagevec_count(&pagevec) > 0)
868 fscache_mark_pages_cached(op, &pagevec);
869 ret = -ENODATA;
870 } else {
871 ret = -ENOBUFS;
872 }
873
9f10523f 874 fscache_retrieval_complete(op, *nr_pages);
9ae326a6
DH
875 _leave(" = %d", ret);
876 return ret;
877}
878
879/*
880 * request a page be stored in the cache
881 * - cache withdrawal is prevented by the caller
882 * - this request may be ignored if there's no cache block available, in which
883 * case -ENOBUFS will be returned
884 * - if the op is in progress, 0 will be returned
885 */
886int cachefiles_write_page(struct fscache_storage *op, struct page *page)
887{
888 struct cachefiles_object *object;
889 struct cachefiles_cache *cache;
9ae326a6 890 struct file *file;
765927b2 891 struct path path;
a17754fb
DH
892 loff_t pos, eof;
893 size_t len;
9ae326a6 894 void *data;
cf897526 895 int ret = -ENOBUFS;
9ae326a6
DH
896
897 ASSERT(op != NULL);
898 ASSERT(page != NULL);
899
900 object = container_of(op->op.object,
901 struct cachefiles_object, fscache);
902
903 _enter("%p,%p{%lx},,,", object, page, page->index);
904
905 if (!object->backer) {
906 _leave(" = -ENOBUFS");
907 return -ENOBUFS;
908 }
909
ce40fa78 910 ASSERT(d_is_reg(object->backer));
9ae326a6
DH
911
912 cache = container_of(object->fscache.cache,
913 struct cachefiles_cache, cache);
914
102f4d90
DH
915 pos = (loff_t)page->index << PAGE_SHIFT;
916
917 /* We mustn't write more data than we have, so we have to beware of a
918 * partial page at EOF.
919 */
920 eof = object->fscache.store_limit_l;
921 if (pos >= eof)
922 goto error;
923
9ae326a6
DH
924 /* write the page to the backing filesystem and let it store it in its
925 * own time */
765927b2
AV
926 path.mnt = cache->mnt;
927 path.dentry = object->backer;
98c350cd 928 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
9ae326a6
DH
929 if (IS_ERR(file)) {
930 ret = PTR_ERR(file);
102f4d90 931 goto error_2;
9ae326a6
DH
932 }
933
102f4d90
DH
934 len = PAGE_SIZE;
935 if (eof & ~PAGE_MASK) {
936 if (eof - pos < PAGE_SIZE) {
937 _debug("cut short %llx to %llx",
938 pos, eof);
939 len = eof - pos;
940 ASSERTCMP(pos + len, ==, eof);
941 }
9ae326a6
DH
942 }
943
102f4d90
DH
944 data = kmap(page);
945 ret = __kernel_write(file, data, len, &pos);
946 kunmap(page);
947 fput(file);
948 if (ret != len)
949 goto error_eio;
950
951 _leave(" = 0");
952 return 0;
953
954error_eio:
955 ret = -EIO;
956error_2:
957 if (ret == -EIO)
958 cachefiles_io_error_obj(object,
959 "Write page to backing file failed");
960error:
961 _leave(" = -ENOBUFS [%d]", ret);
962 return -ENOBUFS;
9ae326a6
DH
963}
964
965/*
966 * detach a backing block from a page
967 * - cache withdrawal is prevented by the caller
968 */
969void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
970{
971 struct cachefiles_object *object;
9ae326a6
DH
972
973 object = container_of(_object, struct cachefiles_object, fscache);
9ae326a6
DH
974
975 _enter("%p,{%lu}", object, page->index);
976
977 spin_unlock(&object->fscache.cookie->lock);
978}