raid1.c 58 KB

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