raid5.c 239 KB

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