raid5.c 149 KB

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