blob: 4b1fb5ca65b8058c4713e92dda3b1772cdeb6cfe [file] [log] [blame]
David Howells9ae326a2009-04-03 16:42:41 +01001/* 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Howells9ae326a2009-04-03 16:42:41 +010014#include <linux/file.h>
Mel Gormana0b8cab32013-07-03 15:02:32 -070015#include <linux/swap.h>
David Howells9ae326a2009-04-03 16:42:41 +010016#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 */
24static int cachefiles_read_waiter(wait_queue_t *wait, unsigned mode,
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;
30 struct wait_bit_key *key = _key;
31 struct page *page = wait->private;
32
33 ASSERT(key);
34
35 _enter("{%lu},%u,%d,{%p,%u}",
36 monitor->netfs_page->index, mode, sync,
37 key->flags, key->bit_nr);
38
39 if (key->flags != &page->flags ||
40 key->bit_nr != PG_locked)
41 return 0;
42
43 _debug("--- monitor %p %lx ---", page, page->flags);
44
David Howells5e929b32009-11-19 18:11:55 +000045 if (!PageUptodate(page) && !PageError(page)) {
46 /* unlocked, not uptodate and not erronous? */
47 _debug("page probably truncated");
48 }
David Howells9ae326a2009-04-03 16:42:41 +010049
50 /* remove from the waitqueue */
51 list_del(&wait->task_list);
52
53 /* move onto the action list and queue for FS-Cache thread pool */
54 ASSERT(monitor->op);
55
56 object = container_of(monitor->op->op.object,
57 struct cachefiles_object, fscache);
58
59 spin_lock(&object->work_lock);
60 list_add_tail(&monitor->op_link, &monitor->op->to_do);
61 spin_unlock(&object->work_lock);
62
63 fscache_enqueue_retrieval(monitor->op);
64 return 0;
65}
66
67/*
David Howells5e929b32009-11-19 18:11:55 +000068 * handle a probably truncated page
69 * - check to see if the page is still relevant and reissue the read if
70 * possible
71 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
72 * must wait again and 0 if successful
73 */
74static int cachefiles_read_reissue(struct cachefiles_object *object,
75 struct cachefiles_one_read *monitor)
76{
77 struct address_space *bmapping = object->backer->d_inode->i_mapping;
78 struct page *backpage = monitor->back_page, *backpage2;
79 int ret;
80
David Howells37491a12012-12-20 21:52:34 +000081 _enter("{ino=%lx},{%lx,%lx}",
David Howells5e929b32009-11-19 18:11:55 +000082 object->backer->d_inode->i_ino,
83 backpage->index, backpage->flags);
84
85 /* skip if the page was truncated away completely */
86 if (backpage->mapping != bmapping) {
David Howells37491a12012-12-20 21:52:34 +000087 _leave(" = -ENODATA [mapping]");
David Howells5e929b32009-11-19 18:11:55 +000088 return -ENODATA;
89 }
90
91 backpage2 = find_get_page(bmapping, backpage->index);
92 if (!backpage2) {
David Howells37491a12012-12-20 21:52:34 +000093 _leave(" = -ENODATA [gone]");
David Howells5e929b32009-11-19 18:11:55 +000094 return -ENODATA;
95 }
96
97 if (backpage != backpage2) {
98 put_page(backpage2);
David Howells37491a12012-12-20 21:52:34 +000099 _leave(" = -ENODATA [different]");
David Howells5e929b32009-11-19 18:11:55 +0000100 return -ENODATA;
101 }
102
103 /* the page is still there and we already have a ref on it, so we don't
104 * need a second */
105 put_page(backpage2);
106
107 INIT_LIST_HEAD(&monitor->op_link);
108 add_page_wait_queue(backpage, &monitor->monitor);
109
110 if (trylock_page(backpage)) {
111 ret = -EIO;
112 if (PageError(backpage))
113 goto unlock_discard;
114 ret = 0;
115 if (PageUptodate(backpage))
116 goto unlock_discard;
117
David Howells37491a12012-12-20 21:52:34 +0000118 _debug("reissue read");
David Howells5e929b32009-11-19 18:11:55 +0000119 ret = bmapping->a_ops->readpage(NULL, backpage);
120 if (ret < 0)
121 goto unlock_discard;
122 }
123
124 /* but the page may have been read before the monitor was installed, so
125 * the monitor may miss the event - so we have to ensure that we do get
126 * one in such a case */
127 if (trylock_page(backpage)) {
128 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
129 unlock_page(backpage);
130 }
131
132 /* it'll reappear on the todo list */
David Howells37491a12012-12-20 21:52:34 +0000133 _leave(" = -EINPROGRESS");
David Howells5e929b32009-11-19 18:11:55 +0000134 return -EINPROGRESS;
135
136unlock_discard:
137 unlock_page(backpage);
138 spin_lock_irq(&object->work_lock);
139 list_del(&monitor->op_link);
140 spin_unlock_irq(&object->work_lock);
David Howells37491a12012-12-20 21:52:34 +0000141 _leave(" = %d", ret);
David Howells5e929b32009-11-19 18:11:55 +0000142 return ret;
143}
144
145/*
David Howells9ae326a2009-04-03 16:42:41 +0100146 * copy data from backing pages to netfs pages to complete a read operation
147 * - driven by FS-Cache's thread pool
148 */
149static void cachefiles_read_copier(struct fscache_operation *_op)
150{
151 struct cachefiles_one_read *monitor;
152 struct cachefiles_object *object;
153 struct fscache_retrieval *op;
154 struct pagevec pagevec;
155 int error, max;
156
157 op = container_of(_op, struct fscache_retrieval, op);
158 object = container_of(op->op.object,
159 struct cachefiles_object, fscache);
160
161 _enter("{ino=%lu}", object->backer->d_inode->i_ino);
162
163 pagevec_init(&pagevec, 0);
164
165 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
200 page_cache_release(monitor->back_page);
201
202 fscache_end_io(op, monitor->netfs_page, error);
203 page_cache_release(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 */
253 bmapping = object->backer->d_inode->i_mapping;
254 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) {
David Howells5f4f9f42012-12-20 21:52:33 +0000262 newpage = __page_cache_alloc(cachefiles_gfp |
263 __GFP_COLD);
David Howells9ae326a2009-04-03 16:42:41 +0100264 if (!newpage)
265 goto nomem_monitor;
266 }
267
Johannes Weiner55881bc2014-04-03 14:47:36 -0700268 ret = add_to_page_cache_lru(newpage, bmapping,
269 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100270 if (ret == 0)
271 goto installed_new_backing_page;
272 if (ret != -EEXIST)
273 goto nomem_page;
274 }
275
Johannes Weiner55881bc2014-04-03 14:47:36 -0700276 /* we've installed a new backing page, so now we need to start
277 * it reading */
David Howells9ae326a2009-04-03 16:42:41 +0100278installed_new_backing_page:
279 _debug("- new %p", newpage);
280
281 backpage = newpage;
282 newpage = NULL;
283
David Howells9ae326a2009-04-03 16:42:41 +0100284read_backing_page:
285 ret = bmapping->a_ops->readpage(NULL, backpage);
286 if (ret < 0)
287 goto read_error;
288
289 /* set the monitor to transfer the data across */
290monitor_backing_page:
291 _debug("- monitor add");
292
293 /* install the monitor */
294 page_cache_get(monitor->netfs_page);
295 page_cache_get(backpage);
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) {
316 page_cache_release(newpage);
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
David Howellsc4d6d8d2012-12-20 21:52:32 +0000336 fscache_mark_page_cached(op, netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100337
338 copy_highpage(netpage, backpage);
339 fscache_end_io(op, netpage, 0);
David Howells9f105232012-12-20 21:52:35 +0000340 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100341
342success:
343 _debug("success");
344 ret = 0;
345
346out:
347 if (backpage)
348 page_cache_release(backpage);
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);
David Howellsb4cf1e02012-12-05 13:34:45 +0000358 if (ret == -ENOMEM) {
359 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100360 goto out;
David Howellsb4cf1e02012-12-05 13:34:45 +0000361 }
David Howells9ae326a2009-04-03 16:42:41 +0100362io_error:
363 cachefiles_io_error_obj(object, "Page read error on backing file");
David Howells9f105232012-12-20 21:52:35 +0000364 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100365 ret = -ENOBUFS;
366 goto out;
367
368nomem_page:
369 page_cache_release(newpage);
370nomem_monitor:
371 fscache_put_retrieval(monitor->op);
372 kfree(monitor);
373nomem:
David Howells9f105232012-12-20 21:52:35 +0000374 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100375 _leave(" = -ENOMEM");
376 return -ENOMEM;
377}
378
379/*
380 * read a page from the cache or allocate a block in which to store it
381 * - cache withdrawal is prevented by the caller
382 * - returns -EINTR if interrupted
383 * - returns -ENOMEM if ran out of memory
384 * - returns -ENOBUFS if no buffers can be made available
385 * - returns -ENOBUFS if page is beyond EOF
386 * - if the page is backed by a block in the cache:
387 * - a read will be started which will call the callback on completion
388 * - 0 will be returned
389 * - else if the page is unbacked:
390 * - the metadata will be retained
391 * - -ENODATA will be returned
392 */
393int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
394 struct page *page,
395 gfp_t gfp)
396{
397 struct cachefiles_object *object;
398 struct cachefiles_cache *cache;
399 struct pagevec pagevec;
400 struct inode *inode;
401 sector_t block0, block;
402 unsigned shift;
403 int ret;
404
405 object = container_of(op->op.object,
406 struct cachefiles_object, fscache);
407 cache = container_of(object->fscache.cache,
408 struct cachefiles_cache, cache);
409
410 _enter("{%p},{%lx},,,", object, page->index);
411
412 if (!object->backer)
David Howells9f105232012-12-20 21:52:35 +0000413 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100414
415 inode = object->backer->d_inode;
416 ASSERT(S_ISREG(inode->i_mode));
417 ASSERT(inode->i_mapping->a_ops->bmap);
418 ASSERT(inode->i_mapping->a_ops->readpages);
419
420 /* calculate the shift required to use bmap */
421 if (inode->i_sb->s_blocksize > PAGE_SIZE)
David Howells9f105232012-12-20 21:52:35 +0000422 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100423
424 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
425
David Howells4fbf4292009-11-19 18:11:04 +0000426 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
Tejun Heo8af7c122010-07-20 22:09:01 +0200427 op->op.flags |= FSCACHE_OP_ASYNC;
David Howells9ae326a2009-04-03 16:42:41 +0100428 op->op.processor = cachefiles_read_copier;
429
430 pagevec_init(&pagevec, 0);
431
432 /* we assume the absence or presence of the first block is a good
433 * enough indication for the page as a whole
434 * - TODO: don't use bmap() for this as it is _not_ actually good
435 * enough for this as it doesn't indicate errors, but it's all we've
436 * got for the moment
437 */
438 block0 = page->index;
439 block0 <<= shift;
440
441 block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
442 _debug("%llx -> %llx",
443 (unsigned long long) block0,
444 (unsigned long long) block);
445
446 if (block) {
447 /* submit the apparently valid page to the backing fs to be
448 * read from disk */
Mel Gormana0b8cab32013-07-03 15:02:32 -0700449 ret = cachefiles_read_backing_file_one(object, op, page);
David Howells9ae326a2009-04-03 16:42:41 +0100450 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
451 /* there's space in the cache we can use */
David Howellsc4d6d8d2012-12-20 21:52:32 +0000452 fscache_mark_page_cached(op, page);
David Howells9f105232012-12-20 21:52:35 +0000453 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100454 ret = -ENODATA;
455 } else {
David Howells9f105232012-12-20 21:52:35 +0000456 goto enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100457 }
458
459 _leave(" = %d", ret);
460 return ret;
David Howells9f105232012-12-20 21:52:35 +0000461
462enobufs:
463 fscache_retrieval_complete(op, 1);
464 _leave(" = -ENOBUFS");
465 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100466}
467
468/*
469 * read the corresponding pages to the given set from the backing file
470 * - any uncertain pages are simply discarded, to be tried again another time
471 */
472static int cachefiles_read_backing_file(struct cachefiles_object *object,
473 struct fscache_retrieval *op,
David Howellsc4d6d8d2012-12-20 21:52:32 +0000474 struct list_head *list)
David Howells9ae326a2009-04-03 16:42:41 +0100475{
476 struct cachefiles_one_read *monitor = NULL;
477 struct address_space *bmapping = object->backer->d_inode->i_mapping;
David Howells9ae326a2009-04-03 16:42:41 +0100478 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
479 int ret = 0;
480
481 _enter("");
482
David Howells9ae326a2009-04-03 16:42:41 +0100483 list_for_each_entry_safe(netpage, _n, list, lru) {
484 list_del(&netpage->lru);
485
486 _debug("read back %p{%lu,%d}",
487 netpage, netpage->index, page_count(netpage));
488
489 if (!monitor) {
David Howells5f4f9f42012-12-20 21:52:33 +0000490 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100491 if (!monitor)
492 goto nomem;
493
494 monitor->op = fscache_get_retrieval(op);
495 init_waitqueue_func_entry(&monitor->monitor,
496 cachefiles_read_waiter);
497 }
498
499 for (;;) {
500 backpage = find_get_page(bmapping, netpage->index);
501 if (backpage)
502 goto backing_page_already_present;
503
504 if (!newpage) {
David Howells5f4f9f42012-12-20 21:52:33 +0000505 newpage = __page_cache_alloc(cachefiles_gfp |
506 __GFP_COLD);
David Howells9ae326a2009-04-03 16:42:41 +0100507 if (!newpage)
508 goto nomem;
509 }
510
Johannes Weiner55881bc2014-04-03 14:47:36 -0700511 ret = add_to_page_cache_lru(newpage, bmapping,
512 netpage->index,
513 cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100514 if (ret == 0)
515 goto installed_new_backing_page;
516 if (ret != -EEXIST)
517 goto nomem;
518 }
519
Johannes Weiner55881bc2014-04-03 14:47:36 -0700520 /* we've installed a new backing page, so now we need
521 * to start it reading */
David Howells9ae326a2009-04-03 16:42:41 +0100522 installed_new_backing_page:
523 _debug("- new %p", newpage);
524
525 backpage = newpage;
526 newpage = NULL;
527
David Howells9ae326a2009-04-03 16:42:41 +0100528 reread_backing_page:
529 ret = bmapping->a_ops->readpage(NULL, backpage);
530 if (ret < 0)
531 goto read_error;
532
533 /* add the netfs page to the pagecache and LRU, and set the
534 * monitor to transfer the data across */
535 monitor_backing_page:
536 _debug("- monitor add");
537
Johannes Weiner55881bc2014-04-03 14:47:36 -0700538 ret = add_to_page_cache_lru(netpage, op->mapping,
539 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100540 if (ret < 0) {
541 if (ret == -EEXIST) {
542 page_cache_release(netpage);
David Howellsb4cf1e02012-12-05 13:34:45 +0000543 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100544 continue;
545 }
546 goto nomem;
547 }
548
David Howells9ae326a2009-04-03 16:42:41 +0100549 /* install a monitor */
550 page_cache_get(netpage);
551 monitor->netfs_page = netpage;
552
553 page_cache_get(backpage);
554 monitor->back_page = backpage;
555 monitor->monitor.private = backpage;
556 add_page_wait_queue(backpage, &monitor->monitor);
557 monitor = NULL;
558
559 /* but the page may have been read before the monitor was
560 * installed, so the monitor may miss the event - so we have to
561 * ensure that we do get one in such a case */
562 if (trylock_page(backpage)) {
563 _debug("2unlock %p {%lx}", backpage, backpage->flags);
564 unlock_page(backpage);
565 }
566
567 page_cache_release(backpage);
568 backpage = NULL;
569
570 page_cache_release(netpage);
571 netpage = NULL;
572 continue;
573
574 /* if the backing page is already present, it can be in one of
575 * three states: read in progress, read failed or read okay */
576 backing_page_already_present:
577 _debug("- present %p", backpage);
578
579 if (PageError(backpage))
580 goto io_error;
581
582 if (PageUptodate(backpage))
583 goto backing_page_already_uptodate;
584
585 _debug("- not ready %p{%lx}", backpage, backpage->flags);
586
587 if (!trylock_page(backpage))
588 goto monitor_backing_page;
589
590 if (PageError(backpage)) {
591 _debug("error %lx", backpage->flags);
592 unlock_page(backpage);
593 goto io_error;
594 }
595
596 if (PageUptodate(backpage))
597 goto backing_page_already_uptodate_unlock;
598
599 /* we've locked a page that's neither up to date nor erroneous,
600 * so we need to attempt to read it again */
601 goto reread_backing_page;
602
603 /* the backing page is already up to date, attach the netfs
604 * page to the pagecache and LRU and copy the data across */
605 backing_page_already_uptodate_unlock:
606 _debug("uptodate %lx", backpage->flags);
607 unlock_page(backpage);
608 backing_page_already_uptodate:
609 _debug("- uptodate");
610
Johannes Weiner55881bc2014-04-03 14:47:36 -0700611 ret = add_to_page_cache_lru(netpage, op->mapping,
612 netpage->index, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100613 if (ret < 0) {
614 if (ret == -EEXIST) {
615 page_cache_release(netpage);
David Howellsb4cf1e02012-12-05 13:34:45 +0000616 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100617 continue;
618 }
619 goto nomem;
620 }
621
622 copy_highpage(netpage, backpage);
623
624 page_cache_release(backpage);
625 backpage = NULL;
626
David Howellsc4d6d8d2012-12-20 21:52:32 +0000627 fscache_mark_page_cached(op, netpage);
David Howells9ae326a2009-04-03 16:42:41 +0100628
David Howellsc4d6d8d2012-12-20 21:52:32 +0000629 /* the netpage is unlocked and marked up to date here */
David Howells9ae326a2009-04-03 16:42:41 +0100630 fscache_end_io(op, netpage, 0);
631 page_cache_release(netpage);
632 netpage = NULL;
David Howellsb4cf1e02012-12-05 13:34:45 +0000633 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100634 continue;
635 }
636
637 netpage = NULL;
638
639 _debug("out");
640
641out:
642 /* tidy up */
David Howells9ae326a2009-04-03 16:42:41 +0100643 if (newpage)
644 page_cache_release(newpage);
645 if (netpage)
646 page_cache_release(netpage);
647 if (backpage)
648 page_cache_release(backpage);
649 if (monitor) {
650 fscache_put_retrieval(op);
651 kfree(monitor);
652 }
653
654 list_for_each_entry_safe(netpage, _n, list, lru) {
655 list_del(&netpage->lru);
656 page_cache_release(netpage);
David Howells9f105232012-12-20 21:52:35 +0000657 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100658 }
659
660 _leave(" = %d", ret);
661 return ret;
662
663nomem:
664 _debug("nomem");
665 ret = -ENOMEM;
David Howellsb4cf1e02012-12-05 13:34:45 +0000666 goto record_page_complete;
David Howells9ae326a2009-04-03 16:42:41 +0100667
668read_error:
669 _debug("read error %d", ret);
670 if (ret == -ENOMEM)
David Howellsb4cf1e02012-12-05 13:34:45 +0000671 goto record_page_complete;
David Howells9ae326a2009-04-03 16:42:41 +0100672io_error:
673 cachefiles_io_error_obj(object, "Page read error on backing file");
674 ret = -ENOBUFS;
David Howellsb4cf1e02012-12-05 13:34:45 +0000675record_page_complete:
676 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100677 goto out;
678}
679
680/*
681 * read a list of pages from the cache or allocate blocks in which to store
682 * them
683 */
684int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
685 struct list_head *pages,
686 unsigned *nr_pages,
687 gfp_t gfp)
688{
689 struct cachefiles_object *object;
690 struct cachefiles_cache *cache;
691 struct list_head backpages;
692 struct pagevec pagevec;
693 struct inode *inode;
694 struct page *page, *_n;
695 unsigned shift, nrbackpages;
696 int ret, ret2, space;
697
698 object = container_of(op->op.object,
699 struct cachefiles_object, fscache);
700 cache = container_of(object->fscache.cache,
701 struct cachefiles_cache, cache);
702
703 _enter("{OBJ%x,%d},,%d,,",
704 object->fscache.debug_id, atomic_read(&op->op.usage),
705 *nr_pages);
706
707 if (!object->backer)
David Howells9f105232012-12-20 21:52:35 +0000708 goto all_enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100709
710 space = 1;
711 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
712 space = 0;
713
714 inode = object->backer->d_inode;
715 ASSERT(S_ISREG(inode->i_mode));
716 ASSERT(inode->i_mapping->a_ops->bmap);
717 ASSERT(inode->i_mapping->a_ops->readpages);
718
719 /* calculate the shift required to use bmap */
720 if (inode->i_sb->s_blocksize > PAGE_SIZE)
David Howells9f105232012-12-20 21:52:35 +0000721 goto all_enobufs;
David Howells9ae326a2009-04-03 16:42:41 +0100722
723 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
724
725 pagevec_init(&pagevec, 0);
726
David Howells4fbf4292009-11-19 18:11:04 +0000727 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
Tejun Heo8af7c122010-07-20 22:09:01 +0200728 op->op.flags |= FSCACHE_OP_ASYNC;
David Howells9ae326a2009-04-03 16:42:41 +0100729 op->op.processor = cachefiles_read_copier;
730
731 INIT_LIST_HEAD(&backpages);
732 nrbackpages = 0;
733
734 ret = space ? -ENODATA : -ENOBUFS;
735 list_for_each_entry_safe(page, _n, pages, lru) {
736 sector_t block0, block;
737
738 /* we assume the absence or presence of the first block is a
739 * good enough indication for the page as a whole
740 * - TODO: don't use bmap() for this as it is _not_ actually
741 * good enough for this as it doesn't indicate errors, but
742 * it's all we've got for the moment
743 */
744 block0 = page->index;
745 block0 <<= shift;
746
747 block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
748 block0);
749 _debug("%llx -> %llx",
750 (unsigned long long) block0,
751 (unsigned long long) block);
752
753 if (block) {
754 /* we have data - add it to the list to give to the
755 * backing fs */
756 list_move(&page->lru, &backpages);
757 (*nr_pages)--;
758 nrbackpages++;
759 } else if (space && pagevec_add(&pagevec, page) == 0) {
760 fscache_mark_pages_cached(op, &pagevec);
David Howells9f105232012-12-20 21:52:35 +0000761 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100762 ret = -ENODATA;
David Howells9f105232012-12-20 21:52:35 +0000763 } else {
764 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100765 }
766 }
767
768 if (pagevec_count(&pagevec) > 0)
769 fscache_mark_pages_cached(op, &pagevec);
770
771 if (list_empty(pages))
772 ret = 0;
773
774 /* submit the apparently valid pages to the backing fs to be read from
775 * disk */
776 if (nrbackpages > 0) {
David Howellsc4d6d8d2012-12-20 21:52:32 +0000777 ret2 = cachefiles_read_backing_file(object, op, &backpages);
David Howells9ae326a2009-04-03 16:42:41 +0100778 if (ret2 == -ENOMEM || ret2 == -EINTR)
779 ret = ret2;
780 }
781
David Howells9ae326a2009-04-03 16:42:41 +0100782 _leave(" = %d [nr=%u%s]",
783 ret, *nr_pages, list_empty(pages) ? " empty" : "");
784 return ret;
David Howells9f105232012-12-20 21:52:35 +0000785
786all_enobufs:
787 fscache_retrieval_complete(op, *nr_pages);
788 return -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100789}
790
791/*
792 * allocate a block in the cache in which to store a page
793 * - cache withdrawal is prevented by the caller
794 * - returns -EINTR if interrupted
795 * - returns -ENOMEM if ran out of memory
796 * - returns -ENOBUFS if no buffers can be made available
797 * - returns -ENOBUFS if page is beyond EOF
798 * - otherwise:
799 * - the metadata will be retained
800 * - 0 will be returned
801 */
802int cachefiles_allocate_page(struct fscache_retrieval *op,
803 struct page *page,
804 gfp_t gfp)
805{
806 struct cachefiles_object *object;
807 struct cachefiles_cache *cache;
David Howells9ae326a2009-04-03 16:42:41 +0100808 int ret;
809
810 object = container_of(op->op.object,
811 struct cachefiles_object, fscache);
812 cache = container_of(object->fscache.cache,
813 struct cachefiles_cache, cache);
814
815 _enter("%p,{%lx},", object, page->index);
816
817 ret = cachefiles_has_space(cache, 0, 1);
David Howellsc4d6d8d2012-12-20 21:52:32 +0000818 if (ret == 0)
819 fscache_mark_page_cached(op, page);
820 else
David Howells9ae326a2009-04-03 16:42:41 +0100821 ret = -ENOBUFS;
David Howells9ae326a2009-04-03 16:42:41 +0100822
David Howells9f105232012-12-20 21:52:35 +0000823 fscache_retrieval_complete(op, 1);
David Howells9ae326a2009-04-03 16:42:41 +0100824 _leave(" = %d", ret);
825 return ret;
826}
827
828/*
829 * allocate blocks in the cache in which to store a set of pages
830 * - cache withdrawal is prevented by the caller
831 * - returns -EINTR if interrupted
832 * - returns -ENOMEM if ran out of memory
833 * - returns -ENOBUFS if some buffers couldn't be made available
834 * - returns -ENOBUFS if some pages are beyond EOF
835 * - otherwise:
836 * - -ENODATA will be returned
837 * - metadata will be retained for any page marked
838 */
839int cachefiles_allocate_pages(struct fscache_retrieval *op,
840 struct list_head *pages,
841 unsigned *nr_pages,
842 gfp_t gfp)
843{
844 struct cachefiles_object *object;
845 struct cachefiles_cache *cache;
846 struct pagevec pagevec;
847 struct page *page;
848 int ret;
849
850 object = container_of(op->op.object,
851 struct cachefiles_object, fscache);
852 cache = container_of(object->fscache.cache,
853 struct cachefiles_cache, cache);
854
855 _enter("%p,,,%d,", object, *nr_pages);
856
857 ret = cachefiles_has_space(cache, 0, *nr_pages);
858 if (ret == 0) {
859 pagevec_init(&pagevec, 0);
860
861 list_for_each_entry(page, pages, lru) {
862 if (pagevec_add(&pagevec, page) == 0)
863 fscache_mark_pages_cached(op, &pagevec);
864 }
865
866 if (pagevec_count(&pagevec) > 0)
867 fscache_mark_pages_cached(op, &pagevec);
868 ret = -ENODATA;
869 } else {
870 ret = -ENOBUFS;
871 }
872
David Howells9f105232012-12-20 21:52:35 +0000873 fscache_retrieval_complete(op, *nr_pages);
David Howells9ae326a2009-04-03 16:42:41 +0100874 _leave(" = %d", ret);
875 return ret;
876}
877
878/*
879 * request a page be stored in the cache
880 * - cache withdrawal is prevented by the caller
881 * - this request may be ignored if there's no cache block available, in which
882 * case -ENOBUFS will be returned
883 * - if the op is in progress, 0 will be returned
884 */
885int cachefiles_write_page(struct fscache_storage *op, struct page *page)
886{
887 struct cachefiles_object *object;
888 struct cachefiles_cache *cache;
889 mm_segment_t old_fs;
890 struct file *file;
Al Viro765927b2012-06-26 21:58:53 +0400891 struct path path;
David Howellsa17754f2009-11-19 18:11:52 +0000892 loff_t pos, eof;
893 size_t len;
David Howells9ae326a2009-04-03 16:42:41 +0100894 void *data;
895 int ret;
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
910 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
911
912 cache = container_of(object->fscache.cache,
913 struct cachefiles_cache, cache);
914
915 /* write the page to the backing filesystem and let it store it in its
916 * own time */
Al Viro765927b2012-06-26 21:58:53 +0400917 path.mnt = cache->mnt;
918 path.dentry = object->backer;
Justin Lecher98c350c2012-07-30 14:42:53 -0700919 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
David Howells9ae326a2009-04-03 16:42:41 +0100920 if (IS_ERR(file)) {
921 ret = PTR_ERR(file);
922 } else {
923 ret = -EIO;
924 if (file->f_op->write) {
925 pos = (loff_t) page->index << PAGE_SHIFT;
David Howellsa17754f2009-11-19 18:11:52 +0000926
927 /* we mustn't write more data than we have, so we have
928 * to beware of a partial page at EOF */
929 eof = object->fscache.store_limit_l;
930 len = PAGE_SIZE;
931 if (eof & ~PAGE_MASK) {
932 ASSERTCMP(pos, <, eof);
933 if (eof - pos < PAGE_SIZE) {
934 _debug("cut short %llx to %llx",
935 pos, eof);
936 len = eof - pos;
937 ASSERTCMP(pos + len, ==, eof);
938 }
939 }
940
David Howells9ae326a2009-04-03 16:42:41 +0100941 data = kmap(page);
Al Viro03d95eb2013-03-20 13:04:20 -0400942 file_start_write(file);
David Howells9ae326a2009-04-03 16:42:41 +0100943 old_fs = get_fs();
944 set_fs(KERNEL_DS);
945 ret = file->f_op->write(
David Howellsa17754f2009-11-19 18:11:52 +0000946 file, (const void __user *) data, len, &pos);
David Howells9ae326a2009-04-03 16:42:41 +0100947 set_fs(old_fs);
948 kunmap(page);
Al Viro03d95eb2013-03-20 13:04:20 -0400949 file_end_write(file);
David Howellsa17754f2009-11-19 18:11:52 +0000950 if (ret != len)
David Howells9ae326a2009-04-03 16:42:41 +0100951 ret = -EIO;
952 }
953 fput(file);
954 }
955
956 if (ret < 0) {
957 if (ret == -EIO)
958 cachefiles_io_error_obj(
959 object, "Write page to backing file failed");
960 ret = -ENOBUFS;
961 }
962
963 _leave(" = %d", ret);
964 return ret;
965}
966
967/*
968 * detach a backing block from a page
969 * - cache withdrawal is prevented by the caller
970 */
971void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
972{
973 struct cachefiles_object *object;
974 struct cachefiles_cache *cache;
975
976 object = container_of(_object, struct cachefiles_object, fscache);
977 cache = container_of(object->fscache.cache,
978 struct cachefiles_cache, cache);
979
980 _enter("%p,{%lu}", object, page->index);
981
982 spin_unlock(&object->fscache.cookie->lock);
983}