raid5.c 187 KB

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