raid5.c 167 KB

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