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. 10 * 11 * See Documentation/filesystems/caching/netfs-api.txt for more information on 12 * the netfs API. 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 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 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); 39 spin_lock_init(&cookie->stores_lock); 40 INIT_HLIST_HEAD(&cookie->backing_objects); 41 } 42 43 /* 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 /* We keep the active count elevated until relinquishment to prevent an 99 * attempt to wake up every time the object operations queue quiesces. 100 */ 101 atomic_set(&cookie->n_active, 1); 102 103 atomic_inc(&parent->usage); 104 atomic_inc(&parent->n_children); 105 106 cookie->def = def; 107 cookie->parent = parent; 108 cookie->netfs_data = netfs_data; 109 cookie->flags = 0; 110 111 /* radix tree insertion won't use the preallocation pool unless it's 112 * told it may not wait */ 113 INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_WAIT); 114 115 switch (cookie->def->type) { 116 case FSCACHE_COOKIE_TYPE_INDEX: 117 fscache_stat(&fscache_n_cookie_index); 118 break; 119 case FSCACHE_COOKIE_TYPE_DATAFILE: 120 fscache_stat(&fscache_n_cookie_data); 121 break; 122 default: 123 fscache_stat(&fscache_n_cookie_special); 124 break; 125 } 126 127 /* if the object is an index then we need do nothing more here - we 128 * create indices on disk when we need them as an index may exist in 129 * multiple caches */ 130 if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) { 131 if (fscache_acquire_non_index_cookie(cookie) < 0) { 132 atomic_dec(&parent->n_children); 133 __fscache_cookie_put(cookie); 134 fscache_stat(&fscache_n_acquires_nobufs); 135 _leave(" = NULL"); 136 return NULL; 137 } 138 } 139 140 fscache_stat(&fscache_n_acquires_ok); 141 _leave(" = %p", cookie); 142 return cookie; 143 } 144 EXPORT_SYMBOL(__fscache_acquire_cookie); 145 146 /* 147 * acquire a non-index cookie 148 * - this must make sure the index chain is instantiated and instantiate the 149 * object representation too 150 */ 151 static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie) 152 { 153 struct fscache_object *object; 154 struct fscache_cache *cache; 155 uint64_t i_size; 156 int ret; 157 158 _enter(""); 159 160 cookie->flags = 1 << FSCACHE_COOKIE_UNAVAILABLE; 161 162 /* now we need to see whether the backing objects for this cookie yet 163 * exist, if not there'll be nothing to search */ 164 down_read(&fscache_addremove_sem); 165 166 if (list_empty(&fscache_cache_list)) { 167 up_read(&fscache_addremove_sem); 168 _leave(" = 0 [no caches]"); 169 return 0; 170 } 171 172 /* select a cache in which to store the object */ 173 cache = fscache_select_cache_for_object(cookie->parent); 174 if (!cache) { 175 up_read(&fscache_addremove_sem); 176 fscache_stat(&fscache_n_acquires_no_cache); 177 _leave(" = -ENOMEDIUM [no cache]"); 178 return -ENOMEDIUM; 179 } 180 181 _debug("cache %s", cache->tag->name); 182 183 cookie->flags = 184 (1 << FSCACHE_COOKIE_LOOKING_UP) | 185 (1 << FSCACHE_COOKIE_NO_DATA_YET); 186 187 /* ask the cache to allocate objects for this cookie and its parent 188 * chain */ 189 ret = fscache_alloc_object(cache, cookie); 190 if (ret < 0) { 191 up_read(&fscache_addremove_sem); 192 _leave(" = %d", ret); 193 return ret; 194 } 195 196 /* pass on how big the object we're caching is supposed to be */ 197 cookie->def->get_attr(cookie->netfs_data, &i_size); 198 199 spin_lock(&cookie->lock); 200 if (hlist_empty(&cookie->backing_objects)) { 201 spin_unlock(&cookie->lock); 202 goto unavailable; 203 } 204 205 object = hlist_entry(cookie->backing_objects.first, 206 struct fscache_object, cookie_link); 207 208 fscache_set_store_limit(object, i_size); 209 210 /* initiate the process of looking up all the objects in the chain 211 * (done by fscache_initialise_object()) */ 212 fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD); 213 214 spin_unlock(&cookie->lock); 215 216 /* we may be required to wait for lookup to complete at this point */ 217 if (!fscache_defer_lookup) { 218 _debug("non-deferred lookup %p", &cookie->flags); 219 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP, 220 fscache_wait_bit, TASK_UNINTERRUPTIBLE); 221 _debug("complete"); 222 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags)) 223 goto unavailable; 224 } 225 226 up_read(&fscache_addremove_sem); 227 _leave(" = 0 [deferred]"); 228 return 0; 229 230 unavailable: 231 up_read(&fscache_addremove_sem); 232 _leave(" = -ENOBUFS"); 233 return -ENOBUFS; 234 } 235 236 /* 237 * recursively allocate cache object records for a cookie/cache combination 238 * - caller must be holding the addremove sem 239 */ 240 static int fscache_alloc_object(struct fscache_cache *cache, 241 struct fscache_cookie *cookie) 242 { 243 struct fscache_object *object; 244 int ret; 245 246 _enter("%p,%p{%s}", cache, cookie, cookie->def->name); 247 248 spin_lock(&cookie->lock); 249 hlist_for_each_entry(object, &cookie->backing_objects, 250 cookie_link) { 251 if (object->cache == cache) 252 goto object_already_extant; 253 } 254 spin_unlock(&cookie->lock); 255 256 /* ask the cache to allocate an object (we may end up with duplicate 257 * objects at this stage, but we sort that out later) */ 258 fscache_stat(&fscache_n_cop_alloc_object); 259 object = cache->ops->alloc_object(cache, cookie); 260 fscache_stat_d(&fscache_n_cop_alloc_object); 261 if (IS_ERR(object)) { 262 fscache_stat(&fscache_n_object_no_alloc); 263 ret = PTR_ERR(object); 264 goto error; 265 } 266 267 fscache_stat(&fscache_n_object_alloc); 268 269 object->debug_id = atomic_inc_return(&fscache_object_debug_id); 270 271 _debug("ALLOC OBJ%x: %s {%lx}", 272 object->debug_id, cookie->def->name, object->events); 273 274 ret = fscache_alloc_object(cache, cookie->parent); 275 if (ret < 0) 276 goto error_put; 277 278 /* only attach if we managed to allocate all we needed, otherwise 279 * discard the object we just allocated and instead use the one 280 * attached to the cookie */ 281 if (fscache_attach_object(cookie, object) < 0) { 282 fscache_stat(&fscache_n_cop_put_object); 283 cache->ops->put_object(object); 284 fscache_stat_d(&fscache_n_cop_put_object); 285 } 286 287 _leave(" = 0"); 288 return 0; 289 290 object_already_extant: 291 ret = -ENOBUFS; 292 if (fscache_object_is_dead(object)) { 293 spin_unlock(&cookie->lock); 294 goto error; 295 } 296 spin_unlock(&cookie->lock); 297 _leave(" = 0 [found]"); 298 return 0; 299 300 error_put: 301 fscache_stat(&fscache_n_cop_put_object); 302 cache->ops->put_object(object); 303 fscache_stat_d(&fscache_n_cop_put_object); 304 error: 305 _leave(" = %d", ret); 306 return ret; 307 } 308 309 /* 310 * attach a cache object to a cookie 311 */ 312 static int fscache_attach_object(struct fscache_cookie *cookie, 313 struct fscache_object *object) 314 { 315 struct fscache_object *p; 316 struct fscache_cache *cache = object->cache; 317 int ret; 318 319 _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id); 320 321 spin_lock(&cookie->lock); 322 323 /* there may be multiple initial creations of this object, but we only 324 * want one */ 325 ret = -EEXIST; 326 hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) { 327 if (p->cache == object->cache) { 328 if (fscache_object_is_dying(p)) 329 ret = -ENOBUFS; 330 goto cant_attach_object; 331 } 332 } 333 334 /* pin the parent object */ 335 spin_lock_nested(&cookie->parent->lock, 1); 336 hlist_for_each_entry(p, &cookie->parent->backing_objects, 337 cookie_link) { 338 if (p->cache == object->cache) { 339 if (fscache_object_is_dying(p)) { 340 ret = -ENOBUFS; 341 spin_unlock(&cookie->parent->lock); 342 goto cant_attach_object; 343 } 344 object->parent = p; 345 spin_lock(&p->lock); 346 p->n_children++; 347 spin_unlock(&p->lock); 348 break; 349 } 350 } 351 spin_unlock(&cookie->parent->lock); 352 353 /* attach to the cache's object list */ 354 if (list_empty(&object->cache_link)) { 355 spin_lock(&cache->object_list_lock); 356 list_add(&object->cache_link, &cache->object_list); 357 spin_unlock(&cache->object_list_lock); 358 } 359 360 /* attach to the cookie */ 361 object->cookie = cookie; 362 atomic_inc(&cookie->usage); 363 hlist_add_head(&object->cookie_link, &cookie->backing_objects); 364 365 fscache_objlist_add(object); 366 ret = 0; 367 368 cant_attach_object: 369 spin_unlock(&cookie->lock); 370 _leave(" = %d", ret); 371 return ret; 372 } 373 374 /* 375 * Invalidate an object. Callable with spinlocks held. 376 */ 377 void __fscache_invalidate(struct fscache_cookie *cookie) 378 { 379 struct fscache_object *object; 380 381 _enter("{%s}", cookie->def->name); 382 383 fscache_stat(&fscache_n_invalidates); 384 385 /* Only permit invalidation of data files. Invalidating an index will 386 * require the caller to release all its attachments to the tree rooted 387 * there, and if it's doing that, it may as well just retire the 388 * cookie. 389 */ 390 ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); 391 392 /* We will be updating the cookie too. */ 393 BUG_ON(!cookie->def->get_aux); 394 395 /* If there's an object, we tell the object state machine to handle the 396 * invalidation on our behalf, otherwise there's nothing to do. 397 */ 398 if (!hlist_empty(&cookie->backing_objects)) { 399 spin_lock(&cookie->lock); 400 401 if (!hlist_empty(&cookie->backing_objects) && 402 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING, 403 &cookie->flags)) { 404 object = hlist_entry(cookie->backing_objects.first, 405 struct fscache_object, 406 cookie_link); 407 if (fscache_object_is_live(object)) 408 fscache_raise_event( 409 object, FSCACHE_OBJECT_EV_INVALIDATE); 410 } 411 412 spin_unlock(&cookie->lock); 413 } 414 415 _leave(""); 416 } 417 EXPORT_SYMBOL(__fscache_invalidate); 418 419 /* 420 * Wait for object invalidation to complete. 421 */ 422 void __fscache_wait_on_invalidate(struct fscache_cookie *cookie) 423 { 424 _enter("%p", cookie); 425 426 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING, 427 fscache_wait_bit_interruptible, 428 TASK_UNINTERRUPTIBLE); 429 430 _leave(""); 431 } 432 EXPORT_SYMBOL(__fscache_wait_on_invalidate); 433 434 /* 435 * update the index entries backing a cookie 436 */ 437 void __fscache_update_cookie(struct fscache_cookie *cookie) 438 { 439 struct fscache_object *object; 440 441 fscache_stat(&fscache_n_updates); 442 443 if (!cookie) { 444 fscache_stat(&fscache_n_updates_null); 445 _leave(" [no cookie]"); 446 return; 447 } 448 449 _enter("{%s}", cookie->def->name); 450 451 BUG_ON(!cookie->def->get_aux); 452 453 spin_lock(&cookie->lock); 454 455 /* update the index entry on disk in each cache backing this cookie */ 456 hlist_for_each_entry(object, 457 &cookie->backing_objects, cookie_link) { 458 fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE); 459 } 460 461 spin_unlock(&cookie->lock); 462 _leave(""); 463 } 464 EXPORT_SYMBOL(__fscache_update_cookie); 465 466 /* 467 * release a cookie back to the cache 468 * - the object will be marked as recyclable on disk if retire is true 469 * - all dependents of this cookie must have already been unregistered 470 * (indices/files/pages) 471 */ 472 void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) 473 { 474 struct fscache_object *object; 475 476 fscache_stat(&fscache_n_relinquishes); 477 if (retire) 478 fscache_stat(&fscache_n_relinquishes_retire); 479 480 if (!cookie) { 481 fscache_stat(&fscache_n_relinquishes_null); 482 _leave(" [no cookie]"); 483 return; 484 } 485 486 _enter("%p{%s,%p,%d},%d", 487 cookie, cookie->def->name, cookie->netfs_data, 488 atomic_read(&cookie->n_active), retire); 489 490 ASSERTCMP(atomic_read(&cookie->n_active), >, 0); 491 492 if (atomic_read(&cookie->n_children) != 0) { 493 printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n", 494 cookie->def->name); 495 BUG(); 496 } 497 498 /* No further netfs-accessing operations on this cookie permitted */ 499 set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags); 500 if (retire) 501 set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags); 502 503 spin_lock(&cookie->lock); 504 hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) { 505 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL); 506 } 507 spin_unlock(&cookie->lock); 508 509 /* Wait for cessation of activity requiring access to the netfs (when 510 * n_active reaches 0). 511 */ 512 if (!atomic_dec_and_test(&cookie->n_active)) 513 wait_on_atomic_t(&cookie->n_active, fscache_wait_atomic_t, 514 TASK_UNINTERRUPTIBLE); 515 516 /* Clear pointers back to the netfs */ 517 cookie->netfs_data = NULL; 518 cookie->def = NULL; 519 BUG_ON(cookie->stores.rnode); 520 521 if (cookie->parent) { 522 ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0); 523 ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0); 524 atomic_dec(&cookie->parent->n_children); 525 } 526 527 /* Dispose of the netfs's link to the cookie */ 528 ASSERTCMP(atomic_read(&cookie->usage), >, 0); 529 fscache_cookie_put(cookie); 530 531 _leave(""); 532 } 533 EXPORT_SYMBOL(__fscache_relinquish_cookie); 534 535 /* 536 * destroy a cookie 537 */ 538 void __fscache_cookie_put(struct fscache_cookie *cookie) 539 { 540 struct fscache_cookie *parent; 541 542 _enter("%p", cookie); 543 544 for (;;) { 545 _debug("FREE COOKIE %p", cookie); 546 parent = cookie->parent; 547 BUG_ON(!hlist_empty(&cookie->backing_objects)); 548 kmem_cache_free(fscache_cookie_jar, cookie); 549 550 if (!parent) 551 break; 552 553 cookie = parent; 554 BUG_ON(atomic_read(&cookie->usage) <= 0); 555 if (!atomic_dec_and_test(&cookie->usage)) 556 break; 557 } 558 559 _leave(""); 560 } 561 562 /* 563 * check the consistency between the netfs inode and the backing cache 564 * 565 * NOTE: it only serves no-index type 566 */ 567 int __fscache_check_consistency(struct fscache_cookie *cookie) 568 { 569 struct fscache_operation *op; 570 struct fscache_object *object; 571 int ret; 572 573 _enter("%p,", cookie); 574 575 ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); 576 577 if (fscache_wait_for_deferred_lookup(cookie) < 0) 578 return -ERESTARTSYS; 579 580 if (hlist_empty(&cookie->backing_objects)) 581 return 0; 582 583 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY); 584 if (!op) 585 return -ENOMEM; 586 587 fscache_operation_init(op, NULL, NULL); 588 op->flags = FSCACHE_OP_MYTHREAD | 589 (1 << FSCACHE_OP_WAITING) | 590 (1 << FSCACHE_OP_UNUSE_COOKIE); 591 592 spin_lock(&cookie->lock); 593 594 if (hlist_empty(&cookie->backing_objects)) 595 goto inconsistent; 596 object = hlist_entry(cookie->backing_objects.first, 597 struct fscache_object, cookie_link); 598 if (test_bit(FSCACHE_IOERROR, &object->cache->flags)) 599 goto inconsistent; 600 601 op->debug_id = atomic_inc_return(&fscache_op_debug_id); 602 603 atomic_inc(&cookie->n_active); 604 if (fscache_submit_op(object, op) < 0) 605 goto submit_failed; 606 607 /* the work queue now carries its own ref on the object */ 608 spin_unlock(&cookie->lock); 609 610 ret = fscache_wait_for_operation_activation(object, op, 611 NULL, NULL, NULL); 612 if (ret == 0) { 613 /* ask the cache to honour the operation */ 614 ret = object->cache->ops->check_consistency(op); 615 fscache_op_complete(op, false); 616 } else if (ret == -ENOBUFS) { 617 ret = 0; 618 } 619 620 fscache_put_operation(op); 621 _leave(" = %d", ret); 622 return ret; 623 624 submit_failed: 625 atomic_dec(&cookie->n_active); 626 inconsistent: 627 spin_unlock(&cookie->lock); 628 kfree(op); 629 _leave(" = -ESTALE"); 630 return -ESTALE; 631 } 632 EXPORT_SYMBOL(__fscache_check_consistency); 633