dm-raid1.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
  700. r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
  701. flags, recovery_complete, reg);
  702. return r;
  703. }
  704. static void do_recovery(struct mirror_set *ms)
  705. {
  706. int r;
  707. struct region *reg;
  708. struct dm_dirty_log *log = ms->rh.log;
  709. /*
  710. * Start quiescing some regions.
  711. */
  712. rh_recovery_prepare(&ms->rh);
  713. /*
  714. * Copy any already quiesced regions.
  715. */
  716. while ((reg = rh_recovery_start(&ms->rh))) {
  717. r = recover(ms, reg);
  718. if (r)
  719. rh_recovery_end(reg, 0);
  720. }
  721. /*
  722. * Update the in sync flag.
  723. */
  724. if (!ms->in_sync &&
  725. (log->type->get_sync_count(log) == ms->nr_regions)) {
  726. /* the sync is complete */
  727. dm_table_event(ms->ti->table);
  728. ms->in_sync = 1;
  729. }
  730. }
  731. /*-----------------------------------------------------------------
  732. * Reads
  733. *---------------------------------------------------------------*/
  734. static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
  735. {
  736. struct mirror *m = get_default_mirror(ms);
  737. do {
  738. if (likely(!atomic_read(&m->error_count)))
  739. return m;
  740. if (m-- == ms->mirror)
  741. m += ms->nr_mirrors;
  742. } while (m != get_default_mirror(ms));
  743. return NULL;
  744. }
  745. static int default_ok(struct mirror *m)
  746. {
  747. struct mirror *default_mirror = get_default_mirror(m->ms);
  748. return !atomic_read(&default_mirror->error_count);
  749. }
  750. static int mirror_available(struct mirror_set *ms, struct bio *bio)
  751. {
  752. region_t region = bio_to_region(&ms->rh, bio);
  753. if (ms->rh.log->type->in_sync(ms->rh.log, region, 0))
  754. return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
  755. return 0;
  756. }
  757. /*
  758. * remap a buffer to a particular mirror.
  759. */
  760. static sector_t map_sector(struct mirror *m, struct bio *bio)
  761. {
  762. return m->offset + (bio->bi_sector - m->ms->ti->begin);
  763. }
  764. static void map_bio(struct mirror *m, struct bio *bio)
  765. {
  766. bio->bi_bdev = m->dev->bdev;
  767. bio->bi_sector = map_sector(m, bio);
  768. }
  769. static void map_region(struct dm_io_region *io, struct mirror *m,
  770. struct bio *bio)
  771. {
  772. io->bdev = m->dev->bdev;
  773. io->sector = map_sector(m, bio);
  774. io->count = bio->bi_size >> 9;
  775. }
  776. /*-----------------------------------------------------------------
  777. * Reads
  778. *---------------------------------------------------------------*/
  779. static void read_callback(unsigned long error, void *context)
  780. {
  781. struct bio *bio = context;
  782. struct mirror *m;
  783. m = bio_get_m(bio);
  784. bio_set_m(bio, NULL);
  785. if (likely(!error)) {
  786. bio_endio(bio, 0);
  787. return;
  788. }
  789. fail_mirror(m, DM_RAID1_READ_ERROR);
  790. if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
  791. DMWARN_LIMIT("Read failure on mirror device %s. "
  792. "Trying alternative device.",
  793. m->dev->name);
  794. queue_bio(m->ms, bio, bio_rw(bio));
  795. return;
  796. }
  797. DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
  798. m->dev->name);
  799. bio_endio(bio, -EIO);
  800. }
  801. /* Asynchronous read. */
  802. static void read_async_bio(struct mirror *m, struct bio *bio)
  803. {
  804. struct dm_io_region io;
  805. struct dm_io_request io_req = {
  806. .bi_rw = READ,
  807. .mem.type = DM_IO_BVEC,
  808. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  809. .notify.fn = read_callback,
  810. .notify.context = bio,
  811. .client = m->ms->io_client,
  812. };
  813. map_region(&io, m, bio);
  814. bio_set_m(bio, m);
  815. (void) dm_io(&io_req, 1, &io, NULL);
  816. }
  817. static void do_reads(struct mirror_set *ms, struct bio_list *reads)
  818. {
  819. region_t region;
  820. struct bio *bio;
  821. struct mirror *m;
  822. while ((bio = bio_list_pop(reads))) {
  823. region = bio_to_region(&ms->rh, bio);
  824. m = get_default_mirror(ms);
  825. /*
  826. * We can only read balance if the region is in sync.
  827. */
  828. if (likely(rh_in_sync(&ms->rh, region, 1)))
  829. m = choose_mirror(ms, bio->bi_sector);
  830. else if (m && atomic_read(&m->error_count))
  831. m = NULL;
  832. if (likely(m))
  833. read_async_bio(m, bio);
  834. else
  835. bio_endio(bio, -EIO);
  836. }
  837. }
  838. /*-----------------------------------------------------------------
  839. * Writes.
  840. *
  841. * We do different things with the write io depending on the
  842. * state of the region that it's in:
  843. *
  844. * SYNC: increment pending, use kcopyd to write to *all* mirrors
  845. * RECOVERING: delay the io until recovery completes
  846. * NOSYNC: increment pending, just write to the default mirror
  847. *---------------------------------------------------------------*/
  848. /* __bio_mark_nosync
  849. * @ms
  850. * @bio
  851. * @done
  852. * @error
  853. *
  854. * The bio was written on some mirror(s) but failed on other mirror(s).
  855. * We can successfully endio the bio but should avoid the region being
  856. * marked clean by setting the state RH_NOSYNC.
  857. *
  858. * This function is _not_ safe in interrupt context!
  859. */
  860. static void __bio_mark_nosync(struct mirror_set *ms,
  861. struct bio *bio, unsigned done, int error)
  862. {
  863. unsigned long flags;
  864. struct region_hash *rh = &ms->rh;
  865. struct dm_dirty_log *log = ms->rh.log;
  866. struct region *reg;
  867. region_t region = bio_to_region(rh, bio);
  868. int recovering = 0;
  869. /* We must inform the log that the sync count has changed. */
  870. log->type->set_region_sync(log, region, 0);
  871. ms->in_sync = 0;
  872. read_lock(&rh->hash_lock);
  873. reg = __rh_find(rh, region);
  874. read_unlock(&rh->hash_lock);
  875. /* region hash entry should exist because write was in-flight */
  876. BUG_ON(!reg);
  877. BUG_ON(!list_empty(&reg->list));
  878. spin_lock_irqsave(&rh->region_lock, flags);
  879. /*
  880. * Possible cases:
  881. * 1) RH_DIRTY
  882. * 2) RH_NOSYNC: was dirty, other preceeding writes failed
  883. * 3) RH_RECOVERING: flushing pending writes
  884. * Either case, the region should have not been connected to list.
  885. */
  886. recovering = (reg->state == RH_RECOVERING);
  887. reg->state = RH_NOSYNC;
  888. BUG_ON(!list_empty(&reg->list));
  889. spin_unlock_irqrestore(&rh->region_lock, flags);
  890. bio_endio(bio, error);
  891. if (recovering)
  892. complete_resync_work(reg, 0);
  893. }
  894. static void write_callback(unsigned long error, void *context)
  895. {
  896. unsigned i, ret = 0;
  897. struct bio *bio = (struct bio *) context;
  898. struct mirror_set *ms;
  899. int uptodate = 0;
  900. int should_wake = 0;
  901. unsigned long flags;
  902. ms = bio_get_m(bio)->ms;
  903. bio_set_m(bio, NULL);
  904. /*
  905. * NOTE: We don't decrement the pending count here,
  906. * instead it is done by the targets endio function.
  907. * This way we handle both writes to SYNC and NOSYNC
  908. * regions with the same code.
  909. */
  910. if (likely(!error))
  911. goto out;
  912. for (i = 0; i < ms->nr_mirrors; i++)
  913. if (test_bit(i, &error))
  914. fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
  915. else
  916. uptodate = 1;
  917. if (unlikely(!uptodate)) {
  918. DMERR("All replicated volumes dead, failing I/O");
  919. /* None of the writes succeeded, fail the I/O. */
  920. ret = -EIO;
  921. } else if (errors_handled(ms)) {
  922. /*
  923. * Need to raise event. Since raising
  924. * events can block, we need to do it in
  925. * the main thread.
  926. */
  927. spin_lock_irqsave(&ms->lock, flags);
  928. if (!ms->failures.head)
  929. should_wake = 1;
  930. bio_list_add(&ms->failures, bio);
  931. spin_unlock_irqrestore(&ms->lock, flags);
  932. if (should_wake)
  933. wake(ms);
  934. return;
  935. }
  936. out:
  937. bio_endio(bio, ret);
  938. }
  939. static void do_write(struct mirror_set *ms, struct bio *bio)
  940. {
  941. unsigned int i;
  942. struct dm_io_region io[ms->nr_mirrors], *dest = io;
  943. struct mirror *m;
  944. struct dm_io_request io_req = {
  945. .bi_rw = WRITE,
  946. .mem.type = DM_IO_BVEC,
  947. .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
  948. .notify.fn = write_callback,
  949. .notify.context = bio,
  950. .client = ms->io_client,
  951. };
  952. for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
  953. map_region(dest++, m, bio);
  954. /*
  955. * Use default mirror because we only need it to retrieve the reference
  956. * to the mirror set in write_callback().
  957. */
  958. bio_set_m(bio, get_default_mirror(ms));
  959. (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
  960. }
  961. static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  962. {
  963. int state;
  964. struct bio *bio;
  965. struct bio_list sync, nosync, recover, *this_list = NULL;
  966. if (!writes->head)
  967. return;
  968. /*
  969. * Classify each write.
  970. */
  971. bio_list_init(&sync);
  972. bio_list_init(&nosync);
  973. bio_list_init(&recover);
  974. while ((bio = bio_list_pop(writes))) {
  975. state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
  976. switch (state) {
  977. case RH_CLEAN:
  978. case RH_DIRTY:
  979. this_list = &sync;
  980. break;
  981. case RH_NOSYNC:
  982. this_list = &nosync;
  983. break;
  984. case RH_RECOVERING:
  985. this_list = &recover;
  986. break;
  987. }
  988. bio_list_add(this_list, bio);
  989. }
  990. /*
  991. * Increment the pending counts for any regions that will
  992. * be written to (writes to recover regions are going to
  993. * be delayed).
  994. */
  995. rh_inc_pending(&ms->rh, &sync);
  996. rh_inc_pending(&ms->rh, &nosync);
  997. ms->log_failure = rh_flush(&ms->rh) ? 1 : 0;
  998. /*
  999. * Dispatch io.
  1000. */
  1001. if (unlikely(ms->log_failure)) {
  1002. spin_lock_irq(&ms->lock);
  1003. bio_list_merge(&ms->failures, &sync);
  1004. spin_unlock_irq(&ms->lock);
  1005. wake(ms);
  1006. } else
  1007. while ((bio = bio_list_pop(&sync)))
  1008. do_write(ms, bio);
  1009. while ((bio = bio_list_pop(&recover)))
  1010. rh_delay(&ms->rh, bio);
  1011. while ((bio = bio_list_pop(&nosync))) {
  1012. map_bio(get_default_mirror(ms), bio);
  1013. generic_make_request(bio);
  1014. }
  1015. }
  1016. static void do_failures(struct mirror_set *ms, struct bio_list *failures)
  1017. {
  1018. struct bio *bio;
  1019. if (!failures->head)
  1020. return;
  1021. if (!ms->log_failure) {
  1022. while ((bio = bio_list_pop(failures)))
  1023. __bio_mark_nosync(ms, bio, bio->bi_size, 0);
  1024. return;
  1025. }
  1026. /*
  1027. * If the log has failed, unattempted writes are being
  1028. * put on the failures list. We can't issue those writes
  1029. * until a log has been marked, so we must store them.
  1030. *
  1031. * If a 'noflush' suspend is in progress, we can requeue
  1032. * the I/O's to the core. This give userspace a chance
  1033. * to reconfigure the mirror, at which point the core
  1034. * will reissue the writes. If the 'noflush' flag is
  1035. * not set, we have no choice but to return errors.
  1036. *
  1037. * Some writes on the failures list may have been
  1038. * submitted before the log failure and represent a
  1039. * failure to write to one of the devices. It is ok
  1040. * for us to treat them the same and requeue them
  1041. * as well.
  1042. */
  1043. if (dm_noflush_suspending(ms->ti)) {
  1044. while ((bio = bio_list_pop(failures)))
  1045. bio_endio(bio, DM_ENDIO_REQUEUE);
  1046. return;
  1047. }
  1048. if (atomic_read(&ms->suspend)) {
  1049. while ((bio = bio_list_pop(failures)))
  1050. bio_endio(bio, -EIO);
  1051. return;
  1052. }
  1053. spin_lock_irq(&ms->lock);
  1054. bio_list_merge(&ms->failures, failures);
  1055. spin_unlock_irq(&ms->lock);
  1056. delayed_wake(ms);
  1057. }
  1058. static void trigger_event(struct work_struct *work)
  1059. {
  1060. struct mirror_set *ms =
  1061. container_of(work, struct mirror_set, trigger_event);
  1062. dm_table_event(ms->ti->table);
  1063. }
  1064. /*-----------------------------------------------------------------
  1065. * kmirrord
  1066. *---------------------------------------------------------------*/
  1067. static void do_mirror(struct work_struct *work)
  1068. {
  1069. struct mirror_set *ms =container_of(work, struct mirror_set,
  1070. kmirrord_work);
  1071. struct bio_list reads, writes, failures;
  1072. unsigned long flags;
  1073. spin_lock_irqsave(&ms->lock, flags);
  1074. reads = ms->reads;
  1075. writes = ms->writes;
  1076. failures = ms->failures;
  1077. bio_list_init(&ms->reads);
  1078. bio_list_init(&ms->writes);
  1079. bio_list_init(&ms->failures);
  1080. spin_unlock_irqrestore(&ms->lock, flags);
  1081. rh_update_states(&ms->rh);
  1082. do_recovery(ms);
  1083. do_reads(ms, &reads);
  1084. do_writes(ms, &writes);
  1085. do_failures(ms, &failures);
  1086. dm_table_unplug_all(ms->ti->table);
  1087. }
  1088. /*-----------------------------------------------------------------
  1089. * Target functions
  1090. *---------------------------------------------------------------*/
  1091. static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  1092. uint32_t region_size,
  1093. struct dm_target *ti,
  1094. struct dm_dirty_log *dl)
  1095. {
  1096. size_t len;
  1097. struct mirror_set *ms = NULL;
  1098. if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
  1099. return NULL;
  1100. len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
  1101. ms = kzalloc(len, GFP_KERNEL);
  1102. if (!ms) {
  1103. ti->error = "Cannot allocate mirror context";
  1104. return NULL;
  1105. }
  1106. spin_lock_init(&ms->lock);
  1107. ms->ti = ti;
  1108. ms->nr_mirrors = nr_mirrors;
  1109. ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  1110. ms->in_sync = 0;
  1111. ms->log_failure = 0;
  1112. atomic_set(&ms->suspend, 0);
  1113. atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
  1114. len = sizeof(struct dm_raid1_read_record);
  1115. ms->read_record_pool = mempool_create_kmalloc_pool(MIN_READ_RECORDS,
  1116. len);
  1117. if (!ms->read_record_pool) {
  1118. ti->error = "Error creating mirror read_record_pool";
  1119. kfree(ms);
  1120. return NULL;
  1121. }
  1122. ms->io_client = dm_io_client_create(DM_IO_PAGES);
  1123. if (IS_ERR(ms->io_client)) {
  1124. ti->error = "Error creating dm_io client";
  1125. mempool_destroy(ms->read_record_pool);
  1126. kfree(ms);
  1127. return NULL;
  1128. }
  1129. if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
  1130. ti->error = "Error creating dirty region hash";
  1131. dm_io_client_destroy(ms->io_client);
  1132. mempool_destroy(ms->read_record_pool);
  1133. kfree(ms);
  1134. return NULL;
  1135. }
  1136. return ms;
  1137. }
  1138. static void free_context(struct mirror_set *ms, struct dm_target *ti,
  1139. unsigned int m)
  1140. {
  1141. while (m--)
  1142. dm_put_device(ti, ms->mirror[m].dev);
  1143. dm_io_client_destroy(ms->io_client);
  1144. rh_exit(&ms->rh);
  1145. mempool_destroy(ms->read_record_pool);
  1146. kfree(ms);
  1147. }
  1148. static inline int _check_region_size(struct dm_target *ti, uint32_t size)
  1149. {
  1150. return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
  1151. size > ti->len);
  1152. }
  1153. static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  1154. unsigned int mirror, char **argv)
  1155. {
  1156. unsigned long long offset;
  1157. if (sscanf(argv[1], "%llu", &offset) != 1) {
  1158. ti->error = "Invalid offset";
  1159. return -EINVAL;
  1160. }
  1161. if (dm_get_device(ti, argv[0], offset, ti->len,
  1162. dm_table_get_mode(ti->table),
  1163. &ms->mirror[mirror].dev)) {
  1164. ti->error = "Device lookup failure";
  1165. return -ENXIO;
  1166. }
  1167. ms->mirror[mirror].ms = ms;
  1168. atomic_set(&(ms->mirror[mirror].error_count), 0);
  1169. ms->mirror[mirror].error_type = 0;
  1170. ms->mirror[mirror].offset = offset;
  1171. return 0;
  1172. }
  1173. /*
  1174. * Create dirty log: log_type #log_params <log_params>
  1175. */
  1176. static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
  1177. unsigned int argc, char **argv,
  1178. unsigned int *args_used)
  1179. {
  1180. unsigned int param_count;
  1181. struct dm_dirty_log *dl;
  1182. if (argc < 2) {
  1183. ti->error = "Insufficient mirror log arguments";
  1184. return NULL;
  1185. }
  1186. if (sscanf(argv[1], "%u", &param_count) != 1) {
  1187. ti->error = "Invalid mirror log argument count";
  1188. return NULL;
  1189. }
  1190. *args_used = 2 + param_count;
  1191. if (argc < *args_used) {
  1192. ti->error = "Insufficient mirror log arguments";
  1193. return NULL;
  1194. }
  1195. dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
  1196. if (!dl) {
  1197. ti->error = "Error creating mirror dirty log";
  1198. return NULL;
  1199. }
  1200. if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
  1201. ti->error = "Invalid region size";
  1202. dm_dirty_log_destroy(dl);
  1203. return NULL;
  1204. }
  1205. return dl;
  1206. }
  1207. static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
  1208. unsigned *args_used)
  1209. {
  1210. unsigned num_features;
  1211. struct dm_target *ti = ms->ti;
  1212. *args_used = 0;
  1213. if (!argc)
  1214. return 0;
  1215. if (sscanf(argv[0], "%u", &num_features) != 1) {
  1216. ti->error = "Invalid number of features";
  1217. return -EINVAL;
  1218. }
  1219. argc--;
  1220. argv++;
  1221. (*args_used)++;
  1222. if (num_features > argc) {
  1223. ti->error = "Not enough arguments to support feature count";
  1224. return -EINVAL;
  1225. }
  1226. if (!strcmp("handle_errors", argv[0]))
  1227. ms->features |= DM_RAID1_HANDLE_ERRORS;
  1228. else {
  1229. ti->error = "Unrecognised feature requested";
  1230. return -EINVAL;
  1231. }
  1232. (*args_used)++;
  1233. return 0;
  1234. }
  1235. /*
  1236. * Construct a mirror mapping:
  1237. *
  1238. * log_type #log_params <log_params>
  1239. * #mirrors [mirror_path offset]{2,}
  1240. * [#features <features>]
  1241. *
  1242. * log_type is "core" or "disk"
  1243. * #log_params is between 1 and 3
  1244. *
  1245. * If present, features must be "handle_errors".
  1246. */
  1247. static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1248. {
  1249. int r;
  1250. unsigned int nr_mirrors, m, args_used;
  1251. struct mirror_set *ms;
  1252. struct dm_dirty_log *dl;
  1253. dl = create_dirty_log(ti, argc, argv, &args_used);
  1254. if (!dl)
  1255. return -EINVAL;
  1256. argv += args_used;
  1257. argc -= args_used;
  1258. if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
  1259. nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
  1260. ti->error = "Invalid number of mirrors";
  1261. dm_dirty_log_destroy(dl);
  1262. return -EINVAL;
  1263. }
  1264. argv++, argc--;
  1265. if (argc < nr_mirrors * 2) {
  1266. ti->error = "Too few mirror arguments";
  1267. dm_dirty_log_destroy(dl);
  1268. return -EINVAL;
  1269. }
  1270. ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  1271. if (!ms) {
  1272. dm_dirty_log_destroy(dl);
  1273. return -ENOMEM;
  1274. }
  1275. /* Get the mirror parameter sets */
  1276. for (m = 0; m < nr_mirrors; m++) {
  1277. r = get_mirror(ms, ti, m, argv);
  1278. if (r) {
  1279. free_context(ms, ti, m);
  1280. return r;
  1281. }
  1282. argv += 2;
  1283. argc -= 2;
  1284. }
  1285. ti->private = ms;
  1286. ti->split_io = ms->rh.region_size;
  1287. ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
  1288. if (!ms->kmirrord_wq) {
  1289. DMERR("couldn't start kmirrord");
  1290. r = -ENOMEM;
  1291. goto err_free_context;
  1292. }
  1293. INIT_WORK(&ms->kmirrord_work, do_mirror);
  1294. init_timer(&ms->timer);
  1295. ms->timer_pending = 0;
  1296. INIT_WORK(&ms->trigger_event, trigger_event);
  1297. r = parse_features(ms, argc, argv, &args_used);
  1298. if (r)
  1299. goto err_destroy_wq;
  1300. argv += args_used;
  1301. argc -= args_used;
  1302. /*
  1303. * Any read-balancing addition depends on the
  1304. * DM_RAID1_HANDLE_ERRORS flag being present.
  1305. * This is because the decision to balance depends
  1306. * on the sync state of a region. If the above
  1307. * flag is not present, we ignore errors; and
  1308. * the sync state may be inaccurate.
  1309. */
  1310. if (argc) {
  1311. ti->error = "Too many mirror arguments";
  1312. r = -EINVAL;
  1313. goto err_destroy_wq;
  1314. }
  1315. r = dm_kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
  1316. if (r)
  1317. goto err_destroy_wq;
  1318. wake(ms);
  1319. return 0;
  1320. err_destroy_wq:
  1321. destroy_workqueue(ms->kmirrord_wq);
  1322. err_free_context:
  1323. free_context(ms, ti, ms->nr_mirrors);
  1324. return r;
  1325. }
  1326. static void mirror_dtr(struct dm_target *ti)
  1327. {
  1328. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1329. del_timer_sync(&ms->timer);
  1330. flush_workqueue(ms->kmirrord_wq);
  1331. dm_kcopyd_client_destroy(ms->kcopyd_client);
  1332. destroy_workqueue(ms->kmirrord_wq);
  1333. free_context(ms, ti, ms->nr_mirrors);
  1334. }
  1335. static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
  1336. {
  1337. unsigned long flags;
  1338. int should_wake = 0;
  1339. struct bio_list *bl;
  1340. bl = (rw == WRITE) ? &ms->writes : &ms->reads;
  1341. spin_lock_irqsave(&ms->lock, flags);
  1342. should_wake = !(bl->head);
  1343. bio_list_add(bl, bio);
  1344. spin_unlock_irqrestore(&ms->lock, flags);
  1345. if (should_wake)
  1346. wake(ms);
  1347. }
  1348. /*
  1349. * Mirror mapping function
  1350. */
  1351. static int mirror_map(struct dm_target *ti, struct bio *bio,
  1352. union map_info *map_context)
  1353. {
  1354. int r, rw = bio_rw(bio);
  1355. struct mirror *m;
  1356. struct mirror_set *ms = ti->private;
  1357. struct dm_raid1_read_record *read_record = NULL;
  1358. if (rw == WRITE) {
  1359. /* Save region for mirror_end_io() handler */
  1360. map_context->ll = bio_to_region(&ms->rh, bio);
  1361. queue_bio(ms, bio, rw);
  1362. return DM_MAPIO_SUBMITTED;
  1363. }
  1364. r = ms->rh.log->type->in_sync(ms->rh.log,
  1365. bio_to_region(&ms->rh, bio), 0);
  1366. if (r < 0 && r != -EWOULDBLOCK)
  1367. return r;
  1368. /*
  1369. * If region is not in-sync queue the bio.
  1370. */
  1371. if (!r || (r == -EWOULDBLOCK)) {
  1372. if (rw == READA)
  1373. return -EWOULDBLOCK;
  1374. queue_bio(ms, bio, rw);
  1375. return DM_MAPIO_SUBMITTED;
  1376. }
  1377. /*
  1378. * The region is in-sync and we can perform reads directly.
  1379. * Store enough information so we can retry if it fails.
  1380. */
  1381. m = choose_mirror(ms, bio->bi_sector);
  1382. if (unlikely(!m))
  1383. return -EIO;
  1384. read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
  1385. if (likely(read_record)) {
  1386. dm_bio_record(&read_record->details, bio);
  1387. map_context->ptr = read_record;
  1388. read_record->m = m;
  1389. }
  1390. map_bio(m, bio);
  1391. return DM_MAPIO_REMAPPED;
  1392. }
  1393. static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  1394. int error, union map_info *map_context)
  1395. {
  1396. int rw = bio_rw(bio);
  1397. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1398. struct mirror *m = NULL;
  1399. struct dm_bio_details *bd = NULL;
  1400. struct dm_raid1_read_record *read_record = map_context->ptr;
  1401. /*
  1402. * We need to dec pending if this was a write.
  1403. */
  1404. if (rw == WRITE) {
  1405. rh_dec(&ms->rh, map_context->ll);
  1406. return error;
  1407. }
  1408. if (error == -EOPNOTSUPP)
  1409. goto out;
  1410. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  1411. goto out;
  1412. if (unlikely(error)) {
  1413. if (!read_record) {
  1414. /*
  1415. * There wasn't enough memory to record necessary
  1416. * information for a retry or there was no other
  1417. * mirror in-sync.
  1418. */
  1419. DMERR_LIMIT("Mirror read failed.");
  1420. return -EIO;
  1421. }
  1422. m = read_record->m;
  1423. DMERR("Mirror read failed from %s. Trying alternative device.",
  1424. m->dev->name);
  1425. fail_mirror(m, DM_RAID1_READ_ERROR);
  1426. /*
  1427. * A failed read is requeued for another attempt using an intact
  1428. * mirror.
  1429. */
  1430. if (default_ok(m) || mirror_available(ms, bio)) {
  1431. bd = &read_record->details;
  1432. dm_bio_restore(bd, bio);
  1433. mempool_free(read_record, ms->read_record_pool);
  1434. map_context->ptr = NULL;
  1435. queue_bio(ms, bio, rw);
  1436. return 1;
  1437. }
  1438. DMERR("All replicated volumes dead, failing I/O");
  1439. }
  1440. out:
  1441. if (read_record) {
  1442. mempool_free(read_record, ms->read_record_pool);
  1443. map_context->ptr = NULL;
  1444. }
  1445. return error;
  1446. }
  1447. static void mirror_presuspend(struct dm_target *ti)
  1448. {
  1449. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1450. struct dm_dirty_log *log = ms->rh.log;
  1451. atomic_set(&ms->suspend, 1);
  1452. /*
  1453. * We must finish up all the work that we've
  1454. * generated (i.e. recovery work).
  1455. */
  1456. rh_stop_recovery(&ms->rh);
  1457. wait_event(_kmirrord_recovery_stopped,
  1458. !atomic_read(&ms->rh.recovery_in_flight));
  1459. if (log->type->presuspend && log->type->presuspend(log))
  1460. /* FIXME: need better error handling */
  1461. DMWARN("log presuspend failed");
  1462. /*
  1463. * Now that recovery is complete/stopped and the
  1464. * delayed bios are queued, we need to wait for
  1465. * the worker thread to complete. This way,
  1466. * we know that all of our I/O has been pushed.
  1467. */
  1468. flush_workqueue(ms->kmirrord_wq);
  1469. }
  1470. static void mirror_postsuspend(struct dm_target *ti)
  1471. {
  1472. struct mirror_set *ms = ti->private;
  1473. struct dm_dirty_log *log = ms->rh.log;
  1474. if (log->type->postsuspend && log->type->postsuspend(log))
  1475. /* FIXME: need better error handling */
  1476. DMWARN("log postsuspend failed");
  1477. }
  1478. static void mirror_resume(struct dm_target *ti)
  1479. {
  1480. struct mirror_set *ms = ti->private;
  1481. struct dm_dirty_log *log = ms->rh.log;
  1482. atomic_set(&ms->suspend, 0);
  1483. if (log->type->resume && log->type->resume(log))
  1484. /* FIXME: need better error handling */
  1485. DMWARN("log resume failed");
  1486. rh_start_recovery(&ms->rh);
  1487. }
  1488. /*
  1489. * device_status_char
  1490. * @m: mirror device/leg we want the status of
  1491. *
  1492. * We return one character representing the most severe error
  1493. * we have encountered.
  1494. * A => Alive - No failures
  1495. * D => Dead - A write failure occurred leaving mirror out-of-sync
  1496. * S => Sync - A sychronization failure occurred, mirror out-of-sync
  1497. * R => Read - A read failure occurred, mirror data unaffected
  1498. *
  1499. * Returns: <char>
  1500. */
  1501. static char device_status_char(struct mirror *m)
  1502. {
  1503. if (!atomic_read(&(m->error_count)))
  1504. return 'A';
  1505. return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
  1506. (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  1507. (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  1508. }
  1509. static int mirror_status(struct dm_target *ti, status_type_t type,
  1510. char *result, unsigned int maxlen)
  1511. {
  1512. unsigned int m, sz = 0;
  1513. struct mirror_set *ms = (struct mirror_set *) ti->private;
  1514. struct dm_dirty_log *log = ms->rh.log;
  1515. char buffer[ms->nr_mirrors + 1];
  1516. switch (type) {
  1517. case STATUSTYPE_INFO:
  1518. DMEMIT("%d ", ms->nr_mirrors);
  1519. for (m = 0; m < ms->nr_mirrors; m++) {
  1520. DMEMIT("%s ", ms->mirror[m].dev->name);
  1521. buffer[m] = device_status_char(&(ms->mirror[m]));
  1522. }
  1523. buffer[m] = '\0';
  1524. DMEMIT("%llu/%llu 1 %s ",
  1525. (unsigned long long)log->type->get_sync_count(ms->rh.log),
  1526. (unsigned long long)ms->nr_regions, buffer);
  1527. sz += log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
  1528. break;
  1529. case STATUSTYPE_TABLE:
  1530. sz = log->type->status(ms->rh.log, type, result, maxlen);
  1531. DMEMIT("%d", ms->nr_mirrors);
  1532. for (m = 0; m < ms->nr_mirrors; m++)
  1533. DMEMIT(" %s %llu", ms->mirror[m].dev->name,
  1534. (unsigned long long)ms->mirror[m].offset);
  1535. if (ms->features & DM_RAID1_HANDLE_ERRORS)
  1536. DMEMIT(" 1 handle_errors");
  1537. }
  1538. return 0;
  1539. }
  1540. static struct target_type mirror_target = {
  1541. .name = "mirror",
  1542. .version = {1, 0, 20},
  1543. .module = THIS_MODULE,
  1544. .ctr = mirror_ctr,
  1545. .dtr = mirror_dtr,
  1546. .map = mirror_map,
  1547. .end_io = mirror_end_io,
  1548. .presuspend = mirror_presuspend,
  1549. .postsuspend = mirror_postsuspend,
  1550. .resume = mirror_resume,
  1551. .status = mirror_status,
  1552. };
  1553. static int __init dm_mirror_init(void)
  1554. {
  1555. int r;
  1556. r = dm_register_target(&mirror_target);
  1557. if (r < 0)
  1558. DMERR("Failed to register mirror target");
  1559. return r;
  1560. }
  1561. static void __exit dm_mirror_exit(void)
  1562. {
  1563. int r;
  1564. r = dm_unregister_target(&mirror_target);
  1565. if (r < 0)
  1566. DMERR("unregister failed %d", r);
  1567. }
  1568. /* Module hooks */
  1569. module_init(dm_mirror_init);
  1570. module_exit(dm_mirror_exit);
  1571. MODULE_DESCRIPTION(DM_NAME " mirror target");
  1572. MODULE_AUTHOR("Joe Thornber");
  1573. MODULE_LICENSE("GPL");