raid10.c 61 KB

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