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.
30 * Lock descriptions and usage:
32 * Each hash chain of both the block and index hash tables now contains
33 * a built-in lock used to serialize accesses to the hash chain.
35 * Accesses to global data structures mb_cache_list and mb_cache_lru_list
36 * are serialized via the global spinlock mb_cache_spinlock.
38 * Each mb_cache_entry contains a spinlock, e_entry_lock, to serialize
39 * accesses to its local data, such as e_used and e_queued.
43 * Each block hash chain's lock has the highest lock order, followed by an
44 * index hash chain's lock, mb_cache_bg_lock (used to implement mb_cache_entry's
45 * lock), and mb_cach_spinlock, with the lowest order. While holding
46 * either a block or index hash chain lock, a thread can acquire an
47 * mc_cache_bg_lock, which in turn can also acquire mb_cache_spinlock.
51 * Since both mb_cache_entry_get and mb_cache_entry_find scan the block and
52 * index hash chian, it needs to lock the corresponding hash chain. For each
53 * mb_cache_entry within the chain, it needs to lock the mb_cache_entry to
54 * prevent either any simultaneous release or free on the entry and also
55 * to serialize accesses to either the e_used or e_queued member of the entry.
57 * To avoid having a dangling reference to an already freed
58 * mb_cache_entry, an mb_cache_entry is only freed when it is not on a
59 * block hash chain and also no longer being referenced, both e_used,
60 * and e_queued are 0's. When an mb_cache_entry is explicitly freed it is
61 * first removed from a block hash chain.
64 #include <linux/kernel.h>
65 #include <linux/module.h>
67 #include <linux/hash.h>
70 #include <linux/slab.h>
71 #include <linux/sched.h>
72 #include <linux/list_bl.h>
73 #include <linux/mbcache.h>
74 #include <linux/init.h>
75 #include <linux/blockgroup_lock.h>
78 # define mb_debug(f...) do { \
79 printk(KERN_DEBUG f); \
82 #define mb_assert(c) do { if (!(c)) \
83 printk(KERN_ERR "assertion " #c " failed\n"); \
86 # define mb_debug(f...) do { } while(0)
87 # define mb_assert(c) do { } while(0)
89 #define mb_error(f...) do { \
94 #define MB_CACHE_WRITER ((unsigned short)~0U >> 1)
96 #define MB_CACHE_ENTRY_LOCK_BITS __builtin_log2(NR_BG_LOCKS)
97 #define MB_CACHE_ENTRY_LOCK_INDEX(ce) \
98 (hash_long((unsigned long)ce, MB_CACHE_ENTRY_LOCK_BITS))
100 static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
101 static struct blockgroup_lock *mb_cache_bg_lock;
103 MODULE_AUTHOR("Andreas Gruenbacher <a.gruenbacher@computer.org>");
104 MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
105 MODULE_LICENSE("GPL");
107 EXPORT_SYMBOL(mb_cache_create);
108 EXPORT_SYMBOL(mb_cache_shrink);
109 EXPORT_SYMBOL(mb_cache_destroy);
110 EXPORT_SYMBOL(mb_cache_entry_alloc);
111 EXPORT_SYMBOL(mb_cache_entry_insert);
112 EXPORT_SYMBOL(mb_cache_entry_release);
113 EXPORT_SYMBOL(mb_cache_entry_free);
114 EXPORT_SYMBOL(mb_cache_entry_get);
115 #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
116 EXPORT_SYMBOL(mb_cache_entry_find_first);
117 EXPORT_SYMBOL(mb_cache_entry_find_next);
121 * Global data: list of all mbcache's, lru list, and a spinlock for
122 * accessing cache data structures on SMP machines. The lru list is
123 * global across all mbcaches.
126 static LIST_HEAD(mb_cache_list);
127 static LIST_HEAD(mb_cache_lru_list);
128 static DEFINE_SPINLOCK(mb_cache_spinlock);
131 __spin_lock_mb_cache_entry(struct mb_cache_entry *ce)
133 spin_lock(bgl_lock_ptr(mb_cache_bg_lock,
134 MB_CACHE_ENTRY_LOCK_INDEX(ce)));
138 __spin_unlock_mb_cache_entry(struct mb_cache_entry *ce)
140 spin_unlock(bgl_lock_ptr(mb_cache_bg_lock,
141 MB_CACHE_ENTRY_LOCK_INDEX(ce)));
145 __mb_cache_entry_is_block_hashed(struct mb_cache_entry *ce)
147 return !hlist_bl_unhashed(&ce->e_block_list);
152 __mb_cache_entry_unhash_block(struct mb_cache_entry *ce)
154 if (__mb_cache_entry_is_block_hashed(ce))
155 hlist_bl_del_init(&ce->e_block_list);
159 __mb_cache_entry_is_index_hashed(struct mb_cache_entry *ce)
161 return !hlist_bl_unhashed(&ce->e_index.o_list);
165 __mb_cache_entry_unhash_index(struct mb_cache_entry *ce)
167 if (__mb_cache_entry_is_index_hashed(ce))
168 hlist_bl_del_init(&ce->e_index.o_list);
172 * __mb_cache_entry_unhash_unlock()
174 * This function is called to unhash both the block and index hash
176 * It assumes both the block and index hash chain is locked upon entry.
177 * It also unlock both hash chains both exit
180 __mb_cache_entry_unhash_unlock(struct mb_cache_entry *ce)
182 __mb_cache_entry_unhash_index(ce);
183 hlist_bl_unlock(ce->e_index_hash_p);
184 __mb_cache_entry_unhash_block(ce);
185 hlist_bl_unlock(ce->e_block_hash_p);
189 __mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask)
191 struct mb_cache *cache = ce->e_cache;
193 mb_assert(!(ce->e_used || ce->e_queued || atomic_read(&ce->e_refcnt)));
194 kmem_cache_free(cache->c_entry_cache, ce);
195 atomic_dec(&cache->c_entry_count);
199 __mb_cache_entry_release(struct mb_cache_entry *ce)
201 /* First lock the entry to serialize access to its local data. */
202 __spin_lock_mb_cache_entry(ce);
203 /* Wake up all processes queuing for this cache entry. */
205 wake_up_all(&mb_cache_queue);
206 if (ce->e_used >= MB_CACHE_WRITER)
207 ce->e_used -= MB_CACHE_WRITER;
209 * Make sure that all cache entries on lru_list have
210 * both e_used and e_qued of 0s.
213 if (!(ce->e_used || ce->e_queued || atomic_read(&ce->e_refcnt))) {
214 if (!__mb_cache_entry_is_block_hashed(ce)) {
215 __spin_unlock_mb_cache_entry(ce);
219 * Need access to lru list, first drop entry lock,
220 * then reacquire the lock in the proper order.
222 spin_lock(&mb_cache_spinlock);
223 if (list_empty(&ce->e_lru_list))
224 list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
225 spin_unlock(&mb_cache_spinlock);
227 __spin_unlock_mb_cache_entry(ce);
230 mb_assert(list_empty(&ce->e_lru_list));
231 __mb_cache_entry_forget(ce, GFP_KERNEL);
235 * mb_cache_shrink_scan() memory pressure callback
237 * This function is called by the kernel memory management when memory
241 * @sc: shrink_control passed from reclaim
243 * Returns the number of objects freed.
246 mb_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
248 LIST_HEAD(free_list);
249 struct mb_cache_entry *entry, *tmp;
250 int nr_to_scan = sc->nr_to_scan;
251 gfp_t gfp_mask = sc->gfp_mask;
252 unsigned long freed = 0;
254 mb_debug("trying to free %d entries", nr_to_scan);
255 spin_lock(&mb_cache_spinlock);
256 while ((nr_to_scan-- > 0) && !list_empty(&mb_cache_lru_list)) {
257 struct mb_cache_entry *ce =
258 list_entry(mb_cache_lru_list.next,
259 struct mb_cache_entry, e_lru_list);
260 list_del_init(&ce->e_lru_list);
261 if (ce->e_used || ce->e_queued || atomic_read(&ce->e_refcnt))
263 spin_unlock(&mb_cache_spinlock);
264 /* Prevent any find or get operation on the entry */
265 hlist_bl_lock(ce->e_block_hash_p);
266 hlist_bl_lock(ce->e_index_hash_p);
267 /* Ignore if it is touched by a find/get */
268 if (ce->e_used || ce->e_queued || atomic_read(&ce->e_refcnt) ||
269 !list_empty(&ce->e_lru_list)) {
270 hlist_bl_unlock(ce->e_index_hash_p);
271 hlist_bl_unlock(ce->e_block_hash_p);
272 spin_lock(&mb_cache_spinlock);
275 __mb_cache_entry_unhash_unlock(ce);
276 list_add_tail(&ce->e_lru_list, &free_list);
277 spin_lock(&mb_cache_spinlock);
279 spin_unlock(&mb_cache_spinlock);
281 list_for_each_entry_safe(entry, tmp, &free_list, e_lru_list) {
282 __mb_cache_entry_forget(entry, gfp_mask);
289 mb_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
291 struct mb_cache *cache;
292 unsigned long count = 0;
294 spin_lock(&mb_cache_spinlock);
295 list_for_each_entry(cache, &mb_cache_list, c_cache_list) {
296 mb_debug("cache %s (%d)", cache->c_name,
297 atomic_read(&cache->c_entry_count));
298 count += atomic_read(&cache->c_entry_count);
300 spin_unlock(&mb_cache_spinlock);
302 return vfs_pressure_ratio(count);
305 static struct shrinker mb_cache_shrinker = {
306 .count_objects = mb_cache_shrink_count,
307 .scan_objects = mb_cache_shrink_scan,
308 .seeks = DEFAULT_SEEKS,
312 * mb_cache_create() create a new cache
314 * All entries in one cache are equal size. Cache entries may be from
315 * multiple devices. If this is the first mbcache created, registers
316 * the cache with kernel memory management. Returns NULL if no more
317 * memory was available.
319 * @name: name of the cache (informal)
320 * @bucket_bits: log2(number of hash buckets)
323 mb_cache_create(const char *name, int bucket_bits)
325 int n, bucket_count = 1 << bucket_bits;
326 struct mb_cache *cache = NULL;
328 if (!mb_cache_bg_lock) {
329 mb_cache_bg_lock = kmalloc(sizeof(struct blockgroup_lock),
331 if (!mb_cache_bg_lock)
333 bgl_lock_init(mb_cache_bg_lock);
336 cache = kmalloc(sizeof(struct mb_cache), GFP_KERNEL);
339 cache->c_name = name;
340 atomic_set(&cache->c_entry_count, 0);
341 cache->c_bucket_bits = bucket_bits;
342 cache->c_block_hash = kmalloc(bucket_count *
343 sizeof(struct hlist_bl_head), GFP_KERNEL);
344 if (!cache->c_block_hash)
346 for (n=0; n<bucket_count; n++)
347 INIT_HLIST_BL_HEAD(&cache->c_block_hash[n]);
348 cache->c_index_hash = kmalloc(bucket_count *
349 sizeof(struct hlist_bl_head), GFP_KERNEL);
350 if (!cache->c_index_hash)
352 for (n=0; n<bucket_count; n++)
353 INIT_HLIST_BL_HEAD(&cache->c_index_hash[n]);
354 cache->c_entry_cache = kmem_cache_create(name,
355 sizeof(struct mb_cache_entry), 0,
356 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
357 if (!cache->c_entry_cache)
361 * Set an upper limit on the number of cache entries so that the hash
362 * chains won't grow too long.
364 cache->c_max_entries = bucket_count << 4;
366 spin_lock(&mb_cache_spinlock);
367 list_add(&cache->c_cache_list, &mb_cache_list);
368 spin_unlock(&mb_cache_spinlock);
372 kfree(cache->c_index_hash);
375 kfree(cache->c_block_hash);
384 * Removes all cache entries of a device from the cache. All cache entries
385 * currently in use cannot be freed, and thus remain in the cache. All others
388 * @bdev: which device's cache entries to shrink
391 mb_cache_shrink(struct block_device *bdev)
393 LIST_HEAD(free_list);
395 struct mb_cache_entry *ce, *tmp;
397 l = &mb_cache_lru_list;
398 spin_lock(&mb_cache_spinlock);
399 while (!list_is_last(l, &mb_cache_lru_list)) {
401 ce = list_entry(l, struct mb_cache_entry, e_lru_list);
402 if (ce->e_bdev == bdev) {
403 list_del_init(&ce->e_lru_list);
404 if (ce->e_used || ce->e_queued ||
405 atomic_read(&ce->e_refcnt))
407 spin_unlock(&mb_cache_spinlock);
409 * Prevent any find or get operation on the entry.
411 hlist_bl_lock(ce->e_block_hash_p);
412 hlist_bl_lock(ce->e_index_hash_p);
413 /* Ignore if it is touched by a find/get */
414 if (ce->e_used || ce->e_queued ||
415 atomic_read(&ce->e_refcnt) ||
416 !list_empty(&ce->e_lru_list)) {
417 hlist_bl_unlock(ce->e_index_hash_p);
418 hlist_bl_unlock(ce->e_block_hash_p);
419 l = &mb_cache_lru_list;
420 spin_lock(&mb_cache_spinlock);
423 __mb_cache_entry_unhash_unlock(ce);
424 mb_assert(!(ce->e_used || ce->e_queued ||
425 atomic_read(&ce->e_refcnt)));
426 list_add_tail(&ce->e_lru_list, &free_list);
427 l = &mb_cache_lru_list;
428 spin_lock(&mb_cache_spinlock);
431 spin_unlock(&mb_cache_spinlock);
433 list_for_each_entry_safe(ce, tmp, &free_list, e_lru_list) {
434 __mb_cache_entry_forget(ce, GFP_KERNEL);
442 * Shrinks the cache to its minimum possible size (hopefully 0 entries),
443 * and then destroys it. If this was the last mbcache, un-registers the
444 * mbcache from kernel memory management.
447 mb_cache_destroy(struct mb_cache *cache)
449 LIST_HEAD(free_list);
450 struct mb_cache_entry *ce, *tmp;
452 spin_lock(&mb_cache_spinlock);
453 list_for_each_entry_safe(ce, tmp, &mb_cache_lru_list, e_lru_list) {
454 if (ce->e_cache == cache)
455 list_move_tail(&ce->e_lru_list, &free_list);
457 list_del(&cache->c_cache_list);
458 spin_unlock(&mb_cache_spinlock);
460 list_for_each_entry_safe(ce, tmp, &free_list, e_lru_list) {
461 list_del_init(&ce->e_lru_list);
463 * Prevent any find or get operation on the entry.
465 hlist_bl_lock(ce->e_block_hash_p);
466 hlist_bl_lock(ce->e_index_hash_p);
467 mb_assert(!(ce->e_used || ce->e_queued ||
468 atomic_read(&ce->e_refcnt)));
469 __mb_cache_entry_unhash_unlock(ce);
470 __mb_cache_entry_forget(ce, GFP_KERNEL);
473 if (atomic_read(&cache->c_entry_count) > 0) {
474 mb_error("cache %s: %d orphaned entries",
476 atomic_read(&cache->c_entry_count));
479 kfree(cache->c_index_hash);
480 kfree(cache->c_block_hash);
485 * mb_cache_entry_alloc()
487 * Allocates a new cache entry. The new entry will not be valid initially,
488 * and thus cannot be looked up yet. It should be filled with data, and
489 * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
490 * if no more memory was available.
492 struct mb_cache_entry *
493 mb_cache_entry_alloc(struct mb_cache *cache, gfp_t gfp_flags)
495 struct mb_cache_entry *ce;
497 if (atomic_read(&cache->c_entry_count) >= cache->c_max_entries) {
500 l = &mb_cache_lru_list;
501 spin_lock(&mb_cache_spinlock);
502 while (!list_is_last(l, &mb_cache_lru_list)) {
504 ce = list_entry(l, struct mb_cache_entry, e_lru_list);
505 if (ce->e_cache == cache) {
506 list_del_init(&ce->e_lru_list);
507 if (ce->e_used || ce->e_queued ||
508 atomic_read(&ce->e_refcnt))
510 spin_unlock(&mb_cache_spinlock);
512 * Prevent any find or get operation on the
515 hlist_bl_lock(ce->e_block_hash_p);
516 hlist_bl_lock(ce->e_index_hash_p);
517 /* Ignore if it is touched by a find/get */
518 if (ce->e_used || ce->e_queued ||
519 atomic_read(&ce->e_refcnt) ||
520 !list_empty(&ce->e_lru_list)) {
521 hlist_bl_unlock(ce->e_index_hash_p);
522 hlist_bl_unlock(ce->e_block_hash_p);
523 l = &mb_cache_lru_list;
524 spin_lock(&mb_cache_spinlock);
527 mb_assert(list_empty(&ce->e_lru_list));
528 mb_assert(!(ce->e_used || ce->e_queued ||
529 atomic_read(&ce->e_refcnt)));
530 __mb_cache_entry_unhash_unlock(ce);
534 spin_unlock(&mb_cache_spinlock);
537 ce = kmem_cache_alloc(cache->c_entry_cache, gfp_flags);
540 atomic_inc(&cache->c_entry_count);
541 INIT_LIST_HEAD(&ce->e_lru_list);
542 INIT_HLIST_BL_NODE(&ce->e_block_list);
543 INIT_HLIST_BL_NODE(&ce->e_index.o_list);
546 atomic_set(&ce->e_refcnt, 0);
548 ce->e_block_hash_p = &cache->c_block_hash[0];
549 ce->e_index_hash_p = &cache->c_index_hash[0];
550 ce->e_used = 1 + MB_CACHE_WRITER;
556 * mb_cache_entry_insert()
558 * Inserts an entry that was allocated using mb_cache_entry_alloc() into
559 * the cache. After this, the cache entry can be looked up, but is not yet
560 * in the lru list as the caller still holds a handle to it. Returns 0 on
561 * success, or -EBUSY if a cache entry for that device + inode exists
562 * already (this may happen after a failed lookup, but when another process
563 * has inserted the same cache entry in the meantime).
565 * @bdev: device the cache entry belongs to
566 * @block: block number
570 mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
571 sector_t block, unsigned int key)
573 struct mb_cache *cache = ce->e_cache;
575 struct hlist_bl_node *l;
576 struct hlist_bl_head *block_hash_p;
577 struct hlist_bl_head *index_hash_p;
578 struct mb_cache_entry *lce;
581 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
582 cache->c_bucket_bits);
583 block_hash_p = &cache->c_block_hash[bucket];
584 hlist_bl_lock(block_hash_p);
585 hlist_bl_for_each_entry(lce, l, block_hash_p, e_block_list) {
586 if (lce->e_bdev == bdev && lce->e_block == block) {
587 hlist_bl_unlock(block_hash_p);
591 mb_assert(!__mb_cache_entry_is_block_hashed(ce));
592 __mb_cache_entry_unhash_block(ce);
593 __mb_cache_entry_unhash_index(ce);
596 ce->e_block_hash_p = block_hash_p;
597 ce->e_index.o_key = key;
598 hlist_bl_add_head(&ce->e_block_list, block_hash_p);
599 hlist_bl_unlock(block_hash_p);
600 bucket = hash_long(key, cache->c_bucket_bits);
601 index_hash_p = &cache->c_index_hash[bucket];
602 hlist_bl_lock(index_hash_p);
603 ce->e_index_hash_p = index_hash_p;
604 hlist_bl_add_head(&ce->e_index.o_list, index_hash_p);
605 hlist_bl_unlock(index_hash_p);
611 * mb_cache_entry_release()
613 * Release a handle to a cache entry. When the last handle to a cache entry
614 * is released it is either freed (if it is invalid) or otherwise inserted
615 * in to the lru list.
618 mb_cache_entry_release(struct mb_cache_entry *ce)
620 __mb_cache_entry_release(ce);
625 * mb_cache_entry_free()
629 mb_cache_entry_free(struct mb_cache_entry *ce)
632 mb_assert(list_empty(&ce->e_lru_list));
633 hlist_bl_lock(ce->e_index_hash_p);
634 __mb_cache_entry_unhash_index(ce);
635 hlist_bl_unlock(ce->e_index_hash_p);
636 hlist_bl_lock(ce->e_block_hash_p);
637 __mb_cache_entry_unhash_block(ce);
638 hlist_bl_unlock(ce->e_block_hash_p);
639 __mb_cache_entry_release(ce);
644 * mb_cache_entry_get()
646 * Get a cache entry by device / block number. (There can only be one entry
647 * in the cache per device and block.) Returns NULL if no such cache entry
648 * exists. The returned cache entry is locked for exclusive access ("single
651 struct mb_cache_entry *
652 mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
656 struct hlist_bl_node *l;
657 struct mb_cache_entry *ce;
658 struct hlist_bl_head *block_hash_p;
660 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
661 cache->c_bucket_bits);
662 block_hash_p = &cache->c_block_hash[bucket];
663 /* First serialize access to the block corresponding hash chain. */
664 hlist_bl_lock(block_hash_p);
665 hlist_bl_for_each_entry(ce, l, block_hash_p, e_block_list) {
666 mb_assert(ce->e_block_hash_p == block_hash_p);
667 if (ce->e_bdev == bdev && ce->e_block == block) {
669 * Prevent a free from removing the entry.
671 atomic_inc(&ce->e_refcnt);
672 hlist_bl_unlock(block_hash_p);
673 __spin_lock_mb_cache_entry(ce);
674 atomic_dec(&ce->e_refcnt);
675 if (ce->e_used > 0) {
677 while (ce->e_used > 0) {
679 prepare_to_wait(&mb_cache_queue, &wait,
680 TASK_UNINTERRUPTIBLE);
681 __spin_unlock_mb_cache_entry(ce);
683 __spin_lock_mb_cache_entry(ce);
686 finish_wait(&mb_cache_queue, &wait);
688 ce->e_used += 1 + MB_CACHE_WRITER;
689 __spin_unlock_mb_cache_entry(ce);
691 if (!list_empty(&ce->e_lru_list)) {
692 spin_lock(&mb_cache_spinlock);
693 list_del_init(&ce->e_lru_list);
694 spin_unlock(&mb_cache_spinlock);
696 if (!__mb_cache_entry_is_block_hashed(ce)) {
697 __mb_cache_entry_release(ce);
703 hlist_bl_unlock(block_hash_p);
707 #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
709 static struct mb_cache_entry *
710 __mb_cache_entry_find(struct hlist_bl_node *l, struct hlist_bl_head *head,
711 struct block_device *bdev, unsigned int key)
714 /* The index hash chain is alredy acquire by caller. */
716 struct mb_cache_entry *ce =
717 hlist_bl_entry(l, struct mb_cache_entry,
719 mb_assert(ce->e_index_hash_p == head);
720 if (ce->e_bdev == bdev && ce->e_index.o_key == key) {
722 * Prevent a free from removing the entry.
724 atomic_inc(&ce->e_refcnt);
725 hlist_bl_unlock(head);
726 __spin_lock_mb_cache_entry(ce);
727 atomic_dec(&ce->e_refcnt);
729 /* Incrementing before holding the lock gives readers
730 priority over writers. */
731 if (ce->e_used >= MB_CACHE_WRITER) {
734 while (ce->e_used >= MB_CACHE_WRITER) {
736 prepare_to_wait(&mb_cache_queue, &wait,
737 TASK_UNINTERRUPTIBLE);
738 __spin_unlock_mb_cache_entry(ce);
740 __spin_lock_mb_cache_entry(ce);
743 finish_wait(&mb_cache_queue, &wait);
745 __spin_unlock_mb_cache_entry(ce);
746 if (!list_empty(&ce->e_lru_list)) {
747 spin_lock(&mb_cache_spinlock);
748 list_del_init(&ce->e_lru_list);
749 spin_unlock(&mb_cache_spinlock);
751 if (!__mb_cache_entry_is_block_hashed(ce)) {
752 __mb_cache_entry_release(ce);
753 return ERR_PTR(-EAGAIN);
759 hlist_bl_unlock(head);
765 * mb_cache_entry_find_first()
767 * Find the first cache entry on a given device with a certain key in
768 * an additional index. Additional matches can be found with
769 * mb_cache_entry_find_next(). Returns NULL if no match was found. The
770 * returned cache entry is locked for shared access ("multiple readers").
772 * @cache: the cache to search
773 * @bdev: the device the cache entry should belong to
774 * @key: the key in the index
776 struct mb_cache_entry *
777 mb_cache_entry_find_first(struct mb_cache *cache, struct block_device *bdev,
780 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
781 struct hlist_bl_node *l;
782 struct mb_cache_entry *ce = NULL;
783 struct hlist_bl_head *index_hash_p;
785 index_hash_p = &cache->c_index_hash[bucket];
786 hlist_bl_lock(index_hash_p);
787 if (!hlist_bl_empty(index_hash_p)) {
788 l = hlist_bl_first(index_hash_p);
789 ce = __mb_cache_entry_find(l, index_hash_p, bdev, key);
791 hlist_bl_unlock(index_hash_p);
797 * mb_cache_entry_find_next()
799 * Find the next cache entry on a given device with a certain key in an
800 * additional index. Returns NULL if no match could be found. The previous
801 * entry is atomatically released, so that mb_cache_entry_find_next() can
802 * be called like this:
804 * entry = mb_cache_entry_find_first();
807 * entry = mb_cache_entry_find_next(entry, ...);
810 * @prev: The previous match
811 * @bdev: the device the cache entry should belong to
812 * @key: the key in the index
814 struct mb_cache_entry *
815 mb_cache_entry_find_next(struct mb_cache_entry *prev,
816 struct block_device *bdev, unsigned int key)
818 struct mb_cache *cache = prev->e_cache;
819 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
820 struct hlist_bl_node *l;
821 struct mb_cache_entry *ce;
822 struct hlist_bl_head *index_hash_p;
824 index_hash_p = &cache->c_index_hash[bucket];
825 mb_assert(prev->e_index_hash_p == index_hash_p);
826 hlist_bl_lock(index_hash_p);
827 mb_assert(!hlist_bl_empty(index_hash_p));
828 l = prev->e_index.o_list.next;
829 ce = __mb_cache_entry_find(l, index_hash_p, bdev, key);
830 __mb_cache_entry_release(prev);
834 #endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
836 static int __init init_mbcache(void)
838 register_shrinker(&mb_cache_shrinker);
842 static void __exit exit_mbcache(void)
844 unregister_shrinker(&mb_cache_shrinker);
847 module_init(init_mbcache)
848 module_exit(exit_mbcache)