raid1.c 91 KB

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