raid5.c 137 KB

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