raid10.c 127 KB

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