raid10.c 60 KB

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