dm-raid1.c 33 KB

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