Merge tag 'phy-for-6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/phy/linux-phy
[linux-2.6-block.git] / include / linux / mbcache.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
7a2508e1
JK
2#ifndef _LINUX_MBCACHE_H
3#define _LINUX_MBCACHE_H
4
5#include <linux/hash.h>
6#include <linux/list_bl.h>
7#include <linux/list.h>
8#include <linux/atomic.h>
9#include <linux/fs.h>
10
11struct mb_cache;
12
a44e84a9
JK
13/* Cache entry flags */
14enum {
15 MBE_REFERENCED_B = 0,
16 MBE_REUSABLE_B
17};
18
7a2508e1
JK
19struct mb_cache_entry {
20 /* List of entries in cache - protected by cache->c_list_lock */
21 struct list_head e_list;
307af6c8
JK
22 /*
23 * Hash table list - protected by hash chain bitlock. The entry is
24 * guaranteed to be hashed while e_refcnt > 0.
25 */
7a2508e1 26 struct hlist_bl_node e_hash_list;
307af6c8
JK
27 /*
28 * Entry refcount. Once it reaches zero, entry is unhashed and freed.
29 * While refcount > 0, the entry is guaranteed to stay in the hash and
30 * e.g. mb_cache_entry_try_delete() will fail.
31 */
7a2508e1
JK
32 atomic_t e_refcnt;
33 /* Key in hash - stable during lifetime of the entry */
34 u32 e_key;
a44e84a9 35 unsigned long e_flags;
c07dfcb4
TE
36 /* User provided value - stable during lifetime of the entry */
37 u64 e_value;
7a2508e1
JK
38};
39
40struct mb_cache *mb_cache_create(int bucket_bits);
41void mb_cache_destroy(struct mb_cache *cache);
42
43int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
c07dfcb4 44 u64 value, bool reusable);
307af6c8
JK
45void __mb_cache_entry_free(struct mb_cache *cache,
46 struct mb_cache_entry *entry);
3dc96bba 47void mb_cache_entry_wait_unused(struct mb_cache_entry *entry);
307af6c8
JK
48static inline void mb_cache_entry_put(struct mb_cache *cache,
49 struct mb_cache_entry *entry)
7a2508e1 50{
3dc96bba
JK
51 unsigned int cnt = atomic_dec_return(&entry->e_refcnt);
52
53 if (cnt > 0) {
307af6c8 54 if (cnt <= 2)
3dc96bba 55 wake_up_var(&entry->e_refcnt);
307af6c8 56 return;
3dc96bba 57 }
307af6c8 58 __mb_cache_entry_free(cache, entry);
7a2508e1
JK
59}
60
3dc96bba
JK
61struct mb_cache_entry *mb_cache_entry_delete_or_get(struct mb_cache *cache,
62 u32 key, u64 value);
6048c64b 63struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key,
c07dfcb4 64 u64 value);
7a2508e1
JK
65struct mb_cache_entry *mb_cache_entry_find_first(struct mb_cache *cache,
66 u32 key);
67struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache *cache,
68 struct mb_cache_entry *entry);
69void mb_cache_entry_touch(struct mb_cache *cache,
70 struct mb_cache_entry *entry);
71
72#endif /* _LINUX_MBCACHE_H */