raid1.c 61 KB

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