raid10.c 57 KB

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