raid5.c 200 KB

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