raid5.c 171 KB

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