raid5.c 197 KB

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