raid1.c 62 KB

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