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
)
86 bufs
= DIV_ROUND_UP(index_key_len
, sizeof(*buf
));
88 if (index_key_len
> sizeof(cookie
->inline_key
)) {
89 buf
= kcalloc(bufs
, sizeof(*buf
), GFP_KERNEL
);
94 buf
= (u32
*)cookie
->inline_key
;
97 memcpy(buf
, index_key
, index_key_len
);
99 /* Calculate a hash and combine this with the length in the first word
102 h
= (unsigned long)cookie
->parent
;
103 h
+= index_key_len
+ cookie
->type
;
105 for (i
= 0; i
< bufs
; i
++)
108 cookie
->key_hash
= h
^ (h
>> 32);
112 static long fscache_compare_cookie(const struct fscache_cookie
*a
,
113 const struct fscache_cookie
*b
)
117 if (a
->key_hash
!= b
->key_hash
)
118 return (long)a
->key_hash
- (long)b
->key_hash
;
119 if (a
->parent
!= b
->parent
)
120 return (long)a
->parent
- (long)b
->parent
;
121 if (a
->key_len
!= b
->key_len
)
122 return (long)a
->key_len
- (long)b
->key_len
;
123 if (a
->type
!= b
->type
)
124 return (long)a
->type
- (long)b
->type
;
126 if (a
->key_len
<= sizeof(a
->inline_key
)) {
133 return memcmp(ka
, kb
, a
->key_len
);
139 struct fscache_cookie
*fscache_alloc_cookie(
140 struct fscache_cookie
*parent
,
141 const struct fscache_cookie_def
*def
,
142 const void *index_key
, size_t index_key_len
,
143 const void *aux_data
, size_t aux_data_len
,
147 struct fscache_cookie
*cookie
;
149 /* allocate and initialise a cookie */
150 cookie
= kmem_cache_zalloc(fscache_cookie_jar
, GFP_KERNEL
);
154 cookie
->key_len
= index_key_len
;
155 cookie
->aux_len
= aux_data_len
;
157 if (fscache_set_key(cookie
, index_key
, index_key_len
) < 0)
160 if (cookie
->aux_len
<= sizeof(cookie
->inline_aux
)) {
161 memcpy(cookie
->inline_aux
, aux_data
, cookie
->aux_len
);
163 cookie
->aux
= kmemdup(aux_data
, cookie
->aux_len
, GFP_KERNEL
);
168 atomic_set(&cookie
->usage
, 1);
169 atomic_set(&cookie
->n_children
, 0);
171 /* We keep the active count elevated until relinquishment to prevent an
172 * attempt to wake up every time the object operations queue quiesces.
174 atomic_set(&cookie
->n_active
, 1);
177 cookie
->parent
= parent
;
178 cookie
->netfs_data
= netfs_data
;
179 cookie
->flags
= (1 << FSCACHE_COOKIE_NO_DATA_YET
);
180 cookie
->type
= def
->type
;
181 spin_lock_init(&cookie
->lock
);
182 spin_lock_init(&cookie
->stores_lock
);
183 INIT_HLIST_HEAD(&cookie
->backing_objects
);
185 /* radix tree insertion won't use the preallocation pool unless it's
186 * told it may not wait */
187 INIT_RADIX_TREE(&cookie
->stores
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
191 fscache_free_cookie(cookie
);
196 * Attempt to insert the new cookie into the hash. If there's a collision, we
197 * return the old cookie if it's not in use and an error otherwise.
199 struct fscache_cookie
*fscache_hash_cookie(struct fscache_cookie
*candidate
)
201 struct fscache_cookie
*cursor
;
202 struct hlist_bl_head
*h
;
203 struct hlist_bl_node
*p
;
206 bucket
= candidate
->key_hash
& (ARRAY_SIZE(fscache_cookie_hash
) - 1);
207 h
= &fscache_cookie_hash
[bucket
];
210 hlist_bl_for_each_entry(cursor
, p
, h
, hash_link
) {
211 if (fscache_compare_cookie(candidate
, cursor
) == 0)
215 __set_bit(FSCACHE_COOKIE_ACQUIRED
, &candidate
->flags
);
216 fscache_cookie_get(candidate
->parent
, fscache_cookie_get_acquire_parent
);
217 atomic_inc(&candidate
->parent
->n_children
);
218 hlist_bl_add_head(&candidate
->hash_link
, h
);
223 if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED
, &cursor
->flags
)) {
224 trace_fscache_cookie(cursor
, fscache_cookie_collision
,
225 atomic_read(&cursor
->usage
));
226 pr_err("Duplicate cookie detected\n");
227 fscache_print_cookie(cursor
, 'O');
228 fscache_print_cookie(candidate
, 'N');
233 fscache_cookie_get(cursor
, fscache_cookie_get_reacquire
);
239 * request a cookie to represent an object (index, datafile, xattr, etc)
240 * - parent specifies the parent object
241 * - the top level index cookie for each netfs is stored in the fscache_netfs
242 * struct upon registration
243 * - def points to the definition
244 * - the netfs_data will be passed to the functions pointed to in *def
245 * - all attached caches will be searched to see if they contain this object
246 * - index objects aren't stored on disk until there's a dependent file that
248 * - other objects are stored in a selected cache immediately, and all the
249 * indices forming the path to it are instantiated if necessary
250 * - we never let on to the netfs about errors
251 * - we may set a negative cookie pointer, but that's okay
253 struct fscache_cookie
*__fscache_acquire_cookie(
254 struct fscache_cookie
*parent
,
255 const struct fscache_cookie_def
*def
,
256 const void *index_key
, size_t index_key_len
,
257 const void *aux_data
, size_t aux_data_len
,
262 struct fscache_cookie
*candidate
, *cookie
;
266 _enter("{%s},{%s},%p,%u",
267 parent
? (char *) parent
->def
->name
: "<no-parent>",
268 def
->name
, netfs_data
, enable
);
270 if (!index_key
|| !index_key_len
|| index_key_len
> 255 || aux_data_len
> 255)
272 if (!aux_data
|| !aux_data_len
) {
277 fscache_stat(&fscache_n_acquires
);
279 /* if there's no parent cookie, then we don't create one here either */
281 fscache_stat(&fscache_n_acquires_null
);
282 _leave(" [no parent]");
286 /* validate the definition */
287 BUG_ON(!def
->name
[0]);
289 BUG_ON(def
->type
== FSCACHE_COOKIE_TYPE_INDEX
&&
290 parent
->type
!= FSCACHE_COOKIE_TYPE_INDEX
);
292 candidate
= fscache_alloc_cookie(parent
, def
,
293 index_key
, index_key_len
,
294 aux_data
, aux_data_len
,
295 netfs_data
, object_size
);
297 fscache_stat(&fscache_n_acquires_oom
);
302 cookie
= fscache_hash_cookie(candidate
);
304 trace_fscache_cookie(candidate
, fscache_cookie_discard
, 1);
308 if (cookie
== candidate
)
311 switch (cookie
->type
) {
312 case FSCACHE_COOKIE_TYPE_INDEX
:
313 fscache_stat(&fscache_n_cookie_index
);
315 case FSCACHE_COOKIE_TYPE_DATAFILE
:
316 fscache_stat(&fscache_n_cookie_data
);
319 fscache_stat(&fscache_n_cookie_special
);
323 trace_fscache_acquire(cookie
);
326 /* if the object is an index then we need do nothing more here
327 * - we create indices on disk when we need them as an index
328 * may exist in multiple caches */
329 if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
330 if (fscache_acquire_non_index_cookie(cookie
, object_size
) == 0) {
331 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
333 atomic_dec(&parent
->n_children
);
334 fscache_cookie_put(cookie
,
335 fscache_cookie_put_acquire_nobufs
);
336 fscache_stat(&fscache_n_acquires_nobufs
);
341 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
345 fscache_stat(&fscache_n_acquires_ok
);
348 fscache_free_cookie(candidate
);
351 EXPORT_SYMBOL(__fscache_acquire_cookie
);
354 * Enable a cookie to permit it to accept new operations.
356 void __fscache_enable_cookie(struct fscache_cookie
*cookie
,
357 const void *aux_data
,
359 bool (*can_enable
)(void *data
),
362 _enter("%p", cookie
);
364 trace_fscache_enable(cookie
);
366 wait_on_bit_lock(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
,
367 TASK_UNINTERRUPTIBLE
);
369 fscache_update_aux(cookie
, aux_data
);
371 if (test_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
))
374 if (can_enable
&& !can_enable(data
)) {
375 /* The netfs decided it didn't want to enable after all */
376 } else if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
377 /* Wait for outstanding disablement to complete */
378 __fscache_wait_on_invalidate(cookie
);
380 if (fscache_acquire_non_index_cookie(cookie
, object_size
) == 0)
381 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
383 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
387 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK
, &cookie
->flags
);
388 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
);
390 EXPORT_SYMBOL(__fscache_enable_cookie
);
393 * acquire a non-index cookie
394 * - this must make sure the index chain is instantiated and instantiate the
395 * object representation too
397 static int fscache_acquire_non_index_cookie(struct fscache_cookie
*cookie
,
400 struct fscache_object
*object
;
401 struct fscache_cache
*cache
;
406 set_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
);
408 /* now we need to see whether the backing objects for this cookie yet
409 * exist, if not there'll be nothing to search */
410 down_read(&fscache_addremove_sem
);
412 if (list_empty(&fscache_cache_list
)) {
413 up_read(&fscache_addremove_sem
);
414 _leave(" = 0 [no caches]");
418 /* select a cache in which to store the object */
419 cache
= fscache_select_cache_for_object(cookie
->parent
);
421 up_read(&fscache_addremove_sem
);
422 fscache_stat(&fscache_n_acquires_no_cache
);
423 _leave(" = -ENOMEDIUM [no cache]");
427 _debug("cache %s", cache
->tag
->name
);
429 set_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
431 /* ask the cache to allocate objects for this cookie and its parent
433 ret
= fscache_alloc_object(cache
, cookie
);
435 up_read(&fscache_addremove_sem
);
436 _leave(" = %d", ret
);
440 spin_lock(&cookie
->lock
);
441 if (hlist_empty(&cookie
->backing_objects
)) {
442 spin_unlock(&cookie
->lock
);
446 object
= hlist_entry(cookie
->backing_objects
.first
,
447 struct fscache_object
, cookie_link
);
449 fscache_set_store_limit(object
, object_size
);
451 /* initiate the process of looking up all the objects in the chain
452 * (done by fscache_initialise_object()) */
453 fscache_raise_event(object
, FSCACHE_OBJECT_EV_NEW_CHILD
);
455 spin_unlock(&cookie
->lock
);
457 /* we may be required to wait for lookup to complete at this point */
458 if (!fscache_defer_lookup
) {
459 _debug("non-deferred lookup %p", &cookie
->flags
);
460 wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
461 TASK_UNINTERRUPTIBLE
);
463 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
))
467 up_read(&fscache_addremove_sem
);
468 _leave(" = 0 [deferred]");
472 up_read(&fscache_addremove_sem
);
473 _leave(" = -ENOBUFS");
478 * recursively allocate cache object records for a cookie/cache combination
479 * - caller must be holding the addremove sem
481 static int fscache_alloc_object(struct fscache_cache
*cache
,
482 struct fscache_cookie
*cookie
)
484 struct fscache_object
*object
;
487 _enter("%p,%p{%s}", cache
, cookie
, cookie
->def
->name
);
489 spin_lock(&cookie
->lock
);
490 hlist_for_each_entry(object
, &cookie
->backing_objects
,
492 if (object
->cache
== cache
)
493 goto object_already_extant
;
495 spin_unlock(&cookie
->lock
);
497 /* ask the cache to allocate an object (we may end up with duplicate
498 * objects at this stage, but we sort that out later) */
499 fscache_stat(&fscache_n_cop_alloc_object
);
500 object
= cache
->ops
->alloc_object(cache
, cookie
);
501 fscache_stat_d(&fscache_n_cop_alloc_object
);
502 if (IS_ERR(object
)) {
503 fscache_stat(&fscache_n_object_no_alloc
);
504 ret
= PTR_ERR(object
);
508 ASSERTCMP(object
->cookie
, ==, cookie
);
509 fscache_stat(&fscache_n_object_alloc
);
511 object
->debug_id
= atomic_inc_return(&fscache_object_debug_id
);
513 _debug("ALLOC OBJ%x: %s {%lx}",
514 object
->debug_id
, cookie
->def
->name
, object
->events
);
516 ret
= fscache_alloc_object(cache
, cookie
->parent
);
520 /* only attach if we managed to allocate all we needed, otherwise
521 * discard the object we just allocated and instead use the one
522 * attached to the cookie */
523 if (fscache_attach_object(cookie
, object
) < 0) {
524 fscache_stat(&fscache_n_cop_put_object
);
525 cache
->ops
->put_object(object
, fscache_obj_put_attach_fail
);
526 fscache_stat_d(&fscache_n_cop_put_object
);
532 object_already_extant
:
534 if (fscache_object_is_dying(object
) ||
535 fscache_cache_is_broken(object
)) {
536 spin_unlock(&cookie
->lock
);
539 spin_unlock(&cookie
->lock
);
540 _leave(" = 0 [found]");
544 fscache_stat(&fscache_n_cop_put_object
);
545 cache
->ops
->put_object(object
, fscache_obj_put_alloc_fail
);
546 fscache_stat_d(&fscache_n_cop_put_object
);
548 _leave(" = %d", ret
);
553 * attach a cache object to a cookie
555 static int fscache_attach_object(struct fscache_cookie
*cookie
,
556 struct fscache_object
*object
)
558 struct fscache_object
*p
;
559 struct fscache_cache
*cache
= object
->cache
;
562 _enter("{%s},{OBJ%x}", cookie
->def
->name
, object
->debug_id
);
564 ASSERTCMP(object
->cookie
, ==, cookie
);
566 spin_lock(&cookie
->lock
);
568 /* there may be multiple initial creations of this object, but we only
571 hlist_for_each_entry(p
, &cookie
->backing_objects
, cookie_link
) {
572 if (p
->cache
== object
->cache
) {
573 if (fscache_object_is_dying(p
))
575 goto cant_attach_object
;
579 /* pin the parent object */
580 spin_lock_nested(&cookie
->parent
->lock
, 1);
581 hlist_for_each_entry(p
, &cookie
->parent
->backing_objects
,
583 if (p
->cache
== object
->cache
) {
584 if (fscache_object_is_dying(p
)) {
586 spin_unlock(&cookie
->parent
->lock
);
587 goto cant_attach_object
;
592 spin_unlock(&p
->lock
);
596 spin_unlock(&cookie
->parent
->lock
);
598 /* attach to the cache's object list */
599 if (list_empty(&object
->cache_link
)) {
600 spin_lock(&cache
->object_list_lock
);
601 list_add(&object
->cache_link
, &cache
->object_list
);
602 spin_unlock(&cache
->object_list_lock
);
605 /* Attach to the cookie. The object already has a ref on it. */
606 hlist_add_head(&object
->cookie_link
, &cookie
->backing_objects
);
608 fscache_objlist_add(object
);
612 spin_unlock(&cookie
->lock
);
613 _leave(" = %d", ret
);
618 * Invalidate an object. Callable with spinlocks held.
620 void __fscache_invalidate(struct fscache_cookie
*cookie
)
622 struct fscache_object
*object
;
624 _enter("{%s}", cookie
->def
->name
);
626 fscache_stat(&fscache_n_invalidates
);
628 /* Only permit invalidation of data files. Invalidating an index will
629 * require the caller to release all its attachments to the tree rooted
630 * there, and if it's doing that, it may as well just retire the
633 ASSERTCMP(cookie
->type
, ==, FSCACHE_COOKIE_TYPE_DATAFILE
);
635 /* If there's an object, we tell the object state machine to handle the
636 * invalidation on our behalf, otherwise there's nothing to do.
638 if (!hlist_empty(&cookie
->backing_objects
)) {
639 spin_lock(&cookie
->lock
);
641 if (fscache_cookie_enabled(cookie
) &&
642 !hlist_empty(&cookie
->backing_objects
) &&
643 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING
,
645 object
= hlist_entry(cookie
->backing_objects
.first
,
646 struct fscache_object
,
648 if (fscache_object_is_live(object
))
650 object
, FSCACHE_OBJECT_EV_INVALIDATE
);
653 spin_unlock(&cookie
->lock
);
658 EXPORT_SYMBOL(__fscache_invalidate
);
661 * Wait for object invalidation to complete.
663 void __fscache_wait_on_invalidate(struct fscache_cookie
*cookie
)
665 _enter("%p", cookie
);
667 wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
,
668 TASK_UNINTERRUPTIBLE
);
672 EXPORT_SYMBOL(__fscache_wait_on_invalidate
);
675 * update the index entries backing a cookie
677 void __fscache_update_cookie(struct fscache_cookie
*cookie
, const void *aux_data
)
679 struct fscache_object
*object
;
681 fscache_stat(&fscache_n_updates
);
684 fscache_stat(&fscache_n_updates_null
);
685 _leave(" [no cookie]");
689 _enter("{%s}", cookie
->def
->name
);
691 spin_lock(&cookie
->lock
);
693 fscache_update_aux(cookie
, aux_data
);
695 if (fscache_cookie_enabled(cookie
)) {
696 /* update the index entry on disk in each cache backing this
699 hlist_for_each_entry(object
,
700 &cookie
->backing_objects
, cookie_link
) {
701 fscache_raise_event(object
, FSCACHE_OBJECT_EV_UPDATE
);
705 spin_unlock(&cookie
->lock
);
708 EXPORT_SYMBOL(__fscache_update_cookie
);
711 * Disable a cookie to stop it from accepting new requests from the netfs.
713 void __fscache_disable_cookie(struct fscache_cookie
*cookie
,
714 const void *aux_data
,
717 struct fscache_object
*object
;
720 _enter("%p,%u", cookie
, invalidate
);
722 trace_fscache_disable(cookie
);
724 ASSERTCMP(atomic_read(&cookie
->n_active
), >, 0);
726 if (atomic_read(&cookie
->n_children
) != 0) {
727 pr_err("Cookie '%s' still has children\n",
732 wait_on_bit_lock(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
,
733 TASK_UNINTERRUPTIBLE
);
735 fscache_update_aux(cookie
, aux_data
);
737 if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
))
738 goto out_unlock_enable
;
740 /* If the cookie is being invalidated, wait for that to complete first
741 * so that we can reuse the flag.
743 __fscache_wait_on_invalidate(cookie
);
745 /* Dispose of the backing objects */
746 set_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
);
748 spin_lock(&cookie
->lock
);
749 if (!hlist_empty(&cookie
->backing_objects
)) {
750 hlist_for_each_entry(object
, &cookie
->backing_objects
, cookie_link
) {
752 set_bit(FSCACHE_OBJECT_RETIRED
, &object
->flags
);
753 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
754 fscache_raise_event(object
, FSCACHE_OBJECT_EV_KILL
);
757 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
))
760 spin_unlock(&cookie
->lock
);
762 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
);
764 /* Wait for cessation of activity requiring access to the netfs (when
765 * n_active reaches 0). This makes sure outstanding reads and writes
768 if (!atomic_dec_and_test(&cookie
->n_active
)) {
769 wait_var_event(&cookie
->n_active
,
770 !atomic_read(&cookie
->n_active
));
773 /* Make sure any pending writes are cancelled. */
774 if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
)
775 fscache_invalidate_writes(cookie
);
777 /* Reset the cookie state if it wasn't relinquished */
778 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED
, &cookie
->flags
)) {
779 atomic_inc(&cookie
->n_active
);
780 set_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
784 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK
, &cookie
->flags
);
785 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
);
788 EXPORT_SYMBOL(__fscache_disable_cookie
);
791 * release a cookie back to the cache
792 * - the object will be marked as recyclable on disk if retire is true
793 * - all dependents of this cookie must have already been unregistered
794 * (indices/files/pages)
796 void __fscache_relinquish_cookie(struct fscache_cookie
*cookie
,
797 const void *aux_data
,
800 fscache_stat(&fscache_n_relinquishes
);
802 fscache_stat(&fscache_n_relinquishes_retire
);
805 fscache_stat(&fscache_n_relinquishes_null
);
806 _leave(" [no cookie]");
810 _enter("%p{%s,%p,%d},%d",
811 cookie
, cookie
->def
->name
, cookie
->netfs_data
,
812 atomic_read(&cookie
->n_active
), retire
);
814 trace_fscache_relinquish(cookie
, retire
);
816 /* No further netfs-accessing operations on this cookie permitted */
817 if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED
, &cookie
->flags
))
820 __fscache_disable_cookie(cookie
, aux_data
, retire
);
822 /* Clear pointers back to the netfs */
823 cookie
->netfs_data
= NULL
;
825 BUG_ON(!radix_tree_empty(&cookie
->stores
));
827 if (cookie
->parent
) {
828 ASSERTCMP(atomic_read(&cookie
->parent
->usage
), >, 0);
829 ASSERTCMP(atomic_read(&cookie
->parent
->n_children
), >, 0);
830 atomic_dec(&cookie
->parent
->n_children
);
833 /* Dispose of the netfs's link to the cookie */
834 ASSERTCMP(atomic_read(&cookie
->usage
), >, 0);
835 fscache_cookie_put(cookie
, fscache_cookie_put_relinquish
);
839 EXPORT_SYMBOL(__fscache_relinquish_cookie
);
842 * Remove a cookie from the hash table.
844 static void fscache_unhash_cookie(struct fscache_cookie
*cookie
)
846 struct hlist_bl_head
*h
;
849 bucket
= cookie
->key_hash
& (ARRAY_SIZE(fscache_cookie_hash
) - 1);
850 h
= &fscache_cookie_hash
[bucket
];
853 hlist_bl_del(&cookie
->hash_link
);
858 * Drop a reference to a cookie.
860 void fscache_cookie_put(struct fscache_cookie
*cookie
,
861 enum fscache_cookie_trace where
)
863 struct fscache_cookie
*parent
;
866 _enter("%p", cookie
);
869 usage
= atomic_dec_return(&cookie
->usage
);
870 trace_fscache_cookie(cookie
, where
, usage
);
876 parent
= cookie
->parent
;
877 fscache_unhash_cookie(cookie
);
878 fscache_free_cookie(cookie
);
881 where
= fscache_cookie_put_parent
;
888 * check the consistency between the netfs inode and the backing cache
890 * NOTE: it only serves no-index type
892 int __fscache_check_consistency(struct fscache_cookie
*cookie
,
893 const void *aux_data
)
895 struct fscache_operation
*op
;
896 struct fscache_object
*object
;
897 bool wake_cookie
= false;
900 _enter("%p,", cookie
);
902 ASSERTCMP(cookie
->type
, ==, FSCACHE_COOKIE_TYPE_DATAFILE
);
904 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
907 if (hlist_empty(&cookie
->backing_objects
))
910 op
= kzalloc(sizeof(*op
), GFP_NOIO
| __GFP_NOMEMALLOC
| __GFP_NORETRY
);
914 fscache_operation_init(cookie
, op
, NULL
, NULL
, NULL
);
915 op
->flags
= FSCACHE_OP_MYTHREAD
|
916 (1 << FSCACHE_OP_WAITING
) |
917 (1 << FSCACHE_OP_UNUSE_COOKIE
);
918 trace_fscache_page_op(cookie
, NULL
, op
, fscache_page_op_check_consistency
);
920 spin_lock(&cookie
->lock
);
922 fscache_update_aux(cookie
, aux_data
);
924 if (!fscache_cookie_enabled(cookie
) ||
925 hlist_empty(&cookie
->backing_objects
))
927 object
= hlist_entry(cookie
->backing_objects
.first
,
928 struct fscache_object
, cookie_link
);
929 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
932 op
->debug_id
= atomic_inc_return(&fscache_op_debug_id
);
934 __fscache_use_cookie(cookie
);
935 if (fscache_submit_op(object
, op
) < 0)
938 /* the work queue now carries its own ref on the object */
939 spin_unlock(&cookie
->lock
);
941 ret
= fscache_wait_for_operation_activation(object
, op
, NULL
, NULL
);
943 /* ask the cache to honour the operation */
944 ret
= object
->cache
->ops
->check_consistency(op
);
945 fscache_op_complete(op
, false);
946 } else if (ret
== -ENOBUFS
) {
950 fscache_put_operation(op
);
951 _leave(" = %d", ret
);
955 wake_cookie
= __fscache_unuse_cookie(cookie
);
957 spin_unlock(&cookie
->lock
);
959 __fscache_wake_unused_cookie(cookie
);
961 _leave(" = -ESTALE");
964 EXPORT_SYMBOL(__fscache_check_consistency
);