raid5.c 168 KB

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