raid1.c 55 KB

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