raid10.c 60 KB

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