raid5.c 164 KB

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