raid10.c 131 KB

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