raid1.c 68 KB

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