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