raid10.c 82 KB

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