raid5.c 240 KB

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