raid5.c 190 KB

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