raid5.c 165 KB

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