raid10.c 67 KB

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