raid5.c 171 KB

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