dm-raid1.c 44 KB

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