raid5.c 196 KB

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