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