dm-raid1.c 32 KB

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