mbcache.c 16 KB

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