mbcache.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * linux/fs/mbcache.c
  3. * (C) 2001-2002 Andreas Gruenbacher, <a.gruenbacher@computer.org>
  4. */
  5. /*
  6. * Filesystem Meta Information Block Cache (mbcache)
  7. *
  8. * The mbcache caches blocks of block devices that need to be located
  9. * by their device/block number, as well as by other criteria (such
  10. * as the block's contents).
  11. *
  12. * There can only be one cache entry in a cache per device and block number.
  13. * Additional indexes need not be unique in this sense. The number of
  14. * additional indexes (=other criteria) can be hardwired at compile time
  15. * or specified at cache create time.
  16. *
  17. * Each cache entry is of fixed size. An entry may be `valid' or `invalid'
  18. * in the cache. A valid entry is in the main hash tables of the cache,
  19. * and may also be in the lru list. An invalid entry is not in any hashes
  20. * or lists.
  21. *
  22. * A valid cache entry is only in the lru list if no handles refer to it.
  23. * Invalid cache entries will be freed when the last handle to the cache
  24. * entry is released. Entries that cannot be freed immediately are put
  25. * back on the lru list.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/hash.h>
  30. #include <linux/fs.h>
  31. #include <linux/mm.h>
  32. #include <linux/slab.h>
  33. #include <linux/sched.h>
  34. #include <linux/init.h>
  35. #include <linux/mbcache.h>
  36. #ifdef MB_CACHE_DEBUG
  37. # define mb_debug(f...) do { \
  38. printk(KERN_DEBUG f); \
  39. printk("\n"); \
  40. } while (0)
  41. #define mb_assert(c) do { if (!(c)) \
  42. printk(KERN_ERR "assertion " #c " failed\n"); \
  43. } while(0)
  44. #else
  45. # define mb_debug(f...) do { } while(0)
  46. # define mb_assert(c) do { } while(0)
  47. #endif
  48. #define mb_error(f...) do { \
  49. printk(KERN_ERR f); \
  50. printk("\n"); \
  51. } while(0)
  52. #define MB_CACHE_WRITER ((unsigned short)~0U >> 1)
  53. static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
  54. MODULE_AUTHOR("Andreas Gruenbacher <a.gruenbacher@computer.org>");
  55. MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
  56. MODULE_LICENSE("GPL");
  57. EXPORT_SYMBOL(mb_cache_create);
  58. EXPORT_SYMBOL(mb_cache_shrink);
  59. EXPORT_SYMBOL(mb_cache_destroy);
  60. EXPORT_SYMBOL(mb_cache_entry_alloc);
  61. EXPORT_SYMBOL(mb_cache_entry_insert);
  62. EXPORT_SYMBOL(mb_cache_entry_release);
  63. EXPORT_SYMBOL(mb_cache_entry_free);
  64. EXPORT_SYMBOL(mb_cache_entry_get);
  65. #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
  66. EXPORT_SYMBOL(mb_cache_entry_find_first);
  67. EXPORT_SYMBOL(mb_cache_entry_find_next);
  68. #endif
  69. struct mb_cache {
  70. struct list_head c_cache_list;
  71. const char *c_name;
  72. struct mb_cache_op c_op;
  73. atomic_t c_entry_count;
  74. int c_bucket_bits;
  75. #ifndef MB_CACHE_INDEXES_COUNT
  76. int c_indexes_count;
  77. #endif
  78. kmem_cache_t *c_entry_cache;
  79. struct list_head *c_block_hash;
  80. struct list_head *c_indexes_hash[0];
  81. };
  82. /*
  83. * Global data: list of all mbcache's, lru list, and a spinlock for
  84. * accessing cache data structures on SMP machines. The lru list is
  85. * global across all mbcaches.
  86. */
  87. static LIST_HEAD(mb_cache_list);
  88. static LIST_HEAD(mb_cache_lru_list);
  89. static DEFINE_SPINLOCK(mb_cache_spinlock);
  90. static struct shrinker *mb_shrinker;
  91. static inline int
  92. mb_cache_indexes(struct mb_cache *cache)
  93. {
  94. #ifdef MB_CACHE_INDEXES_COUNT
  95. return MB_CACHE_INDEXES_COUNT;
  96. #else
  97. return cache->c_indexes_count;
  98. #endif
  99. }
  100. /*
  101. * What the mbcache registers as to get shrunk dynamically.
  102. */
  103. static int mb_cache_shrink_fn(int nr_to_scan, gfp_t gfp_mask);
  104. static inline int
  105. __mb_cache_entry_is_hashed(struct mb_cache_entry *ce)
  106. {
  107. return !list_empty(&ce->e_block_list);
  108. }
  109. static void
  110. __mb_cache_entry_unhash(struct mb_cache_entry *ce)
  111. {
  112. int n;
  113. if (__mb_cache_entry_is_hashed(ce)) {
  114. list_del_init(&ce->e_block_list);
  115. for (n=0; n<mb_cache_indexes(ce->e_cache); n++)
  116. list_del(&ce->e_indexes[n].o_list);
  117. }
  118. }
  119. static void
  120. __mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask)
  121. {
  122. struct mb_cache *cache = ce->e_cache;
  123. mb_assert(!(ce->e_used || ce->e_queued));
  124. if (cache->c_op.free && cache->c_op.free(ce, gfp_mask)) {
  125. /* free failed -- put back on the lru list
  126. for freeing later. */
  127. spin_lock(&mb_cache_spinlock);
  128. list_add(&ce->e_lru_list, &mb_cache_lru_list);
  129. spin_unlock(&mb_cache_spinlock);
  130. } else {
  131. kmem_cache_free(cache->c_entry_cache, ce);
  132. atomic_dec(&cache->c_entry_count);
  133. }
  134. }
  135. static void
  136. __mb_cache_entry_release_unlock(struct mb_cache_entry *ce)
  137. {
  138. /* Wake up all processes queuing for this cache entry. */
  139. if (ce->e_queued)
  140. wake_up_all(&mb_cache_queue);
  141. if (ce->e_used >= MB_CACHE_WRITER)
  142. ce->e_used -= MB_CACHE_WRITER;
  143. ce->e_used--;
  144. if (!(ce->e_used || ce->e_queued)) {
  145. if (!__mb_cache_entry_is_hashed(ce))
  146. goto forget;
  147. mb_assert(list_empty(&ce->e_lru_list));
  148. list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
  149. }
  150. spin_unlock(&mb_cache_spinlock);
  151. return;
  152. forget:
  153. spin_unlock(&mb_cache_spinlock);
  154. __mb_cache_entry_forget(ce, GFP_KERNEL);
  155. }
  156. /*
  157. * mb_cache_shrink_fn() memory pressure callback
  158. *
  159. * This function is called by the kernel memory management when memory
  160. * gets low.
  161. *
  162. * @nr_to_scan: Number of objects to scan
  163. * @gfp_mask: (ignored)
  164. *
  165. * Returns the number of objects which are present in the cache.
  166. */
  167. static int
  168. mb_cache_shrink_fn(int nr_to_scan, gfp_t gfp_mask)
  169. {
  170. LIST_HEAD(free_list);
  171. struct list_head *l, *ltmp;
  172. int count = 0;
  173. spin_lock(&mb_cache_spinlock);
  174. list_for_each(l, &mb_cache_list) {
  175. struct mb_cache *cache =
  176. list_entry(l, struct mb_cache, c_cache_list);
  177. mb_debug("cache %s (%d)", cache->c_name,
  178. atomic_read(&cache->c_entry_count));
  179. count += atomic_read(&cache->c_entry_count);
  180. }
  181. mb_debug("trying to free %d entries", nr_to_scan);
  182. if (nr_to_scan == 0) {
  183. spin_unlock(&mb_cache_spinlock);
  184. goto out;
  185. }
  186. while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) {
  187. struct mb_cache_entry *ce =
  188. list_entry(mb_cache_lru_list.next,
  189. struct mb_cache_entry, e_lru_list);
  190. list_move_tail(&ce->e_lru_list, &free_list);
  191. __mb_cache_entry_unhash(ce);
  192. }
  193. spin_unlock(&mb_cache_spinlock);
  194. list_for_each_safe(l, ltmp, &free_list) {
  195. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  196. e_lru_list), gfp_mask);
  197. }
  198. out:
  199. return (count / 100) * sysctl_vfs_cache_pressure;
  200. }
  201. /*
  202. * mb_cache_create() create a new cache
  203. *
  204. * All entries in one cache are equal size. Cache entries may be from
  205. * multiple devices. If this is the first mbcache created, registers
  206. * the cache with kernel memory management. Returns NULL if no more
  207. * memory was available.
  208. *
  209. * @name: name of the cache (informal)
  210. * @cache_op: contains the callback called when freeing a cache entry
  211. * @entry_size: The size of a cache entry, including
  212. * struct mb_cache_entry
  213. * @indexes_count: number of additional indexes in the cache. Must equal
  214. * MB_CACHE_INDEXES_COUNT if the number of indexes is
  215. * hardwired.
  216. * @bucket_bits: log2(number of hash buckets)
  217. */
  218. struct mb_cache *
  219. mb_cache_create(const char *name, struct mb_cache_op *cache_op,
  220. size_t entry_size, int indexes_count, int bucket_bits)
  221. {
  222. int m=0, n, bucket_count = 1 << bucket_bits;
  223. struct mb_cache *cache = NULL;
  224. if(entry_size < sizeof(struct mb_cache_entry) +
  225. indexes_count * sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]))
  226. return NULL;
  227. cache = kmalloc(sizeof(struct mb_cache) +
  228. indexes_count * sizeof(struct list_head), GFP_KERNEL);
  229. if (!cache)
  230. goto fail;
  231. cache->c_name = name;
  232. cache->c_op.free = NULL;
  233. if (cache_op)
  234. cache->c_op.free = cache_op->free;
  235. atomic_set(&cache->c_entry_count, 0);
  236. cache->c_bucket_bits = bucket_bits;
  237. #ifdef MB_CACHE_INDEXES_COUNT
  238. mb_assert(indexes_count == MB_CACHE_INDEXES_COUNT);
  239. #else
  240. cache->c_indexes_count = indexes_count;
  241. #endif
  242. cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head),
  243. GFP_KERNEL);
  244. if (!cache->c_block_hash)
  245. goto fail;
  246. for (n=0; n<bucket_count; n++)
  247. INIT_LIST_HEAD(&cache->c_block_hash[n]);
  248. for (m=0; m<indexes_count; m++) {
  249. cache->c_indexes_hash[m] = kmalloc(bucket_count *
  250. sizeof(struct list_head),
  251. GFP_KERNEL);
  252. if (!cache->c_indexes_hash[m])
  253. goto fail;
  254. for (n=0; n<bucket_count; n++)
  255. INIT_LIST_HEAD(&cache->c_indexes_hash[m][n]);
  256. }
  257. cache->c_entry_cache = kmem_cache_create(name, entry_size, 0,
  258. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL, NULL);
  259. if (!cache->c_entry_cache)
  260. goto fail;
  261. spin_lock(&mb_cache_spinlock);
  262. list_add(&cache->c_cache_list, &mb_cache_list);
  263. spin_unlock(&mb_cache_spinlock);
  264. return cache;
  265. fail:
  266. if (cache) {
  267. while (--m >= 0)
  268. kfree(cache->c_indexes_hash[m]);
  269. kfree(cache->c_block_hash);
  270. kfree(cache);
  271. }
  272. return NULL;
  273. }
  274. /*
  275. * mb_cache_shrink()
  276. *
  277. * Removes all cache entries of a device from the cache. All cache entries
  278. * currently in use cannot be freed, and thus remain in the cache. All others
  279. * are freed.
  280. *
  281. * @bdev: which device's cache entries to shrink
  282. */
  283. void
  284. mb_cache_shrink(struct block_device *bdev)
  285. {
  286. LIST_HEAD(free_list);
  287. struct list_head *l, *ltmp;
  288. spin_lock(&mb_cache_spinlock);
  289. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  290. struct mb_cache_entry *ce =
  291. list_entry(l, struct mb_cache_entry, e_lru_list);
  292. if (ce->e_bdev == bdev) {
  293. list_move_tail(&ce->e_lru_list, &free_list);
  294. __mb_cache_entry_unhash(ce);
  295. }
  296. }
  297. spin_unlock(&mb_cache_spinlock);
  298. list_for_each_safe(l, ltmp, &free_list) {
  299. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  300. e_lru_list), GFP_KERNEL);
  301. }
  302. }
  303. /*
  304. * mb_cache_destroy()
  305. *
  306. * Shrinks the cache to its minimum possible size (hopefully 0 entries),
  307. * and then destroys it. If this was the last mbcache, un-registers the
  308. * mbcache from kernel memory management.
  309. */
  310. void
  311. mb_cache_destroy(struct mb_cache *cache)
  312. {
  313. LIST_HEAD(free_list);
  314. struct list_head *l, *ltmp;
  315. int n;
  316. spin_lock(&mb_cache_spinlock);
  317. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  318. struct mb_cache_entry *ce =
  319. list_entry(l, struct mb_cache_entry, e_lru_list);
  320. if (ce->e_cache == cache) {
  321. list_move_tail(&ce->e_lru_list, &free_list);
  322. __mb_cache_entry_unhash(ce);
  323. }
  324. }
  325. list_del(&cache->c_cache_list);
  326. spin_unlock(&mb_cache_spinlock);
  327. list_for_each_safe(l, ltmp, &free_list) {
  328. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  329. e_lru_list), GFP_KERNEL);
  330. }
  331. if (atomic_read(&cache->c_entry_count) > 0) {
  332. mb_error("cache %s: %d orphaned entries",
  333. cache->c_name,
  334. atomic_read(&cache->c_entry_count));
  335. }
  336. kmem_cache_destroy(cache->c_entry_cache);
  337. for (n=0; n < mb_cache_indexes(cache); n++)
  338. kfree(cache->c_indexes_hash[n]);
  339. kfree(cache->c_block_hash);
  340. kfree(cache);
  341. }
  342. /*
  343. * mb_cache_entry_alloc()
  344. *
  345. * Allocates a new cache entry. The new entry will not be valid initially,
  346. * and thus cannot be looked up yet. It should be filled with data, and
  347. * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
  348. * if no more memory was available.
  349. */
  350. struct mb_cache_entry *
  351. mb_cache_entry_alloc(struct mb_cache *cache)
  352. {
  353. struct mb_cache_entry *ce;
  354. atomic_inc(&cache->c_entry_count);
  355. ce = kmem_cache_alloc(cache->c_entry_cache, GFP_KERNEL);
  356. if (ce) {
  357. INIT_LIST_HEAD(&ce->e_lru_list);
  358. INIT_LIST_HEAD(&ce->e_block_list);
  359. ce->e_cache = cache;
  360. ce->e_used = 1 + MB_CACHE_WRITER;
  361. ce->e_queued = 0;
  362. }
  363. return ce;
  364. }
  365. /*
  366. * mb_cache_entry_insert()
  367. *
  368. * Inserts an entry that was allocated using mb_cache_entry_alloc() into
  369. * the cache. After this, the cache entry can be looked up, but is not yet
  370. * in the lru list as the caller still holds a handle to it. Returns 0 on
  371. * success, or -EBUSY if a cache entry for that device + inode exists
  372. * already (this may happen after a failed lookup, but when another process
  373. * has inserted the same cache entry in the meantime).
  374. *
  375. * @bdev: device the cache entry belongs to
  376. * @block: block number
  377. * @keys: array of additional keys. There must be indexes_count entries
  378. * in the array (as specified when creating the cache).
  379. */
  380. int
  381. mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
  382. sector_t block, unsigned int keys[])
  383. {
  384. struct mb_cache *cache = ce->e_cache;
  385. unsigned int bucket;
  386. struct list_head *l;
  387. int error = -EBUSY, n;
  388. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  389. cache->c_bucket_bits);
  390. spin_lock(&mb_cache_spinlock);
  391. list_for_each_prev(l, &cache->c_block_hash[bucket]) {
  392. struct mb_cache_entry *ce =
  393. list_entry(l, struct mb_cache_entry, e_block_list);
  394. if (ce->e_bdev == bdev && ce->e_block == block)
  395. goto out;
  396. }
  397. __mb_cache_entry_unhash(ce);
  398. ce->e_bdev = bdev;
  399. ce->e_block = block;
  400. list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
  401. for (n=0; n<mb_cache_indexes(cache); n++) {
  402. ce->e_indexes[n].o_key = keys[n];
  403. bucket = hash_long(keys[n], cache->c_bucket_bits);
  404. list_add(&ce->e_indexes[n].o_list,
  405. &cache->c_indexes_hash[n][bucket]);
  406. }
  407. error = 0;
  408. out:
  409. spin_unlock(&mb_cache_spinlock);
  410. return error;
  411. }
  412. /*
  413. * mb_cache_entry_release()
  414. *
  415. * Release a handle to a cache entry. When the last handle to a cache entry
  416. * is released it is either freed (if it is invalid) or otherwise inserted
  417. * in to the lru list.
  418. */
  419. void
  420. mb_cache_entry_release(struct mb_cache_entry *ce)
  421. {
  422. spin_lock(&mb_cache_spinlock);
  423. __mb_cache_entry_release_unlock(ce);
  424. }
  425. /*
  426. * mb_cache_entry_free()
  427. *
  428. * This is equivalent to the sequence mb_cache_entry_takeout() --
  429. * mb_cache_entry_release().
  430. */
  431. void
  432. mb_cache_entry_free(struct mb_cache_entry *ce)
  433. {
  434. spin_lock(&mb_cache_spinlock);
  435. mb_assert(list_empty(&ce->e_lru_list));
  436. __mb_cache_entry_unhash(ce);
  437. __mb_cache_entry_release_unlock(ce);
  438. }
  439. /*
  440. * mb_cache_entry_get()
  441. *
  442. * Get a cache entry by device / block number. (There can only be one entry
  443. * in the cache per device and block.) Returns NULL if no such cache entry
  444. * exists. The returned cache entry is locked for exclusive access ("single
  445. * writer").
  446. */
  447. struct mb_cache_entry *
  448. mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
  449. sector_t block)
  450. {
  451. unsigned int bucket;
  452. struct list_head *l;
  453. struct mb_cache_entry *ce;
  454. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  455. cache->c_bucket_bits);
  456. spin_lock(&mb_cache_spinlock);
  457. list_for_each(l, &cache->c_block_hash[bucket]) {
  458. ce = list_entry(l, struct mb_cache_entry, e_block_list);
  459. if (ce->e_bdev == bdev && ce->e_block == block) {
  460. DEFINE_WAIT(wait);
  461. if (!list_empty(&ce->e_lru_list))
  462. list_del_init(&ce->e_lru_list);
  463. while (ce->e_used > 0) {
  464. ce->e_queued++;
  465. prepare_to_wait(&mb_cache_queue, &wait,
  466. TASK_UNINTERRUPTIBLE);
  467. spin_unlock(&mb_cache_spinlock);
  468. schedule();
  469. spin_lock(&mb_cache_spinlock);
  470. ce->e_queued--;
  471. }
  472. finish_wait(&mb_cache_queue, &wait);
  473. ce->e_used += 1 + MB_CACHE_WRITER;
  474. if (!__mb_cache_entry_is_hashed(ce)) {
  475. __mb_cache_entry_release_unlock(ce);
  476. return NULL;
  477. }
  478. goto cleanup;
  479. }
  480. }
  481. ce = NULL;
  482. cleanup:
  483. spin_unlock(&mb_cache_spinlock);
  484. return ce;
  485. }
  486. #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
  487. static struct mb_cache_entry *
  488. __mb_cache_entry_find(struct list_head *l, struct list_head *head,
  489. int index, struct block_device *bdev, unsigned int key)
  490. {
  491. while (l != head) {
  492. struct mb_cache_entry *ce =
  493. list_entry(l, struct mb_cache_entry,
  494. e_indexes[index].o_list);
  495. if (ce->e_bdev == bdev && ce->e_indexes[index].o_key == key) {
  496. DEFINE_WAIT(wait);
  497. if (!list_empty(&ce->e_lru_list))
  498. list_del_init(&ce->e_lru_list);
  499. /* Incrementing before holding the lock gives readers
  500. priority over writers. */
  501. ce->e_used++;
  502. while (ce->e_used >= MB_CACHE_WRITER) {
  503. ce->e_queued++;
  504. prepare_to_wait(&mb_cache_queue, &wait,
  505. TASK_UNINTERRUPTIBLE);
  506. spin_unlock(&mb_cache_spinlock);
  507. schedule();
  508. spin_lock(&mb_cache_spinlock);
  509. ce->e_queued--;
  510. }
  511. finish_wait(&mb_cache_queue, &wait);
  512. if (!__mb_cache_entry_is_hashed(ce)) {
  513. __mb_cache_entry_release_unlock(ce);
  514. spin_lock(&mb_cache_spinlock);
  515. return ERR_PTR(-EAGAIN);
  516. }
  517. return ce;
  518. }
  519. l = l->next;
  520. }
  521. return NULL;
  522. }
  523. /*
  524. * mb_cache_entry_find_first()
  525. *
  526. * Find the first cache entry on a given device with a certain key in
  527. * an additional index. Additonal matches can be found with
  528. * mb_cache_entry_find_next(). Returns NULL if no match was found. The
  529. * returned cache entry is locked for shared access ("multiple readers").
  530. *
  531. * @cache: the cache to search
  532. * @index: the number of the additonal index to search (0<=index<indexes_count)
  533. * @bdev: the device the cache entry should belong to
  534. * @key: the key in the index
  535. */
  536. struct mb_cache_entry *
  537. mb_cache_entry_find_first(struct mb_cache *cache, int index,
  538. struct block_device *bdev, unsigned int key)
  539. {
  540. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  541. struct list_head *l;
  542. struct mb_cache_entry *ce;
  543. mb_assert(index < mb_cache_indexes(cache));
  544. spin_lock(&mb_cache_spinlock);
  545. l = cache->c_indexes_hash[index][bucket].next;
  546. ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
  547. index, bdev, key);
  548. spin_unlock(&mb_cache_spinlock);
  549. return ce;
  550. }
  551. /*
  552. * mb_cache_entry_find_next()
  553. *
  554. * Find the next cache entry on a given device with a certain key in an
  555. * additional index. Returns NULL if no match could be found. The previous
  556. * entry is atomatically released, so that mb_cache_entry_find_next() can
  557. * be called like this:
  558. *
  559. * entry = mb_cache_entry_find_first();
  560. * while (entry) {
  561. * ...
  562. * entry = mb_cache_entry_find_next(entry, ...);
  563. * }
  564. *
  565. * @prev: The previous match
  566. * @index: the number of the additonal index to search (0<=index<indexes_count)
  567. * @bdev: the device the cache entry should belong to
  568. * @key: the key in the index
  569. */
  570. struct mb_cache_entry *
  571. mb_cache_entry_find_next(struct mb_cache_entry *prev, int index,
  572. struct block_device *bdev, unsigned int key)
  573. {
  574. struct mb_cache *cache = prev->e_cache;
  575. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  576. struct list_head *l;
  577. struct mb_cache_entry *ce;
  578. mb_assert(index < mb_cache_indexes(cache));
  579. spin_lock(&mb_cache_spinlock);
  580. l = prev->e_indexes[index].o_list.next;
  581. ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
  582. index, bdev, key);
  583. __mb_cache_entry_release_unlock(prev);
  584. return ce;
  585. }
  586. #endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
  587. static int __init init_mbcache(void)
  588. {
  589. mb_shrinker = set_shrinker(DEFAULT_SEEKS, mb_cache_shrink_fn);
  590. return 0;
  591. }
  592. static void __exit exit_mbcache(void)
  593. {
  594. remove_shrinker(mb_shrinker);
  595. }
  596. module_init(init_mbcache)
  597. module_exit(exit_mbcache)