raid5.c 153 KB

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