dm-raid1.c 28 KB

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