raid5.c 229 KB

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