dm-raid1.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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. struct mirror_set *ms =
  742. kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
  743. if (!ms) {
  744. ti->error = "Cannot allocate mirror context";
  745. return NULL;
  746. }
  747. spin_lock_init(&ms->lock);
  748. bio_list_init(&ms->reads);
  749. bio_list_init(&ms->writes);
  750. bio_list_init(&ms->failures);
  751. bio_list_init(&ms->holds);
  752. ms->ti = ti;
  753. ms->nr_mirrors = nr_mirrors;
  754. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  755. ms->in_sync = 0;
  756. ms->log_failure = 0;
  757. ms->leg_failure = 0;
  758. atomic_set(&ms->suspend, 0);
  759. atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
  760. ms->io_client = dm_io_client_create();
  761. if (IS_ERR(ms->io_client)) {
  762. ti->error = "Error creating dm_io client";
  763. kfree(ms);
  764. return NULL;
  765. }
  766. ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
  767. wakeup_all_recovery_waiters,
  768. ms->ti->begin, MAX_RECOVERY,
  769. dl, region_size, ms->nr_regions);
  770. if (IS_ERR(ms->rh)) {
  771. ti->error = "Error creating dirty region hash";
  772. dm_io_client_destroy(ms->io_client);
  773. kfree(ms);
  774. return NULL;
  775. }
  776. return ms;
  777. }
  778. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  779. unsigned int m)
  780. {
  781. while (m--)
  782. dm_put_device(ti, ms->mirror[m].dev);
  783. dm_io_client_destroy(ms->io_client);
  784. dm_region_hash_destroy(ms->rh);
  785. kfree(ms);
  786. }
  787. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  788. unsigned int mirror, char **argv)
  789. {
  790. unsigned long long offset;
  791. char dummy;
  792. int ret;
  793. if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
  794. offset != (sector_t)offset) {
  795. ti->error = "Invalid offset";
  796. return -EINVAL;
  797. }
  798. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  799. &ms->mirror[mirror].dev);
  800. if (ret) {
  801. ti->error = "Device lookup failure";
  802. return ret;
  803. }
  804. ms->mirror[mirror].ms = ms;
  805. atomic_set(&(ms->mirror[mirror].error_count), 0);
  806. ms->mirror[mirror].error_type = 0;
  807. ms->mirror[mirror].offset = offset;
  808. return 0;
  809. }
  810. /*
  811. * Create dirty log: log_type #log_params <log_params>
  812. */
  813. static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
  814. unsigned argc, char **argv,
  815. unsigned *args_used)
  816. {
  817. unsigned param_count;
  818. struct dm_dirty_log *dl;
  819. char dummy;
  820. if (argc < 2) {
  821. ti->error = "Insufficient mirror log arguments";
  822. return NULL;
  823. }
  824. if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
  825. ti->error = "Invalid mirror log argument count";
  826. return NULL;
  827. }
  828. *args_used = 2 + param_count;
  829. if (argc < *args_used) {
  830. ti->error = "Insufficient mirror log arguments";
  831. return NULL;
  832. }
  833. dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
  834. argv + 2);
  835. if (!dl) {
  836. ti->error = "Error creating mirror dirty log";
  837. return NULL;
  838. }
  839. return dl;
  840. }
  841. static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
  842. unsigned *args_used)
  843. {
  844. unsigned num_features;
  845. struct dm_target *ti = ms->ti;
  846. char dummy;
  847. int i;
  848. *args_used = 0;
  849. if (!argc)
  850. return 0;
  851. if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
  852. ti->error = "Invalid number of features";
  853. return -EINVAL;
  854. }
  855. argc--;
  856. argv++;
  857. (*args_used)++;
  858. if (num_features > argc) {
  859. ti->error = "Not enough arguments to support feature count";
  860. return -EINVAL;
  861. }
  862. for (i = 0; i < num_features; i++) {
  863. if (!strcmp("handle_errors", argv[0]))
  864. ms->features |= DM_RAID1_HANDLE_ERRORS;
  865. else if (!strcmp("keep_log", argv[0]))
  866. ms->features |= DM_RAID1_KEEP_LOG;
  867. else {
  868. ti->error = "Unrecognised feature requested";
  869. return -EINVAL;
  870. }
  871. argc--;
  872. argv++;
  873. (*args_used)++;
  874. }
  875. if (!errors_handled(ms) && keep_log(ms)) {
  876. ti->error = "keep_log feature requires the handle_errors feature";
  877. return -EINVAL;
  878. }
  879. return 0;
  880. }
  881. /*
  882. * Construct a mirror mapping:
  883. *
  884. * log_type #log_params <log_params>
  885. * #mirrors [mirror_path offset]{2,}
  886. * [#features <features>]
  887. *
  888. * log_type is "core" or "disk"
  889. * #log_params is between 1 and 3
  890. *
  891. * If present, supported features are "handle_errors" and "keep_log".
  892. */
  893. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  894. {
  895. int r;
  896. unsigned int nr_mirrors, m, args_used;
  897. struct mirror_set *ms;
  898. struct dm_dirty_log *dl;
  899. char dummy;
  900. dl = create_dirty_log(ti, argc, argv, &args_used);
  901. if (!dl)
  902. return -EINVAL;
  903. argv += args_used;
  904. argc -= args_used;
  905. if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
  906. nr_mirrors < 2 || nr_mirrors > MAX_NR_MIRRORS) {
  907. ti->error = "Invalid number of mirrors";
  908. dm_dirty_log_destroy(dl);
  909. return -EINVAL;
  910. }
  911. argv++, argc--;
  912. if (argc < nr_mirrors * 2) {
  913. ti->error = "Too few mirror arguments";
  914. dm_dirty_log_destroy(dl);
  915. return -EINVAL;
  916. }
  917. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  918. if (!ms) {
  919. dm_dirty_log_destroy(dl);
  920. return -ENOMEM;
  921. }
  922. /* Get the mirror parameter sets */
  923. for (m = 0; m < nr_mirrors; m++) {
  924. r = get_mirror(ms, ti, m, argv);
  925. if (r) {
  926. free_context(ms, ti, m);
  927. return r;
  928. }
  929. argv += 2;
  930. argc -= 2;
  931. }
  932. ti->private = ms;
  933. r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
  934. if (r)
  935. goto err_free_context;
  936. ti->num_flush_bios = 1;
  937. ti->num_discard_bios = 1;
  938. ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
  939. ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
  940. if (!ms->kmirrord_wq) {
  941. DMERR("couldn't start kmirrord");
  942. r = -ENOMEM;
  943. goto err_free_context;
  944. }
  945. INIT_WORK(&ms->kmirrord_work, do_mirror);
  946. timer_setup(&ms->timer, delayed_wake_fn, 0);
  947. ms->timer_pending = 0;
  948. INIT_WORK(&ms->trigger_event, trigger_event);
  949. r = parse_features(ms, argc, argv, &args_used);
  950. if (r)
  951. goto err_destroy_wq;
  952. argv += args_used;
  953. argc -= args_used;
  954. /*
  955. * Any read-balancing addition depends on the
  956. * DM_RAID1_HANDLE_ERRORS flag being present.
  957. * This is because the decision to balance depends
  958. * on the sync state of a region. If the above
  959. * flag is not present, we ignore errors; and
  960. * the sync state may be inaccurate.
  961. */
  962. if (argc) {
  963. ti->error = "Too many mirror arguments";
  964. r = -EINVAL;
  965. goto err_destroy_wq;
  966. }
  967. ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  968. if (IS_ERR(ms->kcopyd_client)) {
  969. r = PTR_ERR(ms->kcopyd_client);
  970. goto err_destroy_wq;
  971. }
  972. wakeup_mirrord(ms);
  973. return 0;
  974. err_destroy_wq:
  975. destroy_workqueue(ms->kmirrord_wq);
  976. err_free_context:
  977. free_context(ms, ti, ms->nr_mirrors);
  978. return r;
  979. }
  980. static void mirror_dtr(struct dm_target *ti)
  981. {
  982. struct mirror_set *ms = (struct mirror_set *) ti->private;
  983. del_timer_sync(&ms->timer);
  984. flush_workqueue(ms->kmirrord_wq);
  985. flush_work(&ms->trigger_event);
  986. dm_kcopyd_client_destroy(ms->kcopyd_client);
  987. destroy_workqueue(ms->kmirrord_wq);
  988. free_context(ms, ti, ms->nr_mirrors);
  989. }
  990. /*
  991. * Mirror mapping function
  992. */
  993. static int mirror_map(struct dm_target *ti, struct bio *bio)
  994. {
  995. int r, rw = bio_data_dir(bio);
  996. struct mirror *m;
  997. struct mirror_set *ms = ti->private;
  998. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  999. struct dm_raid1_bio_record *bio_record =
  1000. dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
  1001. bio_record->details.bi_disk = NULL;
  1002. if (rw == WRITE) {
  1003. /* Save region for mirror_end_io() handler */
  1004. bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
  1005. queue_bio(ms, bio, rw);
  1006. return DM_MAPIO_SUBMITTED;
  1007. }
  1008. r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
  1009. if (r < 0 && r != -EWOULDBLOCK)
  1010. return DM_MAPIO_KILL;
  1011. /*
  1012. * If region is not in-sync queue the bio.
  1013. */
  1014. if (!r || (r == -EWOULDBLOCK)) {
  1015. if (bio->bi_opf & REQ_RAHEAD)
  1016. return DM_MAPIO_KILL;
  1017. queue_bio(ms, bio, rw);
  1018. return DM_MAPIO_SUBMITTED;
  1019. }
  1020. /*
  1021. * The region is in-sync and we can perform reads directly.
  1022. * Store enough information so we can retry if it fails.
  1023. */
  1024. m = choose_mirror(ms, bio->bi_iter.bi_sector);
  1025. if (unlikely(!m))
  1026. return DM_MAPIO_KILL;
  1027. dm_bio_record(&bio_record->details, bio);
  1028. bio_record->m = m;
  1029. map_bio(m, bio);
  1030. return DM_MAPIO_REMAPPED;
  1031. }
  1032. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  1033. blk_status_t *error)
  1034. {
  1035. int rw = bio_data_dir(bio);
  1036. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1037. struct mirror *m = NULL;
  1038. struct dm_bio_details *bd = NULL;
  1039. struct dm_raid1_bio_record *bio_record =
  1040. dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
  1041. /*
  1042. * We need to dec pending if this was a write.
  1043. */
  1044. if (rw == WRITE) {
  1045. if (!(bio->bi_opf & REQ_PREFLUSH) &&
  1046. bio_op(bio) != REQ_OP_DISCARD)
  1047. dm_rh_dec(ms->rh, bio_record->write_region);
  1048. return DM_ENDIO_DONE;
  1049. }
  1050. if (*error == BLK_STS_NOTSUPP)
  1051. goto out;
  1052. if (bio->bi_opf & REQ_RAHEAD)
  1053. goto out;
  1054. if (unlikely(*error)) {
  1055. if (!bio_record->details.bi_disk) {
  1056. /*
  1057. * There wasn't enough memory to record necessary
  1058. * information for a retry or there was no other
  1059. * mirror in-sync.
  1060. */
  1061. DMERR_LIMIT("Mirror read failed.");
  1062. return DM_ENDIO_DONE;
  1063. }
  1064. m = bio_record->m;
  1065. DMERR("Mirror read failed from %s. Trying alternative device.",
  1066. m->dev->name);
  1067. fail_mirror(m, DM_RAID1_READ_ERROR);
  1068. /*
  1069. * A failed read is requeued for another attempt using an intact
  1070. * mirror.
  1071. */
  1072. if (default_ok(m) || mirror_available(ms, bio)) {
  1073. bd = &bio_record->details;
  1074. dm_bio_restore(bd, bio);
  1075. bio_record->details.bi_disk = NULL;
  1076. bio->bi_status = 0;
  1077. queue_bio(ms, bio, rw);
  1078. return DM_ENDIO_INCOMPLETE;
  1079. }
  1080. DMERR("All replicated volumes dead, failing I/O");
  1081. }
  1082. out:
  1083. bio_record->details.bi_disk = NULL;
  1084. return DM_ENDIO_DONE;
  1085. }
  1086. static void mirror_presuspend(struct dm_target *ti)
  1087. {
  1088. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1089. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1090. struct bio_list holds;
  1091. struct bio *bio;
  1092. atomic_set(&ms->suspend, 1);
  1093. /*
  1094. * Process bios in the hold list to start recovery waiting
  1095. * for bios in the hold list. After the process, no bio has
  1096. * a chance to be added in the hold list because ms->suspend
  1097. * is set.
  1098. */
  1099. spin_lock_irq(&ms->lock);
  1100. holds = ms->holds;
  1101. bio_list_init(&ms->holds);
  1102. spin_unlock_irq(&ms->lock);
  1103. while ((bio = bio_list_pop(&holds)))
  1104. hold_bio(ms, bio);
  1105. /*
  1106. * We must finish up all the work that we've
  1107. * generated (i.e. recovery work).
  1108. */
  1109. dm_rh_stop_recovery(ms->rh);
  1110. wait_event(_kmirrord_recovery_stopped,
  1111. !dm_rh_recovery_in_flight(ms->rh));
  1112. if (log->type->presuspend && log->type->presuspend(log))
  1113. /* FIXME: need better error handling */
  1114. DMWARN("log presuspend failed");
  1115. /*
  1116. * Now that recovery is complete/stopped and the
  1117. * delayed bios are queued, we need to wait for
  1118. * the worker thread to complete. This way,
  1119. * we know that all of our I/O has been pushed.
  1120. */
  1121. flush_workqueue(ms->kmirrord_wq);
  1122. }
  1123. static void mirror_postsuspend(struct dm_target *ti)
  1124. {
  1125. struct mirror_set *ms = ti->private;
  1126. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1127. if (log->type->postsuspend && log->type->postsuspend(log))
  1128. /* FIXME: need better error handling */
  1129. DMWARN("log postsuspend failed");
  1130. }
  1131. static void mirror_resume(struct dm_target *ti)
  1132. {
  1133. struct mirror_set *ms = ti->private;
  1134. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1135. atomic_set(&ms->suspend, 0);
  1136. if (log->type->resume && log->type->resume(log))
  1137. /* FIXME: need better error handling */
  1138. DMWARN("log resume failed");
  1139. dm_rh_start_recovery(ms->rh);
  1140. }
  1141. /*
  1142. * device_status_char
  1143. * @m: mirror device/leg we want the status of
  1144. *
  1145. * We return one character representing the most severe error
  1146. * we have encountered.
  1147. * A => Alive - No failures
  1148. * D => Dead - A write failure occurred leaving mirror out-of-sync
  1149. * S => Sync - A sychronization failure occurred, mirror out-of-sync
  1150. * R => Read - A read failure occurred, mirror data unaffected
  1151. *
  1152. * Returns: <char>
  1153. */
  1154. static char device_status_char(struct mirror *m)
  1155. {
  1156. if (!atomic_read(&(m->error_count)))
  1157. return 'A';
  1158. return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
  1159. (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
  1160. (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  1161. (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  1162. }
  1163. static void mirror_status(struct dm_target *ti, status_type_t type,
  1164. unsigned status_flags, char *result, unsigned maxlen)
  1165. {
  1166. unsigned int m, sz = 0;
  1167. int num_feature_args = 0;
  1168. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1169. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1170. char buffer[MAX_NR_MIRRORS + 1];
  1171. switch (type) {
  1172. case STATUSTYPE_INFO:
  1173. DMEMIT("%d ", ms->nr_mirrors);
  1174. for (m = 0; m < ms->nr_mirrors; m++) {
  1175. DMEMIT("%s ", ms->mirror[m].dev->name);
  1176. buffer[m] = device_status_char(&(ms->mirror[m]));
  1177. }
  1178. buffer[m] = '\0';
  1179. DMEMIT("%llu/%llu 1 %s ",
  1180. (unsigned long long)log->type->get_sync_count(log),
  1181. (unsigned long long)ms->nr_regions, buffer);
  1182. sz += log->type->status(log, type, result+sz, maxlen-sz);
  1183. break;
  1184. case STATUSTYPE_TABLE:
  1185. sz = log->type->status(log, type, result, maxlen);
  1186. DMEMIT("%d", ms->nr_mirrors);
  1187. for (m = 0; m < ms->nr_mirrors; m++)
  1188. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1189. (unsigned long long)ms->mirror[m].offset);
  1190. num_feature_args += !!errors_handled(ms);
  1191. num_feature_args += !!keep_log(ms);
  1192. if (num_feature_args) {
  1193. DMEMIT(" %d", num_feature_args);
  1194. if (errors_handled(ms))
  1195. DMEMIT(" handle_errors");
  1196. if (keep_log(ms))
  1197. DMEMIT(" keep_log");
  1198. }
  1199. break;
  1200. }
  1201. }
  1202. static int mirror_iterate_devices(struct dm_target *ti,
  1203. iterate_devices_callout_fn fn, void *data)
  1204. {
  1205. struct mirror_set *ms = ti->private;
  1206. int ret = 0;
  1207. unsigned i;
  1208. for (i = 0; !ret && i < ms->nr_mirrors; i++)
  1209. ret = fn(ti, ms->mirror[i].dev,
  1210. ms->mirror[i].offset, ti->len, data);
  1211. return ret;
  1212. }
  1213. static struct target_type mirror_target = {
  1214. .name = "mirror",
  1215. .version = {1, 14, 0},
  1216. .module = THIS_MODULE,
  1217. .ctr = mirror_ctr,
  1218. .dtr = mirror_dtr,
  1219. .map = mirror_map,
  1220. .end_io = mirror_end_io,
  1221. .presuspend = mirror_presuspend,
  1222. .postsuspend = mirror_postsuspend,
  1223. .resume = mirror_resume,
  1224. .status = mirror_status,
  1225. .iterate_devices = mirror_iterate_devices,
  1226. };
  1227. static int __init dm_mirror_init(void)
  1228. {
  1229. int r;
  1230. r = dm_register_target(&mirror_target);
  1231. if (r < 0) {
  1232. DMERR("Failed to register mirror target");
  1233. goto bad_target;
  1234. }
  1235. return 0;
  1236. bad_target:
  1237. return r;
  1238. }
  1239. static void __exit dm_mirror_exit(void)
  1240. {
  1241. dm_unregister_target(&mirror_target);
  1242. }
  1243. /* Module hooks */
  1244. module_init(dm_mirror_init);
  1245. module_exit(dm_mirror_exit);
  1246. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1247. MODULE_AUTHOR("Joe Thornber");
  1248. MODULE_LICENSE("GPL");