raid10.c 56 KB

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