dm-raid1.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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 = from_timer(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);
  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(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
  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", WQ_MEM_RECLAIM, 0);
  953. if (!ms->kmirrord_wq) {
  954. DMERR("couldn't start kmirrord");
  955. r = -ENOMEM;
  956. goto err_free_context;
  957. }
  958. INIT_WORK(&ms->kmirrord_work, do_mirror);
  959. timer_setup(&ms->timer, delayed_wake_fn, 0);
  960. ms->timer_pending = 0;
  961. INIT_WORK(&ms->trigger_event, trigger_event);
  962. r = parse_features(ms, argc, argv, &args_used);
  963. if (r)
  964. goto err_destroy_wq;
  965. argv += args_used;
  966. argc -= args_used;
  967. /*
  968. * Any read-balancing addition depends on the
  969. * DM_RAID1_HANDLE_ERRORS flag being present.
  970. * This is because the decision to balance depends
  971. * on the sync state of a region. If the above
  972. * flag is not present, we ignore errors; and
  973. * the sync state may be inaccurate.
  974. */
  975. if (argc) {
  976. ti->error = "Too many mirror arguments";
  977. r = -EINVAL;
  978. goto err_destroy_wq;
  979. }
  980. ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  981. if (IS_ERR(ms->kcopyd_client)) {
  982. r = PTR_ERR(ms->kcopyd_client);
  983. goto err_destroy_wq;
  984. }
  985. wakeup_mirrord(ms);
  986. return 0;
  987. err_destroy_wq:
  988. destroy_workqueue(ms->kmirrord_wq);
  989. err_free_context:
  990. free_context(ms, ti, ms->nr_mirrors);
  991. return r;
  992. }
  993. static void mirror_dtr(struct dm_target *ti)
  994. {
  995. struct mirror_set *ms = ti->private;
  996. del_timer_sync(&ms->timer);
  997. flush_workqueue(ms->kmirrord_wq);
  998. flush_work(&ms->trigger_event);
  999. dm_kcopyd_client_destroy(ms->kcopyd_client);
  1000. destroy_workqueue(ms->kmirrord_wq);
  1001. free_context(ms, ti, ms->nr_mirrors);
  1002. }
  1003. /*
  1004. * Mirror mapping function
  1005. */
  1006. static int mirror_map(struct dm_target *ti, struct bio *bio)
  1007. {
  1008. int r, rw = bio_data_dir(bio);
  1009. struct mirror *m;
  1010. struct mirror_set *ms = ti->private;
  1011. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1012. struct dm_raid1_bio_record *bio_record =
  1013. dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
  1014. bio_record->details.bi_bdev = NULL;
  1015. if (rw == WRITE) {
  1016. /* Save region for mirror_end_io() handler */
  1017. bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
  1018. queue_bio(ms, bio, rw);
  1019. return DM_MAPIO_SUBMITTED;
  1020. }
  1021. r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
  1022. if (r < 0 && r != -EWOULDBLOCK)
  1023. return DM_MAPIO_KILL;
  1024. /*
  1025. * If region is not in-sync queue the bio.
  1026. */
  1027. if (!r || (r == -EWOULDBLOCK)) {
  1028. if (bio->bi_opf & REQ_RAHEAD)
  1029. return DM_MAPIO_KILL;
  1030. queue_bio(ms, bio, rw);
  1031. return DM_MAPIO_SUBMITTED;
  1032. }
  1033. /*
  1034. * The region is in-sync and we can perform reads directly.
  1035. * Store enough information so we can retry if it fails.
  1036. */
  1037. m = choose_mirror(ms, bio->bi_iter.bi_sector);
  1038. if (unlikely(!m))
  1039. return DM_MAPIO_KILL;
  1040. dm_bio_record(&bio_record->details, bio);
  1041. bio_record->m = m;
  1042. map_bio(m, bio);
  1043. return DM_MAPIO_REMAPPED;
  1044. }
  1045. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  1046. blk_status_t *error)
  1047. {
  1048. int rw = bio_data_dir(bio);
  1049. struct mirror_set *ms = ti->private;
  1050. struct mirror *m = NULL;
  1051. struct dm_bio_details *bd = NULL;
  1052. struct dm_raid1_bio_record *bio_record =
  1053. dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
  1054. /*
  1055. * We need to dec pending if this was a write.
  1056. */
  1057. if (rw == WRITE) {
  1058. if (!(bio->bi_opf & REQ_PREFLUSH) &&
  1059. bio_op(bio) != REQ_OP_DISCARD)
  1060. dm_rh_dec(ms->rh, bio_record->write_region);
  1061. return DM_ENDIO_DONE;
  1062. }
  1063. if (*error == BLK_STS_NOTSUPP)
  1064. goto out;
  1065. if (bio->bi_opf & REQ_RAHEAD)
  1066. goto out;
  1067. if (unlikely(*error)) {
  1068. if (!bio_record->details.bi_bdev) {
  1069. /*
  1070. * There wasn't enough memory to record necessary
  1071. * information for a retry or there was no other
  1072. * mirror in-sync.
  1073. */
  1074. DMERR_LIMIT("Mirror read failed.");
  1075. return DM_ENDIO_DONE;
  1076. }
  1077. m = bio_record->m;
  1078. DMERR("Mirror read failed from %s. Trying alternative device.",
  1079. m->dev->name);
  1080. fail_mirror(m, DM_RAID1_READ_ERROR);
  1081. /*
  1082. * A failed read is requeued for another attempt using an intact
  1083. * mirror.
  1084. */
  1085. if (default_ok(m) || mirror_available(ms, bio)) {
  1086. bd = &bio_record->details;
  1087. dm_bio_restore(bd, bio);
  1088. bio_record->details.bi_bdev = NULL;
  1089. bio->bi_status = 0;
  1090. queue_bio(ms, bio, rw);
  1091. return DM_ENDIO_INCOMPLETE;
  1092. }
  1093. DMERR("All replicated volumes dead, failing I/O");
  1094. }
  1095. out:
  1096. bio_record->details.bi_bdev = NULL;
  1097. return DM_ENDIO_DONE;
  1098. }
  1099. static void mirror_presuspend(struct dm_target *ti)
  1100. {
  1101. struct mirror_set *ms = ti->private;
  1102. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1103. struct bio_list holds;
  1104. struct bio *bio;
  1105. atomic_set(&ms->suspend, 1);
  1106. /*
  1107. * Process bios in the hold list to start recovery waiting
  1108. * for bios in the hold list. After the process, no bio has
  1109. * a chance to be added in the hold list because ms->suspend
  1110. * is set.
  1111. */
  1112. spin_lock_irq(&ms->lock);
  1113. holds = ms->holds;
  1114. bio_list_init(&ms->holds);
  1115. spin_unlock_irq(&ms->lock);
  1116. while ((bio = bio_list_pop(&holds)))
  1117. hold_bio(ms, bio);
  1118. /*
  1119. * We must finish up all the work that we've
  1120. * generated (i.e. recovery work).
  1121. */
  1122. dm_rh_stop_recovery(ms->rh);
  1123. wait_event(_kmirrord_recovery_stopped,
  1124. !dm_rh_recovery_in_flight(ms->rh));
  1125. if (log->type->presuspend && log->type->presuspend(log))
  1126. /* FIXME: need better error handling */
  1127. DMWARN("log presuspend failed");
  1128. /*
  1129. * Now that recovery is complete/stopped and the
  1130. * delayed bios are queued, we need to wait for
  1131. * the worker thread to complete. This way,
  1132. * we know that all of our I/O has been pushed.
  1133. */
  1134. flush_workqueue(ms->kmirrord_wq);
  1135. }
  1136. static void mirror_postsuspend(struct dm_target *ti)
  1137. {
  1138. struct mirror_set *ms = ti->private;
  1139. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1140. if (log->type->postsuspend && log->type->postsuspend(log))
  1141. /* FIXME: need better error handling */
  1142. DMWARN("log postsuspend failed");
  1143. }
  1144. static void mirror_resume(struct dm_target *ti)
  1145. {
  1146. struct mirror_set *ms = ti->private;
  1147. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1148. atomic_set(&ms->suspend, 0);
  1149. if (log->type->resume && log->type->resume(log))
  1150. /* FIXME: need better error handling */
  1151. DMWARN("log resume failed");
  1152. dm_rh_start_recovery(ms->rh);
  1153. }
  1154. /*
  1155. * device_status_char
  1156. * @m: mirror device/leg we want the status of
  1157. *
  1158. * We return one character representing the most severe error
  1159. * we have encountered.
  1160. * A => Alive - No failures
  1161. * D => Dead - A write failure occurred leaving mirror out-of-sync
  1162. * S => Sync - A sychronization failure occurred, mirror out-of-sync
  1163. * R => Read - A read failure occurred, mirror data unaffected
  1164. *
  1165. * Returns: <char>
  1166. */
  1167. static char device_status_char(struct mirror *m)
  1168. {
  1169. if (!atomic_read(&(m->error_count)))
  1170. return 'A';
  1171. return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
  1172. (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
  1173. (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  1174. (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  1175. }
  1176. static void mirror_status(struct dm_target *ti, status_type_t type,
  1177. unsigned int status_flags, char *result, unsigned int maxlen)
  1178. {
  1179. unsigned int m, sz = 0;
  1180. int num_feature_args = 0;
  1181. struct mirror_set *ms = ti->private;
  1182. struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  1183. char buffer[MAX_NR_MIRRORS + 1];
  1184. switch (type) {
  1185. case STATUSTYPE_INFO:
  1186. DMEMIT("%d ", ms->nr_mirrors);
  1187. for (m = 0; m < ms->nr_mirrors; m++) {
  1188. DMEMIT("%s ", ms->mirror[m].dev->name);
  1189. buffer[m] = device_status_char(&(ms->mirror[m]));
  1190. }
  1191. buffer[m] = '\0';
  1192. DMEMIT("%llu/%llu 1 %s ",
  1193. (unsigned long long)log->type->get_sync_count(log),
  1194. (unsigned long long)ms->nr_regions, buffer);
  1195. sz += log->type->status(log, type, result+sz, maxlen-sz);
  1196. break;
  1197. case STATUSTYPE_TABLE:
  1198. sz = log->type->status(log, type, result, maxlen);
  1199. DMEMIT("%d", ms->nr_mirrors);
  1200. for (m = 0; m < ms->nr_mirrors; m++)
  1201. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1202. (unsigned long long)ms->mirror[m].offset);
  1203. num_feature_args += !!errors_handled(ms);
  1204. num_feature_args += !!keep_log(ms);
  1205. if (num_feature_args) {
  1206. DMEMIT(" %d", num_feature_args);
  1207. if (errors_handled(ms))
  1208. DMEMIT(" handle_errors");
  1209. if (keep_log(ms))
  1210. DMEMIT(" keep_log");
  1211. }
  1212. break;
  1213. case STATUSTYPE_IMA:
  1214. DMEMIT_TARGET_NAME_VERSION(ti->type);
  1215. DMEMIT(",nr_mirrors=%d", ms->nr_mirrors);
  1216. for (m = 0; m < ms->nr_mirrors; m++) {
  1217. DMEMIT(",mirror_device_%d=%s", m, ms->mirror[m].dev->name);
  1218. DMEMIT(",mirror_device_%d_status=%c",
  1219. m, device_status_char(&(ms->mirror[m])));
  1220. }
  1221. DMEMIT(",handle_errors=%c", errors_handled(ms) ? 'y' : 'n');
  1222. DMEMIT(",keep_log=%c", keep_log(ms) ? 'y' : 'n');
  1223. DMEMIT(",log_type_status=");
  1224. sz += log->type->status(log, type, result+sz, maxlen-sz);
  1225. DMEMIT(";");
  1226. break;
  1227. }
  1228. }
  1229. static int mirror_iterate_devices(struct dm_target *ti,
  1230. iterate_devices_callout_fn fn, void *data)
  1231. {
  1232. struct mirror_set *ms = ti->private;
  1233. int ret = 0;
  1234. unsigned int i;
  1235. for (i = 0; !ret && i < ms->nr_mirrors; i++)
  1236. ret = fn(ti, ms->mirror[i].dev,
  1237. ms->mirror[i].offset, ti->len, data);
  1238. return ret;
  1239. }
  1240. static struct target_type mirror_target = {
  1241. .name = "mirror",
  1242. .version = {1, 14, 0},
  1243. .module = THIS_MODULE,
  1244. .ctr = mirror_ctr,
  1245. .dtr = mirror_dtr,
  1246. .map = mirror_map,
  1247. .end_io = mirror_end_io,
  1248. .presuspend = mirror_presuspend,
  1249. .postsuspend = mirror_postsuspend,
  1250. .resume = mirror_resume,
  1251. .status = mirror_status,
  1252. .iterate_devices = mirror_iterate_devices,
  1253. };
  1254. static int __init dm_mirror_init(void)
  1255. {
  1256. int r;
  1257. dm_raid1_wq = alloc_workqueue("dm_raid1_wq", 0, 0);
  1258. if (!dm_raid1_wq) {
  1259. DMERR("Failed to alloc workqueue");
  1260. return -ENOMEM;
  1261. }
  1262. r = dm_register_target(&mirror_target);
  1263. if (r < 0) {
  1264. destroy_workqueue(dm_raid1_wq);
  1265. return r;
  1266. }
  1267. return 0;
  1268. }
  1269. static void __exit dm_mirror_exit(void)
  1270. {
  1271. destroy_workqueue(dm_raid1_wq);
  1272. dm_unregister_target(&mirror_target);
  1273. }
  1274. /* Module hooks */
  1275. module_init(dm_mirror_init);
  1276. module_exit(dm_mirror_exit);
  1277. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1278. MODULE_AUTHOR("Joe Thornber");
  1279. MODULE_LICENSE("GPL");