raid5.c 133 KB

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