raid5.c 196 KB

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