raid5.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790
  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_parity6(struct stripe_head *sh, int method)
  1314. {
  1315. raid6_conf_t *conf = sh->raid_conf;
  1316. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  1317. struct bio *chosen;
  1318. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1319. void *ptrs[disks];
  1320. qd_idx = raid6_next_disk(pd_idx, disks);
  1321. d0_idx = raid6_next_disk(qd_idx, disks);
  1322. pr_debug("compute_parity, stripe %llu, method %d\n",
  1323. (unsigned long long)sh->sector, method);
  1324. switch(method) {
  1325. case READ_MODIFY_WRITE:
  1326. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  1327. case RECONSTRUCT_WRITE:
  1328. for (i= disks; i-- ;)
  1329. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  1330. chosen = sh->dev[i].towrite;
  1331. sh->dev[i].towrite = NULL;
  1332. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1333. wake_up(&conf->wait_for_overlap);
  1334. BUG_ON(sh->dev[i].written);
  1335. sh->dev[i].written = chosen;
  1336. }
  1337. break;
  1338. case CHECK_PARITY:
  1339. BUG(); /* Not implemented yet */
  1340. }
  1341. for (i = disks; i--;)
  1342. if (sh->dev[i].written) {
  1343. sector_t sector = sh->dev[i].sector;
  1344. struct bio *wbi = sh->dev[i].written;
  1345. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  1346. copy_data(1, wbi, sh->dev[i].page, sector);
  1347. wbi = r5_next_bio(wbi, sector);
  1348. }
  1349. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1350. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1351. }
  1352. // switch(method) {
  1353. // case RECONSTRUCT_WRITE:
  1354. // case CHECK_PARITY:
  1355. // case UPDATE_PARITY:
  1356. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  1357. /* FIX: Is this ordering of drives even remotely optimal? */
  1358. count = 0;
  1359. i = d0_idx;
  1360. do {
  1361. ptrs[count++] = page_address(sh->dev[i].page);
  1362. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1363. printk("block %d/%d not uptodate on parity calc\n", i,count);
  1364. i = raid6_next_disk(i, disks);
  1365. } while ( i != d0_idx );
  1366. // break;
  1367. // }
  1368. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  1369. switch(method) {
  1370. case RECONSTRUCT_WRITE:
  1371. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1372. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1373. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1374. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1375. break;
  1376. case UPDATE_PARITY:
  1377. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1378. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1379. break;
  1380. }
  1381. }
  1382. /* Compute one missing block */
  1383. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1384. {
  1385. int i, count, disks = sh->disks;
  1386. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  1387. int pd_idx = sh->pd_idx;
  1388. int qd_idx = raid6_next_disk(pd_idx, disks);
  1389. pr_debug("compute_block_1, stripe %llu, idx %d\n",
  1390. (unsigned long long)sh->sector, dd_idx);
  1391. if ( dd_idx == qd_idx ) {
  1392. /* We're actually computing the Q drive */
  1393. compute_parity6(sh, UPDATE_PARITY);
  1394. } else {
  1395. dest = page_address(sh->dev[dd_idx].page);
  1396. if (!nozero) memset(dest, 0, STRIPE_SIZE);
  1397. count = 0;
  1398. for (i = disks ; i--; ) {
  1399. if (i == dd_idx || i == qd_idx)
  1400. continue;
  1401. p = page_address(sh->dev[i].page);
  1402. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1403. ptr[count++] = p;
  1404. else
  1405. printk("compute_block() %d, stripe %llu, %d"
  1406. " not present\n", dd_idx,
  1407. (unsigned long long)sh->sector, i);
  1408. check_xor();
  1409. }
  1410. if (count)
  1411. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  1412. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1413. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1414. }
  1415. }
  1416. /* Compute two missing blocks */
  1417. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1418. {
  1419. int i, count, disks = sh->disks;
  1420. int pd_idx = sh->pd_idx;
  1421. int qd_idx = raid6_next_disk(pd_idx, disks);
  1422. int d0_idx = raid6_next_disk(qd_idx, disks);
  1423. int faila, failb;
  1424. /* faila and failb are disk numbers relative to d0_idx */
  1425. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1426. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1427. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1428. BUG_ON(faila == failb);
  1429. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1430. pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1431. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1432. if ( failb == disks-1 ) {
  1433. /* Q disk is one of the missing disks */
  1434. if ( faila == disks-2 ) {
  1435. /* Missing P+Q, just recompute */
  1436. compute_parity6(sh, UPDATE_PARITY);
  1437. return;
  1438. } else {
  1439. /* We're missing D+Q; recompute D from P */
  1440. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1441. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1442. return;
  1443. }
  1444. }
  1445. /* We're missing D+P or D+D; build pointer table */
  1446. {
  1447. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1448. void *ptrs[disks];
  1449. count = 0;
  1450. i = d0_idx;
  1451. do {
  1452. ptrs[count++] = page_address(sh->dev[i].page);
  1453. i = raid6_next_disk(i, disks);
  1454. if (i != dd_idx1 && i != dd_idx2 &&
  1455. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1456. printk("compute_2 with missing block %d/%d\n", count, i);
  1457. } while ( i != d0_idx );
  1458. if ( failb == disks-2 ) {
  1459. /* We're missing D+P. */
  1460. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1461. } else {
  1462. /* We're missing D+D. */
  1463. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1464. }
  1465. /* Both the above update both missing blocks */
  1466. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1467. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1468. }
  1469. }
  1470. static int
  1471. handle_write_operations5(struct stripe_head *sh, int rcw, int expand)
  1472. {
  1473. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1474. int locked = 0;
  1475. if (rcw) {
  1476. /* if we are not expanding this is a proper write request, and
  1477. * there will be bios with new data to be drained into the
  1478. * stripe cache
  1479. */
  1480. if (!expand) {
  1481. set_bit(STRIPE_OP_BIODRAIN, &sh->ops.pending);
  1482. sh->ops.count++;
  1483. }
  1484. set_bit(STRIPE_OP_POSTXOR, &sh->ops.pending);
  1485. sh->ops.count++;
  1486. for (i = disks; i--; ) {
  1487. struct r5dev *dev = &sh->dev[i];
  1488. if (dev->towrite) {
  1489. set_bit(R5_LOCKED, &dev->flags);
  1490. if (!expand)
  1491. clear_bit(R5_UPTODATE, &dev->flags);
  1492. locked++;
  1493. }
  1494. }
  1495. } else {
  1496. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1497. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1498. set_bit(STRIPE_OP_PREXOR, &sh->ops.pending);
  1499. set_bit(STRIPE_OP_BIODRAIN, &sh->ops.pending);
  1500. set_bit(STRIPE_OP_POSTXOR, &sh->ops.pending);
  1501. sh->ops.count += 3;
  1502. for (i = disks; i--; ) {
  1503. struct r5dev *dev = &sh->dev[i];
  1504. if (i == pd_idx)
  1505. continue;
  1506. /* For a read-modify write there may be blocks that are
  1507. * locked for reading while others are ready to be
  1508. * written so we distinguish these blocks by the
  1509. * R5_Wantprexor bit
  1510. */
  1511. if (dev->towrite &&
  1512. (test_bit(R5_UPTODATE, &dev->flags) ||
  1513. test_bit(R5_Wantcompute, &dev->flags))) {
  1514. set_bit(R5_Wantprexor, &dev->flags);
  1515. set_bit(R5_LOCKED, &dev->flags);
  1516. clear_bit(R5_UPTODATE, &dev->flags);
  1517. locked++;
  1518. }
  1519. }
  1520. }
  1521. /* keep the parity disk locked while asynchronous operations
  1522. * are in flight
  1523. */
  1524. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1525. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1526. locked++;
  1527. pr_debug("%s: stripe %llu locked: %d pending: %lx\n",
  1528. __FUNCTION__, (unsigned long long)sh->sector,
  1529. locked, sh->ops.pending);
  1530. return locked;
  1531. }
  1532. /*
  1533. * Each stripe/dev can have one or more bion attached.
  1534. * toread/towrite point to the first in a chain.
  1535. * The bi_next chain must be in order.
  1536. */
  1537. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1538. {
  1539. struct bio **bip;
  1540. raid5_conf_t *conf = sh->raid_conf;
  1541. int firstwrite=0;
  1542. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1543. (unsigned long long)bi->bi_sector,
  1544. (unsigned long long)sh->sector);
  1545. spin_lock(&sh->lock);
  1546. spin_lock_irq(&conf->device_lock);
  1547. if (forwrite) {
  1548. bip = &sh->dev[dd_idx].towrite;
  1549. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1550. firstwrite = 1;
  1551. } else
  1552. bip = &sh->dev[dd_idx].toread;
  1553. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1554. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1555. goto overlap;
  1556. bip = & (*bip)->bi_next;
  1557. }
  1558. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1559. goto overlap;
  1560. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1561. if (*bip)
  1562. bi->bi_next = *bip;
  1563. *bip = bi;
  1564. bi->bi_phys_segments ++;
  1565. spin_unlock_irq(&conf->device_lock);
  1566. spin_unlock(&sh->lock);
  1567. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1568. (unsigned long long)bi->bi_sector,
  1569. (unsigned long long)sh->sector, dd_idx);
  1570. if (conf->mddev->bitmap && firstwrite) {
  1571. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1572. STRIPE_SECTORS, 0);
  1573. sh->bm_seq = conf->seq_flush+1;
  1574. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1575. }
  1576. if (forwrite) {
  1577. /* check if page is covered */
  1578. sector_t sector = sh->dev[dd_idx].sector;
  1579. for (bi=sh->dev[dd_idx].towrite;
  1580. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1581. bi && bi->bi_sector <= sector;
  1582. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1583. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1584. sector = bi->bi_sector + (bi->bi_size>>9);
  1585. }
  1586. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1587. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1588. }
  1589. return 1;
  1590. overlap:
  1591. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1592. spin_unlock_irq(&conf->device_lock);
  1593. spin_unlock(&sh->lock);
  1594. return 0;
  1595. }
  1596. static void end_reshape(raid5_conf_t *conf);
  1597. static int page_is_zero(struct page *p)
  1598. {
  1599. char *a = page_address(p);
  1600. return ((*(u32*)a) == 0 &&
  1601. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1602. }
  1603. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1604. {
  1605. int sectors_per_chunk = conf->chunk_size >> 9;
  1606. int pd_idx, dd_idx;
  1607. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1608. raid5_compute_sector(stripe * (disks - conf->max_degraded)
  1609. *sectors_per_chunk + chunk_offset,
  1610. disks, disks - conf->max_degraded,
  1611. &dd_idx, &pd_idx, conf);
  1612. return pd_idx;
  1613. }
  1614. static void
  1615. handle_requests_to_failed_array(raid5_conf_t *conf, struct stripe_head *sh,
  1616. struct stripe_head_state *s, int disks,
  1617. struct bio **return_bi)
  1618. {
  1619. int i;
  1620. for (i = disks; i--; ) {
  1621. struct bio *bi;
  1622. int bitmap_end = 0;
  1623. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1624. mdk_rdev_t *rdev;
  1625. rcu_read_lock();
  1626. rdev = rcu_dereference(conf->disks[i].rdev);
  1627. if (rdev && test_bit(In_sync, &rdev->flags))
  1628. /* multiple read failures in one stripe */
  1629. md_error(conf->mddev, rdev);
  1630. rcu_read_unlock();
  1631. }
  1632. spin_lock_irq(&conf->device_lock);
  1633. /* fail all writes first */
  1634. bi = sh->dev[i].towrite;
  1635. sh->dev[i].towrite = NULL;
  1636. if (bi) {
  1637. s->to_write--;
  1638. bitmap_end = 1;
  1639. }
  1640. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1641. wake_up(&conf->wait_for_overlap);
  1642. while (bi && bi->bi_sector <
  1643. sh->dev[i].sector + STRIPE_SECTORS) {
  1644. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1645. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1646. if (--bi->bi_phys_segments == 0) {
  1647. md_write_end(conf->mddev);
  1648. bi->bi_next = *return_bi;
  1649. *return_bi = bi;
  1650. }
  1651. bi = nextbi;
  1652. }
  1653. /* and fail all 'written' */
  1654. bi = sh->dev[i].written;
  1655. sh->dev[i].written = NULL;
  1656. if (bi) bitmap_end = 1;
  1657. while (bi && bi->bi_sector <
  1658. sh->dev[i].sector + STRIPE_SECTORS) {
  1659. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1660. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1661. if (--bi->bi_phys_segments == 0) {
  1662. md_write_end(conf->mddev);
  1663. bi->bi_next = *return_bi;
  1664. *return_bi = bi;
  1665. }
  1666. bi = bi2;
  1667. }
  1668. /* fail any reads if this device is non-operational and
  1669. * the data has not reached the cache yet.
  1670. */
  1671. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  1672. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1673. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  1674. bi = sh->dev[i].toread;
  1675. sh->dev[i].toread = NULL;
  1676. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1677. wake_up(&conf->wait_for_overlap);
  1678. if (bi) s->to_read--;
  1679. while (bi && bi->bi_sector <
  1680. sh->dev[i].sector + STRIPE_SECTORS) {
  1681. struct bio *nextbi =
  1682. r5_next_bio(bi, sh->dev[i].sector);
  1683. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1684. if (--bi->bi_phys_segments == 0) {
  1685. bi->bi_next = *return_bi;
  1686. *return_bi = bi;
  1687. }
  1688. bi = nextbi;
  1689. }
  1690. }
  1691. spin_unlock_irq(&conf->device_lock);
  1692. if (bitmap_end)
  1693. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1694. STRIPE_SECTORS, 0, 0);
  1695. }
  1696. }
  1697. /* __handle_issuing_new_read_requests5 - returns 0 if there are no more disks
  1698. * to process
  1699. */
  1700. static int __handle_issuing_new_read_requests5(struct stripe_head *sh,
  1701. struct stripe_head_state *s, int disk_idx, int disks)
  1702. {
  1703. struct r5dev *dev = &sh->dev[disk_idx];
  1704. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  1705. /* don't schedule compute operations or reads on the parity block while
  1706. * a check is in flight
  1707. */
  1708. if ((disk_idx == sh->pd_idx) &&
  1709. test_bit(STRIPE_OP_CHECK, &sh->ops.pending))
  1710. return ~0;
  1711. /* is the data in this block needed, and can we get it? */
  1712. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1713. !test_bit(R5_UPTODATE, &dev->flags) && (dev->toread ||
  1714. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1715. s->syncing || s->expanding || (s->failed &&
  1716. (failed_dev->toread || (failed_dev->towrite &&
  1717. !test_bit(R5_OVERWRITE, &failed_dev->flags)
  1718. ))))) {
  1719. /* 1/ We would like to get this block, possibly by computing it,
  1720. * but we might not be able to.
  1721. *
  1722. * 2/ Since parity check operations potentially make the parity
  1723. * block !uptodate it will need to be refreshed before any
  1724. * compute operations on data disks are scheduled.
  1725. *
  1726. * 3/ We hold off parity block re-reads until check operations
  1727. * have quiesced.
  1728. */
  1729. if ((s->uptodate == disks - 1) &&
  1730. !test_bit(STRIPE_OP_CHECK, &sh->ops.pending)) {
  1731. set_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending);
  1732. set_bit(R5_Wantcompute, &dev->flags);
  1733. sh->ops.target = disk_idx;
  1734. s->req_compute = 1;
  1735. sh->ops.count++;
  1736. /* Careful: from this point on 'uptodate' is in the eye
  1737. * of raid5_run_ops which services 'compute' operations
  1738. * before writes. R5_Wantcompute flags a block that will
  1739. * be R5_UPTODATE by the time it is needed for a
  1740. * subsequent operation.
  1741. */
  1742. s->uptodate++;
  1743. return 0; /* uptodate + compute == disks */
  1744. } else if ((s->uptodate < disks - 1) &&
  1745. test_bit(R5_Insync, &dev->flags)) {
  1746. /* Note: we hold off compute operations while checks are
  1747. * in flight, but we still prefer 'compute' over 'read'
  1748. * hence we only read if (uptodate < * disks-1)
  1749. */
  1750. set_bit(R5_LOCKED, &dev->flags);
  1751. set_bit(R5_Wantread, &dev->flags);
  1752. if (!test_and_set_bit(STRIPE_OP_IO, &sh->ops.pending))
  1753. sh->ops.count++;
  1754. s->locked++;
  1755. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  1756. s->syncing);
  1757. }
  1758. }
  1759. return ~0;
  1760. }
  1761. static void handle_issuing_new_read_requests5(struct stripe_head *sh,
  1762. struct stripe_head_state *s, int disks)
  1763. {
  1764. int i;
  1765. /* Clear completed compute operations. Parity recovery
  1766. * (STRIPE_OP_MOD_REPAIR_PD) implies a write-back which is handled
  1767. * later on in this routine
  1768. */
  1769. if (test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.complete) &&
  1770. !test_bit(STRIPE_OP_MOD_REPAIR_PD, &sh->ops.pending)) {
  1771. clear_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.complete);
  1772. clear_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.ack);
  1773. clear_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending);
  1774. }
  1775. /* look for blocks to read/compute, skip this if a compute
  1776. * is already in flight, or if the stripe contents are in the
  1777. * midst of changing due to a write
  1778. */
  1779. if (!test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending) &&
  1780. !test_bit(STRIPE_OP_PREXOR, &sh->ops.pending) &&
  1781. !test_bit(STRIPE_OP_POSTXOR, &sh->ops.pending)) {
  1782. for (i = disks; i--; )
  1783. if (__handle_issuing_new_read_requests5(
  1784. sh, s, i, disks) == 0)
  1785. break;
  1786. }
  1787. set_bit(STRIPE_HANDLE, &sh->state);
  1788. }
  1789. static void handle_issuing_new_read_requests6(struct stripe_head *sh,
  1790. struct stripe_head_state *s, struct r6_state *r6s,
  1791. int disks)
  1792. {
  1793. int i;
  1794. for (i = disks; i--; ) {
  1795. struct r5dev *dev = &sh->dev[i];
  1796. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1797. !test_bit(R5_UPTODATE, &dev->flags) &&
  1798. (dev->toread || (dev->towrite &&
  1799. !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1800. s->syncing || s->expanding ||
  1801. (s->failed >= 1 &&
  1802. (sh->dev[r6s->failed_num[0]].toread ||
  1803. s->to_write)) ||
  1804. (s->failed >= 2 &&
  1805. (sh->dev[r6s->failed_num[1]].toread ||
  1806. s->to_write)))) {
  1807. /* we would like to get this block, possibly
  1808. * by computing it, but we might not be able to
  1809. */
  1810. if (s->uptodate == disks-1) {
  1811. pr_debug("Computing stripe %llu block %d\n",
  1812. (unsigned long long)sh->sector, i);
  1813. compute_block_1(sh, i, 0);
  1814. s->uptodate++;
  1815. } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
  1816. /* Computing 2-failure is *very* expensive; only
  1817. * do it if failed >= 2
  1818. */
  1819. int other;
  1820. for (other = disks; other--; ) {
  1821. if (other == i)
  1822. continue;
  1823. if (!test_bit(R5_UPTODATE,
  1824. &sh->dev[other].flags))
  1825. break;
  1826. }
  1827. BUG_ON(other < 0);
  1828. pr_debug("Computing stripe %llu blocks %d,%d\n",
  1829. (unsigned long long)sh->sector,
  1830. i, other);
  1831. compute_block_2(sh, i, other);
  1832. s->uptodate += 2;
  1833. } else if (test_bit(R5_Insync, &dev->flags)) {
  1834. set_bit(R5_LOCKED, &dev->flags);
  1835. set_bit(R5_Wantread, &dev->flags);
  1836. s->locked++;
  1837. pr_debug("Reading block %d (sync=%d)\n",
  1838. i, s->syncing);
  1839. }
  1840. }
  1841. }
  1842. set_bit(STRIPE_HANDLE, &sh->state);
  1843. }
  1844. /* handle_completed_write_requests
  1845. * any written block on an uptodate or failed drive can be returned.
  1846. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1847. * never LOCKED, so we don't need to test 'failed' directly.
  1848. */
  1849. static void handle_completed_write_requests(raid5_conf_t *conf,
  1850. struct stripe_head *sh, int disks, struct bio **return_bi)
  1851. {
  1852. int i;
  1853. struct r5dev *dev;
  1854. for (i = disks; i--; )
  1855. if (sh->dev[i].written) {
  1856. dev = &sh->dev[i];
  1857. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1858. test_bit(R5_UPTODATE, &dev->flags)) {
  1859. /* We can return any write requests */
  1860. struct bio *wbi, *wbi2;
  1861. int bitmap_end = 0;
  1862. pr_debug("Return write for disc %d\n", i);
  1863. spin_lock_irq(&conf->device_lock);
  1864. wbi = dev->written;
  1865. dev->written = NULL;
  1866. while (wbi && wbi->bi_sector <
  1867. dev->sector + STRIPE_SECTORS) {
  1868. wbi2 = r5_next_bio(wbi, dev->sector);
  1869. if (--wbi->bi_phys_segments == 0) {
  1870. md_write_end(conf->mddev);
  1871. wbi->bi_next = *return_bi;
  1872. *return_bi = wbi;
  1873. }
  1874. wbi = wbi2;
  1875. }
  1876. if (dev->towrite == NULL)
  1877. bitmap_end = 1;
  1878. spin_unlock_irq(&conf->device_lock);
  1879. if (bitmap_end)
  1880. bitmap_endwrite(conf->mddev->bitmap,
  1881. sh->sector,
  1882. STRIPE_SECTORS,
  1883. !test_bit(STRIPE_DEGRADED, &sh->state),
  1884. 0);
  1885. }
  1886. }
  1887. }
  1888. static void handle_issuing_new_write_requests5(raid5_conf_t *conf,
  1889. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  1890. {
  1891. int rmw = 0, rcw = 0, i;
  1892. for (i = disks; i--; ) {
  1893. /* would I have to read this buffer for read_modify_write */
  1894. struct r5dev *dev = &sh->dev[i];
  1895. if ((dev->towrite || i == sh->pd_idx) &&
  1896. !test_bit(R5_LOCKED, &dev->flags) &&
  1897. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1898. test_bit(R5_Wantcompute, &dev->flags))) {
  1899. if (test_bit(R5_Insync, &dev->flags))
  1900. rmw++;
  1901. else
  1902. rmw += 2*disks; /* cannot read it */
  1903. }
  1904. /* Would I have to read this buffer for reconstruct_write */
  1905. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1906. !test_bit(R5_LOCKED, &dev->flags) &&
  1907. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1908. test_bit(R5_Wantcompute, &dev->flags))) {
  1909. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1910. else
  1911. rcw += 2*disks;
  1912. }
  1913. }
  1914. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  1915. (unsigned long long)sh->sector, rmw, rcw);
  1916. set_bit(STRIPE_HANDLE, &sh->state);
  1917. if (rmw < rcw && rmw > 0)
  1918. /* prefer read-modify-write, but need to get some data */
  1919. for (i = disks; i--; ) {
  1920. struct r5dev *dev = &sh->dev[i];
  1921. if ((dev->towrite || i == sh->pd_idx) &&
  1922. !test_bit(R5_LOCKED, &dev->flags) &&
  1923. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1924. test_bit(R5_Wantcompute, &dev->flags)) &&
  1925. test_bit(R5_Insync, &dev->flags)) {
  1926. if (
  1927. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1928. pr_debug("Read_old block "
  1929. "%d for r-m-w\n", i);
  1930. set_bit(R5_LOCKED, &dev->flags);
  1931. set_bit(R5_Wantread, &dev->flags);
  1932. if (!test_and_set_bit(
  1933. STRIPE_OP_IO, &sh->ops.pending))
  1934. sh->ops.count++;
  1935. s->locked++;
  1936. } else {
  1937. set_bit(STRIPE_DELAYED, &sh->state);
  1938. set_bit(STRIPE_HANDLE, &sh->state);
  1939. }
  1940. }
  1941. }
  1942. if (rcw <= rmw && rcw > 0)
  1943. /* want reconstruct write, but need to get some data */
  1944. for (i = disks; i--; ) {
  1945. struct r5dev *dev = &sh->dev[i];
  1946. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  1947. i != sh->pd_idx &&
  1948. !test_bit(R5_LOCKED, &dev->flags) &&
  1949. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1950. test_bit(R5_Wantcompute, &dev->flags)) &&
  1951. test_bit(R5_Insync, &dev->flags)) {
  1952. if (
  1953. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1954. pr_debug("Read_old block "
  1955. "%d for Reconstruct\n", i);
  1956. set_bit(R5_LOCKED, &dev->flags);
  1957. set_bit(R5_Wantread, &dev->flags);
  1958. if (!test_and_set_bit(
  1959. STRIPE_OP_IO, &sh->ops.pending))
  1960. sh->ops.count++;
  1961. s->locked++;
  1962. } else {
  1963. set_bit(STRIPE_DELAYED, &sh->state);
  1964. set_bit(STRIPE_HANDLE, &sh->state);
  1965. }
  1966. }
  1967. }
  1968. /* now if nothing is locked, and if we have enough data,
  1969. * we can start a write request
  1970. */
  1971. /* since handle_stripe can be called at any time we need to handle the
  1972. * case where a compute block operation has been submitted and then a
  1973. * subsequent call wants to start a write request. raid5_run_ops only
  1974. * handles the case where compute block and postxor are requested
  1975. * simultaneously. If this is not the case then new writes need to be
  1976. * held off until the compute completes.
  1977. */
  1978. if ((s->req_compute ||
  1979. !test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending)) &&
  1980. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  1981. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  1982. s->locked += handle_write_operations5(sh, rcw == 0, 0);
  1983. }
  1984. static void handle_issuing_new_write_requests6(raid5_conf_t *conf,
  1985. struct stripe_head *sh, struct stripe_head_state *s,
  1986. struct r6_state *r6s, int disks)
  1987. {
  1988. int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
  1989. int qd_idx = r6s->qd_idx;
  1990. for (i = disks; i--; ) {
  1991. struct r5dev *dev = &sh->dev[i];
  1992. /* Would I have to read this buffer for reconstruct_write */
  1993. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1994. && i != pd_idx && i != qd_idx
  1995. && (!test_bit(R5_LOCKED, &dev->flags)
  1996. ) &&
  1997. !test_bit(R5_UPTODATE, &dev->flags)) {
  1998. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1999. else {
  2000. pr_debug("raid6: must_compute: "
  2001. "disk %d flags=%#lx\n", i, dev->flags);
  2002. must_compute++;
  2003. }
  2004. }
  2005. }
  2006. pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
  2007. (unsigned long long)sh->sector, rcw, must_compute);
  2008. set_bit(STRIPE_HANDLE, &sh->state);
  2009. if (rcw > 0)
  2010. /* want reconstruct write, but need to get some data */
  2011. for (i = disks; i--; ) {
  2012. struct r5dev *dev = &sh->dev[i];
  2013. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2014. && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
  2015. && !test_bit(R5_LOCKED, &dev->flags) &&
  2016. !test_bit(R5_UPTODATE, &dev->flags) &&
  2017. test_bit(R5_Insync, &dev->flags)) {
  2018. if (
  2019. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2020. pr_debug("Read_old stripe %llu "
  2021. "block %d for Reconstruct\n",
  2022. (unsigned long long)sh->sector, i);
  2023. set_bit(R5_LOCKED, &dev->flags);
  2024. set_bit(R5_Wantread, &dev->flags);
  2025. s->locked++;
  2026. } else {
  2027. pr_debug("Request delayed stripe %llu "
  2028. "block %d for Reconstruct\n",
  2029. (unsigned long long)sh->sector, i);
  2030. set_bit(STRIPE_DELAYED, &sh->state);
  2031. set_bit(STRIPE_HANDLE, &sh->state);
  2032. }
  2033. }
  2034. }
  2035. /* now if nothing is locked, and if we have enough data, we can start a
  2036. * write request
  2037. */
  2038. if (s->locked == 0 && rcw == 0 &&
  2039. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2040. if (must_compute > 0) {
  2041. /* We have failed blocks and need to compute them */
  2042. switch (s->failed) {
  2043. case 0:
  2044. BUG();
  2045. case 1:
  2046. compute_block_1(sh, r6s->failed_num[0], 0);
  2047. break;
  2048. case 2:
  2049. compute_block_2(sh, r6s->failed_num[0],
  2050. r6s->failed_num[1]);
  2051. break;
  2052. default: /* This request should have been failed? */
  2053. BUG();
  2054. }
  2055. }
  2056. pr_debug("Computing parity for stripe %llu\n",
  2057. (unsigned long long)sh->sector);
  2058. compute_parity6(sh, RECONSTRUCT_WRITE);
  2059. /* now every locked buffer is ready to be written */
  2060. for (i = disks; i--; )
  2061. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2062. pr_debug("Writing stripe %llu block %d\n",
  2063. (unsigned long long)sh->sector, i);
  2064. s->locked++;
  2065. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2066. }
  2067. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2068. set_bit(STRIPE_INSYNC, &sh->state);
  2069. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2070. atomic_dec(&conf->preread_active_stripes);
  2071. if (atomic_read(&conf->preread_active_stripes) <
  2072. IO_THRESHOLD)
  2073. md_wakeup_thread(conf->mddev->thread);
  2074. }
  2075. }
  2076. }
  2077. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2078. struct stripe_head_state *s, int disks)
  2079. {
  2080. set_bit(STRIPE_HANDLE, &sh->state);
  2081. /* Take one of the following actions:
  2082. * 1/ start a check parity operation if (uptodate == disks)
  2083. * 2/ finish a check parity operation and act on the result
  2084. * 3/ skip to the writeback section if we previously
  2085. * initiated a recovery operation
  2086. */
  2087. if (s->failed == 0 &&
  2088. !test_bit(STRIPE_OP_MOD_REPAIR_PD, &sh->ops.pending)) {
  2089. if (!test_and_set_bit(STRIPE_OP_CHECK, &sh->ops.pending)) {
  2090. BUG_ON(s->uptodate != disks);
  2091. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2092. sh->ops.count++;
  2093. s->uptodate--;
  2094. } else if (
  2095. test_and_clear_bit(STRIPE_OP_CHECK, &sh->ops.complete)) {
  2096. clear_bit(STRIPE_OP_CHECK, &sh->ops.ack);
  2097. clear_bit(STRIPE_OP_CHECK, &sh->ops.pending);
  2098. if (sh->ops.zero_sum_result == 0)
  2099. /* parity is correct (on disc,
  2100. * not in buffer any more)
  2101. */
  2102. set_bit(STRIPE_INSYNC, &sh->state);
  2103. else {
  2104. conf->mddev->resync_mismatches +=
  2105. STRIPE_SECTORS;
  2106. if (test_bit(
  2107. MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2108. /* don't try to repair!! */
  2109. set_bit(STRIPE_INSYNC, &sh->state);
  2110. else {
  2111. set_bit(STRIPE_OP_COMPUTE_BLK,
  2112. &sh->ops.pending);
  2113. set_bit(STRIPE_OP_MOD_REPAIR_PD,
  2114. &sh->ops.pending);
  2115. set_bit(R5_Wantcompute,
  2116. &sh->dev[sh->pd_idx].flags);
  2117. sh->ops.target = sh->pd_idx;
  2118. sh->ops.count++;
  2119. s->uptodate++;
  2120. }
  2121. }
  2122. }
  2123. }
  2124. /* check if we can clear a parity disk reconstruct */
  2125. if (test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.complete) &&
  2126. test_bit(STRIPE_OP_MOD_REPAIR_PD, &sh->ops.pending)) {
  2127. clear_bit(STRIPE_OP_MOD_REPAIR_PD, &sh->ops.pending);
  2128. clear_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.complete);
  2129. clear_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.ack);
  2130. clear_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending);
  2131. }
  2132. /* Wait for check parity and compute block operations to complete
  2133. * before write-back
  2134. */
  2135. if (!test_bit(STRIPE_INSYNC, &sh->state) &&
  2136. !test_bit(STRIPE_OP_CHECK, &sh->ops.pending) &&
  2137. !test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending)) {
  2138. struct r5dev *dev;
  2139. /* either failed parity check, or recovery is happening */
  2140. if (s->failed == 0)
  2141. s->failed_num = sh->pd_idx;
  2142. dev = &sh->dev[s->failed_num];
  2143. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2144. BUG_ON(s->uptodate != disks);
  2145. set_bit(R5_LOCKED, &dev->flags);
  2146. set_bit(R5_Wantwrite, &dev->flags);
  2147. if (!test_and_set_bit(STRIPE_OP_IO, &sh->ops.pending))
  2148. sh->ops.count++;
  2149. clear_bit(STRIPE_DEGRADED, &sh->state);
  2150. s->locked++;
  2151. set_bit(STRIPE_INSYNC, &sh->state);
  2152. }
  2153. }
  2154. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2155. struct stripe_head_state *s,
  2156. struct r6_state *r6s, struct page *tmp_page,
  2157. int disks)
  2158. {
  2159. int update_p = 0, update_q = 0;
  2160. struct r5dev *dev;
  2161. int pd_idx = sh->pd_idx;
  2162. int qd_idx = r6s->qd_idx;
  2163. set_bit(STRIPE_HANDLE, &sh->state);
  2164. BUG_ON(s->failed > 2);
  2165. BUG_ON(s->uptodate < disks);
  2166. /* Want to check and possibly repair P and Q.
  2167. * However there could be one 'failed' device, in which
  2168. * case we can only check one of them, possibly using the
  2169. * other to generate missing data
  2170. */
  2171. /* If !tmp_page, we cannot do the calculations,
  2172. * but as we have set STRIPE_HANDLE, we will soon be called
  2173. * by stripe_handle with a tmp_page - just wait until then.
  2174. */
  2175. if (tmp_page) {
  2176. if (s->failed == r6s->q_failed) {
  2177. /* The only possible failed device holds 'Q', so it
  2178. * makes sense to check P (If anything else were failed,
  2179. * we would have used P to recreate it).
  2180. */
  2181. compute_block_1(sh, pd_idx, 1);
  2182. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2183. compute_block_1(sh, pd_idx, 0);
  2184. update_p = 1;
  2185. }
  2186. }
  2187. if (!r6s->q_failed && s->failed < 2) {
  2188. /* q is not failed, and we didn't use it to generate
  2189. * anything, so it makes sense to check it
  2190. */
  2191. memcpy(page_address(tmp_page),
  2192. page_address(sh->dev[qd_idx].page),
  2193. STRIPE_SIZE);
  2194. compute_parity6(sh, UPDATE_PARITY);
  2195. if (memcmp(page_address(tmp_page),
  2196. page_address(sh->dev[qd_idx].page),
  2197. STRIPE_SIZE) != 0) {
  2198. clear_bit(STRIPE_INSYNC, &sh->state);
  2199. update_q = 1;
  2200. }
  2201. }
  2202. if (update_p || update_q) {
  2203. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2204. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2205. /* don't try to repair!! */
  2206. update_p = update_q = 0;
  2207. }
  2208. /* now write out any block on a failed drive,
  2209. * or P or Q if they need it
  2210. */
  2211. if (s->failed == 2) {
  2212. dev = &sh->dev[r6s->failed_num[1]];
  2213. s->locked++;
  2214. set_bit(R5_LOCKED, &dev->flags);
  2215. set_bit(R5_Wantwrite, &dev->flags);
  2216. }
  2217. if (s->failed >= 1) {
  2218. dev = &sh->dev[r6s->failed_num[0]];
  2219. s->locked++;
  2220. set_bit(R5_LOCKED, &dev->flags);
  2221. set_bit(R5_Wantwrite, &dev->flags);
  2222. }
  2223. if (update_p) {
  2224. dev = &sh->dev[pd_idx];
  2225. s->locked++;
  2226. set_bit(R5_LOCKED, &dev->flags);
  2227. set_bit(R5_Wantwrite, &dev->flags);
  2228. }
  2229. if (update_q) {
  2230. dev = &sh->dev[qd_idx];
  2231. s->locked++;
  2232. set_bit(R5_LOCKED, &dev->flags);
  2233. set_bit(R5_Wantwrite, &dev->flags);
  2234. }
  2235. clear_bit(STRIPE_DEGRADED, &sh->state);
  2236. set_bit(STRIPE_INSYNC, &sh->state);
  2237. }
  2238. }
  2239. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2240. struct r6_state *r6s)
  2241. {
  2242. int i;
  2243. /* We have read all the blocks in this stripe and now we need to
  2244. * copy some of them into a target stripe for expand.
  2245. */
  2246. struct dma_async_tx_descriptor *tx = NULL;
  2247. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2248. for (i = 0; i < sh->disks; i++)
  2249. if (i != sh->pd_idx && (r6s && i != r6s->qd_idx)) {
  2250. int dd_idx, pd_idx, j;
  2251. struct stripe_head *sh2;
  2252. sector_t bn = compute_blocknr(sh, i);
  2253. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  2254. conf->raid_disks -
  2255. conf->max_degraded, &dd_idx,
  2256. &pd_idx, conf);
  2257. sh2 = get_active_stripe(conf, s, conf->raid_disks,
  2258. pd_idx, 1);
  2259. if (sh2 == NULL)
  2260. /* so far only the early blocks of this stripe
  2261. * have been requested. When later blocks
  2262. * get requested, we will try again
  2263. */
  2264. continue;
  2265. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2266. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2267. /* must have already done this block */
  2268. release_stripe(sh2);
  2269. continue;
  2270. }
  2271. /* place all the copies on one channel */
  2272. tx = async_memcpy(sh2->dev[dd_idx].page,
  2273. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2274. ASYNC_TX_DEP_ACK, tx, NULL, NULL);
  2275. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2276. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2277. for (j = 0; j < conf->raid_disks; j++)
  2278. if (j != sh2->pd_idx &&
  2279. (r6s && j != r6s->qd_idx) &&
  2280. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2281. break;
  2282. if (j == conf->raid_disks) {
  2283. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2284. set_bit(STRIPE_HANDLE, &sh2->state);
  2285. }
  2286. release_stripe(sh2);
  2287. /* done submitting copies, wait for them to complete */
  2288. if (i + 1 >= sh->disks) {
  2289. async_tx_ack(tx);
  2290. dma_wait_for_async_tx(tx);
  2291. }
  2292. }
  2293. }
  2294. /*
  2295. * handle_stripe - do things to a stripe.
  2296. *
  2297. * We lock the stripe and then examine the state of various bits
  2298. * to see what needs to be done.
  2299. * Possible results:
  2300. * return some read request which now have data
  2301. * return some write requests which are safely on disc
  2302. * schedule a read on some buffers
  2303. * schedule a write of some buffers
  2304. * return confirmation of parity correctness
  2305. *
  2306. * buffers are taken off read_list or write_list, and bh_cache buffers
  2307. * get BH_Lock set before the stripe lock is released.
  2308. *
  2309. */
  2310. static void handle_stripe5(struct stripe_head *sh)
  2311. {
  2312. raid5_conf_t *conf = sh->raid_conf;
  2313. int disks = sh->disks, i;
  2314. struct bio *return_bi = NULL;
  2315. struct stripe_head_state s;
  2316. struct r5dev *dev;
  2317. unsigned long pending = 0;
  2318. memset(&s, 0, sizeof(s));
  2319. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d "
  2320. "ops=%lx:%lx:%lx\n", (unsigned long long)sh->sector, sh->state,
  2321. atomic_read(&sh->count), sh->pd_idx,
  2322. sh->ops.pending, sh->ops.ack, sh->ops.complete);
  2323. spin_lock(&sh->lock);
  2324. clear_bit(STRIPE_HANDLE, &sh->state);
  2325. clear_bit(STRIPE_DELAYED, &sh->state);
  2326. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2327. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2328. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2329. /* Now to look around and see what can be done */
  2330. rcu_read_lock();
  2331. for (i=disks; i--; ) {
  2332. mdk_rdev_t *rdev;
  2333. struct r5dev *dev = &sh->dev[i];
  2334. clear_bit(R5_Insync, &dev->flags);
  2335. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2336. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2337. dev->towrite, dev->written);
  2338. /* maybe we can request a biofill operation
  2339. *
  2340. * new wantfill requests are only permitted while
  2341. * STRIPE_OP_BIOFILL is clear
  2342. */
  2343. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2344. !test_bit(STRIPE_OP_BIOFILL, &sh->ops.pending))
  2345. set_bit(R5_Wantfill, &dev->flags);
  2346. /* now count some things */
  2347. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2348. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2349. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2350. if (test_bit(R5_Wantfill, &dev->flags))
  2351. s.to_fill++;
  2352. else if (dev->toread)
  2353. s.to_read++;
  2354. if (dev->towrite) {
  2355. s.to_write++;
  2356. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2357. s.non_overwrite++;
  2358. }
  2359. if (dev->written)
  2360. s.written++;
  2361. rdev = rcu_dereference(conf->disks[i].rdev);
  2362. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2363. /* The ReadError flag will just be confusing now */
  2364. clear_bit(R5_ReadError, &dev->flags);
  2365. clear_bit(R5_ReWrite, &dev->flags);
  2366. }
  2367. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2368. || test_bit(R5_ReadError, &dev->flags)) {
  2369. s.failed++;
  2370. s.failed_num = i;
  2371. } else
  2372. set_bit(R5_Insync, &dev->flags);
  2373. }
  2374. rcu_read_unlock();
  2375. if (s.to_fill && !test_and_set_bit(STRIPE_OP_BIOFILL, &sh->ops.pending))
  2376. sh->ops.count++;
  2377. pr_debug("locked=%d uptodate=%d to_read=%d"
  2378. " to_write=%d failed=%d failed_num=%d\n",
  2379. s.locked, s.uptodate, s.to_read, s.to_write,
  2380. s.failed, s.failed_num);
  2381. /* check if the array has lost two devices and, if so, some requests might
  2382. * need to be failed
  2383. */
  2384. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  2385. handle_requests_to_failed_array(conf, sh, &s, disks,
  2386. &return_bi);
  2387. if (s.failed > 1 && s.syncing) {
  2388. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2389. clear_bit(STRIPE_SYNCING, &sh->state);
  2390. s.syncing = 0;
  2391. }
  2392. /* might be able to return some write requests if the parity block
  2393. * is safe, or on a failed drive
  2394. */
  2395. dev = &sh->dev[sh->pd_idx];
  2396. if ( s.written &&
  2397. ((test_bit(R5_Insync, &dev->flags) &&
  2398. !test_bit(R5_LOCKED, &dev->flags) &&
  2399. test_bit(R5_UPTODATE, &dev->flags)) ||
  2400. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2401. handle_completed_write_requests(conf, sh, disks, &return_bi);
  2402. /* Now we might consider reading some blocks, either to check/generate
  2403. * parity, or to satisfy requests
  2404. * or to load a block that is being partially written.
  2405. */
  2406. if (s.to_read || s.non_overwrite ||
  2407. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding ||
  2408. test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending))
  2409. handle_issuing_new_read_requests5(sh, &s, disks);
  2410. /* Now we check to see if any write operations have recently
  2411. * completed
  2412. */
  2413. /* leave prexor set until postxor is done, allows us to distinguish
  2414. * a rmw from a rcw during biodrain
  2415. */
  2416. if (test_bit(STRIPE_OP_PREXOR, &sh->ops.complete) &&
  2417. test_bit(STRIPE_OP_POSTXOR, &sh->ops.complete)) {
  2418. clear_bit(STRIPE_OP_PREXOR, &sh->ops.complete);
  2419. clear_bit(STRIPE_OP_PREXOR, &sh->ops.ack);
  2420. clear_bit(STRIPE_OP_PREXOR, &sh->ops.pending);
  2421. for (i = disks; i--; )
  2422. clear_bit(R5_Wantprexor, &sh->dev[i].flags);
  2423. }
  2424. /* if only POSTXOR is set then this is an 'expand' postxor */
  2425. if (test_bit(STRIPE_OP_BIODRAIN, &sh->ops.complete) &&
  2426. test_bit(STRIPE_OP_POSTXOR, &sh->ops.complete)) {
  2427. clear_bit(STRIPE_OP_BIODRAIN, &sh->ops.complete);
  2428. clear_bit(STRIPE_OP_BIODRAIN, &sh->ops.ack);
  2429. clear_bit(STRIPE_OP_BIODRAIN, &sh->ops.pending);
  2430. clear_bit(STRIPE_OP_POSTXOR, &sh->ops.complete);
  2431. clear_bit(STRIPE_OP_POSTXOR, &sh->ops.ack);
  2432. clear_bit(STRIPE_OP_POSTXOR, &sh->ops.pending);
  2433. /* All the 'written' buffers and the parity block are ready to
  2434. * be written back to disk
  2435. */
  2436. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2437. for (i = disks; i--; ) {
  2438. dev = &sh->dev[i];
  2439. if (test_bit(R5_LOCKED, &dev->flags) &&
  2440. (i == sh->pd_idx || dev->written)) {
  2441. pr_debug("Writing block %d\n", i);
  2442. set_bit(R5_Wantwrite, &dev->flags);
  2443. if (!test_and_set_bit(
  2444. STRIPE_OP_IO, &sh->ops.pending))
  2445. sh->ops.count++;
  2446. if (!test_bit(R5_Insync, &dev->flags) ||
  2447. (i == sh->pd_idx && s.failed == 0))
  2448. set_bit(STRIPE_INSYNC, &sh->state);
  2449. }
  2450. }
  2451. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2452. atomic_dec(&conf->preread_active_stripes);
  2453. if (atomic_read(&conf->preread_active_stripes) <
  2454. IO_THRESHOLD)
  2455. md_wakeup_thread(conf->mddev->thread);
  2456. }
  2457. }
  2458. /* Now to consider new write requests and what else, if anything
  2459. * should be read. We do not handle new writes when:
  2460. * 1/ A 'write' operation (copy+xor) is already in flight.
  2461. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2462. * block.
  2463. */
  2464. if (s.to_write && !test_bit(STRIPE_OP_POSTXOR, &sh->ops.pending) &&
  2465. !test_bit(STRIPE_OP_CHECK, &sh->ops.pending))
  2466. handle_issuing_new_write_requests5(conf, sh, &s, disks);
  2467. /* maybe we need to check and possibly fix the parity for this stripe
  2468. * Any reads will already have been scheduled, so we just see if enough
  2469. * data is available. The parity check is held off while parity
  2470. * dependent operations are in flight.
  2471. */
  2472. if ((s.syncing && s.locked == 0 &&
  2473. !test_bit(STRIPE_OP_COMPUTE_BLK, &sh->ops.pending) &&
  2474. !test_bit(STRIPE_INSYNC, &sh->state)) ||
  2475. test_bit(STRIPE_OP_CHECK, &sh->ops.pending) ||
  2476. test_bit(STRIPE_OP_MOD_REPAIR_PD, &sh->ops.pending))
  2477. handle_parity_checks5(conf, sh, &s, disks);
  2478. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2479. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2480. clear_bit(STRIPE_SYNCING, &sh->state);
  2481. }
  2482. /* If the failed drive is just a ReadError, then we might need to progress
  2483. * the repair/check process
  2484. */
  2485. if (s.failed == 1 && !conf->mddev->ro &&
  2486. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2487. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2488. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2489. ) {
  2490. dev = &sh->dev[s.failed_num];
  2491. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2492. set_bit(R5_Wantwrite, &dev->flags);
  2493. if (!test_and_set_bit(STRIPE_OP_IO, &sh->ops.pending))
  2494. sh->ops.count++;
  2495. set_bit(R5_ReWrite, &dev->flags);
  2496. set_bit(R5_LOCKED, &dev->flags);
  2497. s.locked++;
  2498. } else {
  2499. /* let's read it back */
  2500. set_bit(R5_Wantread, &dev->flags);
  2501. if (!test_and_set_bit(STRIPE_OP_IO, &sh->ops.pending))
  2502. sh->ops.count++;
  2503. set_bit(R5_LOCKED, &dev->flags);
  2504. s.locked++;
  2505. }
  2506. }
  2507. /* Finish postxor operations initiated by the expansion
  2508. * process
  2509. */
  2510. if (test_bit(STRIPE_OP_POSTXOR, &sh->ops.complete) &&
  2511. !test_bit(STRIPE_OP_BIODRAIN, &sh->ops.pending)) {
  2512. clear_bit(STRIPE_EXPANDING, &sh->state);
  2513. clear_bit(STRIPE_OP_POSTXOR, &sh->ops.pending);
  2514. clear_bit(STRIPE_OP_POSTXOR, &sh->ops.ack);
  2515. clear_bit(STRIPE_OP_POSTXOR, &sh->ops.complete);
  2516. for (i = conf->raid_disks; i--; ) {
  2517. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2518. if (!test_and_set_bit(STRIPE_OP_IO, &sh->ops.pending))
  2519. sh->ops.count++;
  2520. }
  2521. }
  2522. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2523. !test_bit(STRIPE_OP_POSTXOR, &sh->ops.pending)) {
  2524. /* Need to write out all blocks after computing parity */
  2525. sh->disks = conf->raid_disks;
  2526. sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
  2527. conf->raid_disks);
  2528. s.locked += handle_write_operations5(sh, 0, 1);
  2529. } else if (s.expanded &&
  2530. !test_bit(STRIPE_OP_POSTXOR, &sh->ops.pending)) {
  2531. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2532. atomic_dec(&conf->reshape_stripes);
  2533. wake_up(&conf->wait_for_overlap);
  2534. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2535. }
  2536. if (s.expanding && s.locked == 0)
  2537. handle_stripe_expansion(conf, sh, NULL);
  2538. if (sh->ops.count)
  2539. pending = get_stripe_work(sh);
  2540. spin_unlock(&sh->lock);
  2541. if (pending)
  2542. raid5_run_ops(sh, pending);
  2543. return_io(return_bi);
  2544. }
  2545. static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  2546. {
  2547. raid6_conf_t *conf = sh->raid_conf;
  2548. int disks = sh->disks;
  2549. struct bio *return_bi = NULL;
  2550. int i, pd_idx = sh->pd_idx;
  2551. struct stripe_head_state s;
  2552. struct r6_state r6s;
  2553. struct r5dev *dev, *pdev, *qdev;
  2554. r6s.qd_idx = raid6_next_disk(pd_idx, disks);
  2555. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2556. "pd_idx=%d, qd_idx=%d\n",
  2557. (unsigned long long)sh->sector, sh->state,
  2558. atomic_read(&sh->count), pd_idx, r6s.qd_idx);
  2559. memset(&s, 0, sizeof(s));
  2560. spin_lock(&sh->lock);
  2561. clear_bit(STRIPE_HANDLE, &sh->state);
  2562. clear_bit(STRIPE_DELAYED, &sh->state);
  2563. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2564. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2565. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2566. /* Now to look around and see what can be done */
  2567. rcu_read_lock();
  2568. for (i=disks; i--; ) {
  2569. mdk_rdev_t *rdev;
  2570. dev = &sh->dev[i];
  2571. clear_bit(R5_Insync, &dev->flags);
  2572. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2573. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2574. /* maybe we can reply to a read */
  2575. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  2576. struct bio *rbi, *rbi2;
  2577. pr_debug("Return read for disc %d\n", i);
  2578. spin_lock_irq(&conf->device_lock);
  2579. rbi = dev->toread;
  2580. dev->toread = NULL;
  2581. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  2582. wake_up(&conf->wait_for_overlap);
  2583. spin_unlock_irq(&conf->device_lock);
  2584. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  2585. copy_data(0, rbi, dev->page, dev->sector);
  2586. rbi2 = r5_next_bio(rbi, dev->sector);
  2587. spin_lock_irq(&conf->device_lock);
  2588. if (--rbi->bi_phys_segments == 0) {
  2589. rbi->bi_next = return_bi;
  2590. return_bi = rbi;
  2591. }
  2592. spin_unlock_irq(&conf->device_lock);
  2593. rbi = rbi2;
  2594. }
  2595. }
  2596. /* now count some things */
  2597. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2598. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2599. if (dev->toread)
  2600. s.to_read++;
  2601. if (dev->towrite) {
  2602. s.to_write++;
  2603. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2604. s.non_overwrite++;
  2605. }
  2606. if (dev->written)
  2607. s.written++;
  2608. rdev = rcu_dereference(conf->disks[i].rdev);
  2609. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2610. /* The ReadError flag will just be confusing now */
  2611. clear_bit(R5_ReadError, &dev->flags);
  2612. clear_bit(R5_ReWrite, &dev->flags);
  2613. }
  2614. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2615. || test_bit(R5_ReadError, &dev->flags)) {
  2616. if (s.failed < 2)
  2617. r6s.failed_num[s.failed] = i;
  2618. s.failed++;
  2619. } else
  2620. set_bit(R5_Insync, &dev->flags);
  2621. }
  2622. rcu_read_unlock();
  2623. pr_debug("locked=%d uptodate=%d to_read=%d"
  2624. " to_write=%d failed=%d failed_num=%d,%d\n",
  2625. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2626. r6s.failed_num[0], r6s.failed_num[1]);
  2627. /* check if the array has lost >2 devices and, if so, some requests
  2628. * might need to be failed
  2629. */
  2630. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2631. handle_requests_to_failed_array(conf, sh, &s, disks,
  2632. &return_bi);
  2633. if (s.failed > 2 && s.syncing) {
  2634. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2635. clear_bit(STRIPE_SYNCING, &sh->state);
  2636. s.syncing = 0;
  2637. }
  2638. /*
  2639. * might be able to return some write requests if the parity blocks
  2640. * are safe, or on a failed drive
  2641. */
  2642. pdev = &sh->dev[pd_idx];
  2643. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2644. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2645. qdev = &sh->dev[r6s.qd_idx];
  2646. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
  2647. || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
  2648. if ( s.written &&
  2649. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2650. && !test_bit(R5_LOCKED, &pdev->flags)
  2651. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2652. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2653. && !test_bit(R5_LOCKED, &qdev->flags)
  2654. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2655. handle_completed_write_requests(conf, sh, disks, &return_bi);
  2656. /* Now we might consider reading some blocks, either to check/generate
  2657. * parity, or to satisfy requests
  2658. * or to load a block that is being partially written.
  2659. */
  2660. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2661. (s.syncing && (s.uptodate < disks)) || s.expanding)
  2662. handle_issuing_new_read_requests6(sh, &s, &r6s, disks);
  2663. /* now to consider writing and what else, if anything should be read */
  2664. if (s.to_write)
  2665. handle_issuing_new_write_requests6(conf, sh, &s, &r6s, disks);
  2666. /* maybe we need to check and possibly fix the parity for this stripe
  2667. * Any reads will already have been scheduled, so we just see if enough
  2668. * data is available
  2669. */
  2670. if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
  2671. handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
  2672. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2673. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2674. clear_bit(STRIPE_SYNCING, &sh->state);
  2675. }
  2676. /* If the failed drives are just a ReadError, then we might need
  2677. * to progress the repair/check process
  2678. */
  2679. if (s.failed <= 2 && !conf->mddev->ro)
  2680. for (i = 0; i < s.failed; i++) {
  2681. dev = &sh->dev[r6s.failed_num[i]];
  2682. if (test_bit(R5_ReadError, &dev->flags)
  2683. && !test_bit(R5_LOCKED, &dev->flags)
  2684. && test_bit(R5_UPTODATE, &dev->flags)
  2685. ) {
  2686. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2687. set_bit(R5_Wantwrite, &dev->flags);
  2688. set_bit(R5_ReWrite, &dev->flags);
  2689. set_bit(R5_LOCKED, &dev->flags);
  2690. } else {
  2691. /* let's read it back */
  2692. set_bit(R5_Wantread, &dev->flags);
  2693. set_bit(R5_LOCKED, &dev->flags);
  2694. }
  2695. }
  2696. }
  2697. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2698. /* Need to write out all blocks after computing P&Q */
  2699. sh->disks = conf->raid_disks;
  2700. sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
  2701. conf->raid_disks);
  2702. compute_parity6(sh, RECONSTRUCT_WRITE);
  2703. for (i = conf->raid_disks ; i-- ; ) {
  2704. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2705. s.locked++;
  2706. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2707. }
  2708. clear_bit(STRIPE_EXPANDING, &sh->state);
  2709. } else if (s.expanded) {
  2710. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2711. atomic_dec(&conf->reshape_stripes);
  2712. wake_up(&conf->wait_for_overlap);
  2713. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2714. }
  2715. if (s.expanding && s.locked == 0)
  2716. handle_stripe_expansion(conf, sh, &r6s);
  2717. spin_unlock(&sh->lock);
  2718. return_io(return_bi);
  2719. for (i=disks; i-- ;) {
  2720. int rw;
  2721. struct bio *bi;
  2722. mdk_rdev_t *rdev;
  2723. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  2724. rw = WRITE;
  2725. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  2726. rw = READ;
  2727. else
  2728. continue;
  2729. bi = &sh->dev[i].req;
  2730. bi->bi_rw = rw;
  2731. if (rw == WRITE)
  2732. bi->bi_end_io = raid5_end_write_request;
  2733. else
  2734. bi->bi_end_io = raid5_end_read_request;
  2735. rcu_read_lock();
  2736. rdev = rcu_dereference(conf->disks[i].rdev);
  2737. if (rdev && test_bit(Faulty, &rdev->flags))
  2738. rdev = NULL;
  2739. if (rdev)
  2740. atomic_inc(&rdev->nr_pending);
  2741. rcu_read_unlock();
  2742. if (rdev) {
  2743. if (s.syncing || s.expanding || s.expanded)
  2744. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  2745. bi->bi_bdev = rdev->bdev;
  2746. pr_debug("for %llu schedule op %ld on disc %d\n",
  2747. (unsigned long long)sh->sector, bi->bi_rw, i);
  2748. atomic_inc(&sh->count);
  2749. bi->bi_sector = sh->sector + rdev->data_offset;
  2750. bi->bi_flags = 1 << BIO_UPTODATE;
  2751. bi->bi_vcnt = 1;
  2752. bi->bi_max_vecs = 1;
  2753. bi->bi_idx = 0;
  2754. bi->bi_io_vec = &sh->dev[i].vec;
  2755. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  2756. bi->bi_io_vec[0].bv_offset = 0;
  2757. bi->bi_size = STRIPE_SIZE;
  2758. bi->bi_next = NULL;
  2759. if (rw == WRITE &&
  2760. test_bit(R5_ReWrite, &sh->dev[i].flags))
  2761. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2762. generic_make_request(bi);
  2763. } else {
  2764. if (rw == WRITE)
  2765. set_bit(STRIPE_DEGRADED, &sh->state);
  2766. pr_debug("skip op %ld on disc %d for sector %llu\n",
  2767. bi->bi_rw, i, (unsigned long long)sh->sector);
  2768. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2769. set_bit(STRIPE_HANDLE, &sh->state);
  2770. }
  2771. }
  2772. }
  2773. static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2774. {
  2775. if (sh->raid_conf->level == 6)
  2776. handle_stripe6(sh, tmp_page);
  2777. else
  2778. handle_stripe5(sh);
  2779. }
  2780. static void raid5_activate_delayed(raid5_conf_t *conf)
  2781. {
  2782. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2783. while (!list_empty(&conf->delayed_list)) {
  2784. struct list_head *l = conf->delayed_list.next;
  2785. struct stripe_head *sh;
  2786. sh = list_entry(l, struct stripe_head, lru);
  2787. list_del_init(l);
  2788. clear_bit(STRIPE_DELAYED, &sh->state);
  2789. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2790. atomic_inc(&conf->preread_active_stripes);
  2791. list_add_tail(&sh->lru, &conf->handle_list);
  2792. }
  2793. }
  2794. }
  2795. static void activate_bit_delay(raid5_conf_t *conf)
  2796. {
  2797. /* device_lock is held */
  2798. struct list_head head;
  2799. list_add(&head, &conf->bitmap_list);
  2800. list_del_init(&conf->bitmap_list);
  2801. while (!list_empty(&head)) {
  2802. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2803. list_del_init(&sh->lru);
  2804. atomic_inc(&sh->count);
  2805. __release_stripe(conf, sh);
  2806. }
  2807. }
  2808. static void unplug_slaves(mddev_t *mddev)
  2809. {
  2810. raid5_conf_t *conf = mddev_to_conf(mddev);
  2811. int i;
  2812. rcu_read_lock();
  2813. for (i=0; i<mddev->raid_disks; i++) {
  2814. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2815. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2816. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  2817. atomic_inc(&rdev->nr_pending);
  2818. rcu_read_unlock();
  2819. if (r_queue->unplug_fn)
  2820. r_queue->unplug_fn(r_queue);
  2821. rdev_dec_pending(rdev, mddev);
  2822. rcu_read_lock();
  2823. }
  2824. }
  2825. rcu_read_unlock();
  2826. }
  2827. static void raid5_unplug_device(request_queue_t *q)
  2828. {
  2829. mddev_t *mddev = q->queuedata;
  2830. raid5_conf_t *conf = mddev_to_conf(mddev);
  2831. unsigned long flags;
  2832. spin_lock_irqsave(&conf->device_lock, flags);
  2833. if (blk_remove_plug(q)) {
  2834. conf->seq_flush++;
  2835. raid5_activate_delayed(conf);
  2836. }
  2837. md_wakeup_thread(mddev->thread);
  2838. spin_unlock_irqrestore(&conf->device_lock, flags);
  2839. unplug_slaves(mddev);
  2840. }
  2841. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  2842. sector_t *error_sector)
  2843. {
  2844. mddev_t *mddev = q->queuedata;
  2845. raid5_conf_t *conf = mddev_to_conf(mddev);
  2846. int i, ret = 0;
  2847. rcu_read_lock();
  2848. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  2849. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2850. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  2851. struct block_device *bdev = rdev->bdev;
  2852. request_queue_t *r_queue = bdev_get_queue(bdev);
  2853. if (!r_queue->issue_flush_fn)
  2854. ret = -EOPNOTSUPP;
  2855. else {
  2856. atomic_inc(&rdev->nr_pending);
  2857. rcu_read_unlock();
  2858. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  2859. error_sector);
  2860. rdev_dec_pending(rdev, mddev);
  2861. rcu_read_lock();
  2862. }
  2863. }
  2864. }
  2865. rcu_read_unlock();
  2866. return ret;
  2867. }
  2868. static int raid5_congested(void *data, int bits)
  2869. {
  2870. mddev_t *mddev = data;
  2871. raid5_conf_t *conf = mddev_to_conf(mddev);
  2872. /* No difference between reads and writes. Just check
  2873. * how busy the stripe_cache is
  2874. */
  2875. if (conf->inactive_blocked)
  2876. return 1;
  2877. if (conf->quiesce)
  2878. return 1;
  2879. if (list_empty_careful(&conf->inactive_list))
  2880. return 1;
  2881. return 0;
  2882. }
  2883. /* We want read requests to align with chunks where possible,
  2884. * but write requests don't need to.
  2885. */
  2886. static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
  2887. {
  2888. mddev_t *mddev = q->queuedata;
  2889. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2890. int max;
  2891. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2892. unsigned int bio_sectors = bio->bi_size >> 9;
  2893. if (bio_data_dir(bio) == WRITE)
  2894. return biovec->bv_len; /* always allow writes to be mergeable */
  2895. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2896. if (max < 0) max = 0;
  2897. if (max <= biovec->bv_len && bio_sectors == 0)
  2898. return biovec->bv_len;
  2899. else
  2900. return max;
  2901. }
  2902. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2903. {
  2904. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2905. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2906. unsigned int bio_sectors = bio->bi_size >> 9;
  2907. return chunk_sectors >=
  2908. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2909. }
  2910. /*
  2911. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2912. * later sampled by raid5d.
  2913. */
  2914. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2915. {
  2916. unsigned long flags;
  2917. spin_lock_irqsave(&conf->device_lock, flags);
  2918. bi->bi_next = conf->retry_read_aligned_list;
  2919. conf->retry_read_aligned_list = bi;
  2920. spin_unlock_irqrestore(&conf->device_lock, flags);
  2921. md_wakeup_thread(conf->mddev->thread);
  2922. }
  2923. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  2924. {
  2925. struct bio *bi;
  2926. bi = conf->retry_read_aligned;
  2927. if (bi) {
  2928. conf->retry_read_aligned = NULL;
  2929. return bi;
  2930. }
  2931. bi = conf->retry_read_aligned_list;
  2932. if(bi) {
  2933. conf->retry_read_aligned_list = bi->bi_next;
  2934. bi->bi_next = NULL;
  2935. bi->bi_phys_segments = 1; /* biased count of active stripes */
  2936. bi->bi_hw_segments = 0; /* count of processed stripes */
  2937. }
  2938. return bi;
  2939. }
  2940. /*
  2941. * The "raid5_align_endio" should check if the read succeeded and if it
  2942. * did, call bio_endio on the original bio (having bio_put the new bio
  2943. * first).
  2944. * If the read failed..
  2945. */
  2946. static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
  2947. {
  2948. struct bio* raid_bi = bi->bi_private;
  2949. mddev_t *mddev;
  2950. raid5_conf_t *conf;
  2951. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  2952. mdk_rdev_t *rdev;
  2953. if (bi->bi_size)
  2954. return 1;
  2955. bio_put(bi);
  2956. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  2957. conf = mddev_to_conf(mddev);
  2958. rdev = (void*)raid_bi->bi_next;
  2959. raid_bi->bi_next = NULL;
  2960. rdev_dec_pending(rdev, conf->mddev);
  2961. if (!error && uptodate) {
  2962. bio_endio(raid_bi, bytes, 0);
  2963. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2964. wake_up(&conf->wait_for_stripe);
  2965. return 0;
  2966. }
  2967. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  2968. add_bio_to_retry(raid_bi, conf);
  2969. return 0;
  2970. }
  2971. static int bio_fits_rdev(struct bio *bi)
  2972. {
  2973. request_queue_t *q = bdev_get_queue(bi->bi_bdev);
  2974. if ((bi->bi_size>>9) > q->max_sectors)
  2975. return 0;
  2976. blk_recount_segments(q, bi);
  2977. if (bi->bi_phys_segments > q->max_phys_segments ||
  2978. bi->bi_hw_segments > q->max_hw_segments)
  2979. return 0;
  2980. if (q->merge_bvec_fn)
  2981. /* it's too hard to apply the merge_bvec_fn at this stage,
  2982. * just just give up
  2983. */
  2984. return 0;
  2985. return 1;
  2986. }
  2987. static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
  2988. {
  2989. mddev_t *mddev = q->queuedata;
  2990. raid5_conf_t *conf = mddev_to_conf(mddev);
  2991. const unsigned int raid_disks = conf->raid_disks;
  2992. const unsigned int data_disks = raid_disks - conf->max_degraded;
  2993. unsigned int dd_idx, pd_idx;
  2994. struct bio* align_bi;
  2995. mdk_rdev_t *rdev;
  2996. if (!in_chunk_boundary(mddev, raid_bio)) {
  2997. pr_debug("chunk_aligned_read : non aligned\n");
  2998. return 0;
  2999. }
  3000. /*
  3001. * use bio_clone to make a copy of the bio
  3002. */
  3003. align_bi = bio_clone(raid_bio, GFP_NOIO);
  3004. if (!align_bi)
  3005. return 0;
  3006. /*
  3007. * set bi_end_io to a new function, and set bi_private to the
  3008. * original bio.
  3009. */
  3010. align_bi->bi_end_io = raid5_align_endio;
  3011. align_bi->bi_private = raid_bio;
  3012. /*
  3013. * compute position
  3014. */
  3015. align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
  3016. raid_disks,
  3017. data_disks,
  3018. &dd_idx,
  3019. &pd_idx,
  3020. conf);
  3021. rcu_read_lock();
  3022. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3023. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3024. atomic_inc(&rdev->nr_pending);
  3025. rcu_read_unlock();
  3026. raid_bio->bi_next = (void*)rdev;
  3027. align_bi->bi_bdev = rdev->bdev;
  3028. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3029. align_bi->bi_sector += rdev->data_offset;
  3030. if (!bio_fits_rdev(align_bi)) {
  3031. /* too big in some way */
  3032. bio_put(align_bi);
  3033. rdev_dec_pending(rdev, mddev);
  3034. return 0;
  3035. }
  3036. spin_lock_irq(&conf->device_lock);
  3037. wait_event_lock_irq(conf->wait_for_stripe,
  3038. conf->quiesce == 0,
  3039. conf->device_lock, /* nothing */);
  3040. atomic_inc(&conf->active_aligned_reads);
  3041. spin_unlock_irq(&conf->device_lock);
  3042. generic_make_request(align_bi);
  3043. return 1;
  3044. } else {
  3045. rcu_read_unlock();
  3046. bio_put(align_bi);
  3047. return 0;
  3048. }
  3049. }
  3050. static int make_request(request_queue_t *q, struct bio * bi)
  3051. {
  3052. mddev_t *mddev = q->queuedata;
  3053. raid5_conf_t *conf = mddev_to_conf(mddev);
  3054. unsigned int dd_idx, pd_idx;
  3055. sector_t new_sector;
  3056. sector_t logical_sector, last_sector;
  3057. struct stripe_head *sh;
  3058. const int rw = bio_data_dir(bi);
  3059. int remaining;
  3060. if (unlikely(bio_barrier(bi))) {
  3061. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  3062. return 0;
  3063. }
  3064. md_write_start(mddev, bi);
  3065. disk_stat_inc(mddev->gendisk, ios[rw]);
  3066. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  3067. if (rw == READ &&
  3068. mddev->reshape_position == MaxSector &&
  3069. chunk_aligned_read(q,bi))
  3070. return 0;
  3071. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3072. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3073. bi->bi_next = NULL;
  3074. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3075. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3076. DEFINE_WAIT(w);
  3077. int disks, data_disks;
  3078. retry:
  3079. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3080. if (likely(conf->expand_progress == MaxSector))
  3081. disks = conf->raid_disks;
  3082. else {
  3083. /* spinlock is needed as expand_progress may be
  3084. * 64bit on a 32bit platform, and so it might be
  3085. * possible to see a half-updated value
  3086. * Ofcourse expand_progress could change after
  3087. * the lock is dropped, so once we get a reference
  3088. * to the stripe that we think it is, we will have
  3089. * to check again.
  3090. */
  3091. spin_lock_irq(&conf->device_lock);
  3092. disks = conf->raid_disks;
  3093. if (logical_sector >= conf->expand_progress)
  3094. disks = conf->previous_raid_disks;
  3095. else {
  3096. if (logical_sector >= conf->expand_lo) {
  3097. spin_unlock_irq(&conf->device_lock);
  3098. schedule();
  3099. goto retry;
  3100. }
  3101. }
  3102. spin_unlock_irq(&conf->device_lock);
  3103. }
  3104. data_disks = disks - conf->max_degraded;
  3105. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  3106. &dd_idx, &pd_idx, conf);
  3107. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  3108. (unsigned long long)new_sector,
  3109. (unsigned long long)logical_sector);
  3110. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  3111. if (sh) {
  3112. if (unlikely(conf->expand_progress != MaxSector)) {
  3113. /* expansion might have moved on while waiting for a
  3114. * stripe, so we must do the range check again.
  3115. * Expansion could still move past after this
  3116. * test, but as we are holding a reference to
  3117. * 'sh', we know that if that happens,
  3118. * STRIPE_EXPANDING will get set and the expansion
  3119. * won't proceed until we finish with the stripe.
  3120. */
  3121. int must_retry = 0;
  3122. spin_lock_irq(&conf->device_lock);
  3123. if (logical_sector < conf->expand_progress &&
  3124. disks == conf->previous_raid_disks)
  3125. /* mismatch, need to try again */
  3126. must_retry = 1;
  3127. spin_unlock_irq(&conf->device_lock);
  3128. if (must_retry) {
  3129. release_stripe(sh);
  3130. goto retry;
  3131. }
  3132. }
  3133. /* FIXME what if we get a false positive because these
  3134. * are being updated.
  3135. */
  3136. if (logical_sector >= mddev->suspend_lo &&
  3137. logical_sector < mddev->suspend_hi) {
  3138. release_stripe(sh);
  3139. schedule();
  3140. goto retry;
  3141. }
  3142. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3143. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3144. /* Stripe is busy expanding or
  3145. * add failed due to overlap. Flush everything
  3146. * and wait a while
  3147. */
  3148. raid5_unplug_device(mddev->queue);
  3149. release_stripe(sh);
  3150. schedule();
  3151. goto retry;
  3152. }
  3153. finish_wait(&conf->wait_for_overlap, &w);
  3154. handle_stripe(sh, NULL);
  3155. release_stripe(sh);
  3156. } else {
  3157. /* cannot get stripe for read-ahead, just give-up */
  3158. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3159. finish_wait(&conf->wait_for_overlap, &w);
  3160. break;
  3161. }
  3162. }
  3163. spin_lock_irq(&conf->device_lock);
  3164. remaining = --bi->bi_phys_segments;
  3165. spin_unlock_irq(&conf->device_lock);
  3166. if (remaining == 0) {
  3167. int bytes = bi->bi_size;
  3168. if ( rw == WRITE )
  3169. md_write_end(mddev);
  3170. bi->bi_size = 0;
  3171. bi->bi_end_io(bi, bytes,
  3172. test_bit(BIO_UPTODATE, &bi->bi_flags)
  3173. ? 0 : -EIO);
  3174. }
  3175. return 0;
  3176. }
  3177. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3178. {
  3179. /* reshaping is quite different to recovery/resync so it is
  3180. * handled quite separately ... here.
  3181. *
  3182. * On each call to sync_request, we gather one chunk worth of
  3183. * destination stripes and flag them as expanding.
  3184. * Then we find all the source stripes and request reads.
  3185. * As the reads complete, handle_stripe will copy the data
  3186. * into the destination stripe and release that stripe.
  3187. */
  3188. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3189. struct stripe_head *sh;
  3190. int pd_idx;
  3191. sector_t first_sector, last_sector;
  3192. int raid_disks = conf->previous_raid_disks;
  3193. int data_disks = raid_disks - conf->max_degraded;
  3194. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3195. int i;
  3196. int dd_idx;
  3197. sector_t writepos, safepos, gap;
  3198. if (sector_nr == 0 &&
  3199. conf->expand_progress != 0) {
  3200. /* restarting in the middle, skip the initial sectors */
  3201. sector_nr = conf->expand_progress;
  3202. sector_div(sector_nr, new_data_disks);
  3203. *skipped = 1;
  3204. return sector_nr;
  3205. }
  3206. /* we update the metadata when there is more than 3Meg
  3207. * in the block range (that is rather arbitrary, should
  3208. * probably be time based) or when the data about to be
  3209. * copied would over-write the source of the data at
  3210. * the front of the range.
  3211. * i.e. one new_stripe forward from expand_progress new_maps
  3212. * to after where expand_lo old_maps to
  3213. */
  3214. writepos = conf->expand_progress +
  3215. conf->chunk_size/512*(new_data_disks);
  3216. sector_div(writepos, new_data_disks);
  3217. safepos = conf->expand_lo;
  3218. sector_div(safepos, data_disks);
  3219. gap = conf->expand_progress - conf->expand_lo;
  3220. if (writepos >= safepos ||
  3221. gap > (new_data_disks)*3000*2 /*3Meg*/) {
  3222. /* Cannot proceed until we've updated the superblock... */
  3223. wait_event(conf->wait_for_overlap,
  3224. atomic_read(&conf->reshape_stripes)==0);
  3225. mddev->reshape_position = conf->expand_progress;
  3226. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3227. md_wakeup_thread(mddev->thread);
  3228. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3229. kthread_should_stop());
  3230. spin_lock_irq(&conf->device_lock);
  3231. conf->expand_lo = mddev->reshape_position;
  3232. spin_unlock_irq(&conf->device_lock);
  3233. wake_up(&conf->wait_for_overlap);
  3234. }
  3235. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  3236. int j;
  3237. int skipped = 0;
  3238. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  3239. sh = get_active_stripe(conf, sector_nr+i,
  3240. conf->raid_disks, pd_idx, 0);
  3241. set_bit(STRIPE_EXPANDING, &sh->state);
  3242. atomic_inc(&conf->reshape_stripes);
  3243. /* If any of this stripe is beyond the end of the old
  3244. * array, then we need to zero those blocks
  3245. */
  3246. for (j=sh->disks; j--;) {
  3247. sector_t s;
  3248. if (j == sh->pd_idx)
  3249. continue;
  3250. if (conf->level == 6 &&
  3251. j == raid6_next_disk(sh->pd_idx, sh->disks))
  3252. continue;
  3253. s = compute_blocknr(sh, j);
  3254. if (s < (mddev->array_size<<1)) {
  3255. skipped = 1;
  3256. continue;
  3257. }
  3258. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3259. set_bit(R5_Expanded, &sh->dev[j].flags);
  3260. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3261. }
  3262. if (!skipped) {
  3263. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3264. set_bit(STRIPE_HANDLE, &sh->state);
  3265. }
  3266. release_stripe(sh);
  3267. }
  3268. spin_lock_irq(&conf->device_lock);
  3269. conf->expand_progress = (sector_nr + i) * new_data_disks;
  3270. spin_unlock_irq(&conf->device_lock);
  3271. /* Ok, those stripe are ready. We can start scheduling
  3272. * reads on the source stripes.
  3273. * The source stripes are determined by mapping the first and last
  3274. * block on the destination stripes.
  3275. */
  3276. first_sector =
  3277. raid5_compute_sector(sector_nr*(new_data_disks),
  3278. raid_disks, data_disks,
  3279. &dd_idx, &pd_idx, conf);
  3280. last_sector =
  3281. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  3282. *(new_data_disks) -1,
  3283. raid_disks, data_disks,
  3284. &dd_idx, &pd_idx, conf);
  3285. if (last_sector >= (mddev->size<<1))
  3286. last_sector = (mddev->size<<1)-1;
  3287. while (first_sector <= last_sector) {
  3288. pd_idx = stripe_to_pdidx(first_sector, conf,
  3289. conf->previous_raid_disks);
  3290. sh = get_active_stripe(conf, first_sector,
  3291. conf->previous_raid_disks, pd_idx, 0);
  3292. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3293. set_bit(STRIPE_HANDLE, &sh->state);
  3294. release_stripe(sh);
  3295. first_sector += STRIPE_SECTORS;
  3296. }
  3297. return conf->chunk_size>>9;
  3298. }
  3299. /* FIXME go_faster isn't used */
  3300. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3301. {
  3302. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3303. struct stripe_head *sh;
  3304. int pd_idx;
  3305. int raid_disks = conf->raid_disks;
  3306. sector_t max_sector = mddev->size << 1;
  3307. int sync_blocks;
  3308. int still_degraded = 0;
  3309. int i;
  3310. if (sector_nr >= max_sector) {
  3311. /* just being told to finish up .. nothing much to do */
  3312. unplug_slaves(mddev);
  3313. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3314. end_reshape(conf);
  3315. return 0;
  3316. }
  3317. if (mddev->curr_resync < max_sector) /* aborted */
  3318. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3319. &sync_blocks, 1);
  3320. else /* completed sync */
  3321. conf->fullsync = 0;
  3322. bitmap_close_sync(mddev->bitmap);
  3323. return 0;
  3324. }
  3325. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3326. return reshape_request(mddev, sector_nr, skipped);
  3327. /* if there is too many failed drives and we are trying
  3328. * to resync, then assert that we are finished, because there is
  3329. * nothing we can do.
  3330. */
  3331. if (mddev->degraded >= conf->max_degraded &&
  3332. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3333. sector_t rv = (mddev->size << 1) - sector_nr;
  3334. *skipped = 1;
  3335. return rv;
  3336. }
  3337. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3338. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3339. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3340. /* we can skip this block, and probably more */
  3341. sync_blocks /= STRIPE_SECTORS;
  3342. *skipped = 1;
  3343. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3344. }
  3345. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  3346. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  3347. if (sh == NULL) {
  3348. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  3349. /* make sure we don't swamp the stripe cache if someone else
  3350. * is trying to get access
  3351. */
  3352. schedule_timeout_uninterruptible(1);
  3353. }
  3354. /* Need to check if array will still be degraded after recovery/resync
  3355. * We don't need to check the 'failed' flag as when that gets set,
  3356. * recovery aborts.
  3357. */
  3358. for (i=0; i<mddev->raid_disks; i++)
  3359. if (conf->disks[i].rdev == NULL)
  3360. still_degraded = 1;
  3361. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3362. spin_lock(&sh->lock);
  3363. set_bit(STRIPE_SYNCING, &sh->state);
  3364. clear_bit(STRIPE_INSYNC, &sh->state);
  3365. spin_unlock(&sh->lock);
  3366. handle_stripe(sh, NULL);
  3367. release_stripe(sh);
  3368. return STRIPE_SECTORS;
  3369. }
  3370. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3371. {
  3372. /* We may not be able to submit a whole bio at once as there
  3373. * may not be enough stripe_heads available.
  3374. * We cannot pre-allocate enough stripe_heads as we may need
  3375. * more than exist in the cache (if we allow ever large chunks).
  3376. * So we do one stripe head at a time and record in
  3377. * ->bi_hw_segments how many have been done.
  3378. *
  3379. * We *know* that this entire raid_bio is in one chunk, so
  3380. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3381. */
  3382. struct stripe_head *sh;
  3383. int dd_idx, pd_idx;
  3384. sector_t sector, logical_sector, last_sector;
  3385. int scnt = 0;
  3386. int remaining;
  3387. int handled = 0;
  3388. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3389. sector = raid5_compute_sector( logical_sector,
  3390. conf->raid_disks,
  3391. conf->raid_disks - conf->max_degraded,
  3392. &dd_idx,
  3393. &pd_idx,
  3394. conf);
  3395. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3396. for (; logical_sector < last_sector;
  3397. logical_sector += STRIPE_SECTORS,
  3398. sector += STRIPE_SECTORS,
  3399. scnt++) {
  3400. if (scnt < raid_bio->bi_hw_segments)
  3401. /* already done this stripe */
  3402. continue;
  3403. sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
  3404. if (!sh) {
  3405. /* failed to get a stripe - must wait */
  3406. raid_bio->bi_hw_segments = scnt;
  3407. conf->retry_read_aligned = raid_bio;
  3408. return handled;
  3409. }
  3410. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3411. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3412. release_stripe(sh);
  3413. raid_bio->bi_hw_segments = scnt;
  3414. conf->retry_read_aligned = raid_bio;
  3415. return handled;
  3416. }
  3417. handle_stripe(sh, NULL);
  3418. release_stripe(sh);
  3419. handled++;
  3420. }
  3421. spin_lock_irq(&conf->device_lock);
  3422. remaining = --raid_bio->bi_phys_segments;
  3423. spin_unlock_irq(&conf->device_lock);
  3424. if (remaining == 0) {
  3425. int bytes = raid_bio->bi_size;
  3426. raid_bio->bi_size = 0;
  3427. raid_bio->bi_end_io(raid_bio, bytes,
  3428. test_bit(BIO_UPTODATE, &raid_bio->bi_flags)
  3429. ? 0 : -EIO);
  3430. }
  3431. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3432. wake_up(&conf->wait_for_stripe);
  3433. return handled;
  3434. }
  3435. /*
  3436. * This is our raid5 kernel thread.
  3437. *
  3438. * We scan the hash table for stripes which can be handled now.
  3439. * During the scan, completed stripes are saved for us by the interrupt
  3440. * handler, so that they will not have to wait for our next wakeup.
  3441. */
  3442. static void raid5d (mddev_t *mddev)
  3443. {
  3444. struct stripe_head *sh;
  3445. raid5_conf_t *conf = mddev_to_conf(mddev);
  3446. int handled;
  3447. pr_debug("+++ raid5d active\n");
  3448. md_check_recovery(mddev);
  3449. handled = 0;
  3450. spin_lock_irq(&conf->device_lock);
  3451. while (1) {
  3452. struct list_head *first;
  3453. struct bio *bio;
  3454. if (conf->seq_flush != conf->seq_write) {
  3455. int seq = conf->seq_flush;
  3456. spin_unlock_irq(&conf->device_lock);
  3457. bitmap_unplug(mddev->bitmap);
  3458. spin_lock_irq(&conf->device_lock);
  3459. conf->seq_write = seq;
  3460. activate_bit_delay(conf);
  3461. }
  3462. if (list_empty(&conf->handle_list) &&
  3463. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  3464. !blk_queue_plugged(mddev->queue) &&
  3465. !list_empty(&conf->delayed_list))
  3466. raid5_activate_delayed(conf);
  3467. while ((bio = remove_bio_from_retry(conf))) {
  3468. int ok;
  3469. spin_unlock_irq(&conf->device_lock);
  3470. ok = retry_aligned_read(conf, bio);
  3471. spin_lock_irq(&conf->device_lock);
  3472. if (!ok)
  3473. break;
  3474. handled++;
  3475. }
  3476. if (list_empty(&conf->handle_list)) {
  3477. async_tx_issue_pending_all();
  3478. break;
  3479. }
  3480. first = conf->handle_list.next;
  3481. sh = list_entry(first, struct stripe_head, lru);
  3482. list_del_init(first);
  3483. atomic_inc(&sh->count);
  3484. BUG_ON(atomic_read(&sh->count)!= 1);
  3485. spin_unlock_irq(&conf->device_lock);
  3486. handled++;
  3487. handle_stripe(sh, conf->spare_page);
  3488. release_stripe(sh);
  3489. spin_lock_irq(&conf->device_lock);
  3490. }
  3491. pr_debug("%d stripes handled\n", handled);
  3492. spin_unlock_irq(&conf->device_lock);
  3493. unplug_slaves(mddev);
  3494. pr_debug("--- raid5d inactive\n");
  3495. }
  3496. static ssize_t
  3497. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  3498. {
  3499. raid5_conf_t *conf = mddev_to_conf(mddev);
  3500. if (conf)
  3501. return sprintf(page, "%d\n", conf->max_nr_stripes);
  3502. else
  3503. return 0;
  3504. }
  3505. static ssize_t
  3506. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  3507. {
  3508. raid5_conf_t *conf = mddev_to_conf(mddev);
  3509. char *end;
  3510. int new;
  3511. if (len >= PAGE_SIZE)
  3512. return -EINVAL;
  3513. if (!conf)
  3514. return -ENODEV;
  3515. new = simple_strtoul(page, &end, 10);
  3516. if (!*page || (*end && *end != '\n') )
  3517. return -EINVAL;
  3518. if (new <= 16 || new > 32768)
  3519. return -EINVAL;
  3520. while (new < conf->max_nr_stripes) {
  3521. if (drop_one_stripe(conf))
  3522. conf->max_nr_stripes--;
  3523. else
  3524. break;
  3525. }
  3526. md_allow_write(mddev);
  3527. while (new > conf->max_nr_stripes) {
  3528. if (grow_one_stripe(conf))
  3529. conf->max_nr_stripes++;
  3530. else break;
  3531. }
  3532. return len;
  3533. }
  3534. static struct md_sysfs_entry
  3535. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  3536. raid5_show_stripe_cache_size,
  3537. raid5_store_stripe_cache_size);
  3538. static ssize_t
  3539. stripe_cache_active_show(mddev_t *mddev, char *page)
  3540. {
  3541. raid5_conf_t *conf = mddev_to_conf(mddev);
  3542. if (conf)
  3543. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  3544. else
  3545. return 0;
  3546. }
  3547. static struct md_sysfs_entry
  3548. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  3549. static struct attribute *raid5_attrs[] = {
  3550. &raid5_stripecache_size.attr,
  3551. &raid5_stripecache_active.attr,
  3552. NULL,
  3553. };
  3554. static struct attribute_group raid5_attrs_group = {
  3555. .name = NULL,
  3556. .attrs = raid5_attrs,
  3557. };
  3558. static int run(mddev_t *mddev)
  3559. {
  3560. raid5_conf_t *conf;
  3561. int raid_disk, memory;
  3562. mdk_rdev_t *rdev;
  3563. struct disk_info *disk;
  3564. struct list_head *tmp;
  3565. int working_disks = 0;
  3566. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  3567. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3568. mdname(mddev), mddev->level);
  3569. return -EIO;
  3570. }
  3571. if (mddev->reshape_position != MaxSector) {
  3572. /* Check that we can continue the reshape.
  3573. * Currently only disks can change, it must
  3574. * increase, and we must be past the point where
  3575. * a stripe over-writes itself
  3576. */
  3577. sector_t here_new, here_old;
  3578. int old_disks;
  3579. int max_degraded = (mddev->level == 5 ? 1 : 2);
  3580. if (mddev->new_level != mddev->level ||
  3581. mddev->new_layout != mddev->layout ||
  3582. mddev->new_chunk != mddev->chunk_size) {
  3583. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3584. "required - aborting.\n",
  3585. mdname(mddev));
  3586. return -EINVAL;
  3587. }
  3588. if (mddev->delta_disks <= 0) {
  3589. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3590. "(reduce disks) required - aborting.\n",
  3591. mdname(mddev));
  3592. return -EINVAL;
  3593. }
  3594. old_disks = mddev->raid_disks - mddev->delta_disks;
  3595. /* reshape_position must be on a new-stripe boundary, and one
  3596. * further up in new geometry must map after here in old
  3597. * geometry.
  3598. */
  3599. here_new = mddev->reshape_position;
  3600. if (sector_div(here_new, (mddev->chunk_size>>9)*
  3601. (mddev->raid_disks - max_degraded))) {
  3602. printk(KERN_ERR "raid5: reshape_position not "
  3603. "on a stripe boundary\n");
  3604. return -EINVAL;
  3605. }
  3606. /* here_new is the stripe we will write to */
  3607. here_old = mddev->reshape_position;
  3608. sector_div(here_old, (mddev->chunk_size>>9)*
  3609. (old_disks-max_degraded));
  3610. /* here_old is the first stripe that we might need to read
  3611. * from */
  3612. if (here_new >= here_old) {
  3613. /* Reading from the same stripe as writing to - bad */
  3614. printk(KERN_ERR "raid5: reshape_position too early for "
  3615. "auto-recovery - aborting.\n");
  3616. return -EINVAL;
  3617. }
  3618. printk(KERN_INFO "raid5: reshape will continue\n");
  3619. /* OK, we should be able to continue; */
  3620. }
  3621. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  3622. if ((conf = mddev->private) == NULL)
  3623. goto abort;
  3624. if (mddev->reshape_position == MaxSector) {
  3625. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  3626. } else {
  3627. conf->raid_disks = mddev->raid_disks;
  3628. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3629. }
  3630. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3631. GFP_KERNEL);
  3632. if (!conf->disks)
  3633. goto abort;
  3634. conf->mddev = mddev;
  3635. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3636. goto abort;
  3637. if (mddev->level == 6) {
  3638. conf->spare_page = alloc_page(GFP_KERNEL);
  3639. if (!conf->spare_page)
  3640. goto abort;
  3641. }
  3642. spin_lock_init(&conf->device_lock);
  3643. init_waitqueue_head(&conf->wait_for_stripe);
  3644. init_waitqueue_head(&conf->wait_for_overlap);
  3645. INIT_LIST_HEAD(&conf->handle_list);
  3646. INIT_LIST_HEAD(&conf->delayed_list);
  3647. INIT_LIST_HEAD(&conf->bitmap_list);
  3648. INIT_LIST_HEAD(&conf->inactive_list);
  3649. atomic_set(&conf->active_stripes, 0);
  3650. atomic_set(&conf->preread_active_stripes, 0);
  3651. atomic_set(&conf->active_aligned_reads, 0);
  3652. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  3653. ITERATE_RDEV(mddev,rdev,tmp) {
  3654. raid_disk = rdev->raid_disk;
  3655. if (raid_disk >= conf->raid_disks
  3656. || raid_disk < 0)
  3657. continue;
  3658. disk = conf->disks + raid_disk;
  3659. disk->rdev = rdev;
  3660. if (test_bit(In_sync, &rdev->flags)) {
  3661. char b[BDEVNAME_SIZE];
  3662. printk(KERN_INFO "raid5: device %s operational as raid"
  3663. " disk %d\n", bdevname(rdev->bdev,b),
  3664. raid_disk);
  3665. working_disks++;
  3666. }
  3667. }
  3668. /*
  3669. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3670. */
  3671. mddev->degraded = conf->raid_disks - working_disks;
  3672. conf->mddev = mddev;
  3673. conf->chunk_size = mddev->chunk_size;
  3674. conf->level = mddev->level;
  3675. if (conf->level == 6)
  3676. conf->max_degraded = 2;
  3677. else
  3678. conf->max_degraded = 1;
  3679. conf->algorithm = mddev->layout;
  3680. conf->max_nr_stripes = NR_STRIPES;
  3681. conf->expand_progress = mddev->reshape_position;
  3682. /* device size must be a multiple of chunk size */
  3683. mddev->size &= ~(mddev->chunk_size/1024 -1);
  3684. mddev->resync_max_sectors = mddev->size << 1;
  3685. if (conf->level == 6 && conf->raid_disks < 4) {
  3686. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3687. mdname(mddev), conf->raid_disks);
  3688. goto abort;
  3689. }
  3690. if (!conf->chunk_size || conf->chunk_size % 4) {
  3691. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3692. conf->chunk_size, mdname(mddev));
  3693. goto abort;
  3694. }
  3695. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  3696. printk(KERN_ERR
  3697. "raid5: unsupported parity algorithm %d for %s\n",
  3698. conf->algorithm, mdname(mddev));
  3699. goto abort;
  3700. }
  3701. if (mddev->degraded > conf->max_degraded) {
  3702. printk(KERN_ERR "raid5: not enough operational devices for %s"
  3703. " (%d/%d failed)\n",
  3704. mdname(mddev), mddev->degraded, conf->raid_disks);
  3705. goto abort;
  3706. }
  3707. if (mddev->degraded > 0 &&
  3708. mddev->recovery_cp != MaxSector) {
  3709. if (mddev->ok_start_degraded)
  3710. printk(KERN_WARNING
  3711. "raid5: starting dirty degraded array: %s"
  3712. "- data corruption possible.\n",
  3713. mdname(mddev));
  3714. else {
  3715. printk(KERN_ERR
  3716. "raid5: cannot start dirty degraded array for %s\n",
  3717. mdname(mddev));
  3718. goto abort;
  3719. }
  3720. }
  3721. {
  3722. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3723. if (!mddev->thread) {
  3724. printk(KERN_ERR
  3725. "raid5: couldn't allocate thread for %s\n",
  3726. mdname(mddev));
  3727. goto abort;
  3728. }
  3729. }
  3730. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3731. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3732. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3733. printk(KERN_ERR
  3734. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3735. shrink_stripes(conf);
  3736. md_unregister_thread(mddev->thread);
  3737. goto abort;
  3738. } else
  3739. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3740. memory, mdname(mddev));
  3741. if (mddev->degraded == 0)
  3742. printk("raid5: raid level %d set %s active with %d out of %d"
  3743. " devices, algorithm %d\n", conf->level, mdname(mddev),
  3744. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  3745. conf->algorithm);
  3746. else
  3747. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  3748. " out of %d devices, algorithm %d\n", conf->level,
  3749. mdname(mddev), mddev->raid_disks - mddev->degraded,
  3750. mddev->raid_disks, conf->algorithm);
  3751. print_raid5_conf(conf);
  3752. if (conf->expand_progress != MaxSector) {
  3753. printk("...ok start reshape thread\n");
  3754. conf->expand_lo = conf->expand_progress;
  3755. atomic_set(&conf->reshape_stripes, 0);
  3756. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3757. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3758. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3759. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3760. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3761. "%s_reshape");
  3762. }
  3763. /* read-ahead size must cover two whole stripes, which is
  3764. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3765. */
  3766. {
  3767. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3768. int stripe = data_disks *
  3769. (mddev->chunk_size / PAGE_SIZE);
  3770. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3771. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3772. }
  3773. /* Ok, everything is just fine now */
  3774. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  3775. printk(KERN_WARNING
  3776. "raid5: failed to create sysfs attributes for %s\n",
  3777. mdname(mddev));
  3778. mddev->queue->unplug_fn = raid5_unplug_device;
  3779. mddev->queue->issue_flush_fn = raid5_issue_flush;
  3780. mddev->queue->backing_dev_info.congested_data = mddev;
  3781. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  3782. mddev->array_size = mddev->size * (conf->previous_raid_disks -
  3783. conf->max_degraded);
  3784. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  3785. return 0;
  3786. abort:
  3787. if (conf) {
  3788. print_raid5_conf(conf);
  3789. safe_put_page(conf->spare_page);
  3790. kfree(conf->disks);
  3791. kfree(conf->stripe_hashtbl);
  3792. kfree(conf);
  3793. }
  3794. mddev->private = NULL;
  3795. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  3796. return -EIO;
  3797. }
  3798. static int stop(mddev_t *mddev)
  3799. {
  3800. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3801. md_unregister_thread(mddev->thread);
  3802. mddev->thread = NULL;
  3803. shrink_stripes(conf);
  3804. kfree(conf->stripe_hashtbl);
  3805. mddev->queue->backing_dev_info.congested_fn = NULL;
  3806. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3807. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3808. kfree(conf->disks);
  3809. kfree(conf);
  3810. mddev->private = NULL;
  3811. return 0;
  3812. }
  3813. #ifdef DEBUG
  3814. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  3815. {
  3816. int i;
  3817. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3818. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3819. seq_printf(seq, "sh %llu, count %d.\n",
  3820. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3821. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3822. for (i = 0; i < sh->disks; i++) {
  3823. seq_printf(seq, "(cache%d: %p %ld) ",
  3824. i, sh->dev[i].page, sh->dev[i].flags);
  3825. }
  3826. seq_printf(seq, "\n");
  3827. }
  3828. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  3829. {
  3830. struct stripe_head *sh;
  3831. struct hlist_node *hn;
  3832. int i;
  3833. spin_lock_irq(&conf->device_lock);
  3834. for (i = 0; i < NR_HASH; i++) {
  3835. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3836. if (sh->raid_conf != conf)
  3837. continue;
  3838. print_sh(seq, sh);
  3839. }
  3840. }
  3841. spin_unlock_irq(&conf->device_lock);
  3842. }
  3843. #endif
  3844. static void status (struct seq_file *seq, mddev_t *mddev)
  3845. {
  3846. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3847. int i;
  3848. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3849. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  3850. for (i = 0; i < conf->raid_disks; i++)
  3851. seq_printf (seq, "%s",
  3852. conf->disks[i].rdev &&
  3853. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3854. seq_printf (seq, "]");
  3855. #ifdef DEBUG
  3856. seq_printf (seq, "\n");
  3857. printall(seq, conf);
  3858. #endif
  3859. }
  3860. static void print_raid5_conf (raid5_conf_t *conf)
  3861. {
  3862. int i;
  3863. struct disk_info *tmp;
  3864. printk("RAID5 conf printout:\n");
  3865. if (!conf) {
  3866. printk("(conf==NULL)\n");
  3867. return;
  3868. }
  3869. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  3870. conf->raid_disks - conf->mddev->degraded);
  3871. for (i = 0; i < conf->raid_disks; i++) {
  3872. char b[BDEVNAME_SIZE];
  3873. tmp = conf->disks + i;
  3874. if (tmp->rdev)
  3875. printk(" disk %d, o:%d, dev:%s\n",
  3876. i, !test_bit(Faulty, &tmp->rdev->flags),
  3877. bdevname(tmp->rdev->bdev,b));
  3878. }
  3879. }
  3880. static int raid5_spare_active(mddev_t *mddev)
  3881. {
  3882. int i;
  3883. raid5_conf_t *conf = mddev->private;
  3884. struct disk_info *tmp;
  3885. for (i = 0; i < conf->raid_disks; i++) {
  3886. tmp = conf->disks + i;
  3887. if (tmp->rdev
  3888. && !test_bit(Faulty, &tmp->rdev->flags)
  3889. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  3890. unsigned long flags;
  3891. spin_lock_irqsave(&conf->device_lock, flags);
  3892. mddev->degraded--;
  3893. spin_unlock_irqrestore(&conf->device_lock, flags);
  3894. }
  3895. }
  3896. print_raid5_conf(conf);
  3897. return 0;
  3898. }
  3899. static int raid5_remove_disk(mddev_t *mddev, int number)
  3900. {
  3901. raid5_conf_t *conf = mddev->private;
  3902. int err = 0;
  3903. mdk_rdev_t *rdev;
  3904. struct disk_info *p = conf->disks + number;
  3905. print_raid5_conf(conf);
  3906. rdev = p->rdev;
  3907. if (rdev) {
  3908. if (test_bit(In_sync, &rdev->flags) ||
  3909. atomic_read(&rdev->nr_pending)) {
  3910. err = -EBUSY;
  3911. goto abort;
  3912. }
  3913. p->rdev = NULL;
  3914. synchronize_rcu();
  3915. if (atomic_read(&rdev->nr_pending)) {
  3916. /* lost the race, try later */
  3917. err = -EBUSY;
  3918. p->rdev = rdev;
  3919. }
  3920. }
  3921. abort:
  3922. print_raid5_conf(conf);
  3923. return err;
  3924. }
  3925. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3926. {
  3927. raid5_conf_t *conf = mddev->private;
  3928. int found = 0;
  3929. int disk;
  3930. struct disk_info *p;
  3931. if (mddev->degraded > conf->max_degraded)
  3932. /* no point adding a device */
  3933. return 0;
  3934. /*
  3935. * find the disk ... but prefer rdev->saved_raid_disk
  3936. * if possible.
  3937. */
  3938. if (rdev->saved_raid_disk >= 0 &&
  3939. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3940. disk = rdev->saved_raid_disk;
  3941. else
  3942. disk = 0;
  3943. for ( ; disk < conf->raid_disks; disk++)
  3944. if ((p=conf->disks + disk)->rdev == NULL) {
  3945. clear_bit(In_sync, &rdev->flags);
  3946. rdev->raid_disk = disk;
  3947. found = 1;
  3948. if (rdev->saved_raid_disk != disk)
  3949. conf->fullsync = 1;
  3950. rcu_assign_pointer(p->rdev, rdev);
  3951. break;
  3952. }
  3953. print_raid5_conf(conf);
  3954. return found;
  3955. }
  3956. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3957. {
  3958. /* no resync is happening, and there is enough space
  3959. * on all devices, so we can resize.
  3960. * We need to make sure resync covers any new space.
  3961. * If the array is shrinking we should possibly wait until
  3962. * any io in the removed space completes, but it hardly seems
  3963. * worth it.
  3964. */
  3965. raid5_conf_t *conf = mddev_to_conf(mddev);
  3966. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3967. mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
  3968. set_capacity(mddev->gendisk, mddev->array_size << 1);
  3969. mddev->changed = 1;
  3970. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3971. mddev->recovery_cp = mddev->size << 1;
  3972. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3973. }
  3974. mddev->size = sectors /2;
  3975. mddev->resync_max_sectors = sectors;
  3976. return 0;
  3977. }
  3978. #ifdef CONFIG_MD_RAID5_RESHAPE
  3979. static int raid5_check_reshape(mddev_t *mddev)
  3980. {
  3981. raid5_conf_t *conf = mddev_to_conf(mddev);
  3982. int err;
  3983. if (mddev->delta_disks < 0 ||
  3984. mddev->new_level != mddev->level)
  3985. return -EINVAL; /* Cannot shrink array or change level yet */
  3986. if (mddev->delta_disks == 0)
  3987. return 0; /* nothing to do */
  3988. /* Can only proceed if there are plenty of stripe_heads.
  3989. * We need a minimum of one full stripe,, and for sensible progress
  3990. * it is best to have about 4 times that.
  3991. * If we require 4 times, then the default 256 4K stripe_heads will
  3992. * allow for chunk sizes up to 256K, which is probably OK.
  3993. * If the chunk size is greater, user-space should request more
  3994. * stripe_heads first.
  3995. */
  3996. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3997. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3998. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3999. (mddev->chunk_size / STRIPE_SIZE)*4);
  4000. return -ENOSPC;
  4001. }
  4002. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4003. if (err)
  4004. return err;
  4005. if (mddev->degraded > conf->max_degraded)
  4006. return -EINVAL;
  4007. /* looks like we might be able to manage this */
  4008. return 0;
  4009. }
  4010. static int raid5_start_reshape(mddev_t *mddev)
  4011. {
  4012. raid5_conf_t *conf = mddev_to_conf(mddev);
  4013. mdk_rdev_t *rdev;
  4014. struct list_head *rtmp;
  4015. int spares = 0;
  4016. int added_devices = 0;
  4017. unsigned long flags;
  4018. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4019. return -EBUSY;
  4020. ITERATE_RDEV(mddev, rdev, rtmp)
  4021. if (rdev->raid_disk < 0 &&
  4022. !test_bit(Faulty, &rdev->flags))
  4023. spares++;
  4024. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4025. /* Not enough devices even to make a degraded array
  4026. * of that size
  4027. */
  4028. return -EINVAL;
  4029. atomic_set(&conf->reshape_stripes, 0);
  4030. spin_lock_irq(&conf->device_lock);
  4031. conf->previous_raid_disks = conf->raid_disks;
  4032. conf->raid_disks += mddev->delta_disks;
  4033. conf->expand_progress = 0;
  4034. conf->expand_lo = 0;
  4035. spin_unlock_irq(&conf->device_lock);
  4036. /* Add some new drives, as many as will fit.
  4037. * We know there are enough to make the newly sized array work.
  4038. */
  4039. ITERATE_RDEV(mddev, rdev, rtmp)
  4040. if (rdev->raid_disk < 0 &&
  4041. !test_bit(Faulty, &rdev->flags)) {
  4042. if (raid5_add_disk(mddev, rdev)) {
  4043. char nm[20];
  4044. set_bit(In_sync, &rdev->flags);
  4045. added_devices++;
  4046. rdev->recovery_offset = 0;
  4047. sprintf(nm, "rd%d", rdev->raid_disk);
  4048. if (sysfs_create_link(&mddev->kobj,
  4049. &rdev->kobj, nm))
  4050. printk(KERN_WARNING
  4051. "raid5: failed to create "
  4052. " link %s for %s\n",
  4053. nm, mdname(mddev));
  4054. } else
  4055. break;
  4056. }
  4057. spin_lock_irqsave(&conf->device_lock, flags);
  4058. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  4059. spin_unlock_irqrestore(&conf->device_lock, flags);
  4060. mddev->raid_disks = conf->raid_disks;
  4061. mddev->reshape_position = 0;
  4062. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4063. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4064. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4065. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4066. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4067. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4068. "%s_reshape");
  4069. if (!mddev->sync_thread) {
  4070. mddev->recovery = 0;
  4071. spin_lock_irq(&conf->device_lock);
  4072. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4073. conf->expand_progress = MaxSector;
  4074. spin_unlock_irq(&conf->device_lock);
  4075. return -EAGAIN;
  4076. }
  4077. md_wakeup_thread(mddev->sync_thread);
  4078. md_new_event(mddev);
  4079. return 0;
  4080. }
  4081. #endif
  4082. static void end_reshape(raid5_conf_t *conf)
  4083. {
  4084. struct block_device *bdev;
  4085. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4086. conf->mddev->array_size = conf->mddev->size *
  4087. (conf->raid_disks - conf->max_degraded);
  4088. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  4089. conf->mddev->changed = 1;
  4090. bdev = bdget_disk(conf->mddev->gendisk, 0);
  4091. if (bdev) {
  4092. mutex_lock(&bdev->bd_inode->i_mutex);
  4093. i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
  4094. mutex_unlock(&bdev->bd_inode->i_mutex);
  4095. bdput(bdev);
  4096. }
  4097. spin_lock_irq(&conf->device_lock);
  4098. conf->expand_progress = MaxSector;
  4099. spin_unlock_irq(&conf->device_lock);
  4100. conf->mddev->reshape_position = MaxSector;
  4101. /* read-ahead size must cover two whole stripes, which is
  4102. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4103. */
  4104. {
  4105. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4106. int stripe = data_disks *
  4107. (conf->mddev->chunk_size / PAGE_SIZE);
  4108. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4109. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4110. }
  4111. }
  4112. }
  4113. static void raid5_quiesce(mddev_t *mddev, int state)
  4114. {
  4115. raid5_conf_t *conf = mddev_to_conf(mddev);
  4116. switch(state) {
  4117. case 2: /* resume for a suspend */
  4118. wake_up(&conf->wait_for_overlap);
  4119. break;
  4120. case 1: /* stop all writes */
  4121. spin_lock_irq(&conf->device_lock);
  4122. conf->quiesce = 1;
  4123. wait_event_lock_irq(conf->wait_for_stripe,
  4124. atomic_read(&conf->active_stripes) == 0 &&
  4125. atomic_read(&conf->active_aligned_reads) == 0,
  4126. conf->device_lock, /* nothing */);
  4127. spin_unlock_irq(&conf->device_lock);
  4128. break;
  4129. case 0: /* re-enable writes */
  4130. spin_lock_irq(&conf->device_lock);
  4131. conf->quiesce = 0;
  4132. wake_up(&conf->wait_for_stripe);
  4133. wake_up(&conf->wait_for_overlap);
  4134. spin_unlock_irq(&conf->device_lock);
  4135. break;
  4136. }
  4137. }
  4138. static struct mdk_personality raid6_personality =
  4139. {
  4140. .name = "raid6",
  4141. .level = 6,
  4142. .owner = THIS_MODULE,
  4143. .make_request = make_request,
  4144. .run = run,
  4145. .stop = stop,
  4146. .status = status,
  4147. .error_handler = error,
  4148. .hot_add_disk = raid5_add_disk,
  4149. .hot_remove_disk= raid5_remove_disk,
  4150. .spare_active = raid5_spare_active,
  4151. .sync_request = sync_request,
  4152. .resize = raid5_resize,
  4153. #ifdef CONFIG_MD_RAID5_RESHAPE
  4154. .check_reshape = raid5_check_reshape,
  4155. .start_reshape = raid5_start_reshape,
  4156. #endif
  4157. .quiesce = raid5_quiesce,
  4158. };
  4159. static struct mdk_personality raid5_personality =
  4160. {
  4161. .name = "raid5",
  4162. .level = 5,
  4163. .owner = THIS_MODULE,
  4164. .make_request = make_request,
  4165. .run = run,
  4166. .stop = stop,
  4167. .status = status,
  4168. .error_handler = error,
  4169. .hot_add_disk = raid5_add_disk,
  4170. .hot_remove_disk= raid5_remove_disk,
  4171. .spare_active = raid5_spare_active,
  4172. .sync_request = sync_request,
  4173. .resize = raid5_resize,
  4174. #ifdef CONFIG_MD_RAID5_RESHAPE
  4175. .check_reshape = raid5_check_reshape,
  4176. .start_reshape = raid5_start_reshape,
  4177. #endif
  4178. .quiesce = raid5_quiesce,
  4179. };
  4180. static struct mdk_personality raid4_personality =
  4181. {
  4182. .name = "raid4",
  4183. .level = 4,
  4184. .owner = THIS_MODULE,
  4185. .make_request = make_request,
  4186. .run = run,
  4187. .stop = stop,
  4188. .status = status,
  4189. .error_handler = error,
  4190. .hot_add_disk = raid5_add_disk,
  4191. .hot_remove_disk= raid5_remove_disk,
  4192. .spare_active = raid5_spare_active,
  4193. .sync_request = sync_request,
  4194. .resize = raid5_resize,
  4195. #ifdef CONFIG_MD_RAID5_RESHAPE
  4196. .check_reshape = raid5_check_reshape,
  4197. .start_reshape = raid5_start_reshape,
  4198. #endif
  4199. .quiesce = raid5_quiesce,
  4200. };
  4201. static int __init raid5_init(void)
  4202. {
  4203. int e;
  4204. e = raid6_select_algo();
  4205. if ( e )
  4206. return e;
  4207. register_md_personality(&raid6_personality);
  4208. register_md_personality(&raid5_personality);
  4209. register_md_personality(&raid4_personality);
  4210. return 0;
  4211. }
  4212. static void raid5_exit(void)
  4213. {
  4214. unregister_md_personality(&raid6_personality);
  4215. unregister_md_personality(&raid5_personality);
  4216. unregister_md_personality(&raid4_personality);
  4217. }
  4218. module_init(raid5_init);
  4219. module_exit(raid5_exit);
  4220. MODULE_LICENSE("GPL");
  4221. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  4222. MODULE_ALIAS("md-raid5");
  4223. MODULE_ALIAS("md-raid4");
  4224. MODULE_ALIAS("md-level-5");
  4225. MODULE_ALIAS("md-level-4");
  4226. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  4227. MODULE_ALIAS("md-raid6");
  4228. MODULE_ALIAS("md-level-6");
  4229. /* This used to be two separate modules, they were: */
  4230. MODULE_ALIAS("raid5");
  4231. MODULE_ALIAS("raid6");