x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / md / dm-region-hash.c
blob36dbe29f2fd60e775b05a0c0562e4741a42a8d08
1 /*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include <linux/dm-dirty-log.h>
9 #include <linux/dm-region-hash.h>
11 #include <linux/ctype.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/vmalloc.h>
16 #include "dm.h"
18 #define DM_MSG_PREFIX "region hash"
20 /*-----------------------------------------------------------------
21 * Region hash
23 * The mirror splits itself up into discrete regions. Each
24 * region can be in one of three states: clean, dirty,
25 * nosync. There is no need to put clean regions in the hash.
27 * In addition to being present in the hash table a region _may_
28 * be present on one of three lists.
30 * clean_regions: Regions on this list have no io pending to
31 * them, they are in sync, we are no longer interested in them,
32 * they are dull. dm_rh_update_states() will remove them from the
33 * hash table.
35 * quiesced_regions: These regions have been spun down, ready
36 * for recovery. rh_recovery_start() will remove regions from
37 * this list and hand them to kmirrord, which will schedule the
38 * recovery io with kcopyd.
40 * recovered_regions: Regions that kcopyd has successfully
41 * recovered. dm_rh_update_states() will now schedule any delayed
42 * io, up the recovery_count, and remove the region from the
43 * hash.
45 * There are 2 locks:
46 * A rw spin lock 'hash_lock' protects just the hash table,
47 * this is never held in write mode from interrupt context,
48 * which I believe means that we only have to disable irqs when
49 * doing a write lock.
51 * An ordinary spin lock 'region_lock' that protects the three
52 * lists in the region_hash, with the 'state', 'list' and
53 * 'delayed_bios' fields of the regions. This is used from irq
54 * context, so all other uses will have to suspend local irqs.
55 *---------------------------------------------------------------*/
56 struct dm_region_hash {
57 uint32_t region_size;
58 unsigned region_shift;
60 /* holds persistent region state */
61 struct dm_dirty_log *log;
63 /* hash table */
64 rwlock_t hash_lock;
65 mempool_t *region_pool;
66 unsigned mask;
67 unsigned nr_buckets;
68 unsigned prime;
69 unsigned shift;
70 struct list_head *buckets;
72 unsigned max_recovery; /* Max # of regions to recover in parallel */
74 spinlock_t region_lock;
75 atomic_t recovery_in_flight;
76 struct semaphore recovery_count;
77 struct list_head clean_regions;
78 struct list_head quiesced_regions;
79 struct list_head recovered_regions;
80 struct list_head failed_recovered_regions;
82 void *context;
83 sector_t target_begin;
85 /* Callback function to schedule bios writes */
86 void (*dispatch_bios)(void *context, struct bio_list *bios);
88 /* Callback function to wakeup callers worker thread. */
89 void (*wakeup_workers)(void *context);
91 /* Callback function to wakeup callers recovery waiters. */
92 void (*wakeup_all_recovery_waiters)(void *context);
95 struct dm_region {
96 struct dm_region_hash *rh; /* FIXME: can we get rid of this ? */
97 region_t key;
98 int state;
100 struct list_head hash_list;
101 struct list_head list;
103 atomic_t pending;
104 struct bio_list delayed_bios;
108 * Conversion fns
110 static region_t dm_rh_sector_to_region(struct dm_region_hash *rh, sector_t sector)
112 return sector >> rh->region_shift;
115 sector_t dm_rh_region_to_sector(struct dm_region_hash *rh, region_t region)
117 return region << rh->region_shift;
119 EXPORT_SYMBOL_GPL(dm_rh_region_to_sector);
121 region_t dm_rh_bio_to_region(struct dm_region_hash *rh, struct bio *bio)
123 return dm_rh_sector_to_region(rh, bio->bi_sector - rh->target_begin);
125 EXPORT_SYMBOL_GPL(dm_rh_bio_to_region);
127 void *dm_rh_region_context(struct dm_region *reg)
129 return reg->rh->context;
131 EXPORT_SYMBOL_GPL(dm_rh_region_context);
133 region_t dm_rh_get_region_key(struct dm_region *reg)
135 return reg->key;
137 EXPORT_SYMBOL_GPL(dm_rh_get_region_key);
139 sector_t dm_rh_get_region_size(struct dm_region_hash *rh)
141 return rh->region_size;
143 EXPORT_SYMBOL_GPL(dm_rh_get_region_size);
146 * FIXME: shall we pass in a structure instead of all these args to
147 * dm_region_hash_create()????
149 #define RH_HASH_MULT 2654435387U
150 #define RH_HASH_SHIFT 12
152 #define MIN_REGIONS 64
153 struct dm_region_hash *dm_region_hash_create(
154 void *context, void (*dispatch_bios)(void *context,
155 struct bio_list *bios),
156 void (*wakeup_workers)(void *context),
157 void (*wakeup_all_recovery_waiters)(void *context),
158 sector_t target_begin, unsigned max_recovery,
159 struct dm_dirty_log *log, uint32_t region_size,
160 region_t nr_regions)
162 struct dm_region_hash *rh;
163 unsigned nr_buckets, max_buckets;
164 size_t i;
167 * Calculate a suitable number of buckets for our hash
168 * table.
170 max_buckets = nr_regions >> 6;
171 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
173 nr_buckets >>= 1;
175 rh = kmalloc(sizeof(*rh), GFP_KERNEL);
176 if (!rh) {
177 DMERR("unable to allocate region hash memory");
178 return ERR_PTR(-ENOMEM);
181 rh->context = context;
182 rh->dispatch_bios = dispatch_bios;
183 rh->wakeup_workers = wakeup_workers;
184 rh->wakeup_all_recovery_waiters = wakeup_all_recovery_waiters;
185 rh->target_begin = target_begin;
186 rh->max_recovery = max_recovery;
187 rh->log = log;
188 rh->region_size = region_size;
189 rh->region_shift = ffs(region_size) - 1;
190 rwlock_init(&rh->hash_lock);
191 rh->mask = nr_buckets - 1;
192 rh->nr_buckets = nr_buckets;
194 rh->shift = RH_HASH_SHIFT;
195 rh->prime = RH_HASH_MULT;
197 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
198 if (!rh->buckets) {
199 DMERR("unable to allocate region hash bucket memory");
200 kfree(rh);
201 return ERR_PTR(-ENOMEM);
204 for (i = 0; i < nr_buckets; i++)
205 INIT_LIST_HEAD(rh->buckets + i);
207 spin_lock_init(&rh->region_lock);
208 sema_init(&rh->recovery_count, 0);
209 atomic_set(&rh->recovery_in_flight, 0);
210 INIT_LIST_HEAD(&rh->clean_regions);
211 INIT_LIST_HEAD(&rh->quiesced_regions);
212 INIT_LIST_HEAD(&rh->recovered_regions);
213 INIT_LIST_HEAD(&rh->failed_recovered_regions);
215 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
216 sizeof(struct dm_region));
217 if (!rh->region_pool) {
218 vfree(rh->buckets);
219 kfree(rh);
220 rh = ERR_PTR(-ENOMEM);
223 return rh;
225 EXPORT_SYMBOL_GPL(dm_region_hash_create);
227 void dm_region_hash_destroy(struct dm_region_hash *rh)
229 unsigned h;
230 struct dm_region *reg, *nreg;
232 BUG_ON(!list_empty(&rh->quiesced_regions));
233 for (h = 0; h < rh->nr_buckets; h++) {
234 list_for_each_entry_safe(reg, nreg, rh->buckets + h,
235 hash_list) {
236 BUG_ON(atomic_read(&reg->pending));
237 mempool_free(reg, rh->region_pool);
241 if (rh->log)
242 dm_dirty_log_destroy(rh->log);
244 if (rh->region_pool)
245 mempool_destroy(rh->region_pool);
247 vfree(rh->buckets);
248 kfree(rh);
250 EXPORT_SYMBOL_GPL(dm_region_hash_destroy);
252 struct dm_dirty_log *dm_rh_dirty_log(struct dm_region_hash *rh)
254 return rh->log;
256 EXPORT_SYMBOL_GPL(dm_rh_dirty_log);
258 static unsigned rh_hash(struct dm_region_hash *rh, region_t region)
260 return (unsigned) ((region * rh->prime) >> rh->shift) & rh->mask;
263 static struct dm_region *__rh_lookup(struct dm_region_hash *rh, region_t region)
265 struct dm_region *reg;
266 struct list_head *bucket = rh->buckets + rh_hash(rh, region);
268 list_for_each_entry(reg, bucket, hash_list)
269 if (reg->key == region)
270 return reg;
272 return NULL;
275 static void __rh_insert(struct dm_region_hash *rh, struct dm_region *reg)
277 list_add(&reg->hash_list, rh->buckets + rh_hash(rh, reg->key));
280 static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region)
282 struct dm_region *reg, *nreg;
284 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
285 if (unlikely(!nreg))
286 nreg = kmalloc(sizeof(*nreg), GFP_NOIO | __GFP_NOFAIL);
288 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
289 DM_RH_CLEAN : DM_RH_NOSYNC;
290 nreg->rh = rh;
291 nreg->key = region;
292 INIT_LIST_HEAD(&nreg->list);
293 atomic_set(&nreg->pending, 0);
294 bio_list_init(&nreg->delayed_bios);
296 write_lock_irq(&rh->hash_lock);
297 reg = __rh_lookup(rh, region);
298 if (reg)
299 /* We lost the race. */
300 mempool_free(nreg, rh->region_pool);
301 else {
302 __rh_insert(rh, nreg);
303 if (nreg->state == DM_RH_CLEAN) {
304 spin_lock(&rh->region_lock);
305 list_add(&nreg->list, &rh->clean_regions);
306 spin_unlock(&rh->region_lock);
309 reg = nreg;
311 write_unlock_irq(&rh->hash_lock);
313 return reg;
316 static struct dm_region *__rh_find(struct dm_region_hash *rh, region_t region)
318 struct dm_region *reg;
320 reg = __rh_lookup(rh, region);
321 if (!reg) {
322 read_unlock(&rh->hash_lock);
323 reg = __rh_alloc(rh, region);
324 read_lock(&rh->hash_lock);
327 return reg;
330 int dm_rh_get_state(struct dm_region_hash *rh, region_t region, int may_block)
332 int r;
333 struct dm_region *reg;
335 read_lock(&rh->hash_lock);
336 reg = __rh_lookup(rh, region);
337 read_unlock(&rh->hash_lock);
339 if (reg)
340 return reg->state;
343 * The region wasn't in the hash, so we fall back to the
344 * dirty log.
346 r = rh->log->type->in_sync(rh->log, region, may_block);
349 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
350 * taken as a DM_RH_NOSYNC
352 return r == 1 ? DM_RH_CLEAN : DM_RH_NOSYNC;
354 EXPORT_SYMBOL_GPL(dm_rh_get_state);
356 static void complete_resync_work(struct dm_region *reg, int success)
358 struct dm_region_hash *rh = reg->rh;
360 rh->log->type->set_region_sync(rh->log, reg->key, success);
363 * Dispatch the bios before we call 'wake_up_all'.
364 * This is important because if we are suspending,
365 * we want to know that recovery is complete and
366 * the work queue is flushed. If we wake_up_all
367 * before we dispatch_bios (queue bios and call wake()),
368 * then we risk suspending before the work queue
369 * has been properly flushed.
371 rh->dispatch_bios(rh->context, &reg->delayed_bios);
372 if (atomic_dec_and_test(&rh->recovery_in_flight))
373 rh->wakeup_all_recovery_waiters(rh->context);
374 up(&rh->recovery_count);
377 /* dm_rh_mark_nosync
378 * @ms
379 * @bio
380 * @done
381 * @error
383 * The bio was written on some mirror(s) but failed on other mirror(s).
384 * We can successfully endio the bio but should avoid the region being
385 * marked clean by setting the state DM_RH_NOSYNC.
387 * This function is _not_ safe in interrupt context!
389 void dm_rh_mark_nosync(struct dm_region_hash *rh,
390 struct bio *bio, unsigned done, int error)
392 unsigned long flags;
393 struct dm_dirty_log *log = rh->log;
394 struct dm_region *reg;
395 region_t region = dm_rh_bio_to_region(rh, bio);
396 int recovering = 0;
398 /* We must inform the log that the sync count has changed. */
399 log->type->set_region_sync(log, region, 0);
401 read_lock(&rh->hash_lock);
402 reg = __rh_find(rh, region);
403 read_unlock(&rh->hash_lock);
405 /* region hash entry should exist because write was in-flight */
406 BUG_ON(!reg);
407 BUG_ON(!list_empty(&reg->list));
409 spin_lock_irqsave(&rh->region_lock, flags);
411 * Possible cases:
412 * 1) DM_RH_DIRTY
413 * 2) DM_RH_NOSYNC: was dirty, other preceeding writes failed
414 * 3) DM_RH_RECOVERING: flushing pending writes
415 * Either case, the region should have not been connected to list.
417 recovering = (reg->state == DM_RH_RECOVERING);
418 reg->state = DM_RH_NOSYNC;
419 BUG_ON(!list_empty(&reg->list));
420 spin_unlock_irqrestore(&rh->region_lock, flags);
422 bio_endio(bio, error);
423 if (recovering)
424 complete_resync_work(reg, 0);
426 EXPORT_SYMBOL_GPL(dm_rh_mark_nosync);
428 void dm_rh_update_states(struct dm_region_hash *rh, int errors_handled)
430 struct dm_region *reg, *next;
432 LIST_HEAD(clean);
433 LIST_HEAD(recovered);
434 LIST_HEAD(failed_recovered);
437 * Quickly grab the lists.
439 write_lock_irq(&rh->hash_lock);
440 spin_lock(&rh->region_lock);
441 if (!list_empty(&rh->clean_regions)) {
442 list_splice_init(&rh->clean_regions, &clean);
444 list_for_each_entry(reg, &clean, list)
445 list_del(&reg->hash_list);
448 if (!list_empty(&rh->recovered_regions)) {
449 list_splice_init(&rh->recovered_regions, &recovered);
451 list_for_each_entry(reg, &recovered, list)
452 list_del(&reg->hash_list);
455 if (!list_empty(&rh->failed_recovered_regions)) {
456 list_splice_init(&rh->failed_recovered_regions,
457 &failed_recovered);
459 list_for_each_entry(reg, &failed_recovered, list)
460 list_del(&reg->hash_list);
463 spin_unlock(&rh->region_lock);
464 write_unlock_irq(&rh->hash_lock);
467 * All the regions on the recovered and clean lists have
468 * now been pulled out of the system, so no need to do
469 * any more locking.
471 list_for_each_entry_safe(reg, next, &recovered, list) {
472 rh->log->type->clear_region(rh->log, reg->key);
473 complete_resync_work(reg, 1);
474 mempool_free(reg, rh->region_pool);
477 list_for_each_entry_safe(reg, next, &failed_recovered, list) {
478 complete_resync_work(reg, errors_handled ? 0 : 1);
479 mempool_free(reg, rh->region_pool);
482 list_for_each_entry_safe(reg, next, &clean, list) {
483 rh->log->type->clear_region(rh->log, reg->key);
484 mempool_free(reg, rh->region_pool);
487 rh->log->type->flush(rh->log);
489 EXPORT_SYMBOL_GPL(dm_rh_update_states);
491 static void rh_inc(struct dm_region_hash *rh, region_t region)
493 struct dm_region *reg;
495 read_lock(&rh->hash_lock);
496 reg = __rh_find(rh, region);
498 spin_lock_irq(&rh->region_lock);
499 atomic_inc(&reg->pending);
501 if (reg->state == DM_RH_CLEAN) {
502 reg->state = DM_RH_DIRTY;
503 list_del_init(&reg->list); /* take off the clean list */
504 spin_unlock_irq(&rh->region_lock);
506 rh->log->type->mark_region(rh->log, reg->key);
507 } else
508 spin_unlock_irq(&rh->region_lock);
511 read_unlock(&rh->hash_lock);
514 void dm_rh_inc_pending(struct dm_region_hash *rh, struct bio_list *bios)
516 struct bio *bio;
518 for (bio = bios->head; bio; bio = bio->bi_next)
519 rh_inc(rh, dm_rh_bio_to_region(rh, bio));
521 EXPORT_SYMBOL_GPL(dm_rh_inc_pending);
523 void dm_rh_dec(struct dm_region_hash *rh, region_t region)
525 unsigned long flags;
526 struct dm_region *reg;
527 int should_wake = 0;
529 read_lock(&rh->hash_lock);
530 reg = __rh_lookup(rh, region);
531 read_unlock(&rh->hash_lock);
533 spin_lock_irqsave(&rh->region_lock, flags);
534 if (atomic_dec_and_test(&reg->pending)) {
536 * There is no pending I/O for this region.
537 * We can move the region to corresponding list for next action.
538 * At this point, the region is not yet connected to any list.
540 * If the state is DM_RH_NOSYNC, the region should be kept off
541 * from clean list.
542 * The hash entry for DM_RH_NOSYNC will remain in memory
543 * until the region is recovered or the map is reloaded.
546 /* do nothing for DM_RH_NOSYNC */
547 if (reg->state == DM_RH_RECOVERING) {
548 list_add_tail(&reg->list, &rh->quiesced_regions);
549 } else if (reg->state == DM_RH_DIRTY) {
550 reg->state = DM_RH_CLEAN;
551 list_add(&reg->list, &rh->clean_regions);
553 should_wake = 1;
555 spin_unlock_irqrestore(&rh->region_lock, flags);
557 if (should_wake)
558 rh->wakeup_workers(rh->context);
560 EXPORT_SYMBOL_GPL(dm_rh_dec);
563 * Starts quiescing a region in preparation for recovery.
565 static int __rh_recovery_prepare(struct dm_region_hash *rh)
567 int r;
568 region_t region;
569 struct dm_region *reg;
572 * Ask the dirty log what's next.
574 r = rh->log->type->get_resync_work(rh->log, &region);
575 if (r <= 0)
576 return r;
579 * Get this region, and start it quiescing by setting the
580 * recovering flag.
582 read_lock(&rh->hash_lock);
583 reg = __rh_find(rh, region);
584 read_unlock(&rh->hash_lock);
586 spin_lock_irq(&rh->region_lock);
587 reg->state = DM_RH_RECOVERING;
589 /* Already quiesced ? */
590 if (atomic_read(&reg->pending))
591 list_del_init(&reg->list);
592 else
593 list_move(&reg->list, &rh->quiesced_regions);
595 spin_unlock_irq(&rh->region_lock);
597 return 1;
600 void dm_rh_recovery_prepare(struct dm_region_hash *rh)
602 /* Extra reference to avoid race with dm_rh_stop_recovery */
603 atomic_inc(&rh->recovery_in_flight);
605 while (!down_trylock(&rh->recovery_count)) {
606 atomic_inc(&rh->recovery_in_flight);
607 if (__rh_recovery_prepare(rh) <= 0) {
608 atomic_dec(&rh->recovery_in_flight);
609 up(&rh->recovery_count);
610 break;
614 /* Drop the extra reference */
615 if (atomic_dec_and_test(&rh->recovery_in_flight))
616 rh->wakeup_all_recovery_waiters(rh->context);
618 EXPORT_SYMBOL_GPL(dm_rh_recovery_prepare);
621 * Returns any quiesced regions.
623 struct dm_region *dm_rh_recovery_start(struct dm_region_hash *rh)
625 struct dm_region *reg = NULL;
627 spin_lock_irq(&rh->region_lock);
628 if (!list_empty(&rh->quiesced_regions)) {
629 reg = list_entry(rh->quiesced_regions.next,
630 struct dm_region, list);
631 list_del_init(&reg->list); /* remove from the quiesced list */
633 spin_unlock_irq(&rh->region_lock);
635 return reg;
637 EXPORT_SYMBOL_GPL(dm_rh_recovery_start);
639 void dm_rh_recovery_end(struct dm_region *reg, int success)
641 struct dm_region_hash *rh = reg->rh;
643 spin_lock_irq(&rh->region_lock);
644 if (success)
645 list_add(&reg->list, &reg->rh->recovered_regions);
646 else {
647 reg->state = DM_RH_NOSYNC;
648 list_add(&reg->list, &reg->rh->failed_recovered_regions);
650 spin_unlock_irq(&rh->region_lock);
652 rh->wakeup_workers(rh->context);
654 EXPORT_SYMBOL_GPL(dm_rh_recovery_end);
656 /* Return recovery in flight count. */
657 int dm_rh_recovery_in_flight(struct dm_region_hash *rh)
659 return atomic_read(&rh->recovery_in_flight);
661 EXPORT_SYMBOL_GPL(dm_rh_recovery_in_flight);
663 int dm_rh_flush(struct dm_region_hash *rh)
665 return rh->log->type->flush(rh->log);
667 EXPORT_SYMBOL_GPL(dm_rh_flush);
669 void dm_rh_delay(struct dm_region_hash *rh, struct bio *bio)
671 struct dm_region *reg;
673 read_lock(&rh->hash_lock);
674 reg = __rh_find(rh, dm_rh_bio_to_region(rh, bio));
675 bio_list_add(&reg->delayed_bios, bio);
676 read_unlock(&rh->hash_lock);
678 EXPORT_SYMBOL_GPL(dm_rh_delay);
680 void dm_rh_stop_recovery(struct dm_region_hash *rh)
682 int i;
684 /* wait for any recovering regions */
685 for (i = 0; i < rh->max_recovery; i++)
686 down(&rh->recovery_count);
688 EXPORT_SYMBOL_GPL(dm_rh_stop_recovery);
690 void dm_rh_start_recovery(struct dm_region_hash *rh)
692 int i;
694 for (i = 0; i < rh->max_recovery; i++)
695 up(&rh->recovery_count);
697 rh->wakeup_workers(rh->context);
699 EXPORT_SYMBOL_GPL(dm_rh_start_recovery);
701 MODULE_DESCRIPTION(DM_NAME " region hash");
702 MODULE_AUTHOR("Joe Thornber/Heinz Mauelshagen <dm-devel@redhat.com>");
703 MODULE_LICENSE("GPL");