raid10.c 60 KB

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