dm-raid1.c 32 KB

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