raid10.c 57 KB

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