raid10.c 82 KB

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