raid5.c 200 KB

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