raid1.c 83 KB

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