dm-raid1.c 34 KB

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