dm-raid1.c 35 KB

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