dm-raid1.c 31 KB

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