raid5.c 138 KB

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