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