raid10.c 61 KB

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