raid1.c 61 KB

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