dm-raid1.c 34 KB

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