raid1.c 92 KB

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