dm-region-hash.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/dm-dirty-log.h>
  8. #include <linux/dm-region-hash.h>
  9. #include <linux/ctype.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/vmalloc.h>
  13. #include "dm.h"
  14. #define DM_MSG_PREFIX "region hash"
  15. /*-----------------------------------------------------------------
  16. * Region hash
  17. *
  18. * The mirror splits itself up into discrete regions. Each
  19. * region can be in one of three states: clean, dirty,
  20. * nosync. There is no need to put clean regions in the hash.
  21. *
  22. * In addition to being present in the hash table a region _may_
  23. * be present on one of three lists.
  24. *
  25. * clean_regions: Regions on this list have no io pending to
  26. * them, they are in sync, we are no longer interested in them,
  27. * they are dull. dm_rh_update_states() will remove them from the
  28. * hash table.
  29. *
  30. * quiesced_regions: These regions have been spun down, ready
  31. * for recovery. rh_recovery_start() will remove regions from
  32. * this list and hand them to kmirrord, which will schedule the
  33. * recovery io with kcopyd.
  34. *
  35. * recovered_regions: Regions that kcopyd has successfully
  36. * recovered. dm_rh_update_states() will now schedule any delayed
  37. * io, up the recovery_count, and remove the region from the
  38. * hash.
  39. *
  40. * There are 2 locks:
  41. * A rw spin lock 'hash_lock' protects just the hash table,
  42. * this is never held in write mode from interrupt context,
  43. * which I believe means that we only have to disable irqs when
  44. * doing a write lock.
  45. *
  46. * An ordinary spin lock 'region_lock' that protects the three
  47. * lists in the region_hash, with the 'state', 'list' and
  48. * 'delayed_bios' fields of the regions. This is used from irq
  49. * context, so all other uses will have to suspend local irqs.
  50. *---------------------------------------------------------------*/
  51. struct dm_region_hash {
  52. uint32_t region_size;
  53. unsigned region_shift;
  54. /* holds persistent region state */
  55. struct dm_dirty_log *log;
  56. /* hash table */
  57. rwlock_t hash_lock;
  58. mempool_t *region_pool;
  59. unsigned mask;
  60. unsigned nr_buckets;
  61. unsigned prime;
  62. unsigned shift;
  63. struct list_head *buckets;
  64. unsigned max_recovery; /* Max # of regions to recover in parallel */
  65. spinlock_t region_lock;
  66. atomic_t recovery_in_flight;
  67. struct semaphore recovery_count;
  68. struct list_head clean_regions;
  69. struct list_head quiesced_regions;
  70. struct list_head recovered_regions;
  71. struct list_head failed_recovered_regions;
  72. /*
  73. * If there was a barrier failure no regions can be marked clean.
  74. */
  75. int barrier_failure;
  76. void *context;
  77. sector_t target_begin;
  78. /* Callback function to schedule bios writes */
  79. void (*dispatch_bios)(void *context, struct bio_list *bios);
  80. /* Callback function to wakeup callers worker thread. */
  81. void (*wakeup_workers)(void *context);
  82. /* Callback function to wakeup callers recovery waiters. */
  83. void (*wakeup_all_recovery_waiters)(void *context);
  84. };
  85. struct dm_region {
  86. struct dm_region_hash *rh; /* FIXME: can we get rid of this ? */
  87. region_t key;
  88. int state;
  89. struct list_head hash_list;
  90. struct list_head list;
  91. atomic_t pending;
  92. struct bio_list delayed_bios;
  93. };
  94. /*
  95. * Conversion fns
  96. */
  97. static region_t dm_rh_sector_to_region(struct dm_region_hash *rh, sector_t sector)
  98. {
  99. return sector >> rh->region_shift;
  100. }
  101. sector_t dm_rh_region_to_sector(struct dm_region_hash *rh, region_t region)
  102. {
  103. return region << rh->region_shift;
  104. }
  105. EXPORT_SYMBOL_GPL(dm_rh_region_to_sector);
  106. region_t dm_rh_bio_to_region(struct dm_region_hash *rh, struct bio *bio)
  107. {
  108. return dm_rh_sector_to_region(rh, bio->bi_sector - rh->target_begin);
  109. }
  110. EXPORT_SYMBOL_GPL(dm_rh_bio_to_region);
  111. void *dm_rh_region_context(struct dm_region *reg)
  112. {
  113. return reg->rh->context;
  114. }
  115. EXPORT_SYMBOL_GPL(dm_rh_region_context);
  116. region_t dm_rh_get_region_key(struct dm_region *reg)
  117. {
  118. return reg->key;
  119. }
  120. EXPORT_SYMBOL_GPL(dm_rh_get_region_key);
  121. sector_t dm_rh_get_region_size(struct dm_region_hash *rh)
  122. {
  123. return rh->region_size;
  124. }
  125. EXPORT_SYMBOL_GPL(dm_rh_get_region_size);
  126. /*
  127. * FIXME: shall we pass in a structure instead of all these args to
  128. * dm_region_hash_create()????
  129. */
  130. #define RH_HASH_MULT 2654435387U
  131. #define RH_HASH_SHIFT 12
  132. #define MIN_REGIONS 64
  133. struct dm_region_hash *dm_region_hash_create(
  134. void *context, void (*dispatch_bios)(void *context,
  135. struct bio_list *bios),
  136. void (*wakeup_workers)(void *context),
  137. void (*wakeup_all_recovery_waiters)(void *context),
  138. sector_t target_begin, unsigned max_recovery,
  139. struct dm_dirty_log *log, uint32_t region_size,
  140. region_t nr_regions)
  141. {
  142. struct dm_region_hash *rh;
  143. unsigned nr_buckets, max_buckets;
  144. size_t i;
  145. /*
  146. * Calculate a suitable number of buckets for our hash
  147. * table.
  148. */
  149. max_buckets = nr_regions >> 6;
  150. for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
  151. ;
  152. nr_buckets >>= 1;
  153. rh = kmalloc(sizeof(*rh), GFP_KERNEL);
  154. if (!rh) {
  155. DMERR("unable to allocate region hash memory");
  156. return ERR_PTR(-ENOMEM);
  157. }
  158. rh->context = context;
  159. rh->dispatch_bios = dispatch_bios;
  160. rh->wakeup_workers = wakeup_workers;
  161. rh->wakeup_all_recovery_waiters = wakeup_all_recovery_waiters;
  162. rh->target_begin = target_begin;
  163. rh->max_recovery = max_recovery;
  164. rh->log = log;
  165. rh->region_size = region_size;
  166. rh->region_shift = ffs(region_size) - 1;
  167. rwlock_init(&rh->hash_lock);
  168. rh->mask = nr_buckets - 1;
  169. rh->nr_buckets = nr_buckets;
  170. rh->shift = RH_HASH_SHIFT;
  171. rh->prime = RH_HASH_MULT;
  172. rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
  173. if (!rh->buckets) {
  174. DMERR("unable to allocate region hash bucket memory");
  175. kfree(rh);
  176. return ERR_PTR(-ENOMEM);
  177. }
  178. for (i = 0; i < nr_buckets; i++)
  179. INIT_LIST_HEAD(rh->buckets + i);
  180. spin_lock_init(&rh->region_lock);
  181. sema_init(&rh->recovery_count, 0);
  182. atomic_set(&rh->recovery_in_flight, 0);
  183. INIT_LIST_HEAD(&rh->clean_regions);
  184. INIT_LIST_HEAD(&rh->quiesced_regions);
  185. INIT_LIST_HEAD(&rh->recovered_regions);
  186. INIT_LIST_HEAD(&rh->failed_recovered_regions);
  187. rh->barrier_failure = 0;
  188. rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
  189. sizeof(struct dm_region));
  190. if (!rh->region_pool) {
  191. vfree(rh->buckets);
  192. kfree(rh);
  193. rh = ERR_PTR(-ENOMEM);
  194. }
  195. return rh;
  196. }
  197. EXPORT_SYMBOL_GPL(dm_region_hash_create);
  198. void dm_region_hash_destroy(struct dm_region_hash *rh)
  199. {
  200. unsigned h;
  201. struct dm_region *reg, *nreg;
  202. BUG_ON(!list_empty(&rh->quiesced_regions));
  203. for (h = 0; h < rh->nr_buckets; h++) {
  204. list_for_each_entry_safe(reg, nreg, rh->buckets + h,
  205. hash_list) {
  206. BUG_ON(atomic_read(&reg->pending));
  207. mempool_free(reg, rh->region_pool);
  208. }
  209. }
  210. if (rh->log)
  211. dm_dirty_log_destroy(rh->log);
  212. if (rh->region_pool)
  213. mempool_destroy(rh->region_pool);
  214. vfree(rh->buckets);
  215. kfree(rh);
  216. }
  217. EXPORT_SYMBOL_GPL(dm_region_hash_destroy);
  218. struct dm_dirty_log *dm_rh_dirty_log(struct dm_region_hash *rh)
  219. {
  220. return rh->log;
  221. }
  222. EXPORT_SYMBOL_GPL(dm_rh_dirty_log);
  223. static unsigned rh_hash(struct dm_region_hash *rh, region_t region)
  224. {
  225. return (unsigned) ((region * rh->prime) >> rh->shift) & rh->mask;
  226. }
  227. static struct dm_region *__rh_lookup(struct dm_region_hash *rh, region_t region)
  228. {
  229. struct dm_region *reg;
  230. struct list_head *bucket = rh->buckets + rh_hash(rh, region);
  231. list_for_each_entry(reg, bucket, hash_list)
  232. if (reg->key == region)
  233. return reg;
  234. return NULL;
  235. }
  236. static void __rh_insert(struct dm_region_hash *rh, struct dm_region *reg)
  237. {
  238. list_add(&reg->hash_list, rh->buckets + rh_hash(rh, reg->key));
  239. }
  240. static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region)
  241. {
  242. struct dm_region *reg, *nreg;
  243. nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
  244. if (unlikely(!nreg))
  245. nreg = kmalloc(sizeof(*nreg), GFP_NOIO | __GFP_NOFAIL);
  246. nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
  247. DM_RH_CLEAN : DM_RH_NOSYNC;
  248. nreg->rh = rh;
  249. nreg->key = region;
  250. INIT_LIST_HEAD(&nreg->list);
  251. atomic_set(&nreg->pending, 0);
  252. bio_list_init(&nreg->delayed_bios);
  253. write_lock_irq(&rh->hash_lock);
  254. reg = __rh_lookup(rh, region);
  255. if (reg)
  256. /* We lost the race. */
  257. mempool_free(nreg, rh->region_pool);
  258. else {
  259. __rh_insert(rh, nreg);
  260. if (nreg->state == DM_RH_CLEAN) {
  261. spin_lock(&rh->region_lock);
  262. list_add(&nreg->list, &rh->clean_regions);
  263. spin_unlock(&rh->region_lock);
  264. }
  265. reg = nreg;
  266. }
  267. write_unlock_irq(&rh->hash_lock);
  268. return reg;
  269. }
  270. static struct dm_region *__rh_find(struct dm_region_hash *rh, region_t region)
  271. {
  272. struct dm_region *reg;
  273. reg = __rh_lookup(rh, region);
  274. if (!reg) {
  275. read_unlock(&rh->hash_lock);
  276. reg = __rh_alloc(rh, region);
  277. read_lock(&rh->hash_lock);
  278. }
  279. return reg;
  280. }
  281. int dm_rh_get_state(struct dm_region_hash *rh, region_t region, int may_block)
  282. {
  283. int r;
  284. struct dm_region *reg;
  285. read_lock(&rh->hash_lock);
  286. reg = __rh_lookup(rh, region);
  287. read_unlock(&rh->hash_lock);
  288. if (reg)
  289. return reg->state;
  290. /*
  291. * The region wasn't in the hash, so we fall back to the
  292. * dirty log.
  293. */
  294. r = rh->log->type->in_sync(rh->log, region, may_block);
  295. /*
  296. * Any error from the dirty log (eg. -EWOULDBLOCK) gets
  297. * taken as a DM_RH_NOSYNC
  298. */
  299. return r == 1 ? DM_RH_CLEAN : DM_RH_NOSYNC;
  300. }
  301. EXPORT_SYMBOL_GPL(dm_rh_get_state);
  302. static void complete_resync_work(struct dm_region *reg, int success)
  303. {
  304. struct dm_region_hash *rh = reg->rh;
  305. rh->log->type->set_region_sync(rh->log, reg->key, success);
  306. /*
  307. * Dispatch the bios before we call 'wake_up_all'.
  308. * This is important because if we are suspending,
  309. * we want to know that recovery is complete and
  310. * the work queue is flushed. If we wake_up_all
  311. * before we dispatch_bios (queue bios and call wake()),
  312. * then we risk suspending before the work queue
  313. * has been properly flushed.
  314. */
  315. rh->dispatch_bios(rh->context, &reg->delayed_bios);
  316. if (atomic_dec_and_test(&rh->recovery_in_flight))
  317. rh->wakeup_all_recovery_waiters(rh->context);
  318. up(&rh->recovery_count);
  319. }
  320. /* dm_rh_mark_nosync
  321. * @ms
  322. * @bio
  323. *
  324. * The bio was written on some mirror(s) but failed on other mirror(s).
  325. * We can successfully endio the bio but should avoid the region being
  326. * marked clean by setting the state DM_RH_NOSYNC.
  327. *
  328. * This function is _not_ safe in interrupt context!
  329. */
  330. void dm_rh_mark_nosync(struct dm_region_hash *rh, struct bio *bio)
  331. {
  332. unsigned long flags;
  333. struct dm_dirty_log *log = rh->log;
  334. struct dm_region *reg;
  335. region_t region = dm_rh_bio_to_region(rh, bio);
  336. int recovering = 0;
  337. if (bio_empty_barrier(bio)) {
  338. rh->barrier_failure = 1;
  339. return;
  340. }
  341. /* We must inform the log that the sync count has changed. */
  342. log->type->set_region_sync(log, region, 0);
  343. read_lock(&rh->hash_lock);
  344. reg = __rh_find(rh, region);
  345. read_unlock(&rh->hash_lock);
  346. /* region hash entry should exist because write was in-flight */
  347. BUG_ON(!reg);
  348. BUG_ON(!list_empty(&reg->list));
  349. spin_lock_irqsave(&rh->region_lock, flags);
  350. /*
  351. * Possible cases:
  352. * 1) DM_RH_DIRTY
  353. * 2) DM_RH_NOSYNC: was dirty, other preceeding writes failed
  354. * 3) DM_RH_RECOVERING: flushing pending writes
  355. * Either case, the region should have not been connected to list.
  356. */
  357. recovering = (reg->state == DM_RH_RECOVERING);
  358. reg->state = DM_RH_NOSYNC;
  359. BUG_ON(!list_empty(&reg->list));
  360. spin_unlock_irqrestore(&rh->region_lock, flags);
  361. if (recovering)
  362. complete_resync_work(reg, 0);
  363. }
  364. EXPORT_SYMBOL_GPL(dm_rh_mark_nosync);
  365. void dm_rh_update_states(struct dm_region_hash *rh, int errors_handled)
  366. {
  367. struct dm_region *reg, *next;
  368. LIST_HEAD(clean);
  369. LIST_HEAD(recovered);
  370. LIST_HEAD(failed_recovered);
  371. /*
  372. * Quickly grab the lists.
  373. */
  374. write_lock_irq(&rh->hash_lock);
  375. spin_lock(&rh->region_lock);
  376. if (!list_empty(&rh->clean_regions)) {
  377. list_splice_init(&rh->clean_regions, &clean);
  378. list_for_each_entry(reg, &clean, list)
  379. list_del(&reg->hash_list);
  380. }
  381. if (!list_empty(&rh->recovered_regions)) {
  382. list_splice_init(&rh->recovered_regions, &recovered);
  383. list_for_each_entry(reg, &recovered, list)
  384. list_del(&reg->hash_list);
  385. }
  386. if (!list_empty(&rh->failed_recovered_regions)) {
  387. list_splice_init(&rh->failed_recovered_regions,
  388. &failed_recovered);
  389. list_for_each_entry(reg, &failed_recovered, list)
  390. list_del(&reg->hash_list);
  391. }
  392. spin_unlock(&rh->region_lock);
  393. write_unlock_irq(&rh->hash_lock);
  394. /*
  395. * All the regions on the recovered and clean lists have
  396. * now been pulled out of the system, so no need to do
  397. * any more locking.
  398. */
  399. list_for_each_entry_safe(reg, next, &recovered, list) {
  400. rh->log->type->clear_region(rh->log, reg->key);
  401. complete_resync_work(reg, 1);
  402. mempool_free(reg, rh->region_pool);
  403. }
  404. list_for_each_entry_safe(reg, next, &failed_recovered, list) {
  405. complete_resync_work(reg, errors_handled ? 0 : 1);
  406. mempool_free(reg, rh->region_pool);
  407. }
  408. list_for_each_entry_safe(reg, next, &clean, list) {
  409. rh->log->type->clear_region(rh->log, reg->key);
  410. mempool_free(reg, rh->region_pool);
  411. }
  412. rh->log->type->flush(rh->log);
  413. }
  414. EXPORT_SYMBOL_GPL(dm_rh_update_states);
  415. static void rh_inc(struct dm_region_hash *rh, region_t region)
  416. {
  417. struct dm_region *reg;
  418. read_lock(&rh->hash_lock);
  419. reg = __rh_find(rh, region);
  420. spin_lock_irq(&rh->region_lock);
  421. atomic_inc(&reg->pending);
  422. if (reg->state == DM_RH_CLEAN) {
  423. reg->state = DM_RH_DIRTY;
  424. list_del_init(&reg->list); /* take off the clean list */
  425. spin_unlock_irq(&rh->region_lock);
  426. rh->log->type->mark_region(rh->log, reg->key);
  427. } else
  428. spin_unlock_irq(&rh->region_lock);
  429. read_unlock(&rh->hash_lock);
  430. }
  431. void dm_rh_inc_pending(struct dm_region_hash *rh, struct bio_list *bios)
  432. {
  433. struct bio *bio;
  434. for (bio = bios->head; bio; bio = bio->bi_next) {
  435. if (bio_empty_barrier(bio))
  436. continue;
  437. rh_inc(rh, dm_rh_bio_to_region(rh, bio));
  438. }
  439. }
  440. EXPORT_SYMBOL_GPL(dm_rh_inc_pending);
  441. void dm_rh_dec(struct dm_region_hash *rh, region_t region)
  442. {
  443. unsigned long flags;
  444. struct dm_region *reg;
  445. int should_wake = 0;
  446. read_lock(&rh->hash_lock);
  447. reg = __rh_lookup(rh, region);
  448. read_unlock(&rh->hash_lock);
  449. spin_lock_irqsave(&rh->region_lock, flags);
  450. if (atomic_dec_and_test(&reg->pending)) {
  451. /*
  452. * There is no pending I/O for this region.
  453. * We can move the region to corresponding list for next action.
  454. * At this point, the region is not yet connected to any list.
  455. *
  456. * If the state is DM_RH_NOSYNC, the region should be kept off
  457. * from clean list.
  458. * The hash entry for DM_RH_NOSYNC will remain in memory
  459. * until the region is recovered or the map is reloaded.
  460. */
  461. /* do nothing for DM_RH_NOSYNC */
  462. if (unlikely(rh->barrier_failure)) {
  463. /*
  464. * If a write barrier failed some time ago, we
  465. * don't know whether or not this write made it
  466. * to the disk, so we must resync the device.
  467. */
  468. reg->state = DM_RH_NOSYNC;
  469. } else if (reg->state == DM_RH_RECOVERING) {
  470. list_add_tail(&reg->list, &rh->quiesced_regions);
  471. } else if (reg->state == DM_RH_DIRTY) {
  472. reg->state = DM_RH_CLEAN;
  473. list_add(&reg->list, &rh->clean_regions);
  474. }
  475. should_wake = 1;
  476. }
  477. spin_unlock_irqrestore(&rh->region_lock, flags);
  478. if (should_wake)
  479. rh->wakeup_workers(rh->context);
  480. }
  481. EXPORT_SYMBOL_GPL(dm_rh_dec);
  482. /*
  483. * Starts quiescing a region in preparation for recovery.
  484. */
  485. static int __rh_recovery_prepare(struct dm_region_hash *rh)
  486. {
  487. int r;
  488. region_t region;
  489. struct dm_region *reg;
  490. /*
  491. * Ask the dirty log what's next.
  492. */
  493. r = rh->log->type->get_resync_work(rh->log, &region);
  494. if (r <= 0)
  495. return r;
  496. /*
  497. * Get this region, and start it quiescing by setting the
  498. * recovering flag.
  499. */
  500. read_lock(&rh->hash_lock);
  501. reg = __rh_find(rh, region);
  502. read_unlock(&rh->hash_lock);
  503. spin_lock_irq(&rh->region_lock);
  504. reg->state = DM_RH_RECOVERING;
  505. /* Already quiesced ? */
  506. if (atomic_read(&reg->pending))
  507. list_del_init(&reg->list);
  508. else
  509. list_move(&reg->list, &rh->quiesced_regions);
  510. spin_unlock_irq(&rh->region_lock);
  511. return 1;
  512. }
  513. void dm_rh_recovery_prepare(struct dm_region_hash *rh)
  514. {
  515. /* Extra reference to avoid race with dm_rh_stop_recovery */
  516. atomic_inc(&rh->recovery_in_flight);
  517. while (!down_trylock(&rh->recovery_count)) {
  518. atomic_inc(&rh->recovery_in_flight);
  519. if (__rh_recovery_prepare(rh) <= 0) {
  520. atomic_dec(&rh->recovery_in_flight);
  521. up(&rh->recovery_count);
  522. break;
  523. }
  524. }
  525. /* Drop the extra reference */
  526. if (atomic_dec_and_test(&rh->recovery_in_flight))
  527. rh->wakeup_all_recovery_waiters(rh->context);
  528. }
  529. EXPORT_SYMBOL_GPL(dm_rh_recovery_prepare);
  530. /*
  531. * Returns any quiesced regions.
  532. */
  533. struct dm_region *dm_rh_recovery_start(struct dm_region_hash *rh)
  534. {
  535. struct dm_region *reg = NULL;
  536. spin_lock_irq(&rh->region_lock);
  537. if (!list_empty(&rh->quiesced_regions)) {
  538. reg = list_entry(rh->quiesced_regions.next,
  539. struct dm_region, list);
  540. list_del_init(&reg->list); /* remove from the quiesced list */
  541. }
  542. spin_unlock_irq(&rh->region_lock);
  543. return reg;
  544. }
  545. EXPORT_SYMBOL_GPL(dm_rh_recovery_start);
  546. void dm_rh_recovery_end(struct dm_region *reg, int success)
  547. {
  548. struct dm_region_hash *rh = reg->rh;
  549. spin_lock_irq(&rh->region_lock);
  550. if (success)
  551. list_add(&reg->list, &reg->rh->recovered_regions);
  552. else {
  553. reg->state = DM_RH_NOSYNC;
  554. list_add(&reg->list, &reg->rh->failed_recovered_regions);
  555. }
  556. spin_unlock_irq(&rh->region_lock);
  557. rh->wakeup_workers(rh->context);
  558. }
  559. EXPORT_SYMBOL_GPL(dm_rh_recovery_end);
  560. /* Return recovery in flight count. */
  561. int dm_rh_recovery_in_flight(struct dm_region_hash *rh)
  562. {
  563. return atomic_read(&rh->recovery_in_flight);
  564. }
  565. EXPORT_SYMBOL_GPL(dm_rh_recovery_in_flight);
  566. int dm_rh_flush(struct dm_region_hash *rh)
  567. {
  568. return rh->log->type->flush(rh->log);
  569. }
  570. EXPORT_SYMBOL_GPL(dm_rh_flush);
  571. void dm_rh_delay(struct dm_region_hash *rh, struct bio *bio)
  572. {
  573. struct dm_region *reg;
  574. read_lock(&rh->hash_lock);
  575. reg = __rh_find(rh, dm_rh_bio_to_region(rh, bio));
  576. bio_list_add(&reg->delayed_bios, bio);
  577. read_unlock(&rh->hash_lock);
  578. }
  579. EXPORT_SYMBOL_GPL(dm_rh_delay);
  580. void dm_rh_stop_recovery(struct dm_region_hash *rh)
  581. {
  582. int i;
  583. /* wait for any recovering regions */
  584. for (i = 0; i < rh->max_recovery; i++)
  585. down(&rh->recovery_count);
  586. }
  587. EXPORT_SYMBOL_GPL(dm_rh_stop_recovery);
  588. void dm_rh_start_recovery(struct dm_region_hash *rh)
  589. {
  590. int i;
  591. for (i = 0; i < rh->max_recovery; i++)
  592. up(&rh->recovery_count);
  593. rh->wakeup_workers(rh->context);
  594. }
  595. EXPORT_SYMBOL_GPL(dm_rh_start_recovery);
  596. MODULE_DESCRIPTION(DM_NAME " region hash");
  597. MODULE_AUTHOR("Joe Thornber/Heinz Mauelshagen <dm-devel@redhat.com>");
  598. MODULE_LICENSE("GPL");