diff options
Diffstat (limited to 'fs/mbcache.c')
-rw-r--r-- | fs/mbcache.c | 168 |
1 files changed, 50 insertions, 118 deletions
diff --git a/fs/mbcache.c b/fs/mbcache.c index e28f21b95344..cf4e6cdfd15b 100644 --- a/fs/mbcache.c +++ b/fs/mbcache.c | |||
@@ -79,15 +79,11 @@ EXPORT_SYMBOL(mb_cache_entry_find_next); | |||
79 | struct mb_cache { | 79 | struct mb_cache { |
80 | struct list_head c_cache_list; | 80 | struct list_head c_cache_list; |
81 | const char *c_name; | 81 | const char *c_name; |
82 | struct mb_cache_op c_op; | ||
83 | atomic_t c_entry_count; | 82 | atomic_t c_entry_count; |
84 | int c_bucket_bits; | 83 | int c_bucket_bits; |
85 | #ifndef MB_CACHE_INDEXES_COUNT | 84 | struct kmem_cache *c_entry_cache; |
86 | int c_indexes_count; | ||
87 | #endif | ||
88 | struct kmem_cache *c_entry_cache; | ||
89 | struct list_head *c_block_hash; | 85 | struct list_head *c_block_hash; |
90 | struct list_head *c_indexes_hash[0]; | 86 | struct list_head *c_index_hash; |
91 | }; | 87 | }; |
92 | 88 | ||
93 | 89 | ||
@@ -101,16 +97,6 @@ static LIST_HEAD(mb_cache_list); | |||
101 | static LIST_HEAD(mb_cache_lru_list); | 97 | static LIST_HEAD(mb_cache_lru_list); |
102 | static DEFINE_SPINLOCK(mb_cache_spinlock); | 98 | static DEFINE_SPINLOCK(mb_cache_spinlock); |
103 | 99 | ||
104 | static inline int | ||
105 | mb_cache_indexes(struct mb_cache *cache) | ||
106 | { | ||
107 | #ifdef MB_CACHE_INDEXES_COUNT | ||
108 | return MB_CACHE_INDEXES_COUNT; | ||
109 | #else | ||
110 | return cache->c_indexes_count; | ||
111 | #endif | ||
112 | } | ||
113 | |||
114 | /* | 100 | /* |
115 | * What the mbcache registers as to get shrunk dynamically. | 101 | * What the mbcache registers as to get shrunk dynamically. |
116 | */ | 102 | */ |
@@ -132,12 +118,9 @@ __mb_cache_entry_is_hashed(struct mb_cache_entry *ce) | |||
132 | static void | 118 | static void |
133 | __mb_cache_entry_unhash(struct mb_cache_entry *ce) | 119 | __mb_cache_entry_unhash(struct mb_cache_entry *ce) |
134 | { | 120 | { |
135 | int n; | ||
136 | |||
137 | if (__mb_cache_entry_is_hashed(ce)) { | 121 | if (__mb_cache_entry_is_hashed(ce)) { |
138 | list_del_init(&ce->e_block_list); | 122 | list_del_init(&ce->e_block_list); |
139 | for (n=0; n<mb_cache_indexes(ce->e_cache); n++) | 123 | list_del(&ce->e_index.o_list); |
140 | list_del(&ce->e_indexes[n].o_list); | ||
141 | } | 124 | } |
142 | } | 125 | } |
143 | 126 | ||
@@ -148,16 +131,8 @@ __mb_cache_entry_forget(struct mb_cache_entry *ce, gfp_t gfp_mask) | |||
148 | struct mb_cache *cache = ce->e_cache; | 131 | struct mb_cache *cache = ce->e_cache; |
149 | 132 | ||
150 | mb_assert(!(ce->e_used || ce->e_queued)); | 133 | mb_assert(!(ce->e_used || ce->e_queued)); |
151 | if (cache->c_op.free && cache->c_op.free(ce, gfp_mask)) { | 134 | kmem_cache_free(cache->c_entry_cache, ce); |
152 | /* free failed -- put back on the lru list | 135 | atomic_dec(&cache->c_entry_count); |
153 | for freeing later. */ | ||
154 | spin_lock(&mb_cache_spinlock); | ||
155 | list_add(&ce->e_lru_list, &mb_cache_lru_list); | ||
156 | spin_unlock(&mb_cache_spinlock); | ||
157 | } else { | ||
158 | kmem_cache_free(cache->c_entry_cache, ce); | ||
159 | atomic_dec(&cache->c_entry_count); | ||
160 | } | ||
161 | } | 136 | } |
162 | 137 | ||
163 | 138 | ||
@@ -201,22 +176,12 @@ static int | |||
201 | mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) | 176 | mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) |
202 | { | 177 | { |
203 | LIST_HEAD(free_list); | 178 | LIST_HEAD(free_list); |
204 | struct list_head *l, *ltmp; | 179 | struct mb_cache *cache; |
180 | struct mb_cache_entry *entry, *tmp; | ||
205 | int count = 0; | 181 | int count = 0; |
206 | 182 | ||
207 | spin_lock(&mb_cache_spinlock); | ||
208 | list_for_each(l, &mb_cache_list) { | ||
209 | struct mb_cache *cache = | ||
210 | list_entry(l, struct mb_cache, c_cache_list); | ||
211 | mb_debug("cache %s (%d)", cache->c_name, | ||
212 | atomic_read(&cache->c_entry_count)); | ||
213 | count += atomic_read(&cache->c_entry_count); | ||
214 | } | ||
215 | mb_debug("trying to free %d entries", nr_to_scan); | 183 | mb_debug("trying to free %d entries", nr_to_scan); |
216 | if (nr_to_scan == 0) { | 184 | spin_lock(&mb_cache_spinlock); |
217 | spin_unlock(&mb_cache_spinlock); | ||
218 | goto out; | ||
219 | } | ||
220 | while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) { | 185 | while (nr_to_scan-- && !list_empty(&mb_cache_lru_list)) { |
221 | struct mb_cache_entry *ce = | 186 | struct mb_cache_entry *ce = |
222 | list_entry(mb_cache_lru_list.next, | 187 | list_entry(mb_cache_lru_list.next, |
@@ -224,12 +189,15 @@ mb_cache_shrink_fn(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) | |||
224 | list_move_tail(&ce->e_lru_list, &free_list); | 189 | list_move_tail(&ce->e_lru_list, &free_list); |
225 | __mb_cache_entry_unhash(ce); | 190 | __mb_cache_entry_unhash(ce); |
226 | } | 191 | } |
192 | list_for_each_entry(cache, &mb_cache_list, c_cache_list) { | ||
193 | mb_debug("cache %s (%d)", cache->c_name, | ||
194 | atomic_read(&cache->c_entry_count)); | ||
195 | count += atomic_read(&cache->c_entry_count); | ||
196 | } | ||
227 | spin_unlock(&mb_cache_spinlock); | 197 | spin_unlock(&mb_cache_spinlock); |
228 | list_for_each_safe(l, ltmp, &free_list) { | 198 | list_for_each_entry_safe(entry, tmp, &free_list, e_lru_list) { |
229 | __mb_cache_entry_forget(list_entry(l, struct mb_cache_entry, | 199 | __mb_cache_entry_forget(entry, gfp_mask); |
230 | e_lru_list), gfp_mask); | ||
231 | } | 200 | } |
232 | out: | ||
233 | return (count / 100) * sysctl_vfs_cache_pressure; | 201 | return (count / 100) * sysctl_vfs_cache_pressure; |
234 | } | 202 | } |
235 | 203 | ||
@@ -243,72 +211,49 @@ out: | |||
243 | * memory was available. | 211 | * memory was available. |
244 | * | 212 | * |
245 | * @name: name of the cache (informal) | 213 | * @name: name of the cache (informal) |
246 | * @cache_op: contains the callback called when freeing a cache entry | ||
247 | * @entry_size: The size of a cache entry, including | ||
248 | * struct mb_cache_entry | ||
249 | * @indexes_count: number of additional indexes in the cache. Must equal | ||
250 | * MB_CACHE_INDEXES_COUNT if the number of indexes is | ||
251 | * hardwired. | ||
252 | * @bucket_bits: log2(number of hash buckets) | 214 | * @bucket_bits: log2(number of hash buckets) |
253 | */ | 215 | */ |
254 | struct mb_cache * | 216 | struct mb_cache * |
255 | mb_cache_create(const char *name, struct mb_cache_op *cache_op, | 217 | mb_cache_create(const char *name, int bucket_bits) |
256 | size_t entry_size, int indexes_count, int bucket_bits) | ||
257 | { | 218 | { |
258 | int m=0, n, bucket_count = 1 << bucket_bits; | 219 | int n, bucket_count = 1 << bucket_bits; |
259 | struct mb_cache *cache = NULL; | 220 | struct mb_cache *cache = NULL; |
260 | 221 | ||
261 | if(entry_size < sizeof(struct mb_cache_entry) + | 222 | cache = kmalloc(sizeof(struct mb_cache), GFP_KERNEL); |
262 | indexes_count * sizeof(((struct mb_cache_entry *) 0)->e_indexes[0])) | ||
263 | return NULL; | ||
264 | |||
265 | cache = kmalloc(sizeof(struct mb_cache) + | ||
266 | indexes_count * sizeof(struct list_head), GFP_KERNEL); | ||
267 | if (!cache) | 223 | if (!cache) |
268 | goto fail; | 224 | return NULL; |
269 | cache->c_name = name; | 225 | cache->c_name = name; |
270 | cache->c_op.free = NULL; | ||
271 | if (cache_op) | ||
272 | cache->c_op.free = cache_op->free; | ||
273 | atomic_set(&cache->c_entry_count, 0); | 226 | atomic_set(&cache->c_entry_count, 0); |
274 | cache->c_bucket_bits = bucket_bits; | 227 | cache->c_bucket_bits = bucket_bits; |
275 | #ifdef MB_CACHE_INDEXES_COUNT | ||
276 | mb_assert(indexes_count == MB_CACHE_INDEXES_COUNT); | ||
277 | #else | ||
278 | cache->c_indexes_count = indexes_count; | ||
279 | #endif | ||
280 | cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head), | 228 | cache->c_block_hash = kmalloc(bucket_count * sizeof(struct list_head), |
281 | GFP_KERNEL); | 229 | GFP_KERNEL); |
282 | if (!cache->c_block_hash) | 230 | if (!cache->c_block_hash) |
283 | goto fail; | 231 | goto fail; |
284 | for (n=0; n<bucket_count; n++) | 232 | for (n=0; n<bucket_count; n++) |
285 | INIT_LIST_HEAD(&cache->c_block_hash[n]); | 233 | INIT_LIST_HEAD(&cache->c_block_hash[n]); |
286 | for (m=0; m<indexes_count; m++) { | 234 | cache->c_index_hash = kmalloc(bucket_count * sizeof(struct list_head), |
287 | cache->c_indexes_hash[m] = kmalloc(bucket_count * | 235 | GFP_KERNEL); |
288 | sizeof(struct list_head), | 236 | if (!cache->c_index_hash) |
289 | GFP_KERNEL); | 237 | goto fail; |
290 | if (!cache->c_indexes_hash[m]) | 238 | for (n=0; n<bucket_count; n++) |
291 | goto fail; | 239 | INIT_LIST_HEAD(&cache->c_index_hash[n]); |
292 | for (n=0; n<bucket_count; n++) | 240 | cache->c_entry_cache = kmem_cache_create(name, |
293 | INIT_LIST_HEAD(&cache->c_indexes_hash[m][n]); | 241 | sizeof(struct mb_cache_entry), 0, |
294 | } | ||
295 | cache->c_entry_cache = kmem_cache_create(name, entry_size, 0, | ||
296 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL); | 242 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL); |
297 | if (!cache->c_entry_cache) | 243 | if (!cache->c_entry_cache) |
298 | goto fail; | 244 | goto fail2; |
299 | 245 | ||
300 | spin_lock(&mb_cache_spinlock); | 246 | spin_lock(&mb_cache_spinlock); |
301 | list_add(&cache->c_cache_list, &mb_cache_list); | 247 | list_add(&cache->c_cache_list, &mb_cache_list); |
302 | spin_unlock(&mb_cache_spinlock); | 248 | spin_unlock(&mb_cache_spinlock); |
303 | return cache; | 249 | return cache; |
304 | 250 | ||
251 | fail2: | ||
252 | kfree(cache->c_index_hash); | ||
253 | |||
305 | fail: | 254 | fail: |
306 | if (cache) { | 255 | kfree(cache->c_block_hash); |
307 | while (--m >= 0) | 256 | kfree(cache); |
308 | kfree(cache->c_indexes_hash[m]); | ||
309 | kfree(cache->c_block_hash); | ||
310 | kfree(cache); | ||
311 | } | ||
312 | return NULL; | 257 | return NULL; |
313 | } | 258 | } |
314 | 259 | ||
@@ -357,7 +302,6 @@ mb_cache_destroy(struct mb_cache *cache) | |||
357 | { | 302 | { |
358 | LIST_HEAD(free_list); | 303 | LIST_HEAD(free_list); |
359 | struct list_head *l, *ltmp; | 304 | struct list_head *l, *ltmp; |
360 | int n; | ||
361 | 305 | ||
362 | spin_lock(&mb_cache_spinlock); | 306 | spin_lock(&mb_cache_spinlock); |
363 | list_for_each_safe(l, ltmp, &mb_cache_lru_list) { | 307 | list_for_each_safe(l, ltmp, &mb_cache_lru_list) { |
@@ -384,8 +328,7 @@ mb_cache_destroy(struct mb_cache *cache) | |||
384 | 328 | ||
385 | kmem_cache_destroy(cache->c_entry_cache); | 329 | kmem_cache_destroy(cache->c_entry_cache); |
386 | 330 | ||
387 | for (n=0; n < mb_cache_indexes(cache); n++) | 331 | kfree(cache->c_index_hash); |
388 | kfree(cache->c_indexes_hash[n]); | ||
389 | kfree(cache->c_block_hash); | 332 | kfree(cache->c_block_hash); |
390 | kfree(cache); | 333 | kfree(cache); |
391 | } | 334 | } |
@@ -429,17 +372,16 @@ mb_cache_entry_alloc(struct mb_cache *cache, gfp_t gfp_flags) | |||
429 | * | 372 | * |
430 | * @bdev: device the cache entry belongs to | 373 | * @bdev: device the cache entry belongs to |
431 | * @block: block number | 374 | * @block: block number |
432 | * @keys: array of additional keys. There must be indexes_count entries | 375 | * @key: lookup key |
433 | * in the array (as specified when creating the cache). | ||
434 | */ | 376 | */ |
435 | int | 377 | int |
436 | mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev, | 378 | mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev, |
437 | sector_t block, unsigned int keys[]) | 379 | sector_t block, unsigned int key) |
438 | { | 380 | { |
439 | struct mb_cache *cache = ce->e_cache; | 381 | struct mb_cache *cache = ce->e_cache; |
440 | unsigned int bucket; | 382 | unsigned int bucket; |
441 | struct list_head *l; | 383 | struct list_head *l; |
442 | int error = -EBUSY, n; | 384 | int error = -EBUSY; |
443 | 385 | ||
444 | bucket = hash_long((unsigned long)bdev + (block & 0xffffffff), | 386 | bucket = hash_long((unsigned long)bdev + (block & 0xffffffff), |
445 | cache->c_bucket_bits); | 387 | cache->c_bucket_bits); |
@@ -454,12 +396,9 @@ mb_cache_entry_insert(struct mb_cache_entry *ce, struct block_device *bdev, | |||
454 | ce->e_bdev = bdev; | 396 | ce->e_bdev = bdev; |
455 | ce->e_block = block; | 397 | ce->e_block = block; |
456 | list_add(&ce->e_block_list, &cache->c_block_hash[bucket]); | 398 | list_add(&ce->e_block_list, &cache->c_block_hash[bucket]); |
457 | for (n=0; n<mb_cache_indexes(cache); n++) { | 399 | ce->e_index.o_key = key; |
458 | ce->e_indexes[n].o_key = keys[n]; | 400 | bucket = hash_long(key, cache->c_bucket_bits); |
459 | bucket = hash_long(keys[n], cache->c_bucket_bits); | 401 | list_add(&ce->e_index.o_list, &cache->c_index_hash[bucket]); |
460 | list_add(&ce->e_indexes[n].o_list, | ||
461 | &cache->c_indexes_hash[n][bucket]); | ||
462 | } | ||
463 | error = 0; | 402 | error = 0; |
464 | out: | 403 | out: |
465 | spin_unlock(&mb_cache_spinlock); | 404 | spin_unlock(&mb_cache_spinlock); |
@@ -555,13 +494,12 @@ cleanup: | |||
555 | 494 | ||
556 | static struct mb_cache_entry * | 495 | static struct mb_cache_entry * |
557 | __mb_cache_entry_find(struct list_head *l, struct list_head *head, | 496 | __mb_cache_entry_find(struct list_head *l, struct list_head *head, |
558 | int index, struct block_device *bdev, unsigned int key) | 497 | struct block_device *bdev, unsigned int key) |
559 | { | 498 | { |
560 | while (l != head) { | 499 | while (l != head) { |
561 | struct mb_cache_entry *ce = | 500 | struct mb_cache_entry *ce = |
562 | list_entry(l, struct mb_cache_entry, | 501 | list_entry(l, struct mb_cache_entry, e_index.o_list); |
563 | e_indexes[index].o_list); | 502 | if (ce->e_bdev == bdev && ce->e_index.o_key == key) { |
564 | if (ce->e_bdev == bdev && ce->e_indexes[index].o_key == key) { | ||
565 | DEFINE_WAIT(wait); | 503 | DEFINE_WAIT(wait); |
566 | 504 | ||
567 | if (!list_empty(&ce->e_lru_list)) | 505 | if (!list_empty(&ce->e_lru_list)) |
@@ -603,23 +541,20 @@ __mb_cache_entry_find(struct list_head *l, struct list_head *head, | |||
603 | * returned cache entry is locked for shared access ("multiple readers"). | 541 | * returned cache entry is locked for shared access ("multiple readers"). |
604 | * | 542 | * |
605 | * @cache: the cache to search | 543 | * @cache: the cache to search |
606 | * @index: the number of the additonal index to search (0<=index<indexes_count) | ||
607 | * @bdev: the device the cache entry should belong to | 544 | * @bdev: the device the cache entry should belong to |
608 | * @key: the key in the index | 545 | * @key: the key in the index |
609 | */ | 546 | */ |
610 | struct mb_cache_entry * | 547 | struct mb_cache_entry * |
611 | mb_cache_entry_find_first(struct mb_cache *cache, int index, | 548 | mb_cache_entry_find_first(struct mb_cache *cache, struct block_device *bdev, |
612 | struct block_device *bdev, unsigned int key) | 549 | unsigned int key) |
613 | { | 550 | { |
614 | unsigned int bucket = hash_long(key, cache->c_bucket_bits); | 551 | unsigned int bucket = hash_long(key, cache->c_bucket_bits); |
615 | struct list_head *l; | 552 | struct list_head *l; |
616 | struct mb_cache_entry *ce; | 553 | struct mb_cache_entry *ce; |
617 | 554 | ||
618 | mb_assert(index < mb_cache_indexes(cache)); | ||
619 | spin_lock(&mb_cache_spinlock); | 555 | spin_lock(&mb_cache_spinlock); |
620 | l = cache->c_indexes_hash[index][bucket].next; | 556 | l = cache->c_index_hash[bucket].next; |
621 | ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket], | 557 | ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key); |
622 | index, bdev, key); | ||
623 | spin_unlock(&mb_cache_spinlock); | 558 | spin_unlock(&mb_cache_spinlock); |
624 | return ce; | 559 | return ce; |
625 | } | 560 | } |
@@ -640,12 +575,11 @@ mb_cache_entry_find_first(struct mb_cache *cache, int index, | |||
640 | * } | 575 | * } |
641 | * | 576 | * |
642 | * @prev: The previous match | 577 | * @prev: The previous match |
643 | * @index: the number of the additonal index to search (0<=index<indexes_count) | ||
644 | * @bdev: the device the cache entry should belong to | 578 | * @bdev: the device the cache entry should belong to |
645 | * @key: the key in the index | 579 | * @key: the key in the index |
646 | */ | 580 | */ |
647 | struct mb_cache_entry * | 581 | struct mb_cache_entry * |
648 | mb_cache_entry_find_next(struct mb_cache_entry *prev, int index, | 582 | mb_cache_entry_find_next(struct mb_cache_entry *prev, |
649 | struct block_device *bdev, unsigned int key) | 583 | struct block_device *bdev, unsigned int key) |
650 | { | 584 | { |
651 | struct mb_cache *cache = prev->e_cache; | 585 | struct mb_cache *cache = prev->e_cache; |
@@ -653,11 +587,9 @@ mb_cache_entry_find_next(struct mb_cache_entry *prev, int index, | |||
653 | struct list_head *l; | 587 | struct list_head *l; |
654 | struct mb_cache_entry *ce; | 588 | struct mb_cache_entry *ce; |
655 | 589 | ||
656 | mb_assert(index < mb_cache_indexes(cache)); | ||
657 | spin_lock(&mb_cache_spinlock); | 590 | spin_lock(&mb_cache_spinlock); |
658 | l = prev->e_indexes[index].o_list.next; | 591 | l = prev->e_index.o_list.next; |
659 | ce = __mb_cache_entry_find(l, &cache->c_indexes_hash[index][bucket], | 592 | ce = __mb_cache_entry_find(l, &cache->c_index_hash[bucket], bdev, key); |
660 | index, bdev, key); | ||
661 | __mb_cache_entry_release_unlock(prev); | 593 | __mb_cache_entry_release_unlock(prev); |
662 | return ce; | 594 | return ce; |
663 | } | 595 | } |