raid5.c 180 KB

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