raid10.c 58 KB

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