raid1.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448
  1. /*
  2. * raid1.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
  5. *
  6. * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  7. *
  8. * RAID-1 management functions.
  9. *
  10. * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
  11. *
  12. * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
  13. * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
  14. *
  15. * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
  16. * bitmapped intelligence in resync:
  17. *
  18. * - bitmap marked during normal i/o
  19. * - bitmap used to skip nondirty blocks during sync
  20. *
  21. * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
  22. * - persistent bitmap code
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2, or (at your option)
  27. * any later version.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * (for example /usr/src/linux/COPYING); if not, write to the Free
  31. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/slab.h>
  34. #include <linux/delay.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/module.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/ratelimit.h>
  39. #include <linux/sched/signal.h>
  40. #include <trace/events/block.h>
  41. #include "md.h"
  42. #include "raid1.h"
  43. #include "bitmap.h"
  44. #define UNSUPPORTED_MDDEV_FLAGS \
  45. ((1L << MD_HAS_JOURNAL) | \
  46. (1L << MD_JOURNAL_CLEAN))
  47. /*
  48. * Number of guaranteed r1bios in case of extreme VM load:
  49. */
  50. #define NR_RAID1_BIOS 256
  51. /* when we get a read error on a read-only array, we redirect to another
  52. * device without failing the first device, or trying to over-write to
  53. * correct the read error. To keep track of bad blocks on a per-bio
  54. * level, we store IO_BLOCKED in the appropriate 'bios' pointer
  55. */
  56. #define IO_BLOCKED ((struct bio *)1)
  57. /* When we successfully write to a known bad-block, we need to remove the
  58. * bad-block marking which must be done from process context. So we record
  59. * the success by setting devs[n].bio to IO_MADE_GOOD
  60. */
  61. #define IO_MADE_GOOD ((struct bio *)2)
  62. #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
  63. /* When there are this many requests queue to be written by
  64. * the raid1 thread, we become 'congested' to provide back-pressure
  65. * for writeback.
  66. */
  67. static int max_queued_requests = 1024;
  68. static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
  69. static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
  70. #define raid1_log(md, fmt, args...) \
  71. do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
  72. static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
  73. {
  74. struct pool_info *pi = data;
  75. int size = offsetof(struct r1bio, bios[pi->raid_disks]);
  76. /* allocate a r1bio with room for raid_disks entries in the bios array */
  77. return kzalloc(size, gfp_flags);
  78. }
  79. static void r1bio_pool_free(void *r1_bio, void *data)
  80. {
  81. kfree(r1_bio);
  82. }
  83. #define RESYNC_BLOCK_SIZE (64*1024)
  84. #define RESYNC_DEPTH 32
  85. #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
  86. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  87. #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
  88. #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
  89. #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
  90. #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
  91. static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
  92. {
  93. struct pool_info *pi = data;
  94. struct r1bio *r1_bio;
  95. struct bio *bio;
  96. int need_pages;
  97. int i, j;
  98. r1_bio = r1bio_pool_alloc(gfp_flags, pi);
  99. if (!r1_bio)
  100. return NULL;
  101. /*
  102. * Allocate bios : 1 for reading, n-1 for writing
  103. */
  104. for (j = pi->raid_disks ; j-- ; ) {
  105. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  106. if (!bio)
  107. goto out_free_bio;
  108. r1_bio->bios[j] = bio;
  109. }
  110. /*
  111. * Allocate RESYNC_PAGES data pages and attach them to
  112. * the first bio.
  113. * If this is a user-requested check/repair, allocate
  114. * RESYNC_PAGES for each bio.
  115. */
  116. if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
  117. need_pages = pi->raid_disks;
  118. else
  119. need_pages = 1;
  120. for (j = 0; j < need_pages; j++) {
  121. bio = r1_bio->bios[j];
  122. bio->bi_vcnt = RESYNC_PAGES;
  123. if (bio_alloc_pages(bio, gfp_flags))
  124. goto out_free_pages;
  125. }
  126. /* If not user-requests, copy the page pointers to all bios */
  127. if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
  128. for (i=0; i<RESYNC_PAGES ; i++)
  129. for (j=1; j<pi->raid_disks; j++)
  130. r1_bio->bios[j]->bi_io_vec[i].bv_page =
  131. r1_bio->bios[0]->bi_io_vec[i].bv_page;
  132. }
  133. r1_bio->master_bio = NULL;
  134. return r1_bio;
  135. out_free_pages:
  136. while (--j >= 0)
  137. bio_free_pages(r1_bio->bios[j]);
  138. out_free_bio:
  139. while (++j < pi->raid_disks)
  140. bio_put(r1_bio->bios[j]);
  141. r1bio_pool_free(r1_bio, data);
  142. return NULL;
  143. }
  144. static void r1buf_pool_free(void *__r1_bio, void *data)
  145. {
  146. struct pool_info *pi = data;
  147. int i,j;
  148. struct r1bio *r1bio = __r1_bio;
  149. for (i = 0; i < RESYNC_PAGES; i++)
  150. for (j = pi->raid_disks; j-- ;) {
  151. if (j == 0 ||
  152. r1bio->bios[j]->bi_io_vec[i].bv_page !=
  153. r1bio->bios[0]->bi_io_vec[i].bv_page)
  154. safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
  155. }
  156. for (i=0 ; i < pi->raid_disks; i++)
  157. bio_put(r1bio->bios[i]);
  158. r1bio_pool_free(r1bio, data);
  159. }
  160. static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
  161. {
  162. int i;
  163. for (i = 0; i < conf->raid_disks * 2; i++) {
  164. struct bio **bio = r1_bio->bios + i;
  165. if (!BIO_SPECIAL(*bio))
  166. bio_put(*bio);
  167. *bio = NULL;
  168. }
  169. }
  170. static void free_r1bio(struct r1bio *r1_bio)
  171. {
  172. struct r1conf *conf = r1_bio->mddev->private;
  173. put_all_bios(conf, r1_bio);
  174. mempool_free(r1_bio, conf->r1bio_pool);
  175. }
  176. static void put_buf(struct r1bio *r1_bio)
  177. {
  178. struct r1conf *conf = r1_bio->mddev->private;
  179. sector_t sect = r1_bio->sector;
  180. int i;
  181. for (i = 0; i < conf->raid_disks * 2; i++) {
  182. struct bio *bio = r1_bio->bios[i];
  183. if (bio->bi_end_io)
  184. rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
  185. }
  186. mempool_free(r1_bio, conf->r1buf_pool);
  187. lower_barrier(conf, sect);
  188. }
  189. static void reschedule_retry(struct r1bio *r1_bio)
  190. {
  191. unsigned long flags;
  192. struct mddev *mddev = r1_bio->mddev;
  193. struct r1conf *conf = mddev->private;
  194. int idx;
  195. idx = sector_to_idx(r1_bio->sector);
  196. spin_lock_irqsave(&conf->device_lock, flags);
  197. list_add(&r1_bio->retry_list, &conf->retry_list);
  198. atomic_inc(&conf->nr_queued[idx]);
  199. spin_unlock_irqrestore(&conf->device_lock, flags);
  200. wake_up(&conf->wait_barrier);
  201. md_wakeup_thread(mddev->thread);
  202. }
  203. /*
  204. * raid_end_bio_io() is called when we have finished servicing a mirrored
  205. * operation and are ready to return a success/failure code to the buffer
  206. * cache layer.
  207. */
  208. static void call_bio_endio(struct r1bio *r1_bio)
  209. {
  210. struct bio *bio = r1_bio->master_bio;
  211. int done;
  212. struct r1conf *conf = r1_bio->mddev->private;
  213. sector_t bi_sector = bio->bi_iter.bi_sector;
  214. if (bio->bi_phys_segments) {
  215. unsigned long flags;
  216. spin_lock_irqsave(&conf->device_lock, flags);
  217. bio->bi_phys_segments--;
  218. done = (bio->bi_phys_segments == 0);
  219. spin_unlock_irqrestore(&conf->device_lock, flags);
  220. /*
  221. * make_request() might be waiting for
  222. * bi_phys_segments to decrease
  223. */
  224. wake_up(&conf->wait_barrier);
  225. } else
  226. done = 1;
  227. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  228. bio->bi_error = -EIO;
  229. if (done) {
  230. bio_endio(bio);
  231. /*
  232. * Wake up any possible resync thread that waits for the device
  233. * to go idle.
  234. */
  235. allow_barrier(conf, bi_sector);
  236. }
  237. }
  238. static void raid_end_bio_io(struct r1bio *r1_bio)
  239. {
  240. struct bio *bio = r1_bio->master_bio;
  241. /* if nobody has done the final endio yet, do it now */
  242. if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
  243. pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
  244. (bio_data_dir(bio) == WRITE) ? "write" : "read",
  245. (unsigned long long) bio->bi_iter.bi_sector,
  246. (unsigned long long) bio_end_sector(bio) - 1);
  247. call_bio_endio(r1_bio);
  248. }
  249. free_r1bio(r1_bio);
  250. }
  251. /*
  252. * Update disk head position estimator based on IRQ completion info.
  253. */
  254. static inline void update_head_pos(int disk, struct r1bio *r1_bio)
  255. {
  256. struct r1conf *conf = r1_bio->mddev->private;
  257. conf->mirrors[disk].head_position =
  258. r1_bio->sector + (r1_bio->sectors);
  259. }
  260. /*
  261. * Find the disk number which triggered given bio
  262. */
  263. static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
  264. {
  265. int mirror;
  266. struct r1conf *conf = r1_bio->mddev->private;
  267. int raid_disks = conf->raid_disks;
  268. for (mirror = 0; mirror < raid_disks * 2; mirror++)
  269. if (r1_bio->bios[mirror] == bio)
  270. break;
  271. BUG_ON(mirror == raid_disks * 2);
  272. update_head_pos(mirror, r1_bio);
  273. return mirror;
  274. }
  275. static void raid1_end_read_request(struct bio *bio)
  276. {
  277. int uptodate = !bio->bi_error;
  278. struct r1bio *r1_bio = bio->bi_private;
  279. struct r1conf *conf = r1_bio->mddev->private;
  280. struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
  281. /*
  282. * this branch is our 'one mirror IO has finished' event handler:
  283. */
  284. update_head_pos(r1_bio->read_disk, r1_bio);
  285. if (uptodate)
  286. set_bit(R1BIO_Uptodate, &r1_bio->state);
  287. else if (test_bit(FailFast, &rdev->flags) &&
  288. test_bit(R1BIO_FailFast, &r1_bio->state))
  289. /* This was a fail-fast read so we definitely
  290. * want to retry */
  291. ;
  292. else {
  293. /* If all other devices have failed, we want to return
  294. * the error upwards rather than fail the last device.
  295. * Here we redefine "uptodate" to mean "Don't want to retry"
  296. */
  297. unsigned long flags;
  298. spin_lock_irqsave(&conf->device_lock, flags);
  299. if (r1_bio->mddev->degraded == conf->raid_disks ||
  300. (r1_bio->mddev->degraded == conf->raid_disks-1 &&
  301. test_bit(In_sync, &rdev->flags)))
  302. uptodate = 1;
  303. spin_unlock_irqrestore(&conf->device_lock, flags);
  304. }
  305. if (uptodate) {
  306. raid_end_bio_io(r1_bio);
  307. rdev_dec_pending(rdev, conf->mddev);
  308. } else {
  309. /*
  310. * oops, read error:
  311. */
  312. char b[BDEVNAME_SIZE];
  313. pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
  314. mdname(conf->mddev),
  315. bdevname(rdev->bdev, b),
  316. (unsigned long long)r1_bio->sector);
  317. set_bit(R1BIO_ReadError, &r1_bio->state);
  318. reschedule_retry(r1_bio);
  319. /* don't drop the reference on read_disk yet */
  320. }
  321. }
  322. static void close_write(struct r1bio *r1_bio)
  323. {
  324. /* it really is the end of this request */
  325. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  326. /* free extra copy of the data pages */
  327. int i = r1_bio->behind_page_count;
  328. while (i--)
  329. safe_put_page(r1_bio->behind_bvecs[i].bv_page);
  330. kfree(r1_bio->behind_bvecs);
  331. r1_bio->behind_bvecs = NULL;
  332. }
  333. /* clear the bitmap if all writes complete successfully */
  334. bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
  335. r1_bio->sectors,
  336. !test_bit(R1BIO_Degraded, &r1_bio->state),
  337. test_bit(R1BIO_BehindIO, &r1_bio->state));
  338. md_write_end(r1_bio->mddev);
  339. }
  340. static void r1_bio_write_done(struct r1bio *r1_bio)
  341. {
  342. if (!atomic_dec_and_test(&r1_bio->remaining))
  343. return;
  344. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  345. reschedule_retry(r1_bio);
  346. else {
  347. close_write(r1_bio);
  348. if (test_bit(R1BIO_MadeGood, &r1_bio->state))
  349. reschedule_retry(r1_bio);
  350. else
  351. raid_end_bio_io(r1_bio);
  352. }
  353. }
  354. static void raid1_end_write_request(struct bio *bio)
  355. {
  356. struct r1bio *r1_bio = bio->bi_private;
  357. int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
  358. struct r1conf *conf = r1_bio->mddev->private;
  359. struct bio *to_put = NULL;
  360. int mirror = find_bio_disk(r1_bio, bio);
  361. struct md_rdev *rdev = conf->mirrors[mirror].rdev;
  362. bool discard_error;
  363. discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
  364. /*
  365. * 'one mirror IO has finished' event handler:
  366. */
  367. if (bio->bi_error && !discard_error) {
  368. set_bit(WriteErrorSeen, &rdev->flags);
  369. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  370. set_bit(MD_RECOVERY_NEEDED, &
  371. conf->mddev->recovery);
  372. if (test_bit(FailFast, &rdev->flags) &&
  373. (bio->bi_opf & MD_FAILFAST) &&
  374. /* We never try FailFast to WriteMostly devices */
  375. !test_bit(WriteMostly, &rdev->flags)) {
  376. md_error(r1_bio->mddev, rdev);
  377. if (!test_bit(Faulty, &rdev->flags))
  378. /* This is the only remaining device,
  379. * We need to retry the write without
  380. * FailFast
  381. */
  382. set_bit(R1BIO_WriteError, &r1_bio->state);
  383. else {
  384. /* Finished with this branch */
  385. r1_bio->bios[mirror] = NULL;
  386. to_put = bio;
  387. }
  388. } else
  389. set_bit(R1BIO_WriteError, &r1_bio->state);
  390. } else {
  391. /*
  392. * Set R1BIO_Uptodate in our master bio, so that we
  393. * will return a good error code for to the higher
  394. * levels even if IO on some other mirrored buffer
  395. * fails.
  396. *
  397. * The 'master' represents the composite IO operation
  398. * to user-side. So if something waits for IO, then it
  399. * will wait for the 'master' bio.
  400. */
  401. sector_t first_bad;
  402. int bad_sectors;
  403. r1_bio->bios[mirror] = NULL;
  404. to_put = bio;
  405. /*
  406. * Do not set R1BIO_Uptodate if the current device is
  407. * rebuilding or Faulty. This is because we cannot use
  408. * such device for properly reading the data back (we could
  409. * potentially use it, if the current write would have felt
  410. * before rdev->recovery_offset, but for simplicity we don't
  411. * check this here.
  412. */
  413. if (test_bit(In_sync, &rdev->flags) &&
  414. !test_bit(Faulty, &rdev->flags))
  415. set_bit(R1BIO_Uptodate, &r1_bio->state);
  416. /* Maybe we can clear some bad blocks. */
  417. if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
  418. &first_bad, &bad_sectors) && !discard_error) {
  419. r1_bio->bios[mirror] = IO_MADE_GOOD;
  420. set_bit(R1BIO_MadeGood, &r1_bio->state);
  421. }
  422. }
  423. if (behind) {
  424. if (test_bit(WriteMostly, &rdev->flags))
  425. atomic_dec(&r1_bio->behind_remaining);
  426. /*
  427. * In behind mode, we ACK the master bio once the I/O
  428. * has safely reached all non-writemostly
  429. * disks. Setting the Returned bit ensures that this
  430. * gets done only once -- we don't ever want to return
  431. * -EIO here, instead we'll wait
  432. */
  433. if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
  434. test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  435. /* Maybe we can return now */
  436. if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
  437. struct bio *mbio = r1_bio->master_bio;
  438. pr_debug("raid1: behind end write sectors"
  439. " %llu-%llu\n",
  440. (unsigned long long) mbio->bi_iter.bi_sector,
  441. (unsigned long long) bio_end_sector(mbio) - 1);
  442. call_bio_endio(r1_bio);
  443. }
  444. }
  445. }
  446. if (r1_bio->bios[mirror] == NULL)
  447. rdev_dec_pending(rdev, conf->mddev);
  448. /*
  449. * Let's see if all mirrored write operations have finished
  450. * already.
  451. */
  452. r1_bio_write_done(r1_bio);
  453. if (to_put)
  454. bio_put(to_put);
  455. }
  456. static sector_t align_to_barrier_unit_end(sector_t start_sector,
  457. sector_t sectors)
  458. {
  459. sector_t len;
  460. WARN_ON(sectors == 0);
  461. /*
  462. * len is the number of sectors from start_sector to end of the
  463. * barrier unit which start_sector belongs to.
  464. */
  465. len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
  466. start_sector;
  467. if (len > sectors)
  468. len = sectors;
  469. return len;
  470. }
  471. /*
  472. * This routine returns the disk from which the requested read should
  473. * be done. There is a per-array 'next expected sequential IO' sector
  474. * number - if this matches on the next IO then we use the last disk.
  475. * There is also a per-disk 'last know head position' sector that is
  476. * maintained from IRQ contexts, both the normal and the resync IO
  477. * completion handlers update this position correctly. If there is no
  478. * perfect sequential match then we pick the disk whose head is closest.
  479. *
  480. * If there are 2 mirrors in the same 2 devices, performance degrades
  481. * because position is mirror, not device based.
  482. *
  483. * The rdev for the device selected will have nr_pending incremented.
  484. */
  485. static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
  486. {
  487. const sector_t this_sector = r1_bio->sector;
  488. int sectors;
  489. int best_good_sectors;
  490. int best_disk, best_dist_disk, best_pending_disk;
  491. int has_nonrot_disk;
  492. int disk;
  493. sector_t best_dist;
  494. unsigned int min_pending;
  495. struct md_rdev *rdev;
  496. int choose_first;
  497. int choose_next_idle;
  498. rcu_read_lock();
  499. /*
  500. * Check if we can balance. We can balance on the whole
  501. * device if no resync is going on, or below the resync window.
  502. * We take the first readable disk when above the resync window.
  503. */
  504. retry:
  505. sectors = r1_bio->sectors;
  506. best_disk = -1;
  507. best_dist_disk = -1;
  508. best_dist = MaxSector;
  509. best_pending_disk = -1;
  510. min_pending = UINT_MAX;
  511. best_good_sectors = 0;
  512. has_nonrot_disk = 0;
  513. choose_next_idle = 0;
  514. clear_bit(R1BIO_FailFast, &r1_bio->state);
  515. if ((conf->mddev->recovery_cp < this_sector + sectors) ||
  516. (mddev_is_clustered(conf->mddev) &&
  517. md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
  518. this_sector + sectors)))
  519. choose_first = 1;
  520. else
  521. choose_first = 0;
  522. for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
  523. sector_t dist;
  524. sector_t first_bad;
  525. int bad_sectors;
  526. unsigned int pending;
  527. bool nonrot;
  528. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  529. if (r1_bio->bios[disk] == IO_BLOCKED
  530. || rdev == NULL
  531. || test_bit(Faulty, &rdev->flags))
  532. continue;
  533. if (!test_bit(In_sync, &rdev->flags) &&
  534. rdev->recovery_offset < this_sector + sectors)
  535. continue;
  536. if (test_bit(WriteMostly, &rdev->flags)) {
  537. /* Don't balance among write-mostly, just
  538. * use the first as a last resort */
  539. if (best_dist_disk < 0) {
  540. if (is_badblock(rdev, this_sector, sectors,
  541. &first_bad, &bad_sectors)) {
  542. if (first_bad <= this_sector)
  543. /* Cannot use this */
  544. continue;
  545. best_good_sectors = first_bad - this_sector;
  546. } else
  547. best_good_sectors = sectors;
  548. best_dist_disk = disk;
  549. best_pending_disk = disk;
  550. }
  551. continue;
  552. }
  553. /* This is a reasonable device to use. It might
  554. * even be best.
  555. */
  556. if (is_badblock(rdev, this_sector, sectors,
  557. &first_bad, &bad_sectors)) {
  558. if (best_dist < MaxSector)
  559. /* already have a better device */
  560. continue;
  561. if (first_bad <= this_sector) {
  562. /* cannot read here. If this is the 'primary'
  563. * device, then we must not read beyond
  564. * bad_sectors from another device..
  565. */
  566. bad_sectors -= (this_sector - first_bad);
  567. if (choose_first && sectors > bad_sectors)
  568. sectors = bad_sectors;
  569. if (best_good_sectors > sectors)
  570. best_good_sectors = sectors;
  571. } else {
  572. sector_t good_sectors = first_bad - this_sector;
  573. if (good_sectors > best_good_sectors) {
  574. best_good_sectors = good_sectors;
  575. best_disk = disk;
  576. }
  577. if (choose_first)
  578. break;
  579. }
  580. continue;
  581. } else
  582. best_good_sectors = sectors;
  583. if (best_disk >= 0)
  584. /* At least two disks to choose from so failfast is OK */
  585. set_bit(R1BIO_FailFast, &r1_bio->state);
  586. nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
  587. has_nonrot_disk |= nonrot;
  588. pending = atomic_read(&rdev->nr_pending);
  589. dist = abs(this_sector - conf->mirrors[disk].head_position);
  590. if (choose_first) {
  591. best_disk = disk;
  592. break;
  593. }
  594. /* Don't change to another disk for sequential reads */
  595. if (conf->mirrors[disk].next_seq_sect == this_sector
  596. || dist == 0) {
  597. int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
  598. struct raid1_info *mirror = &conf->mirrors[disk];
  599. best_disk = disk;
  600. /*
  601. * If buffered sequential IO size exceeds optimal
  602. * iosize, check if there is idle disk. If yes, choose
  603. * the idle disk. read_balance could already choose an
  604. * idle disk before noticing it's a sequential IO in
  605. * this disk. This doesn't matter because this disk
  606. * will idle, next time it will be utilized after the
  607. * first disk has IO size exceeds optimal iosize. In
  608. * this way, iosize of the first disk will be optimal
  609. * iosize at least. iosize of the second disk might be
  610. * small, but not a big deal since when the second disk
  611. * starts IO, the first disk is likely still busy.
  612. */
  613. if (nonrot && opt_iosize > 0 &&
  614. mirror->seq_start != MaxSector &&
  615. mirror->next_seq_sect > opt_iosize &&
  616. mirror->next_seq_sect - opt_iosize >=
  617. mirror->seq_start) {
  618. choose_next_idle = 1;
  619. continue;
  620. }
  621. break;
  622. }
  623. if (choose_next_idle)
  624. continue;
  625. if (min_pending > pending) {
  626. min_pending = pending;
  627. best_pending_disk = disk;
  628. }
  629. if (dist < best_dist) {
  630. best_dist = dist;
  631. best_dist_disk = disk;
  632. }
  633. }
  634. /*
  635. * If all disks are rotational, choose the closest disk. If any disk is
  636. * non-rotational, choose the disk with less pending request even the
  637. * disk is rotational, which might/might not be optimal for raids with
  638. * mixed ratation/non-rotational disks depending on workload.
  639. */
  640. if (best_disk == -1) {
  641. if (has_nonrot_disk || min_pending == 0)
  642. best_disk = best_pending_disk;
  643. else
  644. best_disk = best_dist_disk;
  645. }
  646. if (best_disk >= 0) {
  647. rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
  648. if (!rdev)
  649. goto retry;
  650. atomic_inc(&rdev->nr_pending);
  651. sectors = best_good_sectors;
  652. if (conf->mirrors[best_disk].next_seq_sect != this_sector)
  653. conf->mirrors[best_disk].seq_start = this_sector;
  654. conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
  655. }
  656. rcu_read_unlock();
  657. *max_sectors = sectors;
  658. return best_disk;
  659. }
  660. static int raid1_congested(struct mddev *mddev, int bits)
  661. {
  662. struct r1conf *conf = mddev->private;
  663. int i, ret = 0;
  664. if ((bits & (1 << WB_async_congested)) &&
  665. conf->pending_count >= max_queued_requests)
  666. return 1;
  667. rcu_read_lock();
  668. for (i = 0; i < conf->raid_disks * 2; i++) {
  669. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  670. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  671. struct request_queue *q = bdev_get_queue(rdev->bdev);
  672. BUG_ON(!q);
  673. /* Note the '|| 1' - when read_balance prefers
  674. * non-congested targets, it can be removed
  675. */
  676. if ((bits & (1 << WB_async_congested)) || 1)
  677. ret |= bdi_congested(q->backing_dev_info, bits);
  678. else
  679. ret &= bdi_congested(q->backing_dev_info, bits);
  680. }
  681. }
  682. rcu_read_unlock();
  683. return ret;
  684. }
  685. static void flush_pending_writes(struct r1conf *conf)
  686. {
  687. /* Any writes that have been queued but are awaiting
  688. * bitmap updates get flushed here.
  689. */
  690. spin_lock_irq(&conf->device_lock);
  691. if (conf->pending_bio_list.head) {
  692. struct bio *bio;
  693. bio = bio_list_get(&conf->pending_bio_list);
  694. conf->pending_count = 0;
  695. spin_unlock_irq(&conf->device_lock);
  696. /* flush any pending bitmap writes to
  697. * disk before proceeding w/ I/O */
  698. bitmap_unplug(conf->mddev->bitmap);
  699. wake_up(&conf->wait_barrier);
  700. while (bio) { /* submit pending writes */
  701. struct bio *next = bio->bi_next;
  702. struct md_rdev *rdev = (void*)bio->bi_bdev;
  703. bio->bi_next = NULL;
  704. bio->bi_bdev = rdev->bdev;
  705. if (test_bit(Faulty, &rdev->flags)) {
  706. bio->bi_error = -EIO;
  707. bio_endio(bio);
  708. } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
  709. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  710. /* Just ignore it */
  711. bio_endio(bio);
  712. else
  713. generic_make_request(bio);
  714. bio = next;
  715. }
  716. } else
  717. spin_unlock_irq(&conf->device_lock);
  718. }
  719. /* Barriers....
  720. * Sometimes we need to suspend IO while we do something else,
  721. * either some resync/recovery, or reconfigure the array.
  722. * To do this we raise a 'barrier'.
  723. * The 'barrier' is a counter that can be raised multiple times
  724. * to count how many activities are happening which preclude
  725. * normal IO.
  726. * We can only raise the barrier if there is no pending IO.
  727. * i.e. if nr_pending == 0.
  728. * We choose only to raise the barrier if no-one is waiting for the
  729. * barrier to go down. This means that as soon as an IO request
  730. * is ready, no other operations which require a barrier will start
  731. * until the IO request has had a chance.
  732. *
  733. * So: regular IO calls 'wait_barrier'. When that returns there
  734. * is no backgroup IO happening, It must arrange to call
  735. * allow_barrier when it has finished its IO.
  736. * backgroup IO calls must call raise_barrier. Once that returns
  737. * there is no normal IO happeing. It must arrange to call
  738. * lower_barrier when the particular background IO completes.
  739. */
  740. static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
  741. {
  742. int idx = sector_to_idx(sector_nr);
  743. spin_lock_irq(&conf->resync_lock);
  744. /* Wait until no block IO is waiting */
  745. wait_event_lock_irq(conf->wait_barrier,
  746. !atomic_read(&conf->nr_waiting[idx]),
  747. conf->resync_lock);
  748. /* block any new IO from starting */
  749. atomic_inc(&conf->barrier[idx]);
  750. /*
  751. * In raise_barrier() we firstly increase conf->barrier[idx] then
  752. * check conf->nr_pending[idx]. In _wait_barrier() we firstly
  753. * increase conf->nr_pending[idx] then check conf->barrier[idx].
  754. * A memory barrier here to make sure conf->nr_pending[idx] won't
  755. * be fetched before conf->barrier[idx] is increased. Otherwise
  756. * there will be a race between raise_barrier() and _wait_barrier().
  757. */
  758. smp_mb__after_atomic();
  759. /* For these conditions we must wait:
  760. * A: while the array is in frozen state
  761. * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
  762. * existing in corresponding I/O barrier bucket.
  763. * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
  764. * max resync count which allowed on current I/O barrier bucket.
  765. */
  766. wait_event_lock_irq(conf->wait_barrier,
  767. !conf->array_frozen &&
  768. !atomic_read(&conf->nr_pending[idx]) &&
  769. atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH,
  770. conf->resync_lock);
  771. atomic_inc(&conf->nr_pending[idx]);
  772. spin_unlock_irq(&conf->resync_lock);
  773. }
  774. static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
  775. {
  776. int idx = sector_to_idx(sector_nr);
  777. BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
  778. atomic_dec(&conf->barrier[idx]);
  779. atomic_dec(&conf->nr_pending[idx]);
  780. wake_up(&conf->wait_barrier);
  781. }
  782. static void _wait_barrier(struct r1conf *conf, int idx)
  783. {
  784. /*
  785. * We need to increase conf->nr_pending[idx] very early here,
  786. * then raise_barrier() can be blocked when it waits for
  787. * conf->nr_pending[idx] to be 0. Then we can avoid holding
  788. * conf->resync_lock when there is no barrier raised in same
  789. * barrier unit bucket. Also if the array is frozen, I/O
  790. * should be blocked until array is unfrozen.
  791. */
  792. atomic_inc(&conf->nr_pending[idx]);
  793. /*
  794. * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
  795. * check conf->barrier[idx]. In raise_barrier() we firstly increase
  796. * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
  797. * barrier is necessary here to make sure conf->barrier[idx] won't be
  798. * fetched before conf->nr_pending[idx] is increased. Otherwise there
  799. * will be a race between _wait_barrier() and raise_barrier().
  800. */
  801. smp_mb__after_atomic();
  802. /*
  803. * Don't worry about checking two atomic_t variables at same time
  804. * here. If during we check conf->barrier[idx], the array is
  805. * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
  806. * 0, it is safe to return and make the I/O continue. Because the
  807. * array is frozen, all I/O returned here will eventually complete
  808. * or be queued, no race will happen. See code comment in
  809. * frozen_array().
  810. */
  811. if (!READ_ONCE(conf->array_frozen) &&
  812. !atomic_read(&conf->barrier[idx]))
  813. return;
  814. /*
  815. * After holding conf->resync_lock, conf->nr_pending[idx]
  816. * should be decreased before waiting for barrier to drop.
  817. * Otherwise, we may encounter a race condition because
  818. * raise_barrer() might be waiting for conf->nr_pending[idx]
  819. * to be 0 at same time.
  820. */
  821. spin_lock_irq(&conf->resync_lock);
  822. atomic_inc(&conf->nr_waiting[idx]);
  823. atomic_dec(&conf->nr_pending[idx]);
  824. /*
  825. * In case freeze_array() is waiting for
  826. * get_unqueued_pending() == extra
  827. */
  828. wake_up(&conf->wait_barrier);
  829. /* Wait for the barrier in same barrier unit bucket to drop. */
  830. wait_event_lock_irq(conf->wait_barrier,
  831. !conf->array_frozen &&
  832. !atomic_read(&conf->barrier[idx]),
  833. conf->resync_lock);
  834. atomic_inc(&conf->nr_pending[idx]);
  835. atomic_dec(&conf->nr_waiting[idx]);
  836. spin_unlock_irq(&conf->resync_lock);
  837. }
  838. static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
  839. {
  840. int idx = sector_to_idx(sector_nr);
  841. /*
  842. * Very similar to _wait_barrier(). The difference is, for read
  843. * I/O we don't need wait for sync I/O, but if the whole array
  844. * is frozen, the read I/O still has to wait until the array is
  845. * unfrozen. Since there is no ordering requirement with
  846. * conf->barrier[idx] here, memory barrier is unnecessary as well.
  847. */
  848. atomic_inc(&conf->nr_pending[idx]);
  849. if (!READ_ONCE(conf->array_frozen))
  850. return;
  851. spin_lock_irq(&conf->resync_lock);
  852. atomic_inc(&conf->nr_waiting[idx]);
  853. atomic_dec(&conf->nr_pending[idx]);
  854. /*
  855. * In case freeze_array() is waiting for
  856. * get_unqueued_pending() == extra
  857. */
  858. wake_up(&conf->wait_barrier);
  859. /* Wait for array to be unfrozen */
  860. wait_event_lock_irq(conf->wait_barrier,
  861. !conf->array_frozen,
  862. conf->resync_lock);
  863. atomic_inc(&conf->nr_pending[idx]);
  864. atomic_dec(&conf->nr_waiting[idx]);
  865. spin_unlock_irq(&conf->resync_lock);
  866. }
  867. static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
  868. {
  869. int idx = sector_to_idx(sector_nr);
  870. _wait_barrier(conf, idx);
  871. }
  872. static void wait_all_barriers(struct r1conf *conf)
  873. {
  874. int idx;
  875. for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
  876. _wait_barrier(conf, idx);
  877. }
  878. static void _allow_barrier(struct r1conf *conf, int idx)
  879. {
  880. atomic_dec(&conf->nr_pending[idx]);
  881. wake_up(&conf->wait_barrier);
  882. }
  883. static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
  884. {
  885. int idx = sector_to_idx(sector_nr);
  886. _allow_barrier(conf, idx);
  887. }
  888. static void allow_all_barriers(struct r1conf *conf)
  889. {
  890. int idx;
  891. for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
  892. _allow_barrier(conf, idx);
  893. }
  894. /* conf->resync_lock should be held */
  895. static int get_unqueued_pending(struct r1conf *conf)
  896. {
  897. int idx, ret;
  898. for (ret = 0, idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
  899. ret += atomic_read(&conf->nr_pending[idx]) -
  900. atomic_read(&conf->nr_queued[idx]);
  901. return ret;
  902. }
  903. static void freeze_array(struct r1conf *conf, int extra)
  904. {
  905. /* Stop sync I/O and normal I/O and wait for everything to
  906. * go quite.
  907. * This is called in two situations:
  908. * 1) management command handlers (reshape, remove disk, quiesce).
  909. * 2) one normal I/O request failed.
  910. * After array_frozen is set to 1, new sync IO will be blocked at
  911. * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
  912. * or wait_read_barrier(). The flying I/Os will either complete or be
  913. * queued. When everything goes quite, there are only queued I/Os left.
  914. * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
  915. * barrier bucket index which this I/O request hits. When all sync and
  916. * normal I/O are queued, sum of all conf->nr_pending[] will match sum
  917. * of all conf->nr_queued[]. But normal I/O failure is an exception,
  918. * in handle_read_error(), we may call freeze_array() before trying to
  919. * fix the read error. In this case, the error read I/O is not queued,
  920. * so get_unqueued_pending() == 1.
  921. *
  922. * Therefore before this function returns, we need to wait until
  923. * get_unqueued_pendings(conf) gets equal to extra. For
  924. * normal I/O context, extra is 1, in rested situations extra is 0.
  925. */
  926. spin_lock_irq(&conf->resync_lock);
  927. conf->array_frozen = 1;
  928. raid1_log(conf->mddev, "wait freeze");
  929. wait_event_lock_irq_cmd(
  930. conf->wait_barrier,
  931. get_unqueued_pending(conf) == extra,
  932. conf->resync_lock,
  933. flush_pending_writes(conf));
  934. spin_unlock_irq(&conf->resync_lock);
  935. }
  936. static void unfreeze_array(struct r1conf *conf)
  937. {
  938. /* reverse the effect of the freeze */
  939. spin_lock_irq(&conf->resync_lock);
  940. conf->array_frozen = 0;
  941. spin_unlock_irq(&conf->resync_lock);
  942. wake_up(&conf->wait_barrier);
  943. }
  944. /* duplicate the data pages for behind I/O
  945. */
  946. static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
  947. {
  948. int i;
  949. struct bio_vec *bvec;
  950. struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
  951. GFP_NOIO);
  952. if (unlikely(!bvecs))
  953. return;
  954. bio_for_each_segment_all(bvec, bio, i) {
  955. bvecs[i] = *bvec;
  956. bvecs[i].bv_page = alloc_page(GFP_NOIO);
  957. if (unlikely(!bvecs[i].bv_page))
  958. goto do_sync_io;
  959. memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
  960. kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
  961. kunmap(bvecs[i].bv_page);
  962. kunmap(bvec->bv_page);
  963. }
  964. r1_bio->behind_bvecs = bvecs;
  965. r1_bio->behind_page_count = bio->bi_vcnt;
  966. set_bit(R1BIO_BehindIO, &r1_bio->state);
  967. return;
  968. do_sync_io:
  969. for (i = 0; i < bio->bi_vcnt; i++)
  970. if (bvecs[i].bv_page)
  971. put_page(bvecs[i].bv_page);
  972. kfree(bvecs);
  973. pr_debug("%dB behind alloc failed, doing sync I/O\n",
  974. bio->bi_iter.bi_size);
  975. }
  976. struct raid1_plug_cb {
  977. struct blk_plug_cb cb;
  978. struct bio_list pending;
  979. int pending_cnt;
  980. };
  981. static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
  982. {
  983. struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
  984. cb);
  985. struct mddev *mddev = plug->cb.data;
  986. struct r1conf *conf = mddev->private;
  987. struct bio *bio;
  988. if (from_schedule || current->bio_list) {
  989. spin_lock_irq(&conf->device_lock);
  990. bio_list_merge(&conf->pending_bio_list, &plug->pending);
  991. conf->pending_count += plug->pending_cnt;
  992. spin_unlock_irq(&conf->device_lock);
  993. wake_up(&conf->wait_barrier);
  994. md_wakeup_thread(mddev->thread);
  995. kfree(plug);
  996. return;
  997. }
  998. /* we aren't scheduling, so we can do the write-out directly. */
  999. bio = bio_list_get(&plug->pending);
  1000. bitmap_unplug(mddev->bitmap);
  1001. wake_up(&conf->wait_barrier);
  1002. while (bio) { /* submit pending writes */
  1003. struct bio *next = bio->bi_next;
  1004. struct md_rdev *rdev = (void*)bio->bi_bdev;
  1005. bio->bi_next = NULL;
  1006. bio->bi_bdev = rdev->bdev;
  1007. if (test_bit(Faulty, &rdev->flags)) {
  1008. bio->bi_error = -EIO;
  1009. bio_endio(bio);
  1010. } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
  1011. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  1012. /* Just ignore it */
  1013. bio_endio(bio);
  1014. else
  1015. generic_make_request(bio);
  1016. bio = next;
  1017. }
  1018. kfree(plug);
  1019. }
  1020. static inline struct r1bio *
  1021. alloc_r1bio(struct mddev *mddev, struct bio *bio, sector_t sectors_handled)
  1022. {
  1023. struct r1conf *conf = mddev->private;
  1024. struct r1bio *r1_bio;
  1025. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1026. r1_bio->master_bio = bio;
  1027. r1_bio->sectors = bio_sectors(bio) - sectors_handled;
  1028. r1_bio->state = 0;
  1029. r1_bio->mddev = mddev;
  1030. r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
  1031. return r1_bio;
  1032. }
  1033. static void raid1_read_request(struct mddev *mddev, struct bio *bio)
  1034. {
  1035. struct r1conf *conf = mddev->private;
  1036. struct raid1_info *mirror;
  1037. struct r1bio *r1_bio;
  1038. struct bio *read_bio;
  1039. struct bitmap *bitmap = mddev->bitmap;
  1040. const int op = bio_op(bio);
  1041. const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
  1042. int sectors_handled;
  1043. int max_sectors;
  1044. int rdisk;
  1045. /*
  1046. * Still need barrier for READ in case that whole
  1047. * array is frozen.
  1048. */
  1049. wait_read_barrier(conf, bio->bi_iter.bi_sector);
  1050. r1_bio = alloc_r1bio(mddev, bio, 0);
  1051. /*
  1052. * We might need to issue multiple reads to different
  1053. * devices if there are bad blocks around, so we keep
  1054. * track of the number of reads in bio->bi_phys_segments.
  1055. * If this is 0, there is only one r1_bio and no locking
  1056. * will be needed when requests complete. If it is
  1057. * non-zero, then it is the number of not-completed requests.
  1058. */
  1059. bio->bi_phys_segments = 0;
  1060. bio_clear_flag(bio, BIO_SEG_VALID);
  1061. /*
  1062. * make_request() can abort the operation when read-ahead is being
  1063. * used and no empty request is available.
  1064. */
  1065. read_again:
  1066. rdisk = read_balance(conf, r1_bio, &max_sectors);
  1067. if (rdisk < 0) {
  1068. /* couldn't find anywhere to read from */
  1069. raid_end_bio_io(r1_bio);
  1070. return;
  1071. }
  1072. mirror = conf->mirrors + rdisk;
  1073. if (test_bit(WriteMostly, &mirror->rdev->flags) &&
  1074. bitmap) {
  1075. /*
  1076. * Reading from a write-mostly device must take care not to
  1077. * over-take any writes that are 'behind'
  1078. */
  1079. raid1_log(mddev, "wait behind writes");
  1080. wait_event(bitmap->behind_wait,
  1081. atomic_read(&bitmap->behind_writes) == 0);
  1082. }
  1083. r1_bio->read_disk = rdisk;
  1084. read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
  1085. bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
  1086. max_sectors);
  1087. r1_bio->bios[rdisk] = read_bio;
  1088. read_bio->bi_iter.bi_sector = r1_bio->sector +
  1089. mirror->rdev->data_offset;
  1090. read_bio->bi_bdev = mirror->rdev->bdev;
  1091. read_bio->bi_end_io = raid1_end_read_request;
  1092. bio_set_op_attrs(read_bio, op, do_sync);
  1093. if (test_bit(FailFast, &mirror->rdev->flags) &&
  1094. test_bit(R1BIO_FailFast, &r1_bio->state))
  1095. read_bio->bi_opf |= MD_FAILFAST;
  1096. read_bio->bi_private = r1_bio;
  1097. if (mddev->gendisk)
  1098. trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
  1099. read_bio, disk_devt(mddev->gendisk),
  1100. r1_bio->sector);
  1101. if (max_sectors < r1_bio->sectors) {
  1102. /*
  1103. * could not read all from this device, so we will need another
  1104. * r1_bio.
  1105. */
  1106. sectors_handled = (r1_bio->sector + max_sectors
  1107. - bio->bi_iter.bi_sector);
  1108. r1_bio->sectors = max_sectors;
  1109. spin_lock_irq(&conf->device_lock);
  1110. if (bio->bi_phys_segments == 0)
  1111. bio->bi_phys_segments = 2;
  1112. else
  1113. bio->bi_phys_segments++;
  1114. spin_unlock_irq(&conf->device_lock);
  1115. /*
  1116. * Cannot call generic_make_request directly as that will be
  1117. * queued in __make_request and subsequent mempool_alloc might
  1118. * block waiting for it. So hand bio over to raid1d.
  1119. */
  1120. reschedule_retry(r1_bio);
  1121. r1_bio = alloc_r1bio(mddev, bio, sectors_handled);
  1122. goto read_again;
  1123. } else
  1124. generic_make_request(read_bio);
  1125. }
  1126. static void raid1_write_request(struct mddev *mddev, struct bio *bio)
  1127. {
  1128. struct r1conf *conf = mddev->private;
  1129. struct r1bio *r1_bio;
  1130. int i, disks;
  1131. struct bitmap *bitmap = mddev->bitmap;
  1132. unsigned long flags;
  1133. struct md_rdev *blocked_rdev;
  1134. struct blk_plug_cb *cb;
  1135. struct raid1_plug_cb *plug = NULL;
  1136. int first_clone;
  1137. int sectors_handled;
  1138. int max_sectors;
  1139. /*
  1140. * Register the new request and wait if the reconstruction
  1141. * thread has put up a bar for new requests.
  1142. * Continue immediately if no resync is active currently.
  1143. */
  1144. md_write_start(mddev, bio); /* wait on superblock update early */
  1145. if ((bio_end_sector(bio) > mddev->suspend_lo &&
  1146. bio->bi_iter.bi_sector < mddev->suspend_hi) ||
  1147. (mddev_is_clustered(mddev) &&
  1148. md_cluster_ops->area_resyncing(mddev, WRITE,
  1149. bio->bi_iter.bi_sector, bio_end_sector(bio)))) {
  1150. /*
  1151. * As the suspend_* range is controlled by userspace, we want
  1152. * an interruptible wait.
  1153. */
  1154. DEFINE_WAIT(w);
  1155. for (;;) {
  1156. flush_signals(current);
  1157. prepare_to_wait(&conf->wait_barrier,
  1158. &w, TASK_INTERRUPTIBLE);
  1159. if (bio_end_sector(bio) <= mddev->suspend_lo ||
  1160. bio->bi_iter.bi_sector >= mddev->suspend_hi ||
  1161. (mddev_is_clustered(mddev) &&
  1162. !md_cluster_ops->area_resyncing(mddev, WRITE,
  1163. bio->bi_iter.bi_sector,
  1164. bio_end_sector(bio))))
  1165. break;
  1166. schedule();
  1167. }
  1168. finish_wait(&conf->wait_barrier, &w);
  1169. }
  1170. wait_barrier(conf, bio->bi_iter.bi_sector);
  1171. r1_bio = alloc_r1bio(mddev, bio, 0);
  1172. /* We might need to issue multiple writes to different
  1173. * devices if there are bad blocks around, so we keep
  1174. * track of the number of writes in bio->bi_phys_segments.
  1175. * If this is 0, there is only one r1_bio and no locking
  1176. * will be needed when requests complete. If it is
  1177. * non-zero, then it is the number of not-completed requests.
  1178. */
  1179. bio->bi_phys_segments = 0;
  1180. bio_clear_flag(bio, BIO_SEG_VALID);
  1181. if (conf->pending_count >= max_queued_requests) {
  1182. md_wakeup_thread(mddev->thread);
  1183. raid1_log(mddev, "wait queued");
  1184. wait_event(conf->wait_barrier,
  1185. conf->pending_count < max_queued_requests);
  1186. }
  1187. /* first select target devices under rcu_lock and
  1188. * inc refcount on their rdev. Record them by setting
  1189. * bios[x] to bio
  1190. * If there are known/acknowledged bad blocks on any device on
  1191. * which we have seen a write error, we want to avoid writing those
  1192. * blocks.
  1193. * This potentially requires several writes to write around
  1194. * the bad blocks. Each set of writes gets it's own r1bio
  1195. * with a set of bios attached.
  1196. */
  1197. disks = conf->raid_disks * 2;
  1198. retry_write:
  1199. blocked_rdev = NULL;
  1200. rcu_read_lock();
  1201. max_sectors = r1_bio->sectors;
  1202. for (i = 0; i < disks; i++) {
  1203. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1204. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1205. atomic_inc(&rdev->nr_pending);
  1206. blocked_rdev = rdev;
  1207. break;
  1208. }
  1209. r1_bio->bios[i] = NULL;
  1210. if (!rdev || test_bit(Faulty, &rdev->flags)) {
  1211. if (i < conf->raid_disks)
  1212. set_bit(R1BIO_Degraded, &r1_bio->state);
  1213. continue;
  1214. }
  1215. atomic_inc(&rdev->nr_pending);
  1216. if (test_bit(WriteErrorSeen, &rdev->flags)) {
  1217. sector_t first_bad;
  1218. int bad_sectors;
  1219. int is_bad;
  1220. is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
  1221. &first_bad, &bad_sectors);
  1222. if (is_bad < 0) {
  1223. /* mustn't write here until the bad block is
  1224. * acknowledged*/
  1225. set_bit(BlockedBadBlocks, &rdev->flags);
  1226. blocked_rdev = rdev;
  1227. break;
  1228. }
  1229. if (is_bad && first_bad <= r1_bio->sector) {
  1230. /* Cannot write here at all */
  1231. bad_sectors -= (r1_bio->sector - first_bad);
  1232. if (bad_sectors < max_sectors)
  1233. /* mustn't write more than bad_sectors
  1234. * to other devices yet
  1235. */
  1236. max_sectors = bad_sectors;
  1237. rdev_dec_pending(rdev, mddev);
  1238. /* We don't set R1BIO_Degraded as that
  1239. * only applies if the disk is
  1240. * missing, so it might be re-added,
  1241. * and we want to know to recover this
  1242. * chunk.
  1243. * In this case the device is here,
  1244. * and the fact that this chunk is not
  1245. * in-sync is recorded in the bad
  1246. * block log
  1247. */
  1248. continue;
  1249. }
  1250. if (is_bad) {
  1251. int good_sectors = first_bad - r1_bio->sector;
  1252. if (good_sectors < max_sectors)
  1253. max_sectors = good_sectors;
  1254. }
  1255. }
  1256. r1_bio->bios[i] = bio;
  1257. }
  1258. rcu_read_unlock();
  1259. if (unlikely(blocked_rdev)) {
  1260. /* Wait for this device to become unblocked */
  1261. int j;
  1262. for (j = 0; j < i; j++)
  1263. if (r1_bio->bios[j])
  1264. rdev_dec_pending(conf->mirrors[j].rdev, mddev);
  1265. r1_bio->state = 0;
  1266. allow_barrier(conf, bio->bi_iter.bi_sector);
  1267. raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
  1268. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1269. wait_barrier(conf, bio->bi_iter.bi_sector);
  1270. goto retry_write;
  1271. }
  1272. if (max_sectors < r1_bio->sectors) {
  1273. /* We are splitting this write into multiple parts, so
  1274. * we need to prepare for allocating another r1_bio.
  1275. */
  1276. r1_bio->sectors = max_sectors;
  1277. spin_lock_irq(&conf->device_lock);
  1278. if (bio->bi_phys_segments == 0)
  1279. bio->bi_phys_segments = 2;
  1280. else
  1281. bio->bi_phys_segments++;
  1282. spin_unlock_irq(&conf->device_lock);
  1283. }
  1284. sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
  1285. atomic_set(&r1_bio->remaining, 1);
  1286. atomic_set(&r1_bio->behind_remaining, 0);
  1287. first_clone = 1;
  1288. for (i = 0; i < disks; i++) {
  1289. struct bio *mbio = NULL;
  1290. sector_t offset;
  1291. if (!r1_bio->bios[i])
  1292. continue;
  1293. offset = r1_bio->sector - bio->bi_iter.bi_sector;
  1294. if (first_clone) {
  1295. /* do behind I/O ?
  1296. * Not if there are too many, or cannot
  1297. * allocate memory, or a reader on WriteMostly
  1298. * is waiting for behind writes to flush */
  1299. if (bitmap &&
  1300. (atomic_read(&bitmap->behind_writes)
  1301. < mddev->bitmap_info.max_write_behind) &&
  1302. !waitqueue_active(&bitmap->behind_wait)) {
  1303. mbio = bio_clone_bioset_partial(bio, GFP_NOIO,
  1304. mddev->bio_set,
  1305. offset << 9,
  1306. max_sectors << 9);
  1307. alloc_behind_pages(mbio, r1_bio);
  1308. }
  1309. bitmap_startwrite(bitmap, r1_bio->sector,
  1310. r1_bio->sectors,
  1311. test_bit(R1BIO_BehindIO,
  1312. &r1_bio->state));
  1313. first_clone = 0;
  1314. }
  1315. if (!mbio) {
  1316. if (r1_bio->behind_bvecs)
  1317. mbio = bio_clone_bioset_partial(bio, GFP_NOIO,
  1318. mddev->bio_set,
  1319. offset << 9,
  1320. max_sectors << 9);
  1321. else {
  1322. mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
  1323. bio_trim(mbio, offset, max_sectors);
  1324. }
  1325. }
  1326. if (r1_bio->behind_bvecs) {
  1327. struct bio_vec *bvec;
  1328. int j;
  1329. /*
  1330. * We trimmed the bio, so _all is legit
  1331. */
  1332. bio_for_each_segment_all(bvec, mbio, j)
  1333. bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
  1334. if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
  1335. atomic_inc(&r1_bio->behind_remaining);
  1336. }
  1337. r1_bio->bios[i] = mbio;
  1338. mbio->bi_iter.bi_sector = (r1_bio->sector +
  1339. conf->mirrors[i].rdev->data_offset);
  1340. mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1341. mbio->bi_end_io = raid1_end_write_request;
  1342. mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA));
  1343. if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
  1344. !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
  1345. conf->raid_disks - mddev->degraded > 1)
  1346. mbio->bi_opf |= MD_FAILFAST;
  1347. mbio->bi_private = r1_bio;
  1348. atomic_inc(&r1_bio->remaining);
  1349. if (mddev->gendisk)
  1350. trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
  1351. mbio, disk_devt(mddev->gendisk),
  1352. r1_bio->sector);
  1353. /* flush_pending_writes() needs access to the rdev so...*/
  1354. mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
  1355. cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
  1356. if (cb)
  1357. plug = container_of(cb, struct raid1_plug_cb, cb);
  1358. else
  1359. plug = NULL;
  1360. spin_lock_irqsave(&conf->device_lock, flags);
  1361. if (plug) {
  1362. bio_list_add(&plug->pending, mbio);
  1363. plug->pending_cnt++;
  1364. } else {
  1365. bio_list_add(&conf->pending_bio_list, mbio);
  1366. conf->pending_count++;
  1367. }
  1368. spin_unlock_irqrestore(&conf->device_lock, flags);
  1369. if (!plug)
  1370. md_wakeup_thread(mddev->thread);
  1371. }
  1372. /* Mustn't call r1_bio_write_done before this next test,
  1373. * as it could result in the bio being freed.
  1374. */
  1375. if (sectors_handled < bio_sectors(bio)) {
  1376. r1_bio_write_done(r1_bio);
  1377. /* We need another r1_bio. It has already been counted
  1378. * in bio->bi_phys_segments
  1379. */
  1380. r1_bio = alloc_r1bio(mddev, bio, sectors_handled);
  1381. goto retry_write;
  1382. }
  1383. r1_bio_write_done(r1_bio);
  1384. /* In case raid1d snuck in to freeze_array */
  1385. wake_up(&conf->wait_barrier);
  1386. }
  1387. static void raid1_make_request(struct mddev *mddev, struct bio *bio)
  1388. {
  1389. struct bio *split;
  1390. sector_t sectors;
  1391. if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
  1392. md_flush_request(mddev, bio);
  1393. return;
  1394. }
  1395. /* if bio exceeds barrier unit boundary, split it */
  1396. do {
  1397. sectors = align_to_barrier_unit_end(
  1398. bio->bi_iter.bi_sector, bio_sectors(bio));
  1399. if (sectors < bio_sectors(bio)) {
  1400. split = bio_split(bio, sectors, GFP_NOIO, fs_bio_set);
  1401. bio_chain(split, bio);
  1402. } else {
  1403. split = bio;
  1404. }
  1405. if (bio_data_dir(split) == READ)
  1406. raid1_read_request(mddev, split);
  1407. else
  1408. raid1_write_request(mddev, split);
  1409. } while (split != bio);
  1410. }
  1411. static void raid1_status(struct seq_file *seq, struct mddev *mddev)
  1412. {
  1413. struct r1conf *conf = mddev->private;
  1414. int i;
  1415. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  1416. conf->raid_disks - mddev->degraded);
  1417. rcu_read_lock();
  1418. for (i = 0; i < conf->raid_disks; i++) {
  1419. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1420. seq_printf(seq, "%s",
  1421. rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  1422. }
  1423. rcu_read_unlock();
  1424. seq_printf(seq, "]");
  1425. }
  1426. static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
  1427. {
  1428. char b[BDEVNAME_SIZE];
  1429. struct r1conf *conf = mddev->private;
  1430. unsigned long flags;
  1431. /*
  1432. * If it is not operational, then we have already marked it as dead
  1433. * else if it is the last working disks, ignore the error, let the
  1434. * next level up know.
  1435. * else mark the drive as failed
  1436. */
  1437. spin_lock_irqsave(&conf->device_lock, flags);
  1438. if (test_bit(In_sync, &rdev->flags)
  1439. && (conf->raid_disks - mddev->degraded) == 1) {
  1440. /*
  1441. * Don't fail the drive, act as though we were just a
  1442. * normal single drive.
  1443. * However don't try a recovery from this drive as
  1444. * it is very likely to fail.
  1445. */
  1446. conf->recovery_disabled = mddev->recovery_disabled;
  1447. spin_unlock_irqrestore(&conf->device_lock, flags);
  1448. return;
  1449. }
  1450. set_bit(Blocked, &rdev->flags);
  1451. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1452. mddev->degraded++;
  1453. set_bit(Faulty, &rdev->flags);
  1454. } else
  1455. set_bit(Faulty, &rdev->flags);
  1456. spin_unlock_irqrestore(&conf->device_lock, flags);
  1457. /*
  1458. * if recovery is running, make sure it aborts.
  1459. */
  1460. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1461. set_mask_bits(&mddev->sb_flags, 0,
  1462. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  1463. pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
  1464. "md/raid1:%s: Operation continuing on %d devices.\n",
  1465. mdname(mddev), bdevname(rdev->bdev, b),
  1466. mdname(mddev), conf->raid_disks - mddev->degraded);
  1467. }
  1468. static void print_conf(struct r1conf *conf)
  1469. {
  1470. int i;
  1471. pr_debug("RAID1 conf printout:\n");
  1472. if (!conf) {
  1473. pr_debug("(!conf)\n");
  1474. return;
  1475. }
  1476. pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  1477. conf->raid_disks);
  1478. rcu_read_lock();
  1479. for (i = 0; i < conf->raid_disks; i++) {
  1480. char b[BDEVNAME_SIZE];
  1481. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1482. if (rdev)
  1483. pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
  1484. i, !test_bit(In_sync, &rdev->flags),
  1485. !test_bit(Faulty, &rdev->flags),
  1486. bdevname(rdev->bdev,b));
  1487. }
  1488. rcu_read_unlock();
  1489. }
  1490. static void close_sync(struct r1conf *conf)
  1491. {
  1492. wait_all_barriers(conf);
  1493. allow_all_barriers(conf);
  1494. mempool_destroy(conf->r1buf_pool);
  1495. conf->r1buf_pool = NULL;
  1496. }
  1497. static int raid1_spare_active(struct mddev *mddev)
  1498. {
  1499. int i;
  1500. struct r1conf *conf = mddev->private;
  1501. int count = 0;
  1502. unsigned long flags;
  1503. /*
  1504. * Find all failed disks within the RAID1 configuration
  1505. * and mark them readable.
  1506. * Called under mddev lock, so rcu protection not needed.
  1507. * device_lock used to avoid races with raid1_end_read_request
  1508. * which expects 'In_sync' flags and ->degraded to be consistent.
  1509. */
  1510. spin_lock_irqsave(&conf->device_lock, flags);
  1511. for (i = 0; i < conf->raid_disks; i++) {
  1512. struct md_rdev *rdev = conf->mirrors[i].rdev;
  1513. struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
  1514. if (repl
  1515. && !test_bit(Candidate, &repl->flags)
  1516. && repl->recovery_offset == MaxSector
  1517. && !test_bit(Faulty, &repl->flags)
  1518. && !test_and_set_bit(In_sync, &repl->flags)) {
  1519. /* replacement has just become active */
  1520. if (!rdev ||
  1521. !test_and_clear_bit(In_sync, &rdev->flags))
  1522. count++;
  1523. if (rdev) {
  1524. /* Replaced device not technically
  1525. * faulty, but we need to be sure
  1526. * it gets removed and never re-added
  1527. */
  1528. set_bit(Faulty, &rdev->flags);
  1529. sysfs_notify_dirent_safe(
  1530. rdev->sysfs_state);
  1531. }
  1532. }
  1533. if (rdev
  1534. && rdev->recovery_offset == MaxSector
  1535. && !test_bit(Faulty, &rdev->flags)
  1536. && !test_and_set_bit(In_sync, &rdev->flags)) {
  1537. count++;
  1538. sysfs_notify_dirent_safe(rdev->sysfs_state);
  1539. }
  1540. }
  1541. mddev->degraded -= count;
  1542. spin_unlock_irqrestore(&conf->device_lock, flags);
  1543. print_conf(conf);
  1544. return count;
  1545. }
  1546. static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1547. {
  1548. struct r1conf *conf = mddev->private;
  1549. int err = -EEXIST;
  1550. int mirror = 0;
  1551. struct raid1_info *p;
  1552. int first = 0;
  1553. int last = conf->raid_disks - 1;
  1554. if (mddev->recovery_disabled == conf->recovery_disabled)
  1555. return -EBUSY;
  1556. if (md_integrity_add_rdev(rdev, mddev))
  1557. return -ENXIO;
  1558. if (rdev->raid_disk >= 0)
  1559. first = last = rdev->raid_disk;
  1560. /*
  1561. * find the disk ... but prefer rdev->saved_raid_disk
  1562. * if possible.
  1563. */
  1564. if (rdev->saved_raid_disk >= 0 &&
  1565. rdev->saved_raid_disk >= first &&
  1566. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1567. first = last = rdev->saved_raid_disk;
  1568. for (mirror = first; mirror <= last; mirror++) {
  1569. p = conf->mirrors+mirror;
  1570. if (!p->rdev) {
  1571. if (mddev->gendisk)
  1572. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1573. rdev->data_offset << 9);
  1574. p->head_position = 0;
  1575. rdev->raid_disk = mirror;
  1576. err = 0;
  1577. /* As all devices are equivalent, we don't need a full recovery
  1578. * if this was recently any drive of the array
  1579. */
  1580. if (rdev->saved_raid_disk < 0)
  1581. conf->fullsync = 1;
  1582. rcu_assign_pointer(p->rdev, rdev);
  1583. break;
  1584. }
  1585. if (test_bit(WantReplacement, &p->rdev->flags) &&
  1586. p[conf->raid_disks].rdev == NULL) {
  1587. /* Add this device as a replacement */
  1588. clear_bit(In_sync, &rdev->flags);
  1589. set_bit(Replacement, &rdev->flags);
  1590. rdev->raid_disk = mirror;
  1591. err = 0;
  1592. conf->fullsync = 1;
  1593. rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
  1594. break;
  1595. }
  1596. }
  1597. if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1598. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  1599. print_conf(conf);
  1600. return err;
  1601. }
  1602. static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1603. {
  1604. struct r1conf *conf = mddev->private;
  1605. int err = 0;
  1606. int number = rdev->raid_disk;
  1607. struct raid1_info *p = conf->mirrors + number;
  1608. if (rdev != p->rdev)
  1609. p = conf->mirrors + conf->raid_disks + number;
  1610. print_conf(conf);
  1611. if (rdev == p->rdev) {
  1612. if (test_bit(In_sync, &rdev->flags) ||
  1613. atomic_read(&rdev->nr_pending)) {
  1614. err = -EBUSY;
  1615. goto abort;
  1616. }
  1617. /* Only remove non-faulty devices if recovery
  1618. * is not possible.
  1619. */
  1620. if (!test_bit(Faulty, &rdev->flags) &&
  1621. mddev->recovery_disabled != conf->recovery_disabled &&
  1622. mddev->degraded < conf->raid_disks) {
  1623. err = -EBUSY;
  1624. goto abort;
  1625. }
  1626. p->rdev = NULL;
  1627. if (!test_bit(RemoveSynchronized, &rdev->flags)) {
  1628. synchronize_rcu();
  1629. if (atomic_read(&rdev->nr_pending)) {
  1630. /* lost the race, try later */
  1631. err = -EBUSY;
  1632. p->rdev = rdev;
  1633. goto abort;
  1634. }
  1635. }
  1636. if (conf->mirrors[conf->raid_disks + number].rdev) {
  1637. /* We just removed a device that is being replaced.
  1638. * Move down the replacement. We drain all IO before
  1639. * doing this to avoid confusion.
  1640. */
  1641. struct md_rdev *repl =
  1642. conf->mirrors[conf->raid_disks + number].rdev;
  1643. freeze_array(conf, 0);
  1644. clear_bit(Replacement, &repl->flags);
  1645. p->rdev = repl;
  1646. conf->mirrors[conf->raid_disks + number].rdev = NULL;
  1647. unfreeze_array(conf);
  1648. clear_bit(WantReplacement, &rdev->flags);
  1649. } else
  1650. clear_bit(WantReplacement, &rdev->flags);
  1651. err = md_integrity_register(mddev);
  1652. }
  1653. abort:
  1654. print_conf(conf);
  1655. return err;
  1656. }
  1657. static void end_sync_read(struct bio *bio)
  1658. {
  1659. struct r1bio *r1_bio = bio->bi_private;
  1660. update_head_pos(r1_bio->read_disk, r1_bio);
  1661. /*
  1662. * we have read a block, now it needs to be re-written,
  1663. * or re-read if the read failed.
  1664. * We don't do much here, just schedule handling by raid1d
  1665. */
  1666. if (!bio->bi_error)
  1667. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1668. if (atomic_dec_and_test(&r1_bio->remaining))
  1669. reschedule_retry(r1_bio);
  1670. }
  1671. static void end_sync_write(struct bio *bio)
  1672. {
  1673. int uptodate = !bio->bi_error;
  1674. struct r1bio *r1_bio = bio->bi_private;
  1675. struct mddev *mddev = r1_bio->mddev;
  1676. struct r1conf *conf = mddev->private;
  1677. sector_t first_bad;
  1678. int bad_sectors;
  1679. struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
  1680. if (!uptodate) {
  1681. sector_t sync_blocks = 0;
  1682. sector_t s = r1_bio->sector;
  1683. long sectors_to_go = r1_bio->sectors;
  1684. /* make sure these bits doesn't get cleared. */
  1685. do {
  1686. bitmap_end_sync(mddev->bitmap, s,
  1687. &sync_blocks, 1);
  1688. s += sync_blocks;
  1689. sectors_to_go -= sync_blocks;
  1690. } while (sectors_to_go > 0);
  1691. set_bit(WriteErrorSeen, &rdev->flags);
  1692. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1693. set_bit(MD_RECOVERY_NEEDED, &
  1694. mddev->recovery);
  1695. set_bit(R1BIO_WriteError, &r1_bio->state);
  1696. } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
  1697. &first_bad, &bad_sectors) &&
  1698. !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
  1699. r1_bio->sector,
  1700. r1_bio->sectors,
  1701. &first_bad, &bad_sectors)
  1702. )
  1703. set_bit(R1BIO_MadeGood, &r1_bio->state);
  1704. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1705. int s = r1_bio->sectors;
  1706. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1707. test_bit(R1BIO_WriteError, &r1_bio->state))
  1708. reschedule_retry(r1_bio);
  1709. else {
  1710. put_buf(r1_bio);
  1711. md_done_sync(mddev, s, uptodate);
  1712. }
  1713. }
  1714. }
  1715. static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1716. int sectors, struct page *page, int rw)
  1717. {
  1718. if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
  1719. /* success */
  1720. return 1;
  1721. if (rw == WRITE) {
  1722. set_bit(WriteErrorSeen, &rdev->flags);
  1723. if (!test_and_set_bit(WantReplacement,
  1724. &rdev->flags))
  1725. set_bit(MD_RECOVERY_NEEDED, &
  1726. rdev->mddev->recovery);
  1727. }
  1728. /* need to record an error - either for the block or the device */
  1729. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1730. md_error(rdev->mddev, rdev);
  1731. return 0;
  1732. }
  1733. static int fix_sync_read_error(struct r1bio *r1_bio)
  1734. {
  1735. /* Try some synchronous reads of other devices to get
  1736. * good data, much like with normal read errors. Only
  1737. * read into the pages we already have so we don't
  1738. * need to re-issue the read request.
  1739. * We don't need to freeze the array, because being in an
  1740. * active sync request, there is no normal IO, and
  1741. * no overlapping syncs.
  1742. * We don't need to check is_badblock() again as we
  1743. * made sure that anything with a bad block in range
  1744. * will have bi_end_io clear.
  1745. */
  1746. struct mddev *mddev = r1_bio->mddev;
  1747. struct r1conf *conf = mddev->private;
  1748. struct bio *bio = r1_bio->bios[r1_bio->read_disk];
  1749. sector_t sect = r1_bio->sector;
  1750. int sectors = r1_bio->sectors;
  1751. int idx = 0;
  1752. struct md_rdev *rdev;
  1753. rdev = conf->mirrors[r1_bio->read_disk].rdev;
  1754. if (test_bit(FailFast, &rdev->flags)) {
  1755. /* Don't try recovering from here - just fail it
  1756. * ... unless it is the last working device of course */
  1757. md_error(mddev, rdev);
  1758. if (test_bit(Faulty, &rdev->flags))
  1759. /* Don't try to read from here, but make sure
  1760. * put_buf does it's thing
  1761. */
  1762. bio->bi_end_io = end_sync_write;
  1763. }
  1764. while(sectors) {
  1765. int s = sectors;
  1766. int d = r1_bio->read_disk;
  1767. int success = 0;
  1768. int start;
  1769. if (s > (PAGE_SIZE>>9))
  1770. s = PAGE_SIZE >> 9;
  1771. do {
  1772. if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
  1773. /* No rcu protection needed here devices
  1774. * can only be removed when no resync is
  1775. * active, and resync is currently active
  1776. */
  1777. rdev = conf->mirrors[d].rdev;
  1778. if (sync_page_io(rdev, sect, s<<9,
  1779. bio->bi_io_vec[idx].bv_page,
  1780. REQ_OP_READ, 0, false)) {
  1781. success = 1;
  1782. break;
  1783. }
  1784. }
  1785. d++;
  1786. if (d == conf->raid_disks * 2)
  1787. d = 0;
  1788. } while (!success && d != r1_bio->read_disk);
  1789. if (!success) {
  1790. char b[BDEVNAME_SIZE];
  1791. int abort = 0;
  1792. /* Cannot read from anywhere, this block is lost.
  1793. * Record a bad block on each device. If that doesn't
  1794. * work just disable and interrupt the recovery.
  1795. * Don't fail devices as that won't really help.
  1796. */
  1797. pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
  1798. mdname(mddev),
  1799. bdevname(bio->bi_bdev, b),
  1800. (unsigned long long)r1_bio->sector);
  1801. for (d = 0; d < conf->raid_disks * 2; d++) {
  1802. rdev = conf->mirrors[d].rdev;
  1803. if (!rdev || test_bit(Faulty, &rdev->flags))
  1804. continue;
  1805. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1806. abort = 1;
  1807. }
  1808. if (abort) {
  1809. conf->recovery_disabled =
  1810. mddev->recovery_disabled;
  1811. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1812. md_done_sync(mddev, r1_bio->sectors, 0);
  1813. put_buf(r1_bio);
  1814. return 0;
  1815. }
  1816. /* Try next page */
  1817. sectors -= s;
  1818. sect += s;
  1819. idx++;
  1820. continue;
  1821. }
  1822. start = d;
  1823. /* write it back and re-read */
  1824. while (d != r1_bio->read_disk) {
  1825. if (d == 0)
  1826. d = conf->raid_disks * 2;
  1827. d--;
  1828. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1829. continue;
  1830. rdev = conf->mirrors[d].rdev;
  1831. if (r1_sync_page_io(rdev, sect, s,
  1832. bio->bi_io_vec[idx].bv_page,
  1833. WRITE) == 0) {
  1834. r1_bio->bios[d]->bi_end_io = NULL;
  1835. rdev_dec_pending(rdev, mddev);
  1836. }
  1837. }
  1838. d = start;
  1839. while (d != r1_bio->read_disk) {
  1840. if (d == 0)
  1841. d = conf->raid_disks * 2;
  1842. d--;
  1843. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1844. continue;
  1845. rdev = conf->mirrors[d].rdev;
  1846. if (r1_sync_page_io(rdev, sect, s,
  1847. bio->bi_io_vec[idx].bv_page,
  1848. READ) != 0)
  1849. atomic_add(s, &rdev->corrected_errors);
  1850. }
  1851. sectors -= s;
  1852. sect += s;
  1853. idx ++;
  1854. }
  1855. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1856. bio->bi_error = 0;
  1857. return 1;
  1858. }
  1859. static void process_checks(struct r1bio *r1_bio)
  1860. {
  1861. /* We have read all readable devices. If we haven't
  1862. * got the block, then there is no hope left.
  1863. * If we have, then we want to do a comparison
  1864. * and skip the write if everything is the same.
  1865. * If any blocks failed to read, then we need to
  1866. * attempt an over-write
  1867. */
  1868. struct mddev *mddev = r1_bio->mddev;
  1869. struct r1conf *conf = mddev->private;
  1870. int primary;
  1871. int i;
  1872. int vcnt;
  1873. /* Fix variable parts of all bios */
  1874. vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
  1875. for (i = 0; i < conf->raid_disks * 2; i++) {
  1876. int j;
  1877. int size;
  1878. int error;
  1879. struct bio *b = r1_bio->bios[i];
  1880. if (b->bi_end_io != end_sync_read)
  1881. continue;
  1882. /* fixup the bio for reuse, but preserve errno */
  1883. error = b->bi_error;
  1884. bio_reset(b);
  1885. b->bi_error = error;
  1886. b->bi_vcnt = vcnt;
  1887. b->bi_iter.bi_size = r1_bio->sectors << 9;
  1888. b->bi_iter.bi_sector = r1_bio->sector +
  1889. conf->mirrors[i].rdev->data_offset;
  1890. b->bi_bdev = conf->mirrors[i].rdev->bdev;
  1891. b->bi_end_io = end_sync_read;
  1892. b->bi_private = r1_bio;
  1893. size = b->bi_iter.bi_size;
  1894. for (j = 0; j < vcnt ; j++) {
  1895. struct bio_vec *bi;
  1896. bi = &b->bi_io_vec[j];
  1897. bi->bv_offset = 0;
  1898. if (size > PAGE_SIZE)
  1899. bi->bv_len = PAGE_SIZE;
  1900. else
  1901. bi->bv_len = size;
  1902. size -= PAGE_SIZE;
  1903. }
  1904. }
  1905. for (primary = 0; primary < conf->raid_disks * 2; primary++)
  1906. if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
  1907. !r1_bio->bios[primary]->bi_error) {
  1908. r1_bio->bios[primary]->bi_end_io = NULL;
  1909. rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
  1910. break;
  1911. }
  1912. r1_bio->read_disk = primary;
  1913. for (i = 0; i < conf->raid_disks * 2; i++) {
  1914. int j;
  1915. struct bio *pbio = r1_bio->bios[primary];
  1916. struct bio *sbio = r1_bio->bios[i];
  1917. int error = sbio->bi_error;
  1918. if (sbio->bi_end_io != end_sync_read)
  1919. continue;
  1920. /* Now we can 'fixup' the error value */
  1921. sbio->bi_error = 0;
  1922. if (!error) {
  1923. for (j = vcnt; j-- ; ) {
  1924. struct page *p, *s;
  1925. p = pbio->bi_io_vec[j].bv_page;
  1926. s = sbio->bi_io_vec[j].bv_page;
  1927. if (memcmp(page_address(p),
  1928. page_address(s),
  1929. sbio->bi_io_vec[j].bv_len))
  1930. break;
  1931. }
  1932. } else
  1933. j = 0;
  1934. if (j >= 0)
  1935. atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
  1936. if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
  1937. && !error)) {
  1938. /* No need to write to this device. */
  1939. sbio->bi_end_io = NULL;
  1940. rdev_dec_pending(conf->mirrors[i].rdev, mddev);
  1941. continue;
  1942. }
  1943. bio_copy_data(sbio, pbio);
  1944. }
  1945. }
  1946. static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
  1947. {
  1948. struct r1conf *conf = mddev->private;
  1949. int i;
  1950. int disks = conf->raid_disks * 2;
  1951. struct bio *bio, *wbio;
  1952. bio = r1_bio->bios[r1_bio->read_disk];
  1953. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  1954. /* ouch - failed to read all of that. */
  1955. if (!fix_sync_read_error(r1_bio))
  1956. return;
  1957. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1958. process_checks(r1_bio);
  1959. /*
  1960. * schedule writes
  1961. */
  1962. atomic_set(&r1_bio->remaining, 1);
  1963. for (i = 0; i < disks ; i++) {
  1964. wbio = r1_bio->bios[i];
  1965. if (wbio->bi_end_io == NULL ||
  1966. (wbio->bi_end_io == end_sync_read &&
  1967. (i == r1_bio->read_disk ||
  1968. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  1969. continue;
  1970. bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
  1971. if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
  1972. wbio->bi_opf |= MD_FAILFAST;
  1973. wbio->bi_end_io = end_sync_write;
  1974. atomic_inc(&r1_bio->remaining);
  1975. md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
  1976. generic_make_request(wbio);
  1977. }
  1978. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1979. /* if we're here, all write(s) have completed, so clean up */
  1980. int s = r1_bio->sectors;
  1981. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1982. test_bit(R1BIO_WriteError, &r1_bio->state))
  1983. reschedule_retry(r1_bio);
  1984. else {
  1985. put_buf(r1_bio);
  1986. md_done_sync(mddev, s, 1);
  1987. }
  1988. }
  1989. }
  1990. /*
  1991. * This is a kernel thread which:
  1992. *
  1993. * 1. Retries failed read operations on working mirrors.
  1994. * 2. Updates the raid superblock when problems encounter.
  1995. * 3. Performs writes following reads for array synchronising.
  1996. */
  1997. static void fix_read_error(struct r1conf *conf, int read_disk,
  1998. sector_t sect, int sectors)
  1999. {
  2000. struct mddev *mddev = conf->mddev;
  2001. while(sectors) {
  2002. int s = sectors;
  2003. int d = read_disk;
  2004. int success = 0;
  2005. int start;
  2006. struct md_rdev *rdev;
  2007. if (s > (PAGE_SIZE>>9))
  2008. s = PAGE_SIZE >> 9;
  2009. do {
  2010. sector_t first_bad;
  2011. int bad_sectors;
  2012. rcu_read_lock();
  2013. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2014. if (rdev &&
  2015. (test_bit(In_sync, &rdev->flags) ||
  2016. (!test_bit(Faulty, &rdev->flags) &&
  2017. rdev->recovery_offset >= sect + s)) &&
  2018. is_badblock(rdev, sect, s,
  2019. &first_bad, &bad_sectors) == 0) {
  2020. atomic_inc(&rdev->nr_pending);
  2021. rcu_read_unlock();
  2022. if (sync_page_io(rdev, sect, s<<9,
  2023. conf->tmppage, REQ_OP_READ, 0, false))
  2024. success = 1;
  2025. rdev_dec_pending(rdev, mddev);
  2026. if (success)
  2027. break;
  2028. } else
  2029. rcu_read_unlock();
  2030. d++;
  2031. if (d == conf->raid_disks * 2)
  2032. d = 0;
  2033. } while (!success && d != read_disk);
  2034. if (!success) {
  2035. /* Cannot read from anywhere - mark it bad */
  2036. struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
  2037. if (!rdev_set_badblocks(rdev, sect, s, 0))
  2038. md_error(mddev, rdev);
  2039. break;
  2040. }
  2041. /* write it back and re-read */
  2042. start = d;
  2043. while (d != read_disk) {
  2044. if (d==0)
  2045. d = conf->raid_disks * 2;
  2046. d--;
  2047. rcu_read_lock();
  2048. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2049. if (rdev &&
  2050. !test_bit(Faulty, &rdev->flags)) {
  2051. atomic_inc(&rdev->nr_pending);
  2052. rcu_read_unlock();
  2053. r1_sync_page_io(rdev, sect, s,
  2054. conf->tmppage, WRITE);
  2055. rdev_dec_pending(rdev, mddev);
  2056. } else
  2057. rcu_read_unlock();
  2058. }
  2059. d = start;
  2060. while (d != read_disk) {
  2061. char b[BDEVNAME_SIZE];
  2062. if (d==0)
  2063. d = conf->raid_disks * 2;
  2064. d--;
  2065. rcu_read_lock();
  2066. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2067. if (rdev &&
  2068. !test_bit(Faulty, &rdev->flags)) {
  2069. atomic_inc(&rdev->nr_pending);
  2070. rcu_read_unlock();
  2071. if (r1_sync_page_io(rdev, sect, s,
  2072. conf->tmppage, READ)) {
  2073. atomic_add(s, &rdev->corrected_errors);
  2074. pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
  2075. mdname(mddev), s,
  2076. (unsigned long long)(sect +
  2077. rdev->data_offset),
  2078. bdevname(rdev->bdev, b));
  2079. }
  2080. rdev_dec_pending(rdev, mddev);
  2081. } else
  2082. rcu_read_unlock();
  2083. }
  2084. sectors -= s;
  2085. sect += s;
  2086. }
  2087. }
  2088. static int narrow_write_error(struct r1bio *r1_bio, int i)
  2089. {
  2090. struct mddev *mddev = r1_bio->mddev;
  2091. struct r1conf *conf = mddev->private;
  2092. struct md_rdev *rdev = conf->mirrors[i].rdev;
  2093. /* bio has the data to be written to device 'i' where
  2094. * we just recently had a write error.
  2095. * We repeatedly clone the bio and trim down to one block,
  2096. * then try the write. Where the write fails we record
  2097. * a bad block.
  2098. * It is conceivable that the bio doesn't exactly align with
  2099. * blocks. We must handle this somehow.
  2100. *
  2101. * We currently own a reference on the rdev.
  2102. */
  2103. int block_sectors;
  2104. sector_t sector;
  2105. int sectors;
  2106. int sect_to_write = r1_bio->sectors;
  2107. int ok = 1;
  2108. if (rdev->badblocks.shift < 0)
  2109. return 0;
  2110. block_sectors = roundup(1 << rdev->badblocks.shift,
  2111. bdev_logical_block_size(rdev->bdev) >> 9);
  2112. sector = r1_bio->sector;
  2113. sectors = ((sector + block_sectors)
  2114. & ~(sector_t)(block_sectors - 1))
  2115. - sector;
  2116. while (sect_to_write) {
  2117. struct bio *wbio;
  2118. if (sectors > sect_to_write)
  2119. sectors = sect_to_write;
  2120. /* Write at 'sector' for 'sectors'*/
  2121. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  2122. unsigned vcnt = r1_bio->behind_page_count;
  2123. struct bio_vec *vec = r1_bio->behind_bvecs;
  2124. while (!vec->bv_page) {
  2125. vec++;
  2126. vcnt--;
  2127. }
  2128. wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
  2129. memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
  2130. wbio->bi_vcnt = vcnt;
  2131. } else {
  2132. wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
  2133. mddev->bio_set);
  2134. }
  2135. bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
  2136. wbio->bi_iter.bi_sector = r1_bio->sector;
  2137. wbio->bi_iter.bi_size = r1_bio->sectors << 9;
  2138. bio_trim(wbio, sector - r1_bio->sector, sectors);
  2139. wbio->bi_iter.bi_sector += rdev->data_offset;
  2140. wbio->bi_bdev = rdev->bdev;
  2141. if (submit_bio_wait(wbio) < 0)
  2142. /* failure! */
  2143. ok = rdev_set_badblocks(rdev, sector,
  2144. sectors, 0)
  2145. && ok;
  2146. bio_put(wbio);
  2147. sect_to_write -= sectors;
  2148. sector += sectors;
  2149. sectors = block_sectors;
  2150. }
  2151. return ok;
  2152. }
  2153. static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
  2154. {
  2155. int m;
  2156. int s = r1_bio->sectors;
  2157. for (m = 0; m < conf->raid_disks * 2 ; m++) {
  2158. struct md_rdev *rdev = conf->mirrors[m].rdev;
  2159. struct bio *bio = r1_bio->bios[m];
  2160. if (bio->bi_end_io == NULL)
  2161. continue;
  2162. if (!bio->bi_error &&
  2163. test_bit(R1BIO_MadeGood, &r1_bio->state)) {
  2164. rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
  2165. }
  2166. if (bio->bi_error &&
  2167. test_bit(R1BIO_WriteError, &r1_bio->state)) {
  2168. if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
  2169. md_error(conf->mddev, rdev);
  2170. }
  2171. }
  2172. put_buf(r1_bio);
  2173. md_done_sync(conf->mddev, s, 1);
  2174. }
  2175. static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
  2176. {
  2177. int m, idx;
  2178. bool fail = false;
  2179. for (m = 0; m < conf->raid_disks * 2 ; m++)
  2180. if (r1_bio->bios[m] == IO_MADE_GOOD) {
  2181. struct md_rdev *rdev = conf->mirrors[m].rdev;
  2182. rdev_clear_badblocks(rdev,
  2183. r1_bio->sector,
  2184. r1_bio->sectors, 0);
  2185. rdev_dec_pending(rdev, conf->mddev);
  2186. } else if (r1_bio->bios[m] != NULL) {
  2187. /* This drive got a write error. We need to
  2188. * narrow down and record precise write
  2189. * errors.
  2190. */
  2191. fail = true;
  2192. if (!narrow_write_error(r1_bio, m)) {
  2193. md_error(conf->mddev,
  2194. conf->mirrors[m].rdev);
  2195. /* an I/O failed, we can't clear the bitmap */
  2196. set_bit(R1BIO_Degraded, &r1_bio->state);
  2197. }
  2198. rdev_dec_pending(conf->mirrors[m].rdev,
  2199. conf->mddev);
  2200. }
  2201. if (fail) {
  2202. spin_lock_irq(&conf->device_lock);
  2203. list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
  2204. idx = sector_to_idx(r1_bio->sector);
  2205. atomic_inc(&conf->nr_queued[idx]);
  2206. spin_unlock_irq(&conf->device_lock);
  2207. /*
  2208. * In case freeze_array() is waiting for condition
  2209. * get_unqueued_pending() == extra to be true.
  2210. */
  2211. wake_up(&conf->wait_barrier);
  2212. md_wakeup_thread(conf->mddev->thread);
  2213. } else {
  2214. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  2215. close_write(r1_bio);
  2216. raid_end_bio_io(r1_bio);
  2217. }
  2218. }
  2219. static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
  2220. {
  2221. int disk;
  2222. int max_sectors;
  2223. struct mddev *mddev = conf->mddev;
  2224. struct bio *bio;
  2225. char b[BDEVNAME_SIZE];
  2226. struct md_rdev *rdev;
  2227. dev_t bio_dev;
  2228. sector_t bio_sector;
  2229. clear_bit(R1BIO_ReadError, &r1_bio->state);
  2230. /* we got a read error. Maybe the drive is bad. Maybe just
  2231. * the block and we can fix it.
  2232. * We freeze all other IO, and try reading the block from
  2233. * other devices. When we find one, we re-write
  2234. * and check it that fixes the read error.
  2235. * This is all done synchronously while the array is
  2236. * frozen
  2237. */
  2238. bio = r1_bio->bios[r1_bio->read_disk];
  2239. bdevname(bio->bi_bdev, b);
  2240. bio_dev = bio->bi_bdev->bd_dev;
  2241. bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
  2242. bio_put(bio);
  2243. r1_bio->bios[r1_bio->read_disk] = NULL;
  2244. rdev = conf->mirrors[r1_bio->read_disk].rdev;
  2245. if (mddev->ro == 0
  2246. && !test_bit(FailFast, &rdev->flags)) {
  2247. freeze_array(conf, 1);
  2248. fix_read_error(conf, r1_bio->read_disk,
  2249. r1_bio->sector, r1_bio->sectors);
  2250. unfreeze_array(conf);
  2251. } else {
  2252. r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
  2253. }
  2254. rdev_dec_pending(rdev, conf->mddev);
  2255. read_more:
  2256. disk = read_balance(conf, r1_bio, &max_sectors);
  2257. if (disk == -1) {
  2258. pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
  2259. mdname(mddev), b, (unsigned long long)r1_bio->sector);
  2260. raid_end_bio_io(r1_bio);
  2261. } else {
  2262. const unsigned long do_sync
  2263. = r1_bio->master_bio->bi_opf & REQ_SYNC;
  2264. r1_bio->read_disk = disk;
  2265. bio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
  2266. mddev->bio_set);
  2267. bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
  2268. max_sectors);
  2269. r1_bio->bios[r1_bio->read_disk] = bio;
  2270. rdev = conf->mirrors[disk].rdev;
  2271. pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
  2272. mdname(mddev),
  2273. (unsigned long long)r1_bio->sector,
  2274. bdevname(rdev->bdev, b));
  2275. bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
  2276. bio->bi_bdev = rdev->bdev;
  2277. bio->bi_end_io = raid1_end_read_request;
  2278. bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
  2279. if (test_bit(FailFast, &rdev->flags) &&
  2280. test_bit(R1BIO_FailFast, &r1_bio->state))
  2281. bio->bi_opf |= MD_FAILFAST;
  2282. bio->bi_private = r1_bio;
  2283. if (max_sectors < r1_bio->sectors) {
  2284. /* Drat - have to split this up more */
  2285. struct bio *mbio = r1_bio->master_bio;
  2286. int sectors_handled = (r1_bio->sector + max_sectors
  2287. - mbio->bi_iter.bi_sector);
  2288. r1_bio->sectors = max_sectors;
  2289. spin_lock_irq(&conf->device_lock);
  2290. if (mbio->bi_phys_segments == 0)
  2291. mbio->bi_phys_segments = 2;
  2292. else
  2293. mbio->bi_phys_segments++;
  2294. spin_unlock_irq(&conf->device_lock);
  2295. trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
  2296. bio, bio_dev, bio_sector);
  2297. generic_make_request(bio);
  2298. bio = NULL;
  2299. r1_bio = alloc_r1bio(mddev, mbio, sectors_handled);
  2300. set_bit(R1BIO_ReadError, &r1_bio->state);
  2301. goto read_more;
  2302. } else {
  2303. trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
  2304. bio, bio_dev, bio_sector);
  2305. generic_make_request(bio);
  2306. }
  2307. }
  2308. }
  2309. static void raid1d(struct md_thread *thread)
  2310. {
  2311. struct mddev *mddev = thread->mddev;
  2312. struct r1bio *r1_bio;
  2313. unsigned long flags;
  2314. struct r1conf *conf = mddev->private;
  2315. struct list_head *head = &conf->retry_list;
  2316. struct blk_plug plug;
  2317. int idx;
  2318. md_check_recovery(mddev);
  2319. if (!list_empty_careful(&conf->bio_end_io_list) &&
  2320. !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
  2321. LIST_HEAD(tmp);
  2322. spin_lock_irqsave(&conf->device_lock, flags);
  2323. if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
  2324. list_splice_init(&conf->bio_end_io_list, &tmp);
  2325. spin_unlock_irqrestore(&conf->device_lock, flags);
  2326. while (!list_empty(&tmp)) {
  2327. r1_bio = list_first_entry(&tmp, struct r1bio,
  2328. retry_list);
  2329. list_del(&r1_bio->retry_list);
  2330. idx = sector_to_idx(r1_bio->sector);
  2331. atomic_dec(&conf->nr_queued[idx]);
  2332. if (mddev->degraded)
  2333. set_bit(R1BIO_Degraded, &r1_bio->state);
  2334. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  2335. close_write(r1_bio);
  2336. raid_end_bio_io(r1_bio);
  2337. }
  2338. }
  2339. blk_start_plug(&plug);
  2340. for (;;) {
  2341. flush_pending_writes(conf);
  2342. spin_lock_irqsave(&conf->device_lock, flags);
  2343. if (list_empty(head)) {
  2344. spin_unlock_irqrestore(&conf->device_lock, flags);
  2345. break;
  2346. }
  2347. r1_bio = list_entry(head->prev, struct r1bio, retry_list);
  2348. list_del(head->prev);
  2349. idx = sector_to_idx(r1_bio->sector);
  2350. atomic_dec(&conf->nr_queued[idx]);
  2351. spin_unlock_irqrestore(&conf->device_lock, flags);
  2352. mddev = r1_bio->mddev;
  2353. conf = mddev->private;
  2354. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  2355. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  2356. test_bit(R1BIO_WriteError, &r1_bio->state))
  2357. handle_sync_write_finished(conf, r1_bio);
  2358. else
  2359. sync_request_write(mddev, r1_bio);
  2360. } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  2361. test_bit(R1BIO_WriteError, &r1_bio->state))
  2362. handle_write_finished(conf, r1_bio);
  2363. else if (test_bit(R1BIO_ReadError, &r1_bio->state))
  2364. handle_read_error(conf, r1_bio);
  2365. else
  2366. /* just a partial read to be scheduled from separate
  2367. * context
  2368. */
  2369. generic_make_request(r1_bio->bios[r1_bio->read_disk]);
  2370. cond_resched();
  2371. if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
  2372. md_check_recovery(mddev);
  2373. }
  2374. blk_finish_plug(&plug);
  2375. }
  2376. static int init_resync(struct r1conf *conf)
  2377. {
  2378. int buffs;
  2379. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2380. BUG_ON(conf->r1buf_pool);
  2381. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  2382. conf->poolinfo);
  2383. if (!conf->r1buf_pool)
  2384. return -ENOMEM;
  2385. return 0;
  2386. }
  2387. /*
  2388. * perform a "sync" on one "block"
  2389. *
  2390. * We need to make sure that no normal I/O request - particularly write
  2391. * requests - conflict with active sync requests.
  2392. *
  2393. * This is achieved by tracking pending requests and a 'barrier' concept
  2394. * that can be installed to exclude normal IO requests.
  2395. */
  2396. static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
  2397. int *skipped)
  2398. {
  2399. struct r1conf *conf = mddev->private;
  2400. struct r1bio *r1_bio;
  2401. struct bio *bio;
  2402. sector_t max_sector, nr_sectors;
  2403. int disk = -1;
  2404. int i;
  2405. int wonly = -1;
  2406. int write_targets = 0, read_targets = 0;
  2407. sector_t sync_blocks;
  2408. int still_degraded = 0;
  2409. int good_sectors = RESYNC_SECTORS;
  2410. int min_bad = 0; /* number of sectors that are bad in all devices */
  2411. int idx = sector_to_idx(sector_nr);
  2412. if (!conf->r1buf_pool)
  2413. if (init_resync(conf))
  2414. return 0;
  2415. max_sector = mddev->dev_sectors;
  2416. if (sector_nr >= max_sector) {
  2417. /* If we aborted, we need to abort the
  2418. * sync on the 'current' bitmap chunk (there will
  2419. * only be one in raid1 resync.
  2420. * We can find the current addess in mddev->curr_resync
  2421. */
  2422. if (mddev->curr_resync < max_sector) /* aborted */
  2423. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2424. &sync_blocks, 1);
  2425. else /* completed sync */
  2426. conf->fullsync = 0;
  2427. bitmap_close_sync(mddev->bitmap);
  2428. close_sync(conf);
  2429. if (mddev_is_clustered(mddev)) {
  2430. conf->cluster_sync_low = 0;
  2431. conf->cluster_sync_high = 0;
  2432. }
  2433. return 0;
  2434. }
  2435. if (mddev->bitmap == NULL &&
  2436. mddev->recovery_cp == MaxSector &&
  2437. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2438. conf->fullsync == 0) {
  2439. *skipped = 1;
  2440. return max_sector - sector_nr;
  2441. }
  2442. /* before building a request, check if we can skip these blocks..
  2443. * This call the bitmap_start_sync doesn't actually record anything
  2444. */
  2445. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2446. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2447. /* We can skip this block, and probably several more */
  2448. *skipped = 1;
  2449. return sync_blocks;
  2450. }
  2451. /*
  2452. * If there is non-resync activity waiting for a turn, then let it
  2453. * though before starting on this new sync request.
  2454. */
  2455. if (atomic_read(&conf->nr_waiting[idx]))
  2456. schedule_timeout_uninterruptible(1);
  2457. /* we are incrementing sector_nr below. To be safe, we check against
  2458. * sector_nr + two times RESYNC_SECTORS
  2459. */
  2460. bitmap_cond_end_sync(mddev->bitmap, sector_nr,
  2461. mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
  2462. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  2463. raise_barrier(conf, sector_nr);
  2464. rcu_read_lock();
  2465. /*
  2466. * If we get a correctably read error during resync or recovery,
  2467. * we might want to read from a different device. So we
  2468. * flag all drives that could conceivably be read from for READ,
  2469. * and any others (which will be non-In_sync devices) for WRITE.
  2470. * If a read fails, we try reading from something else for which READ
  2471. * is OK.
  2472. */
  2473. r1_bio->mddev = mddev;
  2474. r1_bio->sector = sector_nr;
  2475. r1_bio->state = 0;
  2476. set_bit(R1BIO_IsSync, &r1_bio->state);
  2477. /* make sure good_sectors won't go across barrier unit boundary */
  2478. good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
  2479. for (i = 0; i < conf->raid_disks * 2; i++) {
  2480. struct md_rdev *rdev;
  2481. bio = r1_bio->bios[i];
  2482. bio_reset(bio);
  2483. rdev = rcu_dereference(conf->mirrors[i].rdev);
  2484. if (rdev == NULL ||
  2485. test_bit(Faulty, &rdev->flags)) {
  2486. if (i < conf->raid_disks)
  2487. still_degraded = 1;
  2488. } else if (!test_bit(In_sync, &rdev->flags)) {
  2489. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  2490. bio->bi_end_io = end_sync_write;
  2491. write_targets ++;
  2492. } else {
  2493. /* may need to read from here */
  2494. sector_t first_bad = MaxSector;
  2495. int bad_sectors;
  2496. if (is_badblock(rdev, sector_nr, good_sectors,
  2497. &first_bad, &bad_sectors)) {
  2498. if (first_bad > sector_nr)
  2499. good_sectors = first_bad - sector_nr;
  2500. else {
  2501. bad_sectors -= (sector_nr - first_bad);
  2502. if (min_bad == 0 ||
  2503. min_bad > bad_sectors)
  2504. min_bad = bad_sectors;
  2505. }
  2506. }
  2507. if (sector_nr < first_bad) {
  2508. if (test_bit(WriteMostly, &rdev->flags)) {
  2509. if (wonly < 0)
  2510. wonly = i;
  2511. } else {
  2512. if (disk < 0)
  2513. disk = i;
  2514. }
  2515. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  2516. bio->bi_end_io = end_sync_read;
  2517. read_targets++;
  2518. } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
  2519. test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
  2520. !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
  2521. /*
  2522. * The device is suitable for reading (InSync),
  2523. * but has bad block(s) here. Let's try to correct them,
  2524. * if we are doing resync or repair. Otherwise, leave
  2525. * this device alone for this sync request.
  2526. */
  2527. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  2528. bio->bi_end_io = end_sync_write;
  2529. write_targets++;
  2530. }
  2531. }
  2532. if (bio->bi_end_io) {
  2533. atomic_inc(&rdev->nr_pending);
  2534. bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
  2535. bio->bi_bdev = rdev->bdev;
  2536. bio->bi_private = r1_bio;
  2537. if (test_bit(FailFast, &rdev->flags))
  2538. bio->bi_opf |= MD_FAILFAST;
  2539. }
  2540. }
  2541. rcu_read_unlock();
  2542. if (disk < 0)
  2543. disk = wonly;
  2544. r1_bio->read_disk = disk;
  2545. if (read_targets == 0 && min_bad > 0) {
  2546. /* These sectors are bad on all InSync devices, so we
  2547. * need to mark them bad on all write targets
  2548. */
  2549. int ok = 1;
  2550. for (i = 0 ; i < conf->raid_disks * 2 ; i++)
  2551. if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
  2552. struct md_rdev *rdev = conf->mirrors[i].rdev;
  2553. ok = rdev_set_badblocks(rdev, sector_nr,
  2554. min_bad, 0
  2555. ) && ok;
  2556. }
  2557. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  2558. *skipped = 1;
  2559. put_buf(r1_bio);
  2560. if (!ok) {
  2561. /* Cannot record the badblocks, so need to
  2562. * abort the resync.
  2563. * If there are multiple read targets, could just
  2564. * fail the really bad ones ???
  2565. */
  2566. conf->recovery_disabled = mddev->recovery_disabled;
  2567. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  2568. return 0;
  2569. } else
  2570. return min_bad;
  2571. }
  2572. if (min_bad > 0 && min_bad < good_sectors) {
  2573. /* only resync enough to reach the next bad->good
  2574. * transition */
  2575. good_sectors = min_bad;
  2576. }
  2577. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  2578. /* extra read targets are also write targets */
  2579. write_targets += read_targets-1;
  2580. if (write_targets == 0 || read_targets == 0) {
  2581. /* There is nowhere to write, so all non-sync
  2582. * drives must be failed - so we are finished
  2583. */
  2584. sector_t rv;
  2585. if (min_bad > 0)
  2586. max_sector = sector_nr + min_bad;
  2587. rv = max_sector - sector_nr;
  2588. *skipped = 1;
  2589. put_buf(r1_bio);
  2590. return rv;
  2591. }
  2592. if (max_sector > mddev->resync_max)
  2593. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2594. if (max_sector > sector_nr + good_sectors)
  2595. max_sector = sector_nr + good_sectors;
  2596. nr_sectors = 0;
  2597. sync_blocks = 0;
  2598. do {
  2599. struct page *page;
  2600. int len = PAGE_SIZE;
  2601. if (sector_nr + (len>>9) > max_sector)
  2602. len = (max_sector - sector_nr) << 9;
  2603. if (len == 0)
  2604. break;
  2605. if (sync_blocks == 0) {
  2606. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2607. &sync_blocks, still_degraded) &&
  2608. !conf->fullsync &&
  2609. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  2610. break;
  2611. if ((len >> 9) > sync_blocks)
  2612. len = sync_blocks<<9;
  2613. }
  2614. for (i = 0 ; i < conf->raid_disks * 2; i++) {
  2615. bio = r1_bio->bios[i];
  2616. if (bio->bi_end_io) {
  2617. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2618. if (bio_add_page(bio, page, len, 0) == 0) {
  2619. /* stop here */
  2620. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2621. while (i > 0) {
  2622. i--;
  2623. bio = r1_bio->bios[i];
  2624. if (bio->bi_end_io==NULL)
  2625. continue;
  2626. /* remove last page from this bio */
  2627. bio->bi_vcnt--;
  2628. bio->bi_iter.bi_size -= len;
  2629. bio_clear_flag(bio, BIO_SEG_VALID);
  2630. }
  2631. goto bio_full;
  2632. }
  2633. }
  2634. }
  2635. nr_sectors += len>>9;
  2636. sector_nr += len>>9;
  2637. sync_blocks -= (len>>9);
  2638. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  2639. bio_full:
  2640. r1_bio->sectors = nr_sectors;
  2641. if (mddev_is_clustered(mddev) &&
  2642. conf->cluster_sync_high < sector_nr + nr_sectors) {
  2643. conf->cluster_sync_low = mddev->curr_resync_completed;
  2644. conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
  2645. /* Send resync message */
  2646. md_cluster_ops->resync_info_update(mddev,
  2647. conf->cluster_sync_low,
  2648. conf->cluster_sync_high);
  2649. }
  2650. /* For a user-requested sync, we read all readable devices and do a
  2651. * compare
  2652. */
  2653. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2654. atomic_set(&r1_bio->remaining, read_targets);
  2655. for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
  2656. bio = r1_bio->bios[i];
  2657. if (bio->bi_end_io == end_sync_read) {
  2658. read_targets--;
  2659. md_sync_acct(bio->bi_bdev, nr_sectors);
  2660. if (read_targets == 1)
  2661. bio->bi_opf &= ~MD_FAILFAST;
  2662. generic_make_request(bio);
  2663. }
  2664. }
  2665. } else {
  2666. atomic_set(&r1_bio->remaining, 1);
  2667. bio = r1_bio->bios[r1_bio->read_disk];
  2668. md_sync_acct(bio->bi_bdev, nr_sectors);
  2669. if (read_targets == 1)
  2670. bio->bi_opf &= ~MD_FAILFAST;
  2671. generic_make_request(bio);
  2672. }
  2673. return nr_sectors;
  2674. }
  2675. static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2676. {
  2677. if (sectors)
  2678. return sectors;
  2679. return mddev->dev_sectors;
  2680. }
  2681. static struct r1conf *setup_conf(struct mddev *mddev)
  2682. {
  2683. struct r1conf *conf;
  2684. int i;
  2685. struct raid1_info *disk;
  2686. struct md_rdev *rdev;
  2687. int err = -ENOMEM;
  2688. conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
  2689. if (!conf)
  2690. goto abort;
  2691. conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
  2692. sizeof(atomic_t), GFP_KERNEL);
  2693. if (!conf->nr_pending)
  2694. goto abort;
  2695. conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
  2696. sizeof(atomic_t), GFP_KERNEL);
  2697. if (!conf->nr_waiting)
  2698. goto abort;
  2699. conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
  2700. sizeof(atomic_t), GFP_KERNEL);
  2701. if (!conf->nr_queued)
  2702. goto abort;
  2703. conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
  2704. sizeof(atomic_t), GFP_KERNEL);
  2705. if (!conf->barrier)
  2706. goto abort;
  2707. conf->mirrors = kzalloc(sizeof(struct raid1_info)
  2708. * mddev->raid_disks * 2,
  2709. GFP_KERNEL);
  2710. if (!conf->mirrors)
  2711. goto abort;
  2712. conf->tmppage = alloc_page(GFP_KERNEL);
  2713. if (!conf->tmppage)
  2714. goto abort;
  2715. conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  2716. if (!conf->poolinfo)
  2717. goto abort;
  2718. conf->poolinfo->raid_disks = mddev->raid_disks * 2;
  2719. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2720. r1bio_pool_free,
  2721. conf->poolinfo);
  2722. if (!conf->r1bio_pool)
  2723. goto abort;
  2724. conf->poolinfo->mddev = mddev;
  2725. err = -EINVAL;
  2726. spin_lock_init(&conf->device_lock);
  2727. rdev_for_each(rdev, mddev) {
  2728. struct request_queue *q;
  2729. int disk_idx = rdev->raid_disk;
  2730. if (disk_idx >= mddev->raid_disks
  2731. || disk_idx < 0)
  2732. continue;
  2733. if (test_bit(Replacement, &rdev->flags))
  2734. disk = conf->mirrors + mddev->raid_disks + disk_idx;
  2735. else
  2736. disk = conf->mirrors + disk_idx;
  2737. if (disk->rdev)
  2738. goto abort;
  2739. disk->rdev = rdev;
  2740. q = bdev_get_queue(rdev->bdev);
  2741. disk->head_position = 0;
  2742. disk->seq_start = MaxSector;
  2743. }
  2744. conf->raid_disks = mddev->raid_disks;
  2745. conf->mddev = mddev;
  2746. INIT_LIST_HEAD(&conf->retry_list);
  2747. INIT_LIST_HEAD(&conf->bio_end_io_list);
  2748. spin_lock_init(&conf->resync_lock);
  2749. init_waitqueue_head(&conf->wait_barrier);
  2750. bio_list_init(&conf->pending_bio_list);
  2751. conf->pending_count = 0;
  2752. conf->recovery_disabled = mddev->recovery_disabled - 1;
  2753. err = -EIO;
  2754. for (i = 0; i < conf->raid_disks * 2; i++) {
  2755. disk = conf->mirrors + i;
  2756. if (i < conf->raid_disks &&
  2757. disk[conf->raid_disks].rdev) {
  2758. /* This slot has a replacement. */
  2759. if (!disk->rdev) {
  2760. /* No original, just make the replacement
  2761. * a recovering spare
  2762. */
  2763. disk->rdev =
  2764. disk[conf->raid_disks].rdev;
  2765. disk[conf->raid_disks].rdev = NULL;
  2766. } else if (!test_bit(In_sync, &disk->rdev->flags))
  2767. /* Original is not in_sync - bad */
  2768. goto abort;
  2769. }
  2770. if (!disk->rdev ||
  2771. !test_bit(In_sync, &disk->rdev->flags)) {
  2772. disk->head_position = 0;
  2773. if (disk->rdev &&
  2774. (disk->rdev->saved_raid_disk < 0))
  2775. conf->fullsync = 1;
  2776. }
  2777. }
  2778. err = -ENOMEM;
  2779. conf->thread = md_register_thread(raid1d, mddev, "raid1");
  2780. if (!conf->thread)
  2781. goto abort;
  2782. return conf;
  2783. abort:
  2784. if (conf) {
  2785. mempool_destroy(conf->r1bio_pool);
  2786. kfree(conf->mirrors);
  2787. safe_put_page(conf->tmppage);
  2788. kfree(conf->poolinfo);
  2789. kfree(conf->nr_pending);
  2790. kfree(conf->nr_waiting);
  2791. kfree(conf->nr_queued);
  2792. kfree(conf->barrier);
  2793. kfree(conf);
  2794. }
  2795. return ERR_PTR(err);
  2796. }
  2797. static void raid1_free(struct mddev *mddev, void *priv);
  2798. static int raid1_run(struct mddev *mddev)
  2799. {
  2800. struct r1conf *conf;
  2801. int i;
  2802. struct md_rdev *rdev;
  2803. int ret;
  2804. bool discard_supported = false;
  2805. if (mddev->level != 1) {
  2806. pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
  2807. mdname(mddev), mddev->level);
  2808. return -EIO;
  2809. }
  2810. if (mddev->reshape_position != MaxSector) {
  2811. pr_warn("md/raid1:%s: reshape_position set but not supported\n",
  2812. mdname(mddev));
  2813. return -EIO;
  2814. }
  2815. /*
  2816. * copy the already verified devices into our private RAID1
  2817. * bookkeeping area. [whatever we allocate in run(),
  2818. * should be freed in raid1_free()]
  2819. */
  2820. if (mddev->private == NULL)
  2821. conf = setup_conf(mddev);
  2822. else
  2823. conf = mddev->private;
  2824. if (IS_ERR(conf))
  2825. return PTR_ERR(conf);
  2826. if (mddev->queue)
  2827. blk_queue_max_write_same_sectors(mddev->queue, 0);
  2828. rdev_for_each(rdev, mddev) {
  2829. if (!mddev->gendisk)
  2830. continue;
  2831. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2832. rdev->data_offset << 9);
  2833. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  2834. discard_supported = true;
  2835. }
  2836. mddev->degraded = 0;
  2837. for (i=0; i < conf->raid_disks; i++)
  2838. if (conf->mirrors[i].rdev == NULL ||
  2839. !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
  2840. test_bit(Faulty, &conf->mirrors[i].rdev->flags))
  2841. mddev->degraded++;
  2842. if (conf->raid_disks - mddev->degraded == 1)
  2843. mddev->recovery_cp = MaxSector;
  2844. if (mddev->recovery_cp != MaxSector)
  2845. pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
  2846. mdname(mddev));
  2847. pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
  2848. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2849. mddev->raid_disks);
  2850. /*
  2851. * Ok, everything is just fine now
  2852. */
  2853. mddev->thread = conf->thread;
  2854. conf->thread = NULL;
  2855. mddev->private = conf;
  2856. set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
  2857. md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
  2858. if (mddev->queue) {
  2859. if (discard_supported)
  2860. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  2861. mddev->queue);
  2862. else
  2863. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  2864. mddev->queue);
  2865. }
  2866. ret = md_integrity_register(mddev);
  2867. if (ret) {
  2868. md_unregister_thread(&mddev->thread);
  2869. raid1_free(mddev, conf);
  2870. }
  2871. return ret;
  2872. }
  2873. static void raid1_free(struct mddev *mddev, void *priv)
  2874. {
  2875. struct r1conf *conf = priv;
  2876. mempool_destroy(conf->r1bio_pool);
  2877. kfree(conf->mirrors);
  2878. safe_put_page(conf->tmppage);
  2879. kfree(conf->poolinfo);
  2880. kfree(conf->nr_pending);
  2881. kfree(conf->nr_waiting);
  2882. kfree(conf->nr_queued);
  2883. kfree(conf->barrier);
  2884. kfree(conf);
  2885. }
  2886. static int raid1_resize(struct mddev *mddev, sector_t sectors)
  2887. {
  2888. /* no resync is happening, and there is enough space
  2889. * on all devices, so we can resize.
  2890. * We need to make sure resync covers any new space.
  2891. * If the array is shrinking we should possibly wait until
  2892. * any io in the removed space completes, but it hardly seems
  2893. * worth it.
  2894. */
  2895. sector_t newsize = raid1_size(mddev, sectors, 0);
  2896. if (mddev->external_size &&
  2897. mddev->array_sectors > newsize)
  2898. return -EINVAL;
  2899. if (mddev->bitmap) {
  2900. int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
  2901. if (ret)
  2902. return ret;
  2903. }
  2904. md_set_array_sectors(mddev, newsize);
  2905. set_capacity(mddev->gendisk, mddev->array_sectors);
  2906. revalidate_disk(mddev->gendisk);
  2907. if (sectors > mddev->dev_sectors &&
  2908. mddev->recovery_cp > mddev->dev_sectors) {
  2909. mddev->recovery_cp = mddev->dev_sectors;
  2910. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2911. }
  2912. mddev->dev_sectors = sectors;
  2913. mddev->resync_max_sectors = sectors;
  2914. return 0;
  2915. }
  2916. static int raid1_reshape(struct mddev *mddev)
  2917. {
  2918. /* We need to:
  2919. * 1/ resize the r1bio_pool
  2920. * 2/ resize conf->mirrors
  2921. *
  2922. * We allocate a new r1bio_pool if we can.
  2923. * Then raise a device barrier and wait until all IO stops.
  2924. * Then resize conf->mirrors and swap in the new r1bio pool.
  2925. *
  2926. * At the same time, we "pack" the devices so that all the missing
  2927. * devices have the higher raid_disk numbers.
  2928. */
  2929. mempool_t *newpool, *oldpool;
  2930. struct pool_info *newpoolinfo;
  2931. struct raid1_info *newmirrors;
  2932. struct r1conf *conf = mddev->private;
  2933. int cnt, raid_disks;
  2934. unsigned long flags;
  2935. int d, d2, err;
  2936. /* Cannot change chunk_size, layout, or level */
  2937. if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
  2938. mddev->layout != mddev->new_layout ||
  2939. mddev->level != mddev->new_level) {
  2940. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2941. mddev->new_layout = mddev->layout;
  2942. mddev->new_level = mddev->level;
  2943. return -EINVAL;
  2944. }
  2945. if (!mddev_is_clustered(mddev)) {
  2946. err = md_allow_write(mddev);
  2947. if (err)
  2948. return err;
  2949. }
  2950. raid_disks = mddev->raid_disks + mddev->delta_disks;
  2951. if (raid_disks < conf->raid_disks) {
  2952. cnt=0;
  2953. for (d= 0; d < conf->raid_disks; d++)
  2954. if (conf->mirrors[d].rdev)
  2955. cnt++;
  2956. if (cnt > raid_disks)
  2957. return -EBUSY;
  2958. }
  2959. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  2960. if (!newpoolinfo)
  2961. return -ENOMEM;
  2962. newpoolinfo->mddev = mddev;
  2963. newpoolinfo->raid_disks = raid_disks * 2;
  2964. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2965. r1bio_pool_free, newpoolinfo);
  2966. if (!newpool) {
  2967. kfree(newpoolinfo);
  2968. return -ENOMEM;
  2969. }
  2970. newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
  2971. GFP_KERNEL);
  2972. if (!newmirrors) {
  2973. kfree(newpoolinfo);
  2974. mempool_destroy(newpool);
  2975. return -ENOMEM;
  2976. }
  2977. freeze_array(conf, 0);
  2978. /* ok, everything is stopped */
  2979. oldpool = conf->r1bio_pool;
  2980. conf->r1bio_pool = newpool;
  2981. for (d = d2 = 0; d < conf->raid_disks; d++) {
  2982. struct md_rdev *rdev = conf->mirrors[d].rdev;
  2983. if (rdev && rdev->raid_disk != d2) {
  2984. sysfs_unlink_rdev(mddev, rdev);
  2985. rdev->raid_disk = d2;
  2986. sysfs_unlink_rdev(mddev, rdev);
  2987. if (sysfs_link_rdev(mddev, rdev))
  2988. pr_warn("md/raid1:%s: cannot register rd%d\n",
  2989. mdname(mddev), rdev->raid_disk);
  2990. }
  2991. if (rdev)
  2992. newmirrors[d2++].rdev = rdev;
  2993. }
  2994. kfree(conf->mirrors);
  2995. conf->mirrors = newmirrors;
  2996. kfree(conf->poolinfo);
  2997. conf->poolinfo = newpoolinfo;
  2998. spin_lock_irqsave(&conf->device_lock, flags);
  2999. mddev->degraded += (raid_disks - conf->raid_disks);
  3000. spin_unlock_irqrestore(&conf->device_lock, flags);
  3001. conf->raid_disks = mddev->raid_disks = raid_disks;
  3002. mddev->delta_disks = 0;
  3003. unfreeze_array(conf);
  3004. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  3005. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3006. md_wakeup_thread(mddev->thread);
  3007. mempool_destroy(oldpool);
  3008. return 0;
  3009. }
  3010. static void raid1_quiesce(struct mddev *mddev, int state)
  3011. {
  3012. struct r1conf *conf = mddev->private;
  3013. switch(state) {
  3014. case 2: /* wake for suspend */
  3015. wake_up(&conf->wait_barrier);
  3016. break;
  3017. case 1:
  3018. freeze_array(conf, 0);
  3019. break;
  3020. case 0:
  3021. unfreeze_array(conf);
  3022. break;
  3023. }
  3024. }
  3025. static void *raid1_takeover(struct mddev *mddev)
  3026. {
  3027. /* raid1 can take over:
  3028. * raid5 with 2 devices, any layout or chunk size
  3029. */
  3030. if (mddev->level == 5 && mddev->raid_disks == 2) {
  3031. struct r1conf *conf;
  3032. mddev->new_level = 1;
  3033. mddev->new_layout = 0;
  3034. mddev->new_chunk_sectors = 0;
  3035. conf = setup_conf(mddev);
  3036. if (!IS_ERR(conf)) {
  3037. /* Array must appear to be quiesced */
  3038. conf->array_frozen = 1;
  3039. mddev_clear_unsupported_flags(mddev,
  3040. UNSUPPORTED_MDDEV_FLAGS);
  3041. }
  3042. return conf;
  3043. }
  3044. return ERR_PTR(-EINVAL);
  3045. }
  3046. static struct md_personality raid1_personality =
  3047. {
  3048. .name = "raid1",
  3049. .level = 1,
  3050. .owner = THIS_MODULE,
  3051. .make_request = raid1_make_request,
  3052. .run = raid1_run,
  3053. .free = raid1_free,
  3054. .status = raid1_status,
  3055. .error_handler = raid1_error,
  3056. .hot_add_disk = raid1_add_disk,
  3057. .hot_remove_disk= raid1_remove_disk,
  3058. .spare_active = raid1_spare_active,
  3059. .sync_request = raid1_sync_request,
  3060. .resize = raid1_resize,
  3061. .size = raid1_size,
  3062. .check_reshape = raid1_reshape,
  3063. .quiesce = raid1_quiesce,
  3064. .takeover = raid1_takeover,
  3065. .congested = raid1_congested,
  3066. };
  3067. static int __init raid_init(void)
  3068. {
  3069. return register_md_personality(&raid1_personality);
  3070. }
  3071. static void raid_exit(void)
  3072. {
  3073. unregister_md_personality(&raid1_personality);
  3074. }
  3075. module_init(raid_init);
  3076. module_exit(raid_exit);
  3077. MODULE_LICENSE("GPL");
  3078. MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
  3079. MODULE_ALIAS("md-personality-3"); /* RAID1 */
  3080. MODULE_ALIAS("md-raid1");
  3081. MODULE_ALIAS("md-level-1");
  3082. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);