raid5.c 197 KB

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