raid1.c 60 KB

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