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/workqueue.h>
8 #include <linux/mbcache.h>
11 * Mbcache is a simple key-value store. Keys need not be unique, however
12 * key-value pairs are expected to be unique (we use this fact in
13 * mb_cache_entry_delete_block()).
15 * Ext2 and ext4 use this cache for deduplication of extended attribute blocks.
16 * They use hash of a block contents as a key and block number as a value.
17 * That's why keys need not be unique (different xattr blocks may end up having
18 * the same hash). However block number always uniquely identifies a cache
21 * We provide functions for creation and removal of entries, search by key,
22 * and a special "delete entry with given key-value pair" operation. Fixed
23 * size hash table is used for fast key lookups.
27 /* Hash table of entries */
28 struct hlist_bl_head *c_hash;
29 /* log2 of hash table size */
31 /* Maximum entries in cache to avoid degrading hash too much */
33 /* Protects c_list, c_entry_count */
34 spinlock_t c_list_lock;
35 struct list_head c_list;
36 /* Number of entries in cache */
37 unsigned long c_entry_count;
38 struct shrinker c_shrink;
39 /* Work for shrinking when the cache has too many entries */
40 struct work_struct c_shrink_work;
43 static struct kmem_cache *mb_entry_cache;
45 static unsigned long mb_cache_shrink(struct mb_cache *cache,
46 unsigned int nr_to_scan);
48 static inline bool mb_cache_entry_referenced(struct mb_cache_entry *entry)
50 return entry->_e_hash_list_head & 1;
53 static inline void mb_cache_entry_set_referenced(struct mb_cache_entry *entry)
55 entry->_e_hash_list_head |= 1;
58 static inline void mb_cache_entry_clear_referenced(
59 struct mb_cache_entry *entry)
61 entry->_e_hash_list_head &= ~1;
64 static inline struct hlist_bl_head *mb_cache_entry_head(
65 struct mb_cache_entry *entry)
67 return (struct hlist_bl_head *)
68 (entry->_e_hash_list_head & ~1);
72 * Number of entries to reclaim synchronously when there are too many entries
75 #define SYNC_SHRINK_BATCH 64
78 * mb_cache_entry_create - create entry in cache
79 * @cache - cache where the entry should be created
80 * @mask - gfp mask with which the entry should be allocated
81 * @key - key of the entry
82 * @block - block that contains data
84 * Creates entry in @cache with key @key and records that data is stored in
85 * block @block. The function returns -EBUSY if entry with the same key
86 * and for the same block already exists in cache. Otherwise 0 is returned.
88 int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
91 struct mb_cache_entry *entry, *dup;
92 struct hlist_bl_node *dup_node;
93 struct hlist_bl_head *head;
95 /* Schedule background reclaim if there are too many entries */
96 if (cache->c_entry_count >= cache->c_max_entries)
97 schedule_work(&cache->c_shrink_work);
98 /* Do some sync reclaim if background reclaim cannot keep up */
99 if (cache->c_entry_count >= 2*cache->c_max_entries)
100 mb_cache_shrink(cache, SYNC_SHRINK_BATCH);
102 entry = kmem_cache_alloc(mb_entry_cache, mask);
106 INIT_LIST_HEAD(&entry->e_list);
107 /* One ref for hash, one ref returned */
108 atomic_set(&entry->e_refcnt, 1);
110 entry->e_block = block;
111 head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
112 entry->_e_hash_list_head = (unsigned long)head;
114 hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
115 if (dup->e_key == key && dup->e_block == block) {
116 hlist_bl_unlock(head);
117 kmem_cache_free(mb_entry_cache, entry);
121 hlist_bl_add_head(&entry->e_hash_list, head);
122 hlist_bl_unlock(head);
124 spin_lock(&cache->c_list_lock);
125 list_add_tail(&entry->e_list, &cache->c_list);
126 /* Grab ref for LRU list */
127 atomic_inc(&entry->e_refcnt);
128 cache->c_entry_count++;
129 spin_unlock(&cache->c_list_lock);
133 EXPORT_SYMBOL(mb_cache_entry_create);
135 void __mb_cache_entry_free(struct mb_cache_entry *entry)
137 kmem_cache_free(mb_entry_cache, entry);
139 EXPORT_SYMBOL(__mb_cache_entry_free);
141 static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
142 struct mb_cache_entry *entry,
145 struct mb_cache_entry *old_entry = entry;
146 struct hlist_bl_node *node;
147 struct hlist_bl_head *head;
150 head = mb_cache_entry_head(entry);
152 head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
154 if (entry && !hlist_bl_unhashed(&entry->e_hash_list))
155 node = entry->e_hash_list.next;
157 node = hlist_bl_first(head);
159 entry = hlist_bl_entry(node, struct mb_cache_entry,
161 if (entry->e_key == key) {
162 atomic_inc(&entry->e_refcnt);
169 hlist_bl_unlock(head);
171 mb_cache_entry_put(cache, old_entry);
177 * mb_cache_entry_find_first - find the first entry in cache with given key
178 * @cache: cache where we should search
179 * @key: key to look for
181 * Search in @cache for entry with key @key. Grabs reference to the first
182 * entry found and returns the entry.
184 struct mb_cache_entry *mb_cache_entry_find_first(struct mb_cache *cache,
187 return __entry_find(cache, NULL, key);
189 EXPORT_SYMBOL(mb_cache_entry_find_first);
192 * mb_cache_entry_find_next - find next entry in cache with the same
193 * @cache: cache where we should search
194 * @entry: entry to start search from
196 * Finds next entry in the hash chain which has the same key as @entry.
197 * If @entry is unhashed (which can happen when deletion of entry races
198 * with the search), finds the first entry in the hash chain. The function
199 * drops reference to @entry and returns with a reference to the found entry.
201 struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache *cache,
202 struct mb_cache_entry *entry)
204 return __entry_find(cache, entry, entry->e_key);
206 EXPORT_SYMBOL(mb_cache_entry_find_next);
208 /* mb_cache_entry_delete_block - remove information about block from cache
209 * @cache - cache we work with
210 * @key - key of the entry to remove
211 * @block - block containing data for @key
213 * Remove entry from cache @cache with key @key with data stored in @block.
215 void mb_cache_entry_delete_block(struct mb_cache *cache, u32 key,
218 struct hlist_bl_node *node;
219 struct hlist_bl_head *head;
220 struct mb_cache_entry *entry;
222 head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
224 hlist_bl_for_each_entry(entry, node, head, e_hash_list) {
225 if (entry->e_key == key && entry->e_block == block) {
226 /* We keep hash list reference to keep entry alive */
227 hlist_bl_del_init(&entry->e_hash_list);
228 hlist_bl_unlock(head);
229 spin_lock(&cache->c_list_lock);
230 if (!list_empty(&entry->e_list)) {
231 list_del_init(&entry->e_list);
232 cache->c_entry_count--;
233 atomic_dec(&entry->e_refcnt);
235 spin_unlock(&cache->c_list_lock);
236 mb_cache_entry_put(cache, entry);
240 hlist_bl_unlock(head);
242 EXPORT_SYMBOL(mb_cache_entry_delete_block);
244 /* mb_cache_entry_touch - cache entry got used
245 * @cache - cache the entry belongs to
246 * @entry - entry that got used
248 * Marks entry as used to give hit higher chances of surviving in cache.
250 void mb_cache_entry_touch(struct mb_cache *cache,
251 struct mb_cache_entry *entry)
253 mb_cache_entry_set_referenced(entry);
255 EXPORT_SYMBOL(mb_cache_entry_touch);
257 static unsigned long mb_cache_count(struct shrinker *shrink,
258 struct shrink_control *sc)
260 struct mb_cache *cache = container_of(shrink, struct mb_cache,
263 return cache->c_entry_count;
266 /* Shrink number of entries in cache */
267 static unsigned long mb_cache_shrink(struct mb_cache *cache,
268 unsigned int nr_to_scan)
270 struct mb_cache_entry *entry;
271 struct hlist_bl_head *head;
272 unsigned int shrunk = 0;
274 spin_lock(&cache->c_list_lock);
275 while (nr_to_scan-- && !list_empty(&cache->c_list)) {
276 entry = list_first_entry(&cache->c_list,
277 struct mb_cache_entry, e_list);
278 if (mb_cache_entry_referenced(entry)) {
279 mb_cache_entry_clear_referenced(entry);
280 list_move_tail(&cache->c_list, &entry->e_list);
283 list_del_init(&entry->e_list);
284 cache->c_entry_count--;
286 * We keep LRU list reference so that entry doesn't go away
289 spin_unlock(&cache->c_list_lock);
290 head = mb_cache_entry_head(entry);
292 if (!hlist_bl_unhashed(&entry->e_hash_list)) {
293 hlist_bl_del_init(&entry->e_hash_list);
294 atomic_dec(&entry->e_refcnt);
296 hlist_bl_unlock(head);
297 if (mb_cache_entry_put(cache, entry))
300 spin_lock(&cache->c_list_lock);
302 spin_unlock(&cache->c_list_lock);
307 static unsigned long mb_cache_scan(struct shrinker *shrink,
308 struct shrink_control *sc)
310 int nr_to_scan = sc->nr_to_scan;
311 struct mb_cache *cache = container_of(shrink, struct mb_cache,
313 return mb_cache_shrink(cache, nr_to_scan);
316 /* We shrink 1/X of the cache when we have too many entries in it */
317 #define SHRINK_DIVISOR 16
319 static void mb_cache_shrink_worker(struct work_struct *work)
321 struct mb_cache *cache = container_of(work, struct mb_cache,
323 mb_cache_shrink(cache, cache->c_max_entries / SHRINK_DIVISOR);
327 * mb_cache_create - create cache
328 * @bucket_bits: log2 of the hash table size
330 * Create cache for keys with 2^bucket_bits hash entries.
332 struct mb_cache *mb_cache_create(int bucket_bits)
334 struct mb_cache *cache;
335 int bucket_count = 1 << bucket_bits;
338 if (!try_module_get(THIS_MODULE))
341 cache = kzalloc(sizeof(struct mb_cache), GFP_KERNEL);
344 cache->c_bucket_bits = bucket_bits;
345 cache->c_max_entries = bucket_count << 4;
346 INIT_LIST_HEAD(&cache->c_list);
347 spin_lock_init(&cache->c_list_lock);
348 cache->c_hash = kmalloc(bucket_count * sizeof(struct hlist_bl_head),
350 if (!cache->c_hash) {
354 for (i = 0; i < bucket_count; i++)
355 INIT_HLIST_BL_HEAD(&cache->c_hash[i]);
357 cache->c_shrink.count_objects = mb_cache_count;
358 cache->c_shrink.scan_objects = mb_cache_scan;
359 cache->c_shrink.seeks = DEFAULT_SEEKS;
360 register_shrinker(&cache->c_shrink);
362 INIT_WORK(&cache->c_shrink_work, mb_cache_shrink_worker);
367 module_put(THIS_MODULE);
370 EXPORT_SYMBOL(mb_cache_create);
373 * mb_cache_destroy - destroy cache
374 * @cache: the cache to destroy
376 * Free all entries in cache and cache itself. Caller must make sure nobody
377 * (except shrinker) can reach @cache when calling this.
379 void mb_cache_destroy(struct mb_cache *cache)
381 struct mb_cache_entry *entry, *next;
383 unregister_shrinker(&cache->c_shrink);
386 * We don't bother with any locking. Cache must not be used at this
389 list_for_each_entry_safe(entry, next, &cache->c_list, e_list) {
390 if (!hlist_bl_unhashed(&entry->e_hash_list)) {
391 hlist_bl_del_init(&entry->e_hash_list);
392 atomic_dec(&entry->e_refcnt);
395 list_del(&entry->e_list);
396 WARN_ON(atomic_read(&entry->e_refcnt) != 1);
397 mb_cache_entry_put(cache, entry);
399 kfree(cache->c_hash);
401 module_put(THIS_MODULE);
403 EXPORT_SYMBOL(mb_cache_destroy);
405 static int __init mbcache_init(void)
407 mb_entry_cache = kmem_cache_create("mbcache",
408 sizeof(struct mb_cache_entry), 0,
409 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
410 BUG_ON(!mb_entry_cache);
414 static void __exit mbcache_exit(void)
416 kmem_cache_destroy(mb_entry_cache);
419 module_init(mbcache_init)
420 module_exit(mbcache_exit)
422 MODULE_AUTHOR("Jan Kara <jack@suse.cz>");
423 MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
424 MODULE_LICENSE("GPL");