raid10.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  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 err = -EEXIST;
  974. int mirror;
  975. mirror_info_t *p;
  976. int first = 0;
  977. int last = mddev->raid_disks - 1;
  978. if (mddev->recovery_cp < MaxSector)
  979. /* only hot-add to in-sync arrays, as recovery is
  980. * very different from resync
  981. */
  982. return -EBUSY;
  983. if (!enough(conf))
  984. return -EINVAL;
  985. if (rdev->raid_disk)
  986. first = last = rdev->raid_disk;
  987. if (rdev->saved_raid_disk >= 0 &&
  988. rdev->saved_raid_disk >= first &&
  989. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  990. mirror = rdev->saved_raid_disk;
  991. else
  992. mirror = first;
  993. for ( ; mirror <= last ; mirror++)
  994. if ( !(p=conf->mirrors+mirror)->rdev) {
  995. blk_queue_stack_limits(mddev->queue,
  996. rdev->bdev->bd_disk->queue);
  997. /* as we don't honour merge_bvec_fn, we must never risk
  998. * violating it, so limit ->max_sector to one PAGE, as
  999. * a one page request is never in violation.
  1000. */
  1001. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1002. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  1003. mddev->queue->max_sectors = (PAGE_SIZE>>9);
  1004. p->head_position = 0;
  1005. rdev->raid_disk = mirror;
  1006. err = 0;
  1007. if (rdev->saved_raid_disk != mirror)
  1008. conf->fullsync = 1;
  1009. rcu_assign_pointer(p->rdev, rdev);
  1010. break;
  1011. }
  1012. print_conf(conf);
  1013. return err;
  1014. }
  1015. static int raid10_remove_disk(mddev_t *mddev, int number)
  1016. {
  1017. conf_t *conf = mddev->private;
  1018. int err = 0;
  1019. mdk_rdev_t *rdev;
  1020. mirror_info_t *p = conf->mirrors+ number;
  1021. print_conf(conf);
  1022. rdev = p->rdev;
  1023. if (rdev) {
  1024. if (test_bit(In_sync, &rdev->flags) ||
  1025. atomic_read(&rdev->nr_pending)) {
  1026. err = -EBUSY;
  1027. goto abort;
  1028. }
  1029. /* Only remove faulty devices in recovery
  1030. * is not possible.
  1031. */
  1032. if (!test_bit(Faulty, &rdev->flags) &&
  1033. enough(conf)) {
  1034. err = -EBUSY;
  1035. goto abort;
  1036. }
  1037. p->rdev = NULL;
  1038. synchronize_rcu();
  1039. if (atomic_read(&rdev->nr_pending)) {
  1040. /* lost the race, try later */
  1041. err = -EBUSY;
  1042. p->rdev = rdev;
  1043. }
  1044. }
  1045. abort:
  1046. print_conf(conf);
  1047. return err;
  1048. }
  1049. static void end_sync_read(struct bio *bio, int error)
  1050. {
  1051. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  1052. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  1053. int i,d;
  1054. for (i=0; i<conf->copies; i++)
  1055. if (r10_bio->devs[i].bio == bio)
  1056. break;
  1057. BUG_ON(i == conf->copies);
  1058. update_head_pos(i, r10_bio);
  1059. d = r10_bio->devs[i].devnum;
  1060. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1061. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1062. else {
  1063. atomic_add(r10_bio->sectors,
  1064. &conf->mirrors[d].rdev->corrected_errors);
  1065. if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  1066. md_error(r10_bio->mddev,
  1067. conf->mirrors[d].rdev);
  1068. }
  1069. /* for reconstruct, we always reschedule after a read.
  1070. * for resync, only after all reads
  1071. */
  1072. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1073. atomic_dec_and_test(&r10_bio->remaining)) {
  1074. /* we have read all the blocks,
  1075. * do the comparison in process context in raid10d
  1076. */
  1077. reschedule_retry(r10_bio);
  1078. }
  1079. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1080. }
  1081. static void end_sync_write(struct bio *bio, int error)
  1082. {
  1083. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1084. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  1085. mddev_t *mddev = r10_bio->mddev;
  1086. conf_t *conf = mddev_to_conf(mddev);
  1087. int i,d;
  1088. for (i = 0; i < conf->copies; i++)
  1089. if (r10_bio->devs[i].bio == bio)
  1090. break;
  1091. d = r10_bio->devs[i].devnum;
  1092. if (!uptodate)
  1093. md_error(mddev, conf->mirrors[d].rdev);
  1094. update_head_pos(i, r10_bio);
  1095. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1096. if (r10_bio->master_bio == NULL) {
  1097. /* the primary of several recovery bios */
  1098. md_done_sync(mddev, r10_bio->sectors, 1);
  1099. put_buf(r10_bio);
  1100. break;
  1101. } else {
  1102. r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
  1103. put_buf(r10_bio);
  1104. r10_bio = r10_bio2;
  1105. }
  1106. }
  1107. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1108. }
  1109. /*
  1110. * Note: sync and recover and handled very differently for raid10
  1111. * This code is for resync.
  1112. * For resync, we read through virtual addresses and read all blocks.
  1113. * If there is any error, we schedule a write. The lowest numbered
  1114. * drive is authoritative.
  1115. * However requests come for physical address, so we need to map.
  1116. * For every physical address there are raid_disks/copies virtual addresses,
  1117. * which is always are least one, but is not necessarly an integer.
  1118. * This means that a physical address can span multiple chunks, so we may
  1119. * have to submit multiple io requests for a single sync request.
  1120. */
  1121. /*
  1122. * We check if all blocks are in-sync and only write to blocks that
  1123. * aren't in sync
  1124. */
  1125. static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1126. {
  1127. conf_t *conf = mddev_to_conf(mddev);
  1128. int i, first;
  1129. struct bio *tbio, *fbio;
  1130. atomic_set(&r10_bio->remaining, 1);
  1131. /* find the first device with a block */
  1132. for (i=0; i<conf->copies; i++)
  1133. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1134. break;
  1135. if (i == conf->copies)
  1136. goto done;
  1137. first = i;
  1138. fbio = r10_bio->devs[i].bio;
  1139. /* now find blocks with errors */
  1140. for (i=0 ; i < conf->copies ; i++) {
  1141. int j, d;
  1142. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1143. tbio = r10_bio->devs[i].bio;
  1144. if (tbio->bi_end_io != end_sync_read)
  1145. continue;
  1146. if (i == first)
  1147. continue;
  1148. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1149. /* We know that the bi_io_vec layout is the same for
  1150. * both 'first' and 'i', so we just compare them.
  1151. * All vec entries are PAGE_SIZE;
  1152. */
  1153. for (j = 0; j < vcnt; j++)
  1154. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1155. page_address(tbio->bi_io_vec[j].bv_page),
  1156. PAGE_SIZE))
  1157. break;
  1158. if (j == vcnt)
  1159. continue;
  1160. mddev->resync_mismatches += r10_bio->sectors;
  1161. }
  1162. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1163. /* Don't fix anything. */
  1164. continue;
  1165. /* Ok, we need to write this bio
  1166. * First we need to fixup bv_offset, bv_len and
  1167. * bi_vecs, as the read request might have corrupted these
  1168. */
  1169. tbio->bi_vcnt = vcnt;
  1170. tbio->bi_size = r10_bio->sectors << 9;
  1171. tbio->bi_idx = 0;
  1172. tbio->bi_phys_segments = 0;
  1173. tbio->bi_hw_segments = 0;
  1174. tbio->bi_hw_front_size = 0;
  1175. tbio->bi_hw_back_size = 0;
  1176. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1177. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1178. tbio->bi_next = NULL;
  1179. tbio->bi_rw = WRITE;
  1180. tbio->bi_private = r10_bio;
  1181. tbio->bi_sector = r10_bio->devs[i].addr;
  1182. for (j=0; j < vcnt ; j++) {
  1183. tbio->bi_io_vec[j].bv_offset = 0;
  1184. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1185. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1186. page_address(fbio->bi_io_vec[j].bv_page),
  1187. PAGE_SIZE);
  1188. }
  1189. tbio->bi_end_io = end_sync_write;
  1190. d = r10_bio->devs[i].devnum;
  1191. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1192. atomic_inc(&r10_bio->remaining);
  1193. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1194. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1195. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1196. generic_make_request(tbio);
  1197. }
  1198. done:
  1199. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1200. md_done_sync(mddev, r10_bio->sectors, 1);
  1201. put_buf(r10_bio);
  1202. }
  1203. }
  1204. /*
  1205. * Now for the recovery code.
  1206. * Recovery happens across physical sectors.
  1207. * We recover all non-is_sync drives by finding the virtual address of
  1208. * each, and then choose a working drive that also has that virt address.
  1209. * There is a separate r10_bio for each non-in_sync drive.
  1210. * Only the first two slots are in use. The first for reading,
  1211. * The second for writing.
  1212. *
  1213. */
  1214. static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1215. {
  1216. conf_t *conf = mddev_to_conf(mddev);
  1217. int i, d;
  1218. struct bio *bio, *wbio;
  1219. /* move the pages across to the second bio
  1220. * and submit the write request
  1221. */
  1222. bio = r10_bio->devs[0].bio;
  1223. wbio = r10_bio->devs[1].bio;
  1224. for (i=0; i < wbio->bi_vcnt; i++) {
  1225. struct page *p = bio->bi_io_vec[i].bv_page;
  1226. bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
  1227. wbio->bi_io_vec[i].bv_page = p;
  1228. }
  1229. d = r10_bio->devs[1].devnum;
  1230. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1231. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1232. if (test_bit(R10BIO_Uptodate, &r10_bio->state))
  1233. generic_make_request(wbio);
  1234. else
  1235. bio_endio(wbio, -EIO);
  1236. }
  1237. /*
  1238. * This is a kernel thread which:
  1239. *
  1240. * 1. Retries failed read operations on working mirrors.
  1241. * 2. Updates the raid superblock when problems encounter.
  1242. * 3. Performs writes following reads for array synchronising.
  1243. */
  1244. static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
  1245. {
  1246. int sect = 0; /* Offset from r10_bio->sector */
  1247. int sectors = r10_bio->sectors;
  1248. mdk_rdev_t*rdev;
  1249. while(sectors) {
  1250. int s = sectors;
  1251. int sl = r10_bio->read_slot;
  1252. int success = 0;
  1253. int start;
  1254. if (s > (PAGE_SIZE>>9))
  1255. s = PAGE_SIZE >> 9;
  1256. rcu_read_lock();
  1257. do {
  1258. int d = r10_bio->devs[sl].devnum;
  1259. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1260. if (rdev &&
  1261. test_bit(In_sync, &rdev->flags)) {
  1262. atomic_inc(&rdev->nr_pending);
  1263. rcu_read_unlock();
  1264. success = sync_page_io(rdev->bdev,
  1265. r10_bio->devs[sl].addr +
  1266. sect + rdev->data_offset,
  1267. s<<9,
  1268. conf->tmppage, READ);
  1269. rdev_dec_pending(rdev, mddev);
  1270. rcu_read_lock();
  1271. if (success)
  1272. break;
  1273. }
  1274. sl++;
  1275. if (sl == conf->copies)
  1276. sl = 0;
  1277. } while (!success && sl != r10_bio->read_slot);
  1278. rcu_read_unlock();
  1279. if (!success) {
  1280. /* Cannot read from anywhere -- bye bye array */
  1281. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  1282. md_error(mddev, conf->mirrors[dn].rdev);
  1283. break;
  1284. }
  1285. start = sl;
  1286. /* write it back and re-read */
  1287. rcu_read_lock();
  1288. while (sl != r10_bio->read_slot) {
  1289. int d;
  1290. if (sl==0)
  1291. sl = conf->copies;
  1292. sl--;
  1293. d = r10_bio->devs[sl].devnum;
  1294. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1295. if (rdev &&
  1296. test_bit(In_sync, &rdev->flags)) {
  1297. atomic_inc(&rdev->nr_pending);
  1298. rcu_read_unlock();
  1299. atomic_add(s, &rdev->corrected_errors);
  1300. if (sync_page_io(rdev->bdev,
  1301. r10_bio->devs[sl].addr +
  1302. sect + rdev->data_offset,
  1303. s<<9, conf->tmppage, WRITE)
  1304. == 0)
  1305. /* Well, this device is dead */
  1306. md_error(mddev, rdev);
  1307. rdev_dec_pending(rdev, mddev);
  1308. rcu_read_lock();
  1309. }
  1310. }
  1311. sl = start;
  1312. while (sl != r10_bio->read_slot) {
  1313. int d;
  1314. if (sl==0)
  1315. sl = conf->copies;
  1316. sl--;
  1317. d = r10_bio->devs[sl].devnum;
  1318. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1319. if (rdev &&
  1320. test_bit(In_sync, &rdev->flags)) {
  1321. char b[BDEVNAME_SIZE];
  1322. atomic_inc(&rdev->nr_pending);
  1323. rcu_read_unlock();
  1324. if (sync_page_io(rdev->bdev,
  1325. r10_bio->devs[sl].addr +
  1326. sect + rdev->data_offset,
  1327. s<<9, conf->tmppage, READ) == 0)
  1328. /* Well, this device is dead */
  1329. md_error(mddev, rdev);
  1330. else
  1331. printk(KERN_INFO
  1332. "raid10:%s: read error corrected"
  1333. " (%d sectors at %llu on %s)\n",
  1334. mdname(mddev), s,
  1335. (unsigned long long)(sect+
  1336. rdev->data_offset),
  1337. bdevname(rdev->bdev, b));
  1338. rdev_dec_pending(rdev, mddev);
  1339. rcu_read_lock();
  1340. }
  1341. }
  1342. rcu_read_unlock();
  1343. sectors -= s;
  1344. sect += s;
  1345. }
  1346. }
  1347. static void raid10d(mddev_t *mddev)
  1348. {
  1349. r10bio_t *r10_bio;
  1350. struct bio *bio;
  1351. unsigned long flags;
  1352. conf_t *conf = mddev_to_conf(mddev);
  1353. struct list_head *head = &conf->retry_list;
  1354. int unplug=0;
  1355. mdk_rdev_t *rdev;
  1356. md_check_recovery(mddev);
  1357. for (;;) {
  1358. char b[BDEVNAME_SIZE];
  1359. unplug += flush_pending_writes(conf);
  1360. spin_lock_irqsave(&conf->device_lock, flags);
  1361. if (list_empty(head)) {
  1362. spin_unlock_irqrestore(&conf->device_lock, flags);
  1363. break;
  1364. }
  1365. r10_bio = list_entry(head->prev, r10bio_t, retry_list);
  1366. list_del(head->prev);
  1367. conf->nr_queued--;
  1368. spin_unlock_irqrestore(&conf->device_lock, flags);
  1369. mddev = r10_bio->mddev;
  1370. conf = mddev_to_conf(mddev);
  1371. if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
  1372. sync_request_write(mddev, r10_bio);
  1373. unplug = 1;
  1374. } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  1375. recovery_request_write(mddev, r10_bio);
  1376. unplug = 1;
  1377. } else {
  1378. int mirror;
  1379. /* we got a read error. Maybe the drive is bad. Maybe just
  1380. * the block and we can fix it.
  1381. * We freeze all other IO, and try reading the block from
  1382. * other devices. When we find one, we re-write
  1383. * and check it that fixes the read error.
  1384. * This is all done synchronously while the array is
  1385. * frozen.
  1386. */
  1387. if (mddev->ro == 0) {
  1388. freeze_array(conf);
  1389. fix_read_error(conf, mddev, r10_bio);
  1390. unfreeze_array(conf);
  1391. }
  1392. bio = r10_bio->devs[r10_bio->read_slot].bio;
  1393. r10_bio->devs[r10_bio->read_slot].bio =
  1394. mddev->ro ? IO_BLOCKED : NULL;
  1395. mirror = read_balance(conf, r10_bio);
  1396. if (mirror == -1) {
  1397. printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
  1398. " read error for block %llu\n",
  1399. bdevname(bio->bi_bdev,b),
  1400. (unsigned long long)r10_bio->sector);
  1401. raid_end_bio_io(r10_bio);
  1402. bio_put(bio);
  1403. } else {
  1404. const int do_sync = bio_sync(r10_bio->master_bio);
  1405. bio_put(bio);
  1406. rdev = conf->mirrors[mirror].rdev;
  1407. if (printk_ratelimit())
  1408. printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
  1409. " another mirror\n",
  1410. bdevname(rdev->bdev,b),
  1411. (unsigned long long)r10_bio->sector);
  1412. bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
  1413. r10_bio->devs[r10_bio->read_slot].bio = bio;
  1414. bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
  1415. + rdev->data_offset;
  1416. bio->bi_bdev = rdev->bdev;
  1417. bio->bi_rw = READ | do_sync;
  1418. bio->bi_private = r10_bio;
  1419. bio->bi_end_io = raid10_end_read_request;
  1420. unplug = 1;
  1421. generic_make_request(bio);
  1422. }
  1423. }
  1424. }
  1425. if (unplug)
  1426. unplug_slaves(mddev);
  1427. }
  1428. static int init_resync(conf_t *conf)
  1429. {
  1430. int buffs;
  1431. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1432. BUG_ON(conf->r10buf_pool);
  1433. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  1434. if (!conf->r10buf_pool)
  1435. return -ENOMEM;
  1436. conf->next_resync = 0;
  1437. return 0;
  1438. }
  1439. /*
  1440. * perform a "sync" on one "block"
  1441. *
  1442. * We need to make sure that no normal I/O request - particularly write
  1443. * requests - conflict with active sync requests.
  1444. *
  1445. * This is achieved by tracking pending requests and a 'barrier' concept
  1446. * that can be installed to exclude normal IO requests.
  1447. *
  1448. * Resync and recovery are handled very differently.
  1449. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  1450. *
  1451. * For resync, we iterate over virtual addresses, read all copies,
  1452. * and update if there are differences. If only one copy is live,
  1453. * skip it.
  1454. * For recovery, we iterate over physical addresses, read a good
  1455. * value for each non-in_sync drive, and over-write.
  1456. *
  1457. * So, for recovery we may have several outstanding complex requests for a
  1458. * given address, one for each out-of-sync device. We model this by allocating
  1459. * a number of r10_bio structures, one for each out-of-sync device.
  1460. * As we setup these structures, we collect all bio's together into a list
  1461. * which we then process collectively to add pages, and then process again
  1462. * to pass to generic_make_request.
  1463. *
  1464. * The r10_bio structures are linked using a borrowed master_bio pointer.
  1465. * This link is counted in ->remaining. When the r10_bio that points to NULL
  1466. * has its remaining count decremented to 0, the whole complex operation
  1467. * is complete.
  1468. *
  1469. */
  1470. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1471. {
  1472. conf_t *conf = mddev_to_conf(mddev);
  1473. r10bio_t *r10_bio;
  1474. struct bio *biolist = NULL, *bio;
  1475. sector_t max_sector, nr_sectors;
  1476. int disk;
  1477. int i;
  1478. int max_sync;
  1479. int sync_blocks;
  1480. sector_t sectors_skipped = 0;
  1481. int chunks_skipped = 0;
  1482. if (!conf->r10buf_pool)
  1483. if (init_resync(conf))
  1484. return 0;
  1485. skipped:
  1486. max_sector = mddev->size << 1;
  1487. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1488. max_sector = mddev->resync_max_sectors;
  1489. if (sector_nr >= max_sector) {
  1490. /* If we aborted, we need to abort the
  1491. * sync on the 'current' bitmap chucks (there can
  1492. * be several when recovering multiple devices).
  1493. * as we may have started syncing it but not finished.
  1494. * We can find the current address in
  1495. * mddev->curr_resync, but for recovery,
  1496. * we need to convert that to several
  1497. * virtual addresses.
  1498. */
  1499. if (mddev->curr_resync < max_sector) { /* aborted */
  1500. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1501. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1502. &sync_blocks, 1);
  1503. else for (i=0; i<conf->raid_disks; i++) {
  1504. sector_t sect =
  1505. raid10_find_virt(conf, mddev->curr_resync, i);
  1506. bitmap_end_sync(mddev->bitmap, sect,
  1507. &sync_blocks, 1);
  1508. }
  1509. } else /* completed sync */
  1510. conf->fullsync = 0;
  1511. bitmap_close_sync(mddev->bitmap);
  1512. close_sync(conf);
  1513. *skipped = 1;
  1514. return sectors_skipped;
  1515. }
  1516. if (chunks_skipped >= conf->raid_disks) {
  1517. /* if there has been nothing to do on any drive,
  1518. * then there is nothing to do at all..
  1519. */
  1520. *skipped = 1;
  1521. return (max_sector - sector_nr) + sectors_skipped;
  1522. }
  1523. if (max_sector > mddev->resync_max)
  1524. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  1525. /* make sure whole request will fit in a chunk - if chunks
  1526. * are meaningful
  1527. */
  1528. if (conf->near_copies < conf->raid_disks &&
  1529. max_sector > (sector_nr | conf->chunk_mask))
  1530. max_sector = (sector_nr | conf->chunk_mask) + 1;
  1531. /*
  1532. * If there is non-resync activity waiting for us then
  1533. * put in a delay to throttle resync.
  1534. */
  1535. if (!go_faster && conf->nr_waiting)
  1536. msleep_interruptible(1000);
  1537. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  1538. /* Again, very different code for resync and recovery.
  1539. * Both must result in an r10bio with a list of bios that
  1540. * have bi_end_io, bi_sector, bi_bdev set,
  1541. * and bi_private set to the r10bio.
  1542. * For recovery, we may actually create several r10bios
  1543. * with 2 bios in each, that correspond to the bios in the main one.
  1544. * In this case, the subordinate r10bios link back through a
  1545. * borrowed master_bio pointer, and the counter in the master
  1546. * includes a ref from each subordinate.
  1547. */
  1548. /* First, we decide what to do and set ->bi_end_io
  1549. * To end_sync_read if we want to read, and
  1550. * end_sync_write if we will want to write.
  1551. */
  1552. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  1553. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1554. /* recovery... the complicated one */
  1555. int i, j, k;
  1556. r10_bio = NULL;
  1557. for (i=0 ; i<conf->raid_disks; i++)
  1558. if (conf->mirrors[i].rdev &&
  1559. !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
  1560. int still_degraded = 0;
  1561. /* want to reconstruct this device */
  1562. r10bio_t *rb2 = r10_bio;
  1563. sector_t sect = raid10_find_virt(conf, sector_nr, i);
  1564. int must_sync;
  1565. /* Unless we are doing a full sync, we only need
  1566. * to recover the block if it is set in the bitmap
  1567. */
  1568. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1569. &sync_blocks, 1);
  1570. if (sync_blocks < max_sync)
  1571. max_sync = sync_blocks;
  1572. if (!must_sync &&
  1573. !conf->fullsync) {
  1574. /* yep, skip the sync_blocks here, but don't assume
  1575. * that there will never be anything to do here
  1576. */
  1577. chunks_skipped = -1;
  1578. continue;
  1579. }
  1580. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1581. raise_barrier(conf, rb2 != NULL);
  1582. atomic_set(&r10_bio->remaining, 0);
  1583. r10_bio->master_bio = (struct bio*)rb2;
  1584. if (rb2)
  1585. atomic_inc(&rb2->remaining);
  1586. r10_bio->mddev = mddev;
  1587. set_bit(R10BIO_IsRecover, &r10_bio->state);
  1588. r10_bio->sector = sect;
  1589. raid10_find_phys(conf, r10_bio);
  1590. /* Need to check if this section will still be
  1591. * degraded
  1592. */
  1593. for (j=0; j<conf->copies;j++) {
  1594. int d = r10_bio->devs[j].devnum;
  1595. if (conf->mirrors[d].rdev == NULL ||
  1596. test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
  1597. still_degraded = 1;
  1598. break;
  1599. }
  1600. }
  1601. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1602. &sync_blocks, still_degraded);
  1603. for (j=0; j<conf->copies;j++) {
  1604. int d = r10_bio->devs[j].devnum;
  1605. if (conf->mirrors[d].rdev &&
  1606. test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
  1607. /* This is where we read from */
  1608. bio = r10_bio->devs[0].bio;
  1609. bio->bi_next = biolist;
  1610. biolist = bio;
  1611. bio->bi_private = r10_bio;
  1612. bio->bi_end_io = end_sync_read;
  1613. bio->bi_rw = READ;
  1614. bio->bi_sector = r10_bio->devs[j].addr +
  1615. conf->mirrors[d].rdev->data_offset;
  1616. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1617. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1618. atomic_inc(&r10_bio->remaining);
  1619. /* and we write to 'i' */
  1620. for (k=0; k<conf->copies; k++)
  1621. if (r10_bio->devs[k].devnum == i)
  1622. break;
  1623. BUG_ON(k == conf->copies);
  1624. bio = r10_bio->devs[1].bio;
  1625. bio->bi_next = biolist;
  1626. biolist = bio;
  1627. bio->bi_private = r10_bio;
  1628. bio->bi_end_io = end_sync_write;
  1629. bio->bi_rw = WRITE;
  1630. bio->bi_sector = r10_bio->devs[k].addr +
  1631. conf->mirrors[i].rdev->data_offset;
  1632. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1633. r10_bio->devs[0].devnum = d;
  1634. r10_bio->devs[1].devnum = i;
  1635. break;
  1636. }
  1637. }
  1638. if (j == conf->copies) {
  1639. /* Cannot recover, so abort the recovery */
  1640. put_buf(r10_bio);
  1641. if (rb2)
  1642. atomic_dec(&rb2->remaining);
  1643. r10_bio = rb2;
  1644. if (!test_and_set_bit(MD_RECOVERY_INTR,
  1645. &mddev->recovery))
  1646. printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
  1647. mdname(mddev));
  1648. break;
  1649. }
  1650. }
  1651. if (biolist == NULL) {
  1652. while (r10_bio) {
  1653. r10bio_t *rb2 = r10_bio;
  1654. r10_bio = (r10bio_t*) rb2->master_bio;
  1655. rb2->master_bio = NULL;
  1656. put_buf(rb2);
  1657. }
  1658. goto giveup;
  1659. }
  1660. } else {
  1661. /* resync. Schedule a read for every block at this virt offset */
  1662. int count = 0;
  1663. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1664. &sync_blocks, mddev->degraded) &&
  1665. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1666. /* We can skip this block */
  1667. *skipped = 1;
  1668. return sync_blocks + sectors_skipped;
  1669. }
  1670. if (sync_blocks < max_sync)
  1671. max_sync = sync_blocks;
  1672. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1673. r10_bio->mddev = mddev;
  1674. atomic_set(&r10_bio->remaining, 0);
  1675. raise_barrier(conf, 0);
  1676. conf->next_resync = sector_nr;
  1677. r10_bio->master_bio = NULL;
  1678. r10_bio->sector = sector_nr;
  1679. set_bit(R10BIO_IsSync, &r10_bio->state);
  1680. raid10_find_phys(conf, r10_bio);
  1681. r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
  1682. for (i=0; i<conf->copies; i++) {
  1683. int d = r10_bio->devs[i].devnum;
  1684. bio = r10_bio->devs[i].bio;
  1685. bio->bi_end_io = NULL;
  1686. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1687. if (conf->mirrors[d].rdev == NULL ||
  1688. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  1689. continue;
  1690. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1691. atomic_inc(&r10_bio->remaining);
  1692. bio->bi_next = biolist;
  1693. biolist = bio;
  1694. bio->bi_private = r10_bio;
  1695. bio->bi_end_io = end_sync_read;
  1696. bio->bi_rw = READ;
  1697. bio->bi_sector = r10_bio->devs[i].addr +
  1698. conf->mirrors[d].rdev->data_offset;
  1699. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1700. count++;
  1701. }
  1702. if (count < 2) {
  1703. for (i=0; i<conf->copies; i++) {
  1704. int d = r10_bio->devs[i].devnum;
  1705. if (r10_bio->devs[i].bio->bi_end_io)
  1706. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1707. }
  1708. put_buf(r10_bio);
  1709. biolist = NULL;
  1710. goto giveup;
  1711. }
  1712. }
  1713. for (bio = biolist; bio ; bio=bio->bi_next) {
  1714. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1715. if (bio->bi_end_io)
  1716. bio->bi_flags |= 1 << BIO_UPTODATE;
  1717. bio->bi_vcnt = 0;
  1718. bio->bi_idx = 0;
  1719. bio->bi_phys_segments = 0;
  1720. bio->bi_hw_segments = 0;
  1721. bio->bi_size = 0;
  1722. }
  1723. nr_sectors = 0;
  1724. if (sector_nr + max_sync < max_sector)
  1725. max_sector = sector_nr + max_sync;
  1726. do {
  1727. struct page *page;
  1728. int len = PAGE_SIZE;
  1729. disk = 0;
  1730. if (sector_nr + (len>>9) > max_sector)
  1731. len = (max_sector - sector_nr) << 9;
  1732. if (len == 0)
  1733. break;
  1734. for (bio= biolist ; bio ; bio=bio->bi_next) {
  1735. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  1736. if (bio_add_page(bio, page, len, 0) == 0) {
  1737. /* stop here */
  1738. struct bio *bio2;
  1739. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1740. for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
  1741. /* remove last page from this bio */
  1742. bio2->bi_vcnt--;
  1743. bio2->bi_size -= len;
  1744. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  1745. }
  1746. goto bio_full;
  1747. }
  1748. disk = i;
  1749. }
  1750. nr_sectors += len>>9;
  1751. sector_nr += len>>9;
  1752. } while (biolist->bi_vcnt < RESYNC_PAGES);
  1753. bio_full:
  1754. r10_bio->sectors = nr_sectors;
  1755. while (biolist) {
  1756. bio = biolist;
  1757. biolist = biolist->bi_next;
  1758. bio->bi_next = NULL;
  1759. r10_bio = bio->bi_private;
  1760. r10_bio->sectors = nr_sectors;
  1761. if (bio->bi_end_io == end_sync_read) {
  1762. md_sync_acct(bio->bi_bdev, nr_sectors);
  1763. generic_make_request(bio);
  1764. }
  1765. }
  1766. if (sectors_skipped)
  1767. /* pretend they weren't skipped, it makes
  1768. * no important difference in this case
  1769. */
  1770. md_done_sync(mddev, sectors_skipped, 1);
  1771. return sectors_skipped + nr_sectors;
  1772. giveup:
  1773. /* There is nowhere to write, so all non-sync
  1774. * drives must be failed, so try the next chunk...
  1775. */
  1776. {
  1777. sector_t sec = max_sector - sector_nr;
  1778. sectors_skipped += sec;
  1779. chunks_skipped ++;
  1780. sector_nr = max_sector;
  1781. goto skipped;
  1782. }
  1783. }
  1784. static int run(mddev_t *mddev)
  1785. {
  1786. conf_t *conf;
  1787. int i, disk_idx;
  1788. mirror_info_t *disk;
  1789. mdk_rdev_t *rdev;
  1790. struct list_head *tmp;
  1791. int nc, fc, fo;
  1792. sector_t stride, size;
  1793. if (mddev->chunk_size == 0) {
  1794. printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
  1795. return -EINVAL;
  1796. }
  1797. nc = mddev->layout & 255;
  1798. fc = (mddev->layout >> 8) & 255;
  1799. fo = mddev->layout & (1<<16);
  1800. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  1801. (mddev->layout >> 17)) {
  1802. printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
  1803. mdname(mddev), mddev->layout);
  1804. goto out;
  1805. }
  1806. /*
  1807. * copy the already verified devices into our private RAID10
  1808. * bookkeeping area. [whatever we allocate in run(),
  1809. * should be freed in stop()]
  1810. */
  1811. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  1812. mddev->private = conf;
  1813. if (!conf) {
  1814. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1815. mdname(mddev));
  1816. goto out;
  1817. }
  1818. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1819. GFP_KERNEL);
  1820. if (!conf->mirrors) {
  1821. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1822. mdname(mddev));
  1823. goto out_free_conf;
  1824. }
  1825. conf->tmppage = alloc_page(GFP_KERNEL);
  1826. if (!conf->tmppage)
  1827. goto out_free_conf;
  1828. conf->mddev = mddev;
  1829. conf->raid_disks = mddev->raid_disks;
  1830. conf->near_copies = nc;
  1831. conf->far_copies = fc;
  1832. conf->copies = nc*fc;
  1833. conf->far_offset = fo;
  1834. conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
  1835. conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
  1836. size = mddev->size >> (conf->chunk_shift-1);
  1837. sector_div(size, fc);
  1838. size = size * conf->raid_disks;
  1839. sector_div(size, nc);
  1840. /* 'size' is now the number of chunks in the array */
  1841. /* calculate "used chunks per device" in 'stride' */
  1842. stride = size * conf->copies;
  1843. /* We need to round up when dividing by raid_disks to
  1844. * get the stride size.
  1845. */
  1846. stride += conf->raid_disks - 1;
  1847. sector_div(stride, conf->raid_disks);
  1848. mddev->size = stride << (conf->chunk_shift-1);
  1849. if (fo)
  1850. stride = 1;
  1851. else
  1852. sector_div(stride, fc);
  1853. conf->stride = stride << conf->chunk_shift;
  1854. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  1855. r10bio_pool_free, conf);
  1856. if (!conf->r10bio_pool) {
  1857. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1858. mdname(mddev));
  1859. goto out_free_conf;
  1860. }
  1861. spin_lock_init(&conf->device_lock);
  1862. mddev->queue->queue_lock = &conf->device_lock;
  1863. rdev_for_each(rdev, tmp, mddev) {
  1864. disk_idx = rdev->raid_disk;
  1865. if (disk_idx >= mddev->raid_disks
  1866. || disk_idx < 0)
  1867. continue;
  1868. disk = conf->mirrors + disk_idx;
  1869. disk->rdev = rdev;
  1870. blk_queue_stack_limits(mddev->queue,
  1871. rdev->bdev->bd_disk->queue);
  1872. /* as we don't honour merge_bvec_fn, we must never risk
  1873. * violating it, so limit ->max_sector to one PAGE, as
  1874. * a one page request is never in violation.
  1875. */
  1876. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1877. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  1878. mddev->queue->max_sectors = (PAGE_SIZE>>9);
  1879. disk->head_position = 0;
  1880. }
  1881. INIT_LIST_HEAD(&conf->retry_list);
  1882. spin_lock_init(&conf->resync_lock);
  1883. init_waitqueue_head(&conf->wait_barrier);
  1884. /* need to check that every block has at least one working mirror */
  1885. if (!enough(conf)) {
  1886. printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
  1887. mdname(mddev));
  1888. goto out_free_conf;
  1889. }
  1890. mddev->degraded = 0;
  1891. for (i = 0; i < conf->raid_disks; i++) {
  1892. disk = conf->mirrors + i;
  1893. if (!disk->rdev ||
  1894. !test_bit(In_sync, &disk->rdev->flags)) {
  1895. disk->head_position = 0;
  1896. mddev->degraded++;
  1897. if (disk->rdev)
  1898. conf->fullsync = 1;
  1899. }
  1900. }
  1901. mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
  1902. if (!mddev->thread) {
  1903. printk(KERN_ERR
  1904. "raid10: couldn't allocate thread for %s\n",
  1905. mdname(mddev));
  1906. goto out_free_conf;
  1907. }
  1908. printk(KERN_INFO
  1909. "raid10: raid set %s active with %d out of %d devices\n",
  1910. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1911. mddev->raid_disks);
  1912. /*
  1913. * Ok, everything is just fine now
  1914. */
  1915. mddev->array_sectors = size << conf->chunk_shift;
  1916. mddev->resync_max_sectors = size << conf->chunk_shift;
  1917. mddev->queue->unplug_fn = raid10_unplug;
  1918. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  1919. mddev->queue->backing_dev_info.congested_data = mddev;
  1920. /* Calculate max read-ahead size.
  1921. * We need to readahead at least twice a whole stripe....
  1922. * maybe...
  1923. */
  1924. {
  1925. int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
  1926. stripe /= conf->near_copies;
  1927. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  1928. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  1929. }
  1930. if (conf->near_copies < mddev->raid_disks)
  1931. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  1932. return 0;
  1933. out_free_conf:
  1934. if (conf->r10bio_pool)
  1935. mempool_destroy(conf->r10bio_pool);
  1936. safe_put_page(conf->tmppage);
  1937. kfree(conf->mirrors);
  1938. kfree(conf);
  1939. mddev->private = NULL;
  1940. out:
  1941. return -EIO;
  1942. }
  1943. static int stop(mddev_t *mddev)
  1944. {
  1945. conf_t *conf = mddev_to_conf(mddev);
  1946. md_unregister_thread(mddev->thread);
  1947. mddev->thread = NULL;
  1948. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1949. if (conf->r10bio_pool)
  1950. mempool_destroy(conf->r10bio_pool);
  1951. kfree(conf->mirrors);
  1952. kfree(conf);
  1953. mddev->private = NULL;
  1954. return 0;
  1955. }
  1956. static void raid10_quiesce(mddev_t *mddev, int state)
  1957. {
  1958. conf_t *conf = mddev_to_conf(mddev);
  1959. switch(state) {
  1960. case 1:
  1961. raise_barrier(conf, 0);
  1962. break;
  1963. case 0:
  1964. lower_barrier(conf);
  1965. break;
  1966. }
  1967. if (mddev->thread) {
  1968. if (mddev->bitmap)
  1969. mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
  1970. else
  1971. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1972. md_wakeup_thread(mddev->thread);
  1973. }
  1974. }
  1975. static struct mdk_personality raid10_personality =
  1976. {
  1977. .name = "raid10",
  1978. .level = 10,
  1979. .owner = THIS_MODULE,
  1980. .make_request = make_request,
  1981. .run = run,
  1982. .stop = stop,
  1983. .status = status,
  1984. .error_handler = error,
  1985. .hot_add_disk = raid10_add_disk,
  1986. .hot_remove_disk= raid10_remove_disk,
  1987. .spare_active = raid10_spare_active,
  1988. .sync_request = sync_request,
  1989. .quiesce = raid10_quiesce,
  1990. };
  1991. static int __init raid_init(void)
  1992. {
  1993. return register_md_personality(&raid10_personality);
  1994. }
  1995. static void raid_exit(void)
  1996. {
  1997. unregister_md_personality(&raid10_personality);
  1998. }
  1999. module_init(raid_init);
  2000. module_exit(raid_exit);
  2001. MODULE_LICENSE("GPL");
  2002. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  2003. MODULE_ALIAS("md-raid10");
  2004. MODULE_ALIAS("md-level-10");