raid5.c 190 KB

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