raid5.c 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011
  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->seq_write is the number of the last batch successfully written.
  30. * conf->seq_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 seq_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/blkdev.h>
  45. #include <linux/kthread.h>
  46. #include <linux/raid/pq.h>
  47. #include <linux/async_tx.h>
  48. #include <linux/async.h>
  49. #include <linux/seq_file.h>
  50. #include <linux/cpu.h>
  51. #include <linux/slab.h>
  52. #include "md.h"
  53. #include "raid5.h"
  54. #include "raid0.h"
  55. #include "bitmap.h"
  56. /*
  57. * Stripe cache
  58. */
  59. #define NR_STRIPES 256
  60. #define STRIPE_SIZE PAGE_SIZE
  61. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  62. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  63. #define IO_THRESHOLD 1
  64. #define BYPASS_THRESHOLD 1
  65. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  66. #define HASH_MASK (NR_HASH - 1)
  67. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  68. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  69. * order without overlap. There may be several bio's per stripe+device, and
  70. * a bio could span several devices.
  71. * When walking this list for a particular stripe+device, we must never proceed
  72. * beyond a bio that extends past this device, as the next bio might no longer
  73. * be valid.
  74. * This macro is used to determine the 'next' bio in the list, given the sector
  75. * of the current stripe+device
  76. */
  77. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  78. /*
  79. * The following can be used to debug the driver
  80. */
  81. #define RAID5_PARANOIA 1
  82. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  83. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  84. #else
  85. # define CHECK_DEVLOCK()
  86. #endif
  87. #ifdef DEBUG
  88. #define inline
  89. #define __inline__
  90. #endif
  91. #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
  92. /*
  93. * We maintain a biased count of active stripes in the bottom 16 bits of
  94. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  95. */
  96. static inline int raid5_bi_phys_segments(struct bio *bio)
  97. {
  98. return bio->bi_phys_segments & 0xffff;
  99. }
  100. static inline int raid5_bi_hw_segments(struct bio *bio)
  101. {
  102. return (bio->bi_phys_segments >> 16) & 0xffff;
  103. }
  104. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  105. {
  106. --bio->bi_phys_segments;
  107. return raid5_bi_phys_segments(bio);
  108. }
  109. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  110. {
  111. unsigned short val = raid5_bi_hw_segments(bio);
  112. --val;
  113. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  114. return val;
  115. }
  116. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  117. {
  118. bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
  119. }
  120. /* Find first data disk in a raid6 stripe */
  121. static inline int raid6_d0(struct stripe_head *sh)
  122. {
  123. if (sh->ddf_layout)
  124. /* ddf always start from first device */
  125. return 0;
  126. /* md starts just after Q block */
  127. if (sh->qd_idx == sh->disks - 1)
  128. return 0;
  129. else
  130. return sh->qd_idx + 1;
  131. }
  132. static inline int raid6_next_disk(int disk, int raid_disks)
  133. {
  134. disk++;
  135. return (disk < raid_disks) ? disk : 0;
  136. }
  137. /* When walking through the disks in a raid5, starting at raid6_d0,
  138. * We need to map each disk to a 'slot', where the data disks are slot
  139. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  140. * is raid_disks-1. This help does that mapping.
  141. */
  142. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  143. int *count, int syndrome_disks)
  144. {
  145. int slot = *count;
  146. if (sh->ddf_layout)
  147. (*count)++;
  148. if (idx == sh->pd_idx)
  149. return syndrome_disks;
  150. if (idx == sh->qd_idx)
  151. return syndrome_disks + 1;
  152. if (!sh->ddf_layout)
  153. (*count)++;
  154. return slot;
  155. }
  156. static void return_io(struct bio *return_bi)
  157. {
  158. struct bio *bi = return_bi;
  159. while (bi) {
  160. return_bi = bi->bi_next;
  161. bi->bi_next = NULL;
  162. bi->bi_size = 0;
  163. bio_endio(bi, 0);
  164. bi = return_bi;
  165. }
  166. }
  167. static void print_raid5_conf (raid5_conf_t *conf);
  168. static int stripe_operations_active(struct stripe_head *sh)
  169. {
  170. return sh->check_state || sh->reconstruct_state ||
  171. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  172. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  173. }
  174. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  175. {
  176. if (atomic_dec_and_test(&sh->count)) {
  177. BUG_ON(!list_empty(&sh->lru));
  178. BUG_ON(atomic_read(&conf->active_stripes)==0);
  179. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  180. if (test_bit(STRIPE_DELAYED, &sh->state))
  181. list_add_tail(&sh->lru, &conf->delayed_list);
  182. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  183. sh->bm_seq - conf->seq_write > 0)
  184. list_add_tail(&sh->lru, &conf->bitmap_list);
  185. else {
  186. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  187. list_add_tail(&sh->lru, &conf->handle_list);
  188. }
  189. md_wakeup_thread(conf->mddev->thread);
  190. } else {
  191. BUG_ON(stripe_operations_active(sh));
  192. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  193. atomic_dec(&conf->preread_active_stripes);
  194. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  195. md_wakeup_thread(conf->mddev->thread);
  196. }
  197. atomic_dec(&conf->active_stripes);
  198. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  199. list_add_tail(&sh->lru, &conf->inactive_list);
  200. wake_up(&conf->wait_for_stripe);
  201. if (conf->retry_read_aligned)
  202. md_wakeup_thread(conf->mddev->thread);
  203. }
  204. }
  205. }
  206. }
  207. static void release_stripe(struct stripe_head *sh)
  208. {
  209. raid5_conf_t *conf = sh->raid_conf;
  210. unsigned long flags;
  211. spin_lock_irqsave(&conf->device_lock, flags);
  212. __release_stripe(conf, sh);
  213. spin_unlock_irqrestore(&conf->device_lock, flags);
  214. }
  215. static inline void remove_hash(struct stripe_head *sh)
  216. {
  217. pr_debug("remove_hash(), stripe %llu\n",
  218. (unsigned long long)sh->sector);
  219. hlist_del_init(&sh->hash);
  220. }
  221. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  222. {
  223. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  224. pr_debug("insert_hash(), stripe %llu\n",
  225. (unsigned long long)sh->sector);
  226. CHECK_DEVLOCK();
  227. hlist_add_head(&sh->hash, hp);
  228. }
  229. /* find an idle stripe, make sure it is unhashed, and return it. */
  230. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  231. {
  232. struct stripe_head *sh = NULL;
  233. struct list_head *first;
  234. CHECK_DEVLOCK();
  235. if (list_empty(&conf->inactive_list))
  236. goto out;
  237. first = conf->inactive_list.next;
  238. sh = list_entry(first, struct stripe_head, lru);
  239. list_del_init(first);
  240. remove_hash(sh);
  241. atomic_inc(&conf->active_stripes);
  242. out:
  243. return sh;
  244. }
  245. static void shrink_buffers(struct stripe_head *sh)
  246. {
  247. struct page *p;
  248. int i;
  249. int num = sh->raid_conf->pool_size;
  250. for (i = 0; i < num ; i++) {
  251. p = sh->dev[i].page;
  252. if (!p)
  253. continue;
  254. sh->dev[i].page = NULL;
  255. put_page(p);
  256. }
  257. }
  258. static int grow_buffers(struct stripe_head *sh)
  259. {
  260. int i;
  261. int num = sh->raid_conf->pool_size;
  262. for (i = 0; i < num; i++) {
  263. struct page *page;
  264. if (!(page = alloc_page(GFP_KERNEL))) {
  265. return 1;
  266. }
  267. sh->dev[i].page = page;
  268. }
  269. return 0;
  270. }
  271. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  272. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  273. struct stripe_head *sh);
  274. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  275. {
  276. raid5_conf_t *conf = sh->raid_conf;
  277. int i;
  278. BUG_ON(atomic_read(&sh->count) != 0);
  279. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  280. BUG_ON(stripe_operations_active(sh));
  281. CHECK_DEVLOCK();
  282. pr_debug("init_stripe called, stripe %llu\n",
  283. (unsigned long long)sh->sector);
  284. remove_hash(sh);
  285. sh->generation = conf->generation - previous;
  286. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  287. sh->sector = sector;
  288. stripe_set_idx(sector, conf, previous, sh);
  289. sh->state = 0;
  290. for (i = sh->disks; i--; ) {
  291. struct r5dev *dev = &sh->dev[i];
  292. if (dev->toread || dev->read || dev->towrite || dev->written ||
  293. test_bit(R5_LOCKED, &dev->flags)) {
  294. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  295. (unsigned long long)sh->sector, i, dev->toread,
  296. dev->read, dev->towrite, dev->written,
  297. test_bit(R5_LOCKED, &dev->flags));
  298. BUG();
  299. }
  300. dev->flags = 0;
  301. raid5_build_block(sh, i, previous);
  302. }
  303. insert_hash(conf, sh);
  304. }
  305. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
  306. short generation)
  307. {
  308. struct stripe_head *sh;
  309. struct hlist_node *hn;
  310. CHECK_DEVLOCK();
  311. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  312. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  313. if (sh->sector == sector && sh->generation == generation)
  314. return sh;
  315. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  316. return NULL;
  317. }
  318. /*
  319. * Need to check if array has failed when deciding whether to:
  320. * - start an array
  321. * - remove non-faulty devices
  322. * - add a spare
  323. * - allow a reshape
  324. * This determination is simple when no reshape is happening.
  325. * However if there is a reshape, we need to carefully check
  326. * both the before and after sections.
  327. * This is because some failed devices may only affect one
  328. * of the two sections, and some non-in_sync devices may
  329. * be insync in the section most affected by failed devices.
  330. */
  331. static int has_failed(raid5_conf_t *conf)
  332. {
  333. int degraded;
  334. int i;
  335. if (conf->mddev->reshape_position == MaxSector)
  336. return conf->mddev->degraded > conf->max_degraded;
  337. rcu_read_lock();
  338. degraded = 0;
  339. for (i = 0; i < conf->previous_raid_disks; i++) {
  340. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  341. if (!rdev || test_bit(Faulty, &rdev->flags))
  342. degraded++;
  343. else if (test_bit(In_sync, &rdev->flags))
  344. ;
  345. else
  346. /* not in-sync or faulty.
  347. * If the reshape increases the number of devices,
  348. * this is being recovered by the reshape, so
  349. * this 'previous' section is not in_sync.
  350. * If the number of devices is being reduced however,
  351. * the device can only be part of the array if
  352. * we are reverting a reshape, so this section will
  353. * be in-sync.
  354. */
  355. if (conf->raid_disks >= conf->previous_raid_disks)
  356. degraded++;
  357. }
  358. rcu_read_unlock();
  359. if (degraded > conf->max_degraded)
  360. return 1;
  361. rcu_read_lock();
  362. degraded = 0;
  363. for (i = 0; i < conf->raid_disks; i++) {
  364. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  365. if (!rdev || test_bit(Faulty, &rdev->flags))
  366. degraded++;
  367. else if (test_bit(In_sync, &rdev->flags))
  368. ;
  369. else
  370. /* not in-sync or faulty.
  371. * If reshape increases the number of devices, this
  372. * section has already been recovered, else it
  373. * almost certainly hasn't.
  374. */
  375. if (conf->raid_disks <= conf->previous_raid_disks)
  376. degraded++;
  377. }
  378. rcu_read_unlock();
  379. if (degraded > conf->max_degraded)
  380. return 1;
  381. return 0;
  382. }
  383. static struct stripe_head *
  384. get_active_stripe(raid5_conf_t *conf, sector_t sector,
  385. int previous, int noblock, int noquiesce)
  386. {
  387. struct stripe_head *sh;
  388. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  389. spin_lock_irq(&conf->device_lock);
  390. do {
  391. wait_event_lock_irq(conf->wait_for_stripe,
  392. conf->quiesce == 0 || noquiesce,
  393. conf->device_lock, /* nothing */);
  394. sh = __find_stripe(conf, sector, conf->generation - previous);
  395. if (!sh) {
  396. if (!conf->inactive_blocked)
  397. sh = get_free_stripe(conf);
  398. if (noblock && sh == NULL)
  399. break;
  400. if (!sh) {
  401. conf->inactive_blocked = 1;
  402. wait_event_lock_irq(conf->wait_for_stripe,
  403. !list_empty(&conf->inactive_list) &&
  404. (atomic_read(&conf->active_stripes)
  405. < (conf->max_nr_stripes *3/4)
  406. || !conf->inactive_blocked),
  407. conf->device_lock,
  408. );
  409. conf->inactive_blocked = 0;
  410. } else
  411. init_stripe(sh, sector, previous);
  412. } else {
  413. if (atomic_read(&sh->count)) {
  414. BUG_ON(!list_empty(&sh->lru)
  415. && !test_bit(STRIPE_EXPANDING, &sh->state));
  416. } else {
  417. if (!test_bit(STRIPE_HANDLE, &sh->state))
  418. atomic_inc(&conf->active_stripes);
  419. if (list_empty(&sh->lru) &&
  420. !test_bit(STRIPE_EXPANDING, &sh->state))
  421. BUG();
  422. list_del_init(&sh->lru);
  423. }
  424. }
  425. } while (sh == NULL);
  426. if (sh)
  427. atomic_inc(&sh->count);
  428. spin_unlock_irq(&conf->device_lock);
  429. return sh;
  430. }
  431. static void
  432. raid5_end_read_request(struct bio *bi, int error);
  433. static void
  434. raid5_end_write_request(struct bio *bi, int error);
  435. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  436. {
  437. raid5_conf_t *conf = sh->raid_conf;
  438. int i, disks = sh->disks;
  439. might_sleep();
  440. for (i = disks; i--; ) {
  441. int rw;
  442. struct bio *bi;
  443. mdk_rdev_t *rdev;
  444. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  445. if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
  446. rw = WRITE_FUA;
  447. else
  448. rw = WRITE;
  449. } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  450. rw = READ;
  451. else
  452. continue;
  453. bi = &sh->dev[i].req;
  454. bi->bi_rw = rw;
  455. if (rw == WRITE)
  456. bi->bi_end_io = raid5_end_write_request;
  457. else
  458. bi->bi_end_io = raid5_end_read_request;
  459. rcu_read_lock();
  460. rdev = rcu_dereference(conf->disks[i].rdev);
  461. if (rdev && test_bit(Faulty, &rdev->flags))
  462. rdev = NULL;
  463. if (rdev)
  464. atomic_inc(&rdev->nr_pending);
  465. rcu_read_unlock();
  466. if (rdev) {
  467. if (s->syncing || s->expanding || s->expanded)
  468. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  469. set_bit(STRIPE_IO_STARTED, &sh->state);
  470. bi->bi_bdev = rdev->bdev;
  471. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  472. __func__, (unsigned long long)sh->sector,
  473. bi->bi_rw, i);
  474. atomic_inc(&sh->count);
  475. bi->bi_sector = sh->sector + rdev->data_offset;
  476. bi->bi_flags = 1 << BIO_UPTODATE;
  477. bi->bi_vcnt = 1;
  478. bi->bi_max_vecs = 1;
  479. bi->bi_idx = 0;
  480. bi->bi_io_vec = &sh->dev[i].vec;
  481. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  482. bi->bi_io_vec[0].bv_offset = 0;
  483. bi->bi_size = STRIPE_SIZE;
  484. bi->bi_next = NULL;
  485. if (rw == WRITE &&
  486. test_bit(R5_ReWrite, &sh->dev[i].flags))
  487. atomic_add(STRIPE_SECTORS,
  488. &rdev->corrected_errors);
  489. generic_make_request(bi);
  490. } else {
  491. if (rw == WRITE)
  492. set_bit(STRIPE_DEGRADED, &sh->state);
  493. pr_debug("skip op %ld on disc %d for sector %llu\n",
  494. bi->bi_rw, i, (unsigned long long)sh->sector);
  495. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  496. set_bit(STRIPE_HANDLE, &sh->state);
  497. }
  498. }
  499. }
  500. static struct dma_async_tx_descriptor *
  501. async_copy_data(int frombio, struct bio *bio, struct page *page,
  502. sector_t sector, struct dma_async_tx_descriptor *tx)
  503. {
  504. struct bio_vec *bvl;
  505. struct page *bio_page;
  506. int i;
  507. int page_offset;
  508. struct async_submit_ctl submit;
  509. enum async_tx_flags flags = 0;
  510. if (bio->bi_sector >= sector)
  511. page_offset = (signed)(bio->bi_sector - sector) * 512;
  512. else
  513. page_offset = (signed)(sector - bio->bi_sector) * -512;
  514. if (frombio)
  515. flags |= ASYNC_TX_FENCE;
  516. init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
  517. bio_for_each_segment(bvl, bio, i) {
  518. int len = bio_iovec_idx(bio, i)->bv_len;
  519. int clen;
  520. int b_offset = 0;
  521. if (page_offset < 0) {
  522. b_offset = -page_offset;
  523. page_offset += b_offset;
  524. len -= b_offset;
  525. }
  526. if (len > 0 && page_offset + len > STRIPE_SIZE)
  527. clen = STRIPE_SIZE - page_offset;
  528. else
  529. clen = len;
  530. if (clen > 0) {
  531. b_offset += bio_iovec_idx(bio, i)->bv_offset;
  532. bio_page = bio_iovec_idx(bio, i)->bv_page;
  533. if (frombio)
  534. tx = async_memcpy(page, bio_page, page_offset,
  535. b_offset, clen, &submit);
  536. else
  537. tx = async_memcpy(bio_page, page, b_offset,
  538. page_offset, clen, &submit);
  539. }
  540. /* chain the operations */
  541. submit.depend_tx = tx;
  542. if (clen < len) /* hit end of page */
  543. break;
  544. page_offset += len;
  545. }
  546. return tx;
  547. }
  548. static void ops_complete_biofill(void *stripe_head_ref)
  549. {
  550. struct stripe_head *sh = stripe_head_ref;
  551. struct bio *return_bi = NULL;
  552. raid5_conf_t *conf = sh->raid_conf;
  553. int i;
  554. pr_debug("%s: stripe %llu\n", __func__,
  555. (unsigned long long)sh->sector);
  556. /* clear completed biofills */
  557. spin_lock_irq(&conf->device_lock);
  558. for (i = sh->disks; i--; ) {
  559. struct r5dev *dev = &sh->dev[i];
  560. /* acknowledge completion of a biofill operation */
  561. /* and check if we need to reply to a read request,
  562. * new R5_Wantfill requests are held off until
  563. * !STRIPE_BIOFILL_RUN
  564. */
  565. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  566. struct bio *rbi, *rbi2;
  567. BUG_ON(!dev->read);
  568. rbi = dev->read;
  569. dev->read = NULL;
  570. while (rbi && rbi->bi_sector <
  571. dev->sector + STRIPE_SECTORS) {
  572. rbi2 = r5_next_bio(rbi, dev->sector);
  573. if (!raid5_dec_bi_phys_segments(rbi)) {
  574. rbi->bi_next = return_bi;
  575. return_bi = rbi;
  576. }
  577. rbi = rbi2;
  578. }
  579. }
  580. }
  581. spin_unlock_irq(&conf->device_lock);
  582. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  583. return_io(return_bi);
  584. set_bit(STRIPE_HANDLE, &sh->state);
  585. release_stripe(sh);
  586. }
  587. static void ops_run_biofill(struct stripe_head *sh)
  588. {
  589. struct dma_async_tx_descriptor *tx = NULL;
  590. raid5_conf_t *conf = sh->raid_conf;
  591. struct async_submit_ctl submit;
  592. int i;
  593. pr_debug("%s: stripe %llu\n", __func__,
  594. (unsigned long long)sh->sector);
  595. for (i = sh->disks; i--; ) {
  596. struct r5dev *dev = &sh->dev[i];
  597. if (test_bit(R5_Wantfill, &dev->flags)) {
  598. struct bio *rbi;
  599. spin_lock_irq(&conf->device_lock);
  600. dev->read = rbi = dev->toread;
  601. dev->toread = NULL;
  602. spin_unlock_irq(&conf->device_lock);
  603. while (rbi && rbi->bi_sector <
  604. dev->sector + STRIPE_SECTORS) {
  605. tx = async_copy_data(0, rbi, dev->page,
  606. dev->sector, tx);
  607. rbi = r5_next_bio(rbi, dev->sector);
  608. }
  609. }
  610. }
  611. atomic_inc(&sh->count);
  612. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
  613. async_trigger_callback(&submit);
  614. }
  615. static void mark_target_uptodate(struct stripe_head *sh, int target)
  616. {
  617. struct r5dev *tgt;
  618. if (target < 0)
  619. return;
  620. tgt = &sh->dev[target];
  621. set_bit(R5_UPTODATE, &tgt->flags);
  622. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  623. clear_bit(R5_Wantcompute, &tgt->flags);
  624. }
  625. static void ops_complete_compute(void *stripe_head_ref)
  626. {
  627. struct stripe_head *sh = stripe_head_ref;
  628. pr_debug("%s: stripe %llu\n", __func__,
  629. (unsigned long long)sh->sector);
  630. /* mark the computed target(s) as uptodate */
  631. mark_target_uptodate(sh, sh->ops.target);
  632. mark_target_uptodate(sh, sh->ops.target2);
  633. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  634. if (sh->check_state == check_state_compute_run)
  635. sh->check_state = check_state_compute_result;
  636. set_bit(STRIPE_HANDLE, &sh->state);
  637. release_stripe(sh);
  638. }
  639. /* return a pointer to the address conversion region of the scribble buffer */
  640. static addr_conv_t *to_addr_conv(struct stripe_head *sh,
  641. struct raid5_percpu *percpu)
  642. {
  643. return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
  644. }
  645. static struct dma_async_tx_descriptor *
  646. ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
  647. {
  648. int disks = sh->disks;
  649. struct page **xor_srcs = percpu->scribble;
  650. int target = sh->ops.target;
  651. struct r5dev *tgt = &sh->dev[target];
  652. struct page *xor_dest = tgt->page;
  653. int count = 0;
  654. struct dma_async_tx_descriptor *tx;
  655. struct async_submit_ctl submit;
  656. int i;
  657. pr_debug("%s: stripe %llu block: %d\n",
  658. __func__, (unsigned long long)sh->sector, target);
  659. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  660. for (i = disks; i--; )
  661. if (i != target)
  662. xor_srcs[count++] = sh->dev[i].page;
  663. atomic_inc(&sh->count);
  664. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
  665. ops_complete_compute, sh, to_addr_conv(sh, percpu));
  666. if (unlikely(count == 1))
  667. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  668. else
  669. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  670. return tx;
  671. }
  672. /* set_syndrome_sources - populate source buffers for gen_syndrome
  673. * @srcs - (struct page *) array of size sh->disks
  674. * @sh - stripe_head to parse
  675. *
  676. * Populates srcs in proper layout order for the stripe and returns the
  677. * 'count' of sources to be used in a call to async_gen_syndrome. The P
  678. * destination buffer is recorded in srcs[count] and the Q destination
  679. * is recorded in srcs[count+1]].
  680. */
  681. static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
  682. {
  683. int disks = sh->disks;
  684. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  685. int d0_idx = raid6_d0(sh);
  686. int count;
  687. int i;
  688. for (i = 0; i < disks; i++)
  689. srcs[i] = NULL;
  690. count = 0;
  691. i = d0_idx;
  692. do {
  693. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  694. srcs[slot] = sh->dev[i].page;
  695. i = raid6_next_disk(i, disks);
  696. } while (i != d0_idx);
  697. return syndrome_disks;
  698. }
  699. static struct dma_async_tx_descriptor *
  700. ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
  701. {
  702. int disks = sh->disks;
  703. struct page **blocks = percpu->scribble;
  704. int target;
  705. int qd_idx = sh->qd_idx;
  706. struct dma_async_tx_descriptor *tx;
  707. struct async_submit_ctl submit;
  708. struct r5dev *tgt;
  709. struct page *dest;
  710. int i;
  711. int count;
  712. if (sh->ops.target < 0)
  713. target = sh->ops.target2;
  714. else if (sh->ops.target2 < 0)
  715. target = sh->ops.target;
  716. else
  717. /* we should only have one valid target */
  718. BUG();
  719. BUG_ON(target < 0);
  720. pr_debug("%s: stripe %llu block: %d\n",
  721. __func__, (unsigned long long)sh->sector, target);
  722. tgt = &sh->dev[target];
  723. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  724. dest = tgt->page;
  725. atomic_inc(&sh->count);
  726. if (target == qd_idx) {
  727. count = set_syndrome_sources(blocks, sh);
  728. blocks[count] = NULL; /* regenerating p is not necessary */
  729. BUG_ON(blocks[count+1] != dest); /* q should already be set */
  730. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  731. ops_complete_compute, sh,
  732. to_addr_conv(sh, percpu));
  733. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  734. } else {
  735. /* Compute any data- or p-drive using XOR */
  736. count = 0;
  737. for (i = disks; i-- ; ) {
  738. if (i == target || i == qd_idx)
  739. continue;
  740. blocks[count++] = sh->dev[i].page;
  741. }
  742. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  743. NULL, ops_complete_compute, sh,
  744. to_addr_conv(sh, percpu));
  745. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
  746. }
  747. return tx;
  748. }
  749. static struct dma_async_tx_descriptor *
  750. ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
  751. {
  752. int i, count, disks = sh->disks;
  753. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  754. int d0_idx = raid6_d0(sh);
  755. int faila = -1, failb = -1;
  756. int target = sh->ops.target;
  757. int target2 = sh->ops.target2;
  758. struct r5dev *tgt = &sh->dev[target];
  759. struct r5dev *tgt2 = &sh->dev[target2];
  760. struct dma_async_tx_descriptor *tx;
  761. struct page **blocks = percpu->scribble;
  762. struct async_submit_ctl submit;
  763. pr_debug("%s: stripe %llu block1: %d block2: %d\n",
  764. __func__, (unsigned long long)sh->sector, target, target2);
  765. BUG_ON(target < 0 || target2 < 0);
  766. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  767. BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
  768. /* we need to open-code set_syndrome_sources to handle the
  769. * slot number conversion for 'faila' and 'failb'
  770. */
  771. for (i = 0; i < disks ; i++)
  772. blocks[i] = NULL;
  773. count = 0;
  774. i = d0_idx;
  775. do {
  776. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  777. blocks[slot] = sh->dev[i].page;
  778. if (i == target)
  779. faila = slot;
  780. if (i == target2)
  781. failb = slot;
  782. i = raid6_next_disk(i, disks);
  783. } while (i != d0_idx);
  784. BUG_ON(faila == failb);
  785. if (failb < faila)
  786. swap(faila, failb);
  787. pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
  788. __func__, (unsigned long long)sh->sector, faila, failb);
  789. atomic_inc(&sh->count);
  790. if (failb == syndrome_disks+1) {
  791. /* Q disk is one of the missing disks */
  792. if (faila == syndrome_disks) {
  793. /* Missing P+Q, just recompute */
  794. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  795. ops_complete_compute, sh,
  796. to_addr_conv(sh, percpu));
  797. return async_gen_syndrome(blocks, 0, syndrome_disks+2,
  798. STRIPE_SIZE, &submit);
  799. } else {
  800. struct page *dest;
  801. int data_target;
  802. int qd_idx = sh->qd_idx;
  803. /* Missing D+Q: recompute D from P, then recompute Q */
  804. if (target == qd_idx)
  805. data_target = target2;
  806. else
  807. data_target = target;
  808. count = 0;
  809. for (i = disks; i-- ; ) {
  810. if (i == data_target || i == qd_idx)
  811. continue;
  812. blocks[count++] = sh->dev[i].page;
  813. }
  814. dest = sh->dev[data_target].page;
  815. init_async_submit(&submit,
  816. ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  817. NULL, NULL, NULL,
  818. to_addr_conv(sh, percpu));
  819. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
  820. &submit);
  821. count = set_syndrome_sources(blocks, sh);
  822. init_async_submit(&submit, ASYNC_TX_FENCE, tx,
  823. ops_complete_compute, sh,
  824. to_addr_conv(sh, percpu));
  825. return async_gen_syndrome(blocks, 0, count+2,
  826. STRIPE_SIZE, &submit);
  827. }
  828. } else {
  829. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  830. ops_complete_compute, sh,
  831. to_addr_conv(sh, percpu));
  832. if (failb == syndrome_disks) {
  833. /* We're missing D+P. */
  834. return async_raid6_datap_recov(syndrome_disks+2,
  835. STRIPE_SIZE, faila,
  836. blocks, &submit);
  837. } else {
  838. /* We're missing D+D. */
  839. return async_raid6_2data_recov(syndrome_disks+2,
  840. STRIPE_SIZE, faila, failb,
  841. blocks, &submit);
  842. }
  843. }
  844. }
  845. static void ops_complete_prexor(void *stripe_head_ref)
  846. {
  847. struct stripe_head *sh = stripe_head_ref;
  848. pr_debug("%s: stripe %llu\n", __func__,
  849. (unsigned long long)sh->sector);
  850. }
  851. static struct dma_async_tx_descriptor *
  852. ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
  853. struct dma_async_tx_descriptor *tx)
  854. {
  855. int disks = sh->disks;
  856. struct page **xor_srcs = percpu->scribble;
  857. int count = 0, pd_idx = sh->pd_idx, i;
  858. struct async_submit_ctl submit;
  859. /* existing parity data subtracted */
  860. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  861. pr_debug("%s: stripe %llu\n", __func__,
  862. (unsigned long long)sh->sector);
  863. for (i = disks; i--; ) {
  864. struct r5dev *dev = &sh->dev[i];
  865. /* Only process blocks that are known to be uptodate */
  866. if (test_bit(R5_Wantdrain, &dev->flags))
  867. xor_srcs[count++] = dev->page;
  868. }
  869. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  870. ops_complete_prexor, sh, to_addr_conv(sh, percpu));
  871. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  872. return tx;
  873. }
  874. static struct dma_async_tx_descriptor *
  875. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  876. {
  877. int disks = sh->disks;
  878. int i;
  879. pr_debug("%s: stripe %llu\n", __func__,
  880. (unsigned long long)sh->sector);
  881. for (i = disks; i--; ) {
  882. struct r5dev *dev = &sh->dev[i];
  883. struct bio *chosen;
  884. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  885. struct bio *wbi;
  886. spin_lock(&sh->lock);
  887. chosen = dev->towrite;
  888. dev->towrite = NULL;
  889. BUG_ON(dev->written);
  890. wbi = dev->written = chosen;
  891. spin_unlock(&sh->lock);
  892. while (wbi && wbi->bi_sector <
  893. dev->sector + STRIPE_SECTORS) {
  894. if (wbi->bi_rw & REQ_FUA)
  895. set_bit(R5_WantFUA, &dev->flags);
  896. tx = async_copy_data(1, wbi, dev->page,
  897. dev->sector, tx);
  898. wbi = r5_next_bio(wbi, dev->sector);
  899. }
  900. }
  901. }
  902. return tx;
  903. }
  904. static void ops_complete_reconstruct(void *stripe_head_ref)
  905. {
  906. struct stripe_head *sh = stripe_head_ref;
  907. int disks = sh->disks;
  908. int pd_idx = sh->pd_idx;
  909. int qd_idx = sh->qd_idx;
  910. int i;
  911. bool fua = false;
  912. pr_debug("%s: stripe %llu\n", __func__,
  913. (unsigned long long)sh->sector);
  914. for (i = disks; i--; )
  915. fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
  916. for (i = disks; i--; ) {
  917. struct r5dev *dev = &sh->dev[i];
  918. if (dev->written || i == pd_idx || i == qd_idx) {
  919. set_bit(R5_UPTODATE, &dev->flags);
  920. if (fua)
  921. set_bit(R5_WantFUA, &dev->flags);
  922. }
  923. }
  924. if (sh->reconstruct_state == reconstruct_state_drain_run)
  925. sh->reconstruct_state = reconstruct_state_drain_result;
  926. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  927. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  928. else {
  929. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  930. sh->reconstruct_state = reconstruct_state_result;
  931. }
  932. set_bit(STRIPE_HANDLE, &sh->state);
  933. release_stripe(sh);
  934. }
  935. static void
  936. ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
  937. struct dma_async_tx_descriptor *tx)
  938. {
  939. int disks = sh->disks;
  940. struct page **xor_srcs = percpu->scribble;
  941. struct async_submit_ctl submit;
  942. int count = 0, pd_idx = sh->pd_idx, i;
  943. struct page *xor_dest;
  944. int prexor = 0;
  945. unsigned long flags;
  946. pr_debug("%s: stripe %llu\n", __func__,
  947. (unsigned long long)sh->sector);
  948. /* check if prexor is active which means only process blocks
  949. * that are part of a read-modify-write (written)
  950. */
  951. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  952. prexor = 1;
  953. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  954. for (i = disks; i--; ) {
  955. struct r5dev *dev = &sh->dev[i];
  956. if (dev->written)
  957. xor_srcs[count++] = dev->page;
  958. }
  959. } else {
  960. xor_dest = sh->dev[pd_idx].page;
  961. for (i = disks; i--; ) {
  962. struct r5dev *dev = &sh->dev[i];
  963. if (i != pd_idx)
  964. xor_srcs[count++] = dev->page;
  965. }
  966. }
  967. /* 1/ if we prexor'd then the dest is reused as a source
  968. * 2/ if we did not prexor then we are redoing the parity
  969. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  970. * for the synchronous xor case
  971. */
  972. flags = ASYNC_TX_ACK |
  973. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  974. atomic_inc(&sh->count);
  975. init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
  976. to_addr_conv(sh, percpu));
  977. if (unlikely(count == 1))
  978. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  979. else
  980. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  981. }
  982. static void
  983. ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
  984. struct dma_async_tx_descriptor *tx)
  985. {
  986. struct async_submit_ctl submit;
  987. struct page **blocks = percpu->scribble;
  988. int count;
  989. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  990. count = set_syndrome_sources(blocks, sh);
  991. atomic_inc(&sh->count);
  992. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
  993. sh, to_addr_conv(sh, percpu));
  994. async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  995. }
  996. static void ops_complete_check(void *stripe_head_ref)
  997. {
  998. struct stripe_head *sh = stripe_head_ref;
  999. pr_debug("%s: stripe %llu\n", __func__,
  1000. (unsigned long long)sh->sector);
  1001. sh->check_state = check_state_check_result;
  1002. set_bit(STRIPE_HANDLE, &sh->state);
  1003. release_stripe(sh);
  1004. }
  1005. static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
  1006. {
  1007. int disks = sh->disks;
  1008. int pd_idx = sh->pd_idx;
  1009. int qd_idx = sh->qd_idx;
  1010. struct page *xor_dest;
  1011. struct page **xor_srcs = percpu->scribble;
  1012. struct dma_async_tx_descriptor *tx;
  1013. struct async_submit_ctl submit;
  1014. int count;
  1015. int i;
  1016. pr_debug("%s: stripe %llu\n", __func__,
  1017. (unsigned long long)sh->sector);
  1018. count = 0;
  1019. xor_dest = sh->dev[pd_idx].page;
  1020. xor_srcs[count++] = xor_dest;
  1021. for (i = disks; i--; ) {
  1022. if (i == pd_idx || i == qd_idx)
  1023. continue;
  1024. xor_srcs[count++] = sh->dev[i].page;
  1025. }
  1026. init_async_submit(&submit, 0, NULL, NULL, NULL,
  1027. to_addr_conv(sh, percpu));
  1028. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  1029. &sh->ops.zero_sum_result, &submit);
  1030. atomic_inc(&sh->count);
  1031. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
  1032. tx = async_trigger_callback(&submit);
  1033. }
  1034. static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
  1035. {
  1036. struct page **srcs = percpu->scribble;
  1037. struct async_submit_ctl submit;
  1038. int count;
  1039. pr_debug("%s: stripe %llu checkp: %d\n", __func__,
  1040. (unsigned long long)sh->sector, checkp);
  1041. count = set_syndrome_sources(srcs, sh);
  1042. if (!checkp)
  1043. srcs[count] = NULL;
  1044. atomic_inc(&sh->count);
  1045. init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
  1046. sh, to_addr_conv(sh, percpu));
  1047. async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
  1048. &sh->ops.zero_sum_result, percpu->spare_page, &submit);
  1049. }
  1050. static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1051. {
  1052. int overlap_clear = 0, i, disks = sh->disks;
  1053. struct dma_async_tx_descriptor *tx = NULL;
  1054. raid5_conf_t *conf = sh->raid_conf;
  1055. int level = conf->level;
  1056. struct raid5_percpu *percpu;
  1057. unsigned long cpu;
  1058. cpu = get_cpu();
  1059. percpu = per_cpu_ptr(conf->percpu, cpu);
  1060. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  1061. ops_run_biofill(sh);
  1062. overlap_clear++;
  1063. }
  1064. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  1065. if (level < 6)
  1066. tx = ops_run_compute5(sh, percpu);
  1067. else {
  1068. if (sh->ops.target2 < 0 || sh->ops.target < 0)
  1069. tx = ops_run_compute6_1(sh, percpu);
  1070. else
  1071. tx = ops_run_compute6_2(sh, percpu);
  1072. }
  1073. /* terminate the chain if reconstruct is not set to be run */
  1074. if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
  1075. async_tx_ack(tx);
  1076. }
  1077. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  1078. tx = ops_run_prexor(sh, percpu, tx);
  1079. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  1080. tx = ops_run_biodrain(sh, tx);
  1081. overlap_clear++;
  1082. }
  1083. if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
  1084. if (level < 6)
  1085. ops_run_reconstruct5(sh, percpu, tx);
  1086. else
  1087. ops_run_reconstruct6(sh, percpu, tx);
  1088. }
  1089. if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
  1090. if (sh->check_state == check_state_run)
  1091. ops_run_check_p(sh, percpu);
  1092. else if (sh->check_state == check_state_run_q)
  1093. ops_run_check_pq(sh, percpu, 0);
  1094. else if (sh->check_state == check_state_run_pq)
  1095. ops_run_check_pq(sh, percpu, 1);
  1096. else
  1097. BUG();
  1098. }
  1099. if (overlap_clear)
  1100. for (i = disks; i--; ) {
  1101. struct r5dev *dev = &sh->dev[i];
  1102. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1103. wake_up(&sh->raid_conf->wait_for_overlap);
  1104. }
  1105. put_cpu();
  1106. }
  1107. #ifdef CONFIG_MULTICORE_RAID456
  1108. static void async_run_ops(void *param, async_cookie_t cookie)
  1109. {
  1110. struct stripe_head *sh = param;
  1111. unsigned long ops_request = sh->ops.request;
  1112. clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
  1113. wake_up(&sh->ops.wait_for_ops);
  1114. __raid_run_ops(sh, ops_request);
  1115. release_stripe(sh);
  1116. }
  1117. static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1118. {
  1119. /* since handle_stripe can be called outside of raid5d context
  1120. * we need to ensure sh->ops.request is de-staged before another
  1121. * request arrives
  1122. */
  1123. wait_event(sh->ops.wait_for_ops,
  1124. !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
  1125. sh->ops.request = ops_request;
  1126. atomic_inc(&sh->count);
  1127. async_schedule(async_run_ops, sh);
  1128. }
  1129. #else
  1130. #define raid_run_ops __raid_run_ops
  1131. #endif
  1132. static int grow_one_stripe(raid5_conf_t *conf)
  1133. {
  1134. struct stripe_head *sh;
  1135. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  1136. if (!sh)
  1137. return 0;
  1138. memset(sh, 0, sizeof(*sh) + (conf->pool_size-1)*sizeof(struct r5dev));
  1139. sh->raid_conf = conf;
  1140. spin_lock_init(&sh->lock);
  1141. #ifdef CONFIG_MULTICORE_RAID456
  1142. init_waitqueue_head(&sh->ops.wait_for_ops);
  1143. #endif
  1144. if (grow_buffers(sh)) {
  1145. shrink_buffers(sh);
  1146. kmem_cache_free(conf->slab_cache, sh);
  1147. return 0;
  1148. }
  1149. /* we just created an active stripe so... */
  1150. atomic_set(&sh->count, 1);
  1151. atomic_inc(&conf->active_stripes);
  1152. INIT_LIST_HEAD(&sh->lru);
  1153. release_stripe(sh);
  1154. return 1;
  1155. }
  1156. static int grow_stripes(raid5_conf_t *conf, int num)
  1157. {
  1158. struct kmem_cache *sc;
  1159. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  1160. if (conf->mddev->gendisk)
  1161. sprintf(conf->cache_name[0],
  1162. "raid%d-%s", conf->level, mdname(conf->mddev));
  1163. else
  1164. sprintf(conf->cache_name[0],
  1165. "raid%d-%p", conf->level, conf->mddev);
  1166. sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
  1167. conf->active_name = 0;
  1168. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  1169. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  1170. 0, 0, NULL);
  1171. if (!sc)
  1172. return 1;
  1173. conf->slab_cache = sc;
  1174. conf->pool_size = devs;
  1175. while (num--)
  1176. if (!grow_one_stripe(conf))
  1177. return 1;
  1178. return 0;
  1179. }
  1180. /**
  1181. * scribble_len - return the required size of the scribble region
  1182. * @num - total number of disks in the array
  1183. *
  1184. * The size must be enough to contain:
  1185. * 1/ a struct page pointer for each device in the array +2
  1186. * 2/ room to convert each entry in (1) to its corresponding dma
  1187. * (dma_map_page()) or page (page_address()) address.
  1188. *
  1189. * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
  1190. * calculate over all devices (not just the data blocks), using zeros in place
  1191. * of the P and Q blocks.
  1192. */
  1193. static size_t scribble_len(int num)
  1194. {
  1195. size_t len;
  1196. len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
  1197. return len;
  1198. }
  1199. static int resize_stripes(raid5_conf_t *conf, int newsize)
  1200. {
  1201. /* Make all the stripes able to hold 'newsize' devices.
  1202. * New slots in each stripe get 'page' set to a new page.
  1203. *
  1204. * This happens in stages:
  1205. * 1/ create a new kmem_cache and allocate the required number of
  1206. * stripe_heads.
  1207. * 2/ gather all the old stripe_heads and tranfer the pages across
  1208. * to the new stripe_heads. This will have the side effect of
  1209. * freezing the array as once all stripe_heads have been collected,
  1210. * no IO will be possible. Old stripe heads are freed once their
  1211. * pages have been transferred over, and the old kmem_cache is
  1212. * freed when all stripes are done.
  1213. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  1214. * we simple return a failre status - no need to clean anything up.
  1215. * 4/ allocate new pages for the new slots in the new stripe_heads.
  1216. * If this fails, we don't bother trying the shrink the
  1217. * stripe_heads down again, we just leave them as they are.
  1218. * As each stripe_head is processed the new one is released into
  1219. * active service.
  1220. *
  1221. * Once step2 is started, we cannot afford to wait for a write,
  1222. * so we use GFP_NOIO allocations.
  1223. */
  1224. struct stripe_head *osh, *nsh;
  1225. LIST_HEAD(newstripes);
  1226. struct disk_info *ndisks;
  1227. unsigned long cpu;
  1228. int err;
  1229. struct kmem_cache *sc;
  1230. int i;
  1231. if (newsize <= conf->pool_size)
  1232. return 0; /* never bother to shrink */
  1233. err = md_allow_write(conf->mddev);
  1234. if (err)
  1235. return err;
  1236. /* Step 1 */
  1237. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  1238. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  1239. 0, 0, NULL);
  1240. if (!sc)
  1241. return -ENOMEM;
  1242. for (i = conf->max_nr_stripes; i; i--) {
  1243. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  1244. if (!nsh)
  1245. break;
  1246. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  1247. nsh->raid_conf = conf;
  1248. spin_lock_init(&nsh->lock);
  1249. #ifdef CONFIG_MULTICORE_RAID456
  1250. init_waitqueue_head(&nsh->ops.wait_for_ops);
  1251. #endif
  1252. list_add(&nsh->lru, &newstripes);
  1253. }
  1254. if (i) {
  1255. /* didn't get enough, give up */
  1256. while (!list_empty(&newstripes)) {
  1257. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1258. list_del(&nsh->lru);
  1259. kmem_cache_free(sc, nsh);
  1260. }
  1261. kmem_cache_destroy(sc);
  1262. return -ENOMEM;
  1263. }
  1264. /* Step 2 - Must use GFP_NOIO now.
  1265. * OK, we have enough stripes, start collecting inactive
  1266. * stripes and copying them over
  1267. */
  1268. list_for_each_entry(nsh, &newstripes, lru) {
  1269. spin_lock_irq(&conf->device_lock);
  1270. wait_event_lock_irq(conf->wait_for_stripe,
  1271. !list_empty(&conf->inactive_list),
  1272. conf->device_lock,
  1273. );
  1274. osh = get_free_stripe(conf);
  1275. spin_unlock_irq(&conf->device_lock);
  1276. atomic_set(&nsh->count, 1);
  1277. for(i=0; i<conf->pool_size; i++)
  1278. nsh->dev[i].page = osh->dev[i].page;
  1279. for( ; i<newsize; i++)
  1280. nsh->dev[i].page = NULL;
  1281. kmem_cache_free(conf->slab_cache, osh);
  1282. }
  1283. kmem_cache_destroy(conf->slab_cache);
  1284. /* Step 3.
  1285. * At this point, we are holding all the stripes so the array
  1286. * is completely stalled, so now is a good time to resize
  1287. * conf->disks and the scribble region
  1288. */
  1289. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  1290. if (ndisks) {
  1291. for (i=0; i<conf->raid_disks; i++)
  1292. ndisks[i] = conf->disks[i];
  1293. kfree(conf->disks);
  1294. conf->disks = ndisks;
  1295. } else
  1296. err = -ENOMEM;
  1297. get_online_cpus();
  1298. conf->scribble_len = scribble_len(newsize);
  1299. for_each_present_cpu(cpu) {
  1300. struct raid5_percpu *percpu;
  1301. void *scribble;
  1302. percpu = per_cpu_ptr(conf->percpu, cpu);
  1303. scribble = kmalloc(conf->scribble_len, GFP_NOIO);
  1304. if (scribble) {
  1305. kfree(percpu->scribble);
  1306. percpu->scribble = scribble;
  1307. } else {
  1308. err = -ENOMEM;
  1309. break;
  1310. }
  1311. }
  1312. put_online_cpus();
  1313. /* Step 4, return new stripes to service */
  1314. while(!list_empty(&newstripes)) {
  1315. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1316. list_del_init(&nsh->lru);
  1317. for (i=conf->raid_disks; i < newsize; i++)
  1318. if (nsh->dev[i].page == NULL) {
  1319. struct page *p = alloc_page(GFP_NOIO);
  1320. nsh->dev[i].page = p;
  1321. if (!p)
  1322. err = -ENOMEM;
  1323. }
  1324. release_stripe(nsh);
  1325. }
  1326. /* critical section pass, GFP_NOIO no longer needed */
  1327. conf->slab_cache = sc;
  1328. conf->active_name = 1-conf->active_name;
  1329. conf->pool_size = newsize;
  1330. return err;
  1331. }
  1332. static int drop_one_stripe(raid5_conf_t *conf)
  1333. {
  1334. struct stripe_head *sh;
  1335. spin_lock_irq(&conf->device_lock);
  1336. sh = get_free_stripe(conf);
  1337. spin_unlock_irq(&conf->device_lock);
  1338. if (!sh)
  1339. return 0;
  1340. BUG_ON(atomic_read(&sh->count));
  1341. shrink_buffers(sh);
  1342. kmem_cache_free(conf->slab_cache, sh);
  1343. atomic_dec(&conf->active_stripes);
  1344. return 1;
  1345. }
  1346. static void shrink_stripes(raid5_conf_t *conf)
  1347. {
  1348. while (drop_one_stripe(conf))
  1349. ;
  1350. if (conf->slab_cache)
  1351. kmem_cache_destroy(conf->slab_cache);
  1352. conf->slab_cache = NULL;
  1353. }
  1354. static void raid5_end_read_request(struct bio * bi, int error)
  1355. {
  1356. struct stripe_head *sh = bi->bi_private;
  1357. raid5_conf_t *conf = sh->raid_conf;
  1358. int disks = sh->disks, i;
  1359. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1360. char b[BDEVNAME_SIZE];
  1361. mdk_rdev_t *rdev;
  1362. for (i=0 ; i<disks; i++)
  1363. if (bi == &sh->dev[i].req)
  1364. break;
  1365. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  1366. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1367. uptodate);
  1368. if (i == disks) {
  1369. BUG();
  1370. return;
  1371. }
  1372. if (uptodate) {
  1373. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1374. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1375. rdev = conf->disks[i].rdev;
  1376. printk_rl(KERN_INFO "md/raid:%s: read error corrected"
  1377. " (%lu sectors at %llu on %s)\n",
  1378. mdname(conf->mddev), STRIPE_SECTORS,
  1379. (unsigned long long)(sh->sector
  1380. + rdev->data_offset),
  1381. bdevname(rdev->bdev, b));
  1382. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1383. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1384. }
  1385. if (atomic_read(&conf->disks[i].rdev->read_errors))
  1386. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  1387. } else {
  1388. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  1389. int retry = 0;
  1390. rdev = conf->disks[i].rdev;
  1391. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  1392. atomic_inc(&rdev->read_errors);
  1393. if (conf->mddev->degraded >= conf->max_degraded)
  1394. printk_rl(KERN_WARNING
  1395. "md/raid:%s: read error not correctable "
  1396. "(sector %llu on %s).\n",
  1397. mdname(conf->mddev),
  1398. (unsigned long long)(sh->sector
  1399. + rdev->data_offset),
  1400. bdn);
  1401. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  1402. /* Oh, no!!! */
  1403. printk_rl(KERN_WARNING
  1404. "md/raid:%s: read error NOT corrected!! "
  1405. "(sector %llu on %s).\n",
  1406. mdname(conf->mddev),
  1407. (unsigned long long)(sh->sector
  1408. + rdev->data_offset),
  1409. bdn);
  1410. else if (atomic_read(&rdev->read_errors)
  1411. > conf->max_nr_stripes)
  1412. printk(KERN_WARNING
  1413. "md/raid:%s: Too many read errors, failing device %s.\n",
  1414. mdname(conf->mddev), bdn);
  1415. else
  1416. retry = 1;
  1417. if (retry)
  1418. set_bit(R5_ReadError, &sh->dev[i].flags);
  1419. else {
  1420. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1421. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1422. md_error(conf->mddev, rdev);
  1423. }
  1424. }
  1425. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1426. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1427. set_bit(STRIPE_HANDLE, &sh->state);
  1428. release_stripe(sh);
  1429. }
  1430. static void raid5_end_write_request(struct bio *bi, int error)
  1431. {
  1432. struct stripe_head *sh = bi->bi_private;
  1433. raid5_conf_t *conf = sh->raid_conf;
  1434. int disks = sh->disks, i;
  1435. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1436. for (i=0 ; i<disks; i++)
  1437. if (bi == &sh->dev[i].req)
  1438. break;
  1439. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1440. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1441. uptodate);
  1442. if (i == disks) {
  1443. BUG();
  1444. return;
  1445. }
  1446. if (!uptodate)
  1447. md_error(conf->mddev, conf->disks[i].rdev);
  1448. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1449. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1450. set_bit(STRIPE_HANDLE, &sh->state);
  1451. release_stripe(sh);
  1452. }
  1453. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1454. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1455. {
  1456. struct r5dev *dev = &sh->dev[i];
  1457. bio_init(&dev->req);
  1458. dev->req.bi_io_vec = &dev->vec;
  1459. dev->req.bi_vcnt++;
  1460. dev->req.bi_max_vecs++;
  1461. dev->vec.bv_page = dev->page;
  1462. dev->vec.bv_len = STRIPE_SIZE;
  1463. dev->vec.bv_offset = 0;
  1464. dev->req.bi_sector = sh->sector;
  1465. dev->req.bi_private = sh;
  1466. dev->flags = 0;
  1467. dev->sector = compute_blocknr(sh, i, previous);
  1468. }
  1469. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1470. {
  1471. char b[BDEVNAME_SIZE];
  1472. raid5_conf_t *conf = mddev->private;
  1473. pr_debug("raid456: error called\n");
  1474. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1475. unsigned long flags;
  1476. spin_lock_irqsave(&conf->device_lock, flags);
  1477. mddev->degraded++;
  1478. spin_unlock_irqrestore(&conf->device_lock, flags);
  1479. /*
  1480. * if recovery was running, make sure it aborts.
  1481. */
  1482. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1483. }
  1484. set_bit(Faulty, &rdev->flags);
  1485. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1486. printk(KERN_ALERT
  1487. "md/raid:%s: Disk failure on %s, disabling device.\n"
  1488. "md/raid:%s: Operation continuing on %d devices.\n",
  1489. mdname(mddev),
  1490. bdevname(rdev->bdev, b),
  1491. mdname(mddev),
  1492. conf->raid_disks - mddev->degraded);
  1493. }
  1494. /*
  1495. * Input: a 'big' sector number,
  1496. * Output: index of the data and parity disk, and the sector # in them.
  1497. */
  1498. static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
  1499. int previous, int *dd_idx,
  1500. struct stripe_head *sh)
  1501. {
  1502. sector_t stripe, stripe2;
  1503. sector_t chunk_number;
  1504. unsigned int chunk_offset;
  1505. int pd_idx, qd_idx;
  1506. int ddf_layout = 0;
  1507. sector_t new_sector;
  1508. int algorithm = previous ? conf->prev_algo
  1509. : conf->algorithm;
  1510. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1511. : conf->chunk_sectors;
  1512. int raid_disks = previous ? conf->previous_raid_disks
  1513. : conf->raid_disks;
  1514. int data_disks = raid_disks - conf->max_degraded;
  1515. /* First compute the information on this sector */
  1516. /*
  1517. * Compute the chunk number and the sector offset inside the chunk
  1518. */
  1519. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1520. chunk_number = r_sector;
  1521. /*
  1522. * Compute the stripe number
  1523. */
  1524. stripe = chunk_number;
  1525. *dd_idx = sector_div(stripe, data_disks);
  1526. stripe2 = stripe;
  1527. /*
  1528. * Select the parity disk based on the user selected algorithm.
  1529. */
  1530. pd_idx = qd_idx = ~0;
  1531. switch(conf->level) {
  1532. case 4:
  1533. pd_idx = data_disks;
  1534. break;
  1535. case 5:
  1536. switch (algorithm) {
  1537. case ALGORITHM_LEFT_ASYMMETRIC:
  1538. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1539. if (*dd_idx >= pd_idx)
  1540. (*dd_idx)++;
  1541. break;
  1542. case ALGORITHM_RIGHT_ASYMMETRIC:
  1543. pd_idx = sector_div(stripe2, raid_disks);
  1544. if (*dd_idx >= pd_idx)
  1545. (*dd_idx)++;
  1546. break;
  1547. case ALGORITHM_LEFT_SYMMETRIC:
  1548. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1549. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1550. break;
  1551. case ALGORITHM_RIGHT_SYMMETRIC:
  1552. pd_idx = sector_div(stripe2, raid_disks);
  1553. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1554. break;
  1555. case ALGORITHM_PARITY_0:
  1556. pd_idx = 0;
  1557. (*dd_idx)++;
  1558. break;
  1559. case ALGORITHM_PARITY_N:
  1560. pd_idx = data_disks;
  1561. break;
  1562. default:
  1563. BUG();
  1564. }
  1565. break;
  1566. case 6:
  1567. switch (algorithm) {
  1568. case ALGORITHM_LEFT_ASYMMETRIC:
  1569. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1570. qd_idx = pd_idx + 1;
  1571. if (pd_idx == raid_disks-1) {
  1572. (*dd_idx)++; /* Q D D D P */
  1573. qd_idx = 0;
  1574. } else if (*dd_idx >= pd_idx)
  1575. (*dd_idx) += 2; /* D D P Q D */
  1576. break;
  1577. case ALGORITHM_RIGHT_ASYMMETRIC:
  1578. pd_idx = sector_div(stripe2, raid_disks);
  1579. qd_idx = pd_idx + 1;
  1580. if (pd_idx == raid_disks-1) {
  1581. (*dd_idx)++; /* Q D D D P */
  1582. qd_idx = 0;
  1583. } else if (*dd_idx >= pd_idx)
  1584. (*dd_idx) += 2; /* D D P Q D */
  1585. break;
  1586. case ALGORITHM_LEFT_SYMMETRIC:
  1587. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1588. qd_idx = (pd_idx + 1) % raid_disks;
  1589. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1590. break;
  1591. case ALGORITHM_RIGHT_SYMMETRIC:
  1592. pd_idx = sector_div(stripe2, raid_disks);
  1593. qd_idx = (pd_idx + 1) % raid_disks;
  1594. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1595. break;
  1596. case ALGORITHM_PARITY_0:
  1597. pd_idx = 0;
  1598. qd_idx = 1;
  1599. (*dd_idx) += 2;
  1600. break;
  1601. case ALGORITHM_PARITY_N:
  1602. pd_idx = data_disks;
  1603. qd_idx = data_disks + 1;
  1604. break;
  1605. case ALGORITHM_ROTATING_ZERO_RESTART:
  1606. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1607. * of blocks for computing Q is different.
  1608. */
  1609. pd_idx = sector_div(stripe2, raid_disks);
  1610. qd_idx = pd_idx + 1;
  1611. if (pd_idx == raid_disks-1) {
  1612. (*dd_idx)++; /* Q D D D P */
  1613. qd_idx = 0;
  1614. } else if (*dd_idx >= pd_idx)
  1615. (*dd_idx) += 2; /* D D P Q D */
  1616. ddf_layout = 1;
  1617. break;
  1618. case ALGORITHM_ROTATING_N_RESTART:
  1619. /* Same a left_asymmetric, by first stripe is
  1620. * D D D P Q rather than
  1621. * Q D D D P
  1622. */
  1623. stripe2 += 1;
  1624. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1625. qd_idx = pd_idx + 1;
  1626. if (pd_idx == raid_disks-1) {
  1627. (*dd_idx)++; /* Q D D D P */
  1628. qd_idx = 0;
  1629. } else if (*dd_idx >= pd_idx)
  1630. (*dd_idx) += 2; /* D D P Q D */
  1631. ddf_layout = 1;
  1632. break;
  1633. case ALGORITHM_ROTATING_N_CONTINUE:
  1634. /* Same as left_symmetric but Q is before P */
  1635. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1636. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1637. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1638. ddf_layout = 1;
  1639. break;
  1640. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1641. /* RAID5 left_asymmetric, with Q on last device */
  1642. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1643. if (*dd_idx >= pd_idx)
  1644. (*dd_idx)++;
  1645. qd_idx = raid_disks - 1;
  1646. break;
  1647. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1648. pd_idx = sector_div(stripe2, raid_disks-1);
  1649. if (*dd_idx >= pd_idx)
  1650. (*dd_idx)++;
  1651. qd_idx = raid_disks - 1;
  1652. break;
  1653. case ALGORITHM_LEFT_SYMMETRIC_6:
  1654. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1655. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1656. qd_idx = raid_disks - 1;
  1657. break;
  1658. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1659. pd_idx = sector_div(stripe2, raid_disks-1);
  1660. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1661. qd_idx = raid_disks - 1;
  1662. break;
  1663. case ALGORITHM_PARITY_0_6:
  1664. pd_idx = 0;
  1665. (*dd_idx)++;
  1666. qd_idx = raid_disks - 1;
  1667. break;
  1668. default:
  1669. BUG();
  1670. }
  1671. break;
  1672. }
  1673. if (sh) {
  1674. sh->pd_idx = pd_idx;
  1675. sh->qd_idx = qd_idx;
  1676. sh->ddf_layout = ddf_layout;
  1677. }
  1678. /*
  1679. * Finally, compute the new sector number
  1680. */
  1681. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1682. return new_sector;
  1683. }
  1684. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1685. {
  1686. raid5_conf_t *conf = sh->raid_conf;
  1687. int raid_disks = sh->disks;
  1688. int data_disks = raid_disks - conf->max_degraded;
  1689. sector_t new_sector = sh->sector, check;
  1690. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1691. : conf->chunk_sectors;
  1692. int algorithm = previous ? conf->prev_algo
  1693. : conf->algorithm;
  1694. sector_t stripe;
  1695. int chunk_offset;
  1696. sector_t chunk_number;
  1697. int dummy1, dd_idx = i;
  1698. sector_t r_sector;
  1699. struct stripe_head sh2;
  1700. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1701. stripe = new_sector;
  1702. if (i == sh->pd_idx)
  1703. return 0;
  1704. switch(conf->level) {
  1705. case 4: break;
  1706. case 5:
  1707. switch (algorithm) {
  1708. case ALGORITHM_LEFT_ASYMMETRIC:
  1709. case ALGORITHM_RIGHT_ASYMMETRIC:
  1710. if (i > sh->pd_idx)
  1711. i--;
  1712. break;
  1713. case ALGORITHM_LEFT_SYMMETRIC:
  1714. case ALGORITHM_RIGHT_SYMMETRIC:
  1715. if (i < sh->pd_idx)
  1716. i += raid_disks;
  1717. i -= (sh->pd_idx + 1);
  1718. break;
  1719. case ALGORITHM_PARITY_0:
  1720. i -= 1;
  1721. break;
  1722. case ALGORITHM_PARITY_N:
  1723. break;
  1724. default:
  1725. BUG();
  1726. }
  1727. break;
  1728. case 6:
  1729. if (i == sh->qd_idx)
  1730. return 0; /* It is the Q disk */
  1731. switch (algorithm) {
  1732. case ALGORITHM_LEFT_ASYMMETRIC:
  1733. case ALGORITHM_RIGHT_ASYMMETRIC:
  1734. case ALGORITHM_ROTATING_ZERO_RESTART:
  1735. case ALGORITHM_ROTATING_N_RESTART:
  1736. if (sh->pd_idx == raid_disks-1)
  1737. i--; /* Q D D D P */
  1738. else if (i > sh->pd_idx)
  1739. i -= 2; /* D D P Q D */
  1740. break;
  1741. case ALGORITHM_LEFT_SYMMETRIC:
  1742. case ALGORITHM_RIGHT_SYMMETRIC:
  1743. if (sh->pd_idx == raid_disks-1)
  1744. i--; /* Q D D D P */
  1745. else {
  1746. /* D D P Q D */
  1747. if (i < sh->pd_idx)
  1748. i += raid_disks;
  1749. i -= (sh->pd_idx + 2);
  1750. }
  1751. break;
  1752. case ALGORITHM_PARITY_0:
  1753. i -= 2;
  1754. break;
  1755. case ALGORITHM_PARITY_N:
  1756. break;
  1757. case ALGORITHM_ROTATING_N_CONTINUE:
  1758. /* Like left_symmetric, but P is before Q */
  1759. if (sh->pd_idx == 0)
  1760. i--; /* P D D D Q */
  1761. else {
  1762. /* D D Q P D */
  1763. if (i < sh->pd_idx)
  1764. i += raid_disks;
  1765. i -= (sh->pd_idx + 1);
  1766. }
  1767. break;
  1768. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1769. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1770. if (i > sh->pd_idx)
  1771. i--;
  1772. break;
  1773. case ALGORITHM_LEFT_SYMMETRIC_6:
  1774. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1775. if (i < sh->pd_idx)
  1776. i += data_disks + 1;
  1777. i -= (sh->pd_idx + 1);
  1778. break;
  1779. case ALGORITHM_PARITY_0_6:
  1780. i -= 1;
  1781. break;
  1782. default:
  1783. BUG();
  1784. }
  1785. break;
  1786. }
  1787. chunk_number = stripe * data_disks + i;
  1788. r_sector = chunk_number * sectors_per_chunk + chunk_offset;
  1789. check = raid5_compute_sector(conf, r_sector,
  1790. previous, &dummy1, &sh2);
  1791. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1792. || sh2.qd_idx != sh->qd_idx) {
  1793. printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
  1794. mdname(conf->mddev));
  1795. return 0;
  1796. }
  1797. return r_sector;
  1798. }
  1799. static void
  1800. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  1801. int rcw, int expand)
  1802. {
  1803. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1804. raid5_conf_t *conf = sh->raid_conf;
  1805. int level = conf->level;
  1806. if (rcw) {
  1807. /* if we are not expanding this is a proper write request, and
  1808. * there will be bios with new data to be drained into the
  1809. * stripe cache
  1810. */
  1811. if (!expand) {
  1812. sh->reconstruct_state = reconstruct_state_drain_run;
  1813. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1814. } else
  1815. sh->reconstruct_state = reconstruct_state_run;
  1816. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1817. for (i = disks; i--; ) {
  1818. struct r5dev *dev = &sh->dev[i];
  1819. if (dev->towrite) {
  1820. set_bit(R5_LOCKED, &dev->flags);
  1821. set_bit(R5_Wantdrain, &dev->flags);
  1822. if (!expand)
  1823. clear_bit(R5_UPTODATE, &dev->flags);
  1824. s->locked++;
  1825. }
  1826. }
  1827. if (s->locked + conf->max_degraded == disks)
  1828. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1829. atomic_inc(&conf->pending_full_writes);
  1830. } else {
  1831. BUG_ON(level == 6);
  1832. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1833. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1834. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1835. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1836. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1837. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1838. for (i = disks; i--; ) {
  1839. struct r5dev *dev = &sh->dev[i];
  1840. if (i == pd_idx)
  1841. continue;
  1842. if (dev->towrite &&
  1843. (test_bit(R5_UPTODATE, &dev->flags) ||
  1844. test_bit(R5_Wantcompute, &dev->flags))) {
  1845. set_bit(R5_Wantdrain, &dev->flags);
  1846. set_bit(R5_LOCKED, &dev->flags);
  1847. clear_bit(R5_UPTODATE, &dev->flags);
  1848. s->locked++;
  1849. }
  1850. }
  1851. }
  1852. /* keep the parity disk(s) locked while asynchronous operations
  1853. * are in flight
  1854. */
  1855. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1856. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1857. s->locked++;
  1858. if (level == 6) {
  1859. int qd_idx = sh->qd_idx;
  1860. struct r5dev *dev = &sh->dev[qd_idx];
  1861. set_bit(R5_LOCKED, &dev->flags);
  1862. clear_bit(R5_UPTODATE, &dev->flags);
  1863. s->locked++;
  1864. }
  1865. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1866. __func__, (unsigned long long)sh->sector,
  1867. s->locked, s->ops_request);
  1868. }
  1869. /*
  1870. * Each stripe/dev can have one or more bion attached.
  1871. * toread/towrite point to the first in a chain.
  1872. * The bi_next chain must be in order.
  1873. */
  1874. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1875. {
  1876. struct bio **bip;
  1877. raid5_conf_t *conf = sh->raid_conf;
  1878. int firstwrite=0;
  1879. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1880. (unsigned long long)bi->bi_sector,
  1881. (unsigned long long)sh->sector);
  1882. spin_lock(&sh->lock);
  1883. spin_lock_irq(&conf->device_lock);
  1884. if (forwrite) {
  1885. bip = &sh->dev[dd_idx].towrite;
  1886. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1887. firstwrite = 1;
  1888. } else
  1889. bip = &sh->dev[dd_idx].toread;
  1890. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1891. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1892. goto overlap;
  1893. bip = & (*bip)->bi_next;
  1894. }
  1895. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1896. goto overlap;
  1897. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1898. if (*bip)
  1899. bi->bi_next = *bip;
  1900. *bip = bi;
  1901. bi->bi_phys_segments++;
  1902. spin_unlock_irq(&conf->device_lock);
  1903. spin_unlock(&sh->lock);
  1904. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1905. (unsigned long long)bi->bi_sector,
  1906. (unsigned long long)sh->sector, dd_idx);
  1907. if (conf->mddev->bitmap && firstwrite) {
  1908. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1909. STRIPE_SECTORS, 0);
  1910. sh->bm_seq = conf->seq_flush+1;
  1911. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1912. }
  1913. if (forwrite) {
  1914. /* check if page is covered */
  1915. sector_t sector = sh->dev[dd_idx].sector;
  1916. for (bi=sh->dev[dd_idx].towrite;
  1917. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1918. bi && bi->bi_sector <= sector;
  1919. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1920. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1921. sector = bi->bi_sector + (bi->bi_size>>9);
  1922. }
  1923. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1924. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1925. }
  1926. return 1;
  1927. overlap:
  1928. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1929. spin_unlock_irq(&conf->device_lock);
  1930. spin_unlock(&sh->lock);
  1931. return 0;
  1932. }
  1933. static void end_reshape(raid5_conf_t *conf);
  1934. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  1935. struct stripe_head *sh)
  1936. {
  1937. int sectors_per_chunk =
  1938. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  1939. int dd_idx;
  1940. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1941. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  1942. raid5_compute_sector(conf,
  1943. stripe * (disks - conf->max_degraded)
  1944. *sectors_per_chunk + chunk_offset,
  1945. previous,
  1946. &dd_idx, sh);
  1947. }
  1948. static void
  1949. handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
  1950. struct stripe_head_state *s, int disks,
  1951. struct bio **return_bi)
  1952. {
  1953. int i;
  1954. for (i = disks; i--; ) {
  1955. struct bio *bi;
  1956. int bitmap_end = 0;
  1957. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1958. mdk_rdev_t *rdev;
  1959. rcu_read_lock();
  1960. rdev = rcu_dereference(conf->disks[i].rdev);
  1961. if (rdev && test_bit(In_sync, &rdev->flags))
  1962. /* multiple read failures in one stripe */
  1963. md_error(conf->mddev, rdev);
  1964. rcu_read_unlock();
  1965. }
  1966. spin_lock_irq(&conf->device_lock);
  1967. /* fail all writes first */
  1968. bi = sh->dev[i].towrite;
  1969. sh->dev[i].towrite = NULL;
  1970. if (bi) {
  1971. s->to_write--;
  1972. bitmap_end = 1;
  1973. }
  1974. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1975. wake_up(&conf->wait_for_overlap);
  1976. while (bi && bi->bi_sector <
  1977. sh->dev[i].sector + STRIPE_SECTORS) {
  1978. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1979. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1980. if (!raid5_dec_bi_phys_segments(bi)) {
  1981. md_write_end(conf->mddev);
  1982. bi->bi_next = *return_bi;
  1983. *return_bi = bi;
  1984. }
  1985. bi = nextbi;
  1986. }
  1987. /* and fail all 'written' */
  1988. bi = sh->dev[i].written;
  1989. sh->dev[i].written = NULL;
  1990. if (bi) bitmap_end = 1;
  1991. while (bi && bi->bi_sector <
  1992. sh->dev[i].sector + STRIPE_SECTORS) {
  1993. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1994. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1995. if (!raid5_dec_bi_phys_segments(bi)) {
  1996. md_write_end(conf->mddev);
  1997. bi->bi_next = *return_bi;
  1998. *return_bi = bi;
  1999. }
  2000. bi = bi2;
  2001. }
  2002. /* fail any reads if this device is non-operational and
  2003. * the data has not reached the cache yet.
  2004. */
  2005. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  2006. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  2007. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  2008. bi = sh->dev[i].toread;
  2009. sh->dev[i].toread = NULL;
  2010. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2011. wake_up(&conf->wait_for_overlap);
  2012. if (bi) s->to_read--;
  2013. while (bi && bi->bi_sector <
  2014. sh->dev[i].sector + STRIPE_SECTORS) {
  2015. struct bio *nextbi =
  2016. r5_next_bio(bi, sh->dev[i].sector);
  2017. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2018. if (!raid5_dec_bi_phys_segments(bi)) {
  2019. bi->bi_next = *return_bi;
  2020. *return_bi = bi;
  2021. }
  2022. bi = nextbi;
  2023. }
  2024. }
  2025. spin_unlock_irq(&conf->device_lock);
  2026. if (bitmap_end)
  2027. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2028. STRIPE_SECTORS, 0, 0);
  2029. }
  2030. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2031. if (atomic_dec_and_test(&conf->pending_full_writes))
  2032. md_wakeup_thread(conf->mddev->thread);
  2033. }
  2034. /* fetch_block5 - checks the given member device to see if its data needs
  2035. * to be read or computed to satisfy a request.
  2036. *
  2037. * Returns 1 when no more member devices need to be checked, otherwise returns
  2038. * 0 to tell the loop in handle_stripe_fill5 to continue
  2039. */
  2040. static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
  2041. int disk_idx, int disks)
  2042. {
  2043. struct r5dev *dev = &sh->dev[disk_idx];
  2044. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  2045. /* is the data in this block needed, and can we get it? */
  2046. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2047. !test_bit(R5_UPTODATE, &dev->flags) &&
  2048. (dev->toread ||
  2049. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2050. s->syncing || s->expanding ||
  2051. (s->failed &&
  2052. (failed_dev->toread ||
  2053. (failed_dev->towrite &&
  2054. !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
  2055. /* We would like to get this block, possibly by computing it,
  2056. * otherwise read it if the backing disk is insync
  2057. */
  2058. if ((s->uptodate == disks - 1) &&
  2059. (s->failed && disk_idx == s->failed_num)) {
  2060. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2061. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2062. set_bit(R5_Wantcompute, &dev->flags);
  2063. sh->ops.target = disk_idx;
  2064. sh->ops.target2 = -1;
  2065. s->req_compute = 1;
  2066. /* Careful: from this point on 'uptodate' is in the eye
  2067. * of raid_run_ops which services 'compute' operations
  2068. * before writes. R5_Wantcompute flags a block that will
  2069. * be R5_UPTODATE by the time it is needed for a
  2070. * subsequent operation.
  2071. */
  2072. s->uptodate++;
  2073. return 1; /* uptodate + compute == disks */
  2074. } else if (test_bit(R5_Insync, &dev->flags)) {
  2075. set_bit(R5_LOCKED, &dev->flags);
  2076. set_bit(R5_Wantread, &dev->flags);
  2077. s->locked++;
  2078. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  2079. s->syncing);
  2080. }
  2081. }
  2082. return 0;
  2083. }
  2084. /**
  2085. * handle_stripe_fill5 - read or compute data to satisfy pending requests.
  2086. */
  2087. static void handle_stripe_fill5(struct stripe_head *sh,
  2088. struct stripe_head_state *s, int disks)
  2089. {
  2090. int i;
  2091. /* look for blocks to read/compute, skip this if a compute
  2092. * is already in flight, or if the stripe contents are in the
  2093. * midst of changing due to a write
  2094. */
  2095. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2096. !sh->reconstruct_state)
  2097. for (i = disks; i--; )
  2098. if (fetch_block5(sh, s, i, disks))
  2099. break;
  2100. set_bit(STRIPE_HANDLE, &sh->state);
  2101. }
  2102. /* fetch_block6 - checks the given member device to see if its data needs
  2103. * to be read or computed to satisfy a request.
  2104. *
  2105. * Returns 1 when no more member devices need to be checked, otherwise returns
  2106. * 0 to tell the loop in handle_stripe_fill6 to continue
  2107. */
  2108. static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
  2109. struct r6_state *r6s, int disk_idx, int disks)
  2110. {
  2111. struct r5dev *dev = &sh->dev[disk_idx];
  2112. struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
  2113. &sh->dev[r6s->failed_num[1]] };
  2114. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2115. !test_bit(R5_UPTODATE, &dev->flags) &&
  2116. (dev->toread ||
  2117. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2118. s->syncing || s->expanding ||
  2119. (s->failed >= 1 &&
  2120. (fdev[0]->toread || s->to_write)) ||
  2121. (s->failed >= 2 &&
  2122. (fdev[1]->toread || s->to_write)))) {
  2123. /* we would like to get this block, possibly by computing it,
  2124. * otherwise read it if the backing disk is insync
  2125. */
  2126. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  2127. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  2128. if ((s->uptodate == disks - 1) &&
  2129. (s->failed && (disk_idx == r6s->failed_num[0] ||
  2130. disk_idx == r6s->failed_num[1]))) {
  2131. /* have disk failed, and we're requested to fetch it;
  2132. * do compute it
  2133. */
  2134. pr_debug("Computing stripe %llu block %d\n",
  2135. (unsigned long long)sh->sector, disk_idx);
  2136. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2137. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2138. set_bit(R5_Wantcompute, &dev->flags);
  2139. sh->ops.target = disk_idx;
  2140. sh->ops.target2 = -1; /* no 2nd target */
  2141. s->req_compute = 1;
  2142. s->uptodate++;
  2143. return 1;
  2144. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  2145. /* Computing 2-failure is *very* expensive; only
  2146. * do it if failed >= 2
  2147. */
  2148. int other;
  2149. for (other = disks; other--; ) {
  2150. if (other == disk_idx)
  2151. continue;
  2152. if (!test_bit(R5_UPTODATE,
  2153. &sh->dev[other].flags))
  2154. break;
  2155. }
  2156. BUG_ON(other < 0);
  2157. pr_debug("Computing stripe %llu blocks %d,%d\n",
  2158. (unsigned long long)sh->sector,
  2159. disk_idx, other);
  2160. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2161. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2162. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  2163. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  2164. sh->ops.target = disk_idx;
  2165. sh->ops.target2 = other;
  2166. s->uptodate += 2;
  2167. s->req_compute = 1;
  2168. return 1;
  2169. } else if (test_bit(R5_Insync, &dev->flags)) {
  2170. set_bit(R5_LOCKED, &dev->flags);
  2171. set_bit(R5_Wantread, &dev->flags);
  2172. s->locked++;
  2173. pr_debug("Reading block %d (sync=%d)\n",
  2174. disk_idx, s->syncing);
  2175. }
  2176. }
  2177. return 0;
  2178. }
  2179. /**
  2180. * handle_stripe_fill6 - read or compute data to satisfy pending requests.
  2181. */
  2182. static void handle_stripe_fill6(struct stripe_head *sh,
  2183. struct stripe_head_state *s, struct r6_state *r6s,
  2184. int disks)
  2185. {
  2186. int i;
  2187. /* look for blocks to read/compute, skip this if a compute
  2188. * is already in flight, or if the stripe contents are in the
  2189. * midst of changing due to a write
  2190. */
  2191. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2192. !sh->reconstruct_state)
  2193. for (i = disks; i--; )
  2194. if (fetch_block6(sh, s, r6s, i, disks))
  2195. break;
  2196. set_bit(STRIPE_HANDLE, &sh->state);
  2197. }
  2198. /* handle_stripe_clean_event
  2199. * any written block on an uptodate or failed drive can be returned.
  2200. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  2201. * never LOCKED, so we don't need to test 'failed' directly.
  2202. */
  2203. static void handle_stripe_clean_event(raid5_conf_t *conf,
  2204. struct stripe_head *sh, int disks, struct bio **return_bi)
  2205. {
  2206. int i;
  2207. struct r5dev *dev;
  2208. for (i = disks; i--; )
  2209. if (sh->dev[i].written) {
  2210. dev = &sh->dev[i];
  2211. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2212. test_bit(R5_UPTODATE, &dev->flags)) {
  2213. /* We can return any write requests */
  2214. struct bio *wbi, *wbi2;
  2215. int bitmap_end = 0;
  2216. pr_debug("Return write for disc %d\n", i);
  2217. spin_lock_irq(&conf->device_lock);
  2218. wbi = dev->written;
  2219. dev->written = NULL;
  2220. while (wbi && wbi->bi_sector <
  2221. dev->sector + STRIPE_SECTORS) {
  2222. wbi2 = r5_next_bio(wbi, dev->sector);
  2223. if (!raid5_dec_bi_phys_segments(wbi)) {
  2224. md_write_end(conf->mddev);
  2225. wbi->bi_next = *return_bi;
  2226. *return_bi = wbi;
  2227. }
  2228. wbi = wbi2;
  2229. }
  2230. if (dev->towrite == NULL)
  2231. bitmap_end = 1;
  2232. spin_unlock_irq(&conf->device_lock);
  2233. if (bitmap_end)
  2234. bitmap_endwrite(conf->mddev->bitmap,
  2235. sh->sector,
  2236. STRIPE_SECTORS,
  2237. !test_bit(STRIPE_DEGRADED, &sh->state),
  2238. 0);
  2239. }
  2240. }
  2241. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2242. if (atomic_dec_and_test(&conf->pending_full_writes))
  2243. md_wakeup_thread(conf->mddev->thread);
  2244. }
  2245. static void handle_stripe_dirtying5(raid5_conf_t *conf,
  2246. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  2247. {
  2248. int rmw = 0, rcw = 0, i;
  2249. for (i = disks; i--; ) {
  2250. /* would I have to read this buffer for read_modify_write */
  2251. struct r5dev *dev = &sh->dev[i];
  2252. if ((dev->towrite || i == sh->pd_idx) &&
  2253. !test_bit(R5_LOCKED, &dev->flags) &&
  2254. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2255. test_bit(R5_Wantcompute, &dev->flags))) {
  2256. if (test_bit(R5_Insync, &dev->flags))
  2257. rmw++;
  2258. else
  2259. rmw += 2*disks; /* cannot read it */
  2260. }
  2261. /* Would I have to read this buffer for reconstruct_write */
  2262. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2263. !test_bit(R5_LOCKED, &dev->flags) &&
  2264. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2265. test_bit(R5_Wantcompute, &dev->flags))) {
  2266. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2267. else
  2268. rcw += 2*disks;
  2269. }
  2270. }
  2271. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2272. (unsigned long long)sh->sector, rmw, rcw);
  2273. set_bit(STRIPE_HANDLE, &sh->state);
  2274. if (rmw < rcw && rmw > 0)
  2275. /* prefer read-modify-write, but need to get some data */
  2276. for (i = disks; i--; ) {
  2277. struct r5dev *dev = &sh->dev[i];
  2278. if ((dev->towrite || i == sh->pd_idx) &&
  2279. !test_bit(R5_LOCKED, &dev->flags) &&
  2280. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2281. test_bit(R5_Wantcompute, &dev->flags)) &&
  2282. test_bit(R5_Insync, &dev->flags)) {
  2283. if (
  2284. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2285. pr_debug("Read_old block "
  2286. "%d for r-m-w\n", i);
  2287. set_bit(R5_LOCKED, &dev->flags);
  2288. set_bit(R5_Wantread, &dev->flags);
  2289. s->locked++;
  2290. } else {
  2291. set_bit(STRIPE_DELAYED, &sh->state);
  2292. set_bit(STRIPE_HANDLE, &sh->state);
  2293. }
  2294. }
  2295. }
  2296. if (rcw <= rmw && rcw > 0)
  2297. /* want reconstruct write, but need to get some data */
  2298. for (i = disks; i--; ) {
  2299. struct r5dev *dev = &sh->dev[i];
  2300. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2301. i != sh->pd_idx &&
  2302. !test_bit(R5_LOCKED, &dev->flags) &&
  2303. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2304. test_bit(R5_Wantcompute, &dev->flags)) &&
  2305. test_bit(R5_Insync, &dev->flags)) {
  2306. if (
  2307. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2308. pr_debug("Read_old block "
  2309. "%d for Reconstruct\n", i);
  2310. set_bit(R5_LOCKED, &dev->flags);
  2311. set_bit(R5_Wantread, &dev->flags);
  2312. s->locked++;
  2313. } else {
  2314. set_bit(STRIPE_DELAYED, &sh->state);
  2315. set_bit(STRIPE_HANDLE, &sh->state);
  2316. }
  2317. }
  2318. }
  2319. /* now if nothing is locked, and if we have enough data,
  2320. * we can start a write request
  2321. */
  2322. /* since handle_stripe can be called at any time we need to handle the
  2323. * case where a compute block operation has been submitted and then a
  2324. * subsequent call wants to start a write request. raid_run_ops only
  2325. * handles the case where compute block and reconstruct are requested
  2326. * simultaneously. If this is not the case then new writes need to be
  2327. * held off until the compute completes.
  2328. */
  2329. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2330. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2331. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2332. schedule_reconstruction(sh, s, rcw == 0, 0);
  2333. }
  2334. static void handle_stripe_dirtying6(raid5_conf_t *conf,
  2335. struct stripe_head *sh, struct stripe_head_state *s,
  2336. struct r6_state *r6s, int disks)
  2337. {
  2338. int rcw = 0, pd_idx = sh->pd_idx, i;
  2339. int qd_idx = sh->qd_idx;
  2340. set_bit(STRIPE_HANDLE, &sh->state);
  2341. for (i = disks; i--; ) {
  2342. struct r5dev *dev = &sh->dev[i];
  2343. /* check if we haven't enough data */
  2344. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2345. i != pd_idx && i != qd_idx &&
  2346. !test_bit(R5_LOCKED, &dev->flags) &&
  2347. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2348. test_bit(R5_Wantcompute, &dev->flags))) {
  2349. rcw++;
  2350. if (!test_bit(R5_Insync, &dev->flags))
  2351. continue; /* it's a failed drive */
  2352. if (
  2353. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2354. pr_debug("Read_old stripe %llu "
  2355. "block %d for Reconstruct\n",
  2356. (unsigned long long)sh->sector, i);
  2357. set_bit(R5_LOCKED, &dev->flags);
  2358. set_bit(R5_Wantread, &dev->flags);
  2359. s->locked++;
  2360. } else {
  2361. pr_debug("Request delayed stripe %llu "
  2362. "block %d for Reconstruct\n",
  2363. (unsigned long long)sh->sector, i);
  2364. set_bit(STRIPE_DELAYED, &sh->state);
  2365. set_bit(STRIPE_HANDLE, &sh->state);
  2366. }
  2367. }
  2368. }
  2369. /* now if nothing is locked, and if we have enough data, we can start a
  2370. * write request
  2371. */
  2372. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2373. s->locked == 0 && rcw == 0 &&
  2374. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2375. schedule_reconstruction(sh, s, 1, 0);
  2376. }
  2377. }
  2378. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2379. struct stripe_head_state *s, int disks)
  2380. {
  2381. struct r5dev *dev = NULL;
  2382. set_bit(STRIPE_HANDLE, &sh->state);
  2383. switch (sh->check_state) {
  2384. case check_state_idle:
  2385. /* start a new check operation if there are no failures */
  2386. if (s->failed == 0) {
  2387. BUG_ON(s->uptodate != disks);
  2388. sh->check_state = check_state_run;
  2389. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2390. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2391. s->uptodate--;
  2392. break;
  2393. }
  2394. dev = &sh->dev[s->failed_num];
  2395. /* fall through */
  2396. case check_state_compute_result:
  2397. sh->check_state = check_state_idle;
  2398. if (!dev)
  2399. dev = &sh->dev[sh->pd_idx];
  2400. /* check that a write has not made the stripe insync */
  2401. if (test_bit(STRIPE_INSYNC, &sh->state))
  2402. break;
  2403. /* either failed parity check, or recovery is happening */
  2404. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2405. BUG_ON(s->uptodate != disks);
  2406. set_bit(R5_LOCKED, &dev->flags);
  2407. s->locked++;
  2408. set_bit(R5_Wantwrite, &dev->flags);
  2409. clear_bit(STRIPE_DEGRADED, &sh->state);
  2410. set_bit(STRIPE_INSYNC, &sh->state);
  2411. break;
  2412. case check_state_run:
  2413. break; /* we will be called again upon completion */
  2414. case check_state_check_result:
  2415. sh->check_state = check_state_idle;
  2416. /* if a failure occurred during the check operation, leave
  2417. * STRIPE_INSYNC not set and let the stripe be handled again
  2418. */
  2419. if (s->failed)
  2420. break;
  2421. /* handle a successful check operation, if parity is correct
  2422. * we are done. Otherwise update the mismatch count and repair
  2423. * parity if !MD_RECOVERY_CHECK
  2424. */
  2425. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  2426. /* parity is correct (on disc,
  2427. * not in buffer any more)
  2428. */
  2429. set_bit(STRIPE_INSYNC, &sh->state);
  2430. else {
  2431. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2432. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2433. /* don't try to repair!! */
  2434. set_bit(STRIPE_INSYNC, &sh->state);
  2435. else {
  2436. sh->check_state = check_state_compute_run;
  2437. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2438. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2439. set_bit(R5_Wantcompute,
  2440. &sh->dev[sh->pd_idx].flags);
  2441. sh->ops.target = sh->pd_idx;
  2442. sh->ops.target2 = -1;
  2443. s->uptodate++;
  2444. }
  2445. }
  2446. break;
  2447. case check_state_compute_run:
  2448. break;
  2449. default:
  2450. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2451. __func__, sh->check_state,
  2452. (unsigned long long) sh->sector);
  2453. BUG();
  2454. }
  2455. }
  2456. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2457. struct stripe_head_state *s,
  2458. struct r6_state *r6s, int disks)
  2459. {
  2460. int pd_idx = sh->pd_idx;
  2461. int qd_idx = sh->qd_idx;
  2462. struct r5dev *dev;
  2463. set_bit(STRIPE_HANDLE, &sh->state);
  2464. BUG_ON(s->failed > 2);
  2465. /* Want to check and possibly repair P and Q.
  2466. * However there could be one 'failed' device, in which
  2467. * case we can only check one of them, possibly using the
  2468. * other to generate missing data
  2469. */
  2470. switch (sh->check_state) {
  2471. case check_state_idle:
  2472. /* start a new check operation if there are < 2 failures */
  2473. if (s->failed == r6s->q_failed) {
  2474. /* The only possible failed device holds Q, so it
  2475. * makes sense to check P (If anything else were failed,
  2476. * we would have used P to recreate it).
  2477. */
  2478. sh->check_state = check_state_run;
  2479. }
  2480. if (!r6s->q_failed && s->failed < 2) {
  2481. /* Q is not failed, and we didn't use it to generate
  2482. * anything, so it makes sense to check it
  2483. */
  2484. if (sh->check_state == check_state_run)
  2485. sh->check_state = check_state_run_pq;
  2486. else
  2487. sh->check_state = check_state_run_q;
  2488. }
  2489. /* discard potentially stale zero_sum_result */
  2490. sh->ops.zero_sum_result = 0;
  2491. if (sh->check_state == check_state_run) {
  2492. /* async_xor_zero_sum destroys the contents of P */
  2493. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2494. s->uptodate--;
  2495. }
  2496. if (sh->check_state >= check_state_run &&
  2497. sh->check_state <= check_state_run_pq) {
  2498. /* async_syndrome_zero_sum preserves P and Q, so
  2499. * no need to mark them !uptodate here
  2500. */
  2501. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2502. break;
  2503. }
  2504. /* we have 2-disk failure */
  2505. BUG_ON(s->failed != 2);
  2506. /* fall through */
  2507. case check_state_compute_result:
  2508. sh->check_state = check_state_idle;
  2509. /* check that a write has not made the stripe insync */
  2510. if (test_bit(STRIPE_INSYNC, &sh->state))
  2511. break;
  2512. /* now write out any block on a failed drive,
  2513. * or P or Q if they were recomputed
  2514. */
  2515. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  2516. if (s->failed == 2) {
  2517. dev = &sh->dev[r6s->failed_num[1]];
  2518. s->locked++;
  2519. set_bit(R5_LOCKED, &dev->flags);
  2520. set_bit(R5_Wantwrite, &dev->flags);
  2521. }
  2522. if (s->failed >= 1) {
  2523. dev = &sh->dev[r6s->failed_num[0]];
  2524. s->locked++;
  2525. set_bit(R5_LOCKED, &dev->flags);
  2526. set_bit(R5_Wantwrite, &dev->flags);
  2527. }
  2528. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2529. dev = &sh->dev[pd_idx];
  2530. s->locked++;
  2531. set_bit(R5_LOCKED, &dev->flags);
  2532. set_bit(R5_Wantwrite, &dev->flags);
  2533. }
  2534. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2535. dev = &sh->dev[qd_idx];
  2536. s->locked++;
  2537. set_bit(R5_LOCKED, &dev->flags);
  2538. set_bit(R5_Wantwrite, &dev->flags);
  2539. }
  2540. clear_bit(STRIPE_DEGRADED, &sh->state);
  2541. set_bit(STRIPE_INSYNC, &sh->state);
  2542. break;
  2543. case check_state_run:
  2544. case check_state_run_q:
  2545. case check_state_run_pq:
  2546. break; /* we will be called again upon completion */
  2547. case check_state_check_result:
  2548. sh->check_state = check_state_idle;
  2549. /* handle a successful check operation, if parity is correct
  2550. * we are done. Otherwise update the mismatch count and repair
  2551. * parity if !MD_RECOVERY_CHECK
  2552. */
  2553. if (sh->ops.zero_sum_result == 0) {
  2554. /* both parities are correct */
  2555. if (!s->failed)
  2556. set_bit(STRIPE_INSYNC, &sh->state);
  2557. else {
  2558. /* in contrast to the raid5 case we can validate
  2559. * parity, but still have a failure to write
  2560. * back
  2561. */
  2562. sh->check_state = check_state_compute_result;
  2563. /* Returning at this point means that we may go
  2564. * off and bring p and/or q uptodate again so
  2565. * we make sure to check zero_sum_result again
  2566. * to verify if p or q need writeback
  2567. */
  2568. }
  2569. } else {
  2570. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2571. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2572. /* don't try to repair!! */
  2573. set_bit(STRIPE_INSYNC, &sh->state);
  2574. else {
  2575. int *target = &sh->ops.target;
  2576. sh->ops.target = -1;
  2577. sh->ops.target2 = -1;
  2578. sh->check_state = check_state_compute_run;
  2579. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2580. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2581. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2582. set_bit(R5_Wantcompute,
  2583. &sh->dev[pd_idx].flags);
  2584. *target = pd_idx;
  2585. target = &sh->ops.target2;
  2586. s->uptodate++;
  2587. }
  2588. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2589. set_bit(R5_Wantcompute,
  2590. &sh->dev[qd_idx].flags);
  2591. *target = qd_idx;
  2592. s->uptodate++;
  2593. }
  2594. }
  2595. }
  2596. break;
  2597. case check_state_compute_run:
  2598. break;
  2599. default:
  2600. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2601. __func__, sh->check_state,
  2602. (unsigned long long) sh->sector);
  2603. BUG();
  2604. }
  2605. }
  2606. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2607. struct r6_state *r6s)
  2608. {
  2609. int i;
  2610. /* We have read all the blocks in this stripe and now we need to
  2611. * copy some of them into a target stripe for expand.
  2612. */
  2613. struct dma_async_tx_descriptor *tx = NULL;
  2614. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2615. for (i = 0; i < sh->disks; i++)
  2616. if (i != sh->pd_idx && i != sh->qd_idx) {
  2617. int dd_idx, j;
  2618. struct stripe_head *sh2;
  2619. struct async_submit_ctl submit;
  2620. sector_t bn = compute_blocknr(sh, i, 1);
  2621. sector_t s = raid5_compute_sector(conf, bn, 0,
  2622. &dd_idx, NULL);
  2623. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2624. if (sh2 == NULL)
  2625. /* so far only the early blocks of this stripe
  2626. * have been requested. When later blocks
  2627. * get requested, we will try again
  2628. */
  2629. continue;
  2630. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2631. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2632. /* must have already done this block */
  2633. release_stripe(sh2);
  2634. continue;
  2635. }
  2636. /* place all the copies on one channel */
  2637. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  2638. tx = async_memcpy(sh2->dev[dd_idx].page,
  2639. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2640. &submit);
  2641. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2642. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2643. for (j = 0; j < conf->raid_disks; j++)
  2644. if (j != sh2->pd_idx &&
  2645. (!r6s || j != sh2->qd_idx) &&
  2646. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2647. break;
  2648. if (j == conf->raid_disks) {
  2649. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2650. set_bit(STRIPE_HANDLE, &sh2->state);
  2651. }
  2652. release_stripe(sh2);
  2653. }
  2654. /* done submitting copies, wait for them to complete */
  2655. if (tx) {
  2656. async_tx_ack(tx);
  2657. dma_wait_for_async_tx(tx);
  2658. }
  2659. }
  2660. /*
  2661. * handle_stripe - do things to a stripe.
  2662. *
  2663. * We lock the stripe and then examine the state of various bits
  2664. * to see what needs to be done.
  2665. * Possible results:
  2666. * return some read request which now have data
  2667. * return some write requests which are safely on disc
  2668. * schedule a read on some buffers
  2669. * schedule a write of some buffers
  2670. * return confirmation of parity correctness
  2671. *
  2672. * buffers are taken off read_list or write_list, and bh_cache buffers
  2673. * get BH_Lock set before the stripe lock is released.
  2674. *
  2675. */
  2676. static void handle_stripe5(struct stripe_head *sh)
  2677. {
  2678. raid5_conf_t *conf = sh->raid_conf;
  2679. int disks = sh->disks, i;
  2680. struct bio *return_bi = NULL;
  2681. struct stripe_head_state s;
  2682. struct r5dev *dev;
  2683. mdk_rdev_t *blocked_rdev = NULL;
  2684. int prexor;
  2685. int dec_preread_active = 0;
  2686. memset(&s, 0, sizeof(s));
  2687. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
  2688. "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
  2689. atomic_read(&sh->count), sh->pd_idx, sh->check_state,
  2690. sh->reconstruct_state);
  2691. spin_lock(&sh->lock);
  2692. clear_bit(STRIPE_HANDLE, &sh->state);
  2693. clear_bit(STRIPE_DELAYED, &sh->state);
  2694. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2695. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2696. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2697. /* Now to look around and see what can be done */
  2698. rcu_read_lock();
  2699. for (i=disks; i--; ) {
  2700. mdk_rdev_t *rdev;
  2701. dev = &sh->dev[i];
  2702. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2703. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2704. dev->towrite, dev->written);
  2705. /* maybe we can request a biofill operation
  2706. *
  2707. * new wantfill requests are only permitted while
  2708. * ops_complete_biofill is guaranteed to be inactive
  2709. */
  2710. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2711. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2712. set_bit(R5_Wantfill, &dev->flags);
  2713. /* now count some things */
  2714. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2715. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2716. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2717. if (test_bit(R5_Wantfill, &dev->flags))
  2718. s.to_fill++;
  2719. else if (dev->toread)
  2720. s.to_read++;
  2721. if (dev->towrite) {
  2722. s.to_write++;
  2723. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2724. s.non_overwrite++;
  2725. }
  2726. if (dev->written)
  2727. s.written++;
  2728. rdev = rcu_dereference(conf->disks[i].rdev);
  2729. if (blocked_rdev == NULL &&
  2730. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2731. blocked_rdev = rdev;
  2732. atomic_inc(&rdev->nr_pending);
  2733. }
  2734. clear_bit(R5_Insync, &dev->flags);
  2735. if (!rdev)
  2736. /* Not in-sync */;
  2737. else if (test_bit(In_sync, &rdev->flags))
  2738. set_bit(R5_Insync, &dev->flags);
  2739. else {
  2740. /* could be in-sync depending on recovery/reshape status */
  2741. if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  2742. set_bit(R5_Insync, &dev->flags);
  2743. }
  2744. if (!test_bit(R5_Insync, &dev->flags)) {
  2745. /* The ReadError flag will just be confusing now */
  2746. clear_bit(R5_ReadError, &dev->flags);
  2747. clear_bit(R5_ReWrite, &dev->flags);
  2748. }
  2749. if (test_bit(R5_ReadError, &dev->flags))
  2750. clear_bit(R5_Insync, &dev->flags);
  2751. if (!test_bit(R5_Insync, &dev->flags)) {
  2752. s.failed++;
  2753. s.failed_num = i;
  2754. }
  2755. }
  2756. rcu_read_unlock();
  2757. if (unlikely(blocked_rdev)) {
  2758. if (s.syncing || s.expanding || s.expanded ||
  2759. s.to_write || s.written) {
  2760. set_bit(STRIPE_HANDLE, &sh->state);
  2761. goto unlock;
  2762. }
  2763. /* There is nothing for the blocked_rdev to block */
  2764. rdev_dec_pending(blocked_rdev, conf->mddev);
  2765. blocked_rdev = NULL;
  2766. }
  2767. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2768. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2769. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2770. }
  2771. pr_debug("locked=%d uptodate=%d to_read=%d"
  2772. " to_write=%d failed=%d failed_num=%d\n",
  2773. s.locked, s.uptodate, s.to_read, s.to_write,
  2774. s.failed, s.failed_num);
  2775. /* check if the array has lost two devices and, if so, some requests might
  2776. * need to be failed
  2777. */
  2778. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  2779. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2780. if (s.failed > 1 && s.syncing) {
  2781. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2782. clear_bit(STRIPE_SYNCING, &sh->state);
  2783. s.syncing = 0;
  2784. }
  2785. /* might be able to return some write requests if the parity block
  2786. * is safe, or on a failed drive
  2787. */
  2788. dev = &sh->dev[sh->pd_idx];
  2789. if ( s.written &&
  2790. ((test_bit(R5_Insync, &dev->flags) &&
  2791. !test_bit(R5_LOCKED, &dev->flags) &&
  2792. test_bit(R5_UPTODATE, &dev->flags)) ||
  2793. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2794. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2795. /* Now we might consider reading some blocks, either to check/generate
  2796. * parity, or to satisfy requests
  2797. * or to load a block that is being partially written.
  2798. */
  2799. if (s.to_read || s.non_overwrite ||
  2800. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2801. handle_stripe_fill5(sh, &s, disks);
  2802. /* Now we check to see if any write operations have recently
  2803. * completed
  2804. */
  2805. prexor = 0;
  2806. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  2807. prexor = 1;
  2808. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  2809. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  2810. sh->reconstruct_state = reconstruct_state_idle;
  2811. /* All the 'written' buffers and the parity block are ready to
  2812. * be written back to disk
  2813. */
  2814. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2815. for (i = disks; i--; ) {
  2816. dev = &sh->dev[i];
  2817. if (test_bit(R5_LOCKED, &dev->flags) &&
  2818. (i == sh->pd_idx || dev->written)) {
  2819. pr_debug("Writing block %d\n", i);
  2820. set_bit(R5_Wantwrite, &dev->flags);
  2821. if (prexor)
  2822. continue;
  2823. if (!test_bit(R5_Insync, &dev->flags) ||
  2824. (i == sh->pd_idx && s.failed == 0))
  2825. set_bit(STRIPE_INSYNC, &sh->state);
  2826. }
  2827. }
  2828. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2829. dec_preread_active = 1;
  2830. }
  2831. /* Now to consider new write requests and what else, if anything
  2832. * should be read. We do not handle new writes when:
  2833. * 1/ A 'write' operation (copy+xor) is already in flight.
  2834. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2835. * block.
  2836. */
  2837. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  2838. handle_stripe_dirtying5(conf, sh, &s, disks);
  2839. /* maybe we need to check and possibly fix the parity for this stripe
  2840. * Any reads will already have been scheduled, so we just see if enough
  2841. * data is available. The parity check is held off while parity
  2842. * dependent operations are in flight.
  2843. */
  2844. if (sh->check_state ||
  2845. (s.syncing && s.locked == 0 &&
  2846. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  2847. !test_bit(STRIPE_INSYNC, &sh->state)))
  2848. handle_parity_checks5(conf, sh, &s, disks);
  2849. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2850. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2851. clear_bit(STRIPE_SYNCING, &sh->state);
  2852. }
  2853. /* If the failed drive is just a ReadError, then we might need to progress
  2854. * the repair/check process
  2855. */
  2856. if (s.failed == 1 && !conf->mddev->ro &&
  2857. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2858. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2859. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2860. ) {
  2861. dev = &sh->dev[s.failed_num];
  2862. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2863. set_bit(R5_Wantwrite, &dev->flags);
  2864. set_bit(R5_ReWrite, &dev->flags);
  2865. set_bit(R5_LOCKED, &dev->flags);
  2866. s.locked++;
  2867. } else {
  2868. /* let's read it back */
  2869. set_bit(R5_Wantread, &dev->flags);
  2870. set_bit(R5_LOCKED, &dev->flags);
  2871. s.locked++;
  2872. }
  2873. }
  2874. /* Finish reconstruct operations initiated by the expansion process */
  2875. if (sh->reconstruct_state == reconstruct_state_result) {
  2876. struct stripe_head *sh2
  2877. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  2878. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2879. /* sh cannot be written until sh2 has been read.
  2880. * so arrange for sh to be delayed a little
  2881. */
  2882. set_bit(STRIPE_DELAYED, &sh->state);
  2883. set_bit(STRIPE_HANDLE, &sh->state);
  2884. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2885. &sh2->state))
  2886. atomic_inc(&conf->preread_active_stripes);
  2887. release_stripe(sh2);
  2888. goto unlock;
  2889. }
  2890. if (sh2)
  2891. release_stripe(sh2);
  2892. sh->reconstruct_state = reconstruct_state_idle;
  2893. clear_bit(STRIPE_EXPANDING, &sh->state);
  2894. for (i = conf->raid_disks; i--; ) {
  2895. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2896. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2897. s.locked++;
  2898. }
  2899. }
  2900. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2901. !sh->reconstruct_state) {
  2902. /* Need to write out all blocks after computing parity */
  2903. sh->disks = conf->raid_disks;
  2904. stripe_set_idx(sh->sector, conf, 0, sh);
  2905. schedule_reconstruction(sh, &s, 1, 1);
  2906. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  2907. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2908. atomic_dec(&conf->reshape_stripes);
  2909. wake_up(&conf->wait_for_overlap);
  2910. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2911. }
  2912. if (s.expanding && s.locked == 0 &&
  2913. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2914. handle_stripe_expansion(conf, sh, NULL);
  2915. unlock:
  2916. spin_unlock(&sh->lock);
  2917. /* wait for this device to become unblocked */
  2918. if (unlikely(blocked_rdev))
  2919. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2920. if (s.ops_request)
  2921. raid_run_ops(sh, s.ops_request);
  2922. ops_run_io(sh, &s);
  2923. if (dec_preread_active) {
  2924. /* We delay this until after ops_run_io so that if make_request
  2925. * is waiting on a flush, it won't continue until the writes
  2926. * have actually been submitted.
  2927. */
  2928. atomic_dec(&conf->preread_active_stripes);
  2929. if (atomic_read(&conf->preread_active_stripes) <
  2930. IO_THRESHOLD)
  2931. md_wakeup_thread(conf->mddev->thread);
  2932. }
  2933. return_io(return_bi);
  2934. }
  2935. static void handle_stripe6(struct stripe_head *sh)
  2936. {
  2937. raid5_conf_t *conf = sh->raid_conf;
  2938. int disks = sh->disks;
  2939. struct bio *return_bi = NULL;
  2940. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
  2941. struct stripe_head_state s;
  2942. struct r6_state r6s;
  2943. struct r5dev *dev, *pdev, *qdev;
  2944. mdk_rdev_t *blocked_rdev = NULL;
  2945. int dec_preread_active = 0;
  2946. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2947. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  2948. (unsigned long long)sh->sector, sh->state,
  2949. atomic_read(&sh->count), pd_idx, qd_idx,
  2950. sh->check_state, sh->reconstruct_state);
  2951. memset(&s, 0, sizeof(s));
  2952. spin_lock(&sh->lock);
  2953. clear_bit(STRIPE_HANDLE, &sh->state);
  2954. clear_bit(STRIPE_DELAYED, &sh->state);
  2955. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2956. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2957. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2958. /* Now to look around and see what can be done */
  2959. rcu_read_lock();
  2960. for (i=disks; i--; ) {
  2961. mdk_rdev_t *rdev;
  2962. dev = &sh->dev[i];
  2963. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2964. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2965. /* maybe we can reply to a read
  2966. *
  2967. * new wantfill requests are only permitted while
  2968. * ops_complete_biofill is guaranteed to be inactive
  2969. */
  2970. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2971. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2972. set_bit(R5_Wantfill, &dev->flags);
  2973. /* now count some things */
  2974. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2975. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2976. if (test_bit(R5_Wantcompute, &dev->flags)) {
  2977. s.compute++;
  2978. BUG_ON(s.compute > 2);
  2979. }
  2980. if (test_bit(R5_Wantfill, &dev->flags)) {
  2981. s.to_fill++;
  2982. } else if (dev->toread)
  2983. s.to_read++;
  2984. if (dev->towrite) {
  2985. s.to_write++;
  2986. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2987. s.non_overwrite++;
  2988. }
  2989. if (dev->written)
  2990. s.written++;
  2991. rdev = rcu_dereference(conf->disks[i].rdev);
  2992. if (blocked_rdev == NULL &&
  2993. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2994. blocked_rdev = rdev;
  2995. atomic_inc(&rdev->nr_pending);
  2996. }
  2997. clear_bit(R5_Insync, &dev->flags);
  2998. if (!rdev)
  2999. /* Not in-sync */;
  3000. else if (test_bit(In_sync, &rdev->flags))
  3001. set_bit(R5_Insync, &dev->flags);
  3002. else {
  3003. /* in sync if before recovery_offset */
  3004. if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  3005. set_bit(R5_Insync, &dev->flags);
  3006. }
  3007. if (!test_bit(R5_Insync, &dev->flags)) {
  3008. /* The ReadError flag will just be confusing now */
  3009. clear_bit(R5_ReadError, &dev->flags);
  3010. clear_bit(R5_ReWrite, &dev->flags);
  3011. }
  3012. if (test_bit(R5_ReadError, &dev->flags))
  3013. clear_bit(R5_Insync, &dev->flags);
  3014. if (!test_bit(R5_Insync, &dev->flags)) {
  3015. if (s.failed < 2)
  3016. r6s.failed_num[s.failed] = i;
  3017. s.failed++;
  3018. }
  3019. }
  3020. rcu_read_unlock();
  3021. if (unlikely(blocked_rdev)) {
  3022. if (s.syncing || s.expanding || s.expanded ||
  3023. s.to_write || s.written) {
  3024. set_bit(STRIPE_HANDLE, &sh->state);
  3025. goto unlock;
  3026. }
  3027. /* There is nothing for the blocked_rdev to block */
  3028. rdev_dec_pending(blocked_rdev, conf->mddev);
  3029. blocked_rdev = NULL;
  3030. }
  3031. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  3032. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  3033. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  3034. }
  3035. pr_debug("locked=%d uptodate=%d to_read=%d"
  3036. " to_write=%d failed=%d failed_num=%d,%d\n",
  3037. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  3038. r6s.failed_num[0], r6s.failed_num[1]);
  3039. /* check if the array has lost >2 devices and, if so, some requests
  3040. * might need to be failed
  3041. */
  3042. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  3043. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  3044. if (s.failed > 2 && s.syncing) {
  3045. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  3046. clear_bit(STRIPE_SYNCING, &sh->state);
  3047. s.syncing = 0;
  3048. }
  3049. /*
  3050. * might be able to return some write requests if the parity blocks
  3051. * are safe, or on a failed drive
  3052. */
  3053. pdev = &sh->dev[pd_idx];
  3054. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  3055. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  3056. qdev = &sh->dev[qd_idx];
  3057. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
  3058. || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
  3059. if ( s.written &&
  3060. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  3061. && !test_bit(R5_LOCKED, &pdev->flags)
  3062. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  3063. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  3064. && !test_bit(R5_LOCKED, &qdev->flags)
  3065. && test_bit(R5_UPTODATE, &qdev->flags)))))
  3066. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  3067. /* Now we might consider reading some blocks, either to check/generate
  3068. * parity, or to satisfy requests
  3069. * or to load a block that is being partially written.
  3070. */
  3071. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  3072. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  3073. handle_stripe_fill6(sh, &s, &r6s, disks);
  3074. /* Now we check to see if any write operations have recently
  3075. * completed
  3076. */
  3077. if (sh->reconstruct_state == reconstruct_state_drain_result) {
  3078. sh->reconstruct_state = reconstruct_state_idle;
  3079. /* All the 'written' buffers and the parity blocks are ready to
  3080. * be written back to disk
  3081. */
  3082. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  3083. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
  3084. for (i = disks; i--; ) {
  3085. dev = &sh->dev[i];
  3086. if (test_bit(R5_LOCKED, &dev->flags) &&
  3087. (i == sh->pd_idx || i == qd_idx ||
  3088. dev->written)) {
  3089. pr_debug("Writing block %d\n", i);
  3090. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  3091. set_bit(R5_Wantwrite, &dev->flags);
  3092. if (!test_bit(R5_Insync, &dev->flags) ||
  3093. ((i == sh->pd_idx || i == qd_idx) &&
  3094. s.failed == 0))
  3095. set_bit(STRIPE_INSYNC, &sh->state);
  3096. }
  3097. }
  3098. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3099. dec_preread_active = 1;
  3100. }
  3101. /* Now to consider new write requests and what else, if anything
  3102. * should be read. We do not handle new writes when:
  3103. * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
  3104. * 2/ A 'check' operation is in flight, as it may clobber the parity
  3105. * block.
  3106. */
  3107. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  3108. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  3109. /* maybe we need to check and possibly fix the parity for this stripe
  3110. * Any reads will already have been scheduled, so we just see if enough
  3111. * data is available. The parity check is held off while parity
  3112. * dependent operations are in flight.
  3113. */
  3114. if (sh->check_state ||
  3115. (s.syncing && s.locked == 0 &&
  3116. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3117. !test_bit(STRIPE_INSYNC, &sh->state)))
  3118. handle_parity_checks6(conf, sh, &s, &r6s, disks);
  3119. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  3120. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  3121. clear_bit(STRIPE_SYNCING, &sh->state);
  3122. }
  3123. /* If the failed drives are just a ReadError, then we might need
  3124. * to progress the repair/check process
  3125. */
  3126. if (s.failed <= 2 && !conf->mddev->ro)
  3127. for (i = 0; i < s.failed; i++) {
  3128. dev = &sh->dev[r6s.failed_num[i]];
  3129. if (test_bit(R5_ReadError, &dev->flags)
  3130. && !test_bit(R5_LOCKED, &dev->flags)
  3131. && test_bit(R5_UPTODATE, &dev->flags)
  3132. ) {
  3133. if (!test_bit(R5_ReWrite, &dev->flags)) {
  3134. set_bit(R5_Wantwrite, &dev->flags);
  3135. set_bit(R5_ReWrite, &dev->flags);
  3136. set_bit(R5_LOCKED, &dev->flags);
  3137. s.locked++;
  3138. } else {
  3139. /* let's read it back */
  3140. set_bit(R5_Wantread, &dev->flags);
  3141. set_bit(R5_LOCKED, &dev->flags);
  3142. s.locked++;
  3143. }
  3144. }
  3145. }
  3146. /* Finish reconstruct operations initiated by the expansion process */
  3147. if (sh->reconstruct_state == reconstruct_state_result) {
  3148. sh->reconstruct_state = reconstruct_state_idle;
  3149. clear_bit(STRIPE_EXPANDING, &sh->state);
  3150. for (i = conf->raid_disks; i--; ) {
  3151. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  3152. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3153. s.locked++;
  3154. }
  3155. }
  3156. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  3157. !sh->reconstruct_state) {
  3158. struct stripe_head *sh2
  3159. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  3160. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  3161. /* sh cannot be written until sh2 has been read.
  3162. * so arrange for sh to be delayed a little
  3163. */
  3164. set_bit(STRIPE_DELAYED, &sh->state);
  3165. set_bit(STRIPE_HANDLE, &sh->state);
  3166. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  3167. &sh2->state))
  3168. atomic_inc(&conf->preread_active_stripes);
  3169. release_stripe(sh2);
  3170. goto unlock;
  3171. }
  3172. if (sh2)
  3173. release_stripe(sh2);
  3174. /* Need to write out all blocks after computing P&Q */
  3175. sh->disks = conf->raid_disks;
  3176. stripe_set_idx(sh->sector, conf, 0, sh);
  3177. schedule_reconstruction(sh, &s, 1, 1);
  3178. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  3179. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  3180. atomic_dec(&conf->reshape_stripes);
  3181. wake_up(&conf->wait_for_overlap);
  3182. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3183. }
  3184. if (s.expanding && s.locked == 0 &&
  3185. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  3186. handle_stripe_expansion(conf, sh, &r6s);
  3187. unlock:
  3188. spin_unlock(&sh->lock);
  3189. /* wait for this device to become unblocked */
  3190. if (unlikely(blocked_rdev))
  3191. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  3192. if (s.ops_request)
  3193. raid_run_ops(sh, s.ops_request);
  3194. ops_run_io(sh, &s);
  3195. if (dec_preread_active) {
  3196. /* We delay this until after ops_run_io so that if make_request
  3197. * is waiting on a flush, it won't continue until the writes
  3198. * have actually been submitted.
  3199. */
  3200. atomic_dec(&conf->preread_active_stripes);
  3201. if (atomic_read(&conf->preread_active_stripes) <
  3202. IO_THRESHOLD)
  3203. md_wakeup_thread(conf->mddev->thread);
  3204. }
  3205. return_io(return_bi);
  3206. }
  3207. static void handle_stripe(struct stripe_head *sh)
  3208. {
  3209. if (sh->raid_conf->level == 6)
  3210. handle_stripe6(sh);
  3211. else
  3212. handle_stripe5(sh);
  3213. }
  3214. static void raid5_activate_delayed(raid5_conf_t *conf)
  3215. {
  3216. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  3217. while (!list_empty(&conf->delayed_list)) {
  3218. struct list_head *l = conf->delayed_list.next;
  3219. struct stripe_head *sh;
  3220. sh = list_entry(l, struct stripe_head, lru);
  3221. list_del_init(l);
  3222. clear_bit(STRIPE_DELAYED, &sh->state);
  3223. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3224. atomic_inc(&conf->preread_active_stripes);
  3225. list_add_tail(&sh->lru, &conf->hold_list);
  3226. }
  3227. }
  3228. }
  3229. static void activate_bit_delay(raid5_conf_t *conf)
  3230. {
  3231. /* device_lock is held */
  3232. struct list_head head;
  3233. list_add(&head, &conf->bitmap_list);
  3234. list_del_init(&conf->bitmap_list);
  3235. while (!list_empty(&head)) {
  3236. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  3237. list_del_init(&sh->lru);
  3238. atomic_inc(&sh->count);
  3239. __release_stripe(conf, sh);
  3240. }
  3241. }
  3242. int md_raid5_congested(mddev_t *mddev, int bits)
  3243. {
  3244. raid5_conf_t *conf = mddev->private;
  3245. /* No difference between reads and writes. Just check
  3246. * how busy the stripe_cache is
  3247. */
  3248. if (conf->inactive_blocked)
  3249. return 1;
  3250. if (conf->quiesce)
  3251. return 1;
  3252. if (list_empty_careful(&conf->inactive_list))
  3253. return 1;
  3254. return 0;
  3255. }
  3256. EXPORT_SYMBOL_GPL(md_raid5_congested);
  3257. static int raid5_congested(void *data, int bits)
  3258. {
  3259. mddev_t *mddev = data;
  3260. return mddev_congested(mddev, bits) ||
  3261. md_raid5_congested(mddev, bits);
  3262. }
  3263. /* We want read requests to align with chunks where possible,
  3264. * but write requests don't need to.
  3265. */
  3266. static int raid5_mergeable_bvec(struct request_queue *q,
  3267. struct bvec_merge_data *bvm,
  3268. struct bio_vec *biovec)
  3269. {
  3270. mddev_t *mddev = q->queuedata;
  3271. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  3272. int max;
  3273. unsigned int chunk_sectors = mddev->chunk_sectors;
  3274. unsigned int bio_sectors = bvm->bi_size >> 9;
  3275. if ((bvm->bi_rw & 1) == WRITE)
  3276. return biovec->bv_len; /* always allow writes to be mergeable */
  3277. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3278. chunk_sectors = mddev->new_chunk_sectors;
  3279. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  3280. if (max < 0) max = 0;
  3281. if (max <= biovec->bv_len && bio_sectors == 0)
  3282. return biovec->bv_len;
  3283. else
  3284. return max;
  3285. }
  3286. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  3287. {
  3288. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  3289. unsigned int chunk_sectors = mddev->chunk_sectors;
  3290. unsigned int bio_sectors = bio->bi_size >> 9;
  3291. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3292. chunk_sectors = mddev->new_chunk_sectors;
  3293. return chunk_sectors >=
  3294. ((sector & (chunk_sectors - 1)) + bio_sectors);
  3295. }
  3296. /*
  3297. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  3298. * later sampled by raid5d.
  3299. */
  3300. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  3301. {
  3302. unsigned long flags;
  3303. spin_lock_irqsave(&conf->device_lock, flags);
  3304. bi->bi_next = conf->retry_read_aligned_list;
  3305. conf->retry_read_aligned_list = bi;
  3306. spin_unlock_irqrestore(&conf->device_lock, flags);
  3307. md_wakeup_thread(conf->mddev->thread);
  3308. }
  3309. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  3310. {
  3311. struct bio *bi;
  3312. bi = conf->retry_read_aligned;
  3313. if (bi) {
  3314. conf->retry_read_aligned = NULL;
  3315. return bi;
  3316. }
  3317. bi = conf->retry_read_aligned_list;
  3318. if(bi) {
  3319. conf->retry_read_aligned_list = bi->bi_next;
  3320. bi->bi_next = NULL;
  3321. /*
  3322. * this sets the active strip count to 1 and the processed
  3323. * strip count to zero (upper 8 bits)
  3324. */
  3325. bi->bi_phys_segments = 1; /* biased count of active stripes */
  3326. }
  3327. return bi;
  3328. }
  3329. /*
  3330. * The "raid5_align_endio" should check if the read succeeded and if it
  3331. * did, call bio_endio on the original bio (having bio_put the new bio
  3332. * first).
  3333. * If the read failed..
  3334. */
  3335. static void raid5_align_endio(struct bio *bi, int error)
  3336. {
  3337. struct bio* raid_bi = bi->bi_private;
  3338. mddev_t *mddev;
  3339. raid5_conf_t *conf;
  3340. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3341. mdk_rdev_t *rdev;
  3342. bio_put(bi);
  3343. rdev = (void*)raid_bi->bi_next;
  3344. raid_bi->bi_next = NULL;
  3345. mddev = rdev->mddev;
  3346. conf = mddev->private;
  3347. rdev_dec_pending(rdev, conf->mddev);
  3348. if (!error && uptodate) {
  3349. bio_endio(raid_bi, 0);
  3350. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3351. wake_up(&conf->wait_for_stripe);
  3352. return;
  3353. }
  3354. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3355. add_bio_to_retry(raid_bi, conf);
  3356. }
  3357. static int bio_fits_rdev(struct bio *bi)
  3358. {
  3359. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3360. if ((bi->bi_size>>9) > queue_max_sectors(q))
  3361. return 0;
  3362. blk_recount_segments(q, bi);
  3363. if (bi->bi_phys_segments > queue_max_segments(q))
  3364. return 0;
  3365. if (q->merge_bvec_fn)
  3366. /* it's too hard to apply the merge_bvec_fn at this stage,
  3367. * just just give up
  3368. */
  3369. return 0;
  3370. return 1;
  3371. }
  3372. static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio)
  3373. {
  3374. raid5_conf_t *conf = mddev->private;
  3375. int dd_idx;
  3376. struct bio* align_bi;
  3377. mdk_rdev_t *rdev;
  3378. if (!in_chunk_boundary(mddev, raid_bio)) {
  3379. pr_debug("chunk_aligned_read : non aligned\n");
  3380. return 0;
  3381. }
  3382. /*
  3383. * use bio_clone_mddev to make a copy of the bio
  3384. */
  3385. align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
  3386. if (!align_bi)
  3387. return 0;
  3388. /*
  3389. * set bi_end_io to a new function, and set bi_private to the
  3390. * original bio.
  3391. */
  3392. align_bi->bi_end_io = raid5_align_endio;
  3393. align_bi->bi_private = raid_bio;
  3394. /*
  3395. * compute position
  3396. */
  3397. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3398. 0,
  3399. &dd_idx, NULL);
  3400. rcu_read_lock();
  3401. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3402. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3403. atomic_inc(&rdev->nr_pending);
  3404. rcu_read_unlock();
  3405. raid_bio->bi_next = (void*)rdev;
  3406. align_bi->bi_bdev = rdev->bdev;
  3407. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3408. align_bi->bi_sector += rdev->data_offset;
  3409. if (!bio_fits_rdev(align_bi)) {
  3410. /* too big in some way */
  3411. bio_put(align_bi);
  3412. rdev_dec_pending(rdev, mddev);
  3413. return 0;
  3414. }
  3415. spin_lock_irq(&conf->device_lock);
  3416. wait_event_lock_irq(conf->wait_for_stripe,
  3417. conf->quiesce == 0,
  3418. conf->device_lock, /* nothing */);
  3419. atomic_inc(&conf->active_aligned_reads);
  3420. spin_unlock_irq(&conf->device_lock);
  3421. generic_make_request(align_bi);
  3422. return 1;
  3423. } else {
  3424. rcu_read_unlock();
  3425. bio_put(align_bi);
  3426. return 0;
  3427. }
  3428. }
  3429. /* __get_priority_stripe - get the next stripe to process
  3430. *
  3431. * Full stripe writes are allowed to pass preread active stripes up until
  3432. * the bypass_threshold is exceeded. In general the bypass_count
  3433. * increments when the handle_list is handled before the hold_list; however, it
  3434. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3435. * stripe with in flight i/o. The bypass_count will be reset when the
  3436. * head of the hold_list has changed, i.e. the head was promoted to the
  3437. * handle_list.
  3438. */
  3439. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  3440. {
  3441. struct stripe_head *sh;
  3442. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3443. __func__,
  3444. list_empty(&conf->handle_list) ? "empty" : "busy",
  3445. list_empty(&conf->hold_list) ? "empty" : "busy",
  3446. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3447. if (!list_empty(&conf->handle_list)) {
  3448. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3449. if (list_empty(&conf->hold_list))
  3450. conf->bypass_count = 0;
  3451. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3452. if (conf->hold_list.next == conf->last_hold)
  3453. conf->bypass_count++;
  3454. else {
  3455. conf->last_hold = conf->hold_list.next;
  3456. conf->bypass_count -= conf->bypass_threshold;
  3457. if (conf->bypass_count < 0)
  3458. conf->bypass_count = 0;
  3459. }
  3460. }
  3461. } else if (!list_empty(&conf->hold_list) &&
  3462. ((conf->bypass_threshold &&
  3463. conf->bypass_count > conf->bypass_threshold) ||
  3464. atomic_read(&conf->pending_full_writes) == 0)) {
  3465. sh = list_entry(conf->hold_list.next,
  3466. typeof(*sh), lru);
  3467. conf->bypass_count -= conf->bypass_threshold;
  3468. if (conf->bypass_count < 0)
  3469. conf->bypass_count = 0;
  3470. } else
  3471. return NULL;
  3472. list_del_init(&sh->lru);
  3473. atomic_inc(&sh->count);
  3474. BUG_ON(atomic_read(&sh->count) != 1);
  3475. return sh;
  3476. }
  3477. static int make_request(mddev_t *mddev, struct bio * bi)
  3478. {
  3479. raid5_conf_t *conf = mddev->private;
  3480. int dd_idx;
  3481. sector_t new_sector;
  3482. sector_t logical_sector, last_sector;
  3483. struct stripe_head *sh;
  3484. const int rw = bio_data_dir(bi);
  3485. int remaining;
  3486. int plugged;
  3487. if (unlikely(bi->bi_rw & REQ_FLUSH)) {
  3488. md_flush_request(mddev, bi);
  3489. return 0;
  3490. }
  3491. md_write_start(mddev, bi);
  3492. if (rw == READ &&
  3493. mddev->reshape_position == MaxSector &&
  3494. chunk_aligned_read(mddev,bi))
  3495. return 0;
  3496. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3497. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3498. bi->bi_next = NULL;
  3499. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3500. plugged = mddev_check_plugged(mddev);
  3501. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3502. DEFINE_WAIT(w);
  3503. int disks, data_disks;
  3504. int previous;
  3505. retry:
  3506. previous = 0;
  3507. disks = conf->raid_disks;
  3508. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3509. if (unlikely(conf->reshape_progress != MaxSector)) {
  3510. /* spinlock is needed as reshape_progress may be
  3511. * 64bit on a 32bit platform, and so it might be
  3512. * possible to see a half-updated value
  3513. * Ofcourse reshape_progress could change after
  3514. * the lock is dropped, so once we get a reference
  3515. * to the stripe that we think it is, we will have
  3516. * to check again.
  3517. */
  3518. spin_lock_irq(&conf->device_lock);
  3519. if (mddev->delta_disks < 0
  3520. ? logical_sector < conf->reshape_progress
  3521. : logical_sector >= conf->reshape_progress) {
  3522. disks = conf->previous_raid_disks;
  3523. previous = 1;
  3524. } else {
  3525. if (mddev->delta_disks < 0
  3526. ? logical_sector < conf->reshape_safe
  3527. : logical_sector >= conf->reshape_safe) {
  3528. spin_unlock_irq(&conf->device_lock);
  3529. schedule();
  3530. goto retry;
  3531. }
  3532. }
  3533. spin_unlock_irq(&conf->device_lock);
  3534. }
  3535. data_disks = disks - conf->max_degraded;
  3536. new_sector = raid5_compute_sector(conf, logical_sector,
  3537. previous,
  3538. &dd_idx, NULL);
  3539. pr_debug("raid456: make_request, sector %llu logical %llu\n",
  3540. (unsigned long long)new_sector,
  3541. (unsigned long long)logical_sector);
  3542. sh = get_active_stripe(conf, new_sector, previous,
  3543. (bi->bi_rw&RWA_MASK), 0);
  3544. if (sh) {
  3545. if (unlikely(previous)) {
  3546. /* expansion might have moved on while waiting for a
  3547. * stripe, so we must do the range check again.
  3548. * Expansion could still move past after this
  3549. * test, but as we are holding a reference to
  3550. * 'sh', we know that if that happens,
  3551. * STRIPE_EXPANDING will get set and the expansion
  3552. * won't proceed until we finish with the stripe.
  3553. */
  3554. int must_retry = 0;
  3555. spin_lock_irq(&conf->device_lock);
  3556. if (mddev->delta_disks < 0
  3557. ? logical_sector >= conf->reshape_progress
  3558. : logical_sector < conf->reshape_progress)
  3559. /* mismatch, need to try again */
  3560. must_retry = 1;
  3561. spin_unlock_irq(&conf->device_lock);
  3562. if (must_retry) {
  3563. release_stripe(sh);
  3564. schedule();
  3565. goto retry;
  3566. }
  3567. }
  3568. if (bio_data_dir(bi) == WRITE &&
  3569. logical_sector >= mddev->suspend_lo &&
  3570. logical_sector < mddev->suspend_hi) {
  3571. release_stripe(sh);
  3572. /* As the suspend_* range is controlled by
  3573. * userspace, we want an interruptible
  3574. * wait.
  3575. */
  3576. flush_signals(current);
  3577. prepare_to_wait(&conf->wait_for_overlap,
  3578. &w, TASK_INTERRUPTIBLE);
  3579. if (logical_sector >= mddev->suspend_lo &&
  3580. logical_sector < mddev->suspend_hi)
  3581. schedule();
  3582. goto retry;
  3583. }
  3584. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3585. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3586. /* Stripe is busy expanding or
  3587. * add failed due to overlap. Flush everything
  3588. * and wait a while
  3589. */
  3590. md_wakeup_thread(mddev->thread);
  3591. release_stripe(sh);
  3592. schedule();
  3593. goto retry;
  3594. }
  3595. finish_wait(&conf->wait_for_overlap, &w);
  3596. set_bit(STRIPE_HANDLE, &sh->state);
  3597. clear_bit(STRIPE_DELAYED, &sh->state);
  3598. if ((bi->bi_rw & REQ_SYNC) &&
  3599. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3600. atomic_inc(&conf->preread_active_stripes);
  3601. release_stripe(sh);
  3602. } else {
  3603. /* cannot get stripe for read-ahead, just give-up */
  3604. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3605. finish_wait(&conf->wait_for_overlap, &w);
  3606. break;
  3607. }
  3608. }
  3609. if (!plugged)
  3610. md_wakeup_thread(mddev->thread);
  3611. spin_lock_irq(&conf->device_lock);
  3612. remaining = raid5_dec_bi_phys_segments(bi);
  3613. spin_unlock_irq(&conf->device_lock);
  3614. if (remaining == 0) {
  3615. if ( rw == WRITE )
  3616. md_write_end(mddev);
  3617. bio_endio(bi, 0);
  3618. }
  3619. return 0;
  3620. }
  3621. static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
  3622. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3623. {
  3624. /* reshaping is quite different to recovery/resync so it is
  3625. * handled quite separately ... here.
  3626. *
  3627. * On each call to sync_request, we gather one chunk worth of
  3628. * destination stripes and flag them as expanding.
  3629. * Then we find all the source stripes and request reads.
  3630. * As the reads complete, handle_stripe will copy the data
  3631. * into the destination stripe and release that stripe.
  3632. */
  3633. raid5_conf_t *conf = mddev->private;
  3634. struct stripe_head *sh;
  3635. sector_t first_sector, last_sector;
  3636. int raid_disks = conf->previous_raid_disks;
  3637. int data_disks = raid_disks - conf->max_degraded;
  3638. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3639. int i;
  3640. int dd_idx;
  3641. sector_t writepos, readpos, safepos;
  3642. sector_t stripe_addr;
  3643. int reshape_sectors;
  3644. struct list_head stripes;
  3645. if (sector_nr == 0) {
  3646. /* If restarting in the middle, skip the initial sectors */
  3647. if (mddev->delta_disks < 0 &&
  3648. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3649. sector_nr = raid5_size(mddev, 0, 0)
  3650. - conf->reshape_progress;
  3651. } else if (mddev->delta_disks >= 0 &&
  3652. conf->reshape_progress > 0)
  3653. sector_nr = conf->reshape_progress;
  3654. sector_div(sector_nr, new_data_disks);
  3655. if (sector_nr) {
  3656. mddev->curr_resync_completed = sector_nr;
  3657. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3658. *skipped = 1;
  3659. return sector_nr;
  3660. }
  3661. }
  3662. /* We need to process a full chunk at a time.
  3663. * If old and new chunk sizes differ, we need to process the
  3664. * largest of these
  3665. */
  3666. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  3667. reshape_sectors = mddev->new_chunk_sectors;
  3668. else
  3669. reshape_sectors = mddev->chunk_sectors;
  3670. /* we update the metadata when there is more than 3Meg
  3671. * in the block range (that is rather arbitrary, should
  3672. * probably be time based) or when the data about to be
  3673. * copied would over-write the source of the data at
  3674. * the front of the range.
  3675. * i.e. one new_stripe along from reshape_progress new_maps
  3676. * to after where reshape_safe old_maps to
  3677. */
  3678. writepos = conf->reshape_progress;
  3679. sector_div(writepos, new_data_disks);
  3680. readpos = conf->reshape_progress;
  3681. sector_div(readpos, data_disks);
  3682. safepos = conf->reshape_safe;
  3683. sector_div(safepos, data_disks);
  3684. if (mddev->delta_disks < 0) {
  3685. writepos -= min_t(sector_t, reshape_sectors, writepos);
  3686. readpos += reshape_sectors;
  3687. safepos += reshape_sectors;
  3688. } else {
  3689. writepos += reshape_sectors;
  3690. readpos -= min_t(sector_t, reshape_sectors, readpos);
  3691. safepos -= min_t(sector_t, reshape_sectors, safepos);
  3692. }
  3693. /* 'writepos' is the most advanced device address we might write.
  3694. * 'readpos' is the least advanced device address we might read.
  3695. * 'safepos' is the least address recorded in the metadata as having
  3696. * been reshaped.
  3697. * If 'readpos' is behind 'writepos', then there is no way that we can
  3698. * ensure safety in the face of a crash - that must be done by userspace
  3699. * making a backup of the data. So in that case there is no particular
  3700. * rush to update metadata.
  3701. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3702. * update the metadata to advance 'safepos' to match 'readpos' so that
  3703. * we can be safe in the event of a crash.
  3704. * So we insist on updating metadata if safepos is behind writepos and
  3705. * readpos is beyond writepos.
  3706. * In any case, update the metadata every 10 seconds.
  3707. * Maybe that number should be configurable, but I'm not sure it is
  3708. * worth it.... maybe it could be a multiple of safemode_delay???
  3709. */
  3710. if ((mddev->delta_disks < 0
  3711. ? (safepos > writepos && readpos < writepos)
  3712. : (safepos < writepos && readpos > writepos)) ||
  3713. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3714. /* Cannot proceed until we've updated the superblock... */
  3715. wait_event(conf->wait_for_overlap,
  3716. atomic_read(&conf->reshape_stripes)==0);
  3717. mddev->reshape_position = conf->reshape_progress;
  3718. mddev->curr_resync_completed = sector_nr;
  3719. conf->reshape_checkpoint = jiffies;
  3720. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3721. md_wakeup_thread(mddev->thread);
  3722. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3723. kthread_should_stop());
  3724. spin_lock_irq(&conf->device_lock);
  3725. conf->reshape_safe = mddev->reshape_position;
  3726. spin_unlock_irq(&conf->device_lock);
  3727. wake_up(&conf->wait_for_overlap);
  3728. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3729. }
  3730. if (mddev->delta_disks < 0) {
  3731. BUG_ON(conf->reshape_progress == 0);
  3732. stripe_addr = writepos;
  3733. BUG_ON((mddev->dev_sectors &
  3734. ~((sector_t)reshape_sectors - 1))
  3735. - reshape_sectors - stripe_addr
  3736. != sector_nr);
  3737. } else {
  3738. BUG_ON(writepos != sector_nr + reshape_sectors);
  3739. stripe_addr = sector_nr;
  3740. }
  3741. INIT_LIST_HEAD(&stripes);
  3742. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  3743. int j;
  3744. int skipped_disk = 0;
  3745. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  3746. set_bit(STRIPE_EXPANDING, &sh->state);
  3747. atomic_inc(&conf->reshape_stripes);
  3748. /* If any of this stripe is beyond the end of the old
  3749. * array, then we need to zero those blocks
  3750. */
  3751. for (j=sh->disks; j--;) {
  3752. sector_t s;
  3753. if (j == sh->pd_idx)
  3754. continue;
  3755. if (conf->level == 6 &&
  3756. j == sh->qd_idx)
  3757. continue;
  3758. s = compute_blocknr(sh, j, 0);
  3759. if (s < raid5_size(mddev, 0, 0)) {
  3760. skipped_disk = 1;
  3761. continue;
  3762. }
  3763. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3764. set_bit(R5_Expanded, &sh->dev[j].flags);
  3765. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3766. }
  3767. if (!skipped_disk) {
  3768. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3769. set_bit(STRIPE_HANDLE, &sh->state);
  3770. }
  3771. list_add(&sh->lru, &stripes);
  3772. }
  3773. spin_lock_irq(&conf->device_lock);
  3774. if (mddev->delta_disks < 0)
  3775. conf->reshape_progress -= reshape_sectors * new_data_disks;
  3776. else
  3777. conf->reshape_progress += reshape_sectors * new_data_disks;
  3778. spin_unlock_irq(&conf->device_lock);
  3779. /* Ok, those stripe are ready. We can start scheduling
  3780. * reads on the source stripes.
  3781. * The source stripes are determined by mapping the first and last
  3782. * block on the destination stripes.
  3783. */
  3784. first_sector =
  3785. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  3786. 1, &dd_idx, NULL);
  3787. last_sector =
  3788. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  3789. * new_data_disks - 1),
  3790. 1, &dd_idx, NULL);
  3791. if (last_sector >= mddev->dev_sectors)
  3792. last_sector = mddev->dev_sectors - 1;
  3793. while (first_sector <= last_sector) {
  3794. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  3795. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3796. set_bit(STRIPE_HANDLE, &sh->state);
  3797. release_stripe(sh);
  3798. first_sector += STRIPE_SECTORS;
  3799. }
  3800. /* Now that the sources are clearly marked, we can release
  3801. * the destination stripes
  3802. */
  3803. while (!list_empty(&stripes)) {
  3804. sh = list_entry(stripes.next, struct stripe_head, lru);
  3805. list_del_init(&sh->lru);
  3806. release_stripe(sh);
  3807. }
  3808. /* If this takes us to the resync_max point where we have to pause,
  3809. * then we need to write out the superblock.
  3810. */
  3811. sector_nr += reshape_sectors;
  3812. if ((sector_nr - mddev->curr_resync_completed) * 2
  3813. >= mddev->resync_max - mddev->curr_resync_completed) {
  3814. /* Cannot proceed until we've updated the superblock... */
  3815. wait_event(conf->wait_for_overlap,
  3816. atomic_read(&conf->reshape_stripes) == 0);
  3817. mddev->reshape_position = conf->reshape_progress;
  3818. mddev->curr_resync_completed = sector_nr;
  3819. conf->reshape_checkpoint = jiffies;
  3820. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3821. md_wakeup_thread(mddev->thread);
  3822. wait_event(mddev->sb_wait,
  3823. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3824. || kthread_should_stop());
  3825. spin_lock_irq(&conf->device_lock);
  3826. conf->reshape_safe = mddev->reshape_position;
  3827. spin_unlock_irq(&conf->device_lock);
  3828. wake_up(&conf->wait_for_overlap);
  3829. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3830. }
  3831. return reshape_sectors;
  3832. }
  3833. /* FIXME go_faster isn't used */
  3834. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3835. {
  3836. raid5_conf_t *conf = mddev->private;
  3837. struct stripe_head *sh;
  3838. sector_t max_sector = mddev->dev_sectors;
  3839. sector_t sync_blocks;
  3840. int still_degraded = 0;
  3841. int i;
  3842. if (sector_nr >= max_sector) {
  3843. /* just being told to finish up .. nothing much to do */
  3844. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3845. end_reshape(conf);
  3846. return 0;
  3847. }
  3848. if (mddev->curr_resync < max_sector) /* aborted */
  3849. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3850. &sync_blocks, 1);
  3851. else /* completed sync */
  3852. conf->fullsync = 0;
  3853. bitmap_close_sync(mddev->bitmap);
  3854. return 0;
  3855. }
  3856. /* Allow raid5_quiesce to complete */
  3857. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  3858. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3859. return reshape_request(mddev, sector_nr, skipped);
  3860. /* No need to check resync_max as we never do more than one
  3861. * stripe, and as resync_max will always be on a chunk boundary,
  3862. * if the check in md_do_sync didn't fire, there is no chance
  3863. * of overstepping resync_max here
  3864. */
  3865. /* if there is too many failed drives and we are trying
  3866. * to resync, then assert that we are finished, because there is
  3867. * nothing we can do.
  3868. */
  3869. if (mddev->degraded >= conf->max_degraded &&
  3870. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3871. sector_t rv = mddev->dev_sectors - sector_nr;
  3872. *skipped = 1;
  3873. return rv;
  3874. }
  3875. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3876. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3877. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3878. /* we can skip this block, and probably more */
  3879. sync_blocks /= STRIPE_SECTORS;
  3880. *skipped = 1;
  3881. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3882. }
  3883. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3884. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  3885. if (sh == NULL) {
  3886. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  3887. /* make sure we don't swamp the stripe cache if someone else
  3888. * is trying to get access
  3889. */
  3890. schedule_timeout_uninterruptible(1);
  3891. }
  3892. /* Need to check if array will still be degraded after recovery/resync
  3893. * We don't need to check the 'failed' flag as when that gets set,
  3894. * recovery aborts.
  3895. */
  3896. for (i = 0; i < conf->raid_disks; i++)
  3897. if (conf->disks[i].rdev == NULL)
  3898. still_degraded = 1;
  3899. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3900. spin_lock(&sh->lock);
  3901. set_bit(STRIPE_SYNCING, &sh->state);
  3902. clear_bit(STRIPE_INSYNC, &sh->state);
  3903. spin_unlock(&sh->lock);
  3904. handle_stripe(sh);
  3905. release_stripe(sh);
  3906. return STRIPE_SECTORS;
  3907. }
  3908. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3909. {
  3910. /* We may not be able to submit a whole bio at once as there
  3911. * may not be enough stripe_heads available.
  3912. * We cannot pre-allocate enough stripe_heads as we may need
  3913. * more than exist in the cache (if we allow ever large chunks).
  3914. * So we do one stripe head at a time and record in
  3915. * ->bi_hw_segments how many have been done.
  3916. *
  3917. * We *know* that this entire raid_bio is in one chunk, so
  3918. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3919. */
  3920. struct stripe_head *sh;
  3921. int dd_idx;
  3922. sector_t sector, logical_sector, last_sector;
  3923. int scnt = 0;
  3924. int remaining;
  3925. int handled = 0;
  3926. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3927. sector = raid5_compute_sector(conf, logical_sector,
  3928. 0, &dd_idx, NULL);
  3929. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3930. for (; logical_sector < last_sector;
  3931. logical_sector += STRIPE_SECTORS,
  3932. sector += STRIPE_SECTORS,
  3933. scnt++) {
  3934. if (scnt < raid5_bi_hw_segments(raid_bio))
  3935. /* already done this stripe */
  3936. continue;
  3937. sh = get_active_stripe(conf, sector, 0, 1, 0);
  3938. if (!sh) {
  3939. /* failed to get a stripe - must wait */
  3940. raid5_set_bi_hw_segments(raid_bio, scnt);
  3941. conf->retry_read_aligned = raid_bio;
  3942. return handled;
  3943. }
  3944. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3945. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3946. release_stripe(sh);
  3947. raid5_set_bi_hw_segments(raid_bio, scnt);
  3948. conf->retry_read_aligned = raid_bio;
  3949. return handled;
  3950. }
  3951. handle_stripe(sh);
  3952. release_stripe(sh);
  3953. handled++;
  3954. }
  3955. spin_lock_irq(&conf->device_lock);
  3956. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3957. spin_unlock_irq(&conf->device_lock);
  3958. if (remaining == 0)
  3959. bio_endio(raid_bio, 0);
  3960. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3961. wake_up(&conf->wait_for_stripe);
  3962. return handled;
  3963. }
  3964. /*
  3965. * This is our raid5 kernel thread.
  3966. *
  3967. * We scan the hash table for stripes which can be handled now.
  3968. * During the scan, completed stripes are saved for us by the interrupt
  3969. * handler, so that they will not have to wait for our next wakeup.
  3970. */
  3971. static void raid5d(mddev_t *mddev)
  3972. {
  3973. struct stripe_head *sh;
  3974. raid5_conf_t *conf = mddev->private;
  3975. int handled;
  3976. struct blk_plug plug;
  3977. pr_debug("+++ raid5d active\n");
  3978. md_check_recovery(mddev);
  3979. blk_start_plug(&plug);
  3980. handled = 0;
  3981. spin_lock_irq(&conf->device_lock);
  3982. while (1) {
  3983. struct bio *bio;
  3984. if (atomic_read(&mddev->plug_cnt) == 0 &&
  3985. !list_empty(&conf->bitmap_list)) {
  3986. /* Now is a good time to flush some bitmap updates */
  3987. conf->seq_flush++;
  3988. spin_unlock_irq(&conf->device_lock);
  3989. bitmap_unplug(mddev->bitmap);
  3990. spin_lock_irq(&conf->device_lock);
  3991. conf->seq_write = conf->seq_flush;
  3992. activate_bit_delay(conf);
  3993. }
  3994. if (atomic_read(&mddev->plug_cnt) == 0)
  3995. raid5_activate_delayed(conf);
  3996. while ((bio = remove_bio_from_retry(conf))) {
  3997. int ok;
  3998. spin_unlock_irq(&conf->device_lock);
  3999. ok = retry_aligned_read(conf, bio);
  4000. spin_lock_irq(&conf->device_lock);
  4001. if (!ok)
  4002. break;
  4003. handled++;
  4004. }
  4005. sh = __get_priority_stripe(conf);
  4006. if (!sh)
  4007. break;
  4008. spin_unlock_irq(&conf->device_lock);
  4009. handled++;
  4010. handle_stripe(sh);
  4011. release_stripe(sh);
  4012. cond_resched();
  4013. spin_lock_irq(&conf->device_lock);
  4014. }
  4015. pr_debug("%d stripes handled\n", handled);
  4016. spin_unlock_irq(&conf->device_lock);
  4017. async_tx_issue_pending_all();
  4018. blk_finish_plug(&plug);
  4019. pr_debug("--- raid5d inactive\n");
  4020. }
  4021. static ssize_t
  4022. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  4023. {
  4024. raid5_conf_t *conf = mddev->private;
  4025. if (conf)
  4026. return sprintf(page, "%d\n", conf->max_nr_stripes);
  4027. else
  4028. return 0;
  4029. }
  4030. int
  4031. raid5_set_cache_size(mddev_t *mddev, int size)
  4032. {
  4033. raid5_conf_t *conf = mddev->private;
  4034. int err;
  4035. if (size <= 16 || size > 32768)
  4036. return -EINVAL;
  4037. while (size < conf->max_nr_stripes) {
  4038. if (drop_one_stripe(conf))
  4039. conf->max_nr_stripes--;
  4040. else
  4041. break;
  4042. }
  4043. err = md_allow_write(mddev);
  4044. if (err)
  4045. return err;
  4046. while (size > conf->max_nr_stripes) {
  4047. if (grow_one_stripe(conf))
  4048. conf->max_nr_stripes++;
  4049. else break;
  4050. }
  4051. return 0;
  4052. }
  4053. EXPORT_SYMBOL(raid5_set_cache_size);
  4054. static ssize_t
  4055. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  4056. {
  4057. raid5_conf_t *conf = mddev->private;
  4058. unsigned long new;
  4059. int err;
  4060. if (len >= PAGE_SIZE)
  4061. return -EINVAL;
  4062. if (!conf)
  4063. return -ENODEV;
  4064. if (strict_strtoul(page, 10, &new))
  4065. return -EINVAL;
  4066. err = raid5_set_cache_size(mddev, new);
  4067. if (err)
  4068. return err;
  4069. return len;
  4070. }
  4071. static struct md_sysfs_entry
  4072. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  4073. raid5_show_stripe_cache_size,
  4074. raid5_store_stripe_cache_size);
  4075. static ssize_t
  4076. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  4077. {
  4078. raid5_conf_t *conf = mddev->private;
  4079. if (conf)
  4080. return sprintf(page, "%d\n", conf->bypass_threshold);
  4081. else
  4082. return 0;
  4083. }
  4084. static ssize_t
  4085. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  4086. {
  4087. raid5_conf_t *conf = mddev->private;
  4088. unsigned long new;
  4089. if (len >= PAGE_SIZE)
  4090. return -EINVAL;
  4091. if (!conf)
  4092. return -ENODEV;
  4093. if (strict_strtoul(page, 10, &new))
  4094. return -EINVAL;
  4095. if (new > conf->max_nr_stripes)
  4096. return -EINVAL;
  4097. conf->bypass_threshold = new;
  4098. return len;
  4099. }
  4100. static struct md_sysfs_entry
  4101. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  4102. S_IRUGO | S_IWUSR,
  4103. raid5_show_preread_threshold,
  4104. raid5_store_preread_threshold);
  4105. static ssize_t
  4106. stripe_cache_active_show(mddev_t *mddev, char *page)
  4107. {
  4108. raid5_conf_t *conf = mddev->private;
  4109. if (conf)
  4110. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  4111. else
  4112. return 0;
  4113. }
  4114. static struct md_sysfs_entry
  4115. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  4116. static struct attribute *raid5_attrs[] = {
  4117. &raid5_stripecache_size.attr,
  4118. &raid5_stripecache_active.attr,
  4119. &raid5_preread_bypass_threshold.attr,
  4120. NULL,
  4121. };
  4122. static struct attribute_group raid5_attrs_group = {
  4123. .name = NULL,
  4124. .attrs = raid5_attrs,
  4125. };
  4126. static sector_t
  4127. raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  4128. {
  4129. raid5_conf_t *conf = mddev->private;
  4130. if (!sectors)
  4131. sectors = mddev->dev_sectors;
  4132. if (!raid_disks)
  4133. /* size is defined by the smallest of previous and new size */
  4134. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  4135. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4136. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  4137. return sectors * (raid_disks - conf->max_degraded);
  4138. }
  4139. static void raid5_free_percpu(raid5_conf_t *conf)
  4140. {
  4141. struct raid5_percpu *percpu;
  4142. unsigned long cpu;
  4143. if (!conf->percpu)
  4144. return;
  4145. get_online_cpus();
  4146. for_each_possible_cpu(cpu) {
  4147. percpu = per_cpu_ptr(conf->percpu, cpu);
  4148. safe_put_page(percpu->spare_page);
  4149. kfree(percpu->scribble);
  4150. }
  4151. #ifdef CONFIG_HOTPLUG_CPU
  4152. unregister_cpu_notifier(&conf->cpu_notify);
  4153. #endif
  4154. put_online_cpus();
  4155. free_percpu(conf->percpu);
  4156. }
  4157. static void free_conf(raid5_conf_t *conf)
  4158. {
  4159. shrink_stripes(conf);
  4160. raid5_free_percpu(conf);
  4161. kfree(conf->disks);
  4162. kfree(conf->stripe_hashtbl);
  4163. kfree(conf);
  4164. }
  4165. #ifdef CONFIG_HOTPLUG_CPU
  4166. static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
  4167. void *hcpu)
  4168. {
  4169. raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
  4170. long cpu = (long)hcpu;
  4171. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  4172. switch (action) {
  4173. case CPU_UP_PREPARE:
  4174. case CPU_UP_PREPARE_FROZEN:
  4175. if (conf->level == 6 && !percpu->spare_page)
  4176. percpu->spare_page = alloc_page(GFP_KERNEL);
  4177. if (!percpu->scribble)
  4178. percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4179. if (!percpu->scribble ||
  4180. (conf->level == 6 && !percpu->spare_page)) {
  4181. safe_put_page(percpu->spare_page);
  4182. kfree(percpu->scribble);
  4183. pr_err("%s: failed memory allocation for cpu%ld\n",
  4184. __func__, cpu);
  4185. return notifier_from_errno(-ENOMEM);
  4186. }
  4187. break;
  4188. case CPU_DEAD:
  4189. case CPU_DEAD_FROZEN:
  4190. safe_put_page(percpu->spare_page);
  4191. kfree(percpu->scribble);
  4192. percpu->spare_page = NULL;
  4193. percpu->scribble = NULL;
  4194. break;
  4195. default:
  4196. break;
  4197. }
  4198. return NOTIFY_OK;
  4199. }
  4200. #endif
  4201. static int raid5_alloc_percpu(raid5_conf_t *conf)
  4202. {
  4203. unsigned long cpu;
  4204. struct page *spare_page;
  4205. struct raid5_percpu __percpu *allcpus;
  4206. void *scribble;
  4207. int err;
  4208. allcpus = alloc_percpu(struct raid5_percpu);
  4209. if (!allcpus)
  4210. return -ENOMEM;
  4211. conf->percpu = allcpus;
  4212. get_online_cpus();
  4213. err = 0;
  4214. for_each_present_cpu(cpu) {
  4215. if (conf->level == 6) {
  4216. spare_page = alloc_page(GFP_KERNEL);
  4217. if (!spare_page) {
  4218. err = -ENOMEM;
  4219. break;
  4220. }
  4221. per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
  4222. }
  4223. scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4224. if (!scribble) {
  4225. err = -ENOMEM;
  4226. break;
  4227. }
  4228. per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
  4229. }
  4230. #ifdef CONFIG_HOTPLUG_CPU
  4231. conf->cpu_notify.notifier_call = raid456_cpu_notify;
  4232. conf->cpu_notify.priority = 0;
  4233. if (err == 0)
  4234. err = register_cpu_notifier(&conf->cpu_notify);
  4235. #endif
  4236. put_online_cpus();
  4237. return err;
  4238. }
  4239. static raid5_conf_t *setup_conf(mddev_t *mddev)
  4240. {
  4241. raid5_conf_t *conf;
  4242. int raid_disk, memory, max_disks;
  4243. mdk_rdev_t *rdev;
  4244. struct disk_info *disk;
  4245. if (mddev->new_level != 5
  4246. && mddev->new_level != 4
  4247. && mddev->new_level != 6) {
  4248. printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  4249. mdname(mddev), mddev->new_level);
  4250. return ERR_PTR(-EIO);
  4251. }
  4252. if ((mddev->new_level == 5
  4253. && !algorithm_valid_raid5(mddev->new_layout)) ||
  4254. (mddev->new_level == 6
  4255. && !algorithm_valid_raid6(mddev->new_layout))) {
  4256. printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
  4257. mdname(mddev), mddev->new_layout);
  4258. return ERR_PTR(-EIO);
  4259. }
  4260. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  4261. printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  4262. mdname(mddev), mddev->raid_disks);
  4263. return ERR_PTR(-EINVAL);
  4264. }
  4265. if (!mddev->new_chunk_sectors ||
  4266. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  4267. !is_power_of_2(mddev->new_chunk_sectors)) {
  4268. printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
  4269. mdname(mddev), mddev->new_chunk_sectors << 9);
  4270. return ERR_PTR(-EINVAL);
  4271. }
  4272. conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
  4273. if (conf == NULL)
  4274. goto abort;
  4275. spin_lock_init(&conf->device_lock);
  4276. init_waitqueue_head(&conf->wait_for_stripe);
  4277. init_waitqueue_head(&conf->wait_for_overlap);
  4278. INIT_LIST_HEAD(&conf->handle_list);
  4279. INIT_LIST_HEAD(&conf->hold_list);
  4280. INIT_LIST_HEAD(&conf->delayed_list);
  4281. INIT_LIST_HEAD(&conf->bitmap_list);
  4282. INIT_LIST_HEAD(&conf->inactive_list);
  4283. atomic_set(&conf->active_stripes, 0);
  4284. atomic_set(&conf->preread_active_stripes, 0);
  4285. atomic_set(&conf->active_aligned_reads, 0);
  4286. conf->bypass_threshold = BYPASS_THRESHOLD;
  4287. conf->raid_disks = mddev->raid_disks;
  4288. if (mddev->reshape_position == MaxSector)
  4289. conf->previous_raid_disks = mddev->raid_disks;
  4290. else
  4291. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  4292. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  4293. conf->scribble_len = scribble_len(max_disks);
  4294. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  4295. GFP_KERNEL);
  4296. if (!conf->disks)
  4297. goto abort;
  4298. conf->mddev = mddev;
  4299. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  4300. goto abort;
  4301. conf->level = mddev->new_level;
  4302. if (raid5_alloc_percpu(conf) != 0)
  4303. goto abort;
  4304. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  4305. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4306. raid_disk = rdev->raid_disk;
  4307. if (raid_disk >= max_disks
  4308. || raid_disk < 0)
  4309. continue;
  4310. disk = conf->disks + raid_disk;
  4311. disk->rdev = rdev;
  4312. if (test_bit(In_sync, &rdev->flags)) {
  4313. char b[BDEVNAME_SIZE];
  4314. printk(KERN_INFO "md/raid:%s: device %s operational as raid"
  4315. " disk %d\n",
  4316. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  4317. } else
  4318. /* Cannot rely on bitmap to complete recovery */
  4319. conf->fullsync = 1;
  4320. }
  4321. conf->chunk_sectors = mddev->new_chunk_sectors;
  4322. conf->level = mddev->new_level;
  4323. if (conf->level == 6)
  4324. conf->max_degraded = 2;
  4325. else
  4326. conf->max_degraded = 1;
  4327. conf->algorithm = mddev->new_layout;
  4328. conf->max_nr_stripes = NR_STRIPES;
  4329. conf->reshape_progress = mddev->reshape_position;
  4330. if (conf->reshape_progress != MaxSector) {
  4331. conf->prev_chunk_sectors = mddev->chunk_sectors;
  4332. conf->prev_algo = mddev->layout;
  4333. }
  4334. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  4335. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  4336. if (grow_stripes(conf, conf->max_nr_stripes)) {
  4337. printk(KERN_ERR
  4338. "md/raid:%s: couldn't allocate %dkB for buffers\n",
  4339. mdname(mddev), memory);
  4340. goto abort;
  4341. } else
  4342. printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
  4343. mdname(mddev), memory);
  4344. conf->thread = md_register_thread(raid5d, mddev, NULL);
  4345. if (!conf->thread) {
  4346. printk(KERN_ERR
  4347. "md/raid:%s: couldn't allocate thread.\n",
  4348. mdname(mddev));
  4349. goto abort;
  4350. }
  4351. return conf;
  4352. abort:
  4353. if (conf) {
  4354. free_conf(conf);
  4355. return ERR_PTR(-EIO);
  4356. } else
  4357. return ERR_PTR(-ENOMEM);
  4358. }
  4359. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  4360. {
  4361. switch (algo) {
  4362. case ALGORITHM_PARITY_0:
  4363. if (raid_disk < max_degraded)
  4364. return 1;
  4365. break;
  4366. case ALGORITHM_PARITY_N:
  4367. if (raid_disk >= raid_disks - max_degraded)
  4368. return 1;
  4369. break;
  4370. case ALGORITHM_PARITY_0_6:
  4371. if (raid_disk == 0 ||
  4372. raid_disk == raid_disks - 1)
  4373. return 1;
  4374. break;
  4375. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4376. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4377. case ALGORITHM_LEFT_SYMMETRIC_6:
  4378. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4379. if (raid_disk == raid_disks - 1)
  4380. return 1;
  4381. }
  4382. return 0;
  4383. }
  4384. static int run(mddev_t *mddev)
  4385. {
  4386. raid5_conf_t *conf;
  4387. int working_disks = 0;
  4388. int dirty_parity_disks = 0;
  4389. mdk_rdev_t *rdev;
  4390. sector_t reshape_offset = 0;
  4391. if (mddev->recovery_cp != MaxSector)
  4392. printk(KERN_NOTICE "md/raid:%s: not clean"
  4393. " -- starting background reconstruction\n",
  4394. mdname(mddev));
  4395. if (mddev->reshape_position != MaxSector) {
  4396. /* Check that we can continue the reshape.
  4397. * Currently only disks can change, it must
  4398. * increase, and we must be past the point where
  4399. * a stripe over-writes itself
  4400. */
  4401. sector_t here_new, here_old;
  4402. int old_disks;
  4403. int max_degraded = (mddev->level == 6 ? 2 : 1);
  4404. if (mddev->new_level != mddev->level) {
  4405. printk(KERN_ERR "md/raid:%s: unsupported reshape "
  4406. "required - aborting.\n",
  4407. mdname(mddev));
  4408. return -EINVAL;
  4409. }
  4410. old_disks = mddev->raid_disks - mddev->delta_disks;
  4411. /* reshape_position must be on a new-stripe boundary, and one
  4412. * further up in new geometry must map after here in old
  4413. * geometry.
  4414. */
  4415. here_new = mddev->reshape_position;
  4416. if (sector_div(here_new, mddev->new_chunk_sectors *
  4417. (mddev->raid_disks - max_degraded))) {
  4418. printk(KERN_ERR "md/raid:%s: reshape_position not "
  4419. "on a stripe boundary\n", mdname(mddev));
  4420. return -EINVAL;
  4421. }
  4422. reshape_offset = here_new * mddev->new_chunk_sectors;
  4423. /* here_new is the stripe we will write to */
  4424. here_old = mddev->reshape_position;
  4425. sector_div(here_old, mddev->chunk_sectors *
  4426. (old_disks-max_degraded));
  4427. /* here_old is the first stripe that we might need to read
  4428. * from */
  4429. if (mddev->delta_disks == 0) {
  4430. /* We cannot be sure it is safe to start an in-place
  4431. * reshape. It is only safe if user-space if monitoring
  4432. * and taking constant backups.
  4433. * mdadm always starts a situation like this in
  4434. * readonly mode so it can take control before
  4435. * allowing any writes. So just check for that.
  4436. */
  4437. if ((here_new * mddev->new_chunk_sectors !=
  4438. here_old * mddev->chunk_sectors) ||
  4439. mddev->ro == 0) {
  4440. printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
  4441. " in read-only mode - aborting\n",
  4442. mdname(mddev));
  4443. return -EINVAL;
  4444. }
  4445. } else if (mddev->delta_disks < 0
  4446. ? (here_new * mddev->new_chunk_sectors <=
  4447. here_old * mddev->chunk_sectors)
  4448. : (here_new * mddev->new_chunk_sectors >=
  4449. here_old * mddev->chunk_sectors)) {
  4450. /* Reading from the same stripe as writing to - bad */
  4451. printk(KERN_ERR "md/raid:%s: reshape_position too early for "
  4452. "auto-recovery - aborting.\n",
  4453. mdname(mddev));
  4454. return -EINVAL;
  4455. }
  4456. printk(KERN_INFO "md/raid:%s: reshape will continue\n",
  4457. mdname(mddev));
  4458. /* OK, we should be able to continue; */
  4459. } else {
  4460. BUG_ON(mddev->level != mddev->new_level);
  4461. BUG_ON(mddev->layout != mddev->new_layout);
  4462. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  4463. BUG_ON(mddev->delta_disks != 0);
  4464. }
  4465. if (mddev->private == NULL)
  4466. conf = setup_conf(mddev);
  4467. else
  4468. conf = mddev->private;
  4469. if (IS_ERR(conf))
  4470. return PTR_ERR(conf);
  4471. mddev->thread = conf->thread;
  4472. conf->thread = NULL;
  4473. mddev->private = conf;
  4474. /*
  4475. * 0 for a fully functional array, 1 or 2 for a degraded array.
  4476. */
  4477. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4478. if (rdev->raid_disk < 0)
  4479. continue;
  4480. if (test_bit(In_sync, &rdev->flags)) {
  4481. working_disks++;
  4482. continue;
  4483. }
  4484. /* This disc is not fully in-sync. However if it
  4485. * just stored parity (beyond the recovery_offset),
  4486. * when we don't need to be concerned about the
  4487. * array being dirty.
  4488. * When reshape goes 'backwards', we never have
  4489. * partially completed devices, so we only need
  4490. * to worry about reshape going forwards.
  4491. */
  4492. /* Hack because v0.91 doesn't store recovery_offset properly. */
  4493. if (mddev->major_version == 0 &&
  4494. mddev->minor_version > 90)
  4495. rdev->recovery_offset = reshape_offset;
  4496. if (rdev->recovery_offset < reshape_offset) {
  4497. /* We need to check old and new layout */
  4498. if (!only_parity(rdev->raid_disk,
  4499. conf->algorithm,
  4500. conf->raid_disks,
  4501. conf->max_degraded))
  4502. continue;
  4503. }
  4504. if (!only_parity(rdev->raid_disk,
  4505. conf->prev_algo,
  4506. conf->previous_raid_disks,
  4507. conf->max_degraded))
  4508. continue;
  4509. dirty_parity_disks++;
  4510. }
  4511. mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
  4512. - working_disks);
  4513. if (has_failed(conf)) {
  4514. printk(KERN_ERR "md/raid:%s: not enough operational devices"
  4515. " (%d/%d failed)\n",
  4516. mdname(mddev), mddev->degraded, conf->raid_disks);
  4517. goto abort;
  4518. }
  4519. /* device size must be a multiple of chunk size */
  4520. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  4521. mddev->resync_max_sectors = mddev->dev_sectors;
  4522. if (mddev->degraded > dirty_parity_disks &&
  4523. mddev->recovery_cp != MaxSector) {
  4524. if (mddev->ok_start_degraded)
  4525. printk(KERN_WARNING
  4526. "md/raid:%s: starting dirty degraded array"
  4527. " - data corruption possible.\n",
  4528. mdname(mddev));
  4529. else {
  4530. printk(KERN_ERR
  4531. "md/raid:%s: cannot start dirty degraded array.\n",
  4532. mdname(mddev));
  4533. goto abort;
  4534. }
  4535. }
  4536. if (mddev->degraded == 0)
  4537. printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
  4538. " devices, algorithm %d\n", mdname(mddev), conf->level,
  4539. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4540. mddev->new_layout);
  4541. else
  4542. printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
  4543. " out of %d devices, algorithm %d\n",
  4544. mdname(mddev), conf->level,
  4545. mddev->raid_disks - mddev->degraded,
  4546. mddev->raid_disks, mddev->new_layout);
  4547. print_raid5_conf(conf);
  4548. if (conf->reshape_progress != MaxSector) {
  4549. conf->reshape_safe = conf->reshape_progress;
  4550. atomic_set(&conf->reshape_stripes, 0);
  4551. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4552. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4553. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4554. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4555. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4556. "reshape");
  4557. }
  4558. /* Ok, everything is just fine now */
  4559. if (mddev->to_remove == &raid5_attrs_group)
  4560. mddev->to_remove = NULL;
  4561. else if (mddev->kobj.sd &&
  4562. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4563. printk(KERN_WARNING
  4564. "raid5: failed to create sysfs attributes for %s\n",
  4565. mdname(mddev));
  4566. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4567. if (mddev->queue) {
  4568. int chunk_size;
  4569. /* read-ahead size must cover two whole stripes, which
  4570. * is 2 * (datadisks) * chunksize where 'n' is the
  4571. * number of raid devices
  4572. */
  4573. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4574. int stripe = data_disks *
  4575. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  4576. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4577. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4578. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4579. mddev->queue->backing_dev_info.congested_data = mddev;
  4580. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4581. chunk_size = mddev->chunk_sectors << 9;
  4582. blk_queue_io_min(mddev->queue, chunk_size);
  4583. blk_queue_io_opt(mddev->queue, chunk_size *
  4584. (conf->raid_disks - conf->max_degraded));
  4585. list_for_each_entry(rdev, &mddev->disks, same_set)
  4586. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4587. rdev->data_offset << 9);
  4588. }
  4589. return 0;
  4590. abort:
  4591. md_unregister_thread(mddev->thread);
  4592. mddev->thread = NULL;
  4593. if (conf) {
  4594. print_raid5_conf(conf);
  4595. free_conf(conf);
  4596. }
  4597. mddev->private = NULL;
  4598. printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
  4599. return -EIO;
  4600. }
  4601. static int stop(mddev_t *mddev)
  4602. {
  4603. raid5_conf_t *conf = mddev->private;
  4604. md_unregister_thread(mddev->thread);
  4605. mddev->thread = NULL;
  4606. if (mddev->queue)
  4607. mddev->queue->backing_dev_info.congested_fn = NULL;
  4608. free_conf(conf);
  4609. mddev->private = NULL;
  4610. mddev->to_remove = &raid5_attrs_group;
  4611. return 0;
  4612. }
  4613. #ifdef DEBUG
  4614. static void print_sh(struct seq_file *seq, struct stripe_head *sh)
  4615. {
  4616. int i;
  4617. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  4618. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  4619. seq_printf(seq, "sh %llu, count %d.\n",
  4620. (unsigned long long)sh->sector, atomic_read(&sh->count));
  4621. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  4622. for (i = 0; i < sh->disks; i++) {
  4623. seq_printf(seq, "(cache%d: %p %ld) ",
  4624. i, sh->dev[i].page, sh->dev[i].flags);
  4625. }
  4626. seq_printf(seq, "\n");
  4627. }
  4628. static void printall(struct seq_file *seq, raid5_conf_t *conf)
  4629. {
  4630. struct stripe_head *sh;
  4631. struct hlist_node *hn;
  4632. int i;
  4633. spin_lock_irq(&conf->device_lock);
  4634. for (i = 0; i < NR_HASH; i++) {
  4635. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  4636. if (sh->raid_conf != conf)
  4637. continue;
  4638. print_sh(seq, sh);
  4639. }
  4640. }
  4641. spin_unlock_irq(&conf->device_lock);
  4642. }
  4643. #endif
  4644. static void status(struct seq_file *seq, mddev_t *mddev)
  4645. {
  4646. raid5_conf_t *conf = mddev->private;
  4647. int i;
  4648. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  4649. mddev->chunk_sectors / 2, mddev->layout);
  4650. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4651. for (i = 0; i < conf->raid_disks; i++)
  4652. seq_printf (seq, "%s",
  4653. conf->disks[i].rdev &&
  4654. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4655. seq_printf (seq, "]");
  4656. #ifdef DEBUG
  4657. seq_printf (seq, "\n");
  4658. printall(seq, conf);
  4659. #endif
  4660. }
  4661. static void print_raid5_conf (raid5_conf_t *conf)
  4662. {
  4663. int i;
  4664. struct disk_info *tmp;
  4665. printk(KERN_DEBUG "RAID conf printout:\n");
  4666. if (!conf) {
  4667. printk("(conf==NULL)\n");
  4668. return;
  4669. }
  4670. printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
  4671. conf->raid_disks,
  4672. conf->raid_disks - conf->mddev->degraded);
  4673. for (i = 0; i < conf->raid_disks; i++) {
  4674. char b[BDEVNAME_SIZE];
  4675. tmp = conf->disks + i;
  4676. if (tmp->rdev)
  4677. printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
  4678. i, !test_bit(Faulty, &tmp->rdev->flags),
  4679. bdevname(tmp->rdev->bdev, b));
  4680. }
  4681. }
  4682. static int raid5_spare_active(mddev_t *mddev)
  4683. {
  4684. int i;
  4685. raid5_conf_t *conf = mddev->private;
  4686. struct disk_info *tmp;
  4687. int count = 0;
  4688. unsigned long flags;
  4689. for (i = 0; i < conf->raid_disks; i++) {
  4690. tmp = conf->disks + i;
  4691. if (tmp->rdev
  4692. && tmp->rdev->recovery_offset == MaxSector
  4693. && !test_bit(Faulty, &tmp->rdev->flags)
  4694. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4695. count++;
  4696. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  4697. }
  4698. }
  4699. spin_lock_irqsave(&conf->device_lock, flags);
  4700. mddev->degraded -= count;
  4701. spin_unlock_irqrestore(&conf->device_lock, flags);
  4702. print_raid5_conf(conf);
  4703. return count;
  4704. }
  4705. static int raid5_remove_disk(mddev_t *mddev, int number)
  4706. {
  4707. raid5_conf_t *conf = mddev->private;
  4708. int err = 0;
  4709. mdk_rdev_t *rdev;
  4710. struct disk_info *p = conf->disks + number;
  4711. print_raid5_conf(conf);
  4712. rdev = p->rdev;
  4713. if (rdev) {
  4714. if (number >= conf->raid_disks &&
  4715. conf->reshape_progress == MaxSector)
  4716. clear_bit(In_sync, &rdev->flags);
  4717. if (test_bit(In_sync, &rdev->flags) ||
  4718. atomic_read(&rdev->nr_pending)) {
  4719. err = -EBUSY;
  4720. goto abort;
  4721. }
  4722. /* Only remove non-faulty devices if recovery
  4723. * isn't possible.
  4724. */
  4725. if (!test_bit(Faulty, &rdev->flags) &&
  4726. !has_failed(conf) &&
  4727. number < conf->raid_disks) {
  4728. err = -EBUSY;
  4729. goto abort;
  4730. }
  4731. p->rdev = NULL;
  4732. synchronize_rcu();
  4733. if (atomic_read(&rdev->nr_pending)) {
  4734. /* lost the race, try later */
  4735. err = -EBUSY;
  4736. p->rdev = rdev;
  4737. }
  4738. }
  4739. abort:
  4740. print_raid5_conf(conf);
  4741. return err;
  4742. }
  4743. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  4744. {
  4745. raid5_conf_t *conf = mddev->private;
  4746. int err = -EEXIST;
  4747. int disk;
  4748. struct disk_info *p;
  4749. int first = 0;
  4750. int last = conf->raid_disks - 1;
  4751. if (has_failed(conf))
  4752. /* no point adding a device */
  4753. return -EINVAL;
  4754. if (rdev->raid_disk >= 0)
  4755. first = last = rdev->raid_disk;
  4756. /*
  4757. * find the disk ... but prefer rdev->saved_raid_disk
  4758. * if possible.
  4759. */
  4760. if (rdev->saved_raid_disk >= 0 &&
  4761. rdev->saved_raid_disk >= first &&
  4762. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4763. disk = rdev->saved_raid_disk;
  4764. else
  4765. disk = first;
  4766. for ( ; disk <= last ; disk++)
  4767. if ((p=conf->disks + disk)->rdev == NULL) {
  4768. clear_bit(In_sync, &rdev->flags);
  4769. rdev->raid_disk = disk;
  4770. err = 0;
  4771. if (rdev->saved_raid_disk != disk)
  4772. conf->fullsync = 1;
  4773. rcu_assign_pointer(p->rdev, rdev);
  4774. break;
  4775. }
  4776. print_raid5_conf(conf);
  4777. return err;
  4778. }
  4779. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  4780. {
  4781. /* no resync is happening, and there is enough space
  4782. * on all devices, so we can resize.
  4783. * We need to make sure resync covers any new space.
  4784. * If the array is shrinking we should possibly wait until
  4785. * any io in the removed space completes, but it hardly seems
  4786. * worth it.
  4787. */
  4788. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4789. md_set_array_sectors(mddev, raid5_size(mddev, sectors,
  4790. mddev->raid_disks));
  4791. if (mddev->array_sectors >
  4792. raid5_size(mddev, sectors, mddev->raid_disks))
  4793. return -EINVAL;
  4794. set_capacity(mddev->gendisk, mddev->array_sectors);
  4795. revalidate_disk(mddev->gendisk);
  4796. if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
  4797. mddev->recovery_cp = mddev->dev_sectors;
  4798. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4799. }
  4800. mddev->dev_sectors = sectors;
  4801. mddev->resync_max_sectors = sectors;
  4802. return 0;
  4803. }
  4804. static int check_stripe_cache(mddev_t *mddev)
  4805. {
  4806. /* Can only proceed if there are plenty of stripe_heads.
  4807. * We need a minimum of one full stripe,, and for sensible progress
  4808. * it is best to have about 4 times that.
  4809. * If we require 4 times, then the default 256 4K stripe_heads will
  4810. * allow for chunk sizes up to 256K, which is probably OK.
  4811. * If the chunk size is greater, user-space should request more
  4812. * stripe_heads first.
  4813. */
  4814. raid5_conf_t *conf = mddev->private;
  4815. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  4816. > conf->max_nr_stripes ||
  4817. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  4818. > conf->max_nr_stripes) {
  4819. printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  4820. mdname(mddev),
  4821. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  4822. / STRIPE_SIZE)*4);
  4823. return 0;
  4824. }
  4825. return 1;
  4826. }
  4827. static int check_reshape(mddev_t *mddev)
  4828. {
  4829. raid5_conf_t *conf = mddev->private;
  4830. if (mddev->delta_disks == 0 &&
  4831. mddev->new_layout == mddev->layout &&
  4832. mddev->new_chunk_sectors == mddev->chunk_sectors)
  4833. return 0; /* nothing to do */
  4834. if (mddev->bitmap)
  4835. /* Cannot grow a bitmap yet */
  4836. return -EBUSY;
  4837. if (has_failed(conf))
  4838. return -EINVAL;
  4839. if (mddev->delta_disks < 0) {
  4840. /* We might be able to shrink, but the devices must
  4841. * be made bigger first.
  4842. * For raid6, 4 is the minimum size.
  4843. * Otherwise 2 is the minimum
  4844. */
  4845. int min = 2;
  4846. if (mddev->level == 6)
  4847. min = 4;
  4848. if (mddev->raid_disks + mddev->delta_disks < min)
  4849. return -EINVAL;
  4850. }
  4851. if (!check_stripe_cache(mddev))
  4852. return -ENOSPC;
  4853. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4854. }
  4855. static int raid5_start_reshape(mddev_t *mddev)
  4856. {
  4857. raid5_conf_t *conf = mddev->private;
  4858. mdk_rdev_t *rdev;
  4859. int spares = 0;
  4860. unsigned long flags;
  4861. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4862. return -EBUSY;
  4863. if (!check_stripe_cache(mddev))
  4864. return -ENOSPC;
  4865. list_for_each_entry(rdev, &mddev->disks, same_set)
  4866. if (!test_bit(In_sync, &rdev->flags)
  4867. && !test_bit(Faulty, &rdev->flags))
  4868. spares++;
  4869. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4870. /* Not enough devices even to make a degraded array
  4871. * of that size
  4872. */
  4873. return -EINVAL;
  4874. /* Refuse to reduce size of the array. Any reductions in
  4875. * array size must be through explicit setting of array_size
  4876. * attribute.
  4877. */
  4878. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  4879. < mddev->array_sectors) {
  4880. printk(KERN_ERR "md/raid:%s: array size must be reduced "
  4881. "before number of disks\n", mdname(mddev));
  4882. return -EINVAL;
  4883. }
  4884. atomic_set(&conf->reshape_stripes, 0);
  4885. spin_lock_irq(&conf->device_lock);
  4886. conf->previous_raid_disks = conf->raid_disks;
  4887. conf->raid_disks += mddev->delta_disks;
  4888. conf->prev_chunk_sectors = conf->chunk_sectors;
  4889. conf->chunk_sectors = mddev->new_chunk_sectors;
  4890. conf->prev_algo = conf->algorithm;
  4891. conf->algorithm = mddev->new_layout;
  4892. if (mddev->delta_disks < 0)
  4893. conf->reshape_progress = raid5_size(mddev, 0, 0);
  4894. else
  4895. conf->reshape_progress = 0;
  4896. conf->reshape_safe = conf->reshape_progress;
  4897. conf->generation++;
  4898. spin_unlock_irq(&conf->device_lock);
  4899. /* Add some new drives, as many as will fit.
  4900. * We know there are enough to make the newly sized array work.
  4901. * Don't add devices if we are reducing the number of
  4902. * devices in the array. This is because it is not possible
  4903. * to correctly record the "partially reconstructed" state of
  4904. * such devices during the reshape and confusion could result.
  4905. */
  4906. if (mddev->delta_disks >= 0) {
  4907. int added_devices = 0;
  4908. list_for_each_entry(rdev, &mddev->disks, same_set)
  4909. if (rdev->raid_disk < 0 &&
  4910. !test_bit(Faulty, &rdev->flags)) {
  4911. if (raid5_add_disk(mddev, rdev) == 0) {
  4912. char nm[20];
  4913. if (rdev->raid_disk
  4914. >= conf->previous_raid_disks) {
  4915. set_bit(In_sync, &rdev->flags);
  4916. added_devices++;
  4917. } else
  4918. rdev->recovery_offset = 0;
  4919. sprintf(nm, "rd%d", rdev->raid_disk);
  4920. if (sysfs_create_link(&mddev->kobj,
  4921. &rdev->kobj, nm))
  4922. /* Failure here is OK */;
  4923. }
  4924. } else if (rdev->raid_disk >= conf->previous_raid_disks
  4925. && !test_bit(Faulty, &rdev->flags)) {
  4926. /* This is a spare that was manually added */
  4927. set_bit(In_sync, &rdev->flags);
  4928. added_devices++;
  4929. }
  4930. /* When a reshape changes the number of devices,
  4931. * ->degraded is measured against the larger of the
  4932. * pre and post number of devices.
  4933. */
  4934. spin_lock_irqsave(&conf->device_lock, flags);
  4935. mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
  4936. - added_devices;
  4937. spin_unlock_irqrestore(&conf->device_lock, flags);
  4938. }
  4939. mddev->raid_disks = conf->raid_disks;
  4940. mddev->reshape_position = conf->reshape_progress;
  4941. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4942. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4943. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4944. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4945. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4946. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4947. "reshape");
  4948. if (!mddev->sync_thread) {
  4949. mddev->recovery = 0;
  4950. spin_lock_irq(&conf->device_lock);
  4951. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4952. conf->reshape_progress = MaxSector;
  4953. spin_unlock_irq(&conf->device_lock);
  4954. return -EAGAIN;
  4955. }
  4956. conf->reshape_checkpoint = jiffies;
  4957. md_wakeup_thread(mddev->sync_thread);
  4958. md_new_event(mddev);
  4959. return 0;
  4960. }
  4961. /* This is called from the reshape thread and should make any
  4962. * changes needed in 'conf'
  4963. */
  4964. static void end_reshape(raid5_conf_t *conf)
  4965. {
  4966. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4967. spin_lock_irq(&conf->device_lock);
  4968. conf->previous_raid_disks = conf->raid_disks;
  4969. conf->reshape_progress = MaxSector;
  4970. spin_unlock_irq(&conf->device_lock);
  4971. wake_up(&conf->wait_for_overlap);
  4972. /* read-ahead size must cover two whole stripes, which is
  4973. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4974. */
  4975. if (conf->mddev->queue) {
  4976. int data_disks = conf->raid_disks - conf->max_degraded;
  4977. int stripe = data_disks * ((conf->chunk_sectors << 9)
  4978. / PAGE_SIZE);
  4979. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4980. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4981. }
  4982. }
  4983. }
  4984. /* This is called from the raid5d thread with mddev_lock held.
  4985. * It makes config changes to the device.
  4986. */
  4987. static void raid5_finish_reshape(mddev_t *mddev)
  4988. {
  4989. raid5_conf_t *conf = mddev->private;
  4990. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  4991. if (mddev->delta_disks > 0) {
  4992. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4993. set_capacity(mddev->gendisk, mddev->array_sectors);
  4994. revalidate_disk(mddev->gendisk);
  4995. } else {
  4996. int d;
  4997. mddev->degraded = conf->raid_disks;
  4998. for (d = 0; d < conf->raid_disks ; d++)
  4999. if (conf->disks[d].rdev &&
  5000. test_bit(In_sync,
  5001. &conf->disks[d].rdev->flags))
  5002. mddev->degraded--;
  5003. for (d = conf->raid_disks ;
  5004. d < conf->raid_disks - mddev->delta_disks;
  5005. d++) {
  5006. mdk_rdev_t *rdev = conf->disks[d].rdev;
  5007. if (rdev && raid5_remove_disk(mddev, d) == 0) {
  5008. char nm[20];
  5009. sprintf(nm, "rd%d", rdev->raid_disk);
  5010. sysfs_remove_link(&mddev->kobj, nm);
  5011. rdev->raid_disk = -1;
  5012. }
  5013. }
  5014. }
  5015. mddev->layout = conf->algorithm;
  5016. mddev->chunk_sectors = conf->chunk_sectors;
  5017. mddev->reshape_position = MaxSector;
  5018. mddev->delta_disks = 0;
  5019. }
  5020. }
  5021. static void raid5_quiesce(mddev_t *mddev, int state)
  5022. {
  5023. raid5_conf_t *conf = mddev->private;
  5024. switch(state) {
  5025. case 2: /* resume for a suspend */
  5026. wake_up(&conf->wait_for_overlap);
  5027. break;
  5028. case 1: /* stop all writes */
  5029. spin_lock_irq(&conf->device_lock);
  5030. /* '2' tells resync/reshape to pause so that all
  5031. * active stripes can drain
  5032. */
  5033. conf->quiesce = 2;
  5034. wait_event_lock_irq(conf->wait_for_stripe,
  5035. atomic_read(&conf->active_stripes) == 0 &&
  5036. atomic_read(&conf->active_aligned_reads) == 0,
  5037. conf->device_lock, /* nothing */);
  5038. conf->quiesce = 1;
  5039. spin_unlock_irq(&conf->device_lock);
  5040. /* allow reshape to continue */
  5041. wake_up(&conf->wait_for_overlap);
  5042. break;
  5043. case 0: /* re-enable writes */
  5044. spin_lock_irq(&conf->device_lock);
  5045. conf->quiesce = 0;
  5046. wake_up(&conf->wait_for_stripe);
  5047. wake_up(&conf->wait_for_overlap);
  5048. spin_unlock_irq(&conf->device_lock);
  5049. break;
  5050. }
  5051. }
  5052. static void *raid45_takeover_raid0(mddev_t *mddev, int level)
  5053. {
  5054. struct raid0_private_data *raid0_priv = mddev->private;
  5055. sector_t sectors;
  5056. /* for raid0 takeover only one zone is supported */
  5057. if (raid0_priv->nr_strip_zones > 1) {
  5058. printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  5059. mdname(mddev));
  5060. return ERR_PTR(-EINVAL);
  5061. }
  5062. sectors = raid0_priv->strip_zone[0].zone_end;
  5063. sector_div(sectors, raid0_priv->strip_zone[0].nb_dev);
  5064. mddev->dev_sectors = sectors;
  5065. mddev->new_level = level;
  5066. mddev->new_layout = ALGORITHM_PARITY_N;
  5067. mddev->new_chunk_sectors = mddev->chunk_sectors;
  5068. mddev->raid_disks += 1;
  5069. mddev->delta_disks = 1;
  5070. /* make sure it will be not marked as dirty */
  5071. mddev->recovery_cp = MaxSector;
  5072. return setup_conf(mddev);
  5073. }
  5074. static void *raid5_takeover_raid1(mddev_t *mddev)
  5075. {
  5076. int chunksect;
  5077. if (mddev->raid_disks != 2 ||
  5078. mddev->degraded > 1)
  5079. return ERR_PTR(-EINVAL);
  5080. /* Should check if there are write-behind devices? */
  5081. chunksect = 64*2; /* 64K by default */
  5082. /* The array must be an exact multiple of chunksize */
  5083. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  5084. chunksect >>= 1;
  5085. if ((chunksect<<9) < STRIPE_SIZE)
  5086. /* array size does not allow a suitable chunk size */
  5087. return ERR_PTR(-EINVAL);
  5088. mddev->new_level = 5;
  5089. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5090. mddev->new_chunk_sectors = chunksect;
  5091. return setup_conf(mddev);
  5092. }
  5093. static void *raid5_takeover_raid6(mddev_t *mddev)
  5094. {
  5095. int new_layout;
  5096. switch (mddev->layout) {
  5097. case ALGORITHM_LEFT_ASYMMETRIC_6:
  5098. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  5099. break;
  5100. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  5101. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  5102. break;
  5103. case ALGORITHM_LEFT_SYMMETRIC_6:
  5104. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5105. break;
  5106. case ALGORITHM_RIGHT_SYMMETRIC_6:
  5107. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  5108. break;
  5109. case ALGORITHM_PARITY_0_6:
  5110. new_layout = ALGORITHM_PARITY_0;
  5111. break;
  5112. case ALGORITHM_PARITY_N:
  5113. new_layout = ALGORITHM_PARITY_N;
  5114. break;
  5115. default:
  5116. return ERR_PTR(-EINVAL);
  5117. }
  5118. mddev->new_level = 5;
  5119. mddev->new_layout = new_layout;
  5120. mddev->delta_disks = -1;
  5121. mddev->raid_disks -= 1;
  5122. return setup_conf(mddev);
  5123. }
  5124. static int raid5_check_reshape(mddev_t *mddev)
  5125. {
  5126. /* For a 2-drive array, the layout and chunk size can be changed
  5127. * immediately as not restriping is needed.
  5128. * For larger arrays we record the new value - after validation
  5129. * to be used by a reshape pass.
  5130. */
  5131. raid5_conf_t *conf = mddev->private;
  5132. int new_chunk = mddev->new_chunk_sectors;
  5133. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  5134. return -EINVAL;
  5135. if (new_chunk > 0) {
  5136. if (!is_power_of_2(new_chunk))
  5137. return -EINVAL;
  5138. if (new_chunk < (PAGE_SIZE>>9))
  5139. return -EINVAL;
  5140. if (mddev->array_sectors & (new_chunk-1))
  5141. /* not factor of array size */
  5142. return -EINVAL;
  5143. }
  5144. /* They look valid */
  5145. if (mddev->raid_disks == 2) {
  5146. /* can make the change immediately */
  5147. if (mddev->new_layout >= 0) {
  5148. conf->algorithm = mddev->new_layout;
  5149. mddev->layout = mddev->new_layout;
  5150. }
  5151. if (new_chunk > 0) {
  5152. conf->chunk_sectors = new_chunk ;
  5153. mddev->chunk_sectors = new_chunk;
  5154. }
  5155. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5156. md_wakeup_thread(mddev->thread);
  5157. }
  5158. return check_reshape(mddev);
  5159. }
  5160. static int raid6_check_reshape(mddev_t *mddev)
  5161. {
  5162. int new_chunk = mddev->new_chunk_sectors;
  5163. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  5164. return -EINVAL;
  5165. if (new_chunk > 0) {
  5166. if (!is_power_of_2(new_chunk))
  5167. return -EINVAL;
  5168. if (new_chunk < (PAGE_SIZE >> 9))
  5169. return -EINVAL;
  5170. if (mddev->array_sectors & (new_chunk-1))
  5171. /* not factor of array size */
  5172. return -EINVAL;
  5173. }
  5174. /* They look valid */
  5175. return check_reshape(mddev);
  5176. }
  5177. static void *raid5_takeover(mddev_t *mddev)
  5178. {
  5179. /* raid5 can take over:
  5180. * raid0 - if there is only one strip zone - make it a raid4 layout
  5181. * raid1 - if there are two drives. We need to know the chunk size
  5182. * raid4 - trivial - just use a raid4 layout.
  5183. * raid6 - Providing it is a *_6 layout
  5184. */
  5185. if (mddev->level == 0)
  5186. return raid45_takeover_raid0(mddev, 5);
  5187. if (mddev->level == 1)
  5188. return raid5_takeover_raid1(mddev);
  5189. if (mddev->level == 4) {
  5190. mddev->new_layout = ALGORITHM_PARITY_N;
  5191. mddev->new_level = 5;
  5192. return setup_conf(mddev);
  5193. }
  5194. if (mddev->level == 6)
  5195. return raid5_takeover_raid6(mddev);
  5196. return ERR_PTR(-EINVAL);
  5197. }
  5198. static void *raid4_takeover(mddev_t *mddev)
  5199. {
  5200. /* raid4 can take over:
  5201. * raid0 - if there is only one strip zone
  5202. * raid5 - if layout is right
  5203. */
  5204. if (mddev->level == 0)
  5205. return raid45_takeover_raid0(mddev, 4);
  5206. if (mddev->level == 5 &&
  5207. mddev->layout == ALGORITHM_PARITY_N) {
  5208. mddev->new_layout = 0;
  5209. mddev->new_level = 4;
  5210. return setup_conf(mddev);
  5211. }
  5212. return ERR_PTR(-EINVAL);
  5213. }
  5214. static struct mdk_personality raid5_personality;
  5215. static void *raid6_takeover(mddev_t *mddev)
  5216. {
  5217. /* Currently can only take over a raid5. We map the
  5218. * personality to an equivalent raid6 personality
  5219. * with the Q block at the end.
  5220. */
  5221. int new_layout;
  5222. if (mddev->pers != &raid5_personality)
  5223. return ERR_PTR(-EINVAL);
  5224. if (mddev->degraded > 1)
  5225. return ERR_PTR(-EINVAL);
  5226. if (mddev->raid_disks > 253)
  5227. return ERR_PTR(-EINVAL);
  5228. if (mddev->raid_disks < 3)
  5229. return ERR_PTR(-EINVAL);
  5230. switch (mddev->layout) {
  5231. case ALGORITHM_LEFT_ASYMMETRIC:
  5232. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  5233. break;
  5234. case ALGORITHM_RIGHT_ASYMMETRIC:
  5235. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  5236. break;
  5237. case ALGORITHM_LEFT_SYMMETRIC:
  5238. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  5239. break;
  5240. case ALGORITHM_RIGHT_SYMMETRIC:
  5241. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  5242. break;
  5243. case ALGORITHM_PARITY_0:
  5244. new_layout = ALGORITHM_PARITY_0_6;
  5245. break;
  5246. case ALGORITHM_PARITY_N:
  5247. new_layout = ALGORITHM_PARITY_N;
  5248. break;
  5249. default:
  5250. return ERR_PTR(-EINVAL);
  5251. }
  5252. mddev->new_level = 6;
  5253. mddev->new_layout = new_layout;
  5254. mddev->delta_disks = 1;
  5255. mddev->raid_disks += 1;
  5256. return setup_conf(mddev);
  5257. }
  5258. static struct mdk_personality raid6_personality =
  5259. {
  5260. .name = "raid6",
  5261. .level = 6,
  5262. .owner = THIS_MODULE,
  5263. .make_request = make_request,
  5264. .run = run,
  5265. .stop = stop,
  5266. .status = status,
  5267. .error_handler = error,
  5268. .hot_add_disk = raid5_add_disk,
  5269. .hot_remove_disk= raid5_remove_disk,
  5270. .spare_active = raid5_spare_active,
  5271. .sync_request = sync_request,
  5272. .resize = raid5_resize,
  5273. .size = raid5_size,
  5274. .check_reshape = raid6_check_reshape,
  5275. .start_reshape = raid5_start_reshape,
  5276. .finish_reshape = raid5_finish_reshape,
  5277. .quiesce = raid5_quiesce,
  5278. .takeover = raid6_takeover,
  5279. };
  5280. static struct mdk_personality raid5_personality =
  5281. {
  5282. .name = "raid5",
  5283. .level = 5,
  5284. .owner = THIS_MODULE,
  5285. .make_request = make_request,
  5286. .run = run,
  5287. .stop = stop,
  5288. .status = status,
  5289. .error_handler = error,
  5290. .hot_add_disk = raid5_add_disk,
  5291. .hot_remove_disk= raid5_remove_disk,
  5292. .spare_active = raid5_spare_active,
  5293. .sync_request = sync_request,
  5294. .resize = raid5_resize,
  5295. .size = raid5_size,
  5296. .check_reshape = raid5_check_reshape,
  5297. .start_reshape = raid5_start_reshape,
  5298. .finish_reshape = raid5_finish_reshape,
  5299. .quiesce = raid5_quiesce,
  5300. .takeover = raid5_takeover,
  5301. };
  5302. static struct mdk_personality raid4_personality =
  5303. {
  5304. .name = "raid4",
  5305. .level = 4,
  5306. .owner = THIS_MODULE,
  5307. .make_request = make_request,
  5308. .run = run,
  5309. .stop = stop,
  5310. .status = status,
  5311. .error_handler = error,
  5312. .hot_add_disk = raid5_add_disk,
  5313. .hot_remove_disk= raid5_remove_disk,
  5314. .spare_active = raid5_spare_active,
  5315. .sync_request = sync_request,
  5316. .resize = raid5_resize,
  5317. .size = raid5_size,
  5318. .check_reshape = raid5_check_reshape,
  5319. .start_reshape = raid5_start_reshape,
  5320. .finish_reshape = raid5_finish_reshape,
  5321. .quiesce = raid5_quiesce,
  5322. .takeover = raid4_takeover,
  5323. };
  5324. static int __init raid5_init(void)
  5325. {
  5326. register_md_personality(&raid6_personality);
  5327. register_md_personality(&raid5_personality);
  5328. register_md_personality(&raid4_personality);
  5329. return 0;
  5330. }
  5331. static void raid5_exit(void)
  5332. {
  5333. unregister_md_personality(&raid6_personality);
  5334. unregister_md_personality(&raid5_personality);
  5335. unregister_md_personality(&raid4_personality);
  5336. }
  5337. module_init(raid5_init);
  5338. module_exit(raid5_exit);
  5339. MODULE_LICENSE("GPL");
  5340. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  5341. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  5342. MODULE_ALIAS("md-raid5");
  5343. MODULE_ALIAS("md-raid4");
  5344. MODULE_ALIAS("md-level-5");
  5345. MODULE_ALIAS("md-level-4");
  5346. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  5347. MODULE_ALIAS("md-raid6");
  5348. MODULE_ALIAS("md-level-6");
  5349. /* This used to be two separate modules, they were: */
  5350. MODULE_ALIAS("raid5");
  5351. MODULE_ALIAS("raid6");