raid5.c 161 KB

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