raid5.c 174 KB

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