1 #include <linux/spinlock.h>
2 #include <linux/slab.h>
3 #include <linux/list.h>
4 #include <linux/list_bl.h>
5 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <linux/mbcache2.h>
10 * Mbcache is a simple key-value store. Keys need not be unique, however
11 * key-value pairs are expected to be unique (we use this fact in
12 * mb2_cache_entry_delete_block()).
14 * Ext2 and ext4 use this cache for deduplication of extended attribute blocks.
15 * They use hash of a block contents as a key and block number as a value.
16 * That's why keys need not be unique (different xattr blocks may end up having
17 * the same hash). However block number always uniquely identifies a cache
20 * We provide functions for creation and removal of entries, search by key,
21 * and a special "delete entry with given key-value pair" operation. Fixed
22 * size hash table is used for fast key lookups.
26 /* Hash table of entries */
27 struct hlist_bl_head
*c_hash
;
28 /* log2 of hash table size */
30 /* Protects c_lru_list, c_entry_count */
31 spinlock_t c_lru_list_lock
;
32 struct list_head c_lru_list
;
33 /* Number of entries in cache */
34 unsigned long c_entry_count
;
35 struct shrinker c_shrink
;
38 static struct kmem_cache
*mb2_entry_cache
;
41 * mb2_cache_entry_create - create entry in cache
42 * @cache - cache where the entry should be created
43 * @mask - gfp mask with which the entry should be allocated
44 * @key - key of the entry
45 * @block - block that contains data
47 * Creates entry in @cache with key @key and records that data is stored in
48 * block @block. The function returns -EBUSY if entry with the same key
49 * and for the same block already exists in cache. Otherwise 0 is returned.
51 int mb2_cache_entry_create(struct mb2_cache
*cache
, gfp_t mask
, u32 key
,
54 struct mb2_cache_entry
*entry
, *dup
;
55 struct hlist_bl_node
*dup_node
;
56 struct hlist_bl_head
*head
;
58 entry
= kmem_cache_alloc(mb2_entry_cache
, mask
);
62 INIT_LIST_HEAD(&entry
->e_lru_list
);
63 /* One ref for hash, one ref returned */
64 atomic_set(&entry
->e_refcnt
, 1);
66 entry
->e_block
= block
;
67 head
= &cache
->c_hash
[hash_32(key
, cache
->c_bucket_bits
)];
68 entry
->e_hash_list_head
= head
;
70 hlist_bl_for_each_entry(dup
, dup_node
, head
, e_hash_list
) {
71 if (dup
->e_key
== key
&& dup
->e_block
== block
) {
72 hlist_bl_unlock(head
);
73 kmem_cache_free(mb2_entry_cache
, entry
);
77 hlist_bl_add_head(&entry
->e_hash_list
, head
);
78 hlist_bl_unlock(head
);
80 spin_lock(&cache
->c_lru_list_lock
);
81 list_add_tail(&entry
->e_lru_list
, &cache
->c_lru_list
);
82 /* Grab ref for LRU list */
83 atomic_inc(&entry
->e_refcnt
);
84 cache
->c_entry_count
++;
85 spin_unlock(&cache
->c_lru_list_lock
);
89 EXPORT_SYMBOL(mb2_cache_entry_create
);
91 void __mb2_cache_entry_free(struct mb2_cache_entry
*entry
)
93 kmem_cache_free(mb2_entry_cache
, entry
);
95 EXPORT_SYMBOL(__mb2_cache_entry_free
);
97 static struct mb2_cache_entry
*__entry_find(struct mb2_cache
*cache
,
98 struct mb2_cache_entry
*entry
,
101 struct mb2_cache_entry
*old_entry
= entry
;
102 struct hlist_bl_node
*node
;
103 struct hlist_bl_head
*head
;
106 head
= entry
->e_hash_list_head
;
108 head
= &cache
->c_hash
[hash_32(key
, cache
->c_bucket_bits
)];
110 if (entry
&& !hlist_bl_unhashed(&entry
->e_hash_list
))
111 node
= entry
->e_hash_list
.next
;
113 node
= hlist_bl_first(head
);
115 entry
= hlist_bl_entry(node
, struct mb2_cache_entry
,
117 if (entry
->e_key
== key
) {
118 atomic_inc(&entry
->e_refcnt
);
125 hlist_bl_unlock(head
);
127 mb2_cache_entry_put(cache
, old_entry
);
133 * mb2_cache_entry_find_first - find the first entry in cache with given key
134 * @cache: cache where we should search
135 * @key: key to look for
137 * Search in @cache for entry with key @key. Grabs reference to the first
138 * entry found and returns the entry.
140 struct mb2_cache_entry
*mb2_cache_entry_find_first(struct mb2_cache
*cache
,
143 return __entry_find(cache
, NULL
, key
);
145 EXPORT_SYMBOL(mb2_cache_entry_find_first
);
148 * mb2_cache_entry_find_next - find next entry in cache with the same
149 * @cache: cache where we should search
150 * @entry: entry to start search from
152 * Finds next entry in the hash chain which has the same key as @entry.
153 * If @entry is unhashed (which can happen when deletion of entry races
154 * with the search), finds the first entry in the hash chain. The function
155 * drops reference to @entry and returns with a reference to the found entry.
157 struct mb2_cache_entry
*mb2_cache_entry_find_next(struct mb2_cache
*cache
,
158 struct mb2_cache_entry
*entry
)
160 return __entry_find(cache
, entry
, entry
->e_key
);
162 EXPORT_SYMBOL(mb2_cache_entry_find_next
);
164 /* mb2_cache_entry_delete_block - remove information about block from cache
165 * @cache - cache we work with
166 * @key - key of the entry to remove
167 * @block - block containing data for @key
169 * Remove entry from cache @cache with key @key with data stored in @block.
171 void mb2_cache_entry_delete_block(struct mb2_cache
*cache
, u32 key
,
174 struct hlist_bl_node
*node
;
175 struct hlist_bl_head
*head
;
176 struct mb2_cache_entry
*entry
;
178 head
= &cache
->c_hash
[hash_32(key
, cache
->c_bucket_bits
)];
180 hlist_bl_for_each_entry(entry
, node
, head
, e_hash_list
) {
181 if (entry
->e_key
== key
&& entry
->e_block
== block
) {
182 /* We keep hash list reference to keep entry alive */
183 hlist_bl_del_init(&entry
->e_hash_list
);
184 hlist_bl_unlock(head
);
185 spin_lock(&cache
->c_lru_list_lock
);
186 if (!list_empty(&entry
->e_lru_list
)) {
187 list_del_init(&entry
->e_lru_list
);
188 cache
->c_entry_count
--;
189 atomic_dec(&entry
->e_refcnt
);
191 spin_unlock(&cache
->c_lru_list_lock
);
192 mb2_cache_entry_put(cache
, entry
);
196 hlist_bl_unlock(head
);
198 EXPORT_SYMBOL(mb2_cache_entry_delete_block
);
200 /* mb2_cache_entry_touch - cache entry got used
201 * @cache - cache the entry belongs to
202 * @entry - entry that got used
204 * Move entry in lru list to reflect the fact that it was used.
206 void mb2_cache_entry_touch(struct mb2_cache
*cache
,
207 struct mb2_cache_entry
*entry
)
209 spin_lock(&cache
->c_lru_list_lock
);
210 if (!list_empty(&entry
->e_lru_list
))
211 list_move_tail(&cache
->c_lru_list
, &entry
->e_lru_list
);
212 spin_unlock(&cache
->c_lru_list_lock
);
214 EXPORT_SYMBOL(mb2_cache_entry_touch
);
216 static unsigned long mb2_cache_count(struct shrinker
*shrink
,
217 struct shrink_control
*sc
)
219 struct mb2_cache
*cache
= container_of(shrink
, struct mb2_cache
,
222 return cache
->c_entry_count
;
225 /* Shrink number of entries in cache */
226 static unsigned long mb2_cache_scan(struct shrinker
*shrink
,
227 struct shrink_control
*sc
)
229 int nr_to_scan
= sc
->nr_to_scan
;
230 struct mb2_cache
*cache
= container_of(shrink
, struct mb2_cache
,
232 struct mb2_cache_entry
*entry
;
233 struct hlist_bl_head
*head
;
234 unsigned int shrunk
= 0;
236 spin_lock(&cache
->c_lru_list_lock
);
237 while (nr_to_scan
-- && !list_empty(&cache
->c_lru_list
)) {
238 entry
= list_first_entry(&cache
->c_lru_list
,
239 struct mb2_cache_entry
, e_lru_list
);
240 list_del_init(&entry
->e_lru_list
);
241 cache
->c_entry_count
--;
243 * We keep LRU list reference so that entry doesn't go away
246 spin_unlock(&cache
->c_lru_list_lock
);
247 head
= entry
->e_hash_list_head
;
249 if (!hlist_bl_unhashed(&entry
->e_hash_list
)) {
250 hlist_bl_del_init(&entry
->e_hash_list
);
251 atomic_dec(&entry
->e_refcnt
);
253 hlist_bl_unlock(head
);
254 if (mb2_cache_entry_put(cache
, entry
))
257 spin_lock(&cache
->c_lru_list_lock
);
259 spin_unlock(&cache
->c_lru_list_lock
);
265 * mb2_cache_create - create cache
266 * @bucket_bits: log2 of the hash table size
268 * Create cache for keys with 2^bucket_bits hash entries.
270 struct mb2_cache
*mb2_cache_create(int bucket_bits
)
272 struct mb2_cache
*cache
;
273 int bucket_count
= 1 << bucket_bits
;
276 if (!try_module_get(THIS_MODULE
))
279 cache
= kzalloc(sizeof(struct mb2_cache
), GFP_KERNEL
);
282 cache
->c_bucket_bits
= bucket_bits
;
283 INIT_LIST_HEAD(&cache
->c_lru_list
);
284 spin_lock_init(&cache
->c_lru_list_lock
);
285 cache
->c_hash
= kmalloc(bucket_count
* sizeof(struct hlist_bl_head
),
287 if (!cache
->c_hash
) {
291 for (i
= 0; i
< bucket_count
; i
++)
292 INIT_HLIST_BL_HEAD(&cache
->c_hash
[i
]);
294 cache
->c_shrink
.count_objects
= mb2_cache_count
;
295 cache
->c_shrink
.scan_objects
= mb2_cache_scan
;
296 cache
->c_shrink
.seeks
= DEFAULT_SEEKS
;
297 register_shrinker(&cache
->c_shrink
);
302 module_put(THIS_MODULE
);
305 EXPORT_SYMBOL(mb2_cache_create
);
308 * mb2_cache_destroy - destroy cache
309 * @cache: the cache to destroy
311 * Free all entries in cache and cache itself. Caller must make sure nobody
312 * (except shrinker) can reach @cache when calling this.
314 void mb2_cache_destroy(struct mb2_cache
*cache
)
316 struct mb2_cache_entry
*entry
, *next
;
318 unregister_shrinker(&cache
->c_shrink
);
321 * We don't bother with any locking. Cache must not be used at this
324 list_for_each_entry_safe(entry
, next
, &cache
->c_lru_list
, e_lru_list
) {
325 if (!hlist_bl_unhashed(&entry
->e_hash_list
)) {
326 hlist_bl_del_init(&entry
->e_hash_list
);
327 atomic_dec(&entry
->e_refcnt
);
330 list_del(&entry
->e_lru_list
);
331 WARN_ON(atomic_read(&entry
->e_refcnt
) != 1);
332 mb2_cache_entry_put(cache
, entry
);
334 kfree(cache
->c_hash
);
336 module_put(THIS_MODULE
);
338 EXPORT_SYMBOL(mb2_cache_destroy
);
340 static int __init
mb2cache_init(void)
342 mb2_entry_cache
= kmem_cache_create("mbcache",
343 sizeof(struct mb2_cache_entry
), 0,
344 SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
, NULL
);
345 BUG_ON(!mb2_entry_cache
);
349 static void __exit
mb2cache_exit(void)
351 kmem_cache_destroy(mb2_entry_cache
);
354 module_init(mb2cache_init
)
355 module_exit(mb2cache_exit
)
357 MODULE_AUTHOR("Jan Kara <jack@suse.cz>");
358 MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
359 MODULE_LICENSE("GPL");