Lines Matching defs:rh

106 	struct dm_region_hash *rh;	/* FIXME: can we get rid of this ? */
120 static region_t dm_rh_sector_to_region(struct dm_region_hash *rh, sector_t sector)
122 return sector >> rh->region_shift;
125 sector_t dm_rh_region_to_sector(struct dm_region_hash *rh, region_t region)
127 return region << rh->region_shift;
131 region_t dm_rh_bio_to_region(struct dm_region_hash *rh, struct bio *bio)
133 return dm_rh_sector_to_region(rh, bio->bi_iter.bi_sector -
134 rh->target_begin);
140 return reg->rh->context;
150 sector_t dm_rh_get_region_size(struct dm_region_hash *rh)
152 return rh->region_size;
173 struct dm_region_hash *rh;
187 rh = kzalloc(sizeof(*rh), GFP_KERNEL);
188 if (!rh) {
193 rh->context = context;
194 rh->dispatch_bios = dispatch_bios;
195 rh->wakeup_workers = wakeup_workers;
196 rh->wakeup_all_recovery_waiters = wakeup_all_recovery_waiters;
197 rh->target_begin = target_begin;
198 rh->max_recovery = max_recovery;
199 rh->log = log;
200 rh->region_size = region_size;
201 rh->region_shift = __ffs(region_size);
202 rwlock_init(&rh->hash_lock);
203 rh->mask = nr_buckets - 1;
204 rh->nr_buckets = nr_buckets;
206 rh->shift = RH_HASH_SHIFT;
207 rh->prime = RH_HASH_MULT;
209 rh->buckets = vmalloc(array_size(nr_buckets, sizeof(*rh->buckets)));
210 if (!rh->buckets) {
212 kfree(rh);
217 INIT_LIST_HEAD(rh->buckets + i);
219 spin_lock_init(&rh->region_lock);
220 sema_init(&rh->recovery_count, 0);
221 atomic_set(&rh->recovery_in_flight, 0);
222 INIT_LIST_HEAD(&rh->clean_regions);
223 INIT_LIST_HEAD(&rh->quiesced_regions);
224 INIT_LIST_HEAD(&rh->recovered_regions);
225 INIT_LIST_HEAD(&rh->failed_recovered_regions);
226 rh->flush_failure = 0;
228 ret = mempool_init_kmalloc_pool(&rh->region_pool, MIN_REGIONS,
231 vfree(rh->buckets);
232 kfree(rh);
233 rh = ERR_PTR(-ENOMEM);
236 return rh;
240 void dm_region_hash_destroy(struct dm_region_hash *rh)
245 BUG_ON(!list_empty(&rh->quiesced_regions));
246 for (h = 0; h < rh->nr_buckets; h++) {
247 list_for_each_entry_safe(reg, nreg, rh->buckets + h,
250 mempool_free(reg, &rh->region_pool);
254 if (rh->log)
255 dm_dirty_log_destroy(rh->log);
257 mempool_exit(&rh->region_pool);
258 vfree(rh->buckets);
259 kfree(rh);
263 struct dm_dirty_log *dm_rh_dirty_log(struct dm_region_hash *rh)
265 return rh->log;
269 static unsigned int rh_hash(struct dm_region_hash *rh, region_t region)
271 return (unsigned int) ((region * rh->prime) >> rh->shift) & rh->mask;
274 static struct dm_region *__rh_lookup(struct dm_region_hash *rh, region_t region)
277 struct list_head *bucket = rh->buckets + rh_hash(rh, region);
286 static void __rh_insert(struct dm_region_hash *rh, struct dm_region *reg)
288 list_add(&reg->hash_list, rh->buckets + rh_hash(rh, reg->key));
291 static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region)
295 nreg = mempool_alloc(&rh->region_pool, GFP_ATOMIC);
299 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
301 nreg->rh = rh;
307 write_lock_irq(&rh->hash_lock);
308 reg = __rh_lookup(rh, region);
311 mempool_free(nreg, &rh->region_pool);
313 __rh_insert(rh, nreg);
315 spin_lock(&rh->region_lock);
316 list_add(&nreg->list, &rh->clean_regions);
317 spin_unlock(&rh->region_lock);
322 write_unlock_irq(&rh->hash_lock);
327 static struct dm_region *__rh_find(struct dm_region_hash *rh, region_t region)
331 reg = __rh_lookup(rh, region);
333 read_unlock(&rh->hash_lock);
334 reg = __rh_alloc(rh, region);
335 read_lock(&rh->hash_lock);
341 int dm_rh_get_state(struct dm_region_hash *rh, region_t region, int may_block)
346 read_lock(&rh->hash_lock);
347 reg = __rh_lookup(rh, region);
348 read_unlock(&rh->hash_lock);
357 r = rh->log->type->in_sync(rh->log, region, may_block);
369 struct dm_region_hash *rh = reg->rh;
371 rh->log->type->set_region_sync(rh->log, reg->key, success);
382 rh->dispatch_bios(rh->context, &reg->delayed_bios);
383 if (atomic_dec_and_test(&rh->recovery_in_flight))
384 rh->wakeup_all_recovery_waiters(rh->context);
385 up(&rh->recovery_count);
398 void dm_rh_mark_nosync(struct dm_region_hash *rh, struct bio *bio)
401 struct dm_dirty_log *log = rh->log;
403 region_t region = dm_rh_bio_to_region(rh, bio);
407 rh->flush_failure = 1;
417 read_lock(&rh->hash_lock);
418 reg = __rh_find(rh, region);
419 read_unlock(&rh->hash_lock);
425 spin_lock_irqsave(&rh->region_lock, flags);
436 spin_unlock_irqrestore(&rh->region_lock, flags);
443 void dm_rh_update_states(struct dm_region_hash *rh, int errors_handled)
454 write_lock_irq(&rh->hash_lock);
455 spin_lock(&rh->region_lock);
456 if (!list_empty(&rh->clean_regions)) {
457 list_splice_init(&rh->clean_regions, &clean);
463 if (!list_empty(&rh->recovered_regions)) {
464 list_splice_init(&rh->recovered_regions, &recovered);
470 if (!list_empty(&rh->failed_recovered_regions)) {
471 list_splice_init(&rh->failed_recovered_regions,
478 spin_unlock(&rh->region_lock);
479 write_unlock_irq(&rh->hash_lock);
487 rh->log->type->clear_region(rh->log, reg->key);
489 mempool_free(reg, &rh->region_pool);
494 mempool_free(reg, &rh->region_pool);
498 rh->log->type->clear_region(rh->log, reg->key);
499 mempool_free(reg, &rh->region_pool);
502 rh->log->type->flush(rh->log);
506 static void rh_inc(struct dm_region_hash *rh, region_t region)
510 read_lock(&rh->hash_lock);
511 reg = __rh_find(rh, region);
513 spin_lock_irq(&rh->region_lock);
519 spin_unlock_irq(&rh->region_lock);
521 rh->log->type->mark_region(rh->log, reg->key);
523 spin_unlock_irq(&rh->region_lock);
526 read_unlock(&rh->hash_lock);
529 void dm_rh_inc_pending(struct dm_region_hash *rh, struct bio_list *bios)
536 rh_inc(rh, dm_rh_bio_to_region(rh, bio));
541 void dm_rh_dec(struct dm_region_hash *rh, region_t region)
547 read_lock(&rh->hash_lock);
548 reg = __rh_lookup(rh, region);
549 read_unlock(&rh->hash_lock);
551 spin_lock_irqsave(&rh->region_lock, flags);
565 if (unlikely(rh->flush_failure)) {
573 list_add_tail(&reg->list, &rh->quiesced_regions);
576 list_add(&reg->list, &rh->clean_regions);
580 spin_unlock_irqrestore(&rh->region_lock, flags);
583 rh->wakeup_workers(rh->context);
590 static int __rh_recovery_prepare(struct dm_region_hash *rh)
599 r = rh->log->type->get_resync_work(rh->log, &region);
607 read_lock(&rh->hash_lock);
608 reg = __rh_find(rh, region);
609 read_unlock(&rh->hash_lock);
611 spin_lock_irq(&rh->region_lock);
618 list_move(&reg->list, &rh->quiesced_regions);
620 spin_unlock_irq(&rh->region_lock);
625 void dm_rh_recovery_prepare(struct dm_region_hash *rh)
628 atomic_inc(&rh->recovery_in_flight);
630 while (!down_trylock(&rh->recovery_count)) {
631 atomic_inc(&rh->recovery_in_flight);
632 if (__rh_recovery_prepare(rh) <= 0) {
633 atomic_dec(&rh->recovery_in_flight);
634 up(&rh->recovery_count);
640 if (atomic_dec_and_test(&rh->recovery_in_flight))
641 rh->wakeup_all_recovery_waiters(rh->context);
648 struct dm_region *dm_rh_recovery_start(struct dm_region_hash *rh)
652 spin_lock_irq(&rh->region_lock);
653 if (!list_empty(&rh->quiesced_regions)) {
654 reg = list_entry(rh->quiesced_regions.next,
658 spin_unlock_irq(&rh->region_lock);
666 struct dm_region_hash *rh = reg->rh;
668 spin_lock_irq(&rh->region_lock);
670 list_add(&reg->list, &reg->rh->recovered_regions);
672 list_add(&reg->list, &reg->rh->failed_recovered_regions);
674 spin_unlock_irq(&rh->region_lock);
676 rh->wakeup_workers(rh->context);
681 int dm_rh_recovery_in_flight(struct dm_region_hash *rh)
683 return atomic_read(&rh->recovery_in_flight);
687 int dm_rh_flush(struct dm_region_hash *rh)
689 return rh->log->type->flush(rh->log);
693 void dm_rh_delay(struct dm_region_hash *rh, struct bio *bio)
697 read_lock(&rh->hash_lock);
698 reg = __rh_find(rh, dm_rh_bio_to_region(rh, bio));
700 read_unlock(&rh->hash_lock);
704 void dm_rh_stop_recovery(struct dm_region_hash *rh)
709 for (i = 0; i < rh->max_recovery; i++)
710 down(&rh->recovery_count);
714 void dm_rh_start_recovery(struct dm_region_hash *rh)
718 for (i = 0; i < rh->max_recovery; i++)
719 up(&rh->recovery_count);
721 rh->wakeup_workers(rh->context);