raid5.c 190 KB

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