raid10.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644
  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 "md.h"
  27. #include "raid10.h"
  28. #include "raid0.h"
  29. #include "bitmap.h"
  30. /*
  31. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  32. * The layout of data is defined by
  33. * chunk_size
  34. * raid_disks
  35. * near_copies (stored in low byte of layout)
  36. * far_copies (stored in second byte of layout)
  37. * far_offset (stored in bit 16 of layout )
  38. *
  39. * The data to be stored is divided into chunks using chunksize.
  40. * Each device is divided into far_copies sections.
  41. * In each section, chunks are laid out in a style similar to raid0, but
  42. * near_copies copies of each chunk is stored (each on a different drive).
  43. * The starting device for each section is offset near_copies from the starting
  44. * device of the previous section.
  45. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
  46. * drive.
  47. * near_copies and far_copies must be at least one, and their product is at most
  48. * raid_disks.
  49. *
  50. * If far_offset is true, then the far_copies are handled a bit differently.
  51. * The copies are still in different stripes, but instead of be very far apart
  52. * on disk, there are adjacent stripes.
  53. */
  54. /*
  55. * Number of guaranteed r10bios in case of extreme VM load:
  56. */
  57. #define NR_RAID10_BIOS 256
  58. /* When there are this many requests queue to be written by
  59. * the raid10 thread, we become 'congested' to provide back-pressure
  60. * for writeback.
  61. */
  62. static int max_queued_requests = 1024;
  63. static void allow_barrier(struct r10conf *conf);
  64. static void lower_barrier(struct r10conf *conf);
  65. static int enough(struct r10conf *conf, int ignore);
  66. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  67. {
  68. struct r10conf *conf = data;
  69. int size = offsetof(struct r10bio, devs[conf->copies]);
  70. /* allocate a r10bio with room for raid_disks entries in the
  71. * bios array */
  72. return kzalloc(size, gfp_flags);
  73. }
  74. static void r10bio_pool_free(void *r10_bio, void *data)
  75. {
  76. kfree(r10_bio);
  77. }
  78. /* Maximum size of each resync request */
  79. #define RESYNC_BLOCK_SIZE (64*1024)
  80. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  81. /* amount of memory to reserve for resync requests */
  82. #define RESYNC_WINDOW (1024*1024)
  83. /* maximum number of concurrent requests, memory permitting */
  84. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  85. /*
  86. * When performing a resync, we need to read and compare, so
  87. * we need as many pages are there are copies.
  88. * When performing a recovery, we need 2 bios, one for read,
  89. * one for write (we recover only one drive per r10buf)
  90. *
  91. */
  92. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  93. {
  94. struct r10conf *conf = data;
  95. struct page *page;
  96. struct r10bio *r10_bio;
  97. struct bio *bio;
  98. int i, j;
  99. int nalloc;
  100. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  101. if (!r10_bio)
  102. return NULL;
  103. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  104. nalloc = conf->copies; /* resync */
  105. else
  106. nalloc = 2; /* recovery */
  107. /*
  108. * Allocate bios.
  109. */
  110. for (j = nalloc ; j-- ; ) {
  111. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  112. if (!bio)
  113. goto out_free_bio;
  114. r10_bio->devs[j].bio = bio;
  115. if (!conf->have_replacement)
  116. continue;
  117. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  118. if (!bio)
  119. goto out_free_bio;
  120. r10_bio->devs[j].repl_bio = bio;
  121. }
  122. /*
  123. * Allocate RESYNC_PAGES data pages and attach them
  124. * where needed.
  125. */
  126. for (j = 0 ; j < nalloc; j++) {
  127. struct bio *rbio = r10_bio->devs[j].repl_bio;
  128. bio = r10_bio->devs[j].bio;
  129. for (i = 0; i < RESYNC_PAGES; i++) {
  130. if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
  131. &conf->mddev->recovery)) {
  132. /* we can share bv_page's during recovery */
  133. struct bio *rbio = r10_bio->devs[0].bio;
  134. page = rbio->bi_io_vec[i].bv_page;
  135. get_page(page);
  136. } else
  137. page = alloc_page(gfp_flags);
  138. if (unlikely(!page))
  139. goto out_free_pages;
  140. bio->bi_io_vec[i].bv_page = page;
  141. if (rbio)
  142. rbio->bi_io_vec[i].bv_page = page;
  143. }
  144. }
  145. return r10_bio;
  146. out_free_pages:
  147. for ( ; i > 0 ; i--)
  148. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  149. while (j--)
  150. for (i = 0; i < RESYNC_PAGES ; i++)
  151. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  152. j = -1;
  153. out_free_bio:
  154. while (++j < nalloc) {
  155. bio_put(r10_bio->devs[j].bio);
  156. if (r10_bio->devs[j].repl_bio)
  157. bio_put(r10_bio->devs[j].repl_bio);
  158. }
  159. r10bio_pool_free(r10_bio, conf);
  160. return NULL;
  161. }
  162. static void r10buf_pool_free(void *__r10_bio, void *data)
  163. {
  164. int i;
  165. struct r10conf *conf = data;
  166. struct r10bio *r10bio = __r10_bio;
  167. int j;
  168. for (j=0; j < conf->copies; j++) {
  169. struct bio *bio = r10bio->devs[j].bio;
  170. if (bio) {
  171. for (i = 0; i < RESYNC_PAGES; i++) {
  172. safe_put_page(bio->bi_io_vec[i].bv_page);
  173. bio->bi_io_vec[i].bv_page = NULL;
  174. }
  175. bio_put(bio);
  176. }
  177. bio = r10bio->devs[j].repl_bio;
  178. if (bio)
  179. bio_put(bio);
  180. }
  181. r10bio_pool_free(r10bio, conf);
  182. }
  183. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  184. {
  185. int i;
  186. for (i = 0; i < conf->copies; i++) {
  187. struct bio **bio = & r10_bio->devs[i].bio;
  188. if (!BIO_SPECIAL(*bio))
  189. bio_put(*bio);
  190. *bio = NULL;
  191. bio = &r10_bio->devs[i].repl_bio;
  192. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  193. bio_put(*bio);
  194. *bio = NULL;
  195. }
  196. }
  197. static void free_r10bio(struct r10bio *r10_bio)
  198. {
  199. struct r10conf *conf = r10_bio->mddev->private;
  200. put_all_bios(conf, r10_bio);
  201. mempool_free(r10_bio, conf->r10bio_pool);
  202. }
  203. static void put_buf(struct r10bio *r10_bio)
  204. {
  205. struct r10conf *conf = r10_bio->mddev->private;
  206. mempool_free(r10_bio, conf->r10buf_pool);
  207. lower_barrier(conf);
  208. }
  209. static void reschedule_retry(struct r10bio *r10_bio)
  210. {
  211. unsigned long flags;
  212. struct mddev *mddev = r10_bio->mddev;
  213. struct r10conf *conf = mddev->private;
  214. spin_lock_irqsave(&conf->device_lock, flags);
  215. list_add(&r10_bio->retry_list, &conf->retry_list);
  216. conf->nr_queued ++;
  217. spin_unlock_irqrestore(&conf->device_lock, flags);
  218. /* wake up frozen array... */
  219. wake_up(&conf->wait_barrier);
  220. md_wakeup_thread(mddev->thread);
  221. }
  222. /*
  223. * raid_end_bio_io() is called when we have finished servicing a mirrored
  224. * operation and are ready to return a success/failure code to the buffer
  225. * cache layer.
  226. */
  227. static void raid_end_bio_io(struct r10bio *r10_bio)
  228. {
  229. struct bio *bio = r10_bio->master_bio;
  230. int done;
  231. struct r10conf *conf = r10_bio->mddev->private;
  232. if (bio->bi_phys_segments) {
  233. unsigned long flags;
  234. spin_lock_irqsave(&conf->device_lock, flags);
  235. bio->bi_phys_segments--;
  236. done = (bio->bi_phys_segments == 0);
  237. spin_unlock_irqrestore(&conf->device_lock, flags);
  238. } else
  239. done = 1;
  240. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  241. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  242. if (done) {
  243. bio_endio(bio, 0);
  244. /*
  245. * Wake up any possible resync thread that waits for the device
  246. * to go idle.
  247. */
  248. allow_barrier(conf);
  249. }
  250. free_r10bio(r10_bio);
  251. }
  252. /*
  253. * Update disk head position estimator based on IRQ completion info.
  254. */
  255. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  256. {
  257. struct r10conf *conf = r10_bio->mddev->private;
  258. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  259. r10_bio->devs[slot].addr + (r10_bio->sectors);
  260. }
  261. /*
  262. * Find the disk number which triggered given bio
  263. */
  264. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  265. struct bio *bio, int *slotp, int *replp)
  266. {
  267. int slot;
  268. int repl = 0;
  269. for (slot = 0; slot < conf->copies; slot++) {
  270. if (r10_bio->devs[slot].bio == bio)
  271. break;
  272. if (r10_bio->devs[slot].repl_bio == bio) {
  273. repl = 1;
  274. break;
  275. }
  276. }
  277. BUG_ON(slot == conf->copies);
  278. update_head_pos(slot, r10_bio);
  279. if (slotp)
  280. *slotp = slot;
  281. if (replp)
  282. *replp = repl;
  283. return r10_bio->devs[slot].devnum;
  284. }
  285. static void raid10_end_read_request(struct bio *bio, int error)
  286. {
  287. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  288. struct r10bio *r10_bio = bio->bi_private;
  289. int slot, dev;
  290. struct md_rdev *rdev;
  291. struct r10conf *conf = r10_bio->mddev->private;
  292. slot = r10_bio->read_slot;
  293. dev = r10_bio->devs[slot].devnum;
  294. rdev = r10_bio->devs[slot].rdev;
  295. /*
  296. * this branch is our 'one mirror IO has finished' event handler:
  297. */
  298. update_head_pos(slot, r10_bio);
  299. if (uptodate) {
  300. /*
  301. * Set R10BIO_Uptodate in our master bio, so that
  302. * we will return a good error code to the higher
  303. * levels even if IO on some other mirrored buffer fails.
  304. *
  305. * The 'master' represents the composite IO operation to
  306. * user-side. So if something waits for IO, then it will
  307. * wait for the 'master' bio.
  308. */
  309. set_bit(R10BIO_Uptodate, &r10_bio->state);
  310. } else {
  311. /* If all other devices that store this block have
  312. * failed, we want to return the error upwards rather
  313. * than fail the last device. Here we redefine
  314. * "uptodate" to mean "Don't want to retry"
  315. */
  316. unsigned long flags;
  317. spin_lock_irqsave(&conf->device_lock, flags);
  318. if (!enough(conf, rdev->raid_disk))
  319. uptodate = 1;
  320. spin_unlock_irqrestore(&conf->device_lock, flags);
  321. }
  322. if (uptodate) {
  323. raid_end_bio_io(r10_bio);
  324. rdev_dec_pending(rdev, conf->mddev);
  325. } else {
  326. /*
  327. * oops, read error - keep the refcount on the rdev
  328. */
  329. char b[BDEVNAME_SIZE];
  330. printk_ratelimited(KERN_ERR
  331. "md/raid10:%s: %s: rescheduling sector %llu\n",
  332. mdname(conf->mddev),
  333. bdevname(rdev->bdev, b),
  334. (unsigned long long)r10_bio->sector);
  335. set_bit(R10BIO_ReadError, &r10_bio->state);
  336. reschedule_retry(r10_bio);
  337. }
  338. }
  339. static void close_write(struct r10bio *r10_bio)
  340. {
  341. /* clear the bitmap if all writes complete successfully */
  342. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  343. r10_bio->sectors,
  344. !test_bit(R10BIO_Degraded, &r10_bio->state),
  345. 0);
  346. md_write_end(r10_bio->mddev);
  347. }
  348. static void one_write_done(struct r10bio *r10_bio)
  349. {
  350. if (atomic_dec_and_test(&r10_bio->remaining)) {
  351. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  352. reschedule_retry(r10_bio);
  353. else {
  354. close_write(r10_bio);
  355. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  356. reschedule_retry(r10_bio);
  357. else
  358. raid_end_bio_io(r10_bio);
  359. }
  360. }
  361. }
  362. static void raid10_end_write_request(struct bio *bio, int error)
  363. {
  364. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  365. struct r10bio *r10_bio = bio->bi_private;
  366. int dev;
  367. int dec_rdev = 1;
  368. struct r10conf *conf = r10_bio->mddev->private;
  369. int slot, repl;
  370. struct md_rdev *rdev = NULL;
  371. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  372. if (repl)
  373. rdev = conf->mirrors[dev].replacement;
  374. if (!rdev) {
  375. smp_rmb();
  376. repl = 0;
  377. rdev = conf->mirrors[dev].rdev;
  378. }
  379. /*
  380. * this branch is our 'one mirror IO has finished' event handler:
  381. */
  382. if (!uptodate) {
  383. if (repl)
  384. /* Never record new bad blocks to replacement,
  385. * just fail it.
  386. */
  387. md_error(rdev->mddev, rdev);
  388. else {
  389. set_bit(WriteErrorSeen, &rdev->flags);
  390. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  391. set_bit(MD_RECOVERY_NEEDED,
  392. &rdev->mddev->recovery);
  393. set_bit(R10BIO_WriteError, &r10_bio->state);
  394. dec_rdev = 0;
  395. }
  396. } else {
  397. /*
  398. * Set R10BIO_Uptodate in our master bio, so that
  399. * we will return a good error code for to the higher
  400. * levels even if IO on some other mirrored buffer fails.
  401. *
  402. * The 'master' represents the composite IO operation to
  403. * user-side. So if something waits for IO, then it will
  404. * wait for the 'master' bio.
  405. */
  406. sector_t first_bad;
  407. int bad_sectors;
  408. set_bit(R10BIO_Uptodate, &r10_bio->state);
  409. /* Maybe we can clear some bad blocks. */
  410. if (is_badblock(rdev,
  411. r10_bio->devs[slot].addr,
  412. r10_bio->sectors,
  413. &first_bad, &bad_sectors)) {
  414. bio_put(bio);
  415. if (repl)
  416. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  417. else
  418. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  419. dec_rdev = 0;
  420. set_bit(R10BIO_MadeGood, &r10_bio->state);
  421. }
  422. }
  423. /*
  424. *
  425. * Let's see if all mirrored write operations have finished
  426. * already.
  427. */
  428. one_write_done(r10_bio);
  429. if (dec_rdev)
  430. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  431. }
  432. /*
  433. * RAID10 layout manager
  434. * As well as the chunksize and raid_disks count, there are two
  435. * parameters: near_copies and far_copies.
  436. * near_copies * far_copies must be <= raid_disks.
  437. * Normally one of these will be 1.
  438. * If both are 1, we get raid0.
  439. * If near_copies == raid_disks, we get raid1.
  440. *
  441. * Chunks are laid out in raid0 style with near_copies copies of the
  442. * first chunk, followed by near_copies copies of the next chunk and
  443. * so on.
  444. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  445. * as described above, we start again with a device offset of near_copies.
  446. * So we effectively have another copy of the whole array further down all
  447. * the drives, but with blocks on different drives.
  448. * With this layout, and block is never stored twice on the one device.
  449. *
  450. * raid10_find_phys finds the sector offset of a given virtual sector
  451. * on each device that it is on.
  452. *
  453. * raid10_find_virt does the reverse mapping, from a device and a
  454. * sector offset to a virtual address
  455. */
  456. static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
  457. {
  458. int n,f;
  459. sector_t sector;
  460. sector_t chunk;
  461. sector_t stripe;
  462. int dev;
  463. int slot = 0;
  464. /* now calculate first sector/dev */
  465. chunk = r10bio->sector >> geo->chunk_shift;
  466. sector = r10bio->sector & geo->chunk_mask;
  467. chunk *= geo->near_copies;
  468. stripe = chunk;
  469. dev = sector_div(stripe, geo->raid_disks);
  470. if (geo->far_offset)
  471. stripe *= geo->far_copies;
  472. sector += stripe << geo->chunk_shift;
  473. /* and calculate all the others */
  474. for (n = 0; n < geo->near_copies; n++) {
  475. int d = dev;
  476. sector_t s = sector;
  477. r10bio->devs[slot].addr = sector;
  478. r10bio->devs[slot].devnum = d;
  479. slot++;
  480. for (f = 1; f < geo->far_copies; f++) {
  481. d += geo->near_copies;
  482. if (d >= geo->raid_disks)
  483. d -= geo->raid_disks;
  484. s += geo->stride;
  485. r10bio->devs[slot].devnum = d;
  486. r10bio->devs[slot].addr = s;
  487. slot++;
  488. }
  489. dev++;
  490. if (dev >= geo->raid_disks) {
  491. dev = 0;
  492. sector += (geo->chunk_mask + 1);
  493. }
  494. }
  495. }
  496. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  497. {
  498. struct geom *geo = &conf->geo;
  499. if (conf->reshape_progress != MaxSector &&
  500. ((r10bio->sector >= conf->reshape_progress) !=
  501. conf->mddev->reshape_backwards)) {
  502. set_bit(R10BIO_Previous, &r10bio->state);
  503. geo = &conf->prev;
  504. } else
  505. clear_bit(R10BIO_Previous, &r10bio->state);
  506. __raid10_find_phys(geo, r10bio);
  507. }
  508. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  509. {
  510. sector_t offset, chunk, vchunk;
  511. /* Never use conf->prev as this is only called during resync
  512. * or recovery, so reshape isn't happening
  513. */
  514. struct geom *geo = &conf->geo;
  515. offset = sector & geo->chunk_mask;
  516. if (geo->far_offset) {
  517. int fc;
  518. chunk = sector >> geo->chunk_shift;
  519. fc = sector_div(chunk, geo->far_copies);
  520. dev -= fc * geo->near_copies;
  521. if (dev < 0)
  522. dev += geo->raid_disks;
  523. } else {
  524. while (sector >= geo->stride) {
  525. sector -= geo->stride;
  526. if (dev < geo->near_copies)
  527. dev += geo->raid_disks - geo->near_copies;
  528. else
  529. dev -= geo->near_copies;
  530. }
  531. chunk = sector >> geo->chunk_shift;
  532. }
  533. vchunk = chunk * geo->raid_disks + dev;
  534. sector_div(vchunk, geo->near_copies);
  535. return (vchunk << geo->chunk_shift) + offset;
  536. }
  537. /**
  538. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  539. * @q: request queue
  540. * @bvm: properties of new bio
  541. * @biovec: the request that could be merged to it.
  542. *
  543. * Return amount of bytes we can accept at this offset
  544. * This requires checking for end-of-chunk if near_copies != raid_disks,
  545. * and for subordinate merge_bvec_fns if merge_check_needed.
  546. */
  547. static int raid10_mergeable_bvec(struct request_queue *q,
  548. struct bvec_merge_data *bvm,
  549. struct bio_vec *biovec)
  550. {
  551. struct mddev *mddev = q->queuedata;
  552. struct r10conf *conf = mddev->private;
  553. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  554. int max;
  555. unsigned int chunk_sectors = mddev->chunk_sectors;
  556. unsigned int bio_sectors = bvm->bi_size >> 9;
  557. struct geom *geo = &conf->geo;
  558. if (conf->reshape_progress != MaxSector &&
  559. ((sector >= conf->reshape_progress) !=
  560. conf->mddev->reshape_backwards))
  561. geo = &conf->prev;
  562. if (geo->near_copies < geo->raid_disks) {
  563. max = (chunk_sectors - ((sector & (chunk_sectors - 1))
  564. + bio_sectors)) << 9;
  565. if (max < 0)
  566. /* bio_add cannot handle a negative return */
  567. max = 0;
  568. if (max <= biovec->bv_len && bio_sectors == 0)
  569. return biovec->bv_len;
  570. } else
  571. max = biovec->bv_len;
  572. if (mddev->merge_check_needed) {
  573. struct r10bio r10_bio;
  574. int s;
  575. if (conf->reshape_progress != MaxSector) {
  576. /* Cannot give any guidance during reshape */
  577. if (max <= biovec->bv_len && bio_sectors == 0)
  578. return biovec->bv_len;
  579. return 0;
  580. }
  581. r10_bio.sector = sector;
  582. raid10_find_phys(conf, &r10_bio);
  583. rcu_read_lock();
  584. for (s = 0; s < conf->copies; s++) {
  585. int disk = r10_bio.devs[s].devnum;
  586. struct md_rdev *rdev = rcu_dereference(
  587. conf->mirrors[disk].rdev);
  588. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  589. struct request_queue *q =
  590. bdev_get_queue(rdev->bdev);
  591. if (q->merge_bvec_fn) {
  592. bvm->bi_sector = r10_bio.devs[s].addr
  593. + rdev->data_offset;
  594. bvm->bi_bdev = rdev->bdev;
  595. max = min(max, q->merge_bvec_fn(
  596. q, bvm, biovec));
  597. }
  598. }
  599. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  600. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  601. struct request_queue *q =
  602. bdev_get_queue(rdev->bdev);
  603. if (q->merge_bvec_fn) {
  604. bvm->bi_sector = r10_bio.devs[s].addr
  605. + rdev->data_offset;
  606. bvm->bi_bdev = rdev->bdev;
  607. max = min(max, q->merge_bvec_fn(
  608. q, bvm, biovec));
  609. }
  610. }
  611. }
  612. rcu_read_unlock();
  613. }
  614. return max;
  615. }
  616. /*
  617. * This routine returns the disk from which the requested read should
  618. * be done. There is a per-array 'next expected sequential IO' sector
  619. * number - if this matches on the next IO then we use the last disk.
  620. * There is also a per-disk 'last know head position' sector that is
  621. * maintained from IRQ contexts, both the normal and the resync IO
  622. * completion handlers update this position correctly. If there is no
  623. * perfect sequential match then we pick the disk whose head is closest.
  624. *
  625. * If there are 2 mirrors in the same 2 devices, performance degrades
  626. * because position is mirror, not device based.
  627. *
  628. * The rdev for the device selected will have nr_pending incremented.
  629. */
  630. /*
  631. * FIXME: possibly should rethink readbalancing and do it differently
  632. * depending on near_copies / far_copies geometry.
  633. */
  634. static struct md_rdev *read_balance(struct r10conf *conf,
  635. struct r10bio *r10_bio,
  636. int *max_sectors)
  637. {
  638. const sector_t this_sector = r10_bio->sector;
  639. int disk, slot;
  640. int sectors = r10_bio->sectors;
  641. int best_good_sectors;
  642. sector_t new_distance, best_dist;
  643. struct md_rdev *rdev, *best_rdev;
  644. int do_balance;
  645. int best_slot;
  646. struct geom *geo = &conf->geo;
  647. raid10_find_phys(conf, r10_bio);
  648. rcu_read_lock();
  649. retry:
  650. sectors = r10_bio->sectors;
  651. best_slot = -1;
  652. best_rdev = NULL;
  653. best_dist = MaxSector;
  654. best_good_sectors = 0;
  655. do_balance = 1;
  656. /*
  657. * Check if we can balance. We can balance on the whole
  658. * device if no resync is going on (recovery is ok), or below
  659. * the resync window. We take the first readable disk when
  660. * above the resync window.
  661. */
  662. if (conf->mddev->recovery_cp < MaxSector
  663. && (this_sector + sectors >= conf->next_resync))
  664. do_balance = 0;
  665. for (slot = 0; slot < conf->copies ; slot++) {
  666. sector_t first_bad;
  667. int bad_sectors;
  668. sector_t dev_sector;
  669. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  670. continue;
  671. disk = r10_bio->devs[slot].devnum;
  672. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  673. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  674. test_bit(Unmerged, &rdev->flags) ||
  675. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  676. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  677. if (rdev == NULL ||
  678. test_bit(Faulty, &rdev->flags) ||
  679. test_bit(Unmerged, &rdev->flags))
  680. continue;
  681. if (!test_bit(In_sync, &rdev->flags) &&
  682. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  683. continue;
  684. dev_sector = r10_bio->devs[slot].addr;
  685. if (is_badblock(rdev, dev_sector, sectors,
  686. &first_bad, &bad_sectors)) {
  687. if (best_dist < MaxSector)
  688. /* Already have a better slot */
  689. continue;
  690. if (first_bad <= dev_sector) {
  691. /* Cannot read here. If this is the
  692. * 'primary' device, then we must not read
  693. * beyond 'bad_sectors' from another device.
  694. */
  695. bad_sectors -= (dev_sector - first_bad);
  696. if (!do_balance && sectors > bad_sectors)
  697. sectors = bad_sectors;
  698. if (best_good_sectors > sectors)
  699. best_good_sectors = sectors;
  700. } else {
  701. sector_t good_sectors =
  702. first_bad - dev_sector;
  703. if (good_sectors > best_good_sectors) {
  704. best_good_sectors = good_sectors;
  705. best_slot = slot;
  706. best_rdev = rdev;
  707. }
  708. if (!do_balance)
  709. /* Must read from here */
  710. break;
  711. }
  712. continue;
  713. } else
  714. best_good_sectors = sectors;
  715. if (!do_balance)
  716. break;
  717. /* This optimisation is debatable, and completely destroys
  718. * sequential read speed for 'far copies' arrays. So only
  719. * keep it for 'near' arrays, and review those later.
  720. */
  721. if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  722. break;
  723. /* for far > 1 always use the lowest address */
  724. if (geo->far_copies > 1)
  725. new_distance = r10_bio->devs[slot].addr;
  726. else
  727. new_distance = abs(r10_bio->devs[slot].addr -
  728. conf->mirrors[disk].head_position);
  729. if (new_distance < best_dist) {
  730. best_dist = new_distance;
  731. best_slot = slot;
  732. best_rdev = rdev;
  733. }
  734. }
  735. if (slot >= conf->copies) {
  736. slot = best_slot;
  737. rdev = best_rdev;
  738. }
  739. if (slot >= 0) {
  740. atomic_inc(&rdev->nr_pending);
  741. if (test_bit(Faulty, &rdev->flags)) {
  742. /* Cannot risk returning a device that failed
  743. * before we inc'ed nr_pending
  744. */
  745. rdev_dec_pending(rdev, conf->mddev);
  746. goto retry;
  747. }
  748. r10_bio->read_slot = slot;
  749. } else
  750. rdev = NULL;
  751. rcu_read_unlock();
  752. *max_sectors = best_good_sectors;
  753. return rdev;
  754. }
  755. static int raid10_congested(void *data, int bits)
  756. {
  757. struct mddev *mddev = data;
  758. struct r10conf *conf = mddev->private;
  759. int i, ret = 0;
  760. if ((bits & (1 << BDI_async_congested)) &&
  761. conf->pending_count >= max_queued_requests)
  762. return 1;
  763. if (mddev_congested(mddev, bits))
  764. return 1;
  765. rcu_read_lock();
  766. for (i = 0;
  767. (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
  768. && ret == 0;
  769. i++) {
  770. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  771. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  772. struct request_queue *q = bdev_get_queue(rdev->bdev);
  773. ret |= bdi_congested(&q->backing_dev_info, bits);
  774. }
  775. }
  776. rcu_read_unlock();
  777. return ret;
  778. }
  779. static void flush_pending_writes(struct r10conf *conf)
  780. {
  781. /* Any writes that have been queued but are awaiting
  782. * bitmap updates get flushed here.
  783. */
  784. spin_lock_irq(&conf->device_lock);
  785. if (conf->pending_bio_list.head) {
  786. struct bio *bio;
  787. bio = bio_list_get(&conf->pending_bio_list);
  788. conf->pending_count = 0;
  789. spin_unlock_irq(&conf->device_lock);
  790. /* flush any pending bitmap writes to disk
  791. * before proceeding w/ I/O */
  792. bitmap_unplug(conf->mddev->bitmap);
  793. wake_up(&conf->wait_barrier);
  794. while (bio) { /* submit pending writes */
  795. struct bio *next = bio->bi_next;
  796. bio->bi_next = NULL;
  797. generic_make_request(bio);
  798. bio = next;
  799. }
  800. } else
  801. spin_unlock_irq(&conf->device_lock);
  802. }
  803. /* Barriers....
  804. * Sometimes we need to suspend IO while we do something else,
  805. * either some resync/recovery, or reconfigure the array.
  806. * To do this we raise a 'barrier'.
  807. * The 'barrier' is a counter that can be raised multiple times
  808. * to count how many activities are happening which preclude
  809. * normal IO.
  810. * We can only raise the barrier if there is no pending IO.
  811. * i.e. if nr_pending == 0.
  812. * We choose only to raise the barrier if no-one is waiting for the
  813. * barrier to go down. This means that as soon as an IO request
  814. * is ready, no other operations which require a barrier will start
  815. * until the IO request has had a chance.
  816. *
  817. * So: regular IO calls 'wait_barrier'. When that returns there
  818. * is no backgroup IO happening, It must arrange to call
  819. * allow_barrier when it has finished its IO.
  820. * backgroup IO calls must call raise_barrier. Once that returns
  821. * there is no normal IO happeing. It must arrange to call
  822. * lower_barrier when the particular background IO completes.
  823. */
  824. static void raise_barrier(struct r10conf *conf, int force)
  825. {
  826. BUG_ON(force && !conf->barrier);
  827. spin_lock_irq(&conf->resync_lock);
  828. /* Wait until no block IO is waiting (unless 'force') */
  829. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  830. conf->resync_lock, );
  831. /* block any new IO from starting */
  832. conf->barrier++;
  833. /* Now wait for all pending IO to complete */
  834. wait_event_lock_irq(conf->wait_barrier,
  835. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  836. conf->resync_lock, );
  837. spin_unlock_irq(&conf->resync_lock);
  838. }
  839. static void lower_barrier(struct r10conf *conf)
  840. {
  841. unsigned long flags;
  842. spin_lock_irqsave(&conf->resync_lock, flags);
  843. conf->barrier--;
  844. spin_unlock_irqrestore(&conf->resync_lock, flags);
  845. wake_up(&conf->wait_barrier);
  846. }
  847. static void wait_barrier(struct r10conf *conf)
  848. {
  849. spin_lock_irq(&conf->resync_lock);
  850. if (conf->barrier) {
  851. conf->nr_waiting++;
  852. /* Wait for the barrier to drop.
  853. * However if there are already pending
  854. * requests (preventing the barrier from
  855. * rising completely), and the
  856. * pre-process bio queue isn't empty,
  857. * then don't wait, as we need to empty
  858. * that queue to get the nr_pending
  859. * count down.
  860. */
  861. wait_event_lock_irq(conf->wait_barrier,
  862. !conf->barrier ||
  863. (conf->nr_pending &&
  864. current->bio_list &&
  865. !bio_list_empty(current->bio_list)),
  866. conf->resync_lock,
  867. );
  868. conf->nr_waiting--;
  869. }
  870. conf->nr_pending++;
  871. spin_unlock_irq(&conf->resync_lock);
  872. }
  873. static void allow_barrier(struct r10conf *conf)
  874. {
  875. unsigned long flags;
  876. spin_lock_irqsave(&conf->resync_lock, flags);
  877. conf->nr_pending--;
  878. spin_unlock_irqrestore(&conf->resync_lock, flags);
  879. wake_up(&conf->wait_barrier);
  880. }
  881. static void freeze_array(struct r10conf *conf)
  882. {
  883. /* stop syncio and normal IO and wait for everything to
  884. * go quiet.
  885. * We increment barrier and nr_waiting, and then
  886. * wait until nr_pending match nr_queued+1
  887. * This is called in the context of one normal IO request
  888. * that has failed. Thus any sync request that might be pending
  889. * will be blocked by nr_pending, and we need to wait for
  890. * pending IO requests to complete or be queued for re-try.
  891. * Thus the number queued (nr_queued) plus this request (1)
  892. * must match the number of pending IOs (nr_pending) before
  893. * we continue.
  894. */
  895. spin_lock_irq(&conf->resync_lock);
  896. conf->barrier++;
  897. conf->nr_waiting++;
  898. wait_event_lock_irq(conf->wait_barrier,
  899. conf->nr_pending == conf->nr_queued+1,
  900. conf->resync_lock,
  901. flush_pending_writes(conf));
  902. spin_unlock_irq(&conf->resync_lock);
  903. }
  904. static void unfreeze_array(struct r10conf *conf)
  905. {
  906. /* reverse the effect of the freeze */
  907. spin_lock_irq(&conf->resync_lock);
  908. conf->barrier--;
  909. conf->nr_waiting--;
  910. wake_up(&conf->wait_barrier);
  911. spin_unlock_irq(&conf->resync_lock);
  912. }
  913. static sector_t choose_data_offset(struct r10bio *r10_bio,
  914. struct md_rdev *rdev)
  915. {
  916. if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
  917. test_bit(R10BIO_Previous, &r10_bio->state))
  918. return rdev->data_offset;
  919. else
  920. return rdev->new_data_offset;
  921. }
  922. static void make_request(struct mddev *mddev, struct bio * bio)
  923. {
  924. struct r10conf *conf = mddev->private;
  925. struct r10bio *r10_bio;
  926. struct bio *read_bio;
  927. int i;
  928. sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
  929. int chunk_sects = chunk_mask + 1;
  930. const int rw = bio_data_dir(bio);
  931. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  932. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  933. unsigned long flags;
  934. struct md_rdev *blocked_rdev;
  935. int plugged;
  936. int sectors_handled;
  937. int max_sectors;
  938. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  939. md_flush_request(mddev, bio);
  940. return;
  941. }
  942. /* If this request crosses a chunk boundary, we need to
  943. * split it. This will only happen for 1 PAGE (or less) requests.
  944. */
  945. if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
  946. > chunk_sects
  947. && (conf->geo.near_copies < conf->geo.raid_disks
  948. || conf->prev.near_copies < conf->prev.raid_disks))) {
  949. struct bio_pair *bp;
  950. /* Sanity check -- queue functions should prevent this happening */
  951. if (bio->bi_vcnt != 1 ||
  952. bio->bi_idx != 0)
  953. goto bad_map;
  954. /* This is a one page bio that upper layers
  955. * refuse to split for us, so we need to split it.
  956. */
  957. bp = bio_split(bio,
  958. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  959. /* Each of these 'make_request' calls will call 'wait_barrier'.
  960. * If the first succeeds but the second blocks due to the resync
  961. * thread raising the barrier, we will deadlock because the
  962. * IO to the underlying device will be queued in generic_make_request
  963. * and will never complete, so will never reduce nr_pending.
  964. * So increment nr_waiting here so no new raise_barriers will
  965. * succeed, and so the second wait_barrier cannot block.
  966. */
  967. spin_lock_irq(&conf->resync_lock);
  968. conf->nr_waiting++;
  969. spin_unlock_irq(&conf->resync_lock);
  970. make_request(mddev, &bp->bio1);
  971. make_request(mddev, &bp->bio2);
  972. spin_lock_irq(&conf->resync_lock);
  973. conf->nr_waiting--;
  974. wake_up(&conf->wait_barrier);
  975. spin_unlock_irq(&conf->resync_lock);
  976. bio_pair_release(bp);
  977. return;
  978. bad_map:
  979. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  980. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  981. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  982. bio_io_error(bio);
  983. return;
  984. }
  985. md_write_start(mddev, bio);
  986. /*
  987. * Register the new request and wait if the reconstruction
  988. * thread has put up a bar for new requests.
  989. * Continue immediately if no resync is active currently.
  990. */
  991. wait_barrier(conf);
  992. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  993. r10_bio->master_bio = bio;
  994. r10_bio->sectors = bio->bi_size >> 9;
  995. r10_bio->mddev = mddev;
  996. r10_bio->sector = bio->bi_sector;
  997. r10_bio->state = 0;
  998. /* We might need to issue multiple reads to different
  999. * devices if there are bad blocks around, so we keep
  1000. * track of the number of reads in bio->bi_phys_segments.
  1001. * If this is 0, there is only one r10_bio and no locking
  1002. * will be needed when the request completes. If it is
  1003. * non-zero, then it is the number of not-completed requests.
  1004. */
  1005. bio->bi_phys_segments = 0;
  1006. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1007. if (rw == READ) {
  1008. /*
  1009. * read balancing logic:
  1010. */
  1011. struct md_rdev *rdev;
  1012. int slot;
  1013. read_again:
  1014. rdev = read_balance(conf, r10_bio, &max_sectors);
  1015. if (!rdev) {
  1016. raid_end_bio_io(r10_bio);
  1017. return;
  1018. }
  1019. slot = r10_bio->read_slot;
  1020. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1021. md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
  1022. max_sectors);
  1023. r10_bio->devs[slot].bio = read_bio;
  1024. r10_bio->devs[slot].rdev = rdev;
  1025. read_bio->bi_sector = r10_bio->devs[slot].addr +
  1026. choose_data_offset(r10_bio, rdev);
  1027. read_bio->bi_bdev = rdev->bdev;
  1028. read_bio->bi_end_io = raid10_end_read_request;
  1029. read_bio->bi_rw = READ | do_sync;
  1030. read_bio->bi_private = r10_bio;
  1031. if (max_sectors < r10_bio->sectors) {
  1032. /* Could not read all from this device, so we will
  1033. * need another r10_bio.
  1034. */
  1035. sectors_handled = (r10_bio->sectors + max_sectors
  1036. - bio->bi_sector);
  1037. r10_bio->sectors = max_sectors;
  1038. spin_lock_irq(&conf->device_lock);
  1039. if (bio->bi_phys_segments == 0)
  1040. bio->bi_phys_segments = 2;
  1041. else
  1042. bio->bi_phys_segments++;
  1043. spin_unlock(&conf->device_lock);
  1044. /* Cannot call generic_make_request directly
  1045. * as that will be queued in __generic_make_request
  1046. * and subsequent mempool_alloc might block
  1047. * waiting for it. so hand bio over to raid10d.
  1048. */
  1049. reschedule_retry(r10_bio);
  1050. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1051. r10_bio->master_bio = bio;
  1052. r10_bio->sectors = ((bio->bi_size >> 9)
  1053. - sectors_handled);
  1054. r10_bio->state = 0;
  1055. r10_bio->mddev = mddev;
  1056. r10_bio->sector = bio->bi_sector + sectors_handled;
  1057. goto read_again;
  1058. } else
  1059. generic_make_request(read_bio);
  1060. return;
  1061. }
  1062. /*
  1063. * WRITE:
  1064. */
  1065. if (conf->pending_count >= max_queued_requests) {
  1066. md_wakeup_thread(mddev->thread);
  1067. wait_event(conf->wait_barrier,
  1068. conf->pending_count < max_queued_requests);
  1069. }
  1070. /* first select target devices under rcu_lock and
  1071. * inc refcount on their rdev. Record them by setting
  1072. * bios[x] to bio
  1073. * If there are known/acknowledged bad blocks on any device
  1074. * on which we have seen a write error, we want to avoid
  1075. * writing to those blocks. This potentially requires several
  1076. * writes to write around the bad blocks. Each set of writes
  1077. * gets its own r10_bio with a set of bios attached. The number
  1078. * of r10_bios is recored in bio->bi_phys_segments just as with
  1079. * the read case.
  1080. */
  1081. plugged = mddev_check_plugged(mddev);
  1082. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  1083. raid10_find_phys(conf, r10_bio);
  1084. retry_write:
  1085. blocked_rdev = NULL;
  1086. rcu_read_lock();
  1087. max_sectors = r10_bio->sectors;
  1088. for (i = 0; i < conf->copies; i++) {
  1089. int d = r10_bio->devs[i].devnum;
  1090. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  1091. struct md_rdev *rrdev = rcu_dereference(
  1092. conf->mirrors[d].replacement);
  1093. if (rdev == rrdev)
  1094. rrdev = NULL;
  1095. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1096. atomic_inc(&rdev->nr_pending);
  1097. blocked_rdev = rdev;
  1098. break;
  1099. }
  1100. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  1101. atomic_inc(&rrdev->nr_pending);
  1102. blocked_rdev = rrdev;
  1103. break;
  1104. }
  1105. if (rrdev && (test_bit(Faulty, &rrdev->flags)
  1106. || test_bit(Unmerged, &rrdev->flags)))
  1107. rrdev = NULL;
  1108. r10_bio->devs[i].bio = NULL;
  1109. r10_bio->devs[i].repl_bio = NULL;
  1110. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  1111. test_bit(Unmerged, &rdev->flags)) {
  1112. set_bit(R10BIO_Degraded, &r10_bio->state);
  1113. continue;
  1114. }
  1115. if (test_bit(WriteErrorSeen, &rdev->flags)) {
  1116. sector_t first_bad;
  1117. sector_t dev_sector = r10_bio->devs[i].addr;
  1118. int bad_sectors;
  1119. int is_bad;
  1120. is_bad = is_badblock(rdev, dev_sector,
  1121. max_sectors,
  1122. &first_bad, &bad_sectors);
  1123. if (is_bad < 0) {
  1124. /* Mustn't write here until the bad block
  1125. * is acknowledged
  1126. */
  1127. atomic_inc(&rdev->nr_pending);
  1128. set_bit(BlockedBadBlocks, &rdev->flags);
  1129. blocked_rdev = rdev;
  1130. break;
  1131. }
  1132. if (is_bad && first_bad <= dev_sector) {
  1133. /* Cannot write here at all */
  1134. bad_sectors -= (dev_sector - first_bad);
  1135. if (bad_sectors < max_sectors)
  1136. /* Mustn't write more than bad_sectors
  1137. * to other devices yet
  1138. */
  1139. max_sectors = bad_sectors;
  1140. /* We don't set R10BIO_Degraded as that
  1141. * only applies if the disk is missing,
  1142. * so it might be re-added, and we want to
  1143. * know to recover this chunk.
  1144. * In this case the device is here, and the
  1145. * fact that this chunk is not in-sync is
  1146. * recorded in the bad block log.
  1147. */
  1148. continue;
  1149. }
  1150. if (is_bad) {
  1151. int good_sectors = first_bad - dev_sector;
  1152. if (good_sectors < max_sectors)
  1153. max_sectors = good_sectors;
  1154. }
  1155. }
  1156. r10_bio->devs[i].bio = bio;
  1157. atomic_inc(&rdev->nr_pending);
  1158. if (rrdev) {
  1159. r10_bio->devs[i].repl_bio = bio;
  1160. atomic_inc(&rrdev->nr_pending);
  1161. }
  1162. }
  1163. rcu_read_unlock();
  1164. if (unlikely(blocked_rdev)) {
  1165. /* Have to wait for this device to get unblocked, then retry */
  1166. int j;
  1167. int d;
  1168. for (j = 0; j < i; j++) {
  1169. if (r10_bio->devs[j].bio) {
  1170. d = r10_bio->devs[j].devnum;
  1171. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1172. }
  1173. if (r10_bio->devs[j].repl_bio) {
  1174. struct md_rdev *rdev;
  1175. d = r10_bio->devs[j].devnum;
  1176. rdev = conf->mirrors[d].replacement;
  1177. if (!rdev) {
  1178. /* Race with remove_disk */
  1179. smp_mb();
  1180. rdev = conf->mirrors[d].rdev;
  1181. }
  1182. rdev_dec_pending(rdev, mddev);
  1183. }
  1184. }
  1185. allow_barrier(conf);
  1186. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1187. wait_barrier(conf);
  1188. goto retry_write;
  1189. }
  1190. if (max_sectors < r10_bio->sectors) {
  1191. /* We are splitting this into multiple parts, so
  1192. * we need to prepare for allocating another r10_bio.
  1193. */
  1194. r10_bio->sectors = max_sectors;
  1195. spin_lock_irq(&conf->device_lock);
  1196. if (bio->bi_phys_segments == 0)
  1197. bio->bi_phys_segments = 2;
  1198. else
  1199. bio->bi_phys_segments++;
  1200. spin_unlock_irq(&conf->device_lock);
  1201. }
  1202. sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
  1203. atomic_set(&r10_bio->remaining, 1);
  1204. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1205. for (i = 0; i < conf->copies; i++) {
  1206. struct bio *mbio;
  1207. int d = r10_bio->devs[i].devnum;
  1208. if (!r10_bio->devs[i].bio)
  1209. continue;
  1210. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1211. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1212. max_sectors);
  1213. r10_bio->devs[i].bio = mbio;
  1214. mbio->bi_sector = (r10_bio->devs[i].addr+
  1215. choose_data_offset(r10_bio,
  1216. conf->mirrors[d].rdev));
  1217. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1218. mbio->bi_end_io = raid10_end_write_request;
  1219. mbio->bi_rw = WRITE | do_sync | do_fua;
  1220. mbio->bi_private = r10_bio;
  1221. atomic_inc(&r10_bio->remaining);
  1222. spin_lock_irqsave(&conf->device_lock, flags);
  1223. bio_list_add(&conf->pending_bio_list, mbio);
  1224. conf->pending_count++;
  1225. spin_unlock_irqrestore(&conf->device_lock, flags);
  1226. if (!r10_bio->devs[i].repl_bio)
  1227. continue;
  1228. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1229. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1230. max_sectors);
  1231. r10_bio->devs[i].repl_bio = mbio;
  1232. /* We are actively writing to the original device
  1233. * so it cannot disappear, so the replacement cannot
  1234. * become NULL here
  1235. */
  1236. mbio->bi_sector = (r10_bio->devs[i].addr +
  1237. choose_data_offset(
  1238. r10_bio,
  1239. conf->mirrors[d].replacement));
  1240. mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
  1241. mbio->bi_end_io = raid10_end_write_request;
  1242. mbio->bi_rw = WRITE | do_sync | do_fua;
  1243. mbio->bi_private = r10_bio;
  1244. atomic_inc(&r10_bio->remaining);
  1245. spin_lock_irqsave(&conf->device_lock, flags);
  1246. bio_list_add(&conf->pending_bio_list, mbio);
  1247. conf->pending_count++;
  1248. spin_unlock_irqrestore(&conf->device_lock, flags);
  1249. }
  1250. /* Don't remove the bias on 'remaining' (one_write_done) until
  1251. * after checking if we need to go around again.
  1252. */
  1253. if (sectors_handled < (bio->bi_size >> 9)) {
  1254. one_write_done(r10_bio);
  1255. /* We need another r10_bio. It has already been counted
  1256. * in bio->bi_phys_segments.
  1257. */
  1258. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1259. r10_bio->master_bio = bio;
  1260. r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  1261. r10_bio->mddev = mddev;
  1262. r10_bio->sector = bio->bi_sector + sectors_handled;
  1263. r10_bio->state = 0;
  1264. goto retry_write;
  1265. }
  1266. one_write_done(r10_bio);
  1267. /* In case raid10d snuck in to freeze_array */
  1268. wake_up(&conf->wait_barrier);
  1269. if (do_sync || !mddev->bitmap || !plugged)
  1270. md_wakeup_thread(mddev->thread);
  1271. }
  1272. static void status(struct seq_file *seq, struct mddev *mddev)
  1273. {
  1274. struct r10conf *conf = mddev->private;
  1275. int i;
  1276. if (conf->geo.near_copies < conf->geo.raid_disks)
  1277. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1278. if (conf->geo.near_copies > 1)
  1279. seq_printf(seq, " %d near-copies", conf->geo.near_copies);
  1280. if (conf->geo.far_copies > 1) {
  1281. if (conf->geo.far_offset)
  1282. seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
  1283. else
  1284. seq_printf(seq, " %d far-copies", conf->geo.far_copies);
  1285. }
  1286. seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
  1287. conf->geo.raid_disks - mddev->degraded);
  1288. for (i = 0; i < conf->geo.raid_disks; i++)
  1289. seq_printf(seq, "%s",
  1290. conf->mirrors[i].rdev &&
  1291. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1292. seq_printf(seq, "]");
  1293. }
  1294. /* check if there are enough drives for
  1295. * every block to appear on atleast one.
  1296. * Don't consider the device numbered 'ignore'
  1297. * as we might be about to remove it.
  1298. */
  1299. static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
  1300. {
  1301. int first = 0;
  1302. do {
  1303. int n = conf->copies;
  1304. int cnt = 0;
  1305. while (n--) {
  1306. if (conf->mirrors[first].rdev &&
  1307. first != ignore)
  1308. cnt++;
  1309. first = (first+1) % geo->raid_disks;
  1310. }
  1311. if (cnt == 0)
  1312. return 0;
  1313. } while (first != 0);
  1314. return 1;
  1315. }
  1316. static int enough(struct r10conf *conf, int ignore)
  1317. {
  1318. return _enough(conf, &conf->geo, ignore) &&
  1319. _enough(conf, &conf->prev, ignore);
  1320. }
  1321. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1322. {
  1323. char b[BDEVNAME_SIZE];
  1324. struct r10conf *conf = mddev->private;
  1325. /*
  1326. * If it is not operational, then we have already marked it as dead
  1327. * else if it is the last working disks, ignore the error, let the
  1328. * next level up know.
  1329. * else mark the drive as failed
  1330. */
  1331. if (test_bit(In_sync, &rdev->flags)
  1332. && !enough(conf, rdev->raid_disk))
  1333. /*
  1334. * Don't fail the drive, just return an IO error.
  1335. */
  1336. return;
  1337. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1338. unsigned long flags;
  1339. spin_lock_irqsave(&conf->device_lock, flags);
  1340. mddev->degraded++;
  1341. spin_unlock_irqrestore(&conf->device_lock, flags);
  1342. /*
  1343. * if recovery is running, make sure it aborts.
  1344. */
  1345. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1346. }
  1347. set_bit(Blocked, &rdev->flags);
  1348. set_bit(Faulty, &rdev->flags);
  1349. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1350. printk(KERN_ALERT
  1351. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1352. "md/raid10:%s: Operation continuing on %d devices.\n",
  1353. mdname(mddev), bdevname(rdev->bdev, b),
  1354. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1355. }
  1356. static void print_conf(struct r10conf *conf)
  1357. {
  1358. int i;
  1359. struct mirror_info *tmp;
  1360. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1361. if (!conf) {
  1362. printk(KERN_DEBUG "(!conf)\n");
  1363. return;
  1364. }
  1365. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1366. conf->geo.raid_disks);
  1367. for (i = 0; i < conf->geo.raid_disks; i++) {
  1368. char b[BDEVNAME_SIZE];
  1369. tmp = conf->mirrors + i;
  1370. if (tmp->rdev)
  1371. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1372. i, !test_bit(In_sync, &tmp->rdev->flags),
  1373. !test_bit(Faulty, &tmp->rdev->flags),
  1374. bdevname(tmp->rdev->bdev,b));
  1375. }
  1376. }
  1377. static void close_sync(struct r10conf *conf)
  1378. {
  1379. wait_barrier(conf);
  1380. allow_barrier(conf);
  1381. mempool_destroy(conf->r10buf_pool);
  1382. conf->r10buf_pool = NULL;
  1383. }
  1384. static int raid10_spare_active(struct mddev *mddev)
  1385. {
  1386. int i;
  1387. struct r10conf *conf = mddev->private;
  1388. struct mirror_info *tmp;
  1389. int count = 0;
  1390. unsigned long flags;
  1391. /*
  1392. * Find all non-in_sync disks within the RAID10 configuration
  1393. * and mark them in_sync
  1394. */
  1395. for (i = 0; i < conf->geo.raid_disks; i++) {
  1396. tmp = conf->mirrors + i;
  1397. if (tmp->replacement
  1398. && tmp->replacement->recovery_offset == MaxSector
  1399. && !test_bit(Faulty, &tmp->replacement->flags)
  1400. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1401. /* Replacement has just become active */
  1402. if (!tmp->rdev
  1403. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1404. count++;
  1405. if (tmp->rdev) {
  1406. /* Replaced device not technically faulty,
  1407. * but we need to be sure it gets removed
  1408. * and never re-added.
  1409. */
  1410. set_bit(Faulty, &tmp->rdev->flags);
  1411. sysfs_notify_dirent_safe(
  1412. tmp->rdev->sysfs_state);
  1413. }
  1414. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1415. } else if (tmp->rdev
  1416. && !test_bit(Faulty, &tmp->rdev->flags)
  1417. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1418. count++;
  1419. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  1420. }
  1421. }
  1422. spin_lock_irqsave(&conf->device_lock, flags);
  1423. mddev->degraded -= count;
  1424. spin_unlock_irqrestore(&conf->device_lock, flags);
  1425. print_conf(conf);
  1426. return count;
  1427. }
  1428. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1429. {
  1430. struct r10conf *conf = mddev->private;
  1431. int err = -EEXIST;
  1432. int mirror;
  1433. int first = 0;
  1434. int last = conf->geo.raid_disks - 1;
  1435. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1436. if (mddev->recovery_cp < MaxSector)
  1437. /* only hot-add to in-sync arrays, as recovery is
  1438. * very different from resync
  1439. */
  1440. return -EBUSY;
  1441. if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
  1442. return -EINVAL;
  1443. if (rdev->raid_disk >= 0)
  1444. first = last = rdev->raid_disk;
  1445. if (q->merge_bvec_fn) {
  1446. set_bit(Unmerged, &rdev->flags);
  1447. mddev->merge_check_needed = 1;
  1448. }
  1449. if (rdev->saved_raid_disk >= first &&
  1450. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1451. mirror = rdev->saved_raid_disk;
  1452. else
  1453. mirror = first;
  1454. for ( ; mirror <= last ; mirror++) {
  1455. struct mirror_info *p = &conf->mirrors[mirror];
  1456. if (p->recovery_disabled == mddev->recovery_disabled)
  1457. continue;
  1458. if (p->rdev) {
  1459. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1460. p->replacement != NULL)
  1461. continue;
  1462. clear_bit(In_sync, &rdev->flags);
  1463. set_bit(Replacement, &rdev->flags);
  1464. rdev->raid_disk = mirror;
  1465. err = 0;
  1466. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1467. rdev->data_offset << 9);
  1468. conf->fullsync = 1;
  1469. rcu_assign_pointer(p->replacement, rdev);
  1470. break;
  1471. }
  1472. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1473. rdev->data_offset << 9);
  1474. p->head_position = 0;
  1475. p->recovery_disabled = mddev->recovery_disabled - 1;
  1476. rdev->raid_disk = mirror;
  1477. err = 0;
  1478. if (rdev->saved_raid_disk != mirror)
  1479. conf->fullsync = 1;
  1480. rcu_assign_pointer(p->rdev, rdev);
  1481. break;
  1482. }
  1483. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1484. /* Some requests might not have seen this new
  1485. * merge_bvec_fn. We must wait for them to complete
  1486. * before merging the device fully.
  1487. * First we make sure any code which has tested
  1488. * our function has submitted the request, then
  1489. * we wait for all outstanding requests to complete.
  1490. */
  1491. synchronize_sched();
  1492. raise_barrier(conf, 0);
  1493. lower_barrier(conf);
  1494. clear_bit(Unmerged, &rdev->flags);
  1495. }
  1496. md_integrity_add_rdev(rdev, mddev);
  1497. print_conf(conf);
  1498. return err;
  1499. }
  1500. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1501. {
  1502. struct r10conf *conf = mddev->private;
  1503. int err = 0;
  1504. int number = rdev->raid_disk;
  1505. struct md_rdev **rdevp;
  1506. struct mirror_info *p = conf->mirrors + number;
  1507. print_conf(conf);
  1508. if (rdev == p->rdev)
  1509. rdevp = &p->rdev;
  1510. else if (rdev == p->replacement)
  1511. rdevp = &p->replacement;
  1512. else
  1513. return 0;
  1514. if (test_bit(In_sync, &rdev->flags) ||
  1515. atomic_read(&rdev->nr_pending)) {
  1516. err = -EBUSY;
  1517. goto abort;
  1518. }
  1519. /* Only remove faulty devices if recovery
  1520. * is not possible.
  1521. */
  1522. if (!test_bit(Faulty, &rdev->flags) &&
  1523. mddev->recovery_disabled != p->recovery_disabled &&
  1524. (!p->replacement || p->replacement == rdev) &&
  1525. enough(conf, -1)) {
  1526. err = -EBUSY;
  1527. goto abort;
  1528. }
  1529. *rdevp = NULL;
  1530. synchronize_rcu();
  1531. if (atomic_read(&rdev->nr_pending)) {
  1532. /* lost the race, try later */
  1533. err = -EBUSY;
  1534. *rdevp = rdev;
  1535. goto abort;
  1536. } else if (p->replacement) {
  1537. /* We must have just cleared 'rdev' */
  1538. p->rdev = p->replacement;
  1539. clear_bit(Replacement, &p->replacement->flags);
  1540. smp_mb(); /* Make sure other CPUs may see both as identical
  1541. * but will never see neither -- if they are careful.
  1542. */
  1543. p->replacement = NULL;
  1544. clear_bit(WantReplacement, &rdev->flags);
  1545. } else
  1546. /* We might have just remove the Replacement as faulty
  1547. * Clear the flag just in case
  1548. */
  1549. clear_bit(WantReplacement, &rdev->flags);
  1550. err = md_integrity_register(mddev);
  1551. abort:
  1552. print_conf(conf);
  1553. return err;
  1554. }
  1555. static void end_sync_read(struct bio *bio, int error)
  1556. {
  1557. struct r10bio *r10_bio = bio->bi_private;
  1558. struct r10conf *conf = r10_bio->mddev->private;
  1559. int d;
  1560. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1561. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1562. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1563. else
  1564. /* The write handler will notice the lack of
  1565. * R10BIO_Uptodate and record any errors etc
  1566. */
  1567. atomic_add(r10_bio->sectors,
  1568. &conf->mirrors[d].rdev->corrected_errors);
  1569. /* for reconstruct, we always reschedule after a read.
  1570. * for resync, only after all reads
  1571. */
  1572. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1573. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1574. atomic_dec_and_test(&r10_bio->remaining)) {
  1575. /* we have read all the blocks,
  1576. * do the comparison in process context in raid10d
  1577. */
  1578. reschedule_retry(r10_bio);
  1579. }
  1580. }
  1581. static void end_sync_request(struct r10bio *r10_bio)
  1582. {
  1583. struct mddev *mddev = r10_bio->mddev;
  1584. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1585. if (r10_bio->master_bio == NULL) {
  1586. /* the primary of several recovery bios */
  1587. sector_t s = r10_bio->sectors;
  1588. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1589. test_bit(R10BIO_WriteError, &r10_bio->state))
  1590. reschedule_retry(r10_bio);
  1591. else
  1592. put_buf(r10_bio);
  1593. md_done_sync(mddev, s, 1);
  1594. break;
  1595. } else {
  1596. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1597. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1598. test_bit(R10BIO_WriteError, &r10_bio->state))
  1599. reschedule_retry(r10_bio);
  1600. else
  1601. put_buf(r10_bio);
  1602. r10_bio = r10_bio2;
  1603. }
  1604. }
  1605. }
  1606. static void end_sync_write(struct bio *bio, int error)
  1607. {
  1608. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1609. struct r10bio *r10_bio = bio->bi_private;
  1610. struct mddev *mddev = r10_bio->mddev;
  1611. struct r10conf *conf = mddev->private;
  1612. int d;
  1613. sector_t first_bad;
  1614. int bad_sectors;
  1615. int slot;
  1616. int repl;
  1617. struct md_rdev *rdev = NULL;
  1618. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1619. if (repl)
  1620. rdev = conf->mirrors[d].replacement;
  1621. else
  1622. rdev = conf->mirrors[d].rdev;
  1623. if (!uptodate) {
  1624. if (repl)
  1625. md_error(mddev, rdev);
  1626. else {
  1627. set_bit(WriteErrorSeen, &rdev->flags);
  1628. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1629. set_bit(MD_RECOVERY_NEEDED,
  1630. &rdev->mddev->recovery);
  1631. set_bit(R10BIO_WriteError, &r10_bio->state);
  1632. }
  1633. } else if (is_badblock(rdev,
  1634. r10_bio->devs[slot].addr,
  1635. r10_bio->sectors,
  1636. &first_bad, &bad_sectors))
  1637. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1638. rdev_dec_pending(rdev, mddev);
  1639. end_sync_request(r10_bio);
  1640. }
  1641. /*
  1642. * Note: sync and recover and handled very differently for raid10
  1643. * This code is for resync.
  1644. * For resync, we read through virtual addresses and read all blocks.
  1645. * If there is any error, we schedule a write. The lowest numbered
  1646. * drive is authoritative.
  1647. * However requests come for physical address, so we need to map.
  1648. * For every physical address there are raid_disks/copies virtual addresses,
  1649. * which is always are least one, but is not necessarly an integer.
  1650. * This means that a physical address can span multiple chunks, so we may
  1651. * have to submit multiple io requests for a single sync request.
  1652. */
  1653. /*
  1654. * We check if all blocks are in-sync and only write to blocks that
  1655. * aren't in sync
  1656. */
  1657. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1658. {
  1659. struct r10conf *conf = mddev->private;
  1660. int i, first;
  1661. struct bio *tbio, *fbio;
  1662. int vcnt;
  1663. atomic_set(&r10_bio->remaining, 1);
  1664. /* find the first device with a block */
  1665. for (i=0; i<conf->copies; i++)
  1666. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1667. break;
  1668. if (i == conf->copies)
  1669. goto done;
  1670. first = i;
  1671. fbio = r10_bio->devs[i].bio;
  1672. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1673. /* now find blocks with errors */
  1674. for (i=0 ; i < conf->copies ; i++) {
  1675. int j, d;
  1676. tbio = r10_bio->devs[i].bio;
  1677. if (tbio->bi_end_io != end_sync_read)
  1678. continue;
  1679. if (i == first)
  1680. continue;
  1681. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1682. /* We know that the bi_io_vec layout is the same for
  1683. * both 'first' and 'i', so we just compare them.
  1684. * All vec entries are PAGE_SIZE;
  1685. */
  1686. for (j = 0; j < vcnt; j++)
  1687. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1688. page_address(tbio->bi_io_vec[j].bv_page),
  1689. fbio->bi_io_vec[j].bv_len))
  1690. break;
  1691. if (j == vcnt)
  1692. continue;
  1693. mddev->resync_mismatches += r10_bio->sectors;
  1694. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1695. /* Don't fix anything. */
  1696. continue;
  1697. }
  1698. /* Ok, we need to write this bio, either to correct an
  1699. * inconsistency or to correct an unreadable block.
  1700. * First we need to fixup bv_offset, bv_len and
  1701. * bi_vecs, as the read request might have corrupted these
  1702. */
  1703. tbio->bi_vcnt = vcnt;
  1704. tbio->bi_size = r10_bio->sectors << 9;
  1705. tbio->bi_idx = 0;
  1706. tbio->bi_phys_segments = 0;
  1707. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1708. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1709. tbio->bi_next = NULL;
  1710. tbio->bi_rw = WRITE;
  1711. tbio->bi_private = r10_bio;
  1712. tbio->bi_sector = r10_bio->devs[i].addr;
  1713. for (j=0; j < vcnt ; j++) {
  1714. tbio->bi_io_vec[j].bv_offset = 0;
  1715. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1716. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1717. page_address(fbio->bi_io_vec[j].bv_page),
  1718. PAGE_SIZE);
  1719. }
  1720. tbio->bi_end_io = end_sync_write;
  1721. d = r10_bio->devs[i].devnum;
  1722. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1723. atomic_inc(&r10_bio->remaining);
  1724. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1725. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1726. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1727. generic_make_request(tbio);
  1728. }
  1729. /* Now write out to any replacement devices
  1730. * that are active
  1731. */
  1732. for (i = 0; i < conf->copies; i++) {
  1733. int j, d;
  1734. tbio = r10_bio->devs[i].repl_bio;
  1735. if (!tbio || !tbio->bi_end_io)
  1736. continue;
  1737. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1738. && r10_bio->devs[i].bio != fbio)
  1739. for (j = 0; j < vcnt; j++)
  1740. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1741. page_address(fbio->bi_io_vec[j].bv_page),
  1742. PAGE_SIZE);
  1743. d = r10_bio->devs[i].devnum;
  1744. atomic_inc(&r10_bio->remaining);
  1745. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1746. tbio->bi_size >> 9);
  1747. generic_make_request(tbio);
  1748. }
  1749. done:
  1750. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1751. md_done_sync(mddev, r10_bio->sectors, 1);
  1752. put_buf(r10_bio);
  1753. }
  1754. }
  1755. /*
  1756. * Now for the recovery code.
  1757. * Recovery happens across physical sectors.
  1758. * We recover all non-is_sync drives by finding the virtual address of
  1759. * each, and then choose a working drive that also has that virt address.
  1760. * There is a separate r10_bio for each non-in_sync drive.
  1761. * Only the first two slots are in use. The first for reading,
  1762. * The second for writing.
  1763. *
  1764. */
  1765. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1766. {
  1767. /* We got a read error during recovery.
  1768. * We repeat the read in smaller page-sized sections.
  1769. * If a read succeeds, write it to the new device or record
  1770. * a bad block if we cannot.
  1771. * If a read fails, record a bad block on both old and
  1772. * new devices.
  1773. */
  1774. struct mddev *mddev = r10_bio->mddev;
  1775. struct r10conf *conf = mddev->private;
  1776. struct bio *bio = r10_bio->devs[0].bio;
  1777. sector_t sect = 0;
  1778. int sectors = r10_bio->sectors;
  1779. int idx = 0;
  1780. int dr = r10_bio->devs[0].devnum;
  1781. int dw = r10_bio->devs[1].devnum;
  1782. while (sectors) {
  1783. int s = sectors;
  1784. struct md_rdev *rdev;
  1785. sector_t addr;
  1786. int ok;
  1787. if (s > (PAGE_SIZE>>9))
  1788. s = PAGE_SIZE >> 9;
  1789. rdev = conf->mirrors[dr].rdev;
  1790. addr = r10_bio->devs[0].addr + sect,
  1791. ok = sync_page_io(rdev,
  1792. addr,
  1793. s << 9,
  1794. bio->bi_io_vec[idx].bv_page,
  1795. READ, false);
  1796. if (ok) {
  1797. rdev = conf->mirrors[dw].rdev;
  1798. addr = r10_bio->devs[1].addr + sect;
  1799. ok = sync_page_io(rdev,
  1800. addr,
  1801. s << 9,
  1802. bio->bi_io_vec[idx].bv_page,
  1803. WRITE, false);
  1804. if (!ok) {
  1805. set_bit(WriteErrorSeen, &rdev->flags);
  1806. if (!test_and_set_bit(WantReplacement,
  1807. &rdev->flags))
  1808. set_bit(MD_RECOVERY_NEEDED,
  1809. &rdev->mddev->recovery);
  1810. }
  1811. }
  1812. if (!ok) {
  1813. /* We don't worry if we cannot set a bad block -
  1814. * it really is bad so there is no loss in not
  1815. * recording it yet
  1816. */
  1817. rdev_set_badblocks(rdev, addr, s, 0);
  1818. if (rdev != conf->mirrors[dw].rdev) {
  1819. /* need bad block on destination too */
  1820. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  1821. addr = r10_bio->devs[1].addr + sect;
  1822. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  1823. if (!ok) {
  1824. /* just abort the recovery */
  1825. printk(KERN_NOTICE
  1826. "md/raid10:%s: recovery aborted"
  1827. " due to read error\n",
  1828. mdname(mddev));
  1829. conf->mirrors[dw].recovery_disabled
  1830. = mddev->recovery_disabled;
  1831. set_bit(MD_RECOVERY_INTR,
  1832. &mddev->recovery);
  1833. break;
  1834. }
  1835. }
  1836. }
  1837. sectors -= s;
  1838. sect += s;
  1839. idx++;
  1840. }
  1841. }
  1842. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1843. {
  1844. struct r10conf *conf = mddev->private;
  1845. int d;
  1846. struct bio *wbio, *wbio2;
  1847. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  1848. fix_recovery_read_error(r10_bio);
  1849. end_sync_request(r10_bio);
  1850. return;
  1851. }
  1852. /*
  1853. * share the pages with the first bio
  1854. * and submit the write request
  1855. */
  1856. d = r10_bio->devs[1].devnum;
  1857. wbio = r10_bio->devs[1].bio;
  1858. wbio2 = r10_bio->devs[1].repl_bio;
  1859. if (wbio->bi_end_io) {
  1860. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1861. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1862. generic_make_request(wbio);
  1863. }
  1864. if (wbio2 && wbio2->bi_end_io) {
  1865. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  1866. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1867. wbio2->bi_size >> 9);
  1868. generic_make_request(wbio2);
  1869. }
  1870. }
  1871. /*
  1872. * Used by fix_read_error() to decay the per rdev read_errors.
  1873. * We halve the read error count for every hour that has elapsed
  1874. * since the last recorded read error.
  1875. *
  1876. */
  1877. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  1878. {
  1879. struct timespec cur_time_mon;
  1880. unsigned long hours_since_last;
  1881. unsigned int read_errors = atomic_read(&rdev->read_errors);
  1882. ktime_get_ts(&cur_time_mon);
  1883. if (rdev->last_read_error.tv_sec == 0 &&
  1884. rdev->last_read_error.tv_nsec == 0) {
  1885. /* first time we've seen a read error */
  1886. rdev->last_read_error = cur_time_mon;
  1887. return;
  1888. }
  1889. hours_since_last = (cur_time_mon.tv_sec -
  1890. rdev->last_read_error.tv_sec) / 3600;
  1891. rdev->last_read_error = cur_time_mon;
  1892. /*
  1893. * if hours_since_last is > the number of bits in read_errors
  1894. * just set read errors to 0. We do this to avoid
  1895. * overflowing the shift of read_errors by hours_since_last.
  1896. */
  1897. if (hours_since_last >= 8 * sizeof(read_errors))
  1898. atomic_set(&rdev->read_errors, 0);
  1899. else
  1900. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  1901. }
  1902. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1903. int sectors, struct page *page, int rw)
  1904. {
  1905. sector_t first_bad;
  1906. int bad_sectors;
  1907. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  1908. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  1909. return -1;
  1910. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1911. /* success */
  1912. return 1;
  1913. if (rw == WRITE) {
  1914. set_bit(WriteErrorSeen, &rdev->flags);
  1915. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1916. set_bit(MD_RECOVERY_NEEDED,
  1917. &rdev->mddev->recovery);
  1918. }
  1919. /* need to record an error - either for the block or the device */
  1920. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1921. md_error(rdev->mddev, rdev);
  1922. return 0;
  1923. }
  1924. /*
  1925. * This is a kernel thread which:
  1926. *
  1927. * 1. Retries failed read operations on working mirrors.
  1928. * 2. Updates the raid superblock when problems encounter.
  1929. * 3. Performs writes following reads for array synchronising.
  1930. */
  1931. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  1932. {
  1933. int sect = 0; /* Offset from r10_bio->sector */
  1934. int sectors = r10_bio->sectors;
  1935. struct md_rdev*rdev;
  1936. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  1937. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  1938. /* still own a reference to this rdev, so it cannot
  1939. * have been cleared recently.
  1940. */
  1941. rdev = conf->mirrors[d].rdev;
  1942. if (test_bit(Faulty, &rdev->flags))
  1943. /* drive has already been failed, just ignore any
  1944. more fix_read_error() attempts */
  1945. return;
  1946. check_decay_read_errors(mddev, rdev);
  1947. atomic_inc(&rdev->read_errors);
  1948. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  1949. char b[BDEVNAME_SIZE];
  1950. bdevname(rdev->bdev, b);
  1951. printk(KERN_NOTICE
  1952. "md/raid10:%s: %s: Raid device exceeded "
  1953. "read_error threshold [cur %d:max %d]\n",
  1954. mdname(mddev), b,
  1955. atomic_read(&rdev->read_errors), max_read_errors);
  1956. printk(KERN_NOTICE
  1957. "md/raid10:%s: %s: Failing raid device\n",
  1958. mdname(mddev), b);
  1959. md_error(mddev, conf->mirrors[d].rdev);
  1960. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  1961. return;
  1962. }
  1963. while(sectors) {
  1964. int s = sectors;
  1965. int sl = r10_bio->read_slot;
  1966. int success = 0;
  1967. int start;
  1968. if (s > (PAGE_SIZE>>9))
  1969. s = PAGE_SIZE >> 9;
  1970. rcu_read_lock();
  1971. do {
  1972. sector_t first_bad;
  1973. int bad_sectors;
  1974. d = r10_bio->devs[sl].devnum;
  1975. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1976. if (rdev &&
  1977. !test_bit(Unmerged, &rdev->flags) &&
  1978. test_bit(In_sync, &rdev->flags) &&
  1979. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  1980. &first_bad, &bad_sectors) == 0) {
  1981. atomic_inc(&rdev->nr_pending);
  1982. rcu_read_unlock();
  1983. success = sync_page_io(rdev,
  1984. r10_bio->devs[sl].addr +
  1985. sect,
  1986. s<<9,
  1987. conf->tmppage, READ, false);
  1988. rdev_dec_pending(rdev, mddev);
  1989. rcu_read_lock();
  1990. if (success)
  1991. break;
  1992. }
  1993. sl++;
  1994. if (sl == conf->copies)
  1995. sl = 0;
  1996. } while (!success && sl != r10_bio->read_slot);
  1997. rcu_read_unlock();
  1998. if (!success) {
  1999. /* Cannot read from anywhere, just mark the block
  2000. * as bad on the first device to discourage future
  2001. * reads.
  2002. */
  2003. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  2004. rdev = conf->mirrors[dn].rdev;
  2005. if (!rdev_set_badblocks(
  2006. rdev,
  2007. r10_bio->devs[r10_bio->read_slot].addr
  2008. + sect,
  2009. s, 0)) {
  2010. md_error(mddev, rdev);
  2011. r10_bio->devs[r10_bio->read_slot].bio
  2012. = IO_BLOCKED;
  2013. }
  2014. break;
  2015. }
  2016. start = sl;
  2017. /* write it back and re-read */
  2018. rcu_read_lock();
  2019. while (sl != r10_bio->read_slot) {
  2020. char b[BDEVNAME_SIZE];
  2021. if (sl==0)
  2022. sl = conf->copies;
  2023. sl--;
  2024. d = r10_bio->devs[sl].devnum;
  2025. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2026. if (!rdev ||
  2027. test_bit(Unmerged, &rdev->flags) ||
  2028. !test_bit(In_sync, &rdev->flags))
  2029. continue;
  2030. atomic_inc(&rdev->nr_pending);
  2031. rcu_read_unlock();
  2032. if (r10_sync_page_io(rdev,
  2033. r10_bio->devs[sl].addr +
  2034. sect,
  2035. s<<9, conf->tmppage, WRITE)
  2036. == 0) {
  2037. /* Well, this device is dead */
  2038. printk(KERN_NOTICE
  2039. "md/raid10:%s: read correction "
  2040. "write failed"
  2041. " (%d sectors at %llu on %s)\n",
  2042. mdname(mddev), s,
  2043. (unsigned long long)(
  2044. sect +
  2045. choose_data_offset(r10_bio,
  2046. rdev)),
  2047. bdevname(rdev->bdev, b));
  2048. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2049. "drive\n",
  2050. mdname(mddev),
  2051. bdevname(rdev->bdev, b));
  2052. }
  2053. rdev_dec_pending(rdev, mddev);
  2054. rcu_read_lock();
  2055. }
  2056. sl = start;
  2057. while (sl != r10_bio->read_slot) {
  2058. char b[BDEVNAME_SIZE];
  2059. if (sl==0)
  2060. sl = conf->copies;
  2061. sl--;
  2062. d = r10_bio->devs[sl].devnum;
  2063. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2064. if (!rdev ||
  2065. !test_bit(In_sync, &rdev->flags))
  2066. continue;
  2067. atomic_inc(&rdev->nr_pending);
  2068. rcu_read_unlock();
  2069. switch (r10_sync_page_io(rdev,
  2070. r10_bio->devs[sl].addr +
  2071. sect,
  2072. s<<9, conf->tmppage,
  2073. READ)) {
  2074. case 0:
  2075. /* Well, this device is dead */
  2076. printk(KERN_NOTICE
  2077. "md/raid10:%s: unable to read back "
  2078. "corrected sectors"
  2079. " (%d sectors at %llu on %s)\n",
  2080. mdname(mddev), s,
  2081. (unsigned long long)(
  2082. sect +
  2083. choose_data_offset(r10_bio, rdev)),
  2084. bdevname(rdev->bdev, b));
  2085. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2086. "drive\n",
  2087. mdname(mddev),
  2088. bdevname(rdev->bdev, b));
  2089. break;
  2090. case 1:
  2091. printk(KERN_INFO
  2092. "md/raid10:%s: read error corrected"
  2093. " (%d sectors at %llu on %s)\n",
  2094. mdname(mddev), s,
  2095. (unsigned long long)(
  2096. sect +
  2097. choose_data_offset(r10_bio, rdev)),
  2098. bdevname(rdev->bdev, b));
  2099. atomic_add(s, &rdev->corrected_errors);
  2100. }
  2101. rdev_dec_pending(rdev, mddev);
  2102. rcu_read_lock();
  2103. }
  2104. rcu_read_unlock();
  2105. sectors -= s;
  2106. sect += s;
  2107. }
  2108. }
  2109. static void bi_complete(struct bio *bio, int error)
  2110. {
  2111. complete((struct completion *)bio->bi_private);
  2112. }
  2113. static int submit_bio_wait(int rw, struct bio *bio)
  2114. {
  2115. struct completion event;
  2116. rw |= REQ_SYNC;
  2117. init_completion(&event);
  2118. bio->bi_private = &event;
  2119. bio->bi_end_io = bi_complete;
  2120. submit_bio(rw, bio);
  2121. wait_for_completion(&event);
  2122. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  2123. }
  2124. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2125. {
  2126. struct bio *bio = r10_bio->master_bio;
  2127. struct mddev *mddev = r10_bio->mddev;
  2128. struct r10conf *conf = mddev->private;
  2129. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2130. /* bio has the data to be written to slot 'i' where
  2131. * we just recently had a write error.
  2132. * We repeatedly clone the bio and trim down to one block,
  2133. * then try the write. Where the write fails we record
  2134. * a bad block.
  2135. * It is conceivable that the bio doesn't exactly align with
  2136. * blocks. We must handle this.
  2137. *
  2138. * We currently own a reference to the rdev.
  2139. */
  2140. int block_sectors;
  2141. sector_t sector;
  2142. int sectors;
  2143. int sect_to_write = r10_bio->sectors;
  2144. int ok = 1;
  2145. if (rdev->badblocks.shift < 0)
  2146. return 0;
  2147. block_sectors = 1 << rdev->badblocks.shift;
  2148. sector = r10_bio->sector;
  2149. sectors = ((r10_bio->sector + block_sectors)
  2150. & ~(sector_t)(block_sectors - 1))
  2151. - sector;
  2152. while (sect_to_write) {
  2153. struct bio *wbio;
  2154. if (sectors > sect_to_write)
  2155. sectors = sect_to_write;
  2156. /* Write at 'sector' for 'sectors' */
  2157. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2158. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2159. wbio->bi_sector = (r10_bio->devs[i].addr+
  2160. choose_data_offset(r10_bio, rdev) +
  2161. (sector - r10_bio->sector));
  2162. wbio->bi_bdev = rdev->bdev;
  2163. if (submit_bio_wait(WRITE, wbio) == 0)
  2164. /* Failure! */
  2165. ok = rdev_set_badblocks(rdev, sector,
  2166. sectors, 0)
  2167. && ok;
  2168. bio_put(wbio);
  2169. sect_to_write -= sectors;
  2170. sector += sectors;
  2171. sectors = block_sectors;
  2172. }
  2173. return ok;
  2174. }
  2175. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2176. {
  2177. int slot = r10_bio->read_slot;
  2178. struct bio *bio;
  2179. struct r10conf *conf = mddev->private;
  2180. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2181. char b[BDEVNAME_SIZE];
  2182. unsigned long do_sync;
  2183. int max_sectors;
  2184. /* we got a read error. Maybe the drive is bad. Maybe just
  2185. * the block and we can fix it.
  2186. * We freeze all other IO, and try reading the block from
  2187. * other devices. When we find one, we re-write
  2188. * and check it that fixes the read error.
  2189. * This is all done synchronously while the array is
  2190. * frozen.
  2191. */
  2192. bio = r10_bio->devs[slot].bio;
  2193. bdevname(bio->bi_bdev, b);
  2194. bio_put(bio);
  2195. r10_bio->devs[slot].bio = NULL;
  2196. if (mddev->ro == 0) {
  2197. freeze_array(conf);
  2198. fix_read_error(conf, mddev, r10_bio);
  2199. unfreeze_array(conf);
  2200. } else
  2201. r10_bio->devs[slot].bio = IO_BLOCKED;
  2202. rdev_dec_pending(rdev, mddev);
  2203. read_more:
  2204. rdev = read_balance(conf, r10_bio, &max_sectors);
  2205. if (rdev == NULL) {
  2206. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2207. " read error for block %llu\n",
  2208. mdname(mddev), b,
  2209. (unsigned long long)r10_bio->sector);
  2210. raid_end_bio_io(r10_bio);
  2211. return;
  2212. }
  2213. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2214. slot = r10_bio->read_slot;
  2215. printk_ratelimited(
  2216. KERN_ERR
  2217. "md/raid10:%s: %s: redirecting"
  2218. "sector %llu to another mirror\n",
  2219. mdname(mddev),
  2220. bdevname(rdev->bdev, b),
  2221. (unsigned long long)r10_bio->sector);
  2222. bio = bio_clone_mddev(r10_bio->master_bio,
  2223. GFP_NOIO, mddev);
  2224. md_trim_bio(bio,
  2225. r10_bio->sector - bio->bi_sector,
  2226. max_sectors);
  2227. r10_bio->devs[slot].bio = bio;
  2228. r10_bio->devs[slot].rdev = rdev;
  2229. bio->bi_sector = r10_bio->devs[slot].addr
  2230. + choose_data_offset(r10_bio, rdev);
  2231. bio->bi_bdev = rdev->bdev;
  2232. bio->bi_rw = READ | do_sync;
  2233. bio->bi_private = r10_bio;
  2234. bio->bi_end_io = raid10_end_read_request;
  2235. if (max_sectors < r10_bio->sectors) {
  2236. /* Drat - have to split this up more */
  2237. struct bio *mbio = r10_bio->master_bio;
  2238. int sectors_handled =
  2239. r10_bio->sector + max_sectors
  2240. - mbio->bi_sector;
  2241. r10_bio->sectors = max_sectors;
  2242. spin_lock_irq(&conf->device_lock);
  2243. if (mbio->bi_phys_segments == 0)
  2244. mbio->bi_phys_segments = 2;
  2245. else
  2246. mbio->bi_phys_segments++;
  2247. spin_unlock_irq(&conf->device_lock);
  2248. generic_make_request(bio);
  2249. r10_bio = mempool_alloc(conf->r10bio_pool,
  2250. GFP_NOIO);
  2251. r10_bio->master_bio = mbio;
  2252. r10_bio->sectors = (mbio->bi_size >> 9)
  2253. - sectors_handled;
  2254. r10_bio->state = 0;
  2255. set_bit(R10BIO_ReadError,
  2256. &r10_bio->state);
  2257. r10_bio->mddev = mddev;
  2258. r10_bio->sector = mbio->bi_sector
  2259. + sectors_handled;
  2260. goto read_more;
  2261. } else
  2262. generic_make_request(bio);
  2263. }
  2264. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2265. {
  2266. /* Some sort of write request has finished and it
  2267. * succeeded in writing where we thought there was a
  2268. * bad block. So forget the bad block.
  2269. * Or possibly if failed and we need to record
  2270. * a bad block.
  2271. */
  2272. int m;
  2273. struct md_rdev *rdev;
  2274. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2275. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2276. for (m = 0; m < conf->copies; m++) {
  2277. int dev = r10_bio->devs[m].devnum;
  2278. rdev = conf->mirrors[dev].rdev;
  2279. if (r10_bio->devs[m].bio == NULL)
  2280. continue;
  2281. if (test_bit(BIO_UPTODATE,
  2282. &r10_bio->devs[m].bio->bi_flags)) {
  2283. rdev_clear_badblocks(
  2284. rdev,
  2285. r10_bio->devs[m].addr,
  2286. r10_bio->sectors, 0);
  2287. } else {
  2288. if (!rdev_set_badblocks(
  2289. rdev,
  2290. r10_bio->devs[m].addr,
  2291. r10_bio->sectors, 0))
  2292. md_error(conf->mddev, rdev);
  2293. }
  2294. rdev = conf->mirrors[dev].replacement;
  2295. if (r10_bio->devs[m].repl_bio == NULL)
  2296. continue;
  2297. if (test_bit(BIO_UPTODATE,
  2298. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2299. rdev_clear_badblocks(
  2300. rdev,
  2301. r10_bio->devs[m].addr,
  2302. r10_bio->sectors, 0);
  2303. } else {
  2304. if (!rdev_set_badblocks(
  2305. rdev,
  2306. r10_bio->devs[m].addr,
  2307. r10_bio->sectors, 0))
  2308. md_error(conf->mddev, rdev);
  2309. }
  2310. }
  2311. put_buf(r10_bio);
  2312. } else {
  2313. for (m = 0; m < conf->copies; m++) {
  2314. int dev = r10_bio->devs[m].devnum;
  2315. struct bio *bio = r10_bio->devs[m].bio;
  2316. rdev = conf->mirrors[dev].rdev;
  2317. if (bio == IO_MADE_GOOD) {
  2318. rdev_clear_badblocks(
  2319. rdev,
  2320. r10_bio->devs[m].addr,
  2321. r10_bio->sectors, 0);
  2322. rdev_dec_pending(rdev, conf->mddev);
  2323. } else if (bio != NULL &&
  2324. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2325. if (!narrow_write_error(r10_bio, m)) {
  2326. md_error(conf->mddev, rdev);
  2327. set_bit(R10BIO_Degraded,
  2328. &r10_bio->state);
  2329. }
  2330. rdev_dec_pending(rdev, conf->mddev);
  2331. }
  2332. bio = r10_bio->devs[m].repl_bio;
  2333. rdev = conf->mirrors[dev].replacement;
  2334. if (rdev && bio == IO_MADE_GOOD) {
  2335. rdev_clear_badblocks(
  2336. rdev,
  2337. r10_bio->devs[m].addr,
  2338. r10_bio->sectors, 0);
  2339. rdev_dec_pending(rdev, conf->mddev);
  2340. }
  2341. }
  2342. if (test_bit(R10BIO_WriteError,
  2343. &r10_bio->state))
  2344. close_write(r10_bio);
  2345. raid_end_bio_io(r10_bio);
  2346. }
  2347. }
  2348. static void raid10d(struct mddev *mddev)
  2349. {
  2350. struct r10bio *r10_bio;
  2351. unsigned long flags;
  2352. struct r10conf *conf = mddev->private;
  2353. struct list_head *head = &conf->retry_list;
  2354. struct blk_plug plug;
  2355. md_check_recovery(mddev);
  2356. blk_start_plug(&plug);
  2357. for (;;) {
  2358. flush_pending_writes(conf);
  2359. spin_lock_irqsave(&conf->device_lock, flags);
  2360. if (list_empty(head)) {
  2361. spin_unlock_irqrestore(&conf->device_lock, flags);
  2362. break;
  2363. }
  2364. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2365. list_del(head->prev);
  2366. conf->nr_queued--;
  2367. spin_unlock_irqrestore(&conf->device_lock, flags);
  2368. mddev = r10_bio->mddev;
  2369. conf = mddev->private;
  2370. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2371. test_bit(R10BIO_WriteError, &r10_bio->state))
  2372. handle_write_completed(conf, r10_bio);
  2373. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2374. sync_request_write(mddev, r10_bio);
  2375. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2376. recovery_request_write(mddev, r10_bio);
  2377. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2378. handle_read_error(mddev, r10_bio);
  2379. else {
  2380. /* just a partial read to be scheduled from a
  2381. * separate context
  2382. */
  2383. int slot = r10_bio->read_slot;
  2384. generic_make_request(r10_bio->devs[slot].bio);
  2385. }
  2386. cond_resched();
  2387. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2388. md_check_recovery(mddev);
  2389. }
  2390. blk_finish_plug(&plug);
  2391. }
  2392. static int init_resync(struct r10conf *conf)
  2393. {
  2394. int buffs;
  2395. int i;
  2396. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2397. BUG_ON(conf->r10buf_pool);
  2398. conf->have_replacement = 0;
  2399. for (i = 0; i < conf->geo.raid_disks; i++)
  2400. if (conf->mirrors[i].replacement)
  2401. conf->have_replacement = 1;
  2402. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2403. if (!conf->r10buf_pool)
  2404. return -ENOMEM;
  2405. conf->next_resync = 0;
  2406. return 0;
  2407. }
  2408. /*
  2409. * perform a "sync" on one "block"
  2410. *
  2411. * We need to make sure that no normal I/O request - particularly write
  2412. * requests - conflict with active sync requests.
  2413. *
  2414. * This is achieved by tracking pending requests and a 'barrier' concept
  2415. * that can be installed to exclude normal IO requests.
  2416. *
  2417. * Resync and recovery are handled very differently.
  2418. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2419. *
  2420. * For resync, we iterate over virtual addresses, read all copies,
  2421. * and update if there are differences. If only one copy is live,
  2422. * skip it.
  2423. * For recovery, we iterate over physical addresses, read a good
  2424. * value for each non-in_sync drive, and over-write.
  2425. *
  2426. * So, for recovery we may have several outstanding complex requests for a
  2427. * given address, one for each out-of-sync device. We model this by allocating
  2428. * a number of r10_bio structures, one for each out-of-sync device.
  2429. * As we setup these structures, we collect all bio's together into a list
  2430. * which we then process collectively to add pages, and then process again
  2431. * to pass to generic_make_request.
  2432. *
  2433. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2434. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2435. * has its remaining count decremented to 0, the whole complex operation
  2436. * is complete.
  2437. *
  2438. */
  2439. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2440. int *skipped, int go_faster)
  2441. {
  2442. struct r10conf *conf = mddev->private;
  2443. struct r10bio *r10_bio;
  2444. struct bio *biolist = NULL, *bio;
  2445. sector_t max_sector, nr_sectors;
  2446. int i;
  2447. int max_sync;
  2448. sector_t sync_blocks;
  2449. sector_t sectors_skipped = 0;
  2450. int chunks_skipped = 0;
  2451. sector_t chunk_mask = conf->geo.chunk_mask;
  2452. if (!conf->r10buf_pool)
  2453. if (init_resync(conf))
  2454. return 0;
  2455. skipped:
  2456. max_sector = mddev->dev_sectors;
  2457. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2458. max_sector = mddev->resync_max_sectors;
  2459. if (sector_nr >= max_sector) {
  2460. /* If we aborted, we need to abort the
  2461. * sync on the 'current' bitmap chucks (there can
  2462. * be several when recovering multiple devices).
  2463. * as we may have started syncing it but not finished.
  2464. * We can find the current address in
  2465. * mddev->curr_resync, but for recovery,
  2466. * we need to convert that to several
  2467. * virtual addresses.
  2468. */
  2469. if (mddev->curr_resync < max_sector) { /* aborted */
  2470. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2471. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2472. &sync_blocks, 1);
  2473. else for (i = 0; i < conf->geo.raid_disks; i++) {
  2474. sector_t sect =
  2475. raid10_find_virt(conf, mddev->curr_resync, i);
  2476. bitmap_end_sync(mddev->bitmap, sect,
  2477. &sync_blocks, 1);
  2478. }
  2479. } else {
  2480. /* completed sync */
  2481. if ((!mddev->bitmap || conf->fullsync)
  2482. && conf->have_replacement
  2483. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2484. /* Completed a full sync so the replacements
  2485. * are now fully recovered.
  2486. */
  2487. for (i = 0; i < conf->geo.raid_disks; i++)
  2488. if (conf->mirrors[i].replacement)
  2489. conf->mirrors[i].replacement
  2490. ->recovery_offset
  2491. = MaxSector;
  2492. }
  2493. conf->fullsync = 0;
  2494. }
  2495. bitmap_close_sync(mddev->bitmap);
  2496. close_sync(conf);
  2497. *skipped = 1;
  2498. return sectors_skipped;
  2499. }
  2500. if (chunks_skipped >= conf->geo.raid_disks) {
  2501. /* if there has been nothing to do on any drive,
  2502. * then there is nothing to do at all..
  2503. */
  2504. *skipped = 1;
  2505. return (max_sector - sector_nr) + sectors_skipped;
  2506. }
  2507. if (max_sector > mddev->resync_max)
  2508. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2509. /* make sure whole request will fit in a chunk - if chunks
  2510. * are meaningful
  2511. */
  2512. if (conf->geo.near_copies < conf->geo.raid_disks &&
  2513. max_sector > (sector_nr | chunk_mask))
  2514. max_sector = (sector_nr | chunk_mask) + 1;
  2515. /*
  2516. * If there is non-resync activity waiting for us then
  2517. * put in a delay to throttle resync.
  2518. */
  2519. if (!go_faster && conf->nr_waiting)
  2520. msleep_interruptible(1000);
  2521. /* Again, very different code for resync and recovery.
  2522. * Both must result in an r10bio with a list of bios that
  2523. * have bi_end_io, bi_sector, bi_bdev set,
  2524. * and bi_private set to the r10bio.
  2525. * For recovery, we may actually create several r10bios
  2526. * with 2 bios in each, that correspond to the bios in the main one.
  2527. * In this case, the subordinate r10bios link back through a
  2528. * borrowed master_bio pointer, and the counter in the master
  2529. * includes a ref from each subordinate.
  2530. */
  2531. /* First, we decide what to do and set ->bi_end_io
  2532. * To end_sync_read if we want to read, and
  2533. * end_sync_write if we will want to write.
  2534. */
  2535. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2536. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2537. /* recovery... the complicated one */
  2538. int j;
  2539. r10_bio = NULL;
  2540. for (i = 0 ; i < conf->geo.raid_disks; i++) {
  2541. int still_degraded;
  2542. struct r10bio *rb2;
  2543. sector_t sect;
  2544. int must_sync;
  2545. int any_working;
  2546. struct mirror_info *mirror = &conf->mirrors[i];
  2547. if ((mirror->rdev == NULL ||
  2548. test_bit(In_sync, &mirror->rdev->flags))
  2549. &&
  2550. (mirror->replacement == NULL ||
  2551. test_bit(Faulty,
  2552. &mirror->replacement->flags)))
  2553. continue;
  2554. still_degraded = 0;
  2555. /* want to reconstruct this device */
  2556. rb2 = r10_bio;
  2557. sect = raid10_find_virt(conf, sector_nr, i);
  2558. /* Unless we are doing a full sync, or a replacement
  2559. * we only need to recover the block if it is set in
  2560. * the bitmap
  2561. */
  2562. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2563. &sync_blocks, 1);
  2564. if (sync_blocks < max_sync)
  2565. max_sync = sync_blocks;
  2566. if (!must_sync &&
  2567. mirror->replacement == NULL &&
  2568. !conf->fullsync) {
  2569. /* yep, skip the sync_blocks here, but don't assume
  2570. * that there will never be anything to do here
  2571. */
  2572. chunks_skipped = -1;
  2573. continue;
  2574. }
  2575. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2576. raise_barrier(conf, rb2 != NULL);
  2577. atomic_set(&r10_bio->remaining, 0);
  2578. r10_bio->master_bio = (struct bio*)rb2;
  2579. if (rb2)
  2580. atomic_inc(&rb2->remaining);
  2581. r10_bio->mddev = mddev;
  2582. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2583. r10_bio->sector = sect;
  2584. raid10_find_phys(conf, r10_bio);
  2585. /* Need to check if the array will still be
  2586. * degraded
  2587. */
  2588. for (j = 0; j < conf->geo.raid_disks; j++)
  2589. if (conf->mirrors[j].rdev == NULL ||
  2590. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2591. still_degraded = 1;
  2592. break;
  2593. }
  2594. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2595. &sync_blocks, still_degraded);
  2596. any_working = 0;
  2597. for (j=0; j<conf->copies;j++) {
  2598. int k;
  2599. int d = r10_bio->devs[j].devnum;
  2600. sector_t from_addr, to_addr;
  2601. struct md_rdev *rdev;
  2602. sector_t sector, first_bad;
  2603. int bad_sectors;
  2604. if (!conf->mirrors[d].rdev ||
  2605. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2606. continue;
  2607. /* This is where we read from */
  2608. any_working = 1;
  2609. rdev = conf->mirrors[d].rdev;
  2610. sector = r10_bio->devs[j].addr;
  2611. if (is_badblock(rdev, sector, max_sync,
  2612. &first_bad, &bad_sectors)) {
  2613. if (first_bad > sector)
  2614. max_sync = first_bad - sector;
  2615. else {
  2616. bad_sectors -= (sector
  2617. - first_bad);
  2618. if (max_sync > bad_sectors)
  2619. max_sync = bad_sectors;
  2620. continue;
  2621. }
  2622. }
  2623. bio = r10_bio->devs[0].bio;
  2624. bio->bi_next = biolist;
  2625. biolist = bio;
  2626. bio->bi_private = r10_bio;
  2627. bio->bi_end_io = end_sync_read;
  2628. bio->bi_rw = READ;
  2629. from_addr = r10_bio->devs[j].addr;
  2630. bio->bi_sector = from_addr + rdev->data_offset;
  2631. bio->bi_bdev = rdev->bdev;
  2632. atomic_inc(&rdev->nr_pending);
  2633. /* and we write to 'i' (if not in_sync) */
  2634. for (k=0; k<conf->copies; k++)
  2635. if (r10_bio->devs[k].devnum == i)
  2636. break;
  2637. BUG_ON(k == conf->copies);
  2638. to_addr = r10_bio->devs[k].addr;
  2639. r10_bio->devs[0].devnum = d;
  2640. r10_bio->devs[0].addr = from_addr;
  2641. r10_bio->devs[1].devnum = i;
  2642. r10_bio->devs[1].addr = to_addr;
  2643. rdev = mirror->rdev;
  2644. if (!test_bit(In_sync, &rdev->flags)) {
  2645. bio = r10_bio->devs[1].bio;
  2646. bio->bi_next = biolist;
  2647. biolist = bio;
  2648. bio->bi_private = r10_bio;
  2649. bio->bi_end_io = end_sync_write;
  2650. bio->bi_rw = WRITE;
  2651. bio->bi_sector = to_addr
  2652. + rdev->data_offset;
  2653. bio->bi_bdev = rdev->bdev;
  2654. atomic_inc(&r10_bio->remaining);
  2655. } else
  2656. r10_bio->devs[1].bio->bi_end_io = NULL;
  2657. /* and maybe write to replacement */
  2658. bio = r10_bio->devs[1].repl_bio;
  2659. if (bio)
  2660. bio->bi_end_io = NULL;
  2661. rdev = mirror->replacement;
  2662. /* Note: if rdev != NULL, then bio
  2663. * cannot be NULL as r10buf_pool_alloc will
  2664. * have allocated it.
  2665. * So the second test here is pointless.
  2666. * But it keeps semantic-checkers happy, and
  2667. * this comment keeps human reviewers
  2668. * happy.
  2669. */
  2670. if (rdev == NULL || bio == NULL ||
  2671. test_bit(Faulty, &rdev->flags))
  2672. break;
  2673. bio->bi_next = biolist;
  2674. biolist = bio;
  2675. bio->bi_private = r10_bio;
  2676. bio->bi_end_io = end_sync_write;
  2677. bio->bi_rw = WRITE;
  2678. bio->bi_sector = to_addr + rdev->data_offset;
  2679. bio->bi_bdev = rdev->bdev;
  2680. atomic_inc(&r10_bio->remaining);
  2681. break;
  2682. }
  2683. if (j == conf->copies) {
  2684. /* Cannot recover, so abort the recovery or
  2685. * record a bad block */
  2686. put_buf(r10_bio);
  2687. if (rb2)
  2688. atomic_dec(&rb2->remaining);
  2689. r10_bio = rb2;
  2690. if (any_working) {
  2691. /* problem is that there are bad blocks
  2692. * on other device(s)
  2693. */
  2694. int k;
  2695. for (k = 0; k < conf->copies; k++)
  2696. if (r10_bio->devs[k].devnum == i)
  2697. break;
  2698. if (!test_bit(In_sync,
  2699. &mirror->rdev->flags)
  2700. && !rdev_set_badblocks(
  2701. mirror->rdev,
  2702. r10_bio->devs[k].addr,
  2703. max_sync, 0))
  2704. any_working = 0;
  2705. if (mirror->replacement &&
  2706. !rdev_set_badblocks(
  2707. mirror->replacement,
  2708. r10_bio->devs[k].addr,
  2709. max_sync, 0))
  2710. any_working = 0;
  2711. }
  2712. if (!any_working) {
  2713. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2714. &mddev->recovery))
  2715. printk(KERN_INFO "md/raid10:%s: insufficient "
  2716. "working devices for recovery.\n",
  2717. mdname(mddev));
  2718. mirror->recovery_disabled
  2719. = mddev->recovery_disabled;
  2720. }
  2721. break;
  2722. }
  2723. }
  2724. if (biolist == NULL) {
  2725. while (r10_bio) {
  2726. struct r10bio *rb2 = r10_bio;
  2727. r10_bio = (struct r10bio*) rb2->master_bio;
  2728. rb2->master_bio = NULL;
  2729. put_buf(rb2);
  2730. }
  2731. goto giveup;
  2732. }
  2733. } else {
  2734. /* resync. Schedule a read for every block at this virt offset */
  2735. int count = 0;
  2736. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2737. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2738. &sync_blocks, mddev->degraded) &&
  2739. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2740. &mddev->recovery)) {
  2741. /* We can skip this block */
  2742. *skipped = 1;
  2743. return sync_blocks + sectors_skipped;
  2744. }
  2745. if (sync_blocks < max_sync)
  2746. max_sync = sync_blocks;
  2747. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2748. r10_bio->mddev = mddev;
  2749. atomic_set(&r10_bio->remaining, 0);
  2750. raise_barrier(conf, 0);
  2751. conf->next_resync = sector_nr;
  2752. r10_bio->master_bio = NULL;
  2753. r10_bio->sector = sector_nr;
  2754. set_bit(R10BIO_IsSync, &r10_bio->state);
  2755. raid10_find_phys(conf, r10_bio);
  2756. r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
  2757. for (i = 0; i < conf->copies; i++) {
  2758. int d = r10_bio->devs[i].devnum;
  2759. sector_t first_bad, sector;
  2760. int bad_sectors;
  2761. if (r10_bio->devs[i].repl_bio)
  2762. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2763. bio = r10_bio->devs[i].bio;
  2764. bio->bi_end_io = NULL;
  2765. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2766. if (conf->mirrors[d].rdev == NULL ||
  2767. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  2768. continue;
  2769. sector = r10_bio->devs[i].addr;
  2770. if (is_badblock(conf->mirrors[d].rdev,
  2771. sector, max_sync,
  2772. &first_bad, &bad_sectors)) {
  2773. if (first_bad > sector)
  2774. max_sync = first_bad - sector;
  2775. else {
  2776. bad_sectors -= (sector - first_bad);
  2777. if (max_sync > bad_sectors)
  2778. max_sync = max_sync;
  2779. continue;
  2780. }
  2781. }
  2782. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2783. atomic_inc(&r10_bio->remaining);
  2784. bio->bi_next = biolist;
  2785. biolist = bio;
  2786. bio->bi_private = r10_bio;
  2787. bio->bi_end_io = end_sync_read;
  2788. bio->bi_rw = READ;
  2789. bio->bi_sector = sector +
  2790. conf->mirrors[d].rdev->data_offset;
  2791. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  2792. count++;
  2793. if (conf->mirrors[d].replacement == NULL ||
  2794. test_bit(Faulty,
  2795. &conf->mirrors[d].replacement->flags))
  2796. continue;
  2797. /* Need to set up for writing to the replacement */
  2798. bio = r10_bio->devs[i].repl_bio;
  2799. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2800. sector = r10_bio->devs[i].addr;
  2801. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2802. bio->bi_next = biolist;
  2803. biolist = bio;
  2804. bio->bi_private = r10_bio;
  2805. bio->bi_end_io = end_sync_write;
  2806. bio->bi_rw = WRITE;
  2807. bio->bi_sector = sector +
  2808. conf->mirrors[d].replacement->data_offset;
  2809. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  2810. count++;
  2811. }
  2812. if (count < 2) {
  2813. for (i=0; i<conf->copies; i++) {
  2814. int d = r10_bio->devs[i].devnum;
  2815. if (r10_bio->devs[i].bio->bi_end_io)
  2816. rdev_dec_pending(conf->mirrors[d].rdev,
  2817. mddev);
  2818. if (r10_bio->devs[i].repl_bio &&
  2819. r10_bio->devs[i].repl_bio->bi_end_io)
  2820. rdev_dec_pending(
  2821. conf->mirrors[d].replacement,
  2822. mddev);
  2823. }
  2824. put_buf(r10_bio);
  2825. biolist = NULL;
  2826. goto giveup;
  2827. }
  2828. }
  2829. for (bio = biolist; bio ; bio=bio->bi_next) {
  2830. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  2831. if (bio->bi_end_io)
  2832. bio->bi_flags |= 1 << BIO_UPTODATE;
  2833. bio->bi_vcnt = 0;
  2834. bio->bi_idx = 0;
  2835. bio->bi_phys_segments = 0;
  2836. bio->bi_size = 0;
  2837. }
  2838. nr_sectors = 0;
  2839. if (sector_nr + max_sync < max_sector)
  2840. max_sector = sector_nr + max_sync;
  2841. do {
  2842. struct page *page;
  2843. int len = PAGE_SIZE;
  2844. if (sector_nr + (len>>9) > max_sector)
  2845. len = (max_sector - sector_nr) << 9;
  2846. if (len == 0)
  2847. break;
  2848. for (bio= biolist ; bio ; bio=bio->bi_next) {
  2849. struct bio *bio2;
  2850. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2851. if (bio_add_page(bio, page, len, 0))
  2852. continue;
  2853. /* stop here */
  2854. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2855. for (bio2 = biolist;
  2856. bio2 && bio2 != bio;
  2857. bio2 = bio2->bi_next) {
  2858. /* remove last page from this bio */
  2859. bio2->bi_vcnt--;
  2860. bio2->bi_size -= len;
  2861. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  2862. }
  2863. goto bio_full;
  2864. }
  2865. nr_sectors += len>>9;
  2866. sector_nr += len>>9;
  2867. } while (biolist->bi_vcnt < RESYNC_PAGES);
  2868. bio_full:
  2869. r10_bio->sectors = nr_sectors;
  2870. while (biolist) {
  2871. bio = biolist;
  2872. biolist = biolist->bi_next;
  2873. bio->bi_next = NULL;
  2874. r10_bio = bio->bi_private;
  2875. r10_bio->sectors = nr_sectors;
  2876. if (bio->bi_end_io == end_sync_read) {
  2877. md_sync_acct(bio->bi_bdev, nr_sectors);
  2878. generic_make_request(bio);
  2879. }
  2880. }
  2881. if (sectors_skipped)
  2882. /* pretend they weren't skipped, it makes
  2883. * no important difference in this case
  2884. */
  2885. md_done_sync(mddev, sectors_skipped, 1);
  2886. return sectors_skipped + nr_sectors;
  2887. giveup:
  2888. /* There is nowhere to write, so all non-sync
  2889. * drives must be failed or in resync, all drives
  2890. * have a bad block, so try the next chunk...
  2891. */
  2892. if (sector_nr + max_sync < max_sector)
  2893. max_sector = sector_nr + max_sync;
  2894. sectors_skipped += (max_sector - sector_nr);
  2895. chunks_skipped ++;
  2896. sector_nr = max_sector;
  2897. goto skipped;
  2898. }
  2899. static sector_t
  2900. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2901. {
  2902. sector_t size;
  2903. struct r10conf *conf = mddev->private;
  2904. if (!raid_disks)
  2905. raid_disks = conf->geo.raid_disks;
  2906. if (!sectors)
  2907. sectors = conf->dev_sectors;
  2908. size = sectors >> conf->geo.chunk_shift;
  2909. sector_div(size, conf->geo.far_copies);
  2910. size = size * raid_disks;
  2911. sector_div(size, conf->geo.near_copies);
  2912. return size << conf->geo.chunk_shift;
  2913. }
  2914. static void calc_sectors(struct r10conf *conf, sector_t size)
  2915. {
  2916. /* Calculate the number of sectors-per-device that will
  2917. * actually be used, and set conf->dev_sectors and
  2918. * conf->stride
  2919. */
  2920. size = size >> conf->geo.chunk_shift;
  2921. sector_div(size, conf->geo.far_copies);
  2922. size = size * conf->geo.raid_disks;
  2923. sector_div(size, conf->geo.near_copies);
  2924. /* 'size' is now the number of chunks in the array */
  2925. /* calculate "used chunks per device" */
  2926. size = size * conf->copies;
  2927. /* We need to round up when dividing by raid_disks to
  2928. * get the stride size.
  2929. */
  2930. size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
  2931. conf->dev_sectors = size << conf->geo.chunk_shift;
  2932. if (conf->geo.far_offset)
  2933. conf->geo.stride = 1 << conf->geo.chunk_shift;
  2934. else {
  2935. sector_div(size, conf->geo.far_copies);
  2936. conf->geo.stride = size << conf->geo.chunk_shift;
  2937. }
  2938. }
  2939. static struct r10conf *setup_conf(struct mddev *mddev)
  2940. {
  2941. struct r10conf *conf = NULL;
  2942. int nc, fc, fo;
  2943. int err = -EINVAL;
  2944. if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
  2945. !is_power_of_2(mddev->new_chunk_sectors)) {
  2946. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  2947. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  2948. mdname(mddev), PAGE_SIZE);
  2949. goto out;
  2950. }
  2951. nc = mddev->new_layout & 255;
  2952. fc = (mddev->new_layout >> 8) & 255;
  2953. fo = mddev->new_layout & (1<<16);
  2954. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  2955. (mddev->new_layout >> 17)) {
  2956. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  2957. mdname(mddev), mddev->new_layout);
  2958. goto out;
  2959. }
  2960. err = -ENOMEM;
  2961. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  2962. if (!conf)
  2963. goto out;
  2964. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  2965. GFP_KERNEL);
  2966. if (!conf->mirrors)
  2967. goto out;
  2968. conf->tmppage = alloc_page(GFP_KERNEL);
  2969. if (!conf->tmppage)
  2970. goto out;
  2971. conf->geo.raid_disks = mddev->raid_disks;
  2972. conf->geo.near_copies = nc;
  2973. conf->geo.far_copies = fc;
  2974. conf->copies = nc*fc;
  2975. conf->geo.far_offset = fo;
  2976. conf->geo.chunk_mask = mddev->new_chunk_sectors - 1;
  2977. conf->geo.chunk_shift = ffz(~mddev->new_chunk_sectors);
  2978. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  2979. r10bio_pool_free, conf);
  2980. if (!conf->r10bio_pool)
  2981. goto out;
  2982. calc_sectors(conf, mddev->dev_sectors);
  2983. conf->prev = conf->geo;
  2984. conf->reshape_progress = MaxSector;
  2985. spin_lock_init(&conf->device_lock);
  2986. INIT_LIST_HEAD(&conf->retry_list);
  2987. spin_lock_init(&conf->resync_lock);
  2988. init_waitqueue_head(&conf->wait_barrier);
  2989. conf->thread = md_register_thread(raid10d, mddev, NULL);
  2990. if (!conf->thread)
  2991. goto out;
  2992. conf->mddev = mddev;
  2993. return conf;
  2994. out:
  2995. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  2996. mdname(mddev));
  2997. if (conf) {
  2998. if (conf->r10bio_pool)
  2999. mempool_destroy(conf->r10bio_pool);
  3000. kfree(conf->mirrors);
  3001. safe_put_page(conf->tmppage);
  3002. kfree(conf);
  3003. }
  3004. return ERR_PTR(err);
  3005. }
  3006. static int run(struct mddev *mddev)
  3007. {
  3008. struct r10conf *conf;
  3009. int i, disk_idx, chunk_size;
  3010. struct mirror_info *disk;
  3011. struct md_rdev *rdev;
  3012. sector_t size;
  3013. /*
  3014. * copy the already verified devices into our private RAID10
  3015. * bookkeeping area. [whatever we allocate in run(),
  3016. * should be freed in stop()]
  3017. */
  3018. if (mddev->private == NULL) {
  3019. conf = setup_conf(mddev);
  3020. if (IS_ERR(conf))
  3021. return PTR_ERR(conf);
  3022. mddev->private = conf;
  3023. }
  3024. conf = mddev->private;
  3025. if (!conf)
  3026. goto out;
  3027. mddev->thread = conf->thread;
  3028. conf->thread = NULL;
  3029. chunk_size = mddev->chunk_sectors << 9;
  3030. blk_queue_io_min(mddev->queue, chunk_size);
  3031. if (conf->geo.raid_disks % conf->geo.near_copies)
  3032. blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
  3033. else
  3034. blk_queue_io_opt(mddev->queue, chunk_size *
  3035. (conf->geo.raid_disks / conf->geo.near_copies));
  3036. rdev_for_each(rdev, mddev) {
  3037. disk_idx = rdev->raid_disk;
  3038. if (disk_idx < 0)
  3039. continue;
  3040. if (disk_idx >= conf->geo.raid_disks &&
  3041. disk_idx >= conf->prev.raid_disks)
  3042. continue;
  3043. disk = conf->mirrors + disk_idx;
  3044. if (test_bit(Replacement, &rdev->flags)) {
  3045. if (disk->replacement)
  3046. goto out_free_conf;
  3047. disk->replacement = rdev;
  3048. } else {
  3049. if (disk->rdev)
  3050. goto out_free_conf;
  3051. disk->rdev = rdev;
  3052. }
  3053. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3054. rdev->data_offset << 9);
  3055. disk->head_position = 0;
  3056. }
  3057. /* need to check that every block has at least one working mirror */
  3058. if (!enough(conf, -1)) {
  3059. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  3060. mdname(mddev));
  3061. goto out_free_conf;
  3062. }
  3063. mddev->degraded = 0;
  3064. for (i = 0;
  3065. i < conf->geo.raid_disks
  3066. || i < conf->prev.raid_disks;
  3067. i++) {
  3068. disk = conf->mirrors + i;
  3069. if (!disk->rdev && disk->replacement) {
  3070. /* The replacement is all we have - use it */
  3071. disk->rdev = disk->replacement;
  3072. disk->replacement = NULL;
  3073. clear_bit(Replacement, &disk->rdev->flags);
  3074. }
  3075. if (!disk->rdev ||
  3076. !test_bit(In_sync, &disk->rdev->flags)) {
  3077. disk->head_position = 0;
  3078. mddev->degraded++;
  3079. if (disk->rdev)
  3080. conf->fullsync = 1;
  3081. }
  3082. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3083. }
  3084. if (mddev->recovery_cp != MaxSector)
  3085. printk(KERN_NOTICE "md/raid10:%s: not clean"
  3086. " -- starting background reconstruction\n",
  3087. mdname(mddev));
  3088. printk(KERN_INFO
  3089. "md/raid10:%s: active with %d out of %d devices\n",
  3090. mdname(mddev), conf->geo.raid_disks - mddev->degraded,
  3091. conf->geo.raid_disks);
  3092. /*
  3093. * Ok, everything is just fine now
  3094. */
  3095. mddev->dev_sectors = conf->dev_sectors;
  3096. size = raid10_size(mddev, 0, 0);
  3097. md_set_array_sectors(mddev, size);
  3098. mddev->resync_max_sectors = size;
  3099. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  3100. mddev->queue->backing_dev_info.congested_data = mddev;
  3101. /* Calculate max read-ahead size.
  3102. * We need to readahead at least twice a whole stripe....
  3103. * maybe...
  3104. */
  3105. {
  3106. int stripe = conf->geo.raid_disks *
  3107. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3108. stripe /= conf->geo.near_copies;
  3109. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  3110. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  3111. }
  3112. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  3113. if (md_integrity_register(mddev))
  3114. goto out_free_conf;
  3115. return 0;
  3116. out_free_conf:
  3117. md_unregister_thread(&mddev->thread);
  3118. if (conf->r10bio_pool)
  3119. mempool_destroy(conf->r10bio_pool);
  3120. safe_put_page(conf->tmppage);
  3121. kfree(conf->mirrors);
  3122. kfree(conf);
  3123. mddev->private = NULL;
  3124. out:
  3125. return -EIO;
  3126. }
  3127. static int stop(struct mddev *mddev)
  3128. {
  3129. struct r10conf *conf = mddev->private;
  3130. raise_barrier(conf, 0);
  3131. lower_barrier(conf);
  3132. md_unregister_thread(&mddev->thread);
  3133. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3134. if (conf->r10bio_pool)
  3135. mempool_destroy(conf->r10bio_pool);
  3136. kfree(conf->mirrors);
  3137. kfree(conf);
  3138. mddev->private = NULL;
  3139. return 0;
  3140. }
  3141. static void raid10_quiesce(struct mddev *mddev, int state)
  3142. {
  3143. struct r10conf *conf = mddev->private;
  3144. switch(state) {
  3145. case 1:
  3146. raise_barrier(conf, 0);
  3147. break;
  3148. case 0:
  3149. lower_barrier(conf);
  3150. break;
  3151. }
  3152. }
  3153. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3154. {
  3155. /* Resize of 'far' arrays is not supported.
  3156. * For 'near' and 'offset' arrays we can set the
  3157. * number of sectors used to be an appropriate multiple
  3158. * of the chunk size.
  3159. * For 'offset', this is far_copies*chunksize.
  3160. * For 'near' the multiplier is the LCM of
  3161. * near_copies and raid_disks.
  3162. * So if far_copies > 1 && !far_offset, fail.
  3163. * Else find LCM(raid_disks, near_copy)*far_copies and
  3164. * multiply by chunk_size. Then round to this number.
  3165. * This is mostly done by raid10_size()
  3166. */
  3167. struct r10conf *conf = mddev->private;
  3168. sector_t oldsize, size;
  3169. if (mddev->reshape_position != MaxSector)
  3170. return -EBUSY;
  3171. if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
  3172. return -EINVAL;
  3173. oldsize = raid10_size(mddev, 0, 0);
  3174. size = raid10_size(mddev, sectors, 0);
  3175. md_set_array_sectors(mddev, size);
  3176. if (mddev->array_sectors > size)
  3177. return -EINVAL;
  3178. set_capacity(mddev->gendisk, mddev->array_sectors);
  3179. revalidate_disk(mddev->gendisk);
  3180. if (sectors > mddev->dev_sectors &&
  3181. mddev->recovery_cp > oldsize) {
  3182. mddev->recovery_cp = oldsize;
  3183. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3184. }
  3185. calc_sectors(conf, sectors);
  3186. mddev->dev_sectors = conf->dev_sectors;
  3187. mddev->resync_max_sectors = size;
  3188. return 0;
  3189. }
  3190. static void *raid10_takeover_raid0(struct mddev *mddev)
  3191. {
  3192. struct md_rdev *rdev;
  3193. struct r10conf *conf;
  3194. if (mddev->degraded > 0) {
  3195. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3196. mdname(mddev));
  3197. return ERR_PTR(-EINVAL);
  3198. }
  3199. /* Set new parameters */
  3200. mddev->new_level = 10;
  3201. /* new layout: far_copies = 1, near_copies = 2 */
  3202. mddev->new_layout = (1<<8) + 2;
  3203. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3204. mddev->delta_disks = mddev->raid_disks;
  3205. mddev->raid_disks *= 2;
  3206. /* make sure it will be not marked as dirty */
  3207. mddev->recovery_cp = MaxSector;
  3208. conf = setup_conf(mddev);
  3209. if (!IS_ERR(conf)) {
  3210. rdev_for_each(rdev, mddev)
  3211. if (rdev->raid_disk >= 0)
  3212. rdev->new_raid_disk = rdev->raid_disk * 2;
  3213. conf->barrier = 1;
  3214. }
  3215. return conf;
  3216. }
  3217. static void *raid10_takeover(struct mddev *mddev)
  3218. {
  3219. struct r0conf *raid0_conf;
  3220. /* raid10 can take over:
  3221. * raid0 - providing it has only two drives
  3222. */
  3223. if (mddev->level == 0) {
  3224. /* for raid0 takeover only one zone is supported */
  3225. raid0_conf = mddev->private;
  3226. if (raid0_conf->nr_strip_zones > 1) {
  3227. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3228. " with more than one zone.\n",
  3229. mdname(mddev));
  3230. return ERR_PTR(-EINVAL);
  3231. }
  3232. return raid10_takeover_raid0(mddev);
  3233. }
  3234. return ERR_PTR(-EINVAL);
  3235. }
  3236. static struct md_personality raid10_personality =
  3237. {
  3238. .name = "raid10",
  3239. .level = 10,
  3240. .owner = THIS_MODULE,
  3241. .make_request = make_request,
  3242. .run = run,
  3243. .stop = stop,
  3244. .status = status,
  3245. .error_handler = error,
  3246. .hot_add_disk = raid10_add_disk,
  3247. .hot_remove_disk= raid10_remove_disk,
  3248. .spare_active = raid10_spare_active,
  3249. .sync_request = sync_request,
  3250. .quiesce = raid10_quiesce,
  3251. .size = raid10_size,
  3252. .resize = raid10_resize,
  3253. .takeover = raid10_takeover,
  3254. };
  3255. static int __init raid_init(void)
  3256. {
  3257. return register_md_personality(&raid10_personality);
  3258. }
  3259. static void raid_exit(void)
  3260. {
  3261. unregister_md_personality(&raid10_personality);
  3262. }
  3263. module_init(raid_init);
  3264. module_exit(raid_exit);
  3265. MODULE_LICENSE("GPL");
  3266. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  3267. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  3268. MODULE_ALIAS("md-raid10");
  3269. MODULE_ALIAS("md-level-10");
  3270. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);