raid5.c 197 KB

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