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.
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>
18 #define DM_MSG_PREFIX "region hash"
20 /*-----------------------------------------------------------------
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
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
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
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
{
58 unsigned region_shift
;
60 /* holds persistent region state */
61 struct dm_dirty_log
*log
;
65 mempool_t
*region_pool
;
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
;
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
);
96 struct dm_region_hash
*rh
; /* FIXME: can we get rid of this ? */
100 struct list_head hash_list
;
101 struct list_head list
;
104 struct bio_list delayed_bios
;
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
)
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
,
162 struct dm_region_hash
*rh
;
163 unsigned nr_buckets
, max_buckets
;
167 * Calculate a suitable number of buckets for our hash
170 max_buckets
= nr_regions
>> 6;
171 for (nr_buckets
= 128u; nr_buckets
< max_buckets
; nr_buckets
<<= 1)
175 rh
= kmalloc(sizeof(*rh
), GFP_KERNEL
);
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
;
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
));
199 DMERR("unable to allocate region hash bucket memory");
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
) {
220 rh
= ERR_PTR(-ENOMEM
);
225 EXPORT_SYMBOL_GPL(dm_region_hash_create
);
227 void dm_region_hash_destroy(struct dm_region_hash
*rh
)
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
,
236 BUG_ON(atomic_read(®
->pending
));
237 mempool_free(reg
, rh
->region_pool
);
242 dm_dirty_log_destroy(rh
->log
);
245 mempool_destroy(rh
->region_pool
);
250 EXPORT_SYMBOL_GPL(dm_region_hash_destroy
);
252 struct dm_dirty_log
*dm_rh_dirty_log(struct dm_region_hash
*rh
)
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
)
275 static void __rh_insert(struct dm_region_hash
*rh
, struct dm_region
*reg
)
277 list_add(®
->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
);
286 nreg
= kmalloc(sizeof(*nreg
), GFP_NOIO
);
288 nreg
->state
= rh
->log
->type
->in_sync(rh
->log
, region
, 1) ?
289 DM_RH_CLEAN
: DM_RH_NOSYNC
;
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
);
299 /* We lost the race. */
300 mempool_free(nreg
, rh
->region_pool
);
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
);
311 write_unlock_irq(&rh
->hash_lock
);
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
);
322 read_unlock(&rh
->hash_lock
);
323 reg
= __rh_alloc(rh
, region
);
324 read_lock(&rh
->hash_lock
);
330 int dm_rh_get_state(struct dm_region_hash
*rh
, region_t region
, int may_block
)
333 struct dm_region
*reg
;
335 read_lock(&rh
->hash_lock
);
336 reg
= __rh_lookup(rh
, region
);
337 read_unlock(&rh
->hash_lock
);
343 * The region wasn't in the hash, so we fall back to the
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
, ®
->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
);
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
)
393 struct dm_dirty_log
*log
= rh
->log
;
394 struct dm_region
*reg
;
395 region_t region
= dm_rh_bio_to_region(rh
, bio
);
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 */
407 BUG_ON(!list_empty(®
->list
));
409 spin_lock_irqsave(&rh
->region_lock
, flags
);
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(®
->list
));
420 spin_unlock_irqrestore(&rh
->region_lock
, flags
);
422 bio_endio(bio
, error
);
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
;
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(®
->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(®
->hash_list
);
455 if (!list_empty(&rh
->failed_recovered_regions
)) {
456 list_splice_init(&rh
->failed_recovered_regions
,
459 list_for_each_entry(reg
, &failed_recovered
, list
)
460 list_del(®
->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
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(®
->pending
);
501 if (reg
->state
== DM_RH_CLEAN
) {
502 reg
->state
= DM_RH_DIRTY
;
503 list_del_init(®
->list
); /* take off the clean list */
504 spin_unlock_irq(&rh
->region_lock
);
506 rh
->log
->type
->mark_region(rh
->log
, reg
->key
);
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
)
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
)
526 struct dm_region
*reg
;
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(®
->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
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(®
->list
, &rh
->quiesced_regions
);
549 } else if (reg
->state
== DM_RH_DIRTY
) {
550 reg
->state
= DM_RH_CLEAN
;
551 list_add(®
->list
, &rh
->clean_regions
);
555 spin_unlock_irqrestore(&rh
->region_lock
, flags
);
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
)
569 struct dm_region
*reg
;
572 * Ask the dirty log what's next.
574 r
= rh
->log
->type
->get_resync_work(rh
->log
, ®ion
);
579 * Get this region, and start it quiescing by setting the
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(®
->pending
))
591 list_del_init(®
->list
);
593 list_move(®
->list
, &rh
->quiesced_regions
);
595 spin_unlock_irq(&rh
->region_lock
);
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
);
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(®
->list
); /* remove from the quiesced list */
633 spin_unlock_irq(&rh
->region_lock
);
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
);
645 list_add(®
->list
, ®
->rh
->recovered_regions
);
647 reg
->state
= DM_RH_NOSYNC
;
648 list_add(®
->list
, ®
->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(®
->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
)
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
)
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");