1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Volume-level cache cookie handling.
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
8 #define FSCACHE_DEBUG_LEVEL COOKIE
9 #include <linux/export.h>
10 #include <linux/slab.h>
13 #define fscache_volume_hash_shift 10
14 static struct hlist_bl_head fscache_volume_hash[1 << fscache_volume_hash_shift];
15 static atomic_t fscache_volume_debug_id;
16 static LIST_HEAD(fscache_volumes);
18 static void fscache_create_volume_work(struct work_struct *work);
20 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
21 enum fscache_volume_trace where)
25 __refcount_inc(&volume->ref, &ref);
26 trace_fscache_volume(volume->debug_id, ref + 1, where);
30 struct fscache_volume *fscache_try_get_volume(struct fscache_volume *volume,
31 enum fscache_volume_trace where)
35 if (!__refcount_inc_not_zero(&volume->ref, &ref))
38 trace_fscache_volume(volume->debug_id, ref + 1, where);
41 EXPORT_SYMBOL(fscache_try_get_volume);
43 static void fscache_see_volume(struct fscache_volume *volume,
44 enum fscache_volume_trace where)
46 int ref = refcount_read(&volume->ref);
48 trace_fscache_volume(volume->debug_id, ref, where);
52 * Pin the cache behind a volume so that we can access it.
54 static void __fscache_begin_volume_access(struct fscache_volume *volume,
55 struct fscache_cookie *cookie,
56 enum fscache_access_trace why)
60 n_accesses = atomic_inc_return(&volume->n_accesses);
61 smp_mb__after_atomic();
62 trace_fscache_access_volume(volume->debug_id, cookie ? cookie->debug_id : 0,
63 refcount_read(&volume->ref),
68 * fscache_begin_volume_access - Pin a cache so a volume can be accessed
69 * @volume: The volume cookie
70 * @cookie: A datafile cookie for a tracing reference (or NULL)
71 * @why: An indication of the circumstances of the access for tracing
73 * Attempt to pin the cache to prevent it from going away whilst we're
74 * accessing a volume and returns true if successful. This works as follows:
76 * (1) If the cache tests as not live (state is not FSCACHE_CACHE_IS_ACTIVE),
77 * then we return false to indicate access was not permitted.
79 * (2) If the cache tests as live, then we increment the volume's n_accesses
80 * count and then recheck the cache liveness, ending the access if it
83 * (3) When we end the access, we decrement the volume's n_accesses and wake
84 * up the any waiters if it reaches 0.
86 * (4) Whilst the cache is caching, the volume's n_accesses is kept
87 * artificially incremented to prevent wakeups from happening.
89 * (5) When the cache is taken offline, the state is changed to prevent new
90 * accesses, the volume's n_accesses is decremented and we wait for it to
93 * The datafile @cookie and the @why indicator are merely provided for tracing
96 bool fscache_begin_volume_access(struct fscache_volume *volume,
97 struct fscache_cookie *cookie,
98 enum fscache_access_trace why)
100 if (!fscache_cache_is_live(volume->cache))
102 __fscache_begin_volume_access(volume, cookie, why);
103 if (!fscache_cache_is_live(volume->cache)) {
104 fscache_end_volume_access(volume, cookie, fscache_access_unlive);
111 * fscache_end_volume_access - Unpin a cache at the end of an access.
112 * @volume: The volume cookie
113 * @cookie: A datafile cookie for a tracing reference (or NULL)
114 * @why: An indication of the circumstances of the access for tracing
116 * Unpin a cache volume after we've accessed it. The datafile @cookie and the
117 * @why indicator are merely provided for tracing purposes.
119 void fscache_end_volume_access(struct fscache_volume *volume,
120 struct fscache_cookie *cookie,
121 enum fscache_access_trace why)
125 smp_mb__before_atomic();
126 n_accesses = atomic_dec_return(&volume->n_accesses);
127 trace_fscache_access_volume(volume->debug_id, cookie ? cookie->debug_id : 0,
128 refcount_read(&volume->ref),
131 wake_up_var(&volume->n_accesses);
133 EXPORT_SYMBOL(fscache_end_volume_access);
135 static bool fscache_volume_same(const struct fscache_volume *a,
136 const struct fscache_volume *b)
140 if (a->key_hash != b->key_hash ||
141 a->cache != b->cache ||
142 a->key[0] != b->key[0])
145 klen = round_up(a->key[0] + 1, sizeof(__le32));
146 return memcmp(a->key, b->key, klen) == 0;
149 static bool fscache_is_acquire_pending(struct fscache_volume *volume)
151 return test_bit(FSCACHE_VOLUME_ACQUIRE_PENDING, &volume->flags);
154 static void fscache_wait_on_volume_collision(struct fscache_volume *candidate,
155 unsigned int collidee_debug_id)
157 wait_on_bit_timeout(&candidate->flags, FSCACHE_VOLUME_ACQUIRE_PENDING,
158 TASK_UNINTERRUPTIBLE, 20 * HZ);
159 if (fscache_is_acquire_pending(candidate)) {
160 pr_notice("Potential volume collision new=%08x old=%08x",
161 candidate->debug_id, collidee_debug_id);
162 fscache_stat(&fscache_n_volumes_collision);
163 wait_on_bit(&candidate->flags, FSCACHE_VOLUME_ACQUIRE_PENDING,
164 TASK_UNINTERRUPTIBLE);
169 * Attempt to insert the new volume into the hash. If there's a collision, we
170 * wait for the old volume to complete if it's being relinquished and an error
173 static bool fscache_hash_volume(struct fscache_volume *candidate)
175 struct fscache_volume *cursor;
176 struct hlist_bl_head *h;
177 struct hlist_bl_node *p;
178 unsigned int bucket, collidee_debug_id = 0;
180 bucket = candidate->key_hash & (ARRAY_SIZE(fscache_volume_hash) - 1);
181 h = &fscache_volume_hash[bucket];
184 hlist_bl_for_each_entry(cursor, p, h, hash_link) {
185 if (fscache_volume_same(candidate, cursor)) {
186 if (!test_bit(FSCACHE_VOLUME_RELINQUISHED, &cursor->flags))
188 fscache_see_volume(cursor, fscache_volume_get_hash_collision);
189 set_bit(FSCACHE_VOLUME_COLLIDED_WITH, &cursor->flags);
190 set_bit(FSCACHE_VOLUME_ACQUIRE_PENDING, &candidate->flags);
191 collidee_debug_id = cursor->debug_id;
196 hlist_bl_add_head(&candidate->hash_link, h);
199 if (fscache_is_acquire_pending(candidate))
200 fscache_wait_on_volume_collision(candidate, collidee_debug_id);
204 fscache_see_volume(cursor, fscache_volume_collision);
210 * Allocate and initialise a volume representation cookie.
212 static struct fscache_volume *fscache_alloc_volume(const char *volume_key,
213 const char *cache_name,
214 const void *coherency_data,
215 size_t coherency_len)
217 struct fscache_volume *volume;
218 struct fscache_cache *cache;
222 klen = strlen(volume_key);
229 cache = fscache_lookup_cache(cache_name, false);
233 volume = kzalloc(struct_size(volume, coherency, coherency_len),
238 volume->cache = cache;
239 volume->coherency_len = coherency_len;
241 memcpy(volume->coherency, coherency_data, coherency_len);
242 INIT_LIST_HEAD(&volume->proc_link);
243 INIT_WORK(&volume->work, fscache_create_volume_work);
244 refcount_set(&volume->ref, 1);
245 spin_lock_init(&volume->lock);
247 /* Stick the length on the front of the key and pad it out to make
250 hlen = round_up(1 + klen + 1, sizeof(__le32));
251 key = kzalloc(hlen, GFP_KERNEL);
255 memcpy(key + 1, volume_key, klen);
258 volume->key_hash = fscache_hash(0, key, hlen);
260 volume->debug_id = atomic_inc_return(&fscache_volume_debug_id);
261 down_write(&fscache_addremove_sem);
262 atomic_inc(&cache->n_volumes);
263 list_add_tail(&volume->proc_link, &fscache_volumes);
264 fscache_see_volume(volume, fscache_volume_new_acquire);
265 fscache_stat(&fscache_n_volumes);
266 up_write(&fscache_addremove_sem);
267 _leave(" = v=%x", volume->debug_id);
273 fscache_put_cache(cache, fscache_cache_put_alloc_volume);
274 fscache_stat(&fscache_n_volumes_nomem);
279 * Create a volume's representation on disk. Have a volume ref and a cache
280 * access we have to release.
282 static void fscache_create_volume_work(struct work_struct *work)
284 const struct fscache_cache_ops *ops;
285 struct fscache_volume *volume =
286 container_of(work, struct fscache_volume, work);
288 fscache_see_volume(volume, fscache_volume_see_create_work);
290 ops = volume->cache->ops;
291 if (ops->acquire_volume)
292 ops->acquire_volume(volume);
293 fscache_end_cache_access(volume->cache,
294 fscache_access_acquire_volume_end);
296 clear_and_wake_up_bit(FSCACHE_VOLUME_CREATING, &volume->flags);
297 fscache_put_volume(volume, fscache_volume_put_create_work);
301 * Dispatch a worker thread to create a volume's representation on disk.
303 void fscache_create_volume(struct fscache_volume *volume, bool wait)
305 if (test_and_set_bit(FSCACHE_VOLUME_CREATING, &volume->flags))
307 if (volume->cache_priv)
308 goto no_wait; /* We raced */
309 if (!fscache_begin_cache_access(volume->cache,
310 fscache_access_acquire_volume))
313 fscache_get_volume(volume, fscache_volume_get_create_work);
314 if (!schedule_work(&volume->work))
315 fscache_put_volume(volume, fscache_volume_put_create_work);
319 fscache_see_volume(volume, fscache_volume_wait_create_work);
320 wait_on_bit(&volume->flags, FSCACHE_VOLUME_CREATING,
321 TASK_UNINTERRUPTIBLE);
325 clear_bit_unlock(FSCACHE_VOLUME_CREATING, &volume->flags);
326 wake_up_bit(&volume->flags, FSCACHE_VOLUME_CREATING);
330 * Acquire a volume representation cookie and link it to a (proposed) cache.
332 struct fscache_volume *__fscache_acquire_volume(const char *volume_key,
333 const char *cache_name,
334 const void *coherency_data,
335 size_t coherency_len)
337 struct fscache_volume *volume;
339 volume = fscache_alloc_volume(volume_key, cache_name,
340 coherency_data, coherency_len);
342 return ERR_PTR(-ENOMEM);
344 if (!fscache_hash_volume(volume)) {
345 fscache_put_volume(volume, fscache_volume_put_hash_collision);
346 return ERR_PTR(-EBUSY);
349 fscache_create_volume(volume, false);
352 EXPORT_SYMBOL(__fscache_acquire_volume);
354 static void fscache_wake_pending_volume(struct fscache_volume *volume,
355 struct hlist_bl_head *h)
357 struct fscache_volume *cursor;
358 struct hlist_bl_node *p;
360 hlist_bl_for_each_entry(cursor, p, h, hash_link) {
361 if (fscache_volume_same(cursor, volume)) {
362 fscache_see_volume(cursor, fscache_volume_see_hash_wake);
363 clear_and_wake_up_bit(FSCACHE_VOLUME_ACQUIRE_PENDING,
371 * Remove a volume cookie from the hash table.
373 static void fscache_unhash_volume(struct fscache_volume *volume)
375 struct hlist_bl_head *h;
378 bucket = volume->key_hash & (ARRAY_SIZE(fscache_volume_hash) - 1);
379 h = &fscache_volume_hash[bucket];
382 hlist_bl_del(&volume->hash_link);
383 if (test_bit(FSCACHE_VOLUME_COLLIDED_WITH, &volume->flags))
384 fscache_wake_pending_volume(volume, h);
389 * Drop a cache's volume attachments.
391 static void fscache_free_volume(struct fscache_volume *volume)
393 struct fscache_cache *cache = volume->cache;
395 if (volume->cache_priv) {
396 __fscache_begin_volume_access(volume, NULL,
397 fscache_access_relinquish_volume);
398 if (volume->cache_priv)
399 cache->ops->free_volume(volume);
400 fscache_end_volume_access(volume, NULL,
401 fscache_access_relinquish_volume_end);
404 down_write(&fscache_addremove_sem);
405 list_del_init(&volume->proc_link);
406 atomic_dec(&volume->cache->n_volumes);
407 up_write(&fscache_addremove_sem);
409 if (!hlist_bl_unhashed(&volume->hash_link))
410 fscache_unhash_volume(volume);
412 trace_fscache_volume(volume->debug_id, 0, fscache_volume_free);
415 fscache_stat_d(&fscache_n_volumes);
416 fscache_put_cache(cache, fscache_cache_put_volume);
420 * Drop a reference to a volume cookie.
422 void fscache_put_volume(struct fscache_volume *volume,
423 enum fscache_volume_trace where)
426 unsigned int debug_id = volume->debug_id;
430 zero = __refcount_dec_and_test(&volume->ref, &ref);
431 trace_fscache_volume(debug_id, ref - 1, where);
433 fscache_free_volume(volume);
436 EXPORT_SYMBOL(fscache_put_volume);
439 * Relinquish a volume representation cookie.
441 void __fscache_relinquish_volume(struct fscache_volume *volume,
442 const void *coherency_data,
445 if (WARN_ON(test_and_set_bit(FSCACHE_VOLUME_RELINQUISHED, &volume->flags)))
449 set_bit(FSCACHE_VOLUME_INVALIDATE, &volume->flags);
450 } else if (coherency_data) {
451 memcpy(volume->coherency, coherency_data, volume->coherency_len);
454 fscache_put_volume(volume, fscache_volume_put_relinquish);
456 EXPORT_SYMBOL(__fscache_relinquish_volume);
459 * fscache_withdraw_volume - Withdraw a volume from being cached
460 * @volume: Volume cookie
462 * Withdraw a cache volume from service, waiting for all accesses to complete
465 void fscache_withdraw_volume(struct fscache_volume *volume)
469 _debug("withdraw V=%x", volume->debug_id);
471 /* Allow wakeups on dec-to-0 */
472 n_accesses = atomic_dec_return(&volume->n_accesses);
473 trace_fscache_access_volume(volume->debug_id, 0,
474 refcount_read(&volume->ref),
475 n_accesses, fscache_access_cache_unpin);
477 wait_var_event(&volume->n_accesses,
478 atomic_read(&volume->n_accesses) == 0);
480 EXPORT_SYMBOL(fscache_withdraw_volume);
482 #ifdef CONFIG_PROC_FS
484 * Generate a list of volumes in /proc/fs/fscache/volumes
486 static int fscache_volumes_seq_show(struct seq_file *m, void *v)
488 struct fscache_volume *volume;
490 if (v == &fscache_volumes) {
492 "VOLUME REF nCOOK ACC FL CACHE KEY\n"
493 "======== ===== ===== === == =============== ================\n");
497 volume = list_entry(v, struct fscache_volume, proc_link);
499 "%08x %5d %5d %3d %02lx %-15.15s %s\n",
501 refcount_read(&volume->ref),
502 atomic_read(&volume->n_cookies),
503 atomic_read(&volume->n_accesses),
505 volume->cache->name ?: "-",
510 static void *fscache_volumes_seq_start(struct seq_file *m, loff_t *_pos)
511 __acquires(&fscache_addremove_sem)
513 down_read(&fscache_addremove_sem);
514 return seq_list_start_head(&fscache_volumes, *_pos);
517 static void *fscache_volumes_seq_next(struct seq_file *m, void *v, loff_t *_pos)
519 return seq_list_next(v, &fscache_volumes, _pos);
522 static void fscache_volumes_seq_stop(struct seq_file *m, void *v)
523 __releases(&fscache_addremove_sem)
525 up_read(&fscache_addremove_sem);
528 const struct seq_operations fscache_volumes_seq_ops = {
529 .start = fscache_volumes_seq_start,
530 .next = fscache_volumes_seq_next,
531 .stop = fscache_volumes_seq_stop,
532 .show = fscache_volumes_seq_show,
534 #endif /* CONFIG_PROC_FS */