raid5.c 60 KB

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