raid10.c 74 KB

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