raid5.c 183 KB

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