raid10.c 58 KB

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