dm-region-hash.c 18 KB

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