raid5.c 225 KB

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