raid10.c 60 KB

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