Lines Matching full:cache
16 * Ext2 and ext4 use this cache for deduplication of extended attribute blocks.
21 * identifies a cache entry.
33 /* Maximum entries in cache to avoid degrading hash too much */
38 /* Number of entries in cache */
41 /* Work for shrinking when the cache has too many entries */
47 static unsigned long mb_cache_shrink(struct mb_cache *cache,
50 static inline struct hlist_bl_head *mb_cache_entry_head(struct mb_cache *cache, in mb_cache_entry_head() argument
53 return &cache->c_hash[hash_32(key, cache->c_bucket_bits)]; in mb_cache_entry_head()
58 * in cache
63 * mb_cache_entry_create - create entry in cache
64 * @cache - cache where the entry should be created
70 * Creates entry in @cache with key @key and value @value. The function returns
71 * -EBUSY if entry with the same key and value already exists in cache.
74 int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key, in mb_cache_entry_create() argument
82 if (cache->c_entry_count >= cache->c_max_entries) in mb_cache_entry_create()
83 schedule_work(&cache->c_shrink_work); in mb_cache_entry_create()
85 if (cache->c_entry_count >= 2*cache->c_max_entries) in mb_cache_entry_create()
86 mb_cache_shrink(cache, SYNC_SHRINK_BATCH); in mb_cache_entry_create()
97 * avoids nesting of cache->c_list_lock into hash table bit locks which in mb_cache_entry_create()
106 head = mb_cache_entry_head(cache, key); in mb_cache_entry_create()
117 spin_lock(&cache->c_list_lock); in mb_cache_entry_create()
118 list_add_tail(&entry->e_list, &cache->c_list); in mb_cache_entry_create()
119 cache->c_entry_count++; in mb_cache_entry_create()
120 spin_unlock(&cache->c_list_lock); in mb_cache_entry_create()
121 mb_cache_entry_put(cache, entry); in mb_cache_entry_create()
127 void __mb_cache_entry_free(struct mb_cache *cache, struct mb_cache_entry *entry) in __mb_cache_entry_free() argument
131 head = mb_cache_entry_head(cache, entry->e_key); in __mb_cache_entry_free()
152 static struct mb_cache_entry *__entry_find(struct mb_cache *cache, in __entry_find() argument
160 head = mb_cache_entry_head(cache, key); in __entry_find()
179 mb_cache_entry_put(cache, old_entry); in __entry_find()
186 * @cache: cache where we should search
189 * Search in @cache for a reusable entry with key @key. Grabs reference to the
192 struct mb_cache_entry *mb_cache_entry_find_first(struct mb_cache *cache, in mb_cache_entry_find_first() argument
195 return __entry_find(cache, NULL, key); in mb_cache_entry_find_first()
201 * @cache: cache where we should search
209 struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache *cache, in mb_cache_entry_find_next() argument
212 return __entry_find(cache, entry, entry->e_key); in mb_cache_entry_find_next()
217 * mb_cache_entry_get - get a cache entry by value (and key)
218 * @cache - cache we work with
222 struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key, in mb_cache_entry_get() argument
229 head = mb_cache_entry_head(cache, key); in mb_cache_entry_get()
243 /* mb_cache_entry_delete - try to remove a cache entry
244 * @cache - cache we work with
248 * Remove entry from cache @cache with key @key and value @value.
250 void mb_cache_entry_delete(struct mb_cache *cache, u32 key, u64 value) in mb_cache_entry_delete() argument
256 head = mb_cache_entry_head(cache, key); in mb_cache_entry_delete()
263 spin_lock(&cache->c_list_lock); in mb_cache_entry_delete()
266 if (!WARN_ONCE(cache->c_entry_count == 0, in mb_cache_entry_delete()
268 cache->c_entry_count--; in mb_cache_entry_delete()
271 spin_unlock(&cache->c_list_lock); in mb_cache_entry_delete()
272 mb_cache_entry_put(cache, entry); in mb_cache_entry_delete()
280 /* mb_cache_entry_delete_or_get - remove a cache entry if it has no users
281 * @cache - cache we work with
285 * Remove entry from cache @cache with key @key and value @value. The removal
287 * entry was successfully removed or there's no entry in cache. Otherwise the
291 struct mb_cache_entry *mb_cache_entry_delete_or_get(struct mb_cache *cache, in mb_cache_entry_delete_or_get() argument
296 entry = mb_cache_entry_get(cache, key, value); in mb_cache_entry_delete_or_get()
307 spin_lock(&cache->c_list_lock); in mb_cache_entry_delete_or_get()
310 cache->c_entry_count--; in mb_cache_entry_delete_or_get()
311 spin_unlock(&cache->c_list_lock); in mb_cache_entry_delete_or_get()
312 __mb_cache_entry_free(cache, entry); in mb_cache_entry_delete_or_get()
317 /* mb_cache_entry_touch - cache entry got used
318 * @cache - cache the entry belongs to
321 * Marks entry as used to give hit higher chances of surviving in cache.
323 void mb_cache_entry_touch(struct mb_cache *cache, in mb_cache_entry_touch() argument
333 struct mb_cache *cache = container_of(shrink, struct mb_cache, in mb_cache_count() local
336 return cache->c_entry_count; in mb_cache_count()
339 /* Shrink number of entries in cache */
340 static unsigned long mb_cache_shrink(struct mb_cache *cache, in mb_cache_shrink() argument
346 spin_lock(&cache->c_list_lock); in mb_cache_shrink()
347 while (nr_to_scan-- && !list_empty(&cache->c_list)) { in mb_cache_shrink()
348 entry = list_first_entry(&cache->c_list, in mb_cache_shrink()
354 list_move_tail(&entry->e_list, &cache->c_list); in mb_cache_shrink()
358 cache->c_entry_count--; in mb_cache_shrink()
359 spin_unlock(&cache->c_list_lock); in mb_cache_shrink()
360 __mb_cache_entry_free(cache, entry); in mb_cache_shrink()
363 spin_lock(&cache->c_list_lock); in mb_cache_shrink()
365 spin_unlock(&cache->c_list_lock); in mb_cache_shrink()
373 struct mb_cache *cache = container_of(shrink, struct mb_cache, in mb_cache_scan() local
375 return mb_cache_shrink(cache, sc->nr_to_scan); in mb_cache_scan()
378 /* We shrink 1/X of the cache when we have too many entries in it */
383 struct mb_cache *cache = container_of(work, struct mb_cache, in mb_cache_shrink_worker() local
385 mb_cache_shrink(cache, cache->c_max_entries / SHRINK_DIVISOR); in mb_cache_shrink_worker()
389 * mb_cache_create - create cache
392 * Create cache for keys with 2^bucket_bits hash entries.
396 struct mb_cache *cache; in mb_cache_create() local
400 cache = kzalloc(sizeof(struct mb_cache), GFP_KERNEL); in mb_cache_create()
401 if (!cache) in mb_cache_create()
403 cache->c_bucket_bits = bucket_bits; in mb_cache_create()
404 cache->c_max_entries = bucket_count << 4; in mb_cache_create()
405 INIT_LIST_HEAD(&cache->c_list); in mb_cache_create()
406 spin_lock_init(&cache->c_list_lock); in mb_cache_create()
407 cache->c_hash = kmalloc_array(bucket_count, in mb_cache_create()
410 if (!cache->c_hash) { in mb_cache_create()
411 kfree(cache); in mb_cache_create()
415 INIT_HLIST_BL_HEAD(&cache->c_hash[i]); in mb_cache_create()
417 cache->c_shrink.count_objects = mb_cache_count; in mb_cache_create()
418 cache->c_shrink.scan_objects = mb_cache_scan; in mb_cache_create()
419 cache->c_shrink.seeks = DEFAULT_SEEKS; in mb_cache_create()
420 if (register_shrinker(&cache->c_shrink)) { in mb_cache_create()
421 kfree(cache->c_hash); in mb_cache_create()
422 kfree(cache); in mb_cache_create()
426 INIT_WORK(&cache->c_shrink_work, mb_cache_shrink_worker); in mb_cache_create()
428 return cache; in mb_cache_create()
436 * mb_cache_destroy - destroy cache
437 * @cache: the cache to destroy
439 * Free all entries in cache and cache itself. Caller must make sure nobody
440 * (except shrinker) can reach @cache when calling this.
442 void mb_cache_destroy(struct mb_cache *cache) in mb_cache_destroy() argument
446 unregister_shrinker(&cache->c_shrink); in mb_cache_destroy()
449 * We don't bother with any locking. Cache must not be used at this in mb_cache_destroy()
452 list_for_each_entry_safe(entry, next, &cache->c_list, e_list) { in mb_cache_destroy()
455 mb_cache_entry_put(cache, entry); in mb_cache_destroy()
457 kfree(cache->c_hash); in mb_cache_destroy()
458 kfree(cache); in mb_cache_destroy()
481 MODULE_DESCRIPTION("Meta block cache (for extended attributes)");