David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1 | /* Cache page management and data I/O routines |
| 2 | * |
| 3 | * Copyright (C) 2004-2008 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 License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #define FSCACHE_DEBUG_LEVEL PAGE |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/fscache-cache.h> |
| 15 | #include <linux/buffer_head.h> |
| 16 | #include <linux/pagevec.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | |
| 20 | /* |
| 21 | * check to see if a page is being written to the cache |
| 22 | */ |
| 23 | bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page) |
| 24 | { |
| 25 | void *val; |
| 26 | |
| 27 | rcu_read_lock(); |
| 28 | val = radix_tree_lookup(&cookie->stores, page->index); |
| 29 | rcu_read_unlock(); |
| 30 | |
| 31 | return val != NULL; |
| 32 | } |
| 33 | EXPORT_SYMBOL(__fscache_check_page_write); |
| 34 | |
| 35 | /* |
| 36 | * wait for a page to finish being written to the cache |
| 37 | */ |
| 38 | void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page) |
| 39 | { |
| 40 | wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0); |
| 41 | |
| 42 | wait_event(*wq, !__fscache_check_page_write(cookie, page)); |
| 43 | } |
| 44 | EXPORT_SYMBOL(__fscache_wait_on_page_write); |
| 45 | |
| 46 | /* |
Milosz Tanski | 9776de9 | 2014-08-13 12:58:16 -0400 | [diff] [blame] | 47 | * wait for a page to finish being written to the cache. Put a timeout here |
| 48 | * since we might be called recursively via parent fs. |
| 49 | */ |
| 50 | static |
| 51 | bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page) |
| 52 | { |
| 53 | wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0); |
| 54 | |
| 55 | return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page), |
| 56 | HZ); |
| 57 | } |
| 58 | |
| 59 | /* |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 60 | * decide whether a page can be released, possibly by cancelling a store to it |
| 61 | * - we're allowed to sleep if __GFP_WAIT is flagged |
| 62 | */ |
| 63 | bool __fscache_maybe_release_page(struct fscache_cookie *cookie, |
| 64 | struct page *page, |
| 65 | gfp_t gfp) |
| 66 | { |
| 67 | struct page *xpage; |
| 68 | void *val; |
| 69 | |
| 70 | _enter("%p,%p,%x", cookie, page, gfp); |
| 71 | |
David Howells | 8c209ce | 2012-12-05 13:34:49 +0000 | [diff] [blame] | 72 | try_again: |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 73 | rcu_read_lock(); |
| 74 | val = radix_tree_lookup(&cookie->stores, page->index); |
| 75 | if (!val) { |
| 76 | rcu_read_unlock(); |
| 77 | fscache_stat(&fscache_n_store_vmscan_not_storing); |
| 78 | __fscache_uncache_page(cookie, page); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | /* see if the page is actually undergoing storage - if so we can't get |
| 83 | * rid of it till the cache has finished with it */ |
| 84 | if (radix_tree_tag_get(&cookie->stores, page->index, |
| 85 | FSCACHE_COOKIE_STORING_TAG)) { |
| 86 | rcu_read_unlock(); |
| 87 | goto page_busy; |
| 88 | } |
| 89 | |
| 90 | /* the page is pending storage, so we attempt to cancel the store and |
| 91 | * discard the store request so that the page can be reclaimed */ |
| 92 | spin_lock(&cookie->stores_lock); |
| 93 | rcu_read_unlock(); |
| 94 | |
| 95 | if (radix_tree_tag_get(&cookie->stores, page->index, |
| 96 | FSCACHE_COOKIE_STORING_TAG)) { |
| 97 | /* the page started to undergo storage whilst we were looking, |
| 98 | * so now we can only wait or return */ |
| 99 | spin_unlock(&cookie->stores_lock); |
| 100 | goto page_busy; |
| 101 | } |
| 102 | |
| 103 | xpage = radix_tree_delete(&cookie->stores, page->index); |
| 104 | spin_unlock(&cookie->stores_lock); |
| 105 | |
| 106 | if (xpage) { |
| 107 | fscache_stat(&fscache_n_store_vmscan_cancelled); |
| 108 | fscache_stat(&fscache_n_store_radix_deletes); |
| 109 | ASSERTCMP(xpage, ==, page); |
| 110 | } else { |
| 111 | fscache_stat(&fscache_n_store_vmscan_gone); |
| 112 | } |
| 113 | |
| 114 | wake_up_bit(&cookie->flags, 0); |
| 115 | if (xpage) |
| 116 | page_cache_release(xpage); |
| 117 | __fscache_uncache_page(cookie, page); |
| 118 | return true; |
| 119 | |
| 120 | page_busy: |
David Howells | 8c209ce | 2012-12-05 13:34:49 +0000 | [diff] [blame] | 121 | /* We will wait here if we're allowed to, but that could deadlock the |
| 122 | * allocator as the work threads writing to the cache may all end up |
| 123 | * sleeping on memory allocation, so we may need to impose a timeout |
| 124 | * too. */ |
David Howells | 0c59a95 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 125 | if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) { |
David Howells | 8c209ce | 2012-12-05 13:34:49 +0000 | [diff] [blame] | 126 | fscache_stat(&fscache_n_store_vmscan_busy); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | fscache_stat(&fscache_n_store_vmscan_wait); |
Milosz Tanski | 9776de9 | 2014-08-13 12:58:16 -0400 | [diff] [blame] | 131 | if (!release_page_wait_timeout(cookie, page)) |
| 132 | _debug("fscache writeout timeout page: %p{%lx}", |
| 133 | page, page->index); |
| 134 | |
David Howells | 8c209ce | 2012-12-05 13:34:49 +0000 | [diff] [blame] | 135 | gfp &= ~__GFP_WAIT; |
| 136 | goto try_again; |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 137 | } |
| 138 | EXPORT_SYMBOL(__fscache_maybe_release_page); |
| 139 | |
| 140 | /* |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 141 | * note that a page has finished being written to the cache |
| 142 | */ |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 143 | static void fscache_end_page_write(struct fscache_object *object, |
| 144 | struct page *page) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 145 | { |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 146 | struct fscache_cookie *cookie; |
| 147 | struct page *xpage = NULL; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 148 | |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 149 | spin_lock(&object->lock); |
| 150 | cookie = object->cookie; |
| 151 | if (cookie) { |
| 152 | /* delete the page from the tree if it is now no longer |
| 153 | * pending */ |
| 154 | spin_lock(&cookie->stores_lock); |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 155 | radix_tree_tag_clear(&cookie->stores, page->index, |
| 156 | FSCACHE_COOKIE_STORING_TAG); |
David Howells | 285e728 | 2009-11-19 18:11:29 +0000 | [diff] [blame] | 157 | if (!radix_tree_tag_get(&cookie->stores, page->index, |
| 158 | FSCACHE_COOKIE_PENDING_TAG)) { |
| 159 | fscache_stat(&fscache_n_store_radix_deletes); |
| 160 | xpage = radix_tree_delete(&cookie->stores, page->index); |
| 161 | } |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 162 | spin_unlock(&cookie->stores_lock); |
| 163 | wake_up_bit(&cookie->flags, 0); |
| 164 | } |
| 165 | spin_unlock(&object->lock); |
| 166 | if (xpage) |
| 167 | page_cache_release(xpage); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * actually apply the changed attributes to a cache object |
| 172 | */ |
| 173 | static void fscache_attr_changed_op(struct fscache_operation *op) |
| 174 | { |
| 175 | struct fscache_object *object = op->object; |
David Howells | 440f0af | 2009-11-19 18:11:01 +0000 | [diff] [blame] | 176 | int ret; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 177 | |
| 178 | _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id); |
| 179 | |
| 180 | fscache_stat(&fscache_n_attr_changed_calls); |
| 181 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 182 | if (fscache_object_is_active(object)) { |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 183 | fscache_stat(&fscache_n_cop_attr_changed); |
David Howells | 440f0af | 2009-11-19 18:11:01 +0000 | [diff] [blame] | 184 | ret = object->cache->ops->attr_changed(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 185 | fscache_stat_d(&fscache_n_cop_attr_changed); |
David Howells | 440f0af | 2009-11-19 18:11:01 +0000 | [diff] [blame] | 186 | if (ret < 0) |
| 187 | fscache_abort_object(object); |
| 188 | } |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 189 | |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 190 | fscache_op_complete(op, true); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 191 | _leave(""); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * notification that the attributes on an object have changed |
| 196 | */ |
| 197 | int __fscache_attr_changed(struct fscache_cookie *cookie) |
| 198 | { |
| 199 | struct fscache_operation *op; |
| 200 | struct fscache_object *object; |
Milosz Tanski | 3e1199d | 2014-08-13 12:58:26 -0400 | [diff] [blame] | 201 | bool wake_cookie = false; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 202 | |
| 203 | _enter("%p", cookie); |
| 204 | |
| 205 | ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX); |
| 206 | |
| 207 | fscache_stat(&fscache_n_attr_changed); |
| 208 | |
| 209 | op = kzalloc(sizeof(*op), GFP_KERNEL); |
| 210 | if (!op) { |
| 211 | fscache_stat(&fscache_n_attr_changed_nomem); |
| 212 | _leave(" = -ENOMEM"); |
| 213 | return -ENOMEM; |
| 214 | } |
| 215 | |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 216 | fscache_operation_init(op, fscache_attr_changed_op, NULL); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 217 | op->flags = FSCACHE_OP_ASYNC | |
| 218 | (1 << FSCACHE_OP_EXCLUSIVE) | |
| 219 | (1 << FSCACHE_OP_UNUSE_COOKIE); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 220 | |
| 221 | spin_lock(&cookie->lock); |
| 222 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 223 | if (!fscache_cookie_enabled(cookie) || |
| 224 | hlist_empty(&cookie->backing_objects)) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 225 | goto nobufs; |
| 226 | object = hlist_entry(cookie->backing_objects.first, |
| 227 | struct fscache_object, cookie_link); |
| 228 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 229 | __fscache_use_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 230 | if (fscache_submit_exclusive_op(object, op) < 0) |
Milosz Tanski | 3e1199d | 2014-08-13 12:58:26 -0400 | [diff] [blame] | 231 | goto nobufs_dec; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 232 | spin_unlock(&cookie->lock); |
| 233 | fscache_stat(&fscache_n_attr_changed_ok); |
| 234 | fscache_put_operation(op); |
| 235 | _leave(" = 0"); |
| 236 | return 0; |
| 237 | |
Milosz Tanski | 3e1199d | 2014-08-13 12:58:26 -0400 | [diff] [blame] | 238 | nobufs_dec: |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 239 | wake_cookie = __fscache_unuse_cookie(cookie); |
Milosz Tanski | 3e1199d | 2014-08-13 12:58:26 -0400 | [diff] [blame] | 240 | nobufs: |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 241 | spin_unlock(&cookie->lock); |
| 242 | kfree(op); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 243 | if (wake_cookie) |
| 244 | __fscache_wake_unused_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 245 | fscache_stat(&fscache_n_attr_changed_nobufs); |
| 246 | _leave(" = %d", -ENOBUFS); |
| 247 | return -ENOBUFS; |
| 248 | } |
| 249 | EXPORT_SYMBOL(__fscache_attr_changed); |
| 250 | |
| 251 | /* |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 252 | * release a retrieval op reference |
| 253 | */ |
| 254 | static void fscache_release_retrieval_op(struct fscache_operation *_op) |
| 255 | { |
| 256 | struct fscache_retrieval *op = |
| 257 | container_of(_op, struct fscache_retrieval, op); |
| 258 | |
| 259 | _enter("{OP%x}", op->op.debug_id); |
| 260 | |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 261 | ASSERTCMP(atomic_read(&op->n_pages), ==, 0); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 262 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 263 | fscache_hist(fscache_retrieval_histogram, op->start_time); |
| 264 | if (op->context) |
| 265 | fscache_put_context(op->op.object->cookie, op->context); |
| 266 | |
| 267 | _leave(""); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * allocate a retrieval op |
| 272 | */ |
| 273 | static struct fscache_retrieval *fscache_alloc_retrieval( |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 274 | struct fscache_cookie *cookie, |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 275 | struct address_space *mapping, |
| 276 | fscache_rw_complete_t end_io_func, |
| 277 | void *context) |
| 278 | { |
| 279 | struct fscache_retrieval *op; |
| 280 | |
| 281 | /* allocate a retrieval operation and attempt to submit it */ |
| 282 | op = kzalloc(sizeof(*op), GFP_NOIO); |
| 283 | if (!op) { |
| 284 | fscache_stat(&fscache_n_retrievals_nomem); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 288 | fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 289 | op->op.flags = FSCACHE_OP_MYTHREAD | |
| 290 | (1UL << FSCACHE_OP_WAITING) | |
| 291 | (1UL << FSCACHE_OP_UNUSE_COOKIE); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 292 | op->mapping = mapping; |
| 293 | op->end_io_func = end_io_func; |
| 294 | op->context = context; |
| 295 | op->start_time = jiffies; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 296 | INIT_LIST_HEAD(&op->to_do); |
| 297 | return op; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * wait for a deferred lookup to complete |
| 302 | */ |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 303 | int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 304 | { |
| 305 | unsigned long jif; |
| 306 | |
| 307 | _enter(""); |
| 308 | |
| 309 | if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) { |
| 310 | _leave(" = 0 [imm]"); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | fscache_stat(&fscache_n_retrievals_wait); |
| 315 | |
| 316 | jif = jiffies; |
| 317 | if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP, |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 318 | TASK_INTERRUPTIBLE) != 0) { |
| 319 | fscache_stat(&fscache_n_retrievals_intr); |
| 320 | _leave(" = -ERESTARTSYS"); |
| 321 | return -ERESTARTSYS; |
| 322 | } |
| 323 | |
| 324 | ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)); |
| 325 | |
| 326 | smp_rmb(); |
| 327 | fscache_hist(fscache_retrieval_delay_histogram, jif); |
| 328 | _leave(" = 0 [dly]"); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
David Howells | 91c7fbb | 2012-12-14 11:02:22 +0000 | [diff] [blame] | 333 | * Handle cancellation of a pending retrieval op |
| 334 | */ |
| 335 | static void fscache_do_cancel_retrieval(struct fscache_operation *_op) |
| 336 | { |
| 337 | struct fscache_retrieval *op = |
| 338 | container_of(_op, struct fscache_retrieval, op); |
| 339 | |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 340 | atomic_set(&op->n_pages, 0); |
David Howells | 91c7fbb | 2012-12-14 11:02:22 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 344 | * wait for an object to become active (or dead) |
| 345 | */ |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 346 | int fscache_wait_for_operation_activation(struct fscache_object *object, |
| 347 | struct fscache_operation *op, |
| 348 | atomic_t *stat_op_waits, |
| 349 | atomic_t *stat_object_dead, |
| 350 | void (*do_cancel)(struct fscache_operation *)) |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 351 | { |
| 352 | int ret; |
| 353 | |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 354 | if (!test_bit(FSCACHE_OP_WAITING, &op->flags)) |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 355 | goto check_if_dead; |
| 356 | |
| 357 | _debug(">>> WT"); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 358 | if (stat_op_waits) |
| 359 | fscache_stat(stat_op_waits); |
| 360 | if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING, |
David Howells | 9c04caa | 2012-12-07 18:08:02 +0000 | [diff] [blame] | 361 | TASK_INTERRUPTIBLE) != 0) { |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 362 | ret = fscache_cancel_op(op, do_cancel); |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 363 | if (ret == 0) |
| 364 | return -ERESTARTSYS; |
| 365 | |
| 366 | /* it's been removed from the pending queue by another party, |
| 367 | * so we should get to run shortly */ |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 368 | wait_on_bit(&op->flags, FSCACHE_OP_WAITING, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 369 | TASK_UNINTERRUPTIBLE); |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 370 | } |
| 371 | _debug("<<< GO"); |
| 372 | |
| 373 | check_if_dead: |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 374 | if (op->state == FSCACHE_OP_ST_CANCELLED) { |
| 375 | if (stat_object_dead) |
| 376 | fscache_stat(stat_object_dead); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 377 | _leave(" = -ENOBUFS [cancelled]"); |
| 378 | return -ENOBUFS; |
| 379 | } |
David Howells | 8702152 | 2015-02-24 10:52:51 +0000 | [diff] [blame^] | 380 | if (unlikely(fscache_object_is_dying(object) || |
| 381 | fscache_cache_is_broken(object))) { |
| 382 | enum fscache_operation_state state = op->state; |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 383 | fscache_cancel_op(op, do_cancel); |
| 384 | if (stat_object_dead) |
| 385 | fscache_stat(stat_object_dead); |
David Howells | 8702152 | 2015-02-24 10:52:51 +0000 | [diff] [blame^] | 386 | _leave(" = -ENOBUFS [obj dead %d]", state); |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 387 | return -ENOBUFS; |
| 388 | } |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 393 | * read a page from the cache or allocate a block in which to store it |
| 394 | * - we return: |
| 395 | * -ENOMEM - out of memory, nothing done |
| 396 | * -ERESTARTSYS - interrupted |
| 397 | * -ENOBUFS - no backing object available in which to cache the block |
| 398 | * -ENODATA - no data available in the backing object for this block |
| 399 | * 0 - dispatched a read - it'll call end_io_func() when finished |
| 400 | */ |
| 401 | int __fscache_read_or_alloc_page(struct fscache_cookie *cookie, |
| 402 | struct page *page, |
| 403 | fscache_rw_complete_t end_io_func, |
| 404 | void *context, |
| 405 | gfp_t gfp) |
| 406 | { |
| 407 | struct fscache_retrieval *op; |
| 408 | struct fscache_object *object; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 409 | bool wake_cookie = false; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 410 | int ret; |
| 411 | |
| 412 | _enter("%p,%p,,,", cookie, page); |
| 413 | |
| 414 | fscache_stat(&fscache_n_retrievals); |
| 415 | |
| 416 | if (hlist_empty(&cookie->backing_objects)) |
| 417 | goto nobufs; |
| 418 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 419 | if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) { |
| 420 | _leave(" = -ENOBUFS [invalidating]"); |
| 421 | return -ENOBUFS; |
| 422 | } |
| 423 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 424 | ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX); |
| 425 | ASSERTCMP(page, !=, NULL); |
| 426 | |
| 427 | if (fscache_wait_for_deferred_lookup(cookie) < 0) |
| 428 | return -ERESTARTSYS; |
| 429 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 430 | op = fscache_alloc_retrieval(cookie, page->mapping, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 431 | end_io_func, context); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 432 | if (!op) { |
| 433 | _leave(" = -ENOMEM"); |
| 434 | return -ENOMEM; |
| 435 | } |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 436 | atomic_set(&op->n_pages, 1); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 437 | |
| 438 | spin_lock(&cookie->lock); |
| 439 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 440 | if (!fscache_cookie_enabled(cookie) || |
| 441 | hlist_empty(&cookie->backing_objects)) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 442 | goto nobufs_unlock; |
| 443 | object = hlist_entry(cookie->backing_objects.first, |
| 444 | struct fscache_object, cookie_link); |
| 445 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 446 | ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 447 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 448 | __fscache_use_cookie(cookie); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 449 | atomic_inc(&object->n_reads); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 450 | __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 451 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 452 | if (fscache_submit_op(object, &op->op) < 0) |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 453 | goto nobufs_unlock_dec; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 454 | spin_unlock(&cookie->lock); |
| 455 | |
| 456 | fscache_stat(&fscache_n_retrieval_ops); |
| 457 | |
| 458 | /* pin the netfs read context in case we need to do the actual netfs |
| 459 | * read because we've encountered a cache read failure */ |
| 460 | fscache_get_context(object->cookie, op->context); |
| 461 | |
| 462 | /* we wait for the operation to become active, and then process it |
| 463 | * *here*, in this thread, and not in the thread pool */ |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 464 | ret = fscache_wait_for_operation_activation( |
| 465 | object, &op->op, |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 466 | __fscache_stat(&fscache_n_retrieval_op_waits), |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 467 | __fscache_stat(&fscache_n_retrievals_object_dead), |
| 468 | fscache_do_cancel_retrieval); |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 469 | if (ret < 0) |
| 470 | goto error; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 471 | |
| 472 | /* ask the cache to honour the operation */ |
| 473 | if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) { |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 474 | fscache_stat(&fscache_n_cop_allocate_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 475 | ret = object->cache->ops->allocate_page(op, page, gfp); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 476 | fscache_stat_d(&fscache_n_cop_allocate_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 477 | if (ret == 0) |
| 478 | ret = -ENODATA; |
| 479 | } else { |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 480 | fscache_stat(&fscache_n_cop_read_or_alloc_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 481 | ret = object->cache->ops->read_or_alloc_page(op, page, gfp); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 482 | fscache_stat_d(&fscache_n_cop_read_or_alloc_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 483 | } |
| 484 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 485 | error: |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 486 | if (ret == -ENOMEM) |
| 487 | fscache_stat(&fscache_n_retrievals_nomem); |
| 488 | else if (ret == -ERESTARTSYS) |
| 489 | fscache_stat(&fscache_n_retrievals_intr); |
| 490 | else if (ret == -ENODATA) |
| 491 | fscache_stat(&fscache_n_retrievals_nodata); |
| 492 | else if (ret < 0) |
| 493 | fscache_stat(&fscache_n_retrievals_nobufs); |
| 494 | else |
| 495 | fscache_stat(&fscache_n_retrievals_ok); |
| 496 | |
| 497 | fscache_put_retrieval(op); |
| 498 | _leave(" = %d", ret); |
| 499 | return ret; |
| 500 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 501 | nobufs_unlock_dec: |
| 502 | atomic_dec(&object->n_reads); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 503 | wake_cookie = __fscache_unuse_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 504 | nobufs_unlock: |
| 505 | spin_unlock(&cookie->lock); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 506 | if (wake_cookie) |
| 507 | __fscache_wake_unused_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 508 | kfree(op); |
| 509 | nobufs: |
| 510 | fscache_stat(&fscache_n_retrievals_nobufs); |
| 511 | _leave(" = -ENOBUFS"); |
| 512 | return -ENOBUFS; |
| 513 | } |
| 514 | EXPORT_SYMBOL(__fscache_read_or_alloc_page); |
| 515 | |
| 516 | /* |
| 517 | * read a list of page from the cache or allocate a block in which to store |
| 518 | * them |
| 519 | * - we return: |
| 520 | * -ENOMEM - out of memory, some pages may be being read |
| 521 | * -ERESTARTSYS - interrupted, some pages may be being read |
| 522 | * -ENOBUFS - no backing object or space available in which to cache any |
| 523 | * pages not being read |
| 524 | * -ENODATA - no data available in the backing object for some or all of |
| 525 | * the pages |
| 526 | * 0 - dispatched a read on all pages |
| 527 | * |
| 528 | * end_io_func() will be called for each page read from the cache as it is |
| 529 | * finishes being read |
| 530 | * |
| 531 | * any pages for which a read is dispatched will be removed from pages and |
| 532 | * nr_pages |
| 533 | */ |
| 534 | int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie, |
| 535 | struct address_space *mapping, |
| 536 | struct list_head *pages, |
| 537 | unsigned *nr_pages, |
| 538 | fscache_rw_complete_t end_io_func, |
| 539 | void *context, |
| 540 | gfp_t gfp) |
| 541 | { |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 542 | struct fscache_retrieval *op; |
| 543 | struct fscache_object *object; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 544 | bool wake_cookie = false; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 545 | int ret; |
| 546 | |
| 547 | _enter("%p,,%d,,,", cookie, *nr_pages); |
| 548 | |
| 549 | fscache_stat(&fscache_n_retrievals); |
| 550 | |
| 551 | if (hlist_empty(&cookie->backing_objects)) |
| 552 | goto nobufs; |
| 553 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 554 | if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) { |
| 555 | _leave(" = -ENOBUFS [invalidating]"); |
| 556 | return -ENOBUFS; |
| 557 | } |
| 558 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 559 | ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX); |
| 560 | ASSERTCMP(*nr_pages, >, 0); |
| 561 | ASSERT(!list_empty(pages)); |
| 562 | |
| 563 | if (fscache_wait_for_deferred_lookup(cookie) < 0) |
| 564 | return -ERESTARTSYS; |
| 565 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 566 | op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 567 | if (!op) |
| 568 | return -ENOMEM; |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 569 | atomic_set(&op->n_pages, *nr_pages); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 570 | |
| 571 | spin_lock(&cookie->lock); |
| 572 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 573 | if (!fscache_cookie_enabled(cookie) || |
| 574 | hlist_empty(&cookie->backing_objects)) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 575 | goto nobufs_unlock; |
| 576 | object = hlist_entry(cookie->backing_objects.first, |
| 577 | struct fscache_object, cookie_link); |
| 578 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 579 | __fscache_use_cookie(cookie); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 580 | atomic_inc(&object->n_reads); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 581 | __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 582 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 583 | if (fscache_submit_op(object, &op->op) < 0) |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 584 | goto nobufs_unlock_dec; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 585 | spin_unlock(&cookie->lock); |
| 586 | |
| 587 | fscache_stat(&fscache_n_retrieval_ops); |
| 588 | |
| 589 | /* pin the netfs read context in case we need to do the actual netfs |
| 590 | * read because we've encountered a cache read failure */ |
| 591 | fscache_get_context(object->cookie, op->context); |
| 592 | |
| 593 | /* we wait for the operation to become active, and then process it |
| 594 | * *here*, in this thread, and not in the thread pool */ |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 595 | ret = fscache_wait_for_operation_activation( |
| 596 | object, &op->op, |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 597 | __fscache_stat(&fscache_n_retrieval_op_waits), |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 598 | __fscache_stat(&fscache_n_retrievals_object_dead), |
| 599 | fscache_do_cancel_retrieval); |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 600 | if (ret < 0) |
| 601 | goto error; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 602 | |
| 603 | /* ask the cache to honour the operation */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 604 | if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) { |
| 605 | fscache_stat(&fscache_n_cop_allocate_pages); |
| 606 | ret = object->cache->ops->allocate_pages( |
| 607 | op, pages, nr_pages, gfp); |
| 608 | fscache_stat_d(&fscache_n_cop_allocate_pages); |
| 609 | } else { |
| 610 | fscache_stat(&fscache_n_cop_read_or_alloc_pages); |
| 611 | ret = object->cache->ops->read_or_alloc_pages( |
| 612 | op, pages, nr_pages, gfp); |
| 613 | fscache_stat_d(&fscache_n_cop_read_or_alloc_pages); |
| 614 | } |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 615 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 616 | error: |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 617 | if (ret == -ENOMEM) |
| 618 | fscache_stat(&fscache_n_retrievals_nomem); |
| 619 | else if (ret == -ERESTARTSYS) |
| 620 | fscache_stat(&fscache_n_retrievals_intr); |
| 621 | else if (ret == -ENODATA) |
| 622 | fscache_stat(&fscache_n_retrievals_nodata); |
| 623 | else if (ret < 0) |
| 624 | fscache_stat(&fscache_n_retrievals_nobufs); |
| 625 | else |
| 626 | fscache_stat(&fscache_n_retrievals_ok); |
| 627 | |
| 628 | fscache_put_retrieval(op); |
| 629 | _leave(" = %d", ret); |
| 630 | return ret; |
| 631 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 632 | nobufs_unlock_dec: |
| 633 | atomic_dec(&object->n_reads); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 634 | wake_cookie = __fscache_unuse_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 635 | nobufs_unlock: |
| 636 | spin_unlock(&cookie->lock); |
| 637 | kfree(op); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 638 | if (wake_cookie) |
| 639 | __fscache_wake_unused_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 640 | nobufs: |
| 641 | fscache_stat(&fscache_n_retrievals_nobufs); |
| 642 | _leave(" = -ENOBUFS"); |
| 643 | return -ENOBUFS; |
| 644 | } |
| 645 | EXPORT_SYMBOL(__fscache_read_or_alloc_pages); |
| 646 | |
| 647 | /* |
| 648 | * allocate a block in the cache on which to store a page |
| 649 | * - we return: |
| 650 | * -ENOMEM - out of memory, nothing done |
| 651 | * -ERESTARTSYS - interrupted |
| 652 | * -ENOBUFS - no backing object available in which to cache the block |
| 653 | * 0 - block allocated |
| 654 | */ |
| 655 | int __fscache_alloc_page(struct fscache_cookie *cookie, |
| 656 | struct page *page, |
| 657 | gfp_t gfp) |
| 658 | { |
| 659 | struct fscache_retrieval *op; |
| 660 | struct fscache_object *object; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 661 | bool wake_cookie = false; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 662 | int ret; |
| 663 | |
| 664 | _enter("%p,%p,,,", cookie, page); |
| 665 | |
| 666 | fscache_stat(&fscache_n_allocs); |
| 667 | |
| 668 | if (hlist_empty(&cookie->backing_objects)) |
| 669 | goto nobufs; |
| 670 | |
| 671 | ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX); |
| 672 | ASSERTCMP(page, !=, NULL); |
| 673 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 674 | if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) { |
| 675 | _leave(" = -ENOBUFS [invalidating]"); |
| 676 | return -ENOBUFS; |
| 677 | } |
| 678 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 679 | if (fscache_wait_for_deferred_lookup(cookie) < 0) |
| 680 | return -ERESTARTSYS; |
| 681 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 682 | op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 683 | if (!op) |
| 684 | return -ENOMEM; |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 685 | atomic_set(&op->n_pages, 1); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 686 | |
| 687 | spin_lock(&cookie->lock); |
| 688 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 689 | if (!fscache_cookie_enabled(cookie) || |
| 690 | hlist_empty(&cookie->backing_objects)) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 691 | goto nobufs_unlock; |
| 692 | object = hlist_entry(cookie->backing_objects.first, |
| 693 | struct fscache_object, cookie_link); |
| 694 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 695 | __fscache_use_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 696 | if (fscache_submit_op(object, &op->op) < 0) |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 697 | goto nobufs_unlock_dec; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 698 | spin_unlock(&cookie->lock); |
| 699 | |
| 700 | fscache_stat(&fscache_n_alloc_ops); |
| 701 | |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 702 | ret = fscache_wait_for_operation_activation( |
| 703 | object, &op->op, |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 704 | __fscache_stat(&fscache_n_alloc_op_waits), |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 705 | __fscache_stat(&fscache_n_allocs_object_dead), |
| 706 | fscache_do_cancel_retrieval); |
David Howells | 60d543c | 2009-11-19 18:11:45 +0000 | [diff] [blame] | 707 | if (ret < 0) |
| 708 | goto error; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 709 | |
| 710 | /* ask the cache to honour the operation */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 711 | fscache_stat(&fscache_n_cop_allocate_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 712 | ret = object->cache->ops->allocate_page(op, page, gfp); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 713 | fscache_stat_d(&fscache_n_cop_allocate_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 714 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 715 | error: |
| 716 | if (ret == -ERESTARTSYS) |
| 717 | fscache_stat(&fscache_n_allocs_intr); |
| 718 | else if (ret < 0) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 719 | fscache_stat(&fscache_n_allocs_nobufs); |
| 720 | else |
| 721 | fscache_stat(&fscache_n_allocs_ok); |
| 722 | |
| 723 | fscache_put_retrieval(op); |
| 724 | _leave(" = %d", ret); |
| 725 | return ret; |
| 726 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 727 | nobufs_unlock_dec: |
| 728 | wake_cookie = __fscache_unuse_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 729 | nobufs_unlock: |
| 730 | spin_unlock(&cookie->lock); |
| 731 | kfree(op); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 732 | if (wake_cookie) |
| 733 | __fscache_wake_unused_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 734 | nobufs: |
| 735 | fscache_stat(&fscache_n_allocs_nobufs); |
| 736 | _leave(" = -ENOBUFS"); |
| 737 | return -ENOBUFS; |
| 738 | } |
| 739 | EXPORT_SYMBOL(__fscache_alloc_page); |
| 740 | |
| 741 | /* |
Milosz Tanski | 5a6f282 | 2013-08-21 17:30:11 -0400 | [diff] [blame] | 742 | * Unmark pages allocate in the readahead code path (via: |
| 743 | * fscache_readpages_or_alloc) after delegating to the base filesystem |
| 744 | */ |
| 745 | void __fscache_readpages_cancel(struct fscache_cookie *cookie, |
| 746 | struct list_head *pages) |
| 747 | { |
| 748 | struct page *page; |
| 749 | |
| 750 | list_for_each_entry(page, pages, lru) { |
| 751 | if (PageFsCache(page)) |
| 752 | __fscache_uncache_page(cookie, page); |
| 753 | } |
| 754 | } |
| 755 | EXPORT_SYMBOL(__fscache_readpages_cancel); |
| 756 | |
| 757 | /* |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 758 | * release a write op reference |
| 759 | */ |
| 760 | static void fscache_release_write_op(struct fscache_operation *_op) |
| 761 | { |
| 762 | _enter("{OP%x}", _op->debug_id); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * perform the background storage of a page into the cache |
| 767 | */ |
| 768 | static void fscache_write_op(struct fscache_operation *_op) |
| 769 | { |
| 770 | struct fscache_storage *op = |
| 771 | container_of(_op, struct fscache_storage, op); |
| 772 | struct fscache_object *object = op->op.object; |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 773 | struct fscache_cookie *cookie; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 774 | struct page *page; |
| 775 | unsigned n; |
| 776 | void *results[1]; |
| 777 | int ret; |
| 778 | |
| 779 | _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage)); |
| 780 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 781 | spin_lock(&object->lock); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 782 | cookie = object->cookie; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 783 | |
David Howells | 7ef001e | 2012-12-07 10:41:26 +0000 | [diff] [blame] | 784 | if (!fscache_object_is_active(object)) { |
| 785 | /* If we get here, then the on-disk cache object likely longer |
| 786 | * exists, so we should just cancel this write operation. |
| 787 | */ |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 788 | spin_unlock(&object->lock); |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 789 | fscache_op_complete(&op->op, false); |
David Howells | 7ef001e | 2012-12-07 10:41:26 +0000 | [diff] [blame] | 790 | _leave(" [inactive]"); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | if (!cookie) { |
| 795 | /* If we get here, then the cookie belonging to the object was |
| 796 | * detached, probably by the cookie being withdrawn due to |
| 797 | * memory pressure, which means that the pages we might write |
| 798 | * to the cache from no longer exist - therefore, we can just |
| 799 | * cancel this write operation. |
| 800 | */ |
| 801 | spin_unlock(&object->lock); |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 802 | fscache_op_complete(&op->op, false); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 803 | _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}", |
| 804 | _op->flags, _op->state, object->state->short_name, |
| 805 | object->flags); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 806 | return; |
| 807 | } |
| 808 | |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 809 | spin_lock(&cookie->stores_lock); |
| 810 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 811 | fscache_stat(&fscache_n_store_calls); |
| 812 | |
| 813 | /* find a page to store */ |
| 814 | page = NULL; |
| 815 | n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1, |
| 816 | FSCACHE_COOKIE_PENDING_TAG); |
| 817 | if (n != 1) |
| 818 | goto superseded; |
| 819 | page = results[0]; |
| 820 | _debug("gang %d [%lx]", n, page->index); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 821 | if (page->index > op->store_limit) { |
| 822 | fscache_stat(&fscache_n_store_pages_over_limit); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 823 | goto superseded; |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 824 | } |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 825 | |
Dan Carpenter | 08a6685 | 2010-06-01 20:58:22 +0100 | [diff] [blame] | 826 | radix_tree_tag_set(&cookie->stores, page->index, |
| 827 | FSCACHE_COOKIE_STORING_TAG); |
| 828 | radix_tree_tag_clear(&cookie->stores, page->index, |
| 829 | FSCACHE_COOKIE_PENDING_TAG); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 830 | |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 831 | spin_unlock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 832 | spin_unlock(&object->lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 833 | |
Dan Carpenter | 08a6685 | 2010-06-01 20:58:22 +0100 | [diff] [blame] | 834 | fscache_stat(&fscache_n_store_pages); |
| 835 | fscache_stat(&fscache_n_cop_write_page); |
| 836 | ret = object->cache->ops->write_page(op, page); |
| 837 | fscache_stat_d(&fscache_n_cop_write_page); |
Dan Carpenter | 08a6685 | 2010-06-01 20:58:22 +0100 | [diff] [blame] | 838 | fscache_end_page_write(object, page); |
| 839 | if (ret < 0) { |
Dan Carpenter | 08a6685 | 2010-06-01 20:58:22 +0100 | [diff] [blame] | 840 | fscache_abort_object(object); |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 841 | fscache_op_complete(&op->op, true); |
Dan Carpenter | 08a6685 | 2010-06-01 20:58:22 +0100 | [diff] [blame] | 842 | } else { |
| 843 | fscache_enqueue_operation(&op->op); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | _leave(""); |
| 847 | return; |
| 848 | |
| 849 | superseded: |
| 850 | /* this writer is going away and there aren't any more things to |
| 851 | * write */ |
| 852 | _debug("cease"); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 853 | spin_unlock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 854 | clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); |
| 855 | spin_unlock(&object->lock); |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 856 | fscache_op_complete(&op->op, true); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 857 | _leave(""); |
| 858 | } |
| 859 | |
| 860 | /* |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 861 | * Clear the pages pending writing for invalidation |
| 862 | */ |
| 863 | void fscache_invalidate_writes(struct fscache_cookie *cookie) |
| 864 | { |
| 865 | struct page *page; |
| 866 | void *results[16]; |
| 867 | int n, i; |
| 868 | |
| 869 | _enter(""); |
| 870 | |
Sebastian Andrzej Siewior | ee8be57 | 2013-05-10 19:50:24 +0100 | [diff] [blame] | 871 | for (;;) { |
| 872 | spin_lock(&cookie->stores_lock); |
| 873 | n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, |
| 874 | ARRAY_SIZE(results), |
| 875 | FSCACHE_COOKIE_PENDING_TAG); |
| 876 | if (n == 0) { |
| 877 | spin_unlock(&cookie->stores_lock); |
| 878 | break; |
| 879 | } |
| 880 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 881 | for (i = n - 1; i >= 0; i--) { |
| 882 | page = results[i]; |
| 883 | radix_tree_delete(&cookie->stores, page->index); |
| 884 | } |
| 885 | |
| 886 | spin_unlock(&cookie->stores_lock); |
| 887 | |
| 888 | for (i = n - 1; i >= 0; i--) |
| 889 | page_cache_release(results[i]); |
| 890 | } |
| 891 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 892 | _leave(""); |
| 893 | } |
| 894 | |
| 895 | /* |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 896 | * request a page be stored in the cache |
| 897 | * - returns: |
| 898 | * -ENOMEM - out of memory, nothing done |
| 899 | * -ENOBUFS - no backing object available in which to cache the page |
| 900 | * 0 - dispatched a write - it'll call end_io_func() when finished |
| 901 | * |
| 902 | * if the cookie still has a backing object at this point, that object can be |
| 903 | * in one of a few states with respect to storage processing: |
| 904 | * |
| 905 | * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is |
| 906 | * set) |
| 907 | * |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 908 | * (a) no writes yet |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 909 | * |
| 910 | * (b) writes deferred till post-creation (mark page for writing and |
| 911 | * return immediately) |
| 912 | * |
| 913 | * (2) negative lookup, object created, initial fill being made from netfs |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 914 | * |
| 915 | * (a) fill point not yet reached this page (mark page for writing and |
| 916 | * return) |
| 917 | * |
| 918 | * (b) fill point passed this page (queue op to store this page) |
| 919 | * |
| 920 | * (3) object extant (queue op to store this page) |
| 921 | * |
| 922 | * any other state is invalid |
| 923 | */ |
| 924 | int __fscache_write_page(struct fscache_cookie *cookie, |
| 925 | struct page *page, |
| 926 | gfp_t gfp) |
| 927 | { |
| 928 | struct fscache_storage *op; |
| 929 | struct fscache_object *object; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 930 | bool wake_cookie = false; |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 931 | int ret; |
| 932 | |
| 933 | _enter("%p,%x,", cookie, (u32) page->flags); |
| 934 | |
| 935 | ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX); |
| 936 | ASSERT(PageFsCache(page)); |
| 937 | |
| 938 | fscache_stat(&fscache_n_stores); |
| 939 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 940 | if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) { |
| 941 | _leave(" = -ENOBUFS [invalidating]"); |
| 942 | return -ENOBUFS; |
| 943 | } |
| 944 | |
David Howells | 5f4f9f4 | 2012-12-20 21:52:33 +0000 | [diff] [blame] | 945 | op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 946 | if (!op) |
| 947 | goto nomem; |
| 948 | |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 949 | fscache_operation_init(&op->op, fscache_write_op, |
| 950 | fscache_release_write_op); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 951 | op->op.flags = FSCACHE_OP_ASYNC | |
| 952 | (1 << FSCACHE_OP_WAITING) | |
| 953 | (1 << FSCACHE_OP_UNUSE_COOKIE); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 954 | |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 955 | ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 956 | if (ret < 0) |
| 957 | goto nomem_free; |
| 958 | |
| 959 | ret = -ENOBUFS; |
| 960 | spin_lock(&cookie->lock); |
| 961 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 962 | if (!fscache_cookie_enabled(cookie) || |
| 963 | hlist_empty(&cookie->backing_objects)) |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 964 | goto nobufs; |
| 965 | object = hlist_entry(cookie->backing_objects.first, |
| 966 | struct fscache_object, cookie_link); |
| 967 | if (test_bit(FSCACHE_IOERROR, &object->cache->flags)) |
| 968 | goto nobufs; |
| 969 | |
| 970 | /* add the page to the pending-storage radix tree on the backing |
| 971 | * object */ |
| 972 | spin_lock(&object->lock); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 973 | spin_lock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 974 | |
| 975 | _debug("store limit %llx", (unsigned long long) object->store_limit); |
| 976 | |
| 977 | ret = radix_tree_insert(&cookie->stores, page->index, page); |
| 978 | if (ret < 0) { |
| 979 | if (ret == -EEXIST) |
| 980 | goto already_queued; |
| 981 | _debug("insert failed %d", ret); |
| 982 | goto nobufs_unlock_obj; |
| 983 | } |
| 984 | |
| 985 | radix_tree_tag_set(&cookie->stores, page->index, |
| 986 | FSCACHE_COOKIE_PENDING_TAG); |
| 987 | page_cache_get(page); |
| 988 | |
| 989 | /* we only want one writer at a time, but we do need to queue new |
| 990 | * writers after exclusive ops */ |
| 991 | if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags)) |
| 992 | goto already_pending; |
| 993 | |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 994 | spin_unlock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 995 | spin_unlock(&object->lock); |
| 996 | |
| 997 | op->op.debug_id = atomic_inc_return(&fscache_op_debug_id); |
| 998 | op->store_limit = object->store_limit; |
| 999 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 1000 | __fscache_use_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1001 | if (fscache_submit_op(object, &op->op) < 0) |
| 1002 | goto submit_failed; |
| 1003 | |
| 1004 | spin_unlock(&cookie->lock); |
| 1005 | radix_tree_preload_end(); |
| 1006 | fscache_stat(&fscache_n_store_ops); |
| 1007 | fscache_stat(&fscache_n_stores_ok); |
| 1008 | |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 1009 | /* the work queue now carries its own ref on the object */ |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1010 | fscache_put_operation(&op->op); |
| 1011 | _leave(" = 0"); |
| 1012 | return 0; |
| 1013 | |
| 1014 | already_queued: |
| 1015 | fscache_stat(&fscache_n_stores_again); |
| 1016 | already_pending: |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 1017 | spin_unlock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1018 | spin_unlock(&object->lock); |
| 1019 | spin_unlock(&cookie->lock); |
| 1020 | radix_tree_preload_end(); |
| 1021 | kfree(op); |
| 1022 | fscache_stat(&fscache_n_stores_ok); |
| 1023 | _leave(" = 0"); |
| 1024 | return 0; |
| 1025 | |
| 1026 | submit_failed: |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 1027 | spin_lock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1028 | radix_tree_delete(&cookie->stores, page->index); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 1029 | spin_unlock(&cookie->stores_lock); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 1030 | wake_cookie = __fscache_unuse_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1031 | page_cache_release(page); |
| 1032 | ret = -ENOBUFS; |
| 1033 | goto nobufs; |
| 1034 | |
| 1035 | nobufs_unlock_obj: |
Dan Carpenter | 1147d0f | 2010-03-23 14:48:37 +0000 | [diff] [blame] | 1036 | spin_unlock(&cookie->stores_lock); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1037 | spin_unlock(&object->lock); |
| 1038 | nobufs: |
| 1039 | spin_unlock(&cookie->lock); |
| 1040 | radix_tree_preload_end(); |
| 1041 | kfree(op); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 1042 | if (wake_cookie) |
| 1043 | __fscache_wake_unused_cookie(cookie); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1044 | fscache_stat(&fscache_n_stores_nobufs); |
| 1045 | _leave(" = -ENOBUFS"); |
| 1046 | return -ENOBUFS; |
| 1047 | |
| 1048 | nomem_free: |
| 1049 | kfree(op); |
| 1050 | nomem: |
| 1051 | fscache_stat(&fscache_n_stores_oom); |
| 1052 | _leave(" = -ENOMEM"); |
| 1053 | return -ENOMEM; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(__fscache_write_page); |
| 1056 | |
| 1057 | /* |
| 1058 | * remove a page from the cache |
| 1059 | */ |
| 1060 | void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page) |
| 1061 | { |
| 1062 | struct fscache_object *object; |
| 1063 | |
| 1064 | _enter(",%p", page); |
| 1065 | |
| 1066 | ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX); |
| 1067 | ASSERTCMP(page, !=, NULL); |
| 1068 | |
| 1069 | fscache_stat(&fscache_n_uncaches); |
| 1070 | |
| 1071 | /* cache withdrawal may beat us to it */ |
| 1072 | if (!PageFsCache(page)) |
| 1073 | goto done; |
| 1074 | |
| 1075 | /* get the object */ |
| 1076 | spin_lock(&cookie->lock); |
| 1077 | |
| 1078 | if (hlist_empty(&cookie->backing_objects)) { |
| 1079 | ClearPageFsCache(page); |
| 1080 | goto done_unlock; |
| 1081 | } |
| 1082 | |
| 1083 | object = hlist_entry(cookie->backing_objects.first, |
| 1084 | struct fscache_object, cookie_link); |
| 1085 | |
| 1086 | /* there might now be stuff on disk we could read */ |
| 1087 | clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); |
| 1088 | |
| 1089 | /* only invoke the cache backend if we managed to mark the page |
| 1090 | * uncached here; this deals with synchronisation vs withdrawal */ |
| 1091 | if (TestClearPageFsCache(page) && |
| 1092 | object->cache->ops->uncache_page) { |
| 1093 | /* the cache backend releases the cookie lock */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 1094 | fscache_stat(&fscache_n_cop_uncache_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1095 | object->cache->ops->uncache_page(object, page); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 1096 | fscache_stat_d(&fscache_n_cop_uncache_page); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1097 | goto done; |
| 1098 | } |
| 1099 | |
| 1100 | done_unlock: |
| 1101 | spin_unlock(&cookie->lock); |
| 1102 | done: |
| 1103 | _leave(""); |
| 1104 | } |
| 1105 | EXPORT_SYMBOL(__fscache_uncache_page); |
| 1106 | |
| 1107 | /** |
David Howells | c4d6d8d | 2012-12-20 21:52:32 +0000 | [diff] [blame] | 1108 | * fscache_mark_page_cached - Mark a page as being cached |
| 1109 | * @op: The retrieval op pages are being marked for |
| 1110 | * @page: The page to be marked |
| 1111 | * |
| 1112 | * Mark a netfs page as being cached. After this is called, the netfs |
| 1113 | * must call fscache_uncache_page() to remove the mark. |
| 1114 | */ |
| 1115 | void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page) |
| 1116 | { |
| 1117 | struct fscache_cookie *cookie = op->op.object->cookie; |
| 1118 | |
| 1119 | #ifdef CONFIG_FSCACHE_STATS |
| 1120 | atomic_inc(&fscache_n_marks); |
| 1121 | #endif |
| 1122 | |
| 1123 | _debug("- mark %p{%lx}", page, page->index); |
| 1124 | if (TestSetPageFsCache(page)) { |
| 1125 | static bool once_only; |
| 1126 | if (!once_only) { |
| 1127 | once_only = true; |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 1128 | pr_warn("Cookie type %s marked page %lx multiple times\n", |
| 1129 | cookie->def->name, page->index); |
David Howells | c4d6d8d | 2012-12-20 21:52:32 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (cookie->def->mark_page_cached) |
| 1134 | cookie->def->mark_page_cached(cookie->netfs_data, |
| 1135 | op->mapping, page); |
| 1136 | } |
| 1137 | EXPORT_SYMBOL(fscache_mark_page_cached); |
| 1138 | |
| 1139 | /** |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1140 | * fscache_mark_pages_cached - Mark pages as being cached |
| 1141 | * @op: The retrieval op pages are being marked for |
| 1142 | * @pagevec: The pages to be marked |
| 1143 | * |
| 1144 | * Mark a bunch of netfs pages as being cached. After this is called, |
| 1145 | * the netfs must call fscache_uncache_page() to remove the mark. |
| 1146 | */ |
| 1147 | void fscache_mark_pages_cached(struct fscache_retrieval *op, |
| 1148 | struct pagevec *pagevec) |
| 1149 | { |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1150 | unsigned long loop; |
| 1151 | |
David Howells | c4d6d8d | 2012-12-20 21:52:32 +0000 | [diff] [blame] | 1152 | for (loop = 0; loop < pagevec->nr; loop++) |
| 1153 | fscache_mark_page_cached(op, pagevec->pages[loop]); |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1154 | |
David Howells | b510882 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1155 | pagevec_reinit(pagevec); |
| 1156 | } |
| 1157 | EXPORT_SYMBOL(fscache_mark_pages_cached); |
David Howells | c902ce1 | 2011-07-07 12:19:48 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* |
| 1160 | * Uncache all the pages in an inode that are marked PG_fscache, assuming them |
| 1161 | * to be associated with the given cookie. |
| 1162 | */ |
| 1163 | void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie, |
| 1164 | struct inode *inode) |
| 1165 | { |
| 1166 | struct address_space *mapping = inode->i_mapping; |
| 1167 | struct pagevec pvec; |
| 1168 | pgoff_t next; |
| 1169 | int i; |
| 1170 | |
| 1171 | _enter("%p,%p", cookie, inode); |
| 1172 | |
| 1173 | if (!mapping || mapping->nrpages == 0) { |
| 1174 | _leave(" [no pages]"); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | pagevec_init(&pvec, 0); |
| 1179 | next = 0; |
Jan Beulich | b307d46 | 2011-07-21 15:02:43 +0100 | [diff] [blame] | 1180 | do { |
| 1181 | if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) |
| 1182 | break; |
David Howells | c902ce1 | 2011-07-07 12:19:48 +0100 | [diff] [blame] | 1183 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 1184 | struct page *page = pvec.pages[i]; |
Jan Beulich | b307d46 | 2011-07-21 15:02:43 +0100 | [diff] [blame] | 1185 | next = page->index; |
David Howells | c902ce1 | 2011-07-07 12:19:48 +0100 | [diff] [blame] | 1186 | if (PageFsCache(page)) { |
| 1187 | __fscache_wait_on_page_write(cookie, page); |
| 1188 | __fscache_uncache_page(cookie, page); |
| 1189 | } |
| 1190 | } |
| 1191 | pagevec_release(&pvec); |
| 1192 | cond_resched(); |
Jan Beulich | b307d46 | 2011-07-21 15:02:43 +0100 | [diff] [blame] | 1193 | } while (++next); |
David Howells | c902ce1 | 2011-07-07 12:19:48 +0100 | [diff] [blame] | 1194 | |
| 1195 | _leave(""); |
| 1196 | } |
| 1197 | EXPORT_SYMBOL(__fscache_uncache_all_inode_pages); |