mbcache.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. struct kmem_cache *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. __releases(mb_cache_spinlock)
  138. {
  139. /* Wake up all processes queuing for this cache entry. */
  140. if (ce->e_queued)
  141. wake_up_all(&mb_cache_queue);
  142. if (ce->e_used >= MB_CACHE_WRITER)
  143. ce->e_used -= MB_CACHE_WRITER;
  144. ce->e_used--;
  145. if (!(ce->e_used || ce->e_queued)) {
  146. if (!__mb_cache_entry_is_hashed(ce))
  147. goto forget;
  148. mb_assert(list_empty(&ce->e_lru_list));
  149. list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
  150. }
  151. spin_unlock(&mb_cache_spinlock);
  152. return;
  153. forget:
  154. spin_unlock(&mb_cache_spinlock);
  155. __mb_cache_entry_forget(ce, GFP_KERNEL);
  156. }
  157. /*
  158. * mb_cache_shrink_fn() memory pressure callback
  159. *
  160. * This function is called by the kernel memory management when memory
  161. * gets low.
  162. *
  163. * @nr_to_scan: Number of objects to scan
  164. * @gfp_mask: (ignored)
  165. *
  166. * Returns the number of objects which are present in the cache.
  167. */
  168. static int
  169. mb_cache_shrink_fn(int nr_to_scan, gfp_t gfp_mask)
  170. {
  171. LIST_HEAD(free_list);
  172. struct list_head *l, *ltmp;
  173. int count = 0;
  174. spin_lock(&mb_cache_spinlock);
  175. list_for_each(l, &mb_cache_list) {
  176. struct mb_cache *cache =
  177. list_entry(l, struct mb_cache, c_cache_list);
  178. mb_debug("cache %s (%d)", cache->c_name,
  179. atomic_read(&cache->c_entry_count));
  180. count += atomic_read(&cache->c_entry_count);
  181. }
  182. mb_debug("trying to free %d entries", nr_to_scan);
  183. if (nr_to_scan == 0) {
  184. spin_unlock(&mb_cache_spinlock);
  185. goto out;
  186. }
  187. while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) {
  188. struct mb_cache_entry *ce =
  189. list_entry(mb_cache_lru_list.next,
  190. struct mb_cache_entry, e_lru_list);
  191. list_move_tail(&ce->e_lru_list, &free_list);
  192. __mb_cache_entry_unhash(ce);
  193. }
  194. spin_unlock(&mb_cache_spinlock);
  195. list_for_each_safe(l, ltmp, &free_list) {
  196. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  197. e_lru_list), gfp_mask);
  198. }
  199. out:
  200. return (count / 100) * sysctl_vfs_cache_pressure;
  201. }
  202. /*
  203. * mb_cache_create() create a new cache
  204. *
  205. * All entries in one cache are equal size. Cache entries may be from
  206. * multiple devices. If this is the first mbcache created, registers
  207. * the cache with kernel memory management. Returns NULL if no more
  208. * memory was available.
  209. *
  210. * @name: name of the cache (informal)
  211. * @cache_op: contains the callback called when freeing a cache entry
  212. * @entry_size: The size of a cache entry, including
  213. * struct mb_cache_entry
  214. * @indexes_count: number of additional indexes in the cache. Must equal
  215. * MB_CACHE_INDEXES_COUNT if the number of indexes is
  216. * hardwired.
  217. * @bucket_bits: log2(number of hash buckets)
  218. */
  219. struct mb_cache *
  220. mb_cache_create(const char *name, struct mb_cache_op *cache_op,
  221. size_t entry_size, int indexes_count, int bucket_bits)
  222. {
  223. int m=0, n, bucket_count = 1 << bucket_bits;
  224. struct mb_cache *cache = NULL;
  225. if(entry_size < sizeof(struct mb_cache_entry) +
  226. indexes_count * sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]))
  227. return NULL;
  228. cache = kmalloc(sizeof(struct mb_cache) +
  229. indexes_count * sizeof(struct list_head), GFP_KERNEL);
  230. if (!cache)
  231. goto fail;
  232. cache->c_name = name;
  233. cache->c_op.free = NULL;
  234. if (cache_op)
  235. cache->c_op.free = cache_op->free;
  236. atomic_set(&cache->c_entry_count, 0);
  237. cache->c_bucket_bits = bucket_bits;
  238. #ifdef MB_CACHE_INDEXES_COUNT
  239. mb_assert(indexes_count == MB_CACHE_INDEXES_COUNT);
  240. #else
  241. cache->c_indexes_count = indexes_count;
  242. #endif
  243. cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head),
  244. GFP_KERNEL);
  245. if (!cache->c_block_hash)
  246. goto fail;
  247. for (n=0; n<bucket_count; n++)
  248. INIT_LIST_HEAD(&cache->c_block_hash[n]);
  249. for (m=0; m<indexes_count; m++) {
  250. cache->c_indexes_hash[m] = kmalloc(bucket_count *
  251. sizeof(struct list_head),
  252. GFP_KERNEL);
  253. if (!cache->c_indexes_hash[m])
  254. goto fail;
  255. for (n=0; n<bucket_count; n++)
  256. INIT_LIST_HEAD(&cache->c_indexes_hash[m][n]);
  257. }
  258. cache->c_entry_cache = kmem_cache_create(name, entry_size, 0,
  259. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL, NULL);
  260. if (!cache->c_entry_cache)
  261. goto fail;
  262. spin_lock(&mb_cache_spinlock);
  263. list_add(&cache->c_cache_list, &mb_cache_list);
  264. spin_unlock(&mb_cache_spinlock);
  265. return cache;
  266. fail:
  267. if (cache) {
  268. while (--m >= 0)
  269. kfree(cache->c_indexes_hash[m]);
  270. kfree(cache->c_block_hash);
  271. kfree(cache);
  272. }
  273. return NULL;
  274. }
  275. /*
  276. * mb_cache_shrink()
  277. *
  278. * Removes all cache entries 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. * @bdev: which device's cache entries to shrink
  283. */
  284. void
  285. mb_cache_shrink(struct block_device *bdev)
  286. {
  287. LIST_HEAD(free_list);
  288. struct list_head *l, *ltmp;
  289. spin_lock(&mb_cache_spinlock);
  290. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  291. struct mb_cache_entry *ce =
  292. list_entry(l, struct mb_cache_entry, e_lru_list);
  293. if (ce->e_bdev == bdev) {
  294. list_move_tail(&ce->e_lru_list, &free_list);
  295. __mb_cache_entry_unhash(ce);
  296. }
  297. }
  298. spin_unlock(&mb_cache_spinlock);
  299. list_for_each_safe(l, ltmp, &free_list) {
  300. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  301. e_lru_list), GFP_KERNEL);
  302. }
  303. }
  304. /*
  305. * mb_cache_destroy()
  306. *
  307. * Shrinks the cache to its minimum possible size (hopefully 0 entries),
  308. * and then destroys it. If this was the last mbcache, un-registers the
  309. * mbcache from kernel memory management.
  310. */
  311. void
  312. mb_cache_destroy(struct mb_cache *cache)
  313. {
  314. LIST_HEAD(free_list);
  315. struct list_head *l, *ltmp;
  316. int n;
  317. spin_lock(&mb_cache_spinlock);
  318. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  319. struct mb_cache_entry *ce =
  320. list_entry(l, struct mb_cache_entry, e_lru_list);
  321. if (ce->e_cache == cache) {
  322. list_move_tail(&ce->e_lru_list, &free_list);
  323. __mb_cache_entry_unhash(ce);
  324. }
  325. }
  326. list_del(&cache->c_cache_list);
  327. spin_unlock(&mb_cache_spinlock);
  328. list_for_each_safe(l, ltmp, &free_list) {
  329. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  330. e_lru_list), GFP_KERNEL);
  331. }
  332. if (atomic_read(&cache->c_entry_count) > 0) {
  333. mb_error("cache %s: %d orphaned entries",
  334. cache->c_name,
  335. atomic_read(&cache->c_entry_count));
  336. }
  337. kmem_cache_destroy(cache->c_entry_cache);
  338. for (n=0; n < mb_cache_indexes(cache); n++)
  339. kfree(cache->c_indexes_hash[n]);
  340. kfree(cache->c_block_hash);
  341. kfree(cache);
  342. }
  343. /*
  344. * mb_cache_entry_alloc()
  345. *
  346. * Allocates a new cache entry. The new entry will not be valid initially,
  347. * and thus cannot be looked up yet. It should be filled with data, and
  348. * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
  349. * if no more memory was available.
  350. */
  351. struct mb_cache_entry *
  352. mb_cache_entry_alloc(struct mb_cache *cache)
  353. {
  354. struct mb_cache_entry *ce;
  355. atomic_inc(&cache->c_entry_count);
  356. ce = kmem_cache_alloc(cache->c_entry_cache, GFP_KERNEL);
  357. if (ce) {
  358. INIT_LIST_HEAD(&ce->e_lru_list);
  359. INIT_LIST_HEAD(&ce->e_block_list);
  360. ce->e_cache = cache;
  361. ce->e_used = 1 + MB_CACHE_WRITER;
  362. ce->e_queued = 0;
  363. }
  364. return ce;
  365. }
  366. /*
  367. * mb_cache_entry_insert()
  368. *
  369. * Inserts an entry that was allocated using mb_cache_entry_alloc() into
  370. * the cache. After this, the cache entry can be looked up, but is not yet
  371. * in the lru list as the caller still holds a handle to it. Returns 0 on
  372. * success, or -EBUSY if a cache entry for that device + inode exists
  373. * already (this may happen after a failed lookup, but when another process
  374. * has inserted the same cache entry in the meantime).
  375. *
  376. * @bdev: device the cache entry belongs to
  377. * @block: block number
  378. * @keys: array of additional keys. There must be indexes_count entries
  379. * in the array (as specified when creating the cache).
  380. */
  381. int
  382. mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
  383. sector_t block, unsigned int keys[])
  384. {
  385. struct mb_cache *cache = ce->e_cache;
  386. unsigned int bucket;
  387. struct list_head *l;
  388. int error = -EBUSY, n;
  389. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  390. cache->c_bucket_bits);
  391. spin_lock(&mb_cache_spinlock);
  392. list_for_each_prev(l, &cache->c_block_hash[bucket]) {
  393. struct mb_cache_entry *ce =
  394. list_entry(l, struct mb_cache_entry, e_block_list);
  395. if (ce->e_bdev == bdev && ce->e_block == block)
  396. goto out;
  397. }
  398. __mb_cache_entry_unhash(ce);
  399. ce->e_bdev = bdev;
  400. ce->e_block = block;
  401. list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
  402. for (n=0; n<mb_cache_indexes(cache); n++) {
  403. ce->e_indexes[n].o_key = keys[n];
  404. bucket = hash_long(keys[n], cache->c_bucket_bits);
  405. list_add(&ce->e_indexes[n].o_list,
  406. &cache->c_indexes_hash[n][bucket]);
  407. }
  408. error = 0;
  409. out:
  410. spin_unlock(&mb_cache_spinlock);
  411. return error;
  412. }
  413. /*
  414. * mb_cache_entry_release()
  415. *
  416. * Release a handle to a cache entry. When the last handle to a cache entry
  417. * is released it is either freed (if it is invalid) or otherwise inserted
  418. * in to the lru list.
  419. */
  420. void
  421. mb_cache_entry_release(struct mb_cache_entry *ce)
  422. {
  423. spin_lock(&mb_cache_spinlock);
  424. __mb_cache_entry_release_unlock(ce);
  425. }
  426. /*
  427. * mb_cache_entry_free()
  428. *
  429. * This is equivalent to the sequence mb_cache_entry_takeout() --
  430. * mb_cache_entry_release().
  431. */
  432. void
  433. mb_cache_entry_free(struct mb_cache_entry *ce)
  434. {
  435. spin_lock(&mb_cache_spinlock);
  436. mb_assert(list_empty(&ce->e_lru_list));
  437. __mb_cache_entry_unhash(ce);
  438. __mb_cache_entry_release_unlock(ce);
  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. */
  448. struct mb_cache_entry *
  449. mb_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. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  456. cache->c_bucket_bits);
  457. spin_lock(&mb_cache_spinlock);
  458. list_for_each(l, &cache->c_block_hash[bucket]) {
  459. ce = list_entry(l, struct mb_cache_entry, e_block_list);
  460. if (ce->e_bdev == bdev && ce->e_block == block) {
  461. DEFINE_WAIT(wait);
  462. if (!list_empty(&ce->e_lru_list))
  463. list_del_init(&ce->e_lru_list);
  464. while (ce->e_used > 0) {
  465. ce->e_queued++;
  466. prepare_to_wait(&mb_cache_queue, &wait,
  467. TASK_UNINTERRUPTIBLE);
  468. spin_unlock(&mb_cache_spinlock);
  469. schedule();
  470. spin_lock(&mb_cache_spinlock);
  471. ce->e_queued--;
  472. }
  473. finish_wait(&mb_cache_queue, &wait);
  474. ce->e_used += 1 + MB_CACHE_WRITER;
  475. if (!__mb_cache_entry_is_hashed(ce)) {
  476. __mb_cache_entry_release_unlock(ce);
  477. return NULL;
  478. }
  479. goto cleanup;
  480. }
  481. }
  482. ce = NULL;
  483. cleanup:
  484. spin_unlock(&mb_cache_spinlock);
  485. return ce;
  486. }
  487. #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
  488. static struct mb_cache_entry *
  489. __mb_cache_entry_find(struct list_head *l, struct list_head *head,
  490. int index, struct block_device *bdev, unsigned int key)
  491. {
  492. while (l != head) {
  493. struct mb_cache_entry *ce =
  494. list_entry(l, struct mb_cache_entry,
  495. e_indexes[index].o_list);
  496. if (ce->e_bdev == bdev && ce->e_indexes[index].o_key == key) {
  497. DEFINE_WAIT(wait);
  498. if (!list_empty(&ce->e_lru_list))
  499. list_del_init(&ce->e_lru_list);
  500. /* Incrementing before holding the lock gives readers
  501. priority over writers. */
  502. ce->e_used++;
  503. while (ce->e_used >= MB_CACHE_WRITER) {
  504. ce->e_queued++;
  505. prepare_to_wait(&mb_cache_queue, &wait,
  506. TASK_UNINTERRUPTIBLE);
  507. spin_unlock(&mb_cache_spinlock);
  508. schedule();
  509. spin_lock(&mb_cache_spinlock);
  510. ce->e_queued--;
  511. }
  512. finish_wait(&mb_cache_queue, &wait);
  513. if (!__mb_cache_entry_is_hashed(ce)) {
  514. __mb_cache_entry_release_unlock(ce);
  515. spin_lock(&mb_cache_spinlock);
  516. return ERR_PTR(-EAGAIN);
  517. }
  518. return ce;
  519. }
  520. l = l->next;
  521. }
  522. return NULL;
  523. }
  524. /*
  525. * mb_cache_entry_find_first()
  526. *
  527. * Find the first cache entry on a given device with a certain key in
  528. * an additional index. Additonal matches can be found with
  529. * mb_cache_entry_find_next(). Returns NULL if no match was found. The
  530. * returned cache entry is locked for shared access ("multiple readers").
  531. *
  532. * @cache: the cache to search
  533. * @index: the number of the additonal index to search (0<=index<indexes_count)
  534. * @bdev: the device the cache entry should belong to
  535. * @key: the key in the index
  536. */
  537. struct mb_cache_entry *
  538. mb_cache_entry_find_first(struct mb_cache *cache, int index,
  539. struct block_device *bdev, unsigned int key)
  540. {
  541. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  542. struct list_head *l;
  543. struct mb_cache_entry *ce;
  544. mb_assert(index < mb_cache_indexes(cache));
  545. spin_lock(&mb_cache_spinlock);
  546. l = cache->c_indexes_hash[index][bucket].next;
  547. ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
  548. index, bdev, key);
  549. spin_unlock(&mb_cache_spinlock);
  550. return ce;
  551. }
  552. /*
  553. * mb_cache_entry_find_next()
  554. *
  555. * Find the next cache entry on a given device with a certain key in an
  556. * additional index. Returns NULL if no match could be found. The previous
  557. * entry is atomatically released, so that mb_cache_entry_find_next() can
  558. * be called like this:
  559. *
  560. * entry = mb_cache_entry_find_first();
  561. * while (entry) {
  562. * ...
  563. * entry = mb_cache_entry_find_next(entry, ...);
  564. * }
  565. *
  566. * @prev: The previous match
  567. * @index: the number of the additonal index to search (0<=index<indexes_count)
  568. * @bdev: the device the cache entry should belong to
  569. * @key: the key in the index
  570. */
  571. struct mb_cache_entry *
  572. mb_cache_entry_find_next(struct mb_cache_entry *prev, int index,
  573. struct block_device *bdev, unsigned int key)
  574. {
  575. struct mb_cache *cache = prev->e_cache;
  576. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  577. struct list_head *l;
  578. struct mb_cache_entry *ce;
  579. mb_assert(index < mb_cache_indexes(cache));
  580. spin_lock(&mb_cache_spinlock);
  581. l = prev->e_indexes[index].o_list.next;
  582. ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket],
  583. index, bdev, key);
  584. __mb_cache_entry_release_unlock(prev);
  585. return ce;
  586. }
  587. #endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
  588. static int __init init_mbcache(void)
  589. {
  590. mb_shrinker = set_shrinker(DEFAULT_SEEKS, mb_cache_shrink_fn);
  591. return 0;
  592. }
  593. static void __exit exit_mbcache(void)
  594. {
  595. remove_shrinker(mb_shrinker);
  596. }
  597. module_init(init_mbcache)
  598. module_exit(exit_mbcache)