mbcache.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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, unsigned int 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 inline 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 inline void
  120. __mb_cache_entry_forget(struct mb_cache_entry *ce, int 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 inline 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, unsigned int 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, 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. if (cache->c_block_hash)
  270. kfree(cache->c_block_hash);
  271. kfree(cache);
  272. }
  273. return NULL;
  274. }
  275. /*
  276. * mb_cache_shrink()
  277. *
  278. * Removes all cache entires of a device from the cache. All cache entries
  279. * currently in use cannot be freed, and thus remain in the cache. All others
  280. * are freed.
  281. *
  282. * @cache: which cache to shrink
  283. * @bdev: which device's cache entries to shrink
  284. */
  285. void
  286. mb_cache_shrink(struct mb_cache *cache, struct block_device *bdev)
  287. {
  288. LIST_HEAD(free_list);
  289. struct list_head *l, *ltmp;
  290. spin_lock(&mb_cache_spinlock);
  291. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  292. struct mb_cache_entry *ce =
  293. list_entry(l, struct mb_cache_entry, e_lru_list);
  294. if (ce->e_bdev == bdev) {
  295. list_move_tail(&ce->e_lru_list, &free_list);
  296. __mb_cache_entry_unhash(ce);
  297. }
  298. }
  299. spin_unlock(&mb_cache_spinlock);
  300. list_for_each_safe(l, ltmp, &free_list) {
  301. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  302. e_lru_list), GFP_KERNEL);
  303. }
  304. }
  305. /*
  306. * mb_cache_destroy()
  307. *
  308. * Shrinks the cache to its minimum possible size (hopefully 0 entries),
  309. * and then destroys it. If this was the last mbcache, un-registers the
  310. * mbcache from kernel memory management.
  311. */
  312. void
  313. mb_cache_destroy(struct mb_cache *cache)
  314. {
  315. LIST_HEAD(free_list);
  316. struct list_head *l, *ltmp;
  317. int n;
  318. spin_lock(&mb_cache_spinlock);
  319. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  320. struct mb_cache_entry *ce =
  321. list_entry(l, struct mb_cache_entry, e_lru_list);
  322. if (ce->e_cache == cache) {
  323. list_move_tail(&ce->e_lru_list, &free_list);
  324. __mb_cache_entry_unhash(ce);
  325. }
  326. }
  327. list_del(&cache->c_cache_list);
  328. spin_unlock(&mb_cache_spinlock);
  329. list_for_each_safe(l, ltmp, &free_list) {
  330. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  331. e_lru_list), GFP_KERNEL);
  332. }
  333. if (atomic_read(&cache->c_entry_count) > 0) {
  334. mb_error("cache %s: %d orphaned entries",
  335. cache->c_name,
  336. atomic_read(&cache->c_entry_count));
  337. }
  338. kmem_cache_destroy(cache->c_entry_cache);
  339. for (n=0; n < mb_cache_indexes(cache); n++)
  340. kfree(cache->c_indexes_hash[n]);
  341. kfree(cache->c_block_hash);
  342. kfree(cache);
  343. }
  344. /*
  345. * mb_cache_entry_alloc()
  346. *
  347. * Allocates a new cache entry. The new entry will not be valid initially,
  348. * and thus cannot be looked up yet. It should be filled with data, and
  349. * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
  350. * if no more memory was available.
  351. */
  352. struct mb_cache_entry *
  353. mb_cache_entry_alloc(struct mb_cache *cache)
  354. {
  355. struct mb_cache_entry *ce;
  356. atomic_inc(&cache->c_entry_count);
  357. ce = kmem_cache_alloc(cache->c_entry_cache, GFP_KERNEL);
  358. if (ce) {
  359. INIT_LIST_HEAD(&ce->e_lru_list);
  360. INIT_LIST_HEAD(&ce->e_block_list);
  361. ce->e_cache = cache;
  362. ce->e_used = 1 + MB_CACHE_WRITER;
  363. ce->e_queued = 0;
  364. }
  365. return ce;
  366. }
  367. /*
  368. * mb_cache_entry_insert()
  369. *
  370. * Inserts an entry that was allocated using mb_cache_entry_alloc() into
  371. * the cache. After this, the cache entry can be looked up, but is not yet
  372. * in the lru list as the caller still holds a handle to it. Returns 0 on
  373. * success, or -EBUSY if a cache entry for that device + inode exists
  374. * already (this may happen after a failed lookup, but when another process
  375. * has inserted the same cache entry in the meantime).
  376. *
  377. * @bdev: device the cache entry belongs to
  378. * @block: block number
  379. * @keys: array of additional keys. There must be indexes_count entries
  380. * in the array (as specified when creating the cache).
  381. */
  382. int
  383. mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
  384. sector_t block, unsigned int keys[])
  385. {
  386. struct mb_cache *cache = ce->e_cache;
  387. unsigned int bucket;
  388. struct list_head *l;
  389. int error = -EBUSY, n;
  390. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  391. cache->c_bucket_bits);
  392. spin_lock(&mb_cache_spinlock);
  393. list_for_each_prev(l, &cache->c_block_hash[bucket]) {
  394. struct mb_cache_entry *ce =
  395. list_entry(l, struct mb_cache_entry, e_block_list);
  396. if (ce->e_bdev == bdev && ce->e_block == block)
  397. goto out;
  398. }
  399. __mb_cache_entry_unhash(ce);
  400. ce->e_bdev = bdev;
  401. ce->e_block = block;
  402. list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
  403. for (n=0; n<mb_cache_indexes(cache); n++) {
  404. ce->e_indexes[n].o_key = keys[n];
  405. bucket = hash_long(keys[n], cache->c_bucket_bits);
  406. list_add(&ce->e_indexes[n].o_list,
  407. &cache->c_indexes_hash[n][bucket]);
  408. }
  409. error = 0;
  410. out:
  411. spin_unlock(&mb_cache_spinlock);
  412. return error;
  413. }
  414. /*
  415. * mb_cache_entry_release()
  416. *
  417. * Release a handle to a cache entry. When the last handle to a cache entry
  418. * is released it is either freed (if it is invalid) or otherwise inserted
  419. * in to the lru list.
  420. */
  421. void
  422. mb_cache_entry_release(struct mb_cache_entry *ce)
  423. {
  424. spin_lock(&mb_cache_spinlock);
  425. __mb_cache_entry_release_unlock(ce);
  426. }
  427. /*
  428. * mb_cache_entry_free()
  429. *
  430. * This is equivalent to the sequence mb_cache_entry_takeout() --
  431. * mb_cache_entry_release().
  432. */
  433. void
  434. mb_cache_entry_free(struct mb_cache_entry *ce)
  435. {
  436. spin_lock(&mb_cache_spinlock);
  437. mb_assert(list_empty(&ce->e_lru_list));
  438. __mb_cache_entry_unhash(ce);
  439. __mb_cache_entry_release_unlock(ce);
  440. }
  441. /*
  442. * mb_cache_entry_get()
  443. *
  444. * Get a cache entry by device / block number. (There can only be one entry
  445. * in the cache per device and block.) Returns NULL if no such cache entry
  446. * exists. The returned cache entry is locked for exclusive access ("single
  447. * writer").
  448. */
  449. struct mb_cache_entry *
  450. mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
  451. sector_t block)
  452. {
  453. unsigned int bucket;
  454. struct list_head *l;
  455. struct mb_cache_entry *ce;
  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. if (!list_empty(&ce->e_lru_list))
  464. list_del_init(&ce->e_lru_list);
  465. while (ce->e_used > 0) {
  466. ce->e_queued++;
  467. prepare_to_wait(&mb_cache_queue, &wait,
  468. TASK_UNINTERRUPTIBLE);
  469. spin_unlock(&mb_cache_spinlock);
  470. schedule();
  471. spin_lock(&mb_cache_spinlock);
  472. ce->e_queued--;
  473. }
  474. finish_wait(&mb_cache_queue, &wait);
  475. ce->e_used += 1 + MB_CACHE_WRITER;
  476. if (!__mb_cache_entry_is_hashed(ce)) {
  477. __mb_cache_entry_release_unlock(ce);
  478. return NULL;
  479. }
  480. goto cleanup;
  481. }
  482. }
  483. ce = NULL;
  484. cleanup:
  485. spin_unlock(&mb_cache_spinlock);
  486. return ce;
  487. }
  488. #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
  489. static struct mb_cache_entry *
  490. __mb_cache_entry_find(struct list_head *l, struct list_head *head,
  491. int index, struct block_device *bdev, unsigned int key)
  492. {
  493. while (l != head) {
  494. struct mb_cache_entry *ce =
  495. list_entry(l, struct mb_cache_entry,
  496. e_indexes[index].o_list);
  497. if (ce->e_bdev == bdev && ce->e_indexes[index].o_key == key) {
  498. DEFINE_WAIT(wait);
  499. if (!list_empty(&ce->e_lru_list))
  500. list_del_init(&ce->e_lru_list);
  501. /* Incrementing before holding the lock gives readers
  502. priority over writers. */
  503. ce->e_used++;
  504. while (ce->e_used >= MB_CACHE_WRITER) {
  505. ce->e_queued++;
  506. prepare_to_wait(&mb_cache_queue, &wait,
  507. TASK_UNINTERRUPTIBLE);
  508. spin_unlock(&mb_cache_spinlock);
  509. schedule();
  510. spin_lock(&mb_cache_spinlock);
  511. ce->e_queued--;
  512. }
  513. finish_wait(&mb_cache_queue, &wait);
  514. if (!__mb_cache_entry_is_hashed(ce)) {
  515. __mb_cache_entry_release_unlock(ce);
  516. spin_lock(&mb_cache_spinlock);
  517. return ERR_PTR(-EAGAIN);
  518. }
  519. return ce;
  520. }
  521. l = l->next;
  522. }
  523. return NULL;
  524. }
  525. /*
  526. * mb_cache_entry_find_first()
  527. *
  528. * Find the first cache entry on a given device with a certain key in
  529. * an additional index. Additonal matches can be found with
  530. * mb_cache_entry_find_next(). Returns NULL if no match was found. The
  531. * returned cache entry is locked for shared access ("multiple readers").
  532. *
  533. * @cache: the cache to search
  534. * @index: the number of the additonal index to search (0<=index<indexes_count)
  535. * @bdev: the device the cache entry should belong to
  536. * @key: the key in the index
  537. */
  538. struct mb_cache_entry *
  539. mb_cache_entry_find_first(struct mb_cache *cache, int index,
  540. struct block_device *bdev, unsigned int key)
  541. {
  542. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  543. struct list_head *l;
  544. struct mb_cache_entry *ce;
  545. mb_assert(index < mb_cache_indexes(cache));
  546. spin_lock(&mb_cache_spinlock);
  547. l = cache->c_indexes_hash[index][bucket].next;
  548. ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
  549. index, bdev, key);
  550. spin_unlock(&mb_cache_spinlock);
  551. return ce;
  552. }
  553. /*
  554. * mb_cache_entry_find_next()
  555. *
  556. * Find the next cache entry on a given device with a certain key in an
  557. * additional index. Returns NULL if no match could be found. The previous
  558. * entry is atomatically released, so that mb_cache_entry_find_next() can
  559. * be called like this:
  560. *
  561. * entry = mb_cache_entry_find_first();
  562. * while (entry) {
  563. * ...
  564. * entry = mb_cache_entry_find_next(entry, ...);
  565. * }
  566. *
  567. * @prev: The previous match
  568. * @index: the number of the additonal index to search (0<=index<indexes_count)
  569. * @bdev: the device the cache entry should belong to
  570. * @key: the key in the index
  571. */
  572. struct mb_cache_entry *
  573. mb_cache_entry_find_next(struct mb_cache_entry *prev, int index,
  574. struct block_device *bdev, unsigned int key)
  575. {
  576. struct mb_cache *cache = prev->e_cache;
  577. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  578. struct list_head *l;
  579. struct mb_cache_entry *ce;
  580. mb_assert(index < mb_cache_indexes(cache));
  581. spin_lock(&mb_cache_spinlock);
  582. l = prev->e_indexes[index].o_list.next;
  583. ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
  584. index, bdev, key);
  585. __mb_cache_entry_release_unlock(prev);
  586. return ce;
  587. }
  588. #endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
  589. static int __init init_mbcache(void)
  590. {
  591. mb_shrinker = set_shrinker(DEFAULT_SEEKS, mb_cache_shrink_fn);
  592. return 0;
  593. }
  594. static void __exit exit_mbcache(void)
  595. {
  596. remove_shrinker(mb_shrinker);
  597. }
  598. module_init(init_mbcache)
  599. module_exit(exit_mbcache)