raid1.c 87 KB

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