raid1.c 54 KB

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