1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* netfs cookie management
4 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * See Documentation/filesystems/caching/netfs-api.rst for more information on
11 #define FSCACHE_DEBUG_LEVEL COOKIE
12 #include <linux/module.h>
13 #include <linux/slab.h>
16 struct kmem_cache *fscache_cookie_jar;
18 static atomic_t fscache_object_debug_id = ATOMIC_INIT(0);
20 #define fscache_cookie_hash_shift 15
21 static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift];
23 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);
30 static void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
32 struct hlist_node *object;
36 pr_err("%c-cookie c=%p [p=%p fl=%lx nc=%u na=%u]\n",
37 prefix, cookie, cookie->parent, cookie->flags,
38 atomic_read(&cookie->n_children),
39 atomic_read(&cookie->n_active));
40 pr_err("%c-cookie d=%p n=%p\n",
41 prefix, cookie->def, cookie->netfs_data);
43 object = READ_ONCE(cookie->backing_objects.first);
45 pr_err("%c-cookie o=%p\n",
46 prefix, hlist_entry(object, struct fscache_object, cookie_link));
48 pr_err("%c-key=[%u] '", prefix, cookie->key_len);
49 k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
50 cookie->inline_key : cookie->key;
51 for (loop = 0; loop < cookie->key_len; loop++)
52 pr_cont("%02x", k[loop]);
56 void fscache_free_cookie(struct fscache_cookie *cookie)
59 BUG_ON(!hlist_empty(&cookie->backing_objects));
60 if (cookie->aux_len > sizeof(cookie->inline_aux))
62 if (cookie->key_len > sizeof(cookie->inline_key))
64 kmem_cache_free(fscache_cookie_jar, cookie);
69 * Set the index key in a cookie. The cookie struct has space for a 16-byte
70 * key plus length and hash, but if that's not big enough, it's instead a
71 * pointer to a buffer containing 3 bytes of hash, 1 byte of length and then
74 static int fscache_set_key(struct fscache_cookie *cookie,
75 const void *index_key, size_t index_key_len)
80 bufs = DIV_ROUND_UP(index_key_len, sizeof(*buf));
82 if (index_key_len > sizeof(cookie->inline_key)) {
83 buf = kcalloc(bufs, sizeof(*buf), GFP_KERNEL);
88 buf = (u32 *)cookie->inline_key;
91 memcpy(buf, index_key, index_key_len);
92 cookie->key_hash = fscache_hash(0, buf, bufs);
96 static long fscache_compare_cookie(const struct fscache_cookie *a,
97 const struct fscache_cookie *b)
101 if (a->key_hash != b->key_hash)
102 return (long)a->key_hash - (long)b->key_hash;
103 if (a->parent != b->parent)
104 return (long)a->parent - (long)b->parent;
105 if (a->key_len != b->key_len)
106 return (long)a->key_len - (long)b->key_len;
107 if (a->type != b->type)
108 return (long)a->type - (long)b->type;
110 if (a->key_len <= sizeof(a->inline_key)) {
117 return memcmp(ka, kb, a->key_len);
123 struct fscache_cookie *fscache_alloc_cookie(
124 struct fscache_cookie *parent,
125 const struct fscache_cookie_def *def,
126 const void *index_key, size_t index_key_len,
127 const void *aux_data, size_t aux_data_len,
131 struct fscache_cookie *cookie;
133 /* allocate and initialise a cookie */
134 cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
138 cookie->key_len = index_key_len;
139 cookie->aux_len = aux_data_len;
141 if (fscache_set_key(cookie, index_key, index_key_len) < 0)
144 if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
145 memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
147 cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
152 atomic_set(&cookie->usage, 1);
153 atomic_set(&cookie->n_children, 0);
155 /* We keep the active count elevated until relinquishment to prevent an
156 * attempt to wake up every time the object operations queue quiesces.
158 atomic_set(&cookie->n_active, 1);
161 cookie->parent = parent;
162 cookie->netfs_data = netfs_data;
163 cookie->flags = (1 << FSCACHE_COOKIE_NO_DATA_YET);
164 cookie->type = def->type;
165 spin_lock_init(&cookie->lock);
166 spin_lock_init(&cookie->stores_lock);
167 INIT_HLIST_HEAD(&cookie->backing_objects);
169 /* radix tree insertion won't use the preallocation pool unless it's
170 * told it may not wait */
171 INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
175 fscache_free_cookie(cookie);
180 * Attempt to insert the new cookie into the hash. If there's a collision, we
181 * return the old cookie if it's not in use and an error otherwise.
183 struct fscache_cookie *fscache_hash_cookie(struct fscache_cookie *candidate)
185 struct fscache_cookie *cursor;
186 struct hlist_bl_head *h;
187 struct hlist_bl_node *p;
190 bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
191 h = &fscache_cookie_hash[bucket];
194 hlist_bl_for_each_entry(cursor, p, h, hash_link) {
195 if (fscache_compare_cookie(candidate, cursor) == 0)
199 __set_bit(FSCACHE_COOKIE_ACQUIRED, &candidate->flags);
200 fscache_cookie_get(candidate->parent, fscache_cookie_get_acquire_parent);
201 atomic_inc(&candidate->parent->n_children);
202 hlist_bl_add_head(&candidate->hash_link, h);
207 if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED, &cursor->flags)) {
208 trace_fscache_cookie(cursor, fscache_cookie_collision,
209 atomic_read(&cursor->usage));
210 pr_err("Duplicate cookie detected\n");
211 fscache_print_cookie(cursor, 'O');
212 fscache_print_cookie(candidate, 'N');
217 fscache_cookie_get(cursor, fscache_cookie_get_reacquire);
223 * request a cookie to represent an object (index, datafile, xattr, etc)
224 * - parent specifies the parent object
225 * - the top level index cookie for each netfs is stored in the fscache_netfs
226 * struct upon registration
227 * - def points to the definition
228 * - the netfs_data will be passed to the functions pointed to in *def
229 * - all attached caches will be searched to see if they contain this object
230 * - index objects aren't stored on disk until there's a dependent file that
232 * - other objects are stored in a selected cache immediately, and all the
233 * indices forming the path to it are instantiated if necessary
234 * - we never let on to the netfs about errors
235 * - we may set a negative cookie pointer, but that's okay
237 struct fscache_cookie *__fscache_acquire_cookie(
238 struct fscache_cookie *parent,
239 const struct fscache_cookie_def *def,
240 const void *index_key, size_t index_key_len,
241 const void *aux_data, size_t aux_data_len,
246 struct fscache_cookie *candidate, *cookie;
250 _enter("{%s},{%s},%p,%u",
251 parent ? (char *) parent->def->name : "<no-parent>",
252 def->name, netfs_data, enable);
254 if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
256 if (!aux_data || !aux_data_len) {
261 fscache_stat(&fscache_n_acquires);
263 /* if there's no parent cookie, then we don't create one here either */
265 fscache_stat(&fscache_n_acquires_null);
266 _leave(" [no parent]");
270 /* validate the definition */
271 BUG_ON(!def->name[0]);
273 BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
274 parent->type != FSCACHE_COOKIE_TYPE_INDEX);
276 candidate = fscache_alloc_cookie(parent, def,
277 index_key, index_key_len,
278 aux_data, aux_data_len,
279 netfs_data, object_size);
281 fscache_stat(&fscache_n_acquires_oom);
286 cookie = fscache_hash_cookie(candidate);
288 trace_fscache_cookie(candidate, fscache_cookie_discard, 1);
292 if (cookie == candidate)
295 switch (cookie->type) {
296 case FSCACHE_COOKIE_TYPE_INDEX:
297 fscache_stat(&fscache_n_cookie_index);
299 case FSCACHE_COOKIE_TYPE_DATAFILE:
300 fscache_stat(&fscache_n_cookie_data);
303 fscache_stat(&fscache_n_cookie_special);
307 trace_fscache_acquire(cookie);
310 /* if the object is an index then we need do nothing more here
311 * - we create indices on disk when we need them as an index
312 * may exist in multiple caches */
313 if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
314 if (fscache_acquire_non_index_cookie(cookie, object_size) == 0) {
315 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
317 atomic_dec(&parent->n_children);
318 fscache_cookie_put(cookie,
319 fscache_cookie_put_acquire_nobufs);
320 fscache_stat(&fscache_n_acquires_nobufs);
325 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
329 fscache_stat(&fscache_n_acquires_ok);
332 fscache_free_cookie(candidate);
335 EXPORT_SYMBOL(__fscache_acquire_cookie);
338 * Enable a cookie to permit it to accept new operations.
340 void __fscache_enable_cookie(struct fscache_cookie *cookie,
341 const void *aux_data,
343 bool (*can_enable)(void *data),
346 _enter("%p", cookie);
348 trace_fscache_enable(cookie);
350 wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
351 TASK_UNINTERRUPTIBLE);
353 fscache_update_aux(cookie, aux_data);
355 if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
358 if (can_enable && !can_enable(data)) {
359 /* The netfs decided it didn't want to enable after all */
360 } else if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
361 /* Wait for outstanding disablement to complete */
362 __fscache_wait_on_invalidate(cookie);
364 if (fscache_acquire_non_index_cookie(cookie, object_size) == 0)
365 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
367 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
371 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
372 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
374 EXPORT_SYMBOL(__fscache_enable_cookie);
377 * acquire a non-index cookie
378 * - this must make sure the index chain is instantiated and instantiate the
379 * object representation too
381 static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
384 struct fscache_object *object;
385 struct fscache_cache *cache;
390 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
392 /* now we need to see whether the backing objects for this cookie yet
393 * exist, if not there'll be nothing to search */
394 down_read(&fscache_addremove_sem);
396 if (list_empty(&fscache_cache_list)) {
397 up_read(&fscache_addremove_sem);
398 _leave(" = 0 [no caches]");
402 /* select a cache in which to store the object */
403 cache = fscache_select_cache_for_object(cookie->parent);
405 up_read(&fscache_addremove_sem);
406 fscache_stat(&fscache_n_acquires_no_cache);
407 _leave(" = -ENOMEDIUM [no cache]");
411 _debug("cache %s", cache->tag->name);
413 set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
415 /* ask the cache to allocate objects for this cookie and its parent
417 ret = fscache_alloc_object(cache, cookie);
419 up_read(&fscache_addremove_sem);
420 _leave(" = %d", ret);
424 spin_lock(&cookie->lock);
425 if (hlist_empty(&cookie->backing_objects)) {
426 spin_unlock(&cookie->lock);
430 object = hlist_entry(cookie->backing_objects.first,
431 struct fscache_object, cookie_link);
433 fscache_set_store_limit(object, object_size);
435 /* initiate the process of looking up all the objects in the chain
436 * (done by fscache_initialise_object()) */
437 fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
439 spin_unlock(&cookie->lock);
441 /* we may be required to wait for lookup to complete at this point */
442 if (!fscache_defer_lookup) {
443 _debug("non-deferred lookup %p", &cookie->flags);
444 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
445 TASK_UNINTERRUPTIBLE);
447 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
451 up_read(&fscache_addremove_sem);
452 _leave(" = 0 [deferred]");
456 up_read(&fscache_addremove_sem);
457 _leave(" = -ENOBUFS");
462 * recursively allocate cache object records for a cookie/cache combination
463 * - caller must be holding the addremove sem
465 static int fscache_alloc_object(struct fscache_cache *cache,
466 struct fscache_cookie *cookie)
468 struct fscache_object *object;
471 _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
473 spin_lock(&cookie->lock);
474 hlist_for_each_entry(object, &cookie->backing_objects,
476 if (object->cache == cache)
477 goto object_already_extant;
479 spin_unlock(&cookie->lock);
481 /* ask the cache to allocate an object (we may end up with duplicate
482 * objects at this stage, but we sort that out later) */
483 fscache_stat(&fscache_n_cop_alloc_object);
484 object = cache->ops->alloc_object(cache, cookie);
485 fscache_stat_d(&fscache_n_cop_alloc_object);
486 if (IS_ERR(object)) {
487 fscache_stat(&fscache_n_object_no_alloc);
488 ret = PTR_ERR(object);
492 ASSERTCMP(object->cookie, ==, cookie);
493 fscache_stat(&fscache_n_object_alloc);
495 object->debug_id = atomic_inc_return(&fscache_object_debug_id);
497 _debug("ALLOC OBJ%x: %s {%lx}",
498 object->debug_id, cookie->def->name, object->events);
500 ret = fscache_alloc_object(cache, cookie->parent);
504 /* only attach if we managed to allocate all we needed, otherwise
505 * discard the object we just allocated and instead use the one
506 * attached to the cookie */
507 if (fscache_attach_object(cookie, object) < 0) {
508 fscache_stat(&fscache_n_cop_put_object);
509 cache->ops->put_object(object, fscache_obj_put_attach_fail);
510 fscache_stat_d(&fscache_n_cop_put_object);
516 object_already_extant:
518 if (fscache_object_is_dying(object) ||
519 fscache_cache_is_broken(object)) {
520 spin_unlock(&cookie->lock);
523 spin_unlock(&cookie->lock);
524 _leave(" = 0 [found]");
528 fscache_stat(&fscache_n_cop_put_object);
529 cache->ops->put_object(object, fscache_obj_put_alloc_fail);
530 fscache_stat_d(&fscache_n_cop_put_object);
532 _leave(" = %d", ret);
537 * attach a cache object to a cookie
539 static int fscache_attach_object(struct fscache_cookie *cookie,
540 struct fscache_object *object)
542 struct fscache_object *p;
543 struct fscache_cache *cache = object->cache;
546 _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
548 ASSERTCMP(object->cookie, ==, cookie);
550 spin_lock(&cookie->lock);
552 /* there may be multiple initial creations of this object, but we only
555 hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
556 if (p->cache == object->cache) {
557 if (fscache_object_is_dying(p))
559 goto cant_attach_object;
563 /* pin the parent object */
564 spin_lock_nested(&cookie->parent->lock, 1);
565 hlist_for_each_entry(p, &cookie->parent->backing_objects,
567 if (p->cache == object->cache) {
568 if (fscache_object_is_dying(p)) {
570 spin_unlock(&cookie->parent->lock);
571 goto cant_attach_object;
576 spin_unlock(&p->lock);
580 spin_unlock(&cookie->parent->lock);
582 /* attach to the cache's object list */
583 if (list_empty(&object->cache_link)) {
584 spin_lock(&cache->object_list_lock);
585 list_add(&object->cache_link, &cache->object_list);
586 spin_unlock(&cache->object_list_lock);
589 /* Attach to the cookie. The object already has a ref on it. */
590 hlist_add_head(&object->cookie_link, &cookie->backing_objects);
592 fscache_objlist_add(object);
596 spin_unlock(&cookie->lock);
597 _leave(" = %d", ret);
602 * Invalidate an object. Callable with spinlocks held.
604 void __fscache_invalidate(struct fscache_cookie *cookie)
606 struct fscache_object *object;
608 _enter("{%s}", cookie->def->name);
610 fscache_stat(&fscache_n_invalidates);
612 /* Only permit invalidation of data files. Invalidating an index will
613 * require the caller to release all its attachments to the tree rooted
614 * there, and if it's doing that, it may as well just retire the
617 ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
619 /* If there's an object, we tell the object state machine to handle the
620 * invalidation on our behalf, otherwise there's nothing to do.
622 if (!hlist_empty(&cookie->backing_objects)) {
623 spin_lock(&cookie->lock);
625 if (fscache_cookie_enabled(cookie) &&
626 !hlist_empty(&cookie->backing_objects) &&
627 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
629 object = hlist_entry(cookie->backing_objects.first,
630 struct fscache_object,
632 if (fscache_object_is_live(object))
634 object, FSCACHE_OBJECT_EV_INVALIDATE);
637 spin_unlock(&cookie->lock);
642 EXPORT_SYMBOL(__fscache_invalidate);
645 * Wait for object invalidation to complete.
647 void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
649 _enter("%p", cookie);
651 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
652 TASK_UNINTERRUPTIBLE);
656 EXPORT_SYMBOL(__fscache_wait_on_invalidate);
659 * update the index entries backing a cookie
661 void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
663 struct fscache_object *object;
665 fscache_stat(&fscache_n_updates);
668 fscache_stat(&fscache_n_updates_null);
669 _leave(" [no cookie]");
673 _enter("{%s}", cookie->def->name);
675 spin_lock(&cookie->lock);
677 fscache_update_aux(cookie, aux_data);
679 if (fscache_cookie_enabled(cookie)) {
680 /* update the index entry on disk in each cache backing this
683 hlist_for_each_entry(object,
684 &cookie->backing_objects, cookie_link) {
685 fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
689 spin_unlock(&cookie->lock);
692 EXPORT_SYMBOL(__fscache_update_cookie);
695 * Disable a cookie to stop it from accepting new requests from the netfs.
697 void __fscache_disable_cookie(struct fscache_cookie *cookie,
698 const void *aux_data,
701 struct fscache_object *object;
704 _enter("%p,%u", cookie, invalidate);
706 trace_fscache_disable(cookie);
708 ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
710 if (atomic_read(&cookie->n_children) != 0) {
711 pr_err("Cookie '%s' still has children\n",
716 wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
717 TASK_UNINTERRUPTIBLE);
719 fscache_update_aux(cookie, aux_data);
721 if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
722 goto out_unlock_enable;
724 /* If the cookie is being invalidated, wait for that to complete first
725 * so that we can reuse the flag.
727 __fscache_wait_on_invalidate(cookie);
729 /* Dispose of the backing objects */
730 set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
732 spin_lock(&cookie->lock);
733 if (!hlist_empty(&cookie->backing_objects)) {
734 hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
736 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
737 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
738 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
741 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
744 spin_unlock(&cookie->lock);
746 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
748 /* Wait for cessation of activity requiring access to the netfs (when
749 * n_active reaches 0). This makes sure outstanding reads and writes
752 if (!atomic_dec_and_test(&cookie->n_active)) {
753 wait_var_event(&cookie->n_active,
754 !atomic_read(&cookie->n_active));
757 /* Make sure any pending writes are cancelled. */
758 if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX)
759 fscache_invalidate_writes(cookie);
761 /* Reset the cookie state if it wasn't relinquished */
762 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
763 atomic_inc(&cookie->n_active);
764 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
768 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
769 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
772 EXPORT_SYMBOL(__fscache_disable_cookie);
775 * release a cookie back to the cache
776 * - the object will be marked as recyclable on disk if retire is true
777 * - all dependents of this cookie must have already been unregistered
778 * (indices/files/pages)
780 void __fscache_relinquish_cookie(struct fscache_cookie *cookie,
781 const void *aux_data,
784 fscache_stat(&fscache_n_relinquishes);
786 fscache_stat(&fscache_n_relinquishes_retire);
789 fscache_stat(&fscache_n_relinquishes_null);
790 _leave(" [no cookie]");
794 _enter("%p{%s,%p,%d},%d",
795 cookie, cookie->def->name, cookie->netfs_data,
796 atomic_read(&cookie->n_active), retire);
798 trace_fscache_relinquish(cookie, retire);
800 /* No further netfs-accessing operations on this cookie permitted */
801 if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags))
804 __fscache_disable_cookie(cookie, aux_data, retire);
806 /* Clear pointers back to the netfs */
807 cookie->netfs_data = NULL;
809 BUG_ON(!radix_tree_empty(&cookie->stores));
811 if (cookie->parent) {
812 ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
813 ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
814 atomic_dec(&cookie->parent->n_children);
817 /* Dispose of the netfs's link to the cookie */
818 ASSERTCMP(atomic_read(&cookie->usage), >, 0);
819 fscache_cookie_put(cookie, fscache_cookie_put_relinquish);
823 EXPORT_SYMBOL(__fscache_relinquish_cookie);
826 * Remove a cookie from the hash table.
828 static void fscache_unhash_cookie(struct fscache_cookie *cookie)
830 struct hlist_bl_head *h;
833 bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
834 h = &fscache_cookie_hash[bucket];
837 hlist_bl_del(&cookie->hash_link);
842 * Drop a reference to a cookie.
844 void fscache_cookie_put(struct fscache_cookie *cookie,
845 enum fscache_cookie_trace where)
847 struct fscache_cookie *parent;
850 _enter("%p", cookie);
853 usage = atomic_dec_return(&cookie->usage);
854 trace_fscache_cookie(cookie, where, usage);
860 parent = cookie->parent;
861 fscache_unhash_cookie(cookie);
862 fscache_free_cookie(cookie);
865 where = fscache_cookie_put_parent;
872 * check the consistency between the netfs inode and the backing cache
874 * NOTE: it only serves no-index type
876 int __fscache_check_consistency(struct fscache_cookie *cookie,
877 const void *aux_data)
879 struct fscache_operation *op;
880 struct fscache_object *object;
881 bool wake_cookie = false;
884 _enter("%p,", cookie);
886 ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
888 if (fscache_wait_for_deferred_lookup(cookie) < 0)
891 if (hlist_empty(&cookie->backing_objects))
894 op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
898 fscache_operation_init(cookie, op, NULL, NULL, NULL);
899 op->flags = FSCACHE_OP_MYTHREAD |
900 (1 << FSCACHE_OP_WAITING) |
901 (1 << FSCACHE_OP_UNUSE_COOKIE);
902 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_check_consistency);
904 spin_lock(&cookie->lock);
906 fscache_update_aux(cookie, aux_data);
908 if (!fscache_cookie_enabled(cookie) ||
909 hlist_empty(&cookie->backing_objects))
911 object = hlist_entry(cookie->backing_objects.first,
912 struct fscache_object, cookie_link);
913 if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
916 op->debug_id = atomic_inc_return(&fscache_op_debug_id);
918 __fscache_use_cookie(cookie);
919 if (fscache_submit_op(object, op) < 0)
922 /* the work queue now carries its own ref on the object */
923 spin_unlock(&cookie->lock);
925 ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
927 /* ask the cache to honour the operation */
928 ret = object->cache->ops->check_consistency(op);
929 fscache_op_complete(op, false);
930 } else if (ret == -ENOBUFS) {
934 fscache_put_operation(op);
935 _leave(" = %d", ret);
939 wake_cookie = __fscache_unuse_cookie(cookie);
941 spin_unlock(&cookie->lock);
943 __fscache_wake_unused_cookie(cookie);
945 _leave(" = -ESTALE");
948 EXPORT_SYMBOL(__fscache_check_consistency);