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