raid1.c 87 KB

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