dm-raid1.c 36 KB

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