raid1.c 92 KB

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