raid1.c 90 KB

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