raid10.c 92 KB

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