raid1.c 84 KB

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