raid5.c 238 KB

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