raid5.c 221 KB

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