raid1.c 87 KB

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