raid1.c 60 KB

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