raid1.c 58 KB

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