raid10.c 136 KB

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