dm-raid1.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-bio-list.h"
  8. #include "dm-bio-record.h"
  9. #include <linux/init.h>
  10. #include <linux/mempool.h>
  11. #include <linux/module.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/slab.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/device-mapper.h>
  16. #include <linux/dm-io.h>
  17. #include <linux/dm-dirty-log.h>
  18. #include <linux/dm-kcopyd.h>
  19. #include <linux/dm-region-hash.h>
  20. #define DM_MSG_PREFIX "raid1"
  21. #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
  22. #define DM_IO_PAGES 64
  23. #define DM_KCOPYD_PAGES 64
  24. #define DM_RAID1_HANDLE_ERRORS 0x01
  25. #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
  26. static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
  27. /*-----------------------------------------------------------------
  28. * Mirror set structures.
  29. *---------------------------------------------------------------*/
  30. enum dm_raid1_error {
  31. DM_RAID1_WRITE_ERROR,
  32. DM_RAID1_SYNC_ERROR,
  33. DM_RAID1_READ_ERROR
  34. };
  35. struct mirror {
  36. struct mirror_set *ms;
  37. atomic_t error_count;
  38. unsigned long error_type;
  39. struct dm_dev *dev;
  40. sector_t offset;
  41. };
  42. struct mirror_set {
  43. struct dm_target *ti;
  44. struct list_head list;
  45. uint64_t features;
  46. spinlock_t lock; /* protects the lists */
  47. struct bio_list reads;
  48. struct bio_list writes;
  49. struct bio_list failures;
  50. struct dm_region_hash *rh;
  51. struct dm_kcopyd_client *kcopyd_client;
  52. struct dm_io_client *io_client;
  53. mempool_t *read_record_pool;
  54. /* recovery */
  55. region_t nr_regions;
  56. int in_sync;
  57. int log_failure;
  58. atomic_t suspend;
  59. atomic_t default_mirror; /* Default mirror */
  60. struct workqueue_struct *kmirrord_wq;
  61. struct work_struct kmirrord_work;
  62. struct timer_list timer;
  63. unsigned long timer_pending;
  64. struct work_struct trigger_event;
  65. unsigned nr_mirrors;
  66. struct mirror mirror[0];
  67. };
  68. static void wakeup_mirrord(void *context)
  69. {
  70. struct mirror_set *ms = context;
  71. queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
  72. }
  73. static void delayed_wake_fn(unsigned long data)
  74. {
  75. struct mirror_set *ms = (struct mirror_set *) data;
  76. clear_bit(0, &ms->timer_pending);
  77. wakeup_mirrord(ms);
  78. }
  79. static void delayed_wake(struct mirror_set *ms)
  80. {
  81. if (test_and_set_bit(0, &ms->timer_pending))
  82. return;
  83. ms->timer.expires = jiffies + HZ / 5;
  84. ms->timer.data = (unsigned long) ms;
  85. ms->timer.function = delayed_wake_fn;
  86. add_timer(&ms->timer);
  87. }
  88. static void wakeup_all_recovery_waiters(void *context)
  89. {
  90. wake_up_all(&_kmirrord_recovery_stopped);
  91. }
  92. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
  93. {
  94. unsigned long flags;
  95. int should_wake = 0;
  96. struct bio_list *bl;
  97. bl = (rw == WRITE) ? &ms->writes : &ms->reads;
  98. spin_lock_irqsave(&ms->lock, flags);
  99. should_wake = !(bl->head);
  100. bio_list_add(bl, bio);
  101. spin_unlock_irqrestore(&ms->lock, flags);
  102. if (should_wake)
  103. wakeup_mirrord(ms);
  104. }
  105. static void dispatch_bios(void *context, struct bio_list *bio_list)
  106. {
  107. struct mirror_set *ms = context;
  108. struct bio *bio;
  109. while ((bio = bio_list_pop(bio_list)))
  110. queue_bio(ms, bio, WRITE);
  111. }
  112. #define MIN_READ_RECORDS 20
  113. struct dm_raid1_read_record {
  114. struct mirror *m;
  115. struct dm_bio_details details;
  116. };
  117. static struct kmem_cache *_dm_raid1_read_record_cache;
  118. /*
  119. * Every mirror should look like this one.
  120. */
  121. #define DEFAULT_MIRROR 0
  122. /*
  123. * This is yucky. We squirrel the mirror struct away inside
  124. * bi_next for read/write buffers. This is safe since the bh
  125. * doesn't get submitted to the lower levels of block layer.
  126. */
  127. static struct mirror *bio_get_m(struct bio *bio)
  128. {
  129. return (struct mirror *) bio->bi_next;
  130. }
  131. static void bio_set_m(struct bio *bio, struct mirror *m)
  132. {
  133. bio->bi_next = (struct bio *) m;
  134. }
  135. static struct mirror *get_default_mirror(struct mirror_set *ms)
  136. {
  137. return &ms->mirror[atomic_read(&ms->default_mirror)];
  138. }
  139. static void set_default_mirror(struct mirror *m)
  140. {
  141. struct mirror_set *ms = m->ms;
  142. struct mirror *m0 = &(ms->mirror[0]);
  143. atomic_set(&ms->default_mirror, m - m0);
  144. }
  145. /* fail_mirror
  146. * @m: mirror device to fail
  147. * @error_type: one of the enum's, DM_RAID1_*_ERROR
  148. *
  149. * If errors are being handled, record the type of
  150. * error encountered for this device. If this type
  151. * of error has already been recorded, we can return;
  152. * otherwise, we must signal userspace by triggering
  153. * an event. Additionally, if the device is the
  154. * primary device, we must choose a new primary, but
  155. * only if the mirror is in-sync.
  156. *
  157. * This function must not block.
  158. */
  159. static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
  160. {
  161. struct mirror_set *ms = m->ms;
  162. struct mirror *new;
  163. /*
  164. * error_count is used for nothing more than a
  165. * simple way to tell if a device has encountered
  166. * errors.
  167. */
  168. atomic_inc(&m->error_count);
  169. if (test_and_set_bit(error_type, &m->error_type))
  170. return;
  171. if (!errors_handled(ms))
  172. return;
  173. if (m != get_default_mirror(ms))
  174. goto out;
  175. if (!ms->in_sync) {
  176. /*
  177. * Better to issue requests to same failing device
  178. * than to risk returning corrupt data.
  179. */
  180. DMERR("Primary mirror (%s) failed while out-of-sync: "
  181. "Reads may fail.", m->dev->name);
  182. goto out;
  183. }
  184. for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
  185. if (!atomic_read(&new->error_count)) {
  186. set_default_mirror(new);
  187. break;
  188. }
  189. if (unlikely(new == ms->mirror + ms->nr_mirrors))
  190. DMWARN("All sides of mirror have failed.");
  191. out:
  192. schedule_work(&ms->trigger_event);
  193. }
  194. /*-----------------------------------------------------------------
  195. * Recovery.
  196. *
  197. * When a mirror is first activated we may find that some regions
  198. * are in the no-sync state. We have to recover these by
  199. * recopying from the default mirror to all the others.
  200. *---------------------------------------------------------------*/
  201. static void recovery_complete(int read_err, unsigned long write_err,
  202. void *context)
  203. {
  204. struct dm_region *reg = context;
  205. struct mirror_set *ms = dm_rh_region_context(reg);
  206. int m, bit = 0;
  207. if (read_err) {
  208. /* Read error means the failure of default mirror. */
  209. DMERR_LIMIT("Unable to read primary mirror during recovery");
  210. fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
  211. }
  212. if (write_err) {
  213. DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
  214. write_err);
  215. /*
  216. * Bits correspond to devices (excluding default mirror).
  217. * The default mirror cannot change during recovery.
  218. */
  219. for (m = 0; m < ms->nr_mirrors; m++) {
  220. if (&ms->mirror[m] == get_default_mirror(ms))
  221. continue;
  222. if (test_bit(bit, &write_err))
  223. fail_mirror(ms->mirror + m,
  224. DM_RAID1_SYNC_ERROR);
  225. bit++;
  226. }
  227. }
  228. dm_rh_recovery_end(reg, !(read_err || write_err));
  229. }
  230. static int recover(struct mirror_set *ms, struct dm_region *reg)
  231. {
  232. int r;
  233. unsigned i;
  234. struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
  235. struct mirror *m;
  236. unsigned long flags = 0;
  237. region_t key = dm_rh_get_region_key(reg);
  238. sector_t region_size = dm_rh_get_region_size(ms->rh);
  239. /* fill in the source */
  240. m = get_default_mirror(ms);
  241. from.bdev = m->dev->bdev;
  242. from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
  243. if (key == (ms->nr_regions - 1)) {
  244. /*
  245. * The final region may be smaller than
  246. * region_size.
  247. */
  248. from.count = ms->ti->len & (region_size - 1);
  249. if (!from.count)
  250. from.count = region_size;
  251. } else
  252. from.count = region_size;
  253. /* fill in the destinations */
  254. for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
  255. if (&ms->mirror[i] == get_default_mirror(ms))
  256. continue;
  257. m = ms->mirror + i;
  258. dest->bdev = m->dev->bdev;
  259. dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
  260. dest->count = from.count;
  261. dest++;
  262. }
  263. /* hand to kcopyd */
  264. if (!errors_handled(ms))
  265. set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
  266. r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
  267. flags, recovery_complete, reg);
  268. return r;
  269. }
  270. static void do_recovery(struct mirror_set *ms)
  271. {
  272. struct dm_region *reg;
  273. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  274. int r;
  275. /*
  276. * Start quiescing some regions.
  277. */
  278. dm_rh_recovery_prepare(ms->rh);
  279. /*
  280. * Copy any already quiesced regions.
  281. */
  282. while ((reg = dm_rh_recovery_start(ms->rh))) {
  283. r = recover(ms, reg);
  284. if (r)
  285. dm_rh_recovery_end(reg, 0);
  286. }
  287. /*
  288. * Update the in sync flag.
  289. */
  290. if (!ms->in_sync &&
  291. (log->type->get_sync_count(log) == ms->nr_regions)) {
  292. /* the sync is complete */
  293. dm_table_event(ms->ti->table);
  294. ms->in_sync = 1;
  295. }
  296. }
  297. /*-----------------------------------------------------------------
  298. * Reads
  299. *---------------------------------------------------------------*/
  300. static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
  301. {
  302. struct mirror *m = get_default_mirror(ms);
  303. do {
  304. if (likely(!atomic_read(&m->error_count)))
  305. return m;
  306. if (m-- == ms->mirror)
  307. m += ms->nr_mirrors;
  308. } while (m != get_default_mirror(ms));
  309. return NULL;
  310. }
  311. static int default_ok(struct mirror *m)
  312. {
  313. struct mirror *default_mirror = get_default_mirror(m->ms);
  314. return !atomic_read(&default_mirror->error_count);
  315. }
  316. static int mirror_available(struct mirror_set *ms, struct bio *bio)
  317. {
  318. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  319. region_t region = dm_rh_bio_to_region(ms->rh, bio);
  320. if (log->type->in_sync(log, region, 0))
  321. return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
  322. return 0;
  323. }
  324. /*
  325. * remap a buffer to a particular mirror.
  326. */
  327. static sector_t map_sector(struct mirror *m, struct bio *bio)
  328. {
  329. return m->offset + (bio->bi_sector - m->ms->ti->begin);
  330. }
  331. static void map_bio(struct mirror *m, struct bio *bio)
  332. {
  333. bio->bi_bdev = m->dev->bdev;
  334. bio->bi_sector = map_sector(m, bio);
  335. }
  336. static void map_region(struct dm_io_region *io, struct mirror *m,
  337. struct bio *bio)
  338. {
  339. io->bdev = m->dev->bdev;
  340. io->sector = map_sector(m, bio);
  341. io->count = bio->bi_size >> 9;
  342. }
  343. /*-----------------------------------------------------------------
  344. * Reads
  345. *---------------------------------------------------------------*/
  346. static void read_callback(unsigned long error, void *context)
  347. {
  348. struct bio *bio = context;
  349. struct mirror *m;
  350. m = bio_get_m(bio);
  351. bio_set_m(bio, NULL);
  352. if (likely(!error)) {
  353. bio_endio(bio, 0);
  354. return;
  355. }
  356. fail_mirror(m, DM_RAID1_READ_ERROR);
  357. if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
  358. DMWARN_LIMIT("Read failure on mirror device %s. "
  359. "Trying alternative device.",
  360. m->dev->name);
  361. queue_bio(m->ms, bio, bio_rw(bio));
  362. return;
  363. }
  364. DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
  365. m->dev->name);
  366. bio_endio(bio, -EIO);
  367. }
  368. /* Asynchronous read. */
  369. static void read_async_bio(struct mirror *m, struct bio *bio)
  370. {
  371. struct dm_io_region io;
  372. struct dm_io_request io_req = {
  373. .bi_rw = READ,
  374. .mem.type = DM_IO_BVEC,
  375. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  376. .notify.fn = read_callback,
  377. .notify.context = bio,
  378. .client = m->ms->io_client,
  379. };
  380. map_region(&io, m, bio);
  381. bio_set_m(bio, m);
  382. BUG_ON(dm_io(&io_req, 1, &io, NULL));
  383. }
  384. static inline int region_in_sync(struct mirror_set *ms, region_t region,
  385. int may_block)
  386. {
  387. int state = dm_rh_get_state(ms->rh, region, may_block);
  388. return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
  389. }
  390. static void do_reads(struct mirror_set *ms, struct bio_list *reads)
  391. {
  392. region_t region;
  393. struct bio *bio;
  394. struct mirror *m;
  395. while ((bio = bio_list_pop(reads))) {
  396. region = dm_rh_bio_to_region(ms->rh, bio);
  397. m = get_default_mirror(ms);
  398. /*
  399. * We can only read balance if the region is in sync.
  400. */
  401. if (likely(region_in_sync(ms, region, 1)))
  402. m = choose_mirror(ms, bio->bi_sector);
  403. else if (m && atomic_read(&m->error_count))
  404. m = NULL;
  405. if (likely(m))
  406. read_async_bio(m, bio);
  407. else
  408. bio_endio(bio, -EIO);
  409. }
  410. }
  411. /*-----------------------------------------------------------------
  412. * Writes.
  413. *
  414. * We do different things with the write io depending on the
  415. * state of the region that it's in:
  416. *
  417. * SYNC: increment pending, use kcopyd to write to *all* mirrors
  418. * RECOVERING: delay the io until recovery completes
  419. * NOSYNC: increment pending, just write to the default mirror
  420. *---------------------------------------------------------------*/
  421. static void write_callback(unsigned long error, void *context)
  422. {
  423. unsigned i, ret = 0;
  424. struct bio *bio = (struct bio *) context;
  425. struct mirror_set *ms;
  426. int uptodate = 0;
  427. int should_wake = 0;
  428. unsigned long flags;
  429. ms = bio_get_m(bio)->ms;
  430. bio_set_m(bio, NULL);
  431. /*
  432. * NOTE: We don't decrement the pending count here,
  433. * instead it is done by the targets endio function.
  434. * This way we handle both writes to SYNC and NOSYNC
  435. * regions with the same code.
  436. */
  437. if (likely(!error))
  438. goto out;
  439. for (i = 0; i < ms->nr_mirrors; i++)
  440. if (test_bit(i, &error))
  441. fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
  442. else
  443. uptodate = 1;
  444. if (unlikely(!uptodate)) {
  445. DMERR("All replicated volumes dead, failing I/O");
  446. /* None of the writes succeeded, fail the I/O. */
  447. ret = -EIO;
  448. } else if (errors_handled(ms)) {
  449. /*
  450. * Need to raise event. Since raising
  451. * events can block, we need to do it in
  452. * the main thread.
  453. */
  454. spin_lock_irqsave(&ms->lock, flags);
  455. if (!ms->failures.head)
  456. should_wake = 1;
  457. bio_list_add(&ms->failures, bio);
  458. spin_unlock_irqrestore(&ms->lock, flags);
  459. if (should_wake)
  460. wakeup_mirrord(ms);
  461. return;
  462. }
  463. out:
  464. bio_endio(bio, ret);
  465. }
  466. static void do_write(struct mirror_set *ms, struct bio *bio)
  467. {
  468. unsigned int i;
  469. struct dm_io_region io[ms->nr_mirrors], *dest = io;
  470. struct mirror *m;
  471. struct dm_io_request io_req = {
  472. .bi_rw = WRITE,
  473. .mem.type = DM_IO_BVEC,
  474. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  475. .notify.fn = write_callback,
  476. .notify.context = bio,
  477. .client = ms->io_client,
  478. };
  479. for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
  480. map_region(dest++, m, bio);
  481. /*
  482. * Use default mirror because we only need it to retrieve the reference
  483. * to the mirror set in write_callback().
  484. */
  485. bio_set_m(bio, get_default_mirror(ms));
  486. BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
  487. }
  488. static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  489. {
  490. int state;
  491. struct bio *bio;
  492. struct bio_list sync, nosync, recover, *this_list = NULL;
  493. struct bio_list requeue;
  494. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  495. region_t region;
  496. if (!writes->head)
  497. return;
  498. /*
  499. * Classify each write.
  500. */
  501. bio_list_init(&sync);
  502. bio_list_init(&nosync);
  503. bio_list_init(&recover);
  504. bio_list_init(&requeue);
  505. while ((bio = bio_list_pop(writes))) {
  506. region = dm_rh_bio_to_region(ms->rh, bio);
  507. if (log->type->is_remote_recovering &&
  508. log->type->is_remote_recovering(log, region)) {
  509. bio_list_add(&requeue, bio);
  510. continue;
  511. }
  512. state = dm_rh_get_state(ms->rh, region, 1);
  513. switch (state) {
  514. case DM_RH_CLEAN:
  515. case DM_RH_DIRTY:
  516. this_list = &sync;
  517. break;
  518. case DM_RH_NOSYNC:
  519. this_list = &nosync;
  520. break;
  521. case DM_RH_RECOVERING:
  522. this_list = &recover;
  523. break;
  524. }
  525. bio_list_add(this_list, bio);
  526. }
  527. /*
  528. * Add bios that are delayed due to remote recovery
  529. * back on to the write queue
  530. */
  531. if (unlikely(requeue.head)) {
  532. spin_lock_irq(&ms->lock);
  533. bio_list_merge(&ms->writes, &requeue);
  534. spin_unlock_irq(&ms->lock);
  535. }
  536. /*
  537. * Increment the pending counts for any regions that will
  538. * be written to (writes to recover regions are going to
  539. * be delayed).
  540. */
  541. dm_rh_inc_pending(ms->rh, &sync);
  542. dm_rh_inc_pending(ms->rh, &nosync);
  543. ms->log_failure = dm_rh_flush(ms->rh) ? 1 : 0;
  544. /*
  545. * Dispatch io.
  546. */
  547. if (unlikely(ms->log_failure)) {
  548. spin_lock_irq(&ms->lock);
  549. bio_list_merge(&ms->failures, &sync);
  550. spin_unlock_irq(&ms->lock);
  551. wakeup_mirrord(ms);
  552. } else
  553. while ((bio = bio_list_pop(&sync)))
  554. do_write(ms, bio);
  555. while ((bio = bio_list_pop(&recover)))
  556. dm_rh_delay(ms->rh, bio);
  557. while ((bio = bio_list_pop(&nosync))) {
  558. map_bio(get_default_mirror(ms), bio);
  559. generic_make_request(bio);
  560. }
  561. }
  562. static void do_failures(struct mirror_set *ms, struct bio_list *failures)
  563. {
  564. struct bio *bio;
  565. if (!failures->head)
  566. return;
  567. if (!ms->log_failure) {
  568. while ((bio = bio_list_pop(failures))) {
  569. ms->in_sync = 0;
  570. dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
  571. }
  572. return;
  573. }
  574. /*
  575. * If the log has failed, unattempted writes are being
  576. * put on the failures list. We can't issue those writes
  577. * until a log has been marked, so we must store them.
  578. *
  579. * If a 'noflush' suspend is in progress, we can requeue
  580. * the I/O's to the core. This give userspace a chance
  581. * to reconfigure the mirror, at which point the core
  582. * will reissue the writes. If the 'noflush' flag is
  583. * not set, we have no choice but to return errors.
  584. *
  585. * Some writes on the failures list may have been
  586. * submitted before the log failure and represent a
  587. * failure to write to one of the devices. It is ok
  588. * for us to treat them the same and requeue them
  589. * as well.
  590. */
  591. if (dm_noflush_suspending(ms->ti)) {
  592. while ((bio = bio_list_pop(failures)))
  593. bio_endio(bio, DM_ENDIO_REQUEUE);
  594. return;
  595. }
  596. if (atomic_read(&ms->suspend)) {
  597. while ((bio = bio_list_pop(failures)))
  598. bio_endio(bio, -EIO);
  599. return;
  600. }
  601. spin_lock_irq(&ms->lock);
  602. bio_list_merge(&ms->failures, failures);
  603. spin_unlock_irq(&ms->lock);
  604. delayed_wake(ms);
  605. }
  606. static void trigger_event(struct work_struct *work)
  607. {
  608. struct mirror_set *ms =
  609. container_of(work, struct mirror_set, trigger_event);
  610. dm_table_event(ms->ti->table);
  611. }
  612. /*-----------------------------------------------------------------
  613. * kmirrord
  614. *---------------------------------------------------------------*/
  615. static void do_mirror(struct work_struct *work)
  616. {
  617. struct mirror_set *ms = container_of(work, struct mirror_set,
  618. kmirrord_work);
  619. struct bio_list reads, writes, failures;
  620. unsigned long flags;
  621. spin_lock_irqsave(&ms->lock, flags);
  622. reads = ms->reads;
  623. writes = ms->writes;
  624. failures = ms->failures;
  625. bio_list_init(&ms->reads);
  626. bio_list_init(&ms->writes);
  627. bio_list_init(&ms->failures);
  628. spin_unlock_irqrestore(&ms->lock, flags);
  629. dm_rh_update_states(ms->rh, errors_handled(ms));
  630. do_recovery(ms);
  631. do_reads(ms, &reads);
  632. do_writes(ms, &writes);
  633. do_failures(ms, &failures);
  634. dm_table_unplug_all(ms->ti->table);
  635. }
  636. /*-----------------------------------------------------------------
  637. * Target functions
  638. *---------------------------------------------------------------*/
  639. static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  640. uint32_t region_size,
  641. struct dm_target *ti,
  642. struct dm_dirty_log *dl)
  643. {
  644. size_t len;
  645. struct mirror_set *ms = NULL;
  646. len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
  647. ms = kzalloc(len, GFP_KERNEL);
  648. if (!ms) {
  649. ti->error = "Cannot allocate mirror context";
  650. return NULL;
  651. }
  652. spin_lock_init(&ms->lock);
  653. ms->ti = ti;
  654. ms->nr_mirrors = nr_mirrors;
  655. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  656. ms->in_sync = 0;
  657. ms->log_failure = 0;
  658. atomic_set(&ms->suspend, 0);
  659. atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
  660. ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
  661. _dm_raid1_read_record_cache);
  662. if (!ms->read_record_pool) {
  663. ti->error = "Error creating mirror read_record_pool";
  664. kfree(ms);
  665. return NULL;
  666. }
  667. ms->io_client = dm_io_client_create(DM_IO_PAGES);
  668. if (IS_ERR(ms->io_client)) {
  669. ti->error = "Error creating dm_io client";
  670. mempool_destroy(ms->read_record_pool);
  671. kfree(ms);
  672. return NULL;
  673. }
  674. ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
  675. wakeup_all_recovery_waiters,
  676. ms->ti->begin, MAX_RECOVERY,
  677. dl, region_size, ms->nr_regions);
  678. if (IS_ERR(ms->rh)) {
  679. ti->error = "Error creating dirty region hash";
  680. dm_io_client_destroy(ms->io_client);
  681. mempool_destroy(ms->read_record_pool);
  682. kfree(ms);
  683. return NULL;
  684. }
  685. return ms;
  686. }
  687. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  688. unsigned int m)
  689. {
  690. while (m--)
  691. dm_put_device(ti, ms->mirror[m].dev);
  692. dm_io_client_destroy(ms->io_client);
  693. dm_region_hash_destroy(ms->rh);
  694. mempool_destroy(ms->read_record_pool);
  695. kfree(ms);
  696. }
  697. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  698. unsigned int mirror, char **argv)
  699. {
  700. unsigned long long offset;
  701. if (sscanf(argv[1], "%llu", &offset) != 1) {
  702. ti->error = "Invalid offset";
  703. return -EINVAL;
  704. }
  705. if (dm_get_device(ti, argv[0], offset, ti->len,
  706. dm_table_get_mode(ti->table),
  707. &ms->mirror[mirror].dev)) {
  708. ti->error = "Device lookup failure";
  709. return -ENXIO;
  710. }
  711. ms->mirror[mirror].ms = ms;
  712. atomic_set(&(ms->mirror[mirror].error_count), 0);
  713. ms->mirror[mirror].error_type = 0;
  714. ms->mirror[mirror].offset = offset;
  715. return 0;
  716. }
  717. /*
  718. * Create dirty log: log_type #log_params <log_params>
  719. */
  720. static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
  721. unsigned argc, char **argv,
  722. unsigned *args_used)
  723. {
  724. unsigned param_count;
  725. struct dm_dirty_log *dl;
  726. if (argc < 2) {
  727. ti->error = "Insufficient mirror log arguments";
  728. return NULL;
  729. }
  730. if (sscanf(argv[1], "%u", &param_count) != 1) {
  731. ti->error = "Invalid mirror log argument count";
  732. return NULL;
  733. }
  734. *args_used = 2 + param_count;
  735. if (argc < *args_used) {
  736. ti->error = "Insufficient mirror log arguments";
  737. return NULL;
  738. }
  739. dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
  740. if (!dl) {
  741. ti->error = "Error creating mirror dirty log";
  742. return NULL;
  743. }
  744. return dl;
  745. }
  746. static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
  747. unsigned *args_used)
  748. {
  749. unsigned num_features;
  750. struct dm_target *ti = ms->ti;
  751. *args_used = 0;
  752. if (!argc)
  753. return 0;
  754. if (sscanf(argv[0], "%u", &num_features) != 1) {
  755. ti->error = "Invalid number of features";
  756. return -EINVAL;
  757. }
  758. argc--;
  759. argv++;
  760. (*args_used)++;
  761. if (num_features > argc) {
  762. ti->error = "Not enough arguments to support feature count";
  763. return -EINVAL;
  764. }
  765. if (!strcmp("handle_errors", argv[0]))
  766. ms->features |= DM_RAID1_HANDLE_ERRORS;
  767. else {
  768. ti->error = "Unrecognised feature requested";
  769. return -EINVAL;
  770. }
  771. (*args_used)++;
  772. return 0;
  773. }
  774. /*
  775. * Construct a mirror mapping:
  776. *
  777. * log_type #log_params <log_params>
  778. * #mirrors [mirror_path offset]{2,}
  779. * [#features <features>]
  780. *
  781. * log_type is "core" or "disk"
  782. * #log_params is between 1 and 3
  783. *
  784. * If present, features must be "handle_errors".
  785. */
  786. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  787. {
  788. int r;
  789. unsigned int nr_mirrors, m, args_used;
  790. struct mirror_set *ms;
  791. struct dm_dirty_log *dl;
  792. dl = create_dirty_log(ti, argc, argv, &args_used);
  793. if (!dl)
  794. return -EINVAL;
  795. argv += args_used;
  796. argc -= args_used;
  797. if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
  798. nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
  799. ti->error = "Invalid number of mirrors";
  800. dm_dirty_log_destroy(dl);
  801. return -EINVAL;
  802. }
  803. argv++, argc--;
  804. if (argc < nr_mirrors * 2) {
  805. ti->error = "Too few mirror arguments";
  806. dm_dirty_log_destroy(dl);
  807. return -EINVAL;
  808. }
  809. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  810. if (!ms) {
  811. dm_dirty_log_destroy(dl);
  812. return -ENOMEM;
  813. }
  814. /* Get the mirror parameter sets */
  815. for (m = 0; m < nr_mirrors; m++) {
  816. r = get_mirror(ms, ti, m, argv);
  817. if (r) {
  818. free_context(ms, ti, m);
  819. return r;
  820. }
  821. argv += 2;
  822. argc -= 2;
  823. }
  824. ti->private = ms;
  825. ti->split_io = dm_rh_get_region_size(ms->rh);
  826. ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
  827. if (!ms->kmirrord_wq) {
  828. DMERR("couldn't start kmirrord");
  829. r = -ENOMEM;
  830. goto err_free_context;
  831. }
  832. INIT_WORK(&ms->kmirrord_work, do_mirror);
  833. init_timer(&ms->timer);
  834. ms->timer_pending = 0;
  835. INIT_WORK(&ms->trigger_event, trigger_event);
  836. r = parse_features(ms, argc, argv, &args_used);
  837. if (r)
  838. goto err_destroy_wq;
  839. argv += args_used;
  840. argc -= args_used;
  841. /*
  842. * Any read-balancing addition depends on the
  843. * DM_RAID1_HANDLE_ERRORS flag being present.
  844. * This is because the decision to balance depends
  845. * on the sync state of a region. If the above
  846. * flag is not present, we ignore errors; and
  847. * the sync state may be inaccurate.
  848. */
  849. if (argc) {
  850. ti->error = "Too many mirror arguments";
  851. r = -EINVAL;
  852. goto err_destroy_wq;
  853. }
  854. r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
  855. if (r)
  856. goto err_destroy_wq;
  857. wakeup_mirrord(ms);
  858. return 0;
  859. err_destroy_wq:
  860. destroy_workqueue(ms->kmirrord_wq);
  861. err_free_context:
  862. free_context(ms, ti, ms->nr_mirrors);
  863. return r;
  864. }
  865. static void mirror_dtr(struct dm_target *ti)
  866. {
  867. struct mirror_set *ms = (struct mirror_set *) ti->private;
  868. del_timer_sync(&ms->timer);
  869. flush_workqueue(ms->kmirrord_wq);
  870. flush_scheduled_work();
  871. dm_kcopyd_client_destroy(ms->kcopyd_client);
  872. destroy_workqueue(ms->kmirrord_wq);
  873. free_context(ms, ti, ms->nr_mirrors);
  874. }
  875. /*
  876. * Mirror mapping function
  877. */
  878. static int mirror_map(struct dm_target *ti, struct bio *bio,
  879. union map_info *map_context)
  880. {
  881. int r, rw = bio_rw(bio);
  882. struct mirror *m;
  883. struct mirror_set *ms = ti->private;
  884. struct dm_raid1_read_record *read_record = NULL;
  885. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  886. if (rw == WRITE) {
  887. /* Save region for mirror_end_io() handler */
  888. map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
  889. queue_bio(ms, bio, rw);
  890. return DM_MAPIO_SUBMITTED;
  891. }
  892. r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
  893. if (r < 0 && r != -EWOULDBLOCK)
  894. return r;
  895. /*
  896. * If region is not in-sync queue the bio.
  897. */
  898. if (!r || (r == -EWOULDBLOCK)) {
  899. if (rw == READA)
  900. return -EWOULDBLOCK;
  901. queue_bio(ms, bio, rw);
  902. return DM_MAPIO_SUBMITTED;
  903. }
  904. /*
  905. * The region is in-sync and we can perform reads directly.
  906. * Store enough information so we can retry if it fails.
  907. */
  908. m = choose_mirror(ms, bio->bi_sector);
  909. if (unlikely(!m))
  910. return -EIO;
  911. read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
  912. if (likely(read_record)) {
  913. dm_bio_record(&read_record->details, bio);
  914. map_context->ptr = read_record;
  915. read_record->m = m;
  916. }
  917. map_bio(m, bio);
  918. return DM_MAPIO_REMAPPED;
  919. }
  920. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  921. int error, union map_info *map_context)
  922. {
  923. int rw = bio_rw(bio);
  924. struct mirror_set *ms = (struct mirror_set *) ti->private;
  925. struct mirror *m = NULL;
  926. struct dm_bio_details *bd = NULL;
  927. struct dm_raid1_read_record *read_record = map_context->ptr;
  928. /*
  929. * We need to dec pending if this was a write.
  930. */
  931. if (rw == WRITE) {
  932. dm_rh_dec(ms->rh, map_context->ll);
  933. return error;
  934. }
  935. if (error == -EOPNOTSUPP)
  936. goto out;
  937. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  938. goto out;
  939. if (unlikely(error)) {
  940. if (!read_record) {
  941. /*
  942. * There wasn't enough memory to record necessary
  943. * information for a retry or there was no other
  944. * mirror in-sync.
  945. */
  946. DMERR_LIMIT("Mirror read failed.");
  947. return -EIO;
  948. }
  949. m = read_record->m;
  950. DMERR("Mirror read failed from %s. Trying alternative device.",
  951. m->dev->name);
  952. fail_mirror(m, DM_RAID1_READ_ERROR);
  953. /*
  954. * A failed read is requeued for another attempt using an intact
  955. * mirror.
  956. */
  957. if (default_ok(m) || mirror_available(ms, bio)) {
  958. bd = &read_record->details;
  959. dm_bio_restore(bd, bio);
  960. mempool_free(read_record, ms->read_record_pool);
  961. map_context->ptr = NULL;
  962. queue_bio(ms, bio, rw);
  963. return 1;
  964. }
  965. DMERR("All replicated volumes dead, failing I/O");
  966. }
  967. out:
  968. if (read_record) {
  969. mempool_free(read_record, ms->read_record_pool);
  970. map_context->ptr = NULL;
  971. }
  972. return error;
  973. }
  974. static void mirror_presuspend(struct dm_target *ti)
  975. {
  976. struct mirror_set *ms = (struct mirror_set *) ti->private;
  977. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  978. atomic_set(&ms->suspend, 1);
  979. /*
  980. * We must finish up all the work that we've
  981. * generated (i.e. recovery work).
  982. */
  983. dm_rh_stop_recovery(ms->rh);
  984. wait_event(_kmirrord_recovery_stopped,
  985. !dm_rh_recovery_in_flight(ms->rh));
  986. if (log->type->presuspend && log->type->presuspend(log))
  987. /* FIXME: need better error handling */
  988. DMWARN("log presuspend failed");
  989. /*
  990. * Now that recovery is complete/stopped and the
  991. * delayed bios are queued, we need to wait for
  992. * the worker thread to complete. This way,
  993. * we know that all of our I/O has been pushed.
  994. */
  995. flush_workqueue(ms->kmirrord_wq);
  996. }
  997. static void mirror_postsuspend(struct dm_target *ti)
  998. {
  999. struct mirror_set *ms = ti->private;
  1000. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1001. if (log->type->postsuspend && log->type->postsuspend(log))
  1002. /* FIXME: need better error handling */
  1003. DMWARN("log postsuspend failed");
  1004. }
  1005. static void mirror_resume(struct dm_target *ti)
  1006. {
  1007. struct mirror_set *ms = ti->private;
  1008. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1009. atomic_set(&ms->suspend, 0);
  1010. if (log->type->resume && log->type->resume(log))
  1011. /* FIXME: need better error handling */
  1012. DMWARN("log resume failed");
  1013. dm_rh_start_recovery(ms->rh);
  1014. }
  1015. /*
  1016. * device_status_char
  1017. * @m: mirror device/leg we want the status of
  1018. *
  1019. * We return one character representing the most severe error
  1020. * we have encountered.
  1021. * A => Alive - No failures
  1022. * D => Dead - A write failure occurred leaving mirror out-of-sync
  1023. * S => Sync - A sychronization failure occurred, mirror out-of-sync
  1024. * R => Read - A read failure occurred, mirror data unaffected
  1025. *
  1026. * Returns: <char>
  1027. */
  1028. static char device_status_char(struct mirror *m)
  1029. {
  1030. if (!atomic_read(&(m->error_count)))
  1031. return 'A';
  1032. return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
  1033. (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  1034. (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  1035. }
  1036. static int mirror_status(struct dm_target *ti, status_type_t type,
  1037. char *result, unsigned int maxlen)
  1038. {
  1039. unsigned int m, sz = 0;
  1040. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1041. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1042. char buffer[ms->nr_mirrors + 1];
  1043. switch (type) {
  1044. case STATUSTYPE_INFO:
  1045. DMEMIT("%d ", ms->nr_mirrors);
  1046. for (m = 0; m < ms->nr_mirrors; m++) {
  1047. DMEMIT("%s ", ms->mirror[m].dev->name);
  1048. buffer[m] = device_status_char(&(ms->mirror[m]));
  1049. }
  1050. buffer[m] = '\0';
  1051. DMEMIT("%llu/%llu 1 %s ",
  1052. (unsigned long long)log->type->get_sync_count(log),
  1053. (unsigned long long)ms->nr_regions, buffer);
  1054. sz += log->type->status(log, type, result+sz, maxlen-sz);
  1055. break;
  1056. case STATUSTYPE_TABLE:
  1057. sz = log->type->status(log, type, result, maxlen);
  1058. DMEMIT("%d", ms->nr_mirrors);
  1059. for (m = 0; m < ms->nr_mirrors; m++)
  1060. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1061. (unsigned long long)ms->mirror[m].offset);
  1062. if (ms->features & DM_RAID1_HANDLE_ERRORS)
  1063. DMEMIT(" 1 handle_errors");
  1064. }
  1065. return 0;
  1066. }
  1067. static struct target_type mirror_target = {
  1068. .name = "mirror",
  1069. .version = {1, 0, 20},
  1070. .module = THIS_MODULE,
  1071. .ctr = mirror_ctr,
  1072. .dtr = mirror_dtr,
  1073. .map = mirror_map,
  1074. .end_io = mirror_end_io,
  1075. .presuspend = mirror_presuspend,
  1076. .postsuspend = mirror_postsuspend,
  1077. .resume = mirror_resume,
  1078. .status = mirror_status,
  1079. };
  1080. static int __init dm_mirror_init(void)
  1081. {
  1082. int r;
  1083. _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
  1084. if (!_dm_raid1_read_record_cache) {
  1085. DMERR("Can't allocate dm_raid1_read_record cache");
  1086. r = -ENOMEM;
  1087. goto bad_cache;
  1088. }
  1089. r = dm_register_target(&mirror_target);
  1090. if (r < 0) {
  1091. DMERR("Failed to register mirror target");
  1092. goto bad_target;
  1093. }
  1094. return 0;
  1095. bad_target:
  1096. kmem_cache_destroy(_dm_raid1_read_record_cache);
  1097. bad_cache:
  1098. return r;
  1099. }
  1100. static void __exit dm_mirror_exit(void)
  1101. {
  1102. dm_unregister_target(&mirror_target);
  1103. kmem_cache_destroy(_dm_raid1_read_record_cache);
  1104. }
  1105. /* Module hooks */
  1106. module_init(dm_mirror_init);
  1107. module_exit(dm_mirror_exit);
  1108. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1109. MODULE_AUTHOR("Joe Thornber");
  1110. MODULE_LICENSE("GPL");