dm-raid1.c 28 KB

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