raid1.c 87 KB

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