dm-raid1.c 32 KB

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