raid1.c 46 KB

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