raid5.c 136 KB

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