raid10.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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. /* Spinlock only taken to quiet a warning */
  577. spin_lock(conf->mddev->queue->queue_lock);
  578. blk_remove_plug(conf->mddev->queue);
  579. spin_unlock(conf->mddev->queue->queue_lock);
  580. spin_unlock_irq(&conf->device_lock);
  581. /* flush any pending bitmap writes to disk
  582. * before proceeding w/ I/O */
  583. bitmap_unplug(conf->mddev->bitmap);
  584. while (bio) { /* submit pending writes */
  585. struct bio *next = bio->bi_next;
  586. bio->bi_next = NULL;
  587. generic_make_request(bio);
  588. bio = next;
  589. }
  590. rv = 1;
  591. } else
  592. spin_unlock_irq(&conf->device_lock);
  593. return rv;
  594. }
  595. /* Barriers....
  596. * Sometimes we need to suspend IO while we do something else,
  597. * either some resync/recovery, or reconfigure the array.
  598. * To do this we raise a 'barrier'.
  599. * The 'barrier' is a counter that can be raised multiple times
  600. * to count how many activities are happening which preclude
  601. * normal IO.
  602. * We can only raise the barrier if there is no pending IO.
  603. * i.e. if nr_pending == 0.
  604. * We choose only to raise the barrier if no-one is waiting for the
  605. * barrier to go down. This means that as soon as an IO request
  606. * is ready, no other operations which require a barrier will start
  607. * until the IO request has had a chance.
  608. *
  609. * So: regular IO calls 'wait_barrier'. When that returns there
  610. * is no backgroup IO happening, It must arrange to call
  611. * allow_barrier when it has finished its IO.
  612. * backgroup IO calls must call raise_barrier. Once that returns
  613. * there is no normal IO happeing. It must arrange to call
  614. * lower_barrier when the particular background IO completes.
  615. */
  616. static void raise_barrier(conf_t *conf, int force)
  617. {
  618. BUG_ON(force && !conf->barrier);
  619. spin_lock_irq(&conf->resync_lock);
  620. /* Wait until no block IO is waiting (unless 'force') */
  621. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  622. conf->resync_lock,
  623. raid10_unplug(conf->mddev->queue));
  624. /* block any new IO from starting */
  625. conf->barrier++;
  626. /* No wait for all pending IO to complete */
  627. wait_event_lock_irq(conf->wait_barrier,
  628. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  629. conf->resync_lock,
  630. raid10_unplug(conf->mddev->queue));
  631. spin_unlock_irq(&conf->resync_lock);
  632. }
  633. static void lower_barrier(conf_t *conf)
  634. {
  635. unsigned long flags;
  636. spin_lock_irqsave(&conf->resync_lock, flags);
  637. conf->barrier--;
  638. spin_unlock_irqrestore(&conf->resync_lock, flags);
  639. wake_up(&conf->wait_barrier);
  640. }
  641. static void wait_barrier(conf_t *conf)
  642. {
  643. spin_lock_irq(&conf->resync_lock);
  644. if (conf->barrier) {
  645. conf->nr_waiting++;
  646. wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
  647. conf->resync_lock,
  648. raid10_unplug(conf->mddev->queue));
  649. conf->nr_waiting--;
  650. }
  651. conf->nr_pending++;
  652. spin_unlock_irq(&conf->resync_lock);
  653. }
  654. static void allow_barrier(conf_t *conf)
  655. {
  656. unsigned long flags;
  657. spin_lock_irqsave(&conf->resync_lock, flags);
  658. conf->nr_pending--;
  659. spin_unlock_irqrestore(&conf->resync_lock, flags);
  660. wake_up(&conf->wait_barrier);
  661. }
  662. static void freeze_array(conf_t *conf)
  663. {
  664. /* stop syncio and normal IO and wait for everything to
  665. * go quiet.
  666. * We increment barrier and nr_waiting, and then
  667. * wait until nr_pending match nr_queued+1
  668. * This is called in the context of one normal IO request
  669. * that has failed. Thus any sync request that might be pending
  670. * will be blocked by nr_pending, and we need to wait for
  671. * pending IO requests to complete or be queued for re-try.
  672. * Thus the number queued (nr_queued) plus this request (1)
  673. * must match the number of pending IOs (nr_pending) before
  674. * we continue.
  675. */
  676. spin_lock_irq(&conf->resync_lock);
  677. conf->barrier++;
  678. conf->nr_waiting++;
  679. wait_event_lock_irq(conf->wait_barrier,
  680. conf->nr_pending == conf->nr_queued+1,
  681. conf->resync_lock,
  682. ({ flush_pending_writes(conf);
  683. raid10_unplug(conf->mddev->queue); }));
  684. spin_unlock_irq(&conf->resync_lock);
  685. }
  686. static void unfreeze_array(conf_t *conf)
  687. {
  688. /* reverse the effect of the freeze */
  689. spin_lock_irq(&conf->resync_lock);
  690. conf->barrier--;
  691. conf->nr_waiting--;
  692. wake_up(&conf->wait_barrier);
  693. spin_unlock_irq(&conf->resync_lock);
  694. }
  695. static int make_request(mddev_t *mddev, struct bio * bio)
  696. {
  697. conf_t *conf = mddev->private;
  698. mirror_info_t *mirror;
  699. r10bio_t *r10_bio;
  700. struct bio *read_bio;
  701. int i;
  702. int chunk_sects = conf->chunk_mask + 1;
  703. const int rw = bio_data_dir(bio);
  704. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  705. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  706. unsigned long flags;
  707. mdk_rdev_t *blocked_rdev;
  708. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  709. md_flush_request(mddev, bio);
  710. return 0;
  711. }
  712. /* If this request crosses a chunk boundary, we need to
  713. * split it. This will only happen for 1 PAGE (or less) requests.
  714. */
  715. if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
  716. > chunk_sects &&
  717. conf->near_copies < conf->raid_disks)) {
  718. struct bio_pair *bp;
  719. /* Sanity check -- queue functions should prevent this happening */
  720. if (bio->bi_vcnt != 1 ||
  721. bio->bi_idx != 0)
  722. goto bad_map;
  723. /* This is a one page bio that upper layers
  724. * refuse to split for us, so we need to split it.
  725. */
  726. bp = bio_split(bio,
  727. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  728. /* Each of these 'make_request' calls will call 'wait_barrier'.
  729. * If the first succeeds but the second blocks due to the resync
  730. * thread raising the barrier, we will deadlock because the
  731. * IO to the underlying device will be queued in generic_make_request
  732. * and will never complete, so will never reduce nr_pending.
  733. * So increment nr_waiting here so no new raise_barriers will
  734. * succeed, and so the second wait_barrier cannot block.
  735. */
  736. spin_lock_irq(&conf->resync_lock);
  737. conf->nr_waiting++;
  738. spin_unlock_irq(&conf->resync_lock);
  739. if (make_request(mddev, &bp->bio1))
  740. generic_make_request(&bp->bio1);
  741. if (make_request(mddev, &bp->bio2))
  742. generic_make_request(&bp->bio2);
  743. spin_lock_irq(&conf->resync_lock);
  744. conf->nr_waiting--;
  745. wake_up(&conf->wait_barrier);
  746. spin_unlock_irq(&conf->resync_lock);
  747. bio_pair_release(bp);
  748. return 0;
  749. bad_map:
  750. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  751. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  752. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  753. bio_io_error(bio);
  754. return 0;
  755. }
  756. md_write_start(mddev, bio);
  757. /*
  758. * Register the new request and wait if the reconstruction
  759. * thread has put up a bar for new requests.
  760. * Continue immediately if no resync is active currently.
  761. */
  762. wait_barrier(conf);
  763. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  764. r10_bio->master_bio = bio;
  765. r10_bio->sectors = bio->bi_size >> 9;
  766. r10_bio->mddev = mddev;
  767. r10_bio->sector = bio->bi_sector;
  768. r10_bio->state = 0;
  769. if (rw == READ) {
  770. /*
  771. * read balancing logic:
  772. */
  773. int disk = read_balance(conf, r10_bio);
  774. int slot = r10_bio->read_slot;
  775. if (disk < 0) {
  776. raid_end_bio_io(r10_bio);
  777. return 0;
  778. }
  779. mirror = conf->mirrors + disk;
  780. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  781. r10_bio->devs[slot].bio = read_bio;
  782. read_bio->bi_sector = r10_bio->devs[slot].addr +
  783. mirror->rdev->data_offset;
  784. read_bio->bi_bdev = mirror->rdev->bdev;
  785. read_bio->bi_end_io = raid10_end_read_request;
  786. read_bio->bi_rw = READ | do_sync;
  787. read_bio->bi_private = r10_bio;
  788. generic_make_request(read_bio);
  789. return 0;
  790. }
  791. /*
  792. * WRITE:
  793. */
  794. /* first select target devices under rcu_lock and
  795. * inc refcount on their rdev. Record them by setting
  796. * bios[x] to bio
  797. */
  798. raid10_find_phys(conf, r10_bio);
  799. retry_write:
  800. blocked_rdev = NULL;
  801. rcu_read_lock();
  802. for (i = 0; i < conf->copies; i++) {
  803. int d = r10_bio->devs[i].devnum;
  804. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
  805. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  806. atomic_inc(&rdev->nr_pending);
  807. blocked_rdev = rdev;
  808. break;
  809. }
  810. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  811. atomic_inc(&rdev->nr_pending);
  812. r10_bio->devs[i].bio = bio;
  813. } else {
  814. r10_bio->devs[i].bio = NULL;
  815. set_bit(R10BIO_Degraded, &r10_bio->state);
  816. }
  817. }
  818. rcu_read_unlock();
  819. if (unlikely(blocked_rdev)) {
  820. /* Have to wait for this device to get unblocked, then retry */
  821. int j;
  822. int d;
  823. for (j = 0; j < i; j++)
  824. if (r10_bio->devs[j].bio) {
  825. d = r10_bio->devs[j].devnum;
  826. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  827. }
  828. allow_barrier(conf);
  829. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  830. wait_barrier(conf);
  831. goto retry_write;
  832. }
  833. atomic_set(&r10_bio->remaining, 1);
  834. bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
  835. for (i = 0; i < conf->copies; i++) {
  836. struct bio *mbio;
  837. int d = r10_bio->devs[i].devnum;
  838. if (!r10_bio->devs[i].bio)
  839. continue;
  840. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  841. r10_bio->devs[i].bio = mbio;
  842. mbio->bi_sector = r10_bio->devs[i].addr+
  843. conf->mirrors[d].rdev->data_offset;
  844. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  845. mbio->bi_end_io = raid10_end_write_request;
  846. mbio->bi_rw = WRITE | do_sync | do_fua;
  847. mbio->bi_private = r10_bio;
  848. atomic_inc(&r10_bio->remaining);
  849. spin_lock_irqsave(&conf->device_lock, flags);
  850. bio_list_add(&conf->pending_bio_list, mbio);
  851. blk_plug_device_unlocked(mddev->queue);
  852. spin_unlock_irqrestore(&conf->device_lock, flags);
  853. }
  854. if (atomic_dec_and_test(&r10_bio->remaining)) {
  855. /* This matches the end of raid10_end_write_request() */
  856. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  857. r10_bio->sectors,
  858. !test_bit(R10BIO_Degraded, &r10_bio->state),
  859. 0);
  860. md_write_end(mddev);
  861. raid_end_bio_io(r10_bio);
  862. }
  863. /* In case raid10d snuck in to freeze_array */
  864. wake_up(&conf->wait_barrier);
  865. if (do_sync)
  866. md_wakeup_thread(mddev->thread);
  867. return 0;
  868. }
  869. static void status(struct seq_file *seq, mddev_t *mddev)
  870. {
  871. conf_t *conf = mddev->private;
  872. int i;
  873. if (conf->near_copies < conf->raid_disks)
  874. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  875. if (conf->near_copies > 1)
  876. seq_printf(seq, " %d near-copies", conf->near_copies);
  877. if (conf->far_copies > 1) {
  878. if (conf->far_offset)
  879. seq_printf(seq, " %d offset-copies", conf->far_copies);
  880. else
  881. seq_printf(seq, " %d far-copies", conf->far_copies);
  882. }
  883. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  884. conf->raid_disks - mddev->degraded);
  885. for (i = 0; i < conf->raid_disks; i++)
  886. seq_printf(seq, "%s",
  887. conf->mirrors[i].rdev &&
  888. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  889. seq_printf(seq, "]");
  890. }
  891. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  892. {
  893. char b[BDEVNAME_SIZE];
  894. conf_t *conf = mddev->private;
  895. /*
  896. * If it is not operational, then we have already marked it as dead
  897. * else if it is the last working disks, ignore the error, let the
  898. * next level up know.
  899. * else mark the drive as failed
  900. */
  901. if (test_bit(In_sync, &rdev->flags)
  902. && conf->raid_disks-mddev->degraded == 1)
  903. /*
  904. * Don't fail the drive, just return an IO error.
  905. * The test should really be more sophisticated than
  906. * "working_disks == 1", but it isn't critical, and
  907. * can wait until we do more sophisticated "is the drive
  908. * really dead" tests...
  909. */
  910. return;
  911. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  912. unsigned long flags;
  913. spin_lock_irqsave(&conf->device_lock, flags);
  914. mddev->degraded++;
  915. spin_unlock_irqrestore(&conf->device_lock, flags);
  916. /*
  917. * if recovery is running, make sure it aborts.
  918. */
  919. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  920. }
  921. set_bit(Faulty, &rdev->flags);
  922. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  923. printk(KERN_ALERT
  924. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  925. "md/raid10:%s: Operation continuing on %d devices.\n",
  926. mdname(mddev), bdevname(rdev->bdev, b),
  927. mdname(mddev), conf->raid_disks - mddev->degraded);
  928. }
  929. static void print_conf(conf_t *conf)
  930. {
  931. int i;
  932. mirror_info_t *tmp;
  933. printk(KERN_DEBUG "RAID10 conf printout:\n");
  934. if (!conf) {
  935. printk(KERN_DEBUG "(!conf)\n");
  936. return;
  937. }
  938. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  939. conf->raid_disks);
  940. for (i = 0; i < conf->raid_disks; i++) {
  941. char b[BDEVNAME_SIZE];
  942. tmp = conf->mirrors + i;
  943. if (tmp->rdev)
  944. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  945. i, !test_bit(In_sync, &tmp->rdev->flags),
  946. !test_bit(Faulty, &tmp->rdev->flags),
  947. bdevname(tmp->rdev->bdev,b));
  948. }
  949. }
  950. static void close_sync(conf_t *conf)
  951. {
  952. wait_barrier(conf);
  953. allow_barrier(conf);
  954. mempool_destroy(conf->r10buf_pool);
  955. conf->r10buf_pool = NULL;
  956. }
  957. /* check if there are enough drives for
  958. * every block to appear on atleast one
  959. */
  960. static int enough(conf_t *conf)
  961. {
  962. int first = 0;
  963. do {
  964. int n = conf->copies;
  965. int cnt = 0;
  966. while (n--) {
  967. if (conf->mirrors[first].rdev)
  968. cnt++;
  969. first = (first+1) % conf->raid_disks;
  970. }
  971. if (cnt == 0)
  972. return 0;
  973. } while (first != 0);
  974. return 1;
  975. }
  976. static int raid10_spare_active(mddev_t *mddev)
  977. {
  978. int i;
  979. conf_t *conf = mddev->private;
  980. mirror_info_t *tmp;
  981. int count = 0;
  982. unsigned long flags;
  983. /*
  984. * Find all non-in_sync disks within the RAID10 configuration
  985. * and mark them in_sync
  986. */
  987. for (i = 0; i < conf->raid_disks; i++) {
  988. tmp = conf->mirrors + i;
  989. if (tmp->rdev
  990. && !test_bit(Faulty, &tmp->rdev->flags)
  991. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  992. count++;
  993. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  994. }
  995. }
  996. spin_lock_irqsave(&conf->device_lock, flags);
  997. mddev->degraded -= count;
  998. spin_unlock_irqrestore(&conf->device_lock, flags);
  999. print_conf(conf);
  1000. return count;
  1001. }
  1002. static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  1003. {
  1004. conf_t *conf = mddev->private;
  1005. int err = -EEXIST;
  1006. int mirror;
  1007. mirror_info_t *p;
  1008. int first = 0;
  1009. int last = conf->raid_disks - 1;
  1010. if (mddev->recovery_cp < MaxSector)
  1011. /* only hot-add to in-sync arrays, as recovery is
  1012. * very different from resync
  1013. */
  1014. return -EBUSY;
  1015. if (!enough(conf))
  1016. return -EINVAL;
  1017. if (rdev->raid_disk >= 0)
  1018. first = last = rdev->raid_disk;
  1019. if (rdev->saved_raid_disk >= 0 &&
  1020. rdev->saved_raid_disk >= first &&
  1021. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1022. mirror = rdev->saved_raid_disk;
  1023. else
  1024. mirror = first;
  1025. for ( ; mirror <= last ; mirror++)
  1026. if ( !(p=conf->mirrors+mirror)->rdev) {
  1027. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1028. rdev->data_offset << 9);
  1029. /* as we don't honour merge_bvec_fn, we must
  1030. * never risk violating it, so limit
  1031. * ->max_segments to one lying with a single
  1032. * page, as a one page request is never in
  1033. * violation.
  1034. */
  1035. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  1036. blk_queue_max_segments(mddev->queue, 1);
  1037. blk_queue_segment_boundary(mddev->queue,
  1038. PAGE_CACHE_SIZE - 1);
  1039. }
  1040. p->head_position = 0;
  1041. rdev->raid_disk = mirror;
  1042. err = 0;
  1043. if (rdev->saved_raid_disk != mirror)
  1044. conf->fullsync = 1;
  1045. rcu_assign_pointer(p->rdev, rdev);
  1046. break;
  1047. }
  1048. md_integrity_add_rdev(rdev, mddev);
  1049. print_conf(conf);
  1050. return err;
  1051. }
  1052. static int raid10_remove_disk(mddev_t *mddev, int number)
  1053. {
  1054. conf_t *conf = mddev->private;
  1055. int err = 0;
  1056. mdk_rdev_t *rdev;
  1057. mirror_info_t *p = conf->mirrors+ number;
  1058. print_conf(conf);
  1059. rdev = p->rdev;
  1060. if (rdev) {
  1061. if (test_bit(In_sync, &rdev->flags) ||
  1062. atomic_read(&rdev->nr_pending)) {
  1063. err = -EBUSY;
  1064. goto abort;
  1065. }
  1066. /* Only remove faulty devices in recovery
  1067. * is not possible.
  1068. */
  1069. if (!test_bit(Faulty, &rdev->flags) &&
  1070. enough(conf)) {
  1071. err = -EBUSY;
  1072. goto abort;
  1073. }
  1074. p->rdev = NULL;
  1075. synchronize_rcu();
  1076. if (atomic_read(&rdev->nr_pending)) {
  1077. /* lost the race, try later */
  1078. err = -EBUSY;
  1079. p->rdev = rdev;
  1080. goto abort;
  1081. }
  1082. md_integrity_register(mddev);
  1083. }
  1084. abort:
  1085. print_conf(conf);
  1086. return err;
  1087. }
  1088. static void end_sync_read(struct bio *bio, int error)
  1089. {
  1090. r10bio_t *r10_bio = bio->bi_private;
  1091. conf_t *conf = r10_bio->mddev->private;
  1092. int i,d;
  1093. for (i=0; i<conf->copies; i++)
  1094. if (r10_bio->devs[i].bio == bio)
  1095. break;
  1096. BUG_ON(i == conf->copies);
  1097. update_head_pos(i, r10_bio);
  1098. d = r10_bio->devs[i].devnum;
  1099. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1100. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1101. else {
  1102. atomic_add(r10_bio->sectors,
  1103. &conf->mirrors[d].rdev->corrected_errors);
  1104. if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  1105. md_error(r10_bio->mddev,
  1106. conf->mirrors[d].rdev);
  1107. }
  1108. /* for reconstruct, we always reschedule after a read.
  1109. * for resync, only after all reads
  1110. */
  1111. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1112. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1113. atomic_dec_and_test(&r10_bio->remaining)) {
  1114. /* we have read all the blocks,
  1115. * do the comparison in process context in raid10d
  1116. */
  1117. reschedule_retry(r10_bio);
  1118. }
  1119. }
  1120. static void end_sync_write(struct bio *bio, int error)
  1121. {
  1122. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1123. r10bio_t *r10_bio = bio->bi_private;
  1124. mddev_t *mddev = r10_bio->mddev;
  1125. conf_t *conf = mddev->private;
  1126. int i,d;
  1127. for (i = 0; i < conf->copies; i++)
  1128. if (r10_bio->devs[i].bio == bio)
  1129. break;
  1130. d = r10_bio->devs[i].devnum;
  1131. if (!uptodate)
  1132. md_error(mddev, conf->mirrors[d].rdev);
  1133. update_head_pos(i, r10_bio);
  1134. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1135. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1136. if (r10_bio->master_bio == NULL) {
  1137. /* the primary of several recovery bios */
  1138. sector_t s = r10_bio->sectors;
  1139. put_buf(r10_bio);
  1140. md_done_sync(mddev, s, 1);
  1141. break;
  1142. } else {
  1143. r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
  1144. put_buf(r10_bio);
  1145. r10_bio = r10_bio2;
  1146. }
  1147. }
  1148. }
  1149. /*
  1150. * Note: sync and recover and handled very differently for raid10
  1151. * This code is for resync.
  1152. * For resync, we read through virtual addresses and read all blocks.
  1153. * If there is any error, we schedule a write. The lowest numbered
  1154. * drive is authoritative.
  1155. * However requests come for physical address, so we need to map.
  1156. * For every physical address there are raid_disks/copies virtual addresses,
  1157. * which is always are least one, but is not necessarly an integer.
  1158. * This means that a physical address can span multiple chunks, so we may
  1159. * have to submit multiple io requests for a single sync request.
  1160. */
  1161. /*
  1162. * We check if all blocks are in-sync and only write to blocks that
  1163. * aren't in sync
  1164. */
  1165. static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1166. {
  1167. conf_t *conf = mddev->private;
  1168. int i, first;
  1169. struct bio *tbio, *fbio;
  1170. atomic_set(&r10_bio->remaining, 1);
  1171. /* find the first device with a block */
  1172. for (i=0; i<conf->copies; i++)
  1173. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1174. break;
  1175. if (i == conf->copies)
  1176. goto done;
  1177. first = i;
  1178. fbio = r10_bio->devs[i].bio;
  1179. /* now find blocks with errors */
  1180. for (i=0 ; i < conf->copies ; i++) {
  1181. int j, d;
  1182. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1183. tbio = r10_bio->devs[i].bio;
  1184. if (tbio->bi_end_io != end_sync_read)
  1185. continue;
  1186. if (i == first)
  1187. continue;
  1188. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1189. /* We know that the bi_io_vec layout is the same for
  1190. * both 'first' and 'i', so we just compare them.
  1191. * All vec entries are PAGE_SIZE;
  1192. */
  1193. for (j = 0; j < vcnt; j++)
  1194. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1195. page_address(tbio->bi_io_vec[j].bv_page),
  1196. PAGE_SIZE))
  1197. break;
  1198. if (j == vcnt)
  1199. continue;
  1200. mddev->resync_mismatches += r10_bio->sectors;
  1201. }
  1202. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1203. /* Don't fix anything. */
  1204. continue;
  1205. /* Ok, we need to write this bio
  1206. * First we need to fixup bv_offset, bv_len and
  1207. * bi_vecs, as the read request might have corrupted these
  1208. */
  1209. tbio->bi_vcnt = vcnt;
  1210. tbio->bi_size = r10_bio->sectors << 9;
  1211. tbio->bi_idx = 0;
  1212. tbio->bi_phys_segments = 0;
  1213. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1214. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1215. tbio->bi_next = NULL;
  1216. tbio->bi_rw = WRITE;
  1217. tbio->bi_private = r10_bio;
  1218. tbio->bi_sector = r10_bio->devs[i].addr;
  1219. for (j=0; j < vcnt ; j++) {
  1220. tbio->bi_io_vec[j].bv_offset = 0;
  1221. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1222. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1223. page_address(fbio->bi_io_vec[j].bv_page),
  1224. PAGE_SIZE);
  1225. }
  1226. tbio->bi_end_io = end_sync_write;
  1227. d = r10_bio->devs[i].devnum;
  1228. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1229. atomic_inc(&r10_bio->remaining);
  1230. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1231. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1232. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1233. generic_make_request(tbio);
  1234. }
  1235. done:
  1236. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1237. md_done_sync(mddev, r10_bio->sectors, 1);
  1238. put_buf(r10_bio);
  1239. }
  1240. }
  1241. /*
  1242. * Now for the recovery code.
  1243. * Recovery happens across physical sectors.
  1244. * We recover all non-is_sync drives by finding the virtual address of
  1245. * each, and then choose a working drive that also has that virt address.
  1246. * There is a separate r10_bio for each non-in_sync drive.
  1247. * Only the first two slots are in use. The first for reading,
  1248. * The second for writing.
  1249. *
  1250. */
  1251. static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1252. {
  1253. conf_t *conf = mddev->private;
  1254. int i, d;
  1255. struct bio *bio, *wbio;
  1256. /* move the pages across to the second bio
  1257. * and submit the write request
  1258. */
  1259. bio = r10_bio->devs[0].bio;
  1260. wbio = r10_bio->devs[1].bio;
  1261. for (i=0; i < wbio->bi_vcnt; i++) {
  1262. struct page *p = bio->bi_io_vec[i].bv_page;
  1263. bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
  1264. wbio->bi_io_vec[i].bv_page = p;
  1265. }
  1266. d = r10_bio->devs[1].devnum;
  1267. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1268. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1269. if (test_bit(R10BIO_Uptodate, &r10_bio->state))
  1270. generic_make_request(wbio);
  1271. else
  1272. bio_endio(wbio, -EIO);
  1273. }
  1274. /*
  1275. * Used by fix_read_error() to decay the per rdev read_errors.
  1276. * We halve the read error count for every hour that has elapsed
  1277. * since the last recorded read error.
  1278. *
  1279. */
  1280. static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
  1281. {
  1282. struct timespec cur_time_mon;
  1283. unsigned long hours_since_last;
  1284. unsigned int read_errors = atomic_read(&rdev->read_errors);
  1285. ktime_get_ts(&cur_time_mon);
  1286. if (rdev->last_read_error.tv_sec == 0 &&
  1287. rdev->last_read_error.tv_nsec == 0) {
  1288. /* first time we've seen a read error */
  1289. rdev->last_read_error = cur_time_mon;
  1290. return;
  1291. }
  1292. hours_since_last = (cur_time_mon.tv_sec -
  1293. rdev->last_read_error.tv_sec) / 3600;
  1294. rdev->last_read_error = cur_time_mon;
  1295. /*
  1296. * if hours_since_last is > the number of bits in read_errors
  1297. * just set read errors to 0. We do this to avoid
  1298. * overflowing the shift of read_errors by hours_since_last.
  1299. */
  1300. if (hours_since_last >= 8 * sizeof(read_errors))
  1301. atomic_set(&rdev->read_errors, 0);
  1302. else
  1303. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  1304. }
  1305. /*
  1306. * This is a kernel thread which:
  1307. *
  1308. * 1. Retries failed read operations on working mirrors.
  1309. * 2. Updates the raid superblock when problems encounter.
  1310. * 3. Performs writes following reads for array synchronising.
  1311. */
  1312. static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
  1313. {
  1314. int sect = 0; /* Offset from r10_bio->sector */
  1315. int sectors = r10_bio->sectors;
  1316. mdk_rdev_t*rdev;
  1317. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  1318. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  1319. rcu_read_lock();
  1320. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1321. if (rdev) { /* If rdev is not NULL */
  1322. char b[BDEVNAME_SIZE];
  1323. int cur_read_error_count = 0;
  1324. bdevname(rdev->bdev, b);
  1325. if (test_bit(Faulty, &rdev->flags)) {
  1326. rcu_read_unlock();
  1327. /* drive has already been failed, just ignore any
  1328. more fix_read_error() attempts */
  1329. return;
  1330. }
  1331. check_decay_read_errors(mddev, rdev);
  1332. atomic_inc(&rdev->read_errors);
  1333. cur_read_error_count = atomic_read(&rdev->read_errors);
  1334. if (cur_read_error_count > max_read_errors) {
  1335. rcu_read_unlock();
  1336. printk(KERN_NOTICE
  1337. "md/raid10:%s: %s: Raid device exceeded "
  1338. "read_error threshold "
  1339. "[cur %d:max %d]\n",
  1340. mdname(mddev),
  1341. b, cur_read_error_count, max_read_errors);
  1342. printk(KERN_NOTICE
  1343. "md/raid10:%s: %s: Failing raid "
  1344. "device\n", mdname(mddev), b);
  1345. md_error(mddev, conf->mirrors[d].rdev);
  1346. return;
  1347. }
  1348. }
  1349. rcu_read_unlock();
  1350. while(sectors) {
  1351. int s = sectors;
  1352. int sl = r10_bio->read_slot;
  1353. int success = 0;
  1354. int start;
  1355. if (s > (PAGE_SIZE>>9))
  1356. s = PAGE_SIZE >> 9;
  1357. rcu_read_lock();
  1358. do {
  1359. d = r10_bio->devs[sl].devnum;
  1360. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1361. if (rdev &&
  1362. test_bit(In_sync, &rdev->flags)) {
  1363. atomic_inc(&rdev->nr_pending);
  1364. rcu_read_unlock();
  1365. success = sync_page_io(rdev,
  1366. r10_bio->devs[sl].addr +
  1367. sect,
  1368. s<<9,
  1369. conf->tmppage, READ, false);
  1370. rdev_dec_pending(rdev, mddev);
  1371. rcu_read_lock();
  1372. if (success)
  1373. break;
  1374. }
  1375. sl++;
  1376. if (sl == conf->copies)
  1377. sl = 0;
  1378. } while (!success && sl != r10_bio->read_slot);
  1379. rcu_read_unlock();
  1380. if (!success) {
  1381. /* Cannot read from anywhere -- bye bye array */
  1382. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  1383. md_error(mddev, conf->mirrors[dn].rdev);
  1384. break;
  1385. }
  1386. start = sl;
  1387. /* write it back and re-read */
  1388. rcu_read_lock();
  1389. while (sl != r10_bio->read_slot) {
  1390. char b[BDEVNAME_SIZE];
  1391. if (sl==0)
  1392. sl = conf->copies;
  1393. sl--;
  1394. d = r10_bio->devs[sl].devnum;
  1395. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1396. if (rdev &&
  1397. test_bit(In_sync, &rdev->flags)) {
  1398. atomic_inc(&rdev->nr_pending);
  1399. rcu_read_unlock();
  1400. atomic_add(s, &rdev->corrected_errors);
  1401. if (sync_page_io(rdev,
  1402. r10_bio->devs[sl].addr +
  1403. sect,
  1404. s<<9, conf->tmppage, WRITE, false)
  1405. == 0) {
  1406. /* Well, this device is dead */
  1407. printk(KERN_NOTICE
  1408. "md/raid10:%s: read correction "
  1409. "write failed"
  1410. " (%d sectors at %llu on %s)\n",
  1411. mdname(mddev), s,
  1412. (unsigned long long)(sect+
  1413. rdev->data_offset),
  1414. bdevname(rdev->bdev, b));
  1415. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  1416. "drive\n",
  1417. mdname(mddev),
  1418. bdevname(rdev->bdev, b));
  1419. md_error(mddev, rdev);
  1420. }
  1421. rdev_dec_pending(rdev, mddev);
  1422. rcu_read_lock();
  1423. }
  1424. }
  1425. sl = start;
  1426. while (sl != r10_bio->read_slot) {
  1427. if (sl==0)
  1428. sl = conf->copies;
  1429. sl--;
  1430. d = r10_bio->devs[sl].devnum;
  1431. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1432. if (rdev &&
  1433. test_bit(In_sync, &rdev->flags)) {
  1434. char b[BDEVNAME_SIZE];
  1435. atomic_inc(&rdev->nr_pending);
  1436. rcu_read_unlock();
  1437. if (sync_page_io(rdev,
  1438. r10_bio->devs[sl].addr +
  1439. sect,
  1440. s<<9, conf->tmppage,
  1441. READ, false) == 0) {
  1442. /* Well, this device is dead */
  1443. printk(KERN_NOTICE
  1444. "md/raid10:%s: unable to read back "
  1445. "corrected sectors"
  1446. " (%d sectors at %llu on %s)\n",
  1447. mdname(mddev), s,
  1448. (unsigned long long)(sect+
  1449. rdev->data_offset),
  1450. bdevname(rdev->bdev, b));
  1451. printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
  1452. mdname(mddev),
  1453. bdevname(rdev->bdev, b));
  1454. md_error(mddev, rdev);
  1455. } else {
  1456. printk(KERN_INFO
  1457. "md/raid10:%s: read error corrected"
  1458. " (%d sectors at %llu on %s)\n",
  1459. mdname(mddev), s,
  1460. (unsigned long long)(sect+
  1461. rdev->data_offset),
  1462. bdevname(rdev->bdev, b));
  1463. }
  1464. rdev_dec_pending(rdev, mddev);
  1465. rcu_read_lock();
  1466. }
  1467. }
  1468. rcu_read_unlock();
  1469. sectors -= s;
  1470. sect += s;
  1471. }
  1472. }
  1473. static void raid10d(mddev_t *mddev)
  1474. {
  1475. r10bio_t *r10_bio;
  1476. struct bio *bio;
  1477. unsigned long flags;
  1478. conf_t *conf = mddev->private;
  1479. struct list_head *head = &conf->retry_list;
  1480. int unplug=0;
  1481. mdk_rdev_t *rdev;
  1482. md_check_recovery(mddev);
  1483. for (;;) {
  1484. char b[BDEVNAME_SIZE];
  1485. unplug += flush_pending_writes(conf);
  1486. spin_lock_irqsave(&conf->device_lock, flags);
  1487. if (list_empty(head)) {
  1488. spin_unlock_irqrestore(&conf->device_lock, flags);
  1489. break;
  1490. }
  1491. r10_bio = list_entry(head->prev, r10bio_t, retry_list);
  1492. list_del(head->prev);
  1493. conf->nr_queued--;
  1494. spin_unlock_irqrestore(&conf->device_lock, flags);
  1495. mddev = r10_bio->mddev;
  1496. conf = mddev->private;
  1497. if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
  1498. sync_request_write(mddev, r10_bio);
  1499. unplug = 1;
  1500. } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  1501. recovery_request_write(mddev, r10_bio);
  1502. unplug = 1;
  1503. } else {
  1504. int mirror;
  1505. /* we got a read error. Maybe the drive is bad. Maybe just
  1506. * the block and we can fix it.
  1507. * We freeze all other IO, and try reading the block from
  1508. * other devices. When we find one, we re-write
  1509. * and check it that fixes the read error.
  1510. * This is all done synchronously while the array is
  1511. * frozen.
  1512. */
  1513. if (mddev->ro == 0) {
  1514. freeze_array(conf);
  1515. fix_read_error(conf, mddev, r10_bio);
  1516. unfreeze_array(conf);
  1517. }
  1518. bio = r10_bio->devs[r10_bio->read_slot].bio;
  1519. r10_bio->devs[r10_bio->read_slot].bio =
  1520. mddev->ro ? IO_BLOCKED : NULL;
  1521. mirror = read_balance(conf, r10_bio);
  1522. if (mirror == -1) {
  1523. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  1524. " read error for block %llu\n",
  1525. mdname(mddev),
  1526. bdevname(bio->bi_bdev,b),
  1527. (unsigned long long)r10_bio->sector);
  1528. raid_end_bio_io(r10_bio);
  1529. bio_put(bio);
  1530. } else {
  1531. const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  1532. bio_put(bio);
  1533. rdev = conf->mirrors[mirror].rdev;
  1534. if (printk_ratelimit())
  1535. printk(KERN_ERR "md/raid10:%s: %s: redirecting sector %llu to"
  1536. " another mirror\n",
  1537. mdname(mddev),
  1538. bdevname(rdev->bdev,b),
  1539. (unsigned long long)r10_bio->sector);
  1540. bio = bio_clone_mddev(r10_bio->master_bio,
  1541. GFP_NOIO, mddev);
  1542. r10_bio->devs[r10_bio->read_slot].bio = bio;
  1543. bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
  1544. + rdev->data_offset;
  1545. bio->bi_bdev = rdev->bdev;
  1546. bio->bi_rw = READ | do_sync;
  1547. bio->bi_private = r10_bio;
  1548. bio->bi_end_io = raid10_end_read_request;
  1549. unplug = 1;
  1550. generic_make_request(bio);
  1551. }
  1552. }
  1553. cond_resched();
  1554. }
  1555. if (unplug)
  1556. unplug_slaves(mddev);
  1557. }
  1558. static int init_resync(conf_t *conf)
  1559. {
  1560. int buffs;
  1561. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1562. BUG_ON(conf->r10buf_pool);
  1563. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  1564. if (!conf->r10buf_pool)
  1565. return -ENOMEM;
  1566. conf->next_resync = 0;
  1567. return 0;
  1568. }
  1569. /*
  1570. * perform a "sync" on one "block"
  1571. *
  1572. * We need to make sure that no normal I/O request - particularly write
  1573. * requests - conflict with active sync requests.
  1574. *
  1575. * This is achieved by tracking pending requests and a 'barrier' concept
  1576. * that can be installed to exclude normal IO requests.
  1577. *
  1578. * Resync and recovery are handled very differently.
  1579. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  1580. *
  1581. * For resync, we iterate over virtual addresses, read all copies,
  1582. * and update if there are differences. If only one copy is live,
  1583. * skip it.
  1584. * For recovery, we iterate over physical addresses, read a good
  1585. * value for each non-in_sync drive, and over-write.
  1586. *
  1587. * So, for recovery we may have several outstanding complex requests for a
  1588. * given address, one for each out-of-sync device. We model this by allocating
  1589. * a number of r10_bio structures, one for each out-of-sync device.
  1590. * As we setup these structures, we collect all bio's together into a list
  1591. * which we then process collectively to add pages, and then process again
  1592. * to pass to generic_make_request.
  1593. *
  1594. * The r10_bio structures are linked using a borrowed master_bio pointer.
  1595. * This link is counted in ->remaining. When the r10_bio that points to NULL
  1596. * has its remaining count decremented to 0, the whole complex operation
  1597. * is complete.
  1598. *
  1599. */
  1600. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1601. {
  1602. conf_t *conf = mddev->private;
  1603. r10bio_t *r10_bio;
  1604. struct bio *biolist = NULL, *bio;
  1605. sector_t max_sector, nr_sectors;
  1606. int disk;
  1607. int i;
  1608. int max_sync;
  1609. sector_t sync_blocks;
  1610. sector_t sectors_skipped = 0;
  1611. int chunks_skipped = 0;
  1612. if (!conf->r10buf_pool)
  1613. if (init_resync(conf))
  1614. return 0;
  1615. skipped:
  1616. max_sector = mddev->dev_sectors;
  1617. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1618. max_sector = mddev->resync_max_sectors;
  1619. if (sector_nr >= max_sector) {
  1620. /* If we aborted, we need to abort the
  1621. * sync on the 'current' bitmap chucks (there can
  1622. * be several when recovering multiple devices).
  1623. * as we may have started syncing it but not finished.
  1624. * We can find the current address in
  1625. * mddev->curr_resync, but for recovery,
  1626. * we need to convert that to several
  1627. * virtual addresses.
  1628. */
  1629. if (mddev->curr_resync < max_sector) { /* aborted */
  1630. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1631. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1632. &sync_blocks, 1);
  1633. else for (i=0; i<conf->raid_disks; i++) {
  1634. sector_t sect =
  1635. raid10_find_virt(conf, mddev->curr_resync, i);
  1636. bitmap_end_sync(mddev->bitmap, sect,
  1637. &sync_blocks, 1);
  1638. }
  1639. } else /* completed sync */
  1640. conf->fullsync = 0;
  1641. bitmap_close_sync(mddev->bitmap);
  1642. close_sync(conf);
  1643. *skipped = 1;
  1644. return sectors_skipped;
  1645. }
  1646. if (chunks_skipped >= conf->raid_disks) {
  1647. /* if there has been nothing to do on any drive,
  1648. * then there is nothing to do at all..
  1649. */
  1650. *skipped = 1;
  1651. return (max_sector - sector_nr) + sectors_skipped;
  1652. }
  1653. if (max_sector > mddev->resync_max)
  1654. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  1655. /* make sure whole request will fit in a chunk - if chunks
  1656. * are meaningful
  1657. */
  1658. if (conf->near_copies < conf->raid_disks &&
  1659. max_sector > (sector_nr | conf->chunk_mask))
  1660. max_sector = (sector_nr | conf->chunk_mask) + 1;
  1661. /*
  1662. * If there is non-resync activity waiting for us then
  1663. * put in a delay to throttle resync.
  1664. */
  1665. if (!go_faster && conf->nr_waiting)
  1666. msleep_interruptible(1000);
  1667. /* Again, very different code for resync and recovery.
  1668. * Both must result in an r10bio with a list of bios that
  1669. * have bi_end_io, bi_sector, bi_bdev set,
  1670. * and bi_private set to the r10bio.
  1671. * For recovery, we may actually create several r10bios
  1672. * with 2 bios in each, that correspond to the bios in the main one.
  1673. * In this case, the subordinate r10bios link back through a
  1674. * borrowed master_bio pointer, and the counter in the master
  1675. * includes a ref from each subordinate.
  1676. */
  1677. /* First, we decide what to do and set ->bi_end_io
  1678. * To end_sync_read if we want to read, and
  1679. * end_sync_write if we will want to write.
  1680. */
  1681. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  1682. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1683. /* recovery... the complicated one */
  1684. int j, k;
  1685. r10_bio = NULL;
  1686. for (i=0 ; i<conf->raid_disks; i++)
  1687. if (conf->mirrors[i].rdev &&
  1688. !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
  1689. int still_degraded = 0;
  1690. /* want to reconstruct this device */
  1691. r10bio_t *rb2 = r10_bio;
  1692. sector_t sect = raid10_find_virt(conf, sector_nr, i);
  1693. int must_sync;
  1694. /* Unless we are doing a full sync, we only need
  1695. * to recover the block if it is set in the bitmap
  1696. */
  1697. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1698. &sync_blocks, 1);
  1699. if (sync_blocks < max_sync)
  1700. max_sync = sync_blocks;
  1701. if (!must_sync &&
  1702. !conf->fullsync) {
  1703. /* yep, skip the sync_blocks here, but don't assume
  1704. * that there will never be anything to do here
  1705. */
  1706. chunks_skipped = -1;
  1707. continue;
  1708. }
  1709. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1710. raise_barrier(conf, rb2 != NULL);
  1711. atomic_set(&r10_bio->remaining, 0);
  1712. r10_bio->master_bio = (struct bio*)rb2;
  1713. if (rb2)
  1714. atomic_inc(&rb2->remaining);
  1715. r10_bio->mddev = mddev;
  1716. set_bit(R10BIO_IsRecover, &r10_bio->state);
  1717. r10_bio->sector = sect;
  1718. raid10_find_phys(conf, r10_bio);
  1719. /* Need to check if the array will still be
  1720. * degraded
  1721. */
  1722. for (j=0; j<conf->raid_disks; j++)
  1723. if (conf->mirrors[j].rdev == NULL ||
  1724. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  1725. still_degraded = 1;
  1726. break;
  1727. }
  1728. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1729. &sync_blocks, still_degraded);
  1730. for (j=0; j<conf->copies;j++) {
  1731. int d = r10_bio->devs[j].devnum;
  1732. if (conf->mirrors[d].rdev &&
  1733. test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
  1734. /* This is where we read from */
  1735. bio = r10_bio->devs[0].bio;
  1736. bio->bi_next = biolist;
  1737. biolist = bio;
  1738. bio->bi_private = r10_bio;
  1739. bio->bi_end_io = end_sync_read;
  1740. bio->bi_rw = READ;
  1741. bio->bi_sector = r10_bio->devs[j].addr +
  1742. conf->mirrors[d].rdev->data_offset;
  1743. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1744. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1745. atomic_inc(&r10_bio->remaining);
  1746. /* and we write to 'i' */
  1747. for (k=0; k<conf->copies; k++)
  1748. if (r10_bio->devs[k].devnum == i)
  1749. break;
  1750. BUG_ON(k == conf->copies);
  1751. bio = r10_bio->devs[1].bio;
  1752. bio->bi_next = biolist;
  1753. biolist = bio;
  1754. bio->bi_private = r10_bio;
  1755. bio->bi_end_io = end_sync_write;
  1756. bio->bi_rw = WRITE;
  1757. bio->bi_sector = r10_bio->devs[k].addr +
  1758. conf->mirrors[i].rdev->data_offset;
  1759. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1760. r10_bio->devs[0].devnum = d;
  1761. r10_bio->devs[1].devnum = i;
  1762. break;
  1763. }
  1764. }
  1765. if (j == conf->copies) {
  1766. /* Cannot recover, so abort the recovery */
  1767. put_buf(r10_bio);
  1768. if (rb2)
  1769. atomic_dec(&rb2->remaining);
  1770. r10_bio = rb2;
  1771. if (!test_and_set_bit(MD_RECOVERY_INTR,
  1772. &mddev->recovery))
  1773. printk(KERN_INFO "md/raid10:%s: insufficient "
  1774. "working devices for recovery.\n",
  1775. mdname(mddev));
  1776. break;
  1777. }
  1778. }
  1779. if (biolist == NULL) {
  1780. while (r10_bio) {
  1781. r10bio_t *rb2 = r10_bio;
  1782. r10_bio = (r10bio_t*) rb2->master_bio;
  1783. rb2->master_bio = NULL;
  1784. put_buf(rb2);
  1785. }
  1786. goto giveup;
  1787. }
  1788. } else {
  1789. /* resync. Schedule a read for every block at this virt offset */
  1790. int count = 0;
  1791. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  1792. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1793. &sync_blocks, mddev->degraded) &&
  1794. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1795. /* We can skip this block */
  1796. *skipped = 1;
  1797. return sync_blocks + sectors_skipped;
  1798. }
  1799. if (sync_blocks < max_sync)
  1800. max_sync = sync_blocks;
  1801. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1802. r10_bio->mddev = mddev;
  1803. atomic_set(&r10_bio->remaining, 0);
  1804. raise_barrier(conf, 0);
  1805. conf->next_resync = sector_nr;
  1806. r10_bio->master_bio = NULL;
  1807. r10_bio->sector = sector_nr;
  1808. set_bit(R10BIO_IsSync, &r10_bio->state);
  1809. raid10_find_phys(conf, r10_bio);
  1810. r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
  1811. for (i=0; i<conf->copies; i++) {
  1812. int d = r10_bio->devs[i].devnum;
  1813. bio = r10_bio->devs[i].bio;
  1814. bio->bi_end_io = NULL;
  1815. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1816. if (conf->mirrors[d].rdev == NULL ||
  1817. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  1818. continue;
  1819. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1820. atomic_inc(&r10_bio->remaining);
  1821. bio->bi_next = biolist;
  1822. biolist = bio;
  1823. bio->bi_private = r10_bio;
  1824. bio->bi_end_io = end_sync_read;
  1825. bio->bi_rw = READ;
  1826. bio->bi_sector = r10_bio->devs[i].addr +
  1827. conf->mirrors[d].rdev->data_offset;
  1828. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1829. count++;
  1830. }
  1831. if (count < 2) {
  1832. for (i=0; i<conf->copies; i++) {
  1833. int d = r10_bio->devs[i].devnum;
  1834. if (r10_bio->devs[i].bio->bi_end_io)
  1835. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1836. }
  1837. put_buf(r10_bio);
  1838. biolist = NULL;
  1839. goto giveup;
  1840. }
  1841. }
  1842. for (bio = biolist; bio ; bio=bio->bi_next) {
  1843. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1844. if (bio->bi_end_io)
  1845. bio->bi_flags |= 1 << BIO_UPTODATE;
  1846. bio->bi_vcnt = 0;
  1847. bio->bi_idx = 0;
  1848. bio->bi_phys_segments = 0;
  1849. bio->bi_size = 0;
  1850. }
  1851. nr_sectors = 0;
  1852. if (sector_nr + max_sync < max_sector)
  1853. max_sector = sector_nr + max_sync;
  1854. do {
  1855. struct page *page;
  1856. int len = PAGE_SIZE;
  1857. disk = 0;
  1858. if (sector_nr + (len>>9) > max_sector)
  1859. len = (max_sector - sector_nr) << 9;
  1860. if (len == 0)
  1861. break;
  1862. for (bio= biolist ; bio ; bio=bio->bi_next) {
  1863. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  1864. if (bio_add_page(bio, page, len, 0) == 0) {
  1865. /* stop here */
  1866. struct bio *bio2;
  1867. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1868. for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
  1869. /* remove last page from this bio */
  1870. bio2->bi_vcnt--;
  1871. bio2->bi_size -= len;
  1872. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  1873. }
  1874. goto bio_full;
  1875. }
  1876. disk = i;
  1877. }
  1878. nr_sectors += len>>9;
  1879. sector_nr += len>>9;
  1880. } while (biolist->bi_vcnt < RESYNC_PAGES);
  1881. bio_full:
  1882. r10_bio->sectors = nr_sectors;
  1883. while (biolist) {
  1884. bio = biolist;
  1885. biolist = biolist->bi_next;
  1886. bio->bi_next = NULL;
  1887. r10_bio = bio->bi_private;
  1888. r10_bio->sectors = nr_sectors;
  1889. if (bio->bi_end_io == end_sync_read) {
  1890. md_sync_acct(bio->bi_bdev, nr_sectors);
  1891. generic_make_request(bio);
  1892. }
  1893. }
  1894. if (sectors_skipped)
  1895. /* pretend they weren't skipped, it makes
  1896. * no important difference in this case
  1897. */
  1898. md_done_sync(mddev, sectors_skipped, 1);
  1899. return sectors_skipped + nr_sectors;
  1900. giveup:
  1901. /* There is nowhere to write, so all non-sync
  1902. * drives must be failed, so try the next chunk...
  1903. */
  1904. if (sector_nr + max_sync < max_sector)
  1905. max_sector = sector_nr + max_sync;
  1906. sectors_skipped += (max_sector - sector_nr);
  1907. chunks_skipped ++;
  1908. sector_nr = max_sector;
  1909. goto skipped;
  1910. }
  1911. static sector_t
  1912. raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  1913. {
  1914. sector_t size;
  1915. conf_t *conf = mddev->private;
  1916. if (!raid_disks)
  1917. raid_disks = conf->raid_disks;
  1918. if (!sectors)
  1919. sectors = conf->dev_sectors;
  1920. size = sectors >> conf->chunk_shift;
  1921. sector_div(size, conf->far_copies);
  1922. size = size * raid_disks;
  1923. sector_div(size, conf->near_copies);
  1924. return size << conf->chunk_shift;
  1925. }
  1926. static conf_t *setup_conf(mddev_t *mddev)
  1927. {
  1928. conf_t *conf = NULL;
  1929. int nc, fc, fo;
  1930. sector_t stride, size;
  1931. int err = -EINVAL;
  1932. if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
  1933. !is_power_of_2(mddev->new_chunk_sectors)) {
  1934. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  1935. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  1936. mdname(mddev), PAGE_SIZE);
  1937. goto out;
  1938. }
  1939. nc = mddev->new_layout & 255;
  1940. fc = (mddev->new_layout >> 8) & 255;
  1941. fo = mddev->new_layout & (1<<16);
  1942. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  1943. (mddev->new_layout >> 17)) {
  1944. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  1945. mdname(mddev), mddev->new_layout);
  1946. goto out;
  1947. }
  1948. err = -ENOMEM;
  1949. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  1950. if (!conf)
  1951. goto out;
  1952. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1953. GFP_KERNEL);
  1954. if (!conf->mirrors)
  1955. goto out;
  1956. conf->tmppage = alloc_page(GFP_KERNEL);
  1957. if (!conf->tmppage)
  1958. goto out;
  1959. conf->raid_disks = mddev->raid_disks;
  1960. conf->near_copies = nc;
  1961. conf->far_copies = fc;
  1962. conf->copies = nc*fc;
  1963. conf->far_offset = fo;
  1964. conf->chunk_mask = mddev->new_chunk_sectors - 1;
  1965. conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
  1966. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  1967. r10bio_pool_free, conf);
  1968. if (!conf->r10bio_pool)
  1969. goto out;
  1970. size = mddev->dev_sectors >> conf->chunk_shift;
  1971. sector_div(size, fc);
  1972. size = size * conf->raid_disks;
  1973. sector_div(size, nc);
  1974. /* 'size' is now the number of chunks in the array */
  1975. /* calculate "used chunks per device" in 'stride' */
  1976. stride = size * conf->copies;
  1977. /* We need to round up when dividing by raid_disks to
  1978. * get the stride size.
  1979. */
  1980. stride += conf->raid_disks - 1;
  1981. sector_div(stride, conf->raid_disks);
  1982. conf->dev_sectors = stride << conf->chunk_shift;
  1983. if (fo)
  1984. stride = 1;
  1985. else
  1986. sector_div(stride, fc);
  1987. conf->stride = stride << conf->chunk_shift;
  1988. spin_lock_init(&conf->device_lock);
  1989. INIT_LIST_HEAD(&conf->retry_list);
  1990. spin_lock_init(&conf->resync_lock);
  1991. init_waitqueue_head(&conf->wait_barrier);
  1992. conf->thread = md_register_thread(raid10d, mddev, NULL);
  1993. if (!conf->thread)
  1994. goto out;
  1995. conf->mddev = mddev;
  1996. return conf;
  1997. out:
  1998. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  1999. mdname(mddev));
  2000. if (conf) {
  2001. if (conf->r10bio_pool)
  2002. mempool_destroy(conf->r10bio_pool);
  2003. kfree(conf->mirrors);
  2004. safe_put_page(conf->tmppage);
  2005. kfree(conf);
  2006. }
  2007. return ERR_PTR(err);
  2008. }
  2009. static int run(mddev_t *mddev)
  2010. {
  2011. conf_t *conf;
  2012. int i, disk_idx, chunk_size;
  2013. mirror_info_t *disk;
  2014. mdk_rdev_t *rdev;
  2015. sector_t size;
  2016. /*
  2017. * copy the already verified devices into our private RAID10
  2018. * bookkeeping area. [whatever we allocate in run(),
  2019. * should be freed in stop()]
  2020. */
  2021. if (mddev->private == NULL) {
  2022. conf = setup_conf(mddev);
  2023. if (IS_ERR(conf))
  2024. return PTR_ERR(conf);
  2025. mddev->private = conf;
  2026. }
  2027. conf = mddev->private;
  2028. if (!conf)
  2029. goto out;
  2030. mddev->thread = conf->thread;
  2031. conf->thread = NULL;
  2032. chunk_size = mddev->chunk_sectors << 9;
  2033. blk_queue_io_min(mddev->queue, chunk_size);
  2034. if (conf->raid_disks % conf->near_copies)
  2035. blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
  2036. else
  2037. blk_queue_io_opt(mddev->queue, chunk_size *
  2038. (conf->raid_disks / conf->near_copies));
  2039. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2040. disk_idx = rdev->raid_disk;
  2041. if (disk_idx >= conf->raid_disks
  2042. || disk_idx < 0)
  2043. continue;
  2044. disk = conf->mirrors + disk_idx;
  2045. disk->rdev = rdev;
  2046. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2047. rdev->data_offset << 9);
  2048. /* as we don't honour merge_bvec_fn, we must never risk
  2049. * violating it, so limit max_segments to 1 lying
  2050. * within a single page.
  2051. */
  2052. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  2053. blk_queue_max_segments(mddev->queue, 1);
  2054. blk_queue_segment_boundary(mddev->queue,
  2055. PAGE_CACHE_SIZE - 1);
  2056. }
  2057. disk->head_position = 0;
  2058. }
  2059. /* need to check that every block has at least one working mirror */
  2060. if (!enough(conf)) {
  2061. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  2062. mdname(mddev));
  2063. goto out_free_conf;
  2064. }
  2065. mddev->degraded = 0;
  2066. for (i = 0; i < conf->raid_disks; i++) {
  2067. disk = conf->mirrors + i;
  2068. if (!disk->rdev ||
  2069. !test_bit(In_sync, &disk->rdev->flags)) {
  2070. disk->head_position = 0;
  2071. mddev->degraded++;
  2072. if (disk->rdev)
  2073. conf->fullsync = 1;
  2074. }
  2075. }
  2076. if (mddev->recovery_cp != MaxSector)
  2077. printk(KERN_NOTICE "md/raid10:%s: not clean"
  2078. " -- starting background reconstruction\n",
  2079. mdname(mddev));
  2080. printk(KERN_INFO
  2081. "md/raid10:%s: active with %d out of %d devices\n",
  2082. mdname(mddev), conf->raid_disks - mddev->degraded,
  2083. conf->raid_disks);
  2084. /*
  2085. * Ok, everything is just fine now
  2086. */
  2087. mddev->dev_sectors = conf->dev_sectors;
  2088. size = raid10_size(mddev, 0, 0);
  2089. md_set_array_sectors(mddev, size);
  2090. mddev->resync_max_sectors = size;
  2091. mddev->queue->unplug_fn = raid10_unplug;
  2092. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  2093. mddev->queue->backing_dev_info.congested_data = mddev;
  2094. /* Calculate max read-ahead size.
  2095. * We need to readahead at least twice a whole stripe....
  2096. * maybe...
  2097. */
  2098. {
  2099. int stripe = conf->raid_disks *
  2100. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  2101. stripe /= conf->near_copies;
  2102. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  2103. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  2104. }
  2105. if (conf->near_copies < conf->raid_disks)
  2106. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  2107. md_integrity_register(mddev);
  2108. return 0;
  2109. out_free_conf:
  2110. md_unregister_thread(mddev->thread);
  2111. if (conf->r10bio_pool)
  2112. mempool_destroy(conf->r10bio_pool);
  2113. safe_put_page(conf->tmppage);
  2114. kfree(conf->mirrors);
  2115. kfree(conf);
  2116. mddev->private = NULL;
  2117. out:
  2118. return -EIO;
  2119. }
  2120. static int stop(mddev_t *mddev)
  2121. {
  2122. conf_t *conf = mddev->private;
  2123. raise_barrier(conf, 0);
  2124. lower_barrier(conf);
  2125. md_unregister_thread(mddev->thread);
  2126. mddev->thread = NULL;
  2127. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  2128. if (conf->r10bio_pool)
  2129. mempool_destroy(conf->r10bio_pool);
  2130. kfree(conf->mirrors);
  2131. kfree(conf);
  2132. mddev->private = NULL;
  2133. return 0;
  2134. }
  2135. static void raid10_quiesce(mddev_t *mddev, int state)
  2136. {
  2137. conf_t *conf = mddev->private;
  2138. switch(state) {
  2139. case 1:
  2140. raise_barrier(conf, 0);
  2141. break;
  2142. case 0:
  2143. lower_barrier(conf);
  2144. break;
  2145. }
  2146. }
  2147. static void *raid10_takeover_raid0(mddev_t *mddev)
  2148. {
  2149. mdk_rdev_t *rdev;
  2150. conf_t *conf;
  2151. if (mddev->degraded > 0) {
  2152. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  2153. mdname(mddev));
  2154. return ERR_PTR(-EINVAL);
  2155. }
  2156. /* Set new parameters */
  2157. mddev->new_level = 10;
  2158. /* new layout: far_copies = 1, near_copies = 2 */
  2159. mddev->new_layout = (1<<8) + 2;
  2160. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2161. mddev->delta_disks = mddev->raid_disks;
  2162. mddev->raid_disks *= 2;
  2163. /* make sure it will be not marked as dirty */
  2164. mddev->recovery_cp = MaxSector;
  2165. conf = setup_conf(mddev);
  2166. if (!IS_ERR(conf)) {
  2167. list_for_each_entry(rdev, &mddev->disks, same_set)
  2168. if (rdev->raid_disk >= 0)
  2169. rdev->new_raid_disk = rdev->raid_disk * 2;
  2170. conf->barrier = 1;
  2171. }
  2172. return conf;
  2173. }
  2174. static void *raid10_takeover(mddev_t *mddev)
  2175. {
  2176. struct raid0_private_data *raid0_priv;
  2177. /* raid10 can take over:
  2178. * raid0 - providing it has only two drives
  2179. */
  2180. if (mddev->level == 0) {
  2181. /* for raid0 takeover only one zone is supported */
  2182. raid0_priv = mddev->private;
  2183. if (raid0_priv->nr_strip_zones > 1) {
  2184. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  2185. " with more than one zone.\n",
  2186. mdname(mddev));
  2187. return ERR_PTR(-EINVAL);
  2188. }
  2189. return raid10_takeover_raid0(mddev);
  2190. }
  2191. return ERR_PTR(-EINVAL);
  2192. }
  2193. static struct mdk_personality raid10_personality =
  2194. {
  2195. .name = "raid10",
  2196. .level = 10,
  2197. .owner = THIS_MODULE,
  2198. .make_request = make_request,
  2199. .run = run,
  2200. .stop = stop,
  2201. .status = status,
  2202. .error_handler = error,
  2203. .hot_add_disk = raid10_add_disk,
  2204. .hot_remove_disk= raid10_remove_disk,
  2205. .spare_active = raid10_spare_active,
  2206. .sync_request = sync_request,
  2207. .quiesce = raid10_quiesce,
  2208. .size = raid10_size,
  2209. .takeover = raid10_takeover,
  2210. };
  2211. static int __init raid_init(void)
  2212. {
  2213. return register_md_personality(&raid10_personality);
  2214. }
  2215. static void raid_exit(void)
  2216. {
  2217. unregister_md_personality(&raid10_personality);
  2218. }
  2219. module_init(raid_init);
  2220. module_exit(raid_exit);
  2221. MODULE_LICENSE("GPL");
  2222. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  2223. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  2224. MODULE_ALIAS("md-raid10");
  2225. MODULE_ALIAS("md-level-10");