raid5.c 145 KB

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