raid5.c 132 KB

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