dm-raid1.c 34 KB

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