dm-raid1.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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. /*
  503. * If the bio is discard, return an error, but do not
  504. * degrade the array.
  505. */
  506. if (bio->bi_rw & REQ_DISCARD) {
  507. bio_endio(bio, -EOPNOTSUPP);
  508. return;
  509. }
  510. for (i = 0; i < ms->nr_mirrors; i++)
  511. if (test_bit(i, &error))
  512. fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
  513. /*
  514. * Need to raise event. Since raising
  515. * events can block, we need to do it in
  516. * the main thread.
  517. */
  518. spin_lock_irqsave(&ms->lock, flags);
  519. if (!ms->failures.head)
  520. should_wake = 1;
  521. bio_list_add(&ms->failures, bio);
  522. spin_unlock_irqrestore(&ms->lock, flags);
  523. if (should_wake)
  524. wakeup_mirrord(ms);
  525. }
  526. static void do_write(struct mirror_set *ms, struct bio *bio)
  527. {
  528. unsigned int i;
  529. struct dm_io_region io[ms->nr_mirrors], *dest = io;
  530. struct mirror *m;
  531. struct dm_io_request io_req = {
  532. .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
  533. .mem.type = DM_IO_BVEC,
  534. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  535. .notify.fn = write_callback,
  536. .notify.context = bio,
  537. .client = ms->io_client,
  538. };
  539. if (bio->bi_rw & REQ_DISCARD) {
  540. io_req.bi_rw |= REQ_DISCARD;
  541. io_req.mem.type = DM_IO_KMEM;
  542. io_req.mem.ptr.addr = NULL;
  543. }
  544. for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
  545. map_region(dest++, m, bio);
  546. /*
  547. * Use default mirror because we only need it to retrieve the reference
  548. * to the mirror set in write_callback().
  549. */
  550. bio_set_m(bio, get_default_mirror(ms));
  551. BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
  552. }
  553. static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  554. {
  555. int state;
  556. struct bio *bio;
  557. struct bio_list sync, nosync, recover, *this_list = NULL;
  558. struct bio_list requeue;
  559. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  560. region_t region;
  561. if (!writes->head)
  562. return;
  563. /*
  564. * Classify each write.
  565. */
  566. bio_list_init(&sync);
  567. bio_list_init(&nosync);
  568. bio_list_init(&recover);
  569. bio_list_init(&requeue);
  570. while ((bio = bio_list_pop(writes))) {
  571. if ((bio->bi_rw & REQ_FLUSH) ||
  572. (bio->bi_rw & REQ_DISCARD)) {
  573. bio_list_add(&sync, bio);
  574. continue;
  575. }
  576. region = dm_rh_bio_to_region(ms->rh, bio);
  577. if (log->type->is_remote_recovering &&
  578. log->type->is_remote_recovering(log, region)) {
  579. bio_list_add(&requeue, bio);
  580. continue;
  581. }
  582. state = dm_rh_get_state(ms->rh, region, 1);
  583. switch (state) {
  584. case DM_RH_CLEAN:
  585. case DM_RH_DIRTY:
  586. this_list = &sync;
  587. break;
  588. case DM_RH_NOSYNC:
  589. this_list = &nosync;
  590. break;
  591. case DM_RH_RECOVERING:
  592. this_list = &recover;
  593. break;
  594. }
  595. bio_list_add(this_list, bio);
  596. }
  597. /*
  598. * Add bios that are delayed due to remote recovery
  599. * back on to the write queue
  600. */
  601. if (unlikely(requeue.head)) {
  602. spin_lock_irq(&ms->lock);
  603. bio_list_merge(&ms->writes, &requeue);
  604. spin_unlock_irq(&ms->lock);
  605. delayed_wake(ms);
  606. }
  607. /*
  608. * Increment the pending counts for any regions that will
  609. * be written to (writes to recover regions are going to
  610. * be delayed).
  611. */
  612. dm_rh_inc_pending(ms->rh, &sync);
  613. dm_rh_inc_pending(ms->rh, &nosync);
  614. /*
  615. * If the flush fails on a previous call and succeeds here,
  616. * we must not reset the log_failure variable. We need
  617. * userspace interaction to do that.
  618. */
  619. ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
  620. /*
  621. * Dispatch io.
  622. */
  623. if (unlikely(ms->log_failure) && errors_handled(ms)) {
  624. spin_lock_irq(&ms->lock);
  625. bio_list_merge(&ms->failures, &sync);
  626. spin_unlock_irq(&ms->lock);
  627. wakeup_mirrord(ms);
  628. } else
  629. while ((bio = bio_list_pop(&sync)))
  630. do_write(ms, bio);
  631. while ((bio = bio_list_pop(&recover)))
  632. dm_rh_delay(ms->rh, bio);
  633. while ((bio = bio_list_pop(&nosync))) {
  634. if (unlikely(ms->leg_failure) && errors_handled(ms)) {
  635. spin_lock_irq(&ms->lock);
  636. bio_list_add(&ms->failures, bio);
  637. spin_unlock_irq(&ms->lock);
  638. wakeup_mirrord(ms);
  639. } else {
  640. map_bio(get_default_mirror(ms), bio);
  641. generic_make_request(bio);
  642. }
  643. }
  644. }
  645. static void do_failures(struct mirror_set *ms, struct bio_list *failures)
  646. {
  647. struct bio *bio;
  648. if (likely(!failures->head))
  649. return;
  650. /*
  651. * If the log has failed, unattempted writes are being
  652. * put on the holds list. We can't issue those writes
  653. * until a log has been marked, so we must store them.
  654. *
  655. * If a 'noflush' suspend is in progress, we can requeue
  656. * the I/O's to the core. This give userspace a chance
  657. * to reconfigure the mirror, at which point the core
  658. * will reissue the writes. If the 'noflush' flag is
  659. * not set, we have no choice but to return errors.
  660. *
  661. * Some writes on the failures list may have been
  662. * submitted before the log failure and represent a
  663. * failure to write to one of the devices. It is ok
  664. * for us to treat them the same and requeue them
  665. * as well.
  666. */
  667. while ((bio = bio_list_pop(failures))) {
  668. if (!ms->log_failure) {
  669. ms->in_sync = 0;
  670. dm_rh_mark_nosync(ms->rh, bio);
  671. }
  672. /*
  673. * If all the legs are dead, fail the I/O.
  674. * If we have been told to handle errors, hold the bio
  675. * and wait for userspace to deal with the problem.
  676. * Otherwise pretend that the I/O succeeded. (This would
  677. * be wrong if the failed leg returned after reboot and
  678. * got replicated back to the good legs.)
  679. */
  680. if (!get_valid_mirror(ms))
  681. bio_endio(bio, -EIO);
  682. else if (errors_handled(ms))
  683. hold_bio(ms, bio);
  684. else
  685. bio_endio(bio, 0);
  686. }
  687. }
  688. static void trigger_event(struct work_struct *work)
  689. {
  690. struct mirror_set *ms =
  691. container_of(work, struct mirror_set, trigger_event);
  692. dm_table_event(ms->ti->table);
  693. }
  694. /*-----------------------------------------------------------------
  695. * kmirrord
  696. *---------------------------------------------------------------*/
  697. static void do_mirror(struct work_struct *work)
  698. {
  699. struct mirror_set *ms = container_of(work, struct mirror_set,
  700. kmirrord_work);
  701. struct bio_list reads, writes, failures;
  702. unsigned long flags;
  703. spin_lock_irqsave(&ms->lock, flags);
  704. reads = ms->reads;
  705. writes = ms->writes;
  706. failures = ms->failures;
  707. bio_list_init(&ms->reads);
  708. bio_list_init(&ms->writes);
  709. bio_list_init(&ms->failures);
  710. spin_unlock_irqrestore(&ms->lock, flags);
  711. dm_rh_update_states(ms->rh, errors_handled(ms));
  712. do_recovery(ms);
  713. do_reads(ms, &reads);
  714. do_writes(ms, &writes);
  715. do_failures(ms, &failures);
  716. }
  717. /*-----------------------------------------------------------------
  718. * Target functions
  719. *---------------------------------------------------------------*/
  720. static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  721. uint32_t region_size,
  722. struct dm_target *ti,
  723. struct dm_dirty_log *dl)
  724. {
  725. size_t len;
  726. struct mirror_set *ms = NULL;
  727. len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
  728. ms = kzalloc(len, GFP_KERNEL);
  729. if (!ms) {
  730. ti->error = "Cannot allocate mirror context";
  731. return NULL;
  732. }
  733. spin_lock_init(&ms->lock);
  734. bio_list_init(&ms->reads);
  735. bio_list_init(&ms->writes);
  736. bio_list_init(&ms->failures);
  737. bio_list_init(&ms->holds);
  738. ms->ti = ti;
  739. ms->nr_mirrors = nr_mirrors;
  740. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  741. ms->in_sync = 0;
  742. ms->log_failure = 0;
  743. ms->leg_failure = 0;
  744. atomic_set(&ms->suspend, 0);
  745. atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
  746. ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
  747. _dm_raid1_read_record_cache);
  748. if (!ms->read_record_pool) {
  749. ti->error = "Error creating mirror read_record_pool";
  750. kfree(ms);
  751. return NULL;
  752. }
  753. ms->io_client = dm_io_client_create();
  754. if (IS_ERR(ms->io_client)) {
  755. ti->error = "Error creating dm_io client";
  756. mempool_destroy(ms->read_record_pool);
  757. kfree(ms);
  758. return NULL;
  759. }
  760. ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
  761. wakeup_all_recovery_waiters,
  762. ms->ti->begin, MAX_RECOVERY,
  763. dl, region_size, ms->nr_regions);
  764. if (IS_ERR(ms->rh)) {
  765. ti->error = "Error creating dirty region hash";
  766. dm_io_client_destroy(ms->io_client);
  767. mempool_destroy(ms->read_record_pool);
  768. kfree(ms);
  769. return NULL;
  770. }
  771. return ms;
  772. }
  773. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  774. unsigned int m)
  775. {
  776. while (m--)
  777. dm_put_device(ti, ms->mirror[m].dev);
  778. dm_io_client_destroy(ms->io_client);
  779. dm_region_hash_destroy(ms->rh);
  780. mempool_destroy(ms->read_record_pool);
  781. kfree(ms);
  782. }
  783. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  784. unsigned int mirror, char **argv)
  785. {
  786. unsigned long long offset;
  787. char dummy;
  788. if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
  789. ti->error = "Invalid offset";
  790. return -EINVAL;
  791. }
  792. if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  793. &ms->mirror[mirror].dev)) {
  794. ti->error = "Device lookup failure";
  795. return -ENXIO;
  796. }
  797. ms->mirror[mirror].ms = ms;
  798. atomic_set(&(ms->mirror[mirror].error_count), 0);
  799. ms->mirror[mirror].error_type = 0;
  800. ms->mirror[mirror].offset = offset;
  801. return 0;
  802. }
  803. /*
  804. * Create dirty log: log_type #log_params <log_params>
  805. */
  806. static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
  807. unsigned argc, char **argv,
  808. unsigned *args_used)
  809. {
  810. unsigned param_count;
  811. struct dm_dirty_log *dl;
  812. char dummy;
  813. if (argc < 2) {
  814. ti->error = "Insufficient mirror log arguments";
  815. return NULL;
  816. }
  817. if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
  818. ti->error = "Invalid mirror log argument count";
  819. return NULL;
  820. }
  821. *args_used = 2 + param_count;
  822. if (argc < *args_used) {
  823. ti->error = "Insufficient mirror log arguments";
  824. return NULL;
  825. }
  826. dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
  827. argv + 2);
  828. if (!dl) {
  829. ti->error = "Error creating mirror dirty log";
  830. return NULL;
  831. }
  832. return dl;
  833. }
  834. static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
  835. unsigned *args_used)
  836. {
  837. unsigned num_features;
  838. struct dm_target *ti = ms->ti;
  839. char dummy;
  840. *args_used = 0;
  841. if (!argc)
  842. return 0;
  843. if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
  844. ti->error = "Invalid number of features";
  845. return -EINVAL;
  846. }
  847. argc--;
  848. argv++;
  849. (*args_used)++;
  850. if (num_features > argc) {
  851. ti->error = "Not enough arguments to support feature count";
  852. return -EINVAL;
  853. }
  854. if (!strcmp("handle_errors", argv[0]))
  855. ms->features |= DM_RAID1_HANDLE_ERRORS;
  856. else {
  857. ti->error = "Unrecognised feature requested";
  858. return -EINVAL;
  859. }
  860. (*args_used)++;
  861. return 0;
  862. }
  863. /*
  864. * Construct a mirror mapping:
  865. *
  866. * log_type #log_params <log_params>
  867. * #mirrors [mirror_path offset]{2,}
  868. * [#features <features>]
  869. *
  870. * log_type is "core" or "disk"
  871. * #log_params is between 1 and 3
  872. *
  873. * If present, features must be "handle_errors".
  874. */
  875. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  876. {
  877. int r;
  878. unsigned int nr_mirrors, m, args_used;
  879. struct mirror_set *ms;
  880. struct dm_dirty_log *dl;
  881. char dummy;
  882. dl = create_dirty_log(ti, argc, argv, &args_used);
  883. if (!dl)
  884. return -EINVAL;
  885. argv += args_used;
  886. argc -= args_used;
  887. if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
  888. nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
  889. ti->error = "Invalid number of mirrors";
  890. dm_dirty_log_destroy(dl);
  891. return -EINVAL;
  892. }
  893. argv++, argc--;
  894. if (argc < nr_mirrors * 2) {
  895. ti->error = "Too few mirror arguments";
  896. dm_dirty_log_destroy(dl);
  897. return -EINVAL;
  898. }
  899. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  900. if (!ms) {
  901. dm_dirty_log_destroy(dl);
  902. return -ENOMEM;
  903. }
  904. /* Get the mirror parameter sets */
  905. for (m = 0; m < nr_mirrors; m++) {
  906. r = get_mirror(ms, ti, m, argv);
  907. if (r) {
  908. free_context(ms, ti, m);
  909. return r;
  910. }
  911. argv += 2;
  912. argc -= 2;
  913. }
  914. ti->private = ms;
  915. ti->split_io = dm_rh_get_region_size(ms->rh);
  916. ti->num_flush_requests = 1;
  917. ti->num_discard_requests = 1;
  918. ti->discard_zeroes_data_unsupported = 1;
  919. ms->kmirrord_wq = alloc_workqueue("kmirrord",
  920. WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
  921. if (!ms->kmirrord_wq) {
  922. DMERR("couldn't start kmirrord");
  923. r = -ENOMEM;
  924. goto err_free_context;
  925. }
  926. INIT_WORK(&ms->kmirrord_work, do_mirror);
  927. init_timer(&ms->timer);
  928. ms->timer_pending = 0;
  929. INIT_WORK(&ms->trigger_event, trigger_event);
  930. r = parse_features(ms, argc, argv, &args_used);
  931. if (r)
  932. goto err_destroy_wq;
  933. argv += args_used;
  934. argc -= args_used;
  935. /*
  936. * Any read-balancing addition depends on the
  937. * DM_RAID1_HANDLE_ERRORS flag being present.
  938. * This is because the decision to balance depends
  939. * on the sync state of a region. If the above
  940. * flag is not present, we ignore errors; and
  941. * the sync state may be inaccurate.
  942. */
  943. if (argc) {
  944. ti->error = "Too many mirror arguments";
  945. r = -EINVAL;
  946. goto err_destroy_wq;
  947. }
  948. ms->kcopyd_client = dm_kcopyd_client_create();
  949. if (IS_ERR(ms->kcopyd_client)) {
  950. r = PTR_ERR(ms->kcopyd_client);
  951. goto err_destroy_wq;
  952. }
  953. wakeup_mirrord(ms);
  954. return 0;
  955. err_destroy_wq:
  956. destroy_workqueue(ms->kmirrord_wq);
  957. err_free_context:
  958. free_context(ms, ti, ms->nr_mirrors);
  959. return r;
  960. }
  961. static void mirror_dtr(struct dm_target *ti)
  962. {
  963. struct mirror_set *ms = (struct mirror_set *) ti->private;
  964. del_timer_sync(&ms->timer);
  965. flush_workqueue(ms->kmirrord_wq);
  966. flush_work_sync(&ms->trigger_event);
  967. dm_kcopyd_client_destroy(ms->kcopyd_client);
  968. destroy_workqueue(ms->kmirrord_wq);
  969. free_context(ms, ti, ms->nr_mirrors);
  970. }
  971. /*
  972. * Mirror mapping function
  973. */
  974. static int mirror_map(struct dm_target *ti, struct bio *bio,
  975. union map_info *map_context)
  976. {
  977. int r, rw = bio_rw(bio);
  978. struct mirror *m;
  979. struct mirror_set *ms = ti->private;
  980. struct dm_raid1_read_record *read_record = NULL;
  981. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  982. if (rw == WRITE) {
  983. /* Save region for mirror_end_io() handler */
  984. map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
  985. queue_bio(ms, bio, rw);
  986. return DM_MAPIO_SUBMITTED;
  987. }
  988. r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
  989. if (r < 0 && r != -EWOULDBLOCK)
  990. return r;
  991. /*
  992. * If region is not in-sync queue the bio.
  993. */
  994. if (!r || (r == -EWOULDBLOCK)) {
  995. if (rw == READA)
  996. return -EWOULDBLOCK;
  997. queue_bio(ms, bio, rw);
  998. return DM_MAPIO_SUBMITTED;
  999. }
  1000. /*
  1001. * The region is in-sync and we can perform reads directly.
  1002. * Store enough information so we can retry if it fails.
  1003. */
  1004. m = choose_mirror(ms, bio->bi_sector);
  1005. if (unlikely(!m))
  1006. return -EIO;
  1007. read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
  1008. if (likely(read_record)) {
  1009. dm_bio_record(&read_record->details, bio);
  1010. map_context->ptr = read_record;
  1011. read_record->m = m;
  1012. }
  1013. map_bio(m, bio);
  1014. return DM_MAPIO_REMAPPED;
  1015. }
  1016. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  1017. int error, union map_info *map_context)
  1018. {
  1019. int rw = bio_rw(bio);
  1020. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1021. struct mirror *m = NULL;
  1022. struct dm_bio_details *bd = NULL;
  1023. struct dm_raid1_read_record *read_record = map_context->ptr;
  1024. /*
  1025. * We need to dec pending if this was a write.
  1026. */
  1027. if (rw == WRITE) {
  1028. if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
  1029. dm_rh_dec(ms->rh, map_context->ll);
  1030. return error;
  1031. }
  1032. if (error == -EOPNOTSUPP)
  1033. goto out;
  1034. if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
  1035. goto out;
  1036. if (unlikely(error)) {
  1037. if (!read_record) {
  1038. /*
  1039. * There wasn't enough memory to record necessary
  1040. * information for a retry or there was no other
  1041. * mirror in-sync.
  1042. */
  1043. DMERR_LIMIT("Mirror read failed.");
  1044. return -EIO;
  1045. }
  1046. m = read_record->m;
  1047. DMERR("Mirror read failed from %s. Trying alternative device.",
  1048. m->dev->name);
  1049. fail_mirror(m, DM_RAID1_READ_ERROR);
  1050. /*
  1051. * A failed read is requeued for another attempt using an intact
  1052. * mirror.
  1053. */
  1054. if (default_ok(m) || mirror_available(ms, bio)) {
  1055. bd = &read_record->details;
  1056. dm_bio_restore(bd, bio);
  1057. mempool_free(read_record, ms->read_record_pool);
  1058. map_context->ptr = NULL;
  1059. queue_bio(ms, bio, rw);
  1060. return 1;
  1061. }
  1062. DMERR("All replicated volumes dead, failing I/O");
  1063. }
  1064. out:
  1065. if (read_record) {
  1066. mempool_free(read_record, ms->read_record_pool);
  1067. map_context->ptr = NULL;
  1068. }
  1069. return error;
  1070. }
  1071. static void mirror_presuspend(struct dm_target *ti)
  1072. {
  1073. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1074. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1075. struct bio_list holds;
  1076. struct bio *bio;
  1077. atomic_set(&ms->suspend, 1);
  1078. /*
  1079. * Process bios in the hold list to start recovery waiting
  1080. * for bios in the hold list. After the process, no bio has
  1081. * a chance to be added in the hold list because ms->suspend
  1082. * is set.
  1083. */
  1084. spin_lock_irq(&ms->lock);
  1085. holds = ms->holds;
  1086. bio_list_init(&ms->holds);
  1087. spin_unlock_irq(&ms->lock);
  1088. while ((bio = bio_list_pop(&holds)))
  1089. hold_bio(ms, bio);
  1090. /*
  1091. * We must finish up all the work that we've
  1092. * generated (i.e. recovery work).
  1093. */
  1094. dm_rh_stop_recovery(ms->rh);
  1095. wait_event(_kmirrord_recovery_stopped,
  1096. !dm_rh_recovery_in_flight(ms->rh));
  1097. if (log->type->presuspend && log->type->presuspend(log))
  1098. /* FIXME: need better error handling */
  1099. DMWARN("log presuspend failed");
  1100. /*
  1101. * Now that recovery is complete/stopped and the
  1102. * delayed bios are queued, we need to wait for
  1103. * the worker thread to complete. This way,
  1104. * we know that all of our I/O has been pushed.
  1105. */
  1106. flush_workqueue(ms->kmirrord_wq);
  1107. }
  1108. static void mirror_postsuspend(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. if (log->type->postsuspend && log->type->postsuspend(log))
  1113. /* FIXME: need better error handling */
  1114. DMWARN("log postsuspend failed");
  1115. }
  1116. static void mirror_resume(struct dm_target *ti)
  1117. {
  1118. struct mirror_set *ms = ti->private;
  1119. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1120. atomic_set(&ms->suspend, 0);
  1121. if (log->type->resume && log->type->resume(log))
  1122. /* FIXME: need better error handling */
  1123. DMWARN("log resume failed");
  1124. dm_rh_start_recovery(ms->rh);
  1125. }
  1126. /*
  1127. * device_status_char
  1128. * @m: mirror device/leg we want the status of
  1129. *
  1130. * We return one character representing the most severe error
  1131. * we have encountered.
  1132. * A => Alive - No failures
  1133. * D => Dead - A write failure occurred leaving mirror out-of-sync
  1134. * S => Sync - A sychronization failure occurred, mirror out-of-sync
  1135. * R => Read - A read failure occurred, mirror data unaffected
  1136. *
  1137. * Returns: <char>
  1138. */
  1139. static char device_status_char(struct mirror *m)
  1140. {
  1141. if (!atomic_read(&(m->error_count)))
  1142. return 'A';
  1143. return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
  1144. (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
  1145. (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  1146. (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  1147. }
  1148. static void mirror_status(struct dm_target *ti, status_type_t type,
  1149. char *result, unsigned int maxlen)
  1150. {
  1151. unsigned int m, sz = 0;
  1152. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1153. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1154. char buffer[ms->nr_mirrors + 1];
  1155. switch (type) {
  1156. case STATUSTYPE_INFO:
  1157. DMEMIT("%d ", ms->nr_mirrors);
  1158. for (m = 0; m < ms->nr_mirrors; m++) {
  1159. DMEMIT("%s ", ms->mirror[m].dev->name);
  1160. buffer[m] = device_status_char(&(ms->mirror[m]));
  1161. }
  1162. buffer[m] = '\0';
  1163. DMEMIT("%llu/%llu 1 %s ",
  1164. (unsigned long long)log->type->get_sync_count(log),
  1165. (unsigned long long)ms->nr_regions, buffer);
  1166. sz += log->type->status(log, type, result+sz, maxlen-sz);
  1167. break;
  1168. case STATUSTYPE_TABLE:
  1169. sz = log->type->status(log, type, result, maxlen);
  1170. DMEMIT("%d", ms->nr_mirrors);
  1171. for (m = 0; m < ms->nr_mirrors; m++)
  1172. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1173. (unsigned long long)ms->mirror[m].offset);
  1174. if (ms->features & DM_RAID1_HANDLE_ERRORS)
  1175. DMEMIT(" 1 handle_errors");
  1176. }
  1177. }
  1178. static int mirror_iterate_devices(struct dm_target *ti,
  1179. iterate_devices_callout_fn fn, void *data)
  1180. {
  1181. struct mirror_set *ms = ti->private;
  1182. int ret = 0;
  1183. unsigned i;
  1184. for (i = 0; !ret && i < ms->nr_mirrors; i++)
  1185. ret = fn(ti, ms->mirror[i].dev,
  1186. ms->mirror[i].offset, ti->len, data);
  1187. return ret;
  1188. }
  1189. static struct target_type mirror_target = {
  1190. .name = "mirror",
  1191. .version = {1, 12, 1},
  1192. .module = THIS_MODULE,
  1193. .ctr = mirror_ctr,
  1194. .dtr = mirror_dtr,
  1195. .map = mirror_map,
  1196. .end_io = mirror_end_io,
  1197. .presuspend = mirror_presuspend,
  1198. .postsuspend = mirror_postsuspend,
  1199. .resume = mirror_resume,
  1200. .status = mirror_status,
  1201. .iterate_devices = mirror_iterate_devices,
  1202. };
  1203. static int __init dm_mirror_init(void)
  1204. {
  1205. int r;
  1206. _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
  1207. if (!_dm_raid1_read_record_cache) {
  1208. DMERR("Can't allocate dm_raid1_read_record cache");
  1209. r = -ENOMEM;
  1210. goto bad_cache;
  1211. }
  1212. r = dm_register_target(&mirror_target);
  1213. if (r < 0) {
  1214. DMERR("Failed to register mirror target");
  1215. goto bad_target;
  1216. }
  1217. return 0;
  1218. bad_target:
  1219. kmem_cache_destroy(_dm_raid1_read_record_cache);
  1220. bad_cache:
  1221. return r;
  1222. }
  1223. static void __exit dm_mirror_exit(void)
  1224. {
  1225. dm_unregister_target(&mirror_target);
  1226. kmem_cache_destroy(_dm_raid1_read_record_cache);
  1227. }
  1228. /* Module hooks */
  1229. module_init(dm_mirror_init);
  1230. module_exit(dm_mirror_exit);
  1231. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1232. MODULE_AUTHOR("Joe Thornber");
  1233. MODULE_LICENSE("GPL");