raid10.c 135 KB

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