raid5.c 142 KB

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