raid10.c 65 KB

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