raid5.c 190 KB

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