raid5.c 203 KB

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