raid10.c 128 KB

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