dm-raid1.c 34 KB

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