dm-raid1.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm.h"
  7. #include "dm-bio-list.h"
  8. #include "dm-io.h"
  9. #include "dm-log.h"
  10. #include "kcopyd.h"
  11. #include <linux/ctype.h>
  12. #include <linux/init.h>
  13. #include <linux/mempool.h>
  14. #include <linux/module.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/slab.h>
  17. #include <linux/time.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/workqueue.h>
  20. static struct workqueue_struct *_kmirrord_wq;
  21. static struct work_struct _kmirrord_work;
  22. static inline void wake(void)
  23. {
  24. queue_work(_kmirrord_wq, &_kmirrord_work);
  25. }
  26. /*-----------------------------------------------------------------
  27. * Region hash
  28. *
  29. * The mirror splits itself up into discrete regions. Each
  30. * region can be in one of three states: clean, dirty,
  31. * nosync. There is no need to put clean regions in the hash.
  32. *
  33. * In addition to being present in the hash table a region _may_
  34. * be present on one of three lists.
  35. *
  36. * clean_regions: Regions on this list have no io pending to
  37. * them, they are in sync, we are no longer interested in them,
  38. * they are dull. rh_update_states() will remove them from the
  39. * hash table.
  40. *
  41. * quiesced_regions: These regions have been spun down, ready
  42. * for recovery. rh_recovery_start() will remove regions from
  43. * this list and hand them to kmirrord, which will schedule the
  44. * recovery io with kcopyd.
  45. *
  46. * recovered_regions: Regions that kcopyd has successfully
  47. * recovered. rh_update_states() will now schedule any delayed
  48. * io, up the recovery_count, and remove the region from the
  49. * hash.
  50. *
  51. * There are 2 locks:
  52. * A rw spin lock 'hash_lock' protects just the hash table,
  53. * this is never held in write mode from interrupt context,
  54. * which I believe means that we only have to disable irqs when
  55. * doing a write lock.
  56. *
  57. * An ordinary spin lock 'region_lock' that protects the three
  58. * lists in the region_hash, with the 'state', 'list' and
  59. * 'bhs_delayed' fields of the regions. This is used from irq
  60. * context, so all other uses will have to suspend local irqs.
  61. *---------------------------------------------------------------*/
  62. struct mirror_set;
  63. struct region_hash {
  64. struct mirror_set *ms;
  65. uint32_t region_size;
  66. unsigned region_shift;
  67. /* holds persistent region state */
  68. struct dirty_log *log;
  69. /* hash table */
  70. rwlock_t hash_lock;
  71. mempool_t *region_pool;
  72. unsigned int mask;
  73. unsigned int nr_buckets;
  74. struct list_head *buckets;
  75. spinlock_t region_lock;
  76. struct semaphore recovery_count;
  77. struct list_head clean_regions;
  78. struct list_head quiesced_regions;
  79. struct list_head recovered_regions;
  80. };
  81. enum {
  82. RH_CLEAN,
  83. RH_DIRTY,
  84. RH_NOSYNC,
  85. RH_RECOVERING
  86. };
  87. struct region {
  88. struct region_hash *rh; /* FIXME: can we get rid of this ? */
  89. region_t key;
  90. int state;
  91. struct list_head hash_list;
  92. struct list_head list;
  93. atomic_t pending;
  94. struct bio_list delayed_bios;
  95. };
  96. /*
  97. * Conversion fns
  98. */
  99. static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
  100. {
  101. return bio->bi_sector >> rh->region_shift;
  102. }
  103. static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
  104. {
  105. return region << rh->region_shift;
  106. }
  107. /* FIXME move this */
  108. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
  109. #define MIN_REGIONS 64
  110. #define MAX_RECOVERY 1
  111. static int rh_init(struct region_hash *rh, struct mirror_set *ms,
  112. struct dirty_log *log, uint32_t region_size,
  113. region_t nr_regions)
  114. {
  115. unsigned int nr_buckets, max_buckets;
  116. size_t i;
  117. /*
  118. * Calculate a suitable number of buckets for our hash
  119. * table.
  120. */
  121. max_buckets = nr_regions >> 6;
  122. for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
  123. ;
  124. nr_buckets >>= 1;
  125. rh->ms = ms;
  126. rh->log = log;
  127. rh->region_size = region_size;
  128. rh->region_shift = ffs(region_size) - 1;
  129. rwlock_init(&rh->hash_lock);
  130. rh->mask = nr_buckets - 1;
  131. rh->nr_buckets = nr_buckets;
  132. rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
  133. if (!rh->buckets) {
  134. DMERR("unable to allocate region hash memory");
  135. return -ENOMEM;
  136. }
  137. for (i = 0; i < nr_buckets; i++)
  138. INIT_LIST_HEAD(rh->buckets + i);
  139. spin_lock_init(&rh->region_lock);
  140. sema_init(&rh->recovery_count, 0);
  141. INIT_LIST_HEAD(&rh->clean_regions);
  142. INIT_LIST_HEAD(&rh->quiesced_regions);
  143. INIT_LIST_HEAD(&rh->recovered_regions);
  144. rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
  145. sizeof(struct region));
  146. if (!rh->region_pool) {
  147. vfree(rh->buckets);
  148. rh->buckets = NULL;
  149. return -ENOMEM;
  150. }
  151. return 0;
  152. }
  153. static void rh_exit(struct region_hash *rh)
  154. {
  155. unsigned int h;
  156. struct region *reg, *nreg;
  157. BUG_ON(!list_empty(&rh->quiesced_regions));
  158. for (h = 0; h < rh->nr_buckets; h++) {
  159. list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
  160. BUG_ON(atomic_read(&reg->pending));
  161. mempool_free(reg, rh->region_pool);
  162. }
  163. }
  164. if (rh->log)
  165. dm_destroy_dirty_log(rh->log);
  166. if (rh->region_pool)
  167. mempool_destroy(rh->region_pool);
  168. vfree(rh->buckets);
  169. }
  170. #define RH_HASH_MULT 2654435387U
  171. static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
  172. {
  173. return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
  174. }
  175. static struct region *__rh_lookup(struct region_hash *rh, region_t region)
  176. {
  177. struct region *reg;
  178. list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
  179. if (reg->key == region)
  180. return reg;
  181. return NULL;
  182. }
  183. static void __rh_insert(struct region_hash *rh, struct region *reg)
  184. {
  185. unsigned int h = rh_hash(rh, reg->key);
  186. list_add(&reg->hash_list, rh->buckets + h);
  187. }
  188. static struct region *__rh_alloc(struct region_hash *rh, region_t region)
  189. {
  190. struct region *reg, *nreg;
  191. read_unlock(&rh->hash_lock);
  192. nreg = mempool_alloc(rh->region_pool, GFP_NOIO);
  193. nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
  194. RH_CLEAN : RH_NOSYNC;
  195. nreg->rh = rh;
  196. nreg->key = region;
  197. INIT_LIST_HEAD(&nreg->list);
  198. atomic_set(&nreg->pending, 0);
  199. bio_list_init(&nreg->delayed_bios);
  200. write_lock_irq(&rh->hash_lock);
  201. reg = __rh_lookup(rh, region);
  202. if (reg)
  203. /* we lost the race */
  204. mempool_free(nreg, rh->region_pool);
  205. else {
  206. __rh_insert(rh, nreg);
  207. if (nreg->state == RH_CLEAN) {
  208. spin_lock(&rh->region_lock);
  209. list_add(&nreg->list, &rh->clean_regions);
  210. spin_unlock(&rh->region_lock);
  211. }
  212. reg = nreg;
  213. }
  214. write_unlock_irq(&rh->hash_lock);
  215. read_lock(&rh->hash_lock);
  216. return reg;
  217. }
  218. static inline struct region *__rh_find(struct region_hash *rh, region_t region)
  219. {
  220. struct region *reg;
  221. reg = __rh_lookup(rh, region);
  222. if (!reg)
  223. reg = __rh_alloc(rh, region);
  224. return reg;
  225. }
  226. static int rh_state(struct region_hash *rh, region_t region, int may_block)
  227. {
  228. int r;
  229. struct region *reg;
  230. read_lock(&rh->hash_lock);
  231. reg = __rh_lookup(rh, region);
  232. read_unlock(&rh->hash_lock);
  233. if (reg)
  234. return reg->state;
  235. /*
  236. * The region wasn't in the hash, so we fall back to the
  237. * dirty log.
  238. */
  239. r = rh->log->type->in_sync(rh->log, region, may_block);
  240. /*
  241. * Any error from the dirty log (eg. -EWOULDBLOCK) gets
  242. * taken as a RH_NOSYNC
  243. */
  244. return r == 1 ? RH_CLEAN : RH_NOSYNC;
  245. }
  246. static inline int rh_in_sync(struct region_hash *rh,
  247. region_t region, int may_block)
  248. {
  249. int state = rh_state(rh, region, may_block);
  250. return state == RH_CLEAN || state == RH_DIRTY;
  251. }
  252. static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
  253. {
  254. struct bio *bio;
  255. while ((bio = bio_list_pop(bio_list))) {
  256. queue_bio(ms, bio, WRITE);
  257. }
  258. }
  259. static void rh_update_states(struct region_hash *rh)
  260. {
  261. struct region *reg, *next;
  262. LIST_HEAD(clean);
  263. LIST_HEAD(recovered);
  264. /*
  265. * Quickly grab the lists.
  266. */
  267. write_lock_irq(&rh->hash_lock);
  268. spin_lock(&rh->region_lock);
  269. if (!list_empty(&rh->clean_regions)) {
  270. list_splice(&rh->clean_regions, &clean);
  271. INIT_LIST_HEAD(&rh->clean_regions);
  272. list_for_each_entry (reg, &clean, list) {
  273. rh->log->type->clear_region(rh->log, reg->key);
  274. list_del(&reg->hash_list);
  275. }
  276. }
  277. if (!list_empty(&rh->recovered_regions)) {
  278. list_splice(&rh->recovered_regions, &recovered);
  279. INIT_LIST_HEAD(&rh->recovered_regions);
  280. list_for_each_entry (reg, &recovered, list)
  281. list_del(&reg->hash_list);
  282. }
  283. spin_unlock(&rh->region_lock);
  284. write_unlock_irq(&rh->hash_lock);
  285. /*
  286. * All the regions on the recovered and clean lists have
  287. * now been pulled out of the system, so no need to do
  288. * any more locking.
  289. */
  290. list_for_each_entry_safe (reg, next, &recovered, list) {
  291. rh->log->type->clear_region(rh->log, reg->key);
  292. rh->log->type->complete_resync_work(rh->log, reg->key, 1);
  293. dispatch_bios(rh->ms, &reg->delayed_bios);
  294. up(&rh->recovery_count);
  295. mempool_free(reg, rh->region_pool);
  296. }
  297. if (!list_empty(&recovered))
  298. rh->log->type->flush(rh->log);
  299. list_for_each_entry_safe (reg, next, &clean, list)
  300. mempool_free(reg, rh->region_pool);
  301. }
  302. static void rh_inc(struct region_hash *rh, region_t region)
  303. {
  304. struct region *reg;
  305. read_lock(&rh->hash_lock);
  306. reg = __rh_find(rh, region);
  307. spin_lock_irq(&rh->region_lock);
  308. atomic_inc(&reg->pending);
  309. if (reg->state == RH_CLEAN) {
  310. reg->state = RH_DIRTY;
  311. list_del_init(&reg->list); /* take off the clean list */
  312. spin_unlock_irq(&rh->region_lock);
  313. rh->log->type->mark_region(rh->log, reg->key);
  314. } else
  315. spin_unlock_irq(&rh->region_lock);
  316. read_unlock(&rh->hash_lock);
  317. }
  318. static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
  319. {
  320. struct bio *bio;
  321. for (bio = bios->head; bio; bio = bio->bi_next)
  322. rh_inc(rh, bio_to_region(rh, bio));
  323. }
  324. static void rh_dec(struct region_hash *rh, region_t region)
  325. {
  326. unsigned long flags;
  327. struct region *reg;
  328. int should_wake = 0;
  329. read_lock(&rh->hash_lock);
  330. reg = __rh_lookup(rh, region);
  331. read_unlock(&rh->hash_lock);
  332. spin_lock_irqsave(&rh->region_lock, flags);
  333. if (atomic_dec_and_test(&reg->pending)) {
  334. /*
  335. * There is no pending I/O for this region.
  336. * We can move the region to corresponding list for next action.
  337. * At this point, the region is not yet connected to any list.
  338. *
  339. * If the state is RH_NOSYNC, the region should be kept off
  340. * from clean list.
  341. * The hash entry for RH_NOSYNC will remain in memory
  342. * until the region is recovered or the map is reloaded.
  343. */
  344. /* do nothing for RH_NOSYNC */
  345. if (reg->state == RH_RECOVERING) {
  346. list_add_tail(&reg->list, &rh->quiesced_regions);
  347. } else if (reg->state == RH_DIRTY) {
  348. reg->state = RH_CLEAN;
  349. list_add(&reg->list, &rh->clean_regions);
  350. }
  351. should_wake = 1;
  352. }
  353. spin_unlock_irqrestore(&rh->region_lock, flags);
  354. if (should_wake)
  355. wake();
  356. }
  357. /*
  358. * Starts quiescing a region in preparation for recovery.
  359. */
  360. static int __rh_recovery_prepare(struct region_hash *rh)
  361. {
  362. int r;
  363. struct region *reg;
  364. region_t region;
  365. /*
  366. * Ask the dirty log what's next.
  367. */
  368. r = rh->log->type->get_resync_work(rh->log, &region);
  369. if (r <= 0)
  370. return r;
  371. /*
  372. * Get this region, and start it quiescing by setting the
  373. * recovering flag.
  374. */
  375. read_lock(&rh->hash_lock);
  376. reg = __rh_find(rh, region);
  377. read_unlock(&rh->hash_lock);
  378. spin_lock_irq(&rh->region_lock);
  379. reg->state = RH_RECOVERING;
  380. /* Already quiesced ? */
  381. if (atomic_read(&reg->pending))
  382. list_del_init(&reg->list);
  383. else {
  384. list_del_init(&reg->list);
  385. list_add(&reg->list, &rh->quiesced_regions);
  386. }
  387. spin_unlock_irq(&rh->region_lock);
  388. return 1;
  389. }
  390. static void rh_recovery_prepare(struct region_hash *rh)
  391. {
  392. while (!down_trylock(&rh->recovery_count))
  393. if (__rh_recovery_prepare(rh) <= 0) {
  394. up(&rh->recovery_count);
  395. break;
  396. }
  397. }
  398. /*
  399. * Returns any quiesced regions.
  400. */
  401. static struct region *rh_recovery_start(struct region_hash *rh)
  402. {
  403. struct region *reg = NULL;
  404. spin_lock_irq(&rh->region_lock);
  405. if (!list_empty(&rh->quiesced_regions)) {
  406. reg = list_entry(rh->quiesced_regions.next,
  407. struct region, list);
  408. list_del_init(&reg->list); /* remove from the quiesced list */
  409. }
  410. spin_unlock_irq(&rh->region_lock);
  411. return reg;
  412. }
  413. /* FIXME: success ignored for now */
  414. static void rh_recovery_end(struct region *reg, int success)
  415. {
  416. struct region_hash *rh = reg->rh;
  417. spin_lock_irq(&rh->region_lock);
  418. list_add(&reg->list, &reg->rh->recovered_regions);
  419. spin_unlock_irq(&rh->region_lock);
  420. wake();
  421. }
  422. static void rh_flush(struct region_hash *rh)
  423. {
  424. rh->log->type->flush(rh->log);
  425. }
  426. static void rh_delay(struct region_hash *rh, struct bio *bio)
  427. {
  428. struct region *reg;
  429. read_lock(&rh->hash_lock);
  430. reg = __rh_find(rh, bio_to_region(rh, bio));
  431. bio_list_add(&reg->delayed_bios, bio);
  432. read_unlock(&rh->hash_lock);
  433. }
  434. static void rh_stop_recovery(struct region_hash *rh)
  435. {
  436. int i;
  437. /* wait for any recovering regions */
  438. for (i = 0; i < MAX_RECOVERY; i++)
  439. down(&rh->recovery_count);
  440. }
  441. static void rh_start_recovery(struct region_hash *rh)
  442. {
  443. int i;
  444. for (i = 0; i < MAX_RECOVERY; i++)
  445. up(&rh->recovery_count);
  446. wake();
  447. }
  448. /*-----------------------------------------------------------------
  449. * Mirror set structures.
  450. *---------------------------------------------------------------*/
  451. struct mirror {
  452. atomic_t error_count;
  453. struct dm_dev *dev;
  454. sector_t offset;
  455. };
  456. struct mirror_set {
  457. struct dm_target *ti;
  458. struct list_head list;
  459. struct region_hash rh;
  460. struct kcopyd_client *kcopyd_client;
  461. spinlock_t lock; /* protects the next two lists */
  462. struct bio_list reads;
  463. struct bio_list writes;
  464. /* recovery */
  465. region_t nr_regions;
  466. int in_sync;
  467. struct mirror *default_mirror; /* Default mirror */
  468. unsigned int nr_mirrors;
  469. struct mirror mirror[0];
  470. };
  471. /*
  472. * Every mirror should look like this one.
  473. */
  474. #define DEFAULT_MIRROR 0
  475. /*
  476. * This is yucky. We squirrel the mirror_set struct away inside
  477. * bi_next for write buffers. This is safe since the bh
  478. * doesn't get submitted to the lower levels of block layer.
  479. */
  480. static struct mirror_set *bio_get_ms(struct bio *bio)
  481. {
  482. return (struct mirror_set *) bio->bi_next;
  483. }
  484. static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
  485. {
  486. bio->bi_next = (struct bio *) ms;
  487. }
  488. /*-----------------------------------------------------------------
  489. * Recovery.
  490. *
  491. * When a mirror is first activated we may find that some regions
  492. * are in the no-sync state. We have to recover these by
  493. * recopying from the default mirror to all the others.
  494. *---------------------------------------------------------------*/
  495. static void recovery_complete(int read_err, unsigned int write_err,
  496. void *context)
  497. {
  498. struct region *reg = (struct region *) context;
  499. /* FIXME: better error handling */
  500. rh_recovery_end(reg, read_err || write_err);
  501. }
  502. static int recover(struct mirror_set *ms, struct region *reg)
  503. {
  504. int r;
  505. unsigned int i;
  506. struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
  507. struct mirror *m;
  508. unsigned long flags = 0;
  509. /* fill in the source */
  510. m = ms->default_mirror;
  511. from.bdev = m->dev->bdev;
  512. from.sector = m->offset + region_to_sector(reg->rh, reg->key);
  513. if (reg->key == (ms->nr_regions - 1)) {
  514. /*
  515. * The final region may be smaller than
  516. * region_size.
  517. */
  518. from.count = ms->ti->len & (reg->rh->region_size - 1);
  519. if (!from.count)
  520. from.count = reg->rh->region_size;
  521. } else
  522. from.count = reg->rh->region_size;
  523. /* fill in the destinations */
  524. for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
  525. if (&ms->mirror[i] == ms->default_mirror)
  526. continue;
  527. m = ms->mirror + i;
  528. dest->bdev = m->dev->bdev;
  529. dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
  530. dest->count = from.count;
  531. dest++;
  532. }
  533. /* hand to kcopyd */
  534. set_bit(KCOPYD_IGNORE_ERROR, &flags);
  535. r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
  536. recovery_complete, reg);
  537. return r;
  538. }
  539. static void do_recovery(struct mirror_set *ms)
  540. {
  541. int r;
  542. struct region *reg;
  543. struct dirty_log *log = ms->rh.log;
  544. /*
  545. * Start quiescing some regions.
  546. */
  547. rh_recovery_prepare(&ms->rh);
  548. /*
  549. * Copy any already quiesced regions.
  550. */
  551. while ((reg = rh_recovery_start(&ms->rh))) {
  552. r = recover(ms, reg);
  553. if (r)
  554. rh_recovery_end(reg, 0);
  555. }
  556. /*
  557. * Update the in sync flag.
  558. */
  559. if (!ms->in_sync &&
  560. (log->type->get_sync_count(log) == ms->nr_regions)) {
  561. /* the sync is complete */
  562. dm_table_event(ms->ti->table);
  563. ms->in_sync = 1;
  564. }
  565. }
  566. /*-----------------------------------------------------------------
  567. * Reads
  568. *---------------------------------------------------------------*/
  569. static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
  570. {
  571. /* FIXME: add read balancing */
  572. return ms->default_mirror;
  573. }
  574. /*
  575. * remap a buffer to a particular mirror.
  576. */
  577. static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
  578. {
  579. bio->bi_bdev = m->dev->bdev;
  580. bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
  581. }
  582. static void do_reads(struct mirror_set *ms, struct bio_list *reads)
  583. {
  584. region_t region;
  585. struct bio *bio;
  586. struct mirror *m;
  587. while ((bio = bio_list_pop(reads))) {
  588. region = bio_to_region(&ms->rh, bio);
  589. /*
  590. * We can only read balance if the region is in sync.
  591. */
  592. if (rh_in_sync(&ms->rh, region, 0))
  593. m = choose_mirror(ms, bio->bi_sector);
  594. else
  595. m = ms->default_mirror;
  596. map_bio(ms, m, bio);
  597. generic_make_request(bio);
  598. }
  599. }
  600. /*-----------------------------------------------------------------
  601. * Writes.
  602. *
  603. * We do different things with the write io depending on the
  604. * state of the region that it's in:
  605. *
  606. * SYNC: increment pending, use kcopyd to write to *all* mirrors
  607. * RECOVERING: delay the io until recovery completes
  608. * NOSYNC: increment pending, just write to the default mirror
  609. *---------------------------------------------------------------*/
  610. static void write_callback(unsigned long error, void *context)
  611. {
  612. unsigned int i;
  613. int uptodate = 1;
  614. struct bio *bio = (struct bio *) context;
  615. struct mirror_set *ms;
  616. ms = bio_get_ms(bio);
  617. bio_set_ms(bio, NULL);
  618. /*
  619. * NOTE: We don't decrement the pending count here,
  620. * instead it is done by the targets endio function.
  621. * This way we handle both writes to SYNC and NOSYNC
  622. * regions with the same code.
  623. */
  624. if (error) {
  625. /*
  626. * only error the io if all mirrors failed.
  627. * FIXME: bogus
  628. */
  629. uptodate = 0;
  630. for (i = 0; i < ms->nr_mirrors; i++)
  631. if (!test_bit(i, &error)) {
  632. uptodate = 1;
  633. break;
  634. }
  635. }
  636. bio_endio(bio, bio->bi_size, 0);
  637. }
  638. static void do_write(struct mirror_set *ms, struct bio *bio)
  639. {
  640. unsigned int i;
  641. struct io_region io[KCOPYD_MAX_REGIONS+1];
  642. struct mirror *m;
  643. for (i = 0; i < ms->nr_mirrors; i++) {
  644. m = ms->mirror + i;
  645. io[i].bdev = m->dev->bdev;
  646. io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
  647. io[i].count = bio->bi_size >> 9;
  648. }
  649. bio_set_ms(bio, ms);
  650. dm_io_async_bvec(ms->nr_mirrors, io, WRITE,
  651. bio->bi_io_vec + bio->bi_idx,
  652. write_callback, bio);
  653. }
  654. static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  655. {
  656. int state;
  657. struct bio *bio;
  658. struct bio_list sync, nosync, recover, *this_list = NULL;
  659. if (!writes->head)
  660. return;
  661. /*
  662. * Classify each write.
  663. */
  664. bio_list_init(&sync);
  665. bio_list_init(&nosync);
  666. bio_list_init(&recover);
  667. while ((bio = bio_list_pop(writes))) {
  668. state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
  669. switch (state) {
  670. case RH_CLEAN:
  671. case RH_DIRTY:
  672. this_list = &sync;
  673. break;
  674. case RH_NOSYNC:
  675. this_list = &nosync;
  676. break;
  677. case RH_RECOVERING:
  678. this_list = &recover;
  679. break;
  680. }
  681. bio_list_add(this_list, bio);
  682. }
  683. /*
  684. * Increment the pending counts for any regions that will
  685. * be written to (writes to recover regions are going to
  686. * be delayed).
  687. */
  688. rh_inc_pending(&ms->rh, &sync);
  689. rh_inc_pending(&ms->rh, &nosync);
  690. rh_flush(&ms->rh);
  691. /*
  692. * Dispatch io.
  693. */
  694. while ((bio = bio_list_pop(&sync)))
  695. do_write(ms, bio);
  696. while ((bio = bio_list_pop(&recover)))
  697. rh_delay(&ms->rh, bio);
  698. while ((bio = bio_list_pop(&nosync))) {
  699. map_bio(ms, ms->default_mirror, bio);
  700. generic_make_request(bio);
  701. }
  702. }
  703. /*-----------------------------------------------------------------
  704. * kmirrord
  705. *---------------------------------------------------------------*/
  706. static LIST_HEAD(_mirror_sets);
  707. static DECLARE_RWSEM(_mirror_sets_lock);
  708. static void do_mirror(struct mirror_set *ms)
  709. {
  710. struct bio_list reads, writes;
  711. spin_lock(&ms->lock);
  712. reads = ms->reads;
  713. writes = ms->writes;
  714. bio_list_init(&ms->reads);
  715. bio_list_init(&ms->writes);
  716. spin_unlock(&ms->lock);
  717. rh_update_states(&ms->rh);
  718. do_recovery(ms);
  719. do_reads(ms, &reads);
  720. do_writes(ms, &writes);
  721. }
  722. static void do_work(void *ignored)
  723. {
  724. struct mirror_set *ms;
  725. down_read(&_mirror_sets_lock);
  726. list_for_each_entry (ms, &_mirror_sets, list)
  727. do_mirror(ms);
  728. up_read(&_mirror_sets_lock);
  729. }
  730. /*-----------------------------------------------------------------
  731. * Target functions
  732. *---------------------------------------------------------------*/
  733. static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  734. uint32_t region_size,
  735. struct dm_target *ti,
  736. struct dirty_log *dl)
  737. {
  738. size_t len;
  739. struct mirror_set *ms = NULL;
  740. if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
  741. return NULL;
  742. len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
  743. ms = kmalloc(len, GFP_KERNEL);
  744. if (!ms) {
  745. ti->error = "dm-mirror: Cannot allocate mirror context";
  746. return NULL;
  747. }
  748. memset(ms, 0, len);
  749. spin_lock_init(&ms->lock);
  750. ms->ti = ti;
  751. ms->nr_mirrors = nr_mirrors;
  752. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  753. ms->in_sync = 0;
  754. ms->default_mirror = &ms->mirror[DEFAULT_MIRROR];
  755. if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
  756. ti->error = "dm-mirror: Error creating dirty region hash";
  757. kfree(ms);
  758. return NULL;
  759. }
  760. return ms;
  761. }
  762. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  763. unsigned int m)
  764. {
  765. while (m--)
  766. dm_put_device(ti, ms->mirror[m].dev);
  767. rh_exit(&ms->rh);
  768. kfree(ms);
  769. }
  770. static inline int _check_region_size(struct dm_target *ti, uint32_t size)
  771. {
  772. return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) ||
  773. size > ti->len);
  774. }
  775. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  776. unsigned int mirror, char **argv)
  777. {
  778. unsigned long long offset;
  779. if (sscanf(argv[1], "%llu", &offset) != 1) {
  780. ti->error = "dm-mirror: Invalid offset";
  781. return -EINVAL;
  782. }
  783. if (dm_get_device(ti, argv[0], offset, ti->len,
  784. dm_table_get_mode(ti->table),
  785. &ms->mirror[mirror].dev)) {
  786. ti->error = "dm-mirror: Device lookup failure";
  787. return -ENXIO;
  788. }
  789. ms->mirror[mirror].offset = offset;
  790. return 0;
  791. }
  792. static int add_mirror_set(struct mirror_set *ms)
  793. {
  794. down_write(&_mirror_sets_lock);
  795. list_add_tail(&ms->list, &_mirror_sets);
  796. up_write(&_mirror_sets_lock);
  797. wake();
  798. return 0;
  799. }
  800. static void del_mirror_set(struct mirror_set *ms)
  801. {
  802. down_write(&_mirror_sets_lock);
  803. list_del(&ms->list);
  804. up_write(&_mirror_sets_lock);
  805. }
  806. /*
  807. * Create dirty log: log_type #log_params <log_params>
  808. */
  809. static struct dirty_log *create_dirty_log(struct dm_target *ti,
  810. unsigned int argc, char **argv,
  811. unsigned int *args_used)
  812. {
  813. unsigned int param_count;
  814. struct dirty_log *dl;
  815. if (argc < 2) {
  816. ti->error = "dm-mirror: Insufficient mirror log arguments";
  817. return NULL;
  818. }
  819. if (sscanf(argv[1], "%u", &param_count) != 1) {
  820. ti->error = "dm-mirror: Invalid mirror log argument count";
  821. return NULL;
  822. }
  823. *args_used = 2 + param_count;
  824. if (argc < *args_used) {
  825. ti->error = "dm-mirror: Insufficient mirror log arguments";
  826. return NULL;
  827. }
  828. dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
  829. if (!dl) {
  830. ti->error = "dm-mirror: Error creating mirror dirty log";
  831. return NULL;
  832. }
  833. if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
  834. ti->error = "dm-mirror: Invalid region size";
  835. dm_destroy_dirty_log(dl);
  836. return NULL;
  837. }
  838. return dl;
  839. }
  840. /*
  841. * Construct a mirror mapping:
  842. *
  843. * log_type #log_params <log_params>
  844. * #mirrors [mirror_path offset]{2,}
  845. *
  846. * log_type is "core" or "disk"
  847. * #log_params is between 1 and 3
  848. */
  849. #define DM_IO_PAGES 64
  850. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  851. {
  852. int r;
  853. unsigned int nr_mirrors, m, args_used;
  854. struct mirror_set *ms;
  855. struct dirty_log *dl;
  856. dl = create_dirty_log(ti, argc, argv, &args_used);
  857. if (!dl)
  858. return -EINVAL;
  859. argv += args_used;
  860. argc -= args_used;
  861. if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
  862. nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
  863. ti->error = "dm-mirror: Invalid number of mirrors";
  864. dm_destroy_dirty_log(dl);
  865. return -EINVAL;
  866. }
  867. argv++, argc--;
  868. if (argc != nr_mirrors * 2) {
  869. ti->error = "dm-mirror: Wrong number of mirror arguments";
  870. dm_destroy_dirty_log(dl);
  871. return -EINVAL;
  872. }
  873. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  874. if (!ms) {
  875. dm_destroy_dirty_log(dl);
  876. return -ENOMEM;
  877. }
  878. /* Get the mirror parameter sets */
  879. for (m = 0; m < nr_mirrors; m++) {
  880. r = get_mirror(ms, ti, m, argv);
  881. if (r) {
  882. free_context(ms, ti, m);
  883. return r;
  884. }
  885. argv += 2;
  886. argc -= 2;
  887. }
  888. ti->private = ms;
  889. ti->split_io = ms->rh.region_size;
  890. r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
  891. if (r) {
  892. free_context(ms, ti, ms->nr_mirrors);
  893. return r;
  894. }
  895. add_mirror_set(ms);
  896. return 0;
  897. }
  898. static void mirror_dtr(struct dm_target *ti)
  899. {
  900. struct mirror_set *ms = (struct mirror_set *) ti->private;
  901. del_mirror_set(ms);
  902. kcopyd_client_destroy(ms->kcopyd_client);
  903. free_context(ms, ti, ms->nr_mirrors);
  904. }
  905. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
  906. {
  907. int should_wake = 0;
  908. struct bio_list *bl;
  909. bl = (rw == WRITE) ? &ms->writes : &ms->reads;
  910. spin_lock(&ms->lock);
  911. should_wake = !(bl->head);
  912. bio_list_add(bl, bio);
  913. spin_unlock(&ms->lock);
  914. if (should_wake)
  915. wake();
  916. }
  917. /*
  918. * Mirror mapping function
  919. */
  920. static int mirror_map(struct dm_target *ti, struct bio *bio,
  921. union map_info *map_context)
  922. {
  923. int r, rw = bio_rw(bio);
  924. struct mirror *m;
  925. struct mirror_set *ms = ti->private;
  926. map_context->ll = bio->bi_sector >> ms->rh.region_shift;
  927. if (rw == WRITE) {
  928. queue_bio(ms, bio, rw);
  929. return 0;
  930. }
  931. r = ms->rh.log->type->in_sync(ms->rh.log,
  932. bio_to_region(&ms->rh, bio), 0);
  933. if (r < 0 && r != -EWOULDBLOCK)
  934. return r;
  935. if (r == -EWOULDBLOCK) /* FIXME: ugly */
  936. r = 0;
  937. /*
  938. * We don't want to fast track a recovery just for a read
  939. * ahead. So we just let it silently fail.
  940. * FIXME: get rid of this.
  941. */
  942. if (!r && rw == READA)
  943. return -EIO;
  944. if (!r) {
  945. /* Pass this io over to the daemon */
  946. queue_bio(ms, bio, rw);
  947. return 0;
  948. }
  949. m = choose_mirror(ms, bio->bi_sector);
  950. if (!m)
  951. return -EIO;
  952. map_bio(ms, m, bio);
  953. return 1;
  954. }
  955. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  956. int error, union map_info *map_context)
  957. {
  958. int rw = bio_rw(bio);
  959. struct mirror_set *ms = (struct mirror_set *) ti->private;
  960. region_t region = map_context->ll;
  961. /*
  962. * We need to dec pending if this was a write.
  963. */
  964. if (rw == WRITE)
  965. rh_dec(&ms->rh, region);
  966. return 0;
  967. }
  968. static void mirror_postsuspend(struct dm_target *ti)
  969. {
  970. struct mirror_set *ms = (struct mirror_set *) ti->private;
  971. struct dirty_log *log = ms->rh.log;
  972. rh_stop_recovery(&ms->rh);
  973. if (log->type->suspend && log->type->suspend(log))
  974. /* FIXME: need better error handling */
  975. DMWARN("log suspend failed");
  976. }
  977. static void mirror_resume(struct dm_target *ti)
  978. {
  979. struct mirror_set *ms = (struct mirror_set *) ti->private;
  980. struct dirty_log *log = ms->rh.log;
  981. if (log->type->resume && log->type->resume(log))
  982. /* FIXME: need better error handling */
  983. DMWARN("log resume failed");
  984. rh_start_recovery(&ms->rh);
  985. }
  986. static int mirror_status(struct dm_target *ti, status_type_t type,
  987. char *result, unsigned int maxlen)
  988. {
  989. unsigned int m, sz;
  990. struct mirror_set *ms = (struct mirror_set *) ti->private;
  991. sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
  992. switch (type) {
  993. case STATUSTYPE_INFO:
  994. DMEMIT("%d ", ms->nr_mirrors);
  995. for (m = 0; m < ms->nr_mirrors; m++)
  996. DMEMIT("%s ", ms->mirror[m].dev->name);
  997. DMEMIT("%llu/%llu",
  998. (unsigned long long)ms->rh.log->type->
  999. get_sync_count(ms->rh.log),
  1000. (unsigned long long)ms->nr_regions);
  1001. break;
  1002. case STATUSTYPE_TABLE:
  1003. DMEMIT("%d ", ms->nr_mirrors);
  1004. for (m = 0; m < ms->nr_mirrors; m++)
  1005. DMEMIT("%s %llu ", ms->mirror[m].dev->name,
  1006. (unsigned long long)ms->mirror[m].offset);
  1007. }
  1008. return 0;
  1009. }
  1010. static struct target_type mirror_target = {
  1011. .name = "mirror",
  1012. .version = {1, 0, 1},
  1013. .module = THIS_MODULE,
  1014. .ctr = mirror_ctr,
  1015. .dtr = mirror_dtr,
  1016. .map = mirror_map,
  1017. .end_io = mirror_end_io,
  1018. .postsuspend = mirror_postsuspend,
  1019. .resume = mirror_resume,
  1020. .status = mirror_status,
  1021. };
  1022. static int __init dm_mirror_init(void)
  1023. {
  1024. int r;
  1025. r = dm_dirty_log_init();
  1026. if (r)
  1027. return r;
  1028. _kmirrord_wq = create_singlethread_workqueue("kmirrord");
  1029. if (!_kmirrord_wq) {
  1030. DMERR("couldn't start kmirrord");
  1031. dm_dirty_log_exit();
  1032. return r;
  1033. }
  1034. INIT_WORK(&_kmirrord_work, do_work, NULL);
  1035. r = dm_register_target(&mirror_target);
  1036. if (r < 0) {
  1037. DMERR("%s: Failed to register mirror target",
  1038. mirror_target.name);
  1039. dm_dirty_log_exit();
  1040. destroy_workqueue(_kmirrord_wq);
  1041. }
  1042. return r;
  1043. }
  1044. static void __exit dm_mirror_exit(void)
  1045. {
  1046. int r;
  1047. r = dm_unregister_target(&mirror_target);
  1048. if (r < 0)
  1049. DMERR("%s: unregister failed %d", mirror_target.name, r);
  1050. destroy_workqueue(_kmirrord_wq);
  1051. dm_dirty_log_exit();
  1052. }
  1053. /* Module hooks */
  1054. module_init(dm_mirror_init);
  1055. module_exit(dm_mirror_exit);
  1056. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1057. MODULE_AUTHOR("Joe Thornber");
  1058. MODULE_LICENSE("GPL");