1 /* netfs cookie management
3 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
11 * See Documentation/filesystems/caching/netfs-api.txt for more information on
15 #define FSCACHE_DEBUG_LEVEL COOKIE
16 #include <linux/module.h>
17 #include <linux/slab.h>
20 struct kmem_cache *fscache_cookie_jar;
22 static atomic_t fscache_object_debug_id = ATOMIC_INIT(0);
24 #define fscache_cookie_hash_shift 15
25 static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift];
27 static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
29 static int fscache_alloc_object(struct fscache_cache *cache,
30 struct fscache_cookie *cookie);
31 static int fscache_attach_object(struct fscache_cookie *cookie,
32 struct fscache_object *object);
34 static void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
36 struct hlist_node *object;
40 pr_err("%c-cookie c=%p [p=%p fl=%lx nc=%u na=%u]\n",
41 prefix, cookie, cookie->parent, cookie->flags,
42 atomic_read(&cookie->n_children),
43 atomic_read(&cookie->n_active));
44 pr_err("%c-cookie d=%p n=%p\n",
45 prefix, cookie->def, cookie->netfs_data);
47 object = READ_ONCE(cookie->backing_objects.first);
49 pr_err("%c-cookie o=%p\n",
50 prefix, hlist_entry(object, struct fscache_object, cookie_link));
52 pr_err("%c-key=[%u] '", prefix, cookie->key_len);
53 k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
54 cookie->inline_key : cookie->key;
55 for (loop = 0; loop < cookie->key_len; loop++)
56 pr_cont("%02x", k[loop]);
60 void fscache_free_cookie(struct fscache_cookie *cookie)
63 BUG_ON(!hlist_empty(&cookie->backing_objects));
64 if (cookie->aux_len > sizeof(cookie->inline_aux))
66 if (cookie->key_len > sizeof(cookie->inline_key))
68 kmem_cache_free(fscache_cookie_jar, cookie);
73 * Set the index key in a cookie. The cookie struct has space for a 16-byte
74 * key plus length and hash, but if that's not big enough, it's instead a
75 * pointer to a buffer containing 3 bytes of hash, 1 byte of length and then
78 static int fscache_set_key(struct fscache_cookie *cookie,
79 const void *index_key, size_t index_key_len)
84 bufs = DIV_ROUND_UP(index_key_len, sizeof(*buf));
86 if (index_key_len > sizeof(cookie->inline_key)) {
87 buf = kcalloc(bufs, sizeof(*buf), GFP_KERNEL);
92 buf = (u32 *)cookie->inline_key;
95 memcpy(buf, index_key, index_key_len);
96 cookie->key_hash = fscache_hash(0, buf, bufs);
100 static long fscache_compare_cookie(const struct fscache_cookie *a,
101 const struct fscache_cookie *b)
105 if (a->key_hash != b->key_hash)
106 return (long)a->key_hash - (long)b->key_hash;
107 if (a->parent != b->parent)
108 return (long)a->parent - (long)b->parent;
109 if (a->key_len != b->key_len)
110 return (long)a->key_len - (long)b->key_len;
111 if (a->type != b->type)
112 return (long)a->type - (long)b->type;
114 if (a->key_len <= sizeof(a->inline_key)) {
121 return memcmp(ka, kb, a->key_len);
127 struct fscache_cookie *fscache_alloc_cookie(
128 struct fscache_cookie *parent,
129 const struct fscache_cookie_def *def,
130 const void *index_key, size_t index_key_len,
131 const void *aux_data, size_t aux_data_len,
135 struct fscache_cookie *cookie;
137 /* allocate and initialise a cookie */
138 cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
142 cookie->key_len = index_key_len;
143 cookie->aux_len = aux_data_len;
145 if (fscache_set_key(cookie, index_key, index_key_len) < 0)
148 if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
149 memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
151 cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
156 atomic_set(&cookie->usage, 1);
157 atomic_set(&cookie->n_children, 0);
159 /* We keep the active count elevated until relinquishment to prevent an
160 * attempt to wake up every time the object operations queue quiesces.
162 atomic_set(&cookie->n_active, 1);
165 cookie->parent = parent;
166 cookie->netfs_data = netfs_data;
167 cookie->flags = (1 << FSCACHE_COOKIE_NO_DATA_YET);
168 cookie->type = def->type;
169 spin_lock_init(&cookie->lock);
170 spin_lock_init(&cookie->stores_lock);
171 INIT_HLIST_HEAD(&cookie->backing_objects);
173 /* radix tree insertion won't use the preallocation pool unless it's
174 * told it may not wait */
175 INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
179 fscache_free_cookie(cookie);
184 * Attempt to insert the new cookie into the hash. If there's a collision, we
185 * return the old cookie if it's not in use and an error otherwise.
187 struct fscache_cookie *fscache_hash_cookie(struct fscache_cookie *candidate)
189 struct fscache_cookie *cursor;
190 struct hlist_bl_head *h;
191 struct hlist_bl_node *p;
194 bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
195 h = &fscache_cookie_hash[bucket];
198 hlist_bl_for_each_entry(cursor, p, h, hash_link) {
199 if (fscache_compare_cookie(candidate, cursor) == 0)
203 __set_bit(FSCACHE_COOKIE_ACQUIRED, &candidate->flags);
204 fscache_cookie_get(candidate->parent, fscache_cookie_get_acquire_parent);
205 atomic_inc(&candidate->parent->n_children);
206 hlist_bl_add_head(&candidate->hash_link, h);
211 if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED, &cursor->flags)) {
212 trace_fscache_cookie(cursor, fscache_cookie_collision,
213 atomic_read(&cursor->usage));
214 pr_err("Duplicate cookie detected\n");
215 fscache_print_cookie(cursor, 'O');
216 fscache_print_cookie(candidate, 'N');
221 fscache_cookie_get(cursor, fscache_cookie_get_reacquire);
227 * request a cookie to represent an object (index, datafile, xattr, etc)
228 * - parent specifies the parent object
229 * - the top level index cookie for each netfs is stored in the fscache_netfs
230 * struct upon registration
231 * - def points to the definition
232 * - the netfs_data will be passed to the functions pointed to in *def
233 * - all attached caches will be searched to see if they contain this object
234 * - index objects aren't stored on disk until there's a dependent file that
236 * - other objects are stored in a selected cache immediately, and all the
237 * indices forming the path to it are instantiated if necessary
238 * - we never let on to the netfs about errors
239 * - we may set a negative cookie pointer, but that's okay
241 struct fscache_cookie *__fscache_acquire_cookie(
242 struct fscache_cookie *parent,
243 const struct fscache_cookie_def *def,
244 const void *index_key, size_t index_key_len,
245 const void *aux_data, size_t aux_data_len,
250 struct fscache_cookie *candidate, *cookie;
254 _enter("{%s},{%s},%p,%u",
255 parent ? (char *) parent->def->name : "<no-parent>",
256 def->name, netfs_data, enable);
258 if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
260 if (!aux_data || !aux_data_len) {
265 fscache_stat(&fscache_n_acquires);
267 /* if there's no parent cookie, then we don't create one here either */
269 fscache_stat(&fscache_n_acquires_null);
270 _leave(" [no parent]");
274 /* validate the definition */
275 BUG_ON(!def->name[0]);
277 BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
278 parent->type != FSCACHE_COOKIE_TYPE_INDEX);
280 candidate = fscache_alloc_cookie(parent, def,
281 index_key, index_key_len,
282 aux_data, aux_data_len,
283 netfs_data, object_size);
285 fscache_stat(&fscache_n_acquires_oom);
290 cookie = fscache_hash_cookie(candidate);
292 trace_fscache_cookie(candidate, fscache_cookie_discard, 1);
296 if (cookie == candidate)
299 switch (cookie->type) {
300 case FSCACHE_COOKIE_TYPE_INDEX:
301 fscache_stat(&fscache_n_cookie_index);
303 case FSCACHE_COOKIE_TYPE_DATAFILE:
304 fscache_stat(&fscache_n_cookie_data);
307 fscache_stat(&fscache_n_cookie_special);
311 trace_fscache_acquire(cookie);
314 /* if the object is an index then we need do nothing more here
315 * - we create indices on disk when we need them as an index
316 * may exist in multiple caches */
317 if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
318 if (fscache_acquire_non_index_cookie(cookie, object_size) == 0) {
319 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
321 atomic_dec(&parent->n_children);
322 fscache_cookie_put(cookie,
323 fscache_cookie_put_acquire_nobufs);
324 fscache_stat(&fscache_n_acquires_nobufs);
329 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
333 fscache_stat(&fscache_n_acquires_ok);
336 fscache_free_cookie(candidate);
339 EXPORT_SYMBOL(__fscache_acquire_cookie);
342 * Enable a cookie to permit it to accept new operations.
344 void __fscache_enable_cookie(struct fscache_cookie *cookie,
345 const void *aux_data,
347 bool (*can_enable)(void *data),
350 _enter("%p", cookie);
352 trace_fscache_enable(cookie);
354 wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
355 TASK_UNINTERRUPTIBLE);
357 fscache_update_aux(cookie, aux_data);
359 if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
362 if (can_enable && !can_enable(data)) {
363 /* The netfs decided it didn't want to enable after all */
364 } else if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
365 /* Wait for outstanding disablement to complete */
366 __fscache_wait_on_invalidate(cookie);
368 if (fscache_acquire_non_index_cookie(cookie, object_size) == 0)
369 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
371 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
375 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
376 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
378 EXPORT_SYMBOL(__fscache_enable_cookie);
381 * acquire a non-index cookie
382 * - this must make sure the index chain is instantiated and instantiate the
383 * object representation too
385 static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
388 struct fscache_object *object;
389 struct fscache_cache *cache;
394 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
396 /* now we need to see whether the backing objects for this cookie yet
397 * exist, if not there'll be nothing to search */
398 down_read(&fscache_addremove_sem);
400 if (list_empty(&fscache_cache_list)) {
401 up_read(&fscache_addremove_sem);
402 _leave(" = 0 [no caches]");
406 /* select a cache in which to store the object */
407 cache = fscache_select_cache_for_object(cookie->parent);
409 up_read(&fscache_addremove_sem);
410 fscache_stat(&fscache_n_acquires_no_cache);
411 _leave(" = -ENOMEDIUM [no cache]");
415 _debug("cache %s", cache->tag->name);
417 set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
419 /* ask the cache to allocate objects for this cookie and its parent
421 ret = fscache_alloc_object(cache, cookie);
423 up_read(&fscache_addremove_sem);
424 _leave(" = %d", ret);
428 spin_lock(&cookie->lock);
429 if (hlist_empty(&cookie->backing_objects)) {
430 spin_unlock(&cookie->lock);
434 object = hlist_entry(cookie->backing_objects.first,
435 struct fscache_object, cookie_link);
437 fscache_set_store_limit(object, object_size);
439 /* initiate the process of looking up all the objects in the chain
440 * (done by fscache_initialise_object()) */
441 fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
443 spin_unlock(&cookie->lock);
445 /* we may be required to wait for lookup to complete at this point */
446 if (!fscache_defer_lookup) {
447 _debug("non-deferred lookup %p", &cookie->flags);
448 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
449 TASK_UNINTERRUPTIBLE);
451 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
455 up_read(&fscache_addremove_sem);
456 _leave(" = 0 [deferred]");
460 up_read(&fscache_addremove_sem);
461 _leave(" = -ENOBUFS");
466 * recursively allocate cache object records for a cookie/cache combination
467 * - caller must be holding the addremove sem
469 static int fscache_alloc_object(struct fscache_cache *cache,
470 struct fscache_cookie *cookie)
472 struct fscache_object *object;
475 _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
477 spin_lock(&cookie->lock);
478 hlist_for_each_entry(object, &cookie->backing_objects,
480 if (object->cache == cache)
481 goto object_already_extant;
483 spin_unlock(&cookie->lock);
485 /* ask the cache to allocate an object (we may end up with duplicate
486 * objects at this stage, but we sort that out later) */
487 fscache_stat(&fscache_n_cop_alloc_object);
488 object = cache->ops->alloc_object(cache, cookie);
489 fscache_stat_d(&fscache_n_cop_alloc_object);
490 if (IS_ERR(object)) {
491 fscache_stat(&fscache_n_object_no_alloc);
492 ret = PTR_ERR(object);
496 ASSERTCMP(object->cookie, ==, cookie);
497 fscache_stat(&fscache_n_object_alloc);
499 object->debug_id = atomic_inc_return(&fscache_object_debug_id);
501 _debug("ALLOC OBJ%x: %s {%lx}",
502 object->debug_id, cookie->def->name, object->events);
504 ret = fscache_alloc_object(cache, cookie->parent);
508 /* only attach if we managed to allocate all we needed, otherwise
509 * discard the object we just allocated and instead use the one
510 * attached to the cookie */
511 if (fscache_attach_object(cookie, object) < 0) {
512 fscache_stat(&fscache_n_cop_put_object);
513 cache->ops->put_object(object, fscache_obj_put_attach_fail);
514 fscache_stat_d(&fscache_n_cop_put_object);
520 object_already_extant:
522 if (fscache_object_is_dying(object) ||
523 fscache_cache_is_broken(object)) {
524 spin_unlock(&cookie->lock);
527 spin_unlock(&cookie->lock);
528 _leave(" = 0 [found]");
532 fscache_stat(&fscache_n_cop_put_object);
533 cache->ops->put_object(object, fscache_obj_put_alloc_fail);
534 fscache_stat_d(&fscache_n_cop_put_object);
536 _leave(" = %d", ret);
541 * attach a cache object to a cookie
543 static int fscache_attach_object(struct fscache_cookie *cookie,
544 struct fscache_object *object)
546 struct fscache_object *p;
547 struct fscache_cache *cache = object->cache;
550 _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
552 ASSERTCMP(object->cookie, ==, cookie);
554 spin_lock(&cookie->lock);
556 /* there may be multiple initial creations of this object, but we only
559 hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
560 if (p->cache == object->cache) {
561 if (fscache_object_is_dying(p))
563 goto cant_attach_object;
567 /* pin the parent object */
568 spin_lock_nested(&cookie->parent->lock, 1);
569 hlist_for_each_entry(p, &cookie->parent->backing_objects,
571 if (p->cache == object->cache) {
572 if (fscache_object_is_dying(p)) {
574 spin_unlock(&cookie->parent->lock);
575 goto cant_attach_object;
580 spin_unlock(&p->lock);
584 spin_unlock(&cookie->parent->lock);
586 /* attach to the cache's object list */
587 if (list_empty(&object->cache_link)) {
588 spin_lock(&cache->object_list_lock);
589 list_add(&object->cache_link, &cache->object_list);
590 spin_unlock(&cache->object_list_lock);
593 /* Attach to the cookie. The object already has a ref on it. */
594 hlist_add_head(&object->cookie_link, &cookie->backing_objects);
596 fscache_objlist_add(object);
600 spin_unlock(&cookie->lock);
601 _leave(" = %d", ret);
606 * Invalidate an object. Callable with spinlocks held.
608 void __fscache_invalidate(struct fscache_cookie *cookie)
610 struct fscache_object *object;
612 _enter("{%s}", cookie->def->name);
614 fscache_stat(&fscache_n_invalidates);
616 /* Only permit invalidation of data files. Invalidating an index will
617 * require the caller to release all its attachments to the tree rooted
618 * there, and if it's doing that, it may as well just retire the
621 ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
623 /* If there's an object, we tell the object state machine to handle the
624 * invalidation on our behalf, otherwise there's nothing to do.
626 if (!hlist_empty(&cookie->backing_objects)) {
627 spin_lock(&cookie->lock);
629 if (fscache_cookie_enabled(cookie) &&
630 !hlist_empty(&cookie->backing_objects) &&
631 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
633 object = hlist_entry(cookie->backing_objects.first,
634 struct fscache_object,
636 if (fscache_object_is_live(object))
638 object, FSCACHE_OBJECT_EV_INVALIDATE);
641 spin_unlock(&cookie->lock);
646 EXPORT_SYMBOL(__fscache_invalidate);
649 * Wait for object invalidation to complete.
651 void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
653 _enter("%p", cookie);
655 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
656 TASK_UNINTERRUPTIBLE);
660 EXPORT_SYMBOL(__fscache_wait_on_invalidate);
663 * update the index entries backing a cookie
665 void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
667 struct fscache_object *object;
669 fscache_stat(&fscache_n_updates);
672 fscache_stat(&fscache_n_updates_null);
673 _leave(" [no cookie]");
677 _enter("{%s}", cookie->def->name);
679 spin_lock(&cookie->lock);
681 fscache_update_aux(cookie, aux_data);
683 if (fscache_cookie_enabled(cookie)) {
684 /* update the index entry on disk in each cache backing this
687 hlist_for_each_entry(object,
688 &cookie->backing_objects, cookie_link) {
689 fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
693 spin_unlock(&cookie->lock);
696 EXPORT_SYMBOL(__fscache_update_cookie);
699 * Disable a cookie to stop it from accepting new requests from the netfs.
701 void __fscache_disable_cookie(struct fscache_cookie *cookie,
702 const void *aux_data,
705 struct fscache_object *object;
708 _enter("%p,%u", cookie, invalidate);
710 trace_fscache_disable(cookie);
712 ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
714 if (atomic_read(&cookie->n_children) != 0) {
715 pr_err("Cookie '%s' still has children\n",
720 wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
721 TASK_UNINTERRUPTIBLE);
723 fscache_update_aux(cookie, aux_data);
725 if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
726 goto out_unlock_enable;
728 /* If the cookie is being invalidated, wait for that to complete first
729 * so that we can reuse the flag.
731 __fscache_wait_on_invalidate(cookie);
733 /* Dispose of the backing objects */
734 set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
736 spin_lock(&cookie->lock);
737 if (!hlist_empty(&cookie->backing_objects)) {
738 hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
740 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
741 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
742 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
745 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
748 spin_unlock(&cookie->lock);
750 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
752 /* Wait for cessation of activity requiring access to the netfs (when
753 * n_active reaches 0). This makes sure outstanding reads and writes
756 if (!atomic_dec_and_test(&cookie->n_active)) {
757 wait_var_event(&cookie->n_active,
758 !atomic_read(&cookie->n_active));
761 /* Make sure any pending writes are cancelled. */
762 if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX)
763 fscache_invalidate_writes(cookie);
765 /* Reset the cookie state if it wasn't relinquished */
766 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
767 atomic_inc(&cookie->n_active);
768 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
772 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
773 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
776 EXPORT_SYMBOL(__fscache_disable_cookie);
779 * release a cookie back to the cache
780 * - the object will be marked as recyclable on disk if retire is true
781 * - all dependents of this cookie must have already been unregistered
782 * (indices/files/pages)
784 void __fscache_relinquish_cookie(struct fscache_cookie *cookie,
785 const void *aux_data,
788 fscache_stat(&fscache_n_relinquishes);
790 fscache_stat(&fscache_n_relinquishes_retire);
793 fscache_stat(&fscache_n_relinquishes_null);
794 _leave(" [no cookie]");
798 _enter("%p{%s,%p,%d},%d",
799 cookie, cookie->def->name, cookie->netfs_data,
800 atomic_read(&cookie->n_active), retire);
802 trace_fscache_relinquish(cookie, retire);
804 /* No further netfs-accessing operations on this cookie permitted */
805 if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags))
808 __fscache_disable_cookie(cookie, aux_data, retire);
810 /* Clear pointers back to the netfs */
811 cookie->netfs_data = NULL;
813 BUG_ON(!radix_tree_empty(&cookie->stores));
815 if (cookie->parent) {
816 ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
817 ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
818 atomic_dec(&cookie->parent->n_children);
821 /* Dispose of the netfs's link to the cookie */
822 ASSERTCMP(atomic_read(&cookie->usage), >, 0);
823 fscache_cookie_put(cookie, fscache_cookie_put_relinquish);
827 EXPORT_SYMBOL(__fscache_relinquish_cookie);
830 * Remove a cookie from the hash table.
832 static void fscache_unhash_cookie(struct fscache_cookie *cookie)
834 struct hlist_bl_head *h;
837 bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
838 h = &fscache_cookie_hash[bucket];
841 hlist_bl_del(&cookie->hash_link);
846 * Drop a reference to a cookie.
848 void fscache_cookie_put(struct fscache_cookie *cookie,
849 enum fscache_cookie_trace where)
851 struct fscache_cookie *parent;
854 _enter("%p", cookie);
857 usage = atomic_dec_return(&cookie->usage);
858 trace_fscache_cookie(cookie, where, usage);
864 parent = cookie->parent;
865 fscache_unhash_cookie(cookie);
866 fscache_free_cookie(cookie);
869 where = fscache_cookie_put_parent;
876 * check the consistency between the netfs inode and the backing cache
878 * NOTE: it only serves no-index type
880 int __fscache_check_consistency(struct fscache_cookie *cookie,
881 const void *aux_data)
883 struct fscache_operation *op;
884 struct fscache_object *object;
885 bool wake_cookie = false;
888 _enter("%p,", cookie);
890 ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
892 if (fscache_wait_for_deferred_lookup(cookie) < 0)
895 if (hlist_empty(&cookie->backing_objects))
898 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
902 fscache_operation_init(cookie, op, NULL, NULL, NULL);
903 op->flags = FSCACHE_OP_MYTHREAD |
904 (1 << FSCACHE_OP_WAITING) |
905 (1 << FSCACHE_OP_UNUSE_COOKIE);
906 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_check_consistency);
908 spin_lock(&cookie->lock);
910 fscache_update_aux(cookie, aux_data);
912 if (!fscache_cookie_enabled(cookie) ||
913 hlist_empty(&cookie->backing_objects))
915 object = hlist_entry(cookie->backing_objects.first,
916 struct fscache_object, cookie_link);
917 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
920 op->debug_id = atomic_inc_return(&fscache_op_debug_id);
922 __fscache_use_cookie(cookie);
923 if (fscache_submit_op(object, op) < 0)
926 /* the work queue now carries its own ref on the object */
927 spin_unlock(&cookie->lock);
929 ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
931 /* ask the cache to honour the operation */
932 ret = object->cache->ops->check_consistency(op);
933 fscache_op_complete(op, false);
934 } else if (ret == -ENOBUFS) {
938 fscache_put_operation(op);
939 _leave(" = %d", ret);
943 wake_cookie = __fscache_unuse_cookie(cookie);
945 spin_unlock(&cookie->lock);
947 __fscache_wake_unused_cookie(cookie);
949 _leave(" = -ESTALE");
952 EXPORT_SYMBOL(__fscache_check_consistency);