dm-raid1.c 29 KB

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