raid10.c 62 KB

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