dm-raid1.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm.h"
  7. #include "dm-bio-list.h"
  8. #include "dm-io.h"
  9. #include "dm-log.h"
  10. #include "kcopyd.h"
  11. #include <linux/ctype.h>
  12. #include <linux/init.h>
  13. #include <linux/mempool.h>
  14. #include <linux/module.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/slab.h>
  17. #include <linux/time.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/workqueue.h>
  20. #define DM_MSG_PREFIX "raid1"
  21. static struct workqueue_struct *_kmirrord_wq;
  22. static struct work_struct _kmirrord_work;
  23. static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
  24. static inline void wake(void)
  25. {
  26. queue_work(_kmirrord_wq, &_kmirrord_work);
  27. }
  28. /*-----------------------------------------------------------------
  29. * Region hash
  30. *
  31. * The mirror splits itself up into discrete regions. Each
  32. * region can be in one of three states: clean, dirty,
  33. * nosync. There is no need to put clean regions in the hash.
  34. *
  35. * In addition to being present in the hash table a region _may_
  36. * be present on one of three lists.
  37. *
  38. * clean_regions: Regions on this list have no io pending to
  39. * them, they are in sync, we are no longer interested in them,
  40. * they are dull. rh_update_states() will remove them from the
  41. * hash table.
  42. *
  43. * quiesced_regions: These regions have been spun down, ready
  44. * for recovery. rh_recovery_start() will remove regions from
  45. * this list and hand them to kmirrord, which will schedule the
  46. * recovery io with kcopyd.
  47. *
  48. * recovered_regions: Regions that kcopyd has successfully
  49. * recovered. rh_update_states() will now schedule any delayed
  50. * io, up the recovery_count, and remove the region from the
  51. * hash.
  52. *
  53. * There are 2 locks:
  54. * A rw spin lock 'hash_lock' protects just the hash table,
  55. * this is never held in write mode from interrupt context,
  56. * which I believe means that we only have to disable irqs when
  57. * doing a write lock.
  58. *
  59. * An ordinary spin lock 'region_lock' that protects the three
  60. * lists in the region_hash, with the 'state', 'list' and
  61. * 'bhs_delayed' fields of the regions. This is used from irq
  62. * context, so all other uses will have to suspend local irqs.
  63. *---------------------------------------------------------------*/
  64. struct mirror_set;
  65. struct region_hash {
  66. struct mirror_set *ms;
  67. uint32_t region_size;
  68. unsigned region_shift;
  69. /* holds persistent region state */
  70. struct dirty_log *log;
  71. /* hash table */
  72. rwlock_t hash_lock;
  73. mempool_t *region_pool;
  74. unsigned int mask;
  75. unsigned int nr_buckets;
  76. struct list_head *buckets;
  77. spinlock_t region_lock;
  78. atomic_t recovery_in_flight;
  79. struct semaphore recovery_count;
  80. struct list_head clean_regions;
  81. struct list_head quiesced_regions;
  82. struct list_head recovered_regions;
  83. };
  84. enum {
  85. RH_CLEAN,
  86. RH_DIRTY,
  87. RH_NOSYNC,
  88. RH_RECOVERING
  89. };
  90. struct region {
  91. struct region_hash *rh; /* FIXME: can we get rid of this ? */
  92. region_t key;
  93. int state;
  94. struct list_head hash_list;
  95. struct list_head list;
  96. atomic_t pending;
  97. struct bio_list delayed_bios;
  98. };
  99. /*-----------------------------------------------------------------
  100. * Mirror set structures.
  101. *---------------------------------------------------------------*/
  102. struct mirror {
  103. atomic_t error_count;
  104. struct dm_dev *dev;
  105. sector_t offset;
  106. };
  107. struct mirror_set {
  108. struct dm_target *ti;
  109. struct list_head list;
  110. struct region_hash rh;
  111. struct kcopyd_client *kcopyd_client;
  112. spinlock_t lock; /* protects the next two lists */
  113. struct bio_list reads;
  114. struct bio_list writes;
  115. /* recovery */
  116. region_t nr_regions;
  117. int in_sync;
  118. struct mirror *default_mirror; /* Default mirror */
  119. unsigned int nr_mirrors;
  120. struct mirror mirror[0];
  121. };
  122. /*
  123. * Conversion fns
  124. */
  125. static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
  126. {
  127. return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
  128. }
  129. static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
  130. {
  131. return region << rh->region_shift;
  132. }
  133. /* FIXME move this */
  134. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
  135. #define MIN_REGIONS 64
  136. #define MAX_RECOVERY 1
  137. static int rh_init(struct region_hash *rh, struct mirror_set *ms,
  138. struct dirty_log *log, uint32_t region_size,
  139. region_t nr_regions)
  140. {
  141. unsigned int nr_buckets, max_buckets;
  142. size_t i;
  143. /*
  144. * Calculate a suitable number of buckets for our hash
  145. * table.
  146. */
  147. max_buckets = nr_regions >> 6;
  148. for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
  149. ;
  150. nr_buckets >>= 1;
  151. rh->ms = ms;
  152. rh->log = log;
  153. rh->region_size = region_size;
  154. rh->region_shift = ffs(region_size) - 1;
  155. rwlock_init(&rh->hash_lock);
  156. rh->mask = nr_buckets - 1;
  157. rh->nr_buckets = nr_buckets;
  158. rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
  159. if (!rh->buckets) {
  160. DMERR("unable to allocate region hash memory");
  161. return -ENOMEM;
  162. }
  163. for (i = 0; i < nr_buckets; i++)
  164. INIT_LIST_HEAD(rh->buckets + i);
  165. spin_lock_init(&rh->region_lock);
  166. sema_init(&rh->recovery_count, 0);
  167. atomic_set(&rh->recovery_in_flight, 0);
  168. INIT_LIST_HEAD(&rh->clean_regions);
  169. INIT_LIST_HEAD(&rh->quiesced_regions);
  170. INIT_LIST_HEAD(&rh->recovered_regions);
  171. rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
  172. sizeof(struct region));
  173. if (!rh->region_pool) {
  174. vfree(rh->buckets);
  175. rh->buckets = NULL;
  176. return -ENOMEM;
  177. }
  178. return 0;
  179. }
  180. static void rh_exit(struct region_hash *rh)
  181. {
  182. unsigned int h;
  183. struct region *reg, *nreg;
  184. BUG_ON(!list_empty(&rh->quiesced_regions));
  185. for (h = 0; h < rh->nr_buckets; h++) {
  186. list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
  187. BUG_ON(atomic_read(&reg->pending));
  188. mempool_free(reg, rh->region_pool);
  189. }
  190. }
  191. if (rh->log)
  192. dm_destroy_dirty_log(rh->log);
  193. if (rh->region_pool)
  194. mempool_destroy(rh->region_pool);
  195. vfree(rh->buckets);
  196. }
  197. #define RH_HASH_MULT 2654435387U
  198. static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
  199. {
  200. return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
  201. }
  202. static struct region *__rh_lookup(struct region_hash *rh, region_t region)
  203. {
  204. struct region *reg;
  205. list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
  206. if (reg->key == region)
  207. return reg;
  208. return NULL;
  209. }
  210. static void __rh_insert(struct region_hash *rh, struct region *reg)
  211. {
  212. unsigned int h = rh_hash(rh, reg->key);
  213. list_add(&reg->hash_list, rh->buckets + h);
  214. }
  215. static struct region *__rh_alloc(struct region_hash *rh, region_t region)
  216. {
  217. struct region *reg, *nreg;
  218. read_unlock(&rh->hash_lock);
  219. nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
  220. if (unlikely(!nreg))
  221. nreg = kmalloc(sizeof(struct region), GFP_NOIO);
  222. nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
  223. RH_CLEAN : RH_NOSYNC;
  224. nreg->rh = rh;
  225. nreg->key = region;
  226. INIT_LIST_HEAD(&nreg->list);
  227. atomic_set(&nreg->pending, 0);
  228. bio_list_init(&nreg->delayed_bios);
  229. write_lock_irq(&rh->hash_lock);
  230. reg = __rh_lookup(rh, region);
  231. if (reg)
  232. /* we lost the race */
  233. mempool_free(nreg, rh->region_pool);
  234. else {
  235. __rh_insert(rh, nreg);
  236. if (nreg->state == RH_CLEAN) {
  237. spin_lock(&rh->region_lock);
  238. list_add(&nreg->list, &rh->clean_regions);
  239. spin_unlock(&rh->region_lock);
  240. }
  241. reg = nreg;
  242. }
  243. write_unlock_irq(&rh->hash_lock);
  244. read_lock(&rh->hash_lock);
  245. return reg;
  246. }
  247. static inline struct region *__rh_find(struct region_hash *rh, region_t region)
  248. {
  249. struct region *reg;
  250. reg = __rh_lookup(rh, region);
  251. if (!reg)
  252. reg = __rh_alloc(rh, region);
  253. return reg;
  254. }
  255. static int rh_state(struct region_hash *rh, region_t region, int may_block)
  256. {
  257. int r;
  258. struct region *reg;
  259. read_lock(&rh->hash_lock);
  260. reg = __rh_lookup(rh, region);
  261. read_unlock(&rh->hash_lock);
  262. if (reg)
  263. return reg->state;
  264. /*
  265. * The region wasn't in the hash, so we fall back to the
  266. * dirty log.
  267. */
  268. r = rh->log->type->in_sync(rh->log, region, may_block);
  269. /*
  270. * Any error from the dirty log (eg. -EWOULDBLOCK) gets
  271. * taken as a RH_NOSYNC
  272. */
  273. return r == 1 ? RH_CLEAN : RH_NOSYNC;
  274. }
  275. static inline int rh_in_sync(struct region_hash *rh,
  276. region_t region, int may_block)
  277. {
  278. int state = rh_state(rh, region, may_block);
  279. return state == RH_CLEAN || state == RH_DIRTY;
  280. }
  281. static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
  282. {
  283. struct bio *bio;
  284. while ((bio = bio_list_pop(bio_list))) {
  285. queue_bio(ms, bio, WRITE);
  286. }
  287. }
  288. static void rh_update_states(struct region_hash *rh)
  289. {
  290. struct region *reg, *next;
  291. LIST_HEAD(clean);
  292. LIST_HEAD(recovered);
  293. /*
  294. * Quickly grab the lists.
  295. */
  296. write_lock_irq(&rh->hash_lock);
  297. spin_lock(&rh->region_lock);
  298. if (!list_empty(&rh->clean_regions)) {
  299. list_splice(&rh->clean_regions, &clean);
  300. INIT_LIST_HEAD(&rh->clean_regions);
  301. list_for_each_entry (reg, &clean, list) {
  302. rh->log->type->clear_region(rh->log, reg->key);
  303. list_del(&reg->hash_list);
  304. }
  305. }
  306. if (!list_empty(&rh->recovered_regions)) {
  307. list_splice(&rh->recovered_regions, &recovered);
  308. INIT_LIST_HEAD(&rh->recovered_regions);
  309. list_for_each_entry (reg, &recovered, list)
  310. list_del(&reg->hash_list);
  311. }
  312. spin_unlock(&rh->region_lock);
  313. write_unlock_irq(&rh->hash_lock);
  314. /*
  315. * All the regions on the recovered and clean lists have
  316. * now been pulled out of the system, so no need to do
  317. * any more locking.
  318. */
  319. list_for_each_entry_safe (reg, next, &recovered, list) {
  320. rh->log->type->clear_region(rh->log, reg->key);
  321. rh->log->type->complete_resync_work(rh->log, reg->key, 1);
  322. dispatch_bios(rh->ms, &reg->delayed_bios);
  323. if (atomic_dec_and_test(&rh->recovery_in_flight))
  324. wake_up_all(&_kmirrord_recovery_stopped);
  325. up(&rh->recovery_count);
  326. mempool_free(reg, rh->region_pool);
  327. }
  328. if (!list_empty(&recovered))
  329. rh->log->type->flush(rh->log);
  330. list_for_each_entry_safe (reg, next, &clean, list)
  331. mempool_free(reg, rh->region_pool);
  332. }
  333. static void rh_inc(struct region_hash *rh, region_t region)
  334. {
  335. struct region *reg;
  336. read_lock(&rh->hash_lock);
  337. reg = __rh_find(rh, region);
  338. spin_lock_irq(&rh->region_lock);
  339. atomic_inc(&reg->pending);
  340. if (reg->state == RH_CLEAN) {
  341. reg->state = RH_DIRTY;
  342. list_del_init(&reg->list); /* take off the clean list */
  343. spin_unlock_irq(&rh->region_lock);
  344. rh->log->type->mark_region(rh->log, reg->key);
  345. } else
  346. spin_unlock_irq(&rh->region_lock);
  347. read_unlock(&rh->hash_lock);
  348. }
  349. static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
  350. {
  351. struct bio *bio;
  352. for (bio = bios->head; bio; bio = bio->bi_next)
  353. rh_inc(rh, bio_to_region(rh, bio));
  354. }
  355. static void rh_dec(struct region_hash *rh, region_t region)
  356. {
  357. unsigned long flags;
  358. struct region *reg;
  359. int should_wake = 0;
  360. read_lock(&rh->hash_lock);
  361. reg = __rh_lookup(rh, region);
  362. read_unlock(&rh->hash_lock);
  363. spin_lock_irqsave(&rh->region_lock, flags);
  364. if (atomic_dec_and_test(&reg->pending)) {
  365. /*
  366. * There is no pending I/O for this region.
  367. * We can move the region to corresponding list for next action.
  368. * At this point, the region is not yet connected to any list.
  369. *
  370. * If the state is RH_NOSYNC, the region should be kept off
  371. * from clean list.
  372. * The hash entry for RH_NOSYNC will remain in memory
  373. * until the region is recovered or the map is reloaded.
  374. */
  375. /* do nothing for RH_NOSYNC */
  376. if (reg->state == RH_RECOVERING) {
  377. list_add_tail(&reg->list, &rh->quiesced_regions);
  378. } else if (reg->state == RH_DIRTY) {
  379. reg->state = RH_CLEAN;
  380. list_add(&reg->list, &rh->clean_regions);
  381. }
  382. should_wake = 1;
  383. }
  384. spin_unlock_irqrestore(&rh->region_lock, flags);
  385. if (should_wake)
  386. wake();
  387. }
  388. /*
  389. * Starts quiescing a region in preparation for recovery.
  390. */
  391. static int __rh_recovery_prepare(struct region_hash *rh)
  392. {
  393. int r;
  394. struct region *reg;
  395. region_t region;
  396. /*
  397. * Ask the dirty log what's next.
  398. */
  399. r = rh->log->type->get_resync_work(rh->log, &region);
  400. if (r <= 0)
  401. return r;
  402. /*
  403. * Get this region, and start it quiescing by setting the
  404. * recovering flag.
  405. */
  406. read_lock(&rh->hash_lock);
  407. reg = __rh_find(rh, region);
  408. read_unlock(&rh->hash_lock);
  409. spin_lock_irq(&rh->region_lock);
  410. reg->state = RH_RECOVERING;
  411. /* Already quiesced ? */
  412. if (atomic_read(&reg->pending))
  413. list_del_init(&reg->list);
  414. else
  415. list_move(&reg->list, &rh->quiesced_regions);
  416. spin_unlock_irq(&rh->region_lock);
  417. return 1;
  418. }
  419. static void rh_recovery_prepare(struct region_hash *rh)
  420. {
  421. /* Extra reference to avoid race with rh_stop_recovery */
  422. atomic_inc(&rh->recovery_in_flight);
  423. while (!down_trylock(&rh->recovery_count)) {
  424. atomic_inc(&rh->recovery_in_flight);
  425. if (__rh_recovery_prepare(rh) <= 0) {
  426. atomic_dec(&rh->recovery_in_flight);
  427. up(&rh->recovery_count);
  428. break;
  429. }
  430. }
  431. /* Drop the extra reference */
  432. if (atomic_dec_and_test(&rh->recovery_in_flight))
  433. wake_up_all(&_kmirrord_recovery_stopped);
  434. }
  435. /*
  436. * Returns any quiesced regions.
  437. */
  438. static struct region *rh_recovery_start(struct region_hash *rh)
  439. {
  440. struct region *reg = NULL;
  441. spin_lock_irq(&rh->region_lock);
  442. if (!list_empty(&rh->quiesced_regions)) {
  443. reg = list_entry(rh->quiesced_regions.next,
  444. struct region, list);
  445. list_del_init(&reg->list); /* remove from the quiesced list */
  446. }
  447. spin_unlock_irq(&rh->region_lock);
  448. return reg;
  449. }
  450. /* FIXME: success ignored for now */
  451. static void rh_recovery_end(struct region *reg, int success)
  452. {
  453. struct region_hash *rh = reg->rh;
  454. spin_lock_irq(&rh->region_lock);
  455. list_add(&reg->list, &reg->rh->recovered_regions);
  456. spin_unlock_irq(&rh->region_lock);
  457. wake();
  458. }
  459. static void rh_flush(struct region_hash *rh)
  460. {
  461. rh->log->type->flush(rh->log);
  462. }
  463. static void rh_delay(struct region_hash *rh, struct bio *bio)
  464. {
  465. struct region *reg;
  466. read_lock(&rh->hash_lock);
  467. reg = __rh_find(rh, bio_to_region(rh, bio));
  468. bio_list_add(&reg->delayed_bios, bio);
  469. read_unlock(&rh->hash_lock);
  470. }
  471. static void rh_stop_recovery(struct region_hash *rh)
  472. {
  473. int i;
  474. /* wait for any recovering regions */
  475. for (i = 0; i < MAX_RECOVERY; i++)
  476. down(&rh->recovery_count);
  477. }
  478. static void rh_start_recovery(struct region_hash *rh)
  479. {
  480. int i;
  481. for (i = 0; i < MAX_RECOVERY; i++)
  482. up(&rh->recovery_count);
  483. wake();
  484. }
  485. /*
  486. * Every mirror should look like this one.
  487. */
  488. #define DEFAULT_MIRROR 0
  489. /*
  490. * This is yucky. We squirrel the mirror_set struct away inside
  491. * bi_next for write buffers. This is safe since the bh
  492. * doesn't get submitted to the lower levels of block layer.
  493. */
  494. static struct mirror_set *bio_get_ms(struct bio *bio)
  495. {
  496. return (struct mirror_set *) bio->bi_next;
  497. }
  498. static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
  499. {
  500. bio->bi_next = (struct bio *) ms;
  501. }
  502. /*-----------------------------------------------------------------
  503. * Recovery.
  504. *
  505. * When a mirror is first activated we may find that some regions
  506. * are in the no-sync state. We have to recover these by
  507. * recopying from the default mirror to all the others.
  508. *---------------------------------------------------------------*/
  509. static void recovery_complete(int read_err, unsigned int write_err,
  510. void *context)
  511. {
  512. struct region *reg = (struct region *) context;
  513. /* FIXME: better error handling */
  514. rh_recovery_end(reg, !(read_err || write_err));
  515. }
  516. static int recover(struct mirror_set *ms, struct region *reg)
  517. {
  518. int r;
  519. unsigned int i;
  520. struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
  521. struct mirror *m;
  522. unsigned long flags = 0;
  523. /* fill in the source */
  524. m = ms->default_mirror;
  525. from.bdev = m->dev->bdev;
  526. from.sector = m->offset + region_to_sector(reg->rh, reg->key);
  527. if (reg->key == (ms->nr_regions - 1)) {
  528. /*
  529. * The final region may be smaller than
  530. * region_size.
  531. */
  532. from.count = ms->ti->len & (reg->rh->region_size - 1);
  533. if (!from.count)
  534. from.count = reg->rh->region_size;
  535. } else
  536. from.count = reg->rh->region_size;
  537. /* fill in the destinations */
  538. for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
  539. if (&ms->mirror[i] == ms->default_mirror)
  540. continue;
  541. m = ms->mirror + i;
  542. dest->bdev = m->dev->bdev;
  543. dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
  544. dest->count = from.count;
  545. dest++;
  546. }
  547. /* hand to kcopyd */
  548. set_bit(KCOPYD_IGNORE_ERROR, &flags);
  549. r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
  550. recovery_complete, reg);
  551. return r;
  552. }
  553. static void do_recovery(struct mirror_set *ms)
  554. {
  555. int r;
  556. struct region *reg;
  557. struct dirty_log *log = ms->rh.log;
  558. /*
  559. * Start quiescing some regions.
  560. */
  561. rh_recovery_prepare(&ms->rh);
  562. /*
  563. * Copy any already quiesced regions.
  564. */
  565. while ((reg = rh_recovery_start(&ms->rh))) {
  566. r = recover(ms, reg);
  567. if (r)
  568. rh_recovery_end(reg, 0);
  569. }
  570. /*
  571. * Update the in sync flag.
  572. */
  573. if (!ms->in_sync &&
  574. (log->type->get_sync_count(log) == ms->nr_regions)) {
  575. /* the sync is complete */
  576. dm_table_event(ms->ti->table);
  577. ms->in_sync = 1;
  578. }
  579. }
  580. /*-----------------------------------------------------------------
  581. * Reads
  582. *---------------------------------------------------------------*/
  583. static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
  584. {
  585. /* FIXME: add read balancing */
  586. return ms->default_mirror;
  587. }
  588. /*
  589. * remap a buffer to a particular mirror.
  590. */
  591. static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
  592. {
  593. bio->bi_bdev = m->dev->bdev;
  594. bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
  595. }
  596. static void do_reads(struct mirror_set *ms, struct bio_list *reads)
  597. {
  598. region_t region;
  599. struct bio *bio;
  600. struct mirror *m;
  601. while ((bio = bio_list_pop(reads))) {
  602. region = bio_to_region(&ms->rh, bio);
  603. /*
  604. * We can only read balance if the region is in sync.
  605. */
  606. if (rh_in_sync(&ms->rh, region, 0))
  607. m = choose_mirror(ms, bio->bi_sector);
  608. else
  609. m = ms->default_mirror;
  610. map_bio(ms, m, bio);
  611. generic_make_request(bio);
  612. }
  613. }
  614. /*-----------------------------------------------------------------
  615. * Writes.
  616. *
  617. * We do different things with the write io depending on the
  618. * state of the region that it's in:
  619. *
  620. * SYNC: increment pending, use kcopyd to write to *all* mirrors
  621. * RECOVERING: delay the io until recovery completes
  622. * NOSYNC: increment pending, just write to the default mirror
  623. *---------------------------------------------------------------*/
  624. static void write_callback(unsigned long error, void *context)
  625. {
  626. unsigned int i;
  627. int uptodate = 1;
  628. struct bio *bio = (struct bio *) context;
  629. struct mirror_set *ms;
  630. ms = bio_get_ms(bio);
  631. bio_set_ms(bio, NULL);
  632. /*
  633. * NOTE: We don't decrement the pending count here,
  634. * instead it is done by the targets endio function.
  635. * This way we handle both writes to SYNC and NOSYNC
  636. * regions with the same code.
  637. */
  638. if (error) {
  639. /*
  640. * only error the io if all mirrors failed.
  641. * FIXME: bogus
  642. */
  643. uptodate = 0;
  644. for (i = 0; i < ms->nr_mirrors; i++)
  645. if (!test_bit(i, &error)) {
  646. uptodate = 1;
  647. break;
  648. }
  649. }
  650. bio_endio(bio, bio->bi_size, 0);
  651. }
  652. static void do_write(struct mirror_set *ms, struct bio *bio)
  653. {
  654. unsigned int i;
  655. struct io_region io[KCOPYD_MAX_REGIONS+1];
  656. struct mirror *m;
  657. for (i = 0; i < ms->nr_mirrors; i++) {
  658. m = ms->mirror + i;
  659. io[i].bdev = m->dev->bdev;
  660. io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
  661. io[i].count = bio->bi_size >> 9;
  662. }
  663. bio_set_ms(bio, ms);
  664. dm_io_async_bvec(ms->nr_mirrors, io, WRITE,
  665. bio->bi_io_vec + bio->bi_idx,
  666. write_callback, bio);
  667. }
  668. static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  669. {
  670. int state;
  671. struct bio *bio;
  672. struct bio_list sync, nosync, recover, *this_list = NULL;
  673. if (!writes->head)
  674. return;
  675. /*
  676. * Classify each write.
  677. */
  678. bio_list_init(&sync);
  679. bio_list_init(&nosync);
  680. bio_list_init(&recover);
  681. while ((bio = bio_list_pop(writes))) {
  682. state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
  683. switch (state) {
  684. case RH_CLEAN:
  685. case RH_DIRTY:
  686. this_list = &sync;
  687. break;
  688. case RH_NOSYNC:
  689. this_list = &nosync;
  690. break;
  691. case RH_RECOVERING:
  692. this_list = &recover;
  693. break;
  694. }
  695. bio_list_add(this_list, bio);
  696. }
  697. /*
  698. * Increment the pending counts for any regions that will
  699. * be written to (writes to recover regions are going to
  700. * be delayed).
  701. */
  702. rh_inc_pending(&ms->rh, &sync);
  703. rh_inc_pending(&ms->rh, &nosync);
  704. rh_flush(&ms->rh);
  705. /*
  706. * Dispatch io.
  707. */
  708. while ((bio = bio_list_pop(&sync)))
  709. do_write(ms, bio);
  710. while ((bio = bio_list_pop(&recover)))
  711. rh_delay(&ms->rh, bio);
  712. while ((bio = bio_list_pop(&nosync))) {
  713. map_bio(ms, ms->default_mirror, bio);
  714. generic_make_request(bio);
  715. }
  716. }
  717. /*-----------------------------------------------------------------
  718. * kmirrord
  719. *---------------------------------------------------------------*/
  720. static LIST_HEAD(_mirror_sets);
  721. static DECLARE_RWSEM(_mirror_sets_lock);
  722. static void do_mirror(struct mirror_set *ms)
  723. {
  724. struct bio_list reads, writes;
  725. spin_lock(&ms->lock);
  726. reads = ms->reads;
  727. writes = ms->writes;
  728. bio_list_init(&ms->reads);
  729. bio_list_init(&ms->writes);
  730. spin_unlock(&ms->lock);
  731. rh_update_states(&ms->rh);
  732. do_recovery(ms);
  733. do_reads(ms, &reads);
  734. do_writes(ms, &writes);
  735. }
  736. static void do_work(struct work_struct *ignored)
  737. {
  738. struct mirror_set *ms;
  739. down_read(&_mirror_sets_lock);
  740. list_for_each_entry (ms, &_mirror_sets, list)
  741. do_mirror(ms);
  742. up_read(&_mirror_sets_lock);
  743. }
  744. /*-----------------------------------------------------------------
  745. * Target functions
  746. *---------------------------------------------------------------*/
  747. static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  748. uint32_t region_size,
  749. struct dm_target *ti,
  750. struct dirty_log *dl)
  751. {
  752. size_t len;
  753. struct mirror_set *ms = NULL;
  754. if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
  755. return NULL;
  756. len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
  757. ms = kmalloc(len, GFP_KERNEL);
  758. if (!ms) {
  759. ti->error = "Cannot allocate mirror context";
  760. return NULL;
  761. }
  762. memset(ms, 0, len);
  763. spin_lock_init(&ms->lock);
  764. ms->ti = ti;
  765. ms->nr_mirrors = nr_mirrors;
  766. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  767. ms->in_sync = 0;
  768. ms->default_mirror = &ms->mirror[DEFAULT_MIRROR];
  769. if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
  770. ti->error = "Error creating dirty region hash";
  771. kfree(ms);
  772. return NULL;
  773. }
  774. return ms;
  775. }
  776. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  777. unsigned int m)
  778. {
  779. while (m--)
  780. dm_put_device(ti, ms->mirror[m].dev);
  781. rh_exit(&ms->rh);
  782. kfree(ms);
  783. }
  784. static inline int _check_region_size(struct dm_target *ti, uint32_t size)
  785. {
  786. return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) ||
  787. size > ti->len);
  788. }
  789. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  790. unsigned int mirror, char **argv)
  791. {
  792. unsigned long long offset;
  793. if (sscanf(argv[1], "%llu", &offset) != 1) {
  794. ti->error = "Invalid offset";
  795. return -EINVAL;
  796. }
  797. if (dm_get_device(ti, argv[0], offset, ti->len,
  798. dm_table_get_mode(ti->table),
  799. &ms->mirror[mirror].dev)) {
  800. ti->error = "Device lookup failure";
  801. return -ENXIO;
  802. }
  803. ms->mirror[mirror].offset = offset;
  804. return 0;
  805. }
  806. static int add_mirror_set(struct mirror_set *ms)
  807. {
  808. down_write(&_mirror_sets_lock);
  809. list_add_tail(&ms->list, &_mirror_sets);
  810. up_write(&_mirror_sets_lock);
  811. wake();
  812. return 0;
  813. }
  814. static void del_mirror_set(struct mirror_set *ms)
  815. {
  816. down_write(&_mirror_sets_lock);
  817. list_del(&ms->list);
  818. up_write(&_mirror_sets_lock);
  819. }
  820. /*
  821. * Create dirty log: log_type #log_params <log_params>
  822. */
  823. static struct dirty_log *create_dirty_log(struct dm_target *ti,
  824. unsigned int argc, char **argv,
  825. unsigned int *args_used)
  826. {
  827. unsigned int param_count;
  828. struct dirty_log *dl;
  829. if (argc < 2) {
  830. ti->error = "Insufficient mirror log arguments";
  831. return NULL;
  832. }
  833. if (sscanf(argv[1], "%u", &param_count) != 1) {
  834. ti->error = "Invalid mirror log argument count";
  835. return NULL;
  836. }
  837. *args_used = 2 + param_count;
  838. if (argc < *args_used) {
  839. ti->error = "Insufficient mirror log arguments";
  840. return NULL;
  841. }
  842. dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
  843. if (!dl) {
  844. ti->error = "Error creating mirror dirty log";
  845. return NULL;
  846. }
  847. if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
  848. ti->error = "Invalid region size";
  849. dm_destroy_dirty_log(dl);
  850. return NULL;
  851. }
  852. return dl;
  853. }
  854. /*
  855. * Construct a mirror mapping:
  856. *
  857. * log_type #log_params <log_params>
  858. * #mirrors [mirror_path offset]{2,}
  859. *
  860. * log_type is "core" or "disk"
  861. * #log_params is between 1 and 3
  862. */
  863. #define DM_IO_PAGES 64
  864. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  865. {
  866. int r;
  867. unsigned int nr_mirrors, m, args_used;
  868. struct mirror_set *ms;
  869. struct dirty_log *dl;
  870. dl = create_dirty_log(ti, argc, argv, &args_used);
  871. if (!dl)
  872. return -EINVAL;
  873. argv += args_used;
  874. argc -= args_used;
  875. if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
  876. nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
  877. ti->error = "Invalid number of mirrors";
  878. dm_destroy_dirty_log(dl);
  879. return -EINVAL;
  880. }
  881. argv++, argc--;
  882. if (argc != nr_mirrors * 2) {
  883. ti->error = "Wrong number of mirror arguments";
  884. dm_destroy_dirty_log(dl);
  885. return -EINVAL;
  886. }
  887. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  888. if (!ms) {
  889. dm_destroy_dirty_log(dl);
  890. return -ENOMEM;
  891. }
  892. /* Get the mirror parameter sets */
  893. for (m = 0; m < nr_mirrors; m++) {
  894. r = get_mirror(ms, ti, m, argv);
  895. if (r) {
  896. free_context(ms, ti, m);
  897. return r;
  898. }
  899. argv += 2;
  900. argc -= 2;
  901. }
  902. ti->private = ms;
  903. ti->split_io = ms->rh.region_size;
  904. r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
  905. if (r) {
  906. free_context(ms, ti, ms->nr_mirrors);
  907. return r;
  908. }
  909. add_mirror_set(ms);
  910. return 0;
  911. }
  912. static void mirror_dtr(struct dm_target *ti)
  913. {
  914. struct mirror_set *ms = (struct mirror_set *) ti->private;
  915. del_mirror_set(ms);
  916. kcopyd_client_destroy(ms->kcopyd_client);
  917. free_context(ms, ti, ms->nr_mirrors);
  918. }
  919. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
  920. {
  921. int should_wake = 0;
  922. struct bio_list *bl;
  923. bl = (rw == WRITE) ? &ms->writes : &ms->reads;
  924. spin_lock(&ms->lock);
  925. should_wake = !(bl->head);
  926. bio_list_add(bl, bio);
  927. spin_unlock(&ms->lock);
  928. if (should_wake)
  929. wake();
  930. }
  931. /*
  932. * Mirror mapping function
  933. */
  934. static int mirror_map(struct dm_target *ti, struct bio *bio,
  935. union map_info *map_context)
  936. {
  937. int r, rw = bio_rw(bio);
  938. struct mirror *m;
  939. struct mirror_set *ms = ti->private;
  940. map_context->ll = bio_to_region(&ms->rh, bio);
  941. if (rw == WRITE) {
  942. queue_bio(ms, bio, rw);
  943. return 0;
  944. }
  945. r = ms->rh.log->type->in_sync(ms->rh.log,
  946. bio_to_region(&ms->rh, bio), 0);
  947. if (r < 0 && r != -EWOULDBLOCK)
  948. return r;
  949. if (r == -EWOULDBLOCK) /* FIXME: ugly */
  950. r = 0;
  951. /*
  952. * We don't want to fast track a recovery just for a read
  953. * ahead. So we just let it silently fail.
  954. * FIXME: get rid of this.
  955. */
  956. if (!r && rw == READA)
  957. return -EIO;
  958. if (!r) {
  959. /* Pass this io over to the daemon */
  960. queue_bio(ms, bio, rw);
  961. return 0;
  962. }
  963. m = choose_mirror(ms, bio->bi_sector);
  964. if (!m)
  965. return -EIO;
  966. map_bio(ms, m, bio);
  967. return 1;
  968. }
  969. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  970. int error, union map_info *map_context)
  971. {
  972. int rw = bio_rw(bio);
  973. struct mirror_set *ms = (struct mirror_set *) ti->private;
  974. region_t region = map_context->ll;
  975. /*
  976. * We need to dec pending if this was a write.
  977. */
  978. if (rw == WRITE)
  979. rh_dec(&ms->rh, region);
  980. return 0;
  981. }
  982. static void mirror_postsuspend(struct dm_target *ti)
  983. {
  984. struct mirror_set *ms = (struct mirror_set *) ti->private;
  985. struct dirty_log *log = ms->rh.log;
  986. rh_stop_recovery(&ms->rh);
  987. /* Wait for all I/O we generated to complete */
  988. wait_event(_kmirrord_recovery_stopped,
  989. !atomic_read(&ms->rh.recovery_in_flight));
  990. if (log->type->suspend && log->type->suspend(log))
  991. /* FIXME: need better error handling */
  992. DMWARN("log suspend failed");
  993. }
  994. static void mirror_resume(struct dm_target *ti)
  995. {
  996. struct mirror_set *ms = (struct mirror_set *) ti->private;
  997. struct dirty_log *log = ms->rh.log;
  998. if (log->type->resume && log->type->resume(log))
  999. /* FIXME: need better error handling */
  1000. DMWARN("log resume failed");
  1001. rh_start_recovery(&ms->rh);
  1002. }
  1003. static int mirror_status(struct dm_target *ti, status_type_t type,
  1004. char *result, unsigned int maxlen)
  1005. {
  1006. unsigned int m, sz;
  1007. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1008. sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
  1009. switch (type) {
  1010. case STATUSTYPE_INFO:
  1011. DMEMIT("%d ", ms->nr_mirrors);
  1012. for (m = 0; m < ms->nr_mirrors; m++)
  1013. DMEMIT("%s ", ms->mirror[m].dev->name);
  1014. DMEMIT("%llu/%llu",
  1015. (unsigned long long)ms->rh.log->type->
  1016. get_sync_count(ms->rh.log),
  1017. (unsigned long long)ms->nr_regions);
  1018. break;
  1019. case STATUSTYPE_TABLE:
  1020. DMEMIT("%d", ms->nr_mirrors);
  1021. for (m = 0; m < ms->nr_mirrors; m++)
  1022. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1023. (unsigned long long)ms->mirror[m].offset);
  1024. }
  1025. return 0;
  1026. }
  1027. static struct target_type mirror_target = {
  1028. .name = "mirror",
  1029. .version = {1, 0, 2},
  1030. .module = THIS_MODULE,
  1031. .ctr = mirror_ctr,
  1032. .dtr = mirror_dtr,
  1033. .map = mirror_map,
  1034. .end_io = mirror_end_io,
  1035. .postsuspend = mirror_postsuspend,
  1036. .resume = mirror_resume,
  1037. .status = mirror_status,
  1038. };
  1039. static int __init dm_mirror_init(void)
  1040. {
  1041. int r;
  1042. r = dm_dirty_log_init();
  1043. if (r)
  1044. return r;
  1045. _kmirrord_wq = create_singlethread_workqueue("kmirrord");
  1046. if (!_kmirrord_wq) {
  1047. DMERR("couldn't start kmirrord");
  1048. dm_dirty_log_exit();
  1049. return r;
  1050. }
  1051. INIT_WORK(&_kmirrord_work, do_work);
  1052. r = dm_register_target(&mirror_target);
  1053. if (r < 0) {
  1054. DMERR("%s: Failed to register mirror target",
  1055. mirror_target.name);
  1056. dm_dirty_log_exit();
  1057. destroy_workqueue(_kmirrord_wq);
  1058. }
  1059. return r;
  1060. }
  1061. static void __exit dm_mirror_exit(void)
  1062. {
  1063. int r;
  1064. r = dm_unregister_target(&mirror_target);
  1065. if (r < 0)
  1066. DMERR("%s: unregister failed %d", mirror_target.name, r);
  1067. destroy_workqueue(_kmirrord_wq);
  1068. dm_dirty_log_exit();
  1069. }
  1070. /* Module hooks */
  1071. module_init(dm_mirror_init);
  1072. module_exit(dm_mirror_exit);
  1073. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1074. MODULE_AUTHOR("Joe Thornber");
  1075. MODULE_LICENSE("GPL");