raid1.c 90 KB

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