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