raid10.c 88 KB

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