raid1.c 91 KB

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