raid10.c 66 KB

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