raid1.c 87 KB

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