raid10.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730
  1. /*
  2. * raid10.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Neil Brown
  5. *
  6. * RAID-10 support for md.
  7. *
  8. * Base on code in raid1.c. See raid1.c for further copyright information.
  9. *
  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. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/module.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/ratelimit.h>
  26. #include <linux/kthread.h>
  27. #include "md.h"
  28. #include "raid10.h"
  29. #include "raid0.h"
  30. #include "bitmap.h"
  31. /*
  32. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  33. * The layout of data is defined by
  34. * chunk_size
  35. * raid_disks
  36. * near_copies (stored in low byte of layout)
  37. * far_copies (stored in second byte of layout)
  38. * far_offset (stored in bit 16 of layout )
  39. * use_far_sets (stored in bit 17 of layout )
  40. *
  41. * The data to be stored is divided into chunks using chunksize. Each device
  42. * is divided into far_copies sections. In each section, chunks are laid out
  43. * in a style similar to raid0, but near_copies copies of each chunk is stored
  44. * (each on a different drive). The starting device for each section is offset
  45. * near_copies from the starting device of the previous section. Thus there
  46. * are (near_copies * far_copies) of each chunk, and each is on a different
  47. * drive. near_copies and far_copies must be at least one, and their product
  48. * is at most raid_disks.
  49. *
  50. * If far_offset is true, then the far_copies are handled a bit differently.
  51. * The copies are still in different stripes, but instead of being very far
  52. * apart on disk, there are adjacent stripes.
  53. *
  54. * The far and offset algorithms are handled slightly differently if
  55. * 'use_far_sets' is true. In this case, the array's devices are grouped into
  56. * sets that are (near_copies * far_copies) in size. The far copied stripes
  57. * are still shifted by 'near_copies' devices, but this shifting stays confined
  58. * to the set rather than the entire array. This is done to improve the number
  59. * of device combinations that can fail without causing the array to fail.
  60. * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
  61. * on a device):
  62. * A B C D A B C D E
  63. * ... ...
  64. * D A B C E A B C D
  65. * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
  66. * [A B] [C D] [A B] [C D E]
  67. * |...| |...| |...| | ... |
  68. * [B A] [D C] [B A] [E C D]
  69. */
  70. /*
  71. * Number of guaranteed r10bios in case of extreme VM load:
  72. */
  73. #define NR_RAID10_BIOS 256
  74. /* when we get a read error on a read-only array, we redirect to another
  75. * device without failing the first device, or trying to over-write to
  76. * correct the read error. To keep track of bad blocks on a per-bio
  77. * level, we store IO_BLOCKED in the appropriate 'bios' pointer
  78. */
  79. #define IO_BLOCKED ((struct bio *)1)
  80. /* When we successfully write to a known bad-block, we need to remove the
  81. * bad-block marking which must be done from process context. So we record
  82. * the success by setting devs[n].bio to IO_MADE_GOOD
  83. */
  84. #define IO_MADE_GOOD ((struct bio *)2)
  85. #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
  86. /* When there are this many requests queued to be written by
  87. * the raid10 thread, we become 'congested' to provide back-pressure
  88. * for writeback.
  89. */
  90. static int max_queued_requests = 1024;
  91. static void allow_barrier(struct r10conf *conf);
  92. static void lower_barrier(struct r10conf *conf);
  93. static int enough(struct r10conf *conf, int ignore);
  94. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  95. int *skipped);
  96. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
  97. static void end_reshape_write(struct bio *bio, int error);
  98. static void end_reshape(struct r10conf *conf);
  99. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  100. {
  101. struct r10conf *conf = data;
  102. int size = offsetof(struct r10bio, devs[conf->copies]);
  103. /* allocate a r10bio with room for raid_disks entries in the
  104. * bios array */
  105. return kzalloc(size, gfp_flags);
  106. }
  107. static void r10bio_pool_free(void *r10_bio, void *data)
  108. {
  109. kfree(r10_bio);
  110. }
  111. /* Maximum size of each resync request */
  112. #define RESYNC_BLOCK_SIZE (64*1024)
  113. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  114. /* amount of memory to reserve for resync requests */
  115. #define RESYNC_WINDOW (1024*1024)
  116. /* maximum number of concurrent requests, memory permitting */
  117. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  118. /*
  119. * When performing a resync, we need to read and compare, so
  120. * we need as many pages are there are copies.
  121. * When performing a recovery, we need 2 bios, one for read,
  122. * one for write (we recover only one drive per r10buf)
  123. *
  124. */
  125. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  126. {
  127. struct r10conf *conf = data;
  128. struct page *page;
  129. struct r10bio *r10_bio;
  130. struct bio *bio;
  131. int i, j;
  132. int nalloc;
  133. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  134. if (!r10_bio)
  135. return NULL;
  136. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
  137. test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
  138. nalloc = conf->copies; /* resync */
  139. else
  140. nalloc = 2; /* recovery */
  141. /*
  142. * Allocate bios.
  143. */
  144. for (j = nalloc ; j-- ; ) {
  145. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  146. if (!bio)
  147. goto out_free_bio;
  148. r10_bio->devs[j].bio = bio;
  149. if (!conf->have_replacement)
  150. continue;
  151. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  152. if (!bio)
  153. goto out_free_bio;
  154. r10_bio->devs[j].repl_bio = bio;
  155. }
  156. /*
  157. * Allocate RESYNC_PAGES data pages and attach them
  158. * where needed.
  159. */
  160. for (j = 0 ; j < nalloc; j++) {
  161. struct bio *rbio = r10_bio->devs[j].repl_bio;
  162. bio = r10_bio->devs[j].bio;
  163. for (i = 0; i < RESYNC_PAGES; i++) {
  164. if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
  165. &conf->mddev->recovery)) {
  166. /* we can share bv_page's during recovery
  167. * and reshape */
  168. struct bio *rbio = r10_bio->devs[0].bio;
  169. page = rbio->bi_io_vec[i].bv_page;
  170. get_page(page);
  171. } else
  172. page = alloc_page(gfp_flags);
  173. if (unlikely(!page))
  174. goto out_free_pages;
  175. bio->bi_io_vec[i].bv_page = page;
  176. if (rbio)
  177. rbio->bi_io_vec[i].bv_page = page;
  178. }
  179. }
  180. return r10_bio;
  181. out_free_pages:
  182. for ( ; i > 0 ; i--)
  183. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  184. while (j--)
  185. for (i = 0; i < RESYNC_PAGES ; i++)
  186. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  187. j = 0;
  188. out_free_bio:
  189. for ( ; j < nalloc; j++) {
  190. if (r10_bio->devs[j].bio)
  191. bio_put(r10_bio->devs[j].bio);
  192. if (r10_bio->devs[j].repl_bio)
  193. bio_put(r10_bio->devs[j].repl_bio);
  194. }
  195. r10bio_pool_free(r10_bio, conf);
  196. return NULL;
  197. }
  198. static void r10buf_pool_free(void *__r10_bio, void *data)
  199. {
  200. int i;
  201. struct r10conf *conf = data;
  202. struct r10bio *r10bio = __r10_bio;
  203. int j;
  204. for (j=0; j < conf->copies; j++) {
  205. struct bio *bio = r10bio->devs[j].bio;
  206. if (bio) {
  207. for (i = 0; i < RESYNC_PAGES; i++) {
  208. safe_put_page(bio->bi_io_vec[i].bv_page);
  209. bio->bi_io_vec[i].bv_page = NULL;
  210. }
  211. bio_put(bio);
  212. }
  213. bio = r10bio->devs[j].repl_bio;
  214. if (bio)
  215. bio_put(bio);
  216. }
  217. r10bio_pool_free(r10bio, conf);
  218. }
  219. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  220. {
  221. int i;
  222. for (i = 0; i < conf->copies; i++) {
  223. struct bio **bio = & r10_bio->devs[i].bio;
  224. if (!BIO_SPECIAL(*bio))
  225. bio_put(*bio);
  226. *bio = NULL;
  227. bio = &r10_bio->devs[i].repl_bio;
  228. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  229. bio_put(*bio);
  230. *bio = NULL;
  231. }
  232. }
  233. static void free_r10bio(struct r10bio *r10_bio)
  234. {
  235. struct r10conf *conf = r10_bio->mddev->private;
  236. put_all_bios(conf, r10_bio);
  237. mempool_free(r10_bio, conf->r10bio_pool);
  238. }
  239. static void put_buf(struct r10bio *r10_bio)
  240. {
  241. struct r10conf *conf = r10_bio->mddev->private;
  242. mempool_free(r10_bio, conf->r10buf_pool);
  243. lower_barrier(conf);
  244. }
  245. static void reschedule_retry(struct r10bio *r10_bio)
  246. {
  247. unsigned long flags;
  248. struct mddev *mddev = r10_bio->mddev;
  249. struct r10conf *conf = mddev->private;
  250. spin_lock_irqsave(&conf->device_lock, flags);
  251. list_add(&r10_bio->retry_list, &conf->retry_list);
  252. conf->nr_queued ++;
  253. spin_unlock_irqrestore(&conf->device_lock, flags);
  254. /* wake up frozen array... */
  255. wake_up(&conf->wait_barrier);
  256. md_wakeup_thread(mddev->thread);
  257. }
  258. /*
  259. * raid_end_bio_io() is called when we have finished servicing a mirrored
  260. * operation and are ready to return a success/failure code to the buffer
  261. * cache layer.
  262. */
  263. static void raid_end_bio_io(struct r10bio *r10_bio)
  264. {
  265. struct bio *bio = r10_bio->master_bio;
  266. int done;
  267. struct r10conf *conf = r10_bio->mddev->private;
  268. if (bio->bi_phys_segments) {
  269. unsigned long flags;
  270. spin_lock_irqsave(&conf->device_lock, flags);
  271. bio->bi_phys_segments--;
  272. done = (bio->bi_phys_segments == 0);
  273. spin_unlock_irqrestore(&conf->device_lock, flags);
  274. } else
  275. done = 1;
  276. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  277. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  278. if (done) {
  279. bio_endio(bio, 0);
  280. /*
  281. * Wake up any possible resync thread that waits for the device
  282. * to go idle.
  283. */
  284. allow_barrier(conf);
  285. }
  286. free_r10bio(r10_bio);
  287. }
  288. /*
  289. * Update disk head position estimator based on IRQ completion info.
  290. */
  291. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  292. {
  293. struct r10conf *conf = r10_bio->mddev->private;
  294. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  295. r10_bio->devs[slot].addr + (r10_bio->sectors);
  296. }
  297. /*
  298. * Find the disk number which triggered given bio
  299. */
  300. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  301. struct bio *bio, int *slotp, int *replp)
  302. {
  303. int slot;
  304. int repl = 0;
  305. for (slot = 0; slot < conf->copies; slot++) {
  306. if (r10_bio->devs[slot].bio == bio)
  307. break;
  308. if (r10_bio->devs[slot].repl_bio == bio) {
  309. repl = 1;
  310. break;
  311. }
  312. }
  313. BUG_ON(slot == conf->copies);
  314. update_head_pos(slot, r10_bio);
  315. if (slotp)
  316. *slotp = slot;
  317. if (replp)
  318. *replp = repl;
  319. return r10_bio->devs[slot].devnum;
  320. }
  321. static void raid10_end_read_request(struct bio *bio, int error)
  322. {
  323. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  324. struct r10bio *r10_bio = bio->bi_private;
  325. int slot, dev;
  326. struct md_rdev *rdev;
  327. struct r10conf *conf = r10_bio->mddev->private;
  328. slot = r10_bio->read_slot;
  329. dev = r10_bio->devs[slot].devnum;
  330. rdev = r10_bio->devs[slot].rdev;
  331. /*
  332. * this branch is our 'one mirror IO has finished' event handler:
  333. */
  334. update_head_pos(slot, r10_bio);
  335. if (uptodate) {
  336. /*
  337. * Set R10BIO_Uptodate in our master bio, so that
  338. * we will return a good error code to the higher
  339. * levels even if IO on some other mirrored buffer fails.
  340. *
  341. * The 'master' represents the composite IO operation to
  342. * user-side. So if something waits for IO, then it will
  343. * wait for the 'master' bio.
  344. */
  345. set_bit(R10BIO_Uptodate, &r10_bio->state);
  346. } else {
  347. /* If all other devices that store this block have
  348. * failed, we want to return the error upwards rather
  349. * than fail the last device. Here we redefine
  350. * "uptodate" to mean "Don't want to retry"
  351. */
  352. unsigned long flags;
  353. spin_lock_irqsave(&conf->device_lock, flags);
  354. if (!enough(conf, rdev->raid_disk))
  355. uptodate = 1;
  356. spin_unlock_irqrestore(&conf->device_lock, flags);
  357. }
  358. if (uptodate) {
  359. raid_end_bio_io(r10_bio);
  360. rdev_dec_pending(rdev, conf->mddev);
  361. } else {
  362. /*
  363. * oops, read error - keep the refcount on the rdev
  364. */
  365. char b[BDEVNAME_SIZE];
  366. printk_ratelimited(KERN_ERR
  367. "md/raid10:%s: %s: rescheduling sector %llu\n",
  368. mdname(conf->mddev),
  369. bdevname(rdev->bdev, b),
  370. (unsigned long long)r10_bio->sector);
  371. set_bit(R10BIO_ReadError, &r10_bio->state);
  372. reschedule_retry(r10_bio);
  373. }
  374. }
  375. static void close_write(struct r10bio *r10_bio)
  376. {
  377. /* clear the bitmap if all writes complete successfully */
  378. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  379. r10_bio->sectors,
  380. !test_bit(R10BIO_Degraded, &r10_bio->state),
  381. 0);
  382. md_write_end(r10_bio->mddev);
  383. }
  384. static void one_write_done(struct r10bio *r10_bio)
  385. {
  386. if (atomic_dec_and_test(&r10_bio->remaining)) {
  387. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  388. reschedule_retry(r10_bio);
  389. else {
  390. close_write(r10_bio);
  391. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  392. reschedule_retry(r10_bio);
  393. else
  394. raid_end_bio_io(r10_bio);
  395. }
  396. }
  397. }
  398. static void raid10_end_write_request(struct bio *bio, int error)
  399. {
  400. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  401. struct r10bio *r10_bio = bio->bi_private;
  402. int dev;
  403. int dec_rdev = 1;
  404. struct r10conf *conf = r10_bio->mddev->private;
  405. int slot, repl;
  406. struct md_rdev *rdev = NULL;
  407. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  408. if (repl)
  409. rdev = conf->mirrors[dev].replacement;
  410. if (!rdev) {
  411. smp_rmb();
  412. repl = 0;
  413. rdev = conf->mirrors[dev].rdev;
  414. }
  415. /*
  416. * this branch is our 'one mirror IO has finished' event handler:
  417. */
  418. if (!uptodate) {
  419. if (repl)
  420. /* Never record new bad blocks to replacement,
  421. * just fail it.
  422. */
  423. md_error(rdev->mddev, rdev);
  424. else {
  425. set_bit(WriteErrorSeen, &rdev->flags);
  426. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  427. set_bit(MD_RECOVERY_NEEDED,
  428. &rdev->mddev->recovery);
  429. set_bit(R10BIO_WriteError, &r10_bio->state);
  430. dec_rdev = 0;
  431. }
  432. } else {
  433. /*
  434. * Set R10BIO_Uptodate in our master bio, so that
  435. * we will return a good error code for to the higher
  436. * levels even if IO on some other mirrored buffer fails.
  437. *
  438. * The 'master' represents the composite IO operation to
  439. * user-side. So if something waits for IO, then it will
  440. * wait for the 'master' bio.
  441. */
  442. sector_t first_bad;
  443. int bad_sectors;
  444. set_bit(R10BIO_Uptodate, &r10_bio->state);
  445. /* Maybe we can clear some bad blocks. */
  446. if (is_badblock(rdev,
  447. r10_bio->devs[slot].addr,
  448. r10_bio->sectors,
  449. &first_bad, &bad_sectors)) {
  450. bio_put(bio);
  451. if (repl)
  452. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  453. else
  454. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  455. dec_rdev = 0;
  456. set_bit(R10BIO_MadeGood, &r10_bio->state);
  457. }
  458. }
  459. /*
  460. *
  461. * Let's see if all mirrored write operations have finished
  462. * already.
  463. */
  464. one_write_done(r10_bio);
  465. if (dec_rdev)
  466. rdev_dec_pending(rdev, conf->mddev);
  467. }
  468. /*
  469. * RAID10 layout manager
  470. * As well as the chunksize and raid_disks count, there are two
  471. * parameters: near_copies and far_copies.
  472. * near_copies * far_copies must be <= raid_disks.
  473. * Normally one of these will be 1.
  474. * If both are 1, we get raid0.
  475. * If near_copies == raid_disks, we get raid1.
  476. *
  477. * Chunks are laid out in raid0 style with near_copies copies of the
  478. * first chunk, followed by near_copies copies of the next chunk and
  479. * so on.
  480. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  481. * as described above, we start again with a device offset of near_copies.
  482. * So we effectively have another copy of the whole array further down all
  483. * the drives, but with blocks on different drives.
  484. * With this layout, and block is never stored twice on the one device.
  485. *
  486. * raid10_find_phys finds the sector offset of a given virtual sector
  487. * on each device that it is on.
  488. *
  489. * raid10_find_virt does the reverse mapping, from a device and a
  490. * sector offset to a virtual address
  491. */
  492. static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
  493. {
  494. int n,f;
  495. sector_t sector;
  496. sector_t chunk;
  497. sector_t stripe;
  498. int dev;
  499. int slot = 0;
  500. int last_far_set_start, last_far_set_size;
  501. last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
  502. last_far_set_start *= geo->far_set_size;
  503. last_far_set_size = geo->far_set_size;
  504. last_far_set_size += (geo->raid_disks % geo->far_set_size);
  505. /* now calculate first sector/dev */
  506. chunk = r10bio->sector >> geo->chunk_shift;
  507. sector = r10bio->sector & geo->chunk_mask;
  508. chunk *= geo->near_copies;
  509. stripe = chunk;
  510. dev = sector_div(stripe, geo->raid_disks);
  511. if (geo->far_offset)
  512. stripe *= geo->far_copies;
  513. sector += stripe << geo->chunk_shift;
  514. /* and calculate all the others */
  515. for (n = 0; n < geo->near_copies; n++) {
  516. int d = dev;
  517. int set;
  518. sector_t s = sector;
  519. r10bio->devs[slot].devnum = d;
  520. r10bio->devs[slot].addr = s;
  521. slot++;
  522. for (f = 1; f < geo->far_copies; f++) {
  523. set = d / geo->far_set_size;
  524. d += geo->near_copies;
  525. if ((geo->raid_disks % geo->far_set_size) &&
  526. (d > last_far_set_start)) {
  527. d -= last_far_set_start;
  528. d %= last_far_set_size;
  529. d += last_far_set_start;
  530. } else {
  531. d %= geo->far_set_size;
  532. d += geo->far_set_size * set;
  533. }
  534. s += geo->stride;
  535. r10bio->devs[slot].devnum = d;
  536. r10bio->devs[slot].addr = s;
  537. slot++;
  538. }
  539. dev++;
  540. if (dev >= geo->raid_disks) {
  541. dev = 0;
  542. sector += (geo->chunk_mask + 1);
  543. }
  544. }
  545. }
  546. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  547. {
  548. struct geom *geo = &conf->geo;
  549. if (conf->reshape_progress != MaxSector &&
  550. ((r10bio->sector >= conf->reshape_progress) !=
  551. conf->mddev->reshape_backwards)) {
  552. set_bit(R10BIO_Previous, &r10bio->state);
  553. geo = &conf->prev;
  554. } else
  555. clear_bit(R10BIO_Previous, &r10bio->state);
  556. __raid10_find_phys(geo, r10bio);
  557. }
  558. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  559. {
  560. sector_t offset, chunk, vchunk;
  561. /* Never use conf->prev as this is only called during resync
  562. * or recovery, so reshape isn't happening
  563. */
  564. struct geom *geo = &conf->geo;
  565. int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
  566. int far_set_size = geo->far_set_size;
  567. int last_far_set_start;
  568. if (geo->raid_disks % geo->far_set_size) {
  569. last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
  570. last_far_set_start *= geo->far_set_size;
  571. if (dev >= last_far_set_start) {
  572. far_set_size = geo->far_set_size;
  573. far_set_size += (geo->raid_disks % geo->far_set_size);
  574. far_set_start = last_far_set_start;
  575. }
  576. }
  577. offset = sector & geo->chunk_mask;
  578. if (geo->far_offset) {
  579. int fc;
  580. chunk = sector >> geo->chunk_shift;
  581. fc = sector_div(chunk, geo->far_copies);
  582. dev -= fc * geo->near_copies;
  583. if (dev < far_set_start)
  584. dev += far_set_size;
  585. } else {
  586. while (sector >= geo->stride) {
  587. sector -= geo->stride;
  588. if (dev < (geo->near_copies + far_set_start))
  589. dev += far_set_size - geo->near_copies;
  590. else
  591. dev -= geo->near_copies;
  592. }
  593. chunk = sector >> geo->chunk_shift;
  594. }
  595. vchunk = chunk * geo->raid_disks + dev;
  596. sector_div(vchunk, geo->near_copies);
  597. return (vchunk << geo->chunk_shift) + offset;
  598. }
  599. /**
  600. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  601. * @q: request queue
  602. * @bvm: properties of new bio
  603. * @biovec: the request that could be merged to it.
  604. *
  605. * Return amount of bytes we can accept at this offset
  606. * This requires checking for end-of-chunk if near_copies != raid_disks,
  607. * and for subordinate merge_bvec_fns if merge_check_needed.
  608. */
  609. static int raid10_mergeable_bvec(struct request_queue *q,
  610. struct bvec_merge_data *bvm,
  611. struct bio_vec *biovec)
  612. {
  613. struct mddev *mddev = q->queuedata;
  614. struct r10conf *conf = mddev->private;
  615. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  616. int max;
  617. unsigned int chunk_sectors;
  618. unsigned int bio_sectors = bvm->bi_size >> 9;
  619. struct geom *geo = &conf->geo;
  620. chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
  621. if (conf->reshape_progress != MaxSector &&
  622. ((sector >= conf->reshape_progress) !=
  623. conf->mddev->reshape_backwards))
  624. geo = &conf->prev;
  625. if (geo->near_copies < geo->raid_disks) {
  626. max = (chunk_sectors - ((sector & (chunk_sectors - 1))
  627. + bio_sectors)) << 9;
  628. if (max < 0)
  629. /* bio_add cannot handle a negative return */
  630. max = 0;
  631. if (max <= biovec->bv_len && bio_sectors == 0)
  632. return biovec->bv_len;
  633. } else
  634. max = biovec->bv_len;
  635. if (mddev->merge_check_needed) {
  636. struct {
  637. struct r10bio r10_bio;
  638. struct r10dev devs[conf->copies];
  639. } on_stack;
  640. struct r10bio *r10_bio = &on_stack.r10_bio;
  641. int s;
  642. if (conf->reshape_progress != MaxSector) {
  643. /* Cannot give any guidance during reshape */
  644. if (max <= biovec->bv_len && bio_sectors == 0)
  645. return biovec->bv_len;
  646. return 0;
  647. }
  648. r10_bio->sector = sector;
  649. raid10_find_phys(conf, r10_bio);
  650. rcu_read_lock();
  651. for (s = 0; s < conf->copies; s++) {
  652. int disk = r10_bio->devs[s].devnum;
  653. struct md_rdev *rdev = rcu_dereference(
  654. conf->mirrors[disk].rdev);
  655. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  656. struct request_queue *q =
  657. bdev_get_queue(rdev->bdev);
  658. if (q->merge_bvec_fn) {
  659. bvm->bi_sector = r10_bio->devs[s].addr
  660. + rdev->data_offset;
  661. bvm->bi_bdev = rdev->bdev;
  662. max = min(max, q->merge_bvec_fn(
  663. q, bvm, biovec));
  664. }
  665. }
  666. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  667. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  668. struct request_queue *q =
  669. bdev_get_queue(rdev->bdev);
  670. if (q->merge_bvec_fn) {
  671. bvm->bi_sector = r10_bio->devs[s].addr
  672. + rdev->data_offset;
  673. bvm->bi_bdev = rdev->bdev;
  674. max = min(max, q->merge_bvec_fn(
  675. q, bvm, biovec));
  676. }
  677. }
  678. }
  679. rcu_read_unlock();
  680. }
  681. return max;
  682. }
  683. /*
  684. * This routine returns the disk from which the requested read should
  685. * be done. There is a per-array 'next expected sequential IO' sector
  686. * number - if this matches on the next IO then we use the last disk.
  687. * There is also a per-disk 'last know head position' sector that is
  688. * maintained from IRQ contexts, both the normal and the resync IO
  689. * completion handlers update this position correctly. If there is no
  690. * perfect sequential match then we pick the disk whose head is closest.
  691. *
  692. * If there are 2 mirrors in the same 2 devices, performance degrades
  693. * because position is mirror, not device based.
  694. *
  695. * The rdev for the device selected will have nr_pending incremented.
  696. */
  697. /*
  698. * FIXME: possibly should rethink readbalancing and do it differently
  699. * depending on near_copies / far_copies geometry.
  700. */
  701. static struct md_rdev *read_balance(struct r10conf *conf,
  702. struct r10bio *r10_bio,
  703. int *max_sectors)
  704. {
  705. const sector_t this_sector = r10_bio->sector;
  706. int disk, slot;
  707. int sectors = r10_bio->sectors;
  708. int best_good_sectors;
  709. sector_t new_distance, best_dist;
  710. struct md_rdev *best_rdev, *rdev = NULL;
  711. int do_balance;
  712. int best_slot;
  713. struct geom *geo = &conf->geo;
  714. raid10_find_phys(conf, r10_bio);
  715. rcu_read_lock();
  716. retry:
  717. sectors = r10_bio->sectors;
  718. best_slot = -1;
  719. best_rdev = NULL;
  720. best_dist = MaxSector;
  721. best_good_sectors = 0;
  722. do_balance = 1;
  723. /*
  724. * Check if we can balance. We can balance on the whole
  725. * device if no resync is going on (recovery is ok), or below
  726. * the resync window. We take the first readable disk when
  727. * above the resync window.
  728. */
  729. if (conf->mddev->recovery_cp < MaxSector
  730. && (this_sector + sectors >= conf->next_resync))
  731. do_balance = 0;
  732. for (slot = 0; slot < conf->copies ; slot++) {
  733. sector_t first_bad;
  734. int bad_sectors;
  735. sector_t dev_sector;
  736. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  737. continue;
  738. disk = r10_bio->devs[slot].devnum;
  739. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  740. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  741. test_bit(Unmerged, &rdev->flags) ||
  742. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  743. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  744. if (rdev == NULL ||
  745. test_bit(Faulty, &rdev->flags) ||
  746. test_bit(Unmerged, &rdev->flags))
  747. continue;
  748. if (!test_bit(In_sync, &rdev->flags) &&
  749. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  750. continue;
  751. dev_sector = r10_bio->devs[slot].addr;
  752. if (is_badblock(rdev, dev_sector, sectors,
  753. &first_bad, &bad_sectors)) {
  754. if (best_dist < MaxSector)
  755. /* Already have a better slot */
  756. continue;
  757. if (first_bad <= dev_sector) {
  758. /* Cannot read here. If this is the
  759. * 'primary' device, then we must not read
  760. * beyond 'bad_sectors' from another device.
  761. */
  762. bad_sectors -= (dev_sector - first_bad);
  763. if (!do_balance && sectors > bad_sectors)
  764. sectors = bad_sectors;
  765. if (best_good_sectors > sectors)
  766. best_good_sectors = sectors;
  767. } else {
  768. sector_t good_sectors =
  769. first_bad - dev_sector;
  770. if (good_sectors > best_good_sectors) {
  771. best_good_sectors = good_sectors;
  772. best_slot = slot;
  773. best_rdev = rdev;
  774. }
  775. if (!do_balance)
  776. /* Must read from here */
  777. break;
  778. }
  779. continue;
  780. } else
  781. best_good_sectors = sectors;
  782. if (!do_balance)
  783. break;
  784. /* This optimisation is debatable, and completely destroys
  785. * sequential read speed for 'far copies' arrays. So only
  786. * keep it for 'near' arrays, and review those later.
  787. */
  788. if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  789. break;
  790. /* for far > 1 always use the lowest address */
  791. if (geo->far_copies > 1)
  792. new_distance = r10_bio->devs[slot].addr;
  793. else
  794. new_distance = abs(r10_bio->devs[slot].addr -
  795. conf->mirrors[disk].head_position);
  796. if (new_distance < best_dist) {
  797. best_dist = new_distance;
  798. best_slot = slot;
  799. best_rdev = rdev;
  800. }
  801. }
  802. if (slot >= conf->copies) {
  803. slot = best_slot;
  804. rdev = best_rdev;
  805. }
  806. if (slot >= 0) {
  807. atomic_inc(&rdev->nr_pending);
  808. if (test_bit(Faulty, &rdev->flags)) {
  809. /* Cannot risk returning a device that failed
  810. * before we inc'ed nr_pending
  811. */
  812. rdev_dec_pending(rdev, conf->mddev);
  813. goto retry;
  814. }
  815. r10_bio->read_slot = slot;
  816. } else
  817. rdev = NULL;
  818. rcu_read_unlock();
  819. *max_sectors = best_good_sectors;
  820. return rdev;
  821. }
  822. int md_raid10_congested(struct mddev *mddev, int bits)
  823. {
  824. struct r10conf *conf = mddev->private;
  825. int i, ret = 0;
  826. if ((bits & (1 << BDI_async_congested)) &&
  827. conf->pending_count >= max_queued_requests)
  828. return 1;
  829. rcu_read_lock();
  830. for (i = 0;
  831. (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
  832. && ret == 0;
  833. i++) {
  834. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  835. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  836. struct request_queue *q = bdev_get_queue(rdev->bdev);
  837. ret |= bdi_congested(&q->backing_dev_info, bits);
  838. }
  839. }
  840. rcu_read_unlock();
  841. return ret;
  842. }
  843. EXPORT_SYMBOL_GPL(md_raid10_congested);
  844. static int raid10_congested(void *data, int bits)
  845. {
  846. struct mddev *mddev = data;
  847. return mddev_congested(mddev, bits) ||
  848. md_raid10_congested(mddev, bits);
  849. }
  850. static void flush_pending_writes(struct r10conf *conf)
  851. {
  852. /* Any writes that have been queued but are awaiting
  853. * bitmap updates get flushed here.
  854. */
  855. spin_lock_irq(&conf->device_lock);
  856. if (conf->pending_bio_list.head) {
  857. struct bio *bio;
  858. bio = bio_list_get(&conf->pending_bio_list);
  859. conf->pending_count = 0;
  860. spin_unlock_irq(&conf->device_lock);
  861. /* flush any pending bitmap writes to disk
  862. * before proceeding w/ I/O */
  863. bitmap_unplug(conf->mddev->bitmap);
  864. wake_up(&conf->wait_barrier);
  865. while (bio) { /* submit pending writes */
  866. struct bio *next = bio->bi_next;
  867. bio->bi_next = NULL;
  868. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  869. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  870. /* Just ignore it */
  871. bio_endio(bio, 0);
  872. else
  873. generic_make_request(bio);
  874. bio = next;
  875. }
  876. } else
  877. spin_unlock_irq(&conf->device_lock);
  878. }
  879. /* Barriers....
  880. * Sometimes we need to suspend IO while we do something else,
  881. * either some resync/recovery, or reconfigure the array.
  882. * To do this we raise a 'barrier'.
  883. * The 'barrier' is a counter that can be raised multiple times
  884. * to count how many activities are happening which preclude
  885. * normal IO.
  886. * We can only raise the barrier if there is no pending IO.
  887. * i.e. if nr_pending == 0.
  888. * We choose only to raise the barrier if no-one is waiting for the
  889. * barrier to go down. This means that as soon as an IO request
  890. * is ready, no other operations which require a barrier will start
  891. * until the IO request has had a chance.
  892. *
  893. * So: regular IO calls 'wait_barrier'. When that returns there
  894. * is no backgroup IO happening, It must arrange to call
  895. * allow_barrier when it has finished its IO.
  896. * backgroup IO calls must call raise_barrier. Once that returns
  897. * there is no normal IO happeing. It must arrange to call
  898. * lower_barrier when the particular background IO completes.
  899. */
  900. static void raise_barrier(struct r10conf *conf, int force)
  901. {
  902. BUG_ON(force && !conf->barrier);
  903. spin_lock_irq(&conf->resync_lock);
  904. /* Wait until no block IO is waiting (unless 'force') */
  905. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  906. conf->resync_lock);
  907. /* block any new IO from starting */
  908. conf->barrier++;
  909. /* Now wait for all pending IO to complete */
  910. wait_event_lock_irq(conf->wait_barrier,
  911. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  912. conf->resync_lock);
  913. spin_unlock_irq(&conf->resync_lock);
  914. }
  915. static void lower_barrier(struct r10conf *conf)
  916. {
  917. unsigned long flags;
  918. spin_lock_irqsave(&conf->resync_lock, flags);
  919. conf->barrier--;
  920. spin_unlock_irqrestore(&conf->resync_lock, flags);
  921. wake_up(&conf->wait_barrier);
  922. }
  923. static void wait_barrier(struct r10conf *conf)
  924. {
  925. spin_lock_irq(&conf->resync_lock);
  926. if (conf->barrier) {
  927. conf->nr_waiting++;
  928. /* Wait for the barrier to drop.
  929. * However if there are already pending
  930. * requests (preventing the barrier from
  931. * rising completely), and the
  932. * pre-process bio queue isn't empty,
  933. * then don't wait, as we need to empty
  934. * that queue to get the nr_pending
  935. * count down.
  936. */
  937. wait_event_lock_irq(conf->wait_barrier,
  938. !conf->barrier ||
  939. (conf->nr_pending &&
  940. current->bio_list &&
  941. !bio_list_empty(current->bio_list)),
  942. conf->resync_lock);
  943. conf->nr_waiting--;
  944. }
  945. conf->nr_pending++;
  946. spin_unlock_irq(&conf->resync_lock);
  947. }
  948. static void allow_barrier(struct r10conf *conf)
  949. {
  950. unsigned long flags;
  951. spin_lock_irqsave(&conf->resync_lock, flags);
  952. conf->nr_pending--;
  953. spin_unlock_irqrestore(&conf->resync_lock, flags);
  954. wake_up(&conf->wait_barrier);
  955. }
  956. static void freeze_array(struct r10conf *conf)
  957. {
  958. /* stop syncio and normal IO and wait for everything to
  959. * go quiet.
  960. * We increment barrier and nr_waiting, and then
  961. * wait until nr_pending match nr_queued+1
  962. * This is called in the context of one normal IO request
  963. * that has failed. Thus any sync request that might be pending
  964. * will be blocked by nr_pending, and we need to wait for
  965. * pending IO requests to complete or be queued for re-try.
  966. * Thus the number queued (nr_queued) plus this request (1)
  967. * must match the number of pending IOs (nr_pending) before
  968. * we continue.
  969. */
  970. spin_lock_irq(&conf->resync_lock);
  971. conf->barrier++;
  972. conf->nr_waiting++;
  973. wait_event_lock_irq_cmd(conf->wait_barrier,
  974. conf->nr_pending == conf->nr_queued+1,
  975. conf->resync_lock,
  976. flush_pending_writes(conf));
  977. spin_unlock_irq(&conf->resync_lock);
  978. }
  979. static void unfreeze_array(struct r10conf *conf)
  980. {
  981. /* reverse the effect of the freeze */
  982. spin_lock_irq(&conf->resync_lock);
  983. conf->barrier--;
  984. conf->nr_waiting--;
  985. wake_up(&conf->wait_barrier);
  986. spin_unlock_irq(&conf->resync_lock);
  987. }
  988. static sector_t choose_data_offset(struct r10bio *r10_bio,
  989. struct md_rdev *rdev)
  990. {
  991. if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
  992. test_bit(R10BIO_Previous, &r10_bio->state))
  993. return rdev->data_offset;
  994. else
  995. return rdev->new_data_offset;
  996. }
  997. struct raid10_plug_cb {
  998. struct blk_plug_cb cb;
  999. struct bio_list pending;
  1000. int pending_cnt;
  1001. };
  1002. static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1003. {
  1004. struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
  1005. cb);
  1006. struct mddev *mddev = plug->cb.data;
  1007. struct r10conf *conf = mddev->private;
  1008. struct bio *bio;
  1009. if (from_schedule || current->bio_list) {
  1010. spin_lock_irq(&conf->device_lock);
  1011. bio_list_merge(&conf->pending_bio_list, &plug->pending);
  1012. conf->pending_count += plug->pending_cnt;
  1013. spin_unlock_irq(&conf->device_lock);
  1014. wake_up(&conf->wait_barrier);
  1015. md_wakeup_thread(mddev->thread);
  1016. kfree(plug);
  1017. return;
  1018. }
  1019. /* we aren't scheduling, so we can do the write-out directly. */
  1020. bio = bio_list_get(&plug->pending);
  1021. bitmap_unplug(mddev->bitmap);
  1022. wake_up(&conf->wait_barrier);
  1023. while (bio) { /* submit pending writes */
  1024. struct bio *next = bio->bi_next;
  1025. bio->bi_next = NULL;
  1026. generic_make_request(bio);
  1027. bio = next;
  1028. }
  1029. kfree(plug);
  1030. }
  1031. static void make_request(struct mddev *mddev, struct bio * bio)
  1032. {
  1033. struct r10conf *conf = mddev->private;
  1034. struct r10bio *r10_bio;
  1035. struct bio *read_bio;
  1036. int i;
  1037. sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
  1038. int chunk_sects = chunk_mask + 1;
  1039. const int rw = bio_data_dir(bio);
  1040. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  1041. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  1042. const unsigned long do_discard = (bio->bi_rw
  1043. & (REQ_DISCARD | REQ_SECURE));
  1044. const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
  1045. unsigned long flags;
  1046. struct md_rdev *blocked_rdev;
  1047. struct blk_plug_cb *cb;
  1048. struct raid10_plug_cb *plug = NULL;
  1049. int sectors_handled;
  1050. int max_sectors;
  1051. int sectors;
  1052. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  1053. md_flush_request(mddev, bio);
  1054. return;
  1055. }
  1056. /* If this request crosses a chunk boundary, we need to
  1057. * split it. This will only happen for 1 PAGE (or less) requests.
  1058. */
  1059. if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
  1060. > chunk_sects
  1061. && (conf->geo.near_copies < conf->geo.raid_disks
  1062. || conf->prev.near_copies < conf->prev.raid_disks))) {
  1063. struct bio_pair *bp;
  1064. /* Sanity check -- queue functions should prevent this happening */
  1065. if ((bio->bi_vcnt != 1 && bio->bi_vcnt != 0) ||
  1066. bio->bi_idx != 0)
  1067. goto bad_map;
  1068. /* This is a one page bio that upper layers
  1069. * refuse to split for us, so we need to split it.
  1070. */
  1071. bp = bio_split(bio,
  1072. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  1073. /* Each of these 'make_request' calls will call 'wait_barrier'.
  1074. * If the first succeeds but the second blocks due to the resync
  1075. * thread raising the barrier, we will deadlock because the
  1076. * IO to the underlying device will be queued in generic_make_request
  1077. * and will never complete, so will never reduce nr_pending.
  1078. * So increment nr_waiting here so no new raise_barriers will
  1079. * succeed, and so the second wait_barrier cannot block.
  1080. */
  1081. spin_lock_irq(&conf->resync_lock);
  1082. conf->nr_waiting++;
  1083. spin_unlock_irq(&conf->resync_lock);
  1084. make_request(mddev, &bp->bio1);
  1085. make_request(mddev, &bp->bio2);
  1086. spin_lock_irq(&conf->resync_lock);
  1087. conf->nr_waiting--;
  1088. wake_up(&conf->wait_barrier);
  1089. spin_unlock_irq(&conf->resync_lock);
  1090. bio_pair_release(bp);
  1091. return;
  1092. bad_map:
  1093. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  1094. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  1095. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  1096. bio_io_error(bio);
  1097. return;
  1098. }
  1099. md_write_start(mddev, bio);
  1100. /*
  1101. * Register the new request and wait if the reconstruction
  1102. * thread has put up a bar for new requests.
  1103. * Continue immediately if no resync is active currently.
  1104. */
  1105. wait_barrier(conf);
  1106. sectors = bio->bi_size >> 9;
  1107. while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  1108. bio->bi_sector < conf->reshape_progress &&
  1109. bio->bi_sector + sectors > conf->reshape_progress) {
  1110. /* IO spans the reshape position. Need to wait for
  1111. * reshape to pass
  1112. */
  1113. allow_barrier(conf);
  1114. wait_event(conf->wait_barrier,
  1115. conf->reshape_progress <= bio->bi_sector ||
  1116. conf->reshape_progress >= bio->bi_sector + sectors);
  1117. wait_barrier(conf);
  1118. }
  1119. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  1120. bio_data_dir(bio) == WRITE &&
  1121. (mddev->reshape_backwards
  1122. ? (bio->bi_sector < conf->reshape_safe &&
  1123. bio->bi_sector + sectors > conf->reshape_progress)
  1124. : (bio->bi_sector + sectors > conf->reshape_safe &&
  1125. bio->bi_sector < conf->reshape_progress))) {
  1126. /* Need to update reshape_position in metadata */
  1127. mddev->reshape_position = conf->reshape_progress;
  1128. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1129. set_bit(MD_CHANGE_PENDING, &mddev->flags);
  1130. md_wakeup_thread(mddev->thread);
  1131. wait_event(mddev->sb_wait,
  1132. !test_bit(MD_CHANGE_PENDING, &mddev->flags));
  1133. conf->reshape_safe = mddev->reshape_position;
  1134. }
  1135. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1136. r10_bio->master_bio = bio;
  1137. r10_bio->sectors = sectors;
  1138. r10_bio->mddev = mddev;
  1139. r10_bio->sector = bio->bi_sector;
  1140. r10_bio->state = 0;
  1141. /* We might need to issue multiple reads to different
  1142. * devices if there are bad blocks around, so we keep
  1143. * track of the number of reads in bio->bi_phys_segments.
  1144. * If this is 0, there is only one r10_bio and no locking
  1145. * will be needed when the request completes. If it is
  1146. * non-zero, then it is the number of not-completed requests.
  1147. */
  1148. bio->bi_phys_segments = 0;
  1149. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1150. if (rw == READ) {
  1151. /*
  1152. * read balancing logic:
  1153. */
  1154. struct md_rdev *rdev;
  1155. int slot;
  1156. read_again:
  1157. rdev = read_balance(conf, r10_bio, &max_sectors);
  1158. if (!rdev) {
  1159. raid_end_bio_io(r10_bio);
  1160. return;
  1161. }
  1162. slot = r10_bio->read_slot;
  1163. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1164. md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
  1165. max_sectors);
  1166. r10_bio->devs[slot].bio = read_bio;
  1167. r10_bio->devs[slot].rdev = rdev;
  1168. read_bio->bi_sector = r10_bio->devs[slot].addr +
  1169. choose_data_offset(r10_bio, rdev);
  1170. read_bio->bi_bdev = rdev->bdev;
  1171. read_bio->bi_end_io = raid10_end_read_request;
  1172. read_bio->bi_rw = READ | do_sync;
  1173. read_bio->bi_private = r10_bio;
  1174. if (max_sectors < r10_bio->sectors) {
  1175. /* Could not read all from this device, so we will
  1176. * need another r10_bio.
  1177. */
  1178. sectors_handled = (r10_bio->sectors + max_sectors
  1179. - bio->bi_sector);
  1180. r10_bio->sectors = max_sectors;
  1181. spin_lock_irq(&conf->device_lock);
  1182. if (bio->bi_phys_segments == 0)
  1183. bio->bi_phys_segments = 2;
  1184. else
  1185. bio->bi_phys_segments++;
  1186. spin_unlock(&conf->device_lock);
  1187. /* Cannot call generic_make_request directly
  1188. * as that will be queued in __generic_make_request
  1189. * and subsequent mempool_alloc might block
  1190. * waiting for it. so hand bio over to raid10d.
  1191. */
  1192. reschedule_retry(r10_bio);
  1193. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1194. r10_bio->master_bio = bio;
  1195. r10_bio->sectors = ((bio->bi_size >> 9)
  1196. - sectors_handled);
  1197. r10_bio->state = 0;
  1198. r10_bio->mddev = mddev;
  1199. r10_bio->sector = bio->bi_sector + sectors_handled;
  1200. goto read_again;
  1201. } else
  1202. generic_make_request(read_bio);
  1203. return;
  1204. }
  1205. /*
  1206. * WRITE:
  1207. */
  1208. if (conf->pending_count >= max_queued_requests) {
  1209. md_wakeup_thread(mddev->thread);
  1210. wait_event(conf->wait_barrier,
  1211. conf->pending_count < max_queued_requests);
  1212. }
  1213. /* first select target devices under rcu_lock and
  1214. * inc refcount on their rdev. Record them by setting
  1215. * bios[x] to bio
  1216. * If there are known/acknowledged bad blocks on any device
  1217. * on which we have seen a write error, we want to avoid
  1218. * writing to those blocks. This potentially requires several
  1219. * writes to write around the bad blocks. Each set of writes
  1220. * gets its own r10_bio with a set of bios attached. The number
  1221. * of r10_bios is recored in bio->bi_phys_segments just as with
  1222. * the read case.
  1223. */
  1224. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  1225. raid10_find_phys(conf, r10_bio);
  1226. retry_write:
  1227. blocked_rdev = NULL;
  1228. rcu_read_lock();
  1229. max_sectors = r10_bio->sectors;
  1230. for (i = 0; i < conf->copies; i++) {
  1231. int d = r10_bio->devs[i].devnum;
  1232. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  1233. struct md_rdev *rrdev = rcu_dereference(
  1234. conf->mirrors[d].replacement);
  1235. if (rdev == rrdev)
  1236. rrdev = NULL;
  1237. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1238. atomic_inc(&rdev->nr_pending);
  1239. blocked_rdev = rdev;
  1240. break;
  1241. }
  1242. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  1243. atomic_inc(&rrdev->nr_pending);
  1244. blocked_rdev = rrdev;
  1245. break;
  1246. }
  1247. if (rdev && (test_bit(Faulty, &rdev->flags)
  1248. || test_bit(Unmerged, &rdev->flags)))
  1249. rdev = NULL;
  1250. if (rrdev && (test_bit(Faulty, &rrdev->flags)
  1251. || test_bit(Unmerged, &rrdev->flags)))
  1252. rrdev = NULL;
  1253. r10_bio->devs[i].bio = NULL;
  1254. r10_bio->devs[i].repl_bio = NULL;
  1255. if (!rdev && !rrdev) {
  1256. set_bit(R10BIO_Degraded, &r10_bio->state);
  1257. continue;
  1258. }
  1259. if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
  1260. sector_t first_bad;
  1261. sector_t dev_sector = r10_bio->devs[i].addr;
  1262. int bad_sectors;
  1263. int is_bad;
  1264. is_bad = is_badblock(rdev, dev_sector,
  1265. max_sectors,
  1266. &first_bad, &bad_sectors);
  1267. if (is_bad < 0) {
  1268. /* Mustn't write here until the bad block
  1269. * is acknowledged
  1270. */
  1271. atomic_inc(&rdev->nr_pending);
  1272. set_bit(BlockedBadBlocks, &rdev->flags);
  1273. blocked_rdev = rdev;
  1274. break;
  1275. }
  1276. if (is_bad && first_bad <= dev_sector) {
  1277. /* Cannot write here at all */
  1278. bad_sectors -= (dev_sector - first_bad);
  1279. if (bad_sectors < max_sectors)
  1280. /* Mustn't write more than bad_sectors
  1281. * to other devices yet
  1282. */
  1283. max_sectors = bad_sectors;
  1284. /* We don't set R10BIO_Degraded as that
  1285. * only applies if the disk is missing,
  1286. * so it might be re-added, and we want to
  1287. * know to recover this chunk.
  1288. * In this case the device is here, and the
  1289. * fact that this chunk is not in-sync is
  1290. * recorded in the bad block log.
  1291. */
  1292. continue;
  1293. }
  1294. if (is_bad) {
  1295. int good_sectors = first_bad - dev_sector;
  1296. if (good_sectors < max_sectors)
  1297. max_sectors = good_sectors;
  1298. }
  1299. }
  1300. if (rdev) {
  1301. r10_bio->devs[i].bio = bio;
  1302. atomic_inc(&rdev->nr_pending);
  1303. }
  1304. if (rrdev) {
  1305. r10_bio->devs[i].repl_bio = bio;
  1306. atomic_inc(&rrdev->nr_pending);
  1307. }
  1308. }
  1309. rcu_read_unlock();
  1310. if (unlikely(blocked_rdev)) {
  1311. /* Have to wait for this device to get unblocked, then retry */
  1312. int j;
  1313. int d;
  1314. for (j = 0; j < i; j++) {
  1315. if (r10_bio->devs[j].bio) {
  1316. d = r10_bio->devs[j].devnum;
  1317. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1318. }
  1319. if (r10_bio->devs[j].repl_bio) {
  1320. struct md_rdev *rdev;
  1321. d = r10_bio->devs[j].devnum;
  1322. rdev = conf->mirrors[d].replacement;
  1323. if (!rdev) {
  1324. /* Race with remove_disk */
  1325. smp_mb();
  1326. rdev = conf->mirrors[d].rdev;
  1327. }
  1328. rdev_dec_pending(rdev, mddev);
  1329. }
  1330. }
  1331. allow_barrier(conf);
  1332. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1333. wait_barrier(conf);
  1334. goto retry_write;
  1335. }
  1336. if (max_sectors < r10_bio->sectors) {
  1337. /* We are splitting this into multiple parts, so
  1338. * we need to prepare for allocating another r10_bio.
  1339. */
  1340. r10_bio->sectors = max_sectors;
  1341. spin_lock_irq(&conf->device_lock);
  1342. if (bio->bi_phys_segments == 0)
  1343. bio->bi_phys_segments = 2;
  1344. else
  1345. bio->bi_phys_segments++;
  1346. spin_unlock_irq(&conf->device_lock);
  1347. }
  1348. sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
  1349. atomic_set(&r10_bio->remaining, 1);
  1350. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1351. for (i = 0; i < conf->copies; i++) {
  1352. struct bio *mbio;
  1353. int d = r10_bio->devs[i].devnum;
  1354. if (r10_bio->devs[i].bio) {
  1355. struct md_rdev *rdev = conf->mirrors[d].rdev;
  1356. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1357. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1358. max_sectors);
  1359. r10_bio->devs[i].bio = mbio;
  1360. mbio->bi_sector = (r10_bio->devs[i].addr+
  1361. choose_data_offset(r10_bio,
  1362. rdev));
  1363. mbio->bi_bdev = rdev->bdev;
  1364. mbio->bi_end_io = raid10_end_write_request;
  1365. mbio->bi_rw =
  1366. WRITE | do_sync | do_fua | do_discard | do_same;
  1367. mbio->bi_private = r10_bio;
  1368. atomic_inc(&r10_bio->remaining);
  1369. cb = blk_check_plugged(raid10_unplug, mddev,
  1370. sizeof(*plug));
  1371. if (cb)
  1372. plug = container_of(cb, struct raid10_plug_cb,
  1373. cb);
  1374. else
  1375. plug = NULL;
  1376. spin_lock_irqsave(&conf->device_lock, flags);
  1377. if (plug) {
  1378. bio_list_add(&plug->pending, mbio);
  1379. plug->pending_cnt++;
  1380. } else {
  1381. bio_list_add(&conf->pending_bio_list, mbio);
  1382. conf->pending_count++;
  1383. }
  1384. spin_unlock_irqrestore(&conf->device_lock, flags);
  1385. if (!plug)
  1386. md_wakeup_thread(mddev->thread);
  1387. }
  1388. if (r10_bio->devs[i].repl_bio) {
  1389. struct md_rdev *rdev = conf->mirrors[d].replacement;
  1390. if (rdev == NULL) {
  1391. /* Replacement just got moved to main 'rdev' */
  1392. smp_mb();
  1393. rdev = conf->mirrors[d].rdev;
  1394. }
  1395. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1396. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1397. max_sectors);
  1398. r10_bio->devs[i].repl_bio = mbio;
  1399. mbio->bi_sector = (r10_bio->devs[i].addr +
  1400. choose_data_offset(
  1401. r10_bio, rdev));
  1402. mbio->bi_bdev = rdev->bdev;
  1403. mbio->bi_end_io = raid10_end_write_request;
  1404. mbio->bi_rw =
  1405. WRITE | do_sync | do_fua | do_discard | do_same;
  1406. mbio->bi_private = r10_bio;
  1407. atomic_inc(&r10_bio->remaining);
  1408. spin_lock_irqsave(&conf->device_lock, flags);
  1409. bio_list_add(&conf->pending_bio_list, mbio);
  1410. conf->pending_count++;
  1411. spin_unlock_irqrestore(&conf->device_lock, flags);
  1412. if (!mddev_check_plugged(mddev))
  1413. md_wakeup_thread(mddev->thread);
  1414. }
  1415. }
  1416. /* Don't remove the bias on 'remaining' (one_write_done) until
  1417. * after checking if we need to go around again.
  1418. */
  1419. if (sectors_handled < (bio->bi_size >> 9)) {
  1420. one_write_done(r10_bio);
  1421. /* We need another r10_bio. It has already been counted
  1422. * in bio->bi_phys_segments.
  1423. */
  1424. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1425. r10_bio->master_bio = bio;
  1426. r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  1427. r10_bio->mddev = mddev;
  1428. r10_bio->sector = bio->bi_sector + sectors_handled;
  1429. r10_bio->state = 0;
  1430. goto retry_write;
  1431. }
  1432. one_write_done(r10_bio);
  1433. /* In case raid10d snuck in to freeze_array */
  1434. wake_up(&conf->wait_barrier);
  1435. }
  1436. static void status(struct seq_file *seq, struct mddev *mddev)
  1437. {
  1438. struct r10conf *conf = mddev->private;
  1439. int i;
  1440. if (conf->geo.near_copies < conf->geo.raid_disks)
  1441. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1442. if (conf->geo.near_copies > 1)
  1443. seq_printf(seq, " %d near-copies", conf->geo.near_copies);
  1444. if (conf->geo.far_copies > 1) {
  1445. if (conf->geo.far_offset)
  1446. seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
  1447. else
  1448. seq_printf(seq, " %d far-copies", conf->geo.far_copies);
  1449. }
  1450. seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
  1451. conf->geo.raid_disks - mddev->degraded);
  1452. for (i = 0; i < conf->geo.raid_disks; i++)
  1453. seq_printf(seq, "%s",
  1454. conf->mirrors[i].rdev &&
  1455. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1456. seq_printf(seq, "]");
  1457. }
  1458. /* check if there are enough drives for
  1459. * every block to appear on atleast one.
  1460. * Don't consider the device numbered 'ignore'
  1461. * as we might be about to remove it.
  1462. */
  1463. static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
  1464. {
  1465. int first = 0;
  1466. do {
  1467. int n = conf->copies;
  1468. int cnt = 0;
  1469. int this = first;
  1470. while (n--) {
  1471. if (conf->mirrors[this].rdev &&
  1472. this != ignore)
  1473. cnt++;
  1474. this = (this+1) % geo->raid_disks;
  1475. }
  1476. if (cnt == 0)
  1477. return 0;
  1478. first = (first + geo->near_copies) % geo->raid_disks;
  1479. } while (first != 0);
  1480. return 1;
  1481. }
  1482. static int enough(struct r10conf *conf, int ignore)
  1483. {
  1484. return _enough(conf, &conf->geo, ignore) &&
  1485. _enough(conf, &conf->prev, ignore);
  1486. }
  1487. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1488. {
  1489. char b[BDEVNAME_SIZE];
  1490. struct r10conf *conf = mddev->private;
  1491. /*
  1492. * If it is not operational, then we have already marked it as dead
  1493. * else if it is the last working disks, ignore the error, let the
  1494. * next level up know.
  1495. * else mark the drive as failed
  1496. */
  1497. if (test_bit(In_sync, &rdev->flags)
  1498. && !enough(conf, rdev->raid_disk))
  1499. /*
  1500. * Don't fail the drive, just return an IO error.
  1501. */
  1502. return;
  1503. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1504. unsigned long flags;
  1505. spin_lock_irqsave(&conf->device_lock, flags);
  1506. mddev->degraded++;
  1507. spin_unlock_irqrestore(&conf->device_lock, flags);
  1508. /*
  1509. * if recovery is running, make sure it aborts.
  1510. */
  1511. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1512. }
  1513. set_bit(Blocked, &rdev->flags);
  1514. set_bit(Faulty, &rdev->flags);
  1515. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1516. printk(KERN_ALERT
  1517. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1518. "md/raid10:%s: Operation continuing on %d devices.\n",
  1519. mdname(mddev), bdevname(rdev->bdev, b),
  1520. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1521. }
  1522. static void print_conf(struct r10conf *conf)
  1523. {
  1524. int i;
  1525. struct raid10_info *tmp;
  1526. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1527. if (!conf) {
  1528. printk(KERN_DEBUG "(!conf)\n");
  1529. return;
  1530. }
  1531. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1532. conf->geo.raid_disks);
  1533. for (i = 0; i < conf->geo.raid_disks; i++) {
  1534. char b[BDEVNAME_SIZE];
  1535. tmp = conf->mirrors + i;
  1536. if (tmp->rdev)
  1537. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1538. i, !test_bit(In_sync, &tmp->rdev->flags),
  1539. !test_bit(Faulty, &tmp->rdev->flags),
  1540. bdevname(tmp->rdev->bdev,b));
  1541. }
  1542. }
  1543. static void close_sync(struct r10conf *conf)
  1544. {
  1545. wait_barrier(conf);
  1546. allow_barrier(conf);
  1547. mempool_destroy(conf->r10buf_pool);
  1548. conf->r10buf_pool = NULL;
  1549. }
  1550. static int raid10_spare_active(struct mddev *mddev)
  1551. {
  1552. int i;
  1553. struct r10conf *conf = mddev->private;
  1554. struct raid10_info *tmp;
  1555. int count = 0;
  1556. unsigned long flags;
  1557. /*
  1558. * Find all non-in_sync disks within the RAID10 configuration
  1559. * and mark them in_sync
  1560. */
  1561. for (i = 0; i < conf->geo.raid_disks; i++) {
  1562. tmp = conf->mirrors + i;
  1563. if (tmp->replacement
  1564. && tmp->replacement->recovery_offset == MaxSector
  1565. && !test_bit(Faulty, &tmp->replacement->flags)
  1566. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1567. /* Replacement has just become active */
  1568. if (!tmp->rdev
  1569. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1570. count++;
  1571. if (tmp->rdev) {
  1572. /* Replaced device not technically faulty,
  1573. * but we need to be sure it gets removed
  1574. * and never re-added.
  1575. */
  1576. set_bit(Faulty, &tmp->rdev->flags);
  1577. sysfs_notify_dirent_safe(
  1578. tmp->rdev->sysfs_state);
  1579. }
  1580. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1581. } else if (tmp->rdev
  1582. && !test_bit(Faulty, &tmp->rdev->flags)
  1583. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1584. count++;
  1585. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  1586. }
  1587. }
  1588. spin_lock_irqsave(&conf->device_lock, flags);
  1589. mddev->degraded -= count;
  1590. spin_unlock_irqrestore(&conf->device_lock, flags);
  1591. print_conf(conf);
  1592. return count;
  1593. }
  1594. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1595. {
  1596. struct r10conf *conf = mddev->private;
  1597. int err = -EEXIST;
  1598. int mirror;
  1599. int first = 0;
  1600. int last = conf->geo.raid_disks - 1;
  1601. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1602. if (mddev->recovery_cp < MaxSector)
  1603. /* only hot-add to in-sync arrays, as recovery is
  1604. * very different from resync
  1605. */
  1606. return -EBUSY;
  1607. if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
  1608. return -EINVAL;
  1609. if (rdev->raid_disk >= 0)
  1610. first = last = rdev->raid_disk;
  1611. if (q->merge_bvec_fn) {
  1612. set_bit(Unmerged, &rdev->flags);
  1613. mddev->merge_check_needed = 1;
  1614. }
  1615. if (rdev->saved_raid_disk >= first &&
  1616. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1617. mirror = rdev->saved_raid_disk;
  1618. else
  1619. mirror = first;
  1620. for ( ; mirror <= last ; mirror++) {
  1621. struct raid10_info *p = &conf->mirrors[mirror];
  1622. if (p->recovery_disabled == mddev->recovery_disabled)
  1623. continue;
  1624. if (p->rdev) {
  1625. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1626. p->replacement != NULL)
  1627. continue;
  1628. clear_bit(In_sync, &rdev->flags);
  1629. set_bit(Replacement, &rdev->flags);
  1630. rdev->raid_disk = mirror;
  1631. err = 0;
  1632. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1633. rdev->data_offset << 9);
  1634. conf->fullsync = 1;
  1635. rcu_assign_pointer(p->replacement, rdev);
  1636. break;
  1637. }
  1638. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1639. rdev->data_offset << 9);
  1640. p->head_position = 0;
  1641. p->recovery_disabled = mddev->recovery_disabled - 1;
  1642. rdev->raid_disk = mirror;
  1643. err = 0;
  1644. if (rdev->saved_raid_disk != mirror)
  1645. conf->fullsync = 1;
  1646. rcu_assign_pointer(p->rdev, rdev);
  1647. break;
  1648. }
  1649. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1650. /* Some requests might not have seen this new
  1651. * merge_bvec_fn. We must wait for them to complete
  1652. * before merging the device fully.
  1653. * First we make sure any code which has tested
  1654. * our function has submitted the request, then
  1655. * we wait for all outstanding requests to complete.
  1656. */
  1657. synchronize_sched();
  1658. raise_barrier(conf, 0);
  1659. lower_barrier(conf);
  1660. clear_bit(Unmerged, &rdev->flags);
  1661. }
  1662. md_integrity_add_rdev(rdev, mddev);
  1663. if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1664. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  1665. print_conf(conf);
  1666. return err;
  1667. }
  1668. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1669. {
  1670. struct r10conf *conf = mddev->private;
  1671. int err = 0;
  1672. int number = rdev->raid_disk;
  1673. struct md_rdev **rdevp;
  1674. struct raid10_info *p = conf->mirrors + number;
  1675. print_conf(conf);
  1676. if (rdev == p->rdev)
  1677. rdevp = &p->rdev;
  1678. else if (rdev == p->replacement)
  1679. rdevp = &p->replacement;
  1680. else
  1681. return 0;
  1682. if (test_bit(In_sync, &rdev->flags) ||
  1683. atomic_read(&rdev->nr_pending)) {
  1684. err = -EBUSY;
  1685. goto abort;
  1686. }
  1687. /* Only remove faulty devices if recovery
  1688. * is not possible.
  1689. */
  1690. if (!test_bit(Faulty, &rdev->flags) &&
  1691. mddev->recovery_disabled != p->recovery_disabled &&
  1692. (!p->replacement || p->replacement == rdev) &&
  1693. number < conf->geo.raid_disks &&
  1694. enough(conf, -1)) {
  1695. err = -EBUSY;
  1696. goto abort;
  1697. }
  1698. *rdevp = NULL;
  1699. synchronize_rcu();
  1700. if (atomic_read(&rdev->nr_pending)) {
  1701. /* lost the race, try later */
  1702. err = -EBUSY;
  1703. *rdevp = rdev;
  1704. goto abort;
  1705. } else if (p->replacement) {
  1706. /* We must have just cleared 'rdev' */
  1707. p->rdev = p->replacement;
  1708. clear_bit(Replacement, &p->replacement->flags);
  1709. smp_mb(); /* Make sure other CPUs may see both as identical
  1710. * but will never see neither -- if they are careful.
  1711. */
  1712. p->replacement = NULL;
  1713. clear_bit(WantReplacement, &rdev->flags);
  1714. } else
  1715. /* We might have just remove the Replacement as faulty
  1716. * Clear the flag just in case
  1717. */
  1718. clear_bit(WantReplacement, &rdev->flags);
  1719. err = md_integrity_register(mddev);
  1720. abort:
  1721. print_conf(conf);
  1722. return err;
  1723. }
  1724. static void end_sync_read(struct bio *bio, int error)
  1725. {
  1726. struct r10bio *r10_bio = bio->bi_private;
  1727. struct r10conf *conf = r10_bio->mddev->private;
  1728. int d;
  1729. if (bio == r10_bio->master_bio) {
  1730. /* this is a reshape read */
  1731. d = r10_bio->read_slot; /* really the read dev */
  1732. } else
  1733. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1734. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1735. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1736. else
  1737. /* The write handler will notice the lack of
  1738. * R10BIO_Uptodate and record any errors etc
  1739. */
  1740. atomic_add(r10_bio->sectors,
  1741. &conf->mirrors[d].rdev->corrected_errors);
  1742. /* for reconstruct, we always reschedule after a read.
  1743. * for resync, only after all reads
  1744. */
  1745. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1746. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1747. atomic_dec_and_test(&r10_bio->remaining)) {
  1748. /* we have read all the blocks,
  1749. * do the comparison in process context in raid10d
  1750. */
  1751. reschedule_retry(r10_bio);
  1752. }
  1753. }
  1754. static void end_sync_request(struct r10bio *r10_bio)
  1755. {
  1756. struct mddev *mddev = r10_bio->mddev;
  1757. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1758. if (r10_bio->master_bio == NULL) {
  1759. /* the primary of several recovery bios */
  1760. sector_t s = r10_bio->sectors;
  1761. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1762. test_bit(R10BIO_WriteError, &r10_bio->state))
  1763. reschedule_retry(r10_bio);
  1764. else
  1765. put_buf(r10_bio);
  1766. md_done_sync(mddev, s, 1);
  1767. break;
  1768. } else {
  1769. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1770. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1771. test_bit(R10BIO_WriteError, &r10_bio->state))
  1772. reschedule_retry(r10_bio);
  1773. else
  1774. put_buf(r10_bio);
  1775. r10_bio = r10_bio2;
  1776. }
  1777. }
  1778. }
  1779. static void end_sync_write(struct bio *bio, int error)
  1780. {
  1781. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1782. struct r10bio *r10_bio = bio->bi_private;
  1783. struct mddev *mddev = r10_bio->mddev;
  1784. struct r10conf *conf = mddev->private;
  1785. int d;
  1786. sector_t first_bad;
  1787. int bad_sectors;
  1788. int slot;
  1789. int repl;
  1790. struct md_rdev *rdev = NULL;
  1791. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1792. if (repl)
  1793. rdev = conf->mirrors[d].replacement;
  1794. else
  1795. rdev = conf->mirrors[d].rdev;
  1796. if (!uptodate) {
  1797. if (repl)
  1798. md_error(mddev, rdev);
  1799. else {
  1800. set_bit(WriteErrorSeen, &rdev->flags);
  1801. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1802. set_bit(MD_RECOVERY_NEEDED,
  1803. &rdev->mddev->recovery);
  1804. set_bit(R10BIO_WriteError, &r10_bio->state);
  1805. }
  1806. } else if (is_badblock(rdev,
  1807. r10_bio->devs[slot].addr,
  1808. r10_bio->sectors,
  1809. &first_bad, &bad_sectors))
  1810. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1811. rdev_dec_pending(rdev, mddev);
  1812. end_sync_request(r10_bio);
  1813. }
  1814. /*
  1815. * Note: sync and recover and handled very differently for raid10
  1816. * This code is for resync.
  1817. * For resync, we read through virtual addresses and read all blocks.
  1818. * If there is any error, we schedule a write. The lowest numbered
  1819. * drive is authoritative.
  1820. * However requests come for physical address, so we need to map.
  1821. * For every physical address there are raid_disks/copies virtual addresses,
  1822. * which is always are least one, but is not necessarly an integer.
  1823. * This means that a physical address can span multiple chunks, so we may
  1824. * have to submit multiple io requests for a single sync request.
  1825. */
  1826. /*
  1827. * We check if all blocks are in-sync and only write to blocks that
  1828. * aren't in sync
  1829. */
  1830. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1831. {
  1832. struct r10conf *conf = mddev->private;
  1833. int i, first;
  1834. struct bio *tbio, *fbio;
  1835. int vcnt;
  1836. atomic_set(&r10_bio->remaining, 1);
  1837. /* find the first device with a block */
  1838. for (i=0; i<conf->copies; i++)
  1839. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1840. break;
  1841. if (i == conf->copies)
  1842. goto done;
  1843. first = i;
  1844. fbio = r10_bio->devs[i].bio;
  1845. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1846. /* now find blocks with errors */
  1847. for (i=0 ; i < conf->copies ; i++) {
  1848. int j, d;
  1849. tbio = r10_bio->devs[i].bio;
  1850. if (tbio->bi_end_io != end_sync_read)
  1851. continue;
  1852. if (i == first)
  1853. continue;
  1854. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1855. /* We know that the bi_io_vec layout is the same for
  1856. * both 'first' and 'i', so we just compare them.
  1857. * All vec entries are PAGE_SIZE;
  1858. */
  1859. for (j = 0; j < vcnt; j++)
  1860. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1861. page_address(tbio->bi_io_vec[j].bv_page),
  1862. fbio->bi_io_vec[j].bv_len))
  1863. break;
  1864. if (j == vcnt)
  1865. continue;
  1866. atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
  1867. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1868. /* Don't fix anything. */
  1869. continue;
  1870. }
  1871. /* Ok, we need to write this bio, either to correct an
  1872. * inconsistency or to correct an unreadable block.
  1873. * First we need to fixup bv_offset, bv_len and
  1874. * bi_vecs, as the read request might have corrupted these
  1875. */
  1876. tbio->bi_vcnt = vcnt;
  1877. tbio->bi_size = r10_bio->sectors << 9;
  1878. tbio->bi_idx = 0;
  1879. tbio->bi_phys_segments = 0;
  1880. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1881. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1882. tbio->bi_next = NULL;
  1883. tbio->bi_rw = WRITE;
  1884. tbio->bi_private = r10_bio;
  1885. tbio->bi_sector = r10_bio->devs[i].addr;
  1886. for (j=0; j < vcnt ; j++) {
  1887. tbio->bi_io_vec[j].bv_offset = 0;
  1888. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1889. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1890. page_address(fbio->bi_io_vec[j].bv_page),
  1891. PAGE_SIZE);
  1892. }
  1893. tbio->bi_end_io = end_sync_write;
  1894. d = r10_bio->devs[i].devnum;
  1895. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1896. atomic_inc(&r10_bio->remaining);
  1897. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1898. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1899. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1900. generic_make_request(tbio);
  1901. }
  1902. /* Now write out to any replacement devices
  1903. * that are active
  1904. */
  1905. for (i = 0; i < conf->copies; i++) {
  1906. int j, d;
  1907. tbio = r10_bio->devs[i].repl_bio;
  1908. if (!tbio || !tbio->bi_end_io)
  1909. continue;
  1910. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1911. && r10_bio->devs[i].bio != fbio)
  1912. for (j = 0; j < vcnt; j++)
  1913. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1914. page_address(fbio->bi_io_vec[j].bv_page),
  1915. PAGE_SIZE);
  1916. d = r10_bio->devs[i].devnum;
  1917. atomic_inc(&r10_bio->remaining);
  1918. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1919. tbio->bi_size >> 9);
  1920. generic_make_request(tbio);
  1921. }
  1922. done:
  1923. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1924. md_done_sync(mddev, r10_bio->sectors, 1);
  1925. put_buf(r10_bio);
  1926. }
  1927. }
  1928. /*
  1929. * Now for the recovery code.
  1930. * Recovery happens across physical sectors.
  1931. * We recover all non-is_sync drives by finding the virtual address of
  1932. * each, and then choose a working drive that also has that virt address.
  1933. * There is a separate r10_bio for each non-in_sync drive.
  1934. * Only the first two slots are in use. The first for reading,
  1935. * The second for writing.
  1936. *
  1937. */
  1938. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1939. {
  1940. /* We got a read error during recovery.
  1941. * We repeat the read in smaller page-sized sections.
  1942. * If a read succeeds, write it to the new device or record
  1943. * a bad block if we cannot.
  1944. * If a read fails, record a bad block on both old and
  1945. * new devices.
  1946. */
  1947. struct mddev *mddev = r10_bio->mddev;
  1948. struct r10conf *conf = mddev->private;
  1949. struct bio *bio = r10_bio->devs[0].bio;
  1950. sector_t sect = 0;
  1951. int sectors = r10_bio->sectors;
  1952. int idx = 0;
  1953. int dr = r10_bio->devs[0].devnum;
  1954. int dw = r10_bio->devs[1].devnum;
  1955. while (sectors) {
  1956. int s = sectors;
  1957. struct md_rdev *rdev;
  1958. sector_t addr;
  1959. int ok;
  1960. if (s > (PAGE_SIZE>>9))
  1961. s = PAGE_SIZE >> 9;
  1962. rdev = conf->mirrors[dr].rdev;
  1963. addr = r10_bio->devs[0].addr + sect,
  1964. ok = sync_page_io(rdev,
  1965. addr,
  1966. s << 9,
  1967. bio->bi_io_vec[idx].bv_page,
  1968. READ, false);
  1969. if (ok) {
  1970. rdev = conf->mirrors[dw].rdev;
  1971. addr = r10_bio->devs[1].addr + sect;
  1972. ok = sync_page_io(rdev,
  1973. addr,
  1974. s << 9,
  1975. bio->bi_io_vec[idx].bv_page,
  1976. WRITE, false);
  1977. if (!ok) {
  1978. set_bit(WriteErrorSeen, &rdev->flags);
  1979. if (!test_and_set_bit(WantReplacement,
  1980. &rdev->flags))
  1981. set_bit(MD_RECOVERY_NEEDED,
  1982. &rdev->mddev->recovery);
  1983. }
  1984. }
  1985. if (!ok) {
  1986. /* We don't worry if we cannot set a bad block -
  1987. * it really is bad so there is no loss in not
  1988. * recording it yet
  1989. */
  1990. rdev_set_badblocks(rdev, addr, s, 0);
  1991. if (rdev != conf->mirrors[dw].rdev) {
  1992. /* need bad block on destination too */
  1993. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  1994. addr = r10_bio->devs[1].addr + sect;
  1995. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  1996. if (!ok) {
  1997. /* just abort the recovery */
  1998. printk(KERN_NOTICE
  1999. "md/raid10:%s: recovery aborted"
  2000. " due to read error\n",
  2001. mdname(mddev));
  2002. conf->mirrors[dw].recovery_disabled
  2003. = mddev->recovery_disabled;
  2004. set_bit(MD_RECOVERY_INTR,
  2005. &mddev->recovery);
  2006. break;
  2007. }
  2008. }
  2009. }
  2010. sectors -= s;
  2011. sect += s;
  2012. idx++;
  2013. }
  2014. }
  2015. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  2016. {
  2017. struct r10conf *conf = mddev->private;
  2018. int d;
  2019. struct bio *wbio, *wbio2;
  2020. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  2021. fix_recovery_read_error(r10_bio);
  2022. end_sync_request(r10_bio);
  2023. return;
  2024. }
  2025. /*
  2026. * share the pages with the first bio
  2027. * and submit the write request
  2028. */
  2029. d = r10_bio->devs[1].devnum;
  2030. wbio = r10_bio->devs[1].bio;
  2031. wbio2 = r10_bio->devs[1].repl_bio;
  2032. if (wbio->bi_end_io) {
  2033. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2034. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  2035. generic_make_request(wbio);
  2036. }
  2037. if (wbio2 && wbio2->bi_end_io) {
  2038. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  2039. md_sync_acct(conf->mirrors[d].replacement->bdev,
  2040. wbio2->bi_size >> 9);
  2041. generic_make_request(wbio2);
  2042. }
  2043. }
  2044. /*
  2045. * Used by fix_read_error() to decay the per rdev read_errors.
  2046. * We halve the read error count for every hour that has elapsed
  2047. * since the last recorded read error.
  2048. *
  2049. */
  2050. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  2051. {
  2052. struct timespec cur_time_mon;
  2053. unsigned long hours_since_last;
  2054. unsigned int read_errors = atomic_read(&rdev->read_errors);
  2055. ktime_get_ts(&cur_time_mon);
  2056. if (rdev->last_read_error.tv_sec == 0 &&
  2057. rdev->last_read_error.tv_nsec == 0) {
  2058. /* first time we've seen a read error */
  2059. rdev->last_read_error = cur_time_mon;
  2060. return;
  2061. }
  2062. hours_since_last = (cur_time_mon.tv_sec -
  2063. rdev->last_read_error.tv_sec) / 3600;
  2064. rdev->last_read_error = cur_time_mon;
  2065. /*
  2066. * if hours_since_last is > the number of bits in read_errors
  2067. * just set read errors to 0. We do this to avoid
  2068. * overflowing the shift of read_errors by hours_since_last.
  2069. */
  2070. if (hours_since_last >= 8 * sizeof(read_errors))
  2071. atomic_set(&rdev->read_errors, 0);
  2072. else
  2073. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  2074. }
  2075. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  2076. int sectors, struct page *page, int rw)
  2077. {
  2078. sector_t first_bad;
  2079. int bad_sectors;
  2080. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  2081. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  2082. return -1;
  2083. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  2084. /* success */
  2085. return 1;
  2086. if (rw == WRITE) {
  2087. set_bit(WriteErrorSeen, &rdev->flags);
  2088. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  2089. set_bit(MD_RECOVERY_NEEDED,
  2090. &rdev->mddev->recovery);
  2091. }
  2092. /* need to record an error - either for the block or the device */
  2093. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  2094. md_error(rdev->mddev, rdev);
  2095. return 0;
  2096. }
  2097. /*
  2098. * This is a kernel thread which:
  2099. *
  2100. * 1. Retries failed read operations on working mirrors.
  2101. * 2. Updates the raid superblock when problems encounter.
  2102. * 3. Performs writes following reads for array synchronising.
  2103. */
  2104. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  2105. {
  2106. int sect = 0; /* Offset from r10_bio->sector */
  2107. int sectors = r10_bio->sectors;
  2108. struct md_rdev*rdev;
  2109. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  2110. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  2111. /* still own a reference to this rdev, so it cannot
  2112. * have been cleared recently.
  2113. */
  2114. rdev = conf->mirrors[d].rdev;
  2115. if (test_bit(Faulty, &rdev->flags))
  2116. /* drive has already been failed, just ignore any
  2117. more fix_read_error() attempts */
  2118. return;
  2119. check_decay_read_errors(mddev, rdev);
  2120. atomic_inc(&rdev->read_errors);
  2121. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  2122. char b[BDEVNAME_SIZE];
  2123. bdevname(rdev->bdev, b);
  2124. printk(KERN_NOTICE
  2125. "md/raid10:%s: %s: Raid device exceeded "
  2126. "read_error threshold [cur %d:max %d]\n",
  2127. mdname(mddev), b,
  2128. atomic_read(&rdev->read_errors), max_read_errors);
  2129. printk(KERN_NOTICE
  2130. "md/raid10:%s: %s: Failing raid device\n",
  2131. mdname(mddev), b);
  2132. md_error(mddev, conf->mirrors[d].rdev);
  2133. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  2134. return;
  2135. }
  2136. while(sectors) {
  2137. int s = sectors;
  2138. int sl = r10_bio->read_slot;
  2139. int success = 0;
  2140. int start;
  2141. if (s > (PAGE_SIZE>>9))
  2142. s = PAGE_SIZE >> 9;
  2143. rcu_read_lock();
  2144. do {
  2145. sector_t first_bad;
  2146. int bad_sectors;
  2147. d = r10_bio->devs[sl].devnum;
  2148. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2149. if (rdev &&
  2150. !test_bit(Unmerged, &rdev->flags) &&
  2151. test_bit(In_sync, &rdev->flags) &&
  2152. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  2153. &first_bad, &bad_sectors) == 0) {
  2154. atomic_inc(&rdev->nr_pending);
  2155. rcu_read_unlock();
  2156. success = sync_page_io(rdev,
  2157. r10_bio->devs[sl].addr +
  2158. sect,
  2159. s<<9,
  2160. conf->tmppage, READ, false);
  2161. rdev_dec_pending(rdev, mddev);
  2162. rcu_read_lock();
  2163. if (success)
  2164. break;
  2165. }
  2166. sl++;
  2167. if (sl == conf->copies)
  2168. sl = 0;
  2169. } while (!success && sl != r10_bio->read_slot);
  2170. rcu_read_unlock();
  2171. if (!success) {
  2172. /* Cannot read from anywhere, just mark the block
  2173. * as bad on the first device to discourage future
  2174. * reads.
  2175. */
  2176. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  2177. rdev = conf->mirrors[dn].rdev;
  2178. if (!rdev_set_badblocks(
  2179. rdev,
  2180. r10_bio->devs[r10_bio->read_slot].addr
  2181. + sect,
  2182. s, 0)) {
  2183. md_error(mddev, rdev);
  2184. r10_bio->devs[r10_bio->read_slot].bio
  2185. = IO_BLOCKED;
  2186. }
  2187. break;
  2188. }
  2189. start = sl;
  2190. /* write it back and re-read */
  2191. rcu_read_lock();
  2192. while (sl != r10_bio->read_slot) {
  2193. char b[BDEVNAME_SIZE];
  2194. if (sl==0)
  2195. sl = conf->copies;
  2196. sl--;
  2197. d = r10_bio->devs[sl].devnum;
  2198. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2199. if (!rdev ||
  2200. test_bit(Unmerged, &rdev->flags) ||
  2201. !test_bit(In_sync, &rdev->flags))
  2202. continue;
  2203. atomic_inc(&rdev->nr_pending);
  2204. rcu_read_unlock();
  2205. if (r10_sync_page_io(rdev,
  2206. r10_bio->devs[sl].addr +
  2207. sect,
  2208. s, conf->tmppage, WRITE)
  2209. == 0) {
  2210. /* Well, this device is dead */
  2211. printk(KERN_NOTICE
  2212. "md/raid10:%s: read correction "
  2213. "write failed"
  2214. " (%d sectors at %llu on %s)\n",
  2215. mdname(mddev), s,
  2216. (unsigned long long)(
  2217. sect +
  2218. choose_data_offset(r10_bio,
  2219. rdev)),
  2220. bdevname(rdev->bdev, b));
  2221. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2222. "drive\n",
  2223. mdname(mddev),
  2224. bdevname(rdev->bdev, b));
  2225. }
  2226. rdev_dec_pending(rdev, mddev);
  2227. rcu_read_lock();
  2228. }
  2229. sl = start;
  2230. while (sl != r10_bio->read_slot) {
  2231. char b[BDEVNAME_SIZE];
  2232. if (sl==0)
  2233. sl = conf->copies;
  2234. sl--;
  2235. d = r10_bio->devs[sl].devnum;
  2236. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2237. if (!rdev ||
  2238. !test_bit(In_sync, &rdev->flags))
  2239. continue;
  2240. atomic_inc(&rdev->nr_pending);
  2241. rcu_read_unlock();
  2242. switch (r10_sync_page_io(rdev,
  2243. r10_bio->devs[sl].addr +
  2244. sect,
  2245. s, conf->tmppage,
  2246. READ)) {
  2247. case 0:
  2248. /* Well, this device is dead */
  2249. printk(KERN_NOTICE
  2250. "md/raid10:%s: unable to read back "
  2251. "corrected sectors"
  2252. " (%d sectors at %llu on %s)\n",
  2253. mdname(mddev), s,
  2254. (unsigned long long)(
  2255. sect +
  2256. choose_data_offset(r10_bio, rdev)),
  2257. bdevname(rdev->bdev, b));
  2258. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2259. "drive\n",
  2260. mdname(mddev),
  2261. bdevname(rdev->bdev, b));
  2262. break;
  2263. case 1:
  2264. printk(KERN_INFO
  2265. "md/raid10:%s: read error corrected"
  2266. " (%d sectors at %llu on %s)\n",
  2267. mdname(mddev), s,
  2268. (unsigned long long)(
  2269. sect +
  2270. choose_data_offset(r10_bio, rdev)),
  2271. bdevname(rdev->bdev, b));
  2272. atomic_add(s, &rdev->corrected_errors);
  2273. }
  2274. rdev_dec_pending(rdev, mddev);
  2275. rcu_read_lock();
  2276. }
  2277. rcu_read_unlock();
  2278. sectors -= s;
  2279. sect += s;
  2280. }
  2281. }
  2282. static void bi_complete(struct bio *bio, int error)
  2283. {
  2284. complete((struct completion *)bio->bi_private);
  2285. }
  2286. static int submit_bio_wait(int rw, struct bio *bio)
  2287. {
  2288. struct completion event;
  2289. rw |= REQ_SYNC;
  2290. init_completion(&event);
  2291. bio->bi_private = &event;
  2292. bio->bi_end_io = bi_complete;
  2293. submit_bio(rw, bio);
  2294. wait_for_completion(&event);
  2295. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  2296. }
  2297. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2298. {
  2299. struct bio *bio = r10_bio->master_bio;
  2300. struct mddev *mddev = r10_bio->mddev;
  2301. struct r10conf *conf = mddev->private;
  2302. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2303. /* bio has the data to be written to slot 'i' where
  2304. * we just recently had a write error.
  2305. * We repeatedly clone the bio and trim down to one block,
  2306. * then try the write. Where the write fails we record
  2307. * a bad block.
  2308. * It is conceivable that the bio doesn't exactly align with
  2309. * blocks. We must handle this.
  2310. *
  2311. * We currently own a reference to the rdev.
  2312. */
  2313. int block_sectors;
  2314. sector_t sector;
  2315. int sectors;
  2316. int sect_to_write = r10_bio->sectors;
  2317. int ok = 1;
  2318. if (rdev->badblocks.shift < 0)
  2319. return 0;
  2320. block_sectors = 1 << rdev->badblocks.shift;
  2321. sector = r10_bio->sector;
  2322. sectors = ((r10_bio->sector + block_sectors)
  2323. & ~(sector_t)(block_sectors - 1))
  2324. - sector;
  2325. while (sect_to_write) {
  2326. struct bio *wbio;
  2327. if (sectors > sect_to_write)
  2328. sectors = sect_to_write;
  2329. /* Write at 'sector' for 'sectors' */
  2330. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2331. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2332. wbio->bi_sector = (r10_bio->devs[i].addr+
  2333. choose_data_offset(r10_bio, rdev) +
  2334. (sector - r10_bio->sector));
  2335. wbio->bi_bdev = rdev->bdev;
  2336. if (submit_bio_wait(WRITE, wbio) == 0)
  2337. /* Failure! */
  2338. ok = rdev_set_badblocks(rdev, sector,
  2339. sectors, 0)
  2340. && ok;
  2341. bio_put(wbio);
  2342. sect_to_write -= sectors;
  2343. sector += sectors;
  2344. sectors = block_sectors;
  2345. }
  2346. return ok;
  2347. }
  2348. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2349. {
  2350. int slot = r10_bio->read_slot;
  2351. struct bio *bio;
  2352. struct r10conf *conf = mddev->private;
  2353. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2354. char b[BDEVNAME_SIZE];
  2355. unsigned long do_sync;
  2356. int max_sectors;
  2357. /* we got a read error. Maybe the drive is bad. Maybe just
  2358. * the block and we can fix it.
  2359. * We freeze all other IO, and try reading the block from
  2360. * other devices. When we find one, we re-write
  2361. * and check it that fixes the read error.
  2362. * This is all done synchronously while the array is
  2363. * frozen.
  2364. */
  2365. bio = r10_bio->devs[slot].bio;
  2366. bdevname(bio->bi_bdev, b);
  2367. bio_put(bio);
  2368. r10_bio->devs[slot].bio = NULL;
  2369. if (mddev->ro == 0) {
  2370. freeze_array(conf);
  2371. fix_read_error(conf, mddev, r10_bio);
  2372. unfreeze_array(conf);
  2373. } else
  2374. r10_bio->devs[slot].bio = IO_BLOCKED;
  2375. rdev_dec_pending(rdev, mddev);
  2376. read_more:
  2377. rdev = read_balance(conf, r10_bio, &max_sectors);
  2378. if (rdev == NULL) {
  2379. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2380. " read error for block %llu\n",
  2381. mdname(mddev), b,
  2382. (unsigned long long)r10_bio->sector);
  2383. raid_end_bio_io(r10_bio);
  2384. return;
  2385. }
  2386. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2387. slot = r10_bio->read_slot;
  2388. printk_ratelimited(
  2389. KERN_ERR
  2390. "md/raid10:%s: %s: redirecting "
  2391. "sector %llu to another mirror\n",
  2392. mdname(mddev),
  2393. bdevname(rdev->bdev, b),
  2394. (unsigned long long)r10_bio->sector);
  2395. bio = bio_clone_mddev(r10_bio->master_bio,
  2396. GFP_NOIO, mddev);
  2397. md_trim_bio(bio,
  2398. r10_bio->sector - bio->bi_sector,
  2399. max_sectors);
  2400. r10_bio->devs[slot].bio = bio;
  2401. r10_bio->devs[slot].rdev = rdev;
  2402. bio->bi_sector = r10_bio->devs[slot].addr
  2403. + choose_data_offset(r10_bio, rdev);
  2404. bio->bi_bdev = rdev->bdev;
  2405. bio->bi_rw = READ | do_sync;
  2406. bio->bi_private = r10_bio;
  2407. bio->bi_end_io = raid10_end_read_request;
  2408. if (max_sectors < r10_bio->sectors) {
  2409. /* Drat - have to split this up more */
  2410. struct bio *mbio = r10_bio->master_bio;
  2411. int sectors_handled =
  2412. r10_bio->sector + max_sectors
  2413. - mbio->bi_sector;
  2414. r10_bio->sectors = max_sectors;
  2415. spin_lock_irq(&conf->device_lock);
  2416. if (mbio->bi_phys_segments == 0)
  2417. mbio->bi_phys_segments = 2;
  2418. else
  2419. mbio->bi_phys_segments++;
  2420. spin_unlock_irq(&conf->device_lock);
  2421. generic_make_request(bio);
  2422. r10_bio = mempool_alloc(conf->r10bio_pool,
  2423. GFP_NOIO);
  2424. r10_bio->master_bio = mbio;
  2425. r10_bio->sectors = (mbio->bi_size >> 9)
  2426. - sectors_handled;
  2427. r10_bio->state = 0;
  2428. set_bit(R10BIO_ReadError,
  2429. &r10_bio->state);
  2430. r10_bio->mddev = mddev;
  2431. r10_bio->sector = mbio->bi_sector
  2432. + sectors_handled;
  2433. goto read_more;
  2434. } else
  2435. generic_make_request(bio);
  2436. }
  2437. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2438. {
  2439. /* Some sort of write request has finished and it
  2440. * succeeded in writing where we thought there was a
  2441. * bad block. So forget the bad block.
  2442. * Or possibly if failed and we need to record
  2443. * a bad block.
  2444. */
  2445. int m;
  2446. struct md_rdev *rdev;
  2447. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2448. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2449. for (m = 0; m < conf->copies; m++) {
  2450. int dev = r10_bio->devs[m].devnum;
  2451. rdev = conf->mirrors[dev].rdev;
  2452. if (r10_bio->devs[m].bio == NULL)
  2453. continue;
  2454. if (test_bit(BIO_UPTODATE,
  2455. &r10_bio->devs[m].bio->bi_flags)) {
  2456. rdev_clear_badblocks(
  2457. rdev,
  2458. r10_bio->devs[m].addr,
  2459. r10_bio->sectors, 0);
  2460. } else {
  2461. if (!rdev_set_badblocks(
  2462. rdev,
  2463. r10_bio->devs[m].addr,
  2464. r10_bio->sectors, 0))
  2465. md_error(conf->mddev, rdev);
  2466. }
  2467. rdev = conf->mirrors[dev].replacement;
  2468. if (r10_bio->devs[m].repl_bio == NULL)
  2469. continue;
  2470. if (test_bit(BIO_UPTODATE,
  2471. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2472. rdev_clear_badblocks(
  2473. rdev,
  2474. r10_bio->devs[m].addr,
  2475. r10_bio->sectors, 0);
  2476. } else {
  2477. if (!rdev_set_badblocks(
  2478. rdev,
  2479. r10_bio->devs[m].addr,
  2480. r10_bio->sectors, 0))
  2481. md_error(conf->mddev, rdev);
  2482. }
  2483. }
  2484. put_buf(r10_bio);
  2485. } else {
  2486. for (m = 0; m < conf->copies; m++) {
  2487. int dev = r10_bio->devs[m].devnum;
  2488. struct bio *bio = r10_bio->devs[m].bio;
  2489. rdev = conf->mirrors[dev].rdev;
  2490. if (bio == IO_MADE_GOOD) {
  2491. rdev_clear_badblocks(
  2492. rdev,
  2493. r10_bio->devs[m].addr,
  2494. r10_bio->sectors, 0);
  2495. rdev_dec_pending(rdev, conf->mddev);
  2496. } else if (bio != NULL &&
  2497. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2498. if (!narrow_write_error(r10_bio, m)) {
  2499. md_error(conf->mddev, rdev);
  2500. set_bit(R10BIO_Degraded,
  2501. &r10_bio->state);
  2502. }
  2503. rdev_dec_pending(rdev, conf->mddev);
  2504. }
  2505. bio = r10_bio->devs[m].repl_bio;
  2506. rdev = conf->mirrors[dev].replacement;
  2507. if (rdev && bio == IO_MADE_GOOD) {
  2508. rdev_clear_badblocks(
  2509. rdev,
  2510. r10_bio->devs[m].addr,
  2511. r10_bio->sectors, 0);
  2512. rdev_dec_pending(rdev, conf->mddev);
  2513. }
  2514. }
  2515. if (test_bit(R10BIO_WriteError,
  2516. &r10_bio->state))
  2517. close_write(r10_bio);
  2518. raid_end_bio_io(r10_bio);
  2519. }
  2520. }
  2521. static void raid10d(struct md_thread *thread)
  2522. {
  2523. struct mddev *mddev = thread->mddev;
  2524. struct r10bio *r10_bio;
  2525. unsigned long flags;
  2526. struct r10conf *conf = mddev->private;
  2527. struct list_head *head = &conf->retry_list;
  2528. struct blk_plug plug;
  2529. md_check_recovery(mddev);
  2530. blk_start_plug(&plug);
  2531. for (;;) {
  2532. flush_pending_writes(conf);
  2533. spin_lock_irqsave(&conf->device_lock, flags);
  2534. if (list_empty(head)) {
  2535. spin_unlock_irqrestore(&conf->device_lock, flags);
  2536. break;
  2537. }
  2538. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2539. list_del(head->prev);
  2540. conf->nr_queued--;
  2541. spin_unlock_irqrestore(&conf->device_lock, flags);
  2542. mddev = r10_bio->mddev;
  2543. conf = mddev->private;
  2544. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2545. test_bit(R10BIO_WriteError, &r10_bio->state))
  2546. handle_write_completed(conf, r10_bio);
  2547. else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
  2548. reshape_request_write(mddev, r10_bio);
  2549. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2550. sync_request_write(mddev, r10_bio);
  2551. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2552. recovery_request_write(mddev, r10_bio);
  2553. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2554. handle_read_error(mddev, r10_bio);
  2555. else {
  2556. /* just a partial read to be scheduled from a
  2557. * separate context
  2558. */
  2559. int slot = r10_bio->read_slot;
  2560. generic_make_request(r10_bio->devs[slot].bio);
  2561. }
  2562. cond_resched();
  2563. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2564. md_check_recovery(mddev);
  2565. }
  2566. blk_finish_plug(&plug);
  2567. }
  2568. static int init_resync(struct r10conf *conf)
  2569. {
  2570. int buffs;
  2571. int i;
  2572. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2573. BUG_ON(conf->r10buf_pool);
  2574. conf->have_replacement = 0;
  2575. for (i = 0; i < conf->geo.raid_disks; i++)
  2576. if (conf->mirrors[i].replacement)
  2577. conf->have_replacement = 1;
  2578. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2579. if (!conf->r10buf_pool)
  2580. return -ENOMEM;
  2581. conf->next_resync = 0;
  2582. return 0;
  2583. }
  2584. /*
  2585. * perform a "sync" on one "block"
  2586. *
  2587. * We need to make sure that no normal I/O request - particularly write
  2588. * requests - conflict with active sync requests.
  2589. *
  2590. * This is achieved by tracking pending requests and a 'barrier' concept
  2591. * that can be installed to exclude normal IO requests.
  2592. *
  2593. * Resync and recovery are handled very differently.
  2594. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2595. *
  2596. * For resync, we iterate over virtual addresses, read all copies,
  2597. * and update if there are differences. If only one copy is live,
  2598. * skip it.
  2599. * For recovery, we iterate over physical addresses, read a good
  2600. * value for each non-in_sync drive, and over-write.
  2601. *
  2602. * So, for recovery we may have several outstanding complex requests for a
  2603. * given address, one for each out-of-sync device. We model this by allocating
  2604. * a number of r10_bio structures, one for each out-of-sync device.
  2605. * As we setup these structures, we collect all bio's together into a list
  2606. * which we then process collectively to add pages, and then process again
  2607. * to pass to generic_make_request.
  2608. *
  2609. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2610. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2611. * has its remaining count decremented to 0, the whole complex operation
  2612. * is complete.
  2613. *
  2614. */
  2615. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2616. int *skipped, int go_faster)
  2617. {
  2618. struct r10conf *conf = mddev->private;
  2619. struct r10bio *r10_bio;
  2620. struct bio *biolist = NULL, *bio;
  2621. sector_t max_sector, nr_sectors;
  2622. int i;
  2623. int max_sync;
  2624. sector_t sync_blocks;
  2625. sector_t sectors_skipped = 0;
  2626. int chunks_skipped = 0;
  2627. sector_t chunk_mask = conf->geo.chunk_mask;
  2628. if (!conf->r10buf_pool)
  2629. if (init_resync(conf))
  2630. return 0;
  2631. skipped:
  2632. max_sector = mddev->dev_sectors;
  2633. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
  2634. test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2635. max_sector = mddev->resync_max_sectors;
  2636. if (sector_nr >= max_sector) {
  2637. /* If we aborted, we need to abort the
  2638. * sync on the 'current' bitmap chucks (there can
  2639. * be several when recovering multiple devices).
  2640. * as we may have started syncing it but not finished.
  2641. * We can find the current address in
  2642. * mddev->curr_resync, but for recovery,
  2643. * we need to convert that to several
  2644. * virtual addresses.
  2645. */
  2646. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2647. end_reshape(conf);
  2648. return 0;
  2649. }
  2650. if (mddev->curr_resync < max_sector) { /* aborted */
  2651. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2652. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2653. &sync_blocks, 1);
  2654. else for (i = 0; i < conf->geo.raid_disks; i++) {
  2655. sector_t sect =
  2656. raid10_find_virt(conf, mddev->curr_resync, i);
  2657. bitmap_end_sync(mddev->bitmap, sect,
  2658. &sync_blocks, 1);
  2659. }
  2660. } else {
  2661. /* completed sync */
  2662. if ((!mddev->bitmap || conf->fullsync)
  2663. && conf->have_replacement
  2664. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2665. /* Completed a full sync so the replacements
  2666. * are now fully recovered.
  2667. */
  2668. for (i = 0; i < conf->geo.raid_disks; i++)
  2669. if (conf->mirrors[i].replacement)
  2670. conf->mirrors[i].replacement
  2671. ->recovery_offset
  2672. = MaxSector;
  2673. }
  2674. conf->fullsync = 0;
  2675. }
  2676. bitmap_close_sync(mddev->bitmap);
  2677. close_sync(conf);
  2678. *skipped = 1;
  2679. return sectors_skipped;
  2680. }
  2681. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2682. return reshape_request(mddev, sector_nr, skipped);
  2683. if (chunks_skipped >= conf->geo.raid_disks) {
  2684. /* if there has been nothing to do on any drive,
  2685. * then there is nothing to do at all..
  2686. */
  2687. *skipped = 1;
  2688. return (max_sector - sector_nr) + sectors_skipped;
  2689. }
  2690. if (max_sector > mddev->resync_max)
  2691. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2692. /* make sure whole request will fit in a chunk - if chunks
  2693. * are meaningful
  2694. */
  2695. if (conf->geo.near_copies < conf->geo.raid_disks &&
  2696. max_sector > (sector_nr | chunk_mask))
  2697. max_sector = (sector_nr | chunk_mask) + 1;
  2698. /*
  2699. * If there is non-resync activity waiting for us then
  2700. * put in a delay to throttle resync.
  2701. */
  2702. if (!go_faster && conf->nr_waiting)
  2703. msleep_interruptible(1000);
  2704. /* Again, very different code for resync and recovery.
  2705. * Both must result in an r10bio with a list of bios that
  2706. * have bi_end_io, bi_sector, bi_bdev set,
  2707. * and bi_private set to the r10bio.
  2708. * For recovery, we may actually create several r10bios
  2709. * with 2 bios in each, that correspond to the bios in the main one.
  2710. * In this case, the subordinate r10bios link back through a
  2711. * borrowed master_bio pointer, and the counter in the master
  2712. * includes a ref from each subordinate.
  2713. */
  2714. /* First, we decide what to do and set ->bi_end_io
  2715. * To end_sync_read if we want to read, and
  2716. * end_sync_write if we will want to write.
  2717. */
  2718. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2719. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2720. /* recovery... the complicated one */
  2721. int j;
  2722. r10_bio = NULL;
  2723. for (i = 0 ; i < conf->geo.raid_disks; i++) {
  2724. int still_degraded;
  2725. struct r10bio *rb2;
  2726. sector_t sect;
  2727. int must_sync;
  2728. int any_working;
  2729. struct raid10_info *mirror = &conf->mirrors[i];
  2730. if ((mirror->rdev == NULL ||
  2731. test_bit(In_sync, &mirror->rdev->flags))
  2732. &&
  2733. (mirror->replacement == NULL ||
  2734. test_bit(Faulty,
  2735. &mirror->replacement->flags)))
  2736. continue;
  2737. still_degraded = 0;
  2738. /* want to reconstruct this device */
  2739. rb2 = r10_bio;
  2740. sect = raid10_find_virt(conf, sector_nr, i);
  2741. if (sect >= mddev->resync_max_sectors) {
  2742. /* last stripe is not complete - don't
  2743. * try to recover this sector.
  2744. */
  2745. continue;
  2746. }
  2747. /* Unless we are doing a full sync, or a replacement
  2748. * we only need to recover the block if it is set in
  2749. * the bitmap
  2750. */
  2751. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2752. &sync_blocks, 1);
  2753. if (sync_blocks < max_sync)
  2754. max_sync = sync_blocks;
  2755. if (!must_sync &&
  2756. mirror->replacement == NULL &&
  2757. !conf->fullsync) {
  2758. /* yep, skip the sync_blocks here, but don't assume
  2759. * that there will never be anything to do here
  2760. */
  2761. chunks_skipped = -1;
  2762. continue;
  2763. }
  2764. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2765. raise_barrier(conf, rb2 != NULL);
  2766. atomic_set(&r10_bio->remaining, 0);
  2767. r10_bio->master_bio = (struct bio*)rb2;
  2768. if (rb2)
  2769. atomic_inc(&rb2->remaining);
  2770. r10_bio->mddev = mddev;
  2771. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2772. r10_bio->sector = sect;
  2773. raid10_find_phys(conf, r10_bio);
  2774. /* Need to check if the array will still be
  2775. * degraded
  2776. */
  2777. for (j = 0; j < conf->geo.raid_disks; j++)
  2778. if (conf->mirrors[j].rdev == NULL ||
  2779. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2780. still_degraded = 1;
  2781. break;
  2782. }
  2783. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2784. &sync_blocks, still_degraded);
  2785. any_working = 0;
  2786. for (j=0; j<conf->copies;j++) {
  2787. int k;
  2788. int d = r10_bio->devs[j].devnum;
  2789. sector_t from_addr, to_addr;
  2790. struct md_rdev *rdev;
  2791. sector_t sector, first_bad;
  2792. int bad_sectors;
  2793. if (!conf->mirrors[d].rdev ||
  2794. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2795. continue;
  2796. /* This is where we read from */
  2797. any_working = 1;
  2798. rdev = conf->mirrors[d].rdev;
  2799. sector = r10_bio->devs[j].addr;
  2800. if (is_badblock(rdev, sector, max_sync,
  2801. &first_bad, &bad_sectors)) {
  2802. if (first_bad > sector)
  2803. max_sync = first_bad - sector;
  2804. else {
  2805. bad_sectors -= (sector
  2806. - first_bad);
  2807. if (max_sync > bad_sectors)
  2808. max_sync = bad_sectors;
  2809. continue;
  2810. }
  2811. }
  2812. bio = r10_bio->devs[0].bio;
  2813. bio->bi_next = biolist;
  2814. biolist = bio;
  2815. bio->bi_private = r10_bio;
  2816. bio->bi_end_io = end_sync_read;
  2817. bio->bi_rw = READ;
  2818. from_addr = r10_bio->devs[j].addr;
  2819. bio->bi_sector = from_addr + rdev->data_offset;
  2820. bio->bi_bdev = rdev->bdev;
  2821. atomic_inc(&rdev->nr_pending);
  2822. /* and we write to 'i' (if not in_sync) */
  2823. for (k=0; k<conf->copies; k++)
  2824. if (r10_bio->devs[k].devnum == i)
  2825. break;
  2826. BUG_ON(k == conf->copies);
  2827. to_addr = r10_bio->devs[k].addr;
  2828. r10_bio->devs[0].devnum = d;
  2829. r10_bio->devs[0].addr = from_addr;
  2830. r10_bio->devs[1].devnum = i;
  2831. r10_bio->devs[1].addr = to_addr;
  2832. rdev = mirror->rdev;
  2833. if (!test_bit(In_sync, &rdev->flags)) {
  2834. bio = r10_bio->devs[1].bio;
  2835. bio->bi_next = biolist;
  2836. biolist = bio;
  2837. bio->bi_private = r10_bio;
  2838. bio->bi_end_io = end_sync_write;
  2839. bio->bi_rw = WRITE;
  2840. bio->bi_sector = to_addr
  2841. + rdev->data_offset;
  2842. bio->bi_bdev = rdev->bdev;
  2843. atomic_inc(&r10_bio->remaining);
  2844. } else
  2845. r10_bio->devs[1].bio->bi_end_io = NULL;
  2846. /* and maybe write to replacement */
  2847. bio = r10_bio->devs[1].repl_bio;
  2848. if (bio)
  2849. bio->bi_end_io = NULL;
  2850. rdev = mirror->replacement;
  2851. /* Note: if rdev != NULL, then bio
  2852. * cannot be NULL as r10buf_pool_alloc will
  2853. * have allocated it.
  2854. * So the second test here is pointless.
  2855. * But it keeps semantic-checkers happy, and
  2856. * this comment keeps human reviewers
  2857. * happy.
  2858. */
  2859. if (rdev == NULL || bio == NULL ||
  2860. test_bit(Faulty, &rdev->flags))
  2861. break;
  2862. bio->bi_next = biolist;
  2863. biolist = bio;
  2864. bio->bi_private = r10_bio;
  2865. bio->bi_end_io = end_sync_write;
  2866. bio->bi_rw = WRITE;
  2867. bio->bi_sector = to_addr + rdev->data_offset;
  2868. bio->bi_bdev = rdev->bdev;
  2869. atomic_inc(&r10_bio->remaining);
  2870. break;
  2871. }
  2872. if (j == conf->copies) {
  2873. /* Cannot recover, so abort the recovery or
  2874. * record a bad block */
  2875. put_buf(r10_bio);
  2876. if (rb2)
  2877. atomic_dec(&rb2->remaining);
  2878. r10_bio = rb2;
  2879. if (any_working) {
  2880. /* problem is that there are bad blocks
  2881. * on other device(s)
  2882. */
  2883. int k;
  2884. for (k = 0; k < conf->copies; k++)
  2885. if (r10_bio->devs[k].devnum == i)
  2886. break;
  2887. if (!test_bit(In_sync,
  2888. &mirror->rdev->flags)
  2889. && !rdev_set_badblocks(
  2890. mirror->rdev,
  2891. r10_bio->devs[k].addr,
  2892. max_sync, 0))
  2893. any_working = 0;
  2894. if (mirror->replacement &&
  2895. !rdev_set_badblocks(
  2896. mirror->replacement,
  2897. r10_bio->devs[k].addr,
  2898. max_sync, 0))
  2899. any_working = 0;
  2900. }
  2901. if (!any_working) {
  2902. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2903. &mddev->recovery))
  2904. printk(KERN_INFO "md/raid10:%s: insufficient "
  2905. "working devices for recovery.\n",
  2906. mdname(mddev));
  2907. mirror->recovery_disabled
  2908. = mddev->recovery_disabled;
  2909. }
  2910. break;
  2911. }
  2912. }
  2913. if (biolist == NULL) {
  2914. while (r10_bio) {
  2915. struct r10bio *rb2 = r10_bio;
  2916. r10_bio = (struct r10bio*) rb2->master_bio;
  2917. rb2->master_bio = NULL;
  2918. put_buf(rb2);
  2919. }
  2920. goto giveup;
  2921. }
  2922. } else {
  2923. /* resync. Schedule a read for every block at this virt offset */
  2924. int count = 0;
  2925. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2926. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2927. &sync_blocks, mddev->degraded) &&
  2928. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2929. &mddev->recovery)) {
  2930. /* We can skip this block */
  2931. *skipped = 1;
  2932. return sync_blocks + sectors_skipped;
  2933. }
  2934. if (sync_blocks < max_sync)
  2935. max_sync = sync_blocks;
  2936. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2937. r10_bio->mddev = mddev;
  2938. atomic_set(&r10_bio->remaining, 0);
  2939. raise_barrier(conf, 0);
  2940. conf->next_resync = sector_nr;
  2941. r10_bio->master_bio = NULL;
  2942. r10_bio->sector = sector_nr;
  2943. set_bit(R10BIO_IsSync, &r10_bio->state);
  2944. raid10_find_phys(conf, r10_bio);
  2945. r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
  2946. for (i = 0; i < conf->copies; i++) {
  2947. int d = r10_bio->devs[i].devnum;
  2948. sector_t first_bad, sector;
  2949. int bad_sectors;
  2950. if (r10_bio->devs[i].repl_bio)
  2951. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2952. bio = r10_bio->devs[i].bio;
  2953. bio->bi_end_io = NULL;
  2954. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2955. if (conf->mirrors[d].rdev == NULL ||
  2956. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  2957. continue;
  2958. sector = r10_bio->devs[i].addr;
  2959. if (is_badblock(conf->mirrors[d].rdev,
  2960. sector, max_sync,
  2961. &first_bad, &bad_sectors)) {
  2962. if (first_bad > sector)
  2963. max_sync = first_bad - sector;
  2964. else {
  2965. bad_sectors -= (sector - first_bad);
  2966. if (max_sync > bad_sectors)
  2967. max_sync = bad_sectors;
  2968. continue;
  2969. }
  2970. }
  2971. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2972. atomic_inc(&r10_bio->remaining);
  2973. bio->bi_next = biolist;
  2974. biolist = bio;
  2975. bio->bi_private = r10_bio;
  2976. bio->bi_end_io = end_sync_read;
  2977. bio->bi_rw = READ;
  2978. bio->bi_sector = sector +
  2979. conf->mirrors[d].rdev->data_offset;
  2980. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  2981. count++;
  2982. if (conf->mirrors[d].replacement == NULL ||
  2983. test_bit(Faulty,
  2984. &conf->mirrors[d].replacement->flags))
  2985. continue;
  2986. /* Need to set up for writing to the replacement */
  2987. bio = r10_bio->devs[i].repl_bio;
  2988. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2989. sector = r10_bio->devs[i].addr;
  2990. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2991. bio->bi_next = biolist;
  2992. biolist = bio;
  2993. bio->bi_private = r10_bio;
  2994. bio->bi_end_io = end_sync_write;
  2995. bio->bi_rw = WRITE;
  2996. bio->bi_sector = sector +
  2997. conf->mirrors[d].replacement->data_offset;
  2998. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  2999. count++;
  3000. }
  3001. if (count < 2) {
  3002. for (i=0; i<conf->copies; i++) {
  3003. int d = r10_bio->devs[i].devnum;
  3004. if (r10_bio->devs[i].bio->bi_end_io)
  3005. rdev_dec_pending(conf->mirrors[d].rdev,
  3006. mddev);
  3007. if (r10_bio->devs[i].repl_bio &&
  3008. r10_bio->devs[i].repl_bio->bi_end_io)
  3009. rdev_dec_pending(
  3010. conf->mirrors[d].replacement,
  3011. mddev);
  3012. }
  3013. put_buf(r10_bio);
  3014. biolist = NULL;
  3015. goto giveup;
  3016. }
  3017. }
  3018. for (bio = biolist; bio ; bio=bio->bi_next) {
  3019. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  3020. if (bio->bi_end_io)
  3021. bio->bi_flags |= 1 << BIO_UPTODATE;
  3022. bio->bi_vcnt = 0;
  3023. bio->bi_idx = 0;
  3024. bio->bi_phys_segments = 0;
  3025. bio->bi_size = 0;
  3026. }
  3027. nr_sectors = 0;
  3028. if (sector_nr + max_sync < max_sector)
  3029. max_sector = sector_nr + max_sync;
  3030. do {
  3031. struct page *page;
  3032. int len = PAGE_SIZE;
  3033. if (sector_nr + (len>>9) > max_sector)
  3034. len = (max_sector - sector_nr) << 9;
  3035. if (len == 0)
  3036. break;
  3037. for (bio= biolist ; bio ; bio=bio->bi_next) {
  3038. struct bio *bio2;
  3039. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  3040. if (bio_add_page(bio, page, len, 0))
  3041. continue;
  3042. /* stop here */
  3043. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  3044. for (bio2 = biolist;
  3045. bio2 && bio2 != bio;
  3046. bio2 = bio2->bi_next) {
  3047. /* remove last page from this bio */
  3048. bio2->bi_vcnt--;
  3049. bio2->bi_size -= len;
  3050. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  3051. }
  3052. goto bio_full;
  3053. }
  3054. nr_sectors += len>>9;
  3055. sector_nr += len>>9;
  3056. } while (biolist->bi_vcnt < RESYNC_PAGES);
  3057. bio_full:
  3058. r10_bio->sectors = nr_sectors;
  3059. while (biolist) {
  3060. bio = biolist;
  3061. biolist = biolist->bi_next;
  3062. bio->bi_next = NULL;
  3063. r10_bio = bio->bi_private;
  3064. r10_bio->sectors = nr_sectors;
  3065. if (bio->bi_end_io == end_sync_read) {
  3066. md_sync_acct(bio->bi_bdev, nr_sectors);
  3067. generic_make_request(bio);
  3068. }
  3069. }
  3070. if (sectors_skipped)
  3071. /* pretend they weren't skipped, it makes
  3072. * no important difference in this case
  3073. */
  3074. md_done_sync(mddev, sectors_skipped, 1);
  3075. return sectors_skipped + nr_sectors;
  3076. giveup:
  3077. /* There is nowhere to write, so all non-sync
  3078. * drives must be failed or in resync, all drives
  3079. * have a bad block, so try the next chunk...
  3080. */
  3081. if (sector_nr + max_sync < max_sector)
  3082. max_sector = sector_nr + max_sync;
  3083. sectors_skipped += (max_sector - sector_nr);
  3084. chunks_skipped ++;
  3085. sector_nr = max_sector;
  3086. goto skipped;
  3087. }
  3088. static sector_t
  3089. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  3090. {
  3091. sector_t size;
  3092. struct r10conf *conf = mddev->private;
  3093. if (!raid_disks)
  3094. raid_disks = min(conf->geo.raid_disks,
  3095. conf->prev.raid_disks);
  3096. if (!sectors)
  3097. sectors = conf->dev_sectors;
  3098. size = sectors >> conf->geo.chunk_shift;
  3099. sector_div(size, conf->geo.far_copies);
  3100. size = size * raid_disks;
  3101. sector_div(size, conf->geo.near_copies);
  3102. return size << conf->geo.chunk_shift;
  3103. }
  3104. static void calc_sectors(struct r10conf *conf, sector_t size)
  3105. {
  3106. /* Calculate the number of sectors-per-device that will
  3107. * actually be used, and set conf->dev_sectors and
  3108. * conf->stride
  3109. */
  3110. size = size >> conf->geo.chunk_shift;
  3111. sector_div(size, conf->geo.far_copies);
  3112. size = size * conf->geo.raid_disks;
  3113. sector_div(size, conf->geo.near_copies);
  3114. /* 'size' is now the number of chunks in the array */
  3115. /* calculate "used chunks per device" */
  3116. size = size * conf->copies;
  3117. /* We need to round up when dividing by raid_disks to
  3118. * get the stride size.
  3119. */
  3120. size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
  3121. conf->dev_sectors = size << conf->geo.chunk_shift;
  3122. if (conf->geo.far_offset)
  3123. conf->geo.stride = 1 << conf->geo.chunk_shift;
  3124. else {
  3125. sector_div(size, conf->geo.far_copies);
  3126. conf->geo.stride = size << conf->geo.chunk_shift;
  3127. }
  3128. }
  3129. enum geo_type {geo_new, geo_old, geo_start};
  3130. static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
  3131. {
  3132. int nc, fc, fo;
  3133. int layout, chunk, disks;
  3134. switch (new) {
  3135. case geo_old:
  3136. layout = mddev->layout;
  3137. chunk = mddev->chunk_sectors;
  3138. disks = mddev->raid_disks - mddev->delta_disks;
  3139. break;
  3140. case geo_new:
  3141. layout = mddev->new_layout;
  3142. chunk = mddev->new_chunk_sectors;
  3143. disks = mddev->raid_disks;
  3144. break;
  3145. default: /* avoid 'may be unused' warnings */
  3146. case geo_start: /* new when starting reshape - raid_disks not
  3147. * updated yet. */
  3148. layout = mddev->new_layout;
  3149. chunk = mddev->new_chunk_sectors;
  3150. disks = mddev->raid_disks + mddev->delta_disks;
  3151. break;
  3152. }
  3153. if (layout >> 18)
  3154. return -1;
  3155. if (chunk < (PAGE_SIZE >> 9) ||
  3156. !is_power_of_2(chunk))
  3157. return -2;
  3158. nc = layout & 255;
  3159. fc = (layout >> 8) & 255;
  3160. fo = layout & (1<<16);
  3161. geo->raid_disks = disks;
  3162. geo->near_copies = nc;
  3163. geo->far_copies = fc;
  3164. geo->far_offset = fo;
  3165. geo->far_set_size = (layout & (1<<17)) ? disks / fc : disks;
  3166. geo->chunk_mask = chunk - 1;
  3167. geo->chunk_shift = ffz(~chunk);
  3168. return nc*fc;
  3169. }
  3170. static struct r10conf *setup_conf(struct mddev *mddev)
  3171. {
  3172. struct r10conf *conf = NULL;
  3173. int err = -EINVAL;
  3174. struct geom geo;
  3175. int copies;
  3176. copies = setup_geo(&geo, mddev, geo_new);
  3177. if (copies == -2) {
  3178. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  3179. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  3180. mdname(mddev), PAGE_SIZE);
  3181. goto out;
  3182. }
  3183. if (copies < 2 || copies > mddev->raid_disks) {
  3184. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  3185. mdname(mddev), mddev->new_layout);
  3186. goto out;
  3187. }
  3188. err = -ENOMEM;
  3189. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  3190. if (!conf)
  3191. goto out;
  3192. /* FIXME calc properly */
  3193. conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
  3194. max(0,mddev->delta_disks)),
  3195. GFP_KERNEL);
  3196. if (!conf->mirrors)
  3197. goto out;
  3198. conf->tmppage = alloc_page(GFP_KERNEL);
  3199. if (!conf->tmppage)
  3200. goto out;
  3201. conf->geo = geo;
  3202. conf->copies = copies;
  3203. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  3204. r10bio_pool_free, conf);
  3205. if (!conf->r10bio_pool)
  3206. goto out;
  3207. calc_sectors(conf, mddev->dev_sectors);
  3208. if (mddev->reshape_position == MaxSector) {
  3209. conf->prev = conf->geo;
  3210. conf->reshape_progress = MaxSector;
  3211. } else {
  3212. if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
  3213. err = -EINVAL;
  3214. goto out;
  3215. }
  3216. conf->reshape_progress = mddev->reshape_position;
  3217. if (conf->prev.far_offset)
  3218. conf->prev.stride = 1 << conf->prev.chunk_shift;
  3219. else
  3220. /* far_copies must be 1 */
  3221. conf->prev.stride = conf->dev_sectors;
  3222. }
  3223. spin_lock_init(&conf->device_lock);
  3224. INIT_LIST_HEAD(&conf->retry_list);
  3225. spin_lock_init(&conf->resync_lock);
  3226. init_waitqueue_head(&conf->wait_barrier);
  3227. conf->thread = md_register_thread(raid10d, mddev, "raid10");
  3228. if (!conf->thread)
  3229. goto out;
  3230. conf->mddev = mddev;
  3231. return conf;
  3232. out:
  3233. if (err == -ENOMEM)
  3234. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  3235. mdname(mddev));
  3236. if (conf) {
  3237. if (conf->r10bio_pool)
  3238. mempool_destroy(conf->r10bio_pool);
  3239. kfree(conf->mirrors);
  3240. safe_put_page(conf->tmppage);
  3241. kfree(conf);
  3242. }
  3243. return ERR_PTR(err);
  3244. }
  3245. static int run(struct mddev *mddev)
  3246. {
  3247. struct r10conf *conf;
  3248. int i, disk_idx, chunk_size;
  3249. struct raid10_info *disk;
  3250. struct md_rdev *rdev;
  3251. sector_t size;
  3252. sector_t min_offset_diff = 0;
  3253. int first = 1;
  3254. bool discard_supported = false;
  3255. if (mddev->private == NULL) {
  3256. conf = setup_conf(mddev);
  3257. if (IS_ERR(conf))
  3258. return PTR_ERR(conf);
  3259. mddev->private = conf;
  3260. }
  3261. conf = mddev->private;
  3262. if (!conf)
  3263. goto out;
  3264. mddev->thread = conf->thread;
  3265. conf->thread = NULL;
  3266. chunk_size = mddev->chunk_sectors << 9;
  3267. if (mddev->queue) {
  3268. blk_queue_max_discard_sectors(mddev->queue,
  3269. mddev->chunk_sectors);
  3270. blk_queue_max_write_same_sectors(mddev->queue,
  3271. mddev->chunk_sectors);
  3272. blk_queue_io_min(mddev->queue, chunk_size);
  3273. if (conf->geo.raid_disks % conf->geo.near_copies)
  3274. blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
  3275. else
  3276. blk_queue_io_opt(mddev->queue, chunk_size *
  3277. (conf->geo.raid_disks / conf->geo.near_copies));
  3278. }
  3279. rdev_for_each(rdev, mddev) {
  3280. long long diff;
  3281. struct request_queue *q;
  3282. disk_idx = rdev->raid_disk;
  3283. if (disk_idx < 0)
  3284. continue;
  3285. if (disk_idx >= conf->geo.raid_disks &&
  3286. disk_idx >= conf->prev.raid_disks)
  3287. continue;
  3288. disk = conf->mirrors + disk_idx;
  3289. if (test_bit(Replacement, &rdev->flags)) {
  3290. if (disk->replacement)
  3291. goto out_free_conf;
  3292. disk->replacement = rdev;
  3293. } else {
  3294. if (disk->rdev)
  3295. goto out_free_conf;
  3296. disk->rdev = rdev;
  3297. }
  3298. q = bdev_get_queue(rdev->bdev);
  3299. if (q->merge_bvec_fn)
  3300. mddev->merge_check_needed = 1;
  3301. diff = (rdev->new_data_offset - rdev->data_offset);
  3302. if (!mddev->reshape_backwards)
  3303. diff = -diff;
  3304. if (diff < 0)
  3305. diff = 0;
  3306. if (first || diff < min_offset_diff)
  3307. min_offset_diff = diff;
  3308. if (mddev->gendisk)
  3309. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3310. rdev->data_offset << 9);
  3311. disk->head_position = 0;
  3312. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  3313. discard_supported = true;
  3314. }
  3315. if (mddev->queue) {
  3316. if (discard_supported)
  3317. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  3318. mddev->queue);
  3319. else
  3320. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  3321. mddev->queue);
  3322. }
  3323. /* need to check that every block has at least one working mirror */
  3324. if (!enough(conf, -1)) {
  3325. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  3326. mdname(mddev));
  3327. goto out_free_conf;
  3328. }
  3329. if (conf->reshape_progress != MaxSector) {
  3330. /* must ensure that shape change is supported */
  3331. if (conf->geo.far_copies != 1 &&
  3332. conf->geo.far_offset == 0)
  3333. goto out_free_conf;
  3334. if (conf->prev.far_copies != 1 &&
  3335. conf->geo.far_offset == 0)
  3336. goto out_free_conf;
  3337. }
  3338. mddev->degraded = 0;
  3339. for (i = 0;
  3340. i < conf->geo.raid_disks
  3341. || i < conf->prev.raid_disks;
  3342. i++) {
  3343. disk = conf->mirrors + i;
  3344. if (!disk->rdev && disk->replacement) {
  3345. /* The replacement is all we have - use it */
  3346. disk->rdev = disk->replacement;
  3347. disk->replacement = NULL;
  3348. clear_bit(Replacement, &disk->rdev->flags);
  3349. }
  3350. if (!disk->rdev ||
  3351. !test_bit(In_sync, &disk->rdev->flags)) {
  3352. disk->head_position = 0;
  3353. mddev->degraded++;
  3354. if (disk->rdev)
  3355. conf->fullsync = 1;
  3356. }
  3357. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3358. }
  3359. if (mddev->recovery_cp != MaxSector)
  3360. printk(KERN_NOTICE "md/raid10:%s: not clean"
  3361. " -- starting background reconstruction\n",
  3362. mdname(mddev));
  3363. printk(KERN_INFO
  3364. "md/raid10:%s: active with %d out of %d devices\n",
  3365. mdname(mddev), conf->geo.raid_disks - mddev->degraded,
  3366. conf->geo.raid_disks);
  3367. /*
  3368. * Ok, everything is just fine now
  3369. */
  3370. mddev->dev_sectors = conf->dev_sectors;
  3371. size = raid10_size(mddev, 0, 0);
  3372. md_set_array_sectors(mddev, size);
  3373. mddev->resync_max_sectors = size;
  3374. if (mddev->queue) {
  3375. int stripe = conf->geo.raid_disks *
  3376. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3377. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  3378. mddev->queue->backing_dev_info.congested_data = mddev;
  3379. /* Calculate max read-ahead size.
  3380. * We need to readahead at least twice a whole stripe....
  3381. * maybe...
  3382. */
  3383. stripe /= conf->geo.near_copies;
  3384. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3385. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3386. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  3387. }
  3388. if (md_integrity_register(mddev))
  3389. goto out_free_conf;
  3390. if (conf->reshape_progress != MaxSector) {
  3391. unsigned long before_length, after_length;
  3392. before_length = ((1 << conf->prev.chunk_shift) *
  3393. conf->prev.far_copies);
  3394. after_length = ((1 << conf->geo.chunk_shift) *
  3395. conf->geo.far_copies);
  3396. if (max(before_length, after_length) > min_offset_diff) {
  3397. /* This cannot work */
  3398. printk("md/raid10: offset difference not enough to continue reshape\n");
  3399. goto out_free_conf;
  3400. }
  3401. conf->offset_diff = min_offset_diff;
  3402. conf->reshape_safe = conf->reshape_progress;
  3403. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3404. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3405. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3406. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3407. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3408. "reshape");
  3409. }
  3410. return 0;
  3411. out_free_conf:
  3412. md_unregister_thread(&mddev->thread);
  3413. if (conf->r10bio_pool)
  3414. mempool_destroy(conf->r10bio_pool);
  3415. safe_put_page(conf->tmppage);
  3416. kfree(conf->mirrors);
  3417. kfree(conf);
  3418. mddev->private = NULL;
  3419. out:
  3420. return -EIO;
  3421. }
  3422. static int stop(struct mddev *mddev)
  3423. {
  3424. struct r10conf *conf = mddev->private;
  3425. raise_barrier(conf, 0);
  3426. lower_barrier(conf);
  3427. md_unregister_thread(&mddev->thread);
  3428. if (mddev->queue)
  3429. /* the unplug fn references 'conf'*/
  3430. blk_sync_queue(mddev->queue);
  3431. if (conf->r10bio_pool)
  3432. mempool_destroy(conf->r10bio_pool);
  3433. kfree(conf->mirrors);
  3434. kfree(conf);
  3435. mddev->private = NULL;
  3436. return 0;
  3437. }
  3438. static void raid10_quiesce(struct mddev *mddev, int state)
  3439. {
  3440. struct r10conf *conf = mddev->private;
  3441. switch(state) {
  3442. case 1:
  3443. raise_barrier(conf, 0);
  3444. break;
  3445. case 0:
  3446. lower_barrier(conf);
  3447. break;
  3448. }
  3449. }
  3450. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3451. {
  3452. /* Resize of 'far' arrays is not supported.
  3453. * For 'near' and 'offset' arrays we can set the
  3454. * number of sectors used to be an appropriate multiple
  3455. * of the chunk size.
  3456. * For 'offset', this is far_copies*chunksize.
  3457. * For 'near' the multiplier is the LCM of
  3458. * near_copies and raid_disks.
  3459. * So if far_copies > 1 && !far_offset, fail.
  3460. * Else find LCM(raid_disks, near_copy)*far_copies and
  3461. * multiply by chunk_size. Then round to this number.
  3462. * This is mostly done by raid10_size()
  3463. */
  3464. struct r10conf *conf = mddev->private;
  3465. sector_t oldsize, size;
  3466. if (mddev->reshape_position != MaxSector)
  3467. return -EBUSY;
  3468. if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
  3469. return -EINVAL;
  3470. oldsize = raid10_size(mddev, 0, 0);
  3471. size = raid10_size(mddev, sectors, 0);
  3472. if (mddev->external_size &&
  3473. mddev->array_sectors > size)
  3474. return -EINVAL;
  3475. if (mddev->bitmap) {
  3476. int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
  3477. if (ret)
  3478. return ret;
  3479. }
  3480. md_set_array_sectors(mddev, size);
  3481. set_capacity(mddev->gendisk, mddev->array_sectors);
  3482. revalidate_disk(mddev->gendisk);
  3483. if (sectors > mddev->dev_sectors &&
  3484. mddev->recovery_cp > oldsize) {
  3485. mddev->recovery_cp = oldsize;
  3486. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3487. }
  3488. calc_sectors(conf, sectors);
  3489. mddev->dev_sectors = conf->dev_sectors;
  3490. mddev->resync_max_sectors = size;
  3491. return 0;
  3492. }
  3493. static void *raid10_takeover_raid0(struct mddev *mddev)
  3494. {
  3495. struct md_rdev *rdev;
  3496. struct r10conf *conf;
  3497. if (mddev->degraded > 0) {
  3498. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3499. mdname(mddev));
  3500. return ERR_PTR(-EINVAL);
  3501. }
  3502. /* Set new parameters */
  3503. mddev->new_level = 10;
  3504. /* new layout: far_copies = 1, near_copies = 2 */
  3505. mddev->new_layout = (1<<8) + 2;
  3506. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3507. mddev->delta_disks = mddev->raid_disks;
  3508. mddev->raid_disks *= 2;
  3509. /* make sure it will be not marked as dirty */
  3510. mddev->recovery_cp = MaxSector;
  3511. conf = setup_conf(mddev);
  3512. if (!IS_ERR(conf)) {
  3513. rdev_for_each(rdev, mddev)
  3514. if (rdev->raid_disk >= 0)
  3515. rdev->new_raid_disk = rdev->raid_disk * 2;
  3516. conf->barrier = 1;
  3517. }
  3518. return conf;
  3519. }
  3520. static void *raid10_takeover(struct mddev *mddev)
  3521. {
  3522. struct r0conf *raid0_conf;
  3523. /* raid10 can take over:
  3524. * raid0 - providing it has only two drives
  3525. */
  3526. if (mddev->level == 0) {
  3527. /* for raid0 takeover only one zone is supported */
  3528. raid0_conf = mddev->private;
  3529. if (raid0_conf->nr_strip_zones > 1) {
  3530. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3531. " with more than one zone.\n",
  3532. mdname(mddev));
  3533. return ERR_PTR(-EINVAL);
  3534. }
  3535. return raid10_takeover_raid0(mddev);
  3536. }
  3537. return ERR_PTR(-EINVAL);
  3538. }
  3539. static int raid10_check_reshape(struct mddev *mddev)
  3540. {
  3541. /* Called when there is a request to change
  3542. * - layout (to ->new_layout)
  3543. * - chunk size (to ->new_chunk_sectors)
  3544. * - raid_disks (by delta_disks)
  3545. * or when trying to restart a reshape that was ongoing.
  3546. *
  3547. * We need to validate the request and possibly allocate
  3548. * space if that might be an issue later.
  3549. *
  3550. * Currently we reject any reshape of a 'far' mode array,
  3551. * allow chunk size to change if new is generally acceptable,
  3552. * allow raid_disks to increase, and allow
  3553. * a switch between 'near' mode and 'offset' mode.
  3554. */
  3555. struct r10conf *conf = mddev->private;
  3556. struct geom geo;
  3557. if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
  3558. return -EINVAL;
  3559. if (setup_geo(&geo, mddev, geo_start) != conf->copies)
  3560. /* mustn't change number of copies */
  3561. return -EINVAL;
  3562. if (geo.far_copies > 1 && !geo.far_offset)
  3563. /* Cannot switch to 'far' mode */
  3564. return -EINVAL;
  3565. if (mddev->array_sectors & geo.chunk_mask)
  3566. /* not factor of array size */
  3567. return -EINVAL;
  3568. if (!enough(conf, -1))
  3569. return -EINVAL;
  3570. kfree(conf->mirrors_new);
  3571. conf->mirrors_new = NULL;
  3572. if (mddev->delta_disks > 0) {
  3573. /* allocate new 'mirrors' list */
  3574. conf->mirrors_new = kzalloc(
  3575. sizeof(struct raid10_info)
  3576. *(mddev->raid_disks +
  3577. mddev->delta_disks),
  3578. GFP_KERNEL);
  3579. if (!conf->mirrors_new)
  3580. return -ENOMEM;
  3581. }
  3582. return 0;
  3583. }
  3584. /*
  3585. * Need to check if array has failed when deciding whether to:
  3586. * - start an array
  3587. * - remove non-faulty devices
  3588. * - add a spare
  3589. * - allow a reshape
  3590. * This determination is simple when no reshape is happening.
  3591. * However if there is a reshape, we need to carefully check
  3592. * both the before and after sections.
  3593. * This is because some failed devices may only affect one
  3594. * of the two sections, and some non-in_sync devices may
  3595. * be insync in the section most affected by failed devices.
  3596. */
  3597. static int calc_degraded(struct r10conf *conf)
  3598. {
  3599. int degraded, degraded2;
  3600. int i;
  3601. rcu_read_lock();
  3602. degraded = 0;
  3603. /* 'prev' section first */
  3604. for (i = 0; i < conf->prev.raid_disks; i++) {
  3605. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3606. if (!rdev || test_bit(Faulty, &rdev->flags))
  3607. degraded++;
  3608. else if (!test_bit(In_sync, &rdev->flags))
  3609. /* When we can reduce the number of devices in
  3610. * an array, this might not contribute to
  3611. * 'degraded'. It does now.
  3612. */
  3613. degraded++;
  3614. }
  3615. rcu_read_unlock();
  3616. if (conf->geo.raid_disks == conf->prev.raid_disks)
  3617. return degraded;
  3618. rcu_read_lock();
  3619. degraded2 = 0;
  3620. for (i = 0; i < conf->geo.raid_disks; i++) {
  3621. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3622. if (!rdev || test_bit(Faulty, &rdev->flags))
  3623. degraded2++;
  3624. else if (!test_bit(In_sync, &rdev->flags)) {
  3625. /* If reshape is increasing the number of devices,
  3626. * this section has already been recovered, so
  3627. * it doesn't contribute to degraded.
  3628. * else it does.
  3629. */
  3630. if (conf->geo.raid_disks <= conf->prev.raid_disks)
  3631. degraded2++;
  3632. }
  3633. }
  3634. rcu_read_unlock();
  3635. if (degraded2 > degraded)
  3636. return degraded2;
  3637. return degraded;
  3638. }
  3639. static int raid10_start_reshape(struct mddev *mddev)
  3640. {
  3641. /* A 'reshape' has been requested. This commits
  3642. * the various 'new' fields and sets MD_RECOVER_RESHAPE
  3643. * This also checks if there are enough spares and adds them
  3644. * to the array.
  3645. * We currently require enough spares to make the final
  3646. * array non-degraded. We also require that the difference
  3647. * between old and new data_offset - on each device - is
  3648. * enough that we never risk over-writing.
  3649. */
  3650. unsigned long before_length, after_length;
  3651. sector_t min_offset_diff = 0;
  3652. int first = 1;
  3653. struct geom new;
  3654. struct r10conf *conf = mddev->private;
  3655. struct md_rdev *rdev;
  3656. int spares = 0;
  3657. int ret;
  3658. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3659. return -EBUSY;
  3660. if (setup_geo(&new, mddev, geo_start) != conf->copies)
  3661. return -EINVAL;
  3662. before_length = ((1 << conf->prev.chunk_shift) *
  3663. conf->prev.far_copies);
  3664. after_length = ((1 << conf->geo.chunk_shift) *
  3665. conf->geo.far_copies);
  3666. rdev_for_each(rdev, mddev) {
  3667. if (!test_bit(In_sync, &rdev->flags)
  3668. && !test_bit(Faulty, &rdev->flags))
  3669. spares++;
  3670. if (rdev->raid_disk >= 0) {
  3671. long long diff = (rdev->new_data_offset
  3672. - rdev->data_offset);
  3673. if (!mddev->reshape_backwards)
  3674. diff = -diff;
  3675. if (diff < 0)
  3676. diff = 0;
  3677. if (first || diff < min_offset_diff)
  3678. min_offset_diff = diff;
  3679. }
  3680. }
  3681. if (max(before_length, after_length) > min_offset_diff)
  3682. return -EINVAL;
  3683. if (spares < mddev->delta_disks)
  3684. return -EINVAL;
  3685. conf->offset_diff = min_offset_diff;
  3686. spin_lock_irq(&conf->device_lock);
  3687. if (conf->mirrors_new) {
  3688. memcpy(conf->mirrors_new, conf->mirrors,
  3689. sizeof(struct raid10_info)*conf->prev.raid_disks);
  3690. smp_mb();
  3691. kfree(conf->mirrors_old); /* FIXME and elsewhere */
  3692. conf->mirrors_old = conf->mirrors;
  3693. conf->mirrors = conf->mirrors_new;
  3694. conf->mirrors_new = NULL;
  3695. }
  3696. setup_geo(&conf->geo, mddev, geo_start);
  3697. smp_mb();
  3698. if (mddev->reshape_backwards) {
  3699. sector_t size = raid10_size(mddev, 0, 0);
  3700. if (size < mddev->array_sectors) {
  3701. spin_unlock_irq(&conf->device_lock);
  3702. printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
  3703. mdname(mddev));
  3704. return -EINVAL;
  3705. }
  3706. mddev->resync_max_sectors = size;
  3707. conf->reshape_progress = size;
  3708. } else
  3709. conf->reshape_progress = 0;
  3710. spin_unlock_irq(&conf->device_lock);
  3711. if (mddev->delta_disks && mddev->bitmap) {
  3712. ret = bitmap_resize(mddev->bitmap,
  3713. raid10_size(mddev, 0,
  3714. conf->geo.raid_disks),
  3715. 0, 0);
  3716. if (ret)
  3717. goto abort;
  3718. }
  3719. if (mddev->delta_disks > 0) {
  3720. rdev_for_each(rdev, mddev)
  3721. if (rdev->raid_disk < 0 &&
  3722. !test_bit(Faulty, &rdev->flags)) {
  3723. if (raid10_add_disk(mddev, rdev) == 0) {
  3724. if (rdev->raid_disk >=
  3725. conf->prev.raid_disks)
  3726. set_bit(In_sync, &rdev->flags);
  3727. else
  3728. rdev->recovery_offset = 0;
  3729. if (sysfs_link_rdev(mddev, rdev))
  3730. /* Failure here is OK */;
  3731. }
  3732. } else if (rdev->raid_disk >= conf->prev.raid_disks
  3733. && !test_bit(Faulty, &rdev->flags)) {
  3734. /* This is a spare that was manually added */
  3735. set_bit(In_sync, &rdev->flags);
  3736. }
  3737. }
  3738. /* When a reshape changes the number of devices,
  3739. * ->degraded is measured against the larger of the
  3740. * pre and post numbers.
  3741. */
  3742. spin_lock_irq(&conf->device_lock);
  3743. mddev->degraded = calc_degraded(conf);
  3744. spin_unlock_irq(&conf->device_lock);
  3745. mddev->raid_disks = conf->geo.raid_disks;
  3746. mddev->reshape_position = conf->reshape_progress;
  3747. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3748. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3749. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3750. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3751. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3752. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3753. "reshape");
  3754. if (!mddev->sync_thread) {
  3755. ret = -EAGAIN;
  3756. goto abort;
  3757. }
  3758. conf->reshape_checkpoint = jiffies;
  3759. md_wakeup_thread(mddev->sync_thread);
  3760. md_new_event(mddev);
  3761. return 0;
  3762. abort:
  3763. mddev->recovery = 0;
  3764. spin_lock_irq(&conf->device_lock);
  3765. conf->geo = conf->prev;
  3766. mddev->raid_disks = conf->geo.raid_disks;
  3767. rdev_for_each(rdev, mddev)
  3768. rdev->new_data_offset = rdev->data_offset;
  3769. smp_wmb();
  3770. conf->reshape_progress = MaxSector;
  3771. mddev->reshape_position = MaxSector;
  3772. spin_unlock_irq(&conf->device_lock);
  3773. return ret;
  3774. }
  3775. /* Calculate the last device-address that could contain
  3776. * any block from the chunk that includes the array-address 's'
  3777. * and report the next address.
  3778. * i.e. the address returned will be chunk-aligned and after
  3779. * any data that is in the chunk containing 's'.
  3780. */
  3781. static sector_t last_dev_address(sector_t s, struct geom *geo)
  3782. {
  3783. s = (s | geo->chunk_mask) + 1;
  3784. s >>= geo->chunk_shift;
  3785. s *= geo->near_copies;
  3786. s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
  3787. s *= geo->far_copies;
  3788. s <<= geo->chunk_shift;
  3789. return s;
  3790. }
  3791. /* Calculate the first device-address that could contain
  3792. * any block from the chunk that includes the array-address 's'.
  3793. * This too will be the start of a chunk
  3794. */
  3795. static sector_t first_dev_address(sector_t s, struct geom *geo)
  3796. {
  3797. s >>= geo->chunk_shift;
  3798. s *= geo->near_copies;
  3799. sector_div(s, geo->raid_disks);
  3800. s *= geo->far_copies;
  3801. s <<= geo->chunk_shift;
  3802. return s;
  3803. }
  3804. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  3805. int *skipped)
  3806. {
  3807. /* We simply copy at most one chunk (smallest of old and new)
  3808. * at a time, possibly less if that exceeds RESYNC_PAGES,
  3809. * or we hit a bad block or something.
  3810. * This might mean we pause for normal IO in the middle of
  3811. * a chunk, but that is not a problem was mddev->reshape_position
  3812. * can record any location.
  3813. *
  3814. * If we will want to write to a location that isn't
  3815. * yet recorded as 'safe' (i.e. in metadata on disk) then
  3816. * we need to flush all reshape requests and update the metadata.
  3817. *
  3818. * When reshaping forwards (e.g. to more devices), we interpret
  3819. * 'safe' as the earliest block which might not have been copied
  3820. * down yet. We divide this by previous stripe size and multiply
  3821. * by previous stripe length to get lowest device offset that we
  3822. * cannot write to yet.
  3823. * We interpret 'sector_nr' as an address that we want to write to.
  3824. * From this we use last_device_address() to find where we might
  3825. * write to, and first_device_address on the 'safe' position.
  3826. * If this 'next' write position is after the 'safe' position,
  3827. * we must update the metadata to increase the 'safe' position.
  3828. *
  3829. * When reshaping backwards, we round in the opposite direction
  3830. * and perform the reverse test: next write position must not be
  3831. * less than current safe position.
  3832. *
  3833. * In all this the minimum difference in data offsets
  3834. * (conf->offset_diff - always positive) allows a bit of slack,
  3835. * so next can be after 'safe', but not by more than offset_disk
  3836. *
  3837. * We need to prepare all the bios here before we start any IO
  3838. * to ensure the size we choose is acceptable to all devices.
  3839. * The means one for each copy for write-out and an extra one for
  3840. * read-in.
  3841. * We store the read-in bio in ->master_bio and the others in
  3842. * ->devs[x].bio and ->devs[x].repl_bio.
  3843. */
  3844. struct r10conf *conf = mddev->private;
  3845. struct r10bio *r10_bio;
  3846. sector_t next, safe, last;
  3847. int max_sectors;
  3848. int nr_sectors;
  3849. int s;
  3850. struct md_rdev *rdev;
  3851. int need_flush = 0;
  3852. struct bio *blist;
  3853. struct bio *bio, *read_bio;
  3854. int sectors_done = 0;
  3855. if (sector_nr == 0) {
  3856. /* If restarting in the middle, skip the initial sectors */
  3857. if (mddev->reshape_backwards &&
  3858. conf->reshape_progress < raid10_size(mddev, 0, 0)) {
  3859. sector_nr = (raid10_size(mddev, 0, 0)
  3860. - conf->reshape_progress);
  3861. } else if (!mddev->reshape_backwards &&
  3862. conf->reshape_progress > 0)
  3863. sector_nr = conf->reshape_progress;
  3864. if (sector_nr) {
  3865. mddev->curr_resync_completed = sector_nr;
  3866. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3867. *skipped = 1;
  3868. return sector_nr;
  3869. }
  3870. }
  3871. /* We don't use sector_nr to track where we are up to
  3872. * as that doesn't work well for ->reshape_backwards.
  3873. * So just use ->reshape_progress.
  3874. */
  3875. if (mddev->reshape_backwards) {
  3876. /* 'next' is the earliest device address that we might
  3877. * write to for this chunk in the new layout
  3878. */
  3879. next = first_dev_address(conf->reshape_progress - 1,
  3880. &conf->geo);
  3881. /* 'safe' is the last device address that we might read from
  3882. * in the old layout after a restart
  3883. */
  3884. safe = last_dev_address(conf->reshape_safe - 1,
  3885. &conf->prev);
  3886. if (next + conf->offset_diff < safe)
  3887. need_flush = 1;
  3888. last = conf->reshape_progress - 1;
  3889. sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
  3890. & conf->prev.chunk_mask);
  3891. if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
  3892. sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
  3893. } else {
  3894. /* 'next' is after the last device address that we
  3895. * might write to for this chunk in the new layout
  3896. */
  3897. next = last_dev_address(conf->reshape_progress, &conf->geo);
  3898. /* 'safe' is the earliest device address that we might
  3899. * read from in the old layout after a restart
  3900. */
  3901. safe = first_dev_address(conf->reshape_safe, &conf->prev);
  3902. /* Need to update metadata if 'next' might be beyond 'safe'
  3903. * as that would possibly corrupt data
  3904. */
  3905. if (next > safe + conf->offset_diff)
  3906. need_flush = 1;
  3907. sector_nr = conf->reshape_progress;
  3908. last = sector_nr | (conf->geo.chunk_mask
  3909. & conf->prev.chunk_mask);
  3910. if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
  3911. last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
  3912. }
  3913. if (need_flush ||
  3914. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3915. /* Need to update reshape_position in metadata */
  3916. wait_barrier(conf);
  3917. mddev->reshape_position = conf->reshape_progress;
  3918. if (mddev->reshape_backwards)
  3919. mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
  3920. - conf->reshape_progress;
  3921. else
  3922. mddev->curr_resync_completed = conf->reshape_progress;
  3923. conf->reshape_checkpoint = jiffies;
  3924. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3925. md_wakeup_thread(mddev->thread);
  3926. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3927. kthread_should_stop());
  3928. conf->reshape_safe = mddev->reshape_position;
  3929. allow_barrier(conf);
  3930. }
  3931. read_more:
  3932. /* Now schedule reads for blocks from sector_nr to last */
  3933. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  3934. raise_barrier(conf, sectors_done != 0);
  3935. atomic_set(&r10_bio->remaining, 0);
  3936. r10_bio->mddev = mddev;
  3937. r10_bio->sector = sector_nr;
  3938. set_bit(R10BIO_IsReshape, &r10_bio->state);
  3939. r10_bio->sectors = last - sector_nr + 1;
  3940. rdev = read_balance(conf, r10_bio, &max_sectors);
  3941. BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
  3942. if (!rdev) {
  3943. /* Cannot read from here, so need to record bad blocks
  3944. * on all the target devices.
  3945. */
  3946. // FIXME
  3947. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  3948. return sectors_done;
  3949. }
  3950. read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
  3951. read_bio->bi_bdev = rdev->bdev;
  3952. read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
  3953. + rdev->data_offset);
  3954. read_bio->bi_private = r10_bio;
  3955. read_bio->bi_end_io = end_sync_read;
  3956. read_bio->bi_rw = READ;
  3957. read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  3958. read_bio->bi_flags |= 1 << BIO_UPTODATE;
  3959. read_bio->bi_vcnt = 0;
  3960. read_bio->bi_idx = 0;
  3961. read_bio->bi_size = 0;
  3962. r10_bio->master_bio = read_bio;
  3963. r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
  3964. /* Now find the locations in the new layout */
  3965. __raid10_find_phys(&conf->geo, r10_bio);
  3966. blist = read_bio;
  3967. read_bio->bi_next = NULL;
  3968. for (s = 0; s < conf->copies*2; s++) {
  3969. struct bio *b;
  3970. int d = r10_bio->devs[s/2].devnum;
  3971. struct md_rdev *rdev2;
  3972. if (s&1) {
  3973. rdev2 = conf->mirrors[d].replacement;
  3974. b = r10_bio->devs[s/2].repl_bio;
  3975. } else {
  3976. rdev2 = conf->mirrors[d].rdev;
  3977. b = r10_bio->devs[s/2].bio;
  3978. }
  3979. if (!rdev2 || test_bit(Faulty, &rdev2->flags))
  3980. continue;
  3981. b->bi_bdev = rdev2->bdev;
  3982. b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
  3983. b->bi_private = r10_bio;
  3984. b->bi_end_io = end_reshape_write;
  3985. b->bi_rw = WRITE;
  3986. b->bi_flags &= ~(BIO_POOL_MASK - 1);
  3987. b->bi_flags |= 1 << BIO_UPTODATE;
  3988. b->bi_next = blist;
  3989. b->bi_vcnt = 0;
  3990. b->bi_idx = 0;
  3991. b->bi_size = 0;
  3992. blist = b;
  3993. }
  3994. /* Now add as many pages as possible to all of these bios. */
  3995. nr_sectors = 0;
  3996. for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
  3997. struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
  3998. int len = (max_sectors - s) << 9;
  3999. if (len > PAGE_SIZE)
  4000. len = PAGE_SIZE;
  4001. for (bio = blist; bio ; bio = bio->bi_next) {
  4002. struct bio *bio2;
  4003. if (bio_add_page(bio, page, len, 0))
  4004. continue;
  4005. /* Didn't fit, must stop */
  4006. for (bio2 = blist;
  4007. bio2 && bio2 != bio;
  4008. bio2 = bio2->bi_next) {
  4009. /* Remove last page from this bio */
  4010. bio2->bi_vcnt--;
  4011. bio2->bi_size -= len;
  4012. bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
  4013. }
  4014. goto bio_full;
  4015. }
  4016. sector_nr += len >> 9;
  4017. nr_sectors += len >> 9;
  4018. }
  4019. bio_full:
  4020. r10_bio->sectors = nr_sectors;
  4021. /* Now submit the read */
  4022. md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
  4023. atomic_inc(&r10_bio->remaining);
  4024. read_bio->bi_next = NULL;
  4025. generic_make_request(read_bio);
  4026. sector_nr += nr_sectors;
  4027. sectors_done += nr_sectors;
  4028. if (sector_nr <= last)
  4029. goto read_more;
  4030. /* Now that we have done the whole section we can
  4031. * update reshape_progress
  4032. */
  4033. if (mddev->reshape_backwards)
  4034. conf->reshape_progress -= sectors_done;
  4035. else
  4036. conf->reshape_progress += sectors_done;
  4037. return sectors_done;
  4038. }
  4039. static void end_reshape_request(struct r10bio *r10_bio);
  4040. static int handle_reshape_read_error(struct mddev *mddev,
  4041. struct r10bio *r10_bio);
  4042. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  4043. {
  4044. /* Reshape read completed. Hopefully we have a block
  4045. * to write out.
  4046. * If we got a read error then we do sync 1-page reads from
  4047. * elsewhere until we find the data - or give up.
  4048. */
  4049. struct r10conf *conf = mddev->private;
  4050. int s;
  4051. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  4052. if (handle_reshape_read_error(mddev, r10_bio) < 0) {
  4053. /* Reshape has been aborted */
  4054. md_done_sync(mddev, r10_bio->sectors, 0);
  4055. return;
  4056. }
  4057. /* We definitely have the data in the pages, schedule the
  4058. * writes.
  4059. */
  4060. atomic_set(&r10_bio->remaining, 1);
  4061. for (s = 0; s < conf->copies*2; s++) {
  4062. struct bio *b;
  4063. int d = r10_bio->devs[s/2].devnum;
  4064. struct md_rdev *rdev;
  4065. if (s&1) {
  4066. rdev = conf->mirrors[d].replacement;
  4067. b = r10_bio->devs[s/2].repl_bio;
  4068. } else {
  4069. rdev = conf->mirrors[d].rdev;
  4070. b = r10_bio->devs[s/2].bio;
  4071. }
  4072. if (!rdev || test_bit(Faulty, &rdev->flags))
  4073. continue;
  4074. atomic_inc(&rdev->nr_pending);
  4075. md_sync_acct(b->bi_bdev, r10_bio->sectors);
  4076. atomic_inc(&r10_bio->remaining);
  4077. b->bi_next = NULL;
  4078. generic_make_request(b);
  4079. }
  4080. end_reshape_request(r10_bio);
  4081. }
  4082. static void end_reshape(struct r10conf *conf)
  4083. {
  4084. if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
  4085. return;
  4086. spin_lock_irq(&conf->device_lock);
  4087. conf->prev = conf->geo;
  4088. md_finish_reshape(conf->mddev);
  4089. smp_wmb();
  4090. conf->reshape_progress = MaxSector;
  4091. spin_unlock_irq(&conf->device_lock);
  4092. /* read-ahead size must cover two whole stripes, which is
  4093. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4094. */
  4095. if (conf->mddev->queue) {
  4096. int stripe = conf->geo.raid_disks *
  4097. ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
  4098. stripe /= conf->geo.near_copies;
  4099. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4100. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4101. }
  4102. conf->fullsync = 0;
  4103. }
  4104. static int handle_reshape_read_error(struct mddev *mddev,
  4105. struct r10bio *r10_bio)
  4106. {
  4107. /* Use sync reads to get the blocks from somewhere else */
  4108. int sectors = r10_bio->sectors;
  4109. struct r10conf *conf = mddev->private;
  4110. struct {
  4111. struct r10bio r10_bio;
  4112. struct r10dev devs[conf->copies];
  4113. } on_stack;
  4114. struct r10bio *r10b = &on_stack.r10_bio;
  4115. int slot = 0;
  4116. int idx = 0;
  4117. struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
  4118. r10b->sector = r10_bio->sector;
  4119. __raid10_find_phys(&conf->prev, r10b);
  4120. while (sectors) {
  4121. int s = sectors;
  4122. int success = 0;
  4123. int first_slot = slot;
  4124. if (s > (PAGE_SIZE >> 9))
  4125. s = PAGE_SIZE >> 9;
  4126. while (!success) {
  4127. int d = r10b->devs[slot].devnum;
  4128. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4129. sector_t addr;
  4130. if (rdev == NULL ||
  4131. test_bit(Faulty, &rdev->flags) ||
  4132. !test_bit(In_sync, &rdev->flags))
  4133. goto failed;
  4134. addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
  4135. success = sync_page_io(rdev,
  4136. addr,
  4137. s << 9,
  4138. bvec[idx].bv_page,
  4139. READ, false);
  4140. if (success)
  4141. break;
  4142. failed:
  4143. slot++;
  4144. if (slot >= conf->copies)
  4145. slot = 0;
  4146. if (slot == first_slot)
  4147. break;
  4148. }
  4149. if (!success) {
  4150. /* couldn't read this block, must give up */
  4151. set_bit(MD_RECOVERY_INTR,
  4152. &mddev->recovery);
  4153. return -EIO;
  4154. }
  4155. sectors -= s;
  4156. idx++;
  4157. }
  4158. return 0;
  4159. }
  4160. static void end_reshape_write(struct bio *bio, int error)
  4161. {
  4162. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  4163. struct r10bio *r10_bio = bio->bi_private;
  4164. struct mddev *mddev = r10_bio->mddev;
  4165. struct r10conf *conf = mddev->private;
  4166. int d;
  4167. int slot;
  4168. int repl;
  4169. struct md_rdev *rdev = NULL;
  4170. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  4171. if (repl)
  4172. rdev = conf->mirrors[d].replacement;
  4173. if (!rdev) {
  4174. smp_mb();
  4175. rdev = conf->mirrors[d].rdev;
  4176. }
  4177. if (!uptodate) {
  4178. /* FIXME should record badblock */
  4179. md_error(mddev, rdev);
  4180. }
  4181. rdev_dec_pending(rdev, mddev);
  4182. end_reshape_request(r10_bio);
  4183. }
  4184. static void end_reshape_request(struct r10bio *r10_bio)
  4185. {
  4186. if (!atomic_dec_and_test(&r10_bio->remaining))
  4187. return;
  4188. md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
  4189. bio_put(r10_bio->master_bio);
  4190. put_buf(r10_bio);
  4191. }
  4192. static void raid10_finish_reshape(struct mddev *mddev)
  4193. {
  4194. struct r10conf *conf = mddev->private;
  4195. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  4196. return;
  4197. if (mddev->delta_disks > 0) {
  4198. sector_t size = raid10_size(mddev, 0, 0);
  4199. md_set_array_sectors(mddev, size);
  4200. if (mddev->recovery_cp > mddev->resync_max_sectors) {
  4201. mddev->recovery_cp = mddev->resync_max_sectors;
  4202. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4203. }
  4204. mddev->resync_max_sectors = size;
  4205. set_capacity(mddev->gendisk, mddev->array_sectors);
  4206. revalidate_disk(mddev->gendisk);
  4207. } else {
  4208. int d;
  4209. for (d = conf->geo.raid_disks ;
  4210. d < conf->geo.raid_disks - mddev->delta_disks;
  4211. d++) {
  4212. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4213. if (rdev)
  4214. clear_bit(In_sync, &rdev->flags);
  4215. rdev = conf->mirrors[d].replacement;
  4216. if (rdev)
  4217. clear_bit(In_sync, &rdev->flags);
  4218. }
  4219. }
  4220. mddev->layout = mddev->new_layout;
  4221. mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
  4222. mddev->reshape_position = MaxSector;
  4223. mddev->delta_disks = 0;
  4224. mddev->reshape_backwards = 0;
  4225. }
  4226. static struct md_personality raid10_personality =
  4227. {
  4228. .name = "raid10",
  4229. .level = 10,
  4230. .owner = THIS_MODULE,
  4231. .make_request = make_request,
  4232. .run = run,
  4233. .stop = stop,
  4234. .status = status,
  4235. .error_handler = error,
  4236. .hot_add_disk = raid10_add_disk,
  4237. .hot_remove_disk= raid10_remove_disk,
  4238. .spare_active = raid10_spare_active,
  4239. .sync_request = sync_request,
  4240. .quiesce = raid10_quiesce,
  4241. .size = raid10_size,
  4242. .resize = raid10_resize,
  4243. .takeover = raid10_takeover,
  4244. .check_reshape = raid10_check_reshape,
  4245. .start_reshape = raid10_start_reshape,
  4246. .finish_reshape = raid10_finish_reshape,
  4247. };
  4248. static int __init raid_init(void)
  4249. {
  4250. return register_md_personality(&raid10_personality);
  4251. }
  4252. static void raid_exit(void)
  4253. {
  4254. unregister_md_personality(&raid10_personality);
  4255. }
  4256. module_init(raid_init);
  4257. module_exit(raid_exit);
  4258. MODULE_LICENSE("GPL");
  4259. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  4260. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  4261. MODULE_ALIAS("md-raid10");
  4262. MODULE_ALIAS("md-level-10");
  4263. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);