raid5.c 167 KB

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