raid5.c 224 KB

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