dm-raid1.c 34 KB

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