raid5.c 180 KB

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