raid5.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157
  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. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->bm_write is the number of the last batch successfully written.
  30. * conf->bm_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is bm_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/highmem.h>
  47. #include <linux/bitops.h>
  48. #include <linux/kthread.h>
  49. #include <asm/atomic.h>
  50. #include "raid6.h"
  51. #include <linux/raid/bitmap.h>
  52. /*
  53. * Stripe cache
  54. */
  55. #define NR_STRIPES 256
  56. #define STRIPE_SIZE PAGE_SIZE
  57. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  58. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  59. #define IO_THRESHOLD 1
  60. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  61. #define HASH_MASK (NR_HASH - 1)
  62. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  63. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  64. * order without overlap. There may be several bio's per stripe+device, and
  65. * a bio could span several devices.
  66. * When walking this list for a particular stripe+device, we must never proceed
  67. * beyond a bio that extends past this device, as the next bio might no longer
  68. * be valid.
  69. * This macro is used to determine the 'next' bio in the list, given the sector
  70. * of the current stripe+device
  71. */
  72. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  73. /*
  74. * The following can be used to debug the driver
  75. */
  76. #define RAID5_DEBUG 0
  77. #define RAID5_PARANOIA 1
  78. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  79. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  80. #else
  81. # define CHECK_DEVLOCK()
  82. #endif
  83. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  84. #if RAID5_DEBUG
  85. #define inline
  86. #define __inline__
  87. #endif
  88. #if !RAID6_USE_EMPTY_ZERO_PAGE
  89. /* In .bss so it's zeroed */
  90. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  91. #endif
  92. static inline int raid6_next_disk(int disk, int raid_disks)
  93. {
  94. disk++;
  95. return (disk < raid_disks) ? disk : 0;
  96. }
  97. static void print_raid5_conf (raid5_conf_t *conf);
  98. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  99. {
  100. if (atomic_dec_and_test(&sh->count)) {
  101. BUG_ON(!list_empty(&sh->lru));
  102. BUG_ON(atomic_read(&conf->active_stripes)==0);
  103. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  104. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  105. list_add_tail(&sh->lru, &conf->delayed_list);
  106. blk_plug_device(conf->mddev->queue);
  107. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  108. sh->bm_seq - conf->seq_write > 0) {
  109. list_add_tail(&sh->lru, &conf->bitmap_list);
  110. blk_plug_device(conf->mddev->queue);
  111. } else {
  112. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  113. list_add_tail(&sh->lru, &conf->handle_list);
  114. }
  115. md_wakeup_thread(conf->mddev->thread);
  116. } else {
  117. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  118. atomic_dec(&conf->preread_active_stripes);
  119. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  120. md_wakeup_thread(conf->mddev->thread);
  121. }
  122. atomic_dec(&conf->active_stripes);
  123. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  124. list_add_tail(&sh->lru, &conf->inactive_list);
  125. wake_up(&conf->wait_for_stripe);
  126. if (conf->retry_read_aligned)
  127. md_wakeup_thread(conf->mddev->thread);
  128. }
  129. }
  130. }
  131. }
  132. static void release_stripe(struct stripe_head *sh)
  133. {
  134. raid5_conf_t *conf = sh->raid_conf;
  135. unsigned long flags;
  136. spin_lock_irqsave(&conf->device_lock, flags);
  137. __release_stripe(conf, sh);
  138. spin_unlock_irqrestore(&conf->device_lock, flags);
  139. }
  140. static inline void remove_hash(struct stripe_head *sh)
  141. {
  142. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  143. hlist_del_init(&sh->hash);
  144. }
  145. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  146. {
  147. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  148. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  149. CHECK_DEVLOCK();
  150. hlist_add_head(&sh->hash, hp);
  151. }
  152. /* find an idle stripe, make sure it is unhashed, and return it. */
  153. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  154. {
  155. struct stripe_head *sh = NULL;
  156. struct list_head *first;
  157. CHECK_DEVLOCK();
  158. if (list_empty(&conf->inactive_list))
  159. goto out;
  160. first = conf->inactive_list.next;
  161. sh = list_entry(first, struct stripe_head, lru);
  162. list_del_init(first);
  163. remove_hash(sh);
  164. atomic_inc(&conf->active_stripes);
  165. out:
  166. return sh;
  167. }
  168. static void shrink_buffers(struct stripe_head *sh, int num)
  169. {
  170. struct page *p;
  171. int i;
  172. for (i=0; i<num ; i++) {
  173. p = sh->dev[i].page;
  174. if (!p)
  175. continue;
  176. sh->dev[i].page = NULL;
  177. put_page(p);
  178. }
  179. }
  180. static int grow_buffers(struct stripe_head *sh, int num)
  181. {
  182. int i;
  183. for (i=0; i<num; i++) {
  184. struct page *page;
  185. if (!(page = alloc_page(GFP_KERNEL))) {
  186. return 1;
  187. }
  188. sh->dev[i].page = page;
  189. }
  190. return 0;
  191. }
  192. static void raid5_build_block (struct stripe_head *sh, int i);
  193. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  194. {
  195. raid5_conf_t *conf = sh->raid_conf;
  196. int i;
  197. BUG_ON(atomic_read(&sh->count) != 0);
  198. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  199. CHECK_DEVLOCK();
  200. PRINTK("init_stripe called, stripe %llu\n",
  201. (unsigned long long)sh->sector);
  202. remove_hash(sh);
  203. sh->sector = sector;
  204. sh->pd_idx = pd_idx;
  205. sh->state = 0;
  206. sh->disks = disks;
  207. for (i = sh->disks; i--; ) {
  208. struct r5dev *dev = &sh->dev[i];
  209. if (dev->toread || dev->towrite || dev->written ||
  210. test_bit(R5_LOCKED, &dev->flags)) {
  211. printk("sector=%llx i=%d %p %p %p %d\n",
  212. (unsigned long long)sh->sector, i, dev->toread,
  213. dev->towrite, dev->written,
  214. test_bit(R5_LOCKED, &dev->flags));
  215. BUG();
  216. }
  217. dev->flags = 0;
  218. raid5_build_block(sh, i);
  219. }
  220. insert_hash(conf, sh);
  221. }
  222. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  223. {
  224. struct stripe_head *sh;
  225. struct hlist_node *hn;
  226. CHECK_DEVLOCK();
  227. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  228. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  229. if (sh->sector == sector && sh->disks == disks)
  230. return sh;
  231. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  232. return NULL;
  233. }
  234. static void unplug_slaves(mddev_t *mddev);
  235. static void raid5_unplug_device(request_queue_t *q);
  236. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  237. int pd_idx, int noblock)
  238. {
  239. struct stripe_head *sh;
  240. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  241. spin_lock_irq(&conf->device_lock);
  242. do {
  243. wait_event_lock_irq(conf->wait_for_stripe,
  244. conf->quiesce == 0,
  245. conf->device_lock, /* nothing */);
  246. sh = __find_stripe(conf, sector, disks);
  247. if (!sh) {
  248. if (!conf->inactive_blocked)
  249. sh = get_free_stripe(conf);
  250. if (noblock && sh == NULL)
  251. break;
  252. if (!sh) {
  253. conf->inactive_blocked = 1;
  254. wait_event_lock_irq(conf->wait_for_stripe,
  255. !list_empty(&conf->inactive_list) &&
  256. (atomic_read(&conf->active_stripes)
  257. < (conf->max_nr_stripes *3/4)
  258. || !conf->inactive_blocked),
  259. conf->device_lock,
  260. raid5_unplug_device(conf->mddev->queue)
  261. );
  262. conf->inactive_blocked = 0;
  263. } else
  264. init_stripe(sh, sector, pd_idx, disks);
  265. } else {
  266. if (atomic_read(&sh->count)) {
  267. BUG_ON(!list_empty(&sh->lru));
  268. } else {
  269. if (!test_bit(STRIPE_HANDLE, &sh->state))
  270. atomic_inc(&conf->active_stripes);
  271. if (list_empty(&sh->lru) &&
  272. !test_bit(STRIPE_EXPANDING, &sh->state))
  273. BUG();
  274. list_del_init(&sh->lru);
  275. }
  276. }
  277. } while (sh == NULL);
  278. if (sh)
  279. atomic_inc(&sh->count);
  280. spin_unlock_irq(&conf->device_lock);
  281. return sh;
  282. }
  283. static int grow_one_stripe(raid5_conf_t *conf)
  284. {
  285. struct stripe_head *sh;
  286. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  287. if (!sh)
  288. return 0;
  289. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  290. sh->raid_conf = conf;
  291. spin_lock_init(&sh->lock);
  292. if (grow_buffers(sh, conf->raid_disks)) {
  293. shrink_buffers(sh, conf->raid_disks);
  294. kmem_cache_free(conf->slab_cache, sh);
  295. return 0;
  296. }
  297. sh->disks = conf->raid_disks;
  298. /* we just created an active stripe so... */
  299. atomic_set(&sh->count, 1);
  300. atomic_inc(&conf->active_stripes);
  301. INIT_LIST_HEAD(&sh->lru);
  302. release_stripe(sh);
  303. return 1;
  304. }
  305. static int grow_stripes(raid5_conf_t *conf, int num)
  306. {
  307. struct kmem_cache *sc;
  308. int devs = conf->raid_disks;
  309. sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
  310. sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
  311. conf->active_name = 0;
  312. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  313. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  314. 0, 0, NULL, NULL);
  315. if (!sc)
  316. return 1;
  317. conf->slab_cache = sc;
  318. conf->pool_size = devs;
  319. while (num--)
  320. if (!grow_one_stripe(conf))
  321. return 1;
  322. return 0;
  323. }
  324. #ifdef CONFIG_MD_RAID5_RESHAPE
  325. static int resize_stripes(raid5_conf_t *conf, int newsize)
  326. {
  327. /* Make all the stripes able to hold 'newsize' devices.
  328. * New slots in each stripe get 'page' set to a new page.
  329. *
  330. * This happens in stages:
  331. * 1/ create a new kmem_cache and allocate the required number of
  332. * stripe_heads.
  333. * 2/ gather all the old stripe_heads and tranfer the pages across
  334. * to the new stripe_heads. This will have the side effect of
  335. * freezing the array as once all stripe_heads have been collected,
  336. * no IO will be possible. Old stripe heads are freed once their
  337. * pages have been transferred over, and the old kmem_cache is
  338. * freed when all stripes are done.
  339. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  340. * we simple return a failre status - no need to clean anything up.
  341. * 4/ allocate new pages for the new slots in the new stripe_heads.
  342. * If this fails, we don't bother trying the shrink the
  343. * stripe_heads down again, we just leave them as they are.
  344. * As each stripe_head is processed the new one is released into
  345. * active service.
  346. *
  347. * Once step2 is started, we cannot afford to wait for a write,
  348. * so we use GFP_NOIO allocations.
  349. */
  350. struct stripe_head *osh, *nsh;
  351. LIST_HEAD(newstripes);
  352. struct disk_info *ndisks;
  353. int err = 0;
  354. struct kmem_cache *sc;
  355. int i;
  356. if (newsize <= conf->pool_size)
  357. return 0; /* never bother to shrink */
  358. md_allow_write(conf->mddev);
  359. /* Step 1 */
  360. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  361. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  362. 0, 0, NULL, NULL);
  363. if (!sc)
  364. return -ENOMEM;
  365. for (i = conf->max_nr_stripes; i; i--) {
  366. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  367. if (!nsh)
  368. break;
  369. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  370. nsh->raid_conf = conf;
  371. spin_lock_init(&nsh->lock);
  372. list_add(&nsh->lru, &newstripes);
  373. }
  374. if (i) {
  375. /* didn't get enough, give up */
  376. while (!list_empty(&newstripes)) {
  377. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  378. list_del(&nsh->lru);
  379. kmem_cache_free(sc, nsh);
  380. }
  381. kmem_cache_destroy(sc);
  382. return -ENOMEM;
  383. }
  384. /* Step 2 - Must use GFP_NOIO now.
  385. * OK, we have enough stripes, start collecting inactive
  386. * stripes and copying them over
  387. */
  388. list_for_each_entry(nsh, &newstripes, lru) {
  389. spin_lock_irq(&conf->device_lock);
  390. wait_event_lock_irq(conf->wait_for_stripe,
  391. !list_empty(&conf->inactive_list),
  392. conf->device_lock,
  393. unplug_slaves(conf->mddev)
  394. );
  395. osh = get_free_stripe(conf);
  396. spin_unlock_irq(&conf->device_lock);
  397. atomic_set(&nsh->count, 1);
  398. for(i=0; i<conf->pool_size; i++)
  399. nsh->dev[i].page = osh->dev[i].page;
  400. for( ; i<newsize; i++)
  401. nsh->dev[i].page = NULL;
  402. kmem_cache_free(conf->slab_cache, osh);
  403. }
  404. kmem_cache_destroy(conf->slab_cache);
  405. /* Step 3.
  406. * At this point, we are holding all the stripes so the array
  407. * is completely stalled, so now is a good time to resize
  408. * conf->disks.
  409. */
  410. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  411. if (ndisks) {
  412. for (i=0; i<conf->raid_disks; i++)
  413. ndisks[i] = conf->disks[i];
  414. kfree(conf->disks);
  415. conf->disks = ndisks;
  416. } else
  417. err = -ENOMEM;
  418. /* Step 4, return new stripes to service */
  419. while(!list_empty(&newstripes)) {
  420. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  421. list_del_init(&nsh->lru);
  422. for (i=conf->raid_disks; i < newsize; i++)
  423. if (nsh->dev[i].page == NULL) {
  424. struct page *p = alloc_page(GFP_NOIO);
  425. nsh->dev[i].page = p;
  426. if (!p)
  427. err = -ENOMEM;
  428. }
  429. release_stripe(nsh);
  430. }
  431. /* critical section pass, GFP_NOIO no longer needed */
  432. conf->slab_cache = sc;
  433. conf->active_name = 1-conf->active_name;
  434. conf->pool_size = newsize;
  435. return err;
  436. }
  437. #endif
  438. static int drop_one_stripe(raid5_conf_t *conf)
  439. {
  440. struct stripe_head *sh;
  441. spin_lock_irq(&conf->device_lock);
  442. sh = get_free_stripe(conf);
  443. spin_unlock_irq(&conf->device_lock);
  444. if (!sh)
  445. return 0;
  446. BUG_ON(atomic_read(&sh->count));
  447. shrink_buffers(sh, conf->pool_size);
  448. kmem_cache_free(conf->slab_cache, sh);
  449. atomic_dec(&conf->active_stripes);
  450. return 1;
  451. }
  452. static void shrink_stripes(raid5_conf_t *conf)
  453. {
  454. while (drop_one_stripe(conf))
  455. ;
  456. if (conf->slab_cache)
  457. kmem_cache_destroy(conf->slab_cache);
  458. conf->slab_cache = NULL;
  459. }
  460. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  461. int error)
  462. {
  463. struct stripe_head *sh = bi->bi_private;
  464. raid5_conf_t *conf = sh->raid_conf;
  465. int disks = sh->disks, i;
  466. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  467. char b[BDEVNAME_SIZE];
  468. mdk_rdev_t *rdev;
  469. if (bi->bi_size)
  470. return 1;
  471. for (i=0 ; i<disks; i++)
  472. if (bi == &sh->dev[i].req)
  473. break;
  474. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  475. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  476. uptodate);
  477. if (i == disks) {
  478. BUG();
  479. return 0;
  480. }
  481. if (uptodate) {
  482. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  483. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  484. rdev = conf->disks[i].rdev;
  485. printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
  486. mdname(conf->mddev), STRIPE_SECTORS,
  487. (unsigned long long)sh->sector + rdev->data_offset,
  488. bdevname(rdev->bdev, b));
  489. clear_bit(R5_ReadError, &sh->dev[i].flags);
  490. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  491. }
  492. if (atomic_read(&conf->disks[i].rdev->read_errors))
  493. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  494. } else {
  495. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  496. int retry = 0;
  497. rdev = conf->disks[i].rdev;
  498. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  499. atomic_inc(&rdev->read_errors);
  500. if (conf->mddev->degraded)
  501. printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
  502. mdname(conf->mddev),
  503. (unsigned long long)sh->sector + rdev->data_offset,
  504. bdn);
  505. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  506. /* Oh, no!!! */
  507. printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
  508. mdname(conf->mddev),
  509. (unsigned long long)sh->sector + rdev->data_offset,
  510. bdn);
  511. else if (atomic_read(&rdev->read_errors)
  512. > conf->max_nr_stripes)
  513. printk(KERN_WARNING
  514. "raid5:%s: Too many read errors, failing device %s.\n",
  515. mdname(conf->mddev), bdn);
  516. else
  517. retry = 1;
  518. if (retry)
  519. set_bit(R5_ReadError, &sh->dev[i].flags);
  520. else {
  521. clear_bit(R5_ReadError, &sh->dev[i].flags);
  522. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  523. md_error(conf->mddev, rdev);
  524. }
  525. }
  526. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  527. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  528. set_bit(STRIPE_HANDLE, &sh->state);
  529. release_stripe(sh);
  530. return 0;
  531. }
  532. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  533. int error)
  534. {
  535. struct stripe_head *sh = bi->bi_private;
  536. raid5_conf_t *conf = sh->raid_conf;
  537. int disks = sh->disks, i;
  538. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  539. if (bi->bi_size)
  540. return 1;
  541. for (i=0 ; i<disks; i++)
  542. if (bi == &sh->dev[i].req)
  543. break;
  544. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  545. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  546. uptodate);
  547. if (i == disks) {
  548. BUG();
  549. return 0;
  550. }
  551. if (!uptodate)
  552. md_error(conf->mddev, conf->disks[i].rdev);
  553. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  554. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  555. set_bit(STRIPE_HANDLE, &sh->state);
  556. release_stripe(sh);
  557. return 0;
  558. }
  559. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  560. static void raid5_build_block (struct stripe_head *sh, int i)
  561. {
  562. struct r5dev *dev = &sh->dev[i];
  563. bio_init(&dev->req);
  564. dev->req.bi_io_vec = &dev->vec;
  565. dev->req.bi_vcnt++;
  566. dev->req.bi_max_vecs++;
  567. dev->vec.bv_page = dev->page;
  568. dev->vec.bv_len = STRIPE_SIZE;
  569. dev->vec.bv_offset = 0;
  570. dev->req.bi_sector = sh->sector;
  571. dev->req.bi_private = sh;
  572. dev->flags = 0;
  573. dev->sector = compute_blocknr(sh, i);
  574. }
  575. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  576. {
  577. char b[BDEVNAME_SIZE];
  578. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  579. PRINTK("raid5: error called\n");
  580. if (!test_bit(Faulty, &rdev->flags)) {
  581. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  582. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  583. unsigned long flags;
  584. spin_lock_irqsave(&conf->device_lock, flags);
  585. mddev->degraded++;
  586. spin_unlock_irqrestore(&conf->device_lock, flags);
  587. /*
  588. * if recovery was running, make sure it aborts.
  589. */
  590. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  591. }
  592. set_bit(Faulty, &rdev->flags);
  593. printk (KERN_ALERT
  594. "raid5: Disk failure on %s, disabling device."
  595. " Operation continuing on %d devices\n",
  596. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  597. }
  598. }
  599. /*
  600. * Input: a 'big' sector number,
  601. * Output: index of the data and parity disk, and the sector # in them.
  602. */
  603. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  604. unsigned int data_disks, unsigned int * dd_idx,
  605. unsigned int * pd_idx, raid5_conf_t *conf)
  606. {
  607. long stripe;
  608. unsigned long chunk_number;
  609. unsigned int chunk_offset;
  610. sector_t new_sector;
  611. int sectors_per_chunk = conf->chunk_size >> 9;
  612. /* First compute the information on this sector */
  613. /*
  614. * Compute the chunk number and the sector offset inside the chunk
  615. */
  616. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  617. chunk_number = r_sector;
  618. BUG_ON(r_sector != chunk_number);
  619. /*
  620. * Compute the stripe number
  621. */
  622. stripe = chunk_number / data_disks;
  623. /*
  624. * Compute the data disk and parity disk indexes inside the stripe
  625. */
  626. *dd_idx = chunk_number % data_disks;
  627. /*
  628. * Select the parity disk based on the user selected algorithm.
  629. */
  630. switch(conf->level) {
  631. case 4:
  632. *pd_idx = data_disks;
  633. break;
  634. case 5:
  635. switch (conf->algorithm) {
  636. case ALGORITHM_LEFT_ASYMMETRIC:
  637. *pd_idx = data_disks - stripe % raid_disks;
  638. if (*dd_idx >= *pd_idx)
  639. (*dd_idx)++;
  640. break;
  641. case ALGORITHM_RIGHT_ASYMMETRIC:
  642. *pd_idx = stripe % raid_disks;
  643. if (*dd_idx >= *pd_idx)
  644. (*dd_idx)++;
  645. break;
  646. case ALGORITHM_LEFT_SYMMETRIC:
  647. *pd_idx = data_disks - stripe % raid_disks;
  648. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  649. break;
  650. case ALGORITHM_RIGHT_SYMMETRIC:
  651. *pd_idx = stripe % raid_disks;
  652. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  653. break;
  654. default:
  655. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  656. conf->algorithm);
  657. }
  658. break;
  659. case 6:
  660. /**** FIX THIS ****/
  661. switch (conf->algorithm) {
  662. case ALGORITHM_LEFT_ASYMMETRIC:
  663. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  664. if (*pd_idx == raid_disks-1)
  665. (*dd_idx)++; /* Q D D D P */
  666. else if (*dd_idx >= *pd_idx)
  667. (*dd_idx) += 2; /* D D P Q D */
  668. break;
  669. case ALGORITHM_RIGHT_ASYMMETRIC:
  670. *pd_idx = stripe % raid_disks;
  671. if (*pd_idx == raid_disks-1)
  672. (*dd_idx)++; /* Q D D D P */
  673. else if (*dd_idx >= *pd_idx)
  674. (*dd_idx) += 2; /* D D P Q D */
  675. break;
  676. case ALGORITHM_LEFT_SYMMETRIC:
  677. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  678. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  679. break;
  680. case ALGORITHM_RIGHT_SYMMETRIC:
  681. *pd_idx = stripe % raid_disks;
  682. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  683. break;
  684. default:
  685. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  686. conf->algorithm);
  687. }
  688. break;
  689. }
  690. /*
  691. * Finally, compute the new sector number
  692. */
  693. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  694. return new_sector;
  695. }
  696. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  697. {
  698. raid5_conf_t *conf = sh->raid_conf;
  699. int raid_disks = sh->disks;
  700. int data_disks = raid_disks - conf->max_degraded;
  701. sector_t new_sector = sh->sector, check;
  702. int sectors_per_chunk = conf->chunk_size >> 9;
  703. sector_t stripe;
  704. int chunk_offset;
  705. int chunk_number, dummy1, dummy2, dd_idx = i;
  706. sector_t r_sector;
  707. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  708. stripe = new_sector;
  709. BUG_ON(new_sector != stripe);
  710. if (i == sh->pd_idx)
  711. return 0;
  712. switch(conf->level) {
  713. case 4: break;
  714. case 5:
  715. switch (conf->algorithm) {
  716. case ALGORITHM_LEFT_ASYMMETRIC:
  717. case ALGORITHM_RIGHT_ASYMMETRIC:
  718. if (i > sh->pd_idx)
  719. i--;
  720. break;
  721. case ALGORITHM_LEFT_SYMMETRIC:
  722. case ALGORITHM_RIGHT_SYMMETRIC:
  723. if (i < sh->pd_idx)
  724. i += raid_disks;
  725. i -= (sh->pd_idx + 1);
  726. break;
  727. default:
  728. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  729. conf->algorithm);
  730. }
  731. break;
  732. case 6:
  733. if (i == raid6_next_disk(sh->pd_idx, raid_disks))
  734. return 0; /* It is the Q disk */
  735. switch (conf->algorithm) {
  736. case ALGORITHM_LEFT_ASYMMETRIC:
  737. case ALGORITHM_RIGHT_ASYMMETRIC:
  738. if (sh->pd_idx == raid_disks-1)
  739. i--; /* Q D D D P */
  740. else if (i > sh->pd_idx)
  741. i -= 2; /* D D P Q D */
  742. break;
  743. case ALGORITHM_LEFT_SYMMETRIC:
  744. case ALGORITHM_RIGHT_SYMMETRIC:
  745. if (sh->pd_idx == raid_disks-1)
  746. i--; /* Q D D D P */
  747. else {
  748. /* D D P Q D */
  749. if (i < sh->pd_idx)
  750. i += raid_disks;
  751. i -= (sh->pd_idx + 2);
  752. }
  753. break;
  754. default:
  755. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  756. conf->algorithm);
  757. }
  758. break;
  759. }
  760. chunk_number = stripe * data_disks + i;
  761. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  762. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  763. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  764. printk(KERN_ERR "compute_blocknr: map not correct\n");
  765. return 0;
  766. }
  767. return r_sector;
  768. }
  769. /*
  770. * Copy data between a page in the stripe cache, and one or more bion
  771. * The page could align with the middle of the bio, or there could be
  772. * several bion, each with several bio_vecs, which cover part of the page
  773. * Multiple bion are linked together on bi_next. There may be extras
  774. * at the end of this list. We ignore them.
  775. */
  776. static void copy_data(int frombio, struct bio *bio,
  777. struct page *page,
  778. sector_t sector)
  779. {
  780. char *pa = page_address(page);
  781. struct bio_vec *bvl;
  782. int i;
  783. int page_offset;
  784. if (bio->bi_sector >= sector)
  785. page_offset = (signed)(bio->bi_sector - sector) * 512;
  786. else
  787. page_offset = (signed)(sector - bio->bi_sector) * -512;
  788. bio_for_each_segment(bvl, bio, i) {
  789. int len = bio_iovec_idx(bio,i)->bv_len;
  790. int clen;
  791. int b_offset = 0;
  792. if (page_offset < 0) {
  793. b_offset = -page_offset;
  794. page_offset += b_offset;
  795. len -= b_offset;
  796. }
  797. if (len > 0 && page_offset + len > STRIPE_SIZE)
  798. clen = STRIPE_SIZE - page_offset;
  799. else clen = len;
  800. if (clen > 0) {
  801. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  802. if (frombio)
  803. memcpy(pa+page_offset, ba+b_offset, clen);
  804. else
  805. memcpy(ba+b_offset, pa+page_offset, clen);
  806. __bio_kunmap_atomic(ba, KM_USER0);
  807. }
  808. if (clen < len) /* hit end of page */
  809. break;
  810. page_offset += len;
  811. }
  812. }
  813. #define check_xor() do { \
  814. if (count == MAX_XOR_BLOCKS) { \
  815. xor_block(count, STRIPE_SIZE, ptr); \
  816. count = 1; \
  817. } \
  818. } while(0)
  819. static void compute_block(struct stripe_head *sh, int dd_idx)
  820. {
  821. int i, count, disks = sh->disks;
  822. void *ptr[MAX_XOR_BLOCKS], *p;
  823. PRINTK("compute_block, stripe %llu, idx %d\n",
  824. (unsigned long long)sh->sector, dd_idx);
  825. ptr[0] = page_address(sh->dev[dd_idx].page);
  826. memset(ptr[0], 0, STRIPE_SIZE);
  827. count = 1;
  828. for (i = disks ; i--; ) {
  829. if (i == dd_idx)
  830. continue;
  831. p = page_address(sh->dev[i].page);
  832. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  833. ptr[count++] = p;
  834. else
  835. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  836. " not present\n", dd_idx,
  837. (unsigned long long)sh->sector, i);
  838. check_xor();
  839. }
  840. if (count != 1)
  841. xor_block(count, STRIPE_SIZE, ptr);
  842. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  843. }
  844. static void compute_parity5(struct stripe_head *sh, int method)
  845. {
  846. raid5_conf_t *conf = sh->raid_conf;
  847. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  848. void *ptr[MAX_XOR_BLOCKS];
  849. struct bio *chosen;
  850. PRINTK("compute_parity5, stripe %llu, method %d\n",
  851. (unsigned long long)sh->sector, method);
  852. count = 1;
  853. ptr[0] = page_address(sh->dev[pd_idx].page);
  854. switch(method) {
  855. case READ_MODIFY_WRITE:
  856. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
  857. for (i=disks ; i-- ;) {
  858. if (i==pd_idx)
  859. continue;
  860. if (sh->dev[i].towrite &&
  861. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  862. ptr[count++] = page_address(sh->dev[i].page);
  863. chosen = sh->dev[i].towrite;
  864. sh->dev[i].towrite = NULL;
  865. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  866. wake_up(&conf->wait_for_overlap);
  867. BUG_ON(sh->dev[i].written);
  868. sh->dev[i].written = chosen;
  869. check_xor();
  870. }
  871. }
  872. break;
  873. case RECONSTRUCT_WRITE:
  874. memset(ptr[0], 0, STRIPE_SIZE);
  875. for (i= disks; i-- ;)
  876. if (i!=pd_idx && sh->dev[i].towrite) {
  877. chosen = sh->dev[i].towrite;
  878. sh->dev[i].towrite = NULL;
  879. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  880. wake_up(&conf->wait_for_overlap);
  881. BUG_ON(sh->dev[i].written);
  882. sh->dev[i].written = chosen;
  883. }
  884. break;
  885. case CHECK_PARITY:
  886. break;
  887. }
  888. if (count>1) {
  889. xor_block(count, STRIPE_SIZE, ptr);
  890. count = 1;
  891. }
  892. for (i = disks; i--;)
  893. if (sh->dev[i].written) {
  894. sector_t sector = sh->dev[i].sector;
  895. struct bio *wbi = sh->dev[i].written;
  896. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  897. copy_data(1, wbi, sh->dev[i].page, sector);
  898. wbi = r5_next_bio(wbi, sector);
  899. }
  900. set_bit(R5_LOCKED, &sh->dev[i].flags);
  901. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  902. }
  903. switch(method) {
  904. case RECONSTRUCT_WRITE:
  905. case CHECK_PARITY:
  906. for (i=disks; i--;)
  907. if (i != pd_idx) {
  908. ptr[count++] = page_address(sh->dev[i].page);
  909. check_xor();
  910. }
  911. break;
  912. case READ_MODIFY_WRITE:
  913. for (i = disks; i--;)
  914. if (sh->dev[i].written) {
  915. ptr[count++] = page_address(sh->dev[i].page);
  916. check_xor();
  917. }
  918. }
  919. if (count != 1)
  920. xor_block(count, STRIPE_SIZE, ptr);
  921. if (method != CHECK_PARITY) {
  922. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  923. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  924. } else
  925. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  926. }
  927. static void compute_parity6(struct stripe_head *sh, int method)
  928. {
  929. raid6_conf_t *conf = sh->raid_conf;
  930. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  931. struct bio *chosen;
  932. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  933. void *ptrs[disks];
  934. qd_idx = raid6_next_disk(pd_idx, disks);
  935. d0_idx = raid6_next_disk(qd_idx, disks);
  936. PRINTK("compute_parity, stripe %llu, method %d\n",
  937. (unsigned long long)sh->sector, method);
  938. switch(method) {
  939. case READ_MODIFY_WRITE:
  940. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  941. case RECONSTRUCT_WRITE:
  942. for (i= disks; i-- ;)
  943. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  944. chosen = sh->dev[i].towrite;
  945. sh->dev[i].towrite = NULL;
  946. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  947. wake_up(&conf->wait_for_overlap);
  948. BUG_ON(sh->dev[i].written);
  949. sh->dev[i].written = chosen;
  950. }
  951. break;
  952. case CHECK_PARITY:
  953. BUG(); /* Not implemented yet */
  954. }
  955. for (i = disks; i--;)
  956. if (sh->dev[i].written) {
  957. sector_t sector = sh->dev[i].sector;
  958. struct bio *wbi = sh->dev[i].written;
  959. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  960. copy_data(1, wbi, sh->dev[i].page, sector);
  961. wbi = r5_next_bio(wbi, sector);
  962. }
  963. set_bit(R5_LOCKED, &sh->dev[i].flags);
  964. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  965. }
  966. // switch(method) {
  967. // case RECONSTRUCT_WRITE:
  968. // case CHECK_PARITY:
  969. // case UPDATE_PARITY:
  970. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  971. /* FIX: Is this ordering of drives even remotely optimal? */
  972. count = 0;
  973. i = d0_idx;
  974. do {
  975. ptrs[count++] = page_address(sh->dev[i].page);
  976. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  977. printk("block %d/%d not uptodate on parity calc\n", i,count);
  978. i = raid6_next_disk(i, disks);
  979. } while ( i != d0_idx );
  980. // break;
  981. // }
  982. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  983. switch(method) {
  984. case RECONSTRUCT_WRITE:
  985. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  986. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  987. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  988. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  989. break;
  990. case UPDATE_PARITY:
  991. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  992. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  993. break;
  994. }
  995. }
  996. /* Compute one missing block */
  997. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  998. {
  999. int i, count, disks = sh->disks;
  1000. void *ptr[MAX_XOR_BLOCKS], *p;
  1001. int pd_idx = sh->pd_idx;
  1002. int qd_idx = raid6_next_disk(pd_idx, disks);
  1003. PRINTK("compute_block_1, stripe %llu, idx %d\n",
  1004. (unsigned long long)sh->sector, dd_idx);
  1005. if ( dd_idx == qd_idx ) {
  1006. /* We're actually computing the Q drive */
  1007. compute_parity6(sh, UPDATE_PARITY);
  1008. } else {
  1009. ptr[0] = page_address(sh->dev[dd_idx].page);
  1010. if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
  1011. count = 1;
  1012. for (i = disks ; i--; ) {
  1013. if (i == dd_idx || i == qd_idx)
  1014. continue;
  1015. p = page_address(sh->dev[i].page);
  1016. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1017. ptr[count++] = p;
  1018. else
  1019. printk("compute_block() %d, stripe %llu, %d"
  1020. " not present\n", dd_idx,
  1021. (unsigned long long)sh->sector, i);
  1022. check_xor();
  1023. }
  1024. if (count != 1)
  1025. xor_block(count, STRIPE_SIZE, ptr);
  1026. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1027. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1028. }
  1029. }
  1030. /* Compute two missing blocks */
  1031. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1032. {
  1033. int i, count, disks = sh->disks;
  1034. int pd_idx = sh->pd_idx;
  1035. int qd_idx = raid6_next_disk(pd_idx, disks);
  1036. int d0_idx = raid6_next_disk(qd_idx, disks);
  1037. int faila, failb;
  1038. /* faila and failb are disk numbers relative to d0_idx */
  1039. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1040. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1041. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1042. BUG_ON(faila == failb);
  1043. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1044. PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1045. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1046. if ( failb == disks-1 ) {
  1047. /* Q disk is one of the missing disks */
  1048. if ( faila == disks-2 ) {
  1049. /* Missing P+Q, just recompute */
  1050. compute_parity6(sh, UPDATE_PARITY);
  1051. return;
  1052. } else {
  1053. /* We're missing D+Q; recompute D from P */
  1054. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1055. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1056. return;
  1057. }
  1058. }
  1059. /* We're missing D+P or D+D; build pointer table */
  1060. {
  1061. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1062. void *ptrs[disks];
  1063. count = 0;
  1064. i = d0_idx;
  1065. do {
  1066. ptrs[count++] = page_address(sh->dev[i].page);
  1067. i = raid6_next_disk(i, disks);
  1068. if (i != dd_idx1 && i != dd_idx2 &&
  1069. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1070. printk("compute_2 with missing block %d/%d\n", count, i);
  1071. } while ( i != d0_idx );
  1072. if ( failb == disks-2 ) {
  1073. /* We're missing D+P. */
  1074. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1075. } else {
  1076. /* We're missing D+D. */
  1077. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1078. }
  1079. /* Both the above update both missing blocks */
  1080. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1081. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1082. }
  1083. }
  1084. /*
  1085. * Each stripe/dev can have one or more bion attached.
  1086. * toread/towrite point to the first in a chain.
  1087. * The bi_next chain must be in order.
  1088. */
  1089. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1090. {
  1091. struct bio **bip;
  1092. raid5_conf_t *conf = sh->raid_conf;
  1093. int firstwrite=0;
  1094. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  1095. (unsigned long long)bi->bi_sector,
  1096. (unsigned long long)sh->sector);
  1097. spin_lock(&sh->lock);
  1098. spin_lock_irq(&conf->device_lock);
  1099. if (forwrite) {
  1100. bip = &sh->dev[dd_idx].towrite;
  1101. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1102. firstwrite = 1;
  1103. } else
  1104. bip = &sh->dev[dd_idx].toread;
  1105. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1106. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1107. goto overlap;
  1108. bip = & (*bip)->bi_next;
  1109. }
  1110. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1111. goto overlap;
  1112. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1113. if (*bip)
  1114. bi->bi_next = *bip;
  1115. *bip = bi;
  1116. bi->bi_phys_segments ++;
  1117. spin_unlock_irq(&conf->device_lock);
  1118. spin_unlock(&sh->lock);
  1119. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1120. (unsigned long long)bi->bi_sector,
  1121. (unsigned long long)sh->sector, dd_idx);
  1122. if (conf->mddev->bitmap && firstwrite) {
  1123. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1124. STRIPE_SECTORS, 0);
  1125. sh->bm_seq = conf->seq_flush+1;
  1126. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1127. }
  1128. if (forwrite) {
  1129. /* check if page is covered */
  1130. sector_t sector = sh->dev[dd_idx].sector;
  1131. for (bi=sh->dev[dd_idx].towrite;
  1132. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1133. bi && bi->bi_sector <= sector;
  1134. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1135. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1136. sector = bi->bi_sector + (bi->bi_size>>9);
  1137. }
  1138. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1139. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1140. }
  1141. return 1;
  1142. overlap:
  1143. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1144. spin_unlock_irq(&conf->device_lock);
  1145. spin_unlock(&sh->lock);
  1146. return 0;
  1147. }
  1148. static void end_reshape(raid5_conf_t *conf);
  1149. static int page_is_zero(struct page *p)
  1150. {
  1151. char *a = page_address(p);
  1152. return ((*(u32*)a) == 0 &&
  1153. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1154. }
  1155. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1156. {
  1157. int sectors_per_chunk = conf->chunk_size >> 9;
  1158. int pd_idx, dd_idx;
  1159. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1160. raid5_compute_sector(stripe * (disks - conf->max_degraded)
  1161. *sectors_per_chunk + chunk_offset,
  1162. disks, disks - conf->max_degraded,
  1163. &dd_idx, &pd_idx, conf);
  1164. return pd_idx;
  1165. }
  1166. /*
  1167. * handle_stripe - do things to a stripe.
  1168. *
  1169. * We lock the stripe and then examine the state of various bits
  1170. * to see what needs to be done.
  1171. * Possible results:
  1172. * return some read request which now have data
  1173. * return some write requests which are safely on disc
  1174. * schedule a read on some buffers
  1175. * schedule a write of some buffers
  1176. * return confirmation of parity correctness
  1177. *
  1178. * Parity calculations are done inside the stripe lock
  1179. * buffers are taken off read_list or write_list, and bh_cache buffers
  1180. * get BH_Lock set before the stripe lock is released.
  1181. *
  1182. */
  1183. static void handle_stripe5(struct stripe_head *sh)
  1184. {
  1185. raid5_conf_t *conf = sh->raid_conf;
  1186. int disks = sh->disks;
  1187. struct bio *return_bi= NULL;
  1188. struct bio *bi;
  1189. int i;
  1190. int syncing, expanding, expanded;
  1191. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1192. int non_overwrite = 0;
  1193. int failed_num=0;
  1194. struct r5dev *dev;
  1195. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  1196. (unsigned long long)sh->sector, atomic_read(&sh->count),
  1197. sh->pd_idx);
  1198. spin_lock(&sh->lock);
  1199. clear_bit(STRIPE_HANDLE, &sh->state);
  1200. clear_bit(STRIPE_DELAYED, &sh->state);
  1201. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1202. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1203. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  1204. /* Now to look around and see what can be done */
  1205. rcu_read_lock();
  1206. for (i=disks; i--; ) {
  1207. mdk_rdev_t *rdev;
  1208. dev = &sh->dev[i];
  1209. clear_bit(R5_Insync, &dev->flags);
  1210. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1211. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1212. /* maybe we can reply to a read */
  1213. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1214. struct bio *rbi, *rbi2;
  1215. PRINTK("Return read for disc %d\n", i);
  1216. spin_lock_irq(&conf->device_lock);
  1217. rbi = dev->toread;
  1218. dev->toread = NULL;
  1219. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1220. wake_up(&conf->wait_for_overlap);
  1221. spin_unlock_irq(&conf->device_lock);
  1222. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1223. copy_data(0, rbi, dev->page, dev->sector);
  1224. rbi2 = r5_next_bio(rbi, dev->sector);
  1225. spin_lock_irq(&conf->device_lock);
  1226. if (--rbi->bi_phys_segments == 0) {
  1227. rbi->bi_next = return_bi;
  1228. return_bi = rbi;
  1229. }
  1230. spin_unlock_irq(&conf->device_lock);
  1231. rbi = rbi2;
  1232. }
  1233. }
  1234. /* now count some things */
  1235. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1236. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1237. if (dev->toread) to_read++;
  1238. if (dev->towrite) {
  1239. to_write++;
  1240. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1241. non_overwrite++;
  1242. }
  1243. if (dev->written) written++;
  1244. rdev = rcu_dereference(conf->disks[i].rdev);
  1245. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1246. /* The ReadError flag will just be confusing now */
  1247. clear_bit(R5_ReadError, &dev->flags);
  1248. clear_bit(R5_ReWrite, &dev->flags);
  1249. }
  1250. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1251. || test_bit(R5_ReadError, &dev->flags)) {
  1252. failed++;
  1253. failed_num = i;
  1254. } else
  1255. set_bit(R5_Insync, &dev->flags);
  1256. }
  1257. rcu_read_unlock();
  1258. PRINTK("locked=%d uptodate=%d to_read=%d"
  1259. " to_write=%d failed=%d failed_num=%d\n",
  1260. locked, uptodate, to_read, to_write, failed, failed_num);
  1261. /* check if the array has lost two devices and, if so, some requests might
  1262. * need to be failed
  1263. */
  1264. if (failed > 1 && to_read+to_write+written) {
  1265. for (i=disks; i--; ) {
  1266. int bitmap_end = 0;
  1267. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1268. mdk_rdev_t *rdev;
  1269. rcu_read_lock();
  1270. rdev = rcu_dereference(conf->disks[i].rdev);
  1271. if (rdev && test_bit(In_sync, &rdev->flags))
  1272. /* multiple read failures in one stripe */
  1273. md_error(conf->mddev, rdev);
  1274. rcu_read_unlock();
  1275. }
  1276. spin_lock_irq(&conf->device_lock);
  1277. /* fail all writes first */
  1278. bi = sh->dev[i].towrite;
  1279. sh->dev[i].towrite = NULL;
  1280. if (bi) { to_write--; bitmap_end = 1; }
  1281. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1282. wake_up(&conf->wait_for_overlap);
  1283. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1284. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1285. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1286. if (--bi->bi_phys_segments == 0) {
  1287. md_write_end(conf->mddev);
  1288. bi->bi_next = return_bi;
  1289. return_bi = bi;
  1290. }
  1291. bi = nextbi;
  1292. }
  1293. /* and fail all 'written' */
  1294. bi = sh->dev[i].written;
  1295. sh->dev[i].written = NULL;
  1296. if (bi) bitmap_end = 1;
  1297. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1298. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1299. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1300. if (--bi->bi_phys_segments == 0) {
  1301. md_write_end(conf->mddev);
  1302. bi->bi_next = return_bi;
  1303. return_bi = bi;
  1304. }
  1305. bi = bi2;
  1306. }
  1307. /* fail any reads if this device is non-operational */
  1308. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1309. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1310. bi = sh->dev[i].toread;
  1311. sh->dev[i].toread = NULL;
  1312. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1313. wake_up(&conf->wait_for_overlap);
  1314. if (bi) to_read--;
  1315. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1316. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1317. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1318. if (--bi->bi_phys_segments == 0) {
  1319. bi->bi_next = return_bi;
  1320. return_bi = bi;
  1321. }
  1322. bi = nextbi;
  1323. }
  1324. }
  1325. spin_unlock_irq(&conf->device_lock);
  1326. if (bitmap_end)
  1327. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1328. STRIPE_SECTORS, 0, 0);
  1329. }
  1330. }
  1331. if (failed > 1 && syncing) {
  1332. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1333. clear_bit(STRIPE_SYNCING, &sh->state);
  1334. syncing = 0;
  1335. }
  1336. /* might be able to return some write requests if the parity block
  1337. * is safe, or on a failed drive
  1338. */
  1339. dev = &sh->dev[sh->pd_idx];
  1340. if ( written &&
  1341. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  1342. test_bit(R5_UPTODATE, &dev->flags))
  1343. || (failed == 1 && failed_num == sh->pd_idx))
  1344. ) {
  1345. /* any written block on an uptodate or failed drive can be returned.
  1346. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1347. * never LOCKED, so we don't need to test 'failed' directly.
  1348. */
  1349. for (i=disks; i--; )
  1350. if (sh->dev[i].written) {
  1351. dev = &sh->dev[i];
  1352. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1353. test_bit(R5_UPTODATE, &dev->flags) ) {
  1354. /* We can return any write requests */
  1355. struct bio *wbi, *wbi2;
  1356. int bitmap_end = 0;
  1357. PRINTK("Return write for disc %d\n", i);
  1358. spin_lock_irq(&conf->device_lock);
  1359. wbi = dev->written;
  1360. dev->written = NULL;
  1361. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1362. wbi2 = r5_next_bio(wbi, dev->sector);
  1363. if (--wbi->bi_phys_segments == 0) {
  1364. md_write_end(conf->mddev);
  1365. wbi->bi_next = return_bi;
  1366. return_bi = wbi;
  1367. }
  1368. wbi = wbi2;
  1369. }
  1370. if (dev->towrite == NULL)
  1371. bitmap_end = 1;
  1372. spin_unlock_irq(&conf->device_lock);
  1373. if (bitmap_end)
  1374. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1375. STRIPE_SECTORS,
  1376. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1377. }
  1378. }
  1379. }
  1380. /* Now we might consider reading some blocks, either to check/generate
  1381. * parity, or to satisfy requests
  1382. * or to load a block that is being partially written.
  1383. */
  1384. if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
  1385. for (i=disks; i--;) {
  1386. dev = &sh->dev[i];
  1387. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1388. (dev->toread ||
  1389. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1390. syncing ||
  1391. expanding ||
  1392. (failed && (sh->dev[failed_num].toread ||
  1393. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1394. )
  1395. ) {
  1396. /* we would like to get this block, possibly
  1397. * by computing it, but we might not be able to
  1398. */
  1399. if (uptodate == disks-1) {
  1400. PRINTK("Computing block %d\n", i);
  1401. compute_block(sh, i);
  1402. uptodate++;
  1403. } else if (test_bit(R5_Insync, &dev->flags)) {
  1404. set_bit(R5_LOCKED, &dev->flags);
  1405. set_bit(R5_Wantread, &dev->flags);
  1406. locked++;
  1407. PRINTK("Reading block %d (sync=%d)\n",
  1408. i, syncing);
  1409. }
  1410. }
  1411. }
  1412. set_bit(STRIPE_HANDLE, &sh->state);
  1413. }
  1414. /* now to consider writing and what else, if anything should be read */
  1415. if (to_write) {
  1416. int rmw=0, rcw=0;
  1417. for (i=disks ; i--;) {
  1418. /* would I have to read this buffer for read_modify_write */
  1419. dev = &sh->dev[i];
  1420. if ((dev->towrite || i == sh->pd_idx) &&
  1421. (!test_bit(R5_LOCKED, &dev->flags)
  1422. ) &&
  1423. !test_bit(R5_UPTODATE, &dev->flags)) {
  1424. if (test_bit(R5_Insync, &dev->flags)
  1425. /* && !(!mddev->insync && i == sh->pd_idx) */
  1426. )
  1427. rmw++;
  1428. else rmw += 2*disks; /* cannot read it */
  1429. }
  1430. /* Would I have to read this buffer for reconstruct_write */
  1431. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1432. (!test_bit(R5_LOCKED, &dev->flags)
  1433. ) &&
  1434. !test_bit(R5_UPTODATE, &dev->flags)) {
  1435. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1436. else rcw += 2*disks;
  1437. }
  1438. }
  1439. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1440. (unsigned long long)sh->sector, rmw, rcw);
  1441. set_bit(STRIPE_HANDLE, &sh->state);
  1442. if (rmw < rcw && rmw > 0)
  1443. /* prefer read-modify-write, but need to get some data */
  1444. for (i=disks; i--;) {
  1445. dev = &sh->dev[i];
  1446. if ((dev->towrite || i == sh->pd_idx) &&
  1447. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1448. test_bit(R5_Insync, &dev->flags)) {
  1449. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1450. {
  1451. PRINTK("Read_old block %d for r-m-w\n", i);
  1452. set_bit(R5_LOCKED, &dev->flags);
  1453. set_bit(R5_Wantread, &dev->flags);
  1454. locked++;
  1455. } else {
  1456. set_bit(STRIPE_DELAYED, &sh->state);
  1457. set_bit(STRIPE_HANDLE, &sh->state);
  1458. }
  1459. }
  1460. }
  1461. if (rcw <= rmw && rcw > 0)
  1462. /* want reconstruct write, but need to get some data */
  1463. for (i=disks; i--;) {
  1464. dev = &sh->dev[i];
  1465. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1466. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1467. test_bit(R5_Insync, &dev->flags)) {
  1468. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1469. {
  1470. PRINTK("Read_old block %d for Reconstruct\n", i);
  1471. set_bit(R5_LOCKED, &dev->flags);
  1472. set_bit(R5_Wantread, &dev->flags);
  1473. locked++;
  1474. } else {
  1475. set_bit(STRIPE_DELAYED, &sh->state);
  1476. set_bit(STRIPE_HANDLE, &sh->state);
  1477. }
  1478. }
  1479. }
  1480. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1481. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1482. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1483. PRINTK("Computing parity...\n");
  1484. compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1485. /* now every locked buffer is ready to be written */
  1486. for (i=disks; i--;)
  1487. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1488. PRINTK("Writing block %d\n", i);
  1489. locked++;
  1490. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1491. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1492. || (i==sh->pd_idx && failed == 0))
  1493. set_bit(STRIPE_INSYNC, &sh->state);
  1494. }
  1495. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1496. atomic_dec(&conf->preread_active_stripes);
  1497. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1498. md_wakeup_thread(conf->mddev->thread);
  1499. }
  1500. }
  1501. }
  1502. /* maybe we need to check and possibly fix the parity for this stripe
  1503. * Any reads will already have been scheduled, so we just see if enough data
  1504. * is available
  1505. */
  1506. if (syncing && locked == 0 &&
  1507. !test_bit(STRIPE_INSYNC, &sh->state)) {
  1508. set_bit(STRIPE_HANDLE, &sh->state);
  1509. if (failed == 0) {
  1510. BUG_ON(uptodate != disks);
  1511. compute_parity5(sh, CHECK_PARITY);
  1512. uptodate--;
  1513. if (page_is_zero(sh->dev[sh->pd_idx].page)) {
  1514. /* parity is correct (on disc, not in buffer any more) */
  1515. set_bit(STRIPE_INSYNC, &sh->state);
  1516. } else {
  1517. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1518. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1519. /* don't try to repair!! */
  1520. set_bit(STRIPE_INSYNC, &sh->state);
  1521. else {
  1522. compute_block(sh, sh->pd_idx);
  1523. uptodate++;
  1524. }
  1525. }
  1526. }
  1527. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1528. /* either failed parity check, or recovery is happening */
  1529. if (failed==0)
  1530. failed_num = sh->pd_idx;
  1531. dev = &sh->dev[failed_num];
  1532. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1533. BUG_ON(uptodate != disks);
  1534. set_bit(R5_LOCKED, &dev->flags);
  1535. set_bit(R5_Wantwrite, &dev->flags);
  1536. clear_bit(STRIPE_DEGRADED, &sh->state);
  1537. locked++;
  1538. set_bit(STRIPE_INSYNC, &sh->state);
  1539. }
  1540. }
  1541. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1542. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1543. clear_bit(STRIPE_SYNCING, &sh->state);
  1544. }
  1545. /* If the failed drive is just a ReadError, then we might need to progress
  1546. * the repair/check process
  1547. */
  1548. if (failed == 1 && ! conf->mddev->ro &&
  1549. test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1550. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1551. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1552. ) {
  1553. dev = &sh->dev[failed_num];
  1554. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1555. set_bit(R5_Wantwrite, &dev->flags);
  1556. set_bit(R5_ReWrite, &dev->flags);
  1557. set_bit(R5_LOCKED, &dev->flags);
  1558. locked++;
  1559. } else {
  1560. /* let's read it back */
  1561. set_bit(R5_Wantread, &dev->flags);
  1562. set_bit(R5_LOCKED, &dev->flags);
  1563. locked++;
  1564. }
  1565. }
  1566. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1567. /* Need to write out all blocks after computing parity */
  1568. sh->disks = conf->raid_disks;
  1569. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1570. compute_parity5(sh, RECONSTRUCT_WRITE);
  1571. for (i= conf->raid_disks; i--;) {
  1572. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1573. locked++;
  1574. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1575. }
  1576. clear_bit(STRIPE_EXPANDING, &sh->state);
  1577. } else if (expanded) {
  1578. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1579. atomic_dec(&conf->reshape_stripes);
  1580. wake_up(&conf->wait_for_overlap);
  1581. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1582. }
  1583. if (expanding && locked == 0) {
  1584. /* We have read all the blocks in this stripe and now we need to
  1585. * copy some of them into a target stripe for expand.
  1586. */
  1587. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1588. for (i=0; i< sh->disks; i++)
  1589. if (i != sh->pd_idx) {
  1590. int dd_idx, pd_idx, j;
  1591. struct stripe_head *sh2;
  1592. sector_t bn = compute_blocknr(sh, i);
  1593. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1594. conf->raid_disks-1,
  1595. &dd_idx, &pd_idx, conf);
  1596. sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
  1597. if (sh2 == NULL)
  1598. /* so far only the early blocks of this stripe
  1599. * have been requested. When later blocks
  1600. * get requested, we will try again
  1601. */
  1602. continue;
  1603. if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1604. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1605. /* must have already done this block */
  1606. release_stripe(sh2);
  1607. continue;
  1608. }
  1609. memcpy(page_address(sh2->dev[dd_idx].page),
  1610. page_address(sh->dev[i].page),
  1611. STRIPE_SIZE);
  1612. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1613. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1614. for (j=0; j<conf->raid_disks; j++)
  1615. if (j != sh2->pd_idx &&
  1616. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1617. break;
  1618. if (j == conf->raid_disks) {
  1619. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1620. set_bit(STRIPE_HANDLE, &sh2->state);
  1621. }
  1622. release_stripe(sh2);
  1623. }
  1624. }
  1625. spin_unlock(&sh->lock);
  1626. while ((bi=return_bi)) {
  1627. int bytes = bi->bi_size;
  1628. return_bi = bi->bi_next;
  1629. bi->bi_next = NULL;
  1630. bi->bi_size = 0;
  1631. bi->bi_end_io(bi, bytes,
  1632. test_bit(BIO_UPTODATE, &bi->bi_flags)
  1633. ? 0 : -EIO);
  1634. }
  1635. for (i=disks; i-- ;) {
  1636. int rw;
  1637. struct bio *bi;
  1638. mdk_rdev_t *rdev;
  1639. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1640. rw = WRITE;
  1641. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1642. rw = READ;
  1643. else
  1644. continue;
  1645. bi = &sh->dev[i].req;
  1646. bi->bi_rw = rw;
  1647. if (rw == WRITE)
  1648. bi->bi_end_io = raid5_end_write_request;
  1649. else
  1650. bi->bi_end_io = raid5_end_read_request;
  1651. rcu_read_lock();
  1652. rdev = rcu_dereference(conf->disks[i].rdev);
  1653. if (rdev && test_bit(Faulty, &rdev->flags))
  1654. rdev = NULL;
  1655. if (rdev)
  1656. atomic_inc(&rdev->nr_pending);
  1657. rcu_read_unlock();
  1658. if (rdev) {
  1659. if (syncing || expanding || expanded)
  1660. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1661. bi->bi_bdev = rdev->bdev;
  1662. PRINTK("for %llu schedule op %ld on disc %d\n",
  1663. (unsigned long long)sh->sector, bi->bi_rw, i);
  1664. atomic_inc(&sh->count);
  1665. bi->bi_sector = sh->sector + rdev->data_offset;
  1666. bi->bi_flags = 1 << BIO_UPTODATE;
  1667. bi->bi_vcnt = 1;
  1668. bi->bi_max_vecs = 1;
  1669. bi->bi_idx = 0;
  1670. bi->bi_io_vec = &sh->dev[i].vec;
  1671. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1672. bi->bi_io_vec[0].bv_offset = 0;
  1673. bi->bi_size = STRIPE_SIZE;
  1674. bi->bi_next = NULL;
  1675. if (rw == WRITE &&
  1676. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1677. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1678. generic_make_request(bi);
  1679. } else {
  1680. if (rw == WRITE)
  1681. set_bit(STRIPE_DEGRADED, &sh->state);
  1682. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1683. bi->bi_rw, i, (unsigned long long)sh->sector);
  1684. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1685. set_bit(STRIPE_HANDLE, &sh->state);
  1686. }
  1687. }
  1688. }
  1689. static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  1690. {
  1691. raid6_conf_t *conf = sh->raid_conf;
  1692. int disks = sh->disks;
  1693. struct bio *return_bi= NULL;
  1694. struct bio *bi;
  1695. int i;
  1696. int syncing, expanding, expanded;
  1697. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1698. int non_overwrite = 0;
  1699. int failed_num[2] = {0, 0};
  1700. struct r5dev *dev, *pdev, *qdev;
  1701. int pd_idx = sh->pd_idx;
  1702. int qd_idx = raid6_next_disk(pd_idx, disks);
  1703. int p_failed, q_failed;
  1704. PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
  1705. (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
  1706. pd_idx, qd_idx);
  1707. spin_lock(&sh->lock);
  1708. clear_bit(STRIPE_HANDLE, &sh->state);
  1709. clear_bit(STRIPE_DELAYED, &sh->state);
  1710. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1711. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1712. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  1713. /* Now to look around and see what can be done */
  1714. rcu_read_lock();
  1715. for (i=disks; i--; ) {
  1716. mdk_rdev_t *rdev;
  1717. dev = &sh->dev[i];
  1718. clear_bit(R5_Insync, &dev->flags);
  1719. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1720. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1721. /* maybe we can reply to a read */
  1722. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1723. struct bio *rbi, *rbi2;
  1724. PRINTK("Return read for disc %d\n", i);
  1725. spin_lock_irq(&conf->device_lock);
  1726. rbi = dev->toread;
  1727. dev->toread = NULL;
  1728. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1729. wake_up(&conf->wait_for_overlap);
  1730. spin_unlock_irq(&conf->device_lock);
  1731. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1732. copy_data(0, rbi, dev->page, dev->sector);
  1733. rbi2 = r5_next_bio(rbi, dev->sector);
  1734. spin_lock_irq(&conf->device_lock);
  1735. if (--rbi->bi_phys_segments == 0) {
  1736. rbi->bi_next = return_bi;
  1737. return_bi = rbi;
  1738. }
  1739. spin_unlock_irq(&conf->device_lock);
  1740. rbi = rbi2;
  1741. }
  1742. }
  1743. /* now count some things */
  1744. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1745. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1746. if (dev->toread) to_read++;
  1747. if (dev->towrite) {
  1748. to_write++;
  1749. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1750. non_overwrite++;
  1751. }
  1752. if (dev->written) written++;
  1753. rdev = rcu_dereference(conf->disks[i].rdev);
  1754. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1755. /* The ReadError flag will just be confusing now */
  1756. clear_bit(R5_ReadError, &dev->flags);
  1757. clear_bit(R5_ReWrite, &dev->flags);
  1758. }
  1759. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1760. || test_bit(R5_ReadError, &dev->flags)) {
  1761. if ( failed < 2 )
  1762. failed_num[failed] = i;
  1763. failed++;
  1764. } else
  1765. set_bit(R5_Insync, &dev->flags);
  1766. }
  1767. rcu_read_unlock();
  1768. PRINTK("locked=%d uptodate=%d to_read=%d"
  1769. " to_write=%d failed=%d failed_num=%d,%d\n",
  1770. locked, uptodate, to_read, to_write, failed,
  1771. failed_num[0], failed_num[1]);
  1772. /* check if the array has lost >2 devices and, if so, some requests might
  1773. * need to be failed
  1774. */
  1775. if (failed > 2 && to_read+to_write+written) {
  1776. for (i=disks; i--; ) {
  1777. int bitmap_end = 0;
  1778. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1779. mdk_rdev_t *rdev;
  1780. rcu_read_lock();
  1781. rdev = rcu_dereference(conf->disks[i].rdev);
  1782. if (rdev && test_bit(In_sync, &rdev->flags))
  1783. /* multiple read failures in one stripe */
  1784. md_error(conf->mddev, rdev);
  1785. rcu_read_unlock();
  1786. }
  1787. spin_lock_irq(&conf->device_lock);
  1788. /* fail all writes first */
  1789. bi = sh->dev[i].towrite;
  1790. sh->dev[i].towrite = NULL;
  1791. if (bi) { to_write--; bitmap_end = 1; }
  1792. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1793. wake_up(&conf->wait_for_overlap);
  1794. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1795. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1796. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1797. if (--bi->bi_phys_segments == 0) {
  1798. md_write_end(conf->mddev);
  1799. bi->bi_next = return_bi;
  1800. return_bi = bi;
  1801. }
  1802. bi = nextbi;
  1803. }
  1804. /* and fail all 'written' */
  1805. bi = sh->dev[i].written;
  1806. sh->dev[i].written = NULL;
  1807. if (bi) bitmap_end = 1;
  1808. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1809. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1810. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1811. if (--bi->bi_phys_segments == 0) {
  1812. md_write_end(conf->mddev);
  1813. bi->bi_next = return_bi;
  1814. return_bi = bi;
  1815. }
  1816. bi = bi2;
  1817. }
  1818. /* fail any reads if this device is non-operational */
  1819. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1820. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1821. bi = sh->dev[i].toread;
  1822. sh->dev[i].toread = NULL;
  1823. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1824. wake_up(&conf->wait_for_overlap);
  1825. if (bi) to_read--;
  1826. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1827. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1828. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1829. if (--bi->bi_phys_segments == 0) {
  1830. bi->bi_next = return_bi;
  1831. return_bi = bi;
  1832. }
  1833. bi = nextbi;
  1834. }
  1835. }
  1836. spin_unlock_irq(&conf->device_lock);
  1837. if (bitmap_end)
  1838. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1839. STRIPE_SECTORS, 0, 0);
  1840. }
  1841. }
  1842. if (failed > 2 && syncing) {
  1843. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1844. clear_bit(STRIPE_SYNCING, &sh->state);
  1845. syncing = 0;
  1846. }
  1847. /*
  1848. * might be able to return some write requests if the parity blocks
  1849. * are safe, or on a failed drive
  1850. */
  1851. pdev = &sh->dev[pd_idx];
  1852. p_failed = (failed >= 1 && failed_num[0] == pd_idx)
  1853. || (failed >= 2 && failed_num[1] == pd_idx);
  1854. qdev = &sh->dev[qd_idx];
  1855. q_failed = (failed >= 1 && failed_num[0] == qd_idx)
  1856. || (failed >= 2 && failed_num[1] == qd_idx);
  1857. if ( written &&
  1858. ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
  1859. && !test_bit(R5_LOCKED, &pdev->flags)
  1860. && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
  1861. ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
  1862. && !test_bit(R5_LOCKED, &qdev->flags)
  1863. && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
  1864. /* any written block on an uptodate or failed drive can be
  1865. * returned. Note that if we 'wrote' to a failed drive,
  1866. * it will be UPTODATE, but never LOCKED, so we don't need
  1867. * to test 'failed' directly.
  1868. */
  1869. for (i=disks; i--; )
  1870. if (sh->dev[i].written) {
  1871. dev = &sh->dev[i];
  1872. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1873. test_bit(R5_UPTODATE, &dev->flags) ) {
  1874. /* We can return any write requests */
  1875. int bitmap_end = 0;
  1876. struct bio *wbi, *wbi2;
  1877. PRINTK("Return write for stripe %llu disc %d\n",
  1878. (unsigned long long)sh->sector, i);
  1879. spin_lock_irq(&conf->device_lock);
  1880. wbi = dev->written;
  1881. dev->written = NULL;
  1882. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1883. wbi2 = r5_next_bio(wbi, dev->sector);
  1884. if (--wbi->bi_phys_segments == 0) {
  1885. md_write_end(conf->mddev);
  1886. wbi->bi_next = return_bi;
  1887. return_bi = wbi;
  1888. }
  1889. wbi = wbi2;
  1890. }
  1891. if (dev->towrite == NULL)
  1892. bitmap_end = 1;
  1893. spin_unlock_irq(&conf->device_lock);
  1894. if (bitmap_end)
  1895. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1896. STRIPE_SECTORS,
  1897. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1898. }
  1899. }
  1900. }
  1901. /* Now we might consider reading some blocks, either to check/generate
  1902. * parity, or to satisfy requests
  1903. * or to load a block that is being partially written.
  1904. */
  1905. if (to_read || non_overwrite || (to_write && failed) ||
  1906. (syncing && (uptodate < disks)) || expanding) {
  1907. for (i=disks; i--;) {
  1908. dev = &sh->dev[i];
  1909. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1910. (dev->toread ||
  1911. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1912. syncing ||
  1913. expanding ||
  1914. (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
  1915. (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
  1916. )
  1917. ) {
  1918. /* we would like to get this block, possibly
  1919. * by computing it, but we might not be able to
  1920. */
  1921. if (uptodate == disks-1) {
  1922. PRINTK("Computing stripe %llu block %d\n",
  1923. (unsigned long long)sh->sector, i);
  1924. compute_block_1(sh, i, 0);
  1925. uptodate++;
  1926. } else if ( uptodate == disks-2 && failed >= 2 ) {
  1927. /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
  1928. int other;
  1929. for (other=disks; other--;) {
  1930. if ( other == i )
  1931. continue;
  1932. if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
  1933. break;
  1934. }
  1935. BUG_ON(other < 0);
  1936. PRINTK("Computing stripe %llu blocks %d,%d\n",
  1937. (unsigned long long)sh->sector, i, other);
  1938. compute_block_2(sh, i, other);
  1939. uptodate += 2;
  1940. } else if (test_bit(R5_Insync, &dev->flags)) {
  1941. set_bit(R5_LOCKED, &dev->flags);
  1942. set_bit(R5_Wantread, &dev->flags);
  1943. locked++;
  1944. PRINTK("Reading block %d (sync=%d)\n",
  1945. i, syncing);
  1946. }
  1947. }
  1948. }
  1949. set_bit(STRIPE_HANDLE, &sh->state);
  1950. }
  1951. /* now to consider writing and what else, if anything should be read */
  1952. if (to_write) {
  1953. int rcw=0, must_compute=0;
  1954. for (i=disks ; i--;) {
  1955. dev = &sh->dev[i];
  1956. /* Would I have to read this buffer for reconstruct_write */
  1957. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1958. && i != pd_idx && i != qd_idx
  1959. && (!test_bit(R5_LOCKED, &dev->flags)
  1960. ) &&
  1961. !test_bit(R5_UPTODATE, &dev->flags)) {
  1962. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1963. else {
  1964. PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
  1965. must_compute++;
  1966. }
  1967. }
  1968. }
  1969. PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
  1970. (unsigned long long)sh->sector, rcw, must_compute);
  1971. set_bit(STRIPE_HANDLE, &sh->state);
  1972. if (rcw > 0)
  1973. /* want reconstruct write, but need to get some data */
  1974. for (i=disks; i--;) {
  1975. dev = &sh->dev[i];
  1976. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1977. && !(failed == 0 && (i == pd_idx || i == qd_idx))
  1978. && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1979. test_bit(R5_Insync, &dev->flags)) {
  1980. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1981. {
  1982. PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
  1983. (unsigned long long)sh->sector, i);
  1984. set_bit(R5_LOCKED, &dev->flags);
  1985. set_bit(R5_Wantread, &dev->flags);
  1986. locked++;
  1987. } else {
  1988. PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
  1989. (unsigned long long)sh->sector, i);
  1990. set_bit(STRIPE_DELAYED, &sh->state);
  1991. set_bit(STRIPE_HANDLE, &sh->state);
  1992. }
  1993. }
  1994. }
  1995. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1996. if (locked == 0 && rcw == 0 &&
  1997. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1998. if ( must_compute > 0 ) {
  1999. /* We have failed blocks and need to compute them */
  2000. switch ( failed ) {
  2001. case 0: BUG();
  2002. case 1: compute_block_1(sh, failed_num[0], 0); break;
  2003. case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
  2004. default: BUG(); /* This request should have been failed? */
  2005. }
  2006. }
  2007. PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
  2008. compute_parity6(sh, RECONSTRUCT_WRITE);
  2009. /* now every locked buffer is ready to be written */
  2010. for (i=disks; i--;)
  2011. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2012. PRINTK("Writing stripe %llu block %d\n",
  2013. (unsigned long long)sh->sector, i);
  2014. locked++;
  2015. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2016. }
  2017. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2018. set_bit(STRIPE_INSYNC, &sh->state);
  2019. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2020. atomic_dec(&conf->preread_active_stripes);
  2021. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  2022. md_wakeup_thread(conf->mddev->thread);
  2023. }
  2024. }
  2025. }
  2026. /* maybe we need to check and possibly fix the parity for this stripe
  2027. * Any reads will already have been scheduled, so we just see if enough data
  2028. * is available
  2029. */
  2030. if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
  2031. int update_p = 0, update_q = 0;
  2032. struct r5dev *dev;
  2033. set_bit(STRIPE_HANDLE, &sh->state);
  2034. BUG_ON(failed>2);
  2035. BUG_ON(uptodate < disks);
  2036. /* Want to check and possibly repair P and Q.
  2037. * However there could be one 'failed' device, in which
  2038. * case we can only check one of them, possibly using the
  2039. * other to generate missing data
  2040. */
  2041. /* If !tmp_page, we cannot do the calculations,
  2042. * but as we have set STRIPE_HANDLE, we will soon be called
  2043. * by stripe_handle with a tmp_page - just wait until then.
  2044. */
  2045. if (tmp_page) {
  2046. if (failed == q_failed) {
  2047. /* The only possible failed device holds 'Q', so it makes
  2048. * sense to check P (If anything else were failed, we would
  2049. * have used P to recreate it).
  2050. */
  2051. compute_block_1(sh, pd_idx, 1);
  2052. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2053. compute_block_1(sh,pd_idx,0);
  2054. update_p = 1;
  2055. }
  2056. }
  2057. if (!q_failed && failed < 2) {
  2058. /* q is not failed, and we didn't use it to generate
  2059. * anything, so it makes sense to check it
  2060. */
  2061. memcpy(page_address(tmp_page),
  2062. page_address(sh->dev[qd_idx].page),
  2063. STRIPE_SIZE);
  2064. compute_parity6(sh, UPDATE_PARITY);
  2065. if (memcmp(page_address(tmp_page),
  2066. page_address(sh->dev[qd_idx].page),
  2067. STRIPE_SIZE)!= 0) {
  2068. clear_bit(STRIPE_INSYNC, &sh->state);
  2069. update_q = 1;
  2070. }
  2071. }
  2072. if (update_p || update_q) {
  2073. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2074. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2075. /* don't try to repair!! */
  2076. update_p = update_q = 0;
  2077. }
  2078. /* now write out any block on a failed drive,
  2079. * or P or Q if they need it
  2080. */
  2081. if (failed == 2) {
  2082. dev = &sh->dev[failed_num[1]];
  2083. locked++;
  2084. set_bit(R5_LOCKED, &dev->flags);
  2085. set_bit(R5_Wantwrite, &dev->flags);
  2086. }
  2087. if (failed >= 1) {
  2088. dev = &sh->dev[failed_num[0]];
  2089. locked++;
  2090. set_bit(R5_LOCKED, &dev->flags);
  2091. set_bit(R5_Wantwrite, &dev->flags);
  2092. }
  2093. if (update_p) {
  2094. dev = &sh->dev[pd_idx];
  2095. locked ++;
  2096. set_bit(R5_LOCKED, &dev->flags);
  2097. set_bit(R5_Wantwrite, &dev->flags);
  2098. }
  2099. if (update_q) {
  2100. dev = &sh->dev[qd_idx];
  2101. locked++;
  2102. set_bit(R5_LOCKED, &dev->flags);
  2103. set_bit(R5_Wantwrite, &dev->flags);
  2104. }
  2105. clear_bit(STRIPE_DEGRADED, &sh->state);
  2106. set_bit(STRIPE_INSYNC, &sh->state);
  2107. }
  2108. }
  2109. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2110. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2111. clear_bit(STRIPE_SYNCING, &sh->state);
  2112. }
  2113. /* If the failed drives are just a ReadError, then we might need
  2114. * to progress the repair/check process
  2115. */
  2116. if (failed <= 2 && ! conf->mddev->ro)
  2117. for (i=0; i<failed;i++) {
  2118. dev = &sh->dev[failed_num[i]];
  2119. if (test_bit(R5_ReadError, &dev->flags)
  2120. && !test_bit(R5_LOCKED, &dev->flags)
  2121. && test_bit(R5_UPTODATE, &dev->flags)
  2122. ) {
  2123. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2124. set_bit(R5_Wantwrite, &dev->flags);
  2125. set_bit(R5_ReWrite, &dev->flags);
  2126. set_bit(R5_LOCKED, &dev->flags);
  2127. } else {
  2128. /* let's read it back */
  2129. set_bit(R5_Wantread, &dev->flags);
  2130. set_bit(R5_LOCKED, &dev->flags);
  2131. }
  2132. }
  2133. }
  2134. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2135. /* Need to write out all blocks after computing P&Q */
  2136. sh->disks = conf->raid_disks;
  2137. sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
  2138. conf->raid_disks);
  2139. compute_parity6(sh, RECONSTRUCT_WRITE);
  2140. for (i = conf->raid_disks ; i-- ; ) {
  2141. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2142. locked++;
  2143. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2144. }
  2145. clear_bit(STRIPE_EXPANDING, &sh->state);
  2146. } else if (expanded) {
  2147. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2148. atomic_dec(&conf->reshape_stripes);
  2149. wake_up(&conf->wait_for_overlap);
  2150. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2151. }
  2152. if (expanding && locked == 0) {
  2153. /* We have read all the blocks in this stripe and now we need to
  2154. * copy some of them into a target stripe for expand.
  2155. */
  2156. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2157. for (i = 0; i < sh->disks ; i++)
  2158. if (i != pd_idx && i != qd_idx) {
  2159. int dd_idx2, pd_idx2, j;
  2160. struct stripe_head *sh2;
  2161. sector_t bn = compute_blocknr(sh, i);
  2162. sector_t s = raid5_compute_sector(
  2163. bn, conf->raid_disks,
  2164. conf->raid_disks - conf->max_degraded,
  2165. &dd_idx2, &pd_idx2, conf);
  2166. sh2 = get_active_stripe(conf, s,
  2167. conf->raid_disks,
  2168. pd_idx2, 1);
  2169. if (sh2 == NULL)
  2170. /* so for only the early blocks of
  2171. * this stripe have been requests.
  2172. * When later blocks get requests, we
  2173. * will try again
  2174. */
  2175. continue;
  2176. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2177. test_bit(R5_Expanded,
  2178. &sh2->dev[dd_idx2].flags)) {
  2179. /* must have already done this block */
  2180. release_stripe(sh2);
  2181. continue;
  2182. }
  2183. memcpy(page_address(sh2->dev[dd_idx2].page),
  2184. page_address(sh->dev[i].page),
  2185. STRIPE_SIZE);
  2186. set_bit(R5_Expanded, &sh2->dev[dd_idx2].flags);
  2187. set_bit(R5_UPTODATE, &sh2->dev[dd_idx2].flags);
  2188. for (j = 0 ; j < conf->raid_disks ; j++)
  2189. if (j != sh2->pd_idx &&
  2190. j != raid6_next_disk(sh2->pd_idx,
  2191. sh2->disks) &&
  2192. !test_bit(R5_Expanded,
  2193. &sh2->dev[j].flags))
  2194. break;
  2195. if (j == conf->raid_disks) {
  2196. set_bit(STRIPE_EXPAND_READY,
  2197. &sh2->state);
  2198. set_bit(STRIPE_HANDLE, &sh2->state);
  2199. }
  2200. release_stripe(sh2);
  2201. }
  2202. }
  2203. spin_unlock(&sh->lock);
  2204. while ((bi=return_bi)) {
  2205. int bytes = bi->bi_size;
  2206. return_bi = bi->bi_next;
  2207. bi->bi_next = NULL;
  2208. bi->bi_size = 0;
  2209. bi->bi_end_io(bi, bytes,
  2210. test_bit(BIO_UPTODATE, &bi->bi_flags)
  2211. ? 0 : -EIO);
  2212. }
  2213. for (i=disks; i-- ;) {
  2214. int rw;
  2215. struct bio *bi;
  2216. mdk_rdev_t *rdev;
  2217. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  2218. rw = WRITE;
  2219. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  2220. rw = READ;
  2221. else
  2222. continue;
  2223. bi = &sh->dev[i].req;
  2224. bi->bi_rw = rw;
  2225. if (rw == WRITE)
  2226. bi->bi_end_io = raid5_end_write_request;
  2227. else
  2228. bi->bi_end_io = raid5_end_read_request;
  2229. rcu_read_lock();
  2230. rdev = rcu_dereference(conf->disks[i].rdev);
  2231. if (rdev && test_bit(Faulty, &rdev->flags))
  2232. rdev = NULL;
  2233. if (rdev)
  2234. atomic_inc(&rdev->nr_pending);
  2235. rcu_read_unlock();
  2236. if (rdev) {
  2237. if (syncing || expanding || expanded)
  2238. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  2239. bi->bi_bdev = rdev->bdev;
  2240. PRINTK("for %llu schedule op %ld on disc %d\n",
  2241. (unsigned long long)sh->sector, bi->bi_rw, i);
  2242. atomic_inc(&sh->count);
  2243. bi->bi_sector = sh->sector + rdev->data_offset;
  2244. bi->bi_flags = 1 << BIO_UPTODATE;
  2245. bi->bi_vcnt = 1;
  2246. bi->bi_max_vecs = 1;
  2247. bi->bi_idx = 0;
  2248. bi->bi_io_vec = &sh->dev[i].vec;
  2249. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  2250. bi->bi_io_vec[0].bv_offset = 0;
  2251. bi->bi_size = STRIPE_SIZE;
  2252. bi->bi_next = NULL;
  2253. if (rw == WRITE &&
  2254. test_bit(R5_ReWrite, &sh->dev[i].flags))
  2255. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2256. generic_make_request(bi);
  2257. } else {
  2258. if (rw == WRITE)
  2259. set_bit(STRIPE_DEGRADED, &sh->state);
  2260. PRINTK("skip op %ld on disc %d for sector %llu\n",
  2261. bi->bi_rw, i, (unsigned long long)sh->sector);
  2262. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2263. set_bit(STRIPE_HANDLE, &sh->state);
  2264. }
  2265. }
  2266. }
  2267. static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2268. {
  2269. if (sh->raid_conf->level == 6)
  2270. handle_stripe6(sh, tmp_page);
  2271. else
  2272. handle_stripe5(sh);
  2273. }
  2274. static void raid5_activate_delayed(raid5_conf_t *conf)
  2275. {
  2276. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2277. while (!list_empty(&conf->delayed_list)) {
  2278. struct list_head *l = conf->delayed_list.next;
  2279. struct stripe_head *sh;
  2280. sh = list_entry(l, struct stripe_head, lru);
  2281. list_del_init(l);
  2282. clear_bit(STRIPE_DELAYED, &sh->state);
  2283. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2284. atomic_inc(&conf->preread_active_stripes);
  2285. list_add_tail(&sh->lru, &conf->handle_list);
  2286. }
  2287. }
  2288. }
  2289. static void activate_bit_delay(raid5_conf_t *conf)
  2290. {
  2291. /* device_lock is held */
  2292. struct list_head head;
  2293. list_add(&head, &conf->bitmap_list);
  2294. list_del_init(&conf->bitmap_list);
  2295. while (!list_empty(&head)) {
  2296. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2297. list_del_init(&sh->lru);
  2298. atomic_inc(&sh->count);
  2299. __release_stripe(conf, sh);
  2300. }
  2301. }
  2302. static void unplug_slaves(mddev_t *mddev)
  2303. {
  2304. raid5_conf_t *conf = mddev_to_conf(mddev);
  2305. int i;
  2306. rcu_read_lock();
  2307. for (i=0; i<mddev->raid_disks; i++) {
  2308. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2309. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2310. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  2311. atomic_inc(&rdev->nr_pending);
  2312. rcu_read_unlock();
  2313. if (r_queue->unplug_fn)
  2314. r_queue->unplug_fn(r_queue);
  2315. rdev_dec_pending(rdev, mddev);
  2316. rcu_read_lock();
  2317. }
  2318. }
  2319. rcu_read_unlock();
  2320. }
  2321. static void raid5_unplug_device(request_queue_t *q)
  2322. {
  2323. mddev_t *mddev = q->queuedata;
  2324. raid5_conf_t *conf = mddev_to_conf(mddev);
  2325. unsigned long flags;
  2326. spin_lock_irqsave(&conf->device_lock, flags);
  2327. if (blk_remove_plug(q)) {
  2328. conf->seq_flush++;
  2329. raid5_activate_delayed(conf);
  2330. }
  2331. md_wakeup_thread(mddev->thread);
  2332. spin_unlock_irqrestore(&conf->device_lock, flags);
  2333. unplug_slaves(mddev);
  2334. }
  2335. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  2336. sector_t *error_sector)
  2337. {
  2338. mddev_t *mddev = q->queuedata;
  2339. raid5_conf_t *conf = mddev_to_conf(mddev);
  2340. int i, ret = 0;
  2341. rcu_read_lock();
  2342. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  2343. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2344. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  2345. struct block_device *bdev = rdev->bdev;
  2346. request_queue_t *r_queue = bdev_get_queue(bdev);
  2347. if (!r_queue->issue_flush_fn)
  2348. ret = -EOPNOTSUPP;
  2349. else {
  2350. atomic_inc(&rdev->nr_pending);
  2351. rcu_read_unlock();
  2352. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  2353. error_sector);
  2354. rdev_dec_pending(rdev, mddev);
  2355. rcu_read_lock();
  2356. }
  2357. }
  2358. }
  2359. rcu_read_unlock();
  2360. return ret;
  2361. }
  2362. static int raid5_congested(void *data, int bits)
  2363. {
  2364. mddev_t *mddev = data;
  2365. raid5_conf_t *conf = mddev_to_conf(mddev);
  2366. /* No difference between reads and writes. Just check
  2367. * how busy the stripe_cache is
  2368. */
  2369. if (conf->inactive_blocked)
  2370. return 1;
  2371. if (conf->quiesce)
  2372. return 1;
  2373. if (list_empty_careful(&conf->inactive_list))
  2374. return 1;
  2375. return 0;
  2376. }
  2377. /* We want read requests to align with chunks where possible,
  2378. * but write requests don't need to.
  2379. */
  2380. static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
  2381. {
  2382. mddev_t *mddev = q->queuedata;
  2383. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2384. int max;
  2385. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2386. unsigned int bio_sectors = bio->bi_size >> 9;
  2387. if (bio_data_dir(bio) == WRITE)
  2388. return biovec->bv_len; /* always allow writes to be mergeable */
  2389. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2390. if (max < 0) max = 0;
  2391. if (max <= biovec->bv_len && bio_sectors == 0)
  2392. return biovec->bv_len;
  2393. else
  2394. return max;
  2395. }
  2396. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2397. {
  2398. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2399. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2400. unsigned int bio_sectors = bio->bi_size >> 9;
  2401. return chunk_sectors >=
  2402. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2403. }
  2404. /*
  2405. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2406. * later sampled by raid5d.
  2407. */
  2408. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2409. {
  2410. unsigned long flags;
  2411. spin_lock_irqsave(&conf->device_lock, flags);
  2412. bi->bi_next = conf->retry_read_aligned_list;
  2413. conf->retry_read_aligned_list = bi;
  2414. spin_unlock_irqrestore(&conf->device_lock, flags);
  2415. md_wakeup_thread(conf->mddev->thread);
  2416. }
  2417. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  2418. {
  2419. struct bio *bi;
  2420. bi = conf->retry_read_aligned;
  2421. if (bi) {
  2422. conf->retry_read_aligned = NULL;
  2423. return bi;
  2424. }
  2425. bi = conf->retry_read_aligned_list;
  2426. if(bi) {
  2427. conf->retry_read_aligned_list = bi->bi_next;
  2428. bi->bi_next = NULL;
  2429. bi->bi_phys_segments = 1; /* biased count of active stripes */
  2430. bi->bi_hw_segments = 0; /* count of processed stripes */
  2431. }
  2432. return bi;
  2433. }
  2434. /*
  2435. * The "raid5_align_endio" should check if the read succeeded and if it
  2436. * did, call bio_endio on the original bio (having bio_put the new bio
  2437. * first).
  2438. * If the read failed..
  2439. */
  2440. static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
  2441. {
  2442. struct bio* raid_bi = bi->bi_private;
  2443. mddev_t *mddev;
  2444. raid5_conf_t *conf;
  2445. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  2446. mdk_rdev_t *rdev;
  2447. if (bi->bi_size)
  2448. return 1;
  2449. bio_put(bi);
  2450. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  2451. conf = mddev_to_conf(mddev);
  2452. rdev = (void*)raid_bi->bi_next;
  2453. raid_bi->bi_next = NULL;
  2454. rdev_dec_pending(rdev, conf->mddev);
  2455. if (!error && uptodate) {
  2456. bio_endio(raid_bi, bytes, 0);
  2457. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2458. wake_up(&conf->wait_for_stripe);
  2459. return 0;
  2460. }
  2461. PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
  2462. add_bio_to_retry(raid_bi, conf);
  2463. return 0;
  2464. }
  2465. static int bio_fits_rdev(struct bio *bi)
  2466. {
  2467. request_queue_t *q = bdev_get_queue(bi->bi_bdev);
  2468. if ((bi->bi_size>>9) > q->max_sectors)
  2469. return 0;
  2470. blk_recount_segments(q, bi);
  2471. if (bi->bi_phys_segments > q->max_phys_segments ||
  2472. bi->bi_hw_segments > q->max_hw_segments)
  2473. return 0;
  2474. if (q->merge_bvec_fn)
  2475. /* it's too hard to apply the merge_bvec_fn at this stage,
  2476. * just just give up
  2477. */
  2478. return 0;
  2479. return 1;
  2480. }
  2481. static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
  2482. {
  2483. mddev_t *mddev = q->queuedata;
  2484. raid5_conf_t *conf = mddev_to_conf(mddev);
  2485. const unsigned int raid_disks = conf->raid_disks;
  2486. const unsigned int data_disks = raid_disks - conf->max_degraded;
  2487. unsigned int dd_idx, pd_idx;
  2488. struct bio* align_bi;
  2489. mdk_rdev_t *rdev;
  2490. if (!in_chunk_boundary(mddev, raid_bio)) {
  2491. PRINTK("chunk_aligned_read : non aligned\n");
  2492. return 0;
  2493. }
  2494. /*
  2495. * use bio_clone to make a copy of the bio
  2496. */
  2497. align_bi = bio_clone(raid_bio, GFP_NOIO);
  2498. if (!align_bi)
  2499. return 0;
  2500. /*
  2501. * set bi_end_io to a new function, and set bi_private to the
  2502. * original bio.
  2503. */
  2504. align_bi->bi_end_io = raid5_align_endio;
  2505. align_bi->bi_private = raid_bio;
  2506. /*
  2507. * compute position
  2508. */
  2509. align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
  2510. raid_disks,
  2511. data_disks,
  2512. &dd_idx,
  2513. &pd_idx,
  2514. conf);
  2515. rcu_read_lock();
  2516. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  2517. if (rdev && test_bit(In_sync, &rdev->flags)) {
  2518. atomic_inc(&rdev->nr_pending);
  2519. rcu_read_unlock();
  2520. raid_bio->bi_next = (void*)rdev;
  2521. align_bi->bi_bdev = rdev->bdev;
  2522. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  2523. align_bi->bi_sector += rdev->data_offset;
  2524. if (!bio_fits_rdev(align_bi)) {
  2525. /* too big in some way */
  2526. bio_put(align_bi);
  2527. rdev_dec_pending(rdev, mddev);
  2528. return 0;
  2529. }
  2530. spin_lock_irq(&conf->device_lock);
  2531. wait_event_lock_irq(conf->wait_for_stripe,
  2532. conf->quiesce == 0,
  2533. conf->device_lock, /* nothing */);
  2534. atomic_inc(&conf->active_aligned_reads);
  2535. spin_unlock_irq(&conf->device_lock);
  2536. generic_make_request(align_bi);
  2537. return 1;
  2538. } else {
  2539. rcu_read_unlock();
  2540. bio_put(align_bi);
  2541. return 0;
  2542. }
  2543. }
  2544. static int make_request(request_queue_t *q, struct bio * bi)
  2545. {
  2546. mddev_t *mddev = q->queuedata;
  2547. raid5_conf_t *conf = mddev_to_conf(mddev);
  2548. unsigned int dd_idx, pd_idx;
  2549. sector_t new_sector;
  2550. sector_t logical_sector, last_sector;
  2551. struct stripe_head *sh;
  2552. const int rw = bio_data_dir(bi);
  2553. int remaining;
  2554. if (unlikely(bio_barrier(bi))) {
  2555. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  2556. return 0;
  2557. }
  2558. md_write_start(mddev, bi);
  2559. disk_stat_inc(mddev->gendisk, ios[rw]);
  2560. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  2561. if (rw == READ &&
  2562. mddev->reshape_position == MaxSector &&
  2563. chunk_aligned_read(q,bi))
  2564. return 0;
  2565. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2566. last_sector = bi->bi_sector + (bi->bi_size>>9);
  2567. bi->bi_next = NULL;
  2568. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  2569. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  2570. DEFINE_WAIT(w);
  2571. int disks, data_disks;
  2572. retry:
  2573. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  2574. if (likely(conf->expand_progress == MaxSector))
  2575. disks = conf->raid_disks;
  2576. else {
  2577. /* spinlock is needed as expand_progress may be
  2578. * 64bit on a 32bit platform, and so it might be
  2579. * possible to see a half-updated value
  2580. * Ofcourse expand_progress could change after
  2581. * the lock is dropped, so once we get a reference
  2582. * to the stripe that we think it is, we will have
  2583. * to check again.
  2584. */
  2585. spin_lock_irq(&conf->device_lock);
  2586. disks = conf->raid_disks;
  2587. if (logical_sector >= conf->expand_progress)
  2588. disks = conf->previous_raid_disks;
  2589. else {
  2590. if (logical_sector >= conf->expand_lo) {
  2591. spin_unlock_irq(&conf->device_lock);
  2592. schedule();
  2593. goto retry;
  2594. }
  2595. }
  2596. spin_unlock_irq(&conf->device_lock);
  2597. }
  2598. data_disks = disks - conf->max_degraded;
  2599. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  2600. &dd_idx, &pd_idx, conf);
  2601. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  2602. (unsigned long long)new_sector,
  2603. (unsigned long long)logical_sector);
  2604. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  2605. if (sh) {
  2606. if (unlikely(conf->expand_progress != MaxSector)) {
  2607. /* expansion might have moved on while waiting for a
  2608. * stripe, so we must do the range check again.
  2609. * Expansion could still move past after this
  2610. * test, but as we are holding a reference to
  2611. * 'sh', we know that if that happens,
  2612. * STRIPE_EXPANDING will get set and the expansion
  2613. * won't proceed until we finish with the stripe.
  2614. */
  2615. int must_retry = 0;
  2616. spin_lock_irq(&conf->device_lock);
  2617. if (logical_sector < conf->expand_progress &&
  2618. disks == conf->previous_raid_disks)
  2619. /* mismatch, need to try again */
  2620. must_retry = 1;
  2621. spin_unlock_irq(&conf->device_lock);
  2622. if (must_retry) {
  2623. release_stripe(sh);
  2624. goto retry;
  2625. }
  2626. }
  2627. /* FIXME what if we get a false positive because these
  2628. * are being updated.
  2629. */
  2630. if (logical_sector >= mddev->suspend_lo &&
  2631. logical_sector < mddev->suspend_hi) {
  2632. release_stripe(sh);
  2633. schedule();
  2634. goto retry;
  2635. }
  2636. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  2637. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  2638. /* Stripe is busy expanding or
  2639. * add failed due to overlap. Flush everything
  2640. * and wait a while
  2641. */
  2642. raid5_unplug_device(mddev->queue);
  2643. release_stripe(sh);
  2644. schedule();
  2645. goto retry;
  2646. }
  2647. finish_wait(&conf->wait_for_overlap, &w);
  2648. handle_stripe(sh, NULL);
  2649. release_stripe(sh);
  2650. } else {
  2651. /* cannot get stripe for read-ahead, just give-up */
  2652. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2653. finish_wait(&conf->wait_for_overlap, &w);
  2654. break;
  2655. }
  2656. }
  2657. spin_lock_irq(&conf->device_lock);
  2658. remaining = --bi->bi_phys_segments;
  2659. spin_unlock_irq(&conf->device_lock);
  2660. if (remaining == 0) {
  2661. int bytes = bi->bi_size;
  2662. if ( rw == WRITE )
  2663. md_write_end(mddev);
  2664. bi->bi_size = 0;
  2665. bi->bi_end_io(bi, bytes,
  2666. test_bit(BIO_UPTODATE, &bi->bi_flags)
  2667. ? 0 : -EIO);
  2668. }
  2669. return 0;
  2670. }
  2671. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  2672. {
  2673. /* reshaping is quite different to recovery/resync so it is
  2674. * handled quite separately ... here.
  2675. *
  2676. * On each call to sync_request, we gather one chunk worth of
  2677. * destination stripes and flag them as expanding.
  2678. * Then we find all the source stripes and request reads.
  2679. * As the reads complete, handle_stripe will copy the data
  2680. * into the destination stripe and release that stripe.
  2681. */
  2682. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2683. struct stripe_head *sh;
  2684. int pd_idx;
  2685. sector_t first_sector, last_sector;
  2686. int raid_disks = conf->previous_raid_disks;
  2687. int data_disks = raid_disks - conf->max_degraded;
  2688. int new_data_disks = conf->raid_disks - conf->max_degraded;
  2689. int i;
  2690. int dd_idx;
  2691. sector_t writepos, safepos, gap;
  2692. if (sector_nr == 0 &&
  2693. conf->expand_progress != 0) {
  2694. /* restarting in the middle, skip the initial sectors */
  2695. sector_nr = conf->expand_progress;
  2696. sector_div(sector_nr, new_data_disks);
  2697. *skipped = 1;
  2698. return sector_nr;
  2699. }
  2700. /* we update the metadata when there is more than 3Meg
  2701. * in the block range (that is rather arbitrary, should
  2702. * probably be time based) or when the data about to be
  2703. * copied would over-write the source of the data at
  2704. * the front of the range.
  2705. * i.e. one new_stripe forward from expand_progress new_maps
  2706. * to after where expand_lo old_maps to
  2707. */
  2708. writepos = conf->expand_progress +
  2709. conf->chunk_size/512*(new_data_disks);
  2710. sector_div(writepos, new_data_disks);
  2711. safepos = conf->expand_lo;
  2712. sector_div(safepos, data_disks);
  2713. gap = conf->expand_progress - conf->expand_lo;
  2714. if (writepos >= safepos ||
  2715. gap > (new_data_disks)*3000*2 /*3Meg*/) {
  2716. /* Cannot proceed until we've updated the superblock... */
  2717. wait_event(conf->wait_for_overlap,
  2718. atomic_read(&conf->reshape_stripes)==0);
  2719. mddev->reshape_position = conf->expand_progress;
  2720. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2721. md_wakeup_thread(mddev->thread);
  2722. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  2723. kthread_should_stop());
  2724. spin_lock_irq(&conf->device_lock);
  2725. conf->expand_lo = mddev->reshape_position;
  2726. spin_unlock_irq(&conf->device_lock);
  2727. wake_up(&conf->wait_for_overlap);
  2728. }
  2729. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  2730. int j;
  2731. int skipped = 0;
  2732. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  2733. sh = get_active_stripe(conf, sector_nr+i,
  2734. conf->raid_disks, pd_idx, 0);
  2735. set_bit(STRIPE_EXPANDING, &sh->state);
  2736. atomic_inc(&conf->reshape_stripes);
  2737. /* If any of this stripe is beyond the end of the old
  2738. * array, then we need to zero those blocks
  2739. */
  2740. for (j=sh->disks; j--;) {
  2741. sector_t s;
  2742. if (j == sh->pd_idx)
  2743. continue;
  2744. if (conf->level == 6 &&
  2745. j == raid6_next_disk(sh->pd_idx, sh->disks))
  2746. continue;
  2747. s = compute_blocknr(sh, j);
  2748. if (s < (mddev->array_size<<1)) {
  2749. skipped = 1;
  2750. continue;
  2751. }
  2752. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  2753. set_bit(R5_Expanded, &sh->dev[j].flags);
  2754. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  2755. }
  2756. if (!skipped) {
  2757. set_bit(STRIPE_EXPAND_READY, &sh->state);
  2758. set_bit(STRIPE_HANDLE, &sh->state);
  2759. }
  2760. release_stripe(sh);
  2761. }
  2762. spin_lock_irq(&conf->device_lock);
  2763. conf->expand_progress = (sector_nr + i) * new_data_disks;
  2764. spin_unlock_irq(&conf->device_lock);
  2765. /* Ok, those stripe are ready. We can start scheduling
  2766. * reads on the source stripes.
  2767. * The source stripes are determined by mapping the first and last
  2768. * block on the destination stripes.
  2769. */
  2770. first_sector =
  2771. raid5_compute_sector(sector_nr*(new_data_disks),
  2772. raid_disks, data_disks,
  2773. &dd_idx, &pd_idx, conf);
  2774. last_sector =
  2775. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  2776. *(new_data_disks) -1,
  2777. raid_disks, data_disks,
  2778. &dd_idx, &pd_idx, conf);
  2779. if (last_sector >= (mddev->size<<1))
  2780. last_sector = (mddev->size<<1)-1;
  2781. while (first_sector <= last_sector) {
  2782. pd_idx = stripe_to_pdidx(first_sector, conf,
  2783. conf->previous_raid_disks);
  2784. sh = get_active_stripe(conf, first_sector,
  2785. conf->previous_raid_disks, pd_idx, 0);
  2786. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2787. set_bit(STRIPE_HANDLE, &sh->state);
  2788. release_stripe(sh);
  2789. first_sector += STRIPE_SECTORS;
  2790. }
  2791. return conf->chunk_size>>9;
  2792. }
  2793. /* FIXME go_faster isn't used */
  2794. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  2795. {
  2796. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2797. struct stripe_head *sh;
  2798. int pd_idx;
  2799. int raid_disks = conf->raid_disks;
  2800. sector_t max_sector = mddev->size << 1;
  2801. int sync_blocks;
  2802. int still_degraded = 0;
  2803. int i;
  2804. if (sector_nr >= max_sector) {
  2805. /* just being told to finish up .. nothing much to do */
  2806. unplug_slaves(mddev);
  2807. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2808. end_reshape(conf);
  2809. return 0;
  2810. }
  2811. if (mddev->curr_resync < max_sector) /* aborted */
  2812. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2813. &sync_blocks, 1);
  2814. else /* completed sync */
  2815. conf->fullsync = 0;
  2816. bitmap_close_sync(mddev->bitmap);
  2817. return 0;
  2818. }
  2819. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2820. return reshape_request(mddev, sector_nr, skipped);
  2821. /* if there is too many failed drives and we are trying
  2822. * to resync, then assert that we are finished, because there is
  2823. * nothing we can do.
  2824. */
  2825. if (mddev->degraded >= conf->max_degraded &&
  2826. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2827. sector_t rv = (mddev->size << 1) - sector_nr;
  2828. *skipped = 1;
  2829. return rv;
  2830. }
  2831. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2832. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2833. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  2834. /* we can skip this block, and probably more */
  2835. sync_blocks /= STRIPE_SECTORS;
  2836. *skipped = 1;
  2837. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  2838. }
  2839. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  2840. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  2841. if (sh == NULL) {
  2842. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  2843. /* make sure we don't swamp the stripe cache if someone else
  2844. * is trying to get access
  2845. */
  2846. schedule_timeout_uninterruptible(1);
  2847. }
  2848. /* Need to check if array will still be degraded after recovery/resync
  2849. * We don't need to check the 'failed' flag as when that gets set,
  2850. * recovery aborts.
  2851. */
  2852. for (i=0; i<mddev->raid_disks; i++)
  2853. if (conf->disks[i].rdev == NULL)
  2854. still_degraded = 1;
  2855. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  2856. spin_lock(&sh->lock);
  2857. set_bit(STRIPE_SYNCING, &sh->state);
  2858. clear_bit(STRIPE_INSYNC, &sh->state);
  2859. spin_unlock(&sh->lock);
  2860. handle_stripe(sh, NULL);
  2861. release_stripe(sh);
  2862. return STRIPE_SECTORS;
  2863. }
  2864. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  2865. {
  2866. /* We may not be able to submit a whole bio at once as there
  2867. * may not be enough stripe_heads available.
  2868. * We cannot pre-allocate enough stripe_heads as we may need
  2869. * more than exist in the cache (if we allow ever large chunks).
  2870. * So we do one stripe head at a time and record in
  2871. * ->bi_hw_segments how many have been done.
  2872. *
  2873. * We *know* that this entire raid_bio is in one chunk, so
  2874. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  2875. */
  2876. struct stripe_head *sh;
  2877. int dd_idx, pd_idx;
  2878. sector_t sector, logical_sector, last_sector;
  2879. int scnt = 0;
  2880. int remaining;
  2881. int handled = 0;
  2882. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2883. sector = raid5_compute_sector( logical_sector,
  2884. conf->raid_disks,
  2885. conf->raid_disks - conf->max_degraded,
  2886. &dd_idx,
  2887. &pd_idx,
  2888. conf);
  2889. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  2890. for (; logical_sector < last_sector;
  2891. logical_sector += STRIPE_SECTORS,
  2892. sector += STRIPE_SECTORS,
  2893. scnt++) {
  2894. if (scnt < raid_bio->bi_hw_segments)
  2895. /* already done this stripe */
  2896. continue;
  2897. sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
  2898. if (!sh) {
  2899. /* failed to get a stripe - must wait */
  2900. raid_bio->bi_hw_segments = scnt;
  2901. conf->retry_read_aligned = raid_bio;
  2902. return handled;
  2903. }
  2904. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  2905. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  2906. release_stripe(sh);
  2907. raid_bio->bi_hw_segments = scnt;
  2908. conf->retry_read_aligned = raid_bio;
  2909. return handled;
  2910. }
  2911. handle_stripe(sh, NULL);
  2912. release_stripe(sh);
  2913. handled++;
  2914. }
  2915. spin_lock_irq(&conf->device_lock);
  2916. remaining = --raid_bio->bi_phys_segments;
  2917. spin_unlock_irq(&conf->device_lock);
  2918. if (remaining == 0) {
  2919. int bytes = raid_bio->bi_size;
  2920. raid_bio->bi_size = 0;
  2921. raid_bio->bi_end_io(raid_bio, bytes,
  2922. test_bit(BIO_UPTODATE, &raid_bio->bi_flags)
  2923. ? 0 : -EIO);
  2924. }
  2925. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2926. wake_up(&conf->wait_for_stripe);
  2927. return handled;
  2928. }
  2929. /*
  2930. * This is our raid5 kernel thread.
  2931. *
  2932. * We scan the hash table for stripes which can be handled now.
  2933. * During the scan, completed stripes are saved for us by the interrupt
  2934. * handler, so that they will not have to wait for our next wakeup.
  2935. */
  2936. static void raid5d (mddev_t *mddev)
  2937. {
  2938. struct stripe_head *sh;
  2939. raid5_conf_t *conf = mddev_to_conf(mddev);
  2940. int handled;
  2941. PRINTK("+++ raid5d active\n");
  2942. md_check_recovery(mddev);
  2943. handled = 0;
  2944. spin_lock_irq(&conf->device_lock);
  2945. while (1) {
  2946. struct list_head *first;
  2947. struct bio *bio;
  2948. if (conf->seq_flush != conf->seq_write) {
  2949. int seq = conf->seq_flush;
  2950. spin_unlock_irq(&conf->device_lock);
  2951. bitmap_unplug(mddev->bitmap);
  2952. spin_lock_irq(&conf->device_lock);
  2953. conf->seq_write = seq;
  2954. activate_bit_delay(conf);
  2955. }
  2956. if (list_empty(&conf->handle_list) &&
  2957. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  2958. !blk_queue_plugged(mddev->queue) &&
  2959. !list_empty(&conf->delayed_list))
  2960. raid5_activate_delayed(conf);
  2961. while ((bio = remove_bio_from_retry(conf))) {
  2962. int ok;
  2963. spin_unlock_irq(&conf->device_lock);
  2964. ok = retry_aligned_read(conf, bio);
  2965. spin_lock_irq(&conf->device_lock);
  2966. if (!ok)
  2967. break;
  2968. handled++;
  2969. }
  2970. if (list_empty(&conf->handle_list))
  2971. break;
  2972. first = conf->handle_list.next;
  2973. sh = list_entry(first, struct stripe_head, lru);
  2974. list_del_init(first);
  2975. atomic_inc(&sh->count);
  2976. BUG_ON(atomic_read(&sh->count)!= 1);
  2977. spin_unlock_irq(&conf->device_lock);
  2978. handled++;
  2979. handle_stripe(sh, conf->spare_page);
  2980. release_stripe(sh);
  2981. spin_lock_irq(&conf->device_lock);
  2982. }
  2983. PRINTK("%d stripes handled\n", handled);
  2984. spin_unlock_irq(&conf->device_lock);
  2985. unplug_slaves(mddev);
  2986. PRINTK("--- raid5d inactive\n");
  2987. }
  2988. static ssize_t
  2989. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  2990. {
  2991. raid5_conf_t *conf = mddev_to_conf(mddev);
  2992. if (conf)
  2993. return sprintf(page, "%d\n", conf->max_nr_stripes);
  2994. else
  2995. return 0;
  2996. }
  2997. static ssize_t
  2998. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  2999. {
  3000. raid5_conf_t *conf = mddev_to_conf(mddev);
  3001. char *end;
  3002. int new;
  3003. if (len >= PAGE_SIZE)
  3004. return -EINVAL;
  3005. if (!conf)
  3006. return -ENODEV;
  3007. new = simple_strtoul(page, &end, 10);
  3008. if (!*page || (*end && *end != '\n') )
  3009. return -EINVAL;
  3010. if (new <= 16 || new > 32768)
  3011. return -EINVAL;
  3012. while (new < conf->max_nr_stripes) {
  3013. if (drop_one_stripe(conf))
  3014. conf->max_nr_stripes--;
  3015. else
  3016. break;
  3017. }
  3018. md_allow_write(mddev);
  3019. while (new > conf->max_nr_stripes) {
  3020. if (grow_one_stripe(conf))
  3021. conf->max_nr_stripes++;
  3022. else break;
  3023. }
  3024. return len;
  3025. }
  3026. static struct md_sysfs_entry
  3027. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  3028. raid5_show_stripe_cache_size,
  3029. raid5_store_stripe_cache_size);
  3030. static ssize_t
  3031. stripe_cache_active_show(mddev_t *mddev, char *page)
  3032. {
  3033. raid5_conf_t *conf = mddev_to_conf(mddev);
  3034. if (conf)
  3035. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  3036. else
  3037. return 0;
  3038. }
  3039. static struct md_sysfs_entry
  3040. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  3041. static struct attribute *raid5_attrs[] = {
  3042. &raid5_stripecache_size.attr,
  3043. &raid5_stripecache_active.attr,
  3044. NULL,
  3045. };
  3046. static struct attribute_group raid5_attrs_group = {
  3047. .name = NULL,
  3048. .attrs = raid5_attrs,
  3049. };
  3050. static int run(mddev_t *mddev)
  3051. {
  3052. raid5_conf_t *conf;
  3053. int raid_disk, memory;
  3054. mdk_rdev_t *rdev;
  3055. struct disk_info *disk;
  3056. struct list_head *tmp;
  3057. int working_disks = 0;
  3058. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  3059. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3060. mdname(mddev), mddev->level);
  3061. return -EIO;
  3062. }
  3063. if (mddev->reshape_position != MaxSector) {
  3064. /* Check that we can continue the reshape.
  3065. * Currently only disks can change, it must
  3066. * increase, and we must be past the point where
  3067. * a stripe over-writes itself
  3068. */
  3069. sector_t here_new, here_old;
  3070. int old_disks;
  3071. int max_degraded = (mddev->level == 5 ? 1 : 2);
  3072. if (mddev->new_level != mddev->level ||
  3073. mddev->new_layout != mddev->layout ||
  3074. mddev->new_chunk != mddev->chunk_size) {
  3075. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3076. "required - aborting.\n",
  3077. mdname(mddev));
  3078. return -EINVAL;
  3079. }
  3080. if (mddev->delta_disks <= 0) {
  3081. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3082. "(reduce disks) required - aborting.\n",
  3083. mdname(mddev));
  3084. return -EINVAL;
  3085. }
  3086. old_disks = mddev->raid_disks - mddev->delta_disks;
  3087. /* reshape_position must be on a new-stripe boundary, and one
  3088. * further up in new geometry must map after here in old
  3089. * geometry.
  3090. */
  3091. here_new = mddev->reshape_position;
  3092. if (sector_div(here_new, (mddev->chunk_size>>9)*
  3093. (mddev->raid_disks - max_degraded))) {
  3094. printk(KERN_ERR "raid5: reshape_position not "
  3095. "on a stripe boundary\n");
  3096. return -EINVAL;
  3097. }
  3098. /* here_new is the stripe we will write to */
  3099. here_old = mddev->reshape_position;
  3100. sector_div(here_old, (mddev->chunk_size>>9)*
  3101. (old_disks-max_degraded));
  3102. /* here_old is the first stripe that we might need to read
  3103. * from */
  3104. if (here_new >= here_old) {
  3105. /* Reading from the same stripe as writing to - bad */
  3106. printk(KERN_ERR "raid5: reshape_position too early for "
  3107. "auto-recovery - aborting.\n");
  3108. return -EINVAL;
  3109. }
  3110. printk(KERN_INFO "raid5: reshape will continue\n");
  3111. /* OK, we should be able to continue; */
  3112. }
  3113. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  3114. if ((conf = mddev->private) == NULL)
  3115. goto abort;
  3116. if (mddev->reshape_position == MaxSector) {
  3117. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  3118. } else {
  3119. conf->raid_disks = mddev->raid_disks;
  3120. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3121. }
  3122. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3123. GFP_KERNEL);
  3124. if (!conf->disks)
  3125. goto abort;
  3126. conf->mddev = mddev;
  3127. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3128. goto abort;
  3129. if (mddev->level == 6) {
  3130. conf->spare_page = alloc_page(GFP_KERNEL);
  3131. if (!conf->spare_page)
  3132. goto abort;
  3133. }
  3134. spin_lock_init(&conf->device_lock);
  3135. init_waitqueue_head(&conf->wait_for_stripe);
  3136. init_waitqueue_head(&conf->wait_for_overlap);
  3137. INIT_LIST_HEAD(&conf->handle_list);
  3138. INIT_LIST_HEAD(&conf->delayed_list);
  3139. INIT_LIST_HEAD(&conf->bitmap_list);
  3140. INIT_LIST_HEAD(&conf->inactive_list);
  3141. atomic_set(&conf->active_stripes, 0);
  3142. atomic_set(&conf->preread_active_stripes, 0);
  3143. atomic_set(&conf->active_aligned_reads, 0);
  3144. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  3145. ITERATE_RDEV(mddev,rdev,tmp) {
  3146. raid_disk = rdev->raid_disk;
  3147. if (raid_disk >= conf->raid_disks
  3148. || raid_disk < 0)
  3149. continue;
  3150. disk = conf->disks + raid_disk;
  3151. disk->rdev = rdev;
  3152. if (test_bit(In_sync, &rdev->flags)) {
  3153. char b[BDEVNAME_SIZE];
  3154. printk(KERN_INFO "raid5: device %s operational as raid"
  3155. " disk %d\n", bdevname(rdev->bdev,b),
  3156. raid_disk);
  3157. working_disks++;
  3158. }
  3159. }
  3160. /*
  3161. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3162. */
  3163. mddev->degraded = conf->raid_disks - working_disks;
  3164. conf->mddev = mddev;
  3165. conf->chunk_size = mddev->chunk_size;
  3166. conf->level = mddev->level;
  3167. if (conf->level == 6)
  3168. conf->max_degraded = 2;
  3169. else
  3170. conf->max_degraded = 1;
  3171. conf->algorithm = mddev->layout;
  3172. conf->max_nr_stripes = NR_STRIPES;
  3173. conf->expand_progress = mddev->reshape_position;
  3174. /* device size must be a multiple of chunk size */
  3175. mddev->size &= ~(mddev->chunk_size/1024 -1);
  3176. mddev->resync_max_sectors = mddev->size << 1;
  3177. if (conf->level == 6 && conf->raid_disks < 4) {
  3178. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3179. mdname(mddev), conf->raid_disks);
  3180. goto abort;
  3181. }
  3182. if (!conf->chunk_size || conf->chunk_size % 4) {
  3183. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3184. conf->chunk_size, mdname(mddev));
  3185. goto abort;
  3186. }
  3187. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  3188. printk(KERN_ERR
  3189. "raid5: unsupported parity algorithm %d for %s\n",
  3190. conf->algorithm, mdname(mddev));
  3191. goto abort;
  3192. }
  3193. if (mddev->degraded > conf->max_degraded) {
  3194. printk(KERN_ERR "raid5: not enough operational devices for %s"
  3195. " (%d/%d failed)\n",
  3196. mdname(mddev), mddev->degraded, conf->raid_disks);
  3197. goto abort;
  3198. }
  3199. if (mddev->degraded > 0 &&
  3200. mddev->recovery_cp != MaxSector) {
  3201. if (mddev->ok_start_degraded)
  3202. printk(KERN_WARNING
  3203. "raid5: starting dirty degraded array: %s"
  3204. "- data corruption possible.\n",
  3205. mdname(mddev));
  3206. else {
  3207. printk(KERN_ERR
  3208. "raid5: cannot start dirty degraded array for %s\n",
  3209. mdname(mddev));
  3210. goto abort;
  3211. }
  3212. }
  3213. {
  3214. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3215. if (!mddev->thread) {
  3216. printk(KERN_ERR
  3217. "raid5: couldn't allocate thread for %s\n",
  3218. mdname(mddev));
  3219. goto abort;
  3220. }
  3221. }
  3222. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3223. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3224. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3225. printk(KERN_ERR
  3226. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3227. shrink_stripes(conf);
  3228. md_unregister_thread(mddev->thread);
  3229. goto abort;
  3230. } else
  3231. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3232. memory, mdname(mddev));
  3233. if (mddev->degraded == 0)
  3234. printk("raid5: raid level %d set %s active with %d out of %d"
  3235. " devices, algorithm %d\n", conf->level, mdname(mddev),
  3236. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  3237. conf->algorithm);
  3238. else
  3239. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  3240. " out of %d devices, algorithm %d\n", conf->level,
  3241. mdname(mddev), mddev->raid_disks - mddev->degraded,
  3242. mddev->raid_disks, conf->algorithm);
  3243. print_raid5_conf(conf);
  3244. if (conf->expand_progress != MaxSector) {
  3245. printk("...ok start reshape thread\n");
  3246. conf->expand_lo = conf->expand_progress;
  3247. atomic_set(&conf->reshape_stripes, 0);
  3248. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3249. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3250. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3251. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3252. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3253. "%s_reshape");
  3254. }
  3255. /* read-ahead size must cover two whole stripes, which is
  3256. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3257. */
  3258. {
  3259. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3260. int stripe = data_disks *
  3261. (mddev->chunk_size / PAGE_SIZE);
  3262. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3263. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3264. }
  3265. /* Ok, everything is just fine now */
  3266. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  3267. printk(KERN_WARNING
  3268. "raid5: failed to create sysfs attributes for %s\n",
  3269. mdname(mddev));
  3270. mddev->queue->unplug_fn = raid5_unplug_device;
  3271. mddev->queue->issue_flush_fn = raid5_issue_flush;
  3272. mddev->queue->backing_dev_info.congested_data = mddev;
  3273. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  3274. mddev->array_size = mddev->size * (conf->previous_raid_disks -
  3275. conf->max_degraded);
  3276. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  3277. return 0;
  3278. abort:
  3279. if (conf) {
  3280. print_raid5_conf(conf);
  3281. safe_put_page(conf->spare_page);
  3282. kfree(conf->disks);
  3283. kfree(conf->stripe_hashtbl);
  3284. kfree(conf);
  3285. }
  3286. mddev->private = NULL;
  3287. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  3288. return -EIO;
  3289. }
  3290. static int stop(mddev_t *mddev)
  3291. {
  3292. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3293. md_unregister_thread(mddev->thread);
  3294. mddev->thread = NULL;
  3295. shrink_stripes(conf);
  3296. kfree(conf->stripe_hashtbl);
  3297. mddev->queue->backing_dev_info.congested_fn = NULL;
  3298. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3299. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3300. kfree(conf->disks);
  3301. kfree(conf);
  3302. mddev->private = NULL;
  3303. return 0;
  3304. }
  3305. #if RAID5_DEBUG
  3306. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  3307. {
  3308. int i;
  3309. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3310. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3311. seq_printf(seq, "sh %llu, count %d.\n",
  3312. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3313. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3314. for (i = 0; i < sh->disks; i++) {
  3315. seq_printf(seq, "(cache%d: %p %ld) ",
  3316. i, sh->dev[i].page, sh->dev[i].flags);
  3317. }
  3318. seq_printf(seq, "\n");
  3319. }
  3320. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  3321. {
  3322. struct stripe_head *sh;
  3323. struct hlist_node *hn;
  3324. int i;
  3325. spin_lock_irq(&conf->device_lock);
  3326. for (i = 0; i < NR_HASH; i++) {
  3327. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3328. if (sh->raid_conf != conf)
  3329. continue;
  3330. print_sh(seq, sh);
  3331. }
  3332. }
  3333. spin_unlock_irq(&conf->device_lock);
  3334. }
  3335. #endif
  3336. static void status (struct seq_file *seq, mddev_t *mddev)
  3337. {
  3338. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3339. int i;
  3340. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3341. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  3342. for (i = 0; i < conf->raid_disks; i++)
  3343. seq_printf (seq, "%s",
  3344. conf->disks[i].rdev &&
  3345. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3346. seq_printf (seq, "]");
  3347. #if RAID5_DEBUG
  3348. seq_printf (seq, "\n");
  3349. printall(seq, conf);
  3350. #endif
  3351. }
  3352. static void print_raid5_conf (raid5_conf_t *conf)
  3353. {
  3354. int i;
  3355. struct disk_info *tmp;
  3356. printk("RAID5 conf printout:\n");
  3357. if (!conf) {
  3358. printk("(conf==NULL)\n");
  3359. return;
  3360. }
  3361. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  3362. conf->raid_disks - conf->mddev->degraded);
  3363. for (i = 0; i < conf->raid_disks; i++) {
  3364. char b[BDEVNAME_SIZE];
  3365. tmp = conf->disks + i;
  3366. if (tmp->rdev)
  3367. printk(" disk %d, o:%d, dev:%s\n",
  3368. i, !test_bit(Faulty, &tmp->rdev->flags),
  3369. bdevname(tmp->rdev->bdev,b));
  3370. }
  3371. }
  3372. static int raid5_spare_active(mddev_t *mddev)
  3373. {
  3374. int i;
  3375. raid5_conf_t *conf = mddev->private;
  3376. struct disk_info *tmp;
  3377. for (i = 0; i < conf->raid_disks; i++) {
  3378. tmp = conf->disks + i;
  3379. if (tmp->rdev
  3380. && !test_bit(Faulty, &tmp->rdev->flags)
  3381. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  3382. unsigned long flags;
  3383. spin_lock_irqsave(&conf->device_lock, flags);
  3384. mddev->degraded--;
  3385. spin_unlock_irqrestore(&conf->device_lock, flags);
  3386. }
  3387. }
  3388. print_raid5_conf(conf);
  3389. return 0;
  3390. }
  3391. static int raid5_remove_disk(mddev_t *mddev, int number)
  3392. {
  3393. raid5_conf_t *conf = mddev->private;
  3394. int err = 0;
  3395. mdk_rdev_t *rdev;
  3396. struct disk_info *p = conf->disks + number;
  3397. print_raid5_conf(conf);
  3398. rdev = p->rdev;
  3399. if (rdev) {
  3400. if (test_bit(In_sync, &rdev->flags) ||
  3401. atomic_read(&rdev->nr_pending)) {
  3402. err = -EBUSY;
  3403. goto abort;
  3404. }
  3405. p->rdev = NULL;
  3406. synchronize_rcu();
  3407. if (atomic_read(&rdev->nr_pending)) {
  3408. /* lost the race, try later */
  3409. err = -EBUSY;
  3410. p->rdev = rdev;
  3411. }
  3412. }
  3413. abort:
  3414. print_raid5_conf(conf);
  3415. return err;
  3416. }
  3417. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3418. {
  3419. raid5_conf_t *conf = mddev->private;
  3420. int found = 0;
  3421. int disk;
  3422. struct disk_info *p;
  3423. if (mddev->degraded > conf->max_degraded)
  3424. /* no point adding a device */
  3425. return 0;
  3426. /*
  3427. * find the disk ... but prefer rdev->saved_raid_disk
  3428. * if possible.
  3429. */
  3430. if (rdev->saved_raid_disk >= 0 &&
  3431. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3432. disk = rdev->saved_raid_disk;
  3433. else
  3434. disk = 0;
  3435. for ( ; disk < conf->raid_disks; disk++)
  3436. if ((p=conf->disks + disk)->rdev == NULL) {
  3437. clear_bit(In_sync, &rdev->flags);
  3438. rdev->raid_disk = disk;
  3439. found = 1;
  3440. if (rdev->saved_raid_disk != disk)
  3441. conf->fullsync = 1;
  3442. rcu_assign_pointer(p->rdev, rdev);
  3443. break;
  3444. }
  3445. print_raid5_conf(conf);
  3446. return found;
  3447. }
  3448. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3449. {
  3450. /* no resync is happening, and there is enough space
  3451. * on all devices, so we can resize.
  3452. * We need to make sure resync covers any new space.
  3453. * If the array is shrinking we should possibly wait until
  3454. * any io in the removed space completes, but it hardly seems
  3455. * worth it.
  3456. */
  3457. raid5_conf_t *conf = mddev_to_conf(mddev);
  3458. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3459. mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
  3460. set_capacity(mddev->gendisk, mddev->array_size << 1);
  3461. mddev->changed = 1;
  3462. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3463. mddev->recovery_cp = mddev->size << 1;
  3464. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3465. }
  3466. mddev->size = sectors /2;
  3467. mddev->resync_max_sectors = sectors;
  3468. return 0;
  3469. }
  3470. #ifdef CONFIG_MD_RAID5_RESHAPE
  3471. static int raid5_check_reshape(mddev_t *mddev)
  3472. {
  3473. raid5_conf_t *conf = mddev_to_conf(mddev);
  3474. int err;
  3475. if (mddev->delta_disks < 0 ||
  3476. mddev->new_level != mddev->level)
  3477. return -EINVAL; /* Cannot shrink array or change level yet */
  3478. if (mddev->delta_disks == 0)
  3479. return 0; /* nothing to do */
  3480. /* Can only proceed if there are plenty of stripe_heads.
  3481. * We need a minimum of one full stripe,, and for sensible progress
  3482. * it is best to have about 4 times that.
  3483. * If we require 4 times, then the default 256 4K stripe_heads will
  3484. * allow for chunk sizes up to 256K, which is probably OK.
  3485. * If the chunk size is greater, user-space should request more
  3486. * stripe_heads first.
  3487. */
  3488. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3489. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3490. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3491. (mddev->chunk_size / STRIPE_SIZE)*4);
  3492. return -ENOSPC;
  3493. }
  3494. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  3495. if (err)
  3496. return err;
  3497. if (mddev->degraded > conf->max_degraded)
  3498. return -EINVAL;
  3499. /* looks like we might be able to manage this */
  3500. return 0;
  3501. }
  3502. static int raid5_start_reshape(mddev_t *mddev)
  3503. {
  3504. raid5_conf_t *conf = mddev_to_conf(mddev);
  3505. mdk_rdev_t *rdev;
  3506. struct list_head *rtmp;
  3507. int spares = 0;
  3508. int added_devices = 0;
  3509. unsigned long flags;
  3510. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3511. return -EBUSY;
  3512. ITERATE_RDEV(mddev, rdev, rtmp)
  3513. if (rdev->raid_disk < 0 &&
  3514. !test_bit(Faulty, &rdev->flags))
  3515. spares++;
  3516. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  3517. /* Not enough devices even to make a degraded array
  3518. * of that size
  3519. */
  3520. return -EINVAL;
  3521. atomic_set(&conf->reshape_stripes, 0);
  3522. spin_lock_irq(&conf->device_lock);
  3523. conf->previous_raid_disks = conf->raid_disks;
  3524. conf->raid_disks += mddev->delta_disks;
  3525. conf->expand_progress = 0;
  3526. conf->expand_lo = 0;
  3527. spin_unlock_irq(&conf->device_lock);
  3528. /* Add some new drives, as many as will fit.
  3529. * We know there are enough to make the newly sized array work.
  3530. */
  3531. ITERATE_RDEV(mddev, rdev, rtmp)
  3532. if (rdev->raid_disk < 0 &&
  3533. !test_bit(Faulty, &rdev->flags)) {
  3534. if (raid5_add_disk(mddev, rdev)) {
  3535. char nm[20];
  3536. set_bit(In_sync, &rdev->flags);
  3537. added_devices++;
  3538. rdev->recovery_offset = 0;
  3539. sprintf(nm, "rd%d", rdev->raid_disk);
  3540. if (sysfs_create_link(&mddev->kobj,
  3541. &rdev->kobj, nm))
  3542. printk(KERN_WARNING
  3543. "raid5: failed to create "
  3544. " link %s for %s\n",
  3545. nm, mdname(mddev));
  3546. } else
  3547. break;
  3548. }
  3549. spin_lock_irqsave(&conf->device_lock, flags);
  3550. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  3551. spin_unlock_irqrestore(&conf->device_lock, flags);
  3552. mddev->raid_disks = conf->raid_disks;
  3553. mddev->reshape_position = 0;
  3554. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3555. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3556. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3557. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3558. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3559. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3560. "%s_reshape");
  3561. if (!mddev->sync_thread) {
  3562. mddev->recovery = 0;
  3563. spin_lock_irq(&conf->device_lock);
  3564. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  3565. conf->expand_progress = MaxSector;
  3566. spin_unlock_irq(&conf->device_lock);
  3567. return -EAGAIN;
  3568. }
  3569. md_wakeup_thread(mddev->sync_thread);
  3570. md_new_event(mddev);
  3571. return 0;
  3572. }
  3573. #endif
  3574. static void end_reshape(raid5_conf_t *conf)
  3575. {
  3576. struct block_device *bdev;
  3577. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  3578. conf->mddev->array_size = conf->mddev->size *
  3579. (conf->raid_disks - conf->max_degraded);
  3580. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  3581. conf->mddev->changed = 1;
  3582. bdev = bdget_disk(conf->mddev->gendisk, 0);
  3583. if (bdev) {
  3584. mutex_lock(&bdev->bd_inode->i_mutex);
  3585. i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
  3586. mutex_unlock(&bdev->bd_inode->i_mutex);
  3587. bdput(bdev);
  3588. }
  3589. spin_lock_irq(&conf->device_lock);
  3590. conf->expand_progress = MaxSector;
  3591. spin_unlock_irq(&conf->device_lock);
  3592. conf->mddev->reshape_position = MaxSector;
  3593. /* read-ahead size must cover two whole stripes, which is
  3594. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3595. */
  3596. {
  3597. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3598. int stripe = data_disks *
  3599. (conf->mddev->chunk_size / PAGE_SIZE);
  3600. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3601. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3602. }
  3603. }
  3604. }
  3605. static void raid5_quiesce(mddev_t *mddev, int state)
  3606. {
  3607. raid5_conf_t *conf = mddev_to_conf(mddev);
  3608. switch(state) {
  3609. case 2: /* resume for a suspend */
  3610. wake_up(&conf->wait_for_overlap);
  3611. break;
  3612. case 1: /* stop all writes */
  3613. spin_lock_irq(&conf->device_lock);
  3614. conf->quiesce = 1;
  3615. wait_event_lock_irq(conf->wait_for_stripe,
  3616. atomic_read(&conf->active_stripes) == 0 &&
  3617. atomic_read(&conf->active_aligned_reads) == 0,
  3618. conf->device_lock, /* nothing */);
  3619. spin_unlock_irq(&conf->device_lock);
  3620. break;
  3621. case 0: /* re-enable writes */
  3622. spin_lock_irq(&conf->device_lock);
  3623. conf->quiesce = 0;
  3624. wake_up(&conf->wait_for_stripe);
  3625. wake_up(&conf->wait_for_overlap);
  3626. spin_unlock_irq(&conf->device_lock);
  3627. break;
  3628. }
  3629. }
  3630. static struct mdk_personality raid6_personality =
  3631. {
  3632. .name = "raid6",
  3633. .level = 6,
  3634. .owner = THIS_MODULE,
  3635. .make_request = make_request,
  3636. .run = run,
  3637. .stop = stop,
  3638. .status = status,
  3639. .error_handler = error,
  3640. .hot_add_disk = raid5_add_disk,
  3641. .hot_remove_disk= raid5_remove_disk,
  3642. .spare_active = raid5_spare_active,
  3643. .sync_request = sync_request,
  3644. .resize = raid5_resize,
  3645. #ifdef CONFIG_MD_RAID5_RESHAPE
  3646. .check_reshape = raid5_check_reshape,
  3647. .start_reshape = raid5_start_reshape,
  3648. #endif
  3649. .quiesce = raid5_quiesce,
  3650. };
  3651. static struct mdk_personality raid5_personality =
  3652. {
  3653. .name = "raid5",
  3654. .level = 5,
  3655. .owner = THIS_MODULE,
  3656. .make_request = make_request,
  3657. .run = run,
  3658. .stop = stop,
  3659. .status = status,
  3660. .error_handler = error,
  3661. .hot_add_disk = raid5_add_disk,
  3662. .hot_remove_disk= raid5_remove_disk,
  3663. .spare_active = raid5_spare_active,
  3664. .sync_request = sync_request,
  3665. .resize = raid5_resize,
  3666. #ifdef CONFIG_MD_RAID5_RESHAPE
  3667. .check_reshape = raid5_check_reshape,
  3668. .start_reshape = raid5_start_reshape,
  3669. #endif
  3670. .quiesce = raid5_quiesce,
  3671. };
  3672. static struct mdk_personality raid4_personality =
  3673. {
  3674. .name = "raid4",
  3675. .level = 4,
  3676. .owner = THIS_MODULE,
  3677. .make_request = make_request,
  3678. .run = run,
  3679. .stop = stop,
  3680. .status = status,
  3681. .error_handler = error,
  3682. .hot_add_disk = raid5_add_disk,
  3683. .hot_remove_disk= raid5_remove_disk,
  3684. .spare_active = raid5_spare_active,
  3685. .sync_request = sync_request,
  3686. .resize = raid5_resize,
  3687. #ifdef CONFIG_MD_RAID5_RESHAPE
  3688. .check_reshape = raid5_check_reshape,
  3689. .start_reshape = raid5_start_reshape,
  3690. #endif
  3691. .quiesce = raid5_quiesce,
  3692. };
  3693. static int __init raid5_init(void)
  3694. {
  3695. int e;
  3696. e = raid6_select_algo();
  3697. if ( e )
  3698. return e;
  3699. register_md_personality(&raid6_personality);
  3700. register_md_personality(&raid5_personality);
  3701. register_md_personality(&raid4_personality);
  3702. return 0;
  3703. }
  3704. static void raid5_exit(void)
  3705. {
  3706. unregister_md_personality(&raid6_personality);
  3707. unregister_md_personality(&raid5_personality);
  3708. unregister_md_personality(&raid4_personality);
  3709. }
  3710. module_init(raid5_init);
  3711. module_exit(raid5_exit);
  3712. MODULE_LICENSE("GPL");
  3713. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  3714. MODULE_ALIAS("md-raid5");
  3715. MODULE_ALIAS("md-raid4");
  3716. MODULE_ALIAS("md-level-5");
  3717. MODULE_ALIAS("md-level-4");
  3718. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  3719. MODULE_ALIAS("md-raid6");
  3720. MODULE_ALIAS("md-level-6");
  3721. /* This used to be two separate modules, they were: */
  3722. MODULE_ALIAS("raid5");
  3723. MODULE_ALIAS("raid6");