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 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
);
31 * initialise an cookie jar slab element prior to any use
33 void fscache_cookie_init_once(void *_cookie
)
35 struct fscache_cookie
*cookie
= _cookie
;
37 memset(cookie
, 0, sizeof(*cookie
));
38 spin_lock_init(&cookie
->lock
);
39 spin_lock_init(&cookie
->stores_lock
);
40 INIT_HLIST_HEAD(&cookie
->backing_objects
);
44 * request a cookie to represent an object (index, datafile, xattr, etc)
45 * - parent specifies the parent object
46 * - the top level index cookie for each netfs is stored in the fscache_netfs
47 * struct upon registration
48 * - def points to the definition
49 * - the netfs_data will be passed to the functions pointed to in *def
50 * - all attached caches will be searched to see if they contain this object
51 * - index objects aren't stored on disk until there's a dependent file that
53 * - other objects are stored in a selected cache immediately, and all the
54 * indices forming the path to it are instantiated if necessary
55 * - we never let on to the netfs about errors
56 * - we may set a negative cookie pointer, but that's okay
58 struct fscache_cookie
*__fscache_acquire_cookie(
59 struct fscache_cookie
*parent
,
60 const struct fscache_cookie_def
*def
,
64 struct fscache_cookie
*cookie
;
68 _enter("{%s},{%s},%p,%u",
69 parent
? (char *) parent
->def
->name
: "<no-parent>",
70 def
->name
, netfs_data
, enable
);
72 fscache_stat(&fscache_n_acquires
);
74 /* if there's no parent cookie, then we don't create one here either */
76 fscache_stat(&fscache_n_acquires_null
);
77 _leave(" [no parent]");
81 /* validate the definition */
82 BUG_ON(!def
->get_key
);
83 BUG_ON(!def
->name
[0]);
85 BUG_ON(def
->type
== FSCACHE_COOKIE_TYPE_INDEX
&&
86 parent
->def
->type
!= FSCACHE_COOKIE_TYPE_INDEX
);
88 /* allocate and initialise a cookie */
89 cookie
= kmem_cache_alloc(fscache_cookie_jar
, GFP_KERNEL
);
91 fscache_stat(&fscache_n_acquires_oom
);
96 atomic_set(&cookie
->usage
, 1);
97 atomic_set(&cookie
->n_children
, 0);
99 /* We keep the active count elevated until relinquishment to prevent an
100 * attempt to wake up every time the object operations queue quiesces.
102 atomic_set(&cookie
->n_active
, 1);
104 atomic_inc(&parent
->usage
);
105 atomic_inc(&parent
->n_children
);
108 cookie
->parent
= parent
;
109 cookie
->netfs_data
= netfs_data
;
110 cookie
->flags
= (1 << FSCACHE_COOKIE_NO_DATA_YET
);
112 /* radix tree insertion won't use the preallocation pool unless it's
113 * told it may not wait */
114 INIT_RADIX_TREE(&cookie
->stores
, GFP_NOFS
& ~__GFP_WAIT
);
116 switch (cookie
->def
->type
) {
117 case FSCACHE_COOKIE_TYPE_INDEX
:
118 fscache_stat(&fscache_n_cookie_index
);
120 case FSCACHE_COOKIE_TYPE_DATAFILE
:
121 fscache_stat(&fscache_n_cookie_data
);
124 fscache_stat(&fscache_n_cookie_special
);
129 /* if the object is an index then we need do nothing more here
130 * - we create indices on disk when we need them as an index
131 * may exist in multiple caches */
132 if (cookie
->def
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
133 if (fscache_acquire_non_index_cookie(cookie
) == 0) {
134 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
136 atomic_dec(&parent
->n_children
);
137 __fscache_cookie_put(cookie
);
138 fscache_stat(&fscache_n_acquires_nobufs
);
143 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
147 fscache_stat(&fscache_n_acquires_ok
);
148 _leave(" = %p", cookie
);
151 EXPORT_SYMBOL(__fscache_acquire_cookie
);
154 * Enable a cookie to permit it to accept new operations.
156 void __fscache_enable_cookie(struct fscache_cookie
*cookie
,
157 bool (*can_enable
)(void *data
),
160 _enter("%p", cookie
);
162 wait_on_bit_lock(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
,
163 TASK_UNINTERRUPTIBLE
);
165 if (test_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
))
168 if (can_enable
&& !can_enable(data
)) {
169 /* The netfs decided it didn't want to enable after all */
170 } else if (cookie
->def
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
171 /* Wait for outstanding disablement to complete */
172 __fscache_wait_on_invalidate(cookie
);
174 if (fscache_acquire_non_index_cookie(cookie
) == 0)
175 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
177 set_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
);
181 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK
, &cookie
->flags
);
182 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
);
184 EXPORT_SYMBOL(__fscache_enable_cookie
);
187 * acquire a non-index cookie
188 * - this must make sure the index chain is instantiated and instantiate the
189 * object representation too
191 static int fscache_acquire_non_index_cookie(struct fscache_cookie
*cookie
)
193 struct fscache_object
*object
;
194 struct fscache_cache
*cache
;
200 set_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
);
202 /* now we need to see whether the backing objects for this cookie yet
203 * exist, if not there'll be nothing to search */
204 down_read(&fscache_addremove_sem
);
206 if (list_empty(&fscache_cache_list
)) {
207 up_read(&fscache_addremove_sem
);
208 _leave(" = 0 [no caches]");
212 /* select a cache in which to store the object */
213 cache
= fscache_select_cache_for_object(cookie
->parent
);
215 up_read(&fscache_addremove_sem
);
216 fscache_stat(&fscache_n_acquires_no_cache
);
217 _leave(" = -ENOMEDIUM [no cache]");
221 _debug("cache %s", cache
->tag
->name
);
223 set_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
225 /* ask the cache to allocate objects for this cookie and its parent
227 ret
= fscache_alloc_object(cache
, cookie
);
229 up_read(&fscache_addremove_sem
);
230 _leave(" = %d", ret
);
234 /* pass on how big the object we're caching is supposed to be */
235 cookie
->def
->get_attr(cookie
->netfs_data
, &i_size
);
237 spin_lock(&cookie
->lock
);
238 if (hlist_empty(&cookie
->backing_objects
)) {
239 spin_unlock(&cookie
->lock
);
243 object
= hlist_entry(cookie
->backing_objects
.first
,
244 struct fscache_object
, cookie_link
);
246 fscache_set_store_limit(object
, i_size
);
248 /* initiate the process of looking up all the objects in the chain
249 * (done by fscache_initialise_object()) */
250 fscache_raise_event(object
, FSCACHE_OBJECT_EV_NEW_CHILD
);
252 spin_unlock(&cookie
->lock
);
254 /* we may be required to wait for lookup to complete at this point */
255 if (!fscache_defer_lookup
) {
256 _debug("non-deferred lookup %p", &cookie
->flags
);
257 wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
,
258 TASK_UNINTERRUPTIBLE
);
260 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
))
264 up_read(&fscache_addremove_sem
);
265 _leave(" = 0 [deferred]");
269 up_read(&fscache_addremove_sem
);
270 _leave(" = -ENOBUFS");
275 * recursively allocate cache object records for a cookie/cache combination
276 * - caller must be holding the addremove sem
278 static int fscache_alloc_object(struct fscache_cache
*cache
,
279 struct fscache_cookie
*cookie
)
281 struct fscache_object
*object
;
284 _enter("%p,%p{%s}", cache
, cookie
, cookie
->def
->name
);
286 spin_lock(&cookie
->lock
);
287 hlist_for_each_entry(object
, &cookie
->backing_objects
,
289 if (object
->cache
== cache
)
290 goto object_already_extant
;
292 spin_unlock(&cookie
->lock
);
294 /* ask the cache to allocate an object (we may end up with duplicate
295 * objects at this stage, but we sort that out later) */
296 fscache_stat(&fscache_n_cop_alloc_object
);
297 object
= cache
->ops
->alloc_object(cache
, cookie
);
298 fscache_stat_d(&fscache_n_cop_alloc_object
);
299 if (IS_ERR(object
)) {
300 fscache_stat(&fscache_n_object_no_alloc
);
301 ret
= PTR_ERR(object
);
305 fscache_stat(&fscache_n_object_alloc
);
307 object
->debug_id
= atomic_inc_return(&fscache_object_debug_id
);
309 _debug("ALLOC OBJ%x: %s {%lx}",
310 object
->debug_id
, cookie
->def
->name
, object
->events
);
312 ret
= fscache_alloc_object(cache
, cookie
->parent
);
316 /* only attach if we managed to allocate all we needed, otherwise
317 * discard the object we just allocated and instead use the one
318 * attached to the cookie */
319 if (fscache_attach_object(cookie
, object
) < 0) {
320 fscache_stat(&fscache_n_cop_put_object
);
321 cache
->ops
->put_object(object
);
322 fscache_stat_d(&fscache_n_cop_put_object
);
328 object_already_extant
:
330 if (fscache_object_is_dead(object
)) {
331 spin_unlock(&cookie
->lock
);
334 spin_unlock(&cookie
->lock
);
335 _leave(" = 0 [found]");
339 fscache_stat(&fscache_n_cop_put_object
);
340 cache
->ops
->put_object(object
);
341 fscache_stat_d(&fscache_n_cop_put_object
);
343 _leave(" = %d", ret
);
348 * attach a cache object to a cookie
350 static int fscache_attach_object(struct fscache_cookie
*cookie
,
351 struct fscache_object
*object
)
353 struct fscache_object
*p
;
354 struct fscache_cache
*cache
= object
->cache
;
357 _enter("{%s},{OBJ%x}", cookie
->def
->name
, object
->debug_id
);
359 spin_lock(&cookie
->lock
);
361 /* there may be multiple initial creations of this object, but we only
364 hlist_for_each_entry(p
, &cookie
->backing_objects
, cookie_link
) {
365 if (p
->cache
== object
->cache
) {
366 if (fscache_object_is_dying(p
))
368 goto cant_attach_object
;
372 /* pin the parent object */
373 spin_lock_nested(&cookie
->parent
->lock
, 1);
374 hlist_for_each_entry(p
, &cookie
->parent
->backing_objects
,
376 if (p
->cache
== object
->cache
) {
377 if (fscache_object_is_dying(p
)) {
379 spin_unlock(&cookie
->parent
->lock
);
380 goto cant_attach_object
;
385 spin_unlock(&p
->lock
);
389 spin_unlock(&cookie
->parent
->lock
);
391 /* attach to the cache's object list */
392 if (list_empty(&object
->cache_link
)) {
393 spin_lock(&cache
->object_list_lock
);
394 list_add(&object
->cache_link
, &cache
->object_list
);
395 spin_unlock(&cache
->object_list_lock
);
398 /* attach to the cookie */
399 object
->cookie
= cookie
;
400 atomic_inc(&cookie
->usage
);
401 hlist_add_head(&object
->cookie_link
, &cookie
->backing_objects
);
403 fscache_objlist_add(object
);
407 spin_unlock(&cookie
->lock
);
408 _leave(" = %d", ret
);
413 * Invalidate an object. Callable with spinlocks held.
415 void __fscache_invalidate(struct fscache_cookie
*cookie
)
417 struct fscache_object
*object
;
419 _enter("{%s}", cookie
->def
->name
);
421 fscache_stat(&fscache_n_invalidates
);
423 /* Only permit invalidation of data files. Invalidating an index will
424 * require the caller to release all its attachments to the tree rooted
425 * there, and if it's doing that, it may as well just retire the
428 ASSERTCMP(cookie
->def
->type
, ==, FSCACHE_COOKIE_TYPE_DATAFILE
);
430 /* We will be updating the cookie too. */
431 BUG_ON(!cookie
->def
->get_aux
);
433 /* If there's an object, we tell the object state machine to handle the
434 * invalidation on our behalf, otherwise there's nothing to do.
436 if (!hlist_empty(&cookie
->backing_objects
)) {
437 spin_lock(&cookie
->lock
);
439 if (fscache_cookie_enabled(cookie
) &&
440 !hlist_empty(&cookie
->backing_objects
) &&
441 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING
,
443 object
= hlist_entry(cookie
->backing_objects
.first
,
444 struct fscache_object
,
446 if (fscache_object_is_live(object
))
448 object
, FSCACHE_OBJECT_EV_INVALIDATE
);
451 spin_unlock(&cookie
->lock
);
456 EXPORT_SYMBOL(__fscache_invalidate
);
459 * Wait for object invalidation to complete.
461 void __fscache_wait_on_invalidate(struct fscache_cookie
*cookie
)
463 _enter("%p", cookie
);
465 wait_on_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
,
466 TASK_UNINTERRUPTIBLE
);
470 EXPORT_SYMBOL(__fscache_wait_on_invalidate
);
473 * update the index entries backing a cookie
475 void __fscache_update_cookie(struct fscache_cookie
*cookie
)
477 struct fscache_object
*object
;
479 fscache_stat(&fscache_n_updates
);
482 fscache_stat(&fscache_n_updates_null
);
483 _leave(" [no cookie]");
487 _enter("{%s}", cookie
->def
->name
);
489 BUG_ON(!cookie
->def
->get_aux
);
491 spin_lock(&cookie
->lock
);
493 if (fscache_cookie_enabled(cookie
)) {
494 /* update the index entry on disk in each cache backing this
497 hlist_for_each_entry(object
,
498 &cookie
->backing_objects
, cookie_link
) {
499 fscache_raise_event(object
, FSCACHE_OBJECT_EV_UPDATE
);
503 spin_unlock(&cookie
->lock
);
506 EXPORT_SYMBOL(__fscache_update_cookie
);
509 * Disable a cookie to stop it from accepting new requests from the netfs.
511 void __fscache_disable_cookie(struct fscache_cookie
*cookie
, bool invalidate
)
513 struct fscache_object
*object
;
516 _enter("%p,%u", cookie
, invalidate
);
518 ASSERTCMP(atomic_read(&cookie
->n_active
), >, 0);
520 if (atomic_read(&cookie
->n_children
) != 0) {
521 pr_err("Cookie '%s' still has children\n",
526 wait_on_bit_lock(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
,
527 TASK_UNINTERRUPTIBLE
);
528 if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED
, &cookie
->flags
))
529 goto out_unlock_enable
;
531 /* If the cookie is being invalidated, wait for that to complete first
532 * so that we can reuse the flag.
534 __fscache_wait_on_invalidate(cookie
);
536 /* Dispose of the backing objects */
537 set_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
);
539 spin_lock(&cookie
->lock
);
540 if (!hlist_empty(&cookie
->backing_objects
)) {
541 hlist_for_each_entry(object
, &cookie
->backing_objects
, cookie_link
) {
543 set_bit(FSCACHE_OBJECT_RETIRED
, &object
->flags
);
544 fscache_raise_event(object
, FSCACHE_OBJECT_EV_KILL
);
547 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
))
550 spin_unlock(&cookie
->lock
);
552 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
);
554 /* Wait for cessation of activity requiring access to the netfs (when
555 * n_active reaches 0). This makes sure outstanding reads and writes
558 if (!atomic_dec_and_test(&cookie
->n_active
))
559 wait_on_atomic_t(&cookie
->n_active
, fscache_wait_atomic_t
,
560 TASK_UNINTERRUPTIBLE
);
562 /* Reset the cookie state if it wasn't relinquished */
563 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED
, &cookie
->flags
)) {
564 atomic_inc(&cookie
->n_active
);
565 set_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
569 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK
, &cookie
->flags
);
570 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_ENABLEMENT_LOCK
);
573 EXPORT_SYMBOL(__fscache_disable_cookie
);
576 * release a cookie back to the cache
577 * - the object will be marked as recyclable on disk if retire is true
578 * - all dependents of this cookie must have already been unregistered
579 * (indices/files/pages)
581 void __fscache_relinquish_cookie(struct fscache_cookie
*cookie
, bool retire
)
583 fscache_stat(&fscache_n_relinquishes
);
585 fscache_stat(&fscache_n_relinquishes_retire
);
588 fscache_stat(&fscache_n_relinquishes_null
);
589 _leave(" [no cookie]");
593 _enter("%p{%s,%p,%d},%d",
594 cookie
, cookie
->def
->name
, cookie
->netfs_data
,
595 atomic_read(&cookie
->n_active
), retire
);
597 /* No further netfs-accessing operations on this cookie permitted */
598 set_bit(FSCACHE_COOKIE_RELINQUISHED
, &cookie
->flags
);
600 __fscache_disable_cookie(cookie
, retire
);
602 /* Clear pointers back to the netfs */
603 cookie
->netfs_data
= NULL
;
605 BUG_ON(cookie
->stores
.rnode
);
607 if (cookie
->parent
) {
608 ASSERTCMP(atomic_read(&cookie
->parent
->usage
), >, 0);
609 ASSERTCMP(atomic_read(&cookie
->parent
->n_children
), >, 0);
610 atomic_dec(&cookie
->parent
->n_children
);
613 /* Dispose of the netfs's link to the cookie */
614 ASSERTCMP(atomic_read(&cookie
->usage
), >, 0);
615 fscache_cookie_put(cookie
);
619 EXPORT_SYMBOL(__fscache_relinquish_cookie
);
624 void __fscache_cookie_put(struct fscache_cookie
*cookie
)
626 struct fscache_cookie
*parent
;
628 _enter("%p", cookie
);
631 _debug("FREE COOKIE %p", cookie
);
632 parent
= cookie
->parent
;
633 BUG_ON(!hlist_empty(&cookie
->backing_objects
));
634 kmem_cache_free(fscache_cookie_jar
, cookie
);
640 BUG_ON(atomic_read(&cookie
->usage
) <= 0);
641 if (!atomic_dec_and_test(&cookie
->usage
))
649 * check the consistency between the netfs inode and the backing cache
651 * NOTE: it only serves no-index type
653 int __fscache_check_consistency(struct fscache_cookie
*cookie
)
655 struct fscache_operation
*op
;
656 struct fscache_object
*object
;
657 bool wake_cookie
= false;
660 _enter("%p,", cookie
);
662 ASSERTCMP(cookie
->def
->type
, ==, FSCACHE_COOKIE_TYPE_DATAFILE
);
664 if (fscache_wait_for_deferred_lookup(cookie
) < 0)
667 if (hlist_empty(&cookie
->backing_objects
))
670 op
= kzalloc(sizeof(*op
), GFP_NOIO
| __GFP_NOMEMALLOC
| __GFP_NORETRY
);
674 fscache_operation_init(op
, NULL
, NULL
);
675 op
->flags
= FSCACHE_OP_MYTHREAD
|
676 (1 << FSCACHE_OP_WAITING
) |
677 (1 << FSCACHE_OP_UNUSE_COOKIE
);
679 spin_lock(&cookie
->lock
);
681 if (!fscache_cookie_enabled(cookie
) ||
682 hlist_empty(&cookie
->backing_objects
))
684 object
= hlist_entry(cookie
->backing_objects
.first
,
685 struct fscache_object
, cookie_link
);
686 if (test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
))
689 op
->debug_id
= atomic_inc_return(&fscache_op_debug_id
);
691 __fscache_use_cookie(cookie
);
692 if (fscache_submit_op(object
, op
) < 0)
695 /* the work queue now carries its own ref on the object */
696 spin_unlock(&cookie
->lock
);
698 ret
= fscache_wait_for_operation_activation(object
, op
,
701 /* ask the cache to honour the operation */
702 ret
= object
->cache
->ops
->check_consistency(op
);
703 fscache_op_complete(op
, false);
704 } else if (ret
== -ENOBUFS
) {
708 fscache_put_operation(op
);
709 _leave(" = %d", ret
);
713 wake_cookie
= __fscache_unuse_cookie(cookie
);
715 spin_unlock(&cookie
->lock
);
717 __fscache_wake_unused_cookie(cookie
);
719 _leave(" = -ESTALE");
722 EXPORT_SYMBOL(__fscache_check_consistency
);