raid5.c 240 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528
  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 <linux/flex_array.h>
  56. #include <trace/events/block.h>
  57. #include <linux/list_sort.h>
  58. #include "md.h"
  59. #include "raid5.h"
  60. #include "raid0.h"
  61. #include "md-bitmap.h"
  62. #include "raid5-log.h"
  63. #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
  64. #define cpu_to_group(cpu) cpu_to_node(cpu)
  65. #define ANY_GROUP NUMA_NO_NODE
  66. static bool devices_handle_discard_safely = false;
  67. module_param(devices_handle_discard_safely, bool, 0644);
  68. MODULE_PARM_DESC(devices_handle_discard_safely,
  69. "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
  70. static struct workqueue_struct *raid5_wq;
  71. static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
  72. {
  73. int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
  74. return &conf->stripe_hashtbl[hash];
  75. }
  76. static inline int stripe_hash_locks_hash(sector_t sect)
  77. {
  78. return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
  79. }
  80. static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
  81. {
  82. spin_lock_irq(conf->hash_locks + hash);
  83. spin_lock(&conf->device_lock);
  84. }
  85. static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
  86. {
  87. spin_unlock(&conf->device_lock);
  88. spin_unlock_irq(conf->hash_locks + hash);
  89. }
  90. static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
  91. {
  92. int i;
  93. spin_lock_irq(conf->hash_locks);
  94. for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
  95. spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
  96. spin_lock(&conf->device_lock);
  97. }
  98. static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
  99. {
  100. int i;
  101. spin_unlock(&conf->device_lock);
  102. for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
  103. spin_unlock(conf->hash_locks + i);
  104. spin_unlock_irq(conf->hash_locks);
  105. }
  106. /* Find first data disk in a raid6 stripe */
  107. static inline int raid6_d0(struct stripe_head *sh)
  108. {
  109. if (sh->ddf_layout)
  110. /* ddf always start from first device */
  111. return 0;
  112. /* md starts just after Q block */
  113. if (sh->qd_idx == sh->disks - 1)
  114. return 0;
  115. else
  116. return sh->qd_idx + 1;
  117. }
  118. static inline int raid6_next_disk(int disk, int raid_disks)
  119. {
  120. disk++;
  121. return (disk < raid_disks) ? disk : 0;
  122. }
  123. /* When walking through the disks in a raid5, starting at raid6_d0,
  124. * We need to map each disk to a 'slot', where the data disks are slot
  125. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  126. * is raid_disks-1. This help does that mapping.
  127. */
  128. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  129. int *count, int syndrome_disks)
  130. {
  131. int slot = *count;
  132. if (sh->ddf_layout)
  133. (*count)++;
  134. if (idx == sh->pd_idx)
  135. return syndrome_disks;
  136. if (idx == sh->qd_idx)
  137. return syndrome_disks + 1;
  138. if (!sh->ddf_layout)
  139. (*count)++;
  140. return slot;
  141. }
  142. static void print_raid5_conf (struct r5conf *conf);
  143. static int stripe_operations_active(struct stripe_head *sh)
  144. {
  145. return sh->check_state || sh->reconstruct_state ||
  146. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  147. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  148. }
  149. static bool stripe_is_lowprio(struct stripe_head *sh)
  150. {
  151. return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
  152. test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
  153. !test_bit(STRIPE_R5C_CACHING, &sh->state);
  154. }
  155. static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
  156. {
  157. struct r5conf *conf = sh->raid_conf;
  158. struct r5worker_group *group;
  159. int thread_cnt;
  160. int i, cpu = sh->cpu;
  161. if (!cpu_online(cpu)) {
  162. cpu = cpumask_any(cpu_online_mask);
  163. sh->cpu = cpu;
  164. }
  165. if (list_empty(&sh->lru)) {
  166. struct r5worker_group *group;
  167. group = conf->worker_groups + cpu_to_group(cpu);
  168. if (stripe_is_lowprio(sh))
  169. list_add_tail(&sh->lru, &group->loprio_list);
  170. else
  171. list_add_tail(&sh->lru, &group->handle_list);
  172. group->stripes_cnt++;
  173. sh->group = group;
  174. }
  175. if (conf->worker_cnt_per_group == 0) {
  176. md_wakeup_thread(conf->mddev->thread);
  177. return;
  178. }
  179. group = conf->worker_groups + cpu_to_group(sh->cpu);
  180. group->workers[0].working = true;
  181. /* at least one worker should run to avoid race */
  182. queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
  183. thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
  184. /* wakeup more workers */
  185. for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
  186. if (group->workers[i].working == false) {
  187. group->workers[i].working = true;
  188. queue_work_on(sh->cpu, raid5_wq,
  189. &group->workers[i].work);
  190. thread_cnt--;
  191. }
  192. }
  193. }
  194. static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
  195. struct list_head *temp_inactive_list)
  196. {
  197. int i;
  198. int injournal = 0; /* number of date pages with R5_InJournal */
  199. BUG_ON(!list_empty(&sh->lru));
  200. BUG_ON(atomic_read(&conf->active_stripes)==0);
  201. if (r5c_is_writeback(conf->log))
  202. for (i = sh->disks; i--; )
  203. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  204. injournal++;
  205. /*
  206. * In the following cases, the stripe cannot be released to cached
  207. * lists. Therefore, we make the stripe write out and set
  208. * STRIPE_HANDLE:
  209. * 1. when quiesce in r5c write back;
  210. * 2. when resync is requested fot the stripe.
  211. */
  212. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
  213. (conf->quiesce && r5c_is_writeback(conf->log) &&
  214. !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
  215. if (test_bit(STRIPE_R5C_CACHING, &sh->state))
  216. r5c_make_stripe_write_out(sh);
  217. set_bit(STRIPE_HANDLE, &sh->state);
  218. }
  219. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  220. if (test_bit(STRIPE_DELAYED, &sh->state) &&
  221. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  222. list_add_tail(&sh->lru, &conf->delayed_list);
  223. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  224. sh->bm_seq - conf->seq_write > 0)
  225. list_add_tail(&sh->lru, &conf->bitmap_list);
  226. else {
  227. clear_bit(STRIPE_DELAYED, &sh->state);
  228. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  229. if (conf->worker_cnt_per_group == 0) {
  230. if (stripe_is_lowprio(sh))
  231. list_add_tail(&sh->lru,
  232. &conf->loprio_list);
  233. else
  234. list_add_tail(&sh->lru,
  235. &conf->handle_list);
  236. } else {
  237. raid5_wakeup_stripe_thread(sh);
  238. return;
  239. }
  240. }
  241. md_wakeup_thread(conf->mddev->thread);
  242. } else {
  243. BUG_ON(stripe_operations_active(sh));
  244. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  245. if (atomic_dec_return(&conf->preread_active_stripes)
  246. < IO_THRESHOLD)
  247. md_wakeup_thread(conf->mddev->thread);
  248. atomic_dec(&conf->active_stripes);
  249. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  250. if (!r5c_is_writeback(conf->log))
  251. list_add_tail(&sh->lru, temp_inactive_list);
  252. else {
  253. WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
  254. if (injournal == 0)
  255. list_add_tail(&sh->lru, temp_inactive_list);
  256. else if (injournal == conf->raid_disks - conf->max_degraded) {
  257. /* full stripe */
  258. if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
  259. atomic_inc(&conf->r5c_cached_full_stripes);
  260. if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
  261. atomic_dec(&conf->r5c_cached_partial_stripes);
  262. list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
  263. r5c_check_cached_full_stripe(conf);
  264. } else
  265. /*
  266. * STRIPE_R5C_PARTIAL_STRIPE is set in
  267. * r5c_try_caching_write(). No need to
  268. * set it again.
  269. */
  270. list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
  271. }
  272. }
  273. }
  274. }
  275. static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
  276. struct list_head *temp_inactive_list)
  277. {
  278. if (atomic_dec_and_test(&sh->count))
  279. do_release_stripe(conf, sh, temp_inactive_list);
  280. }
  281. /*
  282. * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
  283. *
  284. * Be careful: Only one task can add/delete stripes from temp_inactive_list at
  285. * given time. Adding stripes only takes device lock, while deleting stripes
  286. * only takes hash lock.
  287. */
  288. static void release_inactive_stripe_list(struct r5conf *conf,
  289. struct list_head *temp_inactive_list,
  290. int hash)
  291. {
  292. int size;
  293. bool do_wakeup = false;
  294. unsigned long flags;
  295. if (hash == NR_STRIPE_HASH_LOCKS) {
  296. size = NR_STRIPE_HASH_LOCKS;
  297. hash = NR_STRIPE_HASH_LOCKS - 1;
  298. } else
  299. size = 1;
  300. while (size) {
  301. struct list_head *list = &temp_inactive_list[size - 1];
  302. /*
  303. * We don't hold any lock here yet, raid5_get_active_stripe() might
  304. * remove stripes from the list
  305. */
  306. if (!list_empty_careful(list)) {
  307. spin_lock_irqsave(conf->hash_locks + hash, flags);
  308. if (list_empty(conf->inactive_list + hash) &&
  309. !list_empty(list))
  310. atomic_dec(&conf->empty_inactive_list_nr);
  311. list_splice_tail_init(list, conf->inactive_list + hash);
  312. do_wakeup = true;
  313. spin_unlock_irqrestore(conf->hash_locks + hash, flags);
  314. }
  315. size--;
  316. hash--;
  317. }
  318. if (do_wakeup) {
  319. wake_up(&conf->wait_for_stripe);
  320. if (atomic_read(&conf->active_stripes) == 0)
  321. wake_up(&conf->wait_for_quiescent);
  322. if (conf->retry_read_aligned)
  323. md_wakeup_thread(conf->mddev->thread);
  324. }
  325. }
  326. /* should hold conf->device_lock already */
  327. static int release_stripe_list(struct r5conf *conf,
  328. struct list_head *temp_inactive_list)
  329. {
  330. struct stripe_head *sh, *t;
  331. int count = 0;
  332. struct llist_node *head;
  333. head = llist_del_all(&conf->released_stripes);
  334. head = llist_reverse_order(head);
  335. llist_for_each_entry_safe(sh, t, head, release_list) {
  336. int hash;
  337. /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
  338. smp_mb();
  339. clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
  340. /*
  341. * Don't worry the bit is set here, because if the bit is set
  342. * again, the count is always > 1. This is true for
  343. * STRIPE_ON_UNPLUG_LIST bit too.
  344. */
  345. hash = sh->hash_lock_index;
  346. __release_stripe(conf, sh, &temp_inactive_list[hash]);
  347. count++;
  348. }
  349. return count;
  350. }
  351. void raid5_release_stripe(struct stripe_head *sh)
  352. {
  353. struct r5conf *conf = sh->raid_conf;
  354. unsigned long flags;
  355. struct list_head list;
  356. int hash;
  357. bool wakeup;
  358. /* Avoid release_list until the last reference.
  359. */
  360. if (atomic_add_unless(&sh->count, -1, 1))
  361. return;
  362. if (unlikely(!conf->mddev->thread) ||
  363. test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
  364. goto slow_path;
  365. wakeup = llist_add(&sh->release_list, &conf->released_stripes);
  366. if (wakeup)
  367. md_wakeup_thread(conf->mddev->thread);
  368. return;
  369. slow_path:
  370. /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
  371. if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
  372. INIT_LIST_HEAD(&list);
  373. hash = sh->hash_lock_index;
  374. do_release_stripe(conf, sh, &list);
  375. spin_unlock_irqrestore(&conf->device_lock, flags);
  376. release_inactive_stripe_list(conf, &list, hash);
  377. }
  378. }
  379. static inline void remove_hash(struct stripe_head *sh)
  380. {
  381. pr_debug("remove_hash(), stripe %llu\n",
  382. (unsigned long long)sh->sector);
  383. hlist_del_init(&sh->hash);
  384. }
  385. static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
  386. {
  387. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  388. pr_debug("insert_hash(), stripe %llu\n",
  389. (unsigned long long)sh->sector);
  390. hlist_add_head(&sh->hash, hp);
  391. }
  392. /* find an idle stripe, make sure it is unhashed, and return it. */
  393. static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
  394. {
  395. struct stripe_head *sh = NULL;
  396. struct list_head *first;
  397. if (list_empty(conf->inactive_list + hash))
  398. goto out;
  399. first = (conf->inactive_list + hash)->next;
  400. sh = list_entry(first, struct stripe_head, lru);
  401. list_del_init(first);
  402. remove_hash(sh);
  403. atomic_inc(&conf->active_stripes);
  404. BUG_ON(hash != sh->hash_lock_index);
  405. if (list_empty(conf->inactive_list + hash))
  406. atomic_inc(&conf->empty_inactive_list_nr);
  407. out:
  408. return sh;
  409. }
  410. static void shrink_buffers(struct stripe_head *sh)
  411. {
  412. struct page *p;
  413. int i;
  414. int num = sh->raid_conf->pool_size;
  415. for (i = 0; i < num ; i++) {
  416. WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
  417. p = sh->dev[i].page;
  418. if (!p)
  419. continue;
  420. sh->dev[i].page = NULL;
  421. put_page(p);
  422. }
  423. }
  424. static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
  425. {
  426. int i;
  427. int num = sh->raid_conf->pool_size;
  428. for (i = 0; i < num; i++) {
  429. struct page *page;
  430. if (!(page = alloc_page(gfp))) {
  431. return 1;
  432. }
  433. sh->dev[i].page = page;
  434. sh->dev[i].orig_page = page;
  435. }
  436. return 0;
  437. }
  438. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  439. struct stripe_head *sh);
  440. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  441. {
  442. struct r5conf *conf = sh->raid_conf;
  443. int i, seq;
  444. BUG_ON(atomic_read(&sh->count) != 0);
  445. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  446. BUG_ON(stripe_operations_active(sh));
  447. BUG_ON(sh->batch_head);
  448. pr_debug("init_stripe called, stripe %llu\n",
  449. (unsigned long long)sector);
  450. retry:
  451. seq = read_seqcount_begin(&conf->gen_lock);
  452. sh->generation = conf->generation - previous;
  453. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  454. sh->sector = sector;
  455. stripe_set_idx(sector, conf, previous, sh);
  456. sh->state = 0;
  457. for (i = sh->disks; i--; ) {
  458. struct r5dev *dev = &sh->dev[i];
  459. if (dev->toread || dev->read || dev->towrite || dev->written ||
  460. test_bit(R5_LOCKED, &dev->flags)) {
  461. pr_err("sector=%llx i=%d %p %p %p %p %d\n",
  462. (unsigned long long)sh->sector, i, dev->toread,
  463. dev->read, dev->towrite, dev->written,
  464. test_bit(R5_LOCKED, &dev->flags));
  465. WARN_ON(1);
  466. }
  467. dev->flags = 0;
  468. dev->sector = raid5_compute_blocknr(sh, i, previous);
  469. }
  470. if (read_seqcount_retry(&conf->gen_lock, seq))
  471. goto retry;
  472. sh->overwrite_disks = 0;
  473. insert_hash(conf, sh);
  474. sh->cpu = smp_processor_id();
  475. set_bit(STRIPE_BATCH_READY, &sh->state);
  476. }
  477. static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
  478. short generation)
  479. {
  480. struct stripe_head *sh;
  481. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  482. hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
  483. if (sh->sector == sector && sh->generation == generation)
  484. return sh;
  485. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  486. return NULL;
  487. }
  488. /*
  489. * Need to check if array has failed when deciding whether to:
  490. * - start an array
  491. * - remove non-faulty devices
  492. * - add a spare
  493. * - allow a reshape
  494. * This determination is simple when no reshape is happening.
  495. * However if there is a reshape, we need to carefully check
  496. * both the before and after sections.
  497. * This is because some failed devices may only affect one
  498. * of the two sections, and some non-in_sync devices may
  499. * be insync in the section most affected by failed devices.
  500. */
  501. int raid5_calc_degraded(struct r5conf *conf)
  502. {
  503. int degraded, degraded2;
  504. int i;
  505. rcu_read_lock();
  506. degraded = 0;
  507. for (i = 0; i < conf->previous_raid_disks; i++) {
  508. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  509. if (rdev && test_bit(Faulty, &rdev->flags))
  510. rdev = rcu_dereference(conf->disks[i].replacement);
  511. if (!rdev || test_bit(Faulty, &rdev->flags))
  512. degraded++;
  513. else if (test_bit(In_sync, &rdev->flags))
  514. ;
  515. else
  516. /* not in-sync or faulty.
  517. * If the reshape increases the number of devices,
  518. * this is being recovered by the reshape, so
  519. * this 'previous' section is not in_sync.
  520. * If the number of devices is being reduced however,
  521. * the device can only be part of the array if
  522. * we are reverting a reshape, so this section will
  523. * be in-sync.
  524. */
  525. if (conf->raid_disks >= conf->previous_raid_disks)
  526. degraded++;
  527. }
  528. rcu_read_unlock();
  529. if (conf->raid_disks == conf->previous_raid_disks)
  530. return degraded;
  531. rcu_read_lock();
  532. degraded2 = 0;
  533. for (i = 0; i < conf->raid_disks; i++) {
  534. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  535. if (rdev && test_bit(Faulty, &rdev->flags))
  536. rdev = rcu_dereference(conf->disks[i].replacement);
  537. if (!rdev || test_bit(Faulty, &rdev->flags))
  538. degraded2++;
  539. else if (test_bit(In_sync, &rdev->flags))
  540. ;
  541. else
  542. /* not in-sync or faulty.
  543. * If reshape increases the number of devices, this
  544. * section has already been recovered, else it
  545. * almost certainly hasn't.
  546. */
  547. if (conf->raid_disks <= conf->previous_raid_disks)
  548. degraded2++;
  549. }
  550. rcu_read_unlock();
  551. if (degraded2 > degraded)
  552. return degraded2;
  553. return degraded;
  554. }
  555. static int has_failed(struct r5conf *conf)
  556. {
  557. int degraded;
  558. if (conf->mddev->reshape_position == MaxSector)
  559. return conf->mddev->degraded > conf->max_degraded;
  560. degraded = raid5_calc_degraded(conf);
  561. if (degraded > conf->max_degraded)
  562. return 1;
  563. return 0;
  564. }
  565. struct stripe_head *
  566. raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
  567. int previous, int noblock, int noquiesce)
  568. {
  569. struct stripe_head *sh;
  570. int hash = stripe_hash_locks_hash(sector);
  571. int inc_empty_inactive_list_flag;
  572. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  573. spin_lock_irq(conf->hash_locks + hash);
  574. do {
  575. wait_event_lock_irq(conf->wait_for_quiescent,
  576. conf->quiesce == 0 || noquiesce,
  577. *(conf->hash_locks + hash));
  578. sh = __find_stripe(conf, sector, conf->generation - previous);
  579. if (!sh) {
  580. if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
  581. sh = get_free_stripe(conf, hash);
  582. if (!sh && !test_bit(R5_DID_ALLOC,
  583. &conf->cache_state))
  584. set_bit(R5_ALLOC_MORE,
  585. &conf->cache_state);
  586. }
  587. if (noblock && sh == NULL)
  588. break;
  589. r5c_check_stripe_cache_usage(conf);
  590. if (!sh) {
  591. set_bit(R5_INACTIVE_BLOCKED,
  592. &conf->cache_state);
  593. r5l_wake_reclaim(conf->log, 0);
  594. wait_event_lock_irq(
  595. conf->wait_for_stripe,
  596. !list_empty(conf->inactive_list + hash) &&
  597. (atomic_read(&conf->active_stripes)
  598. < (conf->max_nr_stripes * 3 / 4)
  599. || !test_bit(R5_INACTIVE_BLOCKED,
  600. &conf->cache_state)),
  601. *(conf->hash_locks + hash));
  602. clear_bit(R5_INACTIVE_BLOCKED,
  603. &conf->cache_state);
  604. } else {
  605. init_stripe(sh, sector, previous);
  606. atomic_inc(&sh->count);
  607. }
  608. } else if (!atomic_inc_not_zero(&sh->count)) {
  609. spin_lock(&conf->device_lock);
  610. if (!atomic_read(&sh->count)) {
  611. if (!test_bit(STRIPE_HANDLE, &sh->state))
  612. atomic_inc(&conf->active_stripes);
  613. BUG_ON(list_empty(&sh->lru) &&
  614. !test_bit(STRIPE_EXPANDING, &sh->state));
  615. inc_empty_inactive_list_flag = 0;
  616. if (!list_empty(conf->inactive_list + hash))
  617. inc_empty_inactive_list_flag = 1;
  618. list_del_init(&sh->lru);
  619. if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
  620. atomic_inc(&conf->empty_inactive_list_nr);
  621. if (sh->group) {
  622. sh->group->stripes_cnt--;
  623. sh->group = NULL;
  624. }
  625. }
  626. atomic_inc(&sh->count);
  627. spin_unlock(&conf->device_lock);
  628. }
  629. } while (sh == NULL);
  630. spin_unlock_irq(conf->hash_locks + hash);
  631. return sh;
  632. }
  633. static bool is_full_stripe_write(struct stripe_head *sh)
  634. {
  635. BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
  636. return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
  637. }
  638. static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
  639. {
  640. if (sh1 > sh2) {
  641. spin_lock_irq(&sh2->stripe_lock);
  642. spin_lock_nested(&sh1->stripe_lock, 1);
  643. } else {
  644. spin_lock_irq(&sh1->stripe_lock);
  645. spin_lock_nested(&sh2->stripe_lock, 1);
  646. }
  647. }
  648. static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
  649. {
  650. spin_unlock(&sh1->stripe_lock);
  651. spin_unlock_irq(&sh2->stripe_lock);
  652. }
  653. /* Only freshly new full stripe normal write stripe can be added to a batch list */
  654. static bool stripe_can_batch(struct stripe_head *sh)
  655. {
  656. struct r5conf *conf = sh->raid_conf;
  657. if (raid5_has_log(conf) || raid5_has_ppl(conf))
  658. return false;
  659. return test_bit(STRIPE_BATCH_READY, &sh->state) &&
  660. !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
  661. is_full_stripe_write(sh);
  662. }
  663. /* we only do back search */
  664. static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
  665. {
  666. struct stripe_head *head;
  667. sector_t head_sector, tmp_sec;
  668. int hash;
  669. int dd_idx;
  670. int inc_empty_inactive_list_flag;
  671. /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
  672. tmp_sec = sh->sector;
  673. if (!sector_div(tmp_sec, conf->chunk_sectors))
  674. return;
  675. head_sector = sh->sector - STRIPE_SECTORS;
  676. hash = stripe_hash_locks_hash(head_sector);
  677. spin_lock_irq(conf->hash_locks + hash);
  678. head = __find_stripe(conf, head_sector, conf->generation);
  679. if (head && !atomic_inc_not_zero(&head->count)) {
  680. spin_lock(&conf->device_lock);
  681. if (!atomic_read(&head->count)) {
  682. if (!test_bit(STRIPE_HANDLE, &head->state))
  683. atomic_inc(&conf->active_stripes);
  684. BUG_ON(list_empty(&head->lru) &&
  685. !test_bit(STRIPE_EXPANDING, &head->state));
  686. inc_empty_inactive_list_flag = 0;
  687. if (!list_empty(conf->inactive_list + hash))
  688. inc_empty_inactive_list_flag = 1;
  689. list_del_init(&head->lru);
  690. if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
  691. atomic_inc(&conf->empty_inactive_list_nr);
  692. if (head->group) {
  693. head->group->stripes_cnt--;
  694. head->group = NULL;
  695. }
  696. }
  697. atomic_inc(&head->count);
  698. spin_unlock(&conf->device_lock);
  699. }
  700. spin_unlock_irq(conf->hash_locks + hash);
  701. if (!head)
  702. return;
  703. if (!stripe_can_batch(head))
  704. goto out;
  705. lock_two_stripes(head, sh);
  706. /* clear_batch_ready clear the flag */
  707. if (!stripe_can_batch(head) || !stripe_can_batch(sh))
  708. goto unlock_out;
  709. if (sh->batch_head)
  710. goto unlock_out;
  711. dd_idx = 0;
  712. while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
  713. dd_idx++;
  714. if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
  715. bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
  716. goto unlock_out;
  717. if (head->batch_head) {
  718. spin_lock(&head->batch_head->batch_lock);
  719. /* This batch list is already running */
  720. if (!stripe_can_batch(head)) {
  721. spin_unlock(&head->batch_head->batch_lock);
  722. goto unlock_out;
  723. }
  724. /*
  725. * We must assign batch_head of this stripe within the
  726. * batch_lock, otherwise clear_batch_ready of batch head
  727. * stripe could clear BATCH_READY bit of this stripe and
  728. * this stripe->batch_head doesn't get assigned, which
  729. * could confuse clear_batch_ready for this stripe
  730. */
  731. sh->batch_head = head->batch_head;
  732. /*
  733. * at this point, head's BATCH_READY could be cleared, but we
  734. * can still add the stripe to batch list
  735. */
  736. list_add(&sh->batch_list, &head->batch_list);
  737. spin_unlock(&head->batch_head->batch_lock);
  738. } else {
  739. head->batch_head = head;
  740. sh->batch_head = head->batch_head;
  741. spin_lock(&head->batch_lock);
  742. list_add_tail(&sh->batch_list, &head->batch_list);
  743. spin_unlock(&head->batch_lock);
  744. }
  745. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  746. if (atomic_dec_return(&conf->preread_active_stripes)
  747. < IO_THRESHOLD)
  748. md_wakeup_thread(conf->mddev->thread);
  749. if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
  750. int seq = sh->bm_seq;
  751. if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
  752. sh->batch_head->bm_seq > seq)
  753. seq = sh->batch_head->bm_seq;
  754. set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
  755. sh->batch_head->bm_seq = seq;
  756. }
  757. atomic_inc(&sh->count);
  758. unlock_out:
  759. unlock_two_stripes(head, sh);
  760. out:
  761. raid5_release_stripe(head);
  762. }
  763. /* Determine if 'data_offset' or 'new_data_offset' should be used
  764. * in this stripe_head.
  765. */
  766. static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
  767. {
  768. sector_t progress = conf->reshape_progress;
  769. /* Need a memory barrier to make sure we see the value
  770. * of conf->generation, or ->data_offset that was set before
  771. * reshape_progress was updated.
  772. */
  773. smp_rmb();
  774. if (progress == MaxSector)
  775. return 0;
  776. if (sh->generation == conf->generation - 1)
  777. return 0;
  778. /* We are in a reshape, and this is a new-generation stripe,
  779. * so use new_data_offset.
  780. */
  781. return 1;
  782. }
  783. static void dispatch_bio_list(struct bio_list *tmp)
  784. {
  785. struct bio *bio;
  786. while ((bio = bio_list_pop(tmp)))
  787. generic_make_request(bio);
  788. }
  789. static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
  790. {
  791. const struct r5pending_data *da = list_entry(a,
  792. struct r5pending_data, sibling);
  793. const struct r5pending_data *db = list_entry(b,
  794. struct r5pending_data, sibling);
  795. if (da->sector > db->sector)
  796. return 1;
  797. if (da->sector < db->sector)
  798. return -1;
  799. return 0;
  800. }
  801. static void dispatch_defer_bios(struct r5conf *conf, int target,
  802. struct bio_list *list)
  803. {
  804. struct r5pending_data *data;
  805. struct list_head *first, *next = NULL;
  806. int cnt = 0;
  807. if (conf->pending_data_cnt == 0)
  808. return;
  809. list_sort(NULL, &conf->pending_list, cmp_stripe);
  810. first = conf->pending_list.next;
  811. /* temporarily move the head */
  812. if (conf->next_pending_data)
  813. list_move_tail(&conf->pending_list,
  814. &conf->next_pending_data->sibling);
  815. while (!list_empty(&conf->pending_list)) {
  816. data = list_first_entry(&conf->pending_list,
  817. struct r5pending_data, sibling);
  818. if (&data->sibling == first)
  819. first = data->sibling.next;
  820. next = data->sibling.next;
  821. bio_list_merge(list, &data->bios);
  822. list_move(&data->sibling, &conf->free_list);
  823. cnt++;
  824. if (cnt >= target)
  825. break;
  826. }
  827. conf->pending_data_cnt -= cnt;
  828. BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
  829. if (next != &conf->pending_list)
  830. conf->next_pending_data = list_entry(next,
  831. struct r5pending_data, sibling);
  832. else
  833. conf->next_pending_data = NULL;
  834. /* list isn't empty */
  835. if (first != &conf->pending_list)
  836. list_move_tail(&conf->pending_list, first);
  837. }
  838. static void flush_deferred_bios(struct r5conf *conf)
  839. {
  840. struct bio_list tmp = BIO_EMPTY_LIST;
  841. if (conf->pending_data_cnt == 0)
  842. return;
  843. spin_lock(&conf->pending_bios_lock);
  844. dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
  845. BUG_ON(conf->pending_data_cnt != 0);
  846. spin_unlock(&conf->pending_bios_lock);
  847. dispatch_bio_list(&tmp);
  848. }
  849. static void defer_issue_bios(struct r5conf *conf, sector_t sector,
  850. struct bio_list *bios)
  851. {
  852. struct bio_list tmp = BIO_EMPTY_LIST;
  853. struct r5pending_data *ent;
  854. spin_lock(&conf->pending_bios_lock);
  855. ent = list_first_entry(&conf->free_list, struct r5pending_data,
  856. sibling);
  857. list_move_tail(&ent->sibling, &conf->pending_list);
  858. ent->sector = sector;
  859. bio_list_init(&ent->bios);
  860. bio_list_merge(&ent->bios, bios);
  861. conf->pending_data_cnt++;
  862. if (conf->pending_data_cnt >= PENDING_IO_MAX)
  863. dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
  864. spin_unlock(&conf->pending_bios_lock);
  865. dispatch_bio_list(&tmp);
  866. }
  867. static void
  868. raid5_end_read_request(struct bio *bi);
  869. static void
  870. raid5_end_write_request(struct bio *bi);
  871. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  872. {
  873. struct r5conf *conf = sh->raid_conf;
  874. int i, disks = sh->disks;
  875. struct stripe_head *head_sh = sh;
  876. struct bio_list pending_bios = BIO_EMPTY_LIST;
  877. bool should_defer;
  878. might_sleep();
  879. if (log_stripe(sh, s) == 0)
  880. return;
  881. should_defer = conf->batch_bio_dispatch && conf->group_cnt;
  882. for (i = disks; i--; ) {
  883. int op, op_flags = 0;
  884. int replace_only = 0;
  885. struct bio *bi, *rbi;
  886. struct md_rdev *rdev, *rrdev = NULL;
  887. sh = head_sh;
  888. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  889. op = REQ_OP_WRITE;
  890. if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
  891. op_flags = REQ_FUA;
  892. if (test_bit(R5_Discard, &sh->dev[i].flags))
  893. op = REQ_OP_DISCARD;
  894. } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  895. op = REQ_OP_READ;
  896. else if (test_and_clear_bit(R5_WantReplace,
  897. &sh->dev[i].flags)) {
  898. op = REQ_OP_WRITE;
  899. replace_only = 1;
  900. } else
  901. continue;
  902. if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
  903. op_flags |= REQ_SYNC;
  904. again:
  905. bi = &sh->dev[i].req;
  906. rbi = &sh->dev[i].rreq; /* For writing to replacement */
  907. rcu_read_lock();
  908. rrdev = rcu_dereference(conf->disks[i].replacement);
  909. smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
  910. rdev = rcu_dereference(conf->disks[i].rdev);
  911. if (!rdev) {
  912. rdev = rrdev;
  913. rrdev = NULL;
  914. }
  915. if (op_is_write(op)) {
  916. if (replace_only)
  917. rdev = NULL;
  918. if (rdev == rrdev)
  919. /* We raced and saw duplicates */
  920. rrdev = NULL;
  921. } else {
  922. if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
  923. rdev = rrdev;
  924. rrdev = NULL;
  925. }
  926. if (rdev && test_bit(Faulty, &rdev->flags))
  927. rdev = NULL;
  928. if (rdev)
  929. atomic_inc(&rdev->nr_pending);
  930. if (rrdev && test_bit(Faulty, &rrdev->flags))
  931. rrdev = NULL;
  932. if (rrdev)
  933. atomic_inc(&rrdev->nr_pending);
  934. rcu_read_unlock();
  935. /* We have already checked bad blocks for reads. Now
  936. * need to check for writes. We never accept write errors
  937. * on the replacement, so we don't to check rrdev.
  938. */
  939. while (op_is_write(op) && rdev &&
  940. test_bit(WriteErrorSeen, &rdev->flags)) {
  941. sector_t first_bad;
  942. int bad_sectors;
  943. int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  944. &first_bad, &bad_sectors);
  945. if (!bad)
  946. break;
  947. if (bad < 0) {
  948. set_bit(BlockedBadBlocks, &rdev->flags);
  949. if (!conf->mddev->external &&
  950. conf->mddev->sb_flags) {
  951. /* It is very unlikely, but we might
  952. * still need to write out the
  953. * bad block log - better give it
  954. * a chance*/
  955. md_check_recovery(conf->mddev);
  956. }
  957. /*
  958. * Because md_wait_for_blocked_rdev
  959. * will dec nr_pending, we must
  960. * increment it first.
  961. */
  962. atomic_inc(&rdev->nr_pending);
  963. md_wait_for_blocked_rdev(rdev, conf->mddev);
  964. } else {
  965. /* Acknowledged bad block - skip the write */
  966. rdev_dec_pending(rdev, conf->mddev);
  967. rdev = NULL;
  968. }
  969. }
  970. if (rdev) {
  971. if (s->syncing || s->expanding || s->expanded
  972. || s->replacing)
  973. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  974. set_bit(STRIPE_IO_STARTED, &sh->state);
  975. bio_set_dev(bi, rdev->bdev);
  976. bio_set_op_attrs(bi, op, op_flags);
  977. bi->bi_end_io = op_is_write(op)
  978. ? raid5_end_write_request
  979. : raid5_end_read_request;
  980. bi->bi_private = sh;
  981. pr_debug("%s: for %llu schedule op %d on disc %d\n",
  982. __func__, (unsigned long long)sh->sector,
  983. bi->bi_opf, i);
  984. atomic_inc(&sh->count);
  985. if (sh != head_sh)
  986. atomic_inc(&head_sh->count);
  987. if (use_new_offset(conf, sh))
  988. bi->bi_iter.bi_sector = (sh->sector
  989. + rdev->new_data_offset);
  990. else
  991. bi->bi_iter.bi_sector = (sh->sector
  992. + rdev->data_offset);
  993. if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
  994. bi->bi_opf |= REQ_NOMERGE;
  995. if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
  996. WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
  997. if (!op_is_write(op) &&
  998. test_bit(R5_InJournal, &sh->dev[i].flags))
  999. /*
  1000. * issuing read for a page in journal, this
  1001. * must be preparing for prexor in rmw; read
  1002. * the data into orig_page
  1003. */
  1004. sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
  1005. else
  1006. sh->dev[i].vec.bv_page = sh->dev[i].page;
  1007. bi->bi_vcnt = 1;
  1008. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1009. bi->bi_io_vec[0].bv_offset = 0;
  1010. bi->bi_iter.bi_size = STRIPE_SIZE;
  1011. bi->bi_write_hint = sh->dev[i].write_hint;
  1012. if (!rrdev)
  1013. sh->dev[i].write_hint = RWF_WRITE_LIFE_NOT_SET;
  1014. /*
  1015. * If this is discard request, set bi_vcnt 0. We don't
  1016. * want to confuse SCSI because SCSI will replace payload
  1017. */
  1018. if (op == REQ_OP_DISCARD)
  1019. bi->bi_vcnt = 0;
  1020. if (rrdev)
  1021. set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
  1022. if (conf->mddev->gendisk)
  1023. trace_block_bio_remap(bi->bi_disk->queue,
  1024. bi, disk_devt(conf->mddev->gendisk),
  1025. sh->dev[i].sector);
  1026. if (should_defer && op_is_write(op))
  1027. bio_list_add(&pending_bios, bi);
  1028. else
  1029. generic_make_request(bi);
  1030. }
  1031. if (rrdev) {
  1032. if (s->syncing || s->expanding || s->expanded
  1033. || s->replacing)
  1034. md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
  1035. set_bit(STRIPE_IO_STARTED, &sh->state);
  1036. bio_set_dev(rbi, rrdev->bdev);
  1037. bio_set_op_attrs(rbi, op, op_flags);
  1038. BUG_ON(!op_is_write(op));
  1039. rbi->bi_end_io = raid5_end_write_request;
  1040. rbi->bi_private = sh;
  1041. pr_debug("%s: for %llu schedule op %d on "
  1042. "replacement disc %d\n",
  1043. __func__, (unsigned long long)sh->sector,
  1044. rbi->bi_opf, i);
  1045. atomic_inc(&sh->count);
  1046. if (sh != head_sh)
  1047. atomic_inc(&head_sh->count);
  1048. if (use_new_offset(conf, sh))
  1049. rbi->bi_iter.bi_sector = (sh->sector
  1050. + rrdev->new_data_offset);
  1051. else
  1052. rbi->bi_iter.bi_sector = (sh->sector
  1053. + rrdev->data_offset);
  1054. if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
  1055. WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
  1056. sh->dev[i].rvec.bv_page = sh->dev[i].page;
  1057. rbi->bi_vcnt = 1;
  1058. rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1059. rbi->bi_io_vec[0].bv_offset = 0;
  1060. rbi->bi_iter.bi_size = STRIPE_SIZE;
  1061. rbi->bi_write_hint = sh->dev[i].write_hint;
  1062. sh->dev[i].write_hint = RWF_WRITE_LIFE_NOT_SET;
  1063. /*
  1064. * If this is discard request, set bi_vcnt 0. We don't
  1065. * want to confuse SCSI because SCSI will replace payload
  1066. */
  1067. if (op == REQ_OP_DISCARD)
  1068. rbi->bi_vcnt = 0;
  1069. if (conf->mddev->gendisk)
  1070. trace_block_bio_remap(rbi->bi_disk->queue,
  1071. rbi, disk_devt(conf->mddev->gendisk),
  1072. sh->dev[i].sector);
  1073. if (should_defer && op_is_write(op))
  1074. bio_list_add(&pending_bios, rbi);
  1075. else
  1076. generic_make_request(rbi);
  1077. }
  1078. if (!rdev && !rrdev) {
  1079. if (op_is_write(op))
  1080. set_bit(STRIPE_DEGRADED, &sh->state);
  1081. pr_debug("skip op %d on disc %d for sector %llu\n",
  1082. bi->bi_opf, i, (unsigned long long)sh->sector);
  1083. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1084. set_bit(STRIPE_HANDLE, &sh->state);
  1085. }
  1086. if (!head_sh->batch_head)
  1087. continue;
  1088. sh = list_first_entry(&sh->batch_list, struct stripe_head,
  1089. batch_list);
  1090. if (sh != head_sh)
  1091. goto again;
  1092. }
  1093. if (should_defer && !bio_list_empty(&pending_bios))
  1094. defer_issue_bios(conf, head_sh->sector, &pending_bios);
  1095. }
  1096. static struct dma_async_tx_descriptor *
  1097. async_copy_data(int frombio, struct bio *bio, struct page **page,
  1098. sector_t sector, struct dma_async_tx_descriptor *tx,
  1099. struct stripe_head *sh, int no_skipcopy)
  1100. {
  1101. struct bio_vec bvl;
  1102. struct bvec_iter iter;
  1103. struct page *bio_page;
  1104. int page_offset;
  1105. struct async_submit_ctl submit;
  1106. enum async_tx_flags flags = 0;
  1107. if (bio->bi_iter.bi_sector >= sector)
  1108. page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
  1109. else
  1110. page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
  1111. if (frombio)
  1112. flags |= ASYNC_TX_FENCE;
  1113. init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
  1114. bio_for_each_segment(bvl, bio, iter) {
  1115. int len = bvl.bv_len;
  1116. int clen;
  1117. int b_offset = 0;
  1118. if (page_offset < 0) {
  1119. b_offset = -page_offset;
  1120. page_offset += b_offset;
  1121. len -= b_offset;
  1122. }
  1123. if (len > 0 && page_offset + len > STRIPE_SIZE)
  1124. clen = STRIPE_SIZE - page_offset;
  1125. else
  1126. clen = len;
  1127. if (clen > 0) {
  1128. b_offset += bvl.bv_offset;
  1129. bio_page = bvl.bv_page;
  1130. if (frombio) {
  1131. if (sh->raid_conf->skip_copy &&
  1132. b_offset == 0 && page_offset == 0 &&
  1133. clen == STRIPE_SIZE &&
  1134. !no_skipcopy)
  1135. *page = bio_page;
  1136. else
  1137. tx = async_memcpy(*page, bio_page, page_offset,
  1138. b_offset, clen, &submit);
  1139. } else
  1140. tx = async_memcpy(bio_page, *page, b_offset,
  1141. page_offset, clen, &submit);
  1142. }
  1143. /* chain the operations */
  1144. submit.depend_tx = tx;
  1145. if (clen < len) /* hit end of page */
  1146. break;
  1147. page_offset += len;
  1148. }
  1149. return tx;
  1150. }
  1151. static void ops_complete_biofill(void *stripe_head_ref)
  1152. {
  1153. struct stripe_head *sh = stripe_head_ref;
  1154. int i;
  1155. pr_debug("%s: stripe %llu\n", __func__,
  1156. (unsigned long long)sh->sector);
  1157. /* clear completed biofills */
  1158. for (i = sh->disks; i--; ) {
  1159. struct r5dev *dev = &sh->dev[i];
  1160. /* acknowledge completion of a biofill operation */
  1161. /* and check if we need to reply to a read request,
  1162. * new R5_Wantfill requests are held off until
  1163. * !STRIPE_BIOFILL_RUN
  1164. */
  1165. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  1166. struct bio *rbi, *rbi2;
  1167. BUG_ON(!dev->read);
  1168. rbi = dev->read;
  1169. dev->read = NULL;
  1170. while (rbi && rbi->bi_iter.bi_sector <
  1171. dev->sector + STRIPE_SECTORS) {
  1172. rbi2 = r5_next_bio(rbi, dev->sector);
  1173. bio_endio(rbi);
  1174. rbi = rbi2;
  1175. }
  1176. }
  1177. }
  1178. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  1179. set_bit(STRIPE_HANDLE, &sh->state);
  1180. raid5_release_stripe(sh);
  1181. }
  1182. static void ops_run_biofill(struct stripe_head *sh)
  1183. {
  1184. struct dma_async_tx_descriptor *tx = NULL;
  1185. struct async_submit_ctl submit;
  1186. int i;
  1187. BUG_ON(sh->batch_head);
  1188. pr_debug("%s: stripe %llu\n", __func__,
  1189. (unsigned long long)sh->sector);
  1190. for (i = sh->disks; i--; ) {
  1191. struct r5dev *dev = &sh->dev[i];
  1192. if (test_bit(R5_Wantfill, &dev->flags)) {
  1193. struct bio *rbi;
  1194. spin_lock_irq(&sh->stripe_lock);
  1195. dev->read = rbi = dev->toread;
  1196. dev->toread = NULL;
  1197. spin_unlock_irq(&sh->stripe_lock);
  1198. while (rbi && rbi->bi_iter.bi_sector <
  1199. dev->sector + STRIPE_SECTORS) {
  1200. tx = async_copy_data(0, rbi, &dev->page,
  1201. dev->sector, tx, sh, 0);
  1202. rbi = r5_next_bio(rbi, dev->sector);
  1203. }
  1204. }
  1205. }
  1206. atomic_inc(&sh->count);
  1207. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
  1208. async_trigger_callback(&submit);
  1209. }
  1210. static void mark_target_uptodate(struct stripe_head *sh, int target)
  1211. {
  1212. struct r5dev *tgt;
  1213. if (target < 0)
  1214. return;
  1215. tgt = &sh->dev[target];
  1216. set_bit(R5_UPTODATE, &tgt->flags);
  1217. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1218. clear_bit(R5_Wantcompute, &tgt->flags);
  1219. }
  1220. static void ops_complete_compute(void *stripe_head_ref)
  1221. {
  1222. struct stripe_head *sh = stripe_head_ref;
  1223. pr_debug("%s: stripe %llu\n", __func__,
  1224. (unsigned long long)sh->sector);
  1225. /* mark the computed target(s) as uptodate */
  1226. mark_target_uptodate(sh, sh->ops.target);
  1227. mark_target_uptodate(sh, sh->ops.target2);
  1228. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  1229. if (sh->check_state == check_state_compute_run)
  1230. sh->check_state = check_state_compute_result;
  1231. set_bit(STRIPE_HANDLE, &sh->state);
  1232. raid5_release_stripe(sh);
  1233. }
  1234. /* return a pointer to the address conversion region of the scribble buffer */
  1235. static addr_conv_t *to_addr_conv(struct stripe_head *sh,
  1236. struct raid5_percpu *percpu, int i)
  1237. {
  1238. void *addr;
  1239. addr = flex_array_get(percpu->scribble, i);
  1240. return addr + sizeof(struct page *) * (sh->disks + 2);
  1241. }
  1242. /* return a pointer to the address conversion region of the scribble buffer */
  1243. static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
  1244. {
  1245. void *addr;
  1246. addr = flex_array_get(percpu->scribble, i);
  1247. return addr;
  1248. }
  1249. static struct dma_async_tx_descriptor *
  1250. ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
  1251. {
  1252. int disks = sh->disks;
  1253. struct page **xor_srcs = to_addr_page(percpu, 0);
  1254. int target = sh->ops.target;
  1255. struct r5dev *tgt = &sh->dev[target];
  1256. struct page *xor_dest = tgt->page;
  1257. int count = 0;
  1258. struct dma_async_tx_descriptor *tx;
  1259. struct async_submit_ctl submit;
  1260. int i;
  1261. BUG_ON(sh->batch_head);
  1262. pr_debug("%s: stripe %llu block: %d\n",
  1263. __func__, (unsigned long long)sh->sector, target);
  1264. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1265. for (i = disks; i--; )
  1266. if (i != target)
  1267. xor_srcs[count++] = sh->dev[i].page;
  1268. atomic_inc(&sh->count);
  1269. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
  1270. ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
  1271. if (unlikely(count == 1))
  1272. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  1273. else
  1274. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1275. return tx;
  1276. }
  1277. /* set_syndrome_sources - populate source buffers for gen_syndrome
  1278. * @srcs - (struct page *) array of size sh->disks
  1279. * @sh - stripe_head to parse
  1280. *
  1281. * Populates srcs in proper layout order for the stripe and returns the
  1282. * 'count' of sources to be used in a call to async_gen_syndrome. The P
  1283. * destination buffer is recorded in srcs[count] and the Q destination
  1284. * is recorded in srcs[count+1]].
  1285. */
  1286. static int set_syndrome_sources(struct page **srcs,
  1287. struct stripe_head *sh,
  1288. int srctype)
  1289. {
  1290. int disks = sh->disks;
  1291. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  1292. int d0_idx = raid6_d0(sh);
  1293. int count;
  1294. int i;
  1295. for (i = 0; i < disks; i++)
  1296. srcs[i] = NULL;
  1297. count = 0;
  1298. i = d0_idx;
  1299. do {
  1300. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1301. struct r5dev *dev = &sh->dev[i];
  1302. if (i == sh->qd_idx || i == sh->pd_idx ||
  1303. (srctype == SYNDROME_SRC_ALL) ||
  1304. (srctype == SYNDROME_SRC_WANT_DRAIN &&
  1305. (test_bit(R5_Wantdrain, &dev->flags) ||
  1306. test_bit(R5_InJournal, &dev->flags))) ||
  1307. (srctype == SYNDROME_SRC_WRITTEN &&
  1308. (dev->written ||
  1309. test_bit(R5_InJournal, &dev->flags)))) {
  1310. if (test_bit(R5_InJournal, &dev->flags))
  1311. srcs[slot] = sh->dev[i].orig_page;
  1312. else
  1313. srcs[slot] = sh->dev[i].page;
  1314. }
  1315. i = raid6_next_disk(i, disks);
  1316. } while (i != d0_idx);
  1317. return syndrome_disks;
  1318. }
  1319. static struct dma_async_tx_descriptor *
  1320. ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
  1321. {
  1322. int disks = sh->disks;
  1323. struct page **blocks = to_addr_page(percpu, 0);
  1324. int target;
  1325. int qd_idx = sh->qd_idx;
  1326. struct dma_async_tx_descriptor *tx;
  1327. struct async_submit_ctl submit;
  1328. struct r5dev *tgt;
  1329. struct page *dest;
  1330. int i;
  1331. int count;
  1332. BUG_ON(sh->batch_head);
  1333. if (sh->ops.target < 0)
  1334. target = sh->ops.target2;
  1335. else if (sh->ops.target2 < 0)
  1336. target = sh->ops.target;
  1337. else
  1338. /* we should only have one valid target */
  1339. BUG();
  1340. BUG_ON(target < 0);
  1341. pr_debug("%s: stripe %llu block: %d\n",
  1342. __func__, (unsigned long long)sh->sector, target);
  1343. tgt = &sh->dev[target];
  1344. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1345. dest = tgt->page;
  1346. atomic_inc(&sh->count);
  1347. if (target == qd_idx) {
  1348. count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
  1349. blocks[count] = NULL; /* regenerating p is not necessary */
  1350. BUG_ON(blocks[count+1] != dest); /* q should already be set */
  1351. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1352. ops_complete_compute, sh,
  1353. to_addr_conv(sh, percpu, 0));
  1354. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1355. } else {
  1356. /* Compute any data- or p-drive using XOR */
  1357. count = 0;
  1358. for (i = disks; i-- ; ) {
  1359. if (i == target || i == qd_idx)
  1360. continue;
  1361. blocks[count++] = sh->dev[i].page;
  1362. }
  1363. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  1364. NULL, ops_complete_compute, sh,
  1365. to_addr_conv(sh, percpu, 0));
  1366. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
  1367. }
  1368. return tx;
  1369. }
  1370. static struct dma_async_tx_descriptor *
  1371. ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
  1372. {
  1373. int i, count, disks = sh->disks;
  1374. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  1375. int d0_idx = raid6_d0(sh);
  1376. int faila = -1, failb = -1;
  1377. int target = sh->ops.target;
  1378. int target2 = sh->ops.target2;
  1379. struct r5dev *tgt = &sh->dev[target];
  1380. struct r5dev *tgt2 = &sh->dev[target2];
  1381. struct dma_async_tx_descriptor *tx;
  1382. struct page **blocks = to_addr_page(percpu, 0);
  1383. struct async_submit_ctl submit;
  1384. BUG_ON(sh->batch_head);
  1385. pr_debug("%s: stripe %llu block1: %d block2: %d\n",
  1386. __func__, (unsigned long long)sh->sector, target, target2);
  1387. BUG_ON(target < 0 || target2 < 0);
  1388. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1389. BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
  1390. /* we need to open-code set_syndrome_sources to handle the
  1391. * slot number conversion for 'faila' and 'failb'
  1392. */
  1393. for (i = 0; i < disks ; i++)
  1394. blocks[i] = NULL;
  1395. count = 0;
  1396. i = d0_idx;
  1397. do {
  1398. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1399. blocks[slot] = sh->dev[i].page;
  1400. if (i == target)
  1401. faila = slot;
  1402. if (i == target2)
  1403. failb = slot;
  1404. i = raid6_next_disk(i, disks);
  1405. } while (i != d0_idx);
  1406. BUG_ON(faila == failb);
  1407. if (failb < faila)
  1408. swap(faila, failb);
  1409. pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
  1410. __func__, (unsigned long long)sh->sector, faila, failb);
  1411. atomic_inc(&sh->count);
  1412. if (failb == syndrome_disks+1) {
  1413. /* Q disk is one of the missing disks */
  1414. if (faila == syndrome_disks) {
  1415. /* Missing P+Q, just recompute */
  1416. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1417. ops_complete_compute, sh,
  1418. to_addr_conv(sh, percpu, 0));
  1419. return async_gen_syndrome(blocks, 0, syndrome_disks+2,
  1420. STRIPE_SIZE, &submit);
  1421. } else {
  1422. struct page *dest;
  1423. int data_target;
  1424. int qd_idx = sh->qd_idx;
  1425. /* Missing D+Q: recompute D from P, then recompute Q */
  1426. if (target == qd_idx)
  1427. data_target = target2;
  1428. else
  1429. data_target = target;
  1430. count = 0;
  1431. for (i = disks; i-- ; ) {
  1432. if (i == data_target || i == qd_idx)
  1433. continue;
  1434. blocks[count++] = sh->dev[i].page;
  1435. }
  1436. dest = sh->dev[data_target].page;
  1437. init_async_submit(&submit,
  1438. ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  1439. NULL, NULL, NULL,
  1440. to_addr_conv(sh, percpu, 0));
  1441. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
  1442. &submit);
  1443. count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
  1444. init_async_submit(&submit, ASYNC_TX_FENCE, tx,
  1445. ops_complete_compute, sh,
  1446. to_addr_conv(sh, percpu, 0));
  1447. return async_gen_syndrome(blocks, 0, count+2,
  1448. STRIPE_SIZE, &submit);
  1449. }
  1450. } else {
  1451. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1452. ops_complete_compute, sh,
  1453. to_addr_conv(sh, percpu, 0));
  1454. if (failb == syndrome_disks) {
  1455. /* We're missing D+P. */
  1456. return async_raid6_datap_recov(syndrome_disks+2,
  1457. STRIPE_SIZE, faila,
  1458. blocks, &submit);
  1459. } else {
  1460. /* We're missing D+D. */
  1461. return async_raid6_2data_recov(syndrome_disks+2,
  1462. STRIPE_SIZE, faila, failb,
  1463. blocks, &submit);
  1464. }
  1465. }
  1466. }
  1467. static void ops_complete_prexor(void *stripe_head_ref)
  1468. {
  1469. struct stripe_head *sh = stripe_head_ref;
  1470. pr_debug("%s: stripe %llu\n", __func__,
  1471. (unsigned long long)sh->sector);
  1472. if (r5c_is_writeback(sh->raid_conf->log))
  1473. /*
  1474. * raid5-cache write back uses orig_page during prexor.
  1475. * After prexor, it is time to free orig_page
  1476. */
  1477. r5c_release_extra_page(sh);
  1478. }
  1479. static struct dma_async_tx_descriptor *
  1480. ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
  1481. struct dma_async_tx_descriptor *tx)
  1482. {
  1483. int disks = sh->disks;
  1484. struct page **xor_srcs = to_addr_page(percpu, 0);
  1485. int count = 0, pd_idx = sh->pd_idx, i;
  1486. struct async_submit_ctl submit;
  1487. /* existing parity data subtracted */
  1488. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  1489. BUG_ON(sh->batch_head);
  1490. pr_debug("%s: stripe %llu\n", __func__,
  1491. (unsigned long long)sh->sector);
  1492. for (i = disks; i--; ) {
  1493. struct r5dev *dev = &sh->dev[i];
  1494. /* Only process blocks that are known to be uptodate */
  1495. if (test_bit(R5_InJournal, &dev->flags))
  1496. xor_srcs[count++] = dev->orig_page;
  1497. else if (test_bit(R5_Wantdrain, &dev->flags))
  1498. xor_srcs[count++] = dev->page;
  1499. }
  1500. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  1501. ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
  1502. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1503. return tx;
  1504. }
  1505. static struct dma_async_tx_descriptor *
  1506. ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
  1507. struct dma_async_tx_descriptor *tx)
  1508. {
  1509. struct page **blocks = to_addr_page(percpu, 0);
  1510. int count;
  1511. struct async_submit_ctl submit;
  1512. pr_debug("%s: stripe %llu\n", __func__,
  1513. (unsigned long long)sh->sector);
  1514. count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
  1515. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
  1516. ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
  1517. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1518. return tx;
  1519. }
  1520. static struct dma_async_tx_descriptor *
  1521. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  1522. {
  1523. struct r5conf *conf = sh->raid_conf;
  1524. int disks = sh->disks;
  1525. int i;
  1526. struct stripe_head *head_sh = sh;
  1527. pr_debug("%s: stripe %llu\n", __func__,
  1528. (unsigned long long)sh->sector);
  1529. for (i = disks; i--; ) {
  1530. struct r5dev *dev;
  1531. struct bio *chosen;
  1532. sh = head_sh;
  1533. if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
  1534. struct bio *wbi;
  1535. again:
  1536. dev = &sh->dev[i];
  1537. /*
  1538. * clear R5_InJournal, so when rewriting a page in
  1539. * journal, it is not skipped by r5l_log_stripe()
  1540. */
  1541. clear_bit(R5_InJournal, &dev->flags);
  1542. spin_lock_irq(&sh->stripe_lock);
  1543. chosen = dev->towrite;
  1544. dev->towrite = NULL;
  1545. sh->overwrite_disks = 0;
  1546. BUG_ON(dev->written);
  1547. wbi = dev->written = chosen;
  1548. spin_unlock_irq(&sh->stripe_lock);
  1549. WARN_ON(dev->page != dev->orig_page);
  1550. while (wbi && wbi->bi_iter.bi_sector <
  1551. dev->sector + STRIPE_SECTORS) {
  1552. if (wbi->bi_opf & REQ_FUA)
  1553. set_bit(R5_WantFUA, &dev->flags);
  1554. if (wbi->bi_opf & REQ_SYNC)
  1555. set_bit(R5_SyncIO, &dev->flags);
  1556. if (bio_op(wbi) == REQ_OP_DISCARD)
  1557. set_bit(R5_Discard, &dev->flags);
  1558. else {
  1559. tx = async_copy_data(1, wbi, &dev->page,
  1560. dev->sector, tx, sh,
  1561. r5c_is_writeback(conf->log));
  1562. if (dev->page != dev->orig_page &&
  1563. !r5c_is_writeback(conf->log)) {
  1564. set_bit(R5_SkipCopy, &dev->flags);
  1565. clear_bit(R5_UPTODATE, &dev->flags);
  1566. clear_bit(R5_OVERWRITE, &dev->flags);
  1567. }
  1568. }
  1569. wbi = r5_next_bio(wbi, dev->sector);
  1570. }
  1571. if (head_sh->batch_head) {
  1572. sh = list_first_entry(&sh->batch_list,
  1573. struct stripe_head,
  1574. batch_list);
  1575. if (sh == head_sh)
  1576. continue;
  1577. goto again;
  1578. }
  1579. }
  1580. }
  1581. return tx;
  1582. }
  1583. static void ops_complete_reconstruct(void *stripe_head_ref)
  1584. {
  1585. struct stripe_head *sh = stripe_head_ref;
  1586. int disks = sh->disks;
  1587. int pd_idx = sh->pd_idx;
  1588. int qd_idx = sh->qd_idx;
  1589. int i;
  1590. bool fua = false, sync = false, discard = false;
  1591. pr_debug("%s: stripe %llu\n", __func__,
  1592. (unsigned long long)sh->sector);
  1593. for (i = disks; i--; ) {
  1594. fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
  1595. sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
  1596. discard |= test_bit(R5_Discard, &sh->dev[i].flags);
  1597. }
  1598. for (i = disks; i--; ) {
  1599. struct r5dev *dev = &sh->dev[i];
  1600. if (dev->written || i == pd_idx || i == qd_idx) {
  1601. if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
  1602. set_bit(R5_UPTODATE, &dev->flags);
  1603. if (test_bit(STRIPE_EXPAND_READY, &sh->state))
  1604. set_bit(R5_Expanded, &dev->flags);
  1605. }
  1606. if (fua)
  1607. set_bit(R5_WantFUA, &dev->flags);
  1608. if (sync)
  1609. set_bit(R5_SyncIO, &dev->flags);
  1610. }
  1611. }
  1612. if (sh->reconstruct_state == reconstruct_state_drain_run)
  1613. sh->reconstruct_state = reconstruct_state_drain_result;
  1614. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  1615. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  1616. else {
  1617. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  1618. sh->reconstruct_state = reconstruct_state_result;
  1619. }
  1620. set_bit(STRIPE_HANDLE, &sh->state);
  1621. raid5_release_stripe(sh);
  1622. }
  1623. static void
  1624. ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
  1625. struct dma_async_tx_descriptor *tx)
  1626. {
  1627. int disks = sh->disks;
  1628. struct page **xor_srcs;
  1629. struct async_submit_ctl submit;
  1630. int count, pd_idx = sh->pd_idx, i;
  1631. struct page *xor_dest;
  1632. int prexor = 0;
  1633. unsigned long flags;
  1634. int j = 0;
  1635. struct stripe_head *head_sh = sh;
  1636. int last_stripe;
  1637. pr_debug("%s: stripe %llu\n", __func__,
  1638. (unsigned long long)sh->sector);
  1639. for (i = 0; i < sh->disks; i++) {
  1640. if (pd_idx == i)
  1641. continue;
  1642. if (!test_bit(R5_Discard, &sh->dev[i].flags))
  1643. break;
  1644. }
  1645. if (i >= sh->disks) {
  1646. atomic_inc(&sh->count);
  1647. set_bit(R5_Discard, &sh->dev[pd_idx].flags);
  1648. ops_complete_reconstruct(sh);
  1649. return;
  1650. }
  1651. again:
  1652. count = 0;
  1653. xor_srcs = to_addr_page(percpu, j);
  1654. /* check if prexor is active which means only process blocks
  1655. * that are part of a read-modify-write (written)
  1656. */
  1657. if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  1658. prexor = 1;
  1659. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  1660. for (i = disks; i--; ) {
  1661. struct r5dev *dev = &sh->dev[i];
  1662. if (head_sh->dev[i].written ||
  1663. test_bit(R5_InJournal, &head_sh->dev[i].flags))
  1664. xor_srcs[count++] = dev->page;
  1665. }
  1666. } else {
  1667. xor_dest = sh->dev[pd_idx].page;
  1668. for (i = disks; i--; ) {
  1669. struct r5dev *dev = &sh->dev[i];
  1670. if (i != pd_idx)
  1671. xor_srcs[count++] = dev->page;
  1672. }
  1673. }
  1674. /* 1/ if we prexor'd then the dest is reused as a source
  1675. * 2/ if we did not prexor then we are redoing the parity
  1676. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  1677. * for the synchronous xor case
  1678. */
  1679. last_stripe = !head_sh->batch_head ||
  1680. list_first_entry(&sh->batch_list,
  1681. struct stripe_head, batch_list) == head_sh;
  1682. if (last_stripe) {
  1683. flags = ASYNC_TX_ACK |
  1684. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  1685. atomic_inc(&head_sh->count);
  1686. init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
  1687. to_addr_conv(sh, percpu, j));
  1688. } else {
  1689. flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
  1690. init_async_submit(&submit, flags, tx, NULL, NULL,
  1691. to_addr_conv(sh, percpu, j));
  1692. }
  1693. if (unlikely(count == 1))
  1694. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  1695. else
  1696. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1697. if (!last_stripe) {
  1698. j++;
  1699. sh = list_first_entry(&sh->batch_list, struct stripe_head,
  1700. batch_list);
  1701. goto again;
  1702. }
  1703. }
  1704. static void
  1705. ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
  1706. struct dma_async_tx_descriptor *tx)
  1707. {
  1708. struct async_submit_ctl submit;
  1709. struct page **blocks;
  1710. int count, i, j = 0;
  1711. struct stripe_head *head_sh = sh;
  1712. int last_stripe;
  1713. int synflags;
  1714. unsigned long txflags;
  1715. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  1716. for (i = 0; i < sh->disks; i++) {
  1717. if (sh->pd_idx == i || sh->qd_idx == i)
  1718. continue;
  1719. if (!test_bit(R5_Discard, &sh->dev[i].flags))
  1720. break;
  1721. }
  1722. if (i >= sh->disks) {
  1723. atomic_inc(&sh->count);
  1724. set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
  1725. set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
  1726. ops_complete_reconstruct(sh);
  1727. return;
  1728. }
  1729. again:
  1730. blocks = to_addr_page(percpu, j);
  1731. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  1732. synflags = SYNDROME_SRC_WRITTEN;
  1733. txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
  1734. } else {
  1735. synflags = SYNDROME_SRC_ALL;
  1736. txflags = ASYNC_TX_ACK;
  1737. }
  1738. count = set_syndrome_sources(blocks, sh, synflags);
  1739. last_stripe = !head_sh->batch_head ||
  1740. list_first_entry(&sh->batch_list,
  1741. struct stripe_head, batch_list) == head_sh;
  1742. if (last_stripe) {
  1743. atomic_inc(&head_sh->count);
  1744. init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
  1745. head_sh, to_addr_conv(sh, percpu, j));
  1746. } else
  1747. init_async_submit(&submit, 0, tx, NULL, NULL,
  1748. to_addr_conv(sh, percpu, j));
  1749. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1750. if (!last_stripe) {
  1751. j++;
  1752. sh = list_first_entry(&sh->batch_list, struct stripe_head,
  1753. batch_list);
  1754. goto again;
  1755. }
  1756. }
  1757. static void ops_complete_check(void *stripe_head_ref)
  1758. {
  1759. struct stripe_head *sh = stripe_head_ref;
  1760. pr_debug("%s: stripe %llu\n", __func__,
  1761. (unsigned long long)sh->sector);
  1762. sh->check_state = check_state_check_result;
  1763. set_bit(STRIPE_HANDLE, &sh->state);
  1764. raid5_release_stripe(sh);
  1765. }
  1766. static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
  1767. {
  1768. int disks = sh->disks;
  1769. int pd_idx = sh->pd_idx;
  1770. int qd_idx = sh->qd_idx;
  1771. struct page *xor_dest;
  1772. struct page **xor_srcs = to_addr_page(percpu, 0);
  1773. struct dma_async_tx_descriptor *tx;
  1774. struct async_submit_ctl submit;
  1775. int count;
  1776. int i;
  1777. pr_debug("%s: stripe %llu\n", __func__,
  1778. (unsigned long long)sh->sector);
  1779. BUG_ON(sh->batch_head);
  1780. count = 0;
  1781. xor_dest = sh->dev[pd_idx].page;
  1782. xor_srcs[count++] = xor_dest;
  1783. for (i = disks; i--; ) {
  1784. if (i == pd_idx || i == qd_idx)
  1785. continue;
  1786. xor_srcs[count++] = sh->dev[i].page;
  1787. }
  1788. init_async_submit(&submit, 0, NULL, NULL, NULL,
  1789. to_addr_conv(sh, percpu, 0));
  1790. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  1791. &sh->ops.zero_sum_result, &submit);
  1792. atomic_inc(&sh->count);
  1793. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
  1794. tx = async_trigger_callback(&submit);
  1795. }
  1796. static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
  1797. {
  1798. struct page **srcs = to_addr_page(percpu, 0);
  1799. struct async_submit_ctl submit;
  1800. int count;
  1801. pr_debug("%s: stripe %llu checkp: %d\n", __func__,
  1802. (unsigned long long)sh->sector, checkp);
  1803. BUG_ON(sh->batch_head);
  1804. count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
  1805. if (!checkp)
  1806. srcs[count] = NULL;
  1807. atomic_inc(&sh->count);
  1808. init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
  1809. sh, to_addr_conv(sh, percpu, 0));
  1810. async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
  1811. &sh->ops.zero_sum_result, percpu->spare_page, &submit);
  1812. }
  1813. static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1814. {
  1815. int overlap_clear = 0, i, disks = sh->disks;
  1816. struct dma_async_tx_descriptor *tx = NULL;
  1817. struct r5conf *conf = sh->raid_conf;
  1818. int level = conf->level;
  1819. struct raid5_percpu *percpu;
  1820. unsigned long cpu;
  1821. cpu = get_cpu();
  1822. percpu = per_cpu_ptr(conf->percpu, cpu);
  1823. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  1824. ops_run_biofill(sh);
  1825. overlap_clear++;
  1826. }
  1827. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  1828. if (level < 6)
  1829. tx = ops_run_compute5(sh, percpu);
  1830. else {
  1831. if (sh->ops.target2 < 0 || sh->ops.target < 0)
  1832. tx = ops_run_compute6_1(sh, percpu);
  1833. else
  1834. tx = ops_run_compute6_2(sh, percpu);
  1835. }
  1836. /* terminate the chain if reconstruct is not set to be run */
  1837. if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
  1838. async_tx_ack(tx);
  1839. }
  1840. if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
  1841. if (level < 6)
  1842. tx = ops_run_prexor5(sh, percpu, tx);
  1843. else
  1844. tx = ops_run_prexor6(sh, percpu, tx);
  1845. }
  1846. if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
  1847. tx = ops_run_partial_parity(sh, percpu, tx);
  1848. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  1849. tx = ops_run_biodrain(sh, tx);
  1850. overlap_clear++;
  1851. }
  1852. if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
  1853. if (level < 6)
  1854. ops_run_reconstruct5(sh, percpu, tx);
  1855. else
  1856. ops_run_reconstruct6(sh, percpu, tx);
  1857. }
  1858. if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
  1859. if (sh->check_state == check_state_run)
  1860. ops_run_check_p(sh, percpu);
  1861. else if (sh->check_state == check_state_run_q)
  1862. ops_run_check_pq(sh, percpu, 0);
  1863. else if (sh->check_state == check_state_run_pq)
  1864. ops_run_check_pq(sh, percpu, 1);
  1865. else
  1866. BUG();
  1867. }
  1868. if (overlap_clear && !sh->batch_head)
  1869. for (i = disks; i--; ) {
  1870. struct r5dev *dev = &sh->dev[i];
  1871. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1872. wake_up(&sh->raid_conf->wait_for_overlap);
  1873. }
  1874. put_cpu();
  1875. }
  1876. static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
  1877. {
  1878. if (sh->ppl_page)
  1879. __free_page(sh->ppl_page);
  1880. kmem_cache_free(sc, sh);
  1881. }
  1882. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
  1883. int disks, struct r5conf *conf)
  1884. {
  1885. struct stripe_head *sh;
  1886. int i;
  1887. sh = kmem_cache_zalloc(sc, gfp);
  1888. if (sh) {
  1889. spin_lock_init(&sh->stripe_lock);
  1890. spin_lock_init(&sh->batch_lock);
  1891. INIT_LIST_HEAD(&sh->batch_list);
  1892. INIT_LIST_HEAD(&sh->lru);
  1893. INIT_LIST_HEAD(&sh->r5c);
  1894. INIT_LIST_HEAD(&sh->log_list);
  1895. atomic_set(&sh->count, 1);
  1896. sh->raid_conf = conf;
  1897. sh->log_start = MaxSector;
  1898. for (i = 0; i < disks; i++) {
  1899. struct r5dev *dev = &sh->dev[i];
  1900. bio_init(&dev->req, &dev->vec, 1);
  1901. bio_init(&dev->rreq, &dev->rvec, 1);
  1902. }
  1903. if (raid5_has_ppl(conf)) {
  1904. sh->ppl_page = alloc_page(gfp);
  1905. if (!sh->ppl_page) {
  1906. free_stripe(sc, sh);
  1907. sh = NULL;
  1908. }
  1909. }
  1910. }
  1911. return sh;
  1912. }
  1913. static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
  1914. {
  1915. struct stripe_head *sh;
  1916. sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
  1917. if (!sh)
  1918. return 0;
  1919. if (grow_buffers(sh, gfp)) {
  1920. shrink_buffers(sh);
  1921. free_stripe(conf->slab_cache, sh);
  1922. return 0;
  1923. }
  1924. sh->hash_lock_index =
  1925. conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
  1926. /* we just created an active stripe so... */
  1927. atomic_inc(&conf->active_stripes);
  1928. raid5_release_stripe(sh);
  1929. conf->max_nr_stripes++;
  1930. return 1;
  1931. }
  1932. static int grow_stripes(struct r5conf *conf, int num)
  1933. {
  1934. struct kmem_cache *sc;
  1935. size_t namelen = sizeof(conf->cache_name[0]);
  1936. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  1937. if (conf->mddev->gendisk)
  1938. snprintf(conf->cache_name[0], namelen,
  1939. "raid%d-%s", conf->level, mdname(conf->mddev));
  1940. else
  1941. snprintf(conf->cache_name[0], namelen,
  1942. "raid%d-%p", conf->level, conf->mddev);
  1943. snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
  1944. conf->active_name = 0;
  1945. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  1946. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  1947. 0, 0, NULL);
  1948. if (!sc)
  1949. return 1;
  1950. conf->slab_cache = sc;
  1951. conf->pool_size = devs;
  1952. while (num--)
  1953. if (!grow_one_stripe(conf, GFP_KERNEL))
  1954. return 1;
  1955. return 0;
  1956. }
  1957. /**
  1958. * scribble_len - return the required size of the scribble region
  1959. * @num - total number of disks in the array
  1960. *
  1961. * The size must be enough to contain:
  1962. * 1/ a struct page pointer for each device in the array +2
  1963. * 2/ room to convert each entry in (1) to its corresponding dma
  1964. * (dma_map_page()) or page (page_address()) address.
  1965. *
  1966. * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
  1967. * calculate over all devices (not just the data blocks), using zeros in place
  1968. * of the P and Q blocks.
  1969. */
  1970. static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
  1971. {
  1972. struct flex_array *ret;
  1973. size_t len;
  1974. len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
  1975. ret = flex_array_alloc(len, cnt, flags);
  1976. if (!ret)
  1977. return NULL;
  1978. /* always prealloc all elements, so no locking is required */
  1979. if (flex_array_prealloc(ret, 0, cnt, flags)) {
  1980. flex_array_free(ret);
  1981. return NULL;
  1982. }
  1983. return ret;
  1984. }
  1985. static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
  1986. {
  1987. unsigned long cpu;
  1988. int err = 0;
  1989. /*
  1990. * Never shrink. And mddev_suspend() could deadlock if this is called
  1991. * from raid5d. In that case, scribble_disks and scribble_sectors
  1992. * should equal to new_disks and new_sectors
  1993. */
  1994. if (conf->scribble_disks >= new_disks &&
  1995. conf->scribble_sectors >= new_sectors)
  1996. return 0;
  1997. mddev_suspend(conf->mddev);
  1998. get_online_cpus();
  1999. for_each_present_cpu(cpu) {
  2000. struct raid5_percpu *percpu;
  2001. struct flex_array *scribble;
  2002. percpu = per_cpu_ptr(conf->percpu, cpu);
  2003. scribble = scribble_alloc(new_disks,
  2004. new_sectors / STRIPE_SECTORS,
  2005. GFP_NOIO);
  2006. if (scribble) {
  2007. flex_array_free(percpu->scribble);
  2008. percpu->scribble = scribble;
  2009. } else {
  2010. err = -ENOMEM;
  2011. break;
  2012. }
  2013. }
  2014. put_online_cpus();
  2015. mddev_resume(conf->mddev);
  2016. if (!err) {
  2017. conf->scribble_disks = new_disks;
  2018. conf->scribble_sectors = new_sectors;
  2019. }
  2020. return err;
  2021. }
  2022. static int resize_stripes(struct r5conf *conf, int newsize)
  2023. {
  2024. /* Make all the stripes able to hold 'newsize' devices.
  2025. * New slots in each stripe get 'page' set to a new page.
  2026. *
  2027. * This happens in stages:
  2028. * 1/ create a new kmem_cache and allocate the required number of
  2029. * stripe_heads.
  2030. * 2/ gather all the old stripe_heads and transfer the pages across
  2031. * to the new stripe_heads. This will have the side effect of
  2032. * freezing the array as once all stripe_heads have been collected,
  2033. * no IO will be possible. Old stripe heads are freed once their
  2034. * pages have been transferred over, and the old kmem_cache is
  2035. * freed when all stripes are done.
  2036. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  2037. * we simple return a failure status - no need to clean anything up.
  2038. * 4/ allocate new pages for the new slots in the new stripe_heads.
  2039. * If this fails, we don't bother trying the shrink the
  2040. * stripe_heads down again, we just leave them as they are.
  2041. * As each stripe_head is processed the new one is released into
  2042. * active service.
  2043. *
  2044. * Once step2 is started, we cannot afford to wait for a write,
  2045. * so we use GFP_NOIO allocations.
  2046. */
  2047. struct stripe_head *osh, *nsh;
  2048. LIST_HEAD(newstripes);
  2049. struct disk_info *ndisks;
  2050. int err = 0;
  2051. struct kmem_cache *sc;
  2052. int i;
  2053. int hash, cnt;
  2054. md_allow_write(conf->mddev);
  2055. /* Step 1 */
  2056. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  2057. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  2058. 0, 0, NULL);
  2059. if (!sc)
  2060. return -ENOMEM;
  2061. /* Need to ensure auto-resizing doesn't interfere */
  2062. mutex_lock(&conf->cache_size_mutex);
  2063. for (i = conf->max_nr_stripes; i; i--) {
  2064. nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
  2065. if (!nsh)
  2066. break;
  2067. list_add(&nsh->lru, &newstripes);
  2068. }
  2069. if (i) {
  2070. /* didn't get enough, give up */
  2071. while (!list_empty(&newstripes)) {
  2072. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  2073. list_del(&nsh->lru);
  2074. free_stripe(sc, nsh);
  2075. }
  2076. kmem_cache_destroy(sc);
  2077. mutex_unlock(&conf->cache_size_mutex);
  2078. return -ENOMEM;
  2079. }
  2080. /* Step 2 - Must use GFP_NOIO now.
  2081. * OK, we have enough stripes, start collecting inactive
  2082. * stripes and copying them over
  2083. */
  2084. hash = 0;
  2085. cnt = 0;
  2086. list_for_each_entry(nsh, &newstripes, lru) {
  2087. lock_device_hash_lock(conf, hash);
  2088. wait_event_cmd(conf->wait_for_stripe,
  2089. !list_empty(conf->inactive_list + hash),
  2090. unlock_device_hash_lock(conf, hash),
  2091. lock_device_hash_lock(conf, hash));
  2092. osh = get_free_stripe(conf, hash);
  2093. unlock_device_hash_lock(conf, hash);
  2094. for(i=0; i<conf->pool_size; i++) {
  2095. nsh->dev[i].page = osh->dev[i].page;
  2096. nsh->dev[i].orig_page = osh->dev[i].page;
  2097. }
  2098. nsh->hash_lock_index = hash;
  2099. free_stripe(conf->slab_cache, osh);
  2100. cnt++;
  2101. if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
  2102. !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
  2103. hash++;
  2104. cnt = 0;
  2105. }
  2106. }
  2107. kmem_cache_destroy(conf->slab_cache);
  2108. /* Step 3.
  2109. * At this point, we are holding all the stripes so the array
  2110. * is completely stalled, so now is a good time to resize
  2111. * conf->disks and the scribble region
  2112. */
  2113. ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
  2114. if (ndisks) {
  2115. for (i = 0; i < conf->pool_size; i++)
  2116. ndisks[i] = conf->disks[i];
  2117. for (i = conf->pool_size; i < newsize; i++) {
  2118. ndisks[i].extra_page = alloc_page(GFP_NOIO);
  2119. if (!ndisks[i].extra_page)
  2120. err = -ENOMEM;
  2121. }
  2122. if (err) {
  2123. for (i = conf->pool_size; i < newsize; i++)
  2124. if (ndisks[i].extra_page)
  2125. put_page(ndisks[i].extra_page);
  2126. kfree(ndisks);
  2127. } else {
  2128. kfree(conf->disks);
  2129. conf->disks = ndisks;
  2130. }
  2131. } else
  2132. err = -ENOMEM;
  2133. mutex_unlock(&conf->cache_size_mutex);
  2134. conf->slab_cache = sc;
  2135. conf->active_name = 1-conf->active_name;
  2136. /* Step 4, return new stripes to service */
  2137. while(!list_empty(&newstripes)) {
  2138. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  2139. list_del_init(&nsh->lru);
  2140. for (i=conf->raid_disks; i < newsize; i++)
  2141. if (nsh->dev[i].page == NULL) {
  2142. struct page *p = alloc_page(GFP_NOIO);
  2143. nsh->dev[i].page = p;
  2144. nsh->dev[i].orig_page = p;
  2145. if (!p)
  2146. err = -ENOMEM;
  2147. }
  2148. raid5_release_stripe(nsh);
  2149. }
  2150. /* critical section pass, GFP_NOIO no longer needed */
  2151. if (!err)
  2152. conf->pool_size = newsize;
  2153. return err;
  2154. }
  2155. static int drop_one_stripe(struct r5conf *conf)
  2156. {
  2157. struct stripe_head *sh;
  2158. int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
  2159. spin_lock_irq(conf->hash_locks + hash);
  2160. sh = get_free_stripe(conf, hash);
  2161. spin_unlock_irq(conf->hash_locks + hash);
  2162. if (!sh)
  2163. return 0;
  2164. BUG_ON(atomic_read(&sh->count));
  2165. shrink_buffers(sh);
  2166. free_stripe(conf->slab_cache, sh);
  2167. atomic_dec(&conf->active_stripes);
  2168. conf->max_nr_stripes--;
  2169. return 1;
  2170. }
  2171. static void shrink_stripes(struct r5conf *conf)
  2172. {
  2173. while (conf->max_nr_stripes &&
  2174. drop_one_stripe(conf))
  2175. ;
  2176. kmem_cache_destroy(conf->slab_cache);
  2177. conf->slab_cache = NULL;
  2178. }
  2179. static void raid5_end_read_request(struct bio * bi)
  2180. {
  2181. struct stripe_head *sh = bi->bi_private;
  2182. struct r5conf *conf = sh->raid_conf;
  2183. int disks = sh->disks, i;
  2184. char b[BDEVNAME_SIZE];
  2185. struct md_rdev *rdev = NULL;
  2186. sector_t s;
  2187. for (i=0 ; i<disks; i++)
  2188. if (bi == &sh->dev[i].req)
  2189. break;
  2190. pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
  2191. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  2192. bi->bi_status);
  2193. if (i == disks) {
  2194. bio_reset(bi);
  2195. BUG();
  2196. return;
  2197. }
  2198. if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
  2199. /* If replacement finished while this request was outstanding,
  2200. * 'replacement' might be NULL already.
  2201. * In that case it moved down to 'rdev'.
  2202. * rdev is not removed until all requests are finished.
  2203. */
  2204. rdev = conf->disks[i].replacement;
  2205. if (!rdev)
  2206. rdev = conf->disks[i].rdev;
  2207. if (use_new_offset(conf, sh))
  2208. s = sh->sector + rdev->new_data_offset;
  2209. else
  2210. s = sh->sector + rdev->data_offset;
  2211. if (!bi->bi_status) {
  2212. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  2213. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  2214. /* Note that this cannot happen on a
  2215. * replacement device. We just fail those on
  2216. * any error
  2217. */
  2218. pr_info_ratelimited(
  2219. "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
  2220. mdname(conf->mddev), STRIPE_SECTORS,
  2221. (unsigned long long)s,
  2222. bdevname(rdev->bdev, b));
  2223. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2224. clear_bit(R5_ReadError, &sh->dev[i].flags);
  2225. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  2226. } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
  2227. clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  2228. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  2229. /*
  2230. * end read for a page in journal, this
  2231. * must be preparing for prexor in rmw
  2232. */
  2233. set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
  2234. if (atomic_read(&rdev->read_errors))
  2235. atomic_set(&rdev->read_errors, 0);
  2236. } else {
  2237. const char *bdn = bdevname(rdev->bdev, b);
  2238. int retry = 0;
  2239. int set_bad = 0;
  2240. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  2241. if (!(bi->bi_status == BLK_STS_PROTECTION))
  2242. atomic_inc(&rdev->read_errors);
  2243. if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
  2244. pr_warn_ratelimited(
  2245. "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
  2246. mdname(conf->mddev),
  2247. (unsigned long long)s,
  2248. bdn);
  2249. else if (conf->mddev->degraded >= conf->max_degraded) {
  2250. set_bad = 1;
  2251. pr_warn_ratelimited(
  2252. "md/raid:%s: read error not correctable (sector %llu on %s).\n",
  2253. mdname(conf->mddev),
  2254. (unsigned long long)s,
  2255. bdn);
  2256. } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
  2257. /* Oh, no!!! */
  2258. set_bad = 1;
  2259. pr_warn_ratelimited(
  2260. "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
  2261. mdname(conf->mddev),
  2262. (unsigned long long)s,
  2263. bdn);
  2264. } else if (atomic_read(&rdev->read_errors)
  2265. > conf->max_nr_stripes)
  2266. pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
  2267. mdname(conf->mddev), bdn);
  2268. else
  2269. retry = 1;
  2270. if (set_bad && test_bit(In_sync, &rdev->flags)
  2271. && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
  2272. retry = 1;
  2273. if (retry)
  2274. if (sh->qd_idx >= 0 && sh->pd_idx == i)
  2275. set_bit(R5_ReadError, &sh->dev[i].flags);
  2276. else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
  2277. set_bit(R5_ReadError, &sh->dev[i].flags);
  2278. clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  2279. } else
  2280. set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  2281. else {
  2282. clear_bit(R5_ReadError, &sh->dev[i].flags);
  2283. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  2284. if (!(set_bad
  2285. && test_bit(In_sync, &rdev->flags)
  2286. && rdev_set_badblocks(
  2287. rdev, sh->sector, STRIPE_SECTORS, 0)))
  2288. md_error(conf->mddev, rdev);
  2289. }
  2290. }
  2291. rdev_dec_pending(rdev, conf->mddev);
  2292. bio_reset(bi);
  2293. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2294. set_bit(STRIPE_HANDLE, &sh->state);
  2295. raid5_release_stripe(sh);
  2296. }
  2297. static void raid5_end_write_request(struct bio *bi)
  2298. {
  2299. struct stripe_head *sh = bi->bi_private;
  2300. struct r5conf *conf = sh->raid_conf;
  2301. int disks = sh->disks, i;
  2302. struct md_rdev *uninitialized_var(rdev);
  2303. sector_t first_bad;
  2304. int bad_sectors;
  2305. int replacement = 0;
  2306. for (i = 0 ; i < disks; i++) {
  2307. if (bi == &sh->dev[i].req) {
  2308. rdev = conf->disks[i].rdev;
  2309. break;
  2310. }
  2311. if (bi == &sh->dev[i].rreq) {
  2312. rdev = conf->disks[i].replacement;
  2313. if (rdev)
  2314. replacement = 1;
  2315. else
  2316. /* rdev was removed and 'replacement'
  2317. * replaced it. rdev is not removed
  2318. * until all requests are finished.
  2319. */
  2320. rdev = conf->disks[i].rdev;
  2321. break;
  2322. }
  2323. }
  2324. pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
  2325. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  2326. bi->bi_status);
  2327. if (i == disks) {
  2328. bio_reset(bi);
  2329. BUG();
  2330. return;
  2331. }
  2332. if (replacement) {
  2333. if (bi->bi_status)
  2334. md_error(conf->mddev, rdev);
  2335. else if (is_badblock(rdev, sh->sector,
  2336. STRIPE_SECTORS,
  2337. &first_bad, &bad_sectors))
  2338. set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
  2339. } else {
  2340. if (bi->bi_status) {
  2341. set_bit(STRIPE_DEGRADED, &sh->state);
  2342. set_bit(WriteErrorSeen, &rdev->flags);
  2343. set_bit(R5_WriteError, &sh->dev[i].flags);
  2344. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  2345. set_bit(MD_RECOVERY_NEEDED,
  2346. &rdev->mddev->recovery);
  2347. } else if (is_badblock(rdev, sh->sector,
  2348. STRIPE_SECTORS,
  2349. &first_bad, &bad_sectors)) {
  2350. set_bit(R5_MadeGood, &sh->dev[i].flags);
  2351. if (test_bit(R5_ReadError, &sh->dev[i].flags))
  2352. /* That was a successful write so make
  2353. * sure it looks like we already did
  2354. * a re-write.
  2355. */
  2356. set_bit(R5_ReWrite, &sh->dev[i].flags);
  2357. }
  2358. }
  2359. rdev_dec_pending(rdev, conf->mddev);
  2360. if (sh->batch_head && bi->bi_status && !replacement)
  2361. set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
  2362. bio_reset(bi);
  2363. if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
  2364. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2365. set_bit(STRIPE_HANDLE, &sh->state);
  2366. raid5_release_stripe(sh);
  2367. if (sh->batch_head && sh != sh->batch_head)
  2368. raid5_release_stripe(sh->batch_head);
  2369. }
  2370. static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
  2371. {
  2372. char b[BDEVNAME_SIZE];
  2373. struct r5conf *conf = mddev->private;
  2374. unsigned long flags;
  2375. pr_debug("raid456: error called\n");
  2376. spin_lock_irqsave(&conf->device_lock, flags);
  2377. set_bit(Faulty, &rdev->flags);
  2378. clear_bit(In_sync, &rdev->flags);
  2379. mddev->degraded = raid5_calc_degraded(conf);
  2380. spin_unlock_irqrestore(&conf->device_lock, flags);
  2381. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  2382. set_bit(Blocked, &rdev->flags);
  2383. set_mask_bits(&mddev->sb_flags, 0,
  2384. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  2385. pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
  2386. "md/raid:%s: Operation continuing on %d devices.\n",
  2387. mdname(mddev),
  2388. bdevname(rdev->bdev, b),
  2389. mdname(mddev),
  2390. conf->raid_disks - mddev->degraded);
  2391. r5c_update_on_rdev_error(mddev, rdev);
  2392. }
  2393. /*
  2394. * Input: a 'big' sector number,
  2395. * Output: index of the data and parity disk, and the sector # in them.
  2396. */
  2397. sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
  2398. int previous, int *dd_idx,
  2399. struct stripe_head *sh)
  2400. {
  2401. sector_t stripe, stripe2;
  2402. sector_t chunk_number;
  2403. unsigned int chunk_offset;
  2404. int pd_idx, qd_idx;
  2405. int ddf_layout = 0;
  2406. sector_t new_sector;
  2407. int algorithm = previous ? conf->prev_algo
  2408. : conf->algorithm;
  2409. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  2410. : conf->chunk_sectors;
  2411. int raid_disks = previous ? conf->previous_raid_disks
  2412. : conf->raid_disks;
  2413. int data_disks = raid_disks - conf->max_degraded;
  2414. /* First compute the information on this sector */
  2415. /*
  2416. * Compute the chunk number and the sector offset inside the chunk
  2417. */
  2418. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  2419. chunk_number = r_sector;
  2420. /*
  2421. * Compute the stripe number
  2422. */
  2423. stripe = chunk_number;
  2424. *dd_idx = sector_div(stripe, data_disks);
  2425. stripe2 = stripe;
  2426. /*
  2427. * Select the parity disk based on the user selected algorithm.
  2428. */
  2429. pd_idx = qd_idx = -1;
  2430. switch(conf->level) {
  2431. case 4:
  2432. pd_idx = data_disks;
  2433. break;
  2434. case 5:
  2435. switch (algorithm) {
  2436. case ALGORITHM_LEFT_ASYMMETRIC:
  2437. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  2438. if (*dd_idx >= pd_idx)
  2439. (*dd_idx)++;
  2440. break;
  2441. case ALGORITHM_RIGHT_ASYMMETRIC:
  2442. pd_idx = sector_div(stripe2, raid_disks);
  2443. if (*dd_idx >= pd_idx)
  2444. (*dd_idx)++;
  2445. break;
  2446. case ALGORITHM_LEFT_SYMMETRIC:
  2447. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  2448. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  2449. break;
  2450. case ALGORITHM_RIGHT_SYMMETRIC:
  2451. pd_idx = sector_div(stripe2, raid_disks);
  2452. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  2453. break;
  2454. case ALGORITHM_PARITY_0:
  2455. pd_idx = 0;
  2456. (*dd_idx)++;
  2457. break;
  2458. case ALGORITHM_PARITY_N:
  2459. pd_idx = data_disks;
  2460. break;
  2461. default:
  2462. BUG();
  2463. }
  2464. break;
  2465. case 6:
  2466. switch (algorithm) {
  2467. case ALGORITHM_LEFT_ASYMMETRIC:
  2468. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2469. qd_idx = pd_idx + 1;
  2470. if (pd_idx == raid_disks-1) {
  2471. (*dd_idx)++; /* Q D D D P */
  2472. qd_idx = 0;
  2473. } else if (*dd_idx >= pd_idx)
  2474. (*dd_idx) += 2; /* D D P Q D */
  2475. break;
  2476. case ALGORITHM_RIGHT_ASYMMETRIC:
  2477. pd_idx = sector_div(stripe2, raid_disks);
  2478. qd_idx = pd_idx + 1;
  2479. if (pd_idx == raid_disks-1) {
  2480. (*dd_idx)++; /* Q D D D P */
  2481. qd_idx = 0;
  2482. } else if (*dd_idx >= pd_idx)
  2483. (*dd_idx) += 2; /* D D P Q D */
  2484. break;
  2485. case ALGORITHM_LEFT_SYMMETRIC:
  2486. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2487. qd_idx = (pd_idx + 1) % raid_disks;
  2488. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  2489. break;
  2490. case ALGORITHM_RIGHT_SYMMETRIC:
  2491. pd_idx = sector_div(stripe2, raid_disks);
  2492. qd_idx = (pd_idx + 1) % raid_disks;
  2493. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  2494. break;
  2495. case ALGORITHM_PARITY_0:
  2496. pd_idx = 0;
  2497. qd_idx = 1;
  2498. (*dd_idx) += 2;
  2499. break;
  2500. case ALGORITHM_PARITY_N:
  2501. pd_idx = data_disks;
  2502. qd_idx = data_disks + 1;
  2503. break;
  2504. case ALGORITHM_ROTATING_ZERO_RESTART:
  2505. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  2506. * of blocks for computing Q is different.
  2507. */
  2508. pd_idx = sector_div(stripe2, raid_disks);
  2509. qd_idx = pd_idx + 1;
  2510. if (pd_idx == raid_disks-1) {
  2511. (*dd_idx)++; /* Q D D D P */
  2512. qd_idx = 0;
  2513. } else if (*dd_idx >= pd_idx)
  2514. (*dd_idx) += 2; /* D D P Q D */
  2515. ddf_layout = 1;
  2516. break;
  2517. case ALGORITHM_ROTATING_N_RESTART:
  2518. /* Same a left_asymmetric, by first stripe is
  2519. * D D D P Q rather than
  2520. * Q D D D P
  2521. */
  2522. stripe2 += 1;
  2523. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2524. qd_idx = pd_idx + 1;
  2525. if (pd_idx == raid_disks-1) {
  2526. (*dd_idx)++; /* Q D D D P */
  2527. qd_idx = 0;
  2528. } else if (*dd_idx >= pd_idx)
  2529. (*dd_idx) += 2; /* D D P Q D */
  2530. ddf_layout = 1;
  2531. break;
  2532. case ALGORITHM_ROTATING_N_CONTINUE:
  2533. /* Same as left_symmetric but Q is before P */
  2534. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2535. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  2536. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  2537. ddf_layout = 1;
  2538. break;
  2539. case ALGORITHM_LEFT_ASYMMETRIC_6:
  2540. /* RAID5 left_asymmetric, with Q on last device */
  2541. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  2542. if (*dd_idx >= pd_idx)
  2543. (*dd_idx)++;
  2544. qd_idx = raid_disks - 1;
  2545. break;
  2546. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  2547. pd_idx = sector_div(stripe2, raid_disks-1);
  2548. if (*dd_idx >= pd_idx)
  2549. (*dd_idx)++;
  2550. qd_idx = raid_disks - 1;
  2551. break;
  2552. case ALGORITHM_LEFT_SYMMETRIC_6:
  2553. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  2554. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  2555. qd_idx = raid_disks - 1;
  2556. break;
  2557. case ALGORITHM_RIGHT_SYMMETRIC_6:
  2558. pd_idx = sector_div(stripe2, raid_disks-1);
  2559. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  2560. qd_idx = raid_disks - 1;
  2561. break;
  2562. case ALGORITHM_PARITY_0_6:
  2563. pd_idx = 0;
  2564. (*dd_idx)++;
  2565. qd_idx = raid_disks - 1;
  2566. break;
  2567. default:
  2568. BUG();
  2569. }
  2570. break;
  2571. }
  2572. if (sh) {
  2573. sh->pd_idx = pd_idx;
  2574. sh->qd_idx = qd_idx;
  2575. sh->ddf_layout = ddf_layout;
  2576. }
  2577. /*
  2578. * Finally, compute the new sector number
  2579. */
  2580. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  2581. return new_sector;
  2582. }
  2583. sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
  2584. {
  2585. struct r5conf *conf = sh->raid_conf;
  2586. int raid_disks = sh->disks;
  2587. int data_disks = raid_disks - conf->max_degraded;
  2588. sector_t new_sector = sh->sector, check;
  2589. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  2590. : conf->chunk_sectors;
  2591. int algorithm = previous ? conf->prev_algo
  2592. : conf->algorithm;
  2593. sector_t stripe;
  2594. int chunk_offset;
  2595. sector_t chunk_number;
  2596. int dummy1, dd_idx = i;
  2597. sector_t r_sector;
  2598. struct stripe_head sh2;
  2599. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  2600. stripe = new_sector;
  2601. if (i == sh->pd_idx)
  2602. return 0;
  2603. switch(conf->level) {
  2604. case 4: break;
  2605. case 5:
  2606. switch (algorithm) {
  2607. case ALGORITHM_LEFT_ASYMMETRIC:
  2608. case ALGORITHM_RIGHT_ASYMMETRIC:
  2609. if (i > sh->pd_idx)
  2610. i--;
  2611. break;
  2612. case ALGORITHM_LEFT_SYMMETRIC:
  2613. case ALGORITHM_RIGHT_SYMMETRIC:
  2614. if (i < sh->pd_idx)
  2615. i += raid_disks;
  2616. i -= (sh->pd_idx + 1);
  2617. break;
  2618. case ALGORITHM_PARITY_0:
  2619. i -= 1;
  2620. break;
  2621. case ALGORITHM_PARITY_N:
  2622. break;
  2623. default:
  2624. BUG();
  2625. }
  2626. break;
  2627. case 6:
  2628. if (i == sh->qd_idx)
  2629. return 0; /* It is the Q disk */
  2630. switch (algorithm) {
  2631. case ALGORITHM_LEFT_ASYMMETRIC:
  2632. case ALGORITHM_RIGHT_ASYMMETRIC:
  2633. case ALGORITHM_ROTATING_ZERO_RESTART:
  2634. case ALGORITHM_ROTATING_N_RESTART:
  2635. if (sh->pd_idx == raid_disks-1)
  2636. i--; /* Q D D D P */
  2637. else if (i > sh->pd_idx)
  2638. i -= 2; /* D D P Q D */
  2639. break;
  2640. case ALGORITHM_LEFT_SYMMETRIC:
  2641. case ALGORITHM_RIGHT_SYMMETRIC:
  2642. if (sh->pd_idx == raid_disks-1)
  2643. i--; /* Q D D D P */
  2644. else {
  2645. /* D D P Q D */
  2646. if (i < sh->pd_idx)
  2647. i += raid_disks;
  2648. i -= (sh->pd_idx + 2);
  2649. }
  2650. break;
  2651. case ALGORITHM_PARITY_0:
  2652. i -= 2;
  2653. break;
  2654. case ALGORITHM_PARITY_N:
  2655. break;
  2656. case ALGORITHM_ROTATING_N_CONTINUE:
  2657. /* Like left_symmetric, but P is before Q */
  2658. if (sh->pd_idx == 0)
  2659. i--; /* P D D D Q */
  2660. else {
  2661. /* D D Q P D */
  2662. if (i < sh->pd_idx)
  2663. i += raid_disks;
  2664. i -= (sh->pd_idx + 1);
  2665. }
  2666. break;
  2667. case ALGORITHM_LEFT_ASYMMETRIC_6:
  2668. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  2669. if (i > sh->pd_idx)
  2670. i--;
  2671. break;
  2672. case ALGORITHM_LEFT_SYMMETRIC_6:
  2673. case ALGORITHM_RIGHT_SYMMETRIC_6:
  2674. if (i < sh->pd_idx)
  2675. i += data_disks + 1;
  2676. i -= (sh->pd_idx + 1);
  2677. break;
  2678. case ALGORITHM_PARITY_0_6:
  2679. i -= 1;
  2680. break;
  2681. default:
  2682. BUG();
  2683. }
  2684. break;
  2685. }
  2686. chunk_number = stripe * data_disks + i;
  2687. r_sector = chunk_number * sectors_per_chunk + chunk_offset;
  2688. check = raid5_compute_sector(conf, r_sector,
  2689. previous, &dummy1, &sh2);
  2690. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  2691. || sh2.qd_idx != sh->qd_idx) {
  2692. pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
  2693. mdname(conf->mddev));
  2694. return 0;
  2695. }
  2696. return r_sector;
  2697. }
  2698. /*
  2699. * There are cases where we want handle_stripe_dirtying() and
  2700. * schedule_reconstruction() to delay towrite to some dev of a stripe.
  2701. *
  2702. * This function checks whether we want to delay the towrite. Specifically,
  2703. * we delay the towrite when:
  2704. *
  2705. * 1. degraded stripe has a non-overwrite to the missing dev, AND this
  2706. * stripe has data in journal (for other devices).
  2707. *
  2708. * In this case, when reading data for the non-overwrite dev, it is
  2709. * necessary to handle complex rmw of write back cache (prexor with
  2710. * orig_page, and xor with page). To keep read path simple, we would
  2711. * like to flush data in journal to RAID disks first, so complex rmw
  2712. * is handled in the write patch (handle_stripe_dirtying).
  2713. *
  2714. * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
  2715. *
  2716. * It is important to be able to flush all stripes in raid5-cache.
  2717. * Therefore, we need reserve some space on the journal device for
  2718. * these flushes. If flush operation includes pending writes to the
  2719. * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
  2720. * for the flush out. If we exclude these pending writes from flush
  2721. * operation, we only need (conf->max_degraded + 1) pages per stripe.
  2722. * Therefore, excluding pending writes in these cases enables more
  2723. * efficient use of the journal device.
  2724. *
  2725. * Note: To make sure the stripe makes progress, we only delay
  2726. * towrite for stripes with data already in journal (injournal > 0).
  2727. * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
  2728. * no_space_stripes list.
  2729. *
  2730. * 3. during journal failure
  2731. * In journal failure, we try to flush all cached data to raid disks
  2732. * based on data in stripe cache. The array is read-only to upper
  2733. * layers, so we would skip all pending writes.
  2734. *
  2735. */
  2736. static inline bool delay_towrite(struct r5conf *conf,
  2737. struct r5dev *dev,
  2738. struct stripe_head_state *s)
  2739. {
  2740. /* case 1 above */
  2741. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2742. !test_bit(R5_Insync, &dev->flags) && s->injournal)
  2743. return true;
  2744. /* case 2 above */
  2745. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  2746. s->injournal > 0)
  2747. return true;
  2748. /* case 3 above */
  2749. if (s->log_failed && s->injournal)
  2750. return true;
  2751. return false;
  2752. }
  2753. static void
  2754. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  2755. int rcw, int expand)
  2756. {
  2757. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
  2758. struct r5conf *conf = sh->raid_conf;
  2759. int level = conf->level;
  2760. if (rcw) {
  2761. /*
  2762. * In some cases, handle_stripe_dirtying initially decided to
  2763. * run rmw and allocates extra page for prexor. However, rcw is
  2764. * cheaper later on. We need to free the extra page now,
  2765. * because we won't be able to do that in ops_complete_prexor().
  2766. */
  2767. r5c_release_extra_page(sh);
  2768. for (i = disks; i--; ) {
  2769. struct r5dev *dev = &sh->dev[i];
  2770. if (dev->towrite && !delay_towrite(conf, dev, s)) {
  2771. set_bit(R5_LOCKED, &dev->flags);
  2772. set_bit(R5_Wantdrain, &dev->flags);
  2773. if (!expand)
  2774. clear_bit(R5_UPTODATE, &dev->flags);
  2775. s->locked++;
  2776. } else if (test_bit(R5_InJournal, &dev->flags)) {
  2777. set_bit(R5_LOCKED, &dev->flags);
  2778. s->locked++;
  2779. }
  2780. }
  2781. /* if we are not expanding this is a proper write request, and
  2782. * there will be bios with new data to be drained into the
  2783. * stripe cache
  2784. */
  2785. if (!expand) {
  2786. if (!s->locked)
  2787. /* False alarm, nothing to do */
  2788. return;
  2789. sh->reconstruct_state = reconstruct_state_drain_run;
  2790. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2791. } else
  2792. sh->reconstruct_state = reconstruct_state_run;
  2793. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  2794. if (s->locked + conf->max_degraded == disks)
  2795. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  2796. atomic_inc(&conf->pending_full_writes);
  2797. } else {
  2798. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  2799. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  2800. BUG_ON(level == 6 &&
  2801. (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
  2802. test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
  2803. for (i = disks; i--; ) {
  2804. struct r5dev *dev = &sh->dev[i];
  2805. if (i == pd_idx || i == qd_idx)
  2806. continue;
  2807. if (dev->towrite &&
  2808. (test_bit(R5_UPTODATE, &dev->flags) ||
  2809. test_bit(R5_Wantcompute, &dev->flags))) {
  2810. set_bit(R5_Wantdrain, &dev->flags);
  2811. set_bit(R5_LOCKED, &dev->flags);
  2812. clear_bit(R5_UPTODATE, &dev->flags);
  2813. s->locked++;
  2814. } else if (test_bit(R5_InJournal, &dev->flags)) {
  2815. set_bit(R5_LOCKED, &dev->flags);
  2816. s->locked++;
  2817. }
  2818. }
  2819. if (!s->locked)
  2820. /* False alarm - nothing to do */
  2821. return;
  2822. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  2823. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  2824. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2825. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  2826. }
  2827. /* keep the parity disk(s) locked while asynchronous operations
  2828. * are in flight
  2829. */
  2830. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  2831. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2832. s->locked++;
  2833. if (level == 6) {
  2834. int qd_idx = sh->qd_idx;
  2835. struct r5dev *dev = &sh->dev[qd_idx];
  2836. set_bit(R5_LOCKED, &dev->flags);
  2837. clear_bit(R5_UPTODATE, &dev->flags);
  2838. s->locked++;
  2839. }
  2840. if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
  2841. test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
  2842. !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
  2843. test_bit(R5_Insync, &sh->dev[pd_idx].flags))
  2844. set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
  2845. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  2846. __func__, (unsigned long long)sh->sector,
  2847. s->locked, s->ops_request);
  2848. }
  2849. /*
  2850. * Each stripe/dev can have one or more bion attached.
  2851. * toread/towrite point to the first in a chain.
  2852. * The bi_next chain must be in order.
  2853. */
  2854. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
  2855. int forwrite, int previous)
  2856. {
  2857. struct bio **bip;
  2858. struct r5conf *conf = sh->raid_conf;
  2859. int firstwrite=0;
  2860. pr_debug("adding bi b#%llu to stripe s#%llu\n",
  2861. (unsigned long long)bi->bi_iter.bi_sector,
  2862. (unsigned long long)sh->sector);
  2863. spin_lock_irq(&sh->stripe_lock);
  2864. sh->dev[dd_idx].write_hint = bi->bi_write_hint;
  2865. /* Don't allow new IO added to stripes in batch list */
  2866. if (sh->batch_head)
  2867. goto overlap;
  2868. if (forwrite) {
  2869. bip = &sh->dev[dd_idx].towrite;
  2870. if (*bip == NULL)
  2871. firstwrite = 1;
  2872. } else
  2873. bip = &sh->dev[dd_idx].toread;
  2874. while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
  2875. if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
  2876. goto overlap;
  2877. bip = & (*bip)->bi_next;
  2878. }
  2879. if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
  2880. goto overlap;
  2881. if (forwrite && raid5_has_ppl(conf)) {
  2882. /*
  2883. * With PPL only writes to consecutive data chunks within a
  2884. * stripe are allowed because for a single stripe_head we can
  2885. * only have one PPL entry at a time, which describes one data
  2886. * range. Not really an overlap, but wait_for_overlap can be
  2887. * used to handle this.
  2888. */
  2889. sector_t sector;
  2890. sector_t first = 0;
  2891. sector_t last = 0;
  2892. int count = 0;
  2893. int i;
  2894. for (i = 0; i < sh->disks; i++) {
  2895. if (i != sh->pd_idx &&
  2896. (i == dd_idx || sh->dev[i].towrite)) {
  2897. sector = sh->dev[i].sector;
  2898. if (count == 0 || sector < first)
  2899. first = sector;
  2900. if (sector > last)
  2901. last = sector;
  2902. count++;
  2903. }
  2904. }
  2905. if (first + conf->chunk_sectors * (count - 1) != last)
  2906. goto overlap;
  2907. }
  2908. if (!forwrite || previous)
  2909. clear_bit(STRIPE_BATCH_READY, &sh->state);
  2910. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  2911. if (*bip)
  2912. bi->bi_next = *bip;
  2913. *bip = bi;
  2914. bio_inc_remaining(bi);
  2915. md_write_inc(conf->mddev, bi);
  2916. if (forwrite) {
  2917. /* check if page is covered */
  2918. sector_t sector = sh->dev[dd_idx].sector;
  2919. for (bi=sh->dev[dd_idx].towrite;
  2920. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  2921. bi && bi->bi_iter.bi_sector <= sector;
  2922. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  2923. if (bio_end_sector(bi) >= sector)
  2924. sector = bio_end_sector(bi);
  2925. }
  2926. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  2927. if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
  2928. sh->overwrite_disks++;
  2929. }
  2930. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  2931. (unsigned long long)(*bip)->bi_iter.bi_sector,
  2932. (unsigned long long)sh->sector, dd_idx);
  2933. if (conf->mddev->bitmap && firstwrite) {
  2934. /* Cannot hold spinlock over bitmap_startwrite,
  2935. * but must ensure this isn't added to a batch until
  2936. * we have added to the bitmap and set bm_seq.
  2937. * So set STRIPE_BITMAP_PENDING to prevent
  2938. * batching.
  2939. * If multiple add_stripe_bio() calls race here they
  2940. * much all set STRIPE_BITMAP_PENDING. So only the first one
  2941. * to complete "bitmap_startwrite" gets to set
  2942. * STRIPE_BIT_DELAY. This is important as once a stripe
  2943. * is added to a batch, STRIPE_BIT_DELAY cannot be changed
  2944. * any more.
  2945. */
  2946. set_bit(STRIPE_BITMAP_PENDING, &sh->state);
  2947. spin_unlock_irq(&sh->stripe_lock);
  2948. md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  2949. STRIPE_SECTORS, 0);
  2950. spin_lock_irq(&sh->stripe_lock);
  2951. clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
  2952. if (!sh->batch_head) {
  2953. sh->bm_seq = conf->seq_flush+1;
  2954. set_bit(STRIPE_BIT_DELAY, &sh->state);
  2955. }
  2956. }
  2957. spin_unlock_irq(&sh->stripe_lock);
  2958. if (stripe_can_batch(sh))
  2959. stripe_add_to_batch_list(conf, sh);
  2960. return 1;
  2961. overlap:
  2962. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  2963. spin_unlock_irq(&sh->stripe_lock);
  2964. return 0;
  2965. }
  2966. static void end_reshape(struct r5conf *conf);
  2967. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  2968. struct stripe_head *sh)
  2969. {
  2970. int sectors_per_chunk =
  2971. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  2972. int dd_idx;
  2973. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  2974. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  2975. raid5_compute_sector(conf,
  2976. stripe * (disks - conf->max_degraded)
  2977. *sectors_per_chunk + chunk_offset,
  2978. previous,
  2979. &dd_idx, sh);
  2980. }
  2981. static void
  2982. handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
  2983. struct stripe_head_state *s, int disks)
  2984. {
  2985. int i;
  2986. BUG_ON(sh->batch_head);
  2987. for (i = disks; i--; ) {
  2988. struct bio *bi;
  2989. int bitmap_end = 0;
  2990. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  2991. struct md_rdev *rdev;
  2992. rcu_read_lock();
  2993. rdev = rcu_dereference(conf->disks[i].rdev);
  2994. if (rdev && test_bit(In_sync, &rdev->flags) &&
  2995. !test_bit(Faulty, &rdev->flags))
  2996. atomic_inc(&rdev->nr_pending);
  2997. else
  2998. rdev = NULL;
  2999. rcu_read_unlock();
  3000. if (rdev) {
  3001. if (!rdev_set_badblocks(
  3002. rdev,
  3003. sh->sector,
  3004. STRIPE_SECTORS, 0))
  3005. md_error(conf->mddev, rdev);
  3006. rdev_dec_pending(rdev, conf->mddev);
  3007. }
  3008. }
  3009. spin_lock_irq(&sh->stripe_lock);
  3010. /* fail all writes first */
  3011. bi = sh->dev[i].towrite;
  3012. sh->dev[i].towrite = NULL;
  3013. sh->overwrite_disks = 0;
  3014. spin_unlock_irq(&sh->stripe_lock);
  3015. if (bi)
  3016. bitmap_end = 1;
  3017. log_stripe_write_finished(sh);
  3018. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  3019. wake_up(&conf->wait_for_overlap);
  3020. while (bi && bi->bi_iter.bi_sector <
  3021. sh->dev[i].sector + STRIPE_SECTORS) {
  3022. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  3023. md_write_end(conf->mddev);
  3024. bio_io_error(bi);
  3025. bi = nextbi;
  3026. }
  3027. if (bitmap_end)
  3028. md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  3029. STRIPE_SECTORS, 0, 0);
  3030. bitmap_end = 0;
  3031. /* and fail all 'written' */
  3032. bi = sh->dev[i].written;
  3033. sh->dev[i].written = NULL;
  3034. if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
  3035. WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
  3036. sh->dev[i].page = sh->dev[i].orig_page;
  3037. }
  3038. if (bi) bitmap_end = 1;
  3039. while (bi && bi->bi_iter.bi_sector <
  3040. sh->dev[i].sector + STRIPE_SECTORS) {
  3041. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  3042. md_write_end(conf->mddev);
  3043. bio_io_error(bi);
  3044. bi = bi2;
  3045. }
  3046. /* fail any reads if this device is non-operational and
  3047. * the data has not reached the cache yet.
  3048. */
  3049. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  3050. s->failed > conf->max_degraded &&
  3051. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  3052. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  3053. spin_lock_irq(&sh->stripe_lock);
  3054. bi = sh->dev[i].toread;
  3055. sh->dev[i].toread = NULL;
  3056. spin_unlock_irq(&sh->stripe_lock);
  3057. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  3058. wake_up(&conf->wait_for_overlap);
  3059. if (bi)
  3060. s->to_read--;
  3061. while (bi && bi->bi_iter.bi_sector <
  3062. sh->dev[i].sector + STRIPE_SECTORS) {
  3063. struct bio *nextbi =
  3064. r5_next_bio(bi, sh->dev[i].sector);
  3065. bio_io_error(bi);
  3066. bi = nextbi;
  3067. }
  3068. }
  3069. if (bitmap_end)
  3070. md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  3071. STRIPE_SECTORS, 0, 0);
  3072. /* If we were in the middle of a write the parity block might
  3073. * still be locked - so just clear all R5_LOCKED flags
  3074. */
  3075. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  3076. }
  3077. s->to_write = 0;
  3078. s->written = 0;
  3079. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  3080. if (atomic_dec_and_test(&conf->pending_full_writes))
  3081. md_wakeup_thread(conf->mddev->thread);
  3082. }
  3083. static void
  3084. handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
  3085. struct stripe_head_state *s)
  3086. {
  3087. int abort = 0;
  3088. int i;
  3089. BUG_ON(sh->batch_head);
  3090. clear_bit(STRIPE_SYNCING, &sh->state);
  3091. if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
  3092. wake_up(&conf->wait_for_overlap);
  3093. s->syncing = 0;
  3094. s->replacing = 0;
  3095. /* There is nothing more to do for sync/check/repair.
  3096. * Don't even need to abort as that is handled elsewhere
  3097. * if needed, and not always wanted e.g. if there is a known
  3098. * bad block here.
  3099. * For recover/replace we need to record a bad block on all
  3100. * non-sync devices, or abort the recovery
  3101. */
  3102. if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
  3103. /* During recovery devices cannot be removed, so
  3104. * locking and refcounting of rdevs is not needed
  3105. */
  3106. rcu_read_lock();
  3107. for (i = 0; i < conf->raid_disks; i++) {
  3108. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  3109. if (rdev
  3110. && !test_bit(Faulty, &rdev->flags)
  3111. && !test_bit(In_sync, &rdev->flags)
  3112. && !rdev_set_badblocks(rdev, sh->sector,
  3113. STRIPE_SECTORS, 0))
  3114. abort = 1;
  3115. rdev = rcu_dereference(conf->disks[i].replacement);
  3116. if (rdev
  3117. && !test_bit(Faulty, &rdev->flags)
  3118. && !test_bit(In_sync, &rdev->flags)
  3119. && !rdev_set_badblocks(rdev, sh->sector,
  3120. STRIPE_SECTORS, 0))
  3121. abort = 1;
  3122. }
  3123. rcu_read_unlock();
  3124. if (abort)
  3125. conf->recovery_disabled =
  3126. conf->mddev->recovery_disabled;
  3127. }
  3128. md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
  3129. }
  3130. static int want_replace(struct stripe_head *sh, int disk_idx)
  3131. {
  3132. struct md_rdev *rdev;
  3133. int rv = 0;
  3134. rcu_read_lock();
  3135. rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
  3136. if (rdev
  3137. && !test_bit(Faulty, &rdev->flags)
  3138. && !test_bit(In_sync, &rdev->flags)
  3139. && (rdev->recovery_offset <= sh->sector
  3140. || rdev->mddev->recovery_cp <= sh->sector))
  3141. rv = 1;
  3142. rcu_read_unlock();
  3143. return rv;
  3144. }
  3145. static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
  3146. int disk_idx, int disks)
  3147. {
  3148. struct r5dev *dev = &sh->dev[disk_idx];
  3149. struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
  3150. &sh->dev[s->failed_num[1]] };
  3151. int i;
  3152. if (test_bit(R5_LOCKED, &dev->flags) ||
  3153. test_bit(R5_UPTODATE, &dev->flags))
  3154. /* No point reading this as we already have it or have
  3155. * decided to get it.
  3156. */
  3157. return 0;
  3158. if (dev->toread ||
  3159. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
  3160. /* We need this block to directly satisfy a request */
  3161. return 1;
  3162. if (s->syncing || s->expanding ||
  3163. (s->replacing && want_replace(sh, disk_idx)))
  3164. /* When syncing, or expanding we read everything.
  3165. * When replacing, we need the replaced block.
  3166. */
  3167. return 1;
  3168. if ((s->failed >= 1 && fdev[0]->toread) ||
  3169. (s->failed >= 2 && fdev[1]->toread))
  3170. /* If we want to read from a failed device, then
  3171. * we need to actually read every other device.
  3172. */
  3173. return 1;
  3174. /* Sometimes neither read-modify-write nor reconstruct-write
  3175. * cycles can work. In those cases we read every block we
  3176. * can. Then the parity-update is certain to have enough to
  3177. * work with.
  3178. * This can only be a problem when we need to write something,
  3179. * and some device has failed. If either of those tests
  3180. * fail we need look no further.
  3181. */
  3182. if (!s->failed || !s->to_write)
  3183. return 0;
  3184. if (test_bit(R5_Insync, &dev->flags) &&
  3185. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3186. /* Pre-reads at not permitted until after short delay
  3187. * to gather multiple requests. However if this
  3188. * device is no Insync, the block could only be computed
  3189. * and there is no need to delay that.
  3190. */
  3191. return 0;
  3192. for (i = 0; i < s->failed && i < 2; i++) {
  3193. if (fdev[i]->towrite &&
  3194. !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
  3195. !test_bit(R5_OVERWRITE, &fdev[i]->flags))
  3196. /* If we have a partial write to a failed
  3197. * device, then we will need to reconstruct
  3198. * the content of that device, so all other
  3199. * devices must be read.
  3200. */
  3201. return 1;
  3202. }
  3203. /* If we are forced to do a reconstruct-write, either because
  3204. * the current RAID6 implementation only supports that, or
  3205. * because parity cannot be trusted and we are currently
  3206. * recovering it, there is extra need to be careful.
  3207. * If one of the devices that we would need to read, because
  3208. * it is not being overwritten (and maybe not written at all)
  3209. * is missing/faulty, then we need to read everything we can.
  3210. */
  3211. if (sh->raid_conf->level != 6 &&
  3212. sh->sector < sh->raid_conf->mddev->recovery_cp)
  3213. /* reconstruct-write isn't being forced */
  3214. return 0;
  3215. for (i = 0; i < s->failed && i < 2; i++) {
  3216. if (s->failed_num[i] != sh->pd_idx &&
  3217. s->failed_num[i] != sh->qd_idx &&
  3218. !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
  3219. !test_bit(R5_OVERWRITE, &fdev[i]->flags))
  3220. return 1;
  3221. }
  3222. return 0;
  3223. }
  3224. /* fetch_block - checks the given member device to see if its data needs
  3225. * to be read or computed to satisfy a request.
  3226. *
  3227. * Returns 1 when no more member devices need to be checked, otherwise returns
  3228. * 0 to tell the loop in handle_stripe_fill to continue
  3229. */
  3230. static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
  3231. int disk_idx, int disks)
  3232. {
  3233. struct r5dev *dev = &sh->dev[disk_idx];
  3234. /* is the data in this block needed, and can we get it? */
  3235. if (need_this_block(sh, s, disk_idx, disks)) {
  3236. /* we would like to get this block, possibly by computing it,
  3237. * otherwise read it if the backing disk is insync
  3238. */
  3239. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  3240. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  3241. BUG_ON(sh->batch_head);
  3242. /*
  3243. * In the raid6 case if the only non-uptodate disk is P
  3244. * then we already trusted P to compute the other failed
  3245. * drives. It is safe to compute rather than re-read P.
  3246. * In other cases we only compute blocks from failed
  3247. * devices, otherwise check/repair might fail to detect
  3248. * a real inconsistency.
  3249. */
  3250. if ((s->uptodate == disks - 1) &&
  3251. ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
  3252. (s->failed && (disk_idx == s->failed_num[0] ||
  3253. disk_idx == s->failed_num[1])))) {
  3254. /* have disk failed, and we're requested to fetch it;
  3255. * do compute it
  3256. */
  3257. pr_debug("Computing stripe %llu block %d\n",
  3258. (unsigned long long)sh->sector, disk_idx);
  3259. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3260. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3261. set_bit(R5_Wantcompute, &dev->flags);
  3262. sh->ops.target = disk_idx;
  3263. sh->ops.target2 = -1; /* no 2nd target */
  3264. s->req_compute = 1;
  3265. /* Careful: from this point on 'uptodate' is in the eye
  3266. * of raid_run_ops which services 'compute' operations
  3267. * before writes. R5_Wantcompute flags a block that will
  3268. * be R5_UPTODATE by the time it is needed for a
  3269. * subsequent operation.
  3270. */
  3271. s->uptodate++;
  3272. return 1;
  3273. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  3274. /* Computing 2-failure is *very* expensive; only
  3275. * do it if failed >= 2
  3276. */
  3277. int other;
  3278. for (other = disks; other--; ) {
  3279. if (other == disk_idx)
  3280. continue;
  3281. if (!test_bit(R5_UPTODATE,
  3282. &sh->dev[other].flags))
  3283. break;
  3284. }
  3285. BUG_ON(other < 0);
  3286. pr_debug("Computing stripe %llu blocks %d,%d\n",
  3287. (unsigned long long)sh->sector,
  3288. disk_idx, other);
  3289. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3290. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3291. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  3292. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  3293. sh->ops.target = disk_idx;
  3294. sh->ops.target2 = other;
  3295. s->uptodate += 2;
  3296. s->req_compute = 1;
  3297. return 1;
  3298. } else if (test_bit(R5_Insync, &dev->flags)) {
  3299. set_bit(R5_LOCKED, &dev->flags);
  3300. set_bit(R5_Wantread, &dev->flags);
  3301. s->locked++;
  3302. pr_debug("Reading block %d (sync=%d)\n",
  3303. disk_idx, s->syncing);
  3304. }
  3305. }
  3306. return 0;
  3307. }
  3308. /**
  3309. * handle_stripe_fill - read or compute data to satisfy pending requests.
  3310. */
  3311. static void handle_stripe_fill(struct stripe_head *sh,
  3312. struct stripe_head_state *s,
  3313. int disks)
  3314. {
  3315. int i;
  3316. /* look for blocks to read/compute, skip this if a compute
  3317. * is already in flight, or if the stripe contents are in the
  3318. * midst of changing due to a write
  3319. */
  3320. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  3321. !sh->reconstruct_state) {
  3322. /*
  3323. * For degraded stripe with data in journal, do not handle
  3324. * read requests yet, instead, flush the stripe to raid
  3325. * disks first, this avoids handling complex rmw of write
  3326. * back cache (prexor with orig_page, and then xor with
  3327. * page) in the read path
  3328. */
  3329. if (s->injournal && s->failed) {
  3330. if (test_bit(STRIPE_R5C_CACHING, &sh->state))
  3331. r5c_make_stripe_write_out(sh);
  3332. goto out;
  3333. }
  3334. for (i = disks; i--; )
  3335. if (fetch_block(sh, s, i, disks))
  3336. break;
  3337. }
  3338. out:
  3339. set_bit(STRIPE_HANDLE, &sh->state);
  3340. }
  3341. static void break_stripe_batch_list(struct stripe_head *head_sh,
  3342. unsigned long handle_flags);
  3343. /* handle_stripe_clean_event
  3344. * any written block on an uptodate or failed drive can be returned.
  3345. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  3346. * never LOCKED, so we don't need to test 'failed' directly.
  3347. */
  3348. static void handle_stripe_clean_event(struct r5conf *conf,
  3349. struct stripe_head *sh, int disks)
  3350. {
  3351. int i;
  3352. struct r5dev *dev;
  3353. int discard_pending = 0;
  3354. struct stripe_head *head_sh = sh;
  3355. bool do_endio = false;
  3356. for (i = disks; i--; )
  3357. if (sh->dev[i].written) {
  3358. dev = &sh->dev[i];
  3359. if (!test_bit(R5_LOCKED, &dev->flags) &&
  3360. (test_bit(R5_UPTODATE, &dev->flags) ||
  3361. test_bit(R5_Discard, &dev->flags) ||
  3362. test_bit(R5_SkipCopy, &dev->flags))) {
  3363. /* We can return any write requests */
  3364. struct bio *wbi, *wbi2;
  3365. pr_debug("Return write for disc %d\n", i);
  3366. if (test_and_clear_bit(R5_Discard, &dev->flags))
  3367. clear_bit(R5_UPTODATE, &dev->flags);
  3368. if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
  3369. WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
  3370. }
  3371. do_endio = true;
  3372. returnbi:
  3373. dev->page = dev->orig_page;
  3374. wbi = dev->written;
  3375. dev->written = NULL;
  3376. while (wbi && wbi->bi_iter.bi_sector <
  3377. dev->sector + STRIPE_SECTORS) {
  3378. wbi2 = r5_next_bio(wbi, dev->sector);
  3379. md_write_end(conf->mddev);
  3380. bio_endio(wbi);
  3381. wbi = wbi2;
  3382. }
  3383. md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  3384. STRIPE_SECTORS,
  3385. !test_bit(STRIPE_DEGRADED, &sh->state),
  3386. 0);
  3387. if (head_sh->batch_head) {
  3388. sh = list_first_entry(&sh->batch_list,
  3389. struct stripe_head,
  3390. batch_list);
  3391. if (sh != head_sh) {
  3392. dev = &sh->dev[i];
  3393. goto returnbi;
  3394. }
  3395. }
  3396. sh = head_sh;
  3397. dev = &sh->dev[i];
  3398. } else if (test_bit(R5_Discard, &dev->flags))
  3399. discard_pending = 1;
  3400. }
  3401. log_stripe_write_finished(sh);
  3402. if (!discard_pending &&
  3403. test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
  3404. int hash;
  3405. clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
  3406. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  3407. if (sh->qd_idx >= 0) {
  3408. clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
  3409. clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
  3410. }
  3411. /* now that discard is done we can proceed with any sync */
  3412. clear_bit(STRIPE_DISCARD, &sh->state);
  3413. /*
  3414. * SCSI discard will change some bio fields and the stripe has
  3415. * no updated data, so remove it from hash list and the stripe
  3416. * will be reinitialized
  3417. */
  3418. unhash:
  3419. hash = sh->hash_lock_index;
  3420. spin_lock_irq(conf->hash_locks + hash);
  3421. remove_hash(sh);
  3422. spin_unlock_irq(conf->hash_locks + hash);
  3423. if (head_sh->batch_head) {
  3424. sh = list_first_entry(&sh->batch_list,
  3425. struct stripe_head, batch_list);
  3426. if (sh != head_sh)
  3427. goto unhash;
  3428. }
  3429. sh = head_sh;
  3430. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
  3431. set_bit(STRIPE_HANDLE, &sh->state);
  3432. }
  3433. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  3434. if (atomic_dec_and_test(&conf->pending_full_writes))
  3435. md_wakeup_thread(conf->mddev->thread);
  3436. if (head_sh->batch_head && do_endio)
  3437. break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
  3438. }
  3439. /*
  3440. * For RMW in write back cache, we need extra page in prexor to store the
  3441. * old data. This page is stored in dev->orig_page.
  3442. *
  3443. * This function checks whether we have data for prexor. The exact logic
  3444. * is:
  3445. * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
  3446. */
  3447. static inline bool uptodate_for_rmw(struct r5dev *dev)
  3448. {
  3449. return (test_bit(R5_UPTODATE, &dev->flags)) &&
  3450. (!test_bit(R5_InJournal, &dev->flags) ||
  3451. test_bit(R5_OrigPageUPTDODATE, &dev->flags));
  3452. }
  3453. static int handle_stripe_dirtying(struct r5conf *conf,
  3454. struct stripe_head *sh,
  3455. struct stripe_head_state *s,
  3456. int disks)
  3457. {
  3458. int rmw = 0, rcw = 0, i;
  3459. sector_t recovery_cp = conf->mddev->recovery_cp;
  3460. /* Check whether resync is now happening or should start.
  3461. * If yes, then the array is dirty (after unclean shutdown or
  3462. * initial creation), so parity in some stripes might be inconsistent.
  3463. * In this case, we need to always do reconstruct-write, to ensure
  3464. * that in case of drive failure or read-error correction, we
  3465. * generate correct data from the parity.
  3466. */
  3467. if (conf->rmw_level == PARITY_DISABLE_RMW ||
  3468. (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
  3469. s->failed == 0)) {
  3470. /* Calculate the real rcw later - for now make it
  3471. * look like rcw is cheaper
  3472. */
  3473. rcw = 1; rmw = 2;
  3474. pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
  3475. conf->rmw_level, (unsigned long long)recovery_cp,
  3476. (unsigned long long)sh->sector);
  3477. } else for (i = disks; i--; ) {
  3478. /* would I have to read this buffer for read_modify_write */
  3479. struct r5dev *dev = &sh->dev[i];
  3480. if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
  3481. i == sh->pd_idx || i == sh->qd_idx ||
  3482. test_bit(R5_InJournal, &dev->flags)) &&
  3483. !test_bit(R5_LOCKED, &dev->flags) &&
  3484. !(uptodate_for_rmw(dev) ||
  3485. test_bit(R5_Wantcompute, &dev->flags))) {
  3486. if (test_bit(R5_Insync, &dev->flags))
  3487. rmw++;
  3488. else
  3489. rmw += 2*disks; /* cannot read it */
  3490. }
  3491. /* Would I have to read this buffer for reconstruct_write */
  3492. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  3493. i != sh->pd_idx && i != sh->qd_idx &&
  3494. !test_bit(R5_LOCKED, &dev->flags) &&
  3495. !(test_bit(R5_UPTODATE, &dev->flags) ||
  3496. test_bit(R5_Wantcompute, &dev->flags))) {
  3497. if (test_bit(R5_Insync, &dev->flags))
  3498. rcw++;
  3499. else
  3500. rcw += 2*disks;
  3501. }
  3502. }
  3503. pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
  3504. (unsigned long long)sh->sector, sh->state, rmw, rcw);
  3505. set_bit(STRIPE_HANDLE, &sh->state);
  3506. if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
  3507. /* prefer read-modify-write, but need to get some data */
  3508. if (conf->mddev->queue)
  3509. blk_add_trace_msg(conf->mddev->queue,
  3510. "raid5 rmw %llu %d",
  3511. (unsigned long long)sh->sector, rmw);
  3512. for (i = disks; i--; ) {
  3513. struct r5dev *dev = &sh->dev[i];
  3514. if (test_bit(R5_InJournal, &dev->flags) &&
  3515. dev->page == dev->orig_page &&
  3516. !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
  3517. /* alloc page for prexor */
  3518. struct page *p = alloc_page(GFP_NOIO);
  3519. if (p) {
  3520. dev->orig_page = p;
  3521. continue;
  3522. }
  3523. /*
  3524. * alloc_page() failed, try use
  3525. * disk_info->extra_page
  3526. */
  3527. if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
  3528. &conf->cache_state)) {
  3529. r5c_use_extra_page(sh);
  3530. break;
  3531. }
  3532. /* extra_page in use, add to delayed_list */
  3533. set_bit(STRIPE_DELAYED, &sh->state);
  3534. s->waiting_extra_page = 1;
  3535. return -EAGAIN;
  3536. }
  3537. }
  3538. for (i = disks; i--; ) {
  3539. struct r5dev *dev = &sh->dev[i];
  3540. if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
  3541. i == sh->pd_idx || i == sh->qd_idx ||
  3542. test_bit(R5_InJournal, &dev->flags)) &&
  3543. !test_bit(R5_LOCKED, &dev->flags) &&
  3544. !(uptodate_for_rmw(dev) ||
  3545. test_bit(R5_Wantcompute, &dev->flags)) &&
  3546. test_bit(R5_Insync, &dev->flags)) {
  3547. if (test_bit(STRIPE_PREREAD_ACTIVE,
  3548. &sh->state)) {
  3549. pr_debug("Read_old block %d for r-m-w\n",
  3550. i);
  3551. set_bit(R5_LOCKED, &dev->flags);
  3552. set_bit(R5_Wantread, &dev->flags);
  3553. s->locked++;
  3554. } else {
  3555. set_bit(STRIPE_DELAYED, &sh->state);
  3556. set_bit(STRIPE_HANDLE, &sh->state);
  3557. }
  3558. }
  3559. }
  3560. }
  3561. if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
  3562. /* want reconstruct write, but need to get some data */
  3563. int qread =0;
  3564. rcw = 0;
  3565. for (i = disks; i--; ) {
  3566. struct r5dev *dev = &sh->dev[i];
  3567. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  3568. i != sh->pd_idx && i != sh->qd_idx &&
  3569. !test_bit(R5_LOCKED, &dev->flags) &&
  3570. !(test_bit(R5_UPTODATE, &dev->flags) ||
  3571. test_bit(R5_Wantcompute, &dev->flags))) {
  3572. rcw++;
  3573. if (test_bit(R5_Insync, &dev->flags) &&
  3574. test_bit(STRIPE_PREREAD_ACTIVE,
  3575. &sh->state)) {
  3576. pr_debug("Read_old block "
  3577. "%d for Reconstruct\n", i);
  3578. set_bit(R5_LOCKED, &dev->flags);
  3579. set_bit(R5_Wantread, &dev->flags);
  3580. s->locked++;
  3581. qread++;
  3582. } else {
  3583. set_bit(STRIPE_DELAYED, &sh->state);
  3584. set_bit(STRIPE_HANDLE, &sh->state);
  3585. }
  3586. }
  3587. }
  3588. if (rcw && conf->mddev->queue)
  3589. blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
  3590. (unsigned long long)sh->sector,
  3591. rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
  3592. }
  3593. if (rcw > disks && rmw > disks &&
  3594. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3595. set_bit(STRIPE_DELAYED, &sh->state);
  3596. /* now if nothing is locked, and if we have enough data,
  3597. * we can start a write request
  3598. */
  3599. /* since handle_stripe can be called at any time we need to handle the
  3600. * case where a compute block operation has been submitted and then a
  3601. * subsequent call wants to start a write request. raid_run_ops only
  3602. * handles the case where compute block and reconstruct are requested
  3603. * simultaneously. If this is not the case then new writes need to be
  3604. * held off until the compute completes.
  3605. */
  3606. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  3607. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  3608. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  3609. schedule_reconstruction(sh, s, rcw == 0, 0);
  3610. return 0;
  3611. }
  3612. static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
  3613. struct stripe_head_state *s, int disks)
  3614. {
  3615. struct r5dev *dev = NULL;
  3616. BUG_ON(sh->batch_head);
  3617. set_bit(STRIPE_HANDLE, &sh->state);
  3618. switch (sh->check_state) {
  3619. case check_state_idle:
  3620. /* start a new check operation if there are no failures */
  3621. if (s->failed == 0) {
  3622. BUG_ON(s->uptodate != disks);
  3623. sh->check_state = check_state_run;
  3624. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  3625. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  3626. s->uptodate--;
  3627. break;
  3628. }
  3629. dev = &sh->dev[s->failed_num[0]];
  3630. /* fall through */
  3631. case check_state_compute_result:
  3632. sh->check_state = check_state_idle;
  3633. if (!dev)
  3634. dev = &sh->dev[sh->pd_idx];
  3635. /* check that a write has not made the stripe insync */
  3636. if (test_bit(STRIPE_INSYNC, &sh->state))
  3637. break;
  3638. /* either failed parity check, or recovery is happening */
  3639. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  3640. BUG_ON(s->uptodate != disks);
  3641. set_bit(R5_LOCKED, &dev->flags);
  3642. s->locked++;
  3643. set_bit(R5_Wantwrite, &dev->flags);
  3644. clear_bit(STRIPE_DEGRADED, &sh->state);
  3645. set_bit(STRIPE_INSYNC, &sh->state);
  3646. break;
  3647. case check_state_run:
  3648. break; /* we will be called again upon completion */
  3649. case check_state_check_result:
  3650. sh->check_state = check_state_idle;
  3651. /* if a failure occurred during the check operation, leave
  3652. * STRIPE_INSYNC not set and let the stripe be handled again
  3653. */
  3654. if (s->failed)
  3655. break;
  3656. /* handle a successful check operation, if parity is correct
  3657. * we are done. Otherwise update the mismatch count and repair
  3658. * parity if !MD_RECOVERY_CHECK
  3659. */
  3660. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  3661. /* parity is correct (on disc,
  3662. * not in buffer any more)
  3663. */
  3664. set_bit(STRIPE_INSYNC, &sh->state);
  3665. else {
  3666. atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
  3667. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
  3668. /* don't try to repair!! */
  3669. set_bit(STRIPE_INSYNC, &sh->state);
  3670. pr_warn_ratelimited("%s: mismatch sector in range "
  3671. "%llu-%llu\n", mdname(conf->mddev),
  3672. (unsigned long long) sh->sector,
  3673. (unsigned long long) sh->sector +
  3674. STRIPE_SECTORS);
  3675. } else {
  3676. sh->check_state = check_state_compute_run;
  3677. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3678. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3679. set_bit(R5_Wantcompute,
  3680. &sh->dev[sh->pd_idx].flags);
  3681. sh->ops.target = sh->pd_idx;
  3682. sh->ops.target2 = -1;
  3683. s->uptodate++;
  3684. }
  3685. }
  3686. break;
  3687. case check_state_compute_run:
  3688. break;
  3689. default:
  3690. pr_err("%s: unknown check_state: %d sector: %llu\n",
  3691. __func__, sh->check_state,
  3692. (unsigned long long) sh->sector);
  3693. BUG();
  3694. }
  3695. }
  3696. static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
  3697. struct stripe_head_state *s,
  3698. int disks)
  3699. {
  3700. int pd_idx = sh->pd_idx;
  3701. int qd_idx = sh->qd_idx;
  3702. struct r5dev *dev;
  3703. BUG_ON(sh->batch_head);
  3704. set_bit(STRIPE_HANDLE, &sh->state);
  3705. BUG_ON(s->failed > 2);
  3706. /* Want to check and possibly repair P and Q.
  3707. * However there could be one 'failed' device, in which
  3708. * case we can only check one of them, possibly using the
  3709. * other to generate missing data
  3710. */
  3711. switch (sh->check_state) {
  3712. case check_state_idle:
  3713. /* start a new check operation if there are < 2 failures */
  3714. if (s->failed == s->q_failed) {
  3715. /* The only possible failed device holds Q, so it
  3716. * makes sense to check P (If anything else were failed,
  3717. * we would have used P to recreate it).
  3718. */
  3719. sh->check_state = check_state_run;
  3720. }
  3721. if (!s->q_failed && s->failed < 2) {
  3722. /* Q is not failed, and we didn't use it to generate
  3723. * anything, so it makes sense to check it
  3724. */
  3725. if (sh->check_state == check_state_run)
  3726. sh->check_state = check_state_run_pq;
  3727. else
  3728. sh->check_state = check_state_run_q;
  3729. }
  3730. /* discard potentially stale zero_sum_result */
  3731. sh->ops.zero_sum_result = 0;
  3732. if (sh->check_state == check_state_run) {
  3733. /* async_xor_zero_sum destroys the contents of P */
  3734. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  3735. s->uptodate--;
  3736. }
  3737. if (sh->check_state >= check_state_run &&
  3738. sh->check_state <= check_state_run_pq) {
  3739. /* async_syndrome_zero_sum preserves P and Q, so
  3740. * no need to mark them !uptodate here
  3741. */
  3742. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  3743. break;
  3744. }
  3745. /* we have 2-disk failure */
  3746. BUG_ON(s->failed != 2);
  3747. /* fall through */
  3748. case check_state_compute_result:
  3749. sh->check_state = check_state_idle;
  3750. /* check that a write has not made the stripe insync */
  3751. if (test_bit(STRIPE_INSYNC, &sh->state))
  3752. break;
  3753. /* now write out any block on a failed drive,
  3754. * or P or Q if they were recomputed
  3755. */
  3756. dev = NULL;
  3757. if (s->failed == 2) {
  3758. dev = &sh->dev[s->failed_num[1]];
  3759. s->locked++;
  3760. set_bit(R5_LOCKED, &dev->flags);
  3761. set_bit(R5_Wantwrite, &dev->flags);
  3762. }
  3763. if (s->failed >= 1) {
  3764. dev = &sh->dev[s->failed_num[0]];
  3765. s->locked++;
  3766. set_bit(R5_LOCKED, &dev->flags);
  3767. set_bit(R5_Wantwrite, &dev->flags);
  3768. }
  3769. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  3770. dev = &sh->dev[pd_idx];
  3771. s->locked++;
  3772. set_bit(R5_LOCKED, &dev->flags);
  3773. set_bit(R5_Wantwrite, &dev->flags);
  3774. }
  3775. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  3776. dev = &sh->dev[qd_idx];
  3777. s->locked++;
  3778. set_bit(R5_LOCKED, &dev->flags);
  3779. set_bit(R5_Wantwrite, &dev->flags);
  3780. }
  3781. if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
  3782. "%s: disk%td not up to date\n",
  3783. mdname(conf->mddev),
  3784. dev - (struct r5dev *) &sh->dev)) {
  3785. clear_bit(R5_LOCKED, &dev->flags);
  3786. clear_bit(R5_Wantwrite, &dev->flags);
  3787. s->locked--;
  3788. }
  3789. clear_bit(STRIPE_DEGRADED, &sh->state);
  3790. set_bit(STRIPE_INSYNC, &sh->state);
  3791. break;
  3792. case check_state_run:
  3793. case check_state_run_q:
  3794. case check_state_run_pq:
  3795. break; /* we will be called again upon completion */
  3796. case check_state_check_result:
  3797. sh->check_state = check_state_idle;
  3798. /* handle a successful check operation, if parity is correct
  3799. * we are done. Otherwise update the mismatch count and repair
  3800. * parity if !MD_RECOVERY_CHECK
  3801. */
  3802. if (sh->ops.zero_sum_result == 0) {
  3803. /* both parities are correct */
  3804. if (!s->failed)
  3805. set_bit(STRIPE_INSYNC, &sh->state);
  3806. else {
  3807. /* in contrast to the raid5 case we can validate
  3808. * parity, but still have a failure to write
  3809. * back
  3810. */
  3811. sh->check_state = check_state_compute_result;
  3812. /* Returning at this point means that we may go
  3813. * off and bring p and/or q uptodate again so
  3814. * we make sure to check zero_sum_result again
  3815. * to verify if p or q need writeback
  3816. */
  3817. }
  3818. } else {
  3819. atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
  3820. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
  3821. /* don't try to repair!! */
  3822. set_bit(STRIPE_INSYNC, &sh->state);
  3823. pr_warn_ratelimited("%s: mismatch sector in range "
  3824. "%llu-%llu\n", mdname(conf->mddev),
  3825. (unsigned long long) sh->sector,
  3826. (unsigned long long) sh->sector +
  3827. STRIPE_SECTORS);
  3828. } else {
  3829. int *target = &sh->ops.target;
  3830. sh->ops.target = -1;
  3831. sh->ops.target2 = -1;
  3832. sh->check_state = check_state_compute_run;
  3833. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3834. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3835. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  3836. set_bit(R5_Wantcompute,
  3837. &sh->dev[pd_idx].flags);
  3838. *target = pd_idx;
  3839. target = &sh->ops.target2;
  3840. s->uptodate++;
  3841. }
  3842. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  3843. set_bit(R5_Wantcompute,
  3844. &sh->dev[qd_idx].flags);
  3845. *target = qd_idx;
  3846. s->uptodate++;
  3847. }
  3848. }
  3849. }
  3850. break;
  3851. case check_state_compute_run:
  3852. break;
  3853. default:
  3854. pr_warn("%s: unknown check_state: %d sector: %llu\n",
  3855. __func__, sh->check_state,
  3856. (unsigned long long) sh->sector);
  3857. BUG();
  3858. }
  3859. }
  3860. static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
  3861. {
  3862. int i;
  3863. /* We have read all the blocks in this stripe and now we need to
  3864. * copy some of them into a target stripe for expand.
  3865. */
  3866. struct dma_async_tx_descriptor *tx = NULL;
  3867. BUG_ON(sh->batch_head);
  3868. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3869. for (i = 0; i < sh->disks; i++)
  3870. if (i != sh->pd_idx && i != sh->qd_idx) {
  3871. int dd_idx, j;
  3872. struct stripe_head *sh2;
  3873. struct async_submit_ctl submit;
  3874. sector_t bn = raid5_compute_blocknr(sh, i, 1);
  3875. sector_t s = raid5_compute_sector(conf, bn, 0,
  3876. &dd_idx, NULL);
  3877. sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
  3878. if (sh2 == NULL)
  3879. /* so far only the early blocks of this stripe
  3880. * have been requested. When later blocks
  3881. * get requested, we will try again
  3882. */
  3883. continue;
  3884. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  3885. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  3886. /* must have already done this block */
  3887. raid5_release_stripe(sh2);
  3888. continue;
  3889. }
  3890. /* place all the copies on one channel */
  3891. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  3892. tx = async_memcpy(sh2->dev[dd_idx].page,
  3893. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  3894. &submit);
  3895. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  3896. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  3897. for (j = 0; j < conf->raid_disks; j++)
  3898. if (j != sh2->pd_idx &&
  3899. j != sh2->qd_idx &&
  3900. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  3901. break;
  3902. if (j == conf->raid_disks) {
  3903. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  3904. set_bit(STRIPE_HANDLE, &sh2->state);
  3905. }
  3906. raid5_release_stripe(sh2);
  3907. }
  3908. /* done submitting copies, wait for them to complete */
  3909. async_tx_quiesce(&tx);
  3910. }
  3911. /*
  3912. * handle_stripe - do things to a stripe.
  3913. *
  3914. * We lock the stripe by setting STRIPE_ACTIVE and then examine the
  3915. * state of various bits to see what needs to be done.
  3916. * Possible results:
  3917. * return some read requests which now have data
  3918. * return some write requests which are safely on storage
  3919. * schedule a read on some buffers
  3920. * schedule a write of some buffers
  3921. * return confirmation of parity correctness
  3922. *
  3923. */
  3924. static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
  3925. {
  3926. struct r5conf *conf = sh->raid_conf;
  3927. int disks = sh->disks;
  3928. struct r5dev *dev;
  3929. int i;
  3930. int do_recovery = 0;
  3931. memset(s, 0, sizeof(*s));
  3932. s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
  3933. s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
  3934. s->failed_num[0] = -1;
  3935. s->failed_num[1] = -1;
  3936. s->log_failed = r5l_log_disk_error(conf);
  3937. /* Now to look around and see what can be done */
  3938. rcu_read_lock();
  3939. for (i=disks; i--; ) {
  3940. struct md_rdev *rdev;
  3941. sector_t first_bad;
  3942. int bad_sectors;
  3943. int is_bad = 0;
  3944. dev = &sh->dev[i];
  3945. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  3946. i, dev->flags,
  3947. dev->toread, dev->towrite, dev->written);
  3948. /* maybe we can reply to a read
  3949. *
  3950. * new wantfill requests are only permitted while
  3951. * ops_complete_biofill is guaranteed to be inactive
  3952. */
  3953. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  3954. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  3955. set_bit(R5_Wantfill, &dev->flags);
  3956. /* now count some things */
  3957. if (test_bit(R5_LOCKED, &dev->flags))
  3958. s->locked++;
  3959. if (test_bit(R5_UPTODATE, &dev->flags))
  3960. s->uptodate++;
  3961. if (test_bit(R5_Wantcompute, &dev->flags)) {
  3962. s->compute++;
  3963. BUG_ON(s->compute > 2);
  3964. }
  3965. if (test_bit(R5_Wantfill, &dev->flags))
  3966. s->to_fill++;
  3967. else if (dev->toread)
  3968. s->to_read++;
  3969. if (dev->towrite) {
  3970. s->to_write++;
  3971. if (!test_bit(R5_OVERWRITE, &dev->flags))
  3972. s->non_overwrite++;
  3973. }
  3974. if (dev->written)
  3975. s->written++;
  3976. /* Prefer to use the replacement for reads, but only
  3977. * if it is recovered enough and has no bad blocks.
  3978. */
  3979. rdev = rcu_dereference(conf->disks[i].replacement);
  3980. if (rdev && !test_bit(Faulty, &rdev->flags) &&
  3981. rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
  3982. !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  3983. &first_bad, &bad_sectors))
  3984. set_bit(R5_ReadRepl, &dev->flags);
  3985. else {
  3986. if (rdev && !test_bit(Faulty, &rdev->flags))
  3987. set_bit(R5_NeedReplace, &dev->flags);
  3988. else
  3989. clear_bit(R5_NeedReplace, &dev->flags);
  3990. rdev = rcu_dereference(conf->disks[i].rdev);
  3991. clear_bit(R5_ReadRepl, &dev->flags);
  3992. }
  3993. if (rdev && test_bit(Faulty, &rdev->flags))
  3994. rdev = NULL;
  3995. if (rdev) {
  3996. is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  3997. &first_bad, &bad_sectors);
  3998. if (s->blocked_rdev == NULL
  3999. && (test_bit(Blocked, &rdev->flags)
  4000. || is_bad < 0)) {
  4001. if (is_bad < 0)
  4002. set_bit(BlockedBadBlocks,
  4003. &rdev->flags);
  4004. s->blocked_rdev = rdev;
  4005. atomic_inc(&rdev->nr_pending);
  4006. }
  4007. }
  4008. clear_bit(R5_Insync, &dev->flags);
  4009. if (!rdev)
  4010. /* Not in-sync */;
  4011. else if (is_bad) {
  4012. /* also not in-sync */
  4013. if (!test_bit(WriteErrorSeen, &rdev->flags) &&
  4014. test_bit(R5_UPTODATE, &dev->flags)) {
  4015. /* treat as in-sync, but with a read error
  4016. * which we can now try to correct
  4017. */
  4018. set_bit(R5_Insync, &dev->flags);
  4019. set_bit(R5_ReadError, &dev->flags);
  4020. }
  4021. } else if (test_bit(In_sync, &rdev->flags))
  4022. set_bit(R5_Insync, &dev->flags);
  4023. else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  4024. /* in sync if before recovery_offset */
  4025. set_bit(R5_Insync, &dev->flags);
  4026. else if (test_bit(R5_UPTODATE, &dev->flags) &&
  4027. test_bit(R5_Expanded, &dev->flags))
  4028. /* If we've reshaped into here, we assume it is Insync.
  4029. * We will shortly update recovery_offset to make
  4030. * it official.
  4031. */
  4032. set_bit(R5_Insync, &dev->flags);
  4033. if (test_bit(R5_WriteError, &dev->flags)) {
  4034. /* This flag does not apply to '.replacement'
  4035. * only to .rdev, so make sure to check that*/
  4036. struct md_rdev *rdev2 = rcu_dereference(
  4037. conf->disks[i].rdev);
  4038. if (rdev2 == rdev)
  4039. clear_bit(R5_Insync, &dev->flags);
  4040. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  4041. s->handle_bad_blocks = 1;
  4042. atomic_inc(&rdev2->nr_pending);
  4043. } else
  4044. clear_bit(R5_WriteError, &dev->flags);
  4045. }
  4046. if (test_bit(R5_MadeGood, &dev->flags)) {
  4047. /* This flag does not apply to '.replacement'
  4048. * only to .rdev, so make sure to check that*/
  4049. struct md_rdev *rdev2 = rcu_dereference(
  4050. conf->disks[i].rdev);
  4051. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  4052. s->handle_bad_blocks = 1;
  4053. atomic_inc(&rdev2->nr_pending);
  4054. } else
  4055. clear_bit(R5_MadeGood, &dev->flags);
  4056. }
  4057. if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
  4058. struct md_rdev *rdev2 = rcu_dereference(
  4059. conf->disks[i].replacement);
  4060. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  4061. s->handle_bad_blocks = 1;
  4062. atomic_inc(&rdev2->nr_pending);
  4063. } else
  4064. clear_bit(R5_MadeGoodRepl, &dev->flags);
  4065. }
  4066. if (!test_bit(R5_Insync, &dev->flags)) {
  4067. /* The ReadError flag will just be confusing now */
  4068. clear_bit(R5_ReadError, &dev->flags);
  4069. clear_bit(R5_ReWrite, &dev->flags);
  4070. }
  4071. if (test_bit(R5_ReadError, &dev->flags))
  4072. clear_bit(R5_Insync, &dev->flags);
  4073. if (!test_bit(R5_Insync, &dev->flags)) {
  4074. if (s->failed < 2)
  4075. s->failed_num[s->failed] = i;
  4076. s->failed++;
  4077. if (rdev && !test_bit(Faulty, &rdev->flags))
  4078. do_recovery = 1;
  4079. else if (!rdev) {
  4080. rdev = rcu_dereference(
  4081. conf->disks[i].replacement);
  4082. if (rdev && !test_bit(Faulty, &rdev->flags))
  4083. do_recovery = 1;
  4084. }
  4085. }
  4086. if (test_bit(R5_InJournal, &dev->flags))
  4087. s->injournal++;
  4088. if (test_bit(R5_InJournal, &dev->flags) && dev->written)
  4089. s->just_cached++;
  4090. }
  4091. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  4092. /* If there is a failed device being replaced,
  4093. * we must be recovering.
  4094. * else if we are after recovery_cp, we must be syncing
  4095. * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
  4096. * else we can only be replacing
  4097. * sync and recovery both need to read all devices, and so
  4098. * use the same flag.
  4099. */
  4100. if (do_recovery ||
  4101. sh->sector >= conf->mddev->recovery_cp ||
  4102. test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
  4103. s->syncing = 1;
  4104. else
  4105. s->replacing = 1;
  4106. }
  4107. rcu_read_unlock();
  4108. }
  4109. static int clear_batch_ready(struct stripe_head *sh)
  4110. {
  4111. /* Return '1' if this is a member of batch, or
  4112. * '0' if it is a lone stripe or a head which can now be
  4113. * handled.
  4114. */
  4115. struct stripe_head *tmp;
  4116. if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
  4117. return (sh->batch_head && sh->batch_head != sh);
  4118. spin_lock(&sh->stripe_lock);
  4119. if (!sh->batch_head) {
  4120. spin_unlock(&sh->stripe_lock);
  4121. return 0;
  4122. }
  4123. /*
  4124. * this stripe could be added to a batch list before we check
  4125. * BATCH_READY, skips it
  4126. */
  4127. if (sh->batch_head != sh) {
  4128. spin_unlock(&sh->stripe_lock);
  4129. return 1;
  4130. }
  4131. spin_lock(&sh->batch_lock);
  4132. list_for_each_entry(tmp, &sh->batch_list, batch_list)
  4133. clear_bit(STRIPE_BATCH_READY, &tmp->state);
  4134. spin_unlock(&sh->batch_lock);
  4135. spin_unlock(&sh->stripe_lock);
  4136. /*
  4137. * BATCH_READY is cleared, no new stripes can be added.
  4138. * batch_list can be accessed without lock
  4139. */
  4140. return 0;
  4141. }
  4142. static void break_stripe_batch_list(struct stripe_head *head_sh,
  4143. unsigned long handle_flags)
  4144. {
  4145. struct stripe_head *sh, *next;
  4146. int i;
  4147. int do_wakeup = 0;
  4148. list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
  4149. list_del_init(&sh->batch_list);
  4150. WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
  4151. (1 << STRIPE_SYNCING) |
  4152. (1 << STRIPE_REPLACED) |
  4153. (1 << STRIPE_DELAYED) |
  4154. (1 << STRIPE_BIT_DELAY) |
  4155. (1 << STRIPE_FULL_WRITE) |
  4156. (1 << STRIPE_BIOFILL_RUN) |
  4157. (1 << STRIPE_COMPUTE_RUN) |
  4158. (1 << STRIPE_OPS_REQ_PENDING) |
  4159. (1 << STRIPE_DISCARD) |
  4160. (1 << STRIPE_BATCH_READY) |
  4161. (1 << STRIPE_BATCH_ERR) |
  4162. (1 << STRIPE_BITMAP_PENDING)),
  4163. "stripe state: %lx\n", sh->state);
  4164. WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
  4165. (1 << STRIPE_REPLACED)),
  4166. "head stripe state: %lx\n", head_sh->state);
  4167. set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
  4168. (1 << STRIPE_PREREAD_ACTIVE) |
  4169. (1 << STRIPE_DEGRADED) |
  4170. (1 << STRIPE_ON_UNPLUG_LIST)),
  4171. head_sh->state & (1 << STRIPE_INSYNC));
  4172. sh->check_state = head_sh->check_state;
  4173. sh->reconstruct_state = head_sh->reconstruct_state;
  4174. spin_lock_irq(&sh->stripe_lock);
  4175. sh->batch_head = NULL;
  4176. spin_unlock_irq(&sh->stripe_lock);
  4177. for (i = 0; i < sh->disks; i++) {
  4178. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  4179. do_wakeup = 1;
  4180. sh->dev[i].flags = head_sh->dev[i].flags &
  4181. (~((1 << R5_WriteError) | (1 << R5_Overlap)));
  4182. }
  4183. if (handle_flags == 0 ||
  4184. sh->state & handle_flags)
  4185. set_bit(STRIPE_HANDLE, &sh->state);
  4186. raid5_release_stripe(sh);
  4187. }
  4188. spin_lock_irq(&head_sh->stripe_lock);
  4189. head_sh->batch_head = NULL;
  4190. spin_unlock_irq(&head_sh->stripe_lock);
  4191. for (i = 0; i < head_sh->disks; i++)
  4192. if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
  4193. do_wakeup = 1;
  4194. if (head_sh->state & handle_flags)
  4195. set_bit(STRIPE_HANDLE, &head_sh->state);
  4196. if (do_wakeup)
  4197. wake_up(&head_sh->raid_conf->wait_for_overlap);
  4198. }
  4199. static void handle_stripe(struct stripe_head *sh)
  4200. {
  4201. struct stripe_head_state s;
  4202. struct r5conf *conf = sh->raid_conf;
  4203. int i;
  4204. int prexor;
  4205. int disks = sh->disks;
  4206. struct r5dev *pdev, *qdev;
  4207. clear_bit(STRIPE_HANDLE, &sh->state);
  4208. if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
  4209. /* already being handled, ensure it gets handled
  4210. * again when current action finishes */
  4211. set_bit(STRIPE_HANDLE, &sh->state);
  4212. return;
  4213. }
  4214. if (clear_batch_ready(sh) ) {
  4215. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  4216. return;
  4217. }
  4218. if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
  4219. break_stripe_batch_list(sh, 0);
  4220. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
  4221. spin_lock(&sh->stripe_lock);
  4222. /*
  4223. * Cannot process 'sync' concurrently with 'discard'.
  4224. * Flush data in r5cache before 'sync'.
  4225. */
  4226. if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
  4227. !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
  4228. !test_bit(STRIPE_DISCARD, &sh->state) &&
  4229. test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
  4230. set_bit(STRIPE_SYNCING, &sh->state);
  4231. clear_bit(STRIPE_INSYNC, &sh->state);
  4232. clear_bit(STRIPE_REPLACED, &sh->state);
  4233. }
  4234. spin_unlock(&sh->stripe_lock);
  4235. }
  4236. clear_bit(STRIPE_DELAYED, &sh->state);
  4237. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  4238. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  4239. (unsigned long long)sh->sector, sh->state,
  4240. atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
  4241. sh->check_state, sh->reconstruct_state);
  4242. analyse_stripe(sh, &s);
  4243. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  4244. goto finish;
  4245. if (s.handle_bad_blocks ||
  4246. test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
  4247. set_bit(STRIPE_HANDLE, &sh->state);
  4248. goto finish;
  4249. }
  4250. if (unlikely(s.blocked_rdev)) {
  4251. if (s.syncing || s.expanding || s.expanded ||
  4252. s.replacing || s.to_write || s.written) {
  4253. set_bit(STRIPE_HANDLE, &sh->state);
  4254. goto finish;
  4255. }
  4256. /* There is nothing for the blocked_rdev to block */
  4257. rdev_dec_pending(s.blocked_rdev, conf->mddev);
  4258. s.blocked_rdev = NULL;
  4259. }
  4260. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  4261. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  4262. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  4263. }
  4264. pr_debug("locked=%d uptodate=%d to_read=%d"
  4265. " to_write=%d failed=%d failed_num=%d,%d\n",
  4266. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  4267. s.failed_num[0], s.failed_num[1]);
  4268. /*
  4269. * check if the array has lost more than max_degraded devices and,
  4270. * if so, some requests might need to be failed.
  4271. *
  4272. * When journal device failed (log_failed), we will only process
  4273. * the stripe if there is data need write to raid disks
  4274. */
  4275. if (s.failed > conf->max_degraded ||
  4276. (s.log_failed && s.injournal == 0)) {
  4277. sh->check_state = 0;
  4278. sh->reconstruct_state = 0;
  4279. break_stripe_batch_list(sh, 0);
  4280. if (s.to_read+s.to_write+s.written)
  4281. handle_failed_stripe(conf, sh, &s, disks);
  4282. if (s.syncing + s.replacing)
  4283. handle_failed_sync(conf, sh, &s);
  4284. }
  4285. /* Now we check to see if any write operations have recently
  4286. * completed
  4287. */
  4288. prexor = 0;
  4289. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  4290. prexor = 1;
  4291. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  4292. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  4293. sh->reconstruct_state = reconstruct_state_idle;
  4294. /* All the 'written' buffers and the parity block are ready to
  4295. * be written back to disk
  4296. */
  4297. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
  4298. !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
  4299. BUG_ON(sh->qd_idx >= 0 &&
  4300. !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
  4301. !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
  4302. for (i = disks; i--; ) {
  4303. struct r5dev *dev = &sh->dev[i];
  4304. if (test_bit(R5_LOCKED, &dev->flags) &&
  4305. (i == sh->pd_idx || i == sh->qd_idx ||
  4306. dev->written || test_bit(R5_InJournal,
  4307. &dev->flags))) {
  4308. pr_debug("Writing block %d\n", i);
  4309. set_bit(R5_Wantwrite, &dev->flags);
  4310. if (prexor)
  4311. continue;
  4312. if (s.failed > 1)
  4313. continue;
  4314. if (!test_bit(R5_Insync, &dev->flags) ||
  4315. ((i == sh->pd_idx || i == sh->qd_idx) &&
  4316. s.failed == 0))
  4317. set_bit(STRIPE_INSYNC, &sh->state);
  4318. }
  4319. }
  4320. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  4321. s.dec_preread_active = 1;
  4322. }
  4323. /*
  4324. * might be able to return some write requests if the parity blocks
  4325. * are safe, or on a failed drive
  4326. */
  4327. pdev = &sh->dev[sh->pd_idx];
  4328. s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
  4329. || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
  4330. qdev = &sh->dev[sh->qd_idx];
  4331. s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
  4332. || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
  4333. || conf->level < 6;
  4334. if (s.written &&
  4335. (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  4336. && !test_bit(R5_LOCKED, &pdev->flags)
  4337. && (test_bit(R5_UPTODATE, &pdev->flags) ||
  4338. test_bit(R5_Discard, &pdev->flags))))) &&
  4339. (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  4340. && !test_bit(R5_LOCKED, &qdev->flags)
  4341. && (test_bit(R5_UPTODATE, &qdev->flags) ||
  4342. test_bit(R5_Discard, &qdev->flags))))))
  4343. handle_stripe_clean_event(conf, sh, disks);
  4344. if (s.just_cached)
  4345. r5c_handle_cached_data_endio(conf, sh, disks);
  4346. log_stripe_write_finished(sh);
  4347. /* Now we might consider reading some blocks, either to check/generate
  4348. * parity, or to satisfy requests
  4349. * or to load a block that is being partially written.
  4350. */
  4351. if (s.to_read || s.non_overwrite
  4352. || (conf->level == 6 && s.to_write && s.failed)
  4353. || (s.syncing && (s.uptodate + s.compute < disks))
  4354. || s.replacing
  4355. || s.expanding)
  4356. handle_stripe_fill(sh, &s, disks);
  4357. /*
  4358. * When the stripe finishes full journal write cycle (write to journal
  4359. * and raid disk), this is the clean up procedure so it is ready for
  4360. * next operation.
  4361. */
  4362. r5c_finish_stripe_write_out(conf, sh, &s);
  4363. /*
  4364. * Now to consider new write requests, cache write back and what else,
  4365. * if anything should be read. We do not handle new writes when:
  4366. * 1/ A 'write' operation (copy+xor) is already in flight.
  4367. * 2/ A 'check' operation is in flight, as it may clobber the parity
  4368. * block.
  4369. * 3/ A r5c cache log write is in flight.
  4370. */
  4371. if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
  4372. if (!r5c_is_writeback(conf->log)) {
  4373. if (s.to_write)
  4374. handle_stripe_dirtying(conf, sh, &s, disks);
  4375. } else { /* write back cache */
  4376. int ret = 0;
  4377. /* First, try handle writes in caching phase */
  4378. if (s.to_write)
  4379. ret = r5c_try_caching_write(conf, sh, &s,
  4380. disks);
  4381. /*
  4382. * If caching phase failed: ret == -EAGAIN
  4383. * OR
  4384. * stripe under reclaim: !caching && injournal
  4385. *
  4386. * fall back to handle_stripe_dirtying()
  4387. */
  4388. if (ret == -EAGAIN ||
  4389. /* stripe under reclaim: !caching && injournal */
  4390. (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
  4391. s.injournal > 0)) {
  4392. ret = handle_stripe_dirtying(conf, sh, &s,
  4393. disks);
  4394. if (ret == -EAGAIN)
  4395. goto finish;
  4396. }
  4397. }
  4398. }
  4399. /* maybe we need to check and possibly fix the parity for this stripe
  4400. * Any reads will already have been scheduled, so we just see if enough
  4401. * data is available. The parity check is held off while parity
  4402. * dependent operations are in flight.
  4403. */
  4404. if (sh->check_state ||
  4405. (s.syncing && s.locked == 0 &&
  4406. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  4407. !test_bit(STRIPE_INSYNC, &sh->state))) {
  4408. if (conf->level == 6)
  4409. handle_parity_checks6(conf, sh, &s, disks);
  4410. else
  4411. handle_parity_checks5(conf, sh, &s, disks);
  4412. }
  4413. if ((s.replacing || s.syncing) && s.locked == 0
  4414. && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
  4415. && !test_bit(STRIPE_REPLACED, &sh->state)) {
  4416. /* Write out to replacement devices where possible */
  4417. for (i = 0; i < conf->raid_disks; i++)
  4418. if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
  4419. WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
  4420. set_bit(R5_WantReplace, &sh->dev[i].flags);
  4421. set_bit(R5_LOCKED, &sh->dev[i].flags);
  4422. s.locked++;
  4423. }
  4424. if (s.replacing)
  4425. set_bit(STRIPE_INSYNC, &sh->state);
  4426. set_bit(STRIPE_REPLACED, &sh->state);
  4427. }
  4428. if ((s.syncing || s.replacing) && s.locked == 0 &&
  4429. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  4430. test_bit(STRIPE_INSYNC, &sh->state)) {
  4431. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  4432. clear_bit(STRIPE_SYNCING, &sh->state);
  4433. if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
  4434. wake_up(&conf->wait_for_overlap);
  4435. }
  4436. /* If the failed drives are just a ReadError, then we might need
  4437. * to progress the repair/check process
  4438. */
  4439. if (s.failed <= conf->max_degraded && !conf->mddev->ro)
  4440. for (i = 0; i < s.failed; i++) {
  4441. struct r5dev *dev = &sh->dev[s.failed_num[i]];
  4442. if (test_bit(R5_ReadError, &dev->flags)
  4443. && !test_bit(R5_LOCKED, &dev->flags)
  4444. && test_bit(R5_UPTODATE, &dev->flags)
  4445. ) {
  4446. if (!test_bit(R5_ReWrite, &dev->flags)) {
  4447. set_bit(R5_Wantwrite, &dev->flags);
  4448. set_bit(R5_ReWrite, &dev->flags);
  4449. set_bit(R5_LOCKED, &dev->flags);
  4450. s.locked++;
  4451. } else {
  4452. /* let's read it back */
  4453. set_bit(R5_Wantread, &dev->flags);
  4454. set_bit(R5_LOCKED, &dev->flags);
  4455. s.locked++;
  4456. }
  4457. }
  4458. }
  4459. /* Finish reconstruct operations initiated by the expansion process */
  4460. if (sh->reconstruct_state == reconstruct_state_result) {
  4461. struct stripe_head *sh_src
  4462. = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
  4463. if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
  4464. /* sh cannot be written until sh_src has been read.
  4465. * so arrange for sh to be delayed a little
  4466. */
  4467. set_bit(STRIPE_DELAYED, &sh->state);
  4468. set_bit(STRIPE_HANDLE, &sh->state);
  4469. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  4470. &sh_src->state))
  4471. atomic_inc(&conf->preread_active_stripes);
  4472. raid5_release_stripe(sh_src);
  4473. goto finish;
  4474. }
  4475. if (sh_src)
  4476. raid5_release_stripe(sh_src);
  4477. sh->reconstruct_state = reconstruct_state_idle;
  4478. clear_bit(STRIPE_EXPANDING, &sh->state);
  4479. for (i = conf->raid_disks; i--; ) {
  4480. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  4481. set_bit(R5_LOCKED, &sh->dev[i].flags);
  4482. s.locked++;
  4483. }
  4484. }
  4485. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  4486. !sh->reconstruct_state) {
  4487. /* Need to write out all blocks after computing parity */
  4488. sh->disks = conf->raid_disks;
  4489. stripe_set_idx(sh->sector, conf, 0, sh);
  4490. schedule_reconstruction(sh, &s, 1, 1);
  4491. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  4492. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  4493. atomic_dec(&conf->reshape_stripes);
  4494. wake_up(&conf->wait_for_overlap);
  4495. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  4496. }
  4497. if (s.expanding && s.locked == 0 &&
  4498. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  4499. handle_stripe_expansion(conf, sh);
  4500. finish:
  4501. /* wait for this device to become unblocked */
  4502. if (unlikely(s.blocked_rdev)) {
  4503. if (conf->mddev->external)
  4504. md_wait_for_blocked_rdev(s.blocked_rdev,
  4505. conf->mddev);
  4506. else
  4507. /* Internal metadata will immediately
  4508. * be written by raid5d, so we don't
  4509. * need to wait here.
  4510. */
  4511. rdev_dec_pending(s.blocked_rdev,
  4512. conf->mddev);
  4513. }
  4514. if (s.handle_bad_blocks)
  4515. for (i = disks; i--; ) {
  4516. struct md_rdev *rdev;
  4517. struct r5dev *dev = &sh->dev[i];
  4518. if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
  4519. /* We own a safe reference to the rdev */
  4520. rdev = conf->disks[i].rdev;
  4521. if (!rdev_set_badblocks(rdev, sh->sector,
  4522. STRIPE_SECTORS, 0))
  4523. md_error(conf->mddev, rdev);
  4524. rdev_dec_pending(rdev, conf->mddev);
  4525. }
  4526. if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
  4527. rdev = conf->disks[i].rdev;
  4528. rdev_clear_badblocks(rdev, sh->sector,
  4529. STRIPE_SECTORS, 0);
  4530. rdev_dec_pending(rdev, conf->mddev);
  4531. }
  4532. if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
  4533. rdev = conf->disks[i].replacement;
  4534. if (!rdev)
  4535. /* rdev have been moved down */
  4536. rdev = conf->disks[i].rdev;
  4537. rdev_clear_badblocks(rdev, sh->sector,
  4538. STRIPE_SECTORS, 0);
  4539. rdev_dec_pending(rdev, conf->mddev);
  4540. }
  4541. }
  4542. if (s.ops_request)
  4543. raid_run_ops(sh, s.ops_request);
  4544. ops_run_io(sh, &s);
  4545. if (s.dec_preread_active) {
  4546. /* We delay this until after ops_run_io so that if make_request
  4547. * is waiting on a flush, it won't continue until the writes
  4548. * have actually been submitted.
  4549. */
  4550. atomic_dec(&conf->preread_active_stripes);
  4551. if (atomic_read(&conf->preread_active_stripes) <
  4552. IO_THRESHOLD)
  4553. md_wakeup_thread(conf->mddev->thread);
  4554. }
  4555. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  4556. }
  4557. static void raid5_activate_delayed(struct r5conf *conf)
  4558. {
  4559. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  4560. while (!list_empty(&conf->delayed_list)) {
  4561. struct list_head *l = conf->delayed_list.next;
  4562. struct stripe_head *sh;
  4563. sh = list_entry(l, struct stripe_head, lru);
  4564. list_del_init(l);
  4565. clear_bit(STRIPE_DELAYED, &sh->state);
  4566. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  4567. atomic_inc(&conf->preread_active_stripes);
  4568. list_add_tail(&sh->lru, &conf->hold_list);
  4569. raid5_wakeup_stripe_thread(sh);
  4570. }
  4571. }
  4572. }
  4573. static void activate_bit_delay(struct r5conf *conf,
  4574. struct list_head *temp_inactive_list)
  4575. {
  4576. /* device_lock is held */
  4577. struct list_head head;
  4578. list_add(&head, &conf->bitmap_list);
  4579. list_del_init(&conf->bitmap_list);
  4580. while (!list_empty(&head)) {
  4581. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  4582. int hash;
  4583. list_del_init(&sh->lru);
  4584. atomic_inc(&sh->count);
  4585. hash = sh->hash_lock_index;
  4586. __release_stripe(conf, sh, &temp_inactive_list[hash]);
  4587. }
  4588. }
  4589. static int raid5_congested(struct mddev *mddev, int bits)
  4590. {
  4591. struct r5conf *conf = mddev->private;
  4592. /* No difference between reads and writes. Just check
  4593. * how busy the stripe_cache is
  4594. */
  4595. if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
  4596. return 1;
  4597. /* Also checks whether there is pressure on r5cache log space */
  4598. if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
  4599. return 1;
  4600. if (conf->quiesce)
  4601. return 1;
  4602. if (atomic_read(&conf->empty_inactive_list_nr))
  4603. return 1;
  4604. return 0;
  4605. }
  4606. static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
  4607. {
  4608. struct r5conf *conf = mddev->private;
  4609. sector_t sector = bio->bi_iter.bi_sector;
  4610. unsigned int chunk_sectors;
  4611. unsigned int bio_sectors = bio_sectors(bio);
  4612. WARN_ON_ONCE(bio->bi_partno);
  4613. chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
  4614. return chunk_sectors >=
  4615. ((sector & (chunk_sectors - 1)) + bio_sectors);
  4616. }
  4617. /*
  4618. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  4619. * later sampled by raid5d.
  4620. */
  4621. static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
  4622. {
  4623. unsigned long flags;
  4624. spin_lock_irqsave(&conf->device_lock, flags);
  4625. bi->bi_next = conf->retry_read_aligned_list;
  4626. conf->retry_read_aligned_list = bi;
  4627. spin_unlock_irqrestore(&conf->device_lock, flags);
  4628. md_wakeup_thread(conf->mddev->thread);
  4629. }
  4630. static struct bio *remove_bio_from_retry(struct r5conf *conf,
  4631. unsigned int *offset)
  4632. {
  4633. struct bio *bi;
  4634. bi = conf->retry_read_aligned;
  4635. if (bi) {
  4636. *offset = conf->retry_read_offset;
  4637. conf->retry_read_aligned = NULL;
  4638. return bi;
  4639. }
  4640. bi = conf->retry_read_aligned_list;
  4641. if(bi) {
  4642. conf->retry_read_aligned_list = bi->bi_next;
  4643. bi->bi_next = NULL;
  4644. *offset = 0;
  4645. }
  4646. return bi;
  4647. }
  4648. /*
  4649. * The "raid5_align_endio" should check if the read succeeded and if it
  4650. * did, call bio_endio on the original bio (having bio_put the new bio
  4651. * first).
  4652. * If the read failed..
  4653. */
  4654. static void raid5_align_endio(struct bio *bi)
  4655. {
  4656. struct bio* raid_bi = bi->bi_private;
  4657. struct mddev *mddev;
  4658. struct r5conf *conf;
  4659. struct md_rdev *rdev;
  4660. blk_status_t error = bi->bi_status;
  4661. bio_put(bi);
  4662. rdev = (void*)raid_bi->bi_next;
  4663. raid_bi->bi_next = NULL;
  4664. mddev = rdev->mddev;
  4665. conf = mddev->private;
  4666. rdev_dec_pending(rdev, conf->mddev);
  4667. if (!error) {
  4668. bio_endio(raid_bi);
  4669. if (atomic_dec_and_test(&conf->active_aligned_reads))
  4670. wake_up(&conf->wait_for_quiescent);
  4671. return;
  4672. }
  4673. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  4674. add_bio_to_retry(raid_bi, conf);
  4675. }
  4676. static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
  4677. {
  4678. struct r5conf *conf = mddev->private;
  4679. int dd_idx;
  4680. struct bio* align_bi;
  4681. struct md_rdev *rdev;
  4682. sector_t end_sector;
  4683. if (!in_chunk_boundary(mddev, raid_bio)) {
  4684. pr_debug("%s: non aligned\n", __func__);
  4685. return 0;
  4686. }
  4687. /*
  4688. * use bio_clone_fast to make a copy of the bio
  4689. */
  4690. align_bi = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->bio_set);
  4691. if (!align_bi)
  4692. return 0;
  4693. /*
  4694. * set bi_end_io to a new function, and set bi_private to the
  4695. * original bio.
  4696. */
  4697. align_bi->bi_end_io = raid5_align_endio;
  4698. align_bi->bi_private = raid_bio;
  4699. /*
  4700. * compute position
  4701. */
  4702. align_bi->bi_iter.bi_sector =
  4703. raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
  4704. 0, &dd_idx, NULL);
  4705. end_sector = bio_end_sector(align_bi);
  4706. rcu_read_lock();
  4707. rdev = rcu_dereference(conf->disks[dd_idx].replacement);
  4708. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  4709. rdev->recovery_offset < end_sector) {
  4710. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  4711. if (rdev &&
  4712. (test_bit(Faulty, &rdev->flags) ||
  4713. !(test_bit(In_sync, &rdev->flags) ||
  4714. rdev->recovery_offset >= end_sector)))
  4715. rdev = NULL;
  4716. }
  4717. if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
  4718. rcu_read_unlock();
  4719. bio_put(align_bi);
  4720. return 0;
  4721. }
  4722. if (rdev) {
  4723. sector_t first_bad;
  4724. int bad_sectors;
  4725. atomic_inc(&rdev->nr_pending);
  4726. rcu_read_unlock();
  4727. raid_bio->bi_next = (void*)rdev;
  4728. bio_set_dev(align_bi, rdev->bdev);
  4729. bio_clear_flag(align_bi, BIO_SEG_VALID);
  4730. if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
  4731. bio_sectors(align_bi),
  4732. &first_bad, &bad_sectors)) {
  4733. bio_put(align_bi);
  4734. rdev_dec_pending(rdev, mddev);
  4735. return 0;
  4736. }
  4737. /* No reshape active, so we can trust rdev->data_offset */
  4738. align_bi->bi_iter.bi_sector += rdev->data_offset;
  4739. spin_lock_irq(&conf->device_lock);
  4740. wait_event_lock_irq(conf->wait_for_quiescent,
  4741. conf->quiesce == 0,
  4742. conf->device_lock);
  4743. atomic_inc(&conf->active_aligned_reads);
  4744. spin_unlock_irq(&conf->device_lock);
  4745. if (mddev->gendisk)
  4746. trace_block_bio_remap(align_bi->bi_disk->queue,
  4747. align_bi, disk_devt(mddev->gendisk),
  4748. raid_bio->bi_iter.bi_sector);
  4749. generic_make_request(align_bi);
  4750. return 1;
  4751. } else {
  4752. rcu_read_unlock();
  4753. bio_put(align_bi);
  4754. return 0;
  4755. }
  4756. }
  4757. static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
  4758. {
  4759. struct bio *split;
  4760. sector_t sector = raid_bio->bi_iter.bi_sector;
  4761. unsigned chunk_sects = mddev->chunk_sectors;
  4762. unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
  4763. if (sectors < bio_sectors(raid_bio)) {
  4764. struct r5conf *conf = mddev->private;
  4765. split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
  4766. bio_chain(split, raid_bio);
  4767. generic_make_request(raid_bio);
  4768. raid_bio = split;
  4769. }
  4770. if (!raid5_read_one_chunk(mddev, raid_bio))
  4771. return raid_bio;
  4772. return NULL;
  4773. }
  4774. /* __get_priority_stripe - get the next stripe to process
  4775. *
  4776. * Full stripe writes are allowed to pass preread active stripes up until
  4777. * the bypass_threshold is exceeded. In general the bypass_count
  4778. * increments when the handle_list is handled before the hold_list; however, it
  4779. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  4780. * stripe with in flight i/o. The bypass_count will be reset when the
  4781. * head of the hold_list has changed, i.e. the head was promoted to the
  4782. * handle_list.
  4783. */
  4784. static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
  4785. {
  4786. struct stripe_head *sh, *tmp;
  4787. struct list_head *handle_list = NULL;
  4788. struct r5worker_group *wg;
  4789. bool second_try = !r5c_is_writeback(conf->log) &&
  4790. !r5l_log_disk_error(conf);
  4791. bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
  4792. r5l_log_disk_error(conf);
  4793. again:
  4794. wg = NULL;
  4795. sh = NULL;
  4796. if (conf->worker_cnt_per_group == 0) {
  4797. handle_list = try_loprio ? &conf->loprio_list :
  4798. &conf->handle_list;
  4799. } else if (group != ANY_GROUP) {
  4800. handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
  4801. &conf->worker_groups[group].handle_list;
  4802. wg = &conf->worker_groups[group];
  4803. } else {
  4804. int i;
  4805. for (i = 0; i < conf->group_cnt; i++) {
  4806. handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
  4807. &conf->worker_groups[i].handle_list;
  4808. wg = &conf->worker_groups[i];
  4809. if (!list_empty(handle_list))
  4810. break;
  4811. }
  4812. }
  4813. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  4814. __func__,
  4815. list_empty(handle_list) ? "empty" : "busy",
  4816. list_empty(&conf->hold_list) ? "empty" : "busy",
  4817. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  4818. if (!list_empty(handle_list)) {
  4819. sh = list_entry(handle_list->next, typeof(*sh), lru);
  4820. if (list_empty(&conf->hold_list))
  4821. conf->bypass_count = 0;
  4822. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  4823. if (conf->hold_list.next == conf->last_hold)
  4824. conf->bypass_count++;
  4825. else {
  4826. conf->last_hold = conf->hold_list.next;
  4827. conf->bypass_count -= conf->bypass_threshold;
  4828. if (conf->bypass_count < 0)
  4829. conf->bypass_count = 0;
  4830. }
  4831. }
  4832. } else if (!list_empty(&conf->hold_list) &&
  4833. ((conf->bypass_threshold &&
  4834. conf->bypass_count > conf->bypass_threshold) ||
  4835. atomic_read(&conf->pending_full_writes) == 0)) {
  4836. list_for_each_entry(tmp, &conf->hold_list, lru) {
  4837. if (conf->worker_cnt_per_group == 0 ||
  4838. group == ANY_GROUP ||
  4839. !cpu_online(tmp->cpu) ||
  4840. cpu_to_group(tmp->cpu) == group) {
  4841. sh = tmp;
  4842. break;
  4843. }
  4844. }
  4845. if (sh) {
  4846. conf->bypass_count -= conf->bypass_threshold;
  4847. if (conf->bypass_count < 0)
  4848. conf->bypass_count = 0;
  4849. }
  4850. wg = NULL;
  4851. }
  4852. if (!sh) {
  4853. if (second_try)
  4854. return NULL;
  4855. second_try = true;
  4856. try_loprio = !try_loprio;
  4857. goto again;
  4858. }
  4859. if (wg) {
  4860. wg->stripes_cnt--;
  4861. sh->group = NULL;
  4862. }
  4863. list_del_init(&sh->lru);
  4864. BUG_ON(atomic_inc_return(&sh->count) != 1);
  4865. return sh;
  4866. }
  4867. struct raid5_plug_cb {
  4868. struct blk_plug_cb cb;
  4869. struct list_head list;
  4870. struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
  4871. };
  4872. static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
  4873. {
  4874. struct raid5_plug_cb *cb = container_of(
  4875. blk_cb, struct raid5_plug_cb, cb);
  4876. struct stripe_head *sh;
  4877. struct mddev *mddev = cb->cb.data;
  4878. struct r5conf *conf = mddev->private;
  4879. int cnt = 0;
  4880. int hash;
  4881. if (cb->list.next && !list_empty(&cb->list)) {
  4882. spin_lock_irq(&conf->device_lock);
  4883. while (!list_empty(&cb->list)) {
  4884. sh = list_first_entry(&cb->list, struct stripe_head, lru);
  4885. list_del_init(&sh->lru);
  4886. /*
  4887. * avoid race release_stripe_plug() sees
  4888. * STRIPE_ON_UNPLUG_LIST clear but the stripe
  4889. * is still in our list
  4890. */
  4891. smp_mb__before_atomic();
  4892. clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
  4893. /*
  4894. * STRIPE_ON_RELEASE_LIST could be set here. In that
  4895. * case, the count is always > 1 here
  4896. */
  4897. hash = sh->hash_lock_index;
  4898. __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
  4899. cnt++;
  4900. }
  4901. spin_unlock_irq(&conf->device_lock);
  4902. }
  4903. release_inactive_stripe_list(conf, cb->temp_inactive_list,
  4904. NR_STRIPE_HASH_LOCKS);
  4905. if (mddev->queue)
  4906. trace_block_unplug(mddev->queue, cnt, !from_schedule);
  4907. kfree(cb);
  4908. }
  4909. static void release_stripe_plug(struct mddev *mddev,
  4910. struct stripe_head *sh)
  4911. {
  4912. struct blk_plug_cb *blk_cb = blk_check_plugged(
  4913. raid5_unplug, mddev,
  4914. sizeof(struct raid5_plug_cb));
  4915. struct raid5_plug_cb *cb;
  4916. if (!blk_cb) {
  4917. raid5_release_stripe(sh);
  4918. return;
  4919. }
  4920. cb = container_of(blk_cb, struct raid5_plug_cb, cb);
  4921. if (cb->list.next == NULL) {
  4922. int i;
  4923. INIT_LIST_HEAD(&cb->list);
  4924. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  4925. INIT_LIST_HEAD(cb->temp_inactive_list + i);
  4926. }
  4927. if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
  4928. list_add_tail(&sh->lru, &cb->list);
  4929. else
  4930. raid5_release_stripe(sh);
  4931. }
  4932. static void make_discard_request(struct mddev *mddev, struct bio *bi)
  4933. {
  4934. struct r5conf *conf = mddev->private;
  4935. sector_t logical_sector, last_sector;
  4936. struct stripe_head *sh;
  4937. int stripe_sectors;
  4938. if (mddev->reshape_position != MaxSector)
  4939. /* Skip discard while reshape is happening */
  4940. return;
  4941. logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  4942. last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
  4943. bi->bi_next = NULL;
  4944. stripe_sectors = conf->chunk_sectors *
  4945. (conf->raid_disks - conf->max_degraded);
  4946. logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
  4947. stripe_sectors);
  4948. sector_div(last_sector, stripe_sectors);
  4949. logical_sector *= conf->chunk_sectors;
  4950. last_sector *= conf->chunk_sectors;
  4951. for (; logical_sector < last_sector;
  4952. logical_sector += STRIPE_SECTORS) {
  4953. DEFINE_WAIT(w);
  4954. int d;
  4955. again:
  4956. sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
  4957. prepare_to_wait(&conf->wait_for_overlap, &w,
  4958. TASK_UNINTERRUPTIBLE);
  4959. set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
  4960. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  4961. raid5_release_stripe(sh);
  4962. schedule();
  4963. goto again;
  4964. }
  4965. clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
  4966. spin_lock_irq(&sh->stripe_lock);
  4967. for (d = 0; d < conf->raid_disks; d++) {
  4968. if (d == sh->pd_idx || d == sh->qd_idx)
  4969. continue;
  4970. if (sh->dev[d].towrite || sh->dev[d].toread) {
  4971. set_bit(R5_Overlap, &sh->dev[d].flags);
  4972. spin_unlock_irq(&sh->stripe_lock);
  4973. raid5_release_stripe(sh);
  4974. schedule();
  4975. goto again;
  4976. }
  4977. }
  4978. set_bit(STRIPE_DISCARD, &sh->state);
  4979. finish_wait(&conf->wait_for_overlap, &w);
  4980. sh->overwrite_disks = 0;
  4981. for (d = 0; d < conf->raid_disks; d++) {
  4982. if (d == sh->pd_idx || d == sh->qd_idx)
  4983. continue;
  4984. sh->dev[d].towrite = bi;
  4985. set_bit(R5_OVERWRITE, &sh->dev[d].flags);
  4986. bio_inc_remaining(bi);
  4987. md_write_inc(mddev, bi);
  4988. sh->overwrite_disks++;
  4989. }
  4990. spin_unlock_irq(&sh->stripe_lock);
  4991. if (conf->mddev->bitmap) {
  4992. for (d = 0;
  4993. d < conf->raid_disks - conf->max_degraded;
  4994. d++)
  4995. md_bitmap_startwrite(mddev->bitmap,
  4996. sh->sector,
  4997. STRIPE_SECTORS,
  4998. 0);
  4999. sh->bm_seq = conf->seq_flush + 1;
  5000. set_bit(STRIPE_BIT_DELAY, &sh->state);
  5001. }
  5002. set_bit(STRIPE_HANDLE, &sh->state);
  5003. clear_bit(STRIPE_DELAYED, &sh->state);
  5004. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  5005. atomic_inc(&conf->preread_active_stripes);
  5006. release_stripe_plug(mddev, sh);
  5007. }
  5008. bio_endio(bi);
  5009. }
  5010. static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
  5011. {
  5012. struct r5conf *conf = mddev->private;
  5013. int dd_idx;
  5014. sector_t new_sector;
  5015. sector_t logical_sector, last_sector;
  5016. struct stripe_head *sh;
  5017. const int rw = bio_data_dir(bi);
  5018. DEFINE_WAIT(w);
  5019. bool do_prepare;
  5020. bool do_flush = false;
  5021. if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
  5022. int ret = log_handle_flush_request(conf, bi);
  5023. if (ret == 0)
  5024. return true;
  5025. if (ret == -ENODEV) {
  5026. if (md_flush_request(mddev, bi))
  5027. return true;
  5028. }
  5029. /* ret == -EAGAIN, fallback */
  5030. /*
  5031. * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
  5032. * we need to flush journal device
  5033. */
  5034. do_flush = bi->bi_opf & REQ_PREFLUSH;
  5035. }
  5036. if (!md_write_start(mddev, bi))
  5037. return false;
  5038. /*
  5039. * If array is degraded, better not do chunk aligned read because
  5040. * later we might have to read it again in order to reconstruct
  5041. * data on failed drives.
  5042. */
  5043. if (rw == READ && mddev->degraded == 0 &&
  5044. mddev->reshape_position == MaxSector) {
  5045. bi = chunk_aligned_read(mddev, bi);
  5046. if (!bi)
  5047. return true;
  5048. }
  5049. if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
  5050. make_discard_request(mddev, bi);
  5051. md_write_end(mddev);
  5052. return true;
  5053. }
  5054. logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  5055. last_sector = bio_end_sector(bi);
  5056. bi->bi_next = NULL;
  5057. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  5058. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  5059. int previous;
  5060. int seq;
  5061. do_prepare = false;
  5062. retry:
  5063. seq = read_seqcount_begin(&conf->gen_lock);
  5064. previous = 0;
  5065. if (do_prepare)
  5066. prepare_to_wait(&conf->wait_for_overlap, &w,
  5067. TASK_UNINTERRUPTIBLE);
  5068. if (unlikely(conf->reshape_progress != MaxSector)) {
  5069. /* spinlock is needed as reshape_progress may be
  5070. * 64bit on a 32bit platform, and so it might be
  5071. * possible to see a half-updated value
  5072. * Of course reshape_progress could change after
  5073. * the lock is dropped, so once we get a reference
  5074. * to the stripe that we think it is, we will have
  5075. * to check again.
  5076. */
  5077. spin_lock_irq(&conf->device_lock);
  5078. if (mddev->reshape_backwards
  5079. ? logical_sector < conf->reshape_progress
  5080. : logical_sector >= conf->reshape_progress) {
  5081. previous = 1;
  5082. } else {
  5083. if (mddev->reshape_backwards
  5084. ? logical_sector < conf->reshape_safe
  5085. : logical_sector >= conf->reshape_safe) {
  5086. spin_unlock_irq(&conf->device_lock);
  5087. schedule();
  5088. do_prepare = true;
  5089. goto retry;
  5090. }
  5091. }
  5092. spin_unlock_irq(&conf->device_lock);
  5093. }
  5094. new_sector = raid5_compute_sector(conf, logical_sector,
  5095. previous,
  5096. &dd_idx, NULL);
  5097. pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
  5098. (unsigned long long)new_sector,
  5099. (unsigned long long)logical_sector);
  5100. sh = raid5_get_active_stripe(conf, new_sector, previous,
  5101. (bi->bi_opf & REQ_RAHEAD), 0);
  5102. if (sh) {
  5103. if (unlikely(previous)) {
  5104. /* expansion might have moved on while waiting for a
  5105. * stripe, so we must do the range check again.
  5106. * Expansion could still move past after this
  5107. * test, but as we are holding a reference to
  5108. * 'sh', we know that if that happens,
  5109. * STRIPE_EXPANDING will get set and the expansion
  5110. * won't proceed until we finish with the stripe.
  5111. */
  5112. int must_retry = 0;
  5113. spin_lock_irq(&conf->device_lock);
  5114. if (mddev->reshape_backwards
  5115. ? logical_sector >= conf->reshape_progress
  5116. : logical_sector < conf->reshape_progress)
  5117. /* mismatch, need to try again */
  5118. must_retry = 1;
  5119. spin_unlock_irq(&conf->device_lock);
  5120. if (must_retry) {
  5121. raid5_release_stripe(sh);
  5122. schedule();
  5123. do_prepare = true;
  5124. goto retry;
  5125. }
  5126. }
  5127. if (read_seqcount_retry(&conf->gen_lock, seq)) {
  5128. /* Might have got the wrong stripe_head
  5129. * by accident
  5130. */
  5131. raid5_release_stripe(sh);
  5132. goto retry;
  5133. }
  5134. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  5135. !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
  5136. /* Stripe is busy expanding or
  5137. * add failed due to overlap. Flush everything
  5138. * and wait a while
  5139. */
  5140. md_wakeup_thread(mddev->thread);
  5141. raid5_release_stripe(sh);
  5142. schedule();
  5143. do_prepare = true;
  5144. goto retry;
  5145. }
  5146. if (do_flush) {
  5147. set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
  5148. /* we only need flush for one stripe */
  5149. do_flush = false;
  5150. }
  5151. if (!sh->batch_head || sh == sh->batch_head)
  5152. set_bit(STRIPE_HANDLE, &sh->state);
  5153. clear_bit(STRIPE_DELAYED, &sh->state);
  5154. if ((!sh->batch_head || sh == sh->batch_head) &&
  5155. (bi->bi_opf & REQ_SYNC) &&
  5156. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  5157. atomic_inc(&conf->preread_active_stripes);
  5158. release_stripe_plug(mddev, sh);
  5159. } else {
  5160. /* cannot get stripe for read-ahead, just give-up */
  5161. bi->bi_status = BLK_STS_IOERR;
  5162. break;
  5163. }
  5164. }
  5165. finish_wait(&conf->wait_for_overlap, &w);
  5166. if (rw == WRITE)
  5167. md_write_end(mddev);
  5168. bio_endio(bi);
  5169. return true;
  5170. }
  5171. static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
  5172. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
  5173. {
  5174. /* reshaping is quite different to recovery/resync so it is
  5175. * handled quite separately ... here.
  5176. *
  5177. * On each call to sync_request, we gather one chunk worth of
  5178. * destination stripes and flag them as expanding.
  5179. * Then we find all the source stripes and request reads.
  5180. * As the reads complete, handle_stripe will copy the data
  5181. * into the destination stripe and release that stripe.
  5182. */
  5183. struct r5conf *conf = mddev->private;
  5184. struct stripe_head *sh;
  5185. struct md_rdev *rdev;
  5186. sector_t first_sector, last_sector;
  5187. int raid_disks = conf->previous_raid_disks;
  5188. int data_disks = raid_disks - conf->max_degraded;
  5189. int new_data_disks = conf->raid_disks - conf->max_degraded;
  5190. int i;
  5191. int dd_idx;
  5192. sector_t writepos, readpos, safepos;
  5193. sector_t stripe_addr;
  5194. int reshape_sectors;
  5195. struct list_head stripes;
  5196. sector_t retn;
  5197. if (sector_nr == 0) {
  5198. /* If restarting in the middle, skip the initial sectors */
  5199. if (mddev->reshape_backwards &&
  5200. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  5201. sector_nr = raid5_size(mddev, 0, 0)
  5202. - conf->reshape_progress;
  5203. } else if (mddev->reshape_backwards &&
  5204. conf->reshape_progress == MaxSector) {
  5205. /* shouldn't happen, but just in case, finish up.*/
  5206. sector_nr = MaxSector;
  5207. } else if (!mddev->reshape_backwards &&
  5208. conf->reshape_progress > 0)
  5209. sector_nr = conf->reshape_progress;
  5210. sector_div(sector_nr, new_data_disks);
  5211. if (sector_nr) {
  5212. mddev->curr_resync_completed = sector_nr;
  5213. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  5214. *skipped = 1;
  5215. retn = sector_nr;
  5216. goto finish;
  5217. }
  5218. }
  5219. /* We need to process a full chunk at a time.
  5220. * If old and new chunk sizes differ, we need to process the
  5221. * largest of these
  5222. */
  5223. reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
  5224. /* We update the metadata at least every 10 seconds, or when
  5225. * the data about to be copied would over-write the source of
  5226. * the data at the front of the range. i.e. one new_stripe
  5227. * along from reshape_progress new_maps to after where
  5228. * reshape_safe old_maps to
  5229. */
  5230. writepos = conf->reshape_progress;
  5231. sector_div(writepos, new_data_disks);
  5232. readpos = conf->reshape_progress;
  5233. sector_div(readpos, data_disks);
  5234. safepos = conf->reshape_safe;
  5235. sector_div(safepos, data_disks);
  5236. if (mddev->reshape_backwards) {
  5237. BUG_ON(writepos < reshape_sectors);
  5238. writepos -= reshape_sectors;
  5239. readpos += reshape_sectors;
  5240. safepos += reshape_sectors;
  5241. } else {
  5242. writepos += reshape_sectors;
  5243. /* readpos and safepos are worst-case calculations.
  5244. * A negative number is overly pessimistic, and causes
  5245. * obvious problems for unsigned storage. So clip to 0.
  5246. */
  5247. readpos -= min_t(sector_t, reshape_sectors, readpos);
  5248. safepos -= min_t(sector_t, reshape_sectors, safepos);
  5249. }
  5250. /* Having calculated the 'writepos' possibly use it
  5251. * to set 'stripe_addr' which is where we will write to.
  5252. */
  5253. if (mddev->reshape_backwards) {
  5254. BUG_ON(conf->reshape_progress == 0);
  5255. stripe_addr = writepos;
  5256. BUG_ON((mddev->dev_sectors &
  5257. ~((sector_t)reshape_sectors - 1))
  5258. - reshape_sectors - stripe_addr
  5259. != sector_nr);
  5260. } else {
  5261. BUG_ON(writepos != sector_nr + reshape_sectors);
  5262. stripe_addr = sector_nr;
  5263. }
  5264. /* 'writepos' is the most advanced device address we might write.
  5265. * 'readpos' is the least advanced device address we might read.
  5266. * 'safepos' is the least address recorded in the metadata as having
  5267. * been reshaped.
  5268. * If there is a min_offset_diff, these are adjusted either by
  5269. * increasing the safepos/readpos if diff is negative, or
  5270. * increasing writepos if diff is positive.
  5271. * If 'readpos' is then behind 'writepos', there is no way that we can
  5272. * ensure safety in the face of a crash - that must be done by userspace
  5273. * making a backup of the data. So in that case there is no particular
  5274. * rush to update metadata.
  5275. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  5276. * update the metadata to advance 'safepos' to match 'readpos' so that
  5277. * we can be safe in the event of a crash.
  5278. * So we insist on updating metadata if safepos is behind writepos and
  5279. * readpos is beyond writepos.
  5280. * In any case, update the metadata every 10 seconds.
  5281. * Maybe that number should be configurable, but I'm not sure it is
  5282. * worth it.... maybe it could be a multiple of safemode_delay???
  5283. */
  5284. if (conf->min_offset_diff < 0) {
  5285. safepos += -conf->min_offset_diff;
  5286. readpos += -conf->min_offset_diff;
  5287. } else
  5288. writepos += conf->min_offset_diff;
  5289. if ((mddev->reshape_backwards
  5290. ? (safepos > writepos && readpos < writepos)
  5291. : (safepos < writepos && readpos > writepos)) ||
  5292. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  5293. /* Cannot proceed until we've updated the superblock... */
  5294. wait_event(conf->wait_for_overlap,
  5295. atomic_read(&conf->reshape_stripes)==0
  5296. || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5297. if (atomic_read(&conf->reshape_stripes) != 0)
  5298. return 0;
  5299. mddev->reshape_position = conf->reshape_progress;
  5300. mddev->curr_resync_completed = sector_nr;
  5301. if (!mddev->reshape_backwards)
  5302. /* Can update recovery_offset */
  5303. rdev_for_each(rdev, mddev)
  5304. if (rdev->raid_disk >= 0 &&
  5305. !test_bit(Journal, &rdev->flags) &&
  5306. !test_bit(In_sync, &rdev->flags) &&
  5307. rdev->recovery_offset < sector_nr)
  5308. rdev->recovery_offset = sector_nr;
  5309. conf->reshape_checkpoint = jiffies;
  5310. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  5311. md_wakeup_thread(mddev->thread);
  5312. wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
  5313. test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5314. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  5315. return 0;
  5316. spin_lock_irq(&conf->device_lock);
  5317. conf->reshape_safe = mddev->reshape_position;
  5318. spin_unlock_irq(&conf->device_lock);
  5319. wake_up(&conf->wait_for_overlap);
  5320. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  5321. }
  5322. INIT_LIST_HEAD(&stripes);
  5323. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  5324. int j;
  5325. int skipped_disk = 0;
  5326. sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  5327. set_bit(STRIPE_EXPANDING, &sh->state);
  5328. atomic_inc(&conf->reshape_stripes);
  5329. /* If any of this stripe is beyond the end of the old
  5330. * array, then we need to zero those blocks
  5331. */
  5332. for (j=sh->disks; j--;) {
  5333. sector_t s;
  5334. if (j == sh->pd_idx)
  5335. continue;
  5336. if (conf->level == 6 &&
  5337. j == sh->qd_idx)
  5338. continue;
  5339. s = raid5_compute_blocknr(sh, j, 0);
  5340. if (s < raid5_size(mddev, 0, 0)) {
  5341. skipped_disk = 1;
  5342. continue;
  5343. }
  5344. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  5345. set_bit(R5_Expanded, &sh->dev[j].flags);
  5346. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  5347. }
  5348. if (!skipped_disk) {
  5349. set_bit(STRIPE_EXPAND_READY, &sh->state);
  5350. set_bit(STRIPE_HANDLE, &sh->state);
  5351. }
  5352. list_add(&sh->lru, &stripes);
  5353. }
  5354. spin_lock_irq(&conf->device_lock);
  5355. if (mddev->reshape_backwards)
  5356. conf->reshape_progress -= reshape_sectors * new_data_disks;
  5357. else
  5358. conf->reshape_progress += reshape_sectors * new_data_disks;
  5359. spin_unlock_irq(&conf->device_lock);
  5360. /* Ok, those stripe are ready. We can start scheduling
  5361. * reads on the source stripes.
  5362. * The source stripes are determined by mapping the first and last
  5363. * block on the destination stripes.
  5364. */
  5365. first_sector =
  5366. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  5367. 1, &dd_idx, NULL);
  5368. last_sector =
  5369. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  5370. * new_data_disks - 1),
  5371. 1, &dd_idx, NULL);
  5372. if (last_sector >= mddev->dev_sectors)
  5373. last_sector = mddev->dev_sectors - 1;
  5374. while (first_sector <= last_sector) {
  5375. sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
  5376. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  5377. set_bit(STRIPE_HANDLE, &sh->state);
  5378. raid5_release_stripe(sh);
  5379. first_sector += STRIPE_SECTORS;
  5380. }
  5381. /* Now that the sources are clearly marked, we can release
  5382. * the destination stripes
  5383. */
  5384. while (!list_empty(&stripes)) {
  5385. sh = list_entry(stripes.next, struct stripe_head, lru);
  5386. list_del_init(&sh->lru);
  5387. raid5_release_stripe(sh);
  5388. }
  5389. /* If this takes us to the resync_max point where we have to pause,
  5390. * then we need to write out the superblock.
  5391. */
  5392. sector_nr += reshape_sectors;
  5393. retn = reshape_sectors;
  5394. finish:
  5395. if (mddev->curr_resync_completed > mddev->resync_max ||
  5396. (sector_nr - mddev->curr_resync_completed) * 2
  5397. >= mddev->resync_max - mddev->curr_resync_completed) {
  5398. /* Cannot proceed until we've updated the superblock... */
  5399. wait_event(conf->wait_for_overlap,
  5400. atomic_read(&conf->reshape_stripes) == 0
  5401. || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5402. if (atomic_read(&conf->reshape_stripes) != 0)
  5403. goto ret;
  5404. mddev->reshape_position = conf->reshape_progress;
  5405. mddev->curr_resync_completed = sector_nr;
  5406. if (!mddev->reshape_backwards)
  5407. /* Can update recovery_offset */
  5408. rdev_for_each(rdev, mddev)
  5409. if (rdev->raid_disk >= 0 &&
  5410. !test_bit(Journal, &rdev->flags) &&
  5411. !test_bit(In_sync, &rdev->flags) &&
  5412. rdev->recovery_offset < sector_nr)
  5413. rdev->recovery_offset = sector_nr;
  5414. conf->reshape_checkpoint = jiffies;
  5415. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  5416. md_wakeup_thread(mddev->thread);
  5417. wait_event(mddev->sb_wait,
  5418. !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
  5419. || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5420. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  5421. goto ret;
  5422. spin_lock_irq(&conf->device_lock);
  5423. conf->reshape_safe = mddev->reshape_position;
  5424. spin_unlock_irq(&conf->device_lock);
  5425. wake_up(&conf->wait_for_overlap);
  5426. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  5427. }
  5428. ret:
  5429. return retn;
  5430. }
  5431. static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
  5432. int *skipped)
  5433. {
  5434. struct r5conf *conf = mddev->private;
  5435. struct stripe_head *sh;
  5436. sector_t max_sector = mddev->dev_sectors;
  5437. sector_t sync_blocks;
  5438. int still_degraded = 0;
  5439. int i;
  5440. if (sector_nr >= max_sector) {
  5441. /* just being told to finish up .. nothing much to do */
  5442. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  5443. end_reshape(conf);
  5444. return 0;
  5445. }
  5446. if (mddev->curr_resync < max_sector) /* aborted */
  5447. md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  5448. &sync_blocks, 1);
  5449. else /* completed sync */
  5450. conf->fullsync = 0;
  5451. md_bitmap_close_sync(mddev->bitmap);
  5452. return 0;
  5453. }
  5454. /* Allow raid5_quiesce to complete */
  5455. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  5456. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  5457. return reshape_request(mddev, sector_nr, skipped);
  5458. /* No need to check resync_max as we never do more than one
  5459. * stripe, and as resync_max will always be on a chunk boundary,
  5460. * if the check in md_do_sync didn't fire, there is no chance
  5461. * of overstepping resync_max here
  5462. */
  5463. /* if there is too many failed drives and we are trying
  5464. * to resync, then assert that we are finished, because there is
  5465. * nothing we can do.
  5466. */
  5467. if (mddev->degraded >= conf->max_degraded &&
  5468. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  5469. sector_t rv = mddev->dev_sectors - sector_nr;
  5470. *skipped = 1;
  5471. return rv;
  5472. }
  5473. if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  5474. !conf->fullsync &&
  5475. !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  5476. sync_blocks >= STRIPE_SECTORS) {
  5477. /* we can skip this block, and probably more */
  5478. sync_blocks /= STRIPE_SECTORS;
  5479. *skipped = 1;
  5480. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  5481. }
  5482. md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
  5483. sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
  5484. if (sh == NULL) {
  5485. sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
  5486. /* make sure we don't swamp the stripe cache if someone else
  5487. * is trying to get access
  5488. */
  5489. schedule_timeout_uninterruptible(1);
  5490. }
  5491. /* Need to check if array will still be degraded after recovery/resync
  5492. * Note in case of > 1 drive failures it's possible we're rebuilding
  5493. * one drive while leaving another faulty drive in array.
  5494. */
  5495. rcu_read_lock();
  5496. for (i = 0; i < conf->raid_disks; i++) {
  5497. struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
  5498. if (rdev == NULL || test_bit(Faulty, &rdev->flags))
  5499. still_degraded = 1;
  5500. }
  5501. rcu_read_unlock();
  5502. md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  5503. set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
  5504. set_bit(STRIPE_HANDLE, &sh->state);
  5505. raid5_release_stripe(sh);
  5506. return STRIPE_SECTORS;
  5507. }
  5508. static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
  5509. unsigned int offset)
  5510. {
  5511. /* We may not be able to submit a whole bio at once as there
  5512. * may not be enough stripe_heads available.
  5513. * We cannot pre-allocate enough stripe_heads as we may need
  5514. * more than exist in the cache (if we allow ever large chunks).
  5515. * So we do one stripe head at a time and record in
  5516. * ->bi_hw_segments how many have been done.
  5517. *
  5518. * We *know* that this entire raid_bio is in one chunk, so
  5519. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  5520. */
  5521. struct stripe_head *sh;
  5522. int dd_idx;
  5523. sector_t sector, logical_sector, last_sector;
  5524. int scnt = 0;
  5525. int handled = 0;
  5526. logical_sector = raid_bio->bi_iter.bi_sector &
  5527. ~((sector_t)STRIPE_SECTORS-1);
  5528. sector = raid5_compute_sector(conf, logical_sector,
  5529. 0, &dd_idx, NULL);
  5530. last_sector = bio_end_sector(raid_bio);
  5531. for (; logical_sector < last_sector;
  5532. logical_sector += STRIPE_SECTORS,
  5533. sector += STRIPE_SECTORS,
  5534. scnt++) {
  5535. if (scnt < offset)
  5536. /* already done this stripe */
  5537. continue;
  5538. sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
  5539. if (!sh) {
  5540. /* failed to get a stripe - must wait */
  5541. conf->retry_read_aligned = raid_bio;
  5542. conf->retry_read_offset = scnt;
  5543. return handled;
  5544. }
  5545. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
  5546. raid5_release_stripe(sh);
  5547. conf->retry_read_aligned = raid_bio;
  5548. conf->retry_read_offset = scnt;
  5549. return handled;
  5550. }
  5551. set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
  5552. handle_stripe(sh);
  5553. raid5_release_stripe(sh);
  5554. handled++;
  5555. }
  5556. bio_endio(raid_bio);
  5557. if (atomic_dec_and_test(&conf->active_aligned_reads))
  5558. wake_up(&conf->wait_for_quiescent);
  5559. return handled;
  5560. }
  5561. static int handle_active_stripes(struct r5conf *conf, int group,
  5562. struct r5worker *worker,
  5563. struct list_head *temp_inactive_list)
  5564. {
  5565. struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
  5566. int i, batch_size = 0, hash;
  5567. bool release_inactive = false;
  5568. while (batch_size < MAX_STRIPE_BATCH &&
  5569. (sh = __get_priority_stripe(conf, group)) != NULL)
  5570. batch[batch_size++] = sh;
  5571. if (batch_size == 0) {
  5572. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  5573. if (!list_empty(temp_inactive_list + i))
  5574. break;
  5575. if (i == NR_STRIPE_HASH_LOCKS) {
  5576. spin_unlock_irq(&conf->device_lock);
  5577. log_flush_stripe_to_raid(conf);
  5578. spin_lock_irq(&conf->device_lock);
  5579. return batch_size;
  5580. }
  5581. release_inactive = true;
  5582. }
  5583. spin_unlock_irq(&conf->device_lock);
  5584. release_inactive_stripe_list(conf, temp_inactive_list,
  5585. NR_STRIPE_HASH_LOCKS);
  5586. r5l_flush_stripe_to_raid(conf->log);
  5587. if (release_inactive) {
  5588. spin_lock_irq(&conf->device_lock);
  5589. return 0;
  5590. }
  5591. for (i = 0; i < batch_size; i++)
  5592. handle_stripe(batch[i]);
  5593. log_write_stripe_run(conf);
  5594. cond_resched();
  5595. spin_lock_irq(&conf->device_lock);
  5596. for (i = 0; i < batch_size; i++) {
  5597. hash = batch[i]->hash_lock_index;
  5598. __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
  5599. }
  5600. return batch_size;
  5601. }
  5602. static void raid5_do_work(struct work_struct *work)
  5603. {
  5604. struct r5worker *worker = container_of(work, struct r5worker, work);
  5605. struct r5worker_group *group = worker->group;
  5606. struct r5conf *conf = group->conf;
  5607. struct mddev *mddev = conf->mddev;
  5608. int group_id = group - conf->worker_groups;
  5609. int handled;
  5610. struct blk_plug plug;
  5611. pr_debug("+++ raid5worker active\n");
  5612. blk_start_plug(&plug);
  5613. handled = 0;
  5614. spin_lock_irq(&conf->device_lock);
  5615. while (1) {
  5616. int batch_size, released;
  5617. released = release_stripe_list(conf, worker->temp_inactive_list);
  5618. batch_size = handle_active_stripes(conf, group_id, worker,
  5619. worker->temp_inactive_list);
  5620. worker->working = false;
  5621. if (!batch_size && !released)
  5622. break;
  5623. handled += batch_size;
  5624. wait_event_lock_irq(mddev->sb_wait,
  5625. !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
  5626. conf->device_lock);
  5627. }
  5628. pr_debug("%d stripes handled\n", handled);
  5629. spin_unlock_irq(&conf->device_lock);
  5630. flush_deferred_bios(conf);
  5631. r5l_flush_stripe_to_raid(conf->log);
  5632. async_tx_issue_pending_all();
  5633. blk_finish_plug(&plug);
  5634. pr_debug("--- raid5worker inactive\n");
  5635. }
  5636. /*
  5637. * This is our raid5 kernel thread.
  5638. *
  5639. * We scan the hash table for stripes which can be handled now.
  5640. * During the scan, completed stripes are saved for us by the interrupt
  5641. * handler, so that they will not have to wait for our next wakeup.
  5642. */
  5643. static void raid5d(struct md_thread *thread)
  5644. {
  5645. struct mddev *mddev = thread->mddev;
  5646. struct r5conf *conf = mddev->private;
  5647. int handled;
  5648. struct blk_plug plug;
  5649. pr_debug("+++ raid5d active\n");
  5650. md_check_recovery(mddev);
  5651. blk_start_plug(&plug);
  5652. handled = 0;
  5653. spin_lock_irq(&conf->device_lock);
  5654. while (1) {
  5655. struct bio *bio;
  5656. int batch_size, released;
  5657. unsigned int offset;
  5658. released = release_stripe_list(conf, conf->temp_inactive_list);
  5659. if (released)
  5660. clear_bit(R5_DID_ALLOC, &conf->cache_state);
  5661. if (
  5662. !list_empty(&conf->bitmap_list)) {
  5663. /* Now is a good time to flush some bitmap updates */
  5664. conf->seq_flush++;
  5665. spin_unlock_irq(&conf->device_lock);
  5666. md_bitmap_unplug(mddev->bitmap);
  5667. spin_lock_irq(&conf->device_lock);
  5668. conf->seq_write = conf->seq_flush;
  5669. activate_bit_delay(conf, conf->temp_inactive_list);
  5670. }
  5671. raid5_activate_delayed(conf);
  5672. while ((bio = remove_bio_from_retry(conf, &offset))) {
  5673. int ok;
  5674. spin_unlock_irq(&conf->device_lock);
  5675. ok = retry_aligned_read(conf, bio, offset);
  5676. spin_lock_irq(&conf->device_lock);
  5677. if (!ok)
  5678. break;
  5679. handled++;
  5680. }
  5681. batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
  5682. conf->temp_inactive_list);
  5683. if (!batch_size && !released)
  5684. break;
  5685. handled += batch_size;
  5686. if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
  5687. spin_unlock_irq(&conf->device_lock);
  5688. md_check_recovery(mddev);
  5689. spin_lock_irq(&conf->device_lock);
  5690. }
  5691. }
  5692. pr_debug("%d stripes handled\n", handled);
  5693. spin_unlock_irq(&conf->device_lock);
  5694. if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
  5695. mutex_trylock(&conf->cache_size_mutex)) {
  5696. grow_one_stripe(conf, __GFP_NOWARN);
  5697. /* Set flag even if allocation failed. This helps
  5698. * slow down allocation requests when mem is short
  5699. */
  5700. set_bit(R5_DID_ALLOC, &conf->cache_state);
  5701. mutex_unlock(&conf->cache_size_mutex);
  5702. }
  5703. flush_deferred_bios(conf);
  5704. r5l_flush_stripe_to_raid(conf->log);
  5705. async_tx_issue_pending_all();
  5706. blk_finish_plug(&plug);
  5707. pr_debug("--- raid5d inactive\n");
  5708. }
  5709. static ssize_t
  5710. raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
  5711. {
  5712. struct r5conf *conf;
  5713. int ret = 0;
  5714. spin_lock(&mddev->lock);
  5715. conf = mddev->private;
  5716. if (conf)
  5717. ret = sprintf(page, "%d\n", conf->min_nr_stripes);
  5718. spin_unlock(&mddev->lock);
  5719. return ret;
  5720. }
  5721. int
  5722. raid5_set_cache_size(struct mddev *mddev, int size)
  5723. {
  5724. int result = 0;
  5725. struct r5conf *conf = mddev->private;
  5726. if (size <= 16 || size > 32768)
  5727. return -EINVAL;
  5728. conf->min_nr_stripes = size;
  5729. mutex_lock(&conf->cache_size_mutex);
  5730. while (size < conf->max_nr_stripes &&
  5731. drop_one_stripe(conf))
  5732. ;
  5733. mutex_unlock(&conf->cache_size_mutex);
  5734. md_allow_write(mddev);
  5735. mutex_lock(&conf->cache_size_mutex);
  5736. while (size > conf->max_nr_stripes)
  5737. if (!grow_one_stripe(conf, GFP_KERNEL)) {
  5738. conf->min_nr_stripes = conf->max_nr_stripes;
  5739. result = -ENOMEM;
  5740. break;
  5741. }
  5742. mutex_unlock(&conf->cache_size_mutex);
  5743. return result;
  5744. }
  5745. EXPORT_SYMBOL(raid5_set_cache_size);
  5746. static ssize_t
  5747. raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
  5748. {
  5749. struct r5conf *conf;
  5750. unsigned long new;
  5751. int err;
  5752. if (len >= PAGE_SIZE)
  5753. return -EINVAL;
  5754. if (kstrtoul(page, 10, &new))
  5755. return -EINVAL;
  5756. err = mddev_lock(mddev);
  5757. if (err)
  5758. return err;
  5759. conf = mddev->private;
  5760. if (!conf)
  5761. err = -ENODEV;
  5762. else
  5763. err = raid5_set_cache_size(mddev, new);
  5764. mddev_unlock(mddev);
  5765. return err ?: len;
  5766. }
  5767. static struct md_sysfs_entry
  5768. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  5769. raid5_show_stripe_cache_size,
  5770. raid5_store_stripe_cache_size);
  5771. static ssize_t
  5772. raid5_show_rmw_level(struct mddev *mddev, char *page)
  5773. {
  5774. struct r5conf *conf = mddev->private;
  5775. if (conf)
  5776. return sprintf(page, "%d\n", conf->rmw_level);
  5777. else
  5778. return 0;
  5779. }
  5780. static ssize_t
  5781. raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
  5782. {
  5783. struct r5conf *conf = mddev->private;
  5784. unsigned long new;
  5785. if (!conf)
  5786. return -ENODEV;
  5787. if (len >= PAGE_SIZE)
  5788. return -EINVAL;
  5789. if (kstrtoul(page, 10, &new))
  5790. return -EINVAL;
  5791. if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
  5792. return -EINVAL;
  5793. if (new != PARITY_DISABLE_RMW &&
  5794. new != PARITY_ENABLE_RMW &&
  5795. new != PARITY_PREFER_RMW)
  5796. return -EINVAL;
  5797. conf->rmw_level = new;
  5798. return len;
  5799. }
  5800. static struct md_sysfs_entry
  5801. raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
  5802. raid5_show_rmw_level,
  5803. raid5_store_rmw_level);
  5804. static ssize_t
  5805. raid5_show_preread_threshold(struct mddev *mddev, char *page)
  5806. {
  5807. struct r5conf *conf;
  5808. int ret = 0;
  5809. spin_lock(&mddev->lock);
  5810. conf = mddev->private;
  5811. if (conf)
  5812. ret = sprintf(page, "%d\n", conf->bypass_threshold);
  5813. spin_unlock(&mddev->lock);
  5814. return ret;
  5815. }
  5816. static ssize_t
  5817. raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
  5818. {
  5819. struct r5conf *conf;
  5820. unsigned long new;
  5821. int err;
  5822. if (len >= PAGE_SIZE)
  5823. return -EINVAL;
  5824. if (kstrtoul(page, 10, &new))
  5825. return -EINVAL;
  5826. err = mddev_lock(mddev);
  5827. if (err)
  5828. return err;
  5829. conf = mddev->private;
  5830. if (!conf)
  5831. err = -ENODEV;
  5832. else if (new > conf->min_nr_stripes)
  5833. err = -EINVAL;
  5834. else
  5835. conf->bypass_threshold = new;
  5836. mddev_unlock(mddev);
  5837. return err ?: len;
  5838. }
  5839. static struct md_sysfs_entry
  5840. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  5841. S_IRUGO | S_IWUSR,
  5842. raid5_show_preread_threshold,
  5843. raid5_store_preread_threshold);
  5844. static ssize_t
  5845. raid5_show_skip_copy(struct mddev *mddev, char *page)
  5846. {
  5847. struct r5conf *conf;
  5848. int ret = 0;
  5849. spin_lock(&mddev->lock);
  5850. conf = mddev->private;
  5851. if (conf)
  5852. ret = sprintf(page, "%d\n", conf->skip_copy);
  5853. spin_unlock(&mddev->lock);
  5854. return ret;
  5855. }
  5856. static ssize_t
  5857. raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
  5858. {
  5859. struct r5conf *conf;
  5860. unsigned long new;
  5861. int err;
  5862. if (len >= PAGE_SIZE)
  5863. return -EINVAL;
  5864. if (kstrtoul(page, 10, &new))
  5865. return -EINVAL;
  5866. new = !!new;
  5867. err = mddev_lock(mddev);
  5868. if (err)
  5869. return err;
  5870. conf = mddev->private;
  5871. if (!conf)
  5872. err = -ENODEV;
  5873. else if (new != conf->skip_copy) {
  5874. mddev_suspend(mddev);
  5875. conf->skip_copy = new;
  5876. if (new)
  5877. mddev->queue->backing_dev_info->capabilities |=
  5878. BDI_CAP_STABLE_WRITES;
  5879. else
  5880. mddev->queue->backing_dev_info->capabilities &=
  5881. ~BDI_CAP_STABLE_WRITES;
  5882. mddev_resume(mddev);
  5883. }
  5884. mddev_unlock(mddev);
  5885. return err ?: len;
  5886. }
  5887. static struct md_sysfs_entry
  5888. raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
  5889. raid5_show_skip_copy,
  5890. raid5_store_skip_copy);
  5891. static ssize_t
  5892. stripe_cache_active_show(struct mddev *mddev, char *page)
  5893. {
  5894. struct r5conf *conf = mddev->private;
  5895. if (conf)
  5896. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  5897. else
  5898. return 0;
  5899. }
  5900. static struct md_sysfs_entry
  5901. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  5902. static ssize_t
  5903. raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
  5904. {
  5905. struct r5conf *conf;
  5906. int ret = 0;
  5907. spin_lock(&mddev->lock);
  5908. conf = mddev->private;
  5909. if (conf)
  5910. ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
  5911. spin_unlock(&mddev->lock);
  5912. return ret;
  5913. }
  5914. static int alloc_thread_groups(struct r5conf *conf, int cnt,
  5915. int *group_cnt,
  5916. int *worker_cnt_per_group,
  5917. struct r5worker_group **worker_groups);
  5918. static ssize_t
  5919. raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
  5920. {
  5921. struct r5conf *conf;
  5922. unsigned int new;
  5923. int err;
  5924. struct r5worker_group *new_groups, *old_groups;
  5925. int group_cnt, worker_cnt_per_group;
  5926. if (len >= PAGE_SIZE)
  5927. return -EINVAL;
  5928. if (kstrtouint(page, 10, &new))
  5929. return -EINVAL;
  5930. /* 8192 should be big enough */
  5931. if (new > 8192)
  5932. return -EINVAL;
  5933. err = mddev_lock(mddev);
  5934. if (err)
  5935. return err;
  5936. conf = mddev->private;
  5937. if (!conf)
  5938. err = -ENODEV;
  5939. else if (new != conf->worker_cnt_per_group) {
  5940. mddev_suspend(mddev);
  5941. old_groups = conf->worker_groups;
  5942. if (old_groups)
  5943. flush_workqueue(raid5_wq);
  5944. err = alloc_thread_groups(conf, new,
  5945. &group_cnt, &worker_cnt_per_group,
  5946. &new_groups);
  5947. if (!err) {
  5948. spin_lock_irq(&conf->device_lock);
  5949. conf->group_cnt = group_cnt;
  5950. conf->worker_cnt_per_group = worker_cnt_per_group;
  5951. conf->worker_groups = new_groups;
  5952. spin_unlock_irq(&conf->device_lock);
  5953. if (old_groups)
  5954. kfree(old_groups[0].workers);
  5955. kfree(old_groups);
  5956. }
  5957. mddev_resume(mddev);
  5958. }
  5959. mddev_unlock(mddev);
  5960. return err ?: len;
  5961. }
  5962. static struct md_sysfs_entry
  5963. raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
  5964. raid5_show_group_thread_cnt,
  5965. raid5_store_group_thread_cnt);
  5966. static struct attribute *raid5_attrs[] = {
  5967. &raid5_stripecache_size.attr,
  5968. &raid5_stripecache_active.attr,
  5969. &raid5_preread_bypass_threshold.attr,
  5970. &raid5_group_thread_cnt.attr,
  5971. &raid5_skip_copy.attr,
  5972. &raid5_rmw_level.attr,
  5973. &r5c_journal_mode.attr,
  5974. NULL,
  5975. };
  5976. static struct attribute_group raid5_attrs_group = {
  5977. .name = NULL,
  5978. .attrs = raid5_attrs,
  5979. };
  5980. static int alloc_thread_groups(struct r5conf *conf, int cnt,
  5981. int *group_cnt,
  5982. int *worker_cnt_per_group,
  5983. struct r5worker_group **worker_groups)
  5984. {
  5985. int i, j, k;
  5986. ssize_t size;
  5987. struct r5worker *workers;
  5988. *worker_cnt_per_group = cnt;
  5989. if (cnt == 0) {
  5990. *group_cnt = 0;
  5991. *worker_groups = NULL;
  5992. return 0;
  5993. }
  5994. *group_cnt = num_possible_nodes();
  5995. size = sizeof(struct r5worker) * cnt;
  5996. workers = kcalloc(size, *group_cnt, GFP_NOIO);
  5997. *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
  5998. GFP_NOIO);
  5999. if (!*worker_groups || !workers) {
  6000. kfree(workers);
  6001. kfree(*worker_groups);
  6002. return -ENOMEM;
  6003. }
  6004. for (i = 0; i < *group_cnt; i++) {
  6005. struct r5worker_group *group;
  6006. group = &(*worker_groups)[i];
  6007. INIT_LIST_HEAD(&group->handle_list);
  6008. INIT_LIST_HEAD(&group->loprio_list);
  6009. group->conf = conf;
  6010. group->workers = workers + i * cnt;
  6011. for (j = 0; j < cnt; j++) {
  6012. struct r5worker *worker = group->workers + j;
  6013. worker->group = group;
  6014. INIT_WORK(&worker->work, raid5_do_work);
  6015. for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
  6016. INIT_LIST_HEAD(worker->temp_inactive_list + k);
  6017. }
  6018. }
  6019. return 0;
  6020. }
  6021. static void free_thread_groups(struct r5conf *conf)
  6022. {
  6023. if (conf->worker_groups)
  6024. kfree(conf->worker_groups[0].workers);
  6025. kfree(conf->worker_groups);
  6026. conf->worker_groups = NULL;
  6027. }
  6028. static sector_t
  6029. raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  6030. {
  6031. struct r5conf *conf = mddev->private;
  6032. if (!sectors)
  6033. sectors = mddev->dev_sectors;
  6034. if (!raid_disks)
  6035. /* size is defined by the smallest of previous and new size */
  6036. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  6037. sectors &= ~((sector_t)conf->chunk_sectors - 1);
  6038. sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
  6039. return sectors * (raid_disks - conf->max_degraded);
  6040. }
  6041. static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
  6042. {
  6043. safe_put_page(percpu->spare_page);
  6044. if (percpu->scribble)
  6045. flex_array_free(percpu->scribble);
  6046. percpu->spare_page = NULL;
  6047. percpu->scribble = NULL;
  6048. }
  6049. static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
  6050. {
  6051. if (conf->level == 6 && !percpu->spare_page)
  6052. percpu->spare_page = alloc_page(GFP_KERNEL);
  6053. if (!percpu->scribble)
  6054. percpu->scribble = scribble_alloc(max(conf->raid_disks,
  6055. conf->previous_raid_disks),
  6056. max(conf->chunk_sectors,
  6057. conf->prev_chunk_sectors)
  6058. / STRIPE_SECTORS,
  6059. GFP_KERNEL);
  6060. if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
  6061. free_scratch_buffer(conf, percpu);
  6062. return -ENOMEM;
  6063. }
  6064. return 0;
  6065. }
  6066. static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
  6067. {
  6068. struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
  6069. free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
  6070. return 0;
  6071. }
  6072. static void raid5_free_percpu(struct r5conf *conf)
  6073. {
  6074. if (!conf->percpu)
  6075. return;
  6076. cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
  6077. free_percpu(conf->percpu);
  6078. }
  6079. static void free_conf(struct r5conf *conf)
  6080. {
  6081. int i;
  6082. log_exit(conf);
  6083. unregister_shrinker(&conf->shrinker);
  6084. free_thread_groups(conf);
  6085. shrink_stripes(conf);
  6086. raid5_free_percpu(conf);
  6087. for (i = 0; i < conf->pool_size; i++)
  6088. if (conf->disks[i].extra_page)
  6089. put_page(conf->disks[i].extra_page);
  6090. kfree(conf->disks);
  6091. bioset_exit(&conf->bio_split);
  6092. kfree(conf->stripe_hashtbl);
  6093. kfree(conf->pending_data);
  6094. kfree(conf);
  6095. }
  6096. static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
  6097. {
  6098. struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
  6099. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  6100. if (alloc_scratch_buffer(conf, percpu)) {
  6101. pr_warn("%s: failed memory allocation for cpu%u\n",
  6102. __func__, cpu);
  6103. return -ENOMEM;
  6104. }
  6105. return 0;
  6106. }
  6107. static int raid5_alloc_percpu(struct r5conf *conf)
  6108. {
  6109. int err = 0;
  6110. conf->percpu = alloc_percpu(struct raid5_percpu);
  6111. if (!conf->percpu)
  6112. return -ENOMEM;
  6113. err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
  6114. if (!err) {
  6115. conf->scribble_disks = max(conf->raid_disks,
  6116. conf->previous_raid_disks);
  6117. conf->scribble_sectors = max(conf->chunk_sectors,
  6118. conf->prev_chunk_sectors);
  6119. }
  6120. return err;
  6121. }
  6122. static unsigned long raid5_cache_scan(struct shrinker *shrink,
  6123. struct shrink_control *sc)
  6124. {
  6125. struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
  6126. unsigned long ret = SHRINK_STOP;
  6127. if (mutex_trylock(&conf->cache_size_mutex)) {
  6128. ret= 0;
  6129. while (ret < sc->nr_to_scan &&
  6130. conf->max_nr_stripes > conf->min_nr_stripes) {
  6131. if (drop_one_stripe(conf) == 0) {
  6132. ret = SHRINK_STOP;
  6133. break;
  6134. }
  6135. ret++;
  6136. }
  6137. mutex_unlock(&conf->cache_size_mutex);
  6138. }
  6139. return ret;
  6140. }
  6141. static unsigned long raid5_cache_count(struct shrinker *shrink,
  6142. struct shrink_control *sc)
  6143. {
  6144. struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
  6145. if (conf->max_nr_stripes < conf->min_nr_stripes)
  6146. /* unlikely, but not impossible */
  6147. return 0;
  6148. return conf->max_nr_stripes - conf->min_nr_stripes;
  6149. }
  6150. static struct r5conf *setup_conf(struct mddev *mddev)
  6151. {
  6152. struct r5conf *conf;
  6153. int raid_disk, memory, max_disks;
  6154. struct md_rdev *rdev;
  6155. struct disk_info *disk;
  6156. char pers_name[6];
  6157. int i;
  6158. int group_cnt, worker_cnt_per_group;
  6159. struct r5worker_group *new_group;
  6160. int ret;
  6161. if (mddev->new_level != 5
  6162. && mddev->new_level != 4
  6163. && mddev->new_level != 6) {
  6164. pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  6165. mdname(mddev), mddev->new_level);
  6166. return ERR_PTR(-EIO);
  6167. }
  6168. if ((mddev->new_level == 5
  6169. && !algorithm_valid_raid5(mddev->new_layout)) ||
  6170. (mddev->new_level == 6
  6171. && !algorithm_valid_raid6(mddev->new_layout))) {
  6172. pr_warn("md/raid:%s: layout %d not supported\n",
  6173. mdname(mddev), mddev->new_layout);
  6174. return ERR_PTR(-EIO);
  6175. }
  6176. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  6177. pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  6178. mdname(mddev), mddev->raid_disks);
  6179. return ERR_PTR(-EINVAL);
  6180. }
  6181. if (!mddev->new_chunk_sectors ||
  6182. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  6183. !is_power_of_2(mddev->new_chunk_sectors)) {
  6184. pr_warn("md/raid:%s: invalid chunk size %d\n",
  6185. mdname(mddev), mddev->new_chunk_sectors << 9);
  6186. return ERR_PTR(-EINVAL);
  6187. }
  6188. conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
  6189. if (conf == NULL)
  6190. goto abort;
  6191. INIT_LIST_HEAD(&conf->free_list);
  6192. INIT_LIST_HEAD(&conf->pending_list);
  6193. conf->pending_data = kcalloc(PENDING_IO_MAX,
  6194. sizeof(struct r5pending_data),
  6195. GFP_KERNEL);
  6196. if (!conf->pending_data)
  6197. goto abort;
  6198. for (i = 0; i < PENDING_IO_MAX; i++)
  6199. list_add(&conf->pending_data[i].sibling, &conf->free_list);
  6200. /* Don't enable multi-threading by default*/
  6201. if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
  6202. &new_group)) {
  6203. conf->group_cnt = group_cnt;
  6204. conf->worker_cnt_per_group = worker_cnt_per_group;
  6205. conf->worker_groups = new_group;
  6206. } else
  6207. goto abort;
  6208. spin_lock_init(&conf->device_lock);
  6209. seqcount_init(&conf->gen_lock);
  6210. mutex_init(&conf->cache_size_mutex);
  6211. init_waitqueue_head(&conf->wait_for_quiescent);
  6212. init_waitqueue_head(&conf->wait_for_stripe);
  6213. init_waitqueue_head(&conf->wait_for_overlap);
  6214. INIT_LIST_HEAD(&conf->handle_list);
  6215. INIT_LIST_HEAD(&conf->loprio_list);
  6216. INIT_LIST_HEAD(&conf->hold_list);
  6217. INIT_LIST_HEAD(&conf->delayed_list);
  6218. INIT_LIST_HEAD(&conf->bitmap_list);
  6219. init_llist_head(&conf->released_stripes);
  6220. atomic_set(&conf->active_stripes, 0);
  6221. atomic_set(&conf->preread_active_stripes, 0);
  6222. atomic_set(&conf->active_aligned_reads, 0);
  6223. spin_lock_init(&conf->pending_bios_lock);
  6224. conf->batch_bio_dispatch = true;
  6225. rdev_for_each(rdev, mddev) {
  6226. if (test_bit(Journal, &rdev->flags))
  6227. continue;
  6228. if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
  6229. conf->batch_bio_dispatch = false;
  6230. break;
  6231. }
  6232. }
  6233. conf->bypass_threshold = BYPASS_THRESHOLD;
  6234. conf->recovery_disabled = mddev->recovery_disabled - 1;
  6235. conf->raid_disks = mddev->raid_disks;
  6236. if (mddev->reshape_position == MaxSector)
  6237. conf->previous_raid_disks = mddev->raid_disks;
  6238. else
  6239. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  6240. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  6241. conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
  6242. GFP_KERNEL);
  6243. if (!conf->disks)
  6244. goto abort;
  6245. for (i = 0; i < max_disks; i++) {
  6246. conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
  6247. if (!conf->disks[i].extra_page)
  6248. goto abort;
  6249. }
  6250. ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
  6251. if (ret)
  6252. goto abort;
  6253. conf->mddev = mddev;
  6254. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  6255. goto abort;
  6256. /* We init hash_locks[0] separately to that it can be used
  6257. * as the reference lock in the spin_lock_nest_lock() call
  6258. * in lock_all_device_hash_locks_irq in order to convince
  6259. * lockdep that we know what we are doing.
  6260. */
  6261. spin_lock_init(conf->hash_locks);
  6262. for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
  6263. spin_lock_init(conf->hash_locks + i);
  6264. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  6265. INIT_LIST_HEAD(conf->inactive_list + i);
  6266. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  6267. INIT_LIST_HEAD(conf->temp_inactive_list + i);
  6268. atomic_set(&conf->r5c_cached_full_stripes, 0);
  6269. INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
  6270. atomic_set(&conf->r5c_cached_partial_stripes, 0);
  6271. INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
  6272. atomic_set(&conf->r5c_flushing_full_stripes, 0);
  6273. atomic_set(&conf->r5c_flushing_partial_stripes, 0);
  6274. conf->level = mddev->new_level;
  6275. conf->chunk_sectors = mddev->new_chunk_sectors;
  6276. if (raid5_alloc_percpu(conf) != 0)
  6277. goto abort;
  6278. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  6279. rdev_for_each(rdev, mddev) {
  6280. raid_disk = rdev->raid_disk;
  6281. if (raid_disk >= max_disks
  6282. || raid_disk < 0 || test_bit(Journal, &rdev->flags))
  6283. continue;
  6284. disk = conf->disks + raid_disk;
  6285. if (test_bit(Replacement, &rdev->flags)) {
  6286. if (disk->replacement)
  6287. goto abort;
  6288. disk->replacement = rdev;
  6289. } else {
  6290. if (disk->rdev)
  6291. goto abort;
  6292. disk->rdev = rdev;
  6293. }
  6294. if (test_bit(In_sync, &rdev->flags)) {
  6295. char b[BDEVNAME_SIZE];
  6296. pr_info("md/raid:%s: device %s operational as raid disk %d\n",
  6297. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  6298. } else if (rdev->saved_raid_disk != raid_disk)
  6299. /* Cannot rely on bitmap to complete recovery */
  6300. conf->fullsync = 1;
  6301. }
  6302. conf->level = mddev->new_level;
  6303. if (conf->level == 6) {
  6304. conf->max_degraded = 2;
  6305. if (raid6_call.xor_syndrome)
  6306. conf->rmw_level = PARITY_ENABLE_RMW;
  6307. else
  6308. conf->rmw_level = PARITY_DISABLE_RMW;
  6309. } else {
  6310. conf->max_degraded = 1;
  6311. conf->rmw_level = PARITY_ENABLE_RMW;
  6312. }
  6313. conf->algorithm = mddev->new_layout;
  6314. conf->reshape_progress = mddev->reshape_position;
  6315. if (conf->reshape_progress != MaxSector) {
  6316. conf->prev_chunk_sectors = mddev->chunk_sectors;
  6317. conf->prev_algo = mddev->layout;
  6318. } else {
  6319. conf->prev_chunk_sectors = conf->chunk_sectors;
  6320. conf->prev_algo = conf->algorithm;
  6321. }
  6322. conf->min_nr_stripes = NR_STRIPES;
  6323. if (mddev->reshape_position != MaxSector) {
  6324. int stripes = max_t(int,
  6325. ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
  6326. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
  6327. conf->min_nr_stripes = max(NR_STRIPES, stripes);
  6328. if (conf->min_nr_stripes != NR_STRIPES)
  6329. pr_info("md/raid:%s: force stripe size %d for reshape\n",
  6330. mdname(mddev), conf->min_nr_stripes);
  6331. }
  6332. memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
  6333. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  6334. atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
  6335. if (grow_stripes(conf, conf->min_nr_stripes)) {
  6336. pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
  6337. mdname(mddev), memory);
  6338. goto abort;
  6339. } else
  6340. pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
  6341. /*
  6342. * Losing a stripe head costs more than the time to refill it,
  6343. * it reduces the queue depth and so can hurt throughput.
  6344. * So set it rather large, scaled by number of devices.
  6345. */
  6346. conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
  6347. conf->shrinker.scan_objects = raid5_cache_scan;
  6348. conf->shrinker.count_objects = raid5_cache_count;
  6349. conf->shrinker.batch = 128;
  6350. conf->shrinker.flags = 0;
  6351. if (register_shrinker(&conf->shrinker)) {
  6352. pr_warn("md/raid:%s: couldn't register shrinker.\n",
  6353. mdname(mddev));
  6354. goto abort;
  6355. }
  6356. sprintf(pers_name, "raid%d", mddev->new_level);
  6357. conf->thread = md_register_thread(raid5d, mddev, pers_name);
  6358. if (!conf->thread) {
  6359. pr_warn("md/raid:%s: couldn't allocate thread.\n",
  6360. mdname(mddev));
  6361. goto abort;
  6362. }
  6363. return conf;
  6364. abort:
  6365. if (conf) {
  6366. free_conf(conf);
  6367. return ERR_PTR(-EIO);
  6368. } else
  6369. return ERR_PTR(-ENOMEM);
  6370. }
  6371. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  6372. {
  6373. switch (algo) {
  6374. case ALGORITHM_PARITY_0:
  6375. if (raid_disk < max_degraded)
  6376. return 1;
  6377. break;
  6378. case ALGORITHM_PARITY_N:
  6379. if (raid_disk >= raid_disks - max_degraded)
  6380. return 1;
  6381. break;
  6382. case ALGORITHM_PARITY_0_6:
  6383. if (raid_disk == 0 ||
  6384. raid_disk == raid_disks - 1)
  6385. return 1;
  6386. break;
  6387. case ALGORITHM_LEFT_ASYMMETRIC_6:
  6388. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  6389. case ALGORITHM_LEFT_SYMMETRIC_6:
  6390. case ALGORITHM_RIGHT_SYMMETRIC_6:
  6391. if (raid_disk == raid_disks - 1)
  6392. return 1;
  6393. }
  6394. return 0;
  6395. }
  6396. static int raid5_run(struct mddev *mddev)
  6397. {
  6398. struct r5conf *conf;
  6399. int working_disks = 0;
  6400. int dirty_parity_disks = 0;
  6401. struct md_rdev *rdev;
  6402. struct md_rdev *journal_dev = NULL;
  6403. sector_t reshape_offset = 0;
  6404. int i;
  6405. long long min_offset_diff = 0;
  6406. int first = 1;
  6407. if (mddev_init_writes_pending(mddev) < 0)
  6408. return -ENOMEM;
  6409. if (mddev->recovery_cp != MaxSector)
  6410. pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
  6411. mdname(mddev));
  6412. rdev_for_each(rdev, mddev) {
  6413. long long diff;
  6414. if (test_bit(Journal, &rdev->flags)) {
  6415. journal_dev = rdev;
  6416. continue;
  6417. }
  6418. if (rdev->raid_disk < 0)
  6419. continue;
  6420. diff = (rdev->new_data_offset - rdev->data_offset);
  6421. if (first) {
  6422. min_offset_diff = diff;
  6423. first = 0;
  6424. } else if (mddev->reshape_backwards &&
  6425. diff < min_offset_diff)
  6426. min_offset_diff = diff;
  6427. else if (!mddev->reshape_backwards &&
  6428. diff > min_offset_diff)
  6429. min_offset_diff = diff;
  6430. }
  6431. if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
  6432. (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
  6433. pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
  6434. mdname(mddev));
  6435. return -EINVAL;
  6436. }
  6437. if (mddev->reshape_position != MaxSector) {
  6438. /* Check that we can continue the reshape.
  6439. * Difficulties arise if the stripe we would write to
  6440. * next is at or after the stripe we would read from next.
  6441. * For a reshape that changes the number of devices, this
  6442. * is only possible for a very short time, and mdadm makes
  6443. * sure that time appears to have past before assembling
  6444. * the array. So we fail if that time hasn't passed.
  6445. * For a reshape that keeps the number of devices the same
  6446. * mdadm must be monitoring the reshape can keeping the
  6447. * critical areas read-only and backed up. It will start
  6448. * the array in read-only mode, so we check for that.
  6449. */
  6450. sector_t here_new, here_old;
  6451. int old_disks;
  6452. int max_degraded = (mddev->level == 6 ? 2 : 1);
  6453. int chunk_sectors;
  6454. int new_data_disks;
  6455. if (journal_dev) {
  6456. pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
  6457. mdname(mddev));
  6458. return -EINVAL;
  6459. }
  6460. if (mddev->new_level != mddev->level) {
  6461. pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
  6462. mdname(mddev));
  6463. return -EINVAL;
  6464. }
  6465. old_disks = mddev->raid_disks - mddev->delta_disks;
  6466. /* reshape_position must be on a new-stripe boundary, and one
  6467. * further up in new geometry must map after here in old
  6468. * geometry.
  6469. * If the chunk sizes are different, then as we perform reshape
  6470. * in units of the largest of the two, reshape_position needs
  6471. * be a multiple of the largest chunk size times new data disks.
  6472. */
  6473. here_new = mddev->reshape_position;
  6474. chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
  6475. new_data_disks = mddev->raid_disks - max_degraded;
  6476. if (sector_div(here_new, chunk_sectors * new_data_disks)) {
  6477. pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
  6478. mdname(mddev));
  6479. return -EINVAL;
  6480. }
  6481. reshape_offset = here_new * chunk_sectors;
  6482. /* here_new is the stripe we will write to */
  6483. here_old = mddev->reshape_position;
  6484. sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
  6485. /* here_old is the first stripe that we might need to read
  6486. * from */
  6487. if (mddev->delta_disks == 0) {
  6488. /* We cannot be sure it is safe to start an in-place
  6489. * reshape. It is only safe if user-space is monitoring
  6490. * and taking constant backups.
  6491. * mdadm always starts a situation like this in
  6492. * readonly mode so it can take control before
  6493. * allowing any writes. So just check for that.
  6494. */
  6495. if (abs(min_offset_diff) >= mddev->chunk_sectors &&
  6496. abs(min_offset_diff) >= mddev->new_chunk_sectors)
  6497. /* not really in-place - so OK */;
  6498. else if (mddev->ro == 0) {
  6499. pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
  6500. mdname(mddev));
  6501. return -EINVAL;
  6502. }
  6503. } else if (mddev->reshape_backwards
  6504. ? (here_new * chunk_sectors + min_offset_diff <=
  6505. here_old * chunk_sectors)
  6506. : (here_new * chunk_sectors >=
  6507. here_old * chunk_sectors + (-min_offset_diff))) {
  6508. /* Reading from the same stripe as writing to - bad */
  6509. pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
  6510. mdname(mddev));
  6511. return -EINVAL;
  6512. }
  6513. pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
  6514. /* OK, we should be able to continue; */
  6515. } else {
  6516. BUG_ON(mddev->level != mddev->new_level);
  6517. BUG_ON(mddev->layout != mddev->new_layout);
  6518. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  6519. BUG_ON(mddev->delta_disks != 0);
  6520. }
  6521. if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
  6522. test_bit(MD_HAS_PPL, &mddev->flags)) {
  6523. pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
  6524. mdname(mddev));
  6525. clear_bit(MD_HAS_PPL, &mddev->flags);
  6526. clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
  6527. }
  6528. if (mddev->private == NULL)
  6529. conf = setup_conf(mddev);
  6530. else
  6531. conf = mddev->private;
  6532. if (IS_ERR(conf))
  6533. return PTR_ERR(conf);
  6534. if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
  6535. if (!journal_dev) {
  6536. pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
  6537. mdname(mddev));
  6538. mddev->ro = 1;
  6539. set_disk_ro(mddev->gendisk, 1);
  6540. } else if (mddev->recovery_cp == MaxSector)
  6541. set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
  6542. }
  6543. conf->min_offset_diff = min_offset_diff;
  6544. mddev->thread = conf->thread;
  6545. conf->thread = NULL;
  6546. mddev->private = conf;
  6547. for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
  6548. i++) {
  6549. rdev = conf->disks[i].rdev;
  6550. if (!rdev && conf->disks[i].replacement) {
  6551. /* The replacement is all we have yet */
  6552. rdev = conf->disks[i].replacement;
  6553. conf->disks[i].replacement = NULL;
  6554. clear_bit(Replacement, &rdev->flags);
  6555. conf->disks[i].rdev = rdev;
  6556. }
  6557. if (!rdev)
  6558. continue;
  6559. if (conf->disks[i].replacement &&
  6560. conf->reshape_progress != MaxSector) {
  6561. /* replacements and reshape simply do not mix. */
  6562. pr_warn("md: cannot handle concurrent replacement and reshape.\n");
  6563. goto abort;
  6564. }
  6565. if (test_bit(In_sync, &rdev->flags)) {
  6566. working_disks++;
  6567. continue;
  6568. }
  6569. /* This disc is not fully in-sync. However if it
  6570. * just stored parity (beyond the recovery_offset),
  6571. * when we don't need to be concerned about the
  6572. * array being dirty.
  6573. * When reshape goes 'backwards', we never have
  6574. * partially completed devices, so we only need
  6575. * to worry about reshape going forwards.
  6576. */
  6577. /* Hack because v0.91 doesn't store recovery_offset properly. */
  6578. if (mddev->major_version == 0 &&
  6579. mddev->minor_version > 90)
  6580. rdev->recovery_offset = reshape_offset;
  6581. if (rdev->recovery_offset < reshape_offset) {
  6582. /* We need to check old and new layout */
  6583. if (!only_parity(rdev->raid_disk,
  6584. conf->algorithm,
  6585. conf->raid_disks,
  6586. conf->max_degraded))
  6587. continue;
  6588. }
  6589. if (!only_parity(rdev->raid_disk,
  6590. conf->prev_algo,
  6591. conf->previous_raid_disks,
  6592. conf->max_degraded))
  6593. continue;
  6594. dirty_parity_disks++;
  6595. }
  6596. /*
  6597. * 0 for a fully functional array, 1 or 2 for a degraded array.
  6598. */
  6599. mddev->degraded = raid5_calc_degraded(conf);
  6600. if (has_failed(conf)) {
  6601. pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
  6602. mdname(mddev), mddev->degraded, conf->raid_disks);
  6603. goto abort;
  6604. }
  6605. /* device size must be a multiple of chunk size */
  6606. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  6607. mddev->resync_max_sectors = mddev->dev_sectors;
  6608. if (mddev->degraded > dirty_parity_disks &&
  6609. mddev->recovery_cp != MaxSector) {
  6610. if (test_bit(MD_HAS_PPL, &mddev->flags))
  6611. pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
  6612. mdname(mddev));
  6613. else if (mddev->ok_start_degraded)
  6614. pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
  6615. mdname(mddev));
  6616. else {
  6617. pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
  6618. mdname(mddev));
  6619. goto abort;
  6620. }
  6621. }
  6622. pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
  6623. mdname(mddev), conf->level,
  6624. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  6625. mddev->new_layout);
  6626. print_raid5_conf(conf);
  6627. if (conf->reshape_progress != MaxSector) {
  6628. conf->reshape_safe = conf->reshape_progress;
  6629. atomic_set(&conf->reshape_stripes, 0);
  6630. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6631. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  6632. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  6633. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6634. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  6635. "reshape");
  6636. if (!mddev->sync_thread)
  6637. goto abort;
  6638. }
  6639. /* Ok, everything is just fine now */
  6640. if (mddev->to_remove == &raid5_attrs_group)
  6641. mddev->to_remove = NULL;
  6642. else if (mddev->kobj.sd &&
  6643. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  6644. pr_warn("raid5: failed to create sysfs attributes for %s\n",
  6645. mdname(mddev));
  6646. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  6647. if (mddev->queue) {
  6648. int chunk_size;
  6649. /* read-ahead size must cover two whole stripes, which
  6650. * is 2 * (datadisks) * chunksize where 'n' is the
  6651. * number of raid devices
  6652. */
  6653. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  6654. int stripe = data_disks *
  6655. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  6656. if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
  6657. mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
  6658. chunk_size = mddev->chunk_sectors << 9;
  6659. blk_queue_io_min(mddev->queue, chunk_size);
  6660. blk_queue_io_opt(mddev->queue, chunk_size *
  6661. (conf->raid_disks - conf->max_degraded));
  6662. mddev->queue->limits.raid_partial_stripes_expensive = 1;
  6663. /*
  6664. * We can only discard a whole stripe. It doesn't make sense to
  6665. * discard data disk but write parity disk
  6666. */
  6667. stripe = stripe * PAGE_SIZE;
  6668. /* Round up to power of 2, as discard handling
  6669. * currently assumes that */
  6670. while ((stripe-1) & stripe)
  6671. stripe = (stripe | (stripe-1)) + 1;
  6672. mddev->queue->limits.discard_alignment = stripe;
  6673. mddev->queue->limits.discard_granularity = stripe;
  6674. blk_queue_max_write_same_sectors(mddev->queue, 0);
  6675. blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
  6676. rdev_for_each(rdev, mddev) {
  6677. disk_stack_limits(mddev->gendisk, rdev->bdev,
  6678. rdev->data_offset << 9);
  6679. disk_stack_limits(mddev->gendisk, rdev->bdev,
  6680. rdev->new_data_offset << 9);
  6681. }
  6682. /*
  6683. * zeroing is required, otherwise data
  6684. * could be lost. Consider a scenario: discard a stripe
  6685. * (the stripe could be inconsistent if
  6686. * discard_zeroes_data is 0); write one disk of the
  6687. * stripe (the stripe could be inconsistent again
  6688. * depending on which disks are used to calculate
  6689. * parity); the disk is broken; The stripe data of this
  6690. * disk is lost.
  6691. *
  6692. * We only allow DISCARD if the sysadmin has confirmed that
  6693. * only safe devices are in use by setting a module parameter.
  6694. * A better idea might be to turn DISCARD into WRITE_ZEROES
  6695. * requests, as that is required to be safe.
  6696. */
  6697. if (devices_handle_discard_safely &&
  6698. mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
  6699. mddev->queue->limits.discard_granularity >= stripe)
  6700. blk_queue_flag_set(QUEUE_FLAG_DISCARD,
  6701. mddev->queue);
  6702. else
  6703. blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
  6704. mddev->queue);
  6705. blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
  6706. }
  6707. if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
  6708. goto abort;
  6709. return 0;
  6710. abort:
  6711. md_unregister_thread(&mddev->thread);
  6712. print_raid5_conf(conf);
  6713. free_conf(conf);
  6714. mddev->private = NULL;
  6715. pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
  6716. return -EIO;
  6717. }
  6718. static void raid5_free(struct mddev *mddev, void *priv)
  6719. {
  6720. struct r5conf *conf = priv;
  6721. free_conf(conf);
  6722. mddev->to_remove = &raid5_attrs_group;
  6723. }
  6724. static void raid5_status(struct seq_file *seq, struct mddev *mddev)
  6725. {
  6726. struct r5conf *conf = mddev->private;
  6727. int i;
  6728. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  6729. conf->chunk_sectors / 2, mddev->layout);
  6730. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  6731. rcu_read_lock();
  6732. for (i = 0; i < conf->raid_disks; i++) {
  6733. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  6734. seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  6735. }
  6736. rcu_read_unlock();
  6737. seq_printf (seq, "]");
  6738. }
  6739. static void print_raid5_conf (struct r5conf *conf)
  6740. {
  6741. int i;
  6742. struct disk_info *tmp;
  6743. pr_debug("RAID conf printout:\n");
  6744. if (!conf) {
  6745. pr_debug("(conf==NULL)\n");
  6746. return;
  6747. }
  6748. pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
  6749. conf->raid_disks,
  6750. conf->raid_disks - conf->mddev->degraded);
  6751. for (i = 0; i < conf->raid_disks; i++) {
  6752. char b[BDEVNAME_SIZE];
  6753. tmp = conf->disks + i;
  6754. if (tmp->rdev)
  6755. pr_debug(" disk %d, o:%d, dev:%s\n",
  6756. i, !test_bit(Faulty, &tmp->rdev->flags),
  6757. bdevname(tmp->rdev->bdev, b));
  6758. }
  6759. }
  6760. static int raid5_spare_active(struct mddev *mddev)
  6761. {
  6762. int i;
  6763. struct r5conf *conf = mddev->private;
  6764. struct disk_info *tmp;
  6765. int count = 0;
  6766. unsigned long flags;
  6767. for (i = 0; i < conf->raid_disks; i++) {
  6768. tmp = conf->disks + i;
  6769. if (tmp->replacement
  6770. && tmp->replacement->recovery_offset == MaxSector
  6771. && !test_bit(Faulty, &tmp->replacement->flags)
  6772. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  6773. /* Replacement has just become active. */
  6774. if (!tmp->rdev
  6775. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  6776. count++;
  6777. if (tmp->rdev) {
  6778. /* Replaced device not technically faulty,
  6779. * but we need to be sure it gets removed
  6780. * and never re-added.
  6781. */
  6782. set_bit(Faulty, &tmp->rdev->flags);
  6783. sysfs_notify_dirent_safe(
  6784. tmp->rdev->sysfs_state);
  6785. }
  6786. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  6787. } else if (tmp->rdev
  6788. && tmp->rdev->recovery_offset == MaxSector
  6789. && !test_bit(Faulty, &tmp->rdev->flags)
  6790. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  6791. count++;
  6792. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  6793. }
  6794. }
  6795. spin_lock_irqsave(&conf->device_lock, flags);
  6796. mddev->degraded = raid5_calc_degraded(conf);
  6797. spin_unlock_irqrestore(&conf->device_lock, flags);
  6798. print_raid5_conf(conf);
  6799. return count;
  6800. }
  6801. static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  6802. {
  6803. struct r5conf *conf = mddev->private;
  6804. int err = 0;
  6805. int number = rdev->raid_disk;
  6806. struct md_rdev **rdevp;
  6807. struct disk_info *p = conf->disks + number;
  6808. print_raid5_conf(conf);
  6809. if (test_bit(Journal, &rdev->flags) && conf->log) {
  6810. /*
  6811. * we can't wait pending write here, as this is called in
  6812. * raid5d, wait will deadlock.
  6813. * neilb: there is no locking about new writes here,
  6814. * so this cannot be safe.
  6815. */
  6816. if (atomic_read(&conf->active_stripes) ||
  6817. atomic_read(&conf->r5c_cached_full_stripes) ||
  6818. atomic_read(&conf->r5c_cached_partial_stripes)) {
  6819. return -EBUSY;
  6820. }
  6821. log_exit(conf);
  6822. return 0;
  6823. }
  6824. if (rdev == p->rdev)
  6825. rdevp = &p->rdev;
  6826. else if (rdev == p->replacement)
  6827. rdevp = &p->replacement;
  6828. else
  6829. return 0;
  6830. if (number >= conf->raid_disks &&
  6831. conf->reshape_progress == MaxSector)
  6832. clear_bit(In_sync, &rdev->flags);
  6833. if (test_bit(In_sync, &rdev->flags) ||
  6834. atomic_read(&rdev->nr_pending)) {
  6835. err = -EBUSY;
  6836. goto abort;
  6837. }
  6838. /* Only remove non-faulty devices if recovery
  6839. * isn't possible.
  6840. */
  6841. if (!test_bit(Faulty, &rdev->flags) &&
  6842. mddev->recovery_disabled != conf->recovery_disabled &&
  6843. !has_failed(conf) &&
  6844. (!p->replacement || p->replacement == rdev) &&
  6845. number < conf->raid_disks) {
  6846. err = -EBUSY;
  6847. goto abort;
  6848. }
  6849. *rdevp = NULL;
  6850. if (!test_bit(RemoveSynchronized, &rdev->flags)) {
  6851. synchronize_rcu();
  6852. if (atomic_read(&rdev->nr_pending)) {
  6853. /* lost the race, try later */
  6854. err = -EBUSY;
  6855. *rdevp = rdev;
  6856. }
  6857. }
  6858. if (!err) {
  6859. err = log_modify(conf, rdev, false);
  6860. if (err)
  6861. goto abort;
  6862. }
  6863. if (p->replacement) {
  6864. /* We must have just cleared 'rdev' */
  6865. p->rdev = p->replacement;
  6866. clear_bit(Replacement, &p->replacement->flags);
  6867. smp_mb(); /* Make sure other CPUs may see both as identical
  6868. * but will never see neither - if they are careful
  6869. */
  6870. p->replacement = NULL;
  6871. if (!err)
  6872. err = log_modify(conf, p->rdev, true);
  6873. }
  6874. clear_bit(WantReplacement, &rdev->flags);
  6875. abort:
  6876. print_raid5_conf(conf);
  6877. return err;
  6878. }
  6879. static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  6880. {
  6881. struct r5conf *conf = mddev->private;
  6882. int ret, err = -EEXIST;
  6883. int disk;
  6884. struct disk_info *p;
  6885. int first = 0;
  6886. int last = conf->raid_disks - 1;
  6887. if (test_bit(Journal, &rdev->flags)) {
  6888. if (conf->log)
  6889. return -EBUSY;
  6890. rdev->raid_disk = 0;
  6891. /*
  6892. * The array is in readonly mode if journal is missing, so no
  6893. * write requests running. We should be safe
  6894. */
  6895. ret = log_init(conf, rdev, false);
  6896. if (ret)
  6897. return ret;
  6898. ret = r5l_start(conf->log);
  6899. if (ret)
  6900. return ret;
  6901. return 0;
  6902. }
  6903. if (mddev->recovery_disabled == conf->recovery_disabled)
  6904. return -EBUSY;
  6905. if (rdev->saved_raid_disk < 0 && has_failed(conf))
  6906. /* no point adding a device */
  6907. return -EINVAL;
  6908. if (rdev->raid_disk >= 0)
  6909. first = last = rdev->raid_disk;
  6910. /*
  6911. * find the disk ... but prefer rdev->saved_raid_disk
  6912. * if possible.
  6913. */
  6914. if (rdev->saved_raid_disk >= 0 &&
  6915. rdev->saved_raid_disk >= first &&
  6916. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  6917. first = rdev->saved_raid_disk;
  6918. for (disk = first; disk <= last; disk++) {
  6919. p = conf->disks + disk;
  6920. if (p->rdev == NULL) {
  6921. clear_bit(In_sync, &rdev->flags);
  6922. rdev->raid_disk = disk;
  6923. if (rdev->saved_raid_disk != disk)
  6924. conf->fullsync = 1;
  6925. rcu_assign_pointer(p->rdev, rdev);
  6926. err = log_modify(conf, rdev, true);
  6927. goto out;
  6928. }
  6929. }
  6930. for (disk = first; disk <= last; disk++) {
  6931. p = conf->disks + disk;
  6932. if (test_bit(WantReplacement, &p->rdev->flags) &&
  6933. p->replacement == NULL) {
  6934. clear_bit(In_sync, &rdev->flags);
  6935. set_bit(Replacement, &rdev->flags);
  6936. rdev->raid_disk = disk;
  6937. err = 0;
  6938. conf->fullsync = 1;
  6939. rcu_assign_pointer(p->replacement, rdev);
  6940. break;
  6941. }
  6942. }
  6943. out:
  6944. print_raid5_conf(conf);
  6945. return err;
  6946. }
  6947. static int raid5_resize(struct mddev *mddev, sector_t sectors)
  6948. {
  6949. /* no resync is happening, and there is enough space
  6950. * on all devices, so we can resize.
  6951. * We need to make sure resync covers any new space.
  6952. * If the array is shrinking we should possibly wait until
  6953. * any io in the removed space completes, but it hardly seems
  6954. * worth it.
  6955. */
  6956. sector_t newsize;
  6957. struct r5conf *conf = mddev->private;
  6958. if (raid5_has_log(conf) || raid5_has_ppl(conf))
  6959. return -EINVAL;
  6960. sectors &= ~((sector_t)conf->chunk_sectors - 1);
  6961. newsize = raid5_size(mddev, sectors, mddev->raid_disks);
  6962. if (mddev->external_size &&
  6963. mddev->array_sectors > newsize)
  6964. return -EINVAL;
  6965. if (mddev->bitmap) {
  6966. int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
  6967. if (ret)
  6968. return ret;
  6969. }
  6970. md_set_array_sectors(mddev, newsize);
  6971. if (sectors > mddev->dev_sectors &&
  6972. mddev->recovery_cp > mddev->dev_sectors) {
  6973. mddev->recovery_cp = mddev->dev_sectors;
  6974. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  6975. }
  6976. mddev->dev_sectors = sectors;
  6977. mddev->resync_max_sectors = sectors;
  6978. return 0;
  6979. }
  6980. static int check_stripe_cache(struct mddev *mddev)
  6981. {
  6982. /* Can only proceed if there are plenty of stripe_heads.
  6983. * We need a minimum of one full stripe,, and for sensible progress
  6984. * it is best to have about 4 times that.
  6985. * If we require 4 times, then the default 256 4K stripe_heads will
  6986. * allow for chunk sizes up to 256K, which is probably OK.
  6987. * If the chunk size is greater, user-space should request more
  6988. * stripe_heads first.
  6989. */
  6990. struct r5conf *conf = mddev->private;
  6991. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  6992. > conf->min_nr_stripes ||
  6993. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  6994. > conf->min_nr_stripes) {
  6995. pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  6996. mdname(mddev),
  6997. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  6998. / STRIPE_SIZE)*4);
  6999. return 0;
  7000. }
  7001. return 1;
  7002. }
  7003. static int check_reshape(struct mddev *mddev)
  7004. {
  7005. struct r5conf *conf = mddev->private;
  7006. if (raid5_has_log(conf) || raid5_has_ppl(conf))
  7007. return -EINVAL;
  7008. if (mddev->delta_disks == 0 &&
  7009. mddev->new_layout == mddev->layout &&
  7010. mddev->new_chunk_sectors == mddev->chunk_sectors)
  7011. return 0; /* nothing to do */
  7012. if (has_failed(conf))
  7013. return -EINVAL;
  7014. if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
  7015. /* We might be able to shrink, but the devices must
  7016. * be made bigger first.
  7017. * For raid6, 4 is the minimum size.
  7018. * Otherwise 2 is the minimum
  7019. */
  7020. int min = 2;
  7021. if (mddev->level == 6)
  7022. min = 4;
  7023. if (mddev->raid_disks + mddev->delta_disks < min)
  7024. return -EINVAL;
  7025. }
  7026. if (!check_stripe_cache(mddev))
  7027. return -ENOSPC;
  7028. if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
  7029. mddev->delta_disks > 0)
  7030. if (resize_chunks(conf,
  7031. conf->previous_raid_disks
  7032. + max(0, mddev->delta_disks),
  7033. max(mddev->new_chunk_sectors,
  7034. mddev->chunk_sectors)
  7035. ) < 0)
  7036. return -ENOMEM;
  7037. if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
  7038. return 0; /* never bother to shrink */
  7039. return resize_stripes(conf, (conf->previous_raid_disks
  7040. + mddev->delta_disks));
  7041. }
  7042. static int raid5_start_reshape(struct mddev *mddev)
  7043. {
  7044. struct r5conf *conf = mddev->private;
  7045. struct md_rdev *rdev;
  7046. int spares = 0;
  7047. unsigned long flags;
  7048. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  7049. return -EBUSY;
  7050. if (!check_stripe_cache(mddev))
  7051. return -ENOSPC;
  7052. if (has_failed(conf))
  7053. return -EINVAL;
  7054. rdev_for_each(rdev, mddev) {
  7055. if (!test_bit(In_sync, &rdev->flags)
  7056. && !test_bit(Faulty, &rdev->flags))
  7057. spares++;
  7058. }
  7059. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  7060. /* Not enough devices even to make a degraded array
  7061. * of that size
  7062. */
  7063. return -EINVAL;
  7064. /* Refuse to reduce size of the array. Any reductions in
  7065. * array size must be through explicit setting of array_size
  7066. * attribute.
  7067. */
  7068. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  7069. < mddev->array_sectors) {
  7070. pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
  7071. mdname(mddev));
  7072. return -EINVAL;
  7073. }
  7074. atomic_set(&conf->reshape_stripes, 0);
  7075. spin_lock_irq(&conf->device_lock);
  7076. write_seqcount_begin(&conf->gen_lock);
  7077. conf->previous_raid_disks = conf->raid_disks;
  7078. conf->raid_disks += mddev->delta_disks;
  7079. conf->prev_chunk_sectors = conf->chunk_sectors;
  7080. conf->chunk_sectors = mddev->new_chunk_sectors;
  7081. conf->prev_algo = conf->algorithm;
  7082. conf->algorithm = mddev->new_layout;
  7083. conf->generation++;
  7084. /* Code that selects data_offset needs to see the generation update
  7085. * if reshape_progress has been set - so a memory barrier needed.
  7086. */
  7087. smp_mb();
  7088. if (mddev->reshape_backwards)
  7089. conf->reshape_progress = raid5_size(mddev, 0, 0);
  7090. else
  7091. conf->reshape_progress = 0;
  7092. conf->reshape_safe = conf->reshape_progress;
  7093. write_seqcount_end(&conf->gen_lock);
  7094. spin_unlock_irq(&conf->device_lock);
  7095. /* Now make sure any requests that proceeded on the assumption
  7096. * the reshape wasn't running - like Discard or Read - have
  7097. * completed.
  7098. */
  7099. mddev_suspend(mddev);
  7100. mddev_resume(mddev);
  7101. /* Add some new drives, as many as will fit.
  7102. * We know there are enough to make the newly sized array work.
  7103. * Don't add devices if we are reducing the number of
  7104. * devices in the array. This is because it is not possible
  7105. * to correctly record the "partially reconstructed" state of
  7106. * such devices during the reshape and confusion could result.
  7107. */
  7108. if (mddev->delta_disks >= 0) {
  7109. rdev_for_each(rdev, mddev)
  7110. if (rdev->raid_disk < 0 &&
  7111. !test_bit(Faulty, &rdev->flags)) {
  7112. if (raid5_add_disk(mddev, rdev) == 0) {
  7113. if (rdev->raid_disk
  7114. >= conf->previous_raid_disks)
  7115. set_bit(In_sync, &rdev->flags);
  7116. else
  7117. rdev->recovery_offset = 0;
  7118. if (sysfs_link_rdev(mddev, rdev))
  7119. /* Failure here is OK */;
  7120. }
  7121. } else if (rdev->raid_disk >= conf->previous_raid_disks
  7122. && !test_bit(Faulty, &rdev->flags)) {
  7123. /* This is a spare that was manually added */
  7124. set_bit(In_sync, &rdev->flags);
  7125. }
  7126. /* When a reshape changes the number of devices,
  7127. * ->degraded is measured against the larger of the
  7128. * pre and post number of devices.
  7129. */
  7130. spin_lock_irqsave(&conf->device_lock, flags);
  7131. mddev->degraded = raid5_calc_degraded(conf);
  7132. spin_unlock_irqrestore(&conf->device_lock, flags);
  7133. }
  7134. mddev->raid_disks = conf->raid_disks;
  7135. mddev->reshape_position = conf->reshape_progress;
  7136. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  7137. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  7138. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  7139. clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
  7140. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  7141. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  7142. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  7143. "reshape");
  7144. if (!mddev->sync_thread) {
  7145. mddev->recovery = 0;
  7146. spin_lock_irq(&conf->device_lock);
  7147. write_seqcount_begin(&conf->gen_lock);
  7148. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  7149. mddev->new_chunk_sectors =
  7150. conf->chunk_sectors = conf->prev_chunk_sectors;
  7151. mddev->new_layout = conf->algorithm = conf->prev_algo;
  7152. rdev_for_each(rdev, mddev)
  7153. rdev->new_data_offset = rdev->data_offset;
  7154. smp_wmb();
  7155. conf->generation --;
  7156. conf->reshape_progress = MaxSector;
  7157. mddev->reshape_position = MaxSector;
  7158. write_seqcount_end(&conf->gen_lock);
  7159. spin_unlock_irq(&conf->device_lock);
  7160. return -EAGAIN;
  7161. }
  7162. conf->reshape_checkpoint = jiffies;
  7163. md_wakeup_thread(mddev->sync_thread);
  7164. md_new_event(mddev);
  7165. return 0;
  7166. }
  7167. /* This is called from the reshape thread and should make any
  7168. * changes needed in 'conf'
  7169. */
  7170. static void end_reshape(struct r5conf *conf)
  7171. {
  7172. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  7173. struct md_rdev *rdev;
  7174. spin_lock_irq(&conf->device_lock);
  7175. conf->previous_raid_disks = conf->raid_disks;
  7176. md_finish_reshape(conf->mddev);
  7177. smp_wmb();
  7178. conf->reshape_progress = MaxSector;
  7179. conf->mddev->reshape_position = MaxSector;
  7180. rdev_for_each(rdev, conf->mddev)
  7181. if (rdev->raid_disk >= 0 &&
  7182. !test_bit(Journal, &rdev->flags) &&
  7183. !test_bit(In_sync, &rdev->flags))
  7184. rdev->recovery_offset = MaxSector;
  7185. spin_unlock_irq(&conf->device_lock);
  7186. wake_up(&conf->wait_for_overlap);
  7187. /* read-ahead size must cover two whole stripes, which is
  7188. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  7189. */
  7190. if (conf->mddev->queue) {
  7191. int data_disks = conf->raid_disks - conf->max_degraded;
  7192. int stripe = data_disks * ((conf->chunk_sectors << 9)
  7193. / PAGE_SIZE);
  7194. if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
  7195. conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
  7196. }
  7197. }
  7198. }
  7199. /* This is called from the raid5d thread with mddev_lock held.
  7200. * It makes config changes to the device.
  7201. */
  7202. static void raid5_finish_reshape(struct mddev *mddev)
  7203. {
  7204. struct r5conf *conf = mddev->private;
  7205. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  7206. if (mddev->delta_disks <= 0) {
  7207. int d;
  7208. spin_lock_irq(&conf->device_lock);
  7209. mddev->degraded = raid5_calc_degraded(conf);
  7210. spin_unlock_irq(&conf->device_lock);
  7211. for (d = conf->raid_disks ;
  7212. d < conf->raid_disks - mddev->delta_disks;
  7213. d++) {
  7214. struct md_rdev *rdev = conf->disks[d].rdev;
  7215. if (rdev)
  7216. clear_bit(In_sync, &rdev->flags);
  7217. rdev = conf->disks[d].replacement;
  7218. if (rdev)
  7219. clear_bit(In_sync, &rdev->flags);
  7220. }
  7221. }
  7222. mddev->layout = conf->algorithm;
  7223. mddev->chunk_sectors = conf->chunk_sectors;
  7224. mddev->reshape_position = MaxSector;
  7225. mddev->delta_disks = 0;
  7226. mddev->reshape_backwards = 0;
  7227. }
  7228. }
  7229. static void raid5_quiesce(struct mddev *mddev, int quiesce)
  7230. {
  7231. struct r5conf *conf = mddev->private;
  7232. if (quiesce) {
  7233. /* stop all writes */
  7234. lock_all_device_hash_locks_irq(conf);
  7235. /* '2' tells resync/reshape to pause so that all
  7236. * active stripes can drain
  7237. */
  7238. r5c_flush_cache(conf, INT_MAX);
  7239. conf->quiesce = 2;
  7240. wait_event_cmd(conf->wait_for_quiescent,
  7241. atomic_read(&conf->active_stripes) == 0 &&
  7242. atomic_read(&conf->active_aligned_reads) == 0,
  7243. unlock_all_device_hash_locks_irq(conf),
  7244. lock_all_device_hash_locks_irq(conf));
  7245. conf->quiesce = 1;
  7246. unlock_all_device_hash_locks_irq(conf);
  7247. /* allow reshape to continue */
  7248. wake_up(&conf->wait_for_overlap);
  7249. } else {
  7250. /* re-enable writes */
  7251. lock_all_device_hash_locks_irq(conf);
  7252. conf->quiesce = 0;
  7253. wake_up(&conf->wait_for_quiescent);
  7254. wake_up(&conf->wait_for_overlap);
  7255. unlock_all_device_hash_locks_irq(conf);
  7256. }
  7257. log_quiesce(conf, quiesce);
  7258. }
  7259. static void *raid45_takeover_raid0(struct mddev *mddev, int level)
  7260. {
  7261. struct r0conf *raid0_conf = mddev->private;
  7262. sector_t sectors;
  7263. /* for raid0 takeover only one zone is supported */
  7264. if (raid0_conf->nr_strip_zones > 1) {
  7265. pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  7266. mdname(mddev));
  7267. return ERR_PTR(-EINVAL);
  7268. }
  7269. sectors = raid0_conf->strip_zone[0].zone_end;
  7270. sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
  7271. mddev->dev_sectors = sectors;
  7272. mddev->new_level = level;
  7273. mddev->new_layout = ALGORITHM_PARITY_N;
  7274. mddev->new_chunk_sectors = mddev->chunk_sectors;
  7275. mddev->raid_disks += 1;
  7276. mddev->delta_disks = 1;
  7277. /* make sure it will be not marked as dirty */
  7278. mddev->recovery_cp = MaxSector;
  7279. return setup_conf(mddev);
  7280. }
  7281. static void *raid5_takeover_raid1(struct mddev *mddev)
  7282. {
  7283. int chunksect;
  7284. void *ret;
  7285. if (mddev->raid_disks != 2 ||
  7286. mddev->degraded > 1)
  7287. return ERR_PTR(-EINVAL);
  7288. /* Should check if there are write-behind devices? */
  7289. chunksect = 64*2; /* 64K by default */
  7290. /* The array must be an exact multiple of chunksize */
  7291. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  7292. chunksect >>= 1;
  7293. if ((chunksect<<9) < STRIPE_SIZE)
  7294. /* array size does not allow a suitable chunk size */
  7295. return ERR_PTR(-EINVAL);
  7296. mddev->new_level = 5;
  7297. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  7298. mddev->new_chunk_sectors = chunksect;
  7299. ret = setup_conf(mddev);
  7300. if (!IS_ERR(ret))
  7301. mddev_clear_unsupported_flags(mddev,
  7302. UNSUPPORTED_MDDEV_FLAGS);
  7303. return ret;
  7304. }
  7305. static void *raid5_takeover_raid6(struct mddev *mddev)
  7306. {
  7307. int new_layout;
  7308. switch (mddev->layout) {
  7309. case ALGORITHM_LEFT_ASYMMETRIC_6:
  7310. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  7311. break;
  7312. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  7313. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  7314. break;
  7315. case ALGORITHM_LEFT_SYMMETRIC_6:
  7316. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  7317. break;
  7318. case ALGORITHM_RIGHT_SYMMETRIC_6:
  7319. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  7320. break;
  7321. case ALGORITHM_PARITY_0_6:
  7322. new_layout = ALGORITHM_PARITY_0;
  7323. break;
  7324. case ALGORITHM_PARITY_N:
  7325. new_layout = ALGORITHM_PARITY_N;
  7326. break;
  7327. default:
  7328. return ERR_PTR(-EINVAL);
  7329. }
  7330. mddev->new_level = 5;
  7331. mddev->new_layout = new_layout;
  7332. mddev->delta_disks = -1;
  7333. mddev->raid_disks -= 1;
  7334. return setup_conf(mddev);
  7335. }
  7336. static int raid5_check_reshape(struct mddev *mddev)
  7337. {
  7338. /* For a 2-drive array, the layout and chunk size can be changed
  7339. * immediately as not restriping is needed.
  7340. * For larger arrays we record the new value - after validation
  7341. * to be used by a reshape pass.
  7342. */
  7343. struct r5conf *conf = mddev->private;
  7344. int new_chunk = mddev->new_chunk_sectors;
  7345. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  7346. return -EINVAL;
  7347. if (new_chunk > 0) {
  7348. if (!is_power_of_2(new_chunk))
  7349. return -EINVAL;
  7350. if (new_chunk < (PAGE_SIZE>>9))
  7351. return -EINVAL;
  7352. if (mddev->array_sectors & (new_chunk-1))
  7353. /* not factor of array size */
  7354. return -EINVAL;
  7355. }
  7356. /* They look valid */
  7357. if (mddev->raid_disks == 2) {
  7358. /* can make the change immediately */
  7359. if (mddev->new_layout >= 0) {
  7360. conf->algorithm = mddev->new_layout;
  7361. mddev->layout = mddev->new_layout;
  7362. }
  7363. if (new_chunk > 0) {
  7364. conf->chunk_sectors = new_chunk ;
  7365. mddev->chunk_sectors = new_chunk;
  7366. }
  7367. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  7368. md_wakeup_thread(mddev->thread);
  7369. }
  7370. return check_reshape(mddev);
  7371. }
  7372. static int raid6_check_reshape(struct mddev *mddev)
  7373. {
  7374. int new_chunk = mddev->new_chunk_sectors;
  7375. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  7376. return -EINVAL;
  7377. if (new_chunk > 0) {
  7378. if (!is_power_of_2(new_chunk))
  7379. return -EINVAL;
  7380. if (new_chunk < (PAGE_SIZE >> 9))
  7381. return -EINVAL;
  7382. if (mddev->array_sectors & (new_chunk-1))
  7383. /* not factor of array size */
  7384. return -EINVAL;
  7385. }
  7386. /* They look valid */
  7387. return check_reshape(mddev);
  7388. }
  7389. static void *raid5_takeover(struct mddev *mddev)
  7390. {
  7391. /* raid5 can take over:
  7392. * raid0 - if there is only one strip zone - make it a raid4 layout
  7393. * raid1 - if there are two drives. We need to know the chunk size
  7394. * raid4 - trivial - just use a raid4 layout.
  7395. * raid6 - Providing it is a *_6 layout
  7396. */
  7397. if (mddev->level == 0)
  7398. return raid45_takeover_raid0(mddev, 5);
  7399. if (mddev->level == 1)
  7400. return raid5_takeover_raid1(mddev);
  7401. if (mddev->level == 4) {
  7402. mddev->new_layout = ALGORITHM_PARITY_N;
  7403. mddev->new_level = 5;
  7404. return setup_conf(mddev);
  7405. }
  7406. if (mddev->level == 6)
  7407. return raid5_takeover_raid6(mddev);
  7408. return ERR_PTR(-EINVAL);
  7409. }
  7410. static void *raid4_takeover(struct mddev *mddev)
  7411. {
  7412. /* raid4 can take over:
  7413. * raid0 - if there is only one strip zone
  7414. * raid5 - if layout is right
  7415. */
  7416. if (mddev->level == 0)
  7417. return raid45_takeover_raid0(mddev, 4);
  7418. if (mddev->level == 5 &&
  7419. mddev->layout == ALGORITHM_PARITY_N) {
  7420. mddev->new_layout = 0;
  7421. mddev->new_level = 4;
  7422. return setup_conf(mddev);
  7423. }
  7424. return ERR_PTR(-EINVAL);
  7425. }
  7426. static struct md_personality raid5_personality;
  7427. static void *raid6_takeover(struct mddev *mddev)
  7428. {
  7429. /* Currently can only take over a raid5. We map the
  7430. * personality to an equivalent raid6 personality
  7431. * with the Q block at the end.
  7432. */
  7433. int new_layout;
  7434. if (mddev->pers != &raid5_personality)
  7435. return ERR_PTR(-EINVAL);
  7436. if (mddev->degraded > 1)
  7437. return ERR_PTR(-EINVAL);
  7438. if (mddev->raid_disks > 253)
  7439. return ERR_PTR(-EINVAL);
  7440. if (mddev->raid_disks < 3)
  7441. return ERR_PTR(-EINVAL);
  7442. switch (mddev->layout) {
  7443. case ALGORITHM_LEFT_ASYMMETRIC:
  7444. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  7445. break;
  7446. case ALGORITHM_RIGHT_ASYMMETRIC:
  7447. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  7448. break;
  7449. case ALGORITHM_LEFT_SYMMETRIC:
  7450. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  7451. break;
  7452. case ALGORITHM_RIGHT_SYMMETRIC:
  7453. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  7454. break;
  7455. case ALGORITHM_PARITY_0:
  7456. new_layout = ALGORITHM_PARITY_0_6;
  7457. break;
  7458. case ALGORITHM_PARITY_N:
  7459. new_layout = ALGORITHM_PARITY_N;
  7460. break;
  7461. default:
  7462. return ERR_PTR(-EINVAL);
  7463. }
  7464. mddev->new_level = 6;
  7465. mddev->new_layout = new_layout;
  7466. mddev->delta_disks = 1;
  7467. mddev->raid_disks += 1;
  7468. return setup_conf(mddev);
  7469. }
  7470. static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
  7471. {
  7472. struct r5conf *conf;
  7473. int err;
  7474. err = mddev_lock(mddev);
  7475. if (err)
  7476. return err;
  7477. conf = mddev->private;
  7478. if (!conf) {
  7479. mddev_unlock(mddev);
  7480. return -ENODEV;
  7481. }
  7482. if (strncmp(buf, "ppl", 3) == 0) {
  7483. /* ppl only works with RAID 5 */
  7484. if (!raid5_has_ppl(conf) && conf->level == 5) {
  7485. err = log_init(conf, NULL, true);
  7486. if (!err) {
  7487. err = resize_stripes(conf, conf->pool_size);
  7488. if (err)
  7489. log_exit(conf);
  7490. }
  7491. } else
  7492. err = -EINVAL;
  7493. } else if (strncmp(buf, "resync", 6) == 0) {
  7494. if (raid5_has_ppl(conf)) {
  7495. mddev_suspend(mddev);
  7496. log_exit(conf);
  7497. mddev_resume(mddev);
  7498. err = resize_stripes(conf, conf->pool_size);
  7499. } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
  7500. r5l_log_disk_error(conf)) {
  7501. bool journal_dev_exists = false;
  7502. struct md_rdev *rdev;
  7503. rdev_for_each(rdev, mddev)
  7504. if (test_bit(Journal, &rdev->flags)) {
  7505. journal_dev_exists = true;
  7506. break;
  7507. }
  7508. if (!journal_dev_exists) {
  7509. mddev_suspend(mddev);
  7510. clear_bit(MD_HAS_JOURNAL, &mddev->flags);
  7511. mddev_resume(mddev);
  7512. } else /* need remove journal device first */
  7513. err = -EBUSY;
  7514. } else
  7515. err = -EINVAL;
  7516. } else {
  7517. err = -EINVAL;
  7518. }
  7519. if (!err)
  7520. md_update_sb(mddev, 1);
  7521. mddev_unlock(mddev);
  7522. return err;
  7523. }
  7524. static int raid5_start(struct mddev *mddev)
  7525. {
  7526. struct r5conf *conf = mddev->private;
  7527. return r5l_start(conf->log);
  7528. }
  7529. static struct md_personality raid6_personality =
  7530. {
  7531. .name = "raid6",
  7532. .level = 6,
  7533. .owner = THIS_MODULE,
  7534. .make_request = raid5_make_request,
  7535. .run = raid5_run,
  7536. .start = raid5_start,
  7537. .free = raid5_free,
  7538. .status = raid5_status,
  7539. .error_handler = raid5_error,
  7540. .hot_add_disk = raid5_add_disk,
  7541. .hot_remove_disk= raid5_remove_disk,
  7542. .spare_active = raid5_spare_active,
  7543. .sync_request = raid5_sync_request,
  7544. .resize = raid5_resize,
  7545. .size = raid5_size,
  7546. .check_reshape = raid6_check_reshape,
  7547. .start_reshape = raid5_start_reshape,
  7548. .finish_reshape = raid5_finish_reshape,
  7549. .quiesce = raid5_quiesce,
  7550. .takeover = raid6_takeover,
  7551. .congested = raid5_congested,
  7552. .change_consistency_policy = raid5_change_consistency_policy,
  7553. };
  7554. static struct md_personality raid5_personality =
  7555. {
  7556. .name = "raid5",
  7557. .level = 5,
  7558. .owner = THIS_MODULE,
  7559. .make_request = raid5_make_request,
  7560. .run = raid5_run,
  7561. .start = raid5_start,
  7562. .free = raid5_free,
  7563. .status = raid5_status,
  7564. .error_handler = raid5_error,
  7565. .hot_add_disk = raid5_add_disk,
  7566. .hot_remove_disk= raid5_remove_disk,
  7567. .spare_active = raid5_spare_active,
  7568. .sync_request = raid5_sync_request,
  7569. .resize = raid5_resize,
  7570. .size = raid5_size,
  7571. .check_reshape = raid5_check_reshape,
  7572. .start_reshape = raid5_start_reshape,
  7573. .finish_reshape = raid5_finish_reshape,
  7574. .quiesce = raid5_quiesce,
  7575. .takeover = raid5_takeover,
  7576. .congested = raid5_congested,
  7577. .change_consistency_policy = raid5_change_consistency_policy,
  7578. };
  7579. static struct md_personality raid4_personality =
  7580. {
  7581. .name = "raid4",
  7582. .level = 4,
  7583. .owner = THIS_MODULE,
  7584. .make_request = raid5_make_request,
  7585. .run = raid5_run,
  7586. .start = raid5_start,
  7587. .free = raid5_free,
  7588. .status = raid5_status,
  7589. .error_handler = raid5_error,
  7590. .hot_add_disk = raid5_add_disk,
  7591. .hot_remove_disk= raid5_remove_disk,
  7592. .spare_active = raid5_spare_active,
  7593. .sync_request = raid5_sync_request,
  7594. .resize = raid5_resize,
  7595. .size = raid5_size,
  7596. .check_reshape = raid5_check_reshape,
  7597. .start_reshape = raid5_start_reshape,
  7598. .finish_reshape = raid5_finish_reshape,
  7599. .quiesce = raid5_quiesce,
  7600. .takeover = raid4_takeover,
  7601. .congested = raid5_congested,
  7602. .change_consistency_policy = raid5_change_consistency_policy,
  7603. };
  7604. static int __init raid5_init(void)
  7605. {
  7606. int ret;
  7607. raid5_wq = alloc_workqueue("raid5wq",
  7608. WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
  7609. if (!raid5_wq)
  7610. return -ENOMEM;
  7611. ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
  7612. "md/raid5:prepare",
  7613. raid456_cpu_up_prepare,
  7614. raid456_cpu_dead);
  7615. if (ret) {
  7616. destroy_workqueue(raid5_wq);
  7617. return ret;
  7618. }
  7619. register_md_personality(&raid6_personality);
  7620. register_md_personality(&raid5_personality);
  7621. register_md_personality(&raid4_personality);
  7622. return 0;
  7623. }
  7624. static void raid5_exit(void)
  7625. {
  7626. unregister_md_personality(&raid6_personality);
  7627. unregister_md_personality(&raid5_personality);
  7628. unregister_md_personality(&raid4_personality);
  7629. cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
  7630. destroy_workqueue(raid5_wq);
  7631. }
  7632. module_init(raid5_init);
  7633. module_exit(raid5_exit);
  7634. MODULE_LICENSE("GPL");
  7635. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  7636. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  7637. MODULE_ALIAS("md-raid5");
  7638. MODULE_ALIAS("md-raid4");
  7639. MODULE_ALIAS("md-level-5");
  7640. MODULE_ALIAS("md-level-4");
  7641. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  7642. MODULE_ALIAS("md-raid6");
  7643. MODULE_ALIAS("md-level-6");
  7644. /* This used to be two separate modules, they were: */
  7645. MODULE_ALIAS("raid5");
  7646. MODULE_ALIAS("raid6");