raid5.c 219 KB

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