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