mbcache.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. atomic_t c_entry_count;
  73. int c_bucket_bits;
  74. struct kmem_cache *c_entry_cache;
  75. struct list_head *c_block_hash;
  76. struct list_head *c_index_hash;
  77. };
  78. /*
  79. * Global data: list of all mbcache's, lru list, and a spinlock for
  80. * accessing cache data structures on SMP machines. The lru list is
  81. * global across all mbcaches.
  82. */
  83. static LIST_HEAD(mb_cache_list);
  84. static LIST_HEAD(mb_cache_lru_list);
  85. static DEFINE_SPINLOCK(mb_cache_spinlock);
  86. /*
  87. * What the mbcache registers as to get shrunk dynamically.
  88. */
  89. static int mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask);
  90. static struct shrinker mb_cache_shrinker = {
  91. .shrink = mb_cache_shrink_fn,
  92. .seeks = DEFAULT_SEEKS,
  93. };
  94. static inline int
  95. __mb_cache_entry_is_hashed(struct mb_cache_entry *ce)
  96. {
  97. return !list_empty(&ce->e_block_list);
  98. }
  99. static void
  100. __mb_cache_entry_unhash(struct mb_cache_entry *ce)
  101. {
  102. if (__mb_cache_entry_is_hashed(ce)) {
  103. list_del_init(&ce->e_block_list);
  104. list_del(&ce->e_index.o_list);
  105. }
  106. }
  107. static void
  108. __mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask)
  109. {
  110. struct mb_cache *cache = ce->e_cache;
  111. mb_assert(!(ce->e_used || ce->e_queued));
  112. kmem_cache_free(cache->c_entry_cache, ce);
  113. atomic_dec(&cache->c_entry_count);
  114. }
  115. static void
  116. __mb_cache_entry_release_unlock(struct mb_cache_entry *ce)
  117. __releases(mb_cache_spinlock)
  118. {
  119. /* Wake up all processes queuing for this cache entry. */
  120. if (ce->e_queued)
  121. wake_up_all(&mb_cache_queue);
  122. if (ce->e_used >= MB_CACHE_WRITER)
  123. ce->e_used -= MB_CACHE_WRITER;
  124. ce->e_used--;
  125. if (!(ce->e_used || ce->e_queued)) {
  126. if (!__mb_cache_entry_is_hashed(ce))
  127. goto forget;
  128. mb_assert(list_empty(&ce->e_lru_list));
  129. list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
  130. }
  131. spin_unlock(&mb_cache_spinlock);
  132. return;
  133. forget:
  134. spin_unlock(&mb_cache_spinlock);
  135. __mb_cache_entry_forget(ce, GFP_KERNEL);
  136. }
  137. /*
  138. * mb_cache_shrink_fn() memory pressure callback
  139. *
  140. * This function is called by the kernel memory management when memory
  141. * gets low.
  142. *
  143. * @shrink: (ignored)
  144. * @nr_to_scan: Number of objects to scan
  145. * @gfp_mask: (ignored)
  146. *
  147. * Returns the number of objects which are present in the cache.
  148. */
  149. static int
  150. mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  151. {
  152. LIST_HEAD(free_list);
  153. struct mb_cache *cache;
  154. struct mb_cache_entry *entry, *tmp;
  155. int count = 0;
  156. mb_debug("trying to free %d entries", nr_to_scan);
  157. spin_lock(&mb_cache_spinlock);
  158. while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) {
  159. struct mb_cache_entry *ce =
  160. list_entry(mb_cache_lru_list.next,
  161. struct mb_cache_entry, e_lru_list);
  162. list_move_tail(&ce->e_lru_list, &free_list);
  163. __mb_cache_entry_unhash(ce);
  164. }
  165. list_for_each_entry(cache, &mb_cache_list, c_cache_list) {
  166. mb_debug("cache %s (%d)", cache->c_name,
  167. atomic_read(&cache->c_entry_count));
  168. count += atomic_read(&cache->c_entry_count);
  169. }
  170. spin_unlock(&mb_cache_spinlock);
  171. list_for_each_entry_safe(entry, tmp, &free_list, e_lru_list) {
  172. __mb_cache_entry_forget(entry, gfp_mask);
  173. }
  174. return (count / 100) * sysctl_vfs_cache_pressure;
  175. }
  176. /*
  177. * mb_cache_create() create a new cache
  178. *
  179. * All entries in one cache are equal size. Cache entries may be from
  180. * multiple devices. If this is the first mbcache created, registers
  181. * the cache with kernel memory management. Returns NULL if no more
  182. * memory was available.
  183. *
  184. * @name: name of the cache (informal)
  185. * @bucket_bits: log2(number of hash buckets)
  186. */
  187. struct mb_cache *
  188. mb_cache_create(const char *name, int bucket_bits)
  189. {
  190. int n, bucket_count = 1 << bucket_bits;
  191. struct mb_cache *cache = NULL;
  192. cache = kmalloc(sizeof(struct mb_cache), GFP_KERNEL);
  193. if (!cache)
  194. return NULL;
  195. cache->c_name = name;
  196. atomic_set(&cache->c_entry_count, 0);
  197. cache->c_bucket_bits = bucket_bits;
  198. cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head),
  199. GFP_KERNEL);
  200. if (!cache->c_block_hash)
  201. goto fail;
  202. for (n=0; n<bucket_count; n++)
  203. INIT_LIST_HEAD(&cache->c_block_hash[n]);
  204. cache->c_index_hash = kmalloc(bucket_count * sizeof(struct list_head),
  205. GFP_KERNEL);
  206. if (!cache->c_index_hash)
  207. goto fail;
  208. for (n=0; n<bucket_count; n++)
  209. INIT_LIST_HEAD(&cache->c_index_hash[n]);
  210. cache->c_entry_cache = kmem_cache_create(name,
  211. sizeof(struct mb_cache_entry), 0,
  212. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
  213. if (!cache->c_entry_cache)
  214. goto fail2;
  215. spin_lock(&mb_cache_spinlock);
  216. list_add(&cache->c_cache_list, &mb_cache_list);
  217. spin_unlock(&mb_cache_spinlock);
  218. return cache;
  219. fail2:
  220. kfree(cache->c_index_hash);
  221. fail:
  222. kfree(cache->c_block_hash);
  223. kfree(cache);
  224. return NULL;
  225. }
  226. /*
  227. * mb_cache_shrink()
  228. *
  229. * Removes all cache entries of a device from the cache. All cache entries
  230. * currently in use cannot be freed, and thus remain in the cache. All others
  231. * are freed.
  232. *
  233. * @bdev: which device's cache entries to shrink
  234. */
  235. void
  236. mb_cache_shrink(struct block_device *bdev)
  237. {
  238. LIST_HEAD(free_list);
  239. struct list_head *l, *ltmp;
  240. spin_lock(&mb_cache_spinlock);
  241. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  242. struct mb_cache_entry *ce =
  243. list_entry(l, struct mb_cache_entry, e_lru_list);
  244. if (ce->e_bdev == bdev) {
  245. list_move_tail(&ce->e_lru_list, &free_list);
  246. __mb_cache_entry_unhash(ce);
  247. }
  248. }
  249. spin_unlock(&mb_cache_spinlock);
  250. list_for_each_safe(l, ltmp, &free_list) {
  251. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  252. e_lru_list), GFP_KERNEL);
  253. }
  254. }
  255. /*
  256. * mb_cache_destroy()
  257. *
  258. * Shrinks the cache to its minimum possible size (hopefully 0 entries),
  259. * and then destroys it. If this was the last mbcache, un-registers the
  260. * mbcache from kernel memory management.
  261. */
  262. void
  263. mb_cache_destroy(struct mb_cache *cache)
  264. {
  265. LIST_HEAD(free_list);
  266. struct list_head *l, *ltmp;
  267. spin_lock(&mb_cache_spinlock);
  268. list_for_each_safe(l, ltmp, &mb_cache_lru_list) {
  269. struct mb_cache_entry *ce =
  270. list_entry(l, struct mb_cache_entry, e_lru_list);
  271. if (ce->e_cache == cache) {
  272. list_move_tail(&ce->e_lru_list, &free_list);
  273. __mb_cache_entry_unhash(ce);
  274. }
  275. }
  276. list_del(&cache->c_cache_list);
  277. spin_unlock(&mb_cache_spinlock);
  278. list_for_each_safe(l, ltmp, &free_list) {
  279. __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry,
  280. e_lru_list), GFP_KERNEL);
  281. }
  282. if (atomic_read(&cache->c_entry_count) > 0) {
  283. mb_error("cache %s: %d orphaned entries",
  284. cache->c_name,
  285. atomic_read(&cache->c_entry_count));
  286. }
  287. kmem_cache_destroy(cache->c_entry_cache);
  288. kfree(cache->c_index_hash);
  289. kfree(cache->c_block_hash);
  290. kfree(cache);
  291. }
  292. /*
  293. * mb_cache_entry_alloc()
  294. *
  295. * Allocates a new cache entry. The new entry will not be valid initially,
  296. * and thus cannot be looked up yet. It should be filled with data, and
  297. * then inserted into the cache using mb_cache_entry_insert(). Returns NULL
  298. * if no more memory was available.
  299. */
  300. struct mb_cache_entry *
  301. mb_cache_entry_alloc(struct mb_cache *cache, gfp_t gfp_flags)
  302. {
  303. struct mb_cache_entry *ce;
  304. ce = kmem_cache_alloc(cache->c_entry_cache, gfp_flags);
  305. if (ce) {
  306. atomic_inc(&cache->c_entry_count);
  307. INIT_LIST_HEAD(&ce->e_lru_list);
  308. INIT_LIST_HEAD(&ce->e_block_list);
  309. ce->e_cache = cache;
  310. ce->e_used = 1 + MB_CACHE_WRITER;
  311. ce->e_queued = 0;
  312. }
  313. return ce;
  314. }
  315. /*
  316. * mb_cache_entry_insert()
  317. *
  318. * Inserts an entry that was allocated using mb_cache_entry_alloc() into
  319. * the cache. After this, the cache entry can be looked up, but is not yet
  320. * in the lru list as the caller still holds a handle to it. Returns 0 on
  321. * success, or -EBUSY if a cache entry for that device + inode exists
  322. * already (this may happen after a failed lookup, but when another process
  323. * has inserted the same cache entry in the meantime).
  324. *
  325. * @bdev: device the cache entry belongs to
  326. * @block: block number
  327. * @key: lookup key
  328. */
  329. int
  330. mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
  331. sector_t block, unsigned int key)
  332. {
  333. struct mb_cache *cache = ce->e_cache;
  334. unsigned int bucket;
  335. struct list_head *l;
  336. int error = -EBUSY;
  337. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  338. cache->c_bucket_bits);
  339. spin_lock(&mb_cache_spinlock);
  340. list_for_each_prev(l, &cache->c_block_hash[bucket]) {
  341. struct mb_cache_entry *ce =
  342. list_entry(l, struct mb_cache_entry, e_block_list);
  343. if (ce->e_bdev == bdev && ce->e_block == block)
  344. goto out;
  345. }
  346. __mb_cache_entry_unhash(ce);
  347. ce->e_bdev = bdev;
  348. ce->e_block = block;
  349. list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
  350. ce->e_index.o_key = key;
  351. bucket = hash_long(key, cache->c_bucket_bits);
  352. list_add(&ce->e_index.o_list, &cache->c_index_hash[bucket]);
  353. error = 0;
  354. out:
  355. spin_unlock(&mb_cache_spinlock);
  356. return error;
  357. }
  358. /*
  359. * mb_cache_entry_release()
  360. *
  361. * Release a handle to a cache entry. When the last handle to a cache entry
  362. * is released it is either freed (if it is invalid) or otherwise inserted
  363. * in to the lru list.
  364. */
  365. void
  366. mb_cache_entry_release(struct mb_cache_entry *ce)
  367. {
  368. spin_lock(&mb_cache_spinlock);
  369. __mb_cache_entry_release_unlock(ce);
  370. }
  371. /*
  372. * mb_cache_entry_free()
  373. *
  374. * This is equivalent to the sequence mb_cache_entry_takeout() --
  375. * mb_cache_entry_release().
  376. */
  377. void
  378. mb_cache_entry_free(struct mb_cache_entry *ce)
  379. {
  380. spin_lock(&mb_cache_spinlock);
  381. mb_assert(list_empty(&ce->e_lru_list));
  382. __mb_cache_entry_unhash(ce);
  383. __mb_cache_entry_release_unlock(ce);
  384. }
  385. /*
  386. * mb_cache_entry_get()
  387. *
  388. * Get a cache entry by device / block number. (There can only be one entry
  389. * in the cache per device and block.) Returns NULL if no such cache entry
  390. * exists. The returned cache entry is locked for exclusive access ("single
  391. * writer").
  392. */
  393. struct mb_cache_entry *
  394. mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
  395. sector_t block)
  396. {
  397. unsigned int bucket;
  398. struct list_head *l;
  399. struct mb_cache_entry *ce;
  400. bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
  401. cache->c_bucket_bits);
  402. spin_lock(&mb_cache_spinlock);
  403. list_for_each(l, &cache->c_block_hash[bucket]) {
  404. ce = list_entry(l, struct mb_cache_entry, e_block_list);
  405. if (ce->e_bdev == bdev && ce->e_block == block) {
  406. DEFINE_WAIT(wait);
  407. if (!list_empty(&ce->e_lru_list))
  408. list_del_init(&ce->e_lru_list);
  409. while (ce->e_used > 0) {
  410. ce->e_queued++;
  411. prepare_to_wait(&mb_cache_queue, &wait,
  412. TASK_UNINTERRUPTIBLE);
  413. spin_unlock(&mb_cache_spinlock);
  414. schedule();
  415. spin_lock(&mb_cache_spinlock);
  416. ce->e_queued--;
  417. }
  418. finish_wait(&mb_cache_queue, &wait);
  419. ce->e_used += 1 + MB_CACHE_WRITER;
  420. if (!__mb_cache_entry_is_hashed(ce)) {
  421. __mb_cache_entry_release_unlock(ce);
  422. return NULL;
  423. }
  424. goto cleanup;
  425. }
  426. }
  427. ce = NULL;
  428. cleanup:
  429. spin_unlock(&mb_cache_spinlock);
  430. return ce;
  431. }
  432. #if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
  433. static struct mb_cache_entry *
  434. __mb_cache_entry_find(struct list_head *l, struct list_head *head,
  435. struct block_device *bdev, unsigned int key)
  436. {
  437. while (l != head) {
  438. struct mb_cache_entry *ce =
  439. list_entry(l, struct mb_cache_entry, e_index.o_list);
  440. if (ce->e_bdev == bdev && ce->e_index.o_key == key) {
  441. DEFINE_WAIT(wait);
  442. if (!list_empty(&ce->e_lru_list))
  443. list_del_init(&ce->e_lru_list);
  444. /* Incrementing before holding the lock gives readers
  445. priority over writers. */
  446. ce->e_used++;
  447. while (ce->e_used >= MB_CACHE_WRITER) {
  448. ce->e_queued++;
  449. prepare_to_wait(&mb_cache_queue, &wait,
  450. TASK_UNINTERRUPTIBLE);
  451. spin_unlock(&mb_cache_spinlock);
  452. schedule();
  453. spin_lock(&mb_cache_spinlock);
  454. ce->e_queued--;
  455. }
  456. finish_wait(&mb_cache_queue, &wait);
  457. if (!__mb_cache_entry_is_hashed(ce)) {
  458. __mb_cache_entry_release_unlock(ce);
  459. spin_lock(&mb_cache_spinlock);
  460. return ERR_PTR(-EAGAIN);
  461. }
  462. return ce;
  463. }
  464. l = l->next;
  465. }
  466. return NULL;
  467. }
  468. /*
  469. * mb_cache_entry_find_first()
  470. *
  471. * Find the first cache entry on a given device with a certain key in
  472. * an additional index. Additonal matches can be found with
  473. * mb_cache_entry_find_next(). Returns NULL if no match was found. The
  474. * returned cache entry is locked for shared access ("multiple readers").
  475. *
  476. * @cache: the cache to search
  477. * @bdev: the device the cache entry should belong to
  478. * @key: the key in the index
  479. */
  480. struct mb_cache_entry *
  481. mb_cache_entry_find_first(struct mb_cache *cache, struct block_device *bdev,
  482. unsigned int key)
  483. {
  484. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  485. struct list_head *l;
  486. struct mb_cache_entry *ce;
  487. spin_lock(&mb_cache_spinlock);
  488. l = cache->c_index_hash[bucket].next;
  489. ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key);
  490. spin_unlock(&mb_cache_spinlock);
  491. return ce;
  492. }
  493. /*
  494. * mb_cache_entry_find_next()
  495. *
  496. * Find the next cache entry on a given device with a certain key in an
  497. * additional index. Returns NULL if no match could be found. The previous
  498. * entry is atomatically released, so that mb_cache_entry_find_next() can
  499. * be called like this:
  500. *
  501. * entry = mb_cache_entry_find_first();
  502. * while (entry) {
  503. * ...
  504. * entry = mb_cache_entry_find_next(entry, ...);
  505. * }
  506. *
  507. * @prev: The previous match
  508. * @bdev: the device the cache entry should belong to
  509. * @key: the key in the index
  510. */
  511. struct mb_cache_entry *
  512. mb_cache_entry_find_next(struct mb_cache_entry *prev,
  513. struct block_device *bdev, unsigned int key)
  514. {
  515. struct mb_cache *cache = prev->e_cache;
  516. unsigned int bucket = hash_long(key, cache->c_bucket_bits);
  517. struct list_head *l;
  518. struct mb_cache_entry *ce;
  519. spin_lock(&mb_cache_spinlock);
  520. l = prev->e_index.o_list.next;
  521. ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key);
  522. __mb_cache_entry_release_unlock(prev);
  523. return ce;
  524. }
  525. #endif /* !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0) */
  526. static int __init init_mbcache(void)
  527. {
  528. register_shrinker(&mb_cache_shrinker);
  529. return 0;
  530. }
  531. static void __exit exit_mbcache(void)
  532. {
  533. unregister_shrinker(&mb_cache_shrinker);
  534. }
  535. module_init(init_mbcache)
  536. module_exit(exit_mbcache)