raid5.c 172 KB

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