raid10.c 130 KB

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