Commit | Line | Data |
---|---|---|
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 | */ | |
24 | static 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 | ||
5e929b33 DH |
45 | if (!PageUptodate(page) && !PageError(page)) { |
46 | /* unlocked, not uptodate and not erronous? */ | |
47 | _debug("page probably truncated"); | |
48 | } | |
9ae326a6 DH |
49 | |
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 | ||
5e929b33 DH |
67 | /* |
68 | * 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 | */ | |
74 | static 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 | ||
37491a13 | 81 | _enter("{ino=%lx},{%lx,%lx}", |
5e929b33 DH |
82 | 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) { | |
37491a13 | 87 | _leave(" = -ENODATA [mapping]"); |
5e929b33 DH |
88 | return -ENODATA; |
89 | } | |
90 | ||
91 | backpage2 = find_get_page(bmapping, backpage->index); | |
92 | if (!backpage2) { | |
37491a13 | 93 | _leave(" = -ENODATA [gone]"); |
5e929b33 DH |
94 | return -ENODATA; |
95 | } | |
96 | ||
97 | if (backpage != backpage2) { | |
98 | put_page(backpage2); | |
37491a13 | 99 | _leave(" = -ENODATA [different]"); |
5e929b33 DH |
100 | 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 | ||
37491a13 | 118 | _debug("reissue read"); |
5e929b33 DH |
119 | 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 */ | |
37491a13 | 133 | _leave(" = -EINPROGRESS"); |
5e929b33 DH |
134 | return -EINPROGRESS; |
135 | ||
136 | unlock_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); | |
37491a13 | 141 | _leave(" = %d", ret); |
5e929b33 DH |
142 | return ret; |
143 | } | |
144 | ||
9ae326a6 DH |
145 | /* |
146 | * copy data from backing pages to netfs pages to complete a read operation | |
147 | * - driven by FS-Cache's thread pool | |
148 | */ | |
149 | static 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 | ||
5e929b33 | 177 | recheck: |
9dc8d9bf DH |
178 | if (test_bit(FSCACHE_COOKIE_INVALIDATING, |
179 | &object->fscache.cookie->flags)) { | |
180 | error = -ESTALE; | |
181 | } else if (PageUptodate(monitor->back_page)) { | |
9ae326a6 | 182 | copy_highpage(monitor->netfs_page, monitor->back_page); |
c4d6d8db DH |
183 | fscache_mark_page_cached(monitor->op, |
184 | monitor->netfs_page); | |
9ae326a6 | 185 | error = 0; |
5e929b33 DH |
186 | } 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 { | |
9ae326a6 DH |
193 | cachefiles_io_error_obj( |
194 | object, | |
195 | "Readpage failed on backing file %lx", | |
196 | (unsigned long) monitor->back_page->flags); | |
5e929b33 DH |
197 | error = -EIO; |
198 | } | |
9ae326a6 DH |
199 | |
200 | page_cache_release(monitor->back_page); | |
201 | ||
202 | fscache_end_io(op, monitor->netfs_page, error); | |
203 | page_cache_release(monitor->netfs_page); | |
9f10523f | 204 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
205 | fscache_put_retrieval(op); |
206 | kfree(monitor); | |
207 | ||
5e929b33 | 208 | next: |
9ae326a6 DH |
209 | /* 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 | */ | |
229 | static int cachefiles_read_backing_file_one(struct cachefiles_object *object, | |
230 | struct fscache_retrieval *op, | |
a0b8cab3 | 231 | struct page *netpage) |
9ae326a6 DH |
232 | { |
233 | struct cachefiles_one_read *monitor; | |
234 | struct address_space *bmapping; | |
235 | struct page *newpage, *backpage; | |
236 | int ret; | |
237 | ||
238 | _enter(""); | |
239 | ||
9ae326a6 DH |
240 | _debug("read back %p{%lu,%d}", |
241 | netpage, netpage->index, page_count(netpage)); | |
242 | ||
5f4f9f4a | 243 | monitor = kzalloc(sizeof(*monitor), cachefiles_gfp); |
9ae326a6 DH |
244 | 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) { | |
5f4f9f4a DH |
262 | newpage = __page_cache_alloc(cachefiles_gfp | |
263 | __GFP_COLD); | |
9ae326a6 DH |
264 | if (!newpage) |
265 | goto nomem_monitor; | |
266 | } | |
267 | ||
268 | ret = add_to_page_cache(newpage, bmapping, | |
5f4f9f4a | 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 | ||
276 | /* we've installed a new backing page, so now we need to add it | |
277 | * to the LRU list and start it reading */ | |
278 | installed_new_backing_page: | |
279 | _debug("- new %p", newpage); | |
280 | ||
281 | backpage = newpage; | |
282 | newpage = NULL; | |
283 | ||
a0b8cab3 | 284 | lru_cache_add_file(backpage); |
9ae326a6 DH |
285 | |
286 | read_backing_page: | |
287 | ret = bmapping->a_ops->readpage(NULL, backpage); | |
288 | if (ret < 0) | |
289 | goto read_error; | |
290 | ||
291 | /* set the monitor to transfer the data across */ | |
292 | monitor_backing_page: | |
293 | _debug("- monitor add"); | |
294 | ||
295 | /* install the monitor */ | |
296 | page_cache_get(monitor->netfs_page); | |
297 | page_cache_get(backpage); | |
298 | monitor->back_page = backpage; | |
299 | monitor->monitor.private = backpage; | |
300 | add_page_wait_queue(backpage, &monitor->monitor); | |
301 | monitor = NULL; | |
302 | ||
303 | /* but the page may have been read before the monitor was installed, so | |
304 | * the monitor may miss the event - so we have to ensure that we do get | |
305 | * one in such a case */ | |
306 | if (trylock_page(backpage)) { | |
307 | _debug("jumpstart %p {%lx}", backpage, backpage->flags); | |
308 | unlock_page(backpage); | |
309 | } | |
310 | goto success; | |
311 | ||
312 | /* if the backing page is already present, it can be in one of | |
313 | * three states: read in progress, read failed or read okay */ | |
314 | backing_page_already_present: | |
315 | _debug("- present"); | |
316 | ||
317 | if (newpage) { | |
318 | page_cache_release(newpage); | |
319 | newpage = NULL; | |
320 | } | |
321 | ||
322 | if (PageError(backpage)) | |
323 | goto io_error; | |
324 | ||
325 | if (PageUptodate(backpage)) | |
326 | goto backing_page_already_uptodate; | |
327 | ||
328 | if (!trylock_page(backpage)) | |
329 | goto monitor_backing_page; | |
330 | _debug("read %p {%lx}", backpage, backpage->flags); | |
331 | goto read_backing_page; | |
332 | ||
333 | /* the backing page is already up to date, attach the netfs | |
334 | * page to the pagecache and LRU and copy the data across */ | |
335 | backing_page_already_uptodate: | |
336 | _debug("- uptodate"); | |
337 | ||
c4d6d8db | 338 | fscache_mark_page_cached(op, netpage); |
9ae326a6 DH |
339 | |
340 | copy_highpage(netpage, backpage); | |
341 | fscache_end_io(op, netpage, 0); | |
9f10523f | 342 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
343 | |
344 | success: | |
345 | _debug("success"); | |
346 | ret = 0; | |
347 | ||
348 | out: | |
349 | if (backpage) | |
350 | page_cache_release(backpage); | |
351 | if (monitor) { | |
352 | fscache_put_retrieval(monitor->op); | |
353 | kfree(monitor); | |
354 | } | |
355 | _leave(" = %d", ret); | |
356 | return ret; | |
357 | ||
358 | read_error: | |
359 | _debug("read error %d", ret); | |
b4cf1e08 DH |
360 | if (ret == -ENOMEM) { |
361 | fscache_retrieval_complete(op, 1); | |
9ae326a6 | 362 | goto out; |
b4cf1e08 | 363 | } |
9ae326a6 DH |
364 | io_error: |
365 | cachefiles_io_error_obj(object, "Page read error on backing file"); | |
9f10523f | 366 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
367 | ret = -ENOBUFS; |
368 | goto out; | |
369 | ||
370 | nomem_page: | |
371 | page_cache_release(newpage); | |
372 | nomem_monitor: | |
373 | fscache_put_retrieval(monitor->op); | |
374 | kfree(monitor); | |
375 | nomem: | |
9f10523f | 376 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
377 | _leave(" = -ENOMEM"); |
378 | return -ENOMEM; | |
379 | } | |
380 | ||
381 | /* | |
382 | * read a page from the cache or allocate a block in which to store it | |
383 | * - cache withdrawal is prevented by the caller | |
384 | * - returns -EINTR if interrupted | |
385 | * - returns -ENOMEM if ran out of memory | |
386 | * - returns -ENOBUFS if no buffers can be made available | |
387 | * - returns -ENOBUFS if page is beyond EOF | |
388 | * - if the page is backed by a block in the cache: | |
389 | * - a read will be started which will call the callback on completion | |
390 | * - 0 will be returned | |
391 | * - else if the page is unbacked: | |
392 | * - the metadata will be retained | |
393 | * - -ENODATA will be returned | |
394 | */ | |
395 | int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, | |
396 | struct page *page, | |
397 | gfp_t gfp) | |
398 | { | |
399 | struct cachefiles_object *object; | |
400 | struct cachefiles_cache *cache; | |
401 | struct pagevec pagevec; | |
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 DH |
416 | |
417 | inode = object->backer->d_inode; | |
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 */ | |
423 | if (inode->i_sb->s_blocksize > PAGE_SIZE) | |
9f10523f | 424 | goto enobufs; |
9ae326a6 DH |
425 | |
426 | shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits; | |
427 | ||
4fbf4291 | 428 | op->op.flags &= FSCACHE_OP_KEEP_FLAGS; |
8af7c124 | 429 | op->op.flags |= FSCACHE_OP_ASYNC; |
9ae326a6 DH |
430 | op->op.processor = cachefiles_read_copier; |
431 | ||
432 | pagevec_init(&pagevec, 0); | |
433 | ||
434 | /* we assume the absence or presence of the first block is a good | |
435 | * enough indication for the page as a whole | |
436 | * - TODO: don't use bmap() for this as it is _not_ actually good | |
437 | * enough for this as it doesn't indicate errors, but it's all we've | |
438 | * got for the moment | |
439 | */ | |
440 | block0 = page->index; | |
441 | block0 <<= shift; | |
442 | ||
443 | block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0); | |
444 | _debug("%llx -> %llx", | |
445 | (unsigned long long) block0, | |
446 | (unsigned long long) block); | |
447 | ||
448 | if (block) { | |
449 | /* submit the apparently valid page to the backing fs to be | |
450 | * read from disk */ | |
a0b8cab3 | 451 | ret = cachefiles_read_backing_file_one(object, op, page); |
9ae326a6 DH |
452 | } else if (cachefiles_has_space(cache, 0, 1) == 0) { |
453 | /* there's space in the cache we can use */ | |
c4d6d8db | 454 | fscache_mark_page_cached(op, page); |
9f10523f | 455 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
456 | ret = -ENODATA; |
457 | } else { | |
9f10523f | 458 | goto enobufs; |
9ae326a6 DH |
459 | } |
460 | ||
461 | _leave(" = %d", ret); | |
462 | return ret; | |
9f10523f DH |
463 | |
464 | enobufs: | |
465 | fscache_retrieval_complete(op, 1); | |
466 | _leave(" = -ENOBUFS"); | |
467 | return -ENOBUFS; | |
9ae326a6 DH |
468 | } |
469 | ||
470 | /* | |
471 | * read the corresponding pages to the given set from the backing file | |
472 | * - any uncertain pages are simply discarded, to be tried again another time | |
473 | */ | |
474 | static int cachefiles_read_backing_file(struct cachefiles_object *object, | |
475 | struct fscache_retrieval *op, | |
c4d6d8db | 476 | struct list_head *list) |
9ae326a6 DH |
477 | { |
478 | struct cachefiles_one_read *monitor = NULL; | |
479 | struct address_space *bmapping = object->backer->d_inode->i_mapping; | |
9ae326a6 DH |
480 | struct page *newpage = NULL, *netpage, *_n, *backpage = NULL; |
481 | int ret = 0; | |
482 | ||
483 | _enter(""); | |
484 | ||
9ae326a6 DH |
485 | list_for_each_entry_safe(netpage, _n, list, lru) { |
486 | list_del(&netpage->lru); | |
487 | ||
488 | _debug("read back %p{%lu,%d}", | |
489 | netpage, netpage->index, page_count(netpage)); | |
490 | ||
491 | if (!monitor) { | |
5f4f9f4a | 492 | monitor = kzalloc(sizeof(*monitor), cachefiles_gfp); |
9ae326a6 DH |
493 | if (!monitor) |
494 | goto nomem; | |
495 | ||
496 | monitor->op = fscache_get_retrieval(op); | |
497 | init_waitqueue_func_entry(&monitor->monitor, | |
498 | cachefiles_read_waiter); | |
499 | } | |
500 | ||
501 | for (;;) { | |
502 | backpage = find_get_page(bmapping, netpage->index); | |
503 | if (backpage) | |
504 | goto backing_page_already_present; | |
505 | ||
506 | if (!newpage) { | |
5f4f9f4a DH |
507 | newpage = __page_cache_alloc(cachefiles_gfp | |
508 | __GFP_COLD); | |
9ae326a6 DH |
509 | if (!newpage) |
510 | goto nomem; | |
511 | } | |
512 | ||
513 | ret = add_to_page_cache(newpage, bmapping, | |
5f4f9f4a | 514 | netpage->index, cachefiles_gfp); |
9ae326a6 DH |
515 | if (ret == 0) |
516 | goto installed_new_backing_page; | |
517 | if (ret != -EEXIST) | |
518 | goto nomem; | |
519 | } | |
520 | ||
521 | /* we've installed a new backing page, so now we need to add it | |
522 | * to the LRU list and start it reading */ | |
523 | installed_new_backing_page: | |
524 | _debug("- new %p", newpage); | |
525 | ||
526 | backpage = newpage; | |
527 | newpage = NULL; | |
528 | ||
a0b8cab3 | 529 | lru_cache_add_file(backpage); |
9ae326a6 DH |
530 | |
531 | reread_backing_page: | |
532 | ret = bmapping->a_ops->readpage(NULL, backpage); | |
533 | if (ret < 0) | |
534 | goto read_error; | |
535 | ||
536 | /* add the netfs page to the pagecache and LRU, and set the | |
537 | * monitor to transfer the data across */ | |
538 | monitor_backing_page: | |
539 | _debug("- monitor add"); | |
540 | ||
541 | ret = add_to_page_cache(netpage, op->mapping, netpage->index, | |
5f4f9f4a | 542 | cachefiles_gfp); |
9ae326a6 DH |
543 | if (ret < 0) { |
544 | if (ret == -EEXIST) { | |
545 | page_cache_release(netpage); | |
b4cf1e08 | 546 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
547 | continue; |
548 | } | |
549 | goto nomem; | |
550 | } | |
551 | ||
a0b8cab3 | 552 | lru_cache_add_file(netpage); |
9ae326a6 DH |
553 | |
554 | /* install a monitor */ | |
555 | page_cache_get(netpage); | |
556 | monitor->netfs_page = netpage; | |
557 | ||
558 | page_cache_get(backpage); | |
559 | monitor->back_page = backpage; | |
560 | monitor->monitor.private = backpage; | |
561 | add_page_wait_queue(backpage, &monitor->monitor); | |
562 | monitor = NULL; | |
563 | ||
564 | /* but the page may have been read before the monitor was | |
565 | * installed, so the monitor may miss the event - so we have to | |
566 | * ensure that we do get one in such a case */ | |
567 | if (trylock_page(backpage)) { | |
568 | _debug("2unlock %p {%lx}", backpage, backpage->flags); | |
569 | unlock_page(backpage); | |
570 | } | |
571 | ||
572 | page_cache_release(backpage); | |
573 | backpage = NULL; | |
574 | ||
575 | page_cache_release(netpage); | |
576 | netpage = NULL; | |
577 | continue; | |
578 | ||
579 | /* if the backing page is already present, it can be in one of | |
580 | * three states: read in progress, read failed or read okay */ | |
581 | backing_page_already_present: | |
582 | _debug("- present %p", backpage); | |
583 | ||
584 | if (PageError(backpage)) | |
585 | goto io_error; | |
586 | ||
587 | if (PageUptodate(backpage)) | |
588 | goto backing_page_already_uptodate; | |
589 | ||
590 | _debug("- not ready %p{%lx}", backpage, backpage->flags); | |
591 | ||
592 | if (!trylock_page(backpage)) | |
593 | goto monitor_backing_page; | |
594 | ||
595 | if (PageError(backpage)) { | |
596 | _debug("error %lx", backpage->flags); | |
597 | unlock_page(backpage); | |
598 | goto io_error; | |
599 | } | |
600 | ||
601 | if (PageUptodate(backpage)) | |
602 | goto backing_page_already_uptodate_unlock; | |
603 | ||
604 | /* we've locked a page that's neither up to date nor erroneous, | |
605 | * so we need to attempt to read it again */ | |
606 | goto reread_backing_page; | |
607 | ||
608 | /* the backing page is already up to date, attach the netfs | |
609 | * page to the pagecache and LRU and copy the data across */ | |
610 | backing_page_already_uptodate_unlock: | |
611 | _debug("uptodate %lx", backpage->flags); | |
612 | unlock_page(backpage); | |
613 | backing_page_already_uptodate: | |
614 | _debug("- uptodate"); | |
615 | ||
616 | ret = add_to_page_cache(netpage, op->mapping, netpage->index, | |
5f4f9f4a | 617 | cachefiles_gfp); |
9ae326a6 DH |
618 | if (ret < 0) { |
619 | if (ret == -EEXIST) { | |
620 | page_cache_release(netpage); | |
b4cf1e08 | 621 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
622 | continue; |
623 | } | |
624 | goto nomem; | |
625 | } | |
626 | ||
627 | copy_highpage(netpage, backpage); | |
628 | ||
629 | page_cache_release(backpage); | |
630 | backpage = NULL; | |
631 | ||
c4d6d8db | 632 | fscache_mark_page_cached(op, netpage); |
9ae326a6 | 633 | |
a0b8cab3 | 634 | lru_cache_add_file(netpage); |
9ae326a6 | 635 | |
c4d6d8db | 636 | /* the netpage is unlocked and marked up to date here */ |
9ae326a6 DH |
637 | fscache_end_io(op, netpage, 0); |
638 | page_cache_release(netpage); | |
639 | netpage = NULL; | |
b4cf1e08 | 640 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
641 | continue; |
642 | } | |
643 | ||
644 | netpage = NULL; | |
645 | ||
646 | _debug("out"); | |
647 | ||
648 | out: | |
649 | /* tidy up */ | |
9ae326a6 DH |
650 | if (newpage) |
651 | page_cache_release(newpage); | |
652 | if (netpage) | |
653 | page_cache_release(netpage); | |
654 | if (backpage) | |
655 | page_cache_release(backpage); | |
656 | if (monitor) { | |
657 | fscache_put_retrieval(op); | |
658 | kfree(monitor); | |
659 | } | |
660 | ||
661 | list_for_each_entry_safe(netpage, _n, list, lru) { | |
662 | list_del(&netpage->lru); | |
663 | page_cache_release(netpage); | |
9f10523f | 664 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
665 | } |
666 | ||
667 | _leave(" = %d", ret); | |
668 | return ret; | |
669 | ||
670 | nomem: | |
671 | _debug("nomem"); | |
672 | ret = -ENOMEM; | |
b4cf1e08 | 673 | goto record_page_complete; |
9ae326a6 DH |
674 | |
675 | read_error: | |
676 | _debug("read error %d", ret); | |
677 | if (ret == -ENOMEM) | |
b4cf1e08 | 678 | goto record_page_complete; |
9ae326a6 DH |
679 | io_error: |
680 | cachefiles_io_error_obj(object, "Page read error on backing file"); | |
681 | ret = -ENOBUFS; | |
b4cf1e08 DH |
682 | record_page_complete: |
683 | fscache_retrieval_complete(op, 1); | |
9ae326a6 DH |
684 | goto out; |
685 | } | |
686 | ||
687 | /* | |
688 | * read a list of pages from the cache or allocate blocks in which to store | |
689 | * them | |
690 | */ | |
691 | int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op, | |
692 | struct list_head *pages, | |
693 | unsigned *nr_pages, | |
694 | gfp_t gfp) | |
695 | { | |
696 | struct cachefiles_object *object; | |
697 | struct cachefiles_cache *cache; | |
698 | struct list_head backpages; | |
699 | struct pagevec pagevec; | |
700 | struct inode *inode; | |
701 | struct page *page, *_n; | |
702 | unsigned shift, nrbackpages; | |
703 | int ret, ret2, space; | |
704 | ||
705 | object = container_of(op->op.object, | |
706 | struct cachefiles_object, fscache); | |
707 | cache = container_of(object->fscache.cache, | |
708 | struct cachefiles_cache, cache); | |
709 | ||
710 | _enter("{OBJ%x,%d},,%d,,", | |
711 | object->fscache.debug_id, atomic_read(&op->op.usage), | |
712 | *nr_pages); | |
713 | ||
714 | if (!object->backer) | |
9f10523f | 715 | goto all_enobufs; |
9ae326a6 DH |
716 | |
717 | space = 1; | |
718 | if (cachefiles_has_space(cache, 0, *nr_pages) < 0) | |
719 | space = 0; | |
720 | ||
721 | inode = object->backer->d_inode; | |
722 | ASSERT(S_ISREG(inode->i_mode)); | |
723 | ASSERT(inode->i_mapping->a_ops->bmap); | |
724 | ASSERT(inode->i_mapping->a_ops->readpages); | |
725 | ||
726 | /* calculate the shift required to use bmap */ | |
727 | if (inode->i_sb->s_blocksize > PAGE_SIZE) | |
9f10523f | 728 | goto all_enobufs; |
9ae326a6 DH |
729 | |
730 | shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits; | |
731 | ||
732 | pagevec_init(&pagevec, 0); | |
733 | ||
4fbf4291 | 734 | op->op.flags &= FSCACHE_OP_KEEP_FLAGS; |
8af7c124 | 735 | op->op.flags |= FSCACHE_OP_ASYNC; |
9ae326a6 DH |
736 | op->op.processor = cachefiles_read_copier; |
737 | ||
738 | INIT_LIST_HEAD(&backpages); | |
739 | nrbackpages = 0; | |
740 | ||
741 | ret = space ? -ENODATA : -ENOBUFS; | |
742 | list_for_each_entry_safe(page, _n, pages, lru) { | |
743 | sector_t block0, block; | |
744 | ||
745 | /* we assume the absence or presence of the first block is a | |
746 | * good enough indication for the page as a whole | |
747 | * - TODO: don't use bmap() for this as it is _not_ actually | |
748 | * good enough for this as it doesn't indicate errors, but | |
749 | * it's all we've got for the moment | |
750 | */ | |
751 | block0 = page->index; | |
752 | block0 <<= shift; | |
753 | ||
754 | block = inode->i_mapping->a_ops->bmap(inode->i_mapping, | |
755 | block0); | |
756 | _debug("%llx -> %llx", | |
757 | (unsigned long long) block0, | |
758 | (unsigned long long) block); | |
759 | ||
760 | if (block) { | |
761 | /* we have data - add it to the list to give to the | |
762 | * backing fs */ | |
763 | list_move(&page->lru, &backpages); | |
764 | (*nr_pages)--; | |
765 | nrbackpages++; | |
766 | } else if (space && pagevec_add(&pagevec, page) == 0) { | |
767 | fscache_mark_pages_cached(op, &pagevec); | |
9f10523f | 768 | fscache_retrieval_complete(op, 1); |
9ae326a6 | 769 | ret = -ENODATA; |
9f10523f DH |
770 | } else { |
771 | fscache_retrieval_complete(op, 1); | |
9ae326a6 DH |
772 | } |
773 | } | |
774 | ||
775 | if (pagevec_count(&pagevec) > 0) | |
776 | fscache_mark_pages_cached(op, &pagevec); | |
777 | ||
778 | if (list_empty(pages)) | |
779 | ret = 0; | |
780 | ||
781 | /* submit the apparently valid pages to the backing fs to be read from | |
782 | * disk */ | |
783 | if (nrbackpages > 0) { | |
c4d6d8db | 784 | ret2 = cachefiles_read_backing_file(object, op, &backpages); |
9ae326a6 DH |
785 | if (ret2 == -ENOMEM || ret2 == -EINTR) |
786 | ret = ret2; | |
787 | } | |
788 | ||
9ae326a6 DH |
789 | _leave(" = %d [nr=%u%s]", |
790 | ret, *nr_pages, list_empty(pages) ? " empty" : ""); | |
791 | return ret; | |
9f10523f DH |
792 | |
793 | all_enobufs: | |
794 | fscache_retrieval_complete(op, *nr_pages); | |
795 | return -ENOBUFS; | |
9ae326a6 DH |
796 | } |
797 | ||
798 | /* | |
799 | * allocate a block in the cache in which to store a page | |
800 | * - cache withdrawal is prevented by the caller | |
801 | * - returns -EINTR if interrupted | |
802 | * - returns -ENOMEM if ran out of memory | |
803 | * - returns -ENOBUFS if no buffers can be made available | |
804 | * - returns -ENOBUFS if page is beyond EOF | |
805 | * - otherwise: | |
806 | * - the metadata will be retained | |
807 | * - 0 will be returned | |
808 | */ | |
809 | int cachefiles_allocate_page(struct fscache_retrieval *op, | |
810 | struct page *page, | |
811 | gfp_t gfp) | |
812 | { | |
813 | struct cachefiles_object *object; | |
814 | struct cachefiles_cache *cache; | |
9ae326a6 DH |
815 | int ret; |
816 | ||
817 | object = container_of(op->op.object, | |
818 | struct cachefiles_object, fscache); | |
819 | cache = container_of(object->fscache.cache, | |
820 | struct cachefiles_cache, cache); | |
821 | ||
822 | _enter("%p,{%lx},", object, page->index); | |
823 | ||
824 | ret = cachefiles_has_space(cache, 0, 1); | |
c4d6d8db DH |
825 | if (ret == 0) |
826 | fscache_mark_page_cached(op, page); | |
827 | else | |
9ae326a6 | 828 | ret = -ENOBUFS; |
9ae326a6 | 829 | |
9f10523f | 830 | fscache_retrieval_complete(op, 1); |
9ae326a6 DH |
831 | _leave(" = %d", ret); |
832 | return ret; | |
833 | } | |
834 | ||
835 | /* | |
836 | * allocate blocks in the cache in which to store a set of pages | |
837 | * - cache withdrawal is prevented by the caller | |
838 | * - returns -EINTR if interrupted | |
839 | * - returns -ENOMEM if ran out of memory | |
840 | * - returns -ENOBUFS if some buffers couldn't be made available | |
841 | * - returns -ENOBUFS if some pages are beyond EOF | |
842 | * - otherwise: | |
843 | * - -ENODATA will be returned | |
844 | * - metadata will be retained for any page marked | |
845 | */ | |
846 | int cachefiles_allocate_pages(struct fscache_retrieval *op, | |
847 | struct list_head *pages, | |
848 | unsigned *nr_pages, | |
849 | gfp_t gfp) | |
850 | { | |
851 | struct cachefiles_object *object; | |
852 | struct cachefiles_cache *cache; | |
853 | struct pagevec pagevec; | |
854 | struct page *page; | |
855 | int ret; | |
856 | ||
857 | object = container_of(op->op.object, | |
858 | struct cachefiles_object, fscache); | |
859 | cache = container_of(object->fscache.cache, | |
860 | struct cachefiles_cache, cache); | |
861 | ||
862 | _enter("%p,,,%d,", object, *nr_pages); | |
863 | ||
864 | ret = cachefiles_has_space(cache, 0, *nr_pages); | |
865 | if (ret == 0) { | |
866 | pagevec_init(&pagevec, 0); | |
867 | ||
868 | list_for_each_entry(page, pages, lru) { | |
869 | if (pagevec_add(&pagevec, page) == 0) | |
870 | fscache_mark_pages_cached(op, &pagevec); | |
871 | } | |
872 | ||
873 | if (pagevec_count(&pagevec) > 0) | |
874 | fscache_mark_pages_cached(op, &pagevec); | |
875 | ret = -ENODATA; | |
876 | } else { | |
877 | ret = -ENOBUFS; | |
878 | } | |
879 | ||
9f10523f | 880 | fscache_retrieval_complete(op, *nr_pages); |
9ae326a6 DH |
881 | _leave(" = %d", ret); |
882 | return ret; | |
883 | } | |
884 | ||
885 | /* | |
886 | * request a page be stored in the cache | |
887 | * - cache withdrawal is prevented by the caller | |
888 | * - this request may be ignored if there's no cache block available, in which | |
889 | * case -ENOBUFS will be returned | |
890 | * - if the op is in progress, 0 will be returned | |
891 | */ | |
892 | int cachefiles_write_page(struct fscache_storage *op, struct page *page) | |
893 | { | |
894 | struct cachefiles_object *object; | |
895 | struct cachefiles_cache *cache; | |
896 | mm_segment_t old_fs; | |
897 | struct file *file; | |
765927b2 | 898 | struct path path; |
a17754fb DH |
899 | loff_t pos, eof; |
900 | size_t len; | |
9ae326a6 DH |
901 | void *data; |
902 | int ret; | |
903 | ||
904 | ASSERT(op != NULL); | |
905 | ASSERT(page != NULL); | |
906 | ||
907 | object = container_of(op->op.object, | |
908 | struct cachefiles_object, fscache); | |
909 | ||
910 | _enter("%p,%p{%lx},,,", object, page, page->index); | |
911 | ||
912 | if (!object->backer) { | |
913 | _leave(" = -ENOBUFS"); | |
914 | return -ENOBUFS; | |
915 | } | |
916 | ||
917 | ASSERT(S_ISREG(object->backer->d_inode->i_mode)); | |
918 | ||
919 | cache = container_of(object->fscache.cache, | |
920 | struct cachefiles_cache, cache); | |
921 | ||
922 | /* write the page to the backing filesystem and let it store it in its | |
923 | * own time */ | |
765927b2 AV |
924 | path.mnt = cache->mnt; |
925 | path.dentry = object->backer; | |
98c350cd | 926 | file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred); |
9ae326a6 DH |
927 | if (IS_ERR(file)) { |
928 | ret = PTR_ERR(file); | |
929 | } else { | |
930 | ret = -EIO; | |
931 | if (file->f_op->write) { | |
932 | pos = (loff_t) page->index << PAGE_SHIFT; | |
a17754fb DH |
933 | |
934 | /* we mustn't write more data than we have, so we have | |
935 | * to beware of a partial page at EOF */ | |
936 | eof = object->fscache.store_limit_l; | |
937 | len = PAGE_SIZE; | |
938 | if (eof & ~PAGE_MASK) { | |
939 | ASSERTCMP(pos, <, eof); | |
940 | if (eof - pos < PAGE_SIZE) { | |
941 | _debug("cut short %llx to %llx", | |
942 | pos, eof); | |
943 | len = eof - pos; | |
944 | ASSERTCMP(pos + len, ==, eof); | |
945 | } | |
946 | } | |
947 | ||
9ae326a6 | 948 | data = kmap(page); |
03d95eb2 | 949 | file_start_write(file); |
9ae326a6 DH |
950 | old_fs = get_fs(); |
951 | set_fs(KERNEL_DS); | |
952 | ret = file->f_op->write( | |
a17754fb | 953 | file, (const void __user *) data, len, &pos); |
9ae326a6 DH |
954 | set_fs(old_fs); |
955 | kunmap(page); | |
03d95eb2 | 956 | file_end_write(file); |
a17754fb | 957 | if (ret != len) |
9ae326a6 DH |
958 | ret = -EIO; |
959 | } | |
960 | fput(file); | |
961 | } | |
962 | ||
963 | if (ret < 0) { | |
964 | if (ret == -EIO) | |
965 | cachefiles_io_error_obj( | |
966 | object, "Write page to backing file failed"); | |
967 | ret = -ENOBUFS; | |
968 | } | |
969 | ||
970 | _leave(" = %d", ret); | |
971 | return ret; | |
972 | } | |
973 | ||
974 | /* | |
975 | * detach a backing block from a page | |
976 | * - cache withdrawal is prevented by the caller | |
977 | */ | |
978 | void cachefiles_uncache_page(struct fscache_object *_object, struct page *page) | |
979 | { | |
980 | struct cachefiles_object *object; | |
981 | struct cachefiles_cache *cache; | |
982 | ||
983 | object = container_of(_object, struct cachefiles_object, fscache); | |
984 | cache = container_of(object->fscache.cache, | |
985 | struct cachefiles_cache, cache); | |
986 | ||
987 | _enter("%p,{%lu}", object, page->index); | |
988 | ||
989 | spin_unlock(&object->fscache.cookie->lock); | |
990 | } |