raid5.c 164 KB

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