raid10.c 84 KB

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