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. else
  494. disk = -1;
  495. rcu_read_unlock();
  496. return disk;
  497. }
  498. static void unplug_slaves(mddev_t *mddev)
  499. {
  500. conf_t *conf = mddev_to_conf(mddev);
  501. int i;
  502. rcu_read_lock();
  503. for (i=0; i<mddev->raid_disks; i++) {
  504. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  505. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  506. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  507. atomic_inc(&rdev->nr_pending);
  508. rcu_read_unlock();
  509. if (r_queue->unplug_fn)
  510. r_queue->unplug_fn(r_queue);
  511. rdev_dec_pending(rdev, mddev);
  512. rcu_read_lock();
  513. }
  514. }
  515. rcu_read_unlock();
  516. }
  517. static void raid10_unplug(request_queue_t *q)
  518. {
  519. mddev_t *mddev = q->queuedata;
  520. unplug_slaves(q->queuedata);
  521. md_wakeup_thread(mddev->thread);
  522. }
  523. static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
  524. sector_t *error_sector)
  525. {
  526. mddev_t *mddev = q->queuedata;
  527. conf_t *conf = mddev_to_conf(mddev);
  528. int i, ret = 0;
  529. rcu_read_lock();
  530. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  531. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  532. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  533. struct block_device *bdev = rdev->bdev;
  534. request_queue_t *r_queue = bdev_get_queue(bdev);
  535. if (!r_queue->issue_flush_fn)
  536. ret = -EOPNOTSUPP;
  537. else {
  538. atomic_inc(&rdev->nr_pending);
  539. rcu_read_unlock();
  540. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  541. error_sector);
  542. rdev_dec_pending(rdev, mddev);
  543. rcu_read_lock();
  544. }
  545. }
  546. }
  547. rcu_read_unlock();
  548. return ret;
  549. }
  550. /* Barriers....
  551. * Sometimes we need to suspend IO while we do something else,
  552. * either some resync/recovery, or reconfigure the array.
  553. * To do this we raise a 'barrier'.
  554. * The 'barrier' is a counter that can be raised multiple times
  555. * to count how many activities are happening which preclude
  556. * normal IO.
  557. * We can only raise the barrier if there is no pending IO.
  558. * i.e. if nr_pending == 0.
  559. * We choose only to raise the barrier if no-one is waiting for the
  560. * barrier to go down. This means that as soon as an IO request
  561. * is ready, no other operations which require a barrier will start
  562. * until the IO request has had a chance.
  563. *
  564. * So: regular IO calls 'wait_barrier'. When that returns there
  565. * is no backgroup IO happening, It must arrange to call
  566. * allow_barrier when it has finished its IO.
  567. * backgroup IO calls must call raise_barrier. Once that returns
  568. * there is no normal IO happeing. It must arrange to call
  569. * lower_barrier when the particular background IO completes.
  570. */
  571. #define RESYNC_DEPTH 32
  572. static void raise_barrier(conf_t *conf, int force)
  573. {
  574. BUG_ON(force && !conf->barrier);
  575. spin_lock_irq(&conf->resync_lock);
  576. /* Wait until no block IO is waiting (unless 'force') */
  577. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  578. conf->resync_lock,
  579. raid10_unplug(conf->mddev->queue));
  580. /* block any new IO from starting */
  581. conf->barrier++;
  582. /* No wait for all pending IO to complete */
  583. wait_event_lock_irq(conf->wait_barrier,
  584. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  585. conf->resync_lock,
  586. raid10_unplug(conf->mddev->queue));
  587. spin_unlock_irq(&conf->resync_lock);
  588. }
  589. static void lower_barrier(conf_t *conf)
  590. {
  591. unsigned long flags;
  592. spin_lock_irqsave(&conf->resync_lock, flags);
  593. conf->barrier--;
  594. spin_unlock_irqrestore(&conf->resync_lock, flags);
  595. wake_up(&conf->wait_barrier);
  596. }
  597. static void wait_barrier(conf_t *conf)
  598. {
  599. spin_lock_irq(&conf->resync_lock);
  600. if (conf->barrier) {
  601. conf->nr_waiting++;
  602. wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
  603. conf->resync_lock,
  604. raid10_unplug(conf->mddev->queue));
  605. conf->nr_waiting--;
  606. }
  607. conf->nr_pending++;
  608. spin_unlock_irq(&conf->resync_lock);
  609. }
  610. static void allow_barrier(conf_t *conf)
  611. {
  612. unsigned long flags;
  613. spin_lock_irqsave(&conf->resync_lock, flags);
  614. conf->nr_pending--;
  615. spin_unlock_irqrestore(&conf->resync_lock, flags);
  616. wake_up(&conf->wait_barrier);
  617. }
  618. static void freeze_array(conf_t *conf)
  619. {
  620. /* stop syncio and normal IO and wait for everything to
  621. * go quiet.
  622. * We increment barrier and nr_waiting, and then
  623. * wait until barrier+nr_pending match nr_queued+2
  624. */
  625. spin_lock_irq(&conf->resync_lock);
  626. conf->barrier++;
  627. conf->nr_waiting++;
  628. wait_event_lock_irq(conf->wait_barrier,
  629. conf->barrier+conf->nr_pending == conf->nr_queued+2,
  630. conf->resync_lock,
  631. raid10_unplug(conf->mddev->queue));
  632. spin_unlock_irq(&conf->resync_lock);
  633. }
  634. static void unfreeze_array(conf_t *conf)
  635. {
  636. /* reverse the effect of the freeze */
  637. spin_lock_irq(&conf->resync_lock);
  638. conf->barrier--;
  639. conf->nr_waiting--;
  640. wake_up(&conf->wait_barrier);
  641. spin_unlock_irq(&conf->resync_lock);
  642. }
  643. static int make_request(request_queue_t *q, struct bio * bio)
  644. {
  645. mddev_t *mddev = q->queuedata;
  646. conf_t *conf = mddev_to_conf(mddev);
  647. mirror_info_t *mirror;
  648. r10bio_t *r10_bio;
  649. struct bio *read_bio;
  650. int i;
  651. int chunk_sects = conf->chunk_mask + 1;
  652. const int rw = bio_data_dir(bio);
  653. struct bio_list bl;
  654. unsigned long flags;
  655. if (unlikely(bio_barrier(bio))) {
  656. bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
  657. return 0;
  658. }
  659. /* If this request crosses a chunk boundary, we need to
  660. * split it. This will only happen for 1 PAGE (or less) requests.
  661. */
  662. if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
  663. > chunk_sects &&
  664. conf->near_copies < conf->raid_disks)) {
  665. struct bio_pair *bp;
  666. /* Sanity check -- queue functions should prevent this happening */
  667. if (bio->bi_vcnt != 1 ||
  668. bio->bi_idx != 0)
  669. goto bad_map;
  670. /* This is a one page bio that upper layers
  671. * refuse to split for us, so we need to split it.
  672. */
  673. bp = bio_split(bio, bio_split_pool,
  674. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  675. if (make_request(q, &bp->bio1))
  676. generic_make_request(&bp->bio1);
  677. if (make_request(q, &bp->bio2))
  678. generic_make_request(&bp->bio2);
  679. bio_pair_release(bp);
  680. return 0;
  681. bad_map:
  682. printk("raid10_make_request bug: can't convert block across chunks"
  683. " or bigger than %dk %llu %d\n", chunk_sects/2,
  684. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  685. bio_io_error(bio, bio->bi_size);
  686. return 0;
  687. }
  688. md_write_start(mddev, bio);
  689. /*
  690. * Register the new request and wait if the reconstruction
  691. * thread has put up a bar for new requests.
  692. * Continue immediately if no resync is active currently.
  693. */
  694. wait_barrier(conf);
  695. disk_stat_inc(mddev->gendisk, ios[rw]);
  696. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
  697. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  698. r10_bio->master_bio = bio;
  699. r10_bio->sectors = bio->bi_size >> 9;
  700. r10_bio->mddev = mddev;
  701. r10_bio->sector = bio->bi_sector;
  702. r10_bio->state = 0;
  703. if (rw == READ) {
  704. /*
  705. * read balancing logic:
  706. */
  707. int disk = read_balance(conf, r10_bio);
  708. int slot = r10_bio->read_slot;
  709. if (disk < 0) {
  710. raid_end_bio_io(r10_bio);
  711. return 0;
  712. }
  713. mirror = conf->mirrors + disk;
  714. read_bio = bio_clone(bio, GFP_NOIO);
  715. r10_bio->devs[slot].bio = read_bio;
  716. read_bio->bi_sector = r10_bio->devs[slot].addr +
  717. mirror->rdev->data_offset;
  718. read_bio->bi_bdev = mirror->rdev->bdev;
  719. read_bio->bi_end_io = raid10_end_read_request;
  720. read_bio->bi_rw = READ;
  721. read_bio->bi_private = r10_bio;
  722. generic_make_request(read_bio);
  723. return 0;
  724. }
  725. /*
  726. * WRITE:
  727. */
  728. /* first select target devices under spinlock and
  729. * inc refcount on their rdev. Record them by setting
  730. * bios[x] to bio
  731. */
  732. raid10_find_phys(conf, r10_bio);
  733. rcu_read_lock();
  734. for (i = 0; i < conf->copies; i++) {
  735. int d = r10_bio->devs[i].devnum;
  736. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
  737. if (rdev &&
  738. !test_bit(Faulty, &rdev->flags)) {
  739. atomic_inc(&rdev->nr_pending);
  740. r10_bio->devs[i].bio = bio;
  741. } else {
  742. r10_bio->devs[i].bio = NULL;
  743. set_bit(R10BIO_Degraded, &r10_bio->state);
  744. }
  745. }
  746. rcu_read_unlock();
  747. atomic_set(&r10_bio->remaining, 0);
  748. bio_list_init(&bl);
  749. for (i = 0; i < conf->copies; i++) {
  750. struct bio *mbio;
  751. int d = r10_bio->devs[i].devnum;
  752. if (!r10_bio->devs[i].bio)
  753. continue;
  754. mbio = bio_clone(bio, GFP_NOIO);
  755. r10_bio->devs[i].bio = mbio;
  756. mbio->bi_sector = r10_bio->devs[i].addr+
  757. conf->mirrors[d].rdev->data_offset;
  758. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  759. mbio->bi_end_io = raid10_end_write_request;
  760. mbio->bi_rw = WRITE;
  761. mbio->bi_private = r10_bio;
  762. atomic_inc(&r10_bio->remaining);
  763. bio_list_add(&bl, mbio);
  764. }
  765. bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
  766. spin_lock_irqsave(&conf->device_lock, flags);
  767. bio_list_merge(&conf->pending_bio_list, &bl);
  768. blk_plug_device(mddev->queue);
  769. spin_unlock_irqrestore(&conf->device_lock, flags);
  770. return 0;
  771. }
  772. static void status(struct seq_file *seq, mddev_t *mddev)
  773. {
  774. conf_t *conf = mddev_to_conf(mddev);
  775. int i;
  776. if (conf->near_copies < conf->raid_disks)
  777. seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
  778. if (conf->near_copies > 1)
  779. seq_printf(seq, " %d near-copies", conf->near_copies);
  780. if (conf->far_copies > 1)
  781. seq_printf(seq, " %d far-copies", conf->far_copies);
  782. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  783. conf->working_disks);
  784. for (i = 0; i < conf->raid_disks; i++)
  785. seq_printf(seq, "%s",
  786. conf->mirrors[i].rdev &&
  787. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  788. seq_printf(seq, "]");
  789. }
  790. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  791. {
  792. char b[BDEVNAME_SIZE];
  793. conf_t *conf = mddev_to_conf(mddev);
  794. /*
  795. * If it is not operational, then we have already marked it as dead
  796. * else if it is the last working disks, ignore the error, let the
  797. * next level up know.
  798. * else mark the drive as failed
  799. */
  800. if (test_bit(In_sync, &rdev->flags)
  801. && conf->working_disks == 1)
  802. /*
  803. * Don't fail the drive, just return an IO error.
  804. * The test should really be more sophisticated than
  805. * "working_disks == 1", but it isn't critical, and
  806. * can wait until we do more sophisticated "is the drive
  807. * really dead" tests...
  808. */
  809. return;
  810. if (test_bit(In_sync, &rdev->flags)) {
  811. mddev->degraded++;
  812. conf->working_disks--;
  813. /*
  814. * if recovery is running, make sure it aborts.
  815. */
  816. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  817. }
  818. clear_bit(In_sync, &rdev->flags);
  819. set_bit(Faulty, &rdev->flags);
  820. mddev->sb_dirty = 1;
  821. printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
  822. " Operation continuing on %d devices\n",
  823. bdevname(rdev->bdev,b), conf->working_disks);
  824. }
  825. static void print_conf(conf_t *conf)
  826. {
  827. int i;
  828. mirror_info_t *tmp;
  829. printk("RAID10 conf printout:\n");
  830. if (!conf) {
  831. printk("(!conf)\n");
  832. return;
  833. }
  834. printk(" --- wd:%d rd:%d\n", conf->working_disks,
  835. conf->raid_disks);
  836. for (i = 0; i < conf->raid_disks; i++) {
  837. char b[BDEVNAME_SIZE];
  838. tmp = conf->mirrors + i;
  839. if (tmp->rdev)
  840. printk(" disk %d, wo:%d, o:%d, dev:%s\n",
  841. i, !test_bit(In_sync, &tmp->rdev->flags),
  842. !test_bit(Faulty, &tmp->rdev->flags),
  843. bdevname(tmp->rdev->bdev,b));
  844. }
  845. }
  846. static void close_sync(conf_t *conf)
  847. {
  848. wait_barrier(conf);
  849. allow_barrier(conf);
  850. mempool_destroy(conf->r10buf_pool);
  851. conf->r10buf_pool = NULL;
  852. }
  853. /* check if there are enough drives for
  854. * every block to appear on atleast one
  855. */
  856. static int enough(conf_t *conf)
  857. {
  858. int first = 0;
  859. do {
  860. int n = conf->copies;
  861. int cnt = 0;
  862. while (n--) {
  863. if (conf->mirrors[first].rdev)
  864. cnt++;
  865. first = (first+1) % conf->raid_disks;
  866. }
  867. if (cnt == 0)
  868. return 0;
  869. } while (first != 0);
  870. return 1;
  871. }
  872. static int raid10_spare_active(mddev_t *mddev)
  873. {
  874. int i;
  875. conf_t *conf = mddev->private;
  876. mirror_info_t *tmp;
  877. /*
  878. * Find all non-in_sync disks within the RAID10 configuration
  879. * and mark them in_sync
  880. */
  881. for (i = 0; i < conf->raid_disks; i++) {
  882. tmp = conf->mirrors + i;
  883. if (tmp->rdev
  884. && !test_bit(Faulty, &tmp->rdev->flags)
  885. && !test_bit(In_sync, &tmp->rdev->flags)) {
  886. conf->working_disks++;
  887. mddev->degraded--;
  888. set_bit(In_sync, &tmp->rdev->flags);
  889. }
  890. }
  891. print_conf(conf);
  892. return 0;
  893. }
  894. static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  895. {
  896. conf_t *conf = mddev->private;
  897. int found = 0;
  898. int mirror;
  899. mirror_info_t *p;
  900. if (mddev->recovery_cp < MaxSector)
  901. /* only hot-add to in-sync arrays, as recovery is
  902. * very different from resync
  903. */
  904. return 0;
  905. if (!enough(conf))
  906. return 0;
  907. if (rdev->saved_raid_disk >= 0 &&
  908. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  909. mirror = rdev->saved_raid_disk;
  910. else
  911. mirror = 0;
  912. for ( ; mirror < mddev->raid_disks; mirror++)
  913. if ( !(p=conf->mirrors+mirror)->rdev) {
  914. blk_queue_stack_limits(mddev->queue,
  915. rdev->bdev->bd_disk->queue);
  916. /* as we don't honour merge_bvec_fn, we must never risk
  917. * violating it, so limit ->max_sector to one PAGE, as
  918. * a one page request is never in violation.
  919. */
  920. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  921. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  922. mddev->queue->max_sectors = (PAGE_SIZE>>9);
  923. p->head_position = 0;
  924. rdev->raid_disk = mirror;
  925. found = 1;
  926. if (rdev->saved_raid_disk != mirror)
  927. conf->fullsync = 1;
  928. rcu_assign_pointer(p->rdev, rdev);
  929. break;
  930. }
  931. print_conf(conf);
  932. return found;
  933. }
  934. static int raid10_remove_disk(mddev_t *mddev, int number)
  935. {
  936. conf_t *conf = mddev->private;
  937. int err = 0;
  938. mdk_rdev_t *rdev;
  939. mirror_info_t *p = conf->mirrors+ number;
  940. print_conf(conf);
  941. rdev = p->rdev;
  942. if (rdev) {
  943. if (test_bit(In_sync, &rdev->flags) ||
  944. atomic_read(&rdev->nr_pending)) {
  945. err = -EBUSY;
  946. goto abort;
  947. }
  948. p->rdev = NULL;
  949. synchronize_rcu();
  950. if (atomic_read(&rdev->nr_pending)) {
  951. /* lost the race, try later */
  952. err = -EBUSY;
  953. p->rdev = rdev;
  954. }
  955. }
  956. abort:
  957. print_conf(conf);
  958. return err;
  959. }
  960. static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
  961. {
  962. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  963. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  964. int i,d;
  965. if (bio->bi_size)
  966. return 1;
  967. for (i=0; i<conf->copies; i++)
  968. if (r10_bio->devs[i].bio == bio)
  969. break;
  970. BUG_ON(i == conf->copies);
  971. update_head_pos(i, r10_bio);
  972. d = r10_bio->devs[i].devnum;
  973. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  974. set_bit(R10BIO_Uptodate, &r10_bio->state);
  975. else {
  976. atomic_add(r10_bio->sectors,
  977. &conf->mirrors[d].rdev->corrected_errors);
  978. if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  979. md_error(r10_bio->mddev,
  980. conf->mirrors[d].rdev);
  981. }
  982. /* for reconstruct, we always reschedule after a read.
  983. * for resync, only after all reads
  984. */
  985. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  986. atomic_dec_and_test(&r10_bio->remaining)) {
  987. /* we have read all the blocks,
  988. * do the comparison in process context in raid10d
  989. */
  990. reschedule_retry(r10_bio);
  991. }
  992. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  993. return 0;
  994. }
  995. static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
  996. {
  997. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  998. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  999. mddev_t *mddev = r10_bio->mddev;
  1000. conf_t *conf = mddev_to_conf(mddev);
  1001. int i,d;
  1002. if (bio->bi_size)
  1003. return 1;
  1004. for (i = 0; i < conf->copies; i++)
  1005. if (r10_bio->devs[i].bio == bio)
  1006. break;
  1007. d = r10_bio->devs[i].devnum;
  1008. if (!uptodate)
  1009. md_error(mddev, conf->mirrors[d].rdev);
  1010. update_head_pos(i, r10_bio);
  1011. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1012. if (r10_bio->master_bio == NULL) {
  1013. /* the primary of several recovery bios */
  1014. md_done_sync(mddev, r10_bio->sectors, 1);
  1015. put_buf(r10_bio);
  1016. break;
  1017. } else {
  1018. r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
  1019. put_buf(r10_bio);
  1020. r10_bio = r10_bio2;
  1021. }
  1022. }
  1023. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1024. return 0;
  1025. }
  1026. /*
  1027. * Note: sync and recover and handled very differently for raid10
  1028. * This code is for resync.
  1029. * For resync, we read through virtual addresses and read all blocks.
  1030. * If there is any error, we schedule a write. The lowest numbered
  1031. * drive is authoritative.
  1032. * However requests come for physical address, so we need to map.
  1033. * For every physical address there are raid_disks/copies virtual addresses,
  1034. * which is always are least one, but is not necessarly an integer.
  1035. * This means that a physical address can span multiple chunks, so we may
  1036. * have to submit multiple io requests for a single sync request.
  1037. */
  1038. /*
  1039. * We check if all blocks are in-sync and only write to blocks that
  1040. * aren't in sync
  1041. */
  1042. static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1043. {
  1044. conf_t *conf = mddev_to_conf(mddev);
  1045. int i, first;
  1046. struct bio *tbio, *fbio;
  1047. atomic_set(&r10_bio->remaining, 1);
  1048. /* find the first device with a block */
  1049. for (i=0; i<conf->copies; i++)
  1050. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1051. break;
  1052. if (i == conf->copies)
  1053. goto done;
  1054. first = i;
  1055. fbio = r10_bio->devs[i].bio;
  1056. /* now find blocks with errors */
  1057. for (i=0 ; i < conf->copies ; i++) {
  1058. int j, d;
  1059. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1060. tbio = r10_bio->devs[i].bio;
  1061. if (tbio->bi_end_io != end_sync_read)
  1062. continue;
  1063. if (i == first)
  1064. continue;
  1065. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1066. /* We know that the bi_io_vec layout is the same for
  1067. * both 'first' and 'i', so we just compare them.
  1068. * All vec entries are PAGE_SIZE;
  1069. */
  1070. for (j = 0; j < vcnt; j++)
  1071. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1072. page_address(tbio->bi_io_vec[j].bv_page),
  1073. PAGE_SIZE))
  1074. break;
  1075. if (j == vcnt)
  1076. continue;
  1077. mddev->resync_mismatches += r10_bio->sectors;
  1078. }
  1079. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1080. /* Don't fix anything. */
  1081. continue;
  1082. /* Ok, we need to write this bio
  1083. * First we need to fixup bv_offset, bv_len and
  1084. * bi_vecs, as the read request might have corrupted these
  1085. */
  1086. tbio->bi_vcnt = vcnt;
  1087. tbio->bi_size = r10_bio->sectors << 9;
  1088. tbio->bi_idx = 0;
  1089. tbio->bi_phys_segments = 0;
  1090. tbio->bi_hw_segments = 0;
  1091. tbio->bi_hw_front_size = 0;
  1092. tbio->bi_hw_back_size = 0;
  1093. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1094. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1095. tbio->bi_next = NULL;
  1096. tbio->bi_rw = WRITE;
  1097. tbio->bi_private = r10_bio;
  1098. tbio->bi_sector = r10_bio->devs[i].addr;
  1099. for (j=0; j < vcnt ; j++) {
  1100. tbio->bi_io_vec[j].bv_offset = 0;
  1101. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1102. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1103. page_address(fbio->bi_io_vec[j].bv_page),
  1104. PAGE_SIZE);
  1105. }
  1106. tbio->bi_end_io = end_sync_write;
  1107. d = r10_bio->devs[i].devnum;
  1108. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1109. atomic_inc(&r10_bio->remaining);
  1110. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1111. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1112. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1113. generic_make_request(tbio);
  1114. }
  1115. done:
  1116. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1117. md_done_sync(mddev, r10_bio->sectors, 1);
  1118. put_buf(r10_bio);
  1119. }
  1120. }
  1121. /*
  1122. * Now for the recovery code.
  1123. * Recovery happens across physical sectors.
  1124. * We recover all non-is_sync drives by finding the virtual address of
  1125. * each, and then choose a working drive that also has that virt address.
  1126. * There is a separate r10_bio for each non-in_sync drive.
  1127. * Only the first two slots are in use. The first for reading,
  1128. * The second for writing.
  1129. *
  1130. */
  1131. static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1132. {
  1133. conf_t *conf = mddev_to_conf(mddev);
  1134. int i, d;
  1135. struct bio *bio, *wbio;
  1136. /* move the pages across to the second bio
  1137. * and submit the write request
  1138. */
  1139. bio = r10_bio->devs[0].bio;
  1140. wbio = r10_bio->devs[1].bio;
  1141. for (i=0; i < wbio->bi_vcnt; i++) {
  1142. struct page *p = bio->bi_io_vec[i].bv_page;
  1143. bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
  1144. wbio->bi_io_vec[i].bv_page = p;
  1145. }
  1146. d = r10_bio->devs[1].devnum;
  1147. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1148. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1149. if (test_bit(R10BIO_Uptodate, &r10_bio->state))
  1150. generic_make_request(wbio);
  1151. else
  1152. bio_endio(wbio, wbio->bi_size, -EIO);
  1153. }
  1154. /*
  1155. * This is a kernel thread which:
  1156. *
  1157. * 1. Retries failed read operations on working mirrors.
  1158. * 2. Updates the raid superblock when problems encounter.
  1159. * 3. Performs writes following reads for array syncronising.
  1160. */
  1161. static void raid10d(mddev_t *mddev)
  1162. {
  1163. r10bio_t *r10_bio;
  1164. struct bio *bio;
  1165. unsigned long flags;
  1166. conf_t *conf = mddev_to_conf(mddev);
  1167. struct list_head *head = &conf->retry_list;
  1168. int unplug=0;
  1169. mdk_rdev_t *rdev;
  1170. md_check_recovery(mddev);
  1171. for (;;) {
  1172. char b[BDEVNAME_SIZE];
  1173. spin_lock_irqsave(&conf->device_lock, flags);
  1174. if (conf->pending_bio_list.head) {
  1175. bio = bio_list_get(&conf->pending_bio_list);
  1176. blk_remove_plug(mddev->queue);
  1177. spin_unlock_irqrestore(&conf->device_lock, flags);
  1178. /* flush any pending bitmap writes to disk before proceeding w/ I/O */
  1179. if (bitmap_unplug(mddev->bitmap) != 0)
  1180. printk("%s: bitmap file write failed!\n", mdname(mddev));
  1181. while (bio) { /* submit pending writes */
  1182. struct bio *next = bio->bi_next;
  1183. bio->bi_next = NULL;
  1184. generic_make_request(bio);
  1185. bio = next;
  1186. }
  1187. unplug = 1;
  1188. continue;
  1189. }
  1190. if (list_empty(head))
  1191. break;
  1192. r10_bio = list_entry(head->prev, r10bio_t, retry_list);
  1193. list_del(head->prev);
  1194. conf->nr_queued--;
  1195. spin_unlock_irqrestore(&conf->device_lock, flags);
  1196. mddev = r10_bio->mddev;
  1197. conf = mddev_to_conf(mddev);
  1198. if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
  1199. sync_request_write(mddev, r10_bio);
  1200. unplug = 1;
  1201. } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  1202. recovery_request_write(mddev, r10_bio);
  1203. unplug = 1;
  1204. } else {
  1205. int mirror;
  1206. /* we got a read error. Maybe the drive is bad. Maybe just
  1207. * the block and we can fix it.
  1208. * We freeze all other IO, and try reading the block from
  1209. * other devices. When we find one, we re-write
  1210. * and check it that fixes the read error.
  1211. * This is all done synchronously while the array is
  1212. * frozen.
  1213. */
  1214. int sect = 0; /* Offset from r10_bio->sector */
  1215. int sectors = r10_bio->sectors;
  1216. freeze_array(conf);
  1217. if (mddev->ro == 0) while(sectors) {
  1218. int s = sectors;
  1219. int sl = r10_bio->read_slot;
  1220. int success = 0;
  1221. if (s > (PAGE_SIZE>>9))
  1222. s = PAGE_SIZE >> 9;
  1223. do {
  1224. int d = r10_bio->devs[sl].devnum;
  1225. rdev = conf->mirrors[d].rdev;
  1226. if (rdev &&
  1227. test_bit(In_sync, &rdev->flags) &&
  1228. sync_page_io(rdev->bdev,
  1229. r10_bio->devs[sl].addr +
  1230. sect + rdev->data_offset,
  1231. s<<9,
  1232. conf->tmppage, READ))
  1233. success = 1;
  1234. else {
  1235. sl++;
  1236. if (sl == conf->copies)
  1237. sl = 0;
  1238. }
  1239. } while (!success && sl != r10_bio->read_slot);
  1240. if (success) {
  1241. int start = sl;
  1242. /* write it back and re-read */
  1243. while (sl != r10_bio->read_slot) {
  1244. int d;
  1245. if (sl==0)
  1246. sl = conf->copies;
  1247. sl--;
  1248. d = r10_bio->devs[sl].devnum;
  1249. rdev = conf->mirrors[d].rdev;
  1250. atomic_add(s, &rdev->corrected_errors);
  1251. if (rdev &&
  1252. test_bit(In_sync, &rdev->flags)) {
  1253. if (sync_page_io(rdev->bdev,
  1254. r10_bio->devs[sl].addr +
  1255. sect + rdev->data_offset,
  1256. s<<9, conf->tmppage, WRITE) == 0)
  1257. /* Well, this device is dead */
  1258. md_error(mddev, rdev);
  1259. }
  1260. }
  1261. sl = start;
  1262. while (sl != r10_bio->read_slot) {
  1263. int d;
  1264. if (sl==0)
  1265. sl = conf->copies;
  1266. sl--;
  1267. d = r10_bio->devs[sl].devnum;
  1268. rdev = conf->mirrors[d].rdev;
  1269. if (rdev &&
  1270. test_bit(In_sync, &rdev->flags)) {
  1271. if (sync_page_io(rdev->bdev,
  1272. r10_bio->devs[sl].addr +
  1273. sect + rdev->data_offset,
  1274. s<<9, conf->tmppage, READ) == 0)
  1275. /* Well, this device is dead */
  1276. md_error(mddev, rdev);
  1277. }
  1278. }
  1279. } else {
  1280. /* Cannot read from anywhere -- bye bye array */
  1281. md_error(mddev, conf->mirrors[r10_bio->devs[r10_bio->read_slot].devnum].rdev);
  1282. break;
  1283. }
  1284. sectors -= s;
  1285. sect += s;
  1286. }
  1287. unfreeze_array(conf);
  1288. bio = r10_bio->devs[r10_bio->read_slot].bio;
  1289. r10_bio->devs[r10_bio->read_slot].bio =
  1290. mddev->ro ? IO_BLOCKED : NULL;
  1291. bio_put(bio);
  1292. mirror = read_balance(conf, r10_bio);
  1293. if (mirror == -1) {
  1294. printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
  1295. " read error for block %llu\n",
  1296. bdevname(bio->bi_bdev,b),
  1297. (unsigned long long)r10_bio->sector);
  1298. raid_end_bio_io(r10_bio);
  1299. } else {
  1300. rdev = conf->mirrors[mirror].rdev;
  1301. if (printk_ratelimit())
  1302. printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
  1303. " another mirror\n",
  1304. bdevname(rdev->bdev,b),
  1305. (unsigned long long)r10_bio->sector);
  1306. bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
  1307. r10_bio->devs[r10_bio->read_slot].bio = bio;
  1308. bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
  1309. + rdev->data_offset;
  1310. bio->bi_bdev = rdev->bdev;
  1311. bio->bi_rw = READ;
  1312. bio->bi_private = r10_bio;
  1313. bio->bi_end_io = raid10_end_read_request;
  1314. unplug = 1;
  1315. generic_make_request(bio);
  1316. }
  1317. }
  1318. }
  1319. spin_unlock_irqrestore(&conf->device_lock, flags);
  1320. if (unplug)
  1321. unplug_slaves(mddev);
  1322. }
  1323. static int init_resync(conf_t *conf)
  1324. {
  1325. int buffs;
  1326. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1327. BUG_ON(conf->r10buf_pool);
  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");