raid5.c 172 KB

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