raid1.c 87 KB

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