raid10.c 59 KB

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