raid1.c 91 KB

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