David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 1 | /* netfs cookie management |
| 2 | * |
| 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 10 | * |
| 11 | * See Documentation/filesystems/caching/netfs-api.txt for more information on |
| 12 | * the netfs API. |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define FSCACHE_DEBUG_LEVEL COOKIE |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include "internal.h" |
| 19 | |
| 20 | struct kmem_cache *fscache_cookie_jar; |
| 21 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 22 | static atomic_t fscache_object_debug_id = ATOMIC_INIT(0); |
| 23 | |
| 24 | static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie); |
| 25 | static int fscache_alloc_object(struct fscache_cache *cache, |
| 26 | struct fscache_cookie *cookie); |
| 27 | static int fscache_attach_object(struct fscache_cookie *cookie, |
| 28 | struct fscache_object *object); |
| 29 | |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 30 | /* |
| 31 | * initialise an cookie jar slab element prior to any use |
| 32 | */ |
| 33 | void fscache_cookie_init_once(void *_cookie) |
| 34 | { |
| 35 | struct fscache_cookie *cookie = _cookie; |
| 36 | |
| 37 | memset(cookie, 0, sizeof(*cookie)); |
| 38 | spin_lock_init(&cookie->lock); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 39 | spin_lock_init(&cookie->stores_lock); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 40 | INIT_HLIST_HEAD(&cookie->backing_objects); |
| 41 | } |
| 42 | |
| 43 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 44 | * request a cookie to represent an object (index, datafile, xattr, etc) |
| 45 | * - parent specifies the parent object |
| 46 | * - the top level index cookie for each netfs is stored in the fscache_netfs |
| 47 | * struct upon registration |
| 48 | * - def points to the definition |
| 49 | * - the netfs_data will be passed to the functions pointed to in *def |
| 50 | * - all attached caches will be searched to see if they contain this object |
| 51 | * - index objects aren't stored on disk until there's a dependent file that |
| 52 | * needs storing |
| 53 | * - other objects are stored in a selected cache immediately, and all the |
| 54 | * indices forming the path to it are instantiated if necessary |
| 55 | * - we never let on to the netfs about errors |
| 56 | * - we may set a negative cookie pointer, but that's okay |
| 57 | */ |
| 58 | struct fscache_cookie *__fscache_acquire_cookie( |
| 59 | struct fscache_cookie *parent, |
| 60 | const struct fscache_cookie_def *def, |
| 61 | void *netfs_data) |
| 62 | { |
| 63 | struct fscache_cookie *cookie; |
| 64 | |
| 65 | BUG_ON(!def); |
| 66 | |
| 67 | _enter("{%s},{%s},%p", |
| 68 | parent ? (char *) parent->def->name : "<no-parent>", |
| 69 | def->name, netfs_data); |
| 70 | |
| 71 | fscache_stat(&fscache_n_acquires); |
| 72 | |
| 73 | /* if there's no parent cookie, then we don't create one here either */ |
| 74 | if (!parent) { |
| 75 | fscache_stat(&fscache_n_acquires_null); |
| 76 | _leave(" [no parent]"); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | /* validate the definition */ |
| 81 | BUG_ON(!def->get_key); |
| 82 | BUG_ON(!def->name[0]); |
| 83 | |
| 84 | BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX && |
| 85 | parent->def->type != FSCACHE_COOKIE_TYPE_INDEX); |
| 86 | |
| 87 | /* allocate and initialise a cookie */ |
| 88 | cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL); |
| 89 | if (!cookie) { |
| 90 | fscache_stat(&fscache_n_acquires_oom); |
| 91 | _leave(" [ENOMEM]"); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | atomic_set(&cookie->usage, 1); |
| 96 | atomic_set(&cookie->n_children, 0); |
| 97 | |
| 98 | atomic_inc(&parent->usage); |
| 99 | atomic_inc(&parent->n_children); |
| 100 | |
| 101 | cookie->def = def; |
| 102 | cookie->parent = parent; |
| 103 | cookie->netfs_data = netfs_data; |
| 104 | cookie->flags = 0; |
| 105 | |
David Howells | b34df79 | 2009-11-19 18:11:14 +0000 | [diff] [blame] | 106 | /* radix tree insertion won't use the preallocation pool unless it's |
| 107 | * told it may not wait */ |
| 108 | INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_WAIT); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 109 | |
| 110 | switch (cookie->def->type) { |
| 111 | case FSCACHE_COOKIE_TYPE_INDEX: |
| 112 | fscache_stat(&fscache_n_cookie_index); |
| 113 | break; |
| 114 | case FSCACHE_COOKIE_TYPE_DATAFILE: |
| 115 | fscache_stat(&fscache_n_cookie_data); |
| 116 | break; |
| 117 | default: |
| 118 | fscache_stat(&fscache_n_cookie_special); |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | /* if the object is an index then we need do nothing more here - we |
| 123 | * create indices on disk when we need them as an index may exist in |
| 124 | * multiple caches */ |
| 125 | if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) { |
| 126 | if (fscache_acquire_non_index_cookie(cookie) < 0) { |
| 127 | atomic_dec(&parent->n_children); |
| 128 | __fscache_cookie_put(cookie); |
| 129 | fscache_stat(&fscache_n_acquires_nobufs); |
| 130 | _leave(" = NULL"); |
| 131 | return NULL; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fscache_stat(&fscache_n_acquires_ok); |
| 136 | _leave(" = %p", cookie); |
| 137 | return cookie; |
| 138 | } |
| 139 | EXPORT_SYMBOL(__fscache_acquire_cookie); |
| 140 | |
| 141 | /* |
| 142 | * acquire a non-index cookie |
| 143 | * - this must make sure the index chain is instantiated and instantiate the |
| 144 | * object representation too |
| 145 | */ |
| 146 | static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie) |
| 147 | { |
| 148 | struct fscache_object *object; |
| 149 | struct fscache_cache *cache; |
| 150 | uint64_t i_size; |
| 151 | int ret; |
| 152 | |
| 153 | _enter(""); |
| 154 | |
| 155 | cookie->flags = 1 << FSCACHE_COOKIE_UNAVAILABLE; |
| 156 | |
| 157 | /* now we need to see whether the backing objects for this cookie yet |
| 158 | * exist, if not there'll be nothing to search */ |
| 159 | down_read(&fscache_addremove_sem); |
| 160 | |
| 161 | if (list_empty(&fscache_cache_list)) { |
| 162 | up_read(&fscache_addremove_sem); |
| 163 | _leave(" = 0 [no caches]"); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /* select a cache in which to store the object */ |
| 168 | cache = fscache_select_cache_for_object(cookie->parent); |
| 169 | if (!cache) { |
| 170 | up_read(&fscache_addremove_sem); |
| 171 | fscache_stat(&fscache_n_acquires_no_cache); |
| 172 | _leave(" = -ENOMEDIUM [no cache]"); |
| 173 | return -ENOMEDIUM; |
| 174 | } |
| 175 | |
| 176 | _debug("cache %s", cache->tag->name); |
| 177 | |
| 178 | cookie->flags = |
| 179 | (1 << FSCACHE_COOKIE_LOOKING_UP) | |
| 180 | (1 << FSCACHE_COOKIE_CREATING) | |
| 181 | (1 << FSCACHE_COOKIE_NO_DATA_YET); |
| 182 | |
| 183 | /* ask the cache to allocate objects for this cookie and its parent |
| 184 | * chain */ |
| 185 | ret = fscache_alloc_object(cache, cookie); |
| 186 | if (ret < 0) { |
| 187 | up_read(&fscache_addremove_sem); |
| 188 | _leave(" = %d", ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* pass on how big the object we're caching is supposed to be */ |
| 193 | cookie->def->get_attr(cookie->netfs_data, &i_size); |
| 194 | |
| 195 | spin_lock(&cookie->lock); |
| 196 | if (hlist_empty(&cookie->backing_objects)) { |
| 197 | spin_unlock(&cookie->lock); |
| 198 | goto unavailable; |
| 199 | } |
| 200 | |
| 201 | object = hlist_entry(cookie->backing_objects.first, |
| 202 | struct fscache_object, cookie_link); |
| 203 | |
| 204 | fscache_set_store_limit(object, i_size); |
| 205 | |
| 206 | /* initiate the process of looking up all the objects in the chain |
| 207 | * (done by fscache_initialise_object()) */ |
| 208 | fscache_enqueue_object(object); |
| 209 | |
| 210 | spin_unlock(&cookie->lock); |
| 211 | |
| 212 | /* we may be required to wait for lookup to complete at this point */ |
| 213 | if (!fscache_defer_lookup) { |
| 214 | _debug("non-deferred lookup %p", &cookie->flags); |
| 215 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP, |
| 216 | fscache_wait_bit, TASK_UNINTERRUPTIBLE); |
| 217 | _debug("complete"); |
| 218 | if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags)) |
| 219 | goto unavailable; |
| 220 | } |
| 221 | |
| 222 | up_read(&fscache_addremove_sem); |
| 223 | _leave(" = 0 [deferred]"); |
| 224 | return 0; |
| 225 | |
| 226 | unavailable: |
| 227 | up_read(&fscache_addremove_sem); |
| 228 | _leave(" = -ENOBUFS"); |
| 229 | return -ENOBUFS; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * recursively allocate cache object records for a cookie/cache combination |
| 234 | * - caller must be holding the addremove sem |
| 235 | */ |
| 236 | static int fscache_alloc_object(struct fscache_cache *cache, |
| 237 | struct fscache_cookie *cookie) |
| 238 | { |
| 239 | struct fscache_object *object; |
| 240 | struct hlist_node *_n; |
| 241 | int ret; |
| 242 | |
| 243 | _enter("%p,%p{%s}", cache, cookie, cookie->def->name); |
| 244 | |
| 245 | spin_lock(&cookie->lock); |
| 246 | hlist_for_each_entry(object, _n, &cookie->backing_objects, |
| 247 | cookie_link) { |
| 248 | if (object->cache == cache) |
| 249 | goto object_already_extant; |
| 250 | } |
| 251 | spin_unlock(&cookie->lock); |
| 252 | |
| 253 | /* ask the cache to allocate an object (we may end up with duplicate |
| 254 | * objects at this stage, but we sort that out later) */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 255 | fscache_stat(&fscache_n_cop_alloc_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 256 | object = cache->ops->alloc_object(cache, cookie); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 257 | fscache_stat_d(&fscache_n_cop_alloc_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 258 | if (IS_ERR(object)) { |
| 259 | fscache_stat(&fscache_n_object_no_alloc); |
| 260 | ret = PTR_ERR(object); |
| 261 | goto error; |
| 262 | } |
| 263 | |
| 264 | fscache_stat(&fscache_n_object_alloc); |
| 265 | |
| 266 | object->debug_id = atomic_inc_return(&fscache_object_debug_id); |
| 267 | |
| 268 | _debug("ALLOC OBJ%x: %s {%lx}", |
| 269 | object->debug_id, cookie->def->name, object->events); |
| 270 | |
| 271 | ret = fscache_alloc_object(cache, cookie->parent); |
| 272 | if (ret < 0) |
| 273 | goto error_put; |
| 274 | |
| 275 | /* only attach if we managed to allocate all we needed, otherwise |
| 276 | * discard the object we just allocated and instead use the one |
| 277 | * attached to the cookie */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 278 | if (fscache_attach_object(cookie, object) < 0) { |
| 279 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 280 | cache->ops->put_object(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 281 | fscache_stat_d(&fscache_n_cop_put_object); |
| 282 | } |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 283 | |
| 284 | _leave(" = 0"); |
| 285 | return 0; |
| 286 | |
| 287 | object_already_extant: |
| 288 | ret = -ENOBUFS; |
| 289 | if (object->state >= FSCACHE_OBJECT_DYING) { |
| 290 | spin_unlock(&cookie->lock); |
| 291 | goto error; |
| 292 | } |
| 293 | spin_unlock(&cookie->lock); |
| 294 | _leave(" = 0 [found]"); |
| 295 | return 0; |
| 296 | |
| 297 | error_put: |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 298 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 299 | cache->ops->put_object(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 300 | fscache_stat_d(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 301 | error: |
| 302 | _leave(" = %d", ret); |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * attach a cache object to a cookie |
| 308 | */ |
| 309 | static int fscache_attach_object(struct fscache_cookie *cookie, |
| 310 | struct fscache_object *object) |
| 311 | { |
| 312 | struct fscache_object *p; |
| 313 | struct fscache_cache *cache = object->cache; |
| 314 | struct hlist_node *_n; |
| 315 | int ret; |
| 316 | |
| 317 | _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id); |
| 318 | |
| 319 | spin_lock(&cookie->lock); |
| 320 | |
| 321 | /* there may be multiple initial creations of this object, but we only |
| 322 | * want one */ |
| 323 | ret = -EEXIST; |
| 324 | hlist_for_each_entry(p, _n, &cookie->backing_objects, cookie_link) { |
| 325 | if (p->cache == object->cache) { |
| 326 | if (p->state >= FSCACHE_OBJECT_DYING) |
| 327 | ret = -ENOBUFS; |
| 328 | goto cant_attach_object; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* pin the parent object */ |
| 333 | spin_lock_nested(&cookie->parent->lock, 1); |
| 334 | hlist_for_each_entry(p, _n, &cookie->parent->backing_objects, |
| 335 | cookie_link) { |
| 336 | if (p->cache == object->cache) { |
| 337 | if (p->state >= FSCACHE_OBJECT_DYING) { |
| 338 | ret = -ENOBUFS; |
| 339 | spin_unlock(&cookie->parent->lock); |
| 340 | goto cant_attach_object; |
| 341 | } |
| 342 | object->parent = p; |
| 343 | spin_lock(&p->lock); |
| 344 | p->n_children++; |
| 345 | spin_unlock(&p->lock); |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | spin_unlock(&cookie->parent->lock); |
| 350 | |
| 351 | /* attach to the cache's object list */ |
| 352 | if (list_empty(&object->cache_link)) { |
| 353 | spin_lock(&cache->object_list_lock); |
| 354 | list_add(&object->cache_link, &cache->object_list); |
| 355 | spin_unlock(&cache->object_list_lock); |
| 356 | } |
| 357 | |
| 358 | /* attach to the cookie */ |
| 359 | object->cookie = cookie; |
| 360 | atomic_inc(&cookie->usage); |
| 361 | hlist_add_head(&object->cookie_link, &cookie->backing_objects); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 362 | |
| 363 | fscache_objlist_add(object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 364 | ret = 0; |
| 365 | |
| 366 | cant_attach_object: |
| 367 | spin_unlock(&cookie->lock); |
| 368 | _leave(" = %d", ret); |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | /* |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 373 | * Invalidate an object. Callable with spinlocks held. |
| 374 | */ |
| 375 | void __fscache_invalidate(struct fscache_cookie *cookie) |
| 376 | { |
| 377 | struct fscache_object *object; |
| 378 | |
| 379 | _enter("{%s}", cookie->def->name); |
| 380 | |
| 381 | fscache_stat(&fscache_n_invalidates); |
| 382 | |
| 383 | /* Only permit invalidation of data files. Invalidating an index will |
| 384 | * require the caller to release all its attachments to the tree rooted |
| 385 | * there, and if it's doing that, it may as well just retire the |
| 386 | * cookie. |
| 387 | */ |
| 388 | ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); |
| 389 | |
| 390 | /* We will be updating the cookie too. */ |
| 391 | BUG_ON(!cookie->def->get_aux); |
| 392 | |
| 393 | /* If there's an object, we tell the object state machine to handle the |
| 394 | * invalidation on our behalf, otherwise there's nothing to do. |
| 395 | */ |
| 396 | if (!hlist_empty(&cookie->backing_objects)) { |
| 397 | spin_lock(&cookie->lock); |
| 398 | |
| 399 | if (!hlist_empty(&cookie->backing_objects) && |
| 400 | !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING, |
| 401 | &cookie->flags)) { |
| 402 | object = hlist_entry(cookie->backing_objects.first, |
| 403 | struct fscache_object, |
| 404 | cookie_link); |
| 405 | if (object->state < FSCACHE_OBJECT_DYING) |
| 406 | fscache_raise_event( |
| 407 | object, FSCACHE_OBJECT_EV_INVALIDATE); |
| 408 | } |
| 409 | |
| 410 | spin_unlock(&cookie->lock); |
| 411 | } |
| 412 | |
| 413 | _leave(""); |
| 414 | } |
| 415 | EXPORT_SYMBOL(__fscache_invalidate); |
| 416 | |
| 417 | /* |
| 418 | * Wait for object invalidation to complete. |
| 419 | */ |
| 420 | void __fscache_wait_on_invalidate(struct fscache_cookie *cookie) |
| 421 | { |
| 422 | _enter("%p", cookie); |
| 423 | |
| 424 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING, |
| 425 | fscache_wait_bit_interruptible, |
| 426 | TASK_UNINTERRUPTIBLE); |
| 427 | |
| 428 | _leave(""); |
| 429 | } |
| 430 | EXPORT_SYMBOL(__fscache_wait_on_invalidate); |
| 431 | |
| 432 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 433 | * update the index entries backing a cookie |
| 434 | */ |
| 435 | void __fscache_update_cookie(struct fscache_cookie *cookie) |
| 436 | { |
| 437 | struct fscache_object *object; |
| 438 | struct hlist_node *_p; |
| 439 | |
| 440 | fscache_stat(&fscache_n_updates); |
| 441 | |
| 442 | if (!cookie) { |
| 443 | fscache_stat(&fscache_n_updates_null); |
| 444 | _leave(" [no cookie]"); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | _enter("{%s}", cookie->def->name); |
| 449 | |
| 450 | BUG_ON(!cookie->def->get_aux); |
| 451 | |
| 452 | spin_lock(&cookie->lock); |
| 453 | |
| 454 | /* update the index entry on disk in each cache backing this cookie */ |
| 455 | hlist_for_each_entry(object, _p, |
| 456 | &cookie->backing_objects, cookie_link) { |
| 457 | fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE); |
| 458 | } |
| 459 | |
| 460 | spin_unlock(&cookie->lock); |
| 461 | _leave(""); |
| 462 | } |
| 463 | EXPORT_SYMBOL(__fscache_update_cookie); |
| 464 | |
| 465 | /* |
| 466 | * release a cookie back to the cache |
| 467 | * - the object will be marked as recyclable on disk if retire is true |
| 468 | * - all dependents of this cookie must have already been unregistered |
| 469 | * (indices/files/pages) |
| 470 | */ |
| 471 | void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) |
| 472 | { |
| 473 | struct fscache_cache *cache; |
| 474 | struct fscache_object *object; |
| 475 | unsigned long event; |
| 476 | |
| 477 | fscache_stat(&fscache_n_relinquishes); |
David Howells | 2175bb0 | 2009-11-19 18:11:38 +0000 | [diff] [blame] | 478 | if (retire) |
| 479 | fscache_stat(&fscache_n_relinquishes_retire); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 480 | |
| 481 | if (!cookie) { |
| 482 | fscache_stat(&fscache_n_relinquishes_null); |
| 483 | _leave(" [no cookie]"); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | _enter("%p{%s,%p},%d", |
| 488 | cookie, cookie->def->name, cookie->netfs_data, retire); |
| 489 | |
| 490 | if (atomic_read(&cookie->n_children) != 0) { |
| 491 | printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n", |
| 492 | cookie->def->name); |
| 493 | BUG(); |
| 494 | } |
| 495 | |
| 496 | /* wait for the cookie to finish being instantiated (or to fail) */ |
| 497 | if (test_bit(FSCACHE_COOKIE_CREATING, &cookie->flags)) { |
| 498 | fscache_stat(&fscache_n_relinquishes_waitcrt); |
| 499 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_CREATING, |
| 500 | fscache_wait_bit, TASK_UNINTERRUPTIBLE); |
| 501 | } |
| 502 | |
| 503 | event = retire ? FSCACHE_OBJECT_EV_RETIRE : FSCACHE_OBJECT_EV_RELEASE; |
| 504 | |
David Howells | ef46ed8 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 505 | try_again: |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 506 | spin_lock(&cookie->lock); |
| 507 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 508 | /* break links with all the active objects */ |
| 509 | while (!hlist_empty(&cookie->backing_objects)) { |
David Howells | ef46ed8 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 510 | int n_reads; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 511 | object = hlist_entry(cookie->backing_objects.first, |
| 512 | struct fscache_object, |
| 513 | cookie_link); |
| 514 | |
| 515 | _debug("RELEASE OBJ%x", object->debug_id); |
| 516 | |
David Howells | ef46ed8 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 517 | set_bit(FSCACHE_COOKIE_WAITING_ON_READS, &cookie->flags); |
| 518 | n_reads = atomic_read(&object->n_reads); |
| 519 | if (n_reads) { |
| 520 | int n_ops = object->n_ops; |
| 521 | int n_in_progress = object->n_in_progress; |
David Howells | 0f972b5 | 2012-12-20 21:52:33 +0000 | [diff] [blame] | 522 | spin_unlock(&cookie->lock); |
| 523 | printk(KERN_ERR "FS-Cache:" |
David Howells | ef46ed8 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 524 | " Cookie '%s' still has %d outstanding reads (%d,%d)\n", |
| 525 | cookie->def->name, |
| 526 | n_reads, n_ops, n_in_progress); |
| 527 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_WAITING_ON_READS, |
| 528 | fscache_wait_bit, TASK_UNINTERRUPTIBLE); |
| 529 | printk("Wait finished\n"); |
| 530 | goto try_again; |
David Howells | 0f972b5 | 2012-12-20 21:52:33 +0000 | [diff] [blame] | 531 | } |
| 532 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 533 | /* detach each cache object from the object cookie */ |
| 534 | spin_lock(&object->lock); |
| 535 | hlist_del_init(&object->cookie_link); |
| 536 | |
| 537 | cache = object->cache; |
| 538 | object->cookie = NULL; |
| 539 | fscache_raise_event(object, event); |
| 540 | spin_unlock(&object->lock); |
| 541 | |
| 542 | if (atomic_dec_and_test(&cookie->usage)) |
| 543 | /* the cookie refcount shouldn't be reduced to 0 yet */ |
| 544 | BUG(); |
| 545 | } |
| 546 | |
David Howells | 7e311a2 | 2009-11-19 18:11:11 +0000 | [diff] [blame] | 547 | /* detach pointers back to the netfs */ |
| 548 | cookie->netfs_data = NULL; |
| 549 | cookie->def = NULL; |
| 550 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 551 | spin_unlock(&cookie->lock); |
| 552 | |
| 553 | if (cookie->parent) { |
| 554 | ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0); |
| 555 | ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0); |
| 556 | atomic_dec(&cookie->parent->n_children); |
| 557 | } |
| 558 | |
| 559 | /* finally dispose of the cookie */ |
| 560 | ASSERTCMP(atomic_read(&cookie->usage), >, 0); |
| 561 | fscache_cookie_put(cookie); |
| 562 | |
| 563 | _leave(""); |
| 564 | } |
| 565 | EXPORT_SYMBOL(__fscache_relinquish_cookie); |
| 566 | |
| 567 | /* |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 568 | * destroy a cookie |
| 569 | */ |
| 570 | void __fscache_cookie_put(struct fscache_cookie *cookie) |
| 571 | { |
| 572 | struct fscache_cookie *parent; |
| 573 | |
| 574 | _enter("%p", cookie); |
| 575 | |
| 576 | for (;;) { |
| 577 | _debug("FREE COOKIE %p", cookie); |
| 578 | parent = cookie->parent; |
| 579 | BUG_ON(!hlist_empty(&cookie->backing_objects)); |
| 580 | kmem_cache_free(fscache_cookie_jar, cookie); |
| 581 | |
| 582 | if (!parent) |
| 583 | break; |
| 584 | |
| 585 | cookie = parent; |
| 586 | BUG_ON(atomic_read(&cookie->usage) <= 0); |
| 587 | if (!atomic_dec_and_test(&cookie->usage)) |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | _leave(""); |
| 592 | } |