raid10.c 81 KB

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