raid10.c 93 KB

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