raid1.c 56 KB

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