raid1.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  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. BUG_ON(conf->barrier <= 0);
  583. spin_lock_irqsave(&conf->resync_lock, flags);
  584. conf->barrier--;
  585. spin_unlock_irqrestore(&conf->resync_lock, flags);
  586. wake_up(&conf->wait_barrier);
  587. }
  588. static void wait_barrier(conf_t *conf)
  589. {
  590. spin_lock_irq(&conf->resync_lock);
  591. if (conf->barrier) {
  592. conf->nr_waiting++;
  593. wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
  594. conf->resync_lock,
  595. raid1_unplug(conf->mddev->queue));
  596. conf->nr_waiting--;
  597. }
  598. conf->nr_pending++;
  599. spin_unlock_irq(&conf->resync_lock);
  600. }
  601. static void allow_barrier(conf_t *conf)
  602. {
  603. unsigned long flags;
  604. spin_lock_irqsave(&conf->resync_lock, flags);
  605. conf->nr_pending--;
  606. spin_unlock_irqrestore(&conf->resync_lock, flags);
  607. wake_up(&conf->wait_barrier);
  608. }
  609. static void freeze_array(conf_t *conf)
  610. {
  611. /* stop syncio and normal IO and wait for everything to
  612. * go quite.
  613. * We increment barrier and nr_waiting, and then
  614. * wait until nr_pending match nr_queued+1
  615. * This is called in the context of one normal IO request
  616. * that has failed. Thus any sync request that might be pending
  617. * will be blocked by nr_pending, and we need to wait for
  618. * pending IO requests to complete or be queued for re-try.
  619. * Thus the number queued (nr_queued) plus this request (1)
  620. * must match the number of pending IOs (nr_pending) before
  621. * we continue.
  622. */
  623. spin_lock_irq(&conf->resync_lock);
  624. conf->barrier++;
  625. conf->nr_waiting++;
  626. wait_event_lock_irq(conf->wait_barrier,
  627. conf->nr_pending == conf->nr_queued+1,
  628. conf->resync_lock,
  629. ({ flush_pending_writes(conf);
  630. raid1_unplug(conf->mddev->queue); }));
  631. spin_unlock_irq(&conf->resync_lock);
  632. }
  633. static void unfreeze_array(conf_t *conf)
  634. {
  635. /* reverse the effect of the freeze */
  636. spin_lock_irq(&conf->resync_lock);
  637. conf->barrier--;
  638. conf->nr_waiting--;
  639. wake_up(&conf->wait_barrier);
  640. spin_unlock_irq(&conf->resync_lock);
  641. }
  642. /* duplicate the data pages for behind I/O */
  643. static struct page **alloc_behind_pages(struct bio *bio)
  644. {
  645. int i;
  646. struct bio_vec *bvec;
  647. struct page **pages = kzalloc(bio->bi_vcnt * sizeof(struct page *),
  648. GFP_NOIO);
  649. if (unlikely(!pages))
  650. goto do_sync_io;
  651. bio_for_each_segment(bvec, bio, i) {
  652. pages[i] = alloc_page(GFP_NOIO);
  653. if (unlikely(!pages[i]))
  654. goto do_sync_io;
  655. memcpy(kmap(pages[i]) + bvec->bv_offset,
  656. kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
  657. kunmap(pages[i]);
  658. kunmap(bvec->bv_page);
  659. }
  660. return pages;
  661. do_sync_io:
  662. if (pages)
  663. for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
  664. put_page(pages[i]);
  665. kfree(pages);
  666. PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
  667. return NULL;
  668. }
  669. static int make_request(struct request_queue *q, struct bio * bio)
  670. {
  671. mddev_t *mddev = q->queuedata;
  672. conf_t *conf = mddev->private;
  673. mirror_info_t *mirror;
  674. r1bio_t *r1_bio;
  675. struct bio *read_bio;
  676. int i, targets = 0, disks;
  677. struct bitmap *bitmap;
  678. unsigned long flags;
  679. struct bio_list bl;
  680. struct page **behind_pages = NULL;
  681. const int rw = bio_data_dir(bio);
  682. const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
  683. int cpu;
  684. bool do_barriers;
  685. mdk_rdev_t *blocked_rdev;
  686. /*
  687. * Register the new request and wait if the reconstruction
  688. * thread has put up a bar for new requests.
  689. * Continue immediately if no resync is active currently.
  690. * We test barriers_work *after* md_write_start as md_write_start
  691. * may cause the first superblock write, and that will check out
  692. * if barriers work.
  693. */
  694. md_write_start(mddev, bio); /* wait on superblock update early */
  695. if (bio_data_dir(bio) == WRITE &&
  696. bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
  697. bio->bi_sector < mddev->suspend_hi) {
  698. /* As the suspend_* range is controlled by
  699. * userspace, we want an interruptible
  700. * wait.
  701. */
  702. DEFINE_WAIT(w);
  703. for (;;) {
  704. flush_signals(current);
  705. prepare_to_wait(&conf->wait_barrier,
  706. &w, TASK_INTERRUPTIBLE);
  707. if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
  708. bio->bi_sector >= mddev->suspend_hi)
  709. break;
  710. schedule();
  711. }
  712. finish_wait(&conf->wait_barrier, &w);
  713. }
  714. if (unlikely(!mddev->barriers_work &&
  715. bio_rw_flagged(bio, BIO_RW_BARRIER))) {
  716. if (rw == WRITE)
  717. md_write_end(mddev);
  718. bio_endio(bio, -EOPNOTSUPP);
  719. return 0;
  720. }
  721. wait_barrier(conf);
  722. bitmap = mddev->bitmap;
  723. cpu = part_stat_lock();
  724. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  725. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  726. bio_sectors(bio));
  727. part_stat_unlock();
  728. /*
  729. * make_request() can abort the operation when READA is being
  730. * used and no empty request is available.
  731. *
  732. */
  733. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  734. r1_bio->master_bio = bio;
  735. r1_bio->sectors = bio->bi_size >> 9;
  736. r1_bio->state = 0;
  737. r1_bio->mddev = mddev;
  738. r1_bio->sector = bio->bi_sector;
  739. if (rw == READ) {
  740. /*
  741. * read balancing logic:
  742. */
  743. int rdisk = read_balance(conf, r1_bio);
  744. if (rdisk < 0) {
  745. /* couldn't find anywhere to read from */
  746. raid_end_bio_io(r1_bio);
  747. return 0;
  748. }
  749. mirror = conf->mirrors + rdisk;
  750. r1_bio->read_disk = rdisk;
  751. read_bio = bio_clone(bio, GFP_NOIO);
  752. r1_bio->bios[rdisk] = read_bio;
  753. read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
  754. read_bio->bi_bdev = mirror->rdev->bdev;
  755. read_bio->bi_end_io = raid1_end_read_request;
  756. read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
  757. read_bio->bi_private = r1_bio;
  758. generic_make_request(read_bio);
  759. return 0;
  760. }
  761. /*
  762. * WRITE:
  763. */
  764. /* first select target devices under spinlock and
  765. * inc refcount on their rdev. Record them by setting
  766. * bios[x] to bio
  767. */
  768. disks = conf->raid_disks;
  769. #if 0
  770. { static int first=1;
  771. if (first) printk("First Write sector %llu disks %d\n",
  772. (unsigned long long)r1_bio->sector, disks);
  773. first = 0;
  774. }
  775. #endif
  776. retry_write:
  777. blocked_rdev = NULL;
  778. rcu_read_lock();
  779. for (i = 0; i < disks; i++) {
  780. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  781. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  782. atomic_inc(&rdev->nr_pending);
  783. blocked_rdev = rdev;
  784. break;
  785. }
  786. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  787. atomic_inc(&rdev->nr_pending);
  788. if (test_bit(Faulty, &rdev->flags)) {
  789. rdev_dec_pending(rdev, mddev);
  790. r1_bio->bios[i] = NULL;
  791. } else
  792. r1_bio->bios[i] = bio;
  793. targets++;
  794. } else
  795. r1_bio->bios[i] = NULL;
  796. }
  797. rcu_read_unlock();
  798. if (unlikely(blocked_rdev)) {
  799. /* Wait for this device to become unblocked */
  800. int j;
  801. for (j = 0; j < i; j++)
  802. if (r1_bio->bios[j])
  803. rdev_dec_pending(conf->mirrors[j].rdev, mddev);
  804. allow_barrier(conf);
  805. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  806. wait_barrier(conf);
  807. goto retry_write;
  808. }
  809. BUG_ON(targets == 0); /* we never fail the last device */
  810. if (targets < conf->raid_disks) {
  811. /* array is degraded, we will not clear the bitmap
  812. * on I/O completion (see raid1_end_write_request) */
  813. set_bit(R1BIO_Degraded, &r1_bio->state);
  814. }
  815. /* do behind I/O ? */
  816. if (bitmap &&
  817. atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
  818. (behind_pages = alloc_behind_pages(bio)) != NULL)
  819. set_bit(R1BIO_BehindIO, &r1_bio->state);
  820. atomic_set(&r1_bio->remaining, 0);
  821. atomic_set(&r1_bio->behind_remaining, 0);
  822. do_barriers = bio_rw_flagged(bio, BIO_RW_BARRIER);
  823. if (do_barriers)
  824. set_bit(R1BIO_Barrier, &r1_bio->state);
  825. bio_list_init(&bl);
  826. for (i = 0; i < disks; i++) {
  827. struct bio *mbio;
  828. if (!r1_bio->bios[i])
  829. continue;
  830. mbio = bio_clone(bio, GFP_NOIO);
  831. r1_bio->bios[i] = mbio;
  832. mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
  833. mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  834. mbio->bi_end_io = raid1_end_write_request;
  835. mbio->bi_rw = WRITE | (do_barriers << BIO_RW_BARRIER) |
  836. (do_sync << BIO_RW_SYNCIO);
  837. mbio->bi_private = r1_bio;
  838. if (behind_pages) {
  839. struct bio_vec *bvec;
  840. int j;
  841. /* Yes, I really want the '__' version so that
  842. * we clear any unused pointer in the io_vec, rather
  843. * than leave them unchanged. This is important
  844. * because when we come to free the pages, we won't
  845. * know the originial bi_idx, so we just free
  846. * them all
  847. */
  848. __bio_for_each_segment(bvec, mbio, j, 0)
  849. bvec->bv_page = behind_pages[j];
  850. if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
  851. atomic_inc(&r1_bio->behind_remaining);
  852. }
  853. atomic_inc(&r1_bio->remaining);
  854. bio_list_add(&bl, mbio);
  855. }
  856. kfree(behind_pages); /* the behind pages are attached to the bios now */
  857. bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
  858. test_bit(R1BIO_BehindIO, &r1_bio->state));
  859. spin_lock_irqsave(&conf->device_lock, flags);
  860. bio_list_merge(&conf->pending_bio_list, &bl);
  861. bio_list_init(&bl);
  862. blk_plug_device(mddev->queue);
  863. spin_unlock_irqrestore(&conf->device_lock, flags);
  864. /* In case raid1d snuck into freeze_array */
  865. wake_up(&conf->wait_barrier);
  866. if (do_sync)
  867. md_wakeup_thread(mddev->thread);
  868. #if 0
  869. while ((bio = bio_list_pop(&bl)) != NULL)
  870. generic_make_request(bio);
  871. #endif
  872. return 0;
  873. }
  874. static void status(struct seq_file *seq, mddev_t *mddev)
  875. {
  876. conf_t *conf = mddev->private;
  877. int i;
  878. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  879. conf->raid_disks - mddev->degraded);
  880. rcu_read_lock();
  881. for (i = 0; i < conf->raid_disks; i++) {
  882. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  883. seq_printf(seq, "%s",
  884. rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  885. }
  886. rcu_read_unlock();
  887. seq_printf(seq, "]");
  888. }
  889. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  890. {
  891. char b[BDEVNAME_SIZE];
  892. conf_t *conf = mddev->private;
  893. /*
  894. * If it is not operational, then we have already marked it as dead
  895. * else if it is the last working disks, ignore the error, let the
  896. * next level up know.
  897. * else mark the drive as failed
  898. */
  899. if (test_bit(In_sync, &rdev->flags)
  900. && (conf->raid_disks - mddev->degraded) == 1) {
  901. /*
  902. * Don't fail the drive, act as though we were just a
  903. * normal single drive.
  904. * However don't try a recovery from this drive as
  905. * it is very likely to fail.
  906. */
  907. mddev->recovery_disabled = 1;
  908. return;
  909. }
  910. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  911. unsigned long flags;
  912. spin_lock_irqsave(&conf->device_lock, flags);
  913. mddev->degraded++;
  914. set_bit(Faulty, &rdev->flags);
  915. spin_unlock_irqrestore(&conf->device_lock, flags);
  916. /*
  917. * if recovery is running, make sure it aborts.
  918. */
  919. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  920. } else
  921. set_bit(Faulty, &rdev->flags);
  922. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  923. printk(KERN_ALERT "raid1: Disk failure on %s, disabling device.\n"
  924. "raid1: Operation continuing on %d devices.\n",
  925. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  926. }
  927. static void print_conf(conf_t *conf)
  928. {
  929. int i;
  930. printk("RAID1 conf printout:\n");
  931. if (!conf) {
  932. printk("(!conf)\n");
  933. return;
  934. }
  935. printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  936. conf->raid_disks);
  937. rcu_read_lock();
  938. for (i = 0; i < conf->raid_disks; i++) {
  939. char b[BDEVNAME_SIZE];
  940. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  941. if (rdev)
  942. printk(" disk %d, wo:%d, o:%d, dev:%s\n",
  943. i, !test_bit(In_sync, &rdev->flags),
  944. !test_bit(Faulty, &rdev->flags),
  945. bdevname(rdev->bdev,b));
  946. }
  947. rcu_read_unlock();
  948. }
  949. static void close_sync(conf_t *conf)
  950. {
  951. wait_barrier(conf);
  952. allow_barrier(conf);
  953. mempool_destroy(conf->r1buf_pool);
  954. conf->r1buf_pool = NULL;
  955. }
  956. static int raid1_spare_active(mddev_t *mddev)
  957. {
  958. int i;
  959. conf_t *conf = mddev->private;
  960. /*
  961. * Find all failed disks within the RAID1 configuration
  962. * and mark them readable.
  963. * Called under mddev lock, so rcu protection not needed.
  964. */
  965. for (i = 0; i < conf->raid_disks; i++) {
  966. mdk_rdev_t *rdev = conf->mirrors[i].rdev;
  967. if (rdev
  968. && !test_bit(Faulty, &rdev->flags)
  969. && !test_and_set_bit(In_sync, &rdev->flags)) {
  970. unsigned long flags;
  971. spin_lock_irqsave(&conf->device_lock, flags);
  972. mddev->degraded--;
  973. spin_unlock_irqrestore(&conf->device_lock, flags);
  974. }
  975. }
  976. print_conf(conf);
  977. return 0;
  978. }
  979. static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  980. {
  981. conf_t *conf = mddev->private;
  982. int err = -EEXIST;
  983. int mirror = 0;
  984. mirror_info_t *p;
  985. int first = 0;
  986. int last = mddev->raid_disks - 1;
  987. if (rdev->raid_disk >= 0)
  988. first = last = rdev->raid_disk;
  989. for (mirror = first; mirror <= last; mirror++)
  990. if ( !(p=conf->mirrors+mirror)->rdev) {
  991. disk_stack_limits(mddev->gendisk, rdev->bdev,
  992. rdev->data_offset << 9);
  993. /* as we don't honour merge_bvec_fn, we must never risk
  994. * violating it, so limit ->max_sector to one PAGE, as
  995. * a one page request is never in violation.
  996. */
  997. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  998. queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
  999. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  1000. p->head_position = 0;
  1001. rdev->raid_disk = mirror;
  1002. err = 0;
  1003. /* As all devices are equivalent, we don't need a full recovery
  1004. * if this was recently any drive of the array
  1005. */
  1006. if (rdev->saved_raid_disk < 0)
  1007. conf->fullsync = 1;
  1008. rcu_assign_pointer(p->rdev, rdev);
  1009. break;
  1010. }
  1011. md_integrity_add_rdev(rdev, mddev);
  1012. print_conf(conf);
  1013. return err;
  1014. }
  1015. static int raid1_remove_disk(mddev_t *mddev, int number)
  1016. {
  1017. conf_t *conf = mddev->private;
  1018. int err = 0;
  1019. mdk_rdev_t *rdev;
  1020. mirror_info_t *p = conf->mirrors+ number;
  1021. print_conf(conf);
  1022. rdev = p->rdev;
  1023. if (rdev) {
  1024. if (test_bit(In_sync, &rdev->flags) ||
  1025. atomic_read(&rdev->nr_pending)) {
  1026. err = -EBUSY;
  1027. goto abort;
  1028. }
  1029. /* Only remove non-faulty devices is recovery
  1030. * is not possible.
  1031. */
  1032. if (!test_bit(Faulty, &rdev->flags) &&
  1033. mddev->degraded < conf->raid_disks) {
  1034. err = -EBUSY;
  1035. goto abort;
  1036. }
  1037. p->rdev = NULL;
  1038. synchronize_rcu();
  1039. if (atomic_read(&rdev->nr_pending)) {
  1040. /* lost the race, try later */
  1041. err = -EBUSY;
  1042. p->rdev = rdev;
  1043. goto abort;
  1044. }
  1045. md_integrity_register(mddev);
  1046. }
  1047. abort:
  1048. print_conf(conf);
  1049. return err;
  1050. }
  1051. static void end_sync_read(struct bio *bio, int error)
  1052. {
  1053. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  1054. int i;
  1055. for (i=r1_bio->mddev->raid_disks; i--; )
  1056. if (r1_bio->bios[i] == bio)
  1057. break;
  1058. BUG_ON(i < 0);
  1059. update_head_pos(i, r1_bio);
  1060. /*
  1061. * we have read a block, now it needs to be re-written,
  1062. * or re-read if the read failed.
  1063. * We don't do much here, just schedule handling by raid1d
  1064. */
  1065. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1066. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1067. if (atomic_dec_and_test(&r1_bio->remaining))
  1068. reschedule_retry(r1_bio);
  1069. }
  1070. static void end_sync_write(struct bio *bio, int error)
  1071. {
  1072. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1073. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  1074. mddev_t *mddev = r1_bio->mddev;
  1075. conf_t *conf = mddev->private;
  1076. int i;
  1077. int mirror=0;
  1078. for (i = 0; i < conf->raid_disks; i++)
  1079. if (r1_bio->bios[i] == bio) {
  1080. mirror = i;
  1081. break;
  1082. }
  1083. if (!uptodate) {
  1084. int sync_blocks = 0;
  1085. sector_t s = r1_bio->sector;
  1086. long sectors_to_go = r1_bio->sectors;
  1087. /* make sure these bits doesn't get cleared. */
  1088. do {
  1089. bitmap_end_sync(mddev->bitmap, s,
  1090. &sync_blocks, 1);
  1091. s += sync_blocks;
  1092. sectors_to_go -= sync_blocks;
  1093. } while (sectors_to_go > 0);
  1094. md_error(mddev, conf->mirrors[mirror].rdev);
  1095. }
  1096. update_head_pos(mirror, r1_bio);
  1097. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1098. sector_t s = r1_bio->sectors;
  1099. put_buf(r1_bio);
  1100. md_done_sync(mddev, s, uptodate);
  1101. }
  1102. }
  1103. static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
  1104. {
  1105. conf_t *conf = mddev->private;
  1106. int i;
  1107. int disks = conf->raid_disks;
  1108. struct bio *bio, *wbio;
  1109. bio = r1_bio->bios[r1_bio->read_disk];
  1110. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1111. /* We have read all readable devices. If we haven't
  1112. * got the block, then there is no hope left.
  1113. * If we have, then we want to do a comparison
  1114. * and skip the write if everything is the same.
  1115. * If any blocks failed to read, then we need to
  1116. * attempt an over-write
  1117. */
  1118. int primary;
  1119. if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  1120. for (i=0; i<mddev->raid_disks; i++)
  1121. if (r1_bio->bios[i]->bi_end_io == end_sync_read)
  1122. md_error(mddev, conf->mirrors[i].rdev);
  1123. md_done_sync(mddev, r1_bio->sectors, 1);
  1124. put_buf(r1_bio);
  1125. return;
  1126. }
  1127. for (primary=0; primary<mddev->raid_disks; primary++)
  1128. if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
  1129. test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
  1130. r1_bio->bios[primary]->bi_end_io = NULL;
  1131. rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
  1132. break;
  1133. }
  1134. r1_bio->read_disk = primary;
  1135. for (i=0; i<mddev->raid_disks; i++)
  1136. if (r1_bio->bios[i]->bi_end_io == end_sync_read) {
  1137. int j;
  1138. int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
  1139. struct bio *pbio = r1_bio->bios[primary];
  1140. struct bio *sbio = r1_bio->bios[i];
  1141. if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
  1142. for (j = vcnt; j-- ; ) {
  1143. struct page *p, *s;
  1144. p = pbio->bi_io_vec[j].bv_page;
  1145. s = sbio->bi_io_vec[j].bv_page;
  1146. if (memcmp(page_address(p),
  1147. page_address(s),
  1148. PAGE_SIZE))
  1149. break;
  1150. }
  1151. } else
  1152. j = 0;
  1153. if (j >= 0)
  1154. mddev->resync_mismatches += r1_bio->sectors;
  1155. if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
  1156. && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
  1157. sbio->bi_end_io = NULL;
  1158. rdev_dec_pending(conf->mirrors[i].rdev, mddev);
  1159. } else {
  1160. /* fixup the bio for reuse */
  1161. int size;
  1162. sbio->bi_vcnt = vcnt;
  1163. sbio->bi_size = r1_bio->sectors << 9;
  1164. sbio->bi_idx = 0;
  1165. sbio->bi_phys_segments = 0;
  1166. sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1167. sbio->bi_flags |= 1 << BIO_UPTODATE;
  1168. sbio->bi_next = NULL;
  1169. sbio->bi_sector = r1_bio->sector +
  1170. conf->mirrors[i].rdev->data_offset;
  1171. sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1172. size = sbio->bi_size;
  1173. for (j = 0; j < vcnt ; j++) {
  1174. struct bio_vec *bi;
  1175. bi = &sbio->bi_io_vec[j];
  1176. bi->bv_offset = 0;
  1177. if (size > PAGE_SIZE)
  1178. bi->bv_len = PAGE_SIZE;
  1179. else
  1180. bi->bv_len = size;
  1181. size -= PAGE_SIZE;
  1182. memcpy(page_address(bi->bv_page),
  1183. page_address(pbio->bi_io_vec[j].bv_page),
  1184. PAGE_SIZE);
  1185. }
  1186. }
  1187. }
  1188. }
  1189. if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  1190. /* ouch - failed to read all of that.
  1191. * Try some synchronous reads of other devices to get
  1192. * good data, much like with normal read errors. Only
  1193. * read into the pages we already have so we don't
  1194. * need to re-issue the read request.
  1195. * We don't need to freeze the array, because being in an
  1196. * active sync request, there is no normal IO, and
  1197. * no overlapping syncs.
  1198. */
  1199. sector_t sect = r1_bio->sector;
  1200. int sectors = r1_bio->sectors;
  1201. int idx = 0;
  1202. while(sectors) {
  1203. int s = sectors;
  1204. int d = r1_bio->read_disk;
  1205. int success = 0;
  1206. mdk_rdev_t *rdev;
  1207. if (s > (PAGE_SIZE>>9))
  1208. s = PAGE_SIZE >> 9;
  1209. do {
  1210. if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
  1211. /* No rcu protection needed here devices
  1212. * can only be removed when no resync is
  1213. * active, and resync is currently active
  1214. */
  1215. rdev = conf->mirrors[d].rdev;
  1216. if (sync_page_io(rdev->bdev,
  1217. sect + rdev->data_offset,
  1218. s<<9,
  1219. bio->bi_io_vec[idx].bv_page,
  1220. READ)) {
  1221. success = 1;
  1222. break;
  1223. }
  1224. }
  1225. d++;
  1226. if (d == conf->raid_disks)
  1227. d = 0;
  1228. } while (!success && d != r1_bio->read_disk);
  1229. if (success) {
  1230. int start = d;
  1231. /* write it back and re-read */
  1232. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1233. while (d != r1_bio->read_disk) {
  1234. if (d == 0)
  1235. d = conf->raid_disks;
  1236. d--;
  1237. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1238. continue;
  1239. rdev = conf->mirrors[d].rdev;
  1240. atomic_add(s, &rdev->corrected_errors);
  1241. if (sync_page_io(rdev->bdev,
  1242. sect + rdev->data_offset,
  1243. s<<9,
  1244. bio->bi_io_vec[idx].bv_page,
  1245. WRITE) == 0)
  1246. md_error(mddev, rdev);
  1247. }
  1248. d = start;
  1249. while (d != r1_bio->read_disk) {
  1250. if (d == 0)
  1251. d = conf->raid_disks;
  1252. d--;
  1253. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1254. continue;
  1255. rdev = conf->mirrors[d].rdev;
  1256. if (sync_page_io(rdev->bdev,
  1257. sect + rdev->data_offset,
  1258. s<<9,
  1259. bio->bi_io_vec[idx].bv_page,
  1260. READ) == 0)
  1261. md_error(mddev, rdev);
  1262. }
  1263. } else {
  1264. char b[BDEVNAME_SIZE];
  1265. /* Cannot read from anywhere, array is toast */
  1266. md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
  1267. printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
  1268. " for block %llu\n",
  1269. bdevname(bio->bi_bdev,b),
  1270. (unsigned long long)r1_bio->sector);
  1271. md_done_sync(mddev, r1_bio->sectors, 0);
  1272. put_buf(r1_bio);
  1273. return;
  1274. }
  1275. sectors -= s;
  1276. sect += s;
  1277. idx ++;
  1278. }
  1279. }
  1280. /*
  1281. * schedule writes
  1282. */
  1283. atomic_set(&r1_bio->remaining, 1);
  1284. for (i = 0; i < disks ; i++) {
  1285. wbio = r1_bio->bios[i];
  1286. if (wbio->bi_end_io == NULL ||
  1287. (wbio->bi_end_io == end_sync_read &&
  1288. (i == r1_bio->read_disk ||
  1289. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  1290. continue;
  1291. wbio->bi_rw = WRITE;
  1292. wbio->bi_end_io = end_sync_write;
  1293. atomic_inc(&r1_bio->remaining);
  1294. md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
  1295. generic_make_request(wbio);
  1296. }
  1297. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1298. /* if we're here, all write(s) have completed, so clean up */
  1299. md_done_sync(mddev, r1_bio->sectors, 1);
  1300. put_buf(r1_bio);
  1301. }
  1302. }
  1303. /*
  1304. * This is a kernel thread which:
  1305. *
  1306. * 1. Retries failed read operations on working mirrors.
  1307. * 2. Updates the raid superblock when problems encounter.
  1308. * 3. Performs writes following reads for array syncronising.
  1309. */
  1310. static void fix_read_error(conf_t *conf, int read_disk,
  1311. sector_t sect, int sectors)
  1312. {
  1313. mddev_t *mddev = conf->mddev;
  1314. while(sectors) {
  1315. int s = sectors;
  1316. int d = read_disk;
  1317. int success = 0;
  1318. int start;
  1319. mdk_rdev_t *rdev;
  1320. if (s > (PAGE_SIZE>>9))
  1321. s = PAGE_SIZE >> 9;
  1322. do {
  1323. /* Note: no rcu protection needed here
  1324. * as this is synchronous in the raid1d thread
  1325. * which is the thread that might remove
  1326. * a device. If raid1d ever becomes multi-threaded....
  1327. */
  1328. rdev = conf->mirrors[d].rdev;
  1329. if (rdev &&
  1330. test_bit(In_sync, &rdev->flags) &&
  1331. sync_page_io(rdev->bdev,
  1332. sect + rdev->data_offset,
  1333. s<<9,
  1334. conf->tmppage, READ))
  1335. success = 1;
  1336. else {
  1337. d++;
  1338. if (d == conf->raid_disks)
  1339. d = 0;
  1340. }
  1341. } while (!success && d != read_disk);
  1342. if (!success) {
  1343. /* Cannot read from anywhere -- bye bye array */
  1344. md_error(mddev, conf->mirrors[read_disk].rdev);
  1345. break;
  1346. }
  1347. /* write it back and re-read */
  1348. start = d;
  1349. while (d != read_disk) {
  1350. if (d==0)
  1351. d = conf->raid_disks;
  1352. d--;
  1353. rdev = conf->mirrors[d].rdev;
  1354. if (rdev &&
  1355. test_bit(In_sync, &rdev->flags)) {
  1356. if (sync_page_io(rdev->bdev,
  1357. sect + rdev->data_offset,
  1358. s<<9, conf->tmppage, WRITE)
  1359. == 0)
  1360. /* Well, this device is dead */
  1361. md_error(mddev, rdev);
  1362. }
  1363. }
  1364. d = start;
  1365. while (d != read_disk) {
  1366. char b[BDEVNAME_SIZE];
  1367. if (d==0)
  1368. d = conf->raid_disks;
  1369. d--;
  1370. rdev = conf->mirrors[d].rdev;
  1371. if (rdev &&
  1372. test_bit(In_sync, &rdev->flags)) {
  1373. if (sync_page_io(rdev->bdev,
  1374. sect + rdev->data_offset,
  1375. s<<9, conf->tmppage, READ)
  1376. == 0)
  1377. /* Well, this device is dead */
  1378. md_error(mddev, rdev);
  1379. else {
  1380. atomic_add(s, &rdev->corrected_errors);
  1381. printk(KERN_INFO
  1382. "raid1:%s: read error corrected "
  1383. "(%d sectors at %llu on %s)\n",
  1384. mdname(mddev), s,
  1385. (unsigned long long)(sect +
  1386. rdev->data_offset),
  1387. bdevname(rdev->bdev, b));
  1388. }
  1389. }
  1390. }
  1391. sectors -= s;
  1392. sect += s;
  1393. }
  1394. }
  1395. static void raid1d(mddev_t *mddev)
  1396. {
  1397. r1bio_t *r1_bio;
  1398. struct bio *bio;
  1399. unsigned long flags;
  1400. conf_t *conf = mddev->private;
  1401. struct list_head *head = &conf->retry_list;
  1402. int unplug=0;
  1403. mdk_rdev_t *rdev;
  1404. md_check_recovery(mddev);
  1405. for (;;) {
  1406. char b[BDEVNAME_SIZE];
  1407. unplug += flush_pending_writes(conf);
  1408. spin_lock_irqsave(&conf->device_lock, flags);
  1409. if (list_empty(head)) {
  1410. spin_unlock_irqrestore(&conf->device_lock, flags);
  1411. break;
  1412. }
  1413. r1_bio = list_entry(head->prev, r1bio_t, retry_list);
  1414. list_del(head->prev);
  1415. conf->nr_queued--;
  1416. spin_unlock_irqrestore(&conf->device_lock, flags);
  1417. mddev = r1_bio->mddev;
  1418. conf = mddev->private;
  1419. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  1420. sync_request_write(mddev, r1_bio);
  1421. unplug = 1;
  1422. } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
  1423. /* some requests in the r1bio were BIO_RW_BARRIER
  1424. * requests which failed with -EOPNOTSUPP. Hohumm..
  1425. * Better resubmit without the barrier.
  1426. * We know which devices to resubmit for, because
  1427. * all others have had their bios[] entry cleared.
  1428. * We already have a nr_pending reference on these rdevs.
  1429. */
  1430. int i;
  1431. const bool do_sync = bio_rw_flagged(r1_bio->master_bio, BIO_RW_SYNCIO);
  1432. clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
  1433. clear_bit(R1BIO_Barrier, &r1_bio->state);
  1434. for (i=0; i < conf->raid_disks; i++)
  1435. if (r1_bio->bios[i])
  1436. atomic_inc(&r1_bio->remaining);
  1437. for (i=0; i < conf->raid_disks; i++)
  1438. if (r1_bio->bios[i]) {
  1439. struct bio_vec *bvec;
  1440. int j;
  1441. bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
  1442. /* copy pages from the failed bio, as
  1443. * this might be a write-behind device */
  1444. __bio_for_each_segment(bvec, bio, j, 0)
  1445. bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
  1446. bio_put(r1_bio->bios[i]);
  1447. bio->bi_sector = r1_bio->sector +
  1448. conf->mirrors[i].rdev->data_offset;
  1449. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1450. bio->bi_end_io = raid1_end_write_request;
  1451. bio->bi_rw = WRITE |
  1452. (do_sync << BIO_RW_SYNCIO);
  1453. bio->bi_private = r1_bio;
  1454. r1_bio->bios[i] = bio;
  1455. generic_make_request(bio);
  1456. }
  1457. } else {
  1458. int disk;
  1459. /* we got a read error. Maybe the drive is bad. Maybe just
  1460. * the block and we can fix it.
  1461. * We freeze all other IO, and try reading the block from
  1462. * other devices. When we find one, we re-write
  1463. * and check it that fixes the read error.
  1464. * This is all done synchronously while the array is
  1465. * frozen
  1466. */
  1467. if (mddev->ro == 0) {
  1468. freeze_array(conf);
  1469. fix_read_error(conf, r1_bio->read_disk,
  1470. r1_bio->sector,
  1471. r1_bio->sectors);
  1472. unfreeze_array(conf);
  1473. } else
  1474. md_error(mddev,
  1475. conf->mirrors[r1_bio->read_disk].rdev);
  1476. bio = r1_bio->bios[r1_bio->read_disk];
  1477. if ((disk=read_balance(conf, r1_bio)) == -1) {
  1478. printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
  1479. " read error for block %llu\n",
  1480. bdevname(bio->bi_bdev,b),
  1481. (unsigned long long)r1_bio->sector);
  1482. raid_end_bio_io(r1_bio);
  1483. } else {
  1484. const bool do_sync = bio_rw_flagged(r1_bio->master_bio, BIO_RW_SYNCIO);
  1485. r1_bio->bios[r1_bio->read_disk] =
  1486. mddev->ro ? IO_BLOCKED : NULL;
  1487. r1_bio->read_disk = disk;
  1488. bio_put(bio);
  1489. bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
  1490. r1_bio->bios[r1_bio->read_disk] = bio;
  1491. rdev = conf->mirrors[disk].rdev;
  1492. if (printk_ratelimit())
  1493. printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
  1494. " another mirror\n",
  1495. bdevname(rdev->bdev,b),
  1496. (unsigned long long)r1_bio->sector);
  1497. bio->bi_sector = r1_bio->sector + rdev->data_offset;
  1498. bio->bi_bdev = rdev->bdev;
  1499. bio->bi_end_io = raid1_end_read_request;
  1500. bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
  1501. bio->bi_private = r1_bio;
  1502. unplug = 1;
  1503. generic_make_request(bio);
  1504. }
  1505. }
  1506. cond_resched();
  1507. }
  1508. if (unplug)
  1509. unplug_slaves(mddev);
  1510. }
  1511. static int init_resync(conf_t *conf)
  1512. {
  1513. int buffs;
  1514. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1515. BUG_ON(conf->r1buf_pool);
  1516. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  1517. conf->poolinfo);
  1518. if (!conf->r1buf_pool)
  1519. return -ENOMEM;
  1520. conf->next_resync = 0;
  1521. return 0;
  1522. }
  1523. /*
  1524. * perform a "sync" on one "block"
  1525. *
  1526. * We need to make sure that no normal I/O request - particularly write
  1527. * requests - conflict with active sync requests.
  1528. *
  1529. * This is achieved by tracking pending requests and a 'barrier' concept
  1530. * that can be installed to exclude normal IO requests.
  1531. */
  1532. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1533. {
  1534. conf_t *conf = mddev->private;
  1535. r1bio_t *r1_bio;
  1536. struct bio *bio;
  1537. sector_t max_sector, nr_sectors;
  1538. int disk = -1;
  1539. int i;
  1540. int wonly = -1;
  1541. int write_targets = 0, read_targets = 0;
  1542. int sync_blocks;
  1543. int still_degraded = 0;
  1544. if (!conf->r1buf_pool)
  1545. {
  1546. /*
  1547. printk("sync start - bitmap %p\n", mddev->bitmap);
  1548. */
  1549. if (init_resync(conf))
  1550. return 0;
  1551. }
  1552. max_sector = mddev->dev_sectors;
  1553. if (sector_nr >= max_sector) {
  1554. /* If we aborted, we need to abort the
  1555. * sync on the 'current' bitmap chunk (there will
  1556. * only be one in raid1 resync.
  1557. * We can find the current addess in mddev->curr_resync
  1558. */
  1559. if (mddev->curr_resync < max_sector) /* aborted */
  1560. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1561. &sync_blocks, 1);
  1562. else /* completed sync */
  1563. conf->fullsync = 0;
  1564. bitmap_close_sync(mddev->bitmap);
  1565. close_sync(conf);
  1566. return 0;
  1567. }
  1568. if (mddev->bitmap == NULL &&
  1569. mddev->recovery_cp == MaxSector &&
  1570. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  1571. conf->fullsync == 0) {
  1572. *skipped = 1;
  1573. return max_sector - sector_nr;
  1574. }
  1575. /* before building a request, check if we can skip these blocks..
  1576. * This call the bitmap_start_sync doesn't actually record anything
  1577. */
  1578. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1579. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1580. /* We can skip this block, and probably several more */
  1581. *skipped = 1;
  1582. return sync_blocks;
  1583. }
  1584. /*
  1585. * If there is non-resync activity waiting for a turn,
  1586. * and resync is going fast enough,
  1587. * then let it though before starting on this new sync request.
  1588. */
  1589. if (!go_faster && conf->nr_waiting)
  1590. msleep_interruptible(1000);
  1591. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  1592. raise_barrier(conf);
  1593. conf->next_resync = sector_nr;
  1594. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  1595. rcu_read_lock();
  1596. /*
  1597. * If we get a correctably read error during resync or recovery,
  1598. * we might want to read from a different device. So we
  1599. * flag all drives that could conceivably be read from for READ,
  1600. * and any others (which will be non-In_sync devices) for WRITE.
  1601. * If a read fails, we try reading from something else for which READ
  1602. * is OK.
  1603. */
  1604. r1_bio->mddev = mddev;
  1605. r1_bio->sector = sector_nr;
  1606. r1_bio->state = 0;
  1607. set_bit(R1BIO_IsSync, &r1_bio->state);
  1608. for (i=0; i < conf->raid_disks; i++) {
  1609. mdk_rdev_t *rdev;
  1610. bio = r1_bio->bios[i];
  1611. /* take from bio_init */
  1612. bio->bi_next = NULL;
  1613. bio->bi_flags |= 1 << BIO_UPTODATE;
  1614. bio->bi_rw = READ;
  1615. bio->bi_vcnt = 0;
  1616. bio->bi_idx = 0;
  1617. bio->bi_phys_segments = 0;
  1618. bio->bi_size = 0;
  1619. bio->bi_end_io = NULL;
  1620. bio->bi_private = NULL;
  1621. rdev = rcu_dereference(conf->mirrors[i].rdev);
  1622. if (rdev == NULL ||
  1623. test_bit(Faulty, &rdev->flags)) {
  1624. still_degraded = 1;
  1625. continue;
  1626. } else if (!test_bit(In_sync, &rdev->flags)) {
  1627. bio->bi_rw = WRITE;
  1628. bio->bi_end_io = end_sync_write;
  1629. write_targets ++;
  1630. } else {
  1631. /* may need to read from here */
  1632. bio->bi_rw = READ;
  1633. bio->bi_end_io = end_sync_read;
  1634. if (test_bit(WriteMostly, &rdev->flags)) {
  1635. if (wonly < 0)
  1636. wonly = i;
  1637. } else {
  1638. if (disk < 0)
  1639. disk = i;
  1640. }
  1641. read_targets++;
  1642. }
  1643. atomic_inc(&rdev->nr_pending);
  1644. bio->bi_sector = sector_nr + rdev->data_offset;
  1645. bio->bi_bdev = rdev->bdev;
  1646. bio->bi_private = r1_bio;
  1647. }
  1648. rcu_read_unlock();
  1649. if (disk < 0)
  1650. disk = wonly;
  1651. r1_bio->read_disk = disk;
  1652. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  1653. /* extra read targets are also write targets */
  1654. write_targets += read_targets-1;
  1655. if (write_targets == 0 || read_targets == 0) {
  1656. /* There is nowhere to write, so all non-sync
  1657. * drives must be failed - so we are finished
  1658. */
  1659. sector_t rv = max_sector - sector_nr;
  1660. *skipped = 1;
  1661. put_buf(r1_bio);
  1662. return rv;
  1663. }
  1664. if (max_sector > mddev->resync_max)
  1665. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  1666. nr_sectors = 0;
  1667. sync_blocks = 0;
  1668. do {
  1669. struct page *page;
  1670. int len = PAGE_SIZE;
  1671. if (sector_nr + (len>>9) > max_sector)
  1672. len = (max_sector - sector_nr) << 9;
  1673. if (len == 0)
  1674. break;
  1675. if (sync_blocks == 0) {
  1676. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1677. &sync_blocks, still_degraded) &&
  1678. !conf->fullsync &&
  1679. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1680. break;
  1681. BUG_ON(sync_blocks < (PAGE_SIZE>>9));
  1682. if (len > (sync_blocks<<9))
  1683. len = sync_blocks<<9;
  1684. }
  1685. for (i=0 ; i < conf->raid_disks; i++) {
  1686. bio = r1_bio->bios[i];
  1687. if (bio->bi_end_io) {
  1688. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  1689. if (bio_add_page(bio, page, len, 0) == 0) {
  1690. /* stop here */
  1691. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1692. while (i > 0) {
  1693. i--;
  1694. bio = r1_bio->bios[i];
  1695. if (bio->bi_end_io==NULL)
  1696. continue;
  1697. /* remove last page from this bio */
  1698. bio->bi_vcnt--;
  1699. bio->bi_size -= len;
  1700. bio->bi_flags &= ~(1<< BIO_SEG_VALID);
  1701. }
  1702. goto bio_full;
  1703. }
  1704. }
  1705. }
  1706. nr_sectors += len>>9;
  1707. sector_nr += len>>9;
  1708. sync_blocks -= (len>>9);
  1709. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  1710. bio_full:
  1711. r1_bio->sectors = nr_sectors;
  1712. /* For a user-requested sync, we read all readable devices and do a
  1713. * compare
  1714. */
  1715. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1716. atomic_set(&r1_bio->remaining, read_targets);
  1717. for (i=0; i<conf->raid_disks; i++) {
  1718. bio = r1_bio->bios[i];
  1719. if (bio->bi_end_io == end_sync_read) {
  1720. md_sync_acct(bio->bi_bdev, nr_sectors);
  1721. generic_make_request(bio);
  1722. }
  1723. }
  1724. } else {
  1725. atomic_set(&r1_bio->remaining, 1);
  1726. bio = r1_bio->bios[r1_bio->read_disk];
  1727. md_sync_acct(bio->bi_bdev, nr_sectors);
  1728. generic_make_request(bio);
  1729. }
  1730. return nr_sectors;
  1731. }
  1732. static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  1733. {
  1734. if (sectors)
  1735. return sectors;
  1736. return mddev->dev_sectors;
  1737. }
  1738. static conf_t *setup_conf(mddev_t *mddev)
  1739. {
  1740. conf_t *conf;
  1741. int i;
  1742. mirror_info_t *disk;
  1743. mdk_rdev_t *rdev;
  1744. int err = -ENOMEM;
  1745. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  1746. if (!conf)
  1747. goto abort;
  1748. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1749. GFP_KERNEL);
  1750. if (!conf->mirrors)
  1751. goto abort;
  1752. conf->tmppage = alloc_page(GFP_KERNEL);
  1753. if (!conf->tmppage)
  1754. goto abort;
  1755. conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  1756. if (!conf->poolinfo)
  1757. goto abort;
  1758. conf->poolinfo->raid_disks = mddev->raid_disks;
  1759. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1760. r1bio_pool_free,
  1761. conf->poolinfo);
  1762. if (!conf->r1bio_pool)
  1763. goto abort;
  1764. conf->poolinfo->mddev = mddev;
  1765. spin_lock_init(&conf->device_lock);
  1766. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1767. int disk_idx = rdev->raid_disk;
  1768. if (disk_idx >= mddev->raid_disks
  1769. || disk_idx < 0)
  1770. continue;
  1771. disk = conf->mirrors + disk_idx;
  1772. disk->rdev = rdev;
  1773. disk->head_position = 0;
  1774. }
  1775. conf->raid_disks = mddev->raid_disks;
  1776. conf->mddev = mddev;
  1777. INIT_LIST_HEAD(&conf->retry_list);
  1778. spin_lock_init(&conf->resync_lock);
  1779. init_waitqueue_head(&conf->wait_barrier);
  1780. bio_list_init(&conf->pending_bio_list);
  1781. bio_list_init(&conf->flushing_bio_list);
  1782. conf->last_used = -1;
  1783. for (i = 0; i < conf->raid_disks; i++) {
  1784. disk = conf->mirrors + i;
  1785. if (!disk->rdev ||
  1786. !test_bit(In_sync, &disk->rdev->flags)) {
  1787. disk->head_position = 0;
  1788. if (disk->rdev)
  1789. conf->fullsync = 1;
  1790. } else if (conf->last_used < 0)
  1791. /*
  1792. * The first working device is used as a
  1793. * starting point to read balancing.
  1794. */
  1795. conf->last_used = i;
  1796. }
  1797. err = -EIO;
  1798. if (conf->last_used < 0) {
  1799. printk(KERN_ERR "raid1: no operational mirrors for %s\n",
  1800. mdname(mddev));
  1801. goto abort;
  1802. }
  1803. err = -ENOMEM;
  1804. conf->thread = md_register_thread(raid1d, mddev, NULL);
  1805. if (!conf->thread) {
  1806. printk(KERN_ERR
  1807. "raid1: couldn't allocate thread for %s\n",
  1808. mdname(mddev));
  1809. goto abort;
  1810. }
  1811. return conf;
  1812. abort:
  1813. if (conf) {
  1814. if (conf->r1bio_pool)
  1815. mempool_destroy(conf->r1bio_pool);
  1816. kfree(conf->mirrors);
  1817. safe_put_page(conf->tmppage);
  1818. kfree(conf->poolinfo);
  1819. kfree(conf);
  1820. }
  1821. return ERR_PTR(err);
  1822. }
  1823. static int run(mddev_t *mddev)
  1824. {
  1825. conf_t *conf;
  1826. int i;
  1827. mdk_rdev_t *rdev;
  1828. if (mddev->level != 1) {
  1829. printk("raid1: %s: raid level not set to mirroring (%d)\n",
  1830. mdname(mddev), mddev->level);
  1831. return -EIO;
  1832. }
  1833. if (mddev->reshape_position != MaxSector) {
  1834. printk("raid1: %s: reshape_position set but not supported\n",
  1835. mdname(mddev));
  1836. return -EIO;
  1837. }
  1838. /*
  1839. * copy the already verified devices into our private RAID1
  1840. * bookkeeping area. [whatever we allocate in run(),
  1841. * should be freed in stop()]
  1842. */
  1843. if (mddev->private == NULL)
  1844. conf = setup_conf(mddev);
  1845. else
  1846. conf = mddev->private;
  1847. if (IS_ERR(conf))
  1848. return PTR_ERR(conf);
  1849. mddev->queue->queue_lock = &conf->device_lock;
  1850. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1851. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1852. rdev->data_offset << 9);
  1853. /* as we don't honour merge_bvec_fn, we must never risk
  1854. * violating it, so limit ->max_sector to one PAGE, as
  1855. * a one page request is never in violation.
  1856. */
  1857. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1858. queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
  1859. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  1860. }
  1861. mddev->degraded = 0;
  1862. for (i=0; i < conf->raid_disks; i++)
  1863. if (conf->mirrors[i].rdev == NULL ||
  1864. !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
  1865. test_bit(Faulty, &conf->mirrors[i].rdev->flags))
  1866. mddev->degraded++;
  1867. if (conf->raid_disks - mddev->degraded == 1)
  1868. mddev->recovery_cp = MaxSector;
  1869. if (mddev->recovery_cp != MaxSector)
  1870. printk(KERN_NOTICE "raid1: %s is not clean"
  1871. " -- starting background reconstruction\n",
  1872. mdname(mddev));
  1873. printk(KERN_INFO
  1874. "raid1: raid set %s active with %d out of %d mirrors\n",
  1875. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1876. mddev->raid_disks);
  1877. /*
  1878. * Ok, everything is just fine now
  1879. */
  1880. mddev->thread = conf->thread;
  1881. conf->thread = NULL;
  1882. mddev->private = conf;
  1883. md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
  1884. mddev->queue->unplug_fn = raid1_unplug;
  1885. mddev->queue->backing_dev_info.congested_fn = raid1_congested;
  1886. mddev->queue->backing_dev_info.congested_data = mddev;
  1887. md_integrity_register(mddev);
  1888. return 0;
  1889. }
  1890. static int stop(mddev_t *mddev)
  1891. {
  1892. conf_t *conf = mddev->private;
  1893. struct bitmap *bitmap = mddev->bitmap;
  1894. int behind_wait = 0;
  1895. /* wait for behind writes to complete */
  1896. while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
  1897. behind_wait++;
  1898. printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
  1899. set_current_state(TASK_UNINTERRUPTIBLE);
  1900. schedule_timeout(HZ); /* wait a second */
  1901. /* need to kick something here to make sure I/O goes? */
  1902. }
  1903. raise_barrier(conf);
  1904. lower_barrier(conf);
  1905. md_unregister_thread(mddev->thread);
  1906. mddev->thread = NULL;
  1907. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1908. if (conf->r1bio_pool)
  1909. mempool_destroy(conf->r1bio_pool);
  1910. kfree(conf->mirrors);
  1911. kfree(conf->poolinfo);
  1912. kfree(conf);
  1913. mddev->private = NULL;
  1914. return 0;
  1915. }
  1916. static int raid1_resize(mddev_t *mddev, sector_t sectors)
  1917. {
  1918. /* no resync is happening, and there is enough space
  1919. * on all devices, so we can resize.
  1920. * We need to make sure resync covers any new space.
  1921. * If the array is shrinking we should possibly wait until
  1922. * any io in the removed space completes, but it hardly seems
  1923. * worth it.
  1924. */
  1925. md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
  1926. if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
  1927. return -EINVAL;
  1928. set_capacity(mddev->gendisk, mddev->array_sectors);
  1929. mddev->changed = 1;
  1930. revalidate_disk(mddev->gendisk);
  1931. if (sectors > mddev->dev_sectors &&
  1932. mddev->recovery_cp == MaxSector) {
  1933. mddev->recovery_cp = mddev->dev_sectors;
  1934. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1935. }
  1936. mddev->dev_sectors = sectors;
  1937. mddev->resync_max_sectors = sectors;
  1938. return 0;
  1939. }
  1940. static int raid1_reshape(mddev_t *mddev)
  1941. {
  1942. /* We need to:
  1943. * 1/ resize the r1bio_pool
  1944. * 2/ resize conf->mirrors
  1945. *
  1946. * We allocate a new r1bio_pool if we can.
  1947. * Then raise a device barrier and wait until all IO stops.
  1948. * Then resize conf->mirrors and swap in the new r1bio pool.
  1949. *
  1950. * At the same time, we "pack" the devices so that all the missing
  1951. * devices have the higher raid_disk numbers.
  1952. */
  1953. mempool_t *newpool, *oldpool;
  1954. struct pool_info *newpoolinfo;
  1955. mirror_info_t *newmirrors;
  1956. conf_t *conf = mddev->private;
  1957. int cnt, raid_disks;
  1958. unsigned long flags;
  1959. int d, d2, err;
  1960. /* Cannot change chunk_size, layout, or level */
  1961. if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
  1962. mddev->layout != mddev->new_layout ||
  1963. mddev->level != mddev->new_level) {
  1964. mddev->new_chunk_sectors = mddev->chunk_sectors;
  1965. mddev->new_layout = mddev->layout;
  1966. mddev->new_level = mddev->level;
  1967. return -EINVAL;
  1968. }
  1969. err = md_allow_write(mddev);
  1970. if (err)
  1971. return err;
  1972. raid_disks = mddev->raid_disks + mddev->delta_disks;
  1973. if (raid_disks < conf->raid_disks) {
  1974. cnt=0;
  1975. for (d= 0; d < conf->raid_disks; d++)
  1976. if (conf->mirrors[d].rdev)
  1977. cnt++;
  1978. if (cnt > raid_disks)
  1979. return -EBUSY;
  1980. }
  1981. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  1982. if (!newpoolinfo)
  1983. return -ENOMEM;
  1984. newpoolinfo->mddev = mddev;
  1985. newpoolinfo->raid_disks = raid_disks;
  1986. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1987. r1bio_pool_free, newpoolinfo);
  1988. if (!newpool) {
  1989. kfree(newpoolinfo);
  1990. return -ENOMEM;
  1991. }
  1992. newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
  1993. if (!newmirrors) {
  1994. kfree(newpoolinfo);
  1995. mempool_destroy(newpool);
  1996. return -ENOMEM;
  1997. }
  1998. raise_barrier(conf);
  1999. /* ok, everything is stopped */
  2000. oldpool = conf->r1bio_pool;
  2001. conf->r1bio_pool = newpool;
  2002. for (d = d2 = 0; d < conf->raid_disks; d++) {
  2003. mdk_rdev_t *rdev = conf->mirrors[d].rdev;
  2004. if (rdev && rdev->raid_disk != d2) {
  2005. char nm[20];
  2006. sprintf(nm, "rd%d", rdev->raid_disk);
  2007. sysfs_remove_link(&mddev->kobj, nm);
  2008. rdev->raid_disk = d2;
  2009. sprintf(nm, "rd%d", rdev->raid_disk);
  2010. sysfs_remove_link(&mddev->kobj, nm);
  2011. if (sysfs_create_link(&mddev->kobj,
  2012. &rdev->kobj, nm))
  2013. printk(KERN_WARNING
  2014. "md/raid1: cannot register "
  2015. "%s for %s\n",
  2016. nm, mdname(mddev));
  2017. }
  2018. if (rdev)
  2019. newmirrors[d2++].rdev = rdev;
  2020. }
  2021. kfree(conf->mirrors);
  2022. conf->mirrors = newmirrors;
  2023. kfree(conf->poolinfo);
  2024. conf->poolinfo = newpoolinfo;
  2025. spin_lock_irqsave(&conf->device_lock, flags);
  2026. mddev->degraded += (raid_disks - conf->raid_disks);
  2027. spin_unlock_irqrestore(&conf->device_lock, flags);
  2028. conf->raid_disks = mddev->raid_disks = raid_disks;
  2029. mddev->delta_disks = 0;
  2030. conf->last_used = 0; /* just make sure it is in-range */
  2031. lower_barrier(conf);
  2032. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2033. md_wakeup_thread(mddev->thread);
  2034. mempool_destroy(oldpool);
  2035. return 0;
  2036. }
  2037. static void raid1_quiesce(mddev_t *mddev, int state)
  2038. {
  2039. conf_t *conf = mddev->private;
  2040. switch(state) {
  2041. case 2: /* wake for suspend */
  2042. wake_up(&conf->wait_barrier);
  2043. break;
  2044. case 1:
  2045. raise_barrier(conf);
  2046. break;
  2047. case 0:
  2048. lower_barrier(conf);
  2049. break;
  2050. }
  2051. }
  2052. static void *raid1_takeover(mddev_t *mddev)
  2053. {
  2054. /* raid1 can take over:
  2055. * raid5 with 2 devices, any layout or chunk size
  2056. */
  2057. if (mddev->level == 5 && mddev->raid_disks == 2) {
  2058. conf_t *conf;
  2059. mddev->new_level = 1;
  2060. mddev->new_layout = 0;
  2061. mddev->new_chunk_sectors = 0;
  2062. conf = setup_conf(mddev);
  2063. if (!IS_ERR(conf))
  2064. conf->barrier = 1;
  2065. return conf;
  2066. }
  2067. return ERR_PTR(-EINVAL);
  2068. }
  2069. static struct mdk_personality raid1_personality =
  2070. {
  2071. .name = "raid1",
  2072. .level = 1,
  2073. .owner = THIS_MODULE,
  2074. .make_request = make_request,
  2075. .run = run,
  2076. .stop = stop,
  2077. .status = status,
  2078. .error_handler = error,
  2079. .hot_add_disk = raid1_add_disk,
  2080. .hot_remove_disk= raid1_remove_disk,
  2081. .spare_active = raid1_spare_active,
  2082. .sync_request = sync_request,
  2083. .resize = raid1_resize,
  2084. .size = raid1_size,
  2085. .check_reshape = raid1_reshape,
  2086. .quiesce = raid1_quiesce,
  2087. .takeover = raid1_takeover,
  2088. };
  2089. static int __init raid_init(void)
  2090. {
  2091. return register_md_personality(&raid1_personality);
  2092. }
  2093. static void raid_exit(void)
  2094. {
  2095. unregister_md_personality(&raid1_personality);
  2096. }
  2097. module_init(raid_init);
  2098. module_exit(raid_exit);
  2099. MODULE_LICENSE("GPL");
  2100. MODULE_ALIAS("md-personality-3"); /* RAID1 */
  2101. MODULE_ALIAS("md-raid1");
  2102. MODULE_ALIAS("md-level-1");