dm-raid1.c 35 KB

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