Lines Matching refs:cookie

2 /* netfs cookie management
21 static void fscache_unhash_cookie(struct fscache_cookie *cookie);
22 static void fscache_perform_invalidation(struct fscache_cookie *cookie);
35 void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
39 pr_err("%c-cookie c=%08x [fl=%lx na=%u nA=%u s=%c]\n",
41 cookie->debug_id,
42 cookie->flags,
43 atomic_read(&cookie->n_active),
44 atomic_read(&cookie->n_accesses),
45 fscache_cookie_states[cookie->state]);
46 pr_err("%c-cookie V=%08x [%s]\n",
48 cookie->volume->debug_id,
49 cookie->volume->key);
51 k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
52 cookie->inline_key : cookie->key;
53 pr_err("%c-key=[%u] '%*phN'\n", prefix, cookie->key_len, cookie->key_len, k);
56 static void fscache_free_cookie(struct fscache_cookie *cookie)
58 if (WARN_ON_ONCE(!list_empty(&cookie->commit_link))) {
60 list_del_init(&cookie->commit_link);
66 if (WARN_ON_ONCE(test_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags))) {
67 fscache_print_cookie(cookie, 'F');
72 list_del(&cookie->proc_link);
74 if (cookie->aux_len > sizeof(cookie->inline_aux))
75 kfree(cookie->aux);
76 if (cookie->key_len > sizeof(cookie->inline_key))
77 kfree(cookie->key);
79 kmem_cache_free(fscache_cookie_jar, cookie);
82 static void __fscache_queue_cookie(struct fscache_cookie *cookie)
84 if (!queue_work(fscache_wq, &cookie->work))
85 fscache_put_cookie(cookie, fscache_cookie_put_over_queued);
88 static void fscache_queue_cookie(struct fscache_cookie *cookie,
91 fscache_get_cookie(cookie, where);
92 __fscache_queue_cookie(cookie);
96 * Initialise the access gate on a cookie by setting a flag to prevent the
99 * cookie.
101 static void fscache_init_access_gate(struct fscache_cookie *cookie)
105 n_accesses = atomic_read(&cookie->n_accesses);
106 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
108 set_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
113 * @cookie: A data file cookie
116 * Unpin a cache cookie after we've accessed it and bring a deferred
121 void fscache_end_cookie_access(struct fscache_cookie *cookie,
127 n_accesses = atomic_dec_return(&cookie->n_accesses);
128 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
131 !test_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags))
132 fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
137 * Pin the cache behind a cookie so that we can access it.
139 static void __fscache_begin_cookie_access(struct fscache_cookie *cookie,
144 n_accesses = atomic_inc_return(&cookie->n_accesses);
148 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
154 * @cookie: A data file cookie
160 * (1) If the cookie is not being cached (ie. FSCACHE_COOKIE_IS_CACHING is not
163 * (2) If the cookie is being cached, we increment its n_accesses count and
166 * (3) When we end the access, we decrement the cookie's n_accesses and wake
169 * (4) Whilst the cookie is actively being cached, its n_accesses is kept
172 * (5) When the cache is taken offline or if the cookie is culled, the flag is
173 * cleared to prevent new accesses, the cookie's n_accesses is decremented
178 bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
181 if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags))
183 __fscache_begin_cookie_access(cookie, why);
184 if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags) ||
185 !fscache_cache_is_live(cookie->volume->cache)) {
186 fscache_end_cookie_access(cookie, fscache_access_unlive);
192 static inline void wake_up_cookie_state(struct fscache_cookie *cookie)
200 wake_up_var(&cookie->state);
204 * Change the state a cookie is at and wake up anyone waiting for that. Impose
205 * an ordering between the stuff stored in the cookie and the state member.
208 static void __fscache_set_cookie_state(struct fscache_cookie *cookie,
211 smp_store_release(&cookie->state, state);
214 static void fscache_set_cookie_state(struct fscache_cookie *cookie,
217 spin_lock(&cookie->lock);
218 __fscache_set_cookie_state(cookie, state);
219 spin_unlock(&cookie->lock);
220 wake_up_cookie_state(cookie);
225 * @cookie: The cookie that was being looked up
233 void fscache_cookie_lookup_negative(struct fscache_cookie *cookie)
235 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
236 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_CREATING);
242 * @cookie: The cookie that was invalidated
247 void fscache_resume_after_invalidation(struct fscache_cookie *cookie)
249 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE);
254 * fscache_caching_failed - Report that a failure stopped caching on a cookie
255 * @cookie: The cookie that was affected
257 * Tell fscache that caching on a cookie needs to be stopped due to some sort
262 void fscache_caching_failed(struct fscache_cookie *cookie)
264 clear_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
265 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_FAILED);
266 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
272 * Set the index key in a cookie. The cookie struct has space for a 16-byte
277 static int fscache_set_key(struct fscache_cookie *cookie,
285 if (index_key_len > sizeof(cookie->inline_key)) {
289 cookie->key = buf;
291 buf = cookie->inline_key;
295 cookie->key_hash = fscache_hash(cookie->volume->key_hash,
323 * Allocate a cookie.
332 struct fscache_cookie *cookie;
334 /* allocate and initialise a cookie */
335 cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
336 if (!cookie)
340 cookie->volume = volume;
341 cookie->advice = advice;
342 cookie->key_len = index_key_len;
343 cookie->aux_len = aux_data_len;
344 cookie->object_size = object_size;
346 __set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
348 if (fscache_set_key(cookie, index_key, index_key_len) < 0)
351 if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
352 memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
354 cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
355 if (!cookie->aux)
359 refcount_set(&cookie->ref, 1);
360 cookie->debug_id = atomic_inc_return(&fscache_cookie_debug_id);
361 spin_lock_init(&cookie->lock);
362 INIT_LIST_HEAD(&cookie->commit_link);
363 INIT_WORK(&cookie->work, fscache_cookie_worker);
364 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
367 list_add_tail(&cookie->proc_link, &fscache_cookies);
369 fscache_see_cookie(cookie, fscache_cookie_new_acquire);
370 return cookie;
373 fscache_free_cookie(cookie);
377 static inline bool fscache_cookie_is_dropped(struct fscache_cookie *cookie)
379 return READ_ONCE(cookie->state) == FSCACHE_COOKIE_STATE_DROPPED;
397 * Attempt to insert the new cookie into the hash. If there's a collision, we
398 * wait for the old cookie to complete if it's being relinquished and an error
437 pr_err("Duplicate cookie detected\n");
445 * Request a cookie to represent a data storage object within a volume.
447 * We never let on to the netfs about errors. We may set a negative cookie
457 struct fscache_cookie *cookie;
470 cookie = fscache_alloc_cookie(volume, advice,
474 if (!cookie) {
479 if (!fscache_hash_cookie(cookie)) {
480 fscache_see_cookie(cookie, fscache_cookie_discard);
481 fscache_free_cookie(cookie);
485 trace_fscache_acquire(cookie);
487 _leave(" = c=%08x", cookie->debug_id);
488 return cookie;
495 static void fscache_prepare_to_write(struct fscache_cookie *cookie)
497 cookie->volume->cache->ops->prepare_to_write(cookie);
501 * Look up a cookie in the cache.
503 static void fscache_perform_lookup(struct fscache_cookie *cookie)
510 if (!cookie->volume->cache_priv) {
511 fscache_create_volume(cookie->volume, true);
512 if (!cookie->volume->cache_priv) {
513 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
518 if (!cookie->volume->cache->ops->lookup_cookie(cookie)) {
519 if (cookie->state != FSCACHE_COOKIE_STATE_FAILED)
520 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
526 fscache_see_cookie(cookie, fscache_cookie_see_active);
527 spin_lock(&cookie->lock);
528 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
529 __fscache_set_cookie_state(cookie,
532 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE);
533 spin_unlock(&cookie->lock);
534 wake_up_cookie_state(cookie);
538 fscache_end_cookie_access(cookie, trace);
540 fscache_withdraw_cookie(cookie);
541 fscache_end_volume_access(cookie->volume, cookie, trace);
545 * Begin the process of looking up a cookie. We offload the actual process to
548 static bool fscache_begin_lookup(struct fscache_cookie *cookie, bool will_modify)
551 set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags);
552 set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
554 if (!fscache_begin_volume_access(cookie->volume, cookie,
558 __fscache_begin_cookie_access(cookie, fscache_access_lookup_cookie);
559 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_LOOKING_UP);
560 set_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
561 set_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags);
566 * Start using the cookie for I/O. This prevents the backing object from being
569 void __fscache_use_cookie(struct fscache_cookie *cookie, bool will_modify)
575 _enter("c=%08x", cookie->debug_id);
577 if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
578 "Trying to use relinquished cookie\n"))
581 spin_lock(&cookie->lock);
583 n_active = atomic_inc_return(&cookie->n_active);
584 trace_fscache_active(cookie->debug_id, refcount_read(&cookie->ref),
585 n_active, atomic_read(&cookie->n_accesses),
590 state = fscache_cookie_state(cookie);
593 queue = fscache_begin_lookup(cookie, will_modify);
599 set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags);
604 !test_and_set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags)) {
605 set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
610 * but has yet to run the cookie state machine. If this happens
611 * and another thread tries to use the cookie, clear LRU_DISCARD
612 * so we don't end up withdrawing the cookie while in use.
614 if (test_and_clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags))
615 fscache_see_cookie(cookie, fscache_cookie_see_lru_discard_clear);
623 spin_unlock(&cookie->lock);
624 wait_var_event(&cookie->state,
625 fscache_cookie_state(cookie) !=
627 spin_lock(&cookie->lock);
632 WARN(1, "Can't use cookie in state %u\n", state);
636 spin_unlock(&cookie->lock);
638 fscache_queue_cookie(cookie, fscache_cookie_get_use_work);
643 static void fscache_unuse_cookie_locked(struct fscache_cookie *cookie)
645 clear_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags);
646 if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags))
649 cookie->unused_at = jiffies;
651 if (list_empty(&cookie->commit_link)) {
652 fscache_get_cookie(cookie, fscache_cookie_get_lru);
655 list_move_tail(&cookie->commit_link, &fscache_cookie_lru);
663 * Stop using the cookie for I/O.
665 void __fscache_unuse_cookie(struct fscache_cookie *cookie,
668 unsigned int debug_id = cookie->debug_id;
669 unsigned int r = refcount_read(&cookie->ref);
670 unsigned int a = atomic_read(&cookie->n_accesses);
674 __fscache_update_cookie(cookie, aux_data, object_size);
677 c = atomic_fetch_add_unless(&cookie->n_active, -1, 1);
683 spin_lock(&cookie->lock);
684 r = refcount_read(&cookie->ref);
685 a = atomic_read(&cookie->n_accesses);
686 c = atomic_dec_return(&cookie->n_active);
689 fscache_unuse_cookie_locked(cookie);
690 spin_unlock(&cookie->lock);
695 * Perform work upon the cookie, such as committing its cache state,
700 static void fscache_cookie_state_machine(struct fscache_cookie *cookie)
705 _enter("c=%x", cookie->debug_id);
708 spin_lock(&cookie->lock);
710 state = cookie->state;
717 if (atomic_read(&cookie->n_accesses) == 0 &&
718 test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
719 __fscache_set_cookie_state(cookie,
727 spin_unlock(&cookie->lock);
728 fscache_init_access_gate(cookie);
729 fscache_perform_lookup(cookie);
733 spin_unlock(&cookie->lock);
734 fscache_perform_invalidation(cookie);
738 if (test_and_clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags)) {
739 spin_unlock(&cookie->lock);
740 fscache_prepare_to_write(cookie);
741 spin_lock(&cookie->lock);
743 if (test_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags)) {
744 __fscache_set_cookie_state(cookie,
752 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
753 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
755 if (atomic_read(&cookie->n_accesses) != 0)
757 if (test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
758 __fscache_set_cookie_state(cookie,
763 if (test_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags)) {
764 __fscache_set_cookie_state(cookie,
774 if (cookie->cache_priv) {
775 spin_unlock(&cookie->lock);
776 cookie->volume->cache->ops->withdraw_cookie(cookie);
777 spin_lock(&cookie->lock);
780 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
781 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
785 fscache_see_cookie(cookie, fscache_cookie_see_relinquish);
786 fscache_unhash_cookie(cookie);
787 __fscache_set_cookie_state(cookie,
792 fscache_see_cookie(cookie, fscache_cookie_see_lru_discard);
795 fscache_see_cookie(cookie, fscache_cookie_see_withdraw);
801 clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
802 clear_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
803 clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
804 clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
805 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
806 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
815 cookie->debug_id, state);
820 spin_unlock(&cookie->lock);
822 wake_up_cookie_state(cookie);
828 struct fscache_cookie *cookie = container_of(work, struct fscache_cookie, work);
830 fscache_see_cookie(cookie, fscache_cookie_see_work);
831 fscache_cookie_state_machine(cookie);
832 fscache_put_cookie(cookie, fscache_cookie_put_work);
836 * Wait for the object to become inactive. The cookie's work item will be
840 static void __fscache_withdraw_cookie(struct fscache_cookie *cookie)
845 unpinned = test_and_clear_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
848 n_accesses = atomic_read(&cookie->n_accesses);
850 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
853 fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
856 static void fscache_cookie_lru_do_one(struct fscache_cookie *cookie)
858 fscache_see_cookie(cookie, fscache_cookie_see_lru_do_one);
860 spin_lock(&cookie->lock);
861 if (cookie->state != FSCACHE_COOKIE_STATE_ACTIVE ||
862 time_before(jiffies, cookie->unused_at + fscache_lru_cookie_timeout) ||
863 atomic_read(&cookie->n_active) > 0) {
864 spin_unlock(&cookie->lock);
867 set_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
868 spin_unlock(&cookie->lock);
870 _debug("lru c=%x", cookie->debug_id);
871 __fscache_withdraw_cookie(cookie);
874 fscache_put_cookie(cookie, fscache_cookie_put_lru);
879 struct fscache_cookie *cookie;
885 cookie = list_first_entry(&fscache_cookie_lru,
887 unused_at = cookie->unused_at + fscache_lru_cookie_timeout;
893 list_del_init(&cookie->commit_link);
896 fscache_cookie_lru_do_one(cookie);
908 static void fscache_cookie_drop_from_lru(struct fscache_cookie *cookie)
912 if (!list_empty(&cookie->commit_link)) {
914 if (!list_empty(&cookie->commit_link)) {
915 list_del_init(&cookie->commit_link);
922 fscache_put_cookie(cookie, fscache_cookie_put_lru);
927 * Remove a cookie from the hash table.
929 static void fscache_unhash_cookie(struct fscache_cookie *cookie)
934 bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
938 hlist_bl_del(&cookie->hash_link);
939 clear_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags);
944 static void fscache_drop_withdraw_cookie(struct fscache_cookie *cookie)
946 fscache_cookie_drop_from_lru(cookie);
947 __fscache_withdraw_cookie(cookie);
951 * fscache_withdraw_cookie - Mark a cookie for withdrawal
952 * @cookie: The cookie to be withdrawn.
954 * Allow the cache backend to withdraw the backing for a cookie for its own
955 * reasons, even if that cookie is in active use.
957 void fscache_withdraw_cookie(struct fscache_cookie *cookie)
959 set_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
960 fscache_drop_withdraw_cookie(cookie);
965 * Allow the netfs to release a cookie back to the cache.
968 void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
975 cookie->debug_id, atomic_read(&cookie->n_active), retire);
977 if (WARN(test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
978 "Cookie c=%x already relinquished\n", cookie->debug_id))
982 set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
983 trace_fscache_relinquish(cookie, retire);
985 ASSERTCMP(atomic_read(&cookie->n_active), ==, 0);
986 ASSERTCMP(atomic_read(&cookie->volume->n_cookies), >, 0);
987 atomic_dec(&cookie->volume->n_cookies);
989 if (test_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags)) {
990 set_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags);
991 fscache_drop_withdraw_cookie(cookie);
993 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_DROPPED);
994 fscache_unhash_cookie(cookie);
996 fscache_put_cookie(cookie, fscache_cookie_put_relinquish);
1001 * Drop a reference to a cookie.
1003 void fscache_put_cookie(struct fscache_cookie *cookie,
1006 struct fscache_volume *volume = cookie->volume;
1007 unsigned int cookie_debug_id = cookie->debug_id;
1011 zero = __refcount_dec_and_test(&cookie->ref, &ref);
1014 fscache_free_cookie(cookie);
1021 * Get a reference to a cookie.
1023 struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
1028 __refcount_inc(&cookie->ref, &ref);
1029 trace_fscache_cookie(cookie->debug_id, ref + 1, where);
1030 return cookie;
1035 * Ask the cache to effect invalidation of a cookie.
1037 static void fscache_perform_invalidation(struct fscache_cookie *cookie)
1039 if (!cookie->volume->cache->ops->invalidate_cookie(cookie))
1040 fscache_caching_failed(cookie);
1041 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
1047 void __fscache_invalidate(struct fscache_cookie *cookie,
1053 _enter("c=%x", cookie->debug_id);
1057 if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
1058 "Trying to invalidate relinquished cookie\n"))
1062 test_and_set_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
1065 spin_lock(&cookie->lock);
1066 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
1067 fscache_update_aux(cookie, aux_data, &new_size);
1068 cookie->inval_counter++;
1069 trace_fscache_invalidate(cookie, new_size);
1071 switch (cookie->state) {
1074 spin_unlock(&cookie->lock);
1075 _leave(" [no %u]", cookie->state);
1079 if (!test_and_set_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
1080 __fscache_begin_cookie_access(cookie, fscache_access_invalidate_cookie);
1083 spin_unlock(&cookie->lock);
1084 _leave(" [look %x]", cookie->inval_counter);
1089 cookie, fscache_access_invalidate_cookie);
1091 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_INVALIDATING);
1092 spin_unlock(&cookie->lock);
1093 wake_up_cookie_state(cookie);
1096 fscache_queue_cookie(cookie, fscache_cookie_get_inval_work);
1109 struct fscache_cookie *cookie;
1121 cookie = list_entry(v, struct fscache_cookie, proc_link);
1125 cookie->debug_id,
1126 cookie->volume->debug_id,
1127 refcount_read(&cookie->ref),
1128 atomic_read(&cookie->n_active),
1129 atomic_read(&cookie->n_accesses),
1130 fscache_cookie_states[cookie->state],
1131 cookie->flags);
1133 keylen = cookie->key_len;
1134 auxlen = cookie->aux_len;
1138 p = keylen <= sizeof(cookie->inline_key) ?
1139 cookie->inline_key : cookie->key;
1144 p = auxlen <= sizeof(cookie->inline_aux) ?
1145 cookie->inline_aux : cookie->aux;