raid1.c 86 KB

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