dm-raid1.c 34 KB

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