dm-raid1.c 29 KB

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