raid5.c 164 KB

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