raid5.c 238 KB

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