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.txt 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
)
82 bufs
= DIV_ROUND_UP(index_key_len
, sizeof(*buf
));
84 if (index_key_len
> sizeof(cookie
->inline_key
)) {
85 buf
= kcalloc(bufs
, sizeof(*buf
), GFP_KERNEL
);
90 buf
= (u32
*)cookie
->inline_key
;
93 memcpy(buf
, index_key
, index_key_len
);
95 /* Calculate a hash and combine this with the length in the first word
98 h
= (unsigned long)cookie
->parent
;
99 h
+= index_key_len
+ cookie
->type
;
101 for (i
= 0; i
< bufs
; i
++)
104 cookie
->key_hash
= h
^ (h
>> 32);
108 static long fscache_compare_cookie(const struct fscache_cookie
*a
,
109 const struct fscache_cookie
*b
)
113 if (a
->key_hash
!= b
->key_hash
)
114 return (long)a
->key_hash
- (long)b
->key_hash
;
115 if (a
->parent
!= b
->parent
)
116 return (long)a
->parent
- (long)b
->parent
;
117 if (a
->key_len
!= b
->key_len
)
118 return (long)a
->key_len
- (long)b
->key_len
;
119 if (a
->type
!= b
->type
)
120 return (long)a
->type
- (long)b
->type
;
122 if (a
->key_len
<= sizeof(a
->inline_key
)) {
129 return memcmp(ka
, kb
, a
->key_len
);
135 struct fscache_cookie
*fscache_alloc_cookie(
136 struct fscache_cookie
*parent
,
137 const struct fscache_cookie_def
*def
,
138 const void *index_key
, size_t index_key_len
,
139 const void *aux_data
, size_t aux_data_len
,
143 struct fscache_cookie
*cookie
;
145 /* allocate and initialise a cookie */
146 cookie
= kmem_cache_zalloc(fscache_cookie_jar
, GFP_KERNEL
);
150 cookie
->key_len
= index_key_len
;
151 cookie
->aux_len
= aux_data_len
;
153 if (fscache_set_key(cookie
, index_key
, index_key_len
) < 0)
156 if (cookie
->aux_len
<= sizeof(cookie
->inline_aux
)) {
157 memcpy(cookie
->inline_aux
, aux_data
, cookie
->aux_len
);
159 cookie
->aux
= kmemdup(aux_data
, cookie
->aux_len
, GFP_KERNEL
);
164 atomic_set(&cookie
->usage
, 1);
165 atomic_set(&cookie
->n_children
, 0);
167 /* We keep the active count elevated until relinquishment to prevent an
168 * attempt to wake up every time the object operations queue quiesces.
170 atomic_set(&cookie
->n_active
, 1);
173 cookie
->parent
= parent
;
174 cookie
->netfs_data
= netfs_data
;
175 cookie
->flags
= (1 << FSCACHE_COOKIE_NO_DATA_YET
);
176 cookie
->type
= def
->type
;
177 spin_lock_init(&cookie
->lock
);
178 spin_lock_init(&cookie
->stores_lock
);
179 INIT_HLIST_HEAD(&cookie
->backing_objects
);
181 /* radix tree insertion won't use the preallocation pool unless it's
182 * told it may not wait */
183 INIT_RADIX_TREE(&cookie
->stores
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
187 fscache_free_cookie(cookie
);
192 * Attempt to insert the new cookie into the hash. If there's a collision, we
193 * return the old cookie if it's not in use and an error otherwise.
195 struct fscache_cookie
*fscache_hash_cookie(struct fscache_cookie
*candidate
)
197 struct fscache_cookie
*cursor
;
198 struct hlist_bl_head
*h
;
199 struct hlist_bl_node
*p
;
202 bucket
= candidate
->key_hash
& (ARRAY_SIZE(fscache_cookie_hash
) - 1);
203 h
= &fscache_cookie_hash
[bucket
];
206 hlist_bl_for_each_entry(cursor
, p
, h
, hash_link
) {
207 if (fscache_compare_cookie(candidate
, cursor
) == 0)
211 __set_bit(FSCACHE_COOKIE_ACQUIRED
, &candidate
->flags
);
212 fscache_cookie_get(candidate
->parent
, fscache_cookie_get_acquire_parent
);
213 atomic_inc(&candidate
->parent
->n_children
);
214 hlist_bl_add_head(&candidate
->hash_link
, h
);
219 if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED
, &cursor
->flags
)) {
220 trace_fscache_cookie(cursor
, fscache_cookie_collision
,
221 atomic_read(&cursor
->usage
));
222 pr_err("Duplicate cookie detected\n");
223 fscache_print_cookie(cursor
, 'O');
224 fscache_print_cookie(candidate
, 'N');
229 fscache_cookie_get(cursor
, fscache_cookie_get_reacquire
);
235 * request a cookie to represent an object (index, datafile, xattr, etc)
236 * - parent specifies the parent object
237 * - the top level index cookie for each netfs is stored in the fscache_netfs
238 * struct upon registration
239 * - def points to the definition
240 * - the netfs_data will be passed to the functions pointed to in *def
241 * - all attached caches will be searched to see if they contain this object
242 * - index objects aren't stored on disk until there's a dependent file that
244 * - other objects are stored in a selected cache immediately, and all the
245 * indices forming the path to it are instantiated if necessary
246 * - we never let on to the netfs about errors
247 * - we may set a negative cookie pointer, but that's okay
249 struct fscache_cookie
*__fscache_acquire_cookie(
250 struct fscache_cookie
*parent
,
251 const struct fscache_cookie_def
*def
,
252 const void *index_key
, size_t index_key_len
,
253 const void *aux_data
, size_t aux_data_len
,
258 struct fscache_cookie
*candidate
, *cookie
;
262 _enter("{%s},{%s},%p,%u",
263 parent
? (char *) parent
->def
->name
: "<no-parent>",
264 def
->name
, netfs_data
, enable
);
266 if (!index_key
|| !index_key_len
|| index_key_len
> 255 || aux_data_len
> 255)
268 if (!aux_data
|| !aux_data_len
) {
273 fscache_stat(&fscache_n_acquires
);
275 /* if there's no parent cookie, then we don't create one here either */
277 fscache_stat(&fscache_n_acquires_null
);
278 _leave(" [no parent]");
282 /* validate the definition */
283 BUG_ON(!def
->name
[0]);
285 BUG_ON(def
->type
== FSCACHE_COOKIE_TYPE_INDEX
&&
286 parent
->type
!= FSCACHE_COOKIE_TYPE_INDEX
);
288 candidate
= fscache_alloc_cookie(parent
, def
,
289 index_key
, index_key_len
,
290 aux_data
, aux_data_len
,
291 netfs_data
, object_size
);
293 fscache_stat(&fscache_n_acquires_oom
);
298 cookie
= fscache_hash_cookie(candidate
);
300 trace_fscache_cookie(candidate
, fscache_cookie_discard
, 1);
304 if (cookie
== candidate
)
307 switch (cookie
->type
) {
308 case FSCACHE_COOKIE_TYPE_INDEX
:
309 fscache_stat(&fscache_n_cookie_index
);
311 case FSCACHE_COOKIE_TYPE_DATAFILE
:
312 fscache_stat(&fscache_n_cookie_data
);
315 fscache_stat(&fscache_n_cookie_special
);
319 trace_fscache_acquire(cookie
);
322 /* if the object is an index then we need do nothing more here
323 * - we create indices on disk when we need them as an index
324 * may exist in multiple caches */
325 if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
326 if (fscache_acquire_non_index_cookie(cookie
, object_size
) == 0) {
327 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
329 atomic_dec(&parent
->n_children
);
330 fscache_cookie_put(cookie
,
331 fscache_cookie_put_acquire_nobufs
);
332 fscache_stat(&fscache_n_acquires_nobufs
);
337 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
341 fscache_stat(&fscache_n_acquires_ok
);
344 fscache_free_cookie(candidate
);
347 EXPORT_SYMBOL(__fscache_acquire_cookie
);
350 * Enable a cookie to permit it to accept new operations.
352 void __fscache_enable_cookie(struct fscache_cookie
*cookie
,
353 const void *aux_data
,
355 bool (*can_enable
)(void *data
),
358 _enter("%p", cookie
);
360 trace_fscache_enable(cookie
);
362 wait_on_bit_lock(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
,
363 TASK_UNINTERRUPTIBLE
);
365 fscache_update_aux(cookie
, aux_data
);
367 if (test_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
))
370 if (can_enable
&& !can_enable(data
)) {
371 /* The netfs decided it didn't want to enable after all */
372 } else if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
373 /* Wait for outstanding disablement to complete */
374 __fscache_wait_on_invalidate(cookie
);
376 if (fscache_acquire_non_index_cookie(cookie
, object_size
) == 0)
377 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
379 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
383 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK
, &cookie
->flags
);
384 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
);
386 EXPORT_SYMBOL(__fscache_enable_cookie
);
389 * acquire a non-index cookie
390 * - this must make sure the index chain is instantiated and instantiate the
391 * object representation too
393 static int fscache_acquire_non_index_cookie(struct fscache_cookie
*cookie
,
396 struct fscache_object
*object
;
397 struct fscache_cache
*cache
;
402 set_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
);
404 /* now we need to see whether the backing objects for this cookie yet
405 * exist, if not there'll be nothing to search */
406 down_read(&fscache_addremove_sem
);
408 if (list_empty(&fscache_cache_list
)) {
409 up_read(&fscache_addremove_sem
);
410 _leave(" = 0 [no caches]");
414 /* select a cache in which to store the object */
415 cache
= fscache_select_cache_for_object(cookie
->parent
);
417 up_read(&fscache_addremove_sem
);
418 fscache_stat(&fscache_n_acquires_no_cache
);
419 _leave(" = -ENOMEDIUM [no cache]");
423 _debug("cache %s", cache
->tag
->name
);
425 set_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
427 /* ask the cache to allocate objects for this cookie and its parent
429 ret
= fscache_alloc_object(cache
, cookie
);
431 up_read(&fscache_addremove_sem
);
432 _leave(" = %d", ret
);
436 spin_lock(&cookie
->lock
);
437 if (hlist_empty(&cookie
->backing_objects
)) {
438 spin_unlock(&cookie
->lock
);
442 object
= hlist_entry(cookie
->backing_objects
.first
,
443 struct fscache_object
, cookie_link
);
445 fscache_set_store_limit(object
, object_size
);
447 /* initiate the process of looking up all the objects in the chain
448 * (done by fscache_initialise_object()) */
449 fscache_raise_event(object
, FSCACHE_OBJECT_EV_NEW_CHILD
);
451 spin_unlock(&cookie
->lock
);
453 /* we may be required to wait for lookup to complete at this point */
454 if (!fscache_defer_lookup
) {
455 _debug("non-deferred lookup %p", &cookie
->flags
);
456 wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
457 TASK_UNINTERRUPTIBLE
);
459 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
))
463 up_read(&fscache_addremove_sem
);
464 _leave(" = 0 [deferred]");
468 up_read(&fscache_addremove_sem
);
469 _leave(" = -ENOBUFS");
474 * recursively allocate cache object records for a cookie/cache combination
475 * - caller must be holding the addremove sem
477 static int fscache_alloc_object(struct fscache_cache
*cache
,
478 struct fscache_cookie
*cookie
)
480 struct fscache_object
*object
;
483 _enter("%p,%p{%s}", cache
, cookie
, cookie
->def
->name
);
485 spin_lock(&cookie
->lock
);
486 hlist_for_each_entry(object
, &cookie
->backing_objects
,
488 if (object
->cache
== cache
)
489 goto object_already_extant
;
491 spin_unlock(&cookie
->lock
);
493 /* ask the cache to allocate an object (we may end up with duplicate
494 * objects at this stage, but we sort that out later) */
495 fscache_stat(&fscache_n_cop_alloc_object
);
496 object
= cache
->ops
->alloc_object(cache
, cookie
);
497 fscache_stat_d(&fscache_n_cop_alloc_object
);
498 if (IS_ERR(object
)) {
499 fscache_stat(&fscache_n_object_no_alloc
);
500 ret
= PTR_ERR(object
);
504 ASSERTCMP(object
->cookie
, ==, cookie
);
505 fscache_stat(&fscache_n_object_alloc
);
507 object
->debug_id
= atomic_inc_return(&fscache_object_debug_id
);
509 _debug("ALLOC OBJ%x: %s {%lx}",
510 object
->debug_id
, cookie
->def
->name
, object
->events
);
512 ret
= fscache_alloc_object(cache
, cookie
->parent
);
516 /* only attach if we managed to allocate all we needed, otherwise
517 * discard the object we just allocated and instead use the one
518 * attached to the cookie */
519 if (fscache_attach_object(cookie
, object
) < 0) {
520 fscache_stat(&fscache_n_cop_put_object
);
521 cache
->ops
->put_object(object
, fscache_obj_put_attach_fail
);
522 fscache_stat_d(&fscache_n_cop_put_object
);
528 object_already_extant
:
530 if (fscache_object_is_dying(object
) ||
531 fscache_cache_is_broken(object
)) {
532 spin_unlock(&cookie
->lock
);
535 spin_unlock(&cookie
->lock
);
536 _leave(" = 0 [found]");
540 fscache_stat(&fscache_n_cop_put_object
);
541 cache
->ops
->put_object(object
, fscache_obj_put_alloc_fail
);
542 fscache_stat_d(&fscache_n_cop_put_object
);
544 _leave(" = %d", ret
);
549 * attach a cache object to a cookie
551 static int fscache_attach_object(struct fscache_cookie
*cookie
,
552 struct fscache_object
*object
)
554 struct fscache_object
*p
;
555 struct fscache_cache
*cache
= object
->cache
;
558 _enter("{%s},{OBJ%x}", cookie
->def
->name
, object
->debug_id
);
560 ASSERTCMP(object
->cookie
, ==, cookie
);
562 spin_lock(&cookie
->lock
);
564 /* there may be multiple initial creations of this object, but we only
567 hlist_for_each_entry(p
, &cookie
->backing_objects
, cookie_link
) {
568 if (p
->cache
== object
->cache
) {
569 if (fscache_object_is_dying(p
))
571 goto cant_attach_object
;
575 /* pin the parent object */
576 spin_lock_nested(&cookie
->parent
->lock
, 1);
577 hlist_for_each_entry(p
, &cookie
->parent
->backing_objects
,
579 if (p
->cache
== object
->cache
) {
580 if (fscache_object_is_dying(p
)) {
582 spin_unlock(&cookie
->parent
->lock
);
583 goto cant_attach_object
;
588 spin_unlock(&p
->lock
);
592 spin_unlock(&cookie
->parent
->lock
);
594 /* attach to the cache's object list */
595 if (list_empty(&object
->cache_link
)) {
596 spin_lock(&cache
->object_list_lock
);
597 list_add(&object
->cache_link
, &cache
->object_list
);
598 spin_unlock(&cache
->object_list_lock
);
601 /* Attach to the cookie. The object already has a ref on it. */
602 hlist_add_head(&object
->cookie_link
, &cookie
->backing_objects
);
604 fscache_objlist_add(object
);
608 spin_unlock(&cookie
->lock
);
609 _leave(" = %d", ret
);
614 * Invalidate an object. Callable with spinlocks held.
616 void __fscache_invalidate(struct fscache_cookie
*cookie
)
618 struct fscache_object
*object
;
620 _enter("{%s}", cookie
->def
->name
);
622 fscache_stat(&fscache_n_invalidates
);
624 /* Only permit invalidation of data files. Invalidating an index will
625 * require the caller to release all its attachments to the tree rooted
626 * there, and if it's doing that, it may as well just retire the
629 ASSERTCMP(cookie
->type
, ==, FSCACHE_COOKIE_TYPE_DATAFILE
);
631 /* If there's an object, we tell the object state machine to handle the
632 * invalidation on our behalf, otherwise there's nothing to do.
634 if (!hlist_empty(&cookie
->backing_objects
)) {
635 spin_lock(&cookie
->lock
);
637 if (fscache_cookie_enabled(cookie
) &&
638 !hlist_empty(&cookie
->backing_objects
) &&
639 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING
,
641 object
= hlist_entry(cookie
->backing_objects
.first
,
642 struct fscache_object
,
644 if (fscache_object_is_live(object
))
646 object
, FSCACHE_OBJECT_EV_INVALIDATE
);
649 spin_unlock(&cookie
->lock
);
654 EXPORT_SYMBOL(__fscache_invalidate
);
657 * Wait for object invalidation to complete.
659 void __fscache_wait_on_invalidate(struct fscache_cookie
*cookie
)
661 _enter("%p", cookie
);
663 wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
,
664 TASK_UNINTERRUPTIBLE
);
668 EXPORT_SYMBOL(__fscache_wait_on_invalidate
);
671 * update the index entries backing a cookie
673 void __fscache_update_cookie(struct fscache_cookie
*cookie
, const void *aux_data
)
675 struct fscache_object
*object
;
677 fscache_stat(&fscache_n_updates
);
680 fscache_stat(&fscache_n_updates_null
);
681 _leave(" [no cookie]");
685 _enter("{%s}", cookie
->def
->name
);
687 spin_lock(&cookie
->lock
);
689 fscache_update_aux(cookie
, aux_data
);
691 if (fscache_cookie_enabled(cookie
)) {
692 /* update the index entry on disk in each cache backing this
695 hlist_for_each_entry(object
,
696 &cookie
->backing_objects
, cookie_link
) {
697 fscache_raise_event(object
, FSCACHE_OBJECT_EV_UPDATE
);
701 spin_unlock(&cookie
->lock
);
704 EXPORT_SYMBOL(__fscache_update_cookie
);
707 * Disable a cookie to stop it from accepting new requests from the netfs.
709 void __fscache_disable_cookie(struct fscache_cookie
*cookie
,
710 const void *aux_data
,
713 struct fscache_object
*object
;
716 _enter("%p,%u", cookie
, invalidate
);
718 trace_fscache_disable(cookie
);
720 ASSERTCMP(atomic_read(&cookie
->n_active
), >, 0);
722 if (atomic_read(&cookie
->n_children
) != 0) {
723 pr_err("Cookie '%s' still has children\n",
728 wait_on_bit_lock(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
,
729 TASK_UNINTERRUPTIBLE
);
731 fscache_update_aux(cookie
, aux_data
);
733 if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
))
734 goto out_unlock_enable
;
736 /* If the cookie is being invalidated, wait for that to complete first
737 * so that we can reuse the flag.
739 __fscache_wait_on_invalidate(cookie
);
741 /* Dispose of the backing objects */
742 set_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
);
744 spin_lock(&cookie
->lock
);
745 if (!hlist_empty(&cookie
->backing_objects
)) {
746 hlist_for_each_entry(object
, &cookie
->backing_objects
, cookie_link
) {
748 set_bit(FSCACHE_OBJECT_RETIRED
, &object
->flags
);
749 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
750 fscache_raise_event(object
, FSCACHE_OBJECT_EV_KILL
);
753 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
))
756 spin_unlock(&cookie
->lock
);
758 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
);
760 /* Wait for cessation of activity requiring access to the netfs (when
761 * n_active reaches 0). This makes sure outstanding reads and writes
764 if (!atomic_dec_and_test(&cookie
->n_active
)) {
765 wait_var_event(&cookie
->n_active
,
766 !atomic_read(&cookie
->n_active
));
769 /* Make sure any pending writes are cancelled. */
770 if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
)
771 fscache_invalidate_writes(cookie
);
773 /* Reset the cookie state if it wasn't relinquished */
774 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED
, &cookie
->flags
)) {
775 atomic_inc(&cookie
->n_active
);
776 set_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
780 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK
, &cookie
->flags
);
781 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
);
784 EXPORT_SYMBOL(__fscache_disable_cookie
);
787 * release a cookie back to the cache
788 * - the object will be marked as recyclable on disk if retire is true
789 * - all dependents of this cookie must have already been unregistered
790 * (indices/files/pages)
792 void __fscache_relinquish_cookie(struct fscache_cookie
*cookie
,
793 const void *aux_data
,
796 fscache_stat(&fscache_n_relinquishes
);
798 fscache_stat(&fscache_n_relinquishes_retire
);
801 fscache_stat(&fscache_n_relinquishes_null
);
802 _leave(" [no cookie]");
806 _enter("%p{%s,%p,%d},%d",
807 cookie
, cookie
->def
->name
, cookie
->netfs_data
,
808 atomic_read(&cookie
->n_active
), retire
);
810 trace_fscache_relinquish(cookie
, retire
);
812 /* No further netfs-accessing operations on this cookie permitted */
813 if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED
, &cookie
->flags
))
816 __fscache_disable_cookie(cookie
, aux_data
, retire
);
818 /* Clear pointers back to the netfs */
819 cookie
->netfs_data
= NULL
;
821 BUG_ON(!radix_tree_empty(&cookie
->stores
));
823 if (cookie
->parent
) {
824 ASSERTCMP(atomic_read(&cookie
->parent
->usage
), >, 0);
825 ASSERTCMP(atomic_read(&cookie
->parent
->n_children
), >, 0);
826 atomic_dec(&cookie
->parent
->n_children
);
829 /* Dispose of the netfs's link to the cookie */
830 ASSERTCMP(atomic_read(&cookie
->usage
), >, 0);
831 fscache_cookie_put(cookie
, fscache_cookie_put_relinquish
);
835 EXPORT_SYMBOL(__fscache_relinquish_cookie
);
838 * Remove a cookie from the hash table.
840 static void fscache_unhash_cookie(struct fscache_cookie
*cookie
)
842 struct hlist_bl_head
*h
;
845 bucket
= cookie
->key_hash
& (ARRAY_SIZE(fscache_cookie_hash
) - 1);
846 h
= &fscache_cookie_hash
[bucket
];
849 hlist_bl_del(&cookie
->hash_link
);
854 * Drop a reference to a cookie.
856 void fscache_cookie_put(struct fscache_cookie
*cookie
,
857 enum fscache_cookie_trace where
)
859 struct fscache_cookie
*parent
;
862 _enter("%p", cookie
);
865 usage
= atomic_dec_return(&cookie
->usage
);
866 trace_fscache_cookie(cookie
, where
, usage
);
872 parent
= cookie
->parent
;
873 fscache_unhash_cookie(cookie
);
874 fscache_free_cookie(cookie
);
877 where
= fscache_cookie_put_parent
;
884 * check the consistency between the netfs inode and the backing cache
886 * NOTE: it only serves no-index type
888 int __fscache_check_consistency(struct fscache_cookie
*cookie
,
889 const void *aux_data
)
891 struct fscache_operation
*op
;
892 struct fscache_object
*object
;
893 bool wake_cookie
= false;
896 _enter("%p,", cookie
);
898 ASSERTCMP(cookie
->type
, ==, FSCACHE_COOKIE_TYPE_DATAFILE
);
900 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
903 if (hlist_empty(&cookie
->backing_objects
))
906 op
= kzalloc(sizeof(*op
), GFP_NOIO
| __GFP_NOMEMALLOC
| __GFP_NORETRY
);
910 fscache_operation_init(cookie
, op
, NULL
, NULL
, NULL
);
911 op
->flags
= FSCACHE_OP_MYTHREAD
|
912 (1 << FSCACHE_OP_WAITING
) |
913 (1 << FSCACHE_OP_UNUSE_COOKIE
);
914 trace_fscache_page_op(cookie
, NULL
, op
, fscache_page_op_check_consistency
);
916 spin_lock(&cookie
->lock
);
918 fscache_update_aux(cookie
, aux_data
);
920 if (!fscache_cookie_enabled(cookie
) ||
921 hlist_empty(&cookie
->backing_objects
))
923 object
= hlist_entry(cookie
->backing_objects
.first
,
924 struct fscache_object
, cookie_link
);
925 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
928 op
->debug_id
= atomic_inc_return(&fscache_op_debug_id
);
930 __fscache_use_cookie(cookie
);
931 if (fscache_submit_op(object
, op
) < 0)
934 /* the work queue now carries its own ref on the object */
935 spin_unlock(&cookie
->lock
);
937 ret
= fscache_wait_for_operation_activation(object
, op
, NULL
, NULL
);
939 /* ask the cache to honour the operation */
940 ret
= object
->cache
->ops
->check_consistency(op
);
941 fscache_op_complete(op
, false);
942 } else if (ret
== -ENOBUFS
) {
946 fscache_put_operation(op
);
947 _leave(" = %d", ret
);
951 wake_cookie
= __fscache_unuse_cookie(cookie
);
953 spin_unlock(&cookie
->lock
);
955 __fscache_wake_unused_cookie(cookie
);
957 _leave(" = -ESTALE");
960 EXPORT_SYMBOL(__fscache_check_consistency
);