dm-raid1.c 32 KB

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