raid5.c 159 KB

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