3 * (C) 2001-2002 Andreas Gruenbacher, <a.gruenbacher@computer.org>
7 * Filesystem Meta Information Block Cache (mbcache)
9 * The mbcache caches blocks of block devices that need to be located
10 * by their device/block number, as well as by other criteria (such
11 * as the block's contents).
13 * There can only be one cache entry in a cache per device and block number.
14 * Additional indexes need not be unique in this sense. The number of
15 * additional indexes (=other criteria) can be hardwired at compile time
16 * or specified at cache create time.
18 * Each cache entry is of fixed size. An entry may be `valid' or `invalid'
19 * in the cache. A valid entry is in the main hash tables of the cache,
20 * and may also be in the lru list. An invalid entry is not in any hashes
23 * A valid cache entry is only in the lru list if no handles refer to it.
24 * Invalid cache entries will be freed when the last handle to the cache
25 * entry is released. Entries that cannot be freed immediately are put
26 * back on the lru list.
29 #include <linux/kernel.h>
30 #include <linux/module.h>
32 #include <linux/hash.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
37 #include <linux/list_bl.h>
38 #include <linux/mbcache.h>
39 #include <linux/init.h>
42 # define mb_debug(f...) do { \
43 printk(KERN_DEBUG f); \
46 #define mb_assert(c) do { if (!(c)) \
47 printk(KERN_ERR "assertion " #c " failed\n"); \
50 # define mb_debug(f...) do { } while(0)
51 # define mb_assert(c) do { } while(0)
53 #define mb_error(f...) do { \
58 #define MB_CACHE_WRITER ((unsigned short)~0U >> 1)
60 static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
62 MODULE_AUTHOR("Andreas Gruenbacher <a.gruenbacher@computer.org>");
63 MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
64 MODULE_LICENSE("GPL");
66 EXPORT_SYMBOL(mb_cache_create);
67 EXPORT_SYMBOL(mb_cache_shrink);
68 EXPORT_SYMBOL(mb_cache_destroy);
69 EXPORT_SYMBOL(mb_cache_entry_alloc);
70 EXPORT_SYMBOL(mb_cache_entry_insert);
71 EXPORT_SYMBOL(mb_cache_entry_release);
72 EXPORT_SYMBOL(mb_cache_entry_free);
73 EXPORT_SYMBOL(mb_cache_entry_get);
74 #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
75 EXPORT_SYMBOL(mb_cache_entry_find_first);
76 EXPORT_SYMBOL(mb_cache_entry_find_next);
80 * Global data: list of all mbcache's, lru list, and a spinlock for
81 * accessing cache data structures on SMP machines. The lru list is
82 * global across all mbcaches.
85 static LIST_HEAD(mb_cache_list);
86 static LIST_HEAD(mb_cache_lru_list);
87 static DEFINE_SPINLOCK(mb_cache_spinlock);
90 __mb_cache_entry_is_block_hashed(struct mb_cache_entry *ce)
92 return !hlist_bl_unhashed(&ce->e_block_list);
97 __mb_cache_entry_unhash_block(struct mb_cache_entry *ce)
99 if (__mb_cache_entry_is_block_hashed(ce))
100 hlist_bl_del_init(&ce->e_block_list);
104 __mb_cache_entry_is_index_hashed(struct mb_cache_entry *ce)
106 return !hlist_bl_unhashed(&ce->e_index.o_list);
110 __mb_cache_entry_unhash_index(struct mb_cache_entry *ce)
112 if (__mb_cache_entry_is_index_hashed(ce))
113 hlist_bl_del_init(&ce->e_index.o_list);
117 __mb_cache_entry_unhash(struct mb_cache_entry *ce)
119 __mb_cache_entry_unhash_index(ce);
120 __mb_cache_entry_unhash_block(ce);
124 __mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask)
126 struct mb_cache *cache = ce->e_cache;
128 mb_assert(!(ce->e_used || ce->e_queued));
129 kmem_cache_free(cache->c_entry_cache, ce);
130 atomic_dec(&cache->c_entry_count);
135 __mb_cache_entry_release_unlock(struct mb_cache_entry *ce)
136 __releases(mb_cache_spinlock)
138 /* Wake up all processes queuing for this cache entry. */
140 wake_up_all(&mb_cache_queue);
141 if (ce->e_used >= MB_CACHE_WRITER)
142 ce->e_used -= MB_CACHE_WRITER;
144 if (!(ce->e_used || ce->e_queued)) {
145 if (!__mb_cache_entry_is_block_hashed(ce))
147 mb_assert(list_empty(&ce->e_lru_list));
148 list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
150 spin_unlock(&mb_cache_spinlock);
153 spin_unlock(&mb_cache_spinlock);
154 __mb_cache_entry_forget(ce, GFP_KERNEL);
159 * mb_cache_shrink_scan() memory pressure callback
161 * This function is called by the kernel memory management when memory
165 * @sc: shrink_control passed from reclaim
167 * Returns the number of objects freed.
170 mb_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
172 LIST_HEAD(free_list);
173 struct mb_cache_entry *entry, *tmp;
174 int nr_to_scan = sc->nr_to_scan;
175 gfp_t gfp_mask = sc->gfp_mask;
176 unsigned long freed = 0;
178 mb_debug("trying to free %d entries", nr_to_scan);
179 spin_lock(&mb_cache_spinlock);
180 while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) {
181 struct mb_cache_entry *ce =
182 list_entry(mb_cache_lru_list.next,
183 struct mb_cache_entry, e_lru_list);
184 list_move_tail(&ce->e_lru_list, &free_list);
185 __mb_cache_entry_unhash(ce);
188 spin_unlock(&mb_cache_spinlock);
189 list_for_each_entry_safe(entry, tmp, &free_list, e_lru_list) {
190 __mb_cache_entry_forget(entry, gfp_mask);
196 mb_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
198 struct mb_cache *cache;
199 unsigned long count = 0;
201 spin_lock(&mb_cache_spinlock);
202 list_for_each_entry(cache, &mb_cache_list, c_cache_list) {
203 mb_debug("cache %s (%d)", cache->c_name,
204 atomic_read(&cache->c_entry_count));
205 count += atomic_read(&cache->c_entry_count);
207 spin_unlock(&mb_cache_spinlock);
209 return vfs_pressure_ratio(count);
212 static struct shrinker mb_cache_shrinker = {
213 .count_objects = mb_cache_shrink_count,
214 .scan_objects = mb_cache_shrink_scan,
215 .seeks = DEFAULT_SEEKS,
219 * mb_cache_create() create a new cache
221 * All entries in one cache are equal size. Cache entries may be from
222 * multiple devices. If this is the first mbcache created, registers
223 * the cache with kernel memory management. Returns NULL if no more
224 * memory was available.
226 * @name: name of the cache (informal)
227 * @bucket_bits: log2(number of hash buckets)
230 mb_cache_create(const char *name, int bucket_bits)
232 int n, bucket_count = 1 << bucket_bits;
233 struct mb_cache *cache = NULL;
235 cache = kmalloc(sizeof(struct mb_cache), GFP_KERNEL);
238 cache->c_name = name;
239 atomic_set(&cache->c_entry_count, 0);
240 cache->c_bucket_bits = bucket_bits;
241 cache->c_block_hash = kmalloc(bucket_count *
242 sizeof(struct hlist_bl_head), GFP_KERNEL);
243 if (!cache->c_block_hash)
245 for (n=0; n<bucket_count; n++)
246 INIT_HLIST_BL_HEAD(&cache->c_block_hash[n]);
247 cache->c_index_hash = kmalloc(bucket_count *
248 sizeof(struct hlist_bl_head), GFP_KERNEL);
249 if (!cache->c_index_hash)
251 for (n=0; n<bucket_count; n++)
252 INIT_HLIST_BL_HEAD(&cache->c_index_hash[n]);
253 cache->c_entry_cache = kmem_cache_create(name,
254 sizeof(struct mb_cache_entry), 0,
255 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
256 if (!cache->c_entry_cache)
260 * Set an upper limit on the number of cache entries so that the hash
261 * chains won't grow too long.
263 cache->c_max_entries = bucket_count << 4;
265 spin_lock(&mb_cache_spinlock);
266 list_add(&cache->c_cache_list, &mb_cache_list);
267 spin_unlock(&mb_cache_spinlock);
271 kfree(cache->c_index_hash);
274 kfree(cache->c_block_hash);
283 * Removes all cache entries of a device from the cache. All cache entries
284 * currently in use cannot be freed, and thus remain in the cache. All others
287 * @bdev: which device's cache entries to shrink
290 mb_cache_shrink(struct block_device *bdev)
292 LIST_HEAD(free_list);
293 struct list_head *l, *ltmp;
295 spin_lock(&mb_cache_spinlock);
296 list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
297 struct mb_cache_entry *ce =
298 list_entry(l, struct mb_cache_entry, e_lru_list);
299 if (ce->e_bdev == bdev) {
300 list_move_tail(&ce->e_lru_list, &free_list);
301 __mb_cache_entry_unhash(ce);
304 spin_unlock(&mb_cache_spinlock);
305 list_for_each_safe(l, ltmp, &free_list) {
306 __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
307 e_lru_list), GFP_KERNEL);
315 * Shrinks the cache to its minimum possible size (hopefully 0 entries),
316 * and then destroys it. If this was the last mbcache, un-registers the
317 * mbcache from kernel memory management.
320 mb_cache_destroy(struct mb_cache *cache)
322 LIST_HEAD(free_list);
323 struct list_head *l, *ltmp;
325 spin_lock(&mb_cache_spinlock);
326 list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
327 struct mb_cache_entry *ce =
328 list_entry(l, struct mb_cache_entry, e_lru_list);
329 if (ce->e_cache == cache) {
330 list_move_tail(&ce->e_lru_list, &free_list);
331 __mb_cache_entry_unhash(ce);
334 list_del(&cache->c_cache_list);
335 spin_unlock(&mb_cache_spinlock);
337 list_for_each_safe(l, ltmp, &free_list) {
338 __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
339 e_lru_list), GFP_KERNEL);
342 if (atomic_read(&cache->c_entry_count) > 0) {
343 mb_error("cache %s: %d orphaned entries",
345 atomic_read(&cache->c_entry_count));
348 kmem_cache_destroy(cache->c_entry_cache);
350 kfree(cache->c_index_hash);
351 kfree(cache->c_block_hash);
356 * mb_cache_entry_alloc()
358 * Allocates a new cache entry. The new entry will not be valid initially,
359 * and thus cannot be looked up yet. It should be filled with data, and
360 * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
361 * if no more memory was available.
363 struct mb_cache_entry *
364 mb_cache_entry_alloc(struct mb_cache *cache, gfp_t gfp_flags)
366 struct mb_cache_entry *ce = NULL;
368 if (atomic_read(&cache->c_entry_count) >= cache->c_max_entries) {
369 spin_lock(&mb_cache_spinlock);
370 if (!list_empty(&mb_cache_lru_list)) {
371 ce = list_entry(mb_cache_lru_list.next,
372 struct mb_cache_entry, e_lru_list);
373 list_del_init(&ce->e_lru_list);
374 __mb_cache_entry_unhash(ce);
376 spin_unlock(&mb_cache_spinlock);
379 ce = kmem_cache_alloc(cache->c_entry_cache, gfp_flags);
382 atomic_inc(&cache->c_entry_count);
383 INIT_LIST_HEAD(&ce->e_lru_list);
384 INIT_HLIST_BL_NODE(&ce->e_block_list);
385 INIT_HLIST_BL_NODE(&ce->e_index.o_list);
389 ce->e_block_hash_p = &cache->c_block_hash[0];
390 ce->e_index_hash_p = &cache->c_index_hash[0];
391 ce->e_used = 1 + MB_CACHE_WRITER;
397 * mb_cache_entry_insert()
399 * Inserts an entry that was allocated using mb_cache_entry_alloc() into
400 * the cache. After this, the cache entry can be looked up, but is not yet
401 * in the lru list as the caller still holds a handle to it. Returns 0 on
402 * success, or -EBUSY if a cache entry for that device + inode exists
403 * already (this may happen after a failed lookup, but when another process
404 * has inserted the same cache entry in the meantime).
406 * @bdev: device the cache entry belongs to
407 * @block: block number
411 mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
412 sector_t block, unsigned int key)
414 struct mb_cache *cache = ce->e_cache;
416 struct hlist_bl_node *l;
418 struct hlist_bl_head *block_hash_p;
419 struct hlist_bl_head *index_hash_p;
420 struct mb_cache_entry *lce;
423 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
424 cache->c_bucket_bits);
425 block_hash_p = &cache->c_block_hash[bucket];
426 spin_lock(&mb_cache_spinlock);
427 hlist_bl_for_each_entry(lce, l, block_hash_p, e_block_list) {
428 if (lce->e_bdev == bdev && lce->e_block == block)
431 mb_assert(!__mb_cache_entry_is_block_hashed(ce));
432 __mb_cache_entry_unhash(ce);
435 ce->e_block_hash_p = block_hash_p;
436 ce->e_index.o_key = key;
437 bucket = hash_long(key, cache->c_bucket_bits);
438 index_hash_p = &cache->c_index_hash[bucket];
439 ce->e_index_hash_p = index_hash_p;
440 hlist_bl_add_head(&ce->e_index.o_list, index_hash_p);
441 hlist_bl_add_head(&ce->e_block_list, block_hash_p);
444 spin_unlock(&mb_cache_spinlock);
450 * mb_cache_entry_release()
452 * Release a handle to a cache entry. When the last handle to a cache entry
453 * is released it is either freed (if it is invalid) or otherwise inserted
454 * in to the lru list.
457 mb_cache_entry_release(struct mb_cache_entry *ce)
459 spin_lock(&mb_cache_spinlock);
460 __mb_cache_entry_release_unlock(ce);
465 * mb_cache_entry_free()
467 * This is equivalent to the sequence mb_cache_entry_takeout() --
468 * mb_cache_entry_release().
471 mb_cache_entry_free(struct mb_cache_entry *ce)
473 spin_lock(&mb_cache_spinlock);
474 mb_assert(list_empty(&ce->e_lru_list));
475 __mb_cache_entry_unhash(ce);
476 __mb_cache_entry_release_unlock(ce);
481 * mb_cache_entry_get()
483 * Get a cache entry by device / block number. (There can only be one entry
484 * in the cache per device and block.) Returns NULL if no such cache entry
485 * exists. The returned cache entry is locked for exclusive access ("single
488 struct mb_cache_entry *
489 mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
493 struct hlist_bl_node *l;
494 struct mb_cache_entry *ce;
495 struct hlist_bl_head *block_hash_p;
497 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
498 cache->c_bucket_bits);
499 block_hash_p = &cache->c_block_hash[bucket];
500 spin_lock(&mb_cache_spinlock);
501 hlist_bl_for_each_entry(ce, l, block_hash_p, e_block_list) {
502 mb_assert(ce->e_block_hash_p == block_hash_p);
503 if (ce->e_bdev == bdev && ce->e_block == block) {
506 if (!list_empty(&ce->e_lru_list))
507 list_del_init(&ce->e_lru_list);
509 while (ce->e_used > 0) {
511 prepare_to_wait(&mb_cache_queue, &wait,
512 TASK_UNINTERRUPTIBLE);
513 spin_unlock(&mb_cache_spinlock);
515 spin_lock(&mb_cache_spinlock);
518 finish_wait(&mb_cache_queue, &wait);
519 ce->e_used += 1 + MB_CACHE_WRITER;
521 if (!__mb_cache_entry_is_block_hashed(ce)) {
522 __mb_cache_entry_release_unlock(ce);
531 spin_unlock(&mb_cache_spinlock);
535 #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
537 static struct mb_cache_entry *
538 __mb_cache_entry_find(struct hlist_bl_node *l, struct hlist_bl_head *head,
539 struct block_device *bdev, unsigned int key)
542 struct mb_cache_entry *ce =
543 hlist_bl_entry(l, struct mb_cache_entry,
545 mb_assert(ce->e_index_hash_p == head);
546 if (ce->e_bdev == bdev && ce->e_index.o_key == key) {
549 if (!list_empty(&ce->e_lru_list))
550 list_del_init(&ce->e_lru_list);
552 /* Incrementing before holding the lock gives readers
553 priority over writers. */
555 while (ce->e_used >= MB_CACHE_WRITER) {
557 prepare_to_wait(&mb_cache_queue, &wait,
558 TASK_UNINTERRUPTIBLE);
559 spin_unlock(&mb_cache_spinlock);
561 spin_lock(&mb_cache_spinlock);
564 finish_wait(&mb_cache_queue, &wait);
566 if (!__mb_cache_entry_is_block_hashed(ce)) {
567 __mb_cache_entry_release_unlock(ce);
568 spin_lock(&mb_cache_spinlock);
569 return ERR_PTR(-EAGAIN);
580 * mb_cache_entry_find_first()
582 * Find the first cache entry on a given device with a certain key in
583 * an additional index. Additional matches can be found with
584 * mb_cache_entry_find_next(). Returns NULL if no match was found. The
585 * returned cache entry is locked for shared access ("multiple readers").
587 * @cache: the cache to search
588 * @bdev: the device the cache entry should belong to
589 * @key: the key in the index
591 struct mb_cache_entry *
592 mb_cache_entry_find_first(struct mb_cache *cache, struct block_device *bdev,
595 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
596 struct hlist_bl_node *l;
597 struct mb_cache_entry *ce = NULL;
598 struct hlist_bl_head *index_hash_p;
600 index_hash_p = &cache->c_index_hash[bucket];
601 spin_lock(&mb_cache_spinlock);
602 if (!hlist_bl_empty(index_hash_p)) {
603 l = hlist_bl_first(index_hash_p);
604 ce = __mb_cache_entry_find(l, index_hash_p, bdev, key);
606 spin_unlock(&mb_cache_spinlock);
612 * mb_cache_entry_find_next()
614 * Find the next cache entry on a given device with a certain key in an
615 * additional index. Returns NULL if no match could be found. The previous
616 * entry is atomatically released, so that mb_cache_entry_find_next() can
617 * be called like this:
619 * entry = mb_cache_entry_find_first();
622 * entry = mb_cache_entry_find_next(entry, ...);
625 * @prev: The previous match
626 * @bdev: the device the cache entry should belong to
627 * @key: the key in the index
629 struct mb_cache_entry *
630 mb_cache_entry_find_next(struct mb_cache_entry *prev,
631 struct block_device *bdev, unsigned int key)
633 struct mb_cache *cache = prev->e_cache;
634 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
635 struct hlist_bl_node *l;
636 struct mb_cache_entry *ce;
637 struct hlist_bl_head *index_hash_p;
639 index_hash_p = &cache->c_index_hash[bucket];
640 mb_assert(prev->e_index_hash_p == index_hash_p);
641 spin_lock(&mb_cache_spinlock);
642 mb_assert(!hlist_bl_empty(index_hash_p));
643 l = prev->e_index.o_list.next;
644 ce = __mb_cache_entry_find(l, index_hash_p, bdev, key);
645 __mb_cache_entry_release_unlock(prev);
649 #endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
651 static int __init init_mbcache(void)
653 register_shrinker(&mb_cache_shrinker);
657 static void __exit exit_mbcache(void)
659 unregister_shrinker(&mb_cache_shrinker);
662 module_init(init_mbcache)
663 module_exit(exit_mbcache)