raid5.c 134 KB

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