raid5.c 239 KB

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