raid5.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. *
  6. * RAID-5 management functions.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * (for example /usr/src/linux/COPYING); if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/raid/raid5.h>
  21. #include <linux/highmem.h>
  22. #include <linux/bitops.h>
  23. #include <asm/atomic.h>
  24. #include <linux/raid/bitmap.h>
  25. /*
  26. * Stripe cache
  27. */
  28. #define NR_STRIPES 256
  29. #define STRIPE_SIZE PAGE_SIZE
  30. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  31. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  32. #define IO_THRESHOLD 1
  33. #define HASH_PAGES 1
  34. #define HASH_PAGES_ORDER 0
  35. #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
  36. #define HASH_MASK (NR_HASH - 1)
  37. #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
  38. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  39. * order without overlap. There may be several bio's per stripe+device, and
  40. * a bio could span several devices.
  41. * When walking this list for a particular stripe+device, we must never proceed
  42. * beyond a bio that extends past this device, as the next bio might no longer
  43. * be valid.
  44. * This macro is used to determine the 'next' bio in the list, given the sector
  45. * of the current stripe+device
  46. */
  47. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  48. /*
  49. * The following can be used to debug the driver
  50. */
  51. #define RAID5_DEBUG 0
  52. #define RAID5_PARANOIA 1
  53. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  54. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  55. #else
  56. # define CHECK_DEVLOCK()
  57. #endif
  58. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  59. #if RAID5_DEBUG
  60. #define inline
  61. #define __inline__
  62. #endif
  63. static void print_raid5_conf (raid5_conf_t *conf);
  64. static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  65. {
  66. if (atomic_dec_and_test(&sh->count)) {
  67. if (!list_empty(&sh->lru))
  68. BUG();
  69. if (atomic_read(&conf->active_stripes)==0)
  70. BUG();
  71. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  72. if (test_bit(STRIPE_DELAYED, &sh->state))
  73. list_add_tail(&sh->lru, &conf->delayed_list);
  74. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  75. conf->seq_write == sh->bm_seq)
  76. list_add_tail(&sh->lru, &conf->bitmap_list);
  77. else {
  78. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  79. list_add_tail(&sh->lru, &conf->handle_list);
  80. }
  81. md_wakeup_thread(conf->mddev->thread);
  82. } else {
  83. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  84. atomic_dec(&conf->preread_active_stripes);
  85. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  86. md_wakeup_thread(conf->mddev->thread);
  87. }
  88. list_add_tail(&sh->lru, &conf->inactive_list);
  89. atomic_dec(&conf->active_stripes);
  90. if (!conf->inactive_blocked ||
  91. atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
  92. wake_up(&conf->wait_for_stripe);
  93. }
  94. }
  95. }
  96. static void release_stripe(struct stripe_head *sh)
  97. {
  98. raid5_conf_t *conf = sh->raid_conf;
  99. unsigned long flags;
  100. spin_lock_irqsave(&conf->device_lock, flags);
  101. __release_stripe(conf, sh);
  102. spin_unlock_irqrestore(&conf->device_lock, flags);
  103. }
  104. static void remove_hash(struct stripe_head *sh)
  105. {
  106. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  107. if (sh->hash_pprev) {
  108. if (sh->hash_next)
  109. sh->hash_next->hash_pprev = sh->hash_pprev;
  110. *sh->hash_pprev = sh->hash_next;
  111. sh->hash_pprev = NULL;
  112. }
  113. }
  114. static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  115. {
  116. struct stripe_head **shp = &stripe_hash(conf, sh->sector);
  117. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  118. CHECK_DEVLOCK();
  119. if ((sh->hash_next = *shp) != NULL)
  120. (*shp)->hash_pprev = &sh->hash_next;
  121. *shp = sh;
  122. sh->hash_pprev = shp;
  123. }
  124. /* find an idle stripe, make sure it is unhashed, and return it. */
  125. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  126. {
  127. struct stripe_head *sh = NULL;
  128. struct list_head *first;
  129. CHECK_DEVLOCK();
  130. if (list_empty(&conf->inactive_list))
  131. goto out;
  132. first = conf->inactive_list.next;
  133. sh = list_entry(first, struct stripe_head, lru);
  134. list_del_init(first);
  135. remove_hash(sh);
  136. atomic_inc(&conf->active_stripes);
  137. out:
  138. return sh;
  139. }
  140. static void shrink_buffers(struct stripe_head *sh, int num)
  141. {
  142. struct page *p;
  143. int i;
  144. for (i=0; i<num ; i++) {
  145. p = sh->dev[i].page;
  146. if (!p)
  147. continue;
  148. sh->dev[i].page = NULL;
  149. page_cache_release(p);
  150. }
  151. }
  152. static int grow_buffers(struct stripe_head *sh, int num)
  153. {
  154. int i;
  155. for (i=0; i<num; i++) {
  156. struct page *page;
  157. if (!(page = alloc_page(GFP_KERNEL))) {
  158. return 1;
  159. }
  160. sh->dev[i].page = page;
  161. }
  162. return 0;
  163. }
  164. static void raid5_build_block (struct stripe_head *sh, int i);
  165. static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
  166. {
  167. raid5_conf_t *conf = sh->raid_conf;
  168. int disks = conf->raid_disks, i;
  169. if (atomic_read(&sh->count) != 0)
  170. BUG();
  171. if (test_bit(STRIPE_HANDLE, &sh->state))
  172. BUG();
  173. CHECK_DEVLOCK();
  174. PRINTK("init_stripe called, stripe %llu\n",
  175. (unsigned long long)sh->sector);
  176. remove_hash(sh);
  177. sh->sector = sector;
  178. sh->pd_idx = pd_idx;
  179. sh->state = 0;
  180. for (i=disks; i--; ) {
  181. struct r5dev *dev = &sh->dev[i];
  182. if (dev->toread || dev->towrite || dev->written ||
  183. test_bit(R5_LOCKED, &dev->flags)) {
  184. printk("sector=%llx i=%d %p %p %p %d\n",
  185. (unsigned long long)sh->sector, i, dev->toread,
  186. dev->towrite, dev->written,
  187. test_bit(R5_LOCKED, &dev->flags));
  188. BUG();
  189. }
  190. dev->flags = 0;
  191. raid5_build_block(sh, i);
  192. }
  193. insert_hash(conf, sh);
  194. }
  195. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector)
  196. {
  197. struct stripe_head *sh;
  198. CHECK_DEVLOCK();
  199. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  200. for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
  201. if (sh->sector == sector)
  202. return sh;
  203. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  204. return NULL;
  205. }
  206. static void unplug_slaves(mddev_t *mddev);
  207. static void raid5_unplug_device(request_queue_t *q);
  208. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector,
  209. int pd_idx, int noblock)
  210. {
  211. struct stripe_head *sh;
  212. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  213. spin_lock_irq(&conf->device_lock);
  214. do {
  215. wait_event_lock_irq(conf->wait_for_stripe,
  216. conf->quiesce == 0,
  217. conf->device_lock, /* nothing */);
  218. sh = __find_stripe(conf, sector);
  219. if (!sh) {
  220. if (!conf->inactive_blocked)
  221. sh = get_free_stripe(conf);
  222. if (noblock && sh == NULL)
  223. break;
  224. if (!sh) {
  225. conf->inactive_blocked = 1;
  226. wait_event_lock_irq(conf->wait_for_stripe,
  227. !list_empty(&conf->inactive_list) &&
  228. (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
  229. || !conf->inactive_blocked),
  230. conf->device_lock,
  231. unplug_slaves(conf->mddev);
  232. );
  233. conf->inactive_blocked = 0;
  234. } else
  235. init_stripe(sh, sector, pd_idx);
  236. } else {
  237. if (atomic_read(&sh->count)) {
  238. if (!list_empty(&sh->lru))
  239. BUG();
  240. } else {
  241. if (!test_bit(STRIPE_HANDLE, &sh->state))
  242. atomic_inc(&conf->active_stripes);
  243. if (list_empty(&sh->lru))
  244. BUG();
  245. list_del_init(&sh->lru);
  246. }
  247. }
  248. } while (sh == NULL);
  249. if (sh)
  250. atomic_inc(&sh->count);
  251. spin_unlock_irq(&conf->device_lock);
  252. return sh;
  253. }
  254. static int grow_stripes(raid5_conf_t *conf, int num)
  255. {
  256. struct stripe_head *sh;
  257. kmem_cache_t *sc;
  258. int devs = conf->raid_disks;
  259. sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
  260. sc = kmem_cache_create(conf->cache_name,
  261. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  262. 0, 0, NULL, NULL);
  263. if (!sc)
  264. return 1;
  265. conf->slab_cache = sc;
  266. while (num--) {
  267. sh = kmem_cache_alloc(sc, GFP_KERNEL);
  268. if (!sh)
  269. return 1;
  270. memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
  271. sh->raid_conf = conf;
  272. spin_lock_init(&sh->lock);
  273. if (grow_buffers(sh, conf->raid_disks)) {
  274. shrink_buffers(sh, conf->raid_disks);
  275. kmem_cache_free(sc, sh);
  276. return 1;
  277. }
  278. /* we just created an active stripe so... */
  279. atomic_set(&sh->count, 1);
  280. atomic_inc(&conf->active_stripes);
  281. INIT_LIST_HEAD(&sh->lru);
  282. release_stripe(sh);
  283. }
  284. return 0;
  285. }
  286. static void shrink_stripes(raid5_conf_t *conf)
  287. {
  288. struct stripe_head *sh;
  289. while (1) {
  290. spin_lock_irq(&conf->device_lock);
  291. sh = get_free_stripe(conf);
  292. spin_unlock_irq(&conf->device_lock);
  293. if (!sh)
  294. break;
  295. if (atomic_read(&sh->count))
  296. BUG();
  297. shrink_buffers(sh, conf->raid_disks);
  298. kmem_cache_free(conf->slab_cache, sh);
  299. atomic_dec(&conf->active_stripes);
  300. }
  301. kmem_cache_destroy(conf->slab_cache);
  302. conf->slab_cache = NULL;
  303. }
  304. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  305. int error)
  306. {
  307. struct stripe_head *sh = bi->bi_private;
  308. raid5_conf_t *conf = sh->raid_conf;
  309. int disks = conf->raid_disks, i;
  310. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  311. if (bi->bi_size)
  312. return 1;
  313. for (i=0 ; i<disks; i++)
  314. if (bi == &sh->dev[i].req)
  315. break;
  316. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  317. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  318. uptodate);
  319. if (i == disks) {
  320. BUG();
  321. return 0;
  322. }
  323. if (uptodate) {
  324. #if 0
  325. struct bio *bio;
  326. unsigned long flags;
  327. spin_lock_irqsave(&conf->device_lock, flags);
  328. /* we can return a buffer if we bypassed the cache or
  329. * if the top buffer is not in highmem. If there are
  330. * multiple buffers, leave the extra work to
  331. * handle_stripe
  332. */
  333. buffer = sh->bh_read[i];
  334. if (buffer &&
  335. (!PageHighMem(buffer->b_page)
  336. || buffer->b_page == bh->b_page )
  337. ) {
  338. sh->bh_read[i] = buffer->b_reqnext;
  339. buffer->b_reqnext = NULL;
  340. } else
  341. buffer = NULL;
  342. spin_unlock_irqrestore(&conf->device_lock, flags);
  343. if (sh->bh_page[i]==bh->b_page)
  344. set_buffer_uptodate(bh);
  345. if (buffer) {
  346. if (buffer->b_page != bh->b_page)
  347. memcpy(buffer->b_data, bh->b_data, bh->b_size);
  348. buffer->b_end_io(buffer, 1);
  349. }
  350. #else
  351. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  352. #endif
  353. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  354. printk("R5: read error corrected!!\n");
  355. clear_bit(R5_ReadError, &sh->dev[i].flags);
  356. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  357. }
  358. } else {
  359. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  360. if (conf->mddev->degraded) {
  361. printk("R5: read error not correctable.\n");
  362. clear_bit(R5_ReadError, &sh->dev[i].flags);
  363. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  364. md_error(conf->mddev, conf->disks[i].rdev);
  365. } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
  366. /* Oh, no!!! */
  367. printk("R5: read error NOT corrected!!\n");
  368. clear_bit(R5_ReadError, &sh->dev[i].flags);
  369. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  370. md_error(conf->mddev, conf->disks[i].rdev);
  371. } else
  372. set_bit(R5_ReadError, &sh->dev[i].flags);
  373. }
  374. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  375. #if 0
  376. /* must restore b_page before unlocking buffer... */
  377. if (sh->bh_page[i] != bh->b_page) {
  378. bh->b_page = sh->bh_page[i];
  379. bh->b_data = page_address(bh->b_page);
  380. clear_buffer_uptodate(bh);
  381. }
  382. #endif
  383. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  384. set_bit(STRIPE_HANDLE, &sh->state);
  385. release_stripe(sh);
  386. return 0;
  387. }
  388. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  389. int error)
  390. {
  391. struct stripe_head *sh = bi->bi_private;
  392. raid5_conf_t *conf = sh->raid_conf;
  393. int disks = conf->raid_disks, i;
  394. unsigned long flags;
  395. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  396. if (bi->bi_size)
  397. return 1;
  398. for (i=0 ; i<disks; i++)
  399. if (bi == &sh->dev[i].req)
  400. break;
  401. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  402. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  403. uptodate);
  404. if (i == disks) {
  405. BUG();
  406. return 0;
  407. }
  408. spin_lock_irqsave(&conf->device_lock, flags);
  409. if (!uptodate)
  410. md_error(conf->mddev, conf->disks[i].rdev);
  411. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  412. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  413. set_bit(STRIPE_HANDLE, &sh->state);
  414. __release_stripe(conf, sh);
  415. spin_unlock_irqrestore(&conf->device_lock, flags);
  416. return 0;
  417. }
  418. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  419. static void raid5_build_block (struct stripe_head *sh, int i)
  420. {
  421. struct r5dev *dev = &sh->dev[i];
  422. bio_init(&dev->req);
  423. dev->req.bi_io_vec = &dev->vec;
  424. dev->req.bi_vcnt++;
  425. dev->req.bi_max_vecs++;
  426. dev->vec.bv_page = dev->page;
  427. dev->vec.bv_len = STRIPE_SIZE;
  428. dev->vec.bv_offset = 0;
  429. dev->req.bi_sector = sh->sector;
  430. dev->req.bi_private = sh;
  431. dev->flags = 0;
  432. if (i != sh->pd_idx)
  433. dev->sector = compute_blocknr(sh, i);
  434. }
  435. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  436. {
  437. char b[BDEVNAME_SIZE];
  438. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  439. PRINTK("raid5: error called\n");
  440. if (!rdev->faulty) {
  441. mddev->sb_dirty = 1;
  442. if (rdev->in_sync) {
  443. conf->working_disks--;
  444. mddev->degraded++;
  445. conf->failed_disks++;
  446. rdev->in_sync = 0;
  447. /*
  448. * if recovery was running, make sure it aborts.
  449. */
  450. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  451. }
  452. rdev->faulty = 1;
  453. printk (KERN_ALERT
  454. "raid5: Disk failure on %s, disabling device."
  455. " Operation continuing on %d devices\n",
  456. bdevname(rdev->bdev,b), conf->working_disks);
  457. }
  458. }
  459. /*
  460. * Input: a 'big' sector number,
  461. * Output: index of the data and parity disk, and the sector # in them.
  462. */
  463. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  464. unsigned int data_disks, unsigned int * dd_idx,
  465. unsigned int * pd_idx, raid5_conf_t *conf)
  466. {
  467. long stripe;
  468. unsigned long chunk_number;
  469. unsigned int chunk_offset;
  470. sector_t new_sector;
  471. int sectors_per_chunk = conf->chunk_size >> 9;
  472. /* First compute the information on this sector */
  473. /*
  474. * Compute the chunk number and the sector offset inside the chunk
  475. */
  476. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  477. chunk_number = r_sector;
  478. BUG_ON(r_sector != chunk_number);
  479. /*
  480. * Compute the stripe number
  481. */
  482. stripe = chunk_number / data_disks;
  483. /*
  484. * Compute the data disk and parity disk indexes inside the stripe
  485. */
  486. *dd_idx = chunk_number % data_disks;
  487. /*
  488. * Select the parity disk based on the user selected algorithm.
  489. */
  490. if (conf->level == 4)
  491. *pd_idx = data_disks;
  492. else switch (conf->algorithm) {
  493. case ALGORITHM_LEFT_ASYMMETRIC:
  494. *pd_idx = data_disks - stripe % raid_disks;
  495. if (*dd_idx >= *pd_idx)
  496. (*dd_idx)++;
  497. break;
  498. case ALGORITHM_RIGHT_ASYMMETRIC:
  499. *pd_idx = stripe % raid_disks;
  500. if (*dd_idx >= *pd_idx)
  501. (*dd_idx)++;
  502. break;
  503. case ALGORITHM_LEFT_SYMMETRIC:
  504. *pd_idx = data_disks - stripe % raid_disks;
  505. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  506. break;
  507. case ALGORITHM_RIGHT_SYMMETRIC:
  508. *pd_idx = stripe % raid_disks;
  509. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  510. break;
  511. default:
  512. printk("raid5: unsupported algorithm %d\n",
  513. conf->algorithm);
  514. }
  515. /*
  516. * Finally, compute the new sector number
  517. */
  518. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  519. return new_sector;
  520. }
  521. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  522. {
  523. raid5_conf_t *conf = sh->raid_conf;
  524. int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
  525. sector_t new_sector = sh->sector, check;
  526. int sectors_per_chunk = conf->chunk_size >> 9;
  527. sector_t stripe;
  528. int chunk_offset;
  529. int chunk_number, dummy1, dummy2, dd_idx = i;
  530. sector_t r_sector;
  531. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  532. stripe = new_sector;
  533. BUG_ON(new_sector != stripe);
  534. switch (conf->algorithm) {
  535. case ALGORITHM_LEFT_ASYMMETRIC:
  536. case ALGORITHM_RIGHT_ASYMMETRIC:
  537. if (i > sh->pd_idx)
  538. i--;
  539. break;
  540. case ALGORITHM_LEFT_SYMMETRIC:
  541. case ALGORITHM_RIGHT_SYMMETRIC:
  542. if (i < sh->pd_idx)
  543. i += raid_disks;
  544. i -= (sh->pd_idx + 1);
  545. break;
  546. default:
  547. printk("raid5: unsupported algorithm %d\n",
  548. conf->algorithm);
  549. }
  550. chunk_number = stripe * data_disks + i;
  551. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  552. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  553. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  554. printk("compute_blocknr: map not correct\n");
  555. return 0;
  556. }
  557. return r_sector;
  558. }
  559. /*
  560. * Copy data between a page in the stripe cache, and a bio.
  561. * There are no alignment or size guarantees between the page or the
  562. * bio except that there is some overlap.
  563. * All iovecs in the bio must be considered.
  564. */
  565. static void copy_data(int frombio, struct bio *bio,
  566. struct page *page,
  567. sector_t sector)
  568. {
  569. char *pa = page_address(page);
  570. struct bio_vec *bvl;
  571. int i;
  572. int page_offset;
  573. if (bio->bi_sector >= sector)
  574. page_offset = (signed)(bio->bi_sector - sector) * 512;
  575. else
  576. page_offset = (signed)(sector - bio->bi_sector) * -512;
  577. bio_for_each_segment(bvl, bio, i) {
  578. int len = bio_iovec_idx(bio,i)->bv_len;
  579. int clen;
  580. int b_offset = 0;
  581. if (page_offset < 0) {
  582. b_offset = -page_offset;
  583. page_offset += b_offset;
  584. len -= b_offset;
  585. }
  586. if (len > 0 && page_offset + len > STRIPE_SIZE)
  587. clen = STRIPE_SIZE - page_offset;
  588. else clen = len;
  589. if (clen > 0) {
  590. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  591. if (frombio)
  592. memcpy(pa+page_offset, ba+b_offset, clen);
  593. else
  594. memcpy(ba+b_offset, pa+page_offset, clen);
  595. __bio_kunmap_atomic(ba, KM_USER0);
  596. }
  597. if (clen < len) /* hit end of page */
  598. break;
  599. page_offset += len;
  600. }
  601. }
  602. #define check_xor() do { \
  603. if (count == MAX_XOR_BLOCKS) { \
  604. xor_block(count, STRIPE_SIZE, ptr); \
  605. count = 1; \
  606. } \
  607. } while(0)
  608. static void compute_block(struct stripe_head *sh, int dd_idx)
  609. {
  610. raid5_conf_t *conf = sh->raid_conf;
  611. int i, count, disks = conf->raid_disks;
  612. void *ptr[MAX_XOR_BLOCKS], *p;
  613. PRINTK("compute_block, stripe %llu, idx %d\n",
  614. (unsigned long long)sh->sector, dd_idx);
  615. ptr[0] = page_address(sh->dev[dd_idx].page);
  616. memset(ptr[0], 0, STRIPE_SIZE);
  617. count = 1;
  618. for (i = disks ; i--; ) {
  619. if (i == dd_idx)
  620. continue;
  621. p = page_address(sh->dev[i].page);
  622. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  623. ptr[count++] = p;
  624. else
  625. printk("compute_block() %d, stripe %llu, %d"
  626. " not present\n", dd_idx,
  627. (unsigned long long)sh->sector, i);
  628. check_xor();
  629. }
  630. if (count != 1)
  631. xor_block(count, STRIPE_SIZE, ptr);
  632. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  633. }
  634. static void compute_parity(struct stripe_head *sh, int method)
  635. {
  636. raid5_conf_t *conf = sh->raid_conf;
  637. int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
  638. void *ptr[MAX_XOR_BLOCKS];
  639. struct bio *chosen;
  640. PRINTK("compute_parity, stripe %llu, method %d\n",
  641. (unsigned long long)sh->sector, method);
  642. count = 1;
  643. ptr[0] = page_address(sh->dev[pd_idx].page);
  644. switch(method) {
  645. case READ_MODIFY_WRITE:
  646. if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
  647. BUG();
  648. for (i=disks ; i-- ;) {
  649. if (i==pd_idx)
  650. continue;
  651. if (sh->dev[i].towrite &&
  652. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  653. ptr[count++] = page_address(sh->dev[i].page);
  654. chosen = sh->dev[i].towrite;
  655. sh->dev[i].towrite = NULL;
  656. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  657. wake_up(&conf->wait_for_overlap);
  658. if (sh->dev[i].written) BUG();
  659. sh->dev[i].written = chosen;
  660. check_xor();
  661. }
  662. }
  663. break;
  664. case RECONSTRUCT_WRITE:
  665. memset(ptr[0], 0, STRIPE_SIZE);
  666. for (i= disks; i-- ;)
  667. if (i!=pd_idx && sh->dev[i].towrite) {
  668. chosen = sh->dev[i].towrite;
  669. sh->dev[i].towrite = NULL;
  670. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  671. wake_up(&conf->wait_for_overlap);
  672. if (sh->dev[i].written) BUG();
  673. sh->dev[i].written = chosen;
  674. }
  675. break;
  676. case CHECK_PARITY:
  677. break;
  678. }
  679. if (count>1) {
  680. xor_block(count, STRIPE_SIZE, ptr);
  681. count = 1;
  682. }
  683. for (i = disks; i--;)
  684. if (sh->dev[i].written) {
  685. sector_t sector = sh->dev[i].sector;
  686. struct bio *wbi = sh->dev[i].written;
  687. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  688. copy_data(1, wbi, sh->dev[i].page, sector);
  689. wbi = r5_next_bio(wbi, sector);
  690. }
  691. set_bit(R5_LOCKED, &sh->dev[i].flags);
  692. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  693. }
  694. switch(method) {
  695. case RECONSTRUCT_WRITE:
  696. case CHECK_PARITY:
  697. for (i=disks; i--;)
  698. if (i != pd_idx) {
  699. ptr[count++] = page_address(sh->dev[i].page);
  700. check_xor();
  701. }
  702. break;
  703. case READ_MODIFY_WRITE:
  704. for (i = disks; i--;)
  705. if (sh->dev[i].written) {
  706. ptr[count++] = page_address(sh->dev[i].page);
  707. check_xor();
  708. }
  709. }
  710. if (count != 1)
  711. xor_block(count, STRIPE_SIZE, ptr);
  712. if (method != CHECK_PARITY) {
  713. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  714. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  715. } else
  716. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  717. }
  718. /*
  719. * Each stripe/dev can have one or more bion attached.
  720. * toread/towrite point to the first in a chain.
  721. * The bi_next chain must be in order.
  722. */
  723. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  724. {
  725. struct bio **bip;
  726. raid5_conf_t *conf = sh->raid_conf;
  727. int firstwrite=0;
  728. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  729. (unsigned long long)bi->bi_sector,
  730. (unsigned long long)sh->sector);
  731. spin_lock(&sh->lock);
  732. spin_lock_irq(&conf->device_lock);
  733. if (forwrite) {
  734. bip = &sh->dev[dd_idx].towrite;
  735. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  736. firstwrite = 1;
  737. } else
  738. bip = &sh->dev[dd_idx].toread;
  739. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  740. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  741. goto overlap;
  742. bip = & (*bip)->bi_next;
  743. }
  744. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  745. goto overlap;
  746. if (*bip && bi->bi_next && (*bip) != bi->bi_next)
  747. BUG();
  748. if (*bip)
  749. bi->bi_next = *bip;
  750. *bip = bi;
  751. bi->bi_phys_segments ++;
  752. spin_unlock_irq(&conf->device_lock);
  753. spin_unlock(&sh->lock);
  754. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  755. (unsigned long long)bi->bi_sector,
  756. (unsigned long long)sh->sector, dd_idx);
  757. if (conf->mddev->bitmap && firstwrite) {
  758. sh->bm_seq = conf->seq_write;
  759. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  760. STRIPE_SECTORS, 0);
  761. set_bit(STRIPE_BIT_DELAY, &sh->state);
  762. }
  763. if (forwrite) {
  764. /* check if page is covered */
  765. sector_t sector = sh->dev[dd_idx].sector;
  766. for (bi=sh->dev[dd_idx].towrite;
  767. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  768. bi && bi->bi_sector <= sector;
  769. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  770. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  771. sector = bi->bi_sector + (bi->bi_size>>9);
  772. }
  773. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  774. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  775. }
  776. return 1;
  777. overlap:
  778. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  779. spin_unlock_irq(&conf->device_lock);
  780. spin_unlock(&sh->lock);
  781. return 0;
  782. }
  783. /*
  784. * handle_stripe - do things to a stripe.
  785. *
  786. * We lock the stripe and then examine the state of various bits
  787. * to see what needs to be done.
  788. * Possible results:
  789. * return some read request which now have data
  790. * return some write requests which are safely on disc
  791. * schedule a read on some buffers
  792. * schedule a write of some buffers
  793. * return confirmation of parity correctness
  794. *
  795. * Parity calculations are done inside the stripe lock
  796. * buffers are taken off read_list or write_list, and bh_cache buffers
  797. * get BH_Lock set before the stripe lock is released.
  798. *
  799. */
  800. static void handle_stripe(struct stripe_head *sh)
  801. {
  802. raid5_conf_t *conf = sh->raid_conf;
  803. int disks = conf->raid_disks;
  804. struct bio *return_bi= NULL;
  805. struct bio *bi;
  806. int i;
  807. int syncing;
  808. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  809. int non_overwrite = 0;
  810. int failed_num=0;
  811. struct r5dev *dev;
  812. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  813. (unsigned long long)sh->sector, atomic_read(&sh->count),
  814. sh->pd_idx);
  815. spin_lock(&sh->lock);
  816. clear_bit(STRIPE_HANDLE, &sh->state);
  817. clear_bit(STRIPE_DELAYED, &sh->state);
  818. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  819. /* Now to look around and see what can be done */
  820. for (i=disks; i--; ) {
  821. mdk_rdev_t *rdev;
  822. dev = &sh->dev[i];
  823. clear_bit(R5_Insync, &dev->flags);
  824. clear_bit(R5_Syncio, &dev->flags);
  825. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  826. i, dev->flags, dev->toread, dev->towrite, dev->written);
  827. /* maybe we can reply to a read */
  828. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  829. struct bio *rbi, *rbi2;
  830. PRINTK("Return read for disc %d\n", i);
  831. spin_lock_irq(&conf->device_lock);
  832. rbi = dev->toread;
  833. dev->toread = NULL;
  834. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  835. wake_up(&conf->wait_for_overlap);
  836. spin_unlock_irq(&conf->device_lock);
  837. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  838. copy_data(0, rbi, dev->page, dev->sector);
  839. rbi2 = r5_next_bio(rbi, dev->sector);
  840. spin_lock_irq(&conf->device_lock);
  841. if (--rbi->bi_phys_segments == 0) {
  842. rbi->bi_next = return_bi;
  843. return_bi = rbi;
  844. }
  845. spin_unlock_irq(&conf->device_lock);
  846. rbi = rbi2;
  847. }
  848. }
  849. /* now count some things */
  850. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  851. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  852. if (dev->toread) to_read++;
  853. if (dev->towrite) {
  854. to_write++;
  855. if (!test_bit(R5_OVERWRITE, &dev->flags))
  856. non_overwrite++;
  857. }
  858. if (dev->written) written++;
  859. rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
  860. if (!rdev || !rdev->in_sync) {
  861. /* The ReadError flag wil just be confusing now */
  862. clear_bit(R5_ReadError, &dev->flags);
  863. clear_bit(R5_ReWrite, &dev->flags);
  864. }
  865. if (!rdev || !rdev->in_sync
  866. || test_bit(R5_ReadError, &dev->flags)) {
  867. failed++;
  868. failed_num = i;
  869. } else
  870. set_bit(R5_Insync, &dev->flags);
  871. }
  872. PRINTK("locked=%d uptodate=%d to_read=%d"
  873. " to_write=%d failed=%d failed_num=%d\n",
  874. locked, uptodate, to_read, to_write, failed, failed_num);
  875. /* check if the array has lost two devices and, if so, some requests might
  876. * need to be failed
  877. */
  878. if (failed > 1 && to_read+to_write+written) {
  879. for (i=disks; i--; ) {
  880. int bitmap_end = 0;
  881. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  882. mdk_rdev_t *rdev = conf->disks[i].rdev;
  883. if (rdev && rdev->in_sync)
  884. /* multiple read failures in one stripe */
  885. md_error(conf->mddev, rdev);
  886. }
  887. spin_lock_irq(&conf->device_lock);
  888. /* fail all writes first */
  889. bi = sh->dev[i].towrite;
  890. sh->dev[i].towrite = NULL;
  891. if (bi) { to_write--; bitmap_end = 1; }
  892. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  893. wake_up(&conf->wait_for_overlap);
  894. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  895. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  896. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  897. if (--bi->bi_phys_segments == 0) {
  898. md_write_end(conf->mddev);
  899. bi->bi_next = return_bi;
  900. return_bi = bi;
  901. }
  902. bi = nextbi;
  903. }
  904. /* and fail all 'written' */
  905. bi = sh->dev[i].written;
  906. sh->dev[i].written = NULL;
  907. if (bi) bitmap_end = 1;
  908. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  909. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  910. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  911. if (--bi->bi_phys_segments == 0) {
  912. md_write_end(conf->mddev);
  913. bi->bi_next = return_bi;
  914. return_bi = bi;
  915. }
  916. bi = bi2;
  917. }
  918. /* fail any reads if this device is non-operational */
  919. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  920. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  921. bi = sh->dev[i].toread;
  922. sh->dev[i].toread = NULL;
  923. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  924. wake_up(&conf->wait_for_overlap);
  925. if (bi) to_read--;
  926. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  927. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  928. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  929. if (--bi->bi_phys_segments == 0) {
  930. bi->bi_next = return_bi;
  931. return_bi = bi;
  932. }
  933. bi = nextbi;
  934. }
  935. }
  936. spin_unlock_irq(&conf->device_lock);
  937. if (bitmap_end)
  938. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  939. STRIPE_SECTORS, 0, 0);
  940. }
  941. }
  942. if (failed > 1 && syncing) {
  943. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  944. clear_bit(STRIPE_SYNCING, &sh->state);
  945. syncing = 0;
  946. }
  947. /* might be able to return some write requests if the parity block
  948. * is safe, or on a failed drive
  949. */
  950. dev = &sh->dev[sh->pd_idx];
  951. if ( written &&
  952. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  953. test_bit(R5_UPTODATE, &dev->flags))
  954. || (failed == 1 && failed_num == sh->pd_idx))
  955. ) {
  956. /* any written block on an uptodate or failed drive can be returned.
  957. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  958. * never LOCKED, so we don't need to test 'failed' directly.
  959. */
  960. for (i=disks; i--; )
  961. if (sh->dev[i].written) {
  962. dev = &sh->dev[i];
  963. if (!test_bit(R5_LOCKED, &dev->flags) &&
  964. test_bit(R5_UPTODATE, &dev->flags) ) {
  965. /* We can return any write requests */
  966. struct bio *wbi, *wbi2;
  967. int bitmap_end = 0;
  968. PRINTK("Return write for disc %d\n", i);
  969. spin_lock_irq(&conf->device_lock);
  970. wbi = dev->written;
  971. dev->written = NULL;
  972. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  973. wbi2 = r5_next_bio(wbi, dev->sector);
  974. if (--wbi->bi_phys_segments == 0) {
  975. md_write_end(conf->mddev);
  976. wbi->bi_next = return_bi;
  977. return_bi = wbi;
  978. }
  979. wbi = wbi2;
  980. }
  981. if (dev->towrite == NULL)
  982. bitmap_end = 1;
  983. spin_unlock_irq(&conf->device_lock);
  984. if (bitmap_end)
  985. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  986. STRIPE_SECTORS,
  987. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  988. }
  989. }
  990. }
  991. /* Now we might consider reading some blocks, either to check/generate
  992. * parity, or to satisfy requests
  993. * or to load a block that is being partially written.
  994. */
  995. if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
  996. for (i=disks; i--;) {
  997. dev = &sh->dev[i];
  998. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  999. (dev->toread ||
  1000. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1001. syncing ||
  1002. (failed && (sh->dev[failed_num].toread ||
  1003. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1004. )
  1005. ) {
  1006. /* we would like to get this block, possibly
  1007. * by computing it, but we might not be able to
  1008. */
  1009. if (uptodate == disks-1) {
  1010. PRINTK("Computing block %d\n", i);
  1011. compute_block(sh, i);
  1012. uptodate++;
  1013. } else if (test_bit(R5_Insync, &dev->flags)) {
  1014. set_bit(R5_LOCKED, &dev->flags);
  1015. set_bit(R5_Wantread, &dev->flags);
  1016. #if 0
  1017. /* if I am just reading this block and we don't have
  1018. a failed drive, or any pending writes then sidestep the cache */
  1019. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1020. ! syncing && !failed && !to_write) {
  1021. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1022. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1023. }
  1024. #endif
  1025. locked++;
  1026. PRINTK("Reading block %d (sync=%d)\n",
  1027. i, syncing);
  1028. if (syncing)
  1029. md_sync_acct(conf->disks[i].rdev->bdev,
  1030. STRIPE_SECTORS);
  1031. }
  1032. }
  1033. }
  1034. set_bit(STRIPE_HANDLE, &sh->state);
  1035. }
  1036. /* now to consider writing and what else, if anything should be read */
  1037. if (to_write) {
  1038. int rmw=0, rcw=0;
  1039. for (i=disks ; i--;) {
  1040. /* would I have to read this buffer for read_modify_write */
  1041. dev = &sh->dev[i];
  1042. if ((dev->towrite || i == sh->pd_idx) &&
  1043. (!test_bit(R5_LOCKED, &dev->flags)
  1044. #if 0
  1045. || sh->bh_page[i]!=bh->b_page
  1046. #endif
  1047. ) &&
  1048. !test_bit(R5_UPTODATE, &dev->flags)) {
  1049. if (test_bit(R5_Insync, &dev->flags)
  1050. /* && !(!mddev->insync && i == sh->pd_idx) */
  1051. )
  1052. rmw++;
  1053. else rmw += 2*disks; /* cannot read it */
  1054. }
  1055. /* Would I have to read this buffer for reconstruct_write */
  1056. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1057. (!test_bit(R5_LOCKED, &dev->flags)
  1058. #if 0
  1059. || sh->bh_page[i] != bh->b_page
  1060. #endif
  1061. ) &&
  1062. !test_bit(R5_UPTODATE, &dev->flags)) {
  1063. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1064. else rcw += 2*disks;
  1065. }
  1066. }
  1067. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1068. (unsigned long long)sh->sector, rmw, rcw);
  1069. set_bit(STRIPE_HANDLE, &sh->state);
  1070. if (rmw < rcw && rmw > 0)
  1071. /* prefer read-modify-write, but need to get some data */
  1072. for (i=disks; i--;) {
  1073. dev = &sh->dev[i];
  1074. if ((dev->towrite || i == sh->pd_idx) &&
  1075. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1076. test_bit(R5_Insync, &dev->flags)) {
  1077. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1078. {
  1079. PRINTK("Read_old block %d for r-m-w\n", i);
  1080. set_bit(R5_LOCKED, &dev->flags);
  1081. set_bit(R5_Wantread, &dev->flags);
  1082. locked++;
  1083. } else {
  1084. set_bit(STRIPE_DELAYED, &sh->state);
  1085. set_bit(STRIPE_HANDLE, &sh->state);
  1086. }
  1087. }
  1088. }
  1089. if (rcw <= rmw && rcw > 0)
  1090. /* want reconstruct write, but need to get some data */
  1091. for (i=disks; i--;) {
  1092. dev = &sh->dev[i];
  1093. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1094. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1095. test_bit(R5_Insync, &dev->flags)) {
  1096. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1097. {
  1098. PRINTK("Read_old block %d for Reconstruct\n", i);
  1099. set_bit(R5_LOCKED, &dev->flags);
  1100. set_bit(R5_Wantread, &dev->flags);
  1101. locked++;
  1102. } else {
  1103. set_bit(STRIPE_DELAYED, &sh->state);
  1104. set_bit(STRIPE_HANDLE, &sh->state);
  1105. }
  1106. }
  1107. }
  1108. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1109. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1110. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1111. PRINTK("Computing parity...\n");
  1112. compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1113. /* now every locked buffer is ready to be written */
  1114. for (i=disks; i--;)
  1115. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1116. PRINTK("Writing block %d\n", i);
  1117. locked++;
  1118. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1119. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1120. || (i==sh->pd_idx && failed == 0))
  1121. set_bit(STRIPE_INSYNC, &sh->state);
  1122. }
  1123. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1124. atomic_dec(&conf->preread_active_stripes);
  1125. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1126. md_wakeup_thread(conf->mddev->thread);
  1127. }
  1128. }
  1129. }
  1130. /* maybe we need to check and possibly fix the parity for this stripe
  1131. * Any reads will already have been scheduled, so we just see if enough data
  1132. * is available
  1133. */
  1134. if (syncing && locked == 0 &&
  1135. !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
  1136. set_bit(STRIPE_HANDLE, &sh->state);
  1137. if (failed == 0) {
  1138. char *pagea;
  1139. if (uptodate != disks)
  1140. BUG();
  1141. compute_parity(sh, CHECK_PARITY);
  1142. uptodate--;
  1143. pagea = page_address(sh->dev[sh->pd_idx].page);
  1144. if ((*(u32*)pagea) == 0 &&
  1145. !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
  1146. /* parity is correct (on disc, not in buffer any more) */
  1147. set_bit(STRIPE_INSYNC, &sh->state);
  1148. }
  1149. }
  1150. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1151. if (failed==0)
  1152. failed_num = sh->pd_idx;
  1153. /* should be able to compute the missing block and write it to spare */
  1154. if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
  1155. if (uptodate+1 != disks)
  1156. BUG();
  1157. compute_block(sh, failed_num);
  1158. uptodate++;
  1159. }
  1160. if (uptodate != disks)
  1161. BUG();
  1162. dev = &sh->dev[failed_num];
  1163. set_bit(R5_LOCKED, &dev->flags);
  1164. set_bit(R5_Wantwrite, &dev->flags);
  1165. clear_bit(STRIPE_DEGRADED, &sh->state);
  1166. locked++;
  1167. set_bit(STRIPE_INSYNC, &sh->state);
  1168. set_bit(R5_Syncio, &dev->flags);
  1169. }
  1170. }
  1171. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1172. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1173. clear_bit(STRIPE_SYNCING, &sh->state);
  1174. }
  1175. /* If the failed drive is just a ReadError, then we might need to progress
  1176. * the repair/check process
  1177. */
  1178. if (failed == 1 && test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1179. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1180. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1181. ) {
  1182. dev = &sh->dev[failed_num];
  1183. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1184. set_bit(R5_Wantwrite, &dev->flags);
  1185. set_bit(R5_ReWrite, &dev->flags);
  1186. set_bit(R5_LOCKED, &dev->flags);
  1187. } else {
  1188. /* let's read it back */
  1189. set_bit(R5_Wantread, &dev->flags);
  1190. set_bit(R5_LOCKED, &dev->flags);
  1191. }
  1192. }
  1193. spin_unlock(&sh->lock);
  1194. while ((bi=return_bi)) {
  1195. int bytes = bi->bi_size;
  1196. return_bi = bi->bi_next;
  1197. bi->bi_next = NULL;
  1198. bi->bi_size = 0;
  1199. bi->bi_end_io(bi, bytes, 0);
  1200. }
  1201. for (i=disks; i-- ;) {
  1202. int rw;
  1203. struct bio *bi;
  1204. mdk_rdev_t *rdev;
  1205. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1206. rw = 1;
  1207. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1208. rw = 0;
  1209. else
  1210. continue;
  1211. bi = &sh->dev[i].req;
  1212. bi->bi_rw = rw;
  1213. if (rw)
  1214. bi->bi_end_io = raid5_end_write_request;
  1215. else
  1216. bi->bi_end_io = raid5_end_read_request;
  1217. rcu_read_lock();
  1218. rdev = conf->disks[i].rdev;
  1219. if (rdev && rdev->faulty)
  1220. rdev = NULL;
  1221. if (rdev)
  1222. atomic_inc(&rdev->nr_pending);
  1223. rcu_read_unlock();
  1224. if (rdev) {
  1225. if (test_bit(R5_Syncio, &sh->dev[i].flags))
  1226. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1227. bi->bi_bdev = rdev->bdev;
  1228. PRINTK("for %llu schedule op %ld on disc %d\n",
  1229. (unsigned long long)sh->sector, bi->bi_rw, i);
  1230. atomic_inc(&sh->count);
  1231. bi->bi_sector = sh->sector + rdev->data_offset;
  1232. bi->bi_flags = 1 << BIO_UPTODATE;
  1233. bi->bi_vcnt = 1;
  1234. bi->bi_max_vecs = 1;
  1235. bi->bi_idx = 0;
  1236. bi->bi_io_vec = &sh->dev[i].vec;
  1237. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1238. bi->bi_io_vec[0].bv_offset = 0;
  1239. bi->bi_size = STRIPE_SIZE;
  1240. bi->bi_next = NULL;
  1241. generic_make_request(bi);
  1242. } else {
  1243. if (rw == 1)
  1244. set_bit(STRIPE_DEGRADED, &sh->state);
  1245. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1246. bi->bi_rw, i, (unsigned long long)sh->sector);
  1247. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1248. set_bit(STRIPE_HANDLE, &sh->state);
  1249. }
  1250. }
  1251. }
  1252. static inline void raid5_activate_delayed(raid5_conf_t *conf)
  1253. {
  1254. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  1255. while (!list_empty(&conf->delayed_list)) {
  1256. struct list_head *l = conf->delayed_list.next;
  1257. struct stripe_head *sh;
  1258. sh = list_entry(l, struct stripe_head, lru);
  1259. list_del_init(l);
  1260. clear_bit(STRIPE_DELAYED, &sh->state);
  1261. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1262. atomic_inc(&conf->preread_active_stripes);
  1263. list_add_tail(&sh->lru, &conf->handle_list);
  1264. }
  1265. }
  1266. }
  1267. static inline void activate_bit_delay(raid5_conf_t *conf)
  1268. {
  1269. /* device_lock is held */
  1270. struct list_head head;
  1271. list_add(&head, &conf->bitmap_list);
  1272. list_del_init(&conf->bitmap_list);
  1273. while (!list_empty(&head)) {
  1274. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  1275. list_del_init(&sh->lru);
  1276. atomic_inc(&sh->count);
  1277. __release_stripe(conf, sh);
  1278. }
  1279. }
  1280. static void unplug_slaves(mddev_t *mddev)
  1281. {
  1282. raid5_conf_t *conf = mddev_to_conf(mddev);
  1283. int i;
  1284. rcu_read_lock();
  1285. for (i=0; i<mddev->raid_disks; i++) {
  1286. mdk_rdev_t *rdev = conf->disks[i].rdev;
  1287. if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
  1288. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  1289. atomic_inc(&rdev->nr_pending);
  1290. rcu_read_unlock();
  1291. if (r_queue->unplug_fn)
  1292. r_queue->unplug_fn(r_queue);
  1293. rdev_dec_pending(rdev, mddev);
  1294. rcu_read_lock();
  1295. }
  1296. }
  1297. rcu_read_unlock();
  1298. }
  1299. static void raid5_unplug_device(request_queue_t *q)
  1300. {
  1301. mddev_t *mddev = q->queuedata;
  1302. raid5_conf_t *conf = mddev_to_conf(mddev);
  1303. unsigned long flags;
  1304. spin_lock_irqsave(&conf->device_lock, flags);
  1305. if (blk_remove_plug(q)) {
  1306. conf->seq_flush++;
  1307. raid5_activate_delayed(conf);
  1308. }
  1309. md_wakeup_thread(mddev->thread);
  1310. spin_unlock_irqrestore(&conf->device_lock, flags);
  1311. unplug_slaves(mddev);
  1312. }
  1313. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  1314. sector_t *error_sector)
  1315. {
  1316. mddev_t *mddev = q->queuedata;
  1317. raid5_conf_t *conf = mddev_to_conf(mddev);
  1318. int i, ret = 0;
  1319. rcu_read_lock();
  1320. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  1321. mdk_rdev_t *rdev = conf->disks[i].rdev;
  1322. if (rdev && !rdev->faulty) {
  1323. struct block_device *bdev = rdev->bdev;
  1324. request_queue_t *r_queue = bdev_get_queue(bdev);
  1325. if (!r_queue->issue_flush_fn)
  1326. ret = -EOPNOTSUPP;
  1327. else {
  1328. atomic_inc(&rdev->nr_pending);
  1329. rcu_read_unlock();
  1330. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  1331. error_sector);
  1332. rdev_dec_pending(rdev, mddev);
  1333. rcu_read_lock();
  1334. }
  1335. }
  1336. }
  1337. rcu_read_unlock();
  1338. return ret;
  1339. }
  1340. static inline void raid5_plug_device(raid5_conf_t *conf)
  1341. {
  1342. spin_lock_irq(&conf->device_lock);
  1343. blk_plug_device(conf->mddev->queue);
  1344. spin_unlock_irq(&conf->device_lock);
  1345. }
  1346. static int make_request (request_queue_t *q, struct bio * bi)
  1347. {
  1348. mddev_t *mddev = q->queuedata;
  1349. raid5_conf_t *conf = mddev_to_conf(mddev);
  1350. const unsigned int raid_disks = conf->raid_disks;
  1351. const unsigned int data_disks = raid_disks - 1;
  1352. unsigned int dd_idx, pd_idx;
  1353. sector_t new_sector;
  1354. sector_t logical_sector, last_sector;
  1355. struct stripe_head *sh;
  1356. const int rw = bio_data_dir(bi);
  1357. if (unlikely(bio_barrier(bi))) {
  1358. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  1359. return 0;
  1360. }
  1361. md_write_start(mddev, bi);
  1362. disk_stat_inc(mddev->gendisk, ios[rw]);
  1363. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  1364. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  1365. last_sector = bi->bi_sector + (bi->bi_size>>9);
  1366. bi->bi_next = NULL;
  1367. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  1368. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  1369. DEFINE_WAIT(w);
  1370. new_sector = raid5_compute_sector(logical_sector,
  1371. raid_disks, data_disks, &dd_idx, &pd_idx, conf);
  1372. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  1373. (unsigned long long)new_sector,
  1374. (unsigned long long)logical_sector);
  1375. retry:
  1376. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  1377. sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
  1378. if (sh) {
  1379. if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  1380. /* Add failed due to overlap. Flush everything
  1381. * and wait a while
  1382. */
  1383. raid5_unplug_device(mddev->queue);
  1384. release_stripe(sh);
  1385. schedule();
  1386. goto retry;
  1387. }
  1388. finish_wait(&conf->wait_for_overlap, &w);
  1389. raid5_plug_device(conf);
  1390. handle_stripe(sh);
  1391. release_stripe(sh);
  1392. } else {
  1393. /* cannot get stripe for read-ahead, just give-up */
  1394. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1395. finish_wait(&conf->wait_for_overlap, &w);
  1396. break;
  1397. }
  1398. }
  1399. spin_lock_irq(&conf->device_lock);
  1400. if (--bi->bi_phys_segments == 0) {
  1401. int bytes = bi->bi_size;
  1402. if ( bio_data_dir(bi) == WRITE )
  1403. md_write_end(mddev);
  1404. bi->bi_size = 0;
  1405. bi->bi_end_io(bi, bytes, 0);
  1406. }
  1407. spin_unlock_irq(&conf->device_lock);
  1408. return 0;
  1409. }
  1410. /* FIXME go_faster isn't used */
  1411. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1412. {
  1413. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1414. struct stripe_head *sh;
  1415. int sectors_per_chunk = conf->chunk_size >> 9;
  1416. sector_t x;
  1417. unsigned long stripe;
  1418. int chunk_offset;
  1419. int dd_idx, pd_idx;
  1420. sector_t first_sector;
  1421. int raid_disks = conf->raid_disks;
  1422. int data_disks = raid_disks-1;
  1423. sector_t max_sector = mddev->size << 1;
  1424. int sync_blocks;
  1425. if (sector_nr >= max_sector) {
  1426. /* just being told to finish up .. nothing much to do */
  1427. unplug_slaves(mddev);
  1428. if (mddev->curr_resync < max_sector) /* aborted */
  1429. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1430. &sync_blocks, 1);
  1431. else /* compelted sync */
  1432. conf->fullsync = 0;
  1433. bitmap_close_sync(mddev->bitmap);
  1434. return 0;
  1435. }
  1436. /* if there is 1 or more failed drives and we are trying
  1437. * to resync, then assert that we are finished, because there is
  1438. * nothing we can do.
  1439. */
  1440. if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1441. sector_t rv = (mddev->size << 1) - sector_nr;
  1442. *skipped = 1;
  1443. return rv;
  1444. }
  1445. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1446. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  1447. /* we can skip this block, and probably more */
  1448. sync_blocks /= STRIPE_SECTORS;
  1449. *skipped = 1;
  1450. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  1451. }
  1452. x = sector_nr;
  1453. chunk_offset = sector_div(x, sectors_per_chunk);
  1454. stripe = x;
  1455. BUG_ON(x != stripe);
  1456. first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
  1457. + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
  1458. sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
  1459. if (sh == NULL) {
  1460. sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
  1461. /* make sure we don't swamp the stripe cache if someone else
  1462. * is trying to get access
  1463. */
  1464. schedule_timeout_uninterruptible(1);
  1465. }
  1466. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
  1467. spin_lock(&sh->lock);
  1468. set_bit(STRIPE_SYNCING, &sh->state);
  1469. clear_bit(STRIPE_INSYNC, &sh->state);
  1470. spin_unlock(&sh->lock);
  1471. handle_stripe(sh);
  1472. release_stripe(sh);
  1473. return STRIPE_SECTORS;
  1474. }
  1475. /*
  1476. * This is our raid5 kernel thread.
  1477. *
  1478. * We scan the hash table for stripes which can be handled now.
  1479. * During the scan, completed stripes are saved for us by the interrupt
  1480. * handler, so that they will not have to wait for our next wakeup.
  1481. */
  1482. static void raid5d (mddev_t *mddev)
  1483. {
  1484. struct stripe_head *sh;
  1485. raid5_conf_t *conf = mddev_to_conf(mddev);
  1486. int handled;
  1487. PRINTK("+++ raid5d active\n");
  1488. md_check_recovery(mddev);
  1489. handled = 0;
  1490. spin_lock_irq(&conf->device_lock);
  1491. while (1) {
  1492. struct list_head *first;
  1493. if (conf->seq_flush - conf->seq_write > 0) {
  1494. int seq = conf->seq_flush;
  1495. bitmap_unplug(mddev->bitmap);
  1496. conf->seq_write = seq;
  1497. activate_bit_delay(conf);
  1498. }
  1499. if (list_empty(&conf->handle_list) &&
  1500. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  1501. !blk_queue_plugged(mddev->queue) &&
  1502. !list_empty(&conf->delayed_list))
  1503. raid5_activate_delayed(conf);
  1504. if (list_empty(&conf->handle_list))
  1505. break;
  1506. first = conf->handle_list.next;
  1507. sh = list_entry(first, struct stripe_head, lru);
  1508. list_del_init(first);
  1509. atomic_inc(&sh->count);
  1510. if (atomic_read(&sh->count)!= 1)
  1511. BUG();
  1512. spin_unlock_irq(&conf->device_lock);
  1513. handled++;
  1514. handle_stripe(sh);
  1515. release_stripe(sh);
  1516. spin_lock_irq(&conf->device_lock);
  1517. }
  1518. PRINTK("%d stripes handled\n", handled);
  1519. spin_unlock_irq(&conf->device_lock);
  1520. unplug_slaves(mddev);
  1521. PRINTK("--- raid5d inactive\n");
  1522. }
  1523. static int run(mddev_t *mddev)
  1524. {
  1525. raid5_conf_t *conf;
  1526. int raid_disk, memory;
  1527. mdk_rdev_t *rdev;
  1528. struct disk_info *disk;
  1529. struct list_head *tmp;
  1530. if (mddev->level != 5 && mddev->level != 4) {
  1531. printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
  1532. return -EIO;
  1533. }
  1534. mddev->private = kmalloc (sizeof (raid5_conf_t)
  1535. + mddev->raid_disks * sizeof(struct disk_info),
  1536. GFP_KERNEL);
  1537. if ((conf = mddev->private) == NULL)
  1538. goto abort;
  1539. memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
  1540. conf->mddev = mddev;
  1541. if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
  1542. goto abort;
  1543. memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
  1544. spin_lock_init(&conf->device_lock);
  1545. init_waitqueue_head(&conf->wait_for_stripe);
  1546. init_waitqueue_head(&conf->wait_for_overlap);
  1547. INIT_LIST_HEAD(&conf->handle_list);
  1548. INIT_LIST_HEAD(&conf->delayed_list);
  1549. INIT_LIST_HEAD(&conf->bitmap_list);
  1550. INIT_LIST_HEAD(&conf->inactive_list);
  1551. atomic_set(&conf->active_stripes, 0);
  1552. atomic_set(&conf->preread_active_stripes, 0);
  1553. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  1554. ITERATE_RDEV(mddev,rdev,tmp) {
  1555. raid_disk = rdev->raid_disk;
  1556. if (raid_disk >= mddev->raid_disks
  1557. || raid_disk < 0)
  1558. continue;
  1559. disk = conf->disks + raid_disk;
  1560. disk->rdev = rdev;
  1561. if (rdev->in_sync) {
  1562. char b[BDEVNAME_SIZE];
  1563. printk(KERN_INFO "raid5: device %s operational as raid"
  1564. " disk %d\n", bdevname(rdev->bdev,b),
  1565. raid_disk);
  1566. conf->working_disks++;
  1567. }
  1568. }
  1569. conf->raid_disks = mddev->raid_disks;
  1570. /*
  1571. * 0 for a fully functional array, 1 for a degraded array.
  1572. */
  1573. mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
  1574. conf->mddev = mddev;
  1575. conf->chunk_size = mddev->chunk_size;
  1576. conf->level = mddev->level;
  1577. conf->algorithm = mddev->layout;
  1578. conf->max_nr_stripes = NR_STRIPES;
  1579. /* device size must be a multiple of chunk size */
  1580. mddev->size &= ~(mddev->chunk_size/1024 -1);
  1581. mddev->resync_max_sectors = mddev->size << 1;
  1582. if (!conf->chunk_size || conf->chunk_size % 4) {
  1583. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  1584. conf->chunk_size, mdname(mddev));
  1585. goto abort;
  1586. }
  1587. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  1588. printk(KERN_ERR
  1589. "raid5: unsupported parity algorithm %d for %s\n",
  1590. conf->algorithm, mdname(mddev));
  1591. goto abort;
  1592. }
  1593. if (mddev->degraded > 1) {
  1594. printk(KERN_ERR "raid5: not enough operational devices for %s"
  1595. " (%d/%d failed)\n",
  1596. mdname(mddev), conf->failed_disks, conf->raid_disks);
  1597. goto abort;
  1598. }
  1599. if (mddev->degraded == 1 &&
  1600. mddev->recovery_cp != MaxSector) {
  1601. printk(KERN_ERR
  1602. "raid5: cannot start dirty degraded array for %s\n",
  1603. mdname(mddev));
  1604. goto abort;
  1605. }
  1606. {
  1607. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  1608. if (!mddev->thread) {
  1609. printk(KERN_ERR
  1610. "raid5: couldn't allocate thread for %s\n",
  1611. mdname(mddev));
  1612. goto abort;
  1613. }
  1614. }
  1615. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  1616. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  1617. if (grow_stripes(conf, conf->max_nr_stripes)) {
  1618. printk(KERN_ERR
  1619. "raid5: couldn't allocate %dkB for buffers\n", memory);
  1620. shrink_stripes(conf);
  1621. md_unregister_thread(mddev->thread);
  1622. goto abort;
  1623. } else
  1624. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  1625. memory, mdname(mddev));
  1626. if (mddev->degraded == 0)
  1627. printk("raid5: raid level %d set %s active with %d out of %d"
  1628. " devices, algorithm %d\n", conf->level, mdname(mddev),
  1629. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  1630. conf->algorithm);
  1631. else
  1632. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  1633. " out of %d devices, algorithm %d\n", conf->level,
  1634. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1635. mddev->raid_disks, conf->algorithm);
  1636. print_raid5_conf(conf);
  1637. /* read-ahead size must cover two whole stripes, which is
  1638. * 2 * (n-1) * chunksize where 'n' is the number of raid devices
  1639. */
  1640. {
  1641. int stripe = (mddev->raid_disks-1) * mddev->chunk_size
  1642. / PAGE_CACHE_SIZE;
  1643. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  1644. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  1645. }
  1646. /* Ok, everything is just fine now */
  1647. if (mddev->bitmap)
  1648. mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
  1649. mddev->queue->unplug_fn = raid5_unplug_device;
  1650. mddev->queue->issue_flush_fn = raid5_issue_flush;
  1651. mddev->array_size = mddev->size * (mddev->raid_disks - 1);
  1652. return 0;
  1653. abort:
  1654. if (conf) {
  1655. print_raid5_conf(conf);
  1656. if (conf->stripe_hashtbl)
  1657. free_pages((unsigned long) conf->stripe_hashtbl,
  1658. HASH_PAGES_ORDER);
  1659. kfree(conf);
  1660. }
  1661. mddev->private = NULL;
  1662. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  1663. return -EIO;
  1664. }
  1665. static int stop (mddev_t *mddev)
  1666. {
  1667. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1668. md_unregister_thread(mddev->thread);
  1669. mddev->thread = NULL;
  1670. shrink_stripes(conf);
  1671. free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
  1672. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1673. kfree(conf);
  1674. mddev->private = NULL;
  1675. return 0;
  1676. }
  1677. #if RAID5_DEBUG
  1678. static void print_sh (struct stripe_head *sh)
  1679. {
  1680. int i;
  1681. printk("sh %llu, pd_idx %d, state %ld.\n",
  1682. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  1683. printk("sh %llu, count %d.\n",
  1684. (unsigned long long)sh->sector, atomic_read(&sh->count));
  1685. printk("sh %llu, ", (unsigned long long)sh->sector);
  1686. for (i = 0; i < sh->raid_conf->raid_disks; i++) {
  1687. printk("(cache%d: %p %ld) ",
  1688. i, sh->dev[i].page, sh->dev[i].flags);
  1689. }
  1690. printk("\n");
  1691. }
  1692. static void printall (raid5_conf_t *conf)
  1693. {
  1694. struct stripe_head *sh;
  1695. int i;
  1696. spin_lock_irq(&conf->device_lock);
  1697. for (i = 0; i < NR_HASH; i++) {
  1698. sh = conf->stripe_hashtbl[i];
  1699. for (; sh; sh = sh->hash_next) {
  1700. if (sh->raid_conf != conf)
  1701. continue;
  1702. print_sh(sh);
  1703. }
  1704. }
  1705. spin_unlock_irq(&conf->device_lock);
  1706. }
  1707. #endif
  1708. static void status (struct seq_file *seq, mddev_t *mddev)
  1709. {
  1710. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1711. int i;
  1712. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  1713. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  1714. for (i = 0; i < conf->raid_disks; i++)
  1715. seq_printf (seq, "%s",
  1716. conf->disks[i].rdev &&
  1717. conf->disks[i].rdev->in_sync ? "U" : "_");
  1718. seq_printf (seq, "]");
  1719. #if RAID5_DEBUG
  1720. #define D(x) \
  1721. seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
  1722. printall(conf);
  1723. #endif
  1724. }
  1725. static void print_raid5_conf (raid5_conf_t *conf)
  1726. {
  1727. int i;
  1728. struct disk_info *tmp;
  1729. printk("RAID5 conf printout:\n");
  1730. if (!conf) {
  1731. printk("(conf==NULL)\n");
  1732. return;
  1733. }
  1734. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  1735. conf->working_disks, conf->failed_disks);
  1736. for (i = 0; i < conf->raid_disks; i++) {
  1737. char b[BDEVNAME_SIZE];
  1738. tmp = conf->disks + i;
  1739. if (tmp->rdev)
  1740. printk(" disk %d, o:%d, dev:%s\n",
  1741. i, !tmp->rdev->faulty,
  1742. bdevname(tmp->rdev->bdev,b));
  1743. }
  1744. }
  1745. static int raid5_spare_active(mddev_t *mddev)
  1746. {
  1747. int i;
  1748. raid5_conf_t *conf = mddev->private;
  1749. struct disk_info *tmp;
  1750. for (i = 0; i < conf->raid_disks; i++) {
  1751. tmp = conf->disks + i;
  1752. if (tmp->rdev
  1753. && !tmp->rdev->faulty
  1754. && !tmp->rdev->in_sync) {
  1755. mddev->degraded--;
  1756. conf->failed_disks--;
  1757. conf->working_disks++;
  1758. tmp->rdev->in_sync = 1;
  1759. }
  1760. }
  1761. print_raid5_conf(conf);
  1762. return 0;
  1763. }
  1764. static int raid5_remove_disk(mddev_t *mddev, int number)
  1765. {
  1766. raid5_conf_t *conf = mddev->private;
  1767. int err = 0;
  1768. mdk_rdev_t *rdev;
  1769. struct disk_info *p = conf->disks + number;
  1770. print_raid5_conf(conf);
  1771. rdev = p->rdev;
  1772. if (rdev) {
  1773. if (rdev->in_sync ||
  1774. atomic_read(&rdev->nr_pending)) {
  1775. err = -EBUSY;
  1776. goto abort;
  1777. }
  1778. p->rdev = NULL;
  1779. synchronize_rcu();
  1780. if (atomic_read(&rdev->nr_pending)) {
  1781. /* lost the race, try later */
  1782. err = -EBUSY;
  1783. p->rdev = rdev;
  1784. }
  1785. }
  1786. abort:
  1787. print_raid5_conf(conf);
  1788. return err;
  1789. }
  1790. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  1791. {
  1792. raid5_conf_t *conf = mddev->private;
  1793. int found = 0;
  1794. int disk;
  1795. struct disk_info *p;
  1796. if (mddev->degraded > 1)
  1797. /* no point adding a device */
  1798. return 0;
  1799. /*
  1800. * find the disk ...
  1801. */
  1802. for (disk=0; disk < mddev->raid_disks; disk++)
  1803. if ((p=conf->disks + disk)->rdev == NULL) {
  1804. rdev->in_sync = 0;
  1805. rdev->raid_disk = disk;
  1806. found = 1;
  1807. if (rdev->saved_raid_disk != disk)
  1808. conf->fullsync = 1;
  1809. p->rdev = rdev;
  1810. break;
  1811. }
  1812. print_raid5_conf(conf);
  1813. return found;
  1814. }
  1815. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  1816. {
  1817. /* no resync is happening, and there is enough space
  1818. * on all devices, so we can resize.
  1819. * We need to make sure resync covers any new space.
  1820. * If the array is shrinking we should possibly wait until
  1821. * any io in the removed space completes, but it hardly seems
  1822. * worth it.
  1823. */
  1824. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  1825. mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
  1826. set_capacity(mddev->gendisk, mddev->array_size << 1);
  1827. mddev->changed = 1;
  1828. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  1829. mddev->recovery_cp = mddev->size << 1;
  1830. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1831. }
  1832. mddev->size = sectors /2;
  1833. mddev->resync_max_sectors = sectors;
  1834. return 0;
  1835. }
  1836. static void raid5_quiesce(mddev_t *mddev, int state)
  1837. {
  1838. raid5_conf_t *conf = mddev_to_conf(mddev);
  1839. switch(state) {
  1840. case 1: /* stop all writes */
  1841. spin_lock_irq(&conf->device_lock);
  1842. conf->quiesce = 1;
  1843. wait_event_lock_irq(conf->wait_for_stripe,
  1844. atomic_read(&conf->active_stripes) == 0,
  1845. conf->device_lock, /* nothing */);
  1846. spin_unlock_irq(&conf->device_lock);
  1847. break;
  1848. case 0: /* re-enable writes */
  1849. spin_lock_irq(&conf->device_lock);
  1850. conf->quiesce = 0;
  1851. wake_up(&conf->wait_for_stripe);
  1852. spin_unlock_irq(&conf->device_lock);
  1853. break;
  1854. }
  1855. if (mddev->thread) {
  1856. if (mddev->bitmap)
  1857. mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
  1858. else
  1859. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1860. md_wakeup_thread(mddev->thread);
  1861. }
  1862. }
  1863. static mdk_personality_t raid5_personality=
  1864. {
  1865. .name = "raid5",
  1866. .owner = THIS_MODULE,
  1867. .make_request = make_request,
  1868. .run = run,
  1869. .stop = stop,
  1870. .status = status,
  1871. .error_handler = error,
  1872. .hot_add_disk = raid5_add_disk,
  1873. .hot_remove_disk= raid5_remove_disk,
  1874. .spare_active = raid5_spare_active,
  1875. .sync_request = sync_request,
  1876. .resize = raid5_resize,
  1877. .quiesce = raid5_quiesce,
  1878. };
  1879. static int __init raid5_init (void)
  1880. {
  1881. return register_md_personality (RAID5, &raid5_personality);
  1882. }
  1883. static void raid5_exit (void)
  1884. {
  1885. unregister_md_personality (RAID5);
  1886. }
  1887. module_init(raid5_init);
  1888. module_exit(raid5_exit);
  1889. MODULE_LICENSE("GPL");
  1890. MODULE_ALIAS("md-personality-4"); /* RAID5 */