raid5.c 135 KB

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