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