raid5.c 197 KB

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