raid5.c 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441
  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. s->to_write--;
  2225. bitmap_end = 1;
  2226. }
  2227. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2228. wake_up(&conf->wait_for_overlap);
  2229. while (bi && bi->bi_sector <
  2230. sh->dev[i].sector + STRIPE_SECTORS) {
  2231. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  2232. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2233. if (!raid5_dec_bi_active_stripes(bi)) {
  2234. md_write_end(conf->mddev);
  2235. bi->bi_next = *return_bi;
  2236. *return_bi = bi;
  2237. }
  2238. bi = nextbi;
  2239. }
  2240. if (bitmap_end)
  2241. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2242. STRIPE_SECTORS, 0, 0);
  2243. bitmap_end = 0;
  2244. /* and fail all 'written' */
  2245. bi = sh->dev[i].written;
  2246. sh->dev[i].written = NULL;
  2247. if (bi) bitmap_end = 1;
  2248. while (bi && bi->bi_sector <
  2249. sh->dev[i].sector + STRIPE_SECTORS) {
  2250. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  2251. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2252. if (!raid5_dec_bi_active_stripes(bi)) {
  2253. md_write_end(conf->mddev);
  2254. bi->bi_next = *return_bi;
  2255. *return_bi = bi;
  2256. }
  2257. bi = bi2;
  2258. }
  2259. /* fail any reads if this device is non-operational and
  2260. * the data has not reached the cache yet.
  2261. */
  2262. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  2263. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  2264. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  2265. spin_lock_irq(&sh->stripe_lock);
  2266. bi = sh->dev[i].toread;
  2267. sh->dev[i].toread = NULL;
  2268. spin_unlock_irq(&sh->stripe_lock);
  2269. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2270. wake_up(&conf->wait_for_overlap);
  2271. if (bi) s->to_read--;
  2272. while (bi && bi->bi_sector <
  2273. sh->dev[i].sector + STRIPE_SECTORS) {
  2274. struct bio *nextbi =
  2275. r5_next_bio(bi, sh->dev[i].sector);
  2276. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2277. if (!raid5_dec_bi_active_stripes(bi)) {
  2278. bi->bi_next = *return_bi;
  2279. *return_bi = bi;
  2280. }
  2281. bi = nextbi;
  2282. }
  2283. }
  2284. if (bitmap_end)
  2285. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2286. STRIPE_SECTORS, 0, 0);
  2287. /* If we were in the middle of a write the parity block might
  2288. * still be locked - so just clear all R5_LOCKED flags
  2289. */
  2290. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2291. }
  2292. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2293. if (atomic_dec_and_test(&conf->pending_full_writes))
  2294. md_wakeup_thread(conf->mddev->thread);
  2295. }
  2296. static void
  2297. handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
  2298. struct stripe_head_state *s)
  2299. {
  2300. int abort = 0;
  2301. int i;
  2302. clear_bit(STRIPE_SYNCING, &sh->state);
  2303. s->syncing = 0;
  2304. s->replacing = 0;
  2305. /* There is nothing more to do for sync/check/repair.
  2306. * Don't even need to abort as that is handled elsewhere
  2307. * if needed, and not always wanted e.g. if there is a known
  2308. * bad block here.
  2309. * For recover/replace we need to record a bad block on all
  2310. * non-sync devices, or abort the recovery
  2311. */
  2312. if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
  2313. /* During recovery devices cannot be removed, so
  2314. * locking and refcounting of rdevs is not needed
  2315. */
  2316. for (i = 0; i < conf->raid_disks; i++) {
  2317. struct md_rdev *rdev = conf->disks[i].rdev;
  2318. if (rdev
  2319. && !test_bit(Faulty, &rdev->flags)
  2320. && !test_bit(In_sync, &rdev->flags)
  2321. && !rdev_set_badblocks(rdev, sh->sector,
  2322. STRIPE_SECTORS, 0))
  2323. abort = 1;
  2324. rdev = conf->disks[i].replacement;
  2325. if (rdev
  2326. && !test_bit(Faulty, &rdev->flags)
  2327. && !test_bit(In_sync, &rdev->flags)
  2328. && !rdev_set_badblocks(rdev, sh->sector,
  2329. STRIPE_SECTORS, 0))
  2330. abort = 1;
  2331. }
  2332. if (abort)
  2333. conf->recovery_disabled =
  2334. conf->mddev->recovery_disabled;
  2335. }
  2336. md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
  2337. }
  2338. static int want_replace(struct stripe_head *sh, int disk_idx)
  2339. {
  2340. struct md_rdev *rdev;
  2341. int rv = 0;
  2342. /* Doing recovery so rcu locking not required */
  2343. rdev = sh->raid_conf->disks[disk_idx].replacement;
  2344. if (rdev
  2345. && !test_bit(Faulty, &rdev->flags)
  2346. && !test_bit(In_sync, &rdev->flags)
  2347. && (rdev->recovery_offset <= sh->sector
  2348. || rdev->mddev->recovery_cp <= sh->sector))
  2349. rv = 1;
  2350. return rv;
  2351. }
  2352. /* fetch_block - checks the given member device to see if its data needs
  2353. * to be read or computed to satisfy a request.
  2354. *
  2355. * Returns 1 when no more member devices need to be checked, otherwise returns
  2356. * 0 to tell the loop in handle_stripe_fill to continue
  2357. */
  2358. static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
  2359. int disk_idx, int disks)
  2360. {
  2361. struct r5dev *dev = &sh->dev[disk_idx];
  2362. struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
  2363. &sh->dev[s->failed_num[1]] };
  2364. /* is the data in this block needed, and can we get it? */
  2365. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2366. !test_bit(R5_UPTODATE, &dev->flags) &&
  2367. (dev->toread ||
  2368. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2369. s->syncing || s->expanding ||
  2370. (s->replacing && want_replace(sh, disk_idx)) ||
  2371. (s->failed >= 1 && fdev[0]->toread) ||
  2372. (s->failed >= 2 && fdev[1]->toread) ||
  2373. (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
  2374. !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
  2375. (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
  2376. /* we would like to get this block, possibly by computing it,
  2377. * otherwise read it if the backing disk is insync
  2378. */
  2379. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  2380. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  2381. if ((s->uptodate == disks - 1) &&
  2382. (s->failed && (disk_idx == s->failed_num[0] ||
  2383. disk_idx == s->failed_num[1]))) {
  2384. /* have disk failed, and we're requested to fetch it;
  2385. * do compute it
  2386. */
  2387. pr_debug("Computing stripe %llu block %d\n",
  2388. (unsigned long long)sh->sector, disk_idx);
  2389. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2390. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2391. set_bit(R5_Wantcompute, &dev->flags);
  2392. sh->ops.target = disk_idx;
  2393. sh->ops.target2 = -1; /* no 2nd target */
  2394. s->req_compute = 1;
  2395. /* Careful: from this point on 'uptodate' is in the eye
  2396. * of raid_run_ops which services 'compute' operations
  2397. * before writes. R5_Wantcompute flags a block that will
  2398. * be R5_UPTODATE by the time it is needed for a
  2399. * subsequent operation.
  2400. */
  2401. s->uptodate++;
  2402. return 1;
  2403. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  2404. /* Computing 2-failure is *very* expensive; only
  2405. * do it if failed >= 2
  2406. */
  2407. int other;
  2408. for (other = disks; other--; ) {
  2409. if (other == disk_idx)
  2410. continue;
  2411. if (!test_bit(R5_UPTODATE,
  2412. &sh->dev[other].flags))
  2413. break;
  2414. }
  2415. BUG_ON(other < 0);
  2416. pr_debug("Computing stripe %llu blocks %d,%d\n",
  2417. (unsigned long long)sh->sector,
  2418. disk_idx, other);
  2419. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2420. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2421. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  2422. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  2423. sh->ops.target = disk_idx;
  2424. sh->ops.target2 = other;
  2425. s->uptodate += 2;
  2426. s->req_compute = 1;
  2427. return 1;
  2428. } else if (test_bit(R5_Insync, &dev->flags)) {
  2429. set_bit(R5_LOCKED, &dev->flags);
  2430. set_bit(R5_Wantread, &dev->flags);
  2431. s->locked++;
  2432. pr_debug("Reading block %d (sync=%d)\n",
  2433. disk_idx, s->syncing);
  2434. }
  2435. }
  2436. return 0;
  2437. }
  2438. /**
  2439. * handle_stripe_fill - read or compute data to satisfy pending requests.
  2440. */
  2441. static void handle_stripe_fill(struct stripe_head *sh,
  2442. struct stripe_head_state *s,
  2443. int disks)
  2444. {
  2445. int i;
  2446. /* look for blocks to read/compute, skip this if a compute
  2447. * is already in flight, or if the stripe contents are in the
  2448. * midst of changing due to a write
  2449. */
  2450. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2451. !sh->reconstruct_state)
  2452. for (i = disks; i--; )
  2453. if (fetch_block(sh, s, i, disks))
  2454. break;
  2455. set_bit(STRIPE_HANDLE, &sh->state);
  2456. }
  2457. /* handle_stripe_clean_event
  2458. * any written block on an uptodate or failed drive can be returned.
  2459. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  2460. * never LOCKED, so we don't need to test 'failed' directly.
  2461. */
  2462. static void handle_stripe_clean_event(struct r5conf *conf,
  2463. struct stripe_head *sh, int disks, struct bio **return_bi)
  2464. {
  2465. int i;
  2466. struct r5dev *dev;
  2467. for (i = disks; i--; )
  2468. if (sh->dev[i].written) {
  2469. dev = &sh->dev[i];
  2470. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2471. (test_bit(R5_UPTODATE, &dev->flags) ||
  2472. test_and_clear_bit(R5_Discard, &dev->flags))) {
  2473. /* We can return any write requests */
  2474. struct bio *wbi, *wbi2;
  2475. pr_debug("Return write for disc %d\n", i);
  2476. wbi = dev->written;
  2477. dev->written = NULL;
  2478. while (wbi && wbi->bi_sector <
  2479. dev->sector + STRIPE_SECTORS) {
  2480. wbi2 = r5_next_bio(wbi, dev->sector);
  2481. if (!raid5_dec_bi_active_stripes(wbi)) {
  2482. md_write_end(conf->mddev);
  2483. wbi->bi_next = *return_bi;
  2484. *return_bi = wbi;
  2485. }
  2486. wbi = wbi2;
  2487. }
  2488. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2489. STRIPE_SECTORS,
  2490. !test_bit(STRIPE_DEGRADED, &sh->state),
  2491. 0);
  2492. }
  2493. }
  2494. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2495. if (atomic_dec_and_test(&conf->pending_full_writes))
  2496. md_wakeup_thread(conf->mddev->thread);
  2497. }
  2498. static void handle_stripe_dirtying(struct r5conf *conf,
  2499. struct stripe_head *sh,
  2500. struct stripe_head_state *s,
  2501. int disks)
  2502. {
  2503. int rmw = 0, rcw = 0, i;
  2504. sector_t recovery_cp = conf->mddev->recovery_cp;
  2505. /* RAID6 requires 'rcw' in current implementation.
  2506. * Otherwise, check whether resync is now happening or should start.
  2507. * If yes, then the array is dirty (after unclean shutdown or
  2508. * initial creation), so parity in some stripes might be inconsistent.
  2509. * In this case, we need to always do reconstruct-write, to ensure
  2510. * that in case of drive failure or read-error correction, we
  2511. * generate correct data from the parity.
  2512. */
  2513. if (conf->max_degraded == 2 ||
  2514. (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
  2515. /* Calculate the real rcw later - for now make it
  2516. * look like rcw is cheaper
  2517. */
  2518. rcw = 1; rmw = 2;
  2519. pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
  2520. conf->max_degraded, (unsigned long long)recovery_cp,
  2521. (unsigned long long)sh->sector);
  2522. } else for (i = disks; i--; ) {
  2523. /* would I have to read this buffer for read_modify_write */
  2524. struct r5dev *dev = &sh->dev[i];
  2525. if ((dev->towrite || i == sh->pd_idx) &&
  2526. !test_bit(R5_LOCKED, &dev->flags) &&
  2527. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2528. test_bit(R5_Wantcompute, &dev->flags))) {
  2529. if (test_bit(R5_Insync, &dev->flags))
  2530. rmw++;
  2531. else
  2532. rmw += 2*disks; /* cannot read it */
  2533. }
  2534. /* Would I have to read this buffer for reconstruct_write */
  2535. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2536. !test_bit(R5_LOCKED, &dev->flags) &&
  2537. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2538. test_bit(R5_Wantcompute, &dev->flags))) {
  2539. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2540. else
  2541. rcw += 2*disks;
  2542. }
  2543. }
  2544. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2545. (unsigned long long)sh->sector, rmw, rcw);
  2546. set_bit(STRIPE_HANDLE, &sh->state);
  2547. if (rmw < rcw && rmw > 0)
  2548. /* prefer read-modify-write, but need to get some data */
  2549. for (i = disks; i--; ) {
  2550. struct r5dev *dev = &sh->dev[i];
  2551. if ((dev->towrite || i == sh->pd_idx) &&
  2552. !test_bit(R5_LOCKED, &dev->flags) &&
  2553. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2554. test_bit(R5_Wantcompute, &dev->flags)) &&
  2555. test_bit(R5_Insync, &dev->flags)) {
  2556. if (
  2557. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2558. pr_debug("Read_old block "
  2559. "%d for r-m-w\n", i);
  2560. set_bit(R5_LOCKED, &dev->flags);
  2561. set_bit(R5_Wantread, &dev->flags);
  2562. s->locked++;
  2563. } else {
  2564. set_bit(STRIPE_DELAYED, &sh->state);
  2565. set_bit(STRIPE_HANDLE, &sh->state);
  2566. }
  2567. }
  2568. }
  2569. if (rcw <= rmw && rcw > 0) {
  2570. /* want reconstruct write, but need to get some data */
  2571. rcw = 0;
  2572. for (i = disks; i--; ) {
  2573. struct r5dev *dev = &sh->dev[i];
  2574. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2575. i != sh->pd_idx && i != sh->qd_idx &&
  2576. !test_bit(R5_LOCKED, &dev->flags) &&
  2577. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2578. test_bit(R5_Wantcompute, &dev->flags))) {
  2579. rcw++;
  2580. if (!test_bit(R5_Insync, &dev->flags))
  2581. continue; /* it's a failed drive */
  2582. if (
  2583. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2584. pr_debug("Read_old block "
  2585. "%d for Reconstruct\n", i);
  2586. set_bit(R5_LOCKED, &dev->flags);
  2587. set_bit(R5_Wantread, &dev->flags);
  2588. s->locked++;
  2589. } else {
  2590. set_bit(STRIPE_DELAYED, &sh->state);
  2591. set_bit(STRIPE_HANDLE, &sh->state);
  2592. }
  2593. }
  2594. }
  2595. }
  2596. /* now if nothing is locked, and if we have enough data,
  2597. * we can start a write request
  2598. */
  2599. /* since handle_stripe can be called at any time we need to handle the
  2600. * case where a compute block operation has been submitted and then a
  2601. * subsequent call wants to start a write request. raid_run_ops only
  2602. * handles the case where compute block and reconstruct are requested
  2603. * simultaneously. If this is not the case then new writes need to be
  2604. * held off until the compute completes.
  2605. */
  2606. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2607. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2608. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2609. schedule_reconstruction(sh, s, rcw == 0, 0);
  2610. }
  2611. static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
  2612. struct stripe_head_state *s, int disks)
  2613. {
  2614. struct r5dev *dev = NULL;
  2615. set_bit(STRIPE_HANDLE, &sh->state);
  2616. switch (sh->check_state) {
  2617. case check_state_idle:
  2618. /* start a new check operation if there are no failures */
  2619. if (s->failed == 0) {
  2620. BUG_ON(s->uptodate != disks);
  2621. sh->check_state = check_state_run;
  2622. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2623. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2624. s->uptodate--;
  2625. break;
  2626. }
  2627. dev = &sh->dev[s->failed_num[0]];
  2628. /* fall through */
  2629. case check_state_compute_result:
  2630. sh->check_state = check_state_idle;
  2631. if (!dev)
  2632. dev = &sh->dev[sh->pd_idx];
  2633. /* check that a write has not made the stripe insync */
  2634. if (test_bit(STRIPE_INSYNC, &sh->state))
  2635. break;
  2636. /* either failed parity check, or recovery is happening */
  2637. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2638. BUG_ON(s->uptodate != disks);
  2639. set_bit(R5_LOCKED, &dev->flags);
  2640. s->locked++;
  2641. set_bit(R5_Wantwrite, &dev->flags);
  2642. clear_bit(STRIPE_DEGRADED, &sh->state);
  2643. set_bit(STRIPE_INSYNC, &sh->state);
  2644. break;
  2645. case check_state_run:
  2646. break; /* we will be called again upon completion */
  2647. case check_state_check_result:
  2648. sh->check_state = check_state_idle;
  2649. /* if a failure occurred during the check operation, leave
  2650. * STRIPE_INSYNC not set and let the stripe be handled again
  2651. */
  2652. if (s->failed)
  2653. break;
  2654. /* handle a successful check operation, if parity is correct
  2655. * we are done. Otherwise update the mismatch count and repair
  2656. * parity if !MD_RECOVERY_CHECK
  2657. */
  2658. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  2659. /* parity is correct (on disc,
  2660. * not in buffer any more)
  2661. */
  2662. set_bit(STRIPE_INSYNC, &sh->state);
  2663. else {
  2664. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2665. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2666. /* don't try to repair!! */
  2667. set_bit(STRIPE_INSYNC, &sh->state);
  2668. else {
  2669. sh->check_state = check_state_compute_run;
  2670. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2671. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2672. set_bit(R5_Wantcompute,
  2673. &sh->dev[sh->pd_idx].flags);
  2674. sh->ops.target = sh->pd_idx;
  2675. sh->ops.target2 = -1;
  2676. s->uptodate++;
  2677. }
  2678. }
  2679. break;
  2680. case check_state_compute_run:
  2681. break;
  2682. default:
  2683. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2684. __func__, sh->check_state,
  2685. (unsigned long long) sh->sector);
  2686. BUG();
  2687. }
  2688. }
  2689. static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
  2690. struct stripe_head_state *s,
  2691. int disks)
  2692. {
  2693. int pd_idx = sh->pd_idx;
  2694. int qd_idx = sh->qd_idx;
  2695. struct r5dev *dev;
  2696. set_bit(STRIPE_HANDLE, &sh->state);
  2697. BUG_ON(s->failed > 2);
  2698. /* Want to check and possibly repair P and Q.
  2699. * However there could be one 'failed' device, in which
  2700. * case we can only check one of them, possibly using the
  2701. * other to generate missing data
  2702. */
  2703. switch (sh->check_state) {
  2704. case check_state_idle:
  2705. /* start a new check operation if there are < 2 failures */
  2706. if (s->failed == s->q_failed) {
  2707. /* The only possible failed device holds Q, so it
  2708. * makes sense to check P (If anything else were failed,
  2709. * we would have used P to recreate it).
  2710. */
  2711. sh->check_state = check_state_run;
  2712. }
  2713. if (!s->q_failed && s->failed < 2) {
  2714. /* Q is not failed, and we didn't use it to generate
  2715. * anything, so it makes sense to check it
  2716. */
  2717. if (sh->check_state == check_state_run)
  2718. sh->check_state = check_state_run_pq;
  2719. else
  2720. sh->check_state = check_state_run_q;
  2721. }
  2722. /* discard potentially stale zero_sum_result */
  2723. sh->ops.zero_sum_result = 0;
  2724. if (sh->check_state == check_state_run) {
  2725. /* async_xor_zero_sum destroys the contents of P */
  2726. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2727. s->uptodate--;
  2728. }
  2729. if (sh->check_state >= check_state_run &&
  2730. sh->check_state <= check_state_run_pq) {
  2731. /* async_syndrome_zero_sum preserves P and Q, so
  2732. * no need to mark them !uptodate here
  2733. */
  2734. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2735. break;
  2736. }
  2737. /* we have 2-disk failure */
  2738. BUG_ON(s->failed != 2);
  2739. /* fall through */
  2740. case check_state_compute_result:
  2741. sh->check_state = check_state_idle;
  2742. /* check that a write has not made the stripe insync */
  2743. if (test_bit(STRIPE_INSYNC, &sh->state))
  2744. break;
  2745. /* now write out any block on a failed drive,
  2746. * or P or Q if they were recomputed
  2747. */
  2748. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  2749. if (s->failed == 2) {
  2750. dev = &sh->dev[s->failed_num[1]];
  2751. s->locked++;
  2752. set_bit(R5_LOCKED, &dev->flags);
  2753. set_bit(R5_Wantwrite, &dev->flags);
  2754. }
  2755. if (s->failed >= 1) {
  2756. dev = &sh->dev[s->failed_num[0]];
  2757. s->locked++;
  2758. set_bit(R5_LOCKED, &dev->flags);
  2759. set_bit(R5_Wantwrite, &dev->flags);
  2760. }
  2761. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2762. dev = &sh->dev[pd_idx];
  2763. s->locked++;
  2764. set_bit(R5_LOCKED, &dev->flags);
  2765. set_bit(R5_Wantwrite, &dev->flags);
  2766. }
  2767. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2768. dev = &sh->dev[qd_idx];
  2769. s->locked++;
  2770. set_bit(R5_LOCKED, &dev->flags);
  2771. set_bit(R5_Wantwrite, &dev->flags);
  2772. }
  2773. clear_bit(STRIPE_DEGRADED, &sh->state);
  2774. set_bit(STRIPE_INSYNC, &sh->state);
  2775. break;
  2776. case check_state_run:
  2777. case check_state_run_q:
  2778. case check_state_run_pq:
  2779. break; /* we will be called again upon completion */
  2780. case check_state_check_result:
  2781. sh->check_state = check_state_idle;
  2782. /* handle a successful check operation, if parity is correct
  2783. * we are done. Otherwise update the mismatch count and repair
  2784. * parity if !MD_RECOVERY_CHECK
  2785. */
  2786. if (sh->ops.zero_sum_result == 0) {
  2787. /* both parities are correct */
  2788. if (!s->failed)
  2789. set_bit(STRIPE_INSYNC, &sh->state);
  2790. else {
  2791. /* in contrast to the raid5 case we can validate
  2792. * parity, but still have a failure to write
  2793. * back
  2794. */
  2795. sh->check_state = check_state_compute_result;
  2796. /* Returning at this point means that we may go
  2797. * off and bring p and/or q uptodate again so
  2798. * we make sure to check zero_sum_result again
  2799. * to verify if p or q need writeback
  2800. */
  2801. }
  2802. } else {
  2803. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2804. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2805. /* don't try to repair!! */
  2806. set_bit(STRIPE_INSYNC, &sh->state);
  2807. else {
  2808. int *target = &sh->ops.target;
  2809. sh->ops.target = -1;
  2810. sh->ops.target2 = -1;
  2811. sh->check_state = check_state_compute_run;
  2812. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2813. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2814. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2815. set_bit(R5_Wantcompute,
  2816. &sh->dev[pd_idx].flags);
  2817. *target = pd_idx;
  2818. target = &sh->ops.target2;
  2819. s->uptodate++;
  2820. }
  2821. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2822. set_bit(R5_Wantcompute,
  2823. &sh->dev[qd_idx].flags);
  2824. *target = qd_idx;
  2825. s->uptodate++;
  2826. }
  2827. }
  2828. }
  2829. break;
  2830. case check_state_compute_run:
  2831. break;
  2832. default:
  2833. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2834. __func__, sh->check_state,
  2835. (unsigned long long) sh->sector);
  2836. BUG();
  2837. }
  2838. }
  2839. static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
  2840. {
  2841. int i;
  2842. /* We have read all the blocks in this stripe and now we need to
  2843. * copy some of them into a target stripe for expand.
  2844. */
  2845. struct dma_async_tx_descriptor *tx = NULL;
  2846. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2847. for (i = 0; i < sh->disks; i++)
  2848. if (i != sh->pd_idx && i != sh->qd_idx) {
  2849. int dd_idx, j;
  2850. struct stripe_head *sh2;
  2851. struct async_submit_ctl submit;
  2852. sector_t bn = compute_blocknr(sh, i, 1);
  2853. sector_t s = raid5_compute_sector(conf, bn, 0,
  2854. &dd_idx, NULL);
  2855. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2856. if (sh2 == NULL)
  2857. /* so far only the early blocks of this stripe
  2858. * have been requested. When later blocks
  2859. * get requested, we will try again
  2860. */
  2861. continue;
  2862. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2863. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2864. /* must have already done this block */
  2865. release_stripe(sh2);
  2866. continue;
  2867. }
  2868. /* place all the copies on one channel */
  2869. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  2870. tx = async_memcpy(sh2->dev[dd_idx].page,
  2871. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2872. &submit);
  2873. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2874. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2875. for (j = 0; j < conf->raid_disks; j++)
  2876. if (j != sh2->pd_idx &&
  2877. j != sh2->qd_idx &&
  2878. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2879. break;
  2880. if (j == conf->raid_disks) {
  2881. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2882. set_bit(STRIPE_HANDLE, &sh2->state);
  2883. }
  2884. release_stripe(sh2);
  2885. }
  2886. /* done submitting copies, wait for them to complete */
  2887. if (tx) {
  2888. async_tx_ack(tx);
  2889. dma_wait_for_async_tx(tx);
  2890. }
  2891. }
  2892. /*
  2893. * handle_stripe - do things to a stripe.
  2894. *
  2895. * We lock the stripe by setting STRIPE_ACTIVE and then examine the
  2896. * state of various bits to see what needs to be done.
  2897. * Possible results:
  2898. * return some read requests which now have data
  2899. * return some write requests which are safely on storage
  2900. * schedule a read on some buffers
  2901. * schedule a write of some buffers
  2902. * return confirmation of parity correctness
  2903. *
  2904. */
  2905. static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
  2906. {
  2907. struct r5conf *conf = sh->raid_conf;
  2908. int disks = sh->disks;
  2909. struct r5dev *dev;
  2910. int i;
  2911. int do_recovery = 0;
  2912. memset(s, 0, sizeof(*s));
  2913. s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2914. s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2915. s->failed_num[0] = -1;
  2916. s->failed_num[1] = -1;
  2917. /* Now to look around and see what can be done */
  2918. rcu_read_lock();
  2919. for (i=disks; i--; ) {
  2920. struct md_rdev *rdev;
  2921. sector_t first_bad;
  2922. int bad_sectors;
  2923. int is_bad = 0;
  2924. dev = &sh->dev[i];
  2925. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2926. i, dev->flags,
  2927. dev->toread, dev->towrite, dev->written);
  2928. /* maybe we can reply to a read
  2929. *
  2930. * new wantfill requests are only permitted while
  2931. * ops_complete_biofill is guaranteed to be inactive
  2932. */
  2933. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2934. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2935. set_bit(R5_Wantfill, &dev->flags);
  2936. /* now count some things */
  2937. if (test_bit(R5_LOCKED, &dev->flags))
  2938. s->locked++;
  2939. if (test_bit(R5_UPTODATE, &dev->flags))
  2940. s->uptodate++;
  2941. if (test_bit(R5_Wantcompute, &dev->flags)) {
  2942. s->compute++;
  2943. BUG_ON(s->compute > 2);
  2944. }
  2945. if (test_bit(R5_Wantfill, &dev->flags))
  2946. s->to_fill++;
  2947. else if (dev->toread)
  2948. s->to_read++;
  2949. if (dev->towrite) {
  2950. s->to_write++;
  2951. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2952. s->non_overwrite++;
  2953. }
  2954. if (dev->written)
  2955. s->written++;
  2956. /* Prefer to use the replacement for reads, but only
  2957. * if it is recovered enough and has no bad blocks.
  2958. */
  2959. rdev = rcu_dereference(conf->disks[i].replacement);
  2960. if (rdev && !test_bit(Faulty, &rdev->flags) &&
  2961. rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
  2962. !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  2963. &first_bad, &bad_sectors))
  2964. set_bit(R5_ReadRepl, &dev->flags);
  2965. else {
  2966. if (rdev)
  2967. set_bit(R5_NeedReplace, &dev->flags);
  2968. rdev = rcu_dereference(conf->disks[i].rdev);
  2969. clear_bit(R5_ReadRepl, &dev->flags);
  2970. }
  2971. if (rdev && test_bit(Faulty, &rdev->flags))
  2972. rdev = NULL;
  2973. if (rdev) {
  2974. is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  2975. &first_bad, &bad_sectors);
  2976. if (s->blocked_rdev == NULL
  2977. && (test_bit(Blocked, &rdev->flags)
  2978. || is_bad < 0)) {
  2979. if (is_bad < 0)
  2980. set_bit(BlockedBadBlocks,
  2981. &rdev->flags);
  2982. s->blocked_rdev = rdev;
  2983. atomic_inc(&rdev->nr_pending);
  2984. }
  2985. }
  2986. clear_bit(R5_Insync, &dev->flags);
  2987. if (!rdev)
  2988. /* Not in-sync */;
  2989. else if (is_bad) {
  2990. /* also not in-sync */
  2991. if (!test_bit(WriteErrorSeen, &rdev->flags) &&
  2992. test_bit(R5_UPTODATE, &dev->flags)) {
  2993. /* treat as in-sync, but with a read error
  2994. * which we can now try to correct
  2995. */
  2996. set_bit(R5_Insync, &dev->flags);
  2997. set_bit(R5_ReadError, &dev->flags);
  2998. }
  2999. } else if (test_bit(In_sync, &rdev->flags))
  3000. set_bit(R5_Insync, &dev->flags);
  3001. else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  3002. /* in sync if before recovery_offset */
  3003. set_bit(R5_Insync, &dev->flags);
  3004. else if (test_bit(R5_UPTODATE, &dev->flags) &&
  3005. test_bit(R5_Expanded, &dev->flags))
  3006. /* If we've reshaped into here, we assume it is Insync.
  3007. * We will shortly update recovery_offset to make
  3008. * it official.
  3009. */
  3010. set_bit(R5_Insync, &dev->flags);
  3011. if (rdev && test_bit(R5_WriteError, &dev->flags)) {
  3012. /* This flag does not apply to '.replacement'
  3013. * only to .rdev, so make sure to check that*/
  3014. struct md_rdev *rdev2 = rcu_dereference(
  3015. conf->disks[i].rdev);
  3016. if (rdev2 == rdev)
  3017. clear_bit(R5_Insync, &dev->flags);
  3018. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3019. s->handle_bad_blocks = 1;
  3020. atomic_inc(&rdev2->nr_pending);
  3021. } else
  3022. clear_bit(R5_WriteError, &dev->flags);
  3023. }
  3024. if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
  3025. /* This flag does not apply to '.replacement'
  3026. * only to .rdev, so make sure to check that*/
  3027. struct md_rdev *rdev2 = rcu_dereference(
  3028. conf->disks[i].rdev);
  3029. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3030. s->handle_bad_blocks = 1;
  3031. atomic_inc(&rdev2->nr_pending);
  3032. } else
  3033. clear_bit(R5_MadeGood, &dev->flags);
  3034. }
  3035. if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
  3036. struct md_rdev *rdev2 = rcu_dereference(
  3037. conf->disks[i].replacement);
  3038. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3039. s->handle_bad_blocks = 1;
  3040. atomic_inc(&rdev2->nr_pending);
  3041. } else
  3042. clear_bit(R5_MadeGoodRepl, &dev->flags);
  3043. }
  3044. if (!test_bit(R5_Insync, &dev->flags)) {
  3045. /* The ReadError flag will just be confusing now */
  3046. clear_bit(R5_ReadError, &dev->flags);
  3047. clear_bit(R5_ReWrite, &dev->flags);
  3048. }
  3049. if (test_bit(R5_ReadError, &dev->flags))
  3050. clear_bit(R5_Insync, &dev->flags);
  3051. if (!test_bit(R5_Insync, &dev->flags)) {
  3052. if (s->failed < 2)
  3053. s->failed_num[s->failed] = i;
  3054. s->failed++;
  3055. if (rdev && !test_bit(Faulty, &rdev->flags))
  3056. do_recovery = 1;
  3057. }
  3058. }
  3059. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  3060. /* If there is a failed device being replaced,
  3061. * we must be recovering.
  3062. * else if we are after recovery_cp, we must be syncing
  3063. * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
  3064. * else we can only be replacing
  3065. * sync and recovery both need to read all devices, and so
  3066. * use the same flag.
  3067. */
  3068. if (do_recovery ||
  3069. sh->sector >= conf->mddev->recovery_cp ||
  3070. test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
  3071. s->syncing = 1;
  3072. else
  3073. s->replacing = 1;
  3074. }
  3075. rcu_read_unlock();
  3076. }
  3077. static void handle_stripe(struct stripe_head *sh)
  3078. {
  3079. struct stripe_head_state s;
  3080. struct r5conf *conf = sh->raid_conf;
  3081. int i;
  3082. int prexor;
  3083. int disks = sh->disks;
  3084. struct r5dev *pdev, *qdev;
  3085. clear_bit(STRIPE_HANDLE, &sh->state);
  3086. if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
  3087. /* already being handled, ensure it gets handled
  3088. * again when current action finishes */
  3089. set_bit(STRIPE_HANDLE, &sh->state);
  3090. return;
  3091. }
  3092. if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
  3093. set_bit(STRIPE_SYNCING, &sh->state);
  3094. clear_bit(STRIPE_INSYNC, &sh->state);
  3095. }
  3096. clear_bit(STRIPE_DELAYED, &sh->state);
  3097. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  3098. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  3099. (unsigned long long)sh->sector, sh->state,
  3100. atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
  3101. sh->check_state, sh->reconstruct_state);
  3102. analyse_stripe(sh, &s);
  3103. if (s.handle_bad_blocks) {
  3104. set_bit(STRIPE_HANDLE, &sh->state);
  3105. goto finish;
  3106. }
  3107. if (unlikely(s.blocked_rdev)) {
  3108. if (s.syncing || s.expanding || s.expanded ||
  3109. s.replacing || s.to_write || s.written) {
  3110. set_bit(STRIPE_HANDLE, &sh->state);
  3111. goto finish;
  3112. }
  3113. /* There is nothing for the blocked_rdev to block */
  3114. rdev_dec_pending(s.blocked_rdev, conf->mddev);
  3115. s.blocked_rdev = NULL;
  3116. }
  3117. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  3118. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  3119. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  3120. }
  3121. pr_debug("locked=%d uptodate=%d to_read=%d"
  3122. " to_write=%d failed=%d failed_num=%d,%d\n",
  3123. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  3124. s.failed_num[0], s.failed_num[1]);
  3125. /* check if the array has lost more than max_degraded devices and,
  3126. * if so, some requests might need to be failed.
  3127. */
  3128. if (s.failed > conf->max_degraded) {
  3129. sh->check_state = 0;
  3130. sh->reconstruct_state = 0;
  3131. if (s.to_read+s.to_write+s.written)
  3132. handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
  3133. if (s.syncing + s.replacing)
  3134. handle_failed_sync(conf, sh, &s);
  3135. }
  3136. /*
  3137. * might be able to return some write requests if the parity blocks
  3138. * are safe, or on a failed drive
  3139. */
  3140. pdev = &sh->dev[sh->pd_idx];
  3141. s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
  3142. || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
  3143. qdev = &sh->dev[sh->qd_idx];
  3144. s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
  3145. || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
  3146. || conf->level < 6;
  3147. if (s.written &&
  3148. (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  3149. && !test_bit(R5_LOCKED, &pdev->flags)
  3150. && (test_bit(R5_UPTODATE, &pdev->flags) ||
  3151. test_bit(R5_Discard, &pdev->flags))))) &&
  3152. (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  3153. && !test_bit(R5_LOCKED, &qdev->flags)
  3154. && (test_bit(R5_UPTODATE, &qdev->flags) ||
  3155. test_bit(R5_Discard, &qdev->flags))))))
  3156. handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
  3157. /* Now we might consider reading some blocks, either to check/generate
  3158. * parity, or to satisfy requests
  3159. * or to load a block that is being partially written.
  3160. */
  3161. if (s.to_read || s.non_overwrite
  3162. || (conf->level == 6 && s.to_write && s.failed)
  3163. || (s.syncing && (s.uptodate + s.compute < disks))
  3164. || s.replacing
  3165. || s.expanding)
  3166. handle_stripe_fill(sh, &s, disks);
  3167. /* Now we check to see if any write operations have recently
  3168. * completed
  3169. */
  3170. prexor = 0;
  3171. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  3172. prexor = 1;
  3173. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  3174. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  3175. sh->reconstruct_state = reconstruct_state_idle;
  3176. /* All the 'written' buffers and the parity block are ready to
  3177. * be written back to disk
  3178. */
  3179. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
  3180. !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
  3181. BUG_ON(sh->qd_idx >= 0 &&
  3182. !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
  3183. !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
  3184. for (i = disks; i--; ) {
  3185. struct r5dev *dev = &sh->dev[i];
  3186. if (test_bit(R5_LOCKED, &dev->flags) &&
  3187. (i == sh->pd_idx || i == sh->qd_idx ||
  3188. dev->written)) {
  3189. pr_debug("Writing block %d\n", i);
  3190. set_bit(R5_Wantwrite, &dev->flags);
  3191. if (prexor)
  3192. continue;
  3193. if (!test_bit(R5_Insync, &dev->flags) ||
  3194. ((i == sh->pd_idx || i == sh->qd_idx) &&
  3195. s.failed == 0))
  3196. set_bit(STRIPE_INSYNC, &sh->state);
  3197. }
  3198. }
  3199. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3200. s.dec_preread_active = 1;
  3201. }
  3202. /* Now to consider new write requests and what else, if anything
  3203. * should be read. We do not handle new writes when:
  3204. * 1/ A 'write' operation (copy+xor) is already in flight.
  3205. * 2/ A 'check' operation is in flight, as it may clobber the parity
  3206. * block.
  3207. */
  3208. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  3209. handle_stripe_dirtying(conf, sh, &s, disks);
  3210. /* maybe we need to check and possibly fix the parity for this stripe
  3211. * Any reads will already have been scheduled, so we just see if enough
  3212. * data is available. The parity check is held off while parity
  3213. * dependent operations are in flight.
  3214. */
  3215. if (sh->check_state ||
  3216. (s.syncing && s.locked == 0 &&
  3217. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3218. !test_bit(STRIPE_INSYNC, &sh->state))) {
  3219. if (conf->level == 6)
  3220. handle_parity_checks6(conf, sh, &s, disks);
  3221. else
  3222. handle_parity_checks5(conf, sh, &s, disks);
  3223. }
  3224. if (s.replacing && s.locked == 0
  3225. && !test_bit(STRIPE_INSYNC, &sh->state)) {
  3226. /* Write out to replacement devices where possible */
  3227. for (i = 0; i < conf->raid_disks; i++)
  3228. if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
  3229. test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
  3230. set_bit(R5_WantReplace, &sh->dev[i].flags);
  3231. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3232. s.locked++;
  3233. }
  3234. set_bit(STRIPE_INSYNC, &sh->state);
  3235. }
  3236. if ((s.syncing || s.replacing) && s.locked == 0 &&
  3237. test_bit(STRIPE_INSYNC, &sh->state)) {
  3238. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3239. clear_bit(STRIPE_SYNCING, &sh->state);
  3240. }
  3241. /* If the failed drives are just a ReadError, then we might need
  3242. * to progress the repair/check process
  3243. */
  3244. if (s.failed <= conf->max_degraded && !conf->mddev->ro)
  3245. for (i = 0; i < s.failed; i++) {
  3246. struct r5dev *dev = &sh->dev[s.failed_num[i]];
  3247. if (test_bit(R5_ReadError, &dev->flags)
  3248. && !test_bit(R5_LOCKED, &dev->flags)
  3249. && test_bit(R5_UPTODATE, &dev->flags)
  3250. ) {
  3251. if (!test_bit(R5_ReWrite, &dev->flags)) {
  3252. set_bit(R5_Wantwrite, &dev->flags);
  3253. set_bit(R5_ReWrite, &dev->flags);
  3254. set_bit(R5_LOCKED, &dev->flags);
  3255. s.locked++;
  3256. } else {
  3257. /* let's read it back */
  3258. set_bit(R5_Wantread, &dev->flags);
  3259. set_bit(R5_LOCKED, &dev->flags);
  3260. s.locked++;
  3261. }
  3262. }
  3263. }
  3264. /* Finish reconstruct operations initiated by the expansion process */
  3265. if (sh->reconstruct_state == reconstruct_state_result) {
  3266. struct stripe_head *sh_src
  3267. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  3268. if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
  3269. /* sh cannot be written until sh_src has been read.
  3270. * so arrange for sh to be delayed a little
  3271. */
  3272. set_bit(STRIPE_DELAYED, &sh->state);
  3273. set_bit(STRIPE_HANDLE, &sh->state);
  3274. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  3275. &sh_src->state))
  3276. atomic_inc(&conf->preread_active_stripes);
  3277. release_stripe(sh_src);
  3278. goto finish;
  3279. }
  3280. if (sh_src)
  3281. release_stripe(sh_src);
  3282. sh->reconstruct_state = reconstruct_state_idle;
  3283. clear_bit(STRIPE_EXPANDING, &sh->state);
  3284. for (i = conf->raid_disks; i--; ) {
  3285. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  3286. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3287. s.locked++;
  3288. }
  3289. }
  3290. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  3291. !sh->reconstruct_state) {
  3292. /* Need to write out all blocks after computing parity */
  3293. sh->disks = conf->raid_disks;
  3294. stripe_set_idx(sh->sector, conf, 0, sh);
  3295. schedule_reconstruction(sh, &s, 1, 1);
  3296. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  3297. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  3298. atomic_dec(&conf->reshape_stripes);
  3299. wake_up(&conf->wait_for_overlap);
  3300. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3301. }
  3302. if (s.expanding && s.locked == 0 &&
  3303. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  3304. handle_stripe_expansion(conf, sh);
  3305. finish:
  3306. /* wait for this device to become unblocked */
  3307. if (unlikely(s.blocked_rdev)) {
  3308. if (conf->mddev->external)
  3309. md_wait_for_blocked_rdev(s.blocked_rdev,
  3310. conf->mddev);
  3311. else
  3312. /* Internal metadata will immediately
  3313. * be written by raid5d, so we don't
  3314. * need to wait here.
  3315. */
  3316. rdev_dec_pending(s.blocked_rdev,
  3317. conf->mddev);
  3318. }
  3319. if (s.handle_bad_blocks)
  3320. for (i = disks; i--; ) {
  3321. struct md_rdev *rdev;
  3322. struct r5dev *dev = &sh->dev[i];
  3323. if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
  3324. /* We own a safe reference to the rdev */
  3325. rdev = conf->disks[i].rdev;
  3326. if (!rdev_set_badblocks(rdev, sh->sector,
  3327. STRIPE_SECTORS, 0))
  3328. md_error(conf->mddev, rdev);
  3329. rdev_dec_pending(rdev, conf->mddev);
  3330. }
  3331. if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
  3332. rdev = conf->disks[i].rdev;
  3333. rdev_clear_badblocks(rdev, sh->sector,
  3334. STRIPE_SECTORS, 0);
  3335. rdev_dec_pending(rdev, conf->mddev);
  3336. }
  3337. if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
  3338. rdev = conf->disks[i].replacement;
  3339. if (!rdev)
  3340. /* rdev have been moved down */
  3341. rdev = conf->disks[i].rdev;
  3342. rdev_clear_badblocks(rdev, sh->sector,
  3343. STRIPE_SECTORS, 0);
  3344. rdev_dec_pending(rdev, conf->mddev);
  3345. }
  3346. }
  3347. if (s.ops_request)
  3348. raid_run_ops(sh, s.ops_request);
  3349. ops_run_io(sh, &s);
  3350. if (s.dec_preread_active) {
  3351. /* We delay this until after ops_run_io so that if make_request
  3352. * is waiting on a flush, it won't continue until the writes
  3353. * have actually been submitted.
  3354. */
  3355. atomic_dec(&conf->preread_active_stripes);
  3356. if (atomic_read(&conf->preread_active_stripes) <
  3357. IO_THRESHOLD)
  3358. md_wakeup_thread(conf->mddev->thread);
  3359. }
  3360. return_io(s.return_bi);
  3361. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  3362. }
  3363. static void raid5_activate_delayed(struct r5conf *conf)
  3364. {
  3365. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  3366. while (!list_empty(&conf->delayed_list)) {
  3367. struct list_head *l = conf->delayed_list.next;
  3368. struct stripe_head *sh;
  3369. sh = list_entry(l, struct stripe_head, lru);
  3370. list_del_init(l);
  3371. clear_bit(STRIPE_DELAYED, &sh->state);
  3372. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3373. atomic_inc(&conf->preread_active_stripes);
  3374. list_add_tail(&sh->lru, &conf->hold_list);
  3375. }
  3376. }
  3377. }
  3378. static void activate_bit_delay(struct r5conf *conf)
  3379. {
  3380. /* device_lock is held */
  3381. struct list_head head;
  3382. list_add(&head, &conf->bitmap_list);
  3383. list_del_init(&conf->bitmap_list);
  3384. while (!list_empty(&head)) {
  3385. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  3386. list_del_init(&sh->lru);
  3387. atomic_inc(&sh->count);
  3388. __release_stripe(conf, sh);
  3389. }
  3390. }
  3391. int md_raid5_congested(struct mddev *mddev, int bits)
  3392. {
  3393. struct r5conf *conf = mddev->private;
  3394. /* No difference between reads and writes. Just check
  3395. * how busy the stripe_cache is
  3396. */
  3397. if (conf->inactive_blocked)
  3398. return 1;
  3399. if (conf->quiesce)
  3400. return 1;
  3401. if (list_empty_careful(&conf->inactive_list))
  3402. return 1;
  3403. return 0;
  3404. }
  3405. EXPORT_SYMBOL_GPL(md_raid5_congested);
  3406. static int raid5_congested(void *data, int bits)
  3407. {
  3408. struct mddev *mddev = data;
  3409. return mddev_congested(mddev, bits) ||
  3410. md_raid5_congested(mddev, bits);
  3411. }
  3412. /* We want read requests to align with chunks where possible,
  3413. * but write requests don't need to.
  3414. */
  3415. static int raid5_mergeable_bvec(struct request_queue *q,
  3416. struct bvec_merge_data *bvm,
  3417. struct bio_vec *biovec)
  3418. {
  3419. struct mddev *mddev = q->queuedata;
  3420. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  3421. int max;
  3422. unsigned int chunk_sectors = mddev->chunk_sectors;
  3423. unsigned int bio_sectors = bvm->bi_size >> 9;
  3424. if ((bvm->bi_rw & 1) == WRITE)
  3425. return biovec->bv_len; /* always allow writes to be mergeable */
  3426. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3427. chunk_sectors = mddev->new_chunk_sectors;
  3428. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  3429. if (max < 0) max = 0;
  3430. if (max <= biovec->bv_len && bio_sectors == 0)
  3431. return biovec->bv_len;
  3432. else
  3433. return max;
  3434. }
  3435. static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
  3436. {
  3437. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  3438. unsigned int chunk_sectors = mddev->chunk_sectors;
  3439. unsigned int bio_sectors = bio->bi_size >> 9;
  3440. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3441. chunk_sectors = mddev->new_chunk_sectors;
  3442. return chunk_sectors >=
  3443. ((sector & (chunk_sectors - 1)) + bio_sectors);
  3444. }
  3445. /*
  3446. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  3447. * later sampled by raid5d.
  3448. */
  3449. static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
  3450. {
  3451. unsigned long flags;
  3452. spin_lock_irqsave(&conf->device_lock, flags);
  3453. bi->bi_next = conf->retry_read_aligned_list;
  3454. conf->retry_read_aligned_list = bi;
  3455. spin_unlock_irqrestore(&conf->device_lock, flags);
  3456. md_wakeup_thread(conf->mddev->thread);
  3457. }
  3458. static struct bio *remove_bio_from_retry(struct r5conf *conf)
  3459. {
  3460. struct bio *bi;
  3461. bi = conf->retry_read_aligned;
  3462. if (bi) {
  3463. conf->retry_read_aligned = NULL;
  3464. return bi;
  3465. }
  3466. bi = conf->retry_read_aligned_list;
  3467. if(bi) {
  3468. conf->retry_read_aligned_list = bi->bi_next;
  3469. bi->bi_next = NULL;
  3470. /*
  3471. * this sets the active strip count to 1 and the processed
  3472. * strip count to zero (upper 8 bits)
  3473. */
  3474. raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
  3475. }
  3476. return bi;
  3477. }
  3478. /*
  3479. * The "raid5_align_endio" should check if the read succeeded and if it
  3480. * did, call bio_endio on the original bio (having bio_put the new bio
  3481. * first).
  3482. * If the read failed..
  3483. */
  3484. static void raid5_align_endio(struct bio *bi, int error)
  3485. {
  3486. struct bio* raid_bi = bi->bi_private;
  3487. struct mddev *mddev;
  3488. struct r5conf *conf;
  3489. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3490. struct md_rdev *rdev;
  3491. bio_put(bi);
  3492. rdev = (void*)raid_bi->bi_next;
  3493. raid_bi->bi_next = NULL;
  3494. mddev = rdev->mddev;
  3495. conf = mddev->private;
  3496. rdev_dec_pending(rdev, conf->mddev);
  3497. if (!error && uptodate) {
  3498. bio_endio(raid_bi, 0);
  3499. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3500. wake_up(&conf->wait_for_stripe);
  3501. return;
  3502. }
  3503. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3504. add_bio_to_retry(raid_bi, conf);
  3505. }
  3506. static int bio_fits_rdev(struct bio *bi)
  3507. {
  3508. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3509. if ((bi->bi_size>>9) > queue_max_sectors(q))
  3510. return 0;
  3511. blk_recount_segments(q, bi);
  3512. if (bi->bi_phys_segments > queue_max_segments(q))
  3513. return 0;
  3514. if (q->merge_bvec_fn)
  3515. /* it's too hard to apply the merge_bvec_fn at this stage,
  3516. * just just give up
  3517. */
  3518. return 0;
  3519. return 1;
  3520. }
  3521. static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
  3522. {
  3523. struct r5conf *conf = mddev->private;
  3524. int dd_idx;
  3525. struct bio* align_bi;
  3526. struct md_rdev *rdev;
  3527. sector_t end_sector;
  3528. if (!in_chunk_boundary(mddev, raid_bio)) {
  3529. pr_debug("chunk_aligned_read : non aligned\n");
  3530. return 0;
  3531. }
  3532. /*
  3533. * use bio_clone_mddev to make a copy of the bio
  3534. */
  3535. align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
  3536. if (!align_bi)
  3537. return 0;
  3538. /*
  3539. * set bi_end_io to a new function, and set bi_private to the
  3540. * original bio.
  3541. */
  3542. align_bi->bi_end_io = raid5_align_endio;
  3543. align_bi->bi_private = raid_bio;
  3544. /*
  3545. * compute position
  3546. */
  3547. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3548. 0,
  3549. &dd_idx, NULL);
  3550. end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
  3551. rcu_read_lock();
  3552. rdev = rcu_dereference(conf->disks[dd_idx].replacement);
  3553. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  3554. rdev->recovery_offset < end_sector) {
  3555. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3556. if (rdev &&
  3557. (test_bit(Faulty, &rdev->flags) ||
  3558. !(test_bit(In_sync, &rdev->flags) ||
  3559. rdev->recovery_offset >= end_sector)))
  3560. rdev = NULL;
  3561. }
  3562. if (rdev) {
  3563. sector_t first_bad;
  3564. int bad_sectors;
  3565. atomic_inc(&rdev->nr_pending);
  3566. rcu_read_unlock();
  3567. raid_bio->bi_next = (void*)rdev;
  3568. align_bi->bi_bdev = rdev->bdev;
  3569. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3570. if (!bio_fits_rdev(align_bi) ||
  3571. is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
  3572. &first_bad, &bad_sectors)) {
  3573. /* too big in some way, or has a known bad block */
  3574. bio_put(align_bi);
  3575. rdev_dec_pending(rdev, mddev);
  3576. return 0;
  3577. }
  3578. /* No reshape active, so we can trust rdev->data_offset */
  3579. align_bi->bi_sector += rdev->data_offset;
  3580. spin_lock_irq(&conf->device_lock);
  3581. wait_event_lock_irq(conf->wait_for_stripe,
  3582. conf->quiesce == 0,
  3583. conf->device_lock, /* nothing */);
  3584. atomic_inc(&conf->active_aligned_reads);
  3585. spin_unlock_irq(&conf->device_lock);
  3586. generic_make_request(align_bi);
  3587. return 1;
  3588. } else {
  3589. rcu_read_unlock();
  3590. bio_put(align_bi);
  3591. return 0;
  3592. }
  3593. }
  3594. /* __get_priority_stripe - get the next stripe to process
  3595. *
  3596. * Full stripe writes are allowed to pass preread active stripes up until
  3597. * the bypass_threshold is exceeded. In general the bypass_count
  3598. * increments when the handle_list is handled before the hold_list; however, it
  3599. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3600. * stripe with in flight i/o. The bypass_count will be reset when the
  3601. * head of the hold_list has changed, i.e. the head was promoted to the
  3602. * handle_list.
  3603. */
  3604. static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
  3605. {
  3606. struct stripe_head *sh;
  3607. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3608. __func__,
  3609. list_empty(&conf->handle_list) ? "empty" : "busy",
  3610. list_empty(&conf->hold_list) ? "empty" : "busy",
  3611. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3612. if (!list_empty(&conf->handle_list)) {
  3613. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3614. if (list_empty(&conf->hold_list))
  3615. conf->bypass_count = 0;
  3616. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3617. if (conf->hold_list.next == conf->last_hold)
  3618. conf->bypass_count++;
  3619. else {
  3620. conf->last_hold = conf->hold_list.next;
  3621. conf->bypass_count -= conf->bypass_threshold;
  3622. if (conf->bypass_count < 0)
  3623. conf->bypass_count = 0;
  3624. }
  3625. }
  3626. } else if (!list_empty(&conf->hold_list) &&
  3627. ((conf->bypass_threshold &&
  3628. conf->bypass_count > conf->bypass_threshold) ||
  3629. atomic_read(&conf->pending_full_writes) == 0)) {
  3630. sh = list_entry(conf->hold_list.next,
  3631. typeof(*sh), lru);
  3632. conf->bypass_count -= conf->bypass_threshold;
  3633. if (conf->bypass_count < 0)
  3634. conf->bypass_count = 0;
  3635. } else
  3636. return NULL;
  3637. list_del_init(&sh->lru);
  3638. atomic_inc(&sh->count);
  3639. BUG_ON(atomic_read(&sh->count) != 1);
  3640. return sh;
  3641. }
  3642. struct raid5_plug_cb {
  3643. struct blk_plug_cb cb;
  3644. struct list_head list;
  3645. };
  3646. static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
  3647. {
  3648. struct raid5_plug_cb *cb = container_of(
  3649. blk_cb, struct raid5_plug_cb, cb);
  3650. struct stripe_head *sh;
  3651. struct mddev *mddev = cb->cb.data;
  3652. struct r5conf *conf = mddev->private;
  3653. if (cb->list.next && !list_empty(&cb->list)) {
  3654. spin_lock_irq(&conf->device_lock);
  3655. while (!list_empty(&cb->list)) {
  3656. sh = list_first_entry(&cb->list, struct stripe_head, lru);
  3657. list_del_init(&sh->lru);
  3658. /*
  3659. * avoid race release_stripe_plug() sees
  3660. * STRIPE_ON_UNPLUG_LIST clear but the stripe
  3661. * is still in our list
  3662. */
  3663. smp_mb__before_clear_bit();
  3664. clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
  3665. __release_stripe(conf, sh);
  3666. }
  3667. spin_unlock_irq(&conf->device_lock);
  3668. }
  3669. kfree(cb);
  3670. }
  3671. static void release_stripe_plug(struct mddev *mddev,
  3672. struct stripe_head *sh)
  3673. {
  3674. struct blk_plug_cb *blk_cb = blk_check_plugged(
  3675. raid5_unplug, mddev,
  3676. sizeof(struct raid5_plug_cb));
  3677. struct raid5_plug_cb *cb;
  3678. if (!blk_cb) {
  3679. release_stripe(sh);
  3680. return;
  3681. }
  3682. cb = container_of(blk_cb, struct raid5_plug_cb, cb);
  3683. if (cb->list.next == NULL)
  3684. INIT_LIST_HEAD(&cb->list);
  3685. if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
  3686. list_add_tail(&sh->lru, &cb->list);
  3687. else
  3688. release_stripe(sh);
  3689. }
  3690. static void make_discard_request(struct mddev *mddev, struct bio *bi)
  3691. {
  3692. struct r5conf *conf = mddev->private;
  3693. sector_t logical_sector, last_sector;
  3694. struct stripe_head *sh;
  3695. int remaining;
  3696. int stripe_sectors;
  3697. if (mddev->reshape_position != MaxSector)
  3698. /* Skip discard while reshape is happening */
  3699. return;
  3700. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3701. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3702. bi->bi_next = NULL;
  3703. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3704. stripe_sectors = conf->chunk_sectors *
  3705. (conf->raid_disks - conf->max_degraded);
  3706. logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
  3707. stripe_sectors);
  3708. sector_div(last_sector, stripe_sectors);
  3709. logical_sector *= conf->chunk_sectors;
  3710. last_sector *= conf->chunk_sectors;
  3711. for (; logical_sector < last_sector;
  3712. logical_sector += STRIPE_SECTORS) {
  3713. DEFINE_WAIT(w);
  3714. int d;
  3715. again:
  3716. sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
  3717. prepare_to_wait(&conf->wait_for_overlap, &w,
  3718. TASK_UNINTERRUPTIBLE);
  3719. spin_lock_irq(&sh->stripe_lock);
  3720. for (d = 0; d < conf->raid_disks; d++) {
  3721. if (d == sh->pd_idx || d == sh->qd_idx)
  3722. continue;
  3723. if (sh->dev[d].towrite || sh->dev[d].toread) {
  3724. set_bit(R5_Overlap, &sh->dev[d].flags);
  3725. spin_unlock_irq(&sh->stripe_lock);
  3726. release_stripe(sh);
  3727. schedule();
  3728. goto again;
  3729. }
  3730. }
  3731. finish_wait(&conf->wait_for_overlap, &w);
  3732. for (d = 0; d < conf->raid_disks; d++) {
  3733. if (d == sh->pd_idx || d == sh->qd_idx)
  3734. continue;
  3735. sh->dev[d].towrite = bi;
  3736. set_bit(R5_OVERWRITE, &sh->dev[d].flags);
  3737. raid5_inc_bi_active_stripes(bi);
  3738. }
  3739. spin_unlock_irq(&sh->stripe_lock);
  3740. if (conf->mddev->bitmap) {
  3741. for (d = 0;
  3742. d < conf->raid_disks - conf->max_degraded;
  3743. d++)
  3744. bitmap_startwrite(mddev->bitmap,
  3745. sh->sector,
  3746. STRIPE_SECTORS,
  3747. 0);
  3748. sh->bm_seq = conf->seq_flush + 1;
  3749. set_bit(STRIPE_BIT_DELAY, &sh->state);
  3750. }
  3751. set_bit(STRIPE_HANDLE, &sh->state);
  3752. clear_bit(STRIPE_DELAYED, &sh->state);
  3753. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3754. atomic_inc(&conf->preread_active_stripes);
  3755. release_stripe_plug(mddev, sh);
  3756. }
  3757. remaining = raid5_dec_bi_active_stripes(bi);
  3758. if (remaining == 0) {
  3759. md_write_end(mddev);
  3760. bio_endio(bi, 0);
  3761. }
  3762. }
  3763. static void make_request(struct mddev *mddev, struct bio * bi)
  3764. {
  3765. struct r5conf *conf = mddev->private;
  3766. int dd_idx;
  3767. sector_t new_sector;
  3768. sector_t logical_sector, last_sector;
  3769. struct stripe_head *sh;
  3770. const int rw = bio_data_dir(bi);
  3771. int remaining;
  3772. if (unlikely(bi->bi_rw & REQ_FLUSH)) {
  3773. md_flush_request(mddev, bi);
  3774. return;
  3775. }
  3776. md_write_start(mddev, bi);
  3777. if (rw == READ &&
  3778. mddev->reshape_position == MaxSector &&
  3779. chunk_aligned_read(mddev,bi))
  3780. return;
  3781. if (unlikely(bi->bi_rw & REQ_DISCARD)) {
  3782. make_discard_request(mddev, bi);
  3783. return;
  3784. }
  3785. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3786. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3787. bi->bi_next = NULL;
  3788. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3789. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3790. DEFINE_WAIT(w);
  3791. int previous;
  3792. retry:
  3793. previous = 0;
  3794. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3795. if (unlikely(conf->reshape_progress != MaxSector)) {
  3796. /* spinlock is needed as reshape_progress may be
  3797. * 64bit on a 32bit platform, and so it might be
  3798. * possible to see a half-updated value
  3799. * Of course reshape_progress could change after
  3800. * the lock is dropped, so once we get a reference
  3801. * to the stripe that we think it is, we will have
  3802. * to check again.
  3803. */
  3804. spin_lock_irq(&conf->device_lock);
  3805. if (mddev->reshape_backwards
  3806. ? logical_sector < conf->reshape_progress
  3807. : logical_sector >= conf->reshape_progress) {
  3808. previous = 1;
  3809. } else {
  3810. if (mddev->reshape_backwards
  3811. ? logical_sector < conf->reshape_safe
  3812. : logical_sector >= conf->reshape_safe) {
  3813. spin_unlock_irq(&conf->device_lock);
  3814. schedule();
  3815. goto retry;
  3816. }
  3817. }
  3818. spin_unlock_irq(&conf->device_lock);
  3819. }
  3820. new_sector = raid5_compute_sector(conf, logical_sector,
  3821. previous,
  3822. &dd_idx, NULL);
  3823. pr_debug("raid456: make_request, sector %llu logical %llu\n",
  3824. (unsigned long long)new_sector,
  3825. (unsigned long long)logical_sector);
  3826. sh = get_active_stripe(conf, new_sector, previous,
  3827. (bi->bi_rw&RWA_MASK), 0);
  3828. if (sh) {
  3829. if (unlikely(previous)) {
  3830. /* expansion might have moved on while waiting for a
  3831. * stripe, so we must do the range check again.
  3832. * Expansion could still move past after this
  3833. * test, but as we are holding a reference to
  3834. * 'sh', we know that if that happens,
  3835. * STRIPE_EXPANDING will get set and the expansion
  3836. * won't proceed until we finish with the stripe.
  3837. */
  3838. int must_retry = 0;
  3839. spin_lock_irq(&conf->device_lock);
  3840. if (mddev->reshape_backwards
  3841. ? logical_sector >= conf->reshape_progress
  3842. : logical_sector < conf->reshape_progress)
  3843. /* mismatch, need to try again */
  3844. must_retry = 1;
  3845. spin_unlock_irq(&conf->device_lock);
  3846. if (must_retry) {
  3847. release_stripe(sh);
  3848. schedule();
  3849. goto retry;
  3850. }
  3851. }
  3852. if (rw == WRITE &&
  3853. logical_sector >= mddev->suspend_lo &&
  3854. logical_sector < mddev->suspend_hi) {
  3855. release_stripe(sh);
  3856. /* As the suspend_* range is controlled by
  3857. * userspace, we want an interruptible
  3858. * wait.
  3859. */
  3860. flush_signals(current);
  3861. prepare_to_wait(&conf->wait_for_overlap,
  3862. &w, TASK_INTERRUPTIBLE);
  3863. if (logical_sector >= mddev->suspend_lo &&
  3864. logical_sector < mddev->suspend_hi)
  3865. schedule();
  3866. goto retry;
  3867. }
  3868. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3869. !add_stripe_bio(sh, bi, dd_idx, rw)) {
  3870. /* Stripe is busy expanding or
  3871. * add failed due to overlap. Flush everything
  3872. * and wait a while
  3873. */
  3874. md_wakeup_thread(mddev->thread);
  3875. release_stripe(sh);
  3876. schedule();
  3877. goto retry;
  3878. }
  3879. finish_wait(&conf->wait_for_overlap, &w);
  3880. set_bit(STRIPE_HANDLE, &sh->state);
  3881. clear_bit(STRIPE_DELAYED, &sh->state);
  3882. if ((bi->bi_rw & REQ_NOIDLE) &&
  3883. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3884. atomic_inc(&conf->preread_active_stripes);
  3885. release_stripe_plug(mddev, sh);
  3886. } else {
  3887. /* cannot get stripe for read-ahead, just give-up */
  3888. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3889. finish_wait(&conf->wait_for_overlap, &w);
  3890. break;
  3891. }
  3892. }
  3893. remaining = raid5_dec_bi_active_stripes(bi);
  3894. if (remaining == 0) {
  3895. if ( rw == WRITE )
  3896. md_write_end(mddev);
  3897. bio_endio(bi, 0);
  3898. }
  3899. }
  3900. static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
  3901. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
  3902. {
  3903. /* reshaping is quite different to recovery/resync so it is
  3904. * handled quite separately ... here.
  3905. *
  3906. * On each call to sync_request, we gather one chunk worth of
  3907. * destination stripes and flag them as expanding.
  3908. * Then we find all the source stripes and request reads.
  3909. * As the reads complete, handle_stripe will copy the data
  3910. * into the destination stripe and release that stripe.
  3911. */
  3912. struct r5conf *conf = mddev->private;
  3913. struct stripe_head *sh;
  3914. sector_t first_sector, last_sector;
  3915. int raid_disks = conf->previous_raid_disks;
  3916. int data_disks = raid_disks - conf->max_degraded;
  3917. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3918. int i;
  3919. int dd_idx;
  3920. sector_t writepos, readpos, safepos;
  3921. sector_t stripe_addr;
  3922. int reshape_sectors;
  3923. struct list_head stripes;
  3924. if (sector_nr == 0) {
  3925. /* If restarting in the middle, skip the initial sectors */
  3926. if (mddev->reshape_backwards &&
  3927. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3928. sector_nr = raid5_size(mddev, 0, 0)
  3929. - conf->reshape_progress;
  3930. } else if (!mddev->reshape_backwards &&
  3931. conf->reshape_progress > 0)
  3932. sector_nr = conf->reshape_progress;
  3933. sector_div(sector_nr, new_data_disks);
  3934. if (sector_nr) {
  3935. mddev->curr_resync_completed = sector_nr;
  3936. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3937. *skipped = 1;
  3938. return sector_nr;
  3939. }
  3940. }
  3941. /* We need to process a full chunk at a time.
  3942. * If old and new chunk sizes differ, we need to process the
  3943. * largest of these
  3944. */
  3945. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  3946. reshape_sectors = mddev->new_chunk_sectors;
  3947. else
  3948. reshape_sectors = mddev->chunk_sectors;
  3949. /* We update the metadata at least every 10 seconds, or when
  3950. * the data about to be copied would over-write the source of
  3951. * the data at the front of the range. i.e. one new_stripe
  3952. * along from reshape_progress new_maps to after where
  3953. * reshape_safe old_maps to
  3954. */
  3955. writepos = conf->reshape_progress;
  3956. sector_div(writepos, new_data_disks);
  3957. readpos = conf->reshape_progress;
  3958. sector_div(readpos, data_disks);
  3959. safepos = conf->reshape_safe;
  3960. sector_div(safepos, data_disks);
  3961. if (mddev->reshape_backwards) {
  3962. writepos -= min_t(sector_t, reshape_sectors, writepos);
  3963. readpos += reshape_sectors;
  3964. safepos += reshape_sectors;
  3965. } else {
  3966. writepos += reshape_sectors;
  3967. readpos -= min_t(sector_t, reshape_sectors, readpos);
  3968. safepos -= min_t(sector_t, reshape_sectors, safepos);
  3969. }
  3970. /* Having calculated the 'writepos' possibly use it
  3971. * to set 'stripe_addr' which is where we will write to.
  3972. */
  3973. if (mddev->reshape_backwards) {
  3974. BUG_ON(conf->reshape_progress == 0);
  3975. stripe_addr = writepos;
  3976. BUG_ON((mddev->dev_sectors &
  3977. ~((sector_t)reshape_sectors - 1))
  3978. - reshape_sectors - stripe_addr
  3979. != sector_nr);
  3980. } else {
  3981. BUG_ON(writepos != sector_nr + reshape_sectors);
  3982. stripe_addr = sector_nr;
  3983. }
  3984. /* 'writepos' is the most advanced device address we might write.
  3985. * 'readpos' is the least advanced device address we might read.
  3986. * 'safepos' is the least address recorded in the metadata as having
  3987. * been reshaped.
  3988. * If there is a min_offset_diff, these are adjusted either by
  3989. * increasing the safepos/readpos if diff is negative, or
  3990. * increasing writepos if diff is positive.
  3991. * If 'readpos' is then behind 'writepos', there is no way that we can
  3992. * ensure safety in the face of a crash - that must be done by userspace
  3993. * making a backup of the data. So in that case there is no particular
  3994. * rush to update metadata.
  3995. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3996. * update the metadata to advance 'safepos' to match 'readpos' so that
  3997. * we can be safe in the event of a crash.
  3998. * So we insist on updating metadata if safepos is behind writepos and
  3999. * readpos is beyond writepos.
  4000. * In any case, update the metadata every 10 seconds.
  4001. * Maybe that number should be configurable, but I'm not sure it is
  4002. * worth it.... maybe it could be a multiple of safemode_delay???
  4003. */
  4004. if (conf->min_offset_diff < 0) {
  4005. safepos += -conf->min_offset_diff;
  4006. readpos += -conf->min_offset_diff;
  4007. } else
  4008. writepos += conf->min_offset_diff;
  4009. if ((mddev->reshape_backwards
  4010. ? (safepos > writepos && readpos < writepos)
  4011. : (safepos < writepos && readpos > writepos)) ||
  4012. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  4013. /* Cannot proceed until we've updated the superblock... */
  4014. wait_event(conf->wait_for_overlap,
  4015. atomic_read(&conf->reshape_stripes)==0);
  4016. mddev->reshape_position = conf->reshape_progress;
  4017. mddev->curr_resync_completed = sector_nr;
  4018. conf->reshape_checkpoint = jiffies;
  4019. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4020. md_wakeup_thread(mddev->thread);
  4021. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  4022. kthread_should_stop());
  4023. spin_lock_irq(&conf->device_lock);
  4024. conf->reshape_safe = mddev->reshape_position;
  4025. spin_unlock_irq(&conf->device_lock);
  4026. wake_up(&conf->wait_for_overlap);
  4027. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  4028. }
  4029. INIT_LIST_HEAD(&stripes);
  4030. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  4031. int j;
  4032. int skipped_disk = 0;
  4033. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  4034. set_bit(STRIPE_EXPANDING, &sh->state);
  4035. atomic_inc(&conf->reshape_stripes);
  4036. /* If any of this stripe is beyond the end of the old
  4037. * array, then we need to zero those blocks
  4038. */
  4039. for (j=sh->disks; j--;) {
  4040. sector_t s;
  4041. if (j == sh->pd_idx)
  4042. continue;
  4043. if (conf->level == 6 &&
  4044. j == sh->qd_idx)
  4045. continue;
  4046. s = compute_blocknr(sh, j, 0);
  4047. if (s < raid5_size(mddev, 0, 0)) {
  4048. skipped_disk = 1;
  4049. continue;
  4050. }
  4051. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  4052. set_bit(R5_Expanded, &sh->dev[j].flags);
  4053. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  4054. }
  4055. if (!skipped_disk) {
  4056. set_bit(STRIPE_EXPAND_READY, &sh->state);
  4057. set_bit(STRIPE_HANDLE, &sh->state);
  4058. }
  4059. list_add(&sh->lru, &stripes);
  4060. }
  4061. spin_lock_irq(&conf->device_lock);
  4062. if (mddev->reshape_backwards)
  4063. conf->reshape_progress -= reshape_sectors * new_data_disks;
  4064. else
  4065. conf->reshape_progress += reshape_sectors * new_data_disks;
  4066. spin_unlock_irq(&conf->device_lock);
  4067. /* Ok, those stripe are ready. We can start scheduling
  4068. * reads on the source stripes.
  4069. * The source stripes are determined by mapping the first and last
  4070. * block on the destination stripes.
  4071. */
  4072. first_sector =
  4073. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  4074. 1, &dd_idx, NULL);
  4075. last_sector =
  4076. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  4077. * new_data_disks - 1),
  4078. 1, &dd_idx, NULL);
  4079. if (last_sector >= mddev->dev_sectors)
  4080. last_sector = mddev->dev_sectors - 1;
  4081. while (first_sector <= last_sector) {
  4082. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  4083. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  4084. set_bit(STRIPE_HANDLE, &sh->state);
  4085. release_stripe(sh);
  4086. first_sector += STRIPE_SECTORS;
  4087. }
  4088. /* Now that the sources are clearly marked, we can release
  4089. * the destination stripes
  4090. */
  4091. while (!list_empty(&stripes)) {
  4092. sh = list_entry(stripes.next, struct stripe_head, lru);
  4093. list_del_init(&sh->lru);
  4094. release_stripe(sh);
  4095. }
  4096. /* If this takes us to the resync_max point where we have to pause,
  4097. * then we need to write out the superblock.
  4098. */
  4099. sector_nr += reshape_sectors;
  4100. if ((sector_nr - mddev->curr_resync_completed) * 2
  4101. >= mddev->resync_max - mddev->curr_resync_completed) {
  4102. /* Cannot proceed until we've updated the superblock... */
  4103. wait_event(conf->wait_for_overlap,
  4104. atomic_read(&conf->reshape_stripes) == 0);
  4105. mddev->reshape_position = conf->reshape_progress;
  4106. mddev->curr_resync_completed = sector_nr;
  4107. conf->reshape_checkpoint = jiffies;
  4108. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4109. md_wakeup_thread(mddev->thread);
  4110. wait_event(mddev->sb_wait,
  4111. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  4112. || kthread_should_stop());
  4113. spin_lock_irq(&conf->device_lock);
  4114. conf->reshape_safe = mddev->reshape_position;
  4115. spin_unlock_irq(&conf->device_lock);
  4116. wake_up(&conf->wait_for_overlap);
  4117. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  4118. }
  4119. return reshape_sectors;
  4120. }
  4121. /* FIXME go_faster isn't used */
  4122. static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
  4123. {
  4124. struct r5conf *conf = mddev->private;
  4125. struct stripe_head *sh;
  4126. sector_t max_sector = mddev->dev_sectors;
  4127. sector_t sync_blocks;
  4128. int still_degraded = 0;
  4129. int i;
  4130. if (sector_nr >= max_sector) {
  4131. /* just being told to finish up .. nothing much to do */
  4132. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  4133. end_reshape(conf);
  4134. return 0;
  4135. }
  4136. if (mddev->curr_resync < max_sector) /* aborted */
  4137. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  4138. &sync_blocks, 1);
  4139. else /* completed sync */
  4140. conf->fullsync = 0;
  4141. bitmap_close_sync(mddev->bitmap);
  4142. return 0;
  4143. }
  4144. /* Allow raid5_quiesce to complete */
  4145. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  4146. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  4147. return reshape_request(mddev, sector_nr, skipped);
  4148. /* No need to check resync_max as we never do more than one
  4149. * stripe, and as resync_max will always be on a chunk boundary,
  4150. * if the check in md_do_sync didn't fire, there is no chance
  4151. * of overstepping resync_max here
  4152. */
  4153. /* if there is too many failed drives and we are trying
  4154. * to resync, then assert that we are finished, because there is
  4155. * nothing we can do.
  4156. */
  4157. if (mddev->degraded >= conf->max_degraded &&
  4158. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  4159. sector_t rv = mddev->dev_sectors - sector_nr;
  4160. *skipped = 1;
  4161. return rv;
  4162. }
  4163. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  4164. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  4165. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  4166. /* we can skip this block, and probably more */
  4167. sync_blocks /= STRIPE_SECTORS;
  4168. *skipped = 1;
  4169. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  4170. }
  4171. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  4172. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  4173. if (sh == NULL) {
  4174. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  4175. /* make sure we don't swamp the stripe cache if someone else
  4176. * is trying to get access
  4177. */
  4178. schedule_timeout_uninterruptible(1);
  4179. }
  4180. /* Need to check if array will still be degraded after recovery/resync
  4181. * We don't need to check the 'failed' flag as when that gets set,
  4182. * recovery aborts.
  4183. */
  4184. for (i = 0; i < conf->raid_disks; i++)
  4185. if (conf->disks[i].rdev == NULL)
  4186. still_degraded = 1;
  4187. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  4188. set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
  4189. handle_stripe(sh);
  4190. release_stripe(sh);
  4191. return STRIPE_SECTORS;
  4192. }
  4193. static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
  4194. {
  4195. /* We may not be able to submit a whole bio at once as there
  4196. * may not be enough stripe_heads available.
  4197. * We cannot pre-allocate enough stripe_heads as we may need
  4198. * more than exist in the cache (if we allow ever large chunks).
  4199. * So we do one stripe head at a time and record in
  4200. * ->bi_hw_segments how many have been done.
  4201. *
  4202. * We *know* that this entire raid_bio is in one chunk, so
  4203. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  4204. */
  4205. struct stripe_head *sh;
  4206. int dd_idx;
  4207. sector_t sector, logical_sector, last_sector;
  4208. int scnt = 0;
  4209. int remaining;
  4210. int handled = 0;
  4211. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  4212. sector = raid5_compute_sector(conf, logical_sector,
  4213. 0, &dd_idx, NULL);
  4214. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  4215. for (; logical_sector < last_sector;
  4216. logical_sector += STRIPE_SECTORS,
  4217. sector += STRIPE_SECTORS,
  4218. scnt++) {
  4219. if (scnt < raid5_bi_processed_stripes(raid_bio))
  4220. /* already done this stripe */
  4221. continue;
  4222. sh = get_active_stripe(conf, sector, 0, 1, 0);
  4223. if (!sh) {
  4224. /* failed to get a stripe - must wait */
  4225. raid5_set_bi_processed_stripes(raid_bio, scnt);
  4226. conf->retry_read_aligned = raid_bio;
  4227. return handled;
  4228. }
  4229. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  4230. release_stripe(sh);
  4231. raid5_set_bi_processed_stripes(raid_bio, scnt);
  4232. conf->retry_read_aligned = raid_bio;
  4233. return handled;
  4234. }
  4235. set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
  4236. handle_stripe(sh);
  4237. release_stripe(sh);
  4238. handled++;
  4239. }
  4240. remaining = raid5_dec_bi_active_stripes(raid_bio);
  4241. if (remaining == 0)
  4242. bio_endio(raid_bio, 0);
  4243. if (atomic_dec_and_test(&conf->active_aligned_reads))
  4244. wake_up(&conf->wait_for_stripe);
  4245. return handled;
  4246. }
  4247. #define MAX_STRIPE_BATCH 8
  4248. static int handle_active_stripes(struct r5conf *conf)
  4249. {
  4250. struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
  4251. int i, batch_size = 0;
  4252. while (batch_size < MAX_STRIPE_BATCH &&
  4253. (sh = __get_priority_stripe(conf)) != NULL)
  4254. batch[batch_size++] = sh;
  4255. if (batch_size == 0)
  4256. return batch_size;
  4257. spin_unlock_irq(&conf->device_lock);
  4258. for (i = 0; i < batch_size; i++)
  4259. handle_stripe(batch[i]);
  4260. cond_resched();
  4261. spin_lock_irq(&conf->device_lock);
  4262. for (i = 0; i < batch_size; i++)
  4263. __release_stripe(conf, batch[i]);
  4264. return batch_size;
  4265. }
  4266. /*
  4267. * This is our raid5 kernel thread.
  4268. *
  4269. * We scan the hash table for stripes which can be handled now.
  4270. * During the scan, completed stripes are saved for us by the interrupt
  4271. * handler, so that they will not have to wait for our next wakeup.
  4272. */
  4273. static void raid5d(struct md_thread *thread)
  4274. {
  4275. struct mddev *mddev = thread->mddev;
  4276. struct r5conf *conf = mddev->private;
  4277. int handled;
  4278. struct blk_plug plug;
  4279. pr_debug("+++ raid5d active\n");
  4280. md_check_recovery(mddev);
  4281. blk_start_plug(&plug);
  4282. handled = 0;
  4283. spin_lock_irq(&conf->device_lock);
  4284. while (1) {
  4285. struct bio *bio;
  4286. int batch_size;
  4287. if (
  4288. !list_empty(&conf->bitmap_list)) {
  4289. /* Now is a good time to flush some bitmap updates */
  4290. conf->seq_flush++;
  4291. spin_unlock_irq(&conf->device_lock);
  4292. bitmap_unplug(mddev->bitmap);
  4293. spin_lock_irq(&conf->device_lock);
  4294. conf->seq_write = conf->seq_flush;
  4295. activate_bit_delay(conf);
  4296. }
  4297. raid5_activate_delayed(conf);
  4298. while ((bio = remove_bio_from_retry(conf))) {
  4299. int ok;
  4300. spin_unlock_irq(&conf->device_lock);
  4301. ok = retry_aligned_read(conf, bio);
  4302. spin_lock_irq(&conf->device_lock);
  4303. if (!ok)
  4304. break;
  4305. handled++;
  4306. }
  4307. batch_size = handle_active_stripes(conf);
  4308. if (!batch_size)
  4309. break;
  4310. handled += batch_size;
  4311. if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
  4312. spin_unlock_irq(&conf->device_lock);
  4313. md_check_recovery(mddev);
  4314. spin_lock_irq(&conf->device_lock);
  4315. }
  4316. }
  4317. pr_debug("%d stripes handled\n", handled);
  4318. spin_unlock_irq(&conf->device_lock);
  4319. async_tx_issue_pending_all();
  4320. blk_finish_plug(&plug);
  4321. pr_debug("--- raid5d inactive\n");
  4322. }
  4323. static ssize_t
  4324. raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
  4325. {
  4326. struct r5conf *conf = mddev->private;
  4327. if (conf)
  4328. return sprintf(page, "%d\n", conf->max_nr_stripes);
  4329. else
  4330. return 0;
  4331. }
  4332. int
  4333. raid5_set_cache_size(struct mddev *mddev, int size)
  4334. {
  4335. struct r5conf *conf = mddev->private;
  4336. int err;
  4337. if (size <= 16 || size > 32768)
  4338. return -EINVAL;
  4339. while (size < conf->max_nr_stripes) {
  4340. if (drop_one_stripe(conf))
  4341. conf->max_nr_stripes--;
  4342. else
  4343. break;
  4344. }
  4345. err = md_allow_write(mddev);
  4346. if (err)
  4347. return err;
  4348. while (size > conf->max_nr_stripes) {
  4349. if (grow_one_stripe(conf))
  4350. conf->max_nr_stripes++;
  4351. else break;
  4352. }
  4353. return 0;
  4354. }
  4355. EXPORT_SYMBOL(raid5_set_cache_size);
  4356. static ssize_t
  4357. raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
  4358. {
  4359. struct r5conf *conf = mddev->private;
  4360. unsigned long new;
  4361. int err;
  4362. if (len >= PAGE_SIZE)
  4363. return -EINVAL;
  4364. if (!conf)
  4365. return -ENODEV;
  4366. if (strict_strtoul(page, 10, &new))
  4367. return -EINVAL;
  4368. err = raid5_set_cache_size(mddev, new);
  4369. if (err)
  4370. return err;
  4371. return len;
  4372. }
  4373. static struct md_sysfs_entry
  4374. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  4375. raid5_show_stripe_cache_size,
  4376. raid5_store_stripe_cache_size);
  4377. static ssize_t
  4378. raid5_show_preread_threshold(struct mddev *mddev, char *page)
  4379. {
  4380. struct r5conf *conf = mddev->private;
  4381. if (conf)
  4382. return sprintf(page, "%d\n", conf->bypass_threshold);
  4383. else
  4384. return 0;
  4385. }
  4386. static ssize_t
  4387. raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
  4388. {
  4389. struct r5conf *conf = mddev->private;
  4390. unsigned long new;
  4391. if (len >= PAGE_SIZE)
  4392. return -EINVAL;
  4393. if (!conf)
  4394. return -ENODEV;
  4395. if (strict_strtoul(page, 10, &new))
  4396. return -EINVAL;
  4397. if (new > conf->max_nr_stripes)
  4398. return -EINVAL;
  4399. conf->bypass_threshold = new;
  4400. return len;
  4401. }
  4402. static struct md_sysfs_entry
  4403. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  4404. S_IRUGO | S_IWUSR,
  4405. raid5_show_preread_threshold,
  4406. raid5_store_preread_threshold);
  4407. static ssize_t
  4408. stripe_cache_active_show(struct mddev *mddev, char *page)
  4409. {
  4410. struct r5conf *conf = mddev->private;
  4411. if (conf)
  4412. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  4413. else
  4414. return 0;
  4415. }
  4416. static struct md_sysfs_entry
  4417. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  4418. static struct attribute *raid5_attrs[] = {
  4419. &raid5_stripecache_size.attr,
  4420. &raid5_stripecache_active.attr,
  4421. &raid5_preread_bypass_threshold.attr,
  4422. NULL,
  4423. };
  4424. static struct attribute_group raid5_attrs_group = {
  4425. .name = NULL,
  4426. .attrs = raid5_attrs,
  4427. };
  4428. static sector_t
  4429. raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  4430. {
  4431. struct r5conf *conf = mddev->private;
  4432. if (!sectors)
  4433. sectors = mddev->dev_sectors;
  4434. if (!raid_disks)
  4435. /* size is defined by the smallest of previous and new size */
  4436. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  4437. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4438. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  4439. return sectors * (raid_disks - conf->max_degraded);
  4440. }
  4441. static void raid5_free_percpu(struct r5conf *conf)
  4442. {
  4443. struct raid5_percpu *percpu;
  4444. unsigned long cpu;
  4445. if (!conf->percpu)
  4446. return;
  4447. get_online_cpus();
  4448. for_each_possible_cpu(cpu) {
  4449. percpu = per_cpu_ptr(conf->percpu, cpu);
  4450. safe_put_page(percpu->spare_page);
  4451. kfree(percpu->scribble);
  4452. }
  4453. #ifdef CONFIG_HOTPLUG_CPU
  4454. unregister_cpu_notifier(&conf->cpu_notify);
  4455. #endif
  4456. put_online_cpus();
  4457. free_percpu(conf->percpu);
  4458. }
  4459. static void free_conf(struct r5conf *conf)
  4460. {
  4461. shrink_stripes(conf);
  4462. raid5_free_percpu(conf);
  4463. kfree(conf->disks);
  4464. kfree(conf->stripe_hashtbl);
  4465. kfree(conf);
  4466. }
  4467. #ifdef CONFIG_HOTPLUG_CPU
  4468. static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
  4469. void *hcpu)
  4470. {
  4471. struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
  4472. long cpu = (long)hcpu;
  4473. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  4474. switch (action) {
  4475. case CPU_UP_PREPARE:
  4476. case CPU_UP_PREPARE_FROZEN:
  4477. if (conf->level == 6 && !percpu->spare_page)
  4478. percpu->spare_page = alloc_page(GFP_KERNEL);
  4479. if (!percpu->scribble)
  4480. percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4481. if (!percpu->scribble ||
  4482. (conf->level == 6 && !percpu->spare_page)) {
  4483. safe_put_page(percpu->spare_page);
  4484. kfree(percpu->scribble);
  4485. pr_err("%s: failed memory allocation for cpu%ld\n",
  4486. __func__, cpu);
  4487. return notifier_from_errno(-ENOMEM);
  4488. }
  4489. break;
  4490. case CPU_DEAD:
  4491. case CPU_DEAD_FROZEN:
  4492. safe_put_page(percpu->spare_page);
  4493. kfree(percpu->scribble);
  4494. percpu->spare_page = NULL;
  4495. percpu->scribble = NULL;
  4496. break;
  4497. default:
  4498. break;
  4499. }
  4500. return NOTIFY_OK;
  4501. }
  4502. #endif
  4503. static int raid5_alloc_percpu(struct r5conf *conf)
  4504. {
  4505. unsigned long cpu;
  4506. struct page *spare_page;
  4507. struct raid5_percpu __percpu *allcpus;
  4508. void *scribble;
  4509. int err;
  4510. allcpus = alloc_percpu(struct raid5_percpu);
  4511. if (!allcpus)
  4512. return -ENOMEM;
  4513. conf->percpu = allcpus;
  4514. get_online_cpus();
  4515. err = 0;
  4516. for_each_present_cpu(cpu) {
  4517. if (conf->level == 6) {
  4518. spare_page = alloc_page(GFP_KERNEL);
  4519. if (!spare_page) {
  4520. err = -ENOMEM;
  4521. break;
  4522. }
  4523. per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
  4524. }
  4525. scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4526. if (!scribble) {
  4527. err = -ENOMEM;
  4528. break;
  4529. }
  4530. per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
  4531. }
  4532. #ifdef CONFIG_HOTPLUG_CPU
  4533. conf->cpu_notify.notifier_call = raid456_cpu_notify;
  4534. conf->cpu_notify.priority = 0;
  4535. if (err == 0)
  4536. err = register_cpu_notifier(&conf->cpu_notify);
  4537. #endif
  4538. put_online_cpus();
  4539. return err;
  4540. }
  4541. static struct r5conf *setup_conf(struct mddev *mddev)
  4542. {
  4543. struct r5conf *conf;
  4544. int raid_disk, memory, max_disks;
  4545. struct md_rdev *rdev;
  4546. struct disk_info *disk;
  4547. char pers_name[6];
  4548. if (mddev->new_level != 5
  4549. && mddev->new_level != 4
  4550. && mddev->new_level != 6) {
  4551. printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  4552. mdname(mddev), mddev->new_level);
  4553. return ERR_PTR(-EIO);
  4554. }
  4555. if ((mddev->new_level == 5
  4556. && !algorithm_valid_raid5(mddev->new_layout)) ||
  4557. (mddev->new_level == 6
  4558. && !algorithm_valid_raid6(mddev->new_layout))) {
  4559. printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
  4560. mdname(mddev), mddev->new_layout);
  4561. return ERR_PTR(-EIO);
  4562. }
  4563. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  4564. printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  4565. mdname(mddev), mddev->raid_disks);
  4566. return ERR_PTR(-EINVAL);
  4567. }
  4568. if (!mddev->new_chunk_sectors ||
  4569. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  4570. !is_power_of_2(mddev->new_chunk_sectors)) {
  4571. printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
  4572. mdname(mddev), mddev->new_chunk_sectors << 9);
  4573. return ERR_PTR(-EINVAL);
  4574. }
  4575. conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
  4576. if (conf == NULL)
  4577. goto abort;
  4578. spin_lock_init(&conf->device_lock);
  4579. init_waitqueue_head(&conf->wait_for_stripe);
  4580. init_waitqueue_head(&conf->wait_for_overlap);
  4581. INIT_LIST_HEAD(&conf->handle_list);
  4582. INIT_LIST_HEAD(&conf->hold_list);
  4583. INIT_LIST_HEAD(&conf->delayed_list);
  4584. INIT_LIST_HEAD(&conf->bitmap_list);
  4585. INIT_LIST_HEAD(&conf->inactive_list);
  4586. atomic_set(&conf->active_stripes, 0);
  4587. atomic_set(&conf->preread_active_stripes, 0);
  4588. atomic_set(&conf->active_aligned_reads, 0);
  4589. conf->bypass_threshold = BYPASS_THRESHOLD;
  4590. conf->recovery_disabled = mddev->recovery_disabled - 1;
  4591. conf->raid_disks = mddev->raid_disks;
  4592. if (mddev->reshape_position == MaxSector)
  4593. conf->previous_raid_disks = mddev->raid_disks;
  4594. else
  4595. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  4596. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  4597. conf->scribble_len = scribble_len(max_disks);
  4598. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  4599. GFP_KERNEL);
  4600. if (!conf->disks)
  4601. goto abort;
  4602. conf->mddev = mddev;
  4603. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  4604. goto abort;
  4605. conf->level = mddev->new_level;
  4606. if (raid5_alloc_percpu(conf) != 0)
  4607. goto abort;
  4608. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  4609. rdev_for_each(rdev, mddev) {
  4610. raid_disk = rdev->raid_disk;
  4611. if (raid_disk >= max_disks
  4612. || raid_disk < 0)
  4613. continue;
  4614. disk = conf->disks + raid_disk;
  4615. if (test_bit(Replacement, &rdev->flags)) {
  4616. if (disk->replacement)
  4617. goto abort;
  4618. disk->replacement = rdev;
  4619. } else {
  4620. if (disk->rdev)
  4621. goto abort;
  4622. disk->rdev = rdev;
  4623. }
  4624. if (test_bit(In_sync, &rdev->flags)) {
  4625. char b[BDEVNAME_SIZE];
  4626. printk(KERN_INFO "md/raid:%s: device %s operational as raid"
  4627. " disk %d\n",
  4628. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  4629. } else if (rdev->saved_raid_disk != raid_disk)
  4630. /* Cannot rely on bitmap to complete recovery */
  4631. conf->fullsync = 1;
  4632. }
  4633. conf->chunk_sectors = mddev->new_chunk_sectors;
  4634. conf->level = mddev->new_level;
  4635. if (conf->level == 6)
  4636. conf->max_degraded = 2;
  4637. else
  4638. conf->max_degraded = 1;
  4639. conf->algorithm = mddev->new_layout;
  4640. conf->max_nr_stripes = NR_STRIPES;
  4641. conf->reshape_progress = mddev->reshape_position;
  4642. if (conf->reshape_progress != MaxSector) {
  4643. conf->prev_chunk_sectors = mddev->chunk_sectors;
  4644. conf->prev_algo = mddev->layout;
  4645. }
  4646. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  4647. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  4648. if (grow_stripes(conf, conf->max_nr_stripes)) {
  4649. printk(KERN_ERR
  4650. "md/raid:%s: couldn't allocate %dkB for buffers\n",
  4651. mdname(mddev), memory);
  4652. goto abort;
  4653. } else
  4654. printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
  4655. mdname(mddev), memory);
  4656. sprintf(pers_name, "raid%d", mddev->new_level);
  4657. conf->thread = md_register_thread(raid5d, mddev, pers_name);
  4658. if (!conf->thread) {
  4659. printk(KERN_ERR
  4660. "md/raid:%s: couldn't allocate thread.\n",
  4661. mdname(mddev));
  4662. goto abort;
  4663. }
  4664. return conf;
  4665. abort:
  4666. if (conf) {
  4667. free_conf(conf);
  4668. return ERR_PTR(-EIO);
  4669. } else
  4670. return ERR_PTR(-ENOMEM);
  4671. }
  4672. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  4673. {
  4674. switch (algo) {
  4675. case ALGORITHM_PARITY_0:
  4676. if (raid_disk < max_degraded)
  4677. return 1;
  4678. break;
  4679. case ALGORITHM_PARITY_N:
  4680. if (raid_disk >= raid_disks - max_degraded)
  4681. return 1;
  4682. break;
  4683. case ALGORITHM_PARITY_0_6:
  4684. if (raid_disk == 0 ||
  4685. raid_disk == raid_disks - 1)
  4686. return 1;
  4687. break;
  4688. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4689. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4690. case ALGORITHM_LEFT_SYMMETRIC_6:
  4691. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4692. if (raid_disk == raid_disks - 1)
  4693. return 1;
  4694. }
  4695. return 0;
  4696. }
  4697. static int run(struct mddev *mddev)
  4698. {
  4699. struct r5conf *conf;
  4700. int working_disks = 0;
  4701. int dirty_parity_disks = 0;
  4702. struct md_rdev *rdev;
  4703. sector_t reshape_offset = 0;
  4704. int i;
  4705. long long min_offset_diff = 0;
  4706. int first = 1;
  4707. if (mddev->recovery_cp != MaxSector)
  4708. printk(KERN_NOTICE "md/raid:%s: not clean"
  4709. " -- starting background reconstruction\n",
  4710. mdname(mddev));
  4711. rdev_for_each(rdev, mddev) {
  4712. long long diff;
  4713. if (rdev->raid_disk < 0)
  4714. continue;
  4715. diff = (rdev->new_data_offset - rdev->data_offset);
  4716. if (first) {
  4717. min_offset_diff = diff;
  4718. first = 0;
  4719. } else if (mddev->reshape_backwards &&
  4720. diff < min_offset_diff)
  4721. min_offset_diff = diff;
  4722. else if (!mddev->reshape_backwards &&
  4723. diff > min_offset_diff)
  4724. min_offset_diff = diff;
  4725. }
  4726. if (mddev->reshape_position != MaxSector) {
  4727. /* Check that we can continue the reshape.
  4728. * Difficulties arise if the stripe we would write to
  4729. * next is at or after the stripe we would read from next.
  4730. * For a reshape that changes the number of devices, this
  4731. * is only possible for a very short time, and mdadm makes
  4732. * sure that time appears to have past before assembling
  4733. * the array. So we fail if that time hasn't passed.
  4734. * For a reshape that keeps the number of devices the same
  4735. * mdadm must be monitoring the reshape can keeping the
  4736. * critical areas read-only and backed up. It will start
  4737. * the array in read-only mode, so we check for that.
  4738. */
  4739. sector_t here_new, here_old;
  4740. int old_disks;
  4741. int max_degraded = (mddev->level == 6 ? 2 : 1);
  4742. if (mddev->new_level != mddev->level) {
  4743. printk(KERN_ERR "md/raid:%s: unsupported reshape "
  4744. "required - aborting.\n",
  4745. mdname(mddev));
  4746. return -EINVAL;
  4747. }
  4748. old_disks = mddev->raid_disks - mddev->delta_disks;
  4749. /* reshape_position must be on a new-stripe boundary, and one
  4750. * further up in new geometry must map after here in old
  4751. * geometry.
  4752. */
  4753. here_new = mddev->reshape_position;
  4754. if (sector_div(here_new, mddev->new_chunk_sectors *
  4755. (mddev->raid_disks - max_degraded))) {
  4756. printk(KERN_ERR "md/raid:%s: reshape_position not "
  4757. "on a stripe boundary\n", mdname(mddev));
  4758. return -EINVAL;
  4759. }
  4760. reshape_offset = here_new * mddev->new_chunk_sectors;
  4761. /* here_new is the stripe we will write to */
  4762. here_old = mddev->reshape_position;
  4763. sector_div(here_old, mddev->chunk_sectors *
  4764. (old_disks-max_degraded));
  4765. /* here_old is the first stripe that we might need to read
  4766. * from */
  4767. if (mddev->delta_disks == 0) {
  4768. if ((here_new * mddev->new_chunk_sectors !=
  4769. here_old * mddev->chunk_sectors)) {
  4770. printk(KERN_ERR "md/raid:%s: reshape position is"
  4771. " confused - aborting\n", mdname(mddev));
  4772. return -EINVAL;
  4773. }
  4774. /* We cannot be sure it is safe to start an in-place
  4775. * reshape. It is only safe if user-space is monitoring
  4776. * and taking constant backups.
  4777. * mdadm always starts a situation like this in
  4778. * readonly mode so it can take control before
  4779. * allowing any writes. So just check for that.
  4780. */
  4781. if (abs(min_offset_diff) >= mddev->chunk_sectors &&
  4782. abs(min_offset_diff) >= mddev->new_chunk_sectors)
  4783. /* not really in-place - so OK */;
  4784. else if (mddev->ro == 0) {
  4785. printk(KERN_ERR "md/raid:%s: in-place reshape "
  4786. "must be started in read-only mode "
  4787. "- aborting\n",
  4788. mdname(mddev));
  4789. return -EINVAL;
  4790. }
  4791. } else if (mddev->reshape_backwards
  4792. ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
  4793. here_old * mddev->chunk_sectors)
  4794. : (here_new * mddev->new_chunk_sectors >=
  4795. here_old * mddev->chunk_sectors + (-min_offset_diff))) {
  4796. /* Reading from the same stripe as writing to - bad */
  4797. printk(KERN_ERR "md/raid:%s: reshape_position too early for "
  4798. "auto-recovery - aborting.\n",
  4799. mdname(mddev));
  4800. return -EINVAL;
  4801. }
  4802. printk(KERN_INFO "md/raid:%s: reshape will continue\n",
  4803. mdname(mddev));
  4804. /* OK, we should be able to continue; */
  4805. } else {
  4806. BUG_ON(mddev->level != mddev->new_level);
  4807. BUG_ON(mddev->layout != mddev->new_layout);
  4808. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  4809. BUG_ON(mddev->delta_disks != 0);
  4810. }
  4811. if (mddev->private == NULL)
  4812. conf = setup_conf(mddev);
  4813. else
  4814. conf = mddev->private;
  4815. if (IS_ERR(conf))
  4816. return PTR_ERR(conf);
  4817. conf->min_offset_diff = min_offset_diff;
  4818. mddev->thread = conf->thread;
  4819. conf->thread = NULL;
  4820. mddev->private = conf;
  4821. for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
  4822. i++) {
  4823. rdev = conf->disks[i].rdev;
  4824. if (!rdev && conf->disks[i].replacement) {
  4825. /* The replacement is all we have yet */
  4826. rdev = conf->disks[i].replacement;
  4827. conf->disks[i].replacement = NULL;
  4828. clear_bit(Replacement, &rdev->flags);
  4829. conf->disks[i].rdev = rdev;
  4830. }
  4831. if (!rdev)
  4832. continue;
  4833. if (conf->disks[i].replacement &&
  4834. conf->reshape_progress != MaxSector) {
  4835. /* replacements and reshape simply do not mix. */
  4836. printk(KERN_ERR "md: cannot handle concurrent "
  4837. "replacement and reshape.\n");
  4838. goto abort;
  4839. }
  4840. if (test_bit(In_sync, &rdev->flags)) {
  4841. working_disks++;
  4842. continue;
  4843. }
  4844. /* This disc is not fully in-sync. However if it
  4845. * just stored parity (beyond the recovery_offset),
  4846. * when we don't need to be concerned about the
  4847. * array being dirty.
  4848. * When reshape goes 'backwards', we never have
  4849. * partially completed devices, so we only need
  4850. * to worry about reshape going forwards.
  4851. */
  4852. /* Hack because v0.91 doesn't store recovery_offset properly. */
  4853. if (mddev->major_version == 0 &&
  4854. mddev->minor_version > 90)
  4855. rdev->recovery_offset = reshape_offset;
  4856. if (rdev->recovery_offset < reshape_offset) {
  4857. /* We need to check old and new layout */
  4858. if (!only_parity(rdev->raid_disk,
  4859. conf->algorithm,
  4860. conf->raid_disks,
  4861. conf->max_degraded))
  4862. continue;
  4863. }
  4864. if (!only_parity(rdev->raid_disk,
  4865. conf->prev_algo,
  4866. conf->previous_raid_disks,
  4867. conf->max_degraded))
  4868. continue;
  4869. dirty_parity_disks++;
  4870. }
  4871. /*
  4872. * 0 for a fully functional array, 1 or 2 for a degraded array.
  4873. */
  4874. mddev->degraded = calc_degraded(conf);
  4875. if (has_failed(conf)) {
  4876. printk(KERN_ERR "md/raid:%s: not enough operational devices"
  4877. " (%d/%d failed)\n",
  4878. mdname(mddev), mddev->degraded, conf->raid_disks);
  4879. goto abort;
  4880. }
  4881. /* device size must be a multiple of chunk size */
  4882. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  4883. mddev->resync_max_sectors = mddev->dev_sectors;
  4884. if (mddev->degraded > dirty_parity_disks &&
  4885. mddev->recovery_cp != MaxSector) {
  4886. if (mddev->ok_start_degraded)
  4887. printk(KERN_WARNING
  4888. "md/raid:%s: starting dirty degraded array"
  4889. " - data corruption possible.\n",
  4890. mdname(mddev));
  4891. else {
  4892. printk(KERN_ERR
  4893. "md/raid:%s: cannot start dirty degraded array.\n",
  4894. mdname(mddev));
  4895. goto abort;
  4896. }
  4897. }
  4898. if (mddev->degraded == 0)
  4899. printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
  4900. " devices, algorithm %d\n", mdname(mddev), conf->level,
  4901. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4902. mddev->new_layout);
  4903. else
  4904. printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
  4905. " out of %d devices, algorithm %d\n",
  4906. mdname(mddev), conf->level,
  4907. mddev->raid_disks - mddev->degraded,
  4908. mddev->raid_disks, mddev->new_layout);
  4909. print_raid5_conf(conf);
  4910. if (conf->reshape_progress != MaxSector) {
  4911. conf->reshape_safe = conf->reshape_progress;
  4912. atomic_set(&conf->reshape_stripes, 0);
  4913. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4914. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4915. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4916. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4917. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4918. "reshape");
  4919. }
  4920. /* Ok, everything is just fine now */
  4921. if (mddev->to_remove == &raid5_attrs_group)
  4922. mddev->to_remove = NULL;
  4923. else if (mddev->kobj.sd &&
  4924. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4925. printk(KERN_WARNING
  4926. "raid5: failed to create sysfs attributes for %s\n",
  4927. mdname(mddev));
  4928. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4929. if (mddev->queue) {
  4930. int chunk_size;
  4931. bool discard_supported = true;
  4932. /* read-ahead size must cover two whole stripes, which
  4933. * is 2 * (datadisks) * chunksize where 'n' is the
  4934. * number of raid devices
  4935. */
  4936. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4937. int stripe = data_disks *
  4938. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  4939. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4940. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4941. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4942. mddev->queue->backing_dev_info.congested_data = mddev;
  4943. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4944. chunk_size = mddev->chunk_sectors << 9;
  4945. blk_queue_io_min(mddev->queue, chunk_size);
  4946. blk_queue_io_opt(mddev->queue, chunk_size *
  4947. (conf->raid_disks - conf->max_degraded));
  4948. /*
  4949. * We can only discard a whole stripe. It doesn't make sense to
  4950. * discard data disk but write parity disk
  4951. */
  4952. stripe = stripe * PAGE_SIZE;
  4953. mddev->queue->limits.discard_alignment = stripe;
  4954. mddev->queue->limits.discard_granularity = stripe;
  4955. /*
  4956. * unaligned part of discard request will be ignored, so can't
  4957. * guarantee discard_zerors_data
  4958. */
  4959. mddev->queue->limits.discard_zeroes_data = 0;
  4960. rdev_for_each(rdev, mddev) {
  4961. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4962. rdev->data_offset << 9);
  4963. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4964. rdev->new_data_offset << 9);
  4965. /*
  4966. * discard_zeroes_data is required, otherwise data
  4967. * could be lost. Consider a scenario: discard a stripe
  4968. * (the stripe could be inconsistent if
  4969. * discard_zeroes_data is 0); write one disk of the
  4970. * stripe (the stripe could be inconsistent again
  4971. * depending on which disks are used to calculate
  4972. * parity); the disk is broken; The stripe data of this
  4973. * disk is lost.
  4974. */
  4975. if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
  4976. !bdev_get_queue(rdev->bdev)->
  4977. limits.discard_zeroes_data)
  4978. discard_supported = false;
  4979. }
  4980. if (discard_supported &&
  4981. mddev->queue->limits.max_discard_sectors >= stripe &&
  4982. mddev->queue->limits.discard_granularity >= stripe)
  4983. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  4984. mddev->queue);
  4985. else
  4986. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  4987. mddev->queue);
  4988. }
  4989. return 0;
  4990. abort:
  4991. md_unregister_thread(&mddev->thread);
  4992. print_raid5_conf(conf);
  4993. free_conf(conf);
  4994. mddev->private = NULL;
  4995. printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
  4996. return -EIO;
  4997. }
  4998. static int stop(struct mddev *mddev)
  4999. {
  5000. struct r5conf *conf = mddev->private;
  5001. md_unregister_thread(&mddev->thread);
  5002. if (mddev->queue)
  5003. mddev->queue->backing_dev_info.congested_fn = NULL;
  5004. free_conf(conf);
  5005. mddev->private = NULL;
  5006. mddev->to_remove = &raid5_attrs_group;
  5007. return 0;
  5008. }
  5009. static void status(struct seq_file *seq, struct mddev *mddev)
  5010. {
  5011. struct r5conf *conf = mddev->private;
  5012. int i;
  5013. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  5014. mddev->chunk_sectors / 2, mddev->layout);
  5015. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  5016. for (i = 0; i < conf->raid_disks; i++)
  5017. seq_printf (seq, "%s",
  5018. conf->disks[i].rdev &&
  5019. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  5020. seq_printf (seq, "]");
  5021. }
  5022. static void print_raid5_conf (struct r5conf *conf)
  5023. {
  5024. int i;
  5025. struct disk_info *tmp;
  5026. printk(KERN_DEBUG "RAID conf printout:\n");
  5027. if (!conf) {
  5028. printk("(conf==NULL)\n");
  5029. return;
  5030. }
  5031. printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
  5032. conf->raid_disks,
  5033. conf->raid_disks - conf->mddev->degraded);
  5034. for (i = 0; i < conf->raid_disks; i++) {
  5035. char b[BDEVNAME_SIZE];
  5036. tmp = conf->disks + i;
  5037. if (tmp->rdev)
  5038. printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
  5039. i, !test_bit(Faulty, &tmp->rdev->flags),
  5040. bdevname(tmp->rdev->bdev, b));
  5041. }
  5042. }
  5043. static int raid5_spare_active(struct mddev *mddev)
  5044. {
  5045. int i;
  5046. struct r5conf *conf = mddev->private;
  5047. struct disk_info *tmp;
  5048. int count = 0;
  5049. unsigned long flags;
  5050. for (i = 0; i < conf->raid_disks; i++) {
  5051. tmp = conf->disks + i;
  5052. if (tmp->replacement
  5053. && tmp->replacement->recovery_offset == MaxSector
  5054. && !test_bit(Faulty, &tmp->replacement->flags)
  5055. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  5056. /* Replacement has just become active. */
  5057. if (!tmp->rdev
  5058. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  5059. count++;
  5060. if (tmp->rdev) {
  5061. /* Replaced device not technically faulty,
  5062. * but we need to be sure it gets removed
  5063. * and never re-added.
  5064. */
  5065. set_bit(Faulty, &tmp->rdev->flags);
  5066. sysfs_notify_dirent_safe(
  5067. tmp->rdev->sysfs_state);
  5068. }
  5069. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  5070. } else if (tmp->rdev
  5071. && tmp->rdev->recovery_offset == MaxSector
  5072. && !test_bit(Faulty, &tmp->rdev->flags)
  5073. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  5074. count++;
  5075. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  5076. }
  5077. }
  5078. spin_lock_irqsave(&conf->device_lock, flags);
  5079. mddev->degraded = calc_degraded(conf);
  5080. spin_unlock_irqrestore(&conf->device_lock, flags);
  5081. print_raid5_conf(conf);
  5082. return count;
  5083. }
  5084. static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  5085. {
  5086. struct r5conf *conf = mddev->private;
  5087. int err = 0;
  5088. int number = rdev->raid_disk;
  5089. struct md_rdev **rdevp;
  5090. struct disk_info *p = conf->disks + number;
  5091. print_raid5_conf(conf);
  5092. if (rdev == p->rdev)
  5093. rdevp = &p->rdev;
  5094. else if (rdev == p->replacement)
  5095. rdevp = &p->replacement;
  5096. else
  5097. return 0;
  5098. if (number >= conf->raid_disks &&
  5099. conf->reshape_progress == MaxSector)
  5100. clear_bit(In_sync, &rdev->flags);
  5101. if (test_bit(In_sync, &rdev->flags) ||
  5102. atomic_read(&rdev->nr_pending)) {
  5103. err = -EBUSY;
  5104. goto abort;
  5105. }
  5106. /* Only remove non-faulty devices if recovery
  5107. * isn't possible.
  5108. */
  5109. if (!test_bit(Faulty, &rdev->flags) &&
  5110. mddev->recovery_disabled != conf->recovery_disabled &&
  5111. !has_failed(conf) &&
  5112. (!p->replacement || p->replacement == rdev) &&
  5113. number < conf->raid_disks) {
  5114. err = -EBUSY;
  5115. goto abort;
  5116. }
  5117. *rdevp = NULL;
  5118. synchronize_rcu();
  5119. if (atomic_read(&rdev->nr_pending)) {
  5120. /* lost the race, try later */
  5121. err = -EBUSY;
  5122. *rdevp = rdev;
  5123. } else if (p->replacement) {
  5124. /* We must have just cleared 'rdev' */
  5125. p->rdev = p->replacement;
  5126. clear_bit(Replacement, &p->replacement->flags);
  5127. smp_mb(); /* Make sure other CPUs may see both as identical
  5128. * but will never see neither - if they are careful
  5129. */
  5130. p->replacement = NULL;
  5131. clear_bit(WantReplacement, &rdev->flags);
  5132. } else
  5133. /* We might have just removed the Replacement as faulty-
  5134. * clear the bit just in case
  5135. */
  5136. clear_bit(WantReplacement, &rdev->flags);
  5137. abort:
  5138. print_raid5_conf(conf);
  5139. return err;
  5140. }
  5141. static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  5142. {
  5143. struct r5conf *conf = mddev->private;
  5144. int err = -EEXIST;
  5145. int disk;
  5146. struct disk_info *p;
  5147. int first = 0;
  5148. int last = conf->raid_disks - 1;
  5149. if (mddev->recovery_disabled == conf->recovery_disabled)
  5150. return -EBUSY;
  5151. if (rdev->saved_raid_disk < 0 && has_failed(conf))
  5152. /* no point adding a device */
  5153. return -EINVAL;
  5154. if (rdev->raid_disk >= 0)
  5155. first = last = rdev->raid_disk;
  5156. /*
  5157. * find the disk ... but prefer rdev->saved_raid_disk
  5158. * if possible.
  5159. */
  5160. if (rdev->saved_raid_disk >= 0 &&
  5161. rdev->saved_raid_disk >= first &&
  5162. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  5163. first = rdev->saved_raid_disk;
  5164. for (disk = first; disk <= last; disk++) {
  5165. p = conf->disks + disk;
  5166. if (p->rdev == NULL) {
  5167. clear_bit(In_sync, &rdev->flags);
  5168. rdev->raid_disk = disk;
  5169. err = 0;
  5170. if (rdev->saved_raid_disk != disk)
  5171. conf->fullsync = 1;
  5172. rcu_assign_pointer(p->rdev, rdev);
  5173. goto out;
  5174. }
  5175. }
  5176. for (disk = first; disk <= last; disk++) {
  5177. p = conf->disks + disk;
  5178. if (test_bit(WantReplacement, &p->rdev->flags) &&
  5179. p->replacement == NULL) {
  5180. clear_bit(In_sync, &rdev->flags);
  5181. set_bit(Replacement, &rdev->flags);
  5182. rdev->raid_disk = disk;
  5183. err = 0;
  5184. conf->fullsync = 1;
  5185. rcu_assign_pointer(p->replacement, rdev);
  5186. break;
  5187. }
  5188. }
  5189. out:
  5190. print_raid5_conf(conf);
  5191. return err;
  5192. }
  5193. static int raid5_resize(struct mddev *mddev, sector_t sectors)
  5194. {
  5195. /* no resync is happening, and there is enough space
  5196. * on all devices, so we can resize.
  5197. * We need to make sure resync covers any new space.
  5198. * If the array is shrinking we should possibly wait until
  5199. * any io in the removed space completes, but it hardly seems
  5200. * worth it.
  5201. */
  5202. sector_t newsize;
  5203. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  5204. newsize = raid5_size(mddev, sectors, mddev->raid_disks);
  5205. if (mddev->external_size &&
  5206. mddev->array_sectors > newsize)
  5207. return -EINVAL;
  5208. if (mddev->bitmap) {
  5209. int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
  5210. if (ret)
  5211. return ret;
  5212. }
  5213. md_set_array_sectors(mddev, newsize);
  5214. set_capacity(mddev->gendisk, mddev->array_sectors);
  5215. revalidate_disk(mddev->gendisk);
  5216. if (sectors > mddev->dev_sectors &&
  5217. mddev->recovery_cp > mddev->dev_sectors) {
  5218. mddev->recovery_cp = mddev->dev_sectors;
  5219. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  5220. }
  5221. mddev->dev_sectors = sectors;
  5222. mddev->resync_max_sectors = sectors;
  5223. return 0;
  5224. }
  5225. static int check_stripe_cache(struct mddev *mddev)
  5226. {
  5227. /* Can only proceed if there are plenty of stripe_heads.
  5228. * We need a minimum of one full stripe,, and for sensible progress
  5229. * it is best to have about 4 times that.
  5230. * If we require 4 times, then the default 256 4K stripe_heads will
  5231. * allow for chunk sizes up to 256K, which is probably OK.
  5232. * If the chunk size is greater, user-space should request more
  5233. * stripe_heads first.
  5234. */
  5235. struct r5conf *conf = mddev->private;
  5236. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  5237. > conf->max_nr_stripes ||
  5238. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  5239. > conf->max_nr_stripes) {
  5240. printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  5241. mdname(mddev),
  5242. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  5243. / STRIPE_SIZE)*4);
  5244. return 0;
  5245. }
  5246. return 1;
  5247. }
  5248. static int check_reshape(struct mddev *mddev)
  5249. {
  5250. struct r5conf *conf = mddev->private;
  5251. if (mddev->delta_disks == 0 &&
  5252. mddev->new_layout == mddev->layout &&
  5253. mddev->new_chunk_sectors == mddev->chunk_sectors)
  5254. return 0; /* nothing to do */
  5255. if (has_failed(conf))
  5256. return -EINVAL;
  5257. if (mddev->delta_disks < 0) {
  5258. /* We might be able to shrink, but the devices must
  5259. * be made bigger first.
  5260. * For raid6, 4 is the minimum size.
  5261. * Otherwise 2 is the minimum
  5262. */
  5263. int min = 2;
  5264. if (mddev->level == 6)
  5265. min = 4;
  5266. if (mddev->raid_disks + mddev->delta_disks < min)
  5267. return -EINVAL;
  5268. }
  5269. if (!check_stripe_cache(mddev))
  5270. return -ENOSPC;
  5271. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  5272. }
  5273. static int raid5_start_reshape(struct mddev *mddev)
  5274. {
  5275. struct r5conf *conf = mddev->private;
  5276. struct md_rdev *rdev;
  5277. int spares = 0;
  5278. unsigned long flags;
  5279. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  5280. return -EBUSY;
  5281. if (!check_stripe_cache(mddev))
  5282. return -ENOSPC;
  5283. if (has_failed(conf))
  5284. return -EINVAL;
  5285. rdev_for_each(rdev, mddev) {
  5286. if (!test_bit(In_sync, &rdev->flags)
  5287. && !test_bit(Faulty, &rdev->flags))
  5288. spares++;
  5289. }
  5290. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  5291. /* Not enough devices even to make a degraded array
  5292. * of that size
  5293. */
  5294. return -EINVAL;
  5295. /* Refuse to reduce size of the array. Any reductions in
  5296. * array size must be through explicit setting of array_size
  5297. * attribute.
  5298. */
  5299. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  5300. < mddev->array_sectors) {
  5301. printk(KERN_ERR "md/raid:%s: array size must be reduced "
  5302. "before number of disks\n", mdname(mddev));
  5303. return -EINVAL;
  5304. }
  5305. atomic_set(&conf->reshape_stripes, 0);
  5306. spin_lock_irq(&conf->device_lock);
  5307. conf->previous_raid_disks = conf->raid_disks;
  5308. conf->raid_disks += mddev->delta_disks;
  5309. conf->prev_chunk_sectors = conf->chunk_sectors;
  5310. conf->chunk_sectors = mddev->new_chunk_sectors;
  5311. conf->prev_algo = conf->algorithm;
  5312. conf->algorithm = mddev->new_layout;
  5313. conf->generation++;
  5314. /* Code that selects data_offset needs to see the generation update
  5315. * if reshape_progress has been set - so a memory barrier needed.
  5316. */
  5317. smp_mb();
  5318. if (mddev->reshape_backwards)
  5319. conf->reshape_progress = raid5_size(mddev, 0, 0);
  5320. else
  5321. conf->reshape_progress = 0;
  5322. conf->reshape_safe = conf->reshape_progress;
  5323. spin_unlock_irq(&conf->device_lock);
  5324. /* Add some new drives, as many as will fit.
  5325. * We know there are enough to make the newly sized array work.
  5326. * Don't add devices if we are reducing the number of
  5327. * devices in the array. This is because it is not possible
  5328. * to correctly record the "partially reconstructed" state of
  5329. * such devices during the reshape and confusion could result.
  5330. */
  5331. if (mddev->delta_disks >= 0) {
  5332. rdev_for_each(rdev, mddev)
  5333. if (rdev->raid_disk < 0 &&
  5334. !test_bit(Faulty, &rdev->flags)) {
  5335. if (raid5_add_disk(mddev, rdev) == 0) {
  5336. if (rdev->raid_disk
  5337. >= conf->previous_raid_disks)
  5338. set_bit(In_sync, &rdev->flags);
  5339. else
  5340. rdev->recovery_offset = 0;
  5341. if (sysfs_link_rdev(mddev, rdev))
  5342. /* Failure here is OK */;
  5343. }
  5344. } else if (rdev->raid_disk >= conf->previous_raid_disks
  5345. && !test_bit(Faulty, &rdev->flags)) {
  5346. /* This is a spare that was manually added */
  5347. set_bit(In_sync, &rdev->flags);
  5348. }
  5349. /* When a reshape changes the number of devices,
  5350. * ->degraded is measured against the larger of the
  5351. * pre and post number of devices.
  5352. */
  5353. spin_lock_irqsave(&conf->device_lock, flags);
  5354. mddev->degraded = calc_degraded(conf);
  5355. spin_unlock_irqrestore(&conf->device_lock, flags);
  5356. }
  5357. mddev->raid_disks = conf->raid_disks;
  5358. mddev->reshape_position = conf->reshape_progress;
  5359. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5360. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  5361. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  5362. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  5363. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  5364. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  5365. "reshape");
  5366. if (!mddev->sync_thread) {
  5367. mddev->recovery = 0;
  5368. spin_lock_irq(&conf->device_lock);
  5369. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  5370. rdev_for_each(rdev, mddev)
  5371. rdev->new_data_offset = rdev->data_offset;
  5372. smp_wmb();
  5373. conf->reshape_progress = MaxSector;
  5374. mddev->reshape_position = MaxSector;
  5375. spin_unlock_irq(&conf->device_lock);
  5376. return -EAGAIN;
  5377. }
  5378. conf->reshape_checkpoint = jiffies;
  5379. md_wakeup_thread(mddev->sync_thread);
  5380. md_new_event(mddev);
  5381. return 0;
  5382. }
  5383. /* This is called from the reshape thread and should make any
  5384. * changes needed in 'conf'
  5385. */
  5386. static void end_reshape(struct r5conf *conf)
  5387. {
  5388. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  5389. struct md_rdev *rdev;
  5390. spin_lock_irq(&conf->device_lock);
  5391. conf->previous_raid_disks = conf->raid_disks;
  5392. rdev_for_each(rdev, conf->mddev)
  5393. rdev->data_offset = rdev->new_data_offset;
  5394. smp_wmb();
  5395. conf->reshape_progress = MaxSector;
  5396. spin_unlock_irq(&conf->device_lock);
  5397. wake_up(&conf->wait_for_overlap);
  5398. /* read-ahead size must cover two whole stripes, which is
  5399. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  5400. */
  5401. if (conf->mddev->queue) {
  5402. int data_disks = conf->raid_disks - conf->max_degraded;
  5403. int stripe = data_disks * ((conf->chunk_sectors << 9)
  5404. / PAGE_SIZE);
  5405. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  5406. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  5407. }
  5408. }
  5409. }
  5410. /* This is called from the raid5d thread with mddev_lock held.
  5411. * It makes config changes to the device.
  5412. */
  5413. static void raid5_finish_reshape(struct mddev *mddev)
  5414. {
  5415. struct r5conf *conf = mddev->private;
  5416. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  5417. if (mddev->delta_disks > 0) {
  5418. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  5419. set_capacity(mddev->gendisk, mddev->array_sectors);
  5420. revalidate_disk(mddev->gendisk);
  5421. } else {
  5422. int d;
  5423. spin_lock_irq(&conf->device_lock);
  5424. mddev->degraded = calc_degraded(conf);
  5425. spin_unlock_irq(&conf->device_lock);
  5426. for (d = conf->raid_disks ;
  5427. d < conf->raid_disks - mddev->delta_disks;
  5428. d++) {
  5429. struct md_rdev *rdev = conf->disks[d].rdev;
  5430. if (rdev)
  5431. clear_bit(In_sync, &rdev->flags);
  5432. rdev = conf->disks[d].replacement;
  5433. if (rdev)
  5434. clear_bit(In_sync, &rdev->flags);
  5435. }
  5436. }
  5437. mddev->layout = conf->algorithm;
  5438. mddev->chunk_sectors = conf->chunk_sectors;
  5439. mddev->reshape_position = MaxSector;
  5440. mddev->delta_disks = 0;
  5441. mddev->reshape_backwards = 0;
  5442. }
  5443. }
  5444. static void raid5_quiesce(struct mddev *mddev, int state)
  5445. {
  5446. struct r5conf *conf = mddev->private;
  5447. switch(state) {
  5448. case 2: /* resume for a suspend */
  5449. wake_up(&conf->wait_for_overlap);
  5450. break;
  5451. case 1: /* stop all writes */
  5452. spin_lock_irq(&conf->device_lock);
  5453. /* '2' tells resync/reshape to pause so that all
  5454. * active stripes can drain
  5455. */
  5456. conf->quiesce = 2;
  5457. wait_event_lock_irq(conf->wait_for_stripe,
  5458. atomic_read(&conf->active_stripes) == 0 &&
  5459. atomic_read(&conf->active_aligned_reads) == 0,
  5460. conf->device_lock, /* nothing */);
  5461. conf->quiesce = 1;
  5462. spin_unlock_irq(&conf->device_lock);
  5463. /* allow reshape to continue */
  5464. wake_up(&conf->wait_for_overlap);
  5465. break;
  5466. case 0: /* re-enable writes */
  5467. spin_lock_irq(&conf->device_lock);
  5468. conf->quiesce = 0;
  5469. wake_up(&conf->wait_for_stripe);
  5470. wake_up(&conf->wait_for_overlap);
  5471. spin_unlock_irq(&conf->device_lock);
  5472. break;
  5473. }
  5474. }
  5475. static void *raid45_takeover_raid0(struct mddev *mddev, int level)
  5476. {
  5477. struct r0conf *raid0_conf = mddev->private;
  5478. sector_t sectors;
  5479. /* for raid0 takeover only one zone is supported */
  5480. if (raid0_conf->nr_strip_zones > 1) {
  5481. printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  5482. mdname(mddev));
  5483. return ERR_PTR(-EINVAL);
  5484. }
  5485. sectors = raid0_conf->strip_zone[0].zone_end;
  5486. sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
  5487. mddev->dev_sectors = sectors;
  5488. mddev->new_level = level;
  5489. mddev->new_layout = ALGORITHM_PARITY_N;
  5490. mddev->new_chunk_sectors = mddev->chunk_sectors;
  5491. mddev->raid_disks += 1;
  5492. mddev->delta_disks = 1;
  5493. /* make sure it will be not marked as dirty */
  5494. mddev->recovery_cp = MaxSector;
  5495. return setup_conf(mddev);
  5496. }
  5497. static void *raid5_takeover_raid1(struct mddev *mddev)
  5498. {
  5499. int chunksect;
  5500. if (mddev->raid_disks != 2 ||
  5501. mddev->degraded > 1)
  5502. return ERR_PTR(-EINVAL);
  5503. /* Should check if there are write-behind devices? */
  5504. chunksect = 64*2; /* 64K by default */
  5505. /* The array must be an exact multiple of chunksize */
  5506. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  5507. chunksect >>= 1;
  5508. if ((chunksect<<9) < STRIPE_SIZE)
  5509. /* array size does not allow a suitable chunk size */
  5510. return ERR_PTR(-EINVAL);
  5511. mddev->new_level = 5;
  5512. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5513. mddev->new_chunk_sectors = chunksect;
  5514. return setup_conf(mddev);
  5515. }
  5516. static void *raid5_takeover_raid6(struct mddev *mddev)
  5517. {
  5518. int new_layout;
  5519. switch (mddev->layout) {
  5520. case ALGORITHM_LEFT_ASYMMETRIC_6:
  5521. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  5522. break;
  5523. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  5524. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  5525. break;
  5526. case ALGORITHM_LEFT_SYMMETRIC_6:
  5527. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5528. break;
  5529. case ALGORITHM_RIGHT_SYMMETRIC_6:
  5530. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  5531. break;
  5532. case ALGORITHM_PARITY_0_6:
  5533. new_layout = ALGORITHM_PARITY_0;
  5534. break;
  5535. case ALGORITHM_PARITY_N:
  5536. new_layout = ALGORITHM_PARITY_N;
  5537. break;
  5538. default:
  5539. return ERR_PTR(-EINVAL);
  5540. }
  5541. mddev->new_level = 5;
  5542. mddev->new_layout = new_layout;
  5543. mddev->delta_disks = -1;
  5544. mddev->raid_disks -= 1;
  5545. return setup_conf(mddev);
  5546. }
  5547. static int raid5_check_reshape(struct mddev *mddev)
  5548. {
  5549. /* For a 2-drive array, the layout and chunk size can be changed
  5550. * immediately as not restriping is needed.
  5551. * For larger arrays we record the new value - after validation
  5552. * to be used by a reshape pass.
  5553. */
  5554. struct r5conf *conf = mddev->private;
  5555. int new_chunk = mddev->new_chunk_sectors;
  5556. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  5557. return -EINVAL;
  5558. if (new_chunk > 0) {
  5559. if (!is_power_of_2(new_chunk))
  5560. return -EINVAL;
  5561. if (new_chunk < (PAGE_SIZE>>9))
  5562. return -EINVAL;
  5563. if (mddev->array_sectors & (new_chunk-1))
  5564. /* not factor of array size */
  5565. return -EINVAL;
  5566. }
  5567. /* They look valid */
  5568. if (mddev->raid_disks == 2) {
  5569. /* can make the change immediately */
  5570. if (mddev->new_layout >= 0) {
  5571. conf->algorithm = mddev->new_layout;
  5572. mddev->layout = mddev->new_layout;
  5573. }
  5574. if (new_chunk > 0) {
  5575. conf->chunk_sectors = new_chunk ;
  5576. mddev->chunk_sectors = new_chunk;
  5577. }
  5578. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5579. md_wakeup_thread(mddev->thread);
  5580. }
  5581. return check_reshape(mddev);
  5582. }
  5583. static int raid6_check_reshape(struct mddev *mddev)
  5584. {
  5585. int new_chunk = mddev->new_chunk_sectors;
  5586. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  5587. return -EINVAL;
  5588. if (new_chunk > 0) {
  5589. if (!is_power_of_2(new_chunk))
  5590. return -EINVAL;
  5591. if (new_chunk < (PAGE_SIZE >> 9))
  5592. return -EINVAL;
  5593. if (mddev->array_sectors & (new_chunk-1))
  5594. /* not factor of array size */
  5595. return -EINVAL;
  5596. }
  5597. /* They look valid */
  5598. return check_reshape(mddev);
  5599. }
  5600. static void *raid5_takeover(struct mddev *mddev)
  5601. {
  5602. /* raid5 can take over:
  5603. * raid0 - if there is only one strip zone - make it a raid4 layout
  5604. * raid1 - if there are two drives. We need to know the chunk size
  5605. * raid4 - trivial - just use a raid4 layout.
  5606. * raid6 - Providing it is a *_6 layout
  5607. */
  5608. if (mddev->level == 0)
  5609. return raid45_takeover_raid0(mddev, 5);
  5610. if (mddev->level == 1)
  5611. return raid5_takeover_raid1(mddev);
  5612. if (mddev->level == 4) {
  5613. mddev->new_layout = ALGORITHM_PARITY_N;
  5614. mddev->new_level = 5;
  5615. return setup_conf(mddev);
  5616. }
  5617. if (mddev->level == 6)
  5618. return raid5_takeover_raid6(mddev);
  5619. return ERR_PTR(-EINVAL);
  5620. }
  5621. static void *raid4_takeover(struct mddev *mddev)
  5622. {
  5623. /* raid4 can take over:
  5624. * raid0 - if there is only one strip zone
  5625. * raid5 - if layout is right
  5626. */
  5627. if (mddev->level == 0)
  5628. return raid45_takeover_raid0(mddev, 4);
  5629. if (mddev->level == 5 &&
  5630. mddev->layout == ALGORITHM_PARITY_N) {
  5631. mddev->new_layout = 0;
  5632. mddev->new_level = 4;
  5633. return setup_conf(mddev);
  5634. }
  5635. return ERR_PTR(-EINVAL);
  5636. }
  5637. static struct md_personality raid5_personality;
  5638. static void *raid6_takeover(struct mddev *mddev)
  5639. {
  5640. /* Currently can only take over a raid5. We map the
  5641. * personality to an equivalent raid6 personality
  5642. * with the Q block at the end.
  5643. */
  5644. int new_layout;
  5645. if (mddev->pers != &raid5_personality)
  5646. return ERR_PTR(-EINVAL);
  5647. if (mddev->degraded > 1)
  5648. return ERR_PTR(-EINVAL);
  5649. if (mddev->raid_disks > 253)
  5650. return ERR_PTR(-EINVAL);
  5651. if (mddev->raid_disks < 3)
  5652. return ERR_PTR(-EINVAL);
  5653. switch (mddev->layout) {
  5654. case ALGORITHM_LEFT_ASYMMETRIC:
  5655. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  5656. break;
  5657. case ALGORITHM_RIGHT_ASYMMETRIC:
  5658. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  5659. break;
  5660. case ALGORITHM_LEFT_SYMMETRIC:
  5661. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  5662. break;
  5663. case ALGORITHM_RIGHT_SYMMETRIC:
  5664. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  5665. break;
  5666. case ALGORITHM_PARITY_0:
  5667. new_layout = ALGORITHM_PARITY_0_6;
  5668. break;
  5669. case ALGORITHM_PARITY_N:
  5670. new_layout = ALGORITHM_PARITY_N;
  5671. break;
  5672. default:
  5673. return ERR_PTR(-EINVAL);
  5674. }
  5675. mddev->new_level = 6;
  5676. mddev->new_layout = new_layout;
  5677. mddev->delta_disks = 1;
  5678. mddev->raid_disks += 1;
  5679. return setup_conf(mddev);
  5680. }
  5681. static struct md_personality raid6_personality =
  5682. {
  5683. .name = "raid6",
  5684. .level = 6,
  5685. .owner = THIS_MODULE,
  5686. .make_request = make_request,
  5687. .run = run,
  5688. .stop = stop,
  5689. .status = status,
  5690. .error_handler = error,
  5691. .hot_add_disk = raid5_add_disk,
  5692. .hot_remove_disk= raid5_remove_disk,
  5693. .spare_active = raid5_spare_active,
  5694. .sync_request = sync_request,
  5695. .resize = raid5_resize,
  5696. .size = raid5_size,
  5697. .check_reshape = raid6_check_reshape,
  5698. .start_reshape = raid5_start_reshape,
  5699. .finish_reshape = raid5_finish_reshape,
  5700. .quiesce = raid5_quiesce,
  5701. .takeover = raid6_takeover,
  5702. };
  5703. static struct md_personality raid5_personality =
  5704. {
  5705. .name = "raid5",
  5706. .level = 5,
  5707. .owner = THIS_MODULE,
  5708. .make_request = make_request,
  5709. .run = run,
  5710. .stop = stop,
  5711. .status = status,
  5712. .error_handler = error,
  5713. .hot_add_disk = raid5_add_disk,
  5714. .hot_remove_disk= raid5_remove_disk,
  5715. .spare_active = raid5_spare_active,
  5716. .sync_request = sync_request,
  5717. .resize = raid5_resize,
  5718. .size = raid5_size,
  5719. .check_reshape = raid5_check_reshape,
  5720. .start_reshape = raid5_start_reshape,
  5721. .finish_reshape = raid5_finish_reshape,
  5722. .quiesce = raid5_quiesce,
  5723. .takeover = raid5_takeover,
  5724. };
  5725. static struct md_personality raid4_personality =
  5726. {
  5727. .name = "raid4",
  5728. .level = 4,
  5729. .owner = THIS_MODULE,
  5730. .make_request = make_request,
  5731. .run = run,
  5732. .stop = stop,
  5733. .status = status,
  5734. .error_handler = error,
  5735. .hot_add_disk = raid5_add_disk,
  5736. .hot_remove_disk= raid5_remove_disk,
  5737. .spare_active = raid5_spare_active,
  5738. .sync_request = sync_request,
  5739. .resize = raid5_resize,
  5740. .size = raid5_size,
  5741. .check_reshape = raid5_check_reshape,
  5742. .start_reshape = raid5_start_reshape,
  5743. .finish_reshape = raid5_finish_reshape,
  5744. .quiesce = raid5_quiesce,
  5745. .takeover = raid4_takeover,
  5746. };
  5747. static int __init raid5_init(void)
  5748. {
  5749. register_md_personality(&raid6_personality);
  5750. register_md_personality(&raid5_personality);
  5751. register_md_personality(&raid4_personality);
  5752. return 0;
  5753. }
  5754. static void raid5_exit(void)
  5755. {
  5756. unregister_md_personality(&raid6_personality);
  5757. unregister_md_personality(&raid5_personality);
  5758. unregister_md_personality(&raid4_personality);
  5759. }
  5760. module_init(raid5_init);
  5761. module_exit(raid5_exit);
  5762. MODULE_LICENSE("GPL");
  5763. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  5764. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  5765. MODULE_ALIAS("md-raid5");
  5766. MODULE_ALIAS("md-raid4");
  5767. MODULE_ALIAS("md-level-5");
  5768. MODULE_ALIAS("md-level-4");
  5769. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  5770. MODULE_ALIAS("md-raid6");
  5771. MODULE_ALIAS("md-level-6");
  5772. /* This used to be two separate modules, they were: */
  5773. MODULE_ALIAS("raid5");
  5774. MODULE_ALIAS("raid6");