raid10.c 61 KB

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