raid10.c 131 KB

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