dm-raid1.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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-bio-record.h"
  9. #include "dm-io.h"
  10. #include "dm-log.h"
  11. #include "kcopyd.h"
  12. #include <linux/ctype.h>
  13. #include <linux/init.h>
  14. #include <linux/mempool.h>
  15. #include <linux/module.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/slab.h>
  18. #include <linux/time.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/log2.h>
  22. #include <linux/hardirq.h>
  23. #define DM_MSG_PREFIX "raid1"
  24. #define DM_IO_PAGES 64
  25. #define DM_RAID1_HANDLE_ERRORS 0x01
  26. #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
  27. static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
  28. /*-----------------------------------------------------------------
  29. * Region hash
  30. *
  31. * The mirror splits itself up into discrete regions. Each
  32. * region can be in one of three states: clean, dirty,
  33. * nosync. There is no need to put clean regions in the hash.
  34. *
  35. * In addition to being present in the hash table a region _may_
  36. * be present on one of three lists.
  37. *
  38. * clean_regions: Regions on this list have no io pending to
  39. * them, they are in sync, we are no longer interested in them,
  40. * they are dull. rh_update_states() will remove them from the
  41. * hash table.
  42. *
  43. * quiesced_regions: These regions have been spun down, ready
  44. * for recovery. rh_recovery_start() will remove regions from
  45. * this list and hand them to kmirrord, which will schedule the
  46. * recovery io with kcopyd.
  47. *
  48. * recovered_regions: Regions that kcopyd has successfully
  49. * recovered. rh_update_states() will now schedule any delayed
  50. * io, up the recovery_count, and remove the region from the
  51. * hash.
  52. *
  53. * There are 2 locks:
  54. * A rw spin lock 'hash_lock' protects just the hash table,
  55. * this is never held in write mode from interrupt context,
  56. * which I believe means that we only have to disable irqs when
  57. * doing a write lock.
  58. *
  59. * An ordinary spin lock 'region_lock' that protects the three
  60. * lists in the region_hash, with the 'state', 'list' and
  61. * 'bhs_delayed' fields of the regions. This is used from irq
  62. * context, so all other uses will have to suspend local irqs.
  63. *---------------------------------------------------------------*/
  64. struct mirror_set;
  65. struct region_hash {
  66. struct mirror_set *ms;
  67. uint32_t region_size;
  68. unsigned region_shift;
  69. /* holds persistent region state */
  70. struct dm_dirty_log *log;
  71. /* hash table */
  72. rwlock_t hash_lock;
  73. mempool_t *region_pool;
  74. unsigned int mask;
  75. unsigned int nr_buckets;
  76. struct list_head *buckets;
  77. spinlock_t region_lock;
  78. atomic_t recovery_in_flight;
  79. struct semaphore recovery_count;
  80. struct list_head clean_regions;
  81. struct list_head quiesced_regions;
  82. struct list_head recovered_regions;
  83. struct list_head failed_recovered_regions;
  84. };
  85. enum {
  86. RH_CLEAN,
  87. RH_DIRTY,
  88. RH_NOSYNC,
  89. RH_RECOVERING
  90. };
  91. struct region {
  92. struct region_hash *rh; /* FIXME: can we get rid of this ? */
  93. region_t key;
  94. int state;
  95. struct list_head hash_list;
  96. struct list_head list;
  97. atomic_t pending;
  98. struct bio_list delayed_bios;
  99. };
  100. /*-----------------------------------------------------------------
  101. * Mirror set structures.
  102. *---------------------------------------------------------------*/
  103. enum dm_raid1_error {
  104. DM_RAID1_WRITE_ERROR,
  105. DM_RAID1_SYNC_ERROR,
  106. DM_RAID1_READ_ERROR
  107. };
  108. struct mirror {
  109. struct mirror_set *ms;
  110. atomic_t error_count;
  111. unsigned long error_type;
  112. struct dm_dev *dev;
  113. sector_t offset;
  114. };
  115. struct mirror_set {
  116. struct dm_target *ti;
  117. struct list_head list;
  118. struct region_hash rh;
  119. struct dm_kcopyd_client *kcopyd_client;
  120. uint64_t features;
  121. spinlock_t lock; /* protects the lists */
  122. struct bio_list reads;
  123. struct bio_list writes;
  124. struct bio_list failures;
  125. struct dm_io_client *io_client;
  126. mempool_t *read_record_pool;
  127. /* recovery */
  128. region_t nr_regions;
  129. int in_sync;
  130. int log_failure;
  131. atomic_t suspend;
  132. atomic_t default_mirror; /* Default mirror */
  133. struct workqueue_struct *kmirrord_wq;
  134. struct work_struct kmirrord_work;
  135. struct work_struct trigger_event;
  136. unsigned int nr_mirrors;
  137. struct mirror mirror[0];
  138. };
  139. /*
  140. * Conversion fns
  141. */
  142. static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
  143. {
  144. return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
  145. }
  146. static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
  147. {
  148. return region << rh->region_shift;
  149. }
  150. static void wake(struct mirror_set *ms)
  151. {
  152. queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
  153. }
  154. /* FIXME move this */
  155. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
  156. #define MIN_REGIONS 64
  157. #define MAX_RECOVERY 1
  158. static int rh_init(struct region_hash *rh, struct mirror_set *ms,
  159. struct dm_dirty_log *log, uint32_t region_size,
  160. region_t nr_regions)
  161. {
  162. unsigned int nr_buckets, max_buckets;
  163. size_t i;
  164. /*
  165. * Calculate a suitable number of buckets for our hash
  166. * table.
  167. */
  168. max_buckets = nr_regions >> 6;
  169. for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
  170. ;
  171. nr_buckets >>= 1;
  172. rh->ms = ms;
  173. rh->log = log;
  174. rh->region_size = region_size;
  175. rh->region_shift = ffs(region_size) - 1;
  176. rwlock_init(&rh->hash_lock);
  177. rh->mask = nr_buckets - 1;
  178. rh->nr_buckets = nr_buckets;
  179. rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
  180. if (!rh->buckets) {
  181. DMERR("unable to allocate region hash memory");
  182. return -ENOMEM;
  183. }
  184. for (i = 0; i < nr_buckets; i++)
  185. INIT_LIST_HEAD(rh->buckets + i);
  186. spin_lock_init(&rh->region_lock);
  187. sema_init(&rh->recovery_count, 0);
  188. atomic_set(&rh->recovery_in_flight, 0);
  189. INIT_LIST_HEAD(&rh->clean_regions);
  190. INIT_LIST_HEAD(&rh->quiesced_regions);
  191. INIT_LIST_HEAD(&rh->recovered_regions);
  192. INIT_LIST_HEAD(&rh->failed_recovered_regions);
  193. rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
  194. sizeof(struct region));
  195. if (!rh->region_pool) {
  196. vfree(rh->buckets);
  197. rh->buckets = NULL;
  198. return -ENOMEM;
  199. }
  200. return 0;
  201. }
  202. static void rh_exit(struct region_hash *rh)
  203. {
  204. unsigned int h;
  205. struct region *reg, *nreg;
  206. BUG_ON(!list_empty(&rh->quiesced_regions));
  207. for (h = 0; h < rh->nr_buckets; h++) {
  208. list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
  209. BUG_ON(atomic_read(&reg->pending));
  210. mempool_free(reg, rh->region_pool);
  211. }
  212. }
  213. if (rh->log)
  214. dm_dirty_log_destroy(rh->log);
  215. if (rh->region_pool)
  216. mempool_destroy(rh->region_pool);
  217. vfree(rh->buckets);
  218. }
  219. #define RH_HASH_MULT 2654435387U
  220. static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
  221. {
  222. return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
  223. }
  224. static struct region *__rh_lookup(struct region_hash *rh, region_t region)
  225. {
  226. struct region *reg;
  227. list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
  228. if (reg->key == region)
  229. return reg;
  230. return NULL;
  231. }
  232. static void __rh_insert(struct region_hash *rh, struct region *reg)
  233. {
  234. unsigned int h = rh_hash(rh, reg->key);
  235. list_add(&reg->hash_list, rh->buckets + h);
  236. }
  237. static struct region *__rh_alloc(struct region_hash *rh, region_t region)
  238. {
  239. struct region *reg, *nreg;
  240. read_unlock(&rh->hash_lock);
  241. nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
  242. if (unlikely(!nreg))
  243. nreg = kmalloc(sizeof(struct region), GFP_NOIO);
  244. nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
  245. RH_CLEAN : RH_NOSYNC;
  246. nreg->rh = rh;
  247. nreg->key = region;
  248. INIT_LIST_HEAD(&nreg->list);
  249. atomic_set(&nreg->pending, 0);
  250. bio_list_init(&nreg->delayed_bios);
  251. write_lock_irq(&rh->hash_lock);
  252. reg = __rh_lookup(rh, region);
  253. if (reg)
  254. /* we lost the race */
  255. mempool_free(nreg, rh->region_pool);
  256. else {
  257. __rh_insert(rh, nreg);
  258. if (nreg->state == RH_CLEAN) {
  259. spin_lock(&rh->region_lock);
  260. list_add(&nreg->list, &rh->clean_regions);
  261. spin_unlock(&rh->region_lock);
  262. }
  263. reg = nreg;
  264. }
  265. write_unlock_irq(&rh->hash_lock);
  266. read_lock(&rh->hash_lock);
  267. return reg;
  268. }
  269. static inline struct region *__rh_find(struct region_hash *rh, region_t region)
  270. {
  271. struct region *reg;
  272. reg = __rh_lookup(rh, region);
  273. if (!reg)
  274. reg = __rh_alloc(rh, region);
  275. return reg;
  276. }
  277. static int rh_state(struct region_hash *rh, region_t region, int may_block)
  278. {
  279. int r;
  280. struct region *reg;
  281. read_lock(&rh->hash_lock);
  282. reg = __rh_lookup(rh, region);
  283. read_unlock(&rh->hash_lock);
  284. if (reg)
  285. return reg->state;
  286. /*
  287. * The region wasn't in the hash, so we fall back to the
  288. * dirty log.
  289. */
  290. r = rh->log->type->in_sync(rh->log, region, may_block);
  291. /*
  292. * Any error from the dirty log (eg. -EWOULDBLOCK) gets
  293. * taken as a RH_NOSYNC
  294. */
  295. return r == 1 ? RH_CLEAN : RH_NOSYNC;
  296. }
  297. static inline int rh_in_sync(struct region_hash *rh,
  298. region_t region, int may_block)
  299. {
  300. int state = rh_state(rh, region, may_block);
  301. return state == RH_CLEAN || state == RH_DIRTY;
  302. }
  303. static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
  304. {
  305. struct bio *bio;
  306. while ((bio = bio_list_pop(bio_list))) {
  307. queue_bio(ms, bio, WRITE);
  308. }
  309. }
  310. static void complete_resync_work(struct region *reg, int success)
  311. {
  312. struct region_hash *rh = reg->rh;
  313. rh->log->type->set_region_sync(rh->log, reg->key, success);
  314. /*
  315. * Dispatch the bios before we call 'wake_up_all'.
  316. * This is important because if we are suspending,
  317. * we want to know that recovery is complete and
  318. * the work queue is flushed. If we wake_up_all
  319. * before we dispatch_bios (queue bios and call wake()),
  320. * then we risk suspending before the work queue
  321. * has been properly flushed.
  322. */
  323. dispatch_bios(rh->ms, &reg->delayed_bios);
  324. if (atomic_dec_and_test(&rh->recovery_in_flight))
  325. wake_up_all(&_kmirrord_recovery_stopped);
  326. up(&rh->recovery_count);
  327. }
  328. static void rh_update_states(struct region_hash *rh)
  329. {
  330. struct region *reg, *next;
  331. LIST_HEAD(clean);
  332. LIST_HEAD(recovered);
  333. LIST_HEAD(failed_recovered);
  334. /*
  335. * Quickly grab the lists.
  336. */
  337. write_lock_irq(&rh->hash_lock);
  338. spin_lock(&rh->region_lock);
  339. if (!list_empty(&rh->clean_regions)) {
  340. list_splice_init(&rh->clean_regions, &clean);
  341. list_for_each_entry(reg, &clean, list)
  342. list_del(&reg->hash_list);
  343. }
  344. if (!list_empty(&rh->recovered_regions)) {
  345. list_splice_init(&rh->recovered_regions, &recovered);
  346. list_for_each_entry (reg, &recovered, list)
  347. list_del(&reg->hash_list);
  348. }
  349. if (!list_empty(&rh->failed_recovered_regions)) {
  350. list_splice_init(&rh->failed_recovered_regions,
  351. &failed_recovered);
  352. list_for_each_entry(reg, &failed_recovered, list)
  353. list_del(&reg->hash_list);
  354. }
  355. spin_unlock(&rh->region_lock);
  356. write_unlock_irq(&rh->hash_lock);
  357. /*
  358. * All the regions on the recovered and clean lists have
  359. * now been pulled out of the system, so no need to do
  360. * any more locking.
  361. */
  362. list_for_each_entry_safe (reg, next, &recovered, list) {
  363. rh->log->type->clear_region(rh->log, reg->key);
  364. complete_resync_work(reg, 1);
  365. mempool_free(reg, rh->region_pool);
  366. }
  367. list_for_each_entry_safe(reg, next, &failed_recovered, list) {
  368. complete_resync_work(reg, errors_handled(rh->ms) ? 0 : 1);
  369. mempool_free(reg, rh->region_pool);
  370. }
  371. list_for_each_entry_safe(reg, next, &clean, list) {
  372. rh->log->type->clear_region(rh->log, reg->key);
  373. mempool_free(reg, rh->region_pool);
  374. }
  375. rh->log->type->flush(rh->log);
  376. }
  377. static void rh_inc(struct region_hash *rh, region_t region)
  378. {
  379. struct region *reg;
  380. read_lock(&rh->hash_lock);
  381. reg = __rh_find(rh, region);
  382. spin_lock_irq(&rh->region_lock);
  383. atomic_inc(&reg->pending);
  384. if (reg->state == RH_CLEAN) {
  385. reg->state = RH_DIRTY;
  386. list_del_init(&reg->list); /* take off the clean list */
  387. spin_unlock_irq(&rh->region_lock);
  388. rh->log->type->mark_region(rh->log, reg->key);
  389. } else
  390. spin_unlock_irq(&rh->region_lock);
  391. read_unlock(&rh->hash_lock);
  392. }
  393. static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
  394. {
  395. struct bio *bio;
  396. for (bio = bios->head; bio; bio = bio->bi_next)
  397. rh_inc(rh, bio_to_region(rh, bio));
  398. }
  399. static void rh_dec(struct region_hash *rh, region_t region)
  400. {
  401. unsigned long flags;
  402. struct region *reg;
  403. int should_wake = 0;
  404. read_lock(&rh->hash_lock);
  405. reg = __rh_lookup(rh, region);
  406. read_unlock(&rh->hash_lock);
  407. spin_lock_irqsave(&rh->region_lock, flags);
  408. if (atomic_dec_and_test(&reg->pending)) {
  409. /*
  410. * There is no pending I/O for this region.
  411. * We can move the region to corresponding list for next action.
  412. * At this point, the region is not yet connected to any list.
  413. *
  414. * If the state is RH_NOSYNC, the region should be kept off
  415. * from clean list.
  416. * The hash entry for RH_NOSYNC will remain in memory
  417. * until the region is recovered or the map is reloaded.
  418. */
  419. /* do nothing for RH_NOSYNC */
  420. if (reg->state == RH_RECOVERING) {
  421. list_add_tail(&reg->list, &rh->quiesced_regions);
  422. } else if (reg->state == RH_DIRTY) {
  423. reg->state = RH_CLEAN;
  424. list_add(&reg->list, &rh->clean_regions);
  425. }
  426. should_wake = 1;
  427. }
  428. spin_unlock_irqrestore(&rh->region_lock, flags);
  429. if (should_wake)
  430. wake(rh->ms);
  431. }
  432. /*
  433. * Starts quiescing a region in preparation for recovery.
  434. */
  435. static int __rh_recovery_prepare(struct region_hash *rh)
  436. {
  437. int r;
  438. struct region *reg;
  439. region_t region;
  440. /*
  441. * Ask the dirty log what's next.
  442. */
  443. r = rh->log->type->get_resync_work(rh->log, &region);
  444. if (r <= 0)
  445. return r;
  446. /*
  447. * Get this region, and start it quiescing by setting the
  448. * recovering flag.
  449. */
  450. read_lock(&rh->hash_lock);
  451. reg = __rh_find(rh, region);
  452. read_unlock(&rh->hash_lock);
  453. spin_lock_irq(&rh->region_lock);
  454. reg->state = RH_RECOVERING;
  455. /* Already quiesced ? */
  456. if (atomic_read(&reg->pending))
  457. list_del_init(&reg->list);
  458. else
  459. list_move(&reg->list, &rh->quiesced_regions);
  460. spin_unlock_irq(&rh->region_lock);
  461. return 1;
  462. }
  463. static void rh_recovery_prepare(struct region_hash *rh)
  464. {
  465. /* Extra reference to avoid race with rh_stop_recovery */
  466. atomic_inc(&rh->recovery_in_flight);
  467. while (!down_trylock(&rh->recovery_count)) {
  468. atomic_inc(&rh->recovery_in_flight);
  469. if (__rh_recovery_prepare(rh) <= 0) {
  470. atomic_dec(&rh->recovery_in_flight);
  471. up(&rh->recovery_count);
  472. break;
  473. }
  474. }
  475. /* Drop the extra reference */
  476. if (atomic_dec_and_test(&rh->recovery_in_flight))
  477. wake_up_all(&_kmirrord_recovery_stopped);
  478. }
  479. /*
  480. * Returns any quiesced regions.
  481. */
  482. static struct region *rh_recovery_start(struct region_hash *rh)
  483. {
  484. struct region *reg = NULL;
  485. spin_lock_irq(&rh->region_lock);
  486. if (!list_empty(&rh->quiesced_regions)) {
  487. reg = list_entry(rh->quiesced_regions.next,
  488. struct region, list);
  489. list_del_init(&reg->list); /* remove from the quiesced list */
  490. }
  491. spin_unlock_irq(&rh->region_lock);
  492. return reg;
  493. }
  494. static void rh_recovery_end(struct region *reg, int success)
  495. {
  496. struct region_hash *rh = reg->rh;
  497. spin_lock_irq(&rh->region_lock);
  498. if (success)
  499. list_add(&reg->list, &reg->rh->recovered_regions);
  500. else {
  501. reg->state = RH_NOSYNC;
  502. list_add(&reg->list, &reg->rh->failed_recovered_regions);
  503. }
  504. spin_unlock_irq(&rh->region_lock);
  505. wake(rh->ms);
  506. }
  507. static int rh_flush(struct region_hash *rh)
  508. {
  509. return rh->log->type->flush(rh->log);
  510. }
  511. static void rh_delay(struct region_hash *rh, struct bio *bio)
  512. {
  513. struct region *reg;
  514. read_lock(&rh->hash_lock);
  515. reg = __rh_find(rh, bio_to_region(rh, bio));
  516. bio_list_add(&reg->delayed_bios, bio);
  517. read_unlock(&rh->hash_lock);
  518. }
  519. static void rh_stop_recovery(struct region_hash *rh)
  520. {
  521. int i;
  522. /* wait for any recovering regions */
  523. for (i = 0; i < MAX_RECOVERY; i++)
  524. down(&rh->recovery_count);
  525. }
  526. static void rh_start_recovery(struct region_hash *rh)
  527. {
  528. int i;
  529. for (i = 0; i < MAX_RECOVERY; i++)
  530. up(&rh->recovery_count);
  531. wake(rh->ms);
  532. }
  533. #define MIN_READ_RECORDS 20
  534. struct dm_raid1_read_record {
  535. struct mirror *m;
  536. struct dm_bio_details details;
  537. };
  538. /*
  539. * Every mirror should look like this one.
  540. */
  541. #define DEFAULT_MIRROR 0
  542. /*
  543. * This is yucky. We squirrel the mirror struct away inside
  544. * bi_next for read/write buffers. This is safe since the bh
  545. * doesn't get submitted to the lower levels of block layer.
  546. */
  547. static struct mirror *bio_get_m(struct bio *bio)
  548. {
  549. return (struct mirror *) bio->bi_next;
  550. }
  551. static void bio_set_m(struct bio *bio, struct mirror *m)
  552. {
  553. bio->bi_next = (struct bio *) m;
  554. }
  555. static struct mirror *get_default_mirror(struct mirror_set *ms)
  556. {
  557. return &ms->mirror[atomic_read(&ms->default_mirror)];
  558. }
  559. static void set_default_mirror(struct mirror *m)
  560. {
  561. struct mirror_set *ms = m->ms;
  562. struct mirror *m0 = &(ms->mirror[0]);
  563. atomic_set(&ms->default_mirror, m - m0);
  564. }
  565. /* fail_mirror
  566. * @m: mirror device to fail
  567. * @error_type: one of the enum's, DM_RAID1_*_ERROR
  568. *
  569. * If errors are being handled, record the type of
  570. * error encountered for this device. If this type
  571. * of error has already been recorded, we can return;
  572. * otherwise, we must signal userspace by triggering
  573. * an event. Additionally, if the device is the
  574. * primary device, we must choose a new primary, but
  575. * only if the mirror is in-sync.
  576. *
  577. * This function must not block.
  578. */
  579. static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
  580. {
  581. struct mirror_set *ms = m->ms;
  582. struct mirror *new;
  583. if (!errors_handled(ms))
  584. return;
  585. /*
  586. * error_count is used for nothing more than a
  587. * simple way to tell if a device has encountered
  588. * errors.
  589. */
  590. atomic_inc(&m->error_count);
  591. if (test_and_set_bit(error_type, &m->error_type))
  592. return;
  593. if (m != get_default_mirror(ms))
  594. goto out;
  595. if (!ms->in_sync) {
  596. /*
  597. * Better to issue requests to same failing device
  598. * than to risk returning corrupt data.
  599. */
  600. DMERR("Primary mirror (%s) failed while out-of-sync: "
  601. "Reads may fail.", m->dev->name);
  602. goto out;
  603. }
  604. for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
  605. if (!atomic_read(&new->error_count)) {
  606. set_default_mirror(new);
  607. break;
  608. }
  609. if (unlikely(new == ms->mirror + ms->nr_mirrors))
  610. DMWARN("All sides of mirror have failed.");
  611. out:
  612. schedule_work(&ms->trigger_event);
  613. }
  614. /*-----------------------------------------------------------------
  615. * Recovery.
  616. *
  617. * When a mirror is first activated we may find that some regions
  618. * are in the no-sync state. We have to recover these by
  619. * recopying from the default mirror to all the others.
  620. *---------------------------------------------------------------*/
  621. static void recovery_complete(int read_err, unsigned long write_err,
  622. void *context)
  623. {
  624. struct region *reg = (struct region *)context;
  625. struct mirror_set *ms = reg->rh->ms;
  626. int m, bit = 0;
  627. if (read_err) {
  628. /* Read error means the failure of default mirror. */
  629. DMERR_LIMIT("Unable to read primary mirror during recovery");
  630. fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
  631. }
  632. if (write_err) {
  633. DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
  634. write_err);
  635. /*
  636. * Bits correspond to devices (excluding default mirror).
  637. * The default mirror cannot change during recovery.
  638. */
  639. for (m = 0; m < ms->nr_mirrors; m++) {
  640. if (&ms->mirror[m] == get_default_mirror(ms))
  641. continue;
  642. if (test_bit(bit, &write_err))
  643. fail_mirror(ms->mirror + m,
  644. DM_RAID1_SYNC_ERROR);
  645. bit++;
  646. }
  647. }
  648. rh_recovery_end(reg, !(read_err || write_err));
  649. }
  650. static int recover(struct mirror_set *ms, struct region *reg)
  651. {
  652. int r;
  653. unsigned int i;
  654. struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
  655. struct mirror *m;
  656. unsigned long flags = 0;
  657. /* fill in the source */
  658. m = get_default_mirror(ms);
  659. from.bdev = m->dev->bdev;
  660. from.sector = m->offset + region_to_sector(reg->rh, reg->key);
  661. if (reg->key == (ms->nr_regions - 1)) {
  662. /*
  663. * The final region may be smaller than
  664. * region_size.
  665. */
  666. from.count = ms->ti->len & (reg->rh->region_size - 1);
  667. if (!from.count)
  668. from.count = reg->rh->region_size;
  669. } else
  670. from.count = reg->rh->region_size;
  671. /* fill in the destinations */
  672. for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
  673. if (&ms->mirror[i] == get_default_mirror(ms))
  674. continue;
  675. m = ms->mirror + i;
  676. dest->bdev = m->dev->bdev;
  677. dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
  678. dest->count = from.count;
  679. dest++;
  680. }
  681. /* hand to kcopyd */
  682. set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
  683. r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
  684. flags, recovery_complete, reg);
  685. return r;
  686. }
  687. static void do_recovery(struct mirror_set *ms)
  688. {
  689. int r;
  690. struct region *reg;
  691. struct dm_dirty_log *log = ms->rh.log;
  692. /*
  693. * Start quiescing some regions.
  694. */
  695. rh_recovery_prepare(&ms->rh);
  696. /*
  697. * Copy any already quiesced regions.
  698. */
  699. while ((reg = rh_recovery_start(&ms->rh))) {
  700. r = recover(ms, reg);
  701. if (r)
  702. rh_recovery_end(reg, 0);
  703. }
  704. /*
  705. * Update the in sync flag.
  706. */
  707. if (!ms->in_sync &&
  708. (log->type->get_sync_count(log) == ms->nr_regions)) {
  709. /* the sync is complete */
  710. dm_table_event(ms->ti->table);
  711. ms->in_sync = 1;
  712. }
  713. }
  714. /*-----------------------------------------------------------------
  715. * Reads
  716. *---------------------------------------------------------------*/
  717. static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
  718. {
  719. struct mirror *m = get_default_mirror(ms);
  720. do {
  721. if (likely(!atomic_read(&m->error_count)))
  722. return m;
  723. if (m-- == ms->mirror)
  724. m += ms->nr_mirrors;
  725. } while (m != get_default_mirror(ms));
  726. return NULL;
  727. }
  728. static int default_ok(struct mirror *m)
  729. {
  730. struct mirror *default_mirror = get_default_mirror(m->ms);
  731. return !atomic_read(&default_mirror->error_count);
  732. }
  733. static int mirror_available(struct mirror_set *ms, struct bio *bio)
  734. {
  735. region_t region = bio_to_region(&ms->rh, bio);
  736. if (ms->rh.log->type->in_sync(ms->rh.log, region, 0))
  737. return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
  738. return 0;
  739. }
  740. /*
  741. * remap a buffer to a particular mirror.
  742. */
  743. static sector_t map_sector(struct mirror *m, struct bio *bio)
  744. {
  745. return m->offset + (bio->bi_sector - m->ms->ti->begin);
  746. }
  747. static void map_bio(struct mirror *m, struct bio *bio)
  748. {
  749. bio->bi_bdev = m->dev->bdev;
  750. bio->bi_sector = map_sector(m, bio);
  751. }
  752. static void map_region(struct dm_io_region *io, struct mirror *m,
  753. struct bio *bio)
  754. {
  755. io->bdev = m->dev->bdev;
  756. io->sector = map_sector(m, bio);
  757. io->count = bio->bi_size >> 9;
  758. }
  759. /*-----------------------------------------------------------------
  760. * Reads
  761. *---------------------------------------------------------------*/
  762. static void read_callback(unsigned long error, void *context)
  763. {
  764. struct bio *bio = context;
  765. struct mirror *m;
  766. m = bio_get_m(bio);
  767. bio_set_m(bio, NULL);
  768. if (likely(!error)) {
  769. bio_endio(bio, 0);
  770. return;
  771. }
  772. fail_mirror(m, DM_RAID1_READ_ERROR);
  773. if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
  774. DMWARN_LIMIT("Read failure on mirror device %s. "
  775. "Trying alternative device.",
  776. m->dev->name);
  777. queue_bio(m->ms, bio, bio_rw(bio));
  778. return;
  779. }
  780. DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
  781. m->dev->name);
  782. bio_endio(bio, -EIO);
  783. }
  784. /* Asynchronous read. */
  785. static void read_async_bio(struct mirror *m, struct bio *bio)
  786. {
  787. struct dm_io_region io;
  788. struct dm_io_request io_req = {
  789. .bi_rw = READ,
  790. .mem.type = DM_IO_BVEC,
  791. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  792. .notify.fn = read_callback,
  793. .notify.context = bio,
  794. .client = m->ms->io_client,
  795. };
  796. map_region(&io, m, bio);
  797. bio_set_m(bio, m);
  798. (void) dm_io(&io_req, 1, &io, NULL);
  799. }
  800. static void do_reads(struct mirror_set *ms, struct bio_list *reads)
  801. {
  802. region_t region;
  803. struct bio *bio;
  804. struct mirror *m;
  805. while ((bio = bio_list_pop(reads))) {
  806. region = bio_to_region(&ms->rh, bio);
  807. m = get_default_mirror(ms);
  808. /*
  809. * We can only read balance if the region is in sync.
  810. */
  811. if (likely(rh_in_sync(&ms->rh, region, 1)))
  812. m = choose_mirror(ms, bio->bi_sector);
  813. else if (m && atomic_read(&m->error_count))
  814. m = NULL;
  815. if (likely(m))
  816. read_async_bio(m, bio);
  817. else
  818. bio_endio(bio, -EIO);
  819. }
  820. }
  821. /*-----------------------------------------------------------------
  822. * Writes.
  823. *
  824. * We do different things with the write io depending on the
  825. * state of the region that it's in:
  826. *
  827. * SYNC: increment pending, use kcopyd to write to *all* mirrors
  828. * RECOVERING: delay the io until recovery completes
  829. * NOSYNC: increment pending, just write to the default mirror
  830. *---------------------------------------------------------------*/
  831. /* __bio_mark_nosync
  832. * @ms
  833. * @bio
  834. * @done
  835. * @error
  836. *
  837. * The bio was written on some mirror(s) but failed on other mirror(s).
  838. * We can successfully endio the bio but should avoid the region being
  839. * marked clean by setting the state RH_NOSYNC.
  840. *
  841. * This function is _not_ safe in interrupt context!
  842. */
  843. static void __bio_mark_nosync(struct mirror_set *ms,
  844. struct bio *bio, unsigned done, int error)
  845. {
  846. unsigned long flags;
  847. struct region_hash *rh = &ms->rh;
  848. struct dm_dirty_log *log = ms->rh.log;
  849. struct region *reg;
  850. region_t region = bio_to_region(rh, bio);
  851. int recovering = 0;
  852. /* We must inform the log that the sync count has changed. */
  853. log->type->set_region_sync(log, region, 0);
  854. ms->in_sync = 0;
  855. read_lock(&rh->hash_lock);
  856. reg = __rh_find(rh, region);
  857. read_unlock(&rh->hash_lock);
  858. /* region hash entry should exist because write was in-flight */
  859. BUG_ON(!reg);
  860. BUG_ON(!list_empty(&reg->list));
  861. spin_lock_irqsave(&rh->region_lock, flags);
  862. /*
  863. * Possible cases:
  864. * 1) RH_DIRTY
  865. * 2) RH_NOSYNC: was dirty, other preceeding writes failed
  866. * 3) RH_RECOVERING: flushing pending writes
  867. * Either case, the region should have not been connected to list.
  868. */
  869. recovering = (reg->state == RH_RECOVERING);
  870. reg->state = RH_NOSYNC;
  871. BUG_ON(!list_empty(&reg->list));
  872. spin_unlock_irqrestore(&rh->region_lock, flags);
  873. bio_endio(bio, error);
  874. if (recovering)
  875. complete_resync_work(reg, 0);
  876. }
  877. static void write_callback(unsigned long error, void *context)
  878. {
  879. unsigned i, ret = 0;
  880. struct bio *bio = (struct bio *) context;
  881. struct mirror_set *ms;
  882. int uptodate = 0;
  883. int should_wake = 0;
  884. unsigned long flags;
  885. ms = bio_get_m(bio)->ms;
  886. bio_set_m(bio, NULL);
  887. /*
  888. * NOTE: We don't decrement the pending count here,
  889. * instead it is done by the targets endio function.
  890. * This way we handle both writes to SYNC and NOSYNC
  891. * regions with the same code.
  892. */
  893. if (likely(!error))
  894. goto out;
  895. for (i = 0; i < ms->nr_mirrors; i++)
  896. if (test_bit(i, &error))
  897. fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
  898. else
  899. uptodate = 1;
  900. if (unlikely(!uptodate)) {
  901. DMERR("All replicated volumes dead, failing I/O");
  902. /* None of the writes succeeded, fail the I/O. */
  903. ret = -EIO;
  904. } else if (errors_handled(ms)) {
  905. /*
  906. * Need to raise event. Since raising
  907. * events can block, we need to do it in
  908. * the main thread.
  909. */
  910. spin_lock_irqsave(&ms->lock, flags);
  911. if (!ms->failures.head)
  912. should_wake = 1;
  913. bio_list_add(&ms->failures, bio);
  914. spin_unlock_irqrestore(&ms->lock, flags);
  915. if (should_wake)
  916. wake(ms);
  917. return;
  918. }
  919. out:
  920. bio_endio(bio, ret);
  921. }
  922. static void do_write(struct mirror_set *ms, struct bio *bio)
  923. {
  924. unsigned int i;
  925. struct dm_io_region io[ms->nr_mirrors], *dest = io;
  926. struct mirror *m;
  927. struct dm_io_request io_req = {
  928. .bi_rw = WRITE,
  929. .mem.type = DM_IO_BVEC,
  930. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  931. .notify.fn = write_callback,
  932. .notify.context = bio,
  933. .client = ms->io_client,
  934. };
  935. for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
  936. map_region(dest++, m, bio);
  937. /*
  938. * Use default mirror because we only need it to retrieve the reference
  939. * to the mirror set in write_callback().
  940. */
  941. bio_set_m(bio, get_default_mirror(ms));
  942. (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
  943. }
  944. static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  945. {
  946. int state;
  947. struct bio *bio;
  948. struct bio_list sync, nosync, recover, *this_list = NULL;
  949. if (!writes->head)
  950. return;
  951. /*
  952. * Classify each write.
  953. */
  954. bio_list_init(&sync);
  955. bio_list_init(&nosync);
  956. bio_list_init(&recover);
  957. while ((bio = bio_list_pop(writes))) {
  958. state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
  959. switch (state) {
  960. case RH_CLEAN:
  961. case RH_DIRTY:
  962. this_list = &sync;
  963. break;
  964. case RH_NOSYNC:
  965. this_list = &nosync;
  966. break;
  967. case RH_RECOVERING:
  968. this_list = &recover;
  969. break;
  970. }
  971. bio_list_add(this_list, bio);
  972. }
  973. /*
  974. * Increment the pending counts for any regions that will
  975. * be written to (writes to recover regions are going to
  976. * be delayed).
  977. */
  978. rh_inc_pending(&ms->rh, &sync);
  979. rh_inc_pending(&ms->rh, &nosync);
  980. ms->log_failure = rh_flush(&ms->rh) ? 1 : 0;
  981. /*
  982. * Dispatch io.
  983. */
  984. if (unlikely(ms->log_failure)) {
  985. spin_lock_irq(&ms->lock);
  986. bio_list_merge(&ms->failures, &sync);
  987. spin_unlock_irq(&ms->lock);
  988. } else
  989. while ((bio = bio_list_pop(&sync)))
  990. do_write(ms, bio);
  991. while ((bio = bio_list_pop(&recover)))
  992. rh_delay(&ms->rh, bio);
  993. while ((bio = bio_list_pop(&nosync))) {
  994. map_bio(get_default_mirror(ms), bio);
  995. generic_make_request(bio);
  996. }
  997. }
  998. static void do_failures(struct mirror_set *ms, struct bio_list *failures)
  999. {
  1000. struct bio *bio;
  1001. if (!failures->head)
  1002. return;
  1003. if (!ms->log_failure) {
  1004. while ((bio = bio_list_pop(failures)))
  1005. __bio_mark_nosync(ms, bio, bio->bi_size, 0);
  1006. return;
  1007. }
  1008. /*
  1009. * If the log has failed, unattempted writes are being
  1010. * put on the failures list. We can't issue those writes
  1011. * until a log has been marked, so we must store them.
  1012. *
  1013. * If a 'noflush' suspend is in progress, we can requeue
  1014. * the I/O's to the core. This give userspace a chance
  1015. * to reconfigure the mirror, at which point the core
  1016. * will reissue the writes. If the 'noflush' flag is
  1017. * not set, we have no choice but to return errors.
  1018. *
  1019. * Some writes on the failures list may have been
  1020. * submitted before the log failure and represent a
  1021. * failure to write to one of the devices. It is ok
  1022. * for us to treat them the same and requeue them
  1023. * as well.
  1024. */
  1025. if (dm_noflush_suspending(ms->ti)) {
  1026. while ((bio = bio_list_pop(failures)))
  1027. bio_endio(bio, DM_ENDIO_REQUEUE);
  1028. return;
  1029. }
  1030. if (atomic_read(&ms->suspend)) {
  1031. while ((bio = bio_list_pop(failures)))
  1032. bio_endio(bio, -EIO);
  1033. return;
  1034. }
  1035. spin_lock_irq(&ms->lock);
  1036. bio_list_merge(&ms->failures, failures);
  1037. spin_unlock_irq(&ms->lock);
  1038. wake(ms);
  1039. }
  1040. static void trigger_event(struct work_struct *work)
  1041. {
  1042. struct mirror_set *ms =
  1043. container_of(work, struct mirror_set, trigger_event);
  1044. dm_table_event(ms->ti->table);
  1045. }
  1046. /*-----------------------------------------------------------------
  1047. * kmirrord
  1048. *---------------------------------------------------------------*/
  1049. static int _do_mirror(struct work_struct *work)
  1050. {
  1051. struct mirror_set *ms =container_of(work, struct mirror_set,
  1052. kmirrord_work);
  1053. struct bio_list reads, writes, failures;
  1054. unsigned long flags;
  1055. spin_lock_irqsave(&ms->lock, flags);
  1056. reads = ms->reads;
  1057. writes = ms->writes;
  1058. failures = ms->failures;
  1059. bio_list_init(&ms->reads);
  1060. bio_list_init(&ms->writes);
  1061. bio_list_init(&ms->failures);
  1062. spin_unlock_irqrestore(&ms->lock, flags);
  1063. rh_update_states(&ms->rh);
  1064. do_recovery(ms);
  1065. do_reads(ms, &reads);
  1066. do_writes(ms, &writes);
  1067. do_failures(ms, &failures);
  1068. return (ms->failures.head) ? 1 : 0;
  1069. }
  1070. static void do_mirror(struct work_struct *work)
  1071. {
  1072. /*
  1073. * If _do_mirror returns 1, we give it
  1074. * another shot. This helps for cases like
  1075. * 'suspend' where we call flush_workqueue
  1076. * and expect all work to be finished. If
  1077. * a failure happens during a suspend, we
  1078. * couldn't issue a 'wake' because it would
  1079. * not be honored. Therefore, we return '1'
  1080. * from _do_mirror, and retry here.
  1081. */
  1082. while (_do_mirror(work))
  1083. schedule();
  1084. }
  1085. /*-----------------------------------------------------------------
  1086. * Target functions
  1087. *---------------------------------------------------------------*/
  1088. static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  1089. uint32_t region_size,
  1090. struct dm_target *ti,
  1091. struct dm_dirty_log *dl)
  1092. {
  1093. size_t len;
  1094. struct mirror_set *ms = NULL;
  1095. if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
  1096. return NULL;
  1097. len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
  1098. ms = kzalloc(len, GFP_KERNEL);
  1099. if (!ms) {
  1100. ti->error = "Cannot allocate mirror context";
  1101. return NULL;
  1102. }
  1103. spin_lock_init(&ms->lock);
  1104. ms->ti = ti;
  1105. ms->nr_mirrors = nr_mirrors;
  1106. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  1107. ms->in_sync = 0;
  1108. ms->log_failure = 0;
  1109. atomic_set(&ms->suspend, 0);
  1110. atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
  1111. len = sizeof(struct dm_raid1_read_record);
  1112. ms->read_record_pool = mempool_create_kmalloc_pool(MIN_READ_RECORDS,
  1113. len);
  1114. if (!ms->read_record_pool) {
  1115. ti->error = "Error creating mirror read_record_pool";
  1116. kfree(ms);
  1117. return NULL;
  1118. }
  1119. ms->io_client = dm_io_client_create(DM_IO_PAGES);
  1120. if (IS_ERR(ms->io_client)) {
  1121. ti->error = "Error creating dm_io client";
  1122. mempool_destroy(ms->read_record_pool);
  1123. kfree(ms);
  1124. return NULL;
  1125. }
  1126. if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
  1127. ti->error = "Error creating dirty region hash";
  1128. dm_io_client_destroy(ms->io_client);
  1129. mempool_destroy(ms->read_record_pool);
  1130. kfree(ms);
  1131. return NULL;
  1132. }
  1133. return ms;
  1134. }
  1135. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  1136. unsigned int m)
  1137. {
  1138. while (m--)
  1139. dm_put_device(ti, ms->mirror[m].dev);
  1140. dm_io_client_destroy(ms->io_client);
  1141. rh_exit(&ms->rh);
  1142. mempool_destroy(ms->read_record_pool);
  1143. kfree(ms);
  1144. }
  1145. static inline int _check_region_size(struct dm_target *ti, uint32_t size)
  1146. {
  1147. return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
  1148. size > ti->len);
  1149. }
  1150. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  1151. unsigned int mirror, char **argv)
  1152. {
  1153. unsigned long long offset;
  1154. if (sscanf(argv[1], "%llu", &offset) != 1) {
  1155. ti->error = "Invalid offset";
  1156. return -EINVAL;
  1157. }
  1158. if (dm_get_device(ti, argv[0], offset, ti->len,
  1159. dm_table_get_mode(ti->table),
  1160. &ms->mirror[mirror].dev)) {
  1161. ti->error = "Device lookup failure";
  1162. return -ENXIO;
  1163. }
  1164. ms->mirror[mirror].ms = ms;
  1165. atomic_set(&(ms->mirror[mirror].error_count), 0);
  1166. ms->mirror[mirror].error_type = 0;
  1167. ms->mirror[mirror].offset = offset;
  1168. return 0;
  1169. }
  1170. /*
  1171. * Create dirty log: log_type #log_params <log_params>
  1172. */
  1173. static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
  1174. unsigned int argc, char **argv,
  1175. unsigned int *args_used)
  1176. {
  1177. unsigned int param_count;
  1178. struct dm_dirty_log *dl;
  1179. if (argc < 2) {
  1180. ti->error = "Insufficient mirror log arguments";
  1181. return NULL;
  1182. }
  1183. if (sscanf(argv[1], "%u", &param_count) != 1) {
  1184. ti->error = "Invalid mirror log argument count";
  1185. return NULL;
  1186. }
  1187. *args_used = 2 + param_count;
  1188. if (argc < *args_used) {
  1189. ti->error = "Insufficient mirror log arguments";
  1190. return NULL;
  1191. }
  1192. dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
  1193. if (!dl) {
  1194. ti->error = "Error creating mirror dirty log";
  1195. return NULL;
  1196. }
  1197. if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
  1198. ti->error = "Invalid region size";
  1199. dm_dirty_log_destroy(dl);
  1200. return NULL;
  1201. }
  1202. return dl;
  1203. }
  1204. static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
  1205. unsigned *args_used)
  1206. {
  1207. unsigned num_features;
  1208. struct dm_target *ti = ms->ti;
  1209. *args_used = 0;
  1210. if (!argc)
  1211. return 0;
  1212. if (sscanf(argv[0], "%u", &num_features) != 1) {
  1213. ti->error = "Invalid number of features";
  1214. return -EINVAL;
  1215. }
  1216. argc--;
  1217. argv++;
  1218. (*args_used)++;
  1219. if (num_features > argc) {
  1220. ti->error = "Not enough arguments to support feature count";
  1221. return -EINVAL;
  1222. }
  1223. if (!strcmp("handle_errors", argv[0]))
  1224. ms->features |= DM_RAID1_HANDLE_ERRORS;
  1225. else {
  1226. ti->error = "Unrecognised feature requested";
  1227. return -EINVAL;
  1228. }
  1229. (*args_used)++;
  1230. return 0;
  1231. }
  1232. /*
  1233. * Construct a mirror mapping:
  1234. *
  1235. * log_type #log_params <log_params>
  1236. * #mirrors [mirror_path offset]{2,}
  1237. * [#features <features>]
  1238. *
  1239. * log_type is "core" or "disk"
  1240. * #log_params is between 1 and 3
  1241. *
  1242. * If present, features must be "handle_errors".
  1243. */
  1244. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1245. {
  1246. int r;
  1247. unsigned int nr_mirrors, m, args_used;
  1248. struct mirror_set *ms;
  1249. struct dm_dirty_log *dl;
  1250. dl = create_dirty_log(ti, argc, argv, &args_used);
  1251. if (!dl)
  1252. return -EINVAL;
  1253. argv += args_used;
  1254. argc -= args_used;
  1255. if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
  1256. nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
  1257. ti->error = "Invalid number of mirrors";
  1258. dm_dirty_log_destroy(dl);
  1259. return -EINVAL;
  1260. }
  1261. argv++, argc--;
  1262. if (argc < nr_mirrors * 2) {
  1263. ti->error = "Too few mirror arguments";
  1264. dm_dirty_log_destroy(dl);
  1265. return -EINVAL;
  1266. }
  1267. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  1268. if (!ms) {
  1269. dm_dirty_log_destroy(dl);
  1270. return -ENOMEM;
  1271. }
  1272. /* Get the mirror parameter sets */
  1273. for (m = 0; m < nr_mirrors; m++) {
  1274. r = get_mirror(ms, ti, m, argv);
  1275. if (r) {
  1276. free_context(ms, ti, m);
  1277. return r;
  1278. }
  1279. argv += 2;
  1280. argc -= 2;
  1281. }
  1282. ti->private = ms;
  1283. ti->split_io = ms->rh.region_size;
  1284. ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
  1285. if (!ms->kmirrord_wq) {
  1286. DMERR("couldn't start kmirrord");
  1287. r = -ENOMEM;
  1288. goto err_free_context;
  1289. }
  1290. INIT_WORK(&ms->kmirrord_work, do_mirror);
  1291. INIT_WORK(&ms->trigger_event, trigger_event);
  1292. r = parse_features(ms, argc, argv, &args_used);
  1293. if (r)
  1294. goto err_destroy_wq;
  1295. argv += args_used;
  1296. argc -= args_used;
  1297. /*
  1298. * Any read-balancing addition depends on the
  1299. * DM_RAID1_HANDLE_ERRORS flag being present.
  1300. * This is because the decision to balance depends
  1301. * on the sync state of a region. If the above
  1302. * flag is not present, we ignore errors; and
  1303. * the sync state may be inaccurate.
  1304. */
  1305. if (argc) {
  1306. ti->error = "Too many mirror arguments";
  1307. r = -EINVAL;
  1308. goto err_destroy_wq;
  1309. }
  1310. r = dm_kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
  1311. if (r)
  1312. goto err_destroy_wq;
  1313. wake(ms);
  1314. return 0;
  1315. err_destroy_wq:
  1316. destroy_workqueue(ms->kmirrord_wq);
  1317. err_free_context:
  1318. free_context(ms, ti, ms->nr_mirrors);
  1319. return r;
  1320. }
  1321. static void mirror_dtr(struct dm_target *ti)
  1322. {
  1323. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1324. flush_workqueue(ms->kmirrord_wq);
  1325. dm_kcopyd_client_destroy(ms->kcopyd_client);
  1326. destroy_workqueue(ms->kmirrord_wq);
  1327. free_context(ms, ti, ms->nr_mirrors);
  1328. }
  1329. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
  1330. {
  1331. unsigned long flags;
  1332. int should_wake = 0;
  1333. struct bio_list *bl;
  1334. bl = (rw == WRITE) ? &ms->writes : &ms->reads;
  1335. spin_lock_irqsave(&ms->lock, flags);
  1336. should_wake = !(bl->head);
  1337. bio_list_add(bl, bio);
  1338. spin_unlock_irqrestore(&ms->lock, flags);
  1339. if (should_wake)
  1340. wake(ms);
  1341. }
  1342. /*
  1343. * Mirror mapping function
  1344. */
  1345. static int mirror_map(struct dm_target *ti, struct bio *bio,
  1346. union map_info *map_context)
  1347. {
  1348. int r, rw = bio_rw(bio);
  1349. struct mirror *m;
  1350. struct mirror_set *ms = ti->private;
  1351. struct dm_raid1_read_record *read_record = NULL;
  1352. if (rw == WRITE) {
  1353. /* Save region for mirror_end_io() handler */
  1354. map_context->ll = bio_to_region(&ms->rh, bio);
  1355. queue_bio(ms, bio, rw);
  1356. return DM_MAPIO_SUBMITTED;
  1357. }
  1358. r = ms->rh.log->type->in_sync(ms->rh.log,
  1359. bio_to_region(&ms->rh, bio), 0);
  1360. if (r < 0 && r != -EWOULDBLOCK)
  1361. return r;
  1362. /*
  1363. * If region is not in-sync queue the bio.
  1364. */
  1365. if (!r || (r == -EWOULDBLOCK)) {
  1366. if (rw == READA)
  1367. return -EWOULDBLOCK;
  1368. queue_bio(ms, bio, rw);
  1369. return DM_MAPIO_SUBMITTED;
  1370. }
  1371. /*
  1372. * The region is in-sync and we can perform reads directly.
  1373. * Store enough information so we can retry if it fails.
  1374. */
  1375. m = choose_mirror(ms, bio->bi_sector);
  1376. if (unlikely(!m))
  1377. return -EIO;
  1378. read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
  1379. if (likely(read_record)) {
  1380. dm_bio_record(&read_record->details, bio);
  1381. map_context->ptr = read_record;
  1382. read_record->m = m;
  1383. }
  1384. map_bio(m, bio);
  1385. return DM_MAPIO_REMAPPED;
  1386. }
  1387. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  1388. int error, union map_info *map_context)
  1389. {
  1390. int rw = bio_rw(bio);
  1391. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1392. struct mirror *m = NULL;
  1393. struct dm_bio_details *bd = NULL;
  1394. struct dm_raid1_read_record *read_record = map_context->ptr;
  1395. /*
  1396. * We need to dec pending if this was a write.
  1397. */
  1398. if (rw == WRITE) {
  1399. rh_dec(&ms->rh, map_context->ll);
  1400. return error;
  1401. }
  1402. if (error == -EOPNOTSUPP)
  1403. goto out;
  1404. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  1405. goto out;
  1406. if (unlikely(error)) {
  1407. if (!read_record) {
  1408. /*
  1409. * There wasn't enough memory to record necessary
  1410. * information for a retry or there was no other
  1411. * mirror in-sync.
  1412. */
  1413. DMERR_LIMIT("Mirror read failed.");
  1414. return -EIO;
  1415. }
  1416. m = read_record->m;
  1417. DMERR("Mirror read failed from %s. Trying alternative device.",
  1418. m->dev->name);
  1419. fail_mirror(m, DM_RAID1_READ_ERROR);
  1420. /*
  1421. * A failed read is requeued for another attempt using an intact
  1422. * mirror.
  1423. */
  1424. if (default_ok(m) || mirror_available(ms, bio)) {
  1425. bd = &read_record->details;
  1426. dm_bio_restore(bd, bio);
  1427. mempool_free(read_record, ms->read_record_pool);
  1428. map_context->ptr = NULL;
  1429. queue_bio(ms, bio, rw);
  1430. return 1;
  1431. }
  1432. DMERR("All replicated volumes dead, failing I/O");
  1433. }
  1434. out:
  1435. if (read_record) {
  1436. mempool_free(read_record, ms->read_record_pool);
  1437. map_context->ptr = NULL;
  1438. }
  1439. return error;
  1440. }
  1441. static void mirror_presuspend(struct dm_target *ti)
  1442. {
  1443. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1444. struct dm_dirty_log *log = ms->rh.log;
  1445. atomic_set(&ms->suspend, 1);
  1446. /*
  1447. * We must finish up all the work that we've
  1448. * generated (i.e. recovery work).
  1449. */
  1450. rh_stop_recovery(&ms->rh);
  1451. wait_event(_kmirrord_recovery_stopped,
  1452. !atomic_read(&ms->rh.recovery_in_flight));
  1453. if (log->type->presuspend && log->type->presuspend(log))
  1454. /* FIXME: need better error handling */
  1455. DMWARN("log presuspend failed");
  1456. /*
  1457. * Now that recovery is complete/stopped and the
  1458. * delayed bios are queued, we need to wait for
  1459. * the worker thread to complete. This way,
  1460. * we know that all of our I/O has been pushed.
  1461. */
  1462. flush_workqueue(ms->kmirrord_wq);
  1463. }
  1464. static void mirror_postsuspend(struct dm_target *ti)
  1465. {
  1466. struct mirror_set *ms = ti->private;
  1467. struct dm_dirty_log *log = ms->rh.log;
  1468. if (log->type->postsuspend && log->type->postsuspend(log))
  1469. /* FIXME: need better error handling */
  1470. DMWARN("log postsuspend failed");
  1471. }
  1472. static void mirror_resume(struct dm_target *ti)
  1473. {
  1474. struct mirror_set *ms = ti->private;
  1475. struct dm_dirty_log *log = ms->rh.log;
  1476. atomic_set(&ms->suspend, 0);
  1477. if (log->type->resume && log->type->resume(log))
  1478. /* FIXME: need better error handling */
  1479. DMWARN("log resume failed");
  1480. rh_start_recovery(&ms->rh);
  1481. }
  1482. /*
  1483. * device_status_char
  1484. * @m: mirror device/leg we want the status of
  1485. *
  1486. * We return one character representing the most severe error
  1487. * we have encountered.
  1488. * A => Alive - No failures
  1489. * D => Dead - A write failure occurred leaving mirror out-of-sync
  1490. * S => Sync - A sychronization failure occurred, mirror out-of-sync
  1491. * R => Read - A read failure occurred, mirror data unaffected
  1492. *
  1493. * Returns: <char>
  1494. */
  1495. static char device_status_char(struct mirror *m)
  1496. {
  1497. if (!atomic_read(&(m->error_count)))
  1498. return 'A';
  1499. return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
  1500. (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  1501. (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  1502. }
  1503. static int mirror_status(struct dm_target *ti, status_type_t type,
  1504. char *result, unsigned int maxlen)
  1505. {
  1506. unsigned int m, sz = 0;
  1507. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1508. struct dm_dirty_log *log = ms->rh.log;
  1509. char buffer[ms->nr_mirrors + 1];
  1510. switch (type) {
  1511. case STATUSTYPE_INFO:
  1512. DMEMIT("%d ", ms->nr_mirrors);
  1513. for (m = 0; m < ms->nr_mirrors; m++) {
  1514. DMEMIT("%s ", ms->mirror[m].dev->name);
  1515. buffer[m] = device_status_char(&(ms->mirror[m]));
  1516. }
  1517. buffer[m] = '\0';
  1518. DMEMIT("%llu/%llu 1 %s ",
  1519. (unsigned long long)log->type->get_sync_count(ms->rh.log),
  1520. (unsigned long long)ms->nr_regions, buffer);
  1521. sz += log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
  1522. break;
  1523. case STATUSTYPE_TABLE:
  1524. sz = log->type->status(ms->rh.log, type, result, maxlen);
  1525. DMEMIT("%d", ms->nr_mirrors);
  1526. for (m = 0; m < ms->nr_mirrors; m++)
  1527. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1528. (unsigned long long)ms->mirror[m].offset);
  1529. if (ms->features & DM_RAID1_HANDLE_ERRORS)
  1530. DMEMIT(" 1 handle_errors");
  1531. }
  1532. return 0;
  1533. }
  1534. static struct target_type mirror_target = {
  1535. .name = "mirror",
  1536. .version = {1, 0, 20},
  1537. .module = THIS_MODULE,
  1538. .ctr = mirror_ctr,
  1539. .dtr = mirror_dtr,
  1540. .map = mirror_map,
  1541. .end_io = mirror_end_io,
  1542. .presuspend = mirror_presuspend,
  1543. .postsuspend = mirror_postsuspend,
  1544. .resume = mirror_resume,
  1545. .status = mirror_status,
  1546. };
  1547. static int __init dm_mirror_init(void)
  1548. {
  1549. int r;
  1550. r = dm_register_target(&mirror_target);
  1551. if (r < 0)
  1552. DMERR("Failed to register mirror target");
  1553. return r;
  1554. }
  1555. static void __exit dm_mirror_exit(void)
  1556. {
  1557. int r;
  1558. r = dm_unregister_target(&mirror_target);
  1559. if (r < 0)
  1560. DMERR("unregister failed %d", r);
  1561. }
  1562. /* Module hooks */
  1563. module_init(dm_mirror_init);
  1564. module_exit(dm_mirror_exit);
  1565. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1566. MODULE_AUTHOR("Joe Thornber");
  1567. MODULE_LICENSE("GPL");