raid5.c 173 KB

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