dm-raid1.c 32 KB

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