Skip to content

Commit 3dc96bb

Browse files
jankaratytso
authored andcommitted
mbcache: add functions to delete entry if unused
Add function mb_cache_entry_delete_or_get() to delete mbcache entry if it is unused and also add a function to wait for entry to become unused - mb_cache_entry_wait_unused(). We do not share code between the two deleting function as one of them will go away soon. CC: [email protected] Fixes: 82939d7 ("ext4: convert to mbcache2") Signed-off-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 5831891 commit 3dc96bb

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

fs/mbcache.c

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/*
1212
* Mbcache is a simple key-value store. Keys need not be unique, however
1313
* key-value pairs are expected to be unique (we use this fact in
14-
* mb_cache_entry_delete()).
14+
* mb_cache_entry_delete_or_get()).
1515
*
1616
* Ext2 and ext4 use this cache for deduplication of extended attribute blocks.
1717
* Ext4 also uses it for deduplication of xattr values stored in inodes.
@@ -125,6 +125,19 @@ void __mb_cache_entry_free(struct mb_cache_entry *entry)
125125
}
126126
EXPORT_SYMBOL(__mb_cache_entry_free);
127127

128+
/*
129+
* mb_cache_entry_wait_unused - wait to be the last user of the entry
130+
*
131+
* @entry - entry to work on
132+
*
133+
* Wait to be the last user of the entry.
134+
*/
135+
void mb_cache_entry_wait_unused(struct mb_cache_entry *entry)
136+
{
137+
wait_var_event(&entry->e_refcnt, atomic_read(&entry->e_refcnt) <= 3);
138+
}
139+
EXPORT_SYMBOL(mb_cache_entry_wait_unused);
140+
128141
static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
129142
struct mb_cache_entry *entry,
130143
u32 key)
@@ -217,7 +230,7 @@ struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key,
217230
}
218231
EXPORT_SYMBOL(mb_cache_entry_get);
219232

220-
/* mb_cache_entry_delete - remove a cache entry
233+
/* mb_cache_entry_delete - try to remove a cache entry
221234
* @cache - cache we work with
222235
* @key - key
223236
* @value - value
@@ -254,6 +267,55 @@ void mb_cache_entry_delete(struct mb_cache *cache, u32 key, u64 value)
254267
}
255268
EXPORT_SYMBOL(mb_cache_entry_delete);
256269

270+
/* mb_cache_entry_delete_or_get - remove a cache entry if it has no users
271+
* @cache - cache we work with
272+
* @key - key
273+
* @value - value
274+
*
275+
* Remove entry from cache @cache with key @key and value @value. The removal
276+
* happens only if the entry is unused. The function returns NULL in case the
277+
* entry was successfully removed or there's no entry in cache. Otherwise the
278+
* function grabs reference of the entry that we failed to delete because it
279+
* still has users and return it.
280+
*/
281+
struct mb_cache_entry *mb_cache_entry_delete_or_get(struct mb_cache *cache,
282+
u32 key, u64 value)
283+
{
284+
struct hlist_bl_node *node;
285+
struct hlist_bl_head *head;
286+
struct mb_cache_entry *entry;
287+
288+
head = mb_cache_entry_head(cache, key);
289+
hlist_bl_lock(head);
290+
hlist_bl_for_each_entry(entry, node, head, e_hash_list) {
291+
if (entry->e_key == key && entry->e_value == value) {
292+
if (atomic_read(&entry->e_refcnt) > 2) {
293+
atomic_inc(&entry->e_refcnt);
294+
hlist_bl_unlock(head);
295+
return entry;
296+
}
297+
/* We keep hash list reference to keep entry alive */
298+
hlist_bl_del_init(&entry->e_hash_list);
299+
hlist_bl_unlock(head);
300+
spin_lock(&cache->c_list_lock);
301+
if (!list_empty(&entry->e_list)) {
302+
list_del_init(&entry->e_list);
303+
if (!WARN_ONCE(cache->c_entry_count == 0,
304+
"mbcache: attempt to decrement c_entry_count past zero"))
305+
cache->c_entry_count--;
306+
atomic_dec(&entry->e_refcnt);
307+
}
308+
spin_unlock(&cache->c_list_lock);
309+
mb_cache_entry_put(cache, entry);
310+
return NULL;
311+
}
312+
}
313+
hlist_bl_unlock(head);
314+
315+
return NULL;
316+
}
317+
EXPORT_SYMBOL(mb_cache_entry_delete_or_get);
318+
257319
/* mb_cache_entry_touch - cache entry got used
258320
* @cache - cache the entry belongs to
259321
* @entry - entry that got used

include/linux/mbcache.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,23 @@ void mb_cache_destroy(struct mb_cache *cache);
3030
int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
3131
u64 value, bool reusable);
3232
void __mb_cache_entry_free(struct mb_cache_entry *entry);
33+
void mb_cache_entry_wait_unused(struct mb_cache_entry *entry);
3334
static inline int mb_cache_entry_put(struct mb_cache *cache,
3435
struct mb_cache_entry *entry)
3536
{
36-
if (!atomic_dec_and_test(&entry->e_refcnt))
37+
unsigned int cnt = atomic_dec_return(&entry->e_refcnt);
38+
39+
if (cnt > 0) {
40+
if (cnt <= 3)
41+
wake_up_var(&entry->e_refcnt);
3742
return 0;
43+
}
3844
__mb_cache_entry_free(entry);
3945
return 1;
4046
}
4147

48+
struct mb_cache_entry *mb_cache_entry_delete_or_get(struct mb_cache *cache,
49+
u32 key, u64 value);
4250
void mb_cache_entry_delete(struct mb_cache *cache, u32 key, u64 value);
4351
struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key,
4452
u64 value);

0 commit comments

Comments
 (0)