raid10.c 59 KB

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