raid5.c 133 KB

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