raid5.c 220 KB

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