blob: cf4e6cdfd15b5afc091c0f2a060af90450176811 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/mbcache.c
3 * (C) 2001-2002 Andreas Gruenbacher, <a.gruenbacher@computer.org>
4 */
5
6/*
7 * Filesystem Meta Information Block Cache (mbcache)
8 *
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).
12 *
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.
17 *
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
21 * or lists.
22 *
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.
27 */
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31
32#include <linux/hash.h>
33#include <linux/fs.h>
34#include <linux/mm.h>
35#include <linux/slab.h>
36#include <linux/sched.h>
37#include <linux/init.h>
38#include <linux/mbcache.h>
39
40
41#ifdef MB_CACHE_DEBUG
42# define mb_debug(f...) do { \
43 printk(KERN_DEBUG f); \
44 printk("\n"); \
45 } while (0)
46#define mb_assert(c) do { if (!(c)) \
47 printk(KERN_ERR "assertion " #c " failed\n"); \
48 } while(0)
49#else
50# define mb_debug(f...) do { } while(0)
51# define mb_assert(c) do { } while(0)
52#endif
53#define mb_error(f...) do { \
54 printk(KERN_ERR f); \
55 printk("\n"); \
56 } while(0)
57
58#define MB_CACHE_WRITER ((unsigned short)~0U >> 1)
59
Adrian Bunk75c96f82005-05-05 16:16:09 -070060static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62MODULE_AUTHOR("Andreas Gruenbacher <a.gruenbacher@computer.org>");
63MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
64MODULE_LICENSE("GPL");
65
66EXPORT_SYMBOL(mb_cache_create);
67EXPORT_SYMBOL(mb_cache_shrink);
68EXPORT_SYMBOL(mb_cache_destroy);
69EXPORT_SYMBOL(mb_cache_entry_alloc);
70EXPORT_SYMBOL(mb_cache_entry_insert);
71EXPORT_SYMBOL(mb_cache_entry_release);
72EXPORT_SYMBOL(mb_cache_entry_free);
73EXPORT_SYMBOL(mb_cache_entry_get);
74#if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
75EXPORT_SYMBOL(mb_cache_entry_find_first);
76EXPORT_SYMBOL(mb_cache_entry_find_next);
77#endif
78
79struct mb_cache {
80 struct list_head c_cache_list;
81 const char *c_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 atomic_t c_entry_count;
83 int c_bucket_bits;
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +020084 struct kmem_cache *c_entry_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct list_head *c_block_hash;
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +020086 struct list_head *c_index_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
89
90/*
91 * Global data: list of all mbcache's, lru list, and a spinlock for
92 * accessing cache data structures on SMP machines. The lru list is
93 * global across all mbcaches.
94 */
95
96static LIST_HEAD(mb_cache_list);
97static LIST_HEAD(mb_cache_lru_list);
98static DEFINE_SPINLOCK(mb_cache_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * What the mbcache registers as to get shrunk dynamically.
102 */
103
Dave Chinner7f8275d2010-07-19 14:56:17 +1000104static int mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Rusty Russell8e1f9362007-07-17 04:03:17 -0700106static struct shrinker mb_cache_shrinker = {
107 .shrink = mb_cache_shrink_fn,
108 .seeks = DEFAULT_SEEKS,
109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static inline int
112__mb_cache_entry_is_hashed(struct mb_cache_entry *ce)
113{
114 return !list_empty(&ce->e_block_list);
115}
116
117
Arjan van de Ven858119e2006-01-14 13:20:43 -0800118static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119__mb_cache_entry_unhash(struct mb_cache_entry *ce)
120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (__mb_cache_entry_is_hashed(ce)) {
122 list_del_init(&ce->e_block_list);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200123 list_del(&ce->e_index.o_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125}
126
127
Arjan van de Ven858119e2006-01-14 13:20:43 -0800128static void
Al Viro27496a82005-10-21 03:20:48 -0400129__mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct mb_cache *cache = ce->e_cache;
132
133 mb_assert(!(ce->e_used || ce->e_queued));
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200134 kmem_cache_free(cache->c_entry_cache, ce);
135 atomic_dec(&cache->c_entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138
Arjan van de Ven858119e2006-01-14 13:20:43 -0800139static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140__mb_cache_entry_release_unlock(struct mb_cache_entry *ce)
Josh Triplett58f555e2006-09-29 01:59:24 -0700141 __releases(mb_cache_spinlock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 /* Wake up all processes queuing for this cache entry. */
144 if (ce->e_queued)
145 wake_up_all(&mb_cache_queue);
146 if (ce->e_used >= MB_CACHE_WRITER)
147 ce->e_used -= MB_CACHE_WRITER;
148 ce->e_used--;
149 if (!(ce->e_used || ce->e_queued)) {
150 if (!__mb_cache_entry_is_hashed(ce))
151 goto forget;
152 mb_assert(list_empty(&ce->e_lru_list));
153 list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
154 }
155 spin_unlock(&mb_cache_spinlock);
156 return;
157forget:
158 spin_unlock(&mb_cache_spinlock);
159 __mb_cache_entry_forget(ce, GFP_KERNEL);
160}
161
162
163/*
164 * mb_cache_shrink_fn() memory pressure callback
165 *
166 * This function is called by the kernel memory management when memory
167 * gets low.
168 *
Dave Chinner7f8275d2010-07-19 14:56:17 +1000169 * @shrink: (ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * @nr_to_scan: Number of objects to scan
171 * @gfp_mask: (ignored)
172 *
173 * Returns the number of objects which are present in the cache.
174 */
175static int
Dave Chinner7f8275d2010-07-19 14:56:17 +1000176mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 LIST_HEAD(free_list);
Andreas Gruenbachere566d482010-07-21 19:44:45 +0200179 struct mb_cache *cache;
180 struct mb_cache_entry *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int count = 0;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 mb_debug("trying to free %d entries", nr_to_scan);
Andreas Gruenbachere566d482010-07-21 19:44:45 +0200184 spin_lock(&mb_cache_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) {
186 struct mb_cache_entry *ce =
187 list_entry(mb_cache_lru_list.next,
188 struct mb_cache_entry, e_lru_list);
189 list_move_tail(&ce->e_lru_list, &free_list);
190 __mb_cache_entry_unhash(ce);
191 }
Andreas Gruenbachere566d482010-07-21 19:44:45 +0200192 list_for_each_entry(cache, &mb_cache_list, c_cache_list) {
193 mb_debug("cache %s (%d)", cache->c_name,
194 atomic_read(&cache->c_entry_count));
195 count += atomic_read(&cache->c_entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Andreas Gruenbachere566d482010-07-21 19:44:45 +0200197 spin_unlock(&mb_cache_spinlock);
198 list_for_each_entry_safe(entry, tmp, &free_list, e_lru_list) {
199 __mb_cache_entry_forget(entry, gfp_mask);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return (count / 100) * sysctl_vfs_cache_pressure;
202}
203
204
205/*
206 * mb_cache_create() create a new cache
207 *
208 * All entries in one cache are equal size. Cache entries may be from
209 * multiple devices. If this is the first mbcache created, registers
210 * the cache with kernel memory management. Returns NULL if no more
211 * memory was available.
212 *
213 * @name: name of the cache (informal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * @bucket_bits: log2(number of hash buckets)
215 */
216struct mb_cache *
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200217mb_cache_create(const char *name, int bucket_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200219 int n, bucket_count = 1 << bucket_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct mb_cache *cache = NULL;
221
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200222 cache = kmalloc(sizeof(struct mb_cache), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!cache)
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 cache->c_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 atomic_set(&cache->c_entry_count, 0);
227 cache->c_bucket_bits = bucket_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head),
229 GFP_KERNEL);
230 if (!cache->c_block_hash)
231 goto fail;
232 for (n=0; n<bucket_count; n++)
233 INIT_LIST_HEAD(&cache->c_block_hash[n]);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200234 cache->c_index_hash = kmalloc(bucket_count * sizeof(struct list_head),
235 GFP_KERNEL);
236 if (!cache->c_index_hash)
237 goto fail;
238 for (n=0; n<bucket_count; n++)
239 INIT_LIST_HEAD(&cache->c_index_hash[n]);
240 cache->c_entry_cache = kmem_cache_create(name,
241 sizeof(struct mb_cache_entry), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900242 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!cache->c_entry_cache)
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200244 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 spin_lock(&mb_cache_spinlock);
247 list_add(&cache->c_cache_list, &mb_cache_list);
248 spin_unlock(&mb_cache_spinlock);
249 return cache;
250
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200251fail2:
252 kfree(cache->c_index_hash);
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254fail:
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200255 kfree(cache->c_block_hash);
256 kfree(cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return NULL;
258}
259
260
261/*
262 * mb_cache_shrink()
263 *
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800264 * Removes all cache entries of a device from the cache. All cache entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * currently in use cannot be freed, and thus remain in the cache. All others
266 * are freed.
267 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * @bdev: which device's cache entries to shrink
269 */
270void
Andreas Gruenbacher8c52ab42005-07-27 11:45:15 -0700271mb_cache_shrink(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 LIST_HEAD(free_list);
274 struct list_head *l, *ltmp;
275
276 spin_lock(&mb_cache_spinlock);
277 list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
278 struct mb_cache_entry *ce =
279 list_entry(l, struct mb_cache_entry, e_lru_list);
280 if (ce->e_bdev == bdev) {
281 list_move_tail(&ce->e_lru_list, &free_list);
282 __mb_cache_entry_unhash(ce);
283 }
284 }
285 spin_unlock(&mb_cache_spinlock);
286 list_for_each_safe(l, ltmp, &free_list) {
287 __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
288 e_lru_list), GFP_KERNEL);
289 }
290}
291
292
293/*
294 * mb_cache_destroy()
295 *
296 * Shrinks the cache to its minimum possible size (hopefully 0 entries),
297 * and then destroys it. If this was the last mbcache, un-registers the
298 * mbcache from kernel memory management.
299 */
300void
301mb_cache_destroy(struct mb_cache *cache)
302{
303 LIST_HEAD(free_list);
304 struct list_head *l, *ltmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 spin_lock(&mb_cache_spinlock);
307 list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
308 struct mb_cache_entry *ce =
309 list_entry(l, struct mb_cache_entry, e_lru_list);
310 if (ce->e_cache == cache) {
311 list_move_tail(&ce->e_lru_list, &free_list);
312 __mb_cache_entry_unhash(ce);
313 }
314 }
315 list_del(&cache->c_cache_list);
316 spin_unlock(&mb_cache_spinlock);
317
318 list_for_each_safe(l, ltmp, &free_list) {
319 __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
320 e_lru_list), GFP_KERNEL);
321 }
322
323 if (atomic_read(&cache->c_entry_count) > 0) {
324 mb_error("cache %s: %d orphaned entries",
325 cache->c_name,
326 atomic_read(&cache->c_entry_count));
327 }
328
329 kmem_cache_destroy(cache->c_entry_cache);
330
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200331 kfree(cache->c_index_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 kfree(cache->c_block_hash);
333 kfree(cache);
334}
335
336
337/*
338 * mb_cache_entry_alloc()
339 *
340 * Allocates a new cache entry. The new entry will not be valid initially,
341 * and thus cannot be looked up yet. It should be filled with data, and
342 * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
343 * if no more memory was available.
344 */
345struct mb_cache_entry *
Jan Kara335e92e2008-04-15 14:34:43 -0700346mb_cache_entry_alloc(struct mb_cache *cache, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct mb_cache_entry *ce;
349
Jan Kara335e92e2008-04-15 14:34:43 -0700350 ce = kmem_cache_alloc(cache->c_entry_cache, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (ce) {
Ram Guptaf9e83482007-10-25 10:03:28 -0500352 atomic_inc(&cache->c_entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 INIT_LIST_HEAD(&ce->e_lru_list);
354 INIT_LIST_HEAD(&ce->e_block_list);
355 ce->e_cache = cache;
356 ce->e_used = 1 + MB_CACHE_WRITER;
357 ce->e_queued = 0;
358 }
359 return ce;
360}
361
362
363/*
364 * mb_cache_entry_insert()
365 *
366 * Inserts an entry that was allocated using mb_cache_entry_alloc() into
367 * the cache. After this, the cache entry can be looked up, but is not yet
368 * in the lru list as the caller still holds a handle to it. Returns 0 on
369 * success, or -EBUSY if a cache entry for that device + inode exists
370 * already (this may happen after a failed lookup, but when another process
371 * has inserted the same cache entry in the meantime).
372 *
373 * @bdev: device the cache entry belongs to
374 * @block: block number
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200375 * @key: lookup key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
377int
378mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200379 sector_t block, unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct mb_cache *cache = ce->e_cache;
382 unsigned int bucket;
383 struct list_head *l;
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200384 int error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
387 cache->c_bucket_bits);
388 spin_lock(&mb_cache_spinlock);
389 list_for_each_prev(l, &cache->c_block_hash[bucket]) {
390 struct mb_cache_entry *ce =
391 list_entry(l, struct mb_cache_entry, e_block_list);
392 if (ce->e_bdev == bdev && ce->e_block == block)
393 goto out;
394 }
395 __mb_cache_entry_unhash(ce);
396 ce->e_bdev = bdev;
397 ce->e_block = block;
398 list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200399 ce->e_index.o_key = key;
400 bucket = hash_long(key, cache->c_bucket_bits);
401 list_add(&ce->e_index.o_list, &cache->c_index_hash[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 error = 0;
403out:
404 spin_unlock(&mb_cache_spinlock);
405 return error;
406}
407
408
409/*
410 * mb_cache_entry_release()
411 *
412 * Release a handle to a cache entry. When the last handle to a cache entry
413 * is released it is either freed (if it is invalid) or otherwise inserted
414 * in to the lru list.
415 */
416void
417mb_cache_entry_release(struct mb_cache_entry *ce)
418{
419 spin_lock(&mb_cache_spinlock);
420 __mb_cache_entry_release_unlock(ce);
421}
422
423
424/*
425 * mb_cache_entry_free()
426 *
427 * This is equivalent to the sequence mb_cache_entry_takeout() --
428 * mb_cache_entry_release().
429 */
430void
431mb_cache_entry_free(struct mb_cache_entry *ce)
432{
433 spin_lock(&mb_cache_spinlock);
434 mb_assert(list_empty(&ce->e_lru_list));
435 __mb_cache_entry_unhash(ce);
436 __mb_cache_entry_release_unlock(ce);
437}
438
439
440/*
441 * mb_cache_entry_get()
442 *
443 * Get a cache entry by device / block number. (There can only be one entry
444 * in the cache per device and block.) Returns NULL if no such cache entry
445 * exists. The returned cache entry is locked for exclusive access ("single
446 * writer").
447 */
448struct mb_cache_entry *
449mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
450 sector_t block)
451{
452 unsigned int bucket;
453 struct list_head *l;
454 struct mb_cache_entry *ce;
455
456 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
457 cache->c_bucket_bits);
458 spin_lock(&mb_cache_spinlock);
459 list_for_each(l, &cache->c_block_hash[bucket]) {
460 ce = list_entry(l, struct mb_cache_entry, e_block_list);
461 if (ce->e_bdev == bdev && ce->e_block == block) {
462 DEFINE_WAIT(wait);
463
464 if (!list_empty(&ce->e_lru_list))
465 list_del_init(&ce->e_lru_list);
466
467 while (ce->e_used > 0) {
468 ce->e_queued++;
469 prepare_to_wait(&mb_cache_queue, &wait,
470 TASK_UNINTERRUPTIBLE);
471 spin_unlock(&mb_cache_spinlock);
472 schedule();
473 spin_lock(&mb_cache_spinlock);
474 ce->e_queued--;
475 }
476 finish_wait(&mb_cache_queue, &wait);
477 ce->e_used += 1 + MB_CACHE_WRITER;
478
479 if (!__mb_cache_entry_is_hashed(ce)) {
480 __mb_cache_entry_release_unlock(ce);
481 return NULL;
482 }
483 goto cleanup;
484 }
485 }
486 ce = NULL;
487
488cleanup:
489 spin_unlock(&mb_cache_spinlock);
490 return ce;
491}
492
493#if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
494
495static struct mb_cache_entry *
496__mb_cache_entry_find(struct list_head *l, struct list_head *head,
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200497 struct block_device *bdev, unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 while (l != head) {
500 struct mb_cache_entry *ce =
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200501 list_entry(l, struct mb_cache_entry, e_index.o_list);
502 if (ce->e_bdev == bdev && ce->e_index.o_key == key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 DEFINE_WAIT(wait);
504
505 if (!list_empty(&ce->e_lru_list))
506 list_del_init(&ce->e_lru_list);
507
508 /* Incrementing before holding the lock gives readers
509 priority over writers. */
510 ce->e_used++;
511 while (ce->e_used >= MB_CACHE_WRITER) {
512 ce->e_queued++;
513 prepare_to_wait(&mb_cache_queue, &wait,
514 TASK_UNINTERRUPTIBLE);
515 spin_unlock(&mb_cache_spinlock);
516 schedule();
517 spin_lock(&mb_cache_spinlock);
518 ce->e_queued--;
519 }
520 finish_wait(&mb_cache_queue, &wait);
521
522 if (!__mb_cache_entry_is_hashed(ce)) {
523 __mb_cache_entry_release_unlock(ce);
524 spin_lock(&mb_cache_spinlock);
525 return ERR_PTR(-EAGAIN);
526 }
527 return ce;
528 }
529 l = l->next;
530 }
531 return NULL;
532}
533
534
535/*
536 * mb_cache_entry_find_first()
537 *
538 * Find the first cache entry on a given device with a certain key in
539 * an additional index. Additonal matches can be found with
540 * mb_cache_entry_find_next(). Returns NULL if no match was found. The
541 * returned cache entry is locked for shared access ("multiple readers").
542 *
543 * @cache: the cache to search
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * @bdev: the device the cache entry should belong to
545 * @key: the key in the index
546 */
547struct mb_cache_entry *
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200548mb_cache_entry_find_first(struct mb_cache *cache, struct block_device *bdev,
549 unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
552 struct list_head *l;
553 struct mb_cache_entry *ce;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_lock(&mb_cache_spinlock);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200556 l = cache->c_index_hash[bucket].next;
557 ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 spin_unlock(&mb_cache_spinlock);
559 return ce;
560}
561
562
563/*
564 * mb_cache_entry_find_next()
565 *
566 * Find the next cache entry on a given device with a certain key in an
567 * additional index. Returns NULL if no match could be found. The previous
568 * entry is atomatically released, so that mb_cache_entry_find_next() can
569 * be called like this:
570 *
571 * entry = mb_cache_entry_find_first();
572 * while (entry) {
573 * ...
574 * entry = mb_cache_entry_find_next(entry, ...);
575 * }
576 *
577 * @prev: The previous match
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * @bdev: the device the cache entry should belong to
579 * @key: the key in the index
580 */
581struct mb_cache_entry *
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200582mb_cache_entry_find_next(struct mb_cache_entry *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct block_device *bdev, unsigned int key)
584{
585 struct mb_cache *cache = prev->e_cache;
586 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
587 struct list_head *l;
588 struct mb_cache_entry *ce;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&mb_cache_spinlock);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +0200591 l = prev->e_index.o_list.next;
592 ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 __mb_cache_entry_release_unlock(prev);
594 return ce;
595}
596
597#endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
598
599static int __init init_mbcache(void)
600{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700601 register_shrinker(&mb_cache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603}
604
605static void __exit exit_mbcache(void)
606{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700607 unregister_shrinker(&mb_cache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610module_init(init_mbcache)
611module_exit(exit_mbcache)
612