raid5.c 233 KB

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