raid5.c 145 KB

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