raid5.c 172 KB

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