raid1.c 58 KB

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