raid10.c 66 KB

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