raid5.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166
  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/async_tx.h>
  47. #include <linux/seq_file.h>
  48. #include "md.h"
  49. #include "raid5.h"
  50. #include "raid6.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. #if !RAID6_USE_EMPTY_ZERO_PAGE
  89. /* In .bss so it's zeroed */
  90. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  91. #endif
  92. /*
  93. * We maintain a biased count of active stripes in the bottom 16 bits of
  94. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  95. */
  96. static inline int raid5_bi_phys_segments(struct bio *bio)
  97. {
  98. return bio->bi_phys_segments & 0xffff;
  99. }
  100. static inline int raid5_bi_hw_segments(struct bio *bio)
  101. {
  102. return (bio->bi_phys_segments >> 16) & 0xffff;
  103. }
  104. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  105. {
  106. --bio->bi_phys_segments;
  107. return raid5_bi_phys_segments(bio);
  108. }
  109. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  110. {
  111. unsigned short val = raid5_bi_hw_segments(bio);
  112. --val;
  113. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  114. return val;
  115. }
  116. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  117. {
  118. bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
  119. }
  120. /* Find first data disk in a raid6 stripe */
  121. static inline int raid6_d0(struct stripe_head *sh)
  122. {
  123. if (sh->ddf_layout)
  124. /* ddf always start from first device */
  125. return 0;
  126. /* md starts just after Q block */
  127. if (sh->qd_idx == sh->disks - 1)
  128. return 0;
  129. else
  130. return sh->qd_idx + 1;
  131. }
  132. static inline int raid6_next_disk(int disk, int raid_disks)
  133. {
  134. disk++;
  135. return (disk < raid_disks) ? disk : 0;
  136. }
  137. /* When walking through the disks in a raid5, starting at raid6_d0,
  138. * We need to map each disk to a 'slot', where the data disks are slot
  139. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  140. * is raid_disks-1. This help does that mapping.
  141. */
  142. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  143. int *count, int syndrome_disks)
  144. {
  145. int slot;
  146. if (idx == sh->pd_idx)
  147. return syndrome_disks;
  148. if (idx == sh->qd_idx)
  149. return syndrome_disks + 1;
  150. slot = (*count)++;
  151. return slot;
  152. }
  153. static void return_io(struct bio *return_bi)
  154. {
  155. struct bio *bi = return_bi;
  156. while (bi) {
  157. return_bi = bi->bi_next;
  158. bi->bi_next = NULL;
  159. bi->bi_size = 0;
  160. bio_endio(bi, 0);
  161. bi = return_bi;
  162. }
  163. }
  164. static void print_raid5_conf (raid5_conf_t *conf);
  165. static int stripe_operations_active(struct stripe_head *sh)
  166. {
  167. return sh->check_state || sh->reconstruct_state ||
  168. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  169. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  170. }
  171. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  172. {
  173. if (atomic_dec_and_test(&sh->count)) {
  174. BUG_ON(!list_empty(&sh->lru));
  175. BUG_ON(atomic_read(&conf->active_stripes)==0);
  176. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  177. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  178. list_add_tail(&sh->lru, &conf->delayed_list);
  179. blk_plug_device(conf->mddev->queue);
  180. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  181. sh->bm_seq - conf->seq_write > 0) {
  182. list_add_tail(&sh->lru, &conf->bitmap_list);
  183. blk_plug_device(conf->mddev->queue);
  184. } else {
  185. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  186. list_add_tail(&sh->lru, &conf->handle_list);
  187. }
  188. md_wakeup_thread(conf->mddev->thread);
  189. } else {
  190. BUG_ON(stripe_operations_active(sh));
  191. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  192. atomic_dec(&conf->preread_active_stripes);
  193. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  194. md_wakeup_thread(conf->mddev->thread);
  195. }
  196. atomic_dec(&conf->active_stripes);
  197. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  198. list_add_tail(&sh->lru, &conf->inactive_list);
  199. wake_up(&conf->wait_for_stripe);
  200. if (conf->retry_read_aligned)
  201. md_wakeup_thread(conf->mddev->thread);
  202. }
  203. }
  204. }
  205. }
  206. static void release_stripe(struct stripe_head *sh)
  207. {
  208. raid5_conf_t *conf = sh->raid_conf;
  209. unsigned long flags;
  210. spin_lock_irqsave(&conf->device_lock, flags);
  211. __release_stripe(conf, sh);
  212. spin_unlock_irqrestore(&conf->device_lock, flags);
  213. }
  214. static inline void remove_hash(struct stripe_head *sh)
  215. {
  216. pr_debug("remove_hash(), stripe %llu\n",
  217. (unsigned long long)sh->sector);
  218. hlist_del_init(&sh->hash);
  219. }
  220. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  221. {
  222. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  223. pr_debug("insert_hash(), stripe %llu\n",
  224. (unsigned long long)sh->sector);
  225. CHECK_DEVLOCK();
  226. hlist_add_head(&sh->hash, hp);
  227. }
  228. /* find an idle stripe, make sure it is unhashed, and return it. */
  229. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  230. {
  231. struct stripe_head *sh = NULL;
  232. struct list_head *first;
  233. CHECK_DEVLOCK();
  234. if (list_empty(&conf->inactive_list))
  235. goto out;
  236. first = conf->inactive_list.next;
  237. sh = list_entry(first, struct stripe_head, lru);
  238. list_del_init(first);
  239. remove_hash(sh);
  240. atomic_inc(&conf->active_stripes);
  241. out:
  242. return sh;
  243. }
  244. static void shrink_buffers(struct stripe_head *sh, int num)
  245. {
  246. struct page *p;
  247. int i;
  248. for (i=0; i<num ; i++) {
  249. p = sh->dev[i].page;
  250. if (!p)
  251. continue;
  252. sh->dev[i].page = NULL;
  253. put_page(p);
  254. }
  255. }
  256. static int grow_buffers(struct stripe_head *sh, int num)
  257. {
  258. int i;
  259. for (i=0; i<num; i++) {
  260. struct page *page;
  261. if (!(page = alloc_page(GFP_KERNEL))) {
  262. return 1;
  263. }
  264. sh->dev[i].page = page;
  265. }
  266. return 0;
  267. }
  268. static void raid5_build_block(struct stripe_head *sh, int i);
  269. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  270. struct stripe_head *sh);
  271. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  272. {
  273. raid5_conf_t *conf = sh->raid_conf;
  274. int i;
  275. BUG_ON(atomic_read(&sh->count) != 0);
  276. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  277. BUG_ON(stripe_operations_active(sh));
  278. CHECK_DEVLOCK();
  279. pr_debug("init_stripe called, stripe %llu\n",
  280. (unsigned long long)sh->sector);
  281. remove_hash(sh);
  282. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  283. sh->sector = sector;
  284. stripe_set_idx(sector, conf, previous, sh);
  285. sh->state = 0;
  286. for (i = sh->disks; i--; ) {
  287. struct r5dev *dev = &sh->dev[i];
  288. if (dev->toread || dev->read || dev->towrite || dev->written ||
  289. test_bit(R5_LOCKED, &dev->flags)) {
  290. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  291. (unsigned long long)sh->sector, i, dev->toread,
  292. dev->read, dev->towrite, dev->written,
  293. test_bit(R5_LOCKED, &dev->flags));
  294. BUG();
  295. }
  296. dev->flags = 0;
  297. raid5_build_block(sh, i);
  298. }
  299. insert_hash(conf, sh);
  300. }
  301. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  302. {
  303. struct stripe_head *sh;
  304. struct hlist_node *hn;
  305. CHECK_DEVLOCK();
  306. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  307. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  308. if (sh->sector == sector && sh->disks == disks)
  309. return sh;
  310. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  311. return NULL;
  312. }
  313. static void unplug_slaves(mddev_t *mddev);
  314. static void raid5_unplug_device(struct request_queue *q);
  315. static struct stripe_head *
  316. get_active_stripe(raid5_conf_t *conf, sector_t sector,
  317. int previous, int noblock)
  318. {
  319. struct stripe_head *sh;
  320. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  321. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  322. spin_lock_irq(&conf->device_lock);
  323. do {
  324. wait_event_lock_irq(conf->wait_for_stripe,
  325. conf->quiesce == 0,
  326. conf->device_lock, /* nothing */);
  327. sh = __find_stripe(conf, sector, disks);
  328. if (!sh) {
  329. if (!conf->inactive_blocked)
  330. sh = get_free_stripe(conf);
  331. if (noblock && sh == NULL)
  332. break;
  333. if (!sh) {
  334. conf->inactive_blocked = 1;
  335. wait_event_lock_irq(conf->wait_for_stripe,
  336. !list_empty(&conf->inactive_list) &&
  337. (atomic_read(&conf->active_stripes)
  338. < (conf->max_nr_stripes *3/4)
  339. || !conf->inactive_blocked),
  340. conf->device_lock,
  341. raid5_unplug_device(conf->mddev->queue)
  342. );
  343. conf->inactive_blocked = 0;
  344. } else
  345. init_stripe(sh, sector, previous);
  346. } else {
  347. if (atomic_read(&sh->count)) {
  348. BUG_ON(!list_empty(&sh->lru));
  349. } else {
  350. if (!test_bit(STRIPE_HANDLE, &sh->state))
  351. atomic_inc(&conf->active_stripes);
  352. if (list_empty(&sh->lru) &&
  353. !test_bit(STRIPE_EXPANDING, &sh->state))
  354. BUG();
  355. list_del_init(&sh->lru);
  356. }
  357. }
  358. } while (sh == NULL);
  359. if (sh)
  360. atomic_inc(&sh->count);
  361. spin_unlock_irq(&conf->device_lock);
  362. return sh;
  363. }
  364. static void
  365. raid5_end_read_request(struct bio *bi, int error);
  366. static void
  367. raid5_end_write_request(struct bio *bi, int error);
  368. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  369. {
  370. raid5_conf_t *conf = sh->raid_conf;
  371. int i, disks = sh->disks;
  372. might_sleep();
  373. for (i = disks; i--; ) {
  374. int rw;
  375. struct bio *bi;
  376. mdk_rdev_t *rdev;
  377. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  378. rw = WRITE;
  379. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  380. rw = READ;
  381. else
  382. continue;
  383. bi = &sh->dev[i].req;
  384. bi->bi_rw = rw;
  385. if (rw == WRITE)
  386. bi->bi_end_io = raid5_end_write_request;
  387. else
  388. bi->bi_end_io = raid5_end_read_request;
  389. rcu_read_lock();
  390. rdev = rcu_dereference(conf->disks[i].rdev);
  391. if (rdev && test_bit(Faulty, &rdev->flags))
  392. rdev = NULL;
  393. if (rdev)
  394. atomic_inc(&rdev->nr_pending);
  395. rcu_read_unlock();
  396. if (rdev) {
  397. if (s->syncing || s->expanding || s->expanded)
  398. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  399. set_bit(STRIPE_IO_STARTED, &sh->state);
  400. bi->bi_bdev = rdev->bdev;
  401. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  402. __func__, (unsigned long long)sh->sector,
  403. bi->bi_rw, i);
  404. atomic_inc(&sh->count);
  405. bi->bi_sector = sh->sector + rdev->data_offset;
  406. bi->bi_flags = 1 << BIO_UPTODATE;
  407. bi->bi_vcnt = 1;
  408. bi->bi_max_vecs = 1;
  409. bi->bi_idx = 0;
  410. bi->bi_io_vec = &sh->dev[i].vec;
  411. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  412. bi->bi_io_vec[0].bv_offset = 0;
  413. bi->bi_size = STRIPE_SIZE;
  414. bi->bi_next = NULL;
  415. if (rw == WRITE &&
  416. test_bit(R5_ReWrite, &sh->dev[i].flags))
  417. atomic_add(STRIPE_SECTORS,
  418. &rdev->corrected_errors);
  419. generic_make_request(bi);
  420. } else {
  421. if (rw == WRITE)
  422. set_bit(STRIPE_DEGRADED, &sh->state);
  423. pr_debug("skip op %ld on disc %d for sector %llu\n",
  424. bi->bi_rw, i, (unsigned long long)sh->sector);
  425. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  426. set_bit(STRIPE_HANDLE, &sh->state);
  427. }
  428. }
  429. }
  430. static struct dma_async_tx_descriptor *
  431. async_copy_data(int frombio, struct bio *bio, struct page *page,
  432. sector_t sector, struct dma_async_tx_descriptor *tx)
  433. {
  434. struct bio_vec *bvl;
  435. struct page *bio_page;
  436. int i;
  437. int page_offset;
  438. if (bio->bi_sector >= sector)
  439. page_offset = (signed)(bio->bi_sector - sector) * 512;
  440. else
  441. page_offset = (signed)(sector - bio->bi_sector) * -512;
  442. bio_for_each_segment(bvl, bio, i) {
  443. int len = bio_iovec_idx(bio, i)->bv_len;
  444. int clen;
  445. int b_offset = 0;
  446. if (page_offset < 0) {
  447. b_offset = -page_offset;
  448. page_offset += b_offset;
  449. len -= b_offset;
  450. }
  451. if (len > 0 && page_offset + len > STRIPE_SIZE)
  452. clen = STRIPE_SIZE - page_offset;
  453. else
  454. clen = len;
  455. if (clen > 0) {
  456. b_offset += bio_iovec_idx(bio, i)->bv_offset;
  457. bio_page = bio_iovec_idx(bio, i)->bv_page;
  458. if (frombio)
  459. tx = async_memcpy(page, bio_page, page_offset,
  460. b_offset, clen,
  461. ASYNC_TX_DEP_ACK,
  462. tx, NULL, NULL);
  463. else
  464. tx = async_memcpy(bio_page, page, b_offset,
  465. page_offset, clen,
  466. ASYNC_TX_DEP_ACK,
  467. tx, NULL, NULL);
  468. }
  469. if (clen < len) /* hit end of page */
  470. break;
  471. page_offset += len;
  472. }
  473. return tx;
  474. }
  475. static void ops_complete_biofill(void *stripe_head_ref)
  476. {
  477. struct stripe_head *sh = stripe_head_ref;
  478. struct bio *return_bi = NULL;
  479. raid5_conf_t *conf = sh->raid_conf;
  480. int i;
  481. pr_debug("%s: stripe %llu\n", __func__,
  482. (unsigned long long)sh->sector);
  483. /* clear completed biofills */
  484. spin_lock_irq(&conf->device_lock);
  485. for (i = sh->disks; i--; ) {
  486. struct r5dev *dev = &sh->dev[i];
  487. /* acknowledge completion of a biofill operation */
  488. /* and check if we need to reply to a read request,
  489. * new R5_Wantfill requests are held off until
  490. * !STRIPE_BIOFILL_RUN
  491. */
  492. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  493. struct bio *rbi, *rbi2;
  494. BUG_ON(!dev->read);
  495. rbi = dev->read;
  496. dev->read = NULL;
  497. while (rbi && rbi->bi_sector <
  498. dev->sector + STRIPE_SECTORS) {
  499. rbi2 = r5_next_bio(rbi, dev->sector);
  500. if (!raid5_dec_bi_phys_segments(rbi)) {
  501. rbi->bi_next = return_bi;
  502. return_bi = rbi;
  503. }
  504. rbi = rbi2;
  505. }
  506. }
  507. }
  508. spin_unlock_irq(&conf->device_lock);
  509. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  510. return_io(return_bi);
  511. set_bit(STRIPE_HANDLE, &sh->state);
  512. release_stripe(sh);
  513. }
  514. static void ops_run_biofill(struct stripe_head *sh)
  515. {
  516. struct dma_async_tx_descriptor *tx = NULL;
  517. raid5_conf_t *conf = sh->raid_conf;
  518. int i;
  519. pr_debug("%s: stripe %llu\n", __func__,
  520. (unsigned long long)sh->sector);
  521. for (i = sh->disks; i--; ) {
  522. struct r5dev *dev = &sh->dev[i];
  523. if (test_bit(R5_Wantfill, &dev->flags)) {
  524. struct bio *rbi;
  525. spin_lock_irq(&conf->device_lock);
  526. dev->read = rbi = dev->toread;
  527. dev->toread = NULL;
  528. spin_unlock_irq(&conf->device_lock);
  529. while (rbi && rbi->bi_sector <
  530. dev->sector + STRIPE_SECTORS) {
  531. tx = async_copy_data(0, rbi, dev->page,
  532. dev->sector, tx);
  533. rbi = r5_next_bio(rbi, dev->sector);
  534. }
  535. }
  536. }
  537. atomic_inc(&sh->count);
  538. async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
  539. ops_complete_biofill, sh);
  540. }
  541. static void ops_complete_compute5(void *stripe_head_ref)
  542. {
  543. struct stripe_head *sh = stripe_head_ref;
  544. int target = sh->ops.target;
  545. struct r5dev *tgt = &sh->dev[target];
  546. pr_debug("%s: stripe %llu\n", __func__,
  547. (unsigned long long)sh->sector);
  548. set_bit(R5_UPTODATE, &tgt->flags);
  549. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  550. clear_bit(R5_Wantcompute, &tgt->flags);
  551. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  552. if (sh->check_state == check_state_compute_run)
  553. sh->check_state = check_state_compute_result;
  554. set_bit(STRIPE_HANDLE, &sh->state);
  555. release_stripe(sh);
  556. }
  557. static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
  558. {
  559. /* kernel stack size limits the total number of disks */
  560. int disks = sh->disks;
  561. struct page *xor_srcs[disks];
  562. int target = sh->ops.target;
  563. struct r5dev *tgt = &sh->dev[target];
  564. struct page *xor_dest = tgt->page;
  565. int count = 0;
  566. struct dma_async_tx_descriptor *tx;
  567. int i;
  568. pr_debug("%s: stripe %llu block: %d\n",
  569. __func__, (unsigned long long)sh->sector, target);
  570. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  571. for (i = disks; i--; )
  572. if (i != target)
  573. xor_srcs[count++] = sh->dev[i].page;
  574. atomic_inc(&sh->count);
  575. if (unlikely(count == 1))
  576. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  577. 0, NULL, ops_complete_compute5, sh);
  578. else
  579. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  580. ASYNC_TX_XOR_ZERO_DST, NULL,
  581. ops_complete_compute5, sh);
  582. return tx;
  583. }
  584. static void ops_complete_prexor(void *stripe_head_ref)
  585. {
  586. struct stripe_head *sh = stripe_head_ref;
  587. pr_debug("%s: stripe %llu\n", __func__,
  588. (unsigned long long)sh->sector);
  589. }
  590. static struct dma_async_tx_descriptor *
  591. ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  592. {
  593. /* kernel stack size limits the total number of disks */
  594. int disks = sh->disks;
  595. struct page *xor_srcs[disks];
  596. int count = 0, pd_idx = sh->pd_idx, i;
  597. /* existing parity data subtracted */
  598. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  599. pr_debug("%s: stripe %llu\n", __func__,
  600. (unsigned long long)sh->sector);
  601. for (i = disks; i--; ) {
  602. struct r5dev *dev = &sh->dev[i];
  603. /* Only process blocks that are known to be uptodate */
  604. if (test_bit(R5_Wantdrain, &dev->flags))
  605. xor_srcs[count++] = dev->page;
  606. }
  607. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  608. ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
  609. ops_complete_prexor, sh);
  610. return tx;
  611. }
  612. static struct dma_async_tx_descriptor *
  613. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  614. {
  615. int disks = sh->disks;
  616. int i;
  617. pr_debug("%s: stripe %llu\n", __func__,
  618. (unsigned long long)sh->sector);
  619. for (i = disks; i--; ) {
  620. struct r5dev *dev = &sh->dev[i];
  621. struct bio *chosen;
  622. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  623. struct bio *wbi;
  624. spin_lock(&sh->lock);
  625. chosen = dev->towrite;
  626. dev->towrite = NULL;
  627. BUG_ON(dev->written);
  628. wbi = dev->written = chosen;
  629. spin_unlock(&sh->lock);
  630. while (wbi && wbi->bi_sector <
  631. dev->sector + STRIPE_SECTORS) {
  632. tx = async_copy_data(1, wbi, dev->page,
  633. dev->sector, tx);
  634. wbi = r5_next_bio(wbi, dev->sector);
  635. }
  636. }
  637. }
  638. return tx;
  639. }
  640. static void ops_complete_postxor(void *stripe_head_ref)
  641. {
  642. struct stripe_head *sh = stripe_head_ref;
  643. int disks = sh->disks, i, pd_idx = sh->pd_idx;
  644. pr_debug("%s: stripe %llu\n", __func__,
  645. (unsigned long long)sh->sector);
  646. for (i = disks; i--; ) {
  647. struct r5dev *dev = &sh->dev[i];
  648. if (dev->written || i == pd_idx)
  649. set_bit(R5_UPTODATE, &dev->flags);
  650. }
  651. if (sh->reconstruct_state == reconstruct_state_drain_run)
  652. sh->reconstruct_state = reconstruct_state_drain_result;
  653. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  654. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  655. else {
  656. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  657. sh->reconstruct_state = reconstruct_state_result;
  658. }
  659. set_bit(STRIPE_HANDLE, &sh->state);
  660. release_stripe(sh);
  661. }
  662. static void
  663. ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  664. {
  665. /* kernel stack size limits the total number of disks */
  666. int disks = sh->disks;
  667. struct page *xor_srcs[disks];
  668. int count = 0, pd_idx = sh->pd_idx, i;
  669. struct page *xor_dest;
  670. int prexor = 0;
  671. unsigned long flags;
  672. pr_debug("%s: stripe %llu\n", __func__,
  673. (unsigned long long)sh->sector);
  674. /* check if prexor is active which means only process blocks
  675. * that are part of a read-modify-write (written)
  676. */
  677. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  678. prexor = 1;
  679. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  680. for (i = disks; i--; ) {
  681. struct r5dev *dev = &sh->dev[i];
  682. if (dev->written)
  683. xor_srcs[count++] = dev->page;
  684. }
  685. } else {
  686. xor_dest = sh->dev[pd_idx].page;
  687. for (i = disks; i--; ) {
  688. struct r5dev *dev = &sh->dev[i];
  689. if (i != pd_idx)
  690. xor_srcs[count++] = dev->page;
  691. }
  692. }
  693. /* 1/ if we prexor'd then the dest is reused as a source
  694. * 2/ if we did not prexor then we are redoing the parity
  695. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  696. * for the synchronous xor case
  697. */
  698. flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
  699. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  700. atomic_inc(&sh->count);
  701. if (unlikely(count == 1)) {
  702. flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
  703. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  704. flags, tx, ops_complete_postxor, sh);
  705. } else
  706. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  707. flags, tx, ops_complete_postxor, sh);
  708. }
  709. static void ops_complete_check(void *stripe_head_ref)
  710. {
  711. struct stripe_head *sh = stripe_head_ref;
  712. pr_debug("%s: stripe %llu\n", __func__,
  713. (unsigned long long)sh->sector);
  714. sh->check_state = check_state_check_result;
  715. set_bit(STRIPE_HANDLE, &sh->state);
  716. release_stripe(sh);
  717. }
  718. static void ops_run_check(struct stripe_head *sh)
  719. {
  720. /* kernel stack size limits the total number of disks */
  721. int disks = sh->disks;
  722. struct page *xor_srcs[disks];
  723. struct dma_async_tx_descriptor *tx;
  724. int count = 0, pd_idx = sh->pd_idx, i;
  725. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  726. pr_debug("%s: stripe %llu\n", __func__,
  727. (unsigned long long)sh->sector);
  728. for (i = disks; i--; ) {
  729. struct r5dev *dev = &sh->dev[i];
  730. if (i != pd_idx)
  731. xor_srcs[count++] = dev->page;
  732. }
  733. tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  734. &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
  735. atomic_inc(&sh->count);
  736. tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
  737. ops_complete_check, sh);
  738. }
  739. static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
  740. {
  741. int overlap_clear = 0, i, disks = sh->disks;
  742. struct dma_async_tx_descriptor *tx = NULL;
  743. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  744. ops_run_biofill(sh);
  745. overlap_clear++;
  746. }
  747. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  748. tx = ops_run_compute5(sh);
  749. /* terminate the chain if postxor is not set to be run */
  750. if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
  751. async_tx_ack(tx);
  752. }
  753. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  754. tx = ops_run_prexor(sh, tx);
  755. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  756. tx = ops_run_biodrain(sh, tx);
  757. overlap_clear++;
  758. }
  759. if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
  760. ops_run_postxor(sh, tx);
  761. if (test_bit(STRIPE_OP_CHECK, &ops_request))
  762. ops_run_check(sh);
  763. if (overlap_clear)
  764. for (i = disks; i--; ) {
  765. struct r5dev *dev = &sh->dev[i];
  766. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  767. wake_up(&sh->raid_conf->wait_for_overlap);
  768. }
  769. }
  770. static int grow_one_stripe(raid5_conf_t *conf)
  771. {
  772. struct stripe_head *sh;
  773. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  774. if (!sh)
  775. return 0;
  776. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  777. sh->raid_conf = conf;
  778. spin_lock_init(&sh->lock);
  779. if (grow_buffers(sh, conf->raid_disks)) {
  780. shrink_buffers(sh, conf->raid_disks);
  781. kmem_cache_free(conf->slab_cache, sh);
  782. return 0;
  783. }
  784. sh->disks = conf->raid_disks;
  785. /* we just created an active stripe so... */
  786. atomic_set(&sh->count, 1);
  787. atomic_inc(&conf->active_stripes);
  788. INIT_LIST_HEAD(&sh->lru);
  789. release_stripe(sh);
  790. return 1;
  791. }
  792. static int grow_stripes(raid5_conf_t *conf, int num)
  793. {
  794. struct kmem_cache *sc;
  795. int devs = conf->raid_disks;
  796. sprintf(conf->cache_name[0],
  797. "raid%d-%s", conf->level, mdname(conf->mddev));
  798. sprintf(conf->cache_name[1],
  799. "raid%d-%s-alt", conf->level, mdname(conf->mddev));
  800. conf->active_name = 0;
  801. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  802. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  803. 0, 0, NULL);
  804. if (!sc)
  805. return 1;
  806. conf->slab_cache = sc;
  807. conf->pool_size = devs;
  808. while (num--)
  809. if (!grow_one_stripe(conf))
  810. return 1;
  811. return 0;
  812. }
  813. #ifdef CONFIG_MD_RAID5_RESHAPE
  814. static int resize_stripes(raid5_conf_t *conf, int newsize)
  815. {
  816. /* Make all the stripes able to hold 'newsize' devices.
  817. * New slots in each stripe get 'page' set to a new page.
  818. *
  819. * This happens in stages:
  820. * 1/ create a new kmem_cache and allocate the required number of
  821. * stripe_heads.
  822. * 2/ gather all the old stripe_heads and tranfer the pages across
  823. * to the new stripe_heads. This will have the side effect of
  824. * freezing the array as once all stripe_heads have been collected,
  825. * no IO will be possible. Old stripe heads are freed once their
  826. * pages have been transferred over, and the old kmem_cache is
  827. * freed when all stripes are done.
  828. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  829. * we simple return a failre status - no need to clean anything up.
  830. * 4/ allocate new pages for the new slots in the new stripe_heads.
  831. * If this fails, we don't bother trying the shrink the
  832. * stripe_heads down again, we just leave them as they are.
  833. * As each stripe_head is processed the new one is released into
  834. * active service.
  835. *
  836. * Once step2 is started, we cannot afford to wait for a write,
  837. * so we use GFP_NOIO allocations.
  838. */
  839. struct stripe_head *osh, *nsh;
  840. LIST_HEAD(newstripes);
  841. struct disk_info *ndisks;
  842. int err;
  843. struct kmem_cache *sc;
  844. int i;
  845. if (newsize <= conf->pool_size)
  846. return 0; /* never bother to shrink */
  847. err = md_allow_write(conf->mddev);
  848. if (err)
  849. return err;
  850. /* Step 1 */
  851. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  852. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  853. 0, 0, NULL);
  854. if (!sc)
  855. return -ENOMEM;
  856. for (i = conf->max_nr_stripes; i; i--) {
  857. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  858. if (!nsh)
  859. break;
  860. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  861. nsh->raid_conf = conf;
  862. spin_lock_init(&nsh->lock);
  863. list_add(&nsh->lru, &newstripes);
  864. }
  865. if (i) {
  866. /* didn't get enough, give up */
  867. while (!list_empty(&newstripes)) {
  868. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  869. list_del(&nsh->lru);
  870. kmem_cache_free(sc, nsh);
  871. }
  872. kmem_cache_destroy(sc);
  873. return -ENOMEM;
  874. }
  875. /* Step 2 - Must use GFP_NOIO now.
  876. * OK, we have enough stripes, start collecting inactive
  877. * stripes and copying them over
  878. */
  879. list_for_each_entry(nsh, &newstripes, lru) {
  880. spin_lock_irq(&conf->device_lock);
  881. wait_event_lock_irq(conf->wait_for_stripe,
  882. !list_empty(&conf->inactive_list),
  883. conf->device_lock,
  884. unplug_slaves(conf->mddev)
  885. );
  886. osh = get_free_stripe(conf);
  887. spin_unlock_irq(&conf->device_lock);
  888. atomic_set(&nsh->count, 1);
  889. for(i=0; i<conf->pool_size; i++)
  890. nsh->dev[i].page = osh->dev[i].page;
  891. for( ; i<newsize; i++)
  892. nsh->dev[i].page = NULL;
  893. kmem_cache_free(conf->slab_cache, osh);
  894. }
  895. kmem_cache_destroy(conf->slab_cache);
  896. /* Step 3.
  897. * At this point, we are holding all the stripes so the array
  898. * is completely stalled, so now is a good time to resize
  899. * conf->disks.
  900. */
  901. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  902. if (ndisks) {
  903. for (i=0; i<conf->raid_disks; i++)
  904. ndisks[i] = conf->disks[i];
  905. kfree(conf->disks);
  906. conf->disks = ndisks;
  907. } else
  908. err = -ENOMEM;
  909. /* Step 4, return new stripes to service */
  910. while(!list_empty(&newstripes)) {
  911. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  912. list_del_init(&nsh->lru);
  913. for (i=conf->raid_disks; i < newsize; i++)
  914. if (nsh->dev[i].page == NULL) {
  915. struct page *p = alloc_page(GFP_NOIO);
  916. nsh->dev[i].page = p;
  917. if (!p)
  918. err = -ENOMEM;
  919. }
  920. release_stripe(nsh);
  921. }
  922. /* critical section pass, GFP_NOIO no longer needed */
  923. conf->slab_cache = sc;
  924. conf->active_name = 1-conf->active_name;
  925. conf->pool_size = newsize;
  926. return err;
  927. }
  928. #endif
  929. static int drop_one_stripe(raid5_conf_t *conf)
  930. {
  931. struct stripe_head *sh;
  932. spin_lock_irq(&conf->device_lock);
  933. sh = get_free_stripe(conf);
  934. spin_unlock_irq(&conf->device_lock);
  935. if (!sh)
  936. return 0;
  937. BUG_ON(atomic_read(&sh->count));
  938. shrink_buffers(sh, conf->pool_size);
  939. kmem_cache_free(conf->slab_cache, sh);
  940. atomic_dec(&conf->active_stripes);
  941. return 1;
  942. }
  943. static void shrink_stripes(raid5_conf_t *conf)
  944. {
  945. while (drop_one_stripe(conf))
  946. ;
  947. if (conf->slab_cache)
  948. kmem_cache_destroy(conf->slab_cache);
  949. conf->slab_cache = NULL;
  950. }
  951. static void raid5_end_read_request(struct bio * bi, int error)
  952. {
  953. struct stripe_head *sh = bi->bi_private;
  954. raid5_conf_t *conf = sh->raid_conf;
  955. int disks = sh->disks, i;
  956. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  957. char b[BDEVNAME_SIZE];
  958. mdk_rdev_t *rdev;
  959. for (i=0 ; i<disks; i++)
  960. if (bi == &sh->dev[i].req)
  961. break;
  962. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  963. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  964. uptodate);
  965. if (i == disks) {
  966. BUG();
  967. return;
  968. }
  969. if (uptodate) {
  970. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  971. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  972. rdev = conf->disks[i].rdev;
  973. printk_rl(KERN_INFO "raid5:%s: read error corrected"
  974. " (%lu sectors at %llu on %s)\n",
  975. mdname(conf->mddev), STRIPE_SECTORS,
  976. (unsigned long long)(sh->sector
  977. + rdev->data_offset),
  978. bdevname(rdev->bdev, b));
  979. clear_bit(R5_ReadError, &sh->dev[i].flags);
  980. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  981. }
  982. if (atomic_read(&conf->disks[i].rdev->read_errors))
  983. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  984. } else {
  985. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  986. int retry = 0;
  987. rdev = conf->disks[i].rdev;
  988. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  989. atomic_inc(&rdev->read_errors);
  990. if (conf->mddev->degraded)
  991. printk_rl(KERN_WARNING
  992. "raid5:%s: read error not correctable "
  993. "(sector %llu on %s).\n",
  994. mdname(conf->mddev),
  995. (unsigned long long)(sh->sector
  996. + rdev->data_offset),
  997. bdn);
  998. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  999. /* Oh, no!!! */
  1000. printk_rl(KERN_WARNING
  1001. "raid5:%s: read error NOT corrected!! "
  1002. "(sector %llu on %s).\n",
  1003. mdname(conf->mddev),
  1004. (unsigned long long)(sh->sector
  1005. + rdev->data_offset),
  1006. bdn);
  1007. else if (atomic_read(&rdev->read_errors)
  1008. > conf->max_nr_stripes)
  1009. printk(KERN_WARNING
  1010. "raid5:%s: Too many read errors, failing device %s.\n",
  1011. mdname(conf->mddev), bdn);
  1012. else
  1013. retry = 1;
  1014. if (retry)
  1015. set_bit(R5_ReadError, &sh->dev[i].flags);
  1016. else {
  1017. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1018. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1019. md_error(conf->mddev, rdev);
  1020. }
  1021. }
  1022. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1023. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1024. set_bit(STRIPE_HANDLE, &sh->state);
  1025. release_stripe(sh);
  1026. }
  1027. static void raid5_end_write_request(struct bio *bi, int error)
  1028. {
  1029. struct stripe_head *sh = bi->bi_private;
  1030. raid5_conf_t *conf = sh->raid_conf;
  1031. int disks = sh->disks, i;
  1032. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1033. for (i=0 ; i<disks; i++)
  1034. if (bi == &sh->dev[i].req)
  1035. break;
  1036. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1037. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1038. uptodate);
  1039. if (i == disks) {
  1040. BUG();
  1041. return;
  1042. }
  1043. if (!uptodate)
  1044. md_error(conf->mddev, conf->disks[i].rdev);
  1045. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1046. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1047. set_bit(STRIPE_HANDLE, &sh->state);
  1048. release_stripe(sh);
  1049. }
  1050. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  1051. static void raid5_build_block(struct stripe_head *sh, int i)
  1052. {
  1053. struct r5dev *dev = &sh->dev[i];
  1054. bio_init(&dev->req);
  1055. dev->req.bi_io_vec = &dev->vec;
  1056. dev->req.bi_vcnt++;
  1057. dev->req.bi_max_vecs++;
  1058. dev->vec.bv_page = dev->page;
  1059. dev->vec.bv_len = STRIPE_SIZE;
  1060. dev->vec.bv_offset = 0;
  1061. dev->req.bi_sector = sh->sector;
  1062. dev->req.bi_private = sh;
  1063. dev->flags = 0;
  1064. dev->sector = compute_blocknr(sh, i);
  1065. }
  1066. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1067. {
  1068. char b[BDEVNAME_SIZE];
  1069. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1070. pr_debug("raid5: error called\n");
  1071. if (!test_bit(Faulty, &rdev->flags)) {
  1072. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1073. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1074. unsigned long flags;
  1075. spin_lock_irqsave(&conf->device_lock, flags);
  1076. mddev->degraded++;
  1077. spin_unlock_irqrestore(&conf->device_lock, flags);
  1078. /*
  1079. * if recovery was running, make sure it aborts.
  1080. */
  1081. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1082. }
  1083. set_bit(Faulty, &rdev->flags);
  1084. printk(KERN_ALERT
  1085. "raid5: Disk failure on %s, disabling device.\n"
  1086. "raid5: Operation continuing on %d devices.\n",
  1087. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  1088. }
  1089. }
  1090. /*
  1091. * Input: a 'big' sector number,
  1092. * Output: index of the data and parity disk, and the sector # in them.
  1093. */
  1094. static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
  1095. int previous, int *dd_idx,
  1096. struct stripe_head *sh)
  1097. {
  1098. long stripe;
  1099. unsigned long chunk_number;
  1100. unsigned int chunk_offset;
  1101. int pd_idx, qd_idx;
  1102. int ddf_layout = 0;
  1103. sector_t new_sector;
  1104. int sectors_per_chunk = conf->chunk_size >> 9;
  1105. int raid_disks = previous ? conf->previous_raid_disks
  1106. : conf->raid_disks;
  1107. int data_disks = raid_disks - conf->max_degraded;
  1108. /* First compute the information on this sector */
  1109. /*
  1110. * Compute the chunk number and the sector offset inside the chunk
  1111. */
  1112. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1113. chunk_number = r_sector;
  1114. BUG_ON(r_sector != chunk_number);
  1115. /*
  1116. * Compute the stripe number
  1117. */
  1118. stripe = chunk_number / data_disks;
  1119. /*
  1120. * Compute the data disk and parity disk indexes inside the stripe
  1121. */
  1122. *dd_idx = chunk_number % data_disks;
  1123. /*
  1124. * Select the parity disk based on the user selected algorithm.
  1125. */
  1126. pd_idx = qd_idx = ~0;
  1127. switch(conf->level) {
  1128. case 4:
  1129. pd_idx = data_disks;
  1130. break;
  1131. case 5:
  1132. switch (conf->algorithm) {
  1133. case ALGORITHM_LEFT_ASYMMETRIC:
  1134. pd_idx = data_disks - stripe % raid_disks;
  1135. if (*dd_idx >= pd_idx)
  1136. (*dd_idx)++;
  1137. break;
  1138. case ALGORITHM_RIGHT_ASYMMETRIC:
  1139. pd_idx = stripe % raid_disks;
  1140. if (*dd_idx >= pd_idx)
  1141. (*dd_idx)++;
  1142. break;
  1143. case ALGORITHM_LEFT_SYMMETRIC:
  1144. pd_idx = data_disks - stripe % raid_disks;
  1145. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1146. break;
  1147. case ALGORITHM_RIGHT_SYMMETRIC:
  1148. pd_idx = stripe % raid_disks;
  1149. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1150. break;
  1151. case ALGORITHM_PARITY_0:
  1152. pd_idx = 0;
  1153. (*dd_idx)++;
  1154. break;
  1155. case ALGORITHM_PARITY_N:
  1156. pd_idx = data_disks;
  1157. break;
  1158. default:
  1159. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1160. conf->algorithm);
  1161. BUG();
  1162. }
  1163. break;
  1164. case 6:
  1165. switch (conf->algorithm) {
  1166. case ALGORITHM_LEFT_ASYMMETRIC:
  1167. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1168. qd_idx = pd_idx + 1;
  1169. if (pd_idx == raid_disks-1) {
  1170. (*dd_idx)++; /* Q D D D P */
  1171. qd_idx = 0;
  1172. } else if (*dd_idx >= pd_idx)
  1173. (*dd_idx) += 2; /* D D P Q D */
  1174. break;
  1175. case ALGORITHM_RIGHT_ASYMMETRIC:
  1176. pd_idx = stripe % raid_disks;
  1177. qd_idx = pd_idx + 1;
  1178. if (pd_idx == raid_disks-1) {
  1179. (*dd_idx)++; /* Q D D D P */
  1180. qd_idx = 0;
  1181. } else if (*dd_idx >= pd_idx)
  1182. (*dd_idx) += 2; /* D D P Q D */
  1183. break;
  1184. case ALGORITHM_LEFT_SYMMETRIC:
  1185. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1186. qd_idx = (pd_idx + 1) % raid_disks;
  1187. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1188. break;
  1189. case ALGORITHM_RIGHT_SYMMETRIC:
  1190. pd_idx = stripe % raid_disks;
  1191. qd_idx = (pd_idx + 1) % raid_disks;
  1192. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1193. break;
  1194. case ALGORITHM_PARITY_0:
  1195. pd_idx = 0;
  1196. qd_idx = 1;
  1197. (*dd_idx) += 2;
  1198. break;
  1199. case ALGORITHM_PARITY_N:
  1200. pd_idx = data_disks;
  1201. qd_idx = data_disks + 1;
  1202. break;
  1203. case ALGORITHM_ROTATING_ZERO_RESTART:
  1204. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1205. * of blocks for computing Q is different.
  1206. */
  1207. pd_idx = stripe % raid_disks;
  1208. qd_idx = pd_idx + 1;
  1209. if (pd_idx == raid_disks-1) {
  1210. (*dd_idx)++; /* Q D D D P */
  1211. qd_idx = 0;
  1212. } else if (*dd_idx >= pd_idx)
  1213. (*dd_idx) += 2; /* D D P Q D */
  1214. ddf_layout = 1;
  1215. break;
  1216. case ALGORITHM_ROTATING_N_RESTART:
  1217. /* Same a left_asymmetric, by first stripe is
  1218. * D D D P Q rather than
  1219. * Q D D D P
  1220. */
  1221. pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
  1222. qd_idx = pd_idx + 1;
  1223. if (pd_idx == raid_disks-1) {
  1224. (*dd_idx)++; /* Q D D D P */
  1225. qd_idx = 0;
  1226. } else if (*dd_idx >= pd_idx)
  1227. (*dd_idx) += 2; /* D D P Q D */
  1228. ddf_layout = 1;
  1229. break;
  1230. case ALGORITHM_ROTATING_N_CONTINUE:
  1231. /* Same as left_symmetric but Q is before P */
  1232. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1233. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1234. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1235. ddf_layout = 1;
  1236. break;
  1237. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1238. /* RAID5 left_asymmetric, with Q on last device */
  1239. pd_idx = data_disks - stripe % (raid_disks-1);
  1240. if (*dd_idx >= pd_idx)
  1241. (*dd_idx)++;
  1242. qd_idx = raid_disks - 1;
  1243. break;
  1244. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1245. pd_idx = stripe % (raid_disks-1);
  1246. if (*dd_idx >= pd_idx)
  1247. (*dd_idx)++;
  1248. qd_idx = raid_disks - 1;
  1249. break;
  1250. case ALGORITHM_LEFT_SYMMETRIC_6:
  1251. pd_idx = data_disks - stripe % (raid_disks-1);
  1252. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1253. qd_idx = raid_disks - 1;
  1254. break;
  1255. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1256. pd_idx = stripe % (raid_disks-1);
  1257. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1258. qd_idx = raid_disks - 1;
  1259. break;
  1260. case ALGORITHM_PARITY_0_6:
  1261. pd_idx = 0;
  1262. (*dd_idx)++;
  1263. qd_idx = raid_disks - 1;
  1264. break;
  1265. default:
  1266. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1267. conf->algorithm);
  1268. BUG();
  1269. }
  1270. break;
  1271. }
  1272. if (sh) {
  1273. sh->pd_idx = pd_idx;
  1274. sh->qd_idx = qd_idx;
  1275. sh->ddf_layout = ddf_layout;
  1276. }
  1277. /*
  1278. * Finally, compute the new sector number
  1279. */
  1280. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1281. return new_sector;
  1282. }
  1283. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  1284. {
  1285. raid5_conf_t *conf = sh->raid_conf;
  1286. int raid_disks = sh->disks;
  1287. int data_disks = raid_disks - conf->max_degraded;
  1288. sector_t new_sector = sh->sector, check;
  1289. int sectors_per_chunk = conf->chunk_size >> 9;
  1290. sector_t stripe;
  1291. int chunk_offset;
  1292. int chunk_number, dummy1, dd_idx = i;
  1293. sector_t r_sector;
  1294. struct stripe_head sh2;
  1295. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1296. stripe = new_sector;
  1297. BUG_ON(new_sector != stripe);
  1298. if (i == sh->pd_idx)
  1299. return 0;
  1300. switch(conf->level) {
  1301. case 4: break;
  1302. case 5:
  1303. switch (conf->algorithm) {
  1304. case ALGORITHM_LEFT_ASYMMETRIC:
  1305. case ALGORITHM_RIGHT_ASYMMETRIC:
  1306. if (i > sh->pd_idx)
  1307. i--;
  1308. break;
  1309. case ALGORITHM_LEFT_SYMMETRIC:
  1310. case ALGORITHM_RIGHT_SYMMETRIC:
  1311. if (i < sh->pd_idx)
  1312. i += raid_disks;
  1313. i -= (sh->pd_idx + 1);
  1314. break;
  1315. case ALGORITHM_PARITY_0:
  1316. i -= 1;
  1317. break;
  1318. case ALGORITHM_PARITY_N:
  1319. break;
  1320. default:
  1321. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1322. conf->algorithm);
  1323. BUG();
  1324. }
  1325. break;
  1326. case 6:
  1327. if (i == sh->qd_idx)
  1328. return 0; /* It is the Q disk */
  1329. switch (conf->algorithm) {
  1330. case ALGORITHM_LEFT_ASYMMETRIC:
  1331. case ALGORITHM_RIGHT_ASYMMETRIC:
  1332. case ALGORITHM_ROTATING_ZERO_RESTART:
  1333. case ALGORITHM_ROTATING_N_RESTART:
  1334. if (sh->pd_idx == raid_disks-1)
  1335. i--; /* Q D D D P */
  1336. else if (i > sh->pd_idx)
  1337. i -= 2; /* D D P Q D */
  1338. break;
  1339. case ALGORITHM_LEFT_SYMMETRIC:
  1340. case ALGORITHM_RIGHT_SYMMETRIC:
  1341. if (sh->pd_idx == raid_disks-1)
  1342. i--; /* Q D D D P */
  1343. else {
  1344. /* D D P Q D */
  1345. if (i < sh->pd_idx)
  1346. i += raid_disks;
  1347. i -= (sh->pd_idx + 2);
  1348. }
  1349. break;
  1350. case ALGORITHM_PARITY_0:
  1351. i -= 2;
  1352. break;
  1353. case ALGORITHM_PARITY_N:
  1354. break;
  1355. case ALGORITHM_ROTATING_N_CONTINUE:
  1356. if (sh->pd_idx == 0)
  1357. i--; /* P D D D Q */
  1358. else if (i > sh->pd_idx)
  1359. i -= 2; /* D D Q P D */
  1360. break;
  1361. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1362. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1363. if (i > sh->pd_idx)
  1364. i--;
  1365. break;
  1366. case ALGORITHM_LEFT_SYMMETRIC_6:
  1367. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1368. if (i < sh->pd_idx)
  1369. i += data_disks + 1;
  1370. i -= (sh->pd_idx + 1);
  1371. break;
  1372. case ALGORITHM_PARITY_0_6:
  1373. i -= 1;
  1374. break;
  1375. default:
  1376. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1377. conf->algorithm);
  1378. BUG();
  1379. }
  1380. break;
  1381. }
  1382. chunk_number = stripe * data_disks + i;
  1383. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  1384. check = raid5_compute_sector(conf, r_sector,
  1385. (raid_disks != conf->raid_disks),
  1386. &dummy1, &sh2);
  1387. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1388. || sh2.qd_idx != sh->qd_idx) {
  1389. printk(KERN_ERR "compute_blocknr: map not correct\n");
  1390. return 0;
  1391. }
  1392. return r_sector;
  1393. }
  1394. /*
  1395. * Copy data between a page in the stripe cache, and one or more bion
  1396. * The page could align with the middle of the bio, or there could be
  1397. * several bion, each with several bio_vecs, which cover part of the page
  1398. * Multiple bion are linked together on bi_next. There may be extras
  1399. * at the end of this list. We ignore them.
  1400. */
  1401. static void copy_data(int frombio, struct bio *bio,
  1402. struct page *page,
  1403. sector_t sector)
  1404. {
  1405. char *pa = page_address(page);
  1406. struct bio_vec *bvl;
  1407. int i;
  1408. int page_offset;
  1409. if (bio->bi_sector >= sector)
  1410. page_offset = (signed)(bio->bi_sector - sector) * 512;
  1411. else
  1412. page_offset = (signed)(sector - bio->bi_sector) * -512;
  1413. bio_for_each_segment(bvl, bio, i) {
  1414. int len = bio_iovec_idx(bio,i)->bv_len;
  1415. int clen;
  1416. int b_offset = 0;
  1417. if (page_offset < 0) {
  1418. b_offset = -page_offset;
  1419. page_offset += b_offset;
  1420. len -= b_offset;
  1421. }
  1422. if (len > 0 && page_offset + len > STRIPE_SIZE)
  1423. clen = STRIPE_SIZE - page_offset;
  1424. else clen = len;
  1425. if (clen > 0) {
  1426. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  1427. if (frombio)
  1428. memcpy(pa+page_offset, ba+b_offset, clen);
  1429. else
  1430. memcpy(ba+b_offset, pa+page_offset, clen);
  1431. __bio_kunmap_atomic(ba, KM_USER0);
  1432. }
  1433. if (clen < len) /* hit end of page */
  1434. break;
  1435. page_offset += len;
  1436. }
  1437. }
  1438. #define check_xor() do { \
  1439. if (count == MAX_XOR_BLOCKS) { \
  1440. xor_blocks(count, STRIPE_SIZE, dest, ptr);\
  1441. count = 0; \
  1442. } \
  1443. } while(0)
  1444. static void compute_parity6(struct stripe_head *sh, int method)
  1445. {
  1446. raid5_conf_t *conf = sh->raid_conf;
  1447. int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  1448. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  1449. struct bio *chosen;
  1450. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1451. void *ptrs[syndrome_disks+2];
  1452. pd_idx = sh->pd_idx;
  1453. qd_idx = sh->qd_idx;
  1454. d0_idx = raid6_d0(sh);
  1455. pr_debug("compute_parity, stripe %llu, method %d\n",
  1456. (unsigned long long)sh->sector, method);
  1457. switch(method) {
  1458. case READ_MODIFY_WRITE:
  1459. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  1460. case RECONSTRUCT_WRITE:
  1461. for (i= disks; i-- ;)
  1462. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  1463. chosen = sh->dev[i].towrite;
  1464. sh->dev[i].towrite = NULL;
  1465. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1466. wake_up(&conf->wait_for_overlap);
  1467. BUG_ON(sh->dev[i].written);
  1468. sh->dev[i].written = chosen;
  1469. }
  1470. break;
  1471. case CHECK_PARITY:
  1472. BUG(); /* Not implemented yet */
  1473. }
  1474. for (i = disks; i--;)
  1475. if (sh->dev[i].written) {
  1476. sector_t sector = sh->dev[i].sector;
  1477. struct bio *wbi = sh->dev[i].written;
  1478. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  1479. copy_data(1, wbi, sh->dev[i].page, sector);
  1480. wbi = r5_next_bio(wbi, sector);
  1481. }
  1482. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1483. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1484. }
  1485. /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
  1486. for (i = 0; i < disks; i++)
  1487. ptrs[i] = (void *)raid6_empty_zero_page;
  1488. count = 0;
  1489. i = d0_idx;
  1490. do {
  1491. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1492. ptrs[slot] = page_address(sh->dev[i].page);
  1493. if (slot < syndrome_disks &&
  1494. !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  1495. printk(KERN_ERR "block %d/%d not uptodate "
  1496. "on parity calc\n", i, count);
  1497. BUG();
  1498. }
  1499. i = raid6_next_disk(i, disks);
  1500. } while (i != d0_idx);
  1501. BUG_ON(count != syndrome_disks);
  1502. raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
  1503. switch(method) {
  1504. case RECONSTRUCT_WRITE:
  1505. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1506. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1507. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1508. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1509. break;
  1510. case UPDATE_PARITY:
  1511. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1512. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1513. break;
  1514. }
  1515. }
  1516. /* Compute one missing block */
  1517. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1518. {
  1519. int i, count, disks = sh->disks;
  1520. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  1521. int qd_idx = sh->qd_idx;
  1522. pr_debug("compute_block_1, stripe %llu, idx %d\n",
  1523. (unsigned long long)sh->sector, dd_idx);
  1524. if ( dd_idx == qd_idx ) {
  1525. /* We're actually computing the Q drive */
  1526. compute_parity6(sh, UPDATE_PARITY);
  1527. } else {
  1528. dest = page_address(sh->dev[dd_idx].page);
  1529. if (!nozero) memset(dest, 0, STRIPE_SIZE);
  1530. count = 0;
  1531. for (i = disks ; i--; ) {
  1532. if (i == dd_idx || i == qd_idx)
  1533. continue;
  1534. p = page_address(sh->dev[i].page);
  1535. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1536. ptr[count++] = p;
  1537. else
  1538. printk("compute_block() %d, stripe %llu, %d"
  1539. " not present\n", dd_idx,
  1540. (unsigned long long)sh->sector, i);
  1541. check_xor();
  1542. }
  1543. if (count)
  1544. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  1545. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1546. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1547. }
  1548. }
  1549. /* Compute two missing blocks */
  1550. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1551. {
  1552. int i, count, disks = sh->disks;
  1553. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  1554. int d0_idx = raid6_d0(sh);
  1555. int faila = -1, failb = -1;
  1556. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1557. void *ptrs[syndrome_disks+2];
  1558. for (i = 0; i < disks ; i++)
  1559. ptrs[i] = (void *)raid6_empty_zero_page;
  1560. count = 0;
  1561. i = d0_idx;
  1562. do {
  1563. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1564. ptrs[slot] = page_address(sh->dev[i].page);
  1565. if (i == dd_idx1)
  1566. faila = slot;
  1567. if (i == dd_idx2)
  1568. failb = slot;
  1569. i = raid6_next_disk(i, disks);
  1570. } while (i != d0_idx);
  1571. BUG_ON(count != syndrome_disks);
  1572. BUG_ON(faila == failb);
  1573. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1574. pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1575. (unsigned long long)sh->sector, dd_idx1, dd_idx2,
  1576. faila, failb);
  1577. if (failb == syndrome_disks+1) {
  1578. /* Q disk is one of the missing disks */
  1579. if (faila == syndrome_disks) {
  1580. /* Missing P+Q, just recompute */
  1581. compute_parity6(sh, UPDATE_PARITY);
  1582. return;
  1583. } else {
  1584. /* We're missing D+Q; recompute D from P */
  1585. compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
  1586. dd_idx2 : dd_idx1),
  1587. 0);
  1588. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1589. return;
  1590. }
  1591. }
  1592. /* We're missing D+P or D+D; */
  1593. if (failb == syndrome_disks) {
  1594. /* We're missing D+P. */
  1595. raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
  1596. } else {
  1597. /* We're missing D+D. */
  1598. raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
  1599. ptrs);
  1600. }
  1601. /* Both the above update both missing blocks */
  1602. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1603. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1604. }
  1605. static void
  1606. schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
  1607. int rcw, int expand)
  1608. {
  1609. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1610. if (rcw) {
  1611. /* if we are not expanding this is a proper write request, and
  1612. * there will be bios with new data to be drained into the
  1613. * stripe cache
  1614. */
  1615. if (!expand) {
  1616. sh->reconstruct_state = reconstruct_state_drain_run;
  1617. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1618. } else
  1619. sh->reconstruct_state = reconstruct_state_run;
  1620. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1621. for (i = disks; i--; ) {
  1622. struct r5dev *dev = &sh->dev[i];
  1623. if (dev->towrite) {
  1624. set_bit(R5_LOCKED, &dev->flags);
  1625. set_bit(R5_Wantdrain, &dev->flags);
  1626. if (!expand)
  1627. clear_bit(R5_UPTODATE, &dev->flags);
  1628. s->locked++;
  1629. }
  1630. }
  1631. if (s->locked + 1 == disks)
  1632. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1633. atomic_inc(&sh->raid_conf->pending_full_writes);
  1634. } else {
  1635. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1636. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1637. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1638. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1639. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1640. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1641. for (i = disks; i--; ) {
  1642. struct r5dev *dev = &sh->dev[i];
  1643. if (i == pd_idx)
  1644. continue;
  1645. if (dev->towrite &&
  1646. (test_bit(R5_UPTODATE, &dev->flags) ||
  1647. test_bit(R5_Wantcompute, &dev->flags))) {
  1648. set_bit(R5_Wantdrain, &dev->flags);
  1649. set_bit(R5_LOCKED, &dev->flags);
  1650. clear_bit(R5_UPTODATE, &dev->flags);
  1651. s->locked++;
  1652. }
  1653. }
  1654. }
  1655. /* keep the parity disk locked while asynchronous operations
  1656. * are in flight
  1657. */
  1658. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1659. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1660. s->locked++;
  1661. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1662. __func__, (unsigned long long)sh->sector,
  1663. s->locked, s->ops_request);
  1664. }
  1665. /*
  1666. * Each stripe/dev can have one or more bion attached.
  1667. * toread/towrite point to the first in a chain.
  1668. * The bi_next chain must be in order.
  1669. */
  1670. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1671. {
  1672. struct bio **bip;
  1673. raid5_conf_t *conf = sh->raid_conf;
  1674. int firstwrite=0;
  1675. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1676. (unsigned long long)bi->bi_sector,
  1677. (unsigned long long)sh->sector);
  1678. spin_lock(&sh->lock);
  1679. spin_lock_irq(&conf->device_lock);
  1680. if (forwrite) {
  1681. bip = &sh->dev[dd_idx].towrite;
  1682. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1683. firstwrite = 1;
  1684. } else
  1685. bip = &sh->dev[dd_idx].toread;
  1686. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1687. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1688. goto overlap;
  1689. bip = & (*bip)->bi_next;
  1690. }
  1691. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1692. goto overlap;
  1693. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1694. if (*bip)
  1695. bi->bi_next = *bip;
  1696. *bip = bi;
  1697. bi->bi_phys_segments++;
  1698. spin_unlock_irq(&conf->device_lock);
  1699. spin_unlock(&sh->lock);
  1700. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1701. (unsigned long long)bi->bi_sector,
  1702. (unsigned long long)sh->sector, dd_idx);
  1703. if (conf->mddev->bitmap && firstwrite) {
  1704. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1705. STRIPE_SECTORS, 0);
  1706. sh->bm_seq = conf->seq_flush+1;
  1707. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1708. }
  1709. if (forwrite) {
  1710. /* check if page is covered */
  1711. sector_t sector = sh->dev[dd_idx].sector;
  1712. for (bi=sh->dev[dd_idx].towrite;
  1713. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1714. bi && bi->bi_sector <= sector;
  1715. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1716. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1717. sector = bi->bi_sector + (bi->bi_size>>9);
  1718. }
  1719. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1720. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1721. }
  1722. return 1;
  1723. overlap:
  1724. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1725. spin_unlock_irq(&conf->device_lock);
  1726. spin_unlock(&sh->lock);
  1727. return 0;
  1728. }
  1729. static void end_reshape(raid5_conf_t *conf);
  1730. static int page_is_zero(struct page *p)
  1731. {
  1732. char *a = page_address(p);
  1733. return ((*(u32*)a) == 0 &&
  1734. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1735. }
  1736. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  1737. struct stripe_head *sh)
  1738. {
  1739. int sectors_per_chunk = 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 = r6s->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 = r6s->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 && (!r6s || i != r6s->qd_idx)) {
  2364. int dd_idx, j;
  2365. struct stripe_head *sh2;
  2366. sector_t bn = compute_blocknr(sh, i);
  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;
  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. r6s.qd_idx = sh->qd_idx;
  2660. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2661. "pd_idx=%d, qd_idx=%d\n",
  2662. (unsigned long long)sh->sector, sh->state,
  2663. atomic_read(&sh->count), pd_idx, r6s.qd_idx);
  2664. memset(&s, 0, sizeof(s));
  2665. spin_lock(&sh->lock);
  2666. clear_bit(STRIPE_HANDLE, &sh->state);
  2667. clear_bit(STRIPE_DELAYED, &sh->state);
  2668. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2669. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2670. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2671. /* Now to look around and see what can be done */
  2672. rcu_read_lock();
  2673. for (i=disks; i--; ) {
  2674. mdk_rdev_t *rdev;
  2675. dev = &sh->dev[i];
  2676. clear_bit(R5_Insync, &dev->flags);
  2677. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2678. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2679. /* maybe we can reply to a read */
  2680. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  2681. struct bio *rbi, *rbi2;
  2682. pr_debug("Return read for disc %d\n", i);
  2683. spin_lock_irq(&conf->device_lock);
  2684. rbi = dev->toread;
  2685. dev->toread = NULL;
  2686. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  2687. wake_up(&conf->wait_for_overlap);
  2688. spin_unlock_irq(&conf->device_lock);
  2689. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  2690. copy_data(0, rbi, dev->page, dev->sector);
  2691. rbi2 = r5_next_bio(rbi, dev->sector);
  2692. spin_lock_irq(&conf->device_lock);
  2693. if (!raid5_dec_bi_phys_segments(rbi)) {
  2694. rbi->bi_next = return_bi;
  2695. return_bi = rbi;
  2696. }
  2697. spin_unlock_irq(&conf->device_lock);
  2698. rbi = rbi2;
  2699. }
  2700. }
  2701. /* now count some things */
  2702. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2703. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2704. if (dev->toread)
  2705. s.to_read++;
  2706. if (dev->towrite) {
  2707. s.to_write++;
  2708. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2709. s.non_overwrite++;
  2710. }
  2711. if (dev->written)
  2712. s.written++;
  2713. rdev = rcu_dereference(conf->disks[i].rdev);
  2714. if (blocked_rdev == NULL &&
  2715. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2716. blocked_rdev = rdev;
  2717. atomic_inc(&rdev->nr_pending);
  2718. }
  2719. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2720. /* The ReadError flag will just be confusing now */
  2721. clear_bit(R5_ReadError, &dev->flags);
  2722. clear_bit(R5_ReWrite, &dev->flags);
  2723. }
  2724. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2725. || test_bit(R5_ReadError, &dev->flags)) {
  2726. if (s.failed < 2)
  2727. r6s.failed_num[s.failed] = i;
  2728. s.failed++;
  2729. } else
  2730. set_bit(R5_Insync, &dev->flags);
  2731. }
  2732. rcu_read_unlock();
  2733. if (unlikely(blocked_rdev)) {
  2734. if (s.syncing || s.expanding || s.expanded ||
  2735. s.to_write || s.written) {
  2736. set_bit(STRIPE_HANDLE, &sh->state);
  2737. goto unlock;
  2738. }
  2739. /* There is nothing for the blocked_rdev to block */
  2740. rdev_dec_pending(blocked_rdev, conf->mddev);
  2741. blocked_rdev = NULL;
  2742. }
  2743. pr_debug("locked=%d uptodate=%d to_read=%d"
  2744. " to_write=%d failed=%d failed_num=%d,%d\n",
  2745. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2746. r6s.failed_num[0], r6s.failed_num[1]);
  2747. /* check if the array has lost >2 devices and, if so, some requests
  2748. * might need to be failed
  2749. */
  2750. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2751. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2752. if (s.failed > 2 && s.syncing) {
  2753. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2754. clear_bit(STRIPE_SYNCING, &sh->state);
  2755. s.syncing = 0;
  2756. }
  2757. /*
  2758. * might be able to return some write requests if the parity blocks
  2759. * are safe, or on a failed drive
  2760. */
  2761. pdev = &sh->dev[pd_idx];
  2762. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2763. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2764. qdev = &sh->dev[r6s.qd_idx];
  2765. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
  2766. || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
  2767. if ( s.written &&
  2768. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2769. && !test_bit(R5_LOCKED, &pdev->flags)
  2770. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2771. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2772. && !test_bit(R5_LOCKED, &qdev->flags)
  2773. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2774. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2775. /* Now we might consider reading some blocks, either to check/generate
  2776. * parity, or to satisfy requests
  2777. * or to load a block that is being partially written.
  2778. */
  2779. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2780. (s.syncing && (s.uptodate < disks)) || s.expanding)
  2781. handle_stripe_fill6(sh, &s, &r6s, disks);
  2782. /* now to consider writing and what else, if anything should be read */
  2783. if (s.to_write)
  2784. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  2785. /* maybe we need to check and possibly fix the parity for this stripe
  2786. * Any reads will already have been scheduled, so we just see if enough
  2787. * data is available
  2788. */
  2789. if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
  2790. handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
  2791. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2792. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2793. clear_bit(STRIPE_SYNCING, &sh->state);
  2794. }
  2795. /* If the failed drives are just a ReadError, then we might need
  2796. * to progress the repair/check process
  2797. */
  2798. if (s.failed <= 2 && !conf->mddev->ro)
  2799. for (i = 0; i < s.failed; i++) {
  2800. dev = &sh->dev[r6s.failed_num[i]];
  2801. if (test_bit(R5_ReadError, &dev->flags)
  2802. && !test_bit(R5_LOCKED, &dev->flags)
  2803. && test_bit(R5_UPTODATE, &dev->flags)
  2804. ) {
  2805. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2806. set_bit(R5_Wantwrite, &dev->flags);
  2807. set_bit(R5_ReWrite, &dev->flags);
  2808. set_bit(R5_LOCKED, &dev->flags);
  2809. } else {
  2810. /* let's read it back */
  2811. set_bit(R5_Wantread, &dev->flags);
  2812. set_bit(R5_LOCKED, &dev->flags);
  2813. }
  2814. }
  2815. }
  2816. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2817. /* Need to write out all blocks after computing P&Q */
  2818. sh->disks = conf->raid_disks;
  2819. stripe_set_idx(sh->sector, conf, 0, sh);
  2820. compute_parity6(sh, RECONSTRUCT_WRITE);
  2821. for (i = conf->raid_disks ; i-- ; ) {
  2822. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2823. s.locked++;
  2824. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2825. }
  2826. clear_bit(STRIPE_EXPANDING, &sh->state);
  2827. } else if (s.expanded) {
  2828. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2829. atomic_dec(&conf->reshape_stripes);
  2830. wake_up(&conf->wait_for_overlap);
  2831. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2832. }
  2833. if (s.expanding && s.locked == 0 &&
  2834. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2835. handle_stripe_expansion(conf, sh, &r6s);
  2836. unlock:
  2837. spin_unlock(&sh->lock);
  2838. /* wait for this device to become unblocked */
  2839. if (unlikely(blocked_rdev))
  2840. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2841. ops_run_io(sh, &s);
  2842. return_io(return_bi);
  2843. return blocked_rdev == NULL;
  2844. }
  2845. /* returns true if the stripe was handled */
  2846. static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2847. {
  2848. if (sh->raid_conf->level == 6)
  2849. return handle_stripe6(sh, tmp_page);
  2850. else
  2851. return handle_stripe5(sh);
  2852. }
  2853. static void raid5_activate_delayed(raid5_conf_t *conf)
  2854. {
  2855. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2856. while (!list_empty(&conf->delayed_list)) {
  2857. struct list_head *l = conf->delayed_list.next;
  2858. struct stripe_head *sh;
  2859. sh = list_entry(l, struct stripe_head, lru);
  2860. list_del_init(l);
  2861. clear_bit(STRIPE_DELAYED, &sh->state);
  2862. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2863. atomic_inc(&conf->preread_active_stripes);
  2864. list_add_tail(&sh->lru, &conf->hold_list);
  2865. }
  2866. } else
  2867. blk_plug_device(conf->mddev->queue);
  2868. }
  2869. static void activate_bit_delay(raid5_conf_t *conf)
  2870. {
  2871. /* device_lock is held */
  2872. struct list_head head;
  2873. list_add(&head, &conf->bitmap_list);
  2874. list_del_init(&conf->bitmap_list);
  2875. while (!list_empty(&head)) {
  2876. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2877. list_del_init(&sh->lru);
  2878. atomic_inc(&sh->count);
  2879. __release_stripe(conf, sh);
  2880. }
  2881. }
  2882. static void unplug_slaves(mddev_t *mddev)
  2883. {
  2884. raid5_conf_t *conf = mddev_to_conf(mddev);
  2885. int i;
  2886. rcu_read_lock();
  2887. for (i=0; i<mddev->raid_disks; i++) {
  2888. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2889. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2890. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  2891. atomic_inc(&rdev->nr_pending);
  2892. rcu_read_unlock();
  2893. blk_unplug(r_queue);
  2894. rdev_dec_pending(rdev, mddev);
  2895. rcu_read_lock();
  2896. }
  2897. }
  2898. rcu_read_unlock();
  2899. }
  2900. static void raid5_unplug_device(struct request_queue *q)
  2901. {
  2902. mddev_t *mddev = q->queuedata;
  2903. raid5_conf_t *conf = mddev_to_conf(mddev);
  2904. unsigned long flags;
  2905. spin_lock_irqsave(&conf->device_lock, flags);
  2906. if (blk_remove_plug(q)) {
  2907. conf->seq_flush++;
  2908. raid5_activate_delayed(conf);
  2909. }
  2910. md_wakeup_thread(mddev->thread);
  2911. spin_unlock_irqrestore(&conf->device_lock, flags);
  2912. unplug_slaves(mddev);
  2913. }
  2914. static int raid5_congested(void *data, int bits)
  2915. {
  2916. mddev_t *mddev = data;
  2917. raid5_conf_t *conf = mddev_to_conf(mddev);
  2918. /* No difference between reads and writes. Just check
  2919. * how busy the stripe_cache is
  2920. */
  2921. if (conf->inactive_blocked)
  2922. return 1;
  2923. if (conf->quiesce)
  2924. return 1;
  2925. if (list_empty_careful(&conf->inactive_list))
  2926. return 1;
  2927. return 0;
  2928. }
  2929. /* We want read requests to align with chunks where possible,
  2930. * but write requests don't need to.
  2931. */
  2932. static int raid5_mergeable_bvec(struct request_queue *q,
  2933. struct bvec_merge_data *bvm,
  2934. struct bio_vec *biovec)
  2935. {
  2936. mddev_t *mddev = q->queuedata;
  2937. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  2938. int max;
  2939. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2940. unsigned int bio_sectors = bvm->bi_size >> 9;
  2941. if ((bvm->bi_rw & 1) == WRITE)
  2942. return biovec->bv_len; /* always allow writes to be mergeable */
  2943. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2944. if (max < 0) max = 0;
  2945. if (max <= biovec->bv_len && bio_sectors == 0)
  2946. return biovec->bv_len;
  2947. else
  2948. return max;
  2949. }
  2950. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2951. {
  2952. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2953. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2954. unsigned int bio_sectors = bio->bi_size >> 9;
  2955. return chunk_sectors >=
  2956. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2957. }
  2958. /*
  2959. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2960. * later sampled by raid5d.
  2961. */
  2962. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2963. {
  2964. unsigned long flags;
  2965. spin_lock_irqsave(&conf->device_lock, flags);
  2966. bi->bi_next = conf->retry_read_aligned_list;
  2967. conf->retry_read_aligned_list = bi;
  2968. spin_unlock_irqrestore(&conf->device_lock, flags);
  2969. md_wakeup_thread(conf->mddev->thread);
  2970. }
  2971. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  2972. {
  2973. struct bio *bi;
  2974. bi = conf->retry_read_aligned;
  2975. if (bi) {
  2976. conf->retry_read_aligned = NULL;
  2977. return bi;
  2978. }
  2979. bi = conf->retry_read_aligned_list;
  2980. if(bi) {
  2981. conf->retry_read_aligned_list = bi->bi_next;
  2982. bi->bi_next = NULL;
  2983. /*
  2984. * this sets the active strip count to 1 and the processed
  2985. * strip count to zero (upper 8 bits)
  2986. */
  2987. bi->bi_phys_segments = 1; /* biased count of active stripes */
  2988. }
  2989. return bi;
  2990. }
  2991. /*
  2992. * The "raid5_align_endio" should check if the read succeeded and if it
  2993. * did, call bio_endio on the original bio (having bio_put the new bio
  2994. * first).
  2995. * If the read failed..
  2996. */
  2997. static void raid5_align_endio(struct bio *bi, int error)
  2998. {
  2999. struct bio* raid_bi = bi->bi_private;
  3000. mddev_t *mddev;
  3001. raid5_conf_t *conf;
  3002. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3003. mdk_rdev_t *rdev;
  3004. bio_put(bi);
  3005. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  3006. conf = mddev_to_conf(mddev);
  3007. rdev = (void*)raid_bi->bi_next;
  3008. raid_bi->bi_next = NULL;
  3009. rdev_dec_pending(rdev, conf->mddev);
  3010. if (!error && uptodate) {
  3011. bio_endio(raid_bi, 0);
  3012. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3013. wake_up(&conf->wait_for_stripe);
  3014. return;
  3015. }
  3016. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3017. add_bio_to_retry(raid_bi, conf);
  3018. }
  3019. static int bio_fits_rdev(struct bio *bi)
  3020. {
  3021. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3022. if ((bi->bi_size>>9) > q->max_sectors)
  3023. return 0;
  3024. blk_recount_segments(q, bi);
  3025. if (bi->bi_phys_segments > q->max_phys_segments)
  3026. return 0;
  3027. if (q->merge_bvec_fn)
  3028. /* it's too hard to apply the merge_bvec_fn at this stage,
  3029. * just just give up
  3030. */
  3031. return 0;
  3032. return 1;
  3033. }
  3034. static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
  3035. {
  3036. mddev_t *mddev = q->queuedata;
  3037. raid5_conf_t *conf = mddev_to_conf(mddev);
  3038. unsigned int dd_idx;
  3039. struct bio* align_bi;
  3040. mdk_rdev_t *rdev;
  3041. if (!in_chunk_boundary(mddev, raid_bio)) {
  3042. pr_debug("chunk_aligned_read : non aligned\n");
  3043. return 0;
  3044. }
  3045. /*
  3046. * use bio_clone to make a copy of the bio
  3047. */
  3048. align_bi = bio_clone(raid_bio, GFP_NOIO);
  3049. if (!align_bi)
  3050. return 0;
  3051. /*
  3052. * set bi_end_io to a new function, and set bi_private to the
  3053. * original bio.
  3054. */
  3055. align_bi->bi_end_io = raid5_align_endio;
  3056. align_bi->bi_private = raid_bio;
  3057. /*
  3058. * compute position
  3059. */
  3060. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3061. 0,
  3062. &dd_idx, NULL);
  3063. rcu_read_lock();
  3064. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3065. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3066. atomic_inc(&rdev->nr_pending);
  3067. rcu_read_unlock();
  3068. raid_bio->bi_next = (void*)rdev;
  3069. align_bi->bi_bdev = rdev->bdev;
  3070. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3071. align_bi->bi_sector += rdev->data_offset;
  3072. if (!bio_fits_rdev(align_bi)) {
  3073. /* too big in some way */
  3074. bio_put(align_bi);
  3075. rdev_dec_pending(rdev, mddev);
  3076. return 0;
  3077. }
  3078. spin_lock_irq(&conf->device_lock);
  3079. wait_event_lock_irq(conf->wait_for_stripe,
  3080. conf->quiesce == 0,
  3081. conf->device_lock, /* nothing */);
  3082. atomic_inc(&conf->active_aligned_reads);
  3083. spin_unlock_irq(&conf->device_lock);
  3084. generic_make_request(align_bi);
  3085. return 1;
  3086. } else {
  3087. rcu_read_unlock();
  3088. bio_put(align_bi);
  3089. return 0;
  3090. }
  3091. }
  3092. /* __get_priority_stripe - get the next stripe to process
  3093. *
  3094. * Full stripe writes are allowed to pass preread active stripes up until
  3095. * the bypass_threshold is exceeded. In general the bypass_count
  3096. * increments when the handle_list is handled before the hold_list; however, it
  3097. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3098. * stripe with in flight i/o. The bypass_count will be reset when the
  3099. * head of the hold_list has changed, i.e. the head was promoted to the
  3100. * handle_list.
  3101. */
  3102. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  3103. {
  3104. struct stripe_head *sh;
  3105. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3106. __func__,
  3107. list_empty(&conf->handle_list) ? "empty" : "busy",
  3108. list_empty(&conf->hold_list) ? "empty" : "busy",
  3109. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3110. if (!list_empty(&conf->handle_list)) {
  3111. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3112. if (list_empty(&conf->hold_list))
  3113. conf->bypass_count = 0;
  3114. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3115. if (conf->hold_list.next == conf->last_hold)
  3116. conf->bypass_count++;
  3117. else {
  3118. conf->last_hold = conf->hold_list.next;
  3119. conf->bypass_count -= conf->bypass_threshold;
  3120. if (conf->bypass_count < 0)
  3121. conf->bypass_count = 0;
  3122. }
  3123. }
  3124. } else if (!list_empty(&conf->hold_list) &&
  3125. ((conf->bypass_threshold &&
  3126. conf->bypass_count > conf->bypass_threshold) ||
  3127. atomic_read(&conf->pending_full_writes) == 0)) {
  3128. sh = list_entry(conf->hold_list.next,
  3129. typeof(*sh), lru);
  3130. conf->bypass_count -= conf->bypass_threshold;
  3131. if (conf->bypass_count < 0)
  3132. conf->bypass_count = 0;
  3133. } else
  3134. return NULL;
  3135. list_del_init(&sh->lru);
  3136. atomic_inc(&sh->count);
  3137. BUG_ON(atomic_read(&sh->count) != 1);
  3138. return sh;
  3139. }
  3140. static int make_request(struct request_queue *q, struct bio * bi)
  3141. {
  3142. mddev_t *mddev = q->queuedata;
  3143. raid5_conf_t *conf = mddev_to_conf(mddev);
  3144. int dd_idx;
  3145. sector_t new_sector;
  3146. sector_t logical_sector, last_sector;
  3147. struct stripe_head *sh;
  3148. const int rw = bio_data_dir(bi);
  3149. int cpu, remaining;
  3150. if (unlikely(bio_barrier(bi))) {
  3151. bio_endio(bi, -EOPNOTSUPP);
  3152. return 0;
  3153. }
  3154. md_write_start(mddev, bi);
  3155. cpu = part_stat_lock();
  3156. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  3157. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  3158. bio_sectors(bi));
  3159. part_stat_unlock();
  3160. if (rw == READ &&
  3161. mddev->reshape_position == MaxSector &&
  3162. chunk_aligned_read(q,bi))
  3163. return 0;
  3164. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3165. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3166. bi->bi_next = NULL;
  3167. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3168. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3169. DEFINE_WAIT(w);
  3170. int disks, data_disks;
  3171. int previous;
  3172. retry:
  3173. previous = 0;
  3174. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3175. if (likely(conf->expand_progress == MaxSector))
  3176. disks = conf->raid_disks;
  3177. else {
  3178. /* spinlock is needed as expand_progress may be
  3179. * 64bit on a 32bit platform, and so it might be
  3180. * possible to see a half-updated value
  3181. * Ofcourse expand_progress could change after
  3182. * the lock is dropped, so once we get a reference
  3183. * to the stripe that we think it is, we will have
  3184. * to check again.
  3185. */
  3186. spin_lock_irq(&conf->device_lock);
  3187. disks = conf->raid_disks;
  3188. if (logical_sector >= conf->expand_progress) {
  3189. disks = conf->previous_raid_disks;
  3190. previous = 1;
  3191. } else {
  3192. if (logical_sector >= conf->expand_lo) {
  3193. spin_unlock_irq(&conf->device_lock);
  3194. schedule();
  3195. goto retry;
  3196. }
  3197. }
  3198. spin_unlock_irq(&conf->device_lock);
  3199. }
  3200. data_disks = disks - conf->max_degraded;
  3201. new_sector = raid5_compute_sector(conf, logical_sector,
  3202. previous,
  3203. &dd_idx, NULL);
  3204. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  3205. (unsigned long long)new_sector,
  3206. (unsigned long long)logical_sector);
  3207. sh = get_active_stripe(conf, new_sector, previous,
  3208. (bi->bi_rw&RWA_MASK));
  3209. if (sh) {
  3210. if (unlikely(conf->expand_progress != MaxSector)) {
  3211. /* expansion might have moved on while waiting for a
  3212. * stripe, so we must do the range check again.
  3213. * Expansion could still move past after this
  3214. * test, but as we are holding a reference to
  3215. * 'sh', we know that if that happens,
  3216. * STRIPE_EXPANDING will get set and the expansion
  3217. * won't proceed until we finish with the stripe.
  3218. */
  3219. int must_retry = 0;
  3220. spin_lock_irq(&conf->device_lock);
  3221. if (logical_sector < conf->expand_progress &&
  3222. disks == conf->previous_raid_disks)
  3223. /* mismatch, need to try again */
  3224. must_retry = 1;
  3225. spin_unlock_irq(&conf->device_lock);
  3226. if (must_retry) {
  3227. release_stripe(sh);
  3228. goto retry;
  3229. }
  3230. }
  3231. /* FIXME what if we get a false positive because these
  3232. * are being updated.
  3233. */
  3234. if (logical_sector >= mddev->suspend_lo &&
  3235. logical_sector < mddev->suspend_hi) {
  3236. release_stripe(sh);
  3237. schedule();
  3238. goto retry;
  3239. }
  3240. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3241. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3242. /* Stripe is busy expanding or
  3243. * add failed due to overlap. Flush everything
  3244. * and wait a while
  3245. */
  3246. raid5_unplug_device(mddev->queue);
  3247. release_stripe(sh);
  3248. schedule();
  3249. goto retry;
  3250. }
  3251. finish_wait(&conf->wait_for_overlap, &w);
  3252. set_bit(STRIPE_HANDLE, &sh->state);
  3253. clear_bit(STRIPE_DELAYED, &sh->state);
  3254. release_stripe(sh);
  3255. } else {
  3256. /* cannot get stripe for read-ahead, just give-up */
  3257. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3258. finish_wait(&conf->wait_for_overlap, &w);
  3259. break;
  3260. }
  3261. }
  3262. spin_lock_irq(&conf->device_lock);
  3263. remaining = raid5_dec_bi_phys_segments(bi);
  3264. spin_unlock_irq(&conf->device_lock);
  3265. if (remaining == 0) {
  3266. if ( rw == WRITE )
  3267. md_write_end(mddev);
  3268. bio_endio(bi, 0);
  3269. }
  3270. return 0;
  3271. }
  3272. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3273. {
  3274. /* reshaping is quite different to recovery/resync so it is
  3275. * handled quite separately ... here.
  3276. *
  3277. * On each call to sync_request, we gather one chunk worth of
  3278. * destination stripes and flag them as expanding.
  3279. * Then we find all the source stripes and request reads.
  3280. * As the reads complete, handle_stripe will copy the data
  3281. * into the destination stripe and release that stripe.
  3282. */
  3283. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3284. struct stripe_head *sh;
  3285. sector_t first_sector, last_sector;
  3286. int raid_disks = conf->previous_raid_disks;
  3287. int data_disks = raid_disks - conf->max_degraded;
  3288. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3289. int i;
  3290. int dd_idx;
  3291. sector_t writepos, safepos, gap;
  3292. if (sector_nr == 0 &&
  3293. conf->expand_progress != 0) {
  3294. /* restarting in the middle, skip the initial sectors */
  3295. sector_nr = conf->expand_progress;
  3296. sector_div(sector_nr, new_data_disks);
  3297. *skipped = 1;
  3298. return sector_nr;
  3299. }
  3300. /* we update the metadata when there is more than 3Meg
  3301. * in the block range (that is rather arbitrary, should
  3302. * probably be time based) or when the data about to be
  3303. * copied would over-write the source of the data at
  3304. * the front of the range.
  3305. * i.e. one new_stripe forward from expand_progress new_maps
  3306. * to after where expand_lo old_maps to
  3307. */
  3308. writepos = conf->expand_progress +
  3309. conf->chunk_size/512*(new_data_disks);
  3310. sector_div(writepos, new_data_disks);
  3311. safepos = conf->expand_lo;
  3312. sector_div(safepos, data_disks);
  3313. gap = conf->expand_progress - conf->expand_lo;
  3314. if (writepos >= safepos ||
  3315. gap > (new_data_disks)*3000*2 /*3Meg*/) {
  3316. /* Cannot proceed until we've updated the superblock... */
  3317. wait_event(conf->wait_for_overlap,
  3318. atomic_read(&conf->reshape_stripes)==0);
  3319. mddev->reshape_position = conf->expand_progress;
  3320. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3321. md_wakeup_thread(mddev->thread);
  3322. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3323. kthread_should_stop());
  3324. spin_lock_irq(&conf->device_lock);
  3325. conf->expand_lo = mddev->reshape_position;
  3326. spin_unlock_irq(&conf->device_lock);
  3327. wake_up(&conf->wait_for_overlap);
  3328. }
  3329. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  3330. int j;
  3331. int skipped = 0;
  3332. sh = get_active_stripe(conf, sector_nr+i, 0, 0);
  3333. set_bit(STRIPE_EXPANDING, &sh->state);
  3334. atomic_inc(&conf->reshape_stripes);
  3335. /* If any of this stripe is beyond the end of the old
  3336. * array, then we need to zero those blocks
  3337. */
  3338. for (j=sh->disks; j--;) {
  3339. sector_t s;
  3340. if (j == sh->pd_idx)
  3341. continue;
  3342. if (conf->level == 6 &&
  3343. j == sh->qd_idx)
  3344. continue;
  3345. s = compute_blocknr(sh, j);
  3346. if (s < mddev->array_sectors) {
  3347. skipped = 1;
  3348. continue;
  3349. }
  3350. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3351. set_bit(R5_Expanded, &sh->dev[j].flags);
  3352. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3353. }
  3354. if (!skipped) {
  3355. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3356. set_bit(STRIPE_HANDLE, &sh->state);
  3357. }
  3358. release_stripe(sh);
  3359. }
  3360. spin_lock_irq(&conf->device_lock);
  3361. conf->expand_progress = (sector_nr + i) * new_data_disks;
  3362. spin_unlock_irq(&conf->device_lock);
  3363. /* Ok, those stripe are ready. We can start scheduling
  3364. * reads on the source stripes.
  3365. * The source stripes are determined by mapping the first and last
  3366. * block on the destination stripes.
  3367. */
  3368. first_sector =
  3369. raid5_compute_sector(conf, sector_nr*(new_data_disks),
  3370. 1, &dd_idx, NULL);
  3371. last_sector =
  3372. raid5_compute_sector(conf, ((sector_nr+conf->chunk_size/512)
  3373. *(new_data_disks) - 1),
  3374. 1, &dd_idx, NULL);
  3375. if (last_sector >= mddev->dev_sectors)
  3376. last_sector = mddev->dev_sectors - 1;
  3377. while (first_sector <= last_sector) {
  3378. sh = get_active_stripe(conf, first_sector, 1, 0);
  3379. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3380. set_bit(STRIPE_HANDLE, &sh->state);
  3381. release_stripe(sh);
  3382. first_sector += STRIPE_SECTORS;
  3383. }
  3384. /* If this takes us to the resync_max point where we have to pause,
  3385. * then we need to write out the superblock.
  3386. */
  3387. sector_nr += conf->chunk_size>>9;
  3388. if (sector_nr >= mddev->resync_max) {
  3389. /* Cannot proceed until we've updated the superblock... */
  3390. wait_event(conf->wait_for_overlap,
  3391. atomic_read(&conf->reshape_stripes) == 0);
  3392. mddev->reshape_position = conf->expand_progress;
  3393. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3394. md_wakeup_thread(mddev->thread);
  3395. wait_event(mddev->sb_wait,
  3396. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3397. || kthread_should_stop());
  3398. spin_lock_irq(&conf->device_lock);
  3399. conf->expand_lo = mddev->reshape_position;
  3400. spin_unlock_irq(&conf->device_lock);
  3401. wake_up(&conf->wait_for_overlap);
  3402. }
  3403. return conf->chunk_size>>9;
  3404. }
  3405. /* FIXME go_faster isn't used */
  3406. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3407. {
  3408. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3409. struct stripe_head *sh;
  3410. sector_t max_sector = mddev->dev_sectors;
  3411. int sync_blocks;
  3412. int still_degraded = 0;
  3413. int i;
  3414. if (sector_nr >= max_sector) {
  3415. /* just being told to finish up .. nothing much to do */
  3416. unplug_slaves(mddev);
  3417. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3418. end_reshape(conf);
  3419. return 0;
  3420. }
  3421. if (mddev->curr_resync < max_sector) /* aborted */
  3422. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3423. &sync_blocks, 1);
  3424. else /* completed sync */
  3425. conf->fullsync = 0;
  3426. bitmap_close_sync(mddev->bitmap);
  3427. return 0;
  3428. }
  3429. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3430. return reshape_request(mddev, sector_nr, skipped);
  3431. /* No need to check resync_max as we never do more than one
  3432. * stripe, and as resync_max will always be on a chunk boundary,
  3433. * if the check in md_do_sync didn't fire, there is no chance
  3434. * of overstepping resync_max here
  3435. */
  3436. /* if there is too many failed drives and we are trying
  3437. * to resync, then assert that we are finished, because there is
  3438. * nothing we can do.
  3439. */
  3440. if (mddev->degraded >= conf->max_degraded &&
  3441. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3442. sector_t rv = mddev->dev_sectors - sector_nr;
  3443. *skipped = 1;
  3444. return rv;
  3445. }
  3446. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3447. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3448. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3449. /* we can skip this block, and probably more */
  3450. sync_blocks /= STRIPE_SECTORS;
  3451. *skipped = 1;
  3452. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3453. }
  3454. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3455. sh = get_active_stripe(conf, sector_nr, 0, 1);
  3456. if (sh == NULL) {
  3457. sh = get_active_stripe(conf, sector_nr, 0, 0);
  3458. /* make sure we don't swamp the stripe cache if someone else
  3459. * is trying to get access
  3460. */
  3461. schedule_timeout_uninterruptible(1);
  3462. }
  3463. /* Need to check if array will still be degraded after recovery/resync
  3464. * We don't need to check the 'failed' flag as when that gets set,
  3465. * recovery aborts.
  3466. */
  3467. for (i=0; i<mddev->raid_disks; i++)
  3468. if (conf->disks[i].rdev == NULL)
  3469. still_degraded = 1;
  3470. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3471. spin_lock(&sh->lock);
  3472. set_bit(STRIPE_SYNCING, &sh->state);
  3473. clear_bit(STRIPE_INSYNC, &sh->state);
  3474. spin_unlock(&sh->lock);
  3475. /* wait for any blocked device to be handled */
  3476. while(unlikely(!handle_stripe(sh, NULL)))
  3477. ;
  3478. release_stripe(sh);
  3479. return STRIPE_SECTORS;
  3480. }
  3481. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3482. {
  3483. /* We may not be able to submit a whole bio at once as there
  3484. * may not be enough stripe_heads available.
  3485. * We cannot pre-allocate enough stripe_heads as we may need
  3486. * more than exist in the cache (if we allow ever large chunks).
  3487. * So we do one stripe head at a time and record in
  3488. * ->bi_hw_segments how many have been done.
  3489. *
  3490. * We *know* that this entire raid_bio is in one chunk, so
  3491. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3492. */
  3493. struct stripe_head *sh;
  3494. int dd_idx;
  3495. sector_t sector, logical_sector, last_sector;
  3496. int scnt = 0;
  3497. int remaining;
  3498. int handled = 0;
  3499. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3500. sector = raid5_compute_sector(conf, logical_sector,
  3501. 0, &dd_idx, NULL);
  3502. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3503. for (; logical_sector < last_sector;
  3504. logical_sector += STRIPE_SECTORS,
  3505. sector += STRIPE_SECTORS,
  3506. scnt++) {
  3507. if (scnt < raid5_bi_hw_segments(raid_bio))
  3508. /* already done this stripe */
  3509. continue;
  3510. sh = get_active_stripe(conf, sector, 0, 1);
  3511. if (!sh) {
  3512. /* failed to get a stripe - must wait */
  3513. raid5_set_bi_hw_segments(raid_bio, scnt);
  3514. conf->retry_read_aligned = raid_bio;
  3515. return handled;
  3516. }
  3517. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3518. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3519. release_stripe(sh);
  3520. raid5_set_bi_hw_segments(raid_bio, scnt);
  3521. conf->retry_read_aligned = raid_bio;
  3522. return handled;
  3523. }
  3524. handle_stripe(sh, NULL);
  3525. release_stripe(sh);
  3526. handled++;
  3527. }
  3528. spin_lock_irq(&conf->device_lock);
  3529. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3530. spin_unlock_irq(&conf->device_lock);
  3531. if (remaining == 0)
  3532. bio_endio(raid_bio, 0);
  3533. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3534. wake_up(&conf->wait_for_stripe);
  3535. return handled;
  3536. }
  3537. /*
  3538. * This is our raid5 kernel thread.
  3539. *
  3540. * We scan the hash table for stripes which can be handled now.
  3541. * During the scan, completed stripes are saved for us by the interrupt
  3542. * handler, so that they will not have to wait for our next wakeup.
  3543. */
  3544. static void raid5d(mddev_t *mddev)
  3545. {
  3546. struct stripe_head *sh;
  3547. raid5_conf_t *conf = mddev_to_conf(mddev);
  3548. int handled;
  3549. pr_debug("+++ raid5d active\n");
  3550. md_check_recovery(mddev);
  3551. handled = 0;
  3552. spin_lock_irq(&conf->device_lock);
  3553. while (1) {
  3554. struct bio *bio;
  3555. if (conf->seq_flush != conf->seq_write) {
  3556. int seq = conf->seq_flush;
  3557. spin_unlock_irq(&conf->device_lock);
  3558. bitmap_unplug(mddev->bitmap);
  3559. spin_lock_irq(&conf->device_lock);
  3560. conf->seq_write = seq;
  3561. activate_bit_delay(conf);
  3562. }
  3563. while ((bio = remove_bio_from_retry(conf))) {
  3564. int ok;
  3565. spin_unlock_irq(&conf->device_lock);
  3566. ok = retry_aligned_read(conf, bio);
  3567. spin_lock_irq(&conf->device_lock);
  3568. if (!ok)
  3569. break;
  3570. handled++;
  3571. }
  3572. sh = __get_priority_stripe(conf);
  3573. if (!sh)
  3574. break;
  3575. spin_unlock_irq(&conf->device_lock);
  3576. handled++;
  3577. handle_stripe(sh, conf->spare_page);
  3578. release_stripe(sh);
  3579. spin_lock_irq(&conf->device_lock);
  3580. }
  3581. pr_debug("%d stripes handled\n", handled);
  3582. spin_unlock_irq(&conf->device_lock);
  3583. async_tx_issue_pending_all();
  3584. unplug_slaves(mddev);
  3585. pr_debug("--- raid5d inactive\n");
  3586. }
  3587. static ssize_t
  3588. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  3589. {
  3590. raid5_conf_t *conf = mddev_to_conf(mddev);
  3591. if (conf)
  3592. return sprintf(page, "%d\n", conf->max_nr_stripes);
  3593. else
  3594. return 0;
  3595. }
  3596. static ssize_t
  3597. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  3598. {
  3599. raid5_conf_t *conf = mddev_to_conf(mddev);
  3600. unsigned long new;
  3601. int err;
  3602. if (len >= PAGE_SIZE)
  3603. return -EINVAL;
  3604. if (!conf)
  3605. return -ENODEV;
  3606. if (strict_strtoul(page, 10, &new))
  3607. return -EINVAL;
  3608. if (new <= 16 || new > 32768)
  3609. return -EINVAL;
  3610. while (new < conf->max_nr_stripes) {
  3611. if (drop_one_stripe(conf))
  3612. conf->max_nr_stripes--;
  3613. else
  3614. break;
  3615. }
  3616. err = md_allow_write(mddev);
  3617. if (err)
  3618. return err;
  3619. while (new > conf->max_nr_stripes) {
  3620. if (grow_one_stripe(conf))
  3621. conf->max_nr_stripes++;
  3622. else break;
  3623. }
  3624. return len;
  3625. }
  3626. static struct md_sysfs_entry
  3627. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  3628. raid5_show_stripe_cache_size,
  3629. raid5_store_stripe_cache_size);
  3630. static ssize_t
  3631. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  3632. {
  3633. raid5_conf_t *conf = mddev_to_conf(mddev);
  3634. if (conf)
  3635. return sprintf(page, "%d\n", conf->bypass_threshold);
  3636. else
  3637. return 0;
  3638. }
  3639. static ssize_t
  3640. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  3641. {
  3642. raid5_conf_t *conf = mddev_to_conf(mddev);
  3643. unsigned long new;
  3644. if (len >= PAGE_SIZE)
  3645. return -EINVAL;
  3646. if (!conf)
  3647. return -ENODEV;
  3648. if (strict_strtoul(page, 10, &new))
  3649. return -EINVAL;
  3650. if (new > conf->max_nr_stripes)
  3651. return -EINVAL;
  3652. conf->bypass_threshold = new;
  3653. return len;
  3654. }
  3655. static struct md_sysfs_entry
  3656. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  3657. S_IRUGO | S_IWUSR,
  3658. raid5_show_preread_threshold,
  3659. raid5_store_preread_threshold);
  3660. static ssize_t
  3661. stripe_cache_active_show(mddev_t *mddev, char *page)
  3662. {
  3663. raid5_conf_t *conf = mddev_to_conf(mddev);
  3664. if (conf)
  3665. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  3666. else
  3667. return 0;
  3668. }
  3669. static struct md_sysfs_entry
  3670. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  3671. static struct attribute *raid5_attrs[] = {
  3672. &raid5_stripecache_size.attr,
  3673. &raid5_stripecache_active.attr,
  3674. &raid5_preread_bypass_threshold.attr,
  3675. NULL,
  3676. };
  3677. static struct attribute_group raid5_attrs_group = {
  3678. .name = NULL,
  3679. .attrs = raid5_attrs,
  3680. };
  3681. static raid5_conf_t *setup_conf(mddev_t *mddev)
  3682. {
  3683. raid5_conf_t *conf;
  3684. int raid_disk, memory;
  3685. mdk_rdev_t *rdev;
  3686. struct disk_info *disk;
  3687. if (mddev->new_level != 5
  3688. && mddev->new_level != 4
  3689. && mddev->new_level != 6) {
  3690. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3691. mdname(mddev), mddev->new_level);
  3692. return ERR_PTR(-EIO);
  3693. }
  3694. if ((mddev->new_level == 5
  3695. && !algorithm_valid_raid5(mddev->new_layout)) ||
  3696. (mddev->new_level == 6
  3697. && !algorithm_valid_raid6(mddev->new_layout))) {
  3698. printk(KERN_ERR "raid5: %s: layout %d not supported\n",
  3699. mdname(mddev), mddev->new_layout);
  3700. return ERR_PTR(-EIO);
  3701. }
  3702. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  3703. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3704. mdname(mddev), mddev->raid_disks);
  3705. return ERR_PTR(-EINVAL);
  3706. }
  3707. if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
  3708. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3709. mddev->new_chunk, mdname(mddev));
  3710. return ERR_PTR(-EINVAL);
  3711. }
  3712. conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
  3713. if (conf == NULL)
  3714. goto abort;
  3715. conf->raid_disks = mddev->raid_disks;
  3716. if (mddev->reshape_position == MaxSector)
  3717. conf->previous_raid_disks = mddev->raid_disks;
  3718. else
  3719. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3720. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3721. GFP_KERNEL);
  3722. if (!conf->disks)
  3723. goto abort;
  3724. conf->mddev = mddev;
  3725. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3726. goto abort;
  3727. if (mddev->new_level == 6) {
  3728. conf->spare_page = alloc_page(GFP_KERNEL);
  3729. if (!conf->spare_page)
  3730. goto abort;
  3731. }
  3732. spin_lock_init(&conf->device_lock);
  3733. init_waitqueue_head(&conf->wait_for_stripe);
  3734. init_waitqueue_head(&conf->wait_for_overlap);
  3735. INIT_LIST_HEAD(&conf->handle_list);
  3736. INIT_LIST_HEAD(&conf->hold_list);
  3737. INIT_LIST_HEAD(&conf->delayed_list);
  3738. INIT_LIST_HEAD(&conf->bitmap_list);
  3739. INIT_LIST_HEAD(&conf->inactive_list);
  3740. atomic_set(&conf->active_stripes, 0);
  3741. atomic_set(&conf->preread_active_stripes, 0);
  3742. atomic_set(&conf->active_aligned_reads, 0);
  3743. conf->bypass_threshold = BYPASS_THRESHOLD;
  3744. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  3745. list_for_each_entry(rdev, &mddev->disks, same_set) {
  3746. raid_disk = rdev->raid_disk;
  3747. if (raid_disk >= conf->raid_disks
  3748. || raid_disk < 0)
  3749. continue;
  3750. disk = conf->disks + raid_disk;
  3751. disk->rdev = rdev;
  3752. if (test_bit(In_sync, &rdev->flags)) {
  3753. char b[BDEVNAME_SIZE];
  3754. printk(KERN_INFO "raid5: device %s operational as raid"
  3755. " disk %d\n", bdevname(rdev->bdev,b),
  3756. raid_disk);
  3757. } else
  3758. /* Cannot rely on bitmap to complete recovery */
  3759. conf->fullsync = 1;
  3760. }
  3761. conf->chunk_size = mddev->new_chunk;
  3762. conf->level = mddev->new_level;
  3763. if (conf->level == 6)
  3764. conf->max_degraded = 2;
  3765. else
  3766. conf->max_degraded = 1;
  3767. conf->algorithm = mddev->new_layout;
  3768. conf->max_nr_stripes = NR_STRIPES;
  3769. conf->expand_progress = mddev->reshape_position;
  3770. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3771. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3772. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3773. printk(KERN_ERR
  3774. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3775. goto abort;
  3776. } else
  3777. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3778. memory, mdname(mddev));
  3779. conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3780. if (!conf->thread) {
  3781. printk(KERN_ERR
  3782. "raid5: couldn't allocate thread for %s\n",
  3783. mdname(mddev));
  3784. goto abort;
  3785. }
  3786. return conf;
  3787. abort:
  3788. if (conf) {
  3789. shrink_stripes(conf);
  3790. safe_put_page(conf->spare_page);
  3791. kfree(conf->disks);
  3792. kfree(conf->stripe_hashtbl);
  3793. kfree(conf);
  3794. return ERR_PTR(-EIO);
  3795. } else
  3796. return ERR_PTR(-ENOMEM);
  3797. }
  3798. static int run(mddev_t *mddev)
  3799. {
  3800. raid5_conf_t *conf;
  3801. int working_disks = 0;
  3802. mdk_rdev_t *rdev;
  3803. if (mddev->reshape_position != MaxSector) {
  3804. /* Check that we can continue the reshape.
  3805. * Currently only disks can change, it must
  3806. * increase, and we must be past the point where
  3807. * a stripe over-writes itself
  3808. */
  3809. sector_t here_new, here_old;
  3810. int old_disks;
  3811. int max_degraded = (mddev->level == 5 ? 1 : 2);
  3812. if (mddev->new_level != mddev->level ||
  3813. mddev->new_layout != mddev->layout ||
  3814. mddev->new_chunk != mddev->chunk_size) {
  3815. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3816. "required - aborting.\n",
  3817. mdname(mddev));
  3818. return -EINVAL;
  3819. }
  3820. if (mddev->delta_disks <= 0) {
  3821. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3822. "(reduce disks) required - aborting.\n",
  3823. mdname(mddev));
  3824. return -EINVAL;
  3825. }
  3826. old_disks = mddev->raid_disks - mddev->delta_disks;
  3827. /* reshape_position must be on a new-stripe boundary, and one
  3828. * further up in new geometry must map after here in old
  3829. * geometry.
  3830. */
  3831. here_new = mddev->reshape_position;
  3832. if (sector_div(here_new, (mddev->chunk_size>>9)*
  3833. (mddev->raid_disks - max_degraded))) {
  3834. printk(KERN_ERR "raid5: reshape_position not "
  3835. "on a stripe boundary\n");
  3836. return -EINVAL;
  3837. }
  3838. /* here_new is the stripe we will write to */
  3839. here_old = mddev->reshape_position;
  3840. sector_div(here_old, (mddev->chunk_size>>9)*
  3841. (old_disks-max_degraded));
  3842. /* here_old is the first stripe that we might need to read
  3843. * from */
  3844. if (here_new >= here_old) {
  3845. /* Reading from the same stripe as writing to - bad */
  3846. printk(KERN_ERR "raid5: reshape_position too early for "
  3847. "auto-recovery - aborting.\n");
  3848. return -EINVAL;
  3849. }
  3850. printk(KERN_INFO "raid5: reshape will continue\n");
  3851. /* OK, we should be able to continue; */
  3852. } else {
  3853. BUG_ON(mddev->level != mddev->new_level);
  3854. BUG_ON(mddev->layout != mddev->new_layout);
  3855. BUG_ON(mddev->chunk_size != mddev->new_chunk);
  3856. BUG_ON(mddev->delta_disks != 0);
  3857. }
  3858. if (mddev->private == NULL)
  3859. conf = setup_conf(mddev);
  3860. else
  3861. conf = mddev->private;
  3862. if (IS_ERR(conf))
  3863. return PTR_ERR(conf);
  3864. mddev->thread = conf->thread;
  3865. conf->thread = NULL;
  3866. mddev->private = conf;
  3867. /*
  3868. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3869. */
  3870. list_for_each_entry(rdev, &mddev->disks, same_set)
  3871. if (rdev->raid_disk >= 0 &&
  3872. test_bit(In_sync, &rdev->flags))
  3873. working_disks++;
  3874. mddev->degraded = conf->raid_disks - working_disks;
  3875. if (mddev->degraded > conf->max_degraded) {
  3876. printk(KERN_ERR "raid5: not enough operational devices for %s"
  3877. " (%d/%d failed)\n",
  3878. mdname(mddev), mddev->degraded, conf->raid_disks);
  3879. goto abort;
  3880. }
  3881. /* device size must be a multiple of chunk size */
  3882. mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
  3883. mddev->resync_max_sectors = mddev->dev_sectors;
  3884. if (mddev->degraded > 0 &&
  3885. mddev->recovery_cp != MaxSector) {
  3886. if (mddev->ok_start_degraded)
  3887. printk(KERN_WARNING
  3888. "raid5: starting dirty degraded array: %s"
  3889. "- data corruption possible.\n",
  3890. mdname(mddev));
  3891. else {
  3892. printk(KERN_ERR
  3893. "raid5: cannot start dirty degraded array for %s\n",
  3894. mdname(mddev));
  3895. goto abort;
  3896. }
  3897. }
  3898. if (mddev->degraded == 0)
  3899. printk("raid5: raid level %d set %s active with %d out of %d"
  3900. " devices, algorithm %d\n", conf->level, mdname(mddev),
  3901. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  3902. conf->algorithm);
  3903. else
  3904. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  3905. " out of %d devices, algorithm %d\n", conf->level,
  3906. mdname(mddev), mddev->raid_disks - mddev->degraded,
  3907. mddev->raid_disks, conf->algorithm);
  3908. print_raid5_conf(conf);
  3909. if (conf->expand_progress != MaxSector) {
  3910. printk("...ok start reshape thread\n");
  3911. conf->expand_lo = conf->expand_progress;
  3912. atomic_set(&conf->reshape_stripes, 0);
  3913. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3914. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3915. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3916. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3917. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3918. "%s_reshape");
  3919. }
  3920. /* read-ahead size must cover two whole stripes, which is
  3921. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3922. */
  3923. {
  3924. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3925. int stripe = data_disks *
  3926. (mddev->chunk_size / PAGE_SIZE);
  3927. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3928. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3929. }
  3930. /* Ok, everything is just fine now */
  3931. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  3932. printk(KERN_WARNING
  3933. "raid5: failed to create sysfs attributes for %s\n",
  3934. mdname(mddev));
  3935. mddev->queue->queue_lock = &conf->device_lock;
  3936. mddev->queue->unplug_fn = raid5_unplug_device;
  3937. mddev->queue->backing_dev_info.congested_data = mddev;
  3938. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  3939. mddev->array_sectors = mddev->dev_sectors *
  3940. (conf->previous_raid_disks - conf->max_degraded);
  3941. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  3942. return 0;
  3943. abort:
  3944. md_unregister_thread(mddev->thread);
  3945. mddev->thread = NULL;
  3946. if (conf) {
  3947. shrink_stripes(conf);
  3948. print_raid5_conf(conf);
  3949. safe_put_page(conf->spare_page);
  3950. kfree(conf->disks);
  3951. kfree(conf->stripe_hashtbl);
  3952. kfree(conf);
  3953. }
  3954. mddev->private = NULL;
  3955. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  3956. return -EIO;
  3957. }
  3958. static int stop(mddev_t *mddev)
  3959. {
  3960. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3961. md_unregister_thread(mddev->thread);
  3962. mddev->thread = NULL;
  3963. shrink_stripes(conf);
  3964. kfree(conf->stripe_hashtbl);
  3965. mddev->queue->backing_dev_info.congested_fn = NULL;
  3966. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3967. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3968. kfree(conf->disks);
  3969. kfree(conf);
  3970. mddev->private = NULL;
  3971. return 0;
  3972. }
  3973. #ifdef DEBUG
  3974. static void print_sh(struct seq_file *seq, struct stripe_head *sh)
  3975. {
  3976. int i;
  3977. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3978. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3979. seq_printf(seq, "sh %llu, count %d.\n",
  3980. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3981. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3982. for (i = 0; i < sh->disks; i++) {
  3983. seq_printf(seq, "(cache%d: %p %ld) ",
  3984. i, sh->dev[i].page, sh->dev[i].flags);
  3985. }
  3986. seq_printf(seq, "\n");
  3987. }
  3988. static void printall(struct seq_file *seq, raid5_conf_t *conf)
  3989. {
  3990. struct stripe_head *sh;
  3991. struct hlist_node *hn;
  3992. int i;
  3993. spin_lock_irq(&conf->device_lock);
  3994. for (i = 0; i < NR_HASH; i++) {
  3995. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3996. if (sh->raid_conf != conf)
  3997. continue;
  3998. print_sh(seq, sh);
  3999. }
  4000. }
  4001. spin_unlock_irq(&conf->device_lock);
  4002. }
  4003. #endif
  4004. static void status(struct seq_file *seq, mddev_t *mddev)
  4005. {
  4006. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  4007. int i;
  4008. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  4009. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4010. for (i = 0; i < conf->raid_disks; i++)
  4011. seq_printf (seq, "%s",
  4012. conf->disks[i].rdev &&
  4013. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4014. seq_printf (seq, "]");
  4015. #ifdef DEBUG
  4016. seq_printf (seq, "\n");
  4017. printall(seq, conf);
  4018. #endif
  4019. }
  4020. static void print_raid5_conf (raid5_conf_t *conf)
  4021. {
  4022. int i;
  4023. struct disk_info *tmp;
  4024. printk("RAID5 conf printout:\n");
  4025. if (!conf) {
  4026. printk("(conf==NULL)\n");
  4027. return;
  4028. }
  4029. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  4030. conf->raid_disks - conf->mddev->degraded);
  4031. for (i = 0; i < conf->raid_disks; i++) {
  4032. char b[BDEVNAME_SIZE];
  4033. tmp = conf->disks + i;
  4034. if (tmp->rdev)
  4035. printk(" disk %d, o:%d, dev:%s\n",
  4036. i, !test_bit(Faulty, &tmp->rdev->flags),
  4037. bdevname(tmp->rdev->bdev,b));
  4038. }
  4039. }
  4040. static int raid5_spare_active(mddev_t *mddev)
  4041. {
  4042. int i;
  4043. raid5_conf_t *conf = mddev->private;
  4044. struct disk_info *tmp;
  4045. for (i = 0; i < conf->raid_disks; i++) {
  4046. tmp = conf->disks + i;
  4047. if (tmp->rdev
  4048. && !test_bit(Faulty, &tmp->rdev->flags)
  4049. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4050. unsigned long flags;
  4051. spin_lock_irqsave(&conf->device_lock, flags);
  4052. mddev->degraded--;
  4053. spin_unlock_irqrestore(&conf->device_lock, flags);
  4054. }
  4055. }
  4056. print_raid5_conf(conf);
  4057. return 0;
  4058. }
  4059. static int raid5_remove_disk(mddev_t *mddev, int number)
  4060. {
  4061. raid5_conf_t *conf = mddev->private;
  4062. int err = 0;
  4063. mdk_rdev_t *rdev;
  4064. struct disk_info *p = conf->disks + number;
  4065. print_raid5_conf(conf);
  4066. rdev = p->rdev;
  4067. if (rdev) {
  4068. if (test_bit(In_sync, &rdev->flags) ||
  4069. atomic_read(&rdev->nr_pending)) {
  4070. err = -EBUSY;
  4071. goto abort;
  4072. }
  4073. /* Only remove non-faulty devices if recovery
  4074. * isn't possible.
  4075. */
  4076. if (!test_bit(Faulty, &rdev->flags) &&
  4077. mddev->degraded <= conf->max_degraded) {
  4078. err = -EBUSY;
  4079. goto abort;
  4080. }
  4081. p->rdev = NULL;
  4082. synchronize_rcu();
  4083. if (atomic_read(&rdev->nr_pending)) {
  4084. /* lost the race, try later */
  4085. err = -EBUSY;
  4086. p->rdev = rdev;
  4087. }
  4088. }
  4089. abort:
  4090. print_raid5_conf(conf);
  4091. return err;
  4092. }
  4093. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  4094. {
  4095. raid5_conf_t *conf = mddev->private;
  4096. int err = -EEXIST;
  4097. int disk;
  4098. struct disk_info *p;
  4099. int first = 0;
  4100. int last = conf->raid_disks - 1;
  4101. if (mddev->degraded > conf->max_degraded)
  4102. /* no point adding a device */
  4103. return -EINVAL;
  4104. if (rdev->raid_disk >= 0)
  4105. first = last = rdev->raid_disk;
  4106. /*
  4107. * find the disk ... but prefer rdev->saved_raid_disk
  4108. * if possible.
  4109. */
  4110. if (rdev->saved_raid_disk >= 0 &&
  4111. rdev->saved_raid_disk >= first &&
  4112. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4113. disk = rdev->saved_raid_disk;
  4114. else
  4115. disk = first;
  4116. for ( ; disk <= last ; disk++)
  4117. if ((p=conf->disks + disk)->rdev == NULL) {
  4118. clear_bit(In_sync, &rdev->flags);
  4119. rdev->raid_disk = disk;
  4120. err = 0;
  4121. if (rdev->saved_raid_disk != disk)
  4122. conf->fullsync = 1;
  4123. rcu_assign_pointer(p->rdev, rdev);
  4124. break;
  4125. }
  4126. print_raid5_conf(conf);
  4127. return err;
  4128. }
  4129. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  4130. {
  4131. /* no resync is happening, and there is enough space
  4132. * on all devices, so we can resize.
  4133. * We need to make sure resync covers any new space.
  4134. * If the array is shrinking we should possibly wait until
  4135. * any io in the removed space completes, but it hardly seems
  4136. * worth it.
  4137. */
  4138. raid5_conf_t *conf = mddev_to_conf(mddev);
  4139. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  4140. mddev->array_sectors = sectors * (mddev->raid_disks
  4141. - conf->max_degraded);
  4142. set_capacity(mddev->gendisk, mddev->array_sectors);
  4143. mddev->changed = 1;
  4144. if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
  4145. mddev->recovery_cp = mddev->dev_sectors;
  4146. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4147. }
  4148. mddev->dev_sectors = sectors;
  4149. mddev->resync_max_sectors = sectors;
  4150. return 0;
  4151. }
  4152. #ifdef CONFIG_MD_RAID5_RESHAPE
  4153. static int raid5_check_reshape(mddev_t *mddev)
  4154. {
  4155. raid5_conf_t *conf = mddev_to_conf(mddev);
  4156. int err;
  4157. if (mddev->delta_disks < 0 ||
  4158. mddev->new_level != mddev->level)
  4159. return -EINVAL; /* Cannot shrink array or change level yet */
  4160. if (mddev->delta_disks == 0)
  4161. return 0; /* nothing to do */
  4162. if (mddev->bitmap)
  4163. /* Cannot grow a bitmap yet */
  4164. return -EBUSY;
  4165. /* Can only proceed if there are plenty of stripe_heads.
  4166. * We need a minimum of one full stripe,, and for sensible progress
  4167. * it is best to have about 4 times that.
  4168. * If we require 4 times, then the default 256 4K stripe_heads will
  4169. * allow for chunk sizes up to 256K, which is probably OK.
  4170. * If the chunk size is greater, user-space should request more
  4171. * stripe_heads first.
  4172. */
  4173. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  4174. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  4175. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  4176. (mddev->chunk_size / STRIPE_SIZE)*4);
  4177. return -ENOSPC;
  4178. }
  4179. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4180. if (err)
  4181. return err;
  4182. if (mddev->degraded > conf->max_degraded)
  4183. return -EINVAL;
  4184. /* looks like we might be able to manage this */
  4185. return 0;
  4186. }
  4187. static int raid5_start_reshape(mddev_t *mddev)
  4188. {
  4189. raid5_conf_t *conf = mddev_to_conf(mddev);
  4190. mdk_rdev_t *rdev;
  4191. int spares = 0;
  4192. int added_devices = 0;
  4193. unsigned long flags;
  4194. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4195. return -EBUSY;
  4196. list_for_each_entry(rdev, &mddev->disks, same_set)
  4197. if (rdev->raid_disk < 0 &&
  4198. !test_bit(Faulty, &rdev->flags))
  4199. spares++;
  4200. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4201. /* Not enough devices even to make a degraded array
  4202. * of that size
  4203. */
  4204. return -EINVAL;
  4205. atomic_set(&conf->reshape_stripes, 0);
  4206. spin_lock_irq(&conf->device_lock);
  4207. conf->previous_raid_disks = conf->raid_disks;
  4208. conf->raid_disks += mddev->delta_disks;
  4209. conf->expand_progress = 0;
  4210. conf->expand_lo = 0;
  4211. spin_unlock_irq(&conf->device_lock);
  4212. /* Add some new drives, as many as will fit.
  4213. * We know there are enough to make the newly sized array work.
  4214. */
  4215. list_for_each_entry(rdev, &mddev->disks, same_set)
  4216. if (rdev->raid_disk < 0 &&
  4217. !test_bit(Faulty, &rdev->flags)) {
  4218. if (raid5_add_disk(mddev, rdev) == 0) {
  4219. char nm[20];
  4220. set_bit(In_sync, &rdev->flags);
  4221. added_devices++;
  4222. rdev->recovery_offset = 0;
  4223. sprintf(nm, "rd%d", rdev->raid_disk);
  4224. if (sysfs_create_link(&mddev->kobj,
  4225. &rdev->kobj, nm))
  4226. printk(KERN_WARNING
  4227. "raid5: failed to create "
  4228. " link %s for %s\n",
  4229. nm, mdname(mddev));
  4230. } else
  4231. break;
  4232. }
  4233. spin_lock_irqsave(&conf->device_lock, flags);
  4234. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  4235. spin_unlock_irqrestore(&conf->device_lock, flags);
  4236. mddev->raid_disks = conf->raid_disks;
  4237. mddev->reshape_position = 0;
  4238. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4239. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4240. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4241. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4242. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4243. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4244. "%s_reshape");
  4245. if (!mddev->sync_thread) {
  4246. mddev->recovery = 0;
  4247. spin_lock_irq(&conf->device_lock);
  4248. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4249. conf->expand_progress = MaxSector;
  4250. spin_unlock_irq(&conf->device_lock);
  4251. return -EAGAIN;
  4252. }
  4253. md_wakeup_thread(mddev->sync_thread);
  4254. md_new_event(mddev);
  4255. return 0;
  4256. }
  4257. #endif
  4258. static void end_reshape(raid5_conf_t *conf)
  4259. {
  4260. struct block_device *bdev;
  4261. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4262. conf->mddev->array_sectors = conf->mddev->dev_sectors *
  4263. (conf->raid_disks - conf->max_degraded);
  4264. set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
  4265. conf->mddev->changed = 1;
  4266. bdev = bdget_disk(conf->mddev->gendisk, 0);
  4267. if (bdev) {
  4268. mutex_lock(&bdev->bd_inode->i_mutex);
  4269. i_size_write(bdev->bd_inode,
  4270. (loff_t)conf->mddev->array_sectors << 9);
  4271. mutex_unlock(&bdev->bd_inode->i_mutex);
  4272. bdput(bdev);
  4273. }
  4274. spin_lock_irq(&conf->device_lock);
  4275. conf->expand_progress = MaxSector;
  4276. spin_unlock_irq(&conf->device_lock);
  4277. conf->mddev->reshape_position = MaxSector;
  4278. /* read-ahead size must cover two whole stripes, which is
  4279. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4280. */
  4281. {
  4282. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4283. int stripe = data_disks *
  4284. (conf->mddev->chunk_size / PAGE_SIZE);
  4285. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4286. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4287. }
  4288. }
  4289. }
  4290. static void raid5_quiesce(mddev_t *mddev, int state)
  4291. {
  4292. raid5_conf_t *conf = mddev_to_conf(mddev);
  4293. switch(state) {
  4294. case 2: /* resume for a suspend */
  4295. wake_up(&conf->wait_for_overlap);
  4296. break;
  4297. case 1: /* stop all writes */
  4298. spin_lock_irq(&conf->device_lock);
  4299. conf->quiesce = 1;
  4300. wait_event_lock_irq(conf->wait_for_stripe,
  4301. atomic_read(&conf->active_stripes) == 0 &&
  4302. atomic_read(&conf->active_aligned_reads) == 0,
  4303. conf->device_lock, /* nothing */);
  4304. spin_unlock_irq(&conf->device_lock);
  4305. break;
  4306. case 0: /* re-enable writes */
  4307. spin_lock_irq(&conf->device_lock);
  4308. conf->quiesce = 0;
  4309. wake_up(&conf->wait_for_stripe);
  4310. wake_up(&conf->wait_for_overlap);
  4311. spin_unlock_irq(&conf->device_lock);
  4312. break;
  4313. }
  4314. }
  4315. static void *raid5_takeover_raid1(mddev_t *mddev)
  4316. {
  4317. int chunksect;
  4318. if (mddev->raid_disks != 2 ||
  4319. mddev->degraded > 1)
  4320. return ERR_PTR(-EINVAL);
  4321. /* Should check if there are write-behind devices? */
  4322. chunksect = 64*2; /* 64K by default */
  4323. /* The array must be an exact multiple of chunksize */
  4324. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  4325. chunksect >>= 1;
  4326. if ((chunksect<<9) < STRIPE_SIZE)
  4327. /* array size does not allow a suitable chunk size */
  4328. return ERR_PTR(-EINVAL);
  4329. mddev->new_level = 5;
  4330. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  4331. mddev->new_chunk = chunksect << 9;
  4332. return setup_conf(mddev);
  4333. }
  4334. static void *raid5_takeover_raid6(mddev_t *mddev)
  4335. {
  4336. int new_layout;
  4337. switch (mddev->layout) {
  4338. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4339. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  4340. break;
  4341. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4342. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  4343. break;
  4344. case ALGORITHM_LEFT_SYMMETRIC_6:
  4345. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  4346. break;
  4347. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4348. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  4349. break;
  4350. case ALGORITHM_PARITY_0_6:
  4351. new_layout = ALGORITHM_PARITY_0;
  4352. break;
  4353. case ALGORITHM_PARITY_N:
  4354. new_layout = ALGORITHM_PARITY_N;
  4355. break;
  4356. default:
  4357. return ERR_PTR(-EINVAL);
  4358. }
  4359. mddev->new_level = 5;
  4360. mddev->new_layout = new_layout;
  4361. mddev->delta_disks = -1;
  4362. mddev->raid_disks -= 1;
  4363. return setup_conf(mddev);
  4364. }
  4365. static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
  4366. {
  4367. /* Currently the layout and chunk size can only be changed
  4368. * for a 2-drive raid array, as in that case no data shuffling
  4369. * is required.
  4370. * Later we might validate these and set new_* so a reshape
  4371. * can complete the change.
  4372. */
  4373. raid5_conf_t *conf = mddev_to_conf(mddev);
  4374. if (new_layout >= 0 && !algorithm_valid_raid5(new_layout))
  4375. return -EINVAL;
  4376. if (new_chunk > 0) {
  4377. if (new_chunk & (new_chunk-1))
  4378. /* not a power of 2 */
  4379. return -EINVAL;
  4380. if (new_chunk < PAGE_SIZE)
  4381. return -EINVAL;
  4382. if (mddev->array_sectors & ((new_chunk>>9)-1))
  4383. /* not factor of array size */
  4384. return -EINVAL;
  4385. }
  4386. /* They look valid */
  4387. if (mddev->raid_disks != 2)
  4388. return -EINVAL;
  4389. if (new_layout >= 0) {
  4390. conf->algorithm = new_layout;
  4391. mddev->layout = mddev->new_layout = new_layout;
  4392. }
  4393. if (new_chunk > 0) {
  4394. conf->chunk_size = new_chunk;
  4395. mddev->chunk_size = mddev->new_chunk = new_chunk;
  4396. }
  4397. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4398. md_wakeup_thread(mddev->thread);
  4399. return 0;
  4400. }
  4401. static void *raid5_takeover(mddev_t *mddev)
  4402. {
  4403. /* raid5 can take over:
  4404. * raid0 - if all devices are the same - make it a raid4 layout
  4405. * raid1 - if there are two drives. We need to know the chunk size
  4406. * raid4 - trivial - just use a raid4 layout.
  4407. * raid6 - Providing it is a *_6 layout
  4408. *
  4409. * For now, just do raid1
  4410. */
  4411. if (mddev->level == 1)
  4412. return raid5_takeover_raid1(mddev);
  4413. if (mddev->level == 4) {
  4414. mddev->new_layout = ALGORITHM_PARITY_N;
  4415. mddev->new_level = 5;
  4416. return setup_conf(mddev);
  4417. }
  4418. if (mddev->level == 6)
  4419. return raid5_takeover_raid6(mddev);
  4420. return ERR_PTR(-EINVAL);
  4421. }
  4422. static struct mdk_personality raid5_personality;
  4423. static void *raid6_takeover(mddev_t *mddev)
  4424. {
  4425. /* Currently can only take over a raid5. We map the
  4426. * personality to an equivalent raid6 personality
  4427. * with the Q block at the end.
  4428. */
  4429. int new_layout;
  4430. if (mddev->pers != &raid5_personality)
  4431. return ERR_PTR(-EINVAL);
  4432. if (mddev->degraded > 1)
  4433. return ERR_PTR(-EINVAL);
  4434. if (mddev->raid_disks > 253)
  4435. return ERR_PTR(-EINVAL);
  4436. if (mddev->raid_disks < 3)
  4437. return ERR_PTR(-EINVAL);
  4438. switch (mddev->layout) {
  4439. case ALGORITHM_LEFT_ASYMMETRIC:
  4440. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  4441. break;
  4442. case ALGORITHM_RIGHT_ASYMMETRIC:
  4443. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  4444. break;
  4445. case ALGORITHM_LEFT_SYMMETRIC:
  4446. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  4447. break;
  4448. case ALGORITHM_RIGHT_SYMMETRIC:
  4449. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  4450. break;
  4451. case ALGORITHM_PARITY_0:
  4452. new_layout = ALGORITHM_PARITY_0_6;
  4453. break;
  4454. case ALGORITHM_PARITY_N:
  4455. new_layout = ALGORITHM_PARITY_N;
  4456. break;
  4457. default:
  4458. return ERR_PTR(-EINVAL);
  4459. }
  4460. mddev->new_level = 6;
  4461. mddev->new_layout = new_layout;
  4462. mddev->delta_disks = 1;
  4463. mddev->raid_disks += 1;
  4464. return setup_conf(mddev);
  4465. }
  4466. static struct mdk_personality raid6_personality =
  4467. {
  4468. .name = "raid6",
  4469. .level = 6,
  4470. .owner = THIS_MODULE,
  4471. .make_request = make_request,
  4472. .run = run,
  4473. .stop = stop,
  4474. .status = status,
  4475. .error_handler = error,
  4476. .hot_add_disk = raid5_add_disk,
  4477. .hot_remove_disk= raid5_remove_disk,
  4478. .spare_active = raid5_spare_active,
  4479. .sync_request = sync_request,
  4480. .resize = raid5_resize,
  4481. #ifdef CONFIG_MD_RAID5_RESHAPE
  4482. .check_reshape = raid5_check_reshape,
  4483. .start_reshape = raid5_start_reshape,
  4484. #endif
  4485. .quiesce = raid5_quiesce,
  4486. .takeover = raid6_takeover,
  4487. };
  4488. static struct mdk_personality raid5_personality =
  4489. {
  4490. .name = "raid5",
  4491. .level = 5,
  4492. .owner = THIS_MODULE,
  4493. .make_request = make_request,
  4494. .run = run,
  4495. .stop = stop,
  4496. .status = status,
  4497. .error_handler = error,
  4498. .hot_add_disk = raid5_add_disk,
  4499. .hot_remove_disk= raid5_remove_disk,
  4500. .spare_active = raid5_spare_active,
  4501. .sync_request = sync_request,
  4502. .resize = raid5_resize,
  4503. #ifdef CONFIG_MD_RAID5_RESHAPE
  4504. .check_reshape = raid5_check_reshape,
  4505. .start_reshape = raid5_start_reshape,
  4506. #endif
  4507. .quiesce = raid5_quiesce,
  4508. .takeover = raid5_takeover,
  4509. .reconfig = raid5_reconfig,
  4510. };
  4511. static struct mdk_personality raid4_personality =
  4512. {
  4513. .name = "raid4",
  4514. .level = 4,
  4515. .owner = THIS_MODULE,
  4516. .make_request = make_request,
  4517. .run = run,
  4518. .stop = stop,
  4519. .status = status,
  4520. .error_handler = error,
  4521. .hot_add_disk = raid5_add_disk,
  4522. .hot_remove_disk= raid5_remove_disk,
  4523. .spare_active = raid5_spare_active,
  4524. .sync_request = sync_request,
  4525. .resize = raid5_resize,
  4526. #ifdef CONFIG_MD_RAID5_RESHAPE
  4527. .check_reshape = raid5_check_reshape,
  4528. .start_reshape = raid5_start_reshape,
  4529. #endif
  4530. .quiesce = raid5_quiesce,
  4531. };
  4532. static int __init raid5_init(void)
  4533. {
  4534. int e;
  4535. e = raid6_select_algo();
  4536. if ( e )
  4537. return e;
  4538. register_md_personality(&raid6_personality);
  4539. register_md_personality(&raid5_personality);
  4540. register_md_personality(&raid4_personality);
  4541. return 0;
  4542. }
  4543. static void raid5_exit(void)
  4544. {
  4545. unregister_md_personality(&raid6_personality);
  4546. unregister_md_personality(&raid5_personality);
  4547. unregister_md_personality(&raid4_personality);
  4548. }
  4549. module_init(raid5_init);
  4550. module_exit(raid5_exit);
  4551. MODULE_LICENSE("GPL");
  4552. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  4553. MODULE_ALIAS("md-raid5");
  4554. MODULE_ALIAS("md-raid4");
  4555. MODULE_ALIAS("md-level-5");
  4556. MODULE_ALIAS("md-level-4");
  4557. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  4558. MODULE_ALIAS("md-raid6");
  4559. MODULE_ALIAS("md-level-6");
  4560. /* This used to be two separate modules, they were: */
  4561. MODULE_ALIAS("raid5");
  4562. MODULE_ALIAS("raid6");