raid10.c 66 KB

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