raid5.c 200 KB

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