raid5.c 183 KB

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