raid5.c 181 KB

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