1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
60static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
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;
82 atomic_t c_entry_count;
83 int c_max_entries;
84 int c_bucket_bits;
85 struct kmem_cache *c_entry_cache;
86 struct list_head *c_block_hash;
87 struct list_head *c_index_hash;
88};
89
90
91
92
93
94
95
96
97static LIST_HEAD(mb_cache_list);
98static LIST_HEAD(mb_cache_lru_list);
99static DEFINE_SPINLOCK(mb_cache_spinlock);
100
101
102
103
104
105static int mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask);
106
107static struct shrinker mb_cache_shrinker = {
108 .shrink = mb_cache_shrink_fn,
109 .seeks = DEFAULT_SEEKS,
110};
111
112static inline int
113__mb_cache_entry_is_hashed(struct mb_cache_entry *ce)
114{
115 return !list_empty(&ce->e_block_list);
116}
117
118
119static void
120__mb_cache_entry_unhash(struct mb_cache_entry *ce)
121{
122 if (__mb_cache_entry_is_hashed(ce)) {
123 list_del_init(&ce->e_block_list);
124 list_del(&ce->e_index.o_list);
125 }
126}
127
128
129static void
130__mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask)
131{
132 struct mb_cache *cache = ce->e_cache;
133
134 mb_assert(!(ce->e_used || ce->e_queued));
135 kmem_cache_free(cache->c_entry_cache, ce);
136 atomic_dec(&cache->c_entry_count);
137}
138
139
140static void
141__mb_cache_entry_release_unlock(struct mb_cache_entry *ce)
142 __releases(mb_cache_spinlock)
143{
144
145 if (ce->e_queued)
146 wake_up_all(&mb_cache_queue);
147 if (ce->e_used >= MB_CACHE_WRITER)
148 ce->e_used -= MB_CACHE_WRITER;
149 ce->e_used--;
150 if (!(ce->e_used || ce->e_queued)) {
151 if (!__mb_cache_entry_is_hashed(ce))
152 goto forget;
153 mb_assert(list_empty(&ce->e_lru_list));
154 list_add_tail(&ce->e_lru_list, &mb_cache_lru_list);
155 }
156 spin_unlock(&mb_cache_spinlock);
157 return;
158forget:
159 spin_unlock(&mb_cache_spinlock);
160 __mb_cache_entry_forget(ce, GFP_KERNEL);
161}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176static int
177mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
178{
179 LIST_HEAD(free_list);
180 struct mb_cache *cache;
181 struct mb_cache_entry *entry, *tmp;
182 int count = 0;
183
184 mb_debug("trying to free %d entries", nr_to_scan);
185 spin_lock(&mb_cache_spinlock);
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 list_for_each_entry(cache, &mb_cache_list, c_cache_list) {
194 mb_debug("cache %s (%d)", cache->c_name,
195 atomic_read(&cache->c_entry_count));
196 count += atomic_read(&cache->c_entry_count);
197 }
198 spin_unlock(&mb_cache_spinlock);
199 list_for_each_entry_safe(entry, tmp, &free_list, e_lru_list) {
200 __mb_cache_entry_forget(entry, gfp_mask);
201 }
202 return (count / 100) * sysctl_vfs_cache_pressure;
203}
204
205
206
207
208
209
210
211
212
213
214
215
216
217struct mb_cache *
218mb_cache_create(const char *name, int bucket_bits)
219{
220 int n, bucket_count = 1 << bucket_bits;
221 struct mb_cache *cache = NULL;
222
223 cache = kmalloc(sizeof(struct mb_cache), GFP_KERNEL);
224 if (!cache)
225 return NULL;
226 cache->c_name = name;
227 atomic_set(&cache->c_entry_count, 0);
228 cache->c_bucket_bits = bucket_bits;
229 cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head),
230 GFP_KERNEL);
231 if (!cache->c_block_hash)
232 goto fail;
233 for (n=0; n<bucket_count; n++)
234 INIT_LIST_HEAD(&cache->c_block_hash[n]);
235 cache->c_index_hash = kmalloc(bucket_count * sizeof(struct list_head),
236 GFP_KERNEL);
237 if (!cache->c_index_hash)
238 goto fail;
239 for (n=0; n<bucket_count; n++)
240 INIT_LIST_HEAD(&cache->c_index_hash[n]);
241 cache->c_entry_cache = kmem_cache_create(name,
242 sizeof(struct mb_cache_entry), 0,
243 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
244 if (!cache->c_entry_cache)
245 goto fail2;
246
247
248
249
250
251 cache->c_max_entries = bucket_count << 4;
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
258fail2:
259 kfree(cache->c_index_hash);
260
261fail:
262 kfree(cache->c_block_hash);
263 kfree(cache);
264 return NULL;
265}
266
267
268
269
270
271
272
273
274
275
276
277void
278mb_cache_shrink(struct block_device *bdev)
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
302
303
304
305
306
307void
308mb_cache_destroy(struct mb_cache *cache)
309{
310 LIST_HEAD(free_list);
311 struct list_head *l, *ltmp;
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
338 kfree(cache->c_index_hash);
339 kfree(cache->c_block_hash);
340 kfree(cache);
341}
342
343
344
345
346
347
348
349
350
351struct mb_cache_entry *
352mb_cache_entry_alloc(struct mb_cache *cache, gfp_t gfp_flags)
353{
354 struct mb_cache_entry *ce = NULL;
355
356 if (atomic_read(&cache->c_entry_count) >= cache->c_max_entries) {
357 spin_lock(&mb_cache_spinlock);
358 if (!list_empty(&mb_cache_lru_list)) {
359 ce = list_entry(mb_cache_lru_list.next,
360 struct mb_cache_entry, e_lru_list);
361 list_del_init(&ce->e_lru_list);
362 __mb_cache_entry_unhash(ce);
363 }
364 spin_unlock(&mb_cache_spinlock);
365 }
366 if (!ce) {
367 ce = kmem_cache_alloc(cache->c_entry_cache, gfp_flags);
368 if (!ce)
369 return NULL;
370 atomic_inc(&cache->c_entry_count);
371 INIT_LIST_HEAD(&ce->e_lru_list);
372 INIT_LIST_HEAD(&ce->e_block_list);
373 ce->e_cache = cache;
374 ce->e_queued = 0;
375 }
376 ce->e_used = 1 + MB_CACHE_WRITER;
377 return ce;
378}
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395int
396mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev,
397 sector_t block, unsigned int key)
398{
399 struct mb_cache *cache = ce->e_cache;
400 unsigned int bucket;
401 struct list_head *l;
402 int error = -EBUSY;
403
404 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
405 cache->c_bucket_bits);
406 spin_lock(&mb_cache_spinlock);
407 list_for_each_prev(l, &cache->c_block_hash[bucket]) {
408 struct mb_cache_entry *ce =
409 list_entry(l, struct mb_cache_entry, e_block_list);
410 if (ce->e_bdev == bdev && ce->e_block == block)
411 goto out;
412 }
413 __mb_cache_entry_unhash(ce);
414 ce->e_bdev = bdev;
415 ce->e_block = block;
416 list_add(&ce->e_block_list, &cache->c_block_hash[bucket]);
417 ce->e_index.o_key = key;
418 bucket = hash_long(key, cache->c_bucket_bits);
419 list_add(&ce->e_index.o_list, &cache->c_index_hash[bucket]);
420 error = 0;
421out:
422 spin_unlock(&mb_cache_spinlock);
423 return error;
424}
425
426
427
428
429
430
431
432
433
434void
435mb_cache_entry_release(struct mb_cache_entry *ce)
436{
437 spin_lock(&mb_cache_spinlock);
438 __mb_cache_entry_release_unlock(ce);
439}
440
441
442
443
444
445
446
447
448void
449mb_cache_entry_free(struct mb_cache_entry *ce)
450{
451 spin_lock(&mb_cache_spinlock);
452 mb_assert(list_empty(&ce->e_lru_list));
453 __mb_cache_entry_unhash(ce);
454 __mb_cache_entry_release_unlock(ce);
455}
456
457
458
459
460
461
462
463
464
465
466struct mb_cache_entry *
467mb_cache_entry_get(struct mb_cache *cache, struct block_device *bdev,
468 sector_t block)
469{
470 unsigned int bucket;
471 struct list_head *l;
472 struct mb_cache_entry *ce;
473
474 bucket = hash_long((unsigned long)bdev + (block & 0xffffffff),
475 cache->c_bucket_bits);
476 spin_lock(&mb_cache_spinlock);
477 list_for_each(l, &cache->c_block_hash[bucket]) {
478 ce = list_entry(l, struct mb_cache_entry, e_block_list);
479 if (ce->e_bdev == bdev && ce->e_block == block) {
480 DEFINE_WAIT(wait);
481
482 if (!list_empty(&ce->e_lru_list))
483 list_del_init(&ce->e_lru_list);
484
485 while (ce->e_used > 0) {
486 ce->e_queued++;
487 prepare_to_wait(&mb_cache_queue, &wait,
488 TASK_UNINTERRUPTIBLE);
489 spin_unlock(&mb_cache_spinlock);
490 schedule();
491 spin_lock(&mb_cache_spinlock);
492 ce->e_queued--;
493 }
494 finish_wait(&mb_cache_queue, &wait);
495 ce->e_used += 1 + MB_CACHE_WRITER;
496
497 if (!__mb_cache_entry_is_hashed(ce)) {
498 __mb_cache_entry_release_unlock(ce);
499 return NULL;
500 }
501 goto cleanup;
502 }
503 }
504 ce = NULL;
505
506cleanup:
507 spin_unlock(&mb_cache_spinlock);
508 return ce;
509}
510
511#if !defined(MB_CACHE_INDEXES_COUNT) || (MB_CACHE_INDEXES_COUNT > 0)
512
513static struct mb_cache_entry *
514__mb_cache_entry_find(struct list_head *l, struct list_head *head,
515 struct block_device *bdev, unsigned int key)
516{
517 while (l != head) {
518 struct mb_cache_entry *ce =
519 list_entry(l, struct mb_cache_entry, e_index.o_list);
520 if (ce->e_bdev == bdev && ce->e_index.o_key == key) {
521 DEFINE_WAIT(wait);
522
523 if (!list_empty(&ce->e_lru_list))
524 list_del_init(&ce->e_lru_list);
525
526
527
528 ce->e_used++;
529 while (ce->e_used >= MB_CACHE_WRITER) {
530 ce->e_queued++;
531 prepare_to_wait(&mb_cache_queue, &wait,
532 TASK_UNINTERRUPTIBLE);
533 spin_unlock(&mb_cache_spinlock);
534 schedule();
535 spin_lock(&mb_cache_spinlock);
536 ce->e_queued--;
537 }
538 finish_wait(&mb_cache_queue, &wait);
539
540 if (!__mb_cache_entry_is_hashed(ce)) {
541 __mb_cache_entry_release_unlock(ce);
542 spin_lock(&mb_cache_spinlock);
543 return ERR_PTR(-EAGAIN);
544 }
545 return ce;
546 }
547 l = l->next;
548 }
549 return NULL;
550}
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565struct mb_cache_entry *
566mb_cache_entry_find_first(struct mb_cache *cache, struct block_device *bdev,
567 unsigned int key)
568{
569 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
570 struct list_head *l;
571 struct mb_cache_entry *ce;
572
573 spin_lock(&mb_cache_spinlock);
574 l = cache->c_index_hash[bucket].next;
575 ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key);
576 spin_unlock(&mb_cache_spinlock);
577 return ce;
578}
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599struct mb_cache_entry *
600mb_cache_entry_find_next(struct mb_cache_entry *prev,
601 struct block_device *bdev, unsigned int key)
602{
603 struct mb_cache *cache = prev->e_cache;
604 unsigned int bucket = hash_long(key, cache->c_bucket_bits);
605 struct list_head *l;
606 struct mb_cache_entry *ce;
607
608 spin_lock(&mb_cache_spinlock);
609 l = prev->e_index.o_list.next;
610 ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key);
611 __mb_cache_entry_release_unlock(prev);
612 return ce;
613}
614
615#endif
616
617static int __init init_mbcache(void)
618{
619 register_shrinker(&mb_cache_shrinker);
620 return 0;
621}
622
623static void __exit exit_mbcache(void)
624{
625 unregister_shrinker(&mb_cache_shrinker);
626}
627
628module_init(init_mbcache)
629module_exit(exit_mbcache)
630
631