raid1.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908
  1. /*
  2. * raid1.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
  5. *
  6. * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  7. *
  8. * RAID-1 management functions.
  9. *
  10. * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
  11. *
  12. * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
  13. * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
  14. *
  15. * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
  16. * bitmapped intelligence in resync:
  17. *
  18. * - bitmap marked during normal i/o
  19. * - bitmap used to skip nondirty blocks during sync
  20. *
  21. * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
  22. * - persistent bitmap code
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2, or (at your option)
  27. * any later version.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * (for example /usr/src/linux/COPYING); if not, write to the Free
  31. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/slab.h>
  34. #include <linux/delay.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/module.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/ratelimit.h>
  39. #include "md.h"
  40. #include "raid1.h"
  41. #include "bitmap.h"
  42. /*
  43. * Number of guaranteed r1bios in case of extreme VM load:
  44. */
  45. #define NR_RAID1_BIOS 256
  46. /* When there are this many requests queue to be written by
  47. * the raid1 thread, we become 'congested' to provide back-pressure
  48. * for writeback.
  49. */
  50. static int max_queued_requests = 1024;
  51. static void allow_barrier(struct r1conf *conf);
  52. static void lower_barrier(struct r1conf *conf);
  53. static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
  54. {
  55. struct pool_info *pi = data;
  56. int size = offsetof(struct r1bio, bios[pi->raid_disks]);
  57. /* allocate a r1bio with room for raid_disks entries in the bios array */
  58. return kzalloc(size, gfp_flags);
  59. }
  60. static void r1bio_pool_free(void *r1_bio, void *data)
  61. {
  62. kfree(r1_bio);
  63. }
  64. #define RESYNC_BLOCK_SIZE (64*1024)
  65. //#define RESYNC_BLOCK_SIZE PAGE_SIZE
  66. #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
  67. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  68. #define RESYNC_WINDOW (2048*1024)
  69. static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
  70. {
  71. struct pool_info *pi = data;
  72. struct page *page;
  73. struct r1bio *r1_bio;
  74. struct bio *bio;
  75. int i, j;
  76. r1_bio = r1bio_pool_alloc(gfp_flags, pi);
  77. if (!r1_bio)
  78. return NULL;
  79. /*
  80. * Allocate bios : 1 for reading, n-1 for writing
  81. */
  82. for (j = pi->raid_disks ; j-- ; ) {
  83. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  84. if (!bio)
  85. goto out_free_bio;
  86. r1_bio->bios[j] = bio;
  87. }
  88. /*
  89. * Allocate RESYNC_PAGES data pages and attach them to
  90. * the first bio.
  91. * If this is a user-requested check/repair, allocate
  92. * RESYNC_PAGES for each bio.
  93. */
  94. if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
  95. j = pi->raid_disks;
  96. else
  97. j = 1;
  98. while(j--) {
  99. bio = r1_bio->bios[j];
  100. for (i = 0; i < RESYNC_PAGES; i++) {
  101. page = alloc_page(gfp_flags);
  102. if (unlikely(!page))
  103. goto out_free_pages;
  104. bio->bi_io_vec[i].bv_page = page;
  105. bio->bi_vcnt = i+1;
  106. }
  107. }
  108. /* If not user-requests, copy the page pointers to all bios */
  109. if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
  110. for (i=0; i<RESYNC_PAGES ; i++)
  111. for (j=1; j<pi->raid_disks; j++)
  112. r1_bio->bios[j]->bi_io_vec[i].bv_page =
  113. r1_bio->bios[0]->bi_io_vec[i].bv_page;
  114. }
  115. r1_bio->master_bio = NULL;
  116. return r1_bio;
  117. out_free_pages:
  118. for (j=0 ; j < pi->raid_disks; j++)
  119. for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++)
  120. put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
  121. j = -1;
  122. out_free_bio:
  123. while (++j < pi->raid_disks)
  124. bio_put(r1_bio->bios[j]);
  125. r1bio_pool_free(r1_bio, data);
  126. return NULL;
  127. }
  128. static void r1buf_pool_free(void *__r1_bio, void *data)
  129. {
  130. struct pool_info *pi = data;
  131. int i,j;
  132. struct r1bio *r1bio = __r1_bio;
  133. for (i = 0; i < RESYNC_PAGES; i++)
  134. for (j = pi->raid_disks; j-- ;) {
  135. if (j == 0 ||
  136. r1bio->bios[j]->bi_io_vec[i].bv_page !=
  137. r1bio->bios[0]->bi_io_vec[i].bv_page)
  138. safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
  139. }
  140. for (i=0 ; i < pi->raid_disks; i++)
  141. bio_put(r1bio->bios[i]);
  142. r1bio_pool_free(r1bio, data);
  143. }
  144. static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
  145. {
  146. int i;
  147. for (i = 0; i < conf->raid_disks * 2; i++) {
  148. struct bio **bio = r1_bio->bios + i;
  149. if (!BIO_SPECIAL(*bio))
  150. bio_put(*bio);
  151. *bio = NULL;
  152. }
  153. }
  154. static void free_r1bio(struct r1bio *r1_bio)
  155. {
  156. struct r1conf *conf = r1_bio->mddev->private;
  157. put_all_bios(conf, r1_bio);
  158. mempool_free(r1_bio, conf->r1bio_pool);
  159. }
  160. static void put_buf(struct r1bio *r1_bio)
  161. {
  162. struct r1conf *conf = r1_bio->mddev->private;
  163. int i;
  164. for (i = 0; i < conf->raid_disks * 2; i++) {
  165. struct bio *bio = r1_bio->bios[i];
  166. if (bio->bi_end_io)
  167. rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
  168. }
  169. mempool_free(r1_bio, conf->r1buf_pool);
  170. lower_barrier(conf);
  171. }
  172. static void reschedule_retry(struct r1bio *r1_bio)
  173. {
  174. unsigned long flags;
  175. struct mddev *mddev = r1_bio->mddev;
  176. struct r1conf *conf = mddev->private;
  177. spin_lock_irqsave(&conf->device_lock, flags);
  178. list_add(&r1_bio->retry_list, &conf->retry_list);
  179. conf->nr_queued ++;
  180. spin_unlock_irqrestore(&conf->device_lock, flags);
  181. wake_up(&conf->wait_barrier);
  182. md_wakeup_thread(mddev->thread);
  183. }
  184. /*
  185. * raid_end_bio_io() is called when we have finished servicing a mirrored
  186. * operation and are ready to return a success/failure code to the buffer
  187. * cache layer.
  188. */
  189. static void call_bio_endio(struct r1bio *r1_bio)
  190. {
  191. struct bio *bio = r1_bio->master_bio;
  192. int done;
  193. struct r1conf *conf = r1_bio->mddev->private;
  194. if (bio->bi_phys_segments) {
  195. unsigned long flags;
  196. spin_lock_irqsave(&conf->device_lock, flags);
  197. bio->bi_phys_segments--;
  198. done = (bio->bi_phys_segments == 0);
  199. spin_unlock_irqrestore(&conf->device_lock, flags);
  200. } else
  201. done = 1;
  202. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  203. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  204. if (done) {
  205. bio_endio(bio, 0);
  206. /*
  207. * Wake up any possible resync thread that waits for the device
  208. * to go idle.
  209. */
  210. allow_barrier(conf);
  211. }
  212. }
  213. static void raid_end_bio_io(struct r1bio *r1_bio)
  214. {
  215. struct bio *bio = r1_bio->master_bio;
  216. /* if nobody has done the final endio yet, do it now */
  217. if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
  218. pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
  219. (bio_data_dir(bio) == WRITE) ? "write" : "read",
  220. (unsigned long long) bio->bi_sector,
  221. (unsigned long long) bio->bi_sector +
  222. (bio->bi_size >> 9) - 1);
  223. call_bio_endio(r1_bio);
  224. }
  225. free_r1bio(r1_bio);
  226. }
  227. /*
  228. * Update disk head position estimator based on IRQ completion info.
  229. */
  230. static inline void update_head_pos(int disk, struct r1bio *r1_bio)
  231. {
  232. struct r1conf *conf = r1_bio->mddev->private;
  233. conf->mirrors[disk].head_position =
  234. r1_bio->sector + (r1_bio->sectors);
  235. }
  236. /*
  237. * Find the disk number which triggered given bio
  238. */
  239. static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
  240. {
  241. int mirror;
  242. struct r1conf *conf = r1_bio->mddev->private;
  243. int raid_disks = conf->raid_disks;
  244. for (mirror = 0; mirror < raid_disks * 2; mirror++)
  245. if (r1_bio->bios[mirror] == bio)
  246. break;
  247. BUG_ON(mirror == raid_disks * 2);
  248. update_head_pos(mirror, r1_bio);
  249. return mirror;
  250. }
  251. static void raid1_end_read_request(struct bio *bio, int error)
  252. {
  253. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  254. struct r1bio *r1_bio = bio->bi_private;
  255. int mirror;
  256. struct r1conf *conf = r1_bio->mddev->private;
  257. mirror = r1_bio->read_disk;
  258. /*
  259. * this branch is our 'one mirror IO has finished' event handler:
  260. */
  261. update_head_pos(mirror, r1_bio);
  262. if (uptodate)
  263. set_bit(R1BIO_Uptodate, &r1_bio->state);
  264. else {
  265. /* If all other devices have failed, we want to return
  266. * the error upwards rather than fail the last device.
  267. * Here we redefine "uptodate" to mean "Don't want to retry"
  268. */
  269. unsigned long flags;
  270. spin_lock_irqsave(&conf->device_lock, flags);
  271. if (r1_bio->mddev->degraded == conf->raid_disks ||
  272. (r1_bio->mddev->degraded == conf->raid_disks-1 &&
  273. !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
  274. uptodate = 1;
  275. spin_unlock_irqrestore(&conf->device_lock, flags);
  276. }
  277. if (uptodate)
  278. raid_end_bio_io(r1_bio);
  279. else {
  280. /*
  281. * oops, read error:
  282. */
  283. char b[BDEVNAME_SIZE];
  284. printk_ratelimited(
  285. KERN_ERR "md/raid1:%s: %s: "
  286. "rescheduling sector %llu\n",
  287. mdname(conf->mddev),
  288. bdevname(conf->mirrors[mirror].rdev->bdev,
  289. b),
  290. (unsigned long long)r1_bio->sector);
  291. set_bit(R1BIO_ReadError, &r1_bio->state);
  292. reschedule_retry(r1_bio);
  293. }
  294. rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
  295. }
  296. static void close_write(struct r1bio *r1_bio)
  297. {
  298. /* it really is the end of this request */
  299. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  300. /* free extra copy of the data pages */
  301. int i = r1_bio->behind_page_count;
  302. while (i--)
  303. safe_put_page(r1_bio->behind_bvecs[i].bv_page);
  304. kfree(r1_bio->behind_bvecs);
  305. r1_bio->behind_bvecs = NULL;
  306. }
  307. /* clear the bitmap if all writes complete successfully */
  308. bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
  309. r1_bio->sectors,
  310. !test_bit(R1BIO_Degraded, &r1_bio->state),
  311. test_bit(R1BIO_BehindIO, &r1_bio->state));
  312. md_write_end(r1_bio->mddev);
  313. }
  314. static void r1_bio_write_done(struct r1bio *r1_bio)
  315. {
  316. if (!atomic_dec_and_test(&r1_bio->remaining))
  317. return;
  318. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  319. reschedule_retry(r1_bio);
  320. else {
  321. close_write(r1_bio);
  322. if (test_bit(R1BIO_MadeGood, &r1_bio->state))
  323. reschedule_retry(r1_bio);
  324. else
  325. raid_end_bio_io(r1_bio);
  326. }
  327. }
  328. static void raid1_end_write_request(struct bio *bio, int error)
  329. {
  330. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  331. struct r1bio *r1_bio = bio->bi_private;
  332. int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
  333. struct r1conf *conf = r1_bio->mddev->private;
  334. struct bio *to_put = NULL;
  335. mirror = find_bio_disk(r1_bio, bio);
  336. /*
  337. * 'one mirror IO has finished' event handler:
  338. */
  339. if (!uptodate) {
  340. set_bit(WriteErrorSeen,
  341. &conf->mirrors[mirror].rdev->flags);
  342. if (!test_and_set_bit(WantReplacement,
  343. &conf->mirrors[mirror].rdev->flags))
  344. set_bit(MD_RECOVERY_NEEDED, &
  345. conf->mddev->recovery);
  346. set_bit(R1BIO_WriteError, &r1_bio->state);
  347. } else {
  348. /*
  349. * Set R1BIO_Uptodate in our master bio, so that we
  350. * will return a good error code for to the higher
  351. * levels even if IO on some other mirrored buffer
  352. * fails.
  353. *
  354. * The 'master' represents the composite IO operation
  355. * to user-side. So if something waits for IO, then it
  356. * will wait for the 'master' bio.
  357. */
  358. sector_t first_bad;
  359. int bad_sectors;
  360. r1_bio->bios[mirror] = NULL;
  361. to_put = bio;
  362. set_bit(R1BIO_Uptodate, &r1_bio->state);
  363. /* Maybe we can clear some bad blocks. */
  364. if (is_badblock(conf->mirrors[mirror].rdev,
  365. r1_bio->sector, r1_bio->sectors,
  366. &first_bad, &bad_sectors)) {
  367. r1_bio->bios[mirror] = IO_MADE_GOOD;
  368. set_bit(R1BIO_MadeGood, &r1_bio->state);
  369. }
  370. }
  371. if (behind) {
  372. if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
  373. atomic_dec(&r1_bio->behind_remaining);
  374. /*
  375. * In behind mode, we ACK the master bio once the I/O
  376. * has safely reached all non-writemostly
  377. * disks. Setting the Returned bit ensures that this
  378. * gets done only once -- we don't ever want to return
  379. * -EIO here, instead we'll wait
  380. */
  381. if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
  382. test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  383. /* Maybe we can return now */
  384. if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
  385. struct bio *mbio = r1_bio->master_bio;
  386. pr_debug("raid1: behind end write sectors"
  387. " %llu-%llu\n",
  388. (unsigned long long) mbio->bi_sector,
  389. (unsigned long long) mbio->bi_sector +
  390. (mbio->bi_size >> 9) - 1);
  391. call_bio_endio(r1_bio);
  392. }
  393. }
  394. }
  395. if (r1_bio->bios[mirror] == NULL)
  396. rdev_dec_pending(conf->mirrors[mirror].rdev,
  397. conf->mddev);
  398. /*
  399. * Let's see if all mirrored write operations have finished
  400. * already.
  401. */
  402. r1_bio_write_done(r1_bio);
  403. if (to_put)
  404. bio_put(to_put);
  405. }
  406. /*
  407. * This routine returns the disk from which the requested read should
  408. * be done. There is a per-array 'next expected sequential IO' sector
  409. * number - if this matches on the next IO then we use the last disk.
  410. * There is also a per-disk 'last know head position' sector that is
  411. * maintained from IRQ contexts, both the normal and the resync IO
  412. * completion handlers update this position correctly. If there is no
  413. * perfect sequential match then we pick the disk whose head is closest.
  414. *
  415. * If there are 2 mirrors in the same 2 devices, performance degrades
  416. * because position is mirror, not device based.
  417. *
  418. * The rdev for the device selected will have nr_pending incremented.
  419. */
  420. static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
  421. {
  422. const sector_t this_sector = r1_bio->sector;
  423. int sectors;
  424. int best_good_sectors;
  425. int start_disk;
  426. int best_disk;
  427. int i;
  428. sector_t best_dist;
  429. struct md_rdev *rdev;
  430. int choose_first;
  431. rcu_read_lock();
  432. /*
  433. * Check if we can balance. We can balance on the whole
  434. * device if no resync is going on, or below the resync window.
  435. * We take the first readable disk when above the resync window.
  436. */
  437. retry:
  438. sectors = r1_bio->sectors;
  439. best_disk = -1;
  440. best_dist = MaxSector;
  441. best_good_sectors = 0;
  442. if (conf->mddev->recovery_cp < MaxSector &&
  443. (this_sector + sectors >= conf->next_resync)) {
  444. choose_first = 1;
  445. start_disk = 0;
  446. } else {
  447. choose_first = 0;
  448. start_disk = conf->last_used;
  449. }
  450. for (i = 0 ; i < conf->raid_disks * 2 ; i++) {
  451. sector_t dist;
  452. sector_t first_bad;
  453. int bad_sectors;
  454. int disk = start_disk + i;
  455. if (disk >= conf->raid_disks)
  456. disk -= conf->raid_disks;
  457. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  458. if (r1_bio->bios[disk] == IO_BLOCKED
  459. || rdev == NULL
  460. || test_bit(Faulty, &rdev->flags))
  461. continue;
  462. if (!test_bit(In_sync, &rdev->flags) &&
  463. rdev->recovery_offset < this_sector + sectors)
  464. continue;
  465. if (test_bit(WriteMostly, &rdev->flags)) {
  466. /* Don't balance among write-mostly, just
  467. * use the first as a last resort */
  468. if (best_disk < 0) {
  469. if (is_badblock(rdev, this_sector, sectors,
  470. &first_bad, &bad_sectors)) {
  471. if (first_bad < this_sector)
  472. /* Cannot use this */
  473. continue;
  474. best_good_sectors = first_bad - this_sector;
  475. } else
  476. best_good_sectors = sectors;
  477. best_disk = disk;
  478. }
  479. continue;
  480. }
  481. /* This is a reasonable device to use. It might
  482. * even be best.
  483. */
  484. if (is_badblock(rdev, this_sector, sectors,
  485. &first_bad, &bad_sectors)) {
  486. if (best_dist < MaxSector)
  487. /* already have a better device */
  488. continue;
  489. if (first_bad <= this_sector) {
  490. /* cannot read here. If this is the 'primary'
  491. * device, then we must not read beyond
  492. * bad_sectors from another device..
  493. */
  494. bad_sectors -= (this_sector - first_bad);
  495. if (choose_first && sectors > bad_sectors)
  496. sectors = bad_sectors;
  497. if (best_good_sectors > sectors)
  498. best_good_sectors = sectors;
  499. } else {
  500. sector_t good_sectors = first_bad - this_sector;
  501. if (good_sectors > best_good_sectors) {
  502. best_good_sectors = good_sectors;
  503. best_disk = disk;
  504. }
  505. if (choose_first)
  506. break;
  507. }
  508. continue;
  509. } else
  510. best_good_sectors = sectors;
  511. dist = abs(this_sector - conf->mirrors[disk].head_position);
  512. if (choose_first
  513. /* Don't change to another disk for sequential reads */
  514. || conf->next_seq_sect == this_sector
  515. || dist == 0
  516. /* If device is idle, use it */
  517. || atomic_read(&rdev->nr_pending) == 0) {
  518. best_disk = disk;
  519. break;
  520. }
  521. if (dist < best_dist) {
  522. best_dist = dist;
  523. best_disk = disk;
  524. }
  525. }
  526. if (best_disk >= 0) {
  527. rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
  528. if (!rdev)
  529. goto retry;
  530. atomic_inc(&rdev->nr_pending);
  531. if (test_bit(Faulty, &rdev->flags)) {
  532. /* cannot risk returning a device that failed
  533. * before we inc'ed nr_pending
  534. */
  535. rdev_dec_pending(rdev, conf->mddev);
  536. goto retry;
  537. }
  538. sectors = best_good_sectors;
  539. conf->next_seq_sect = this_sector + sectors;
  540. conf->last_used = best_disk;
  541. }
  542. rcu_read_unlock();
  543. *max_sectors = sectors;
  544. return best_disk;
  545. }
  546. int md_raid1_congested(struct mddev *mddev, int bits)
  547. {
  548. struct r1conf *conf = mddev->private;
  549. int i, ret = 0;
  550. if ((bits & (1 << BDI_async_congested)) &&
  551. conf->pending_count >= max_queued_requests)
  552. return 1;
  553. rcu_read_lock();
  554. for (i = 0; i < conf->raid_disks * 2; i++) {
  555. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  556. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  557. struct request_queue *q = bdev_get_queue(rdev->bdev);
  558. BUG_ON(!q);
  559. /* Note the '|| 1' - when read_balance prefers
  560. * non-congested targets, it can be removed
  561. */
  562. if ((bits & (1<<BDI_async_congested)) || 1)
  563. ret |= bdi_congested(&q->backing_dev_info, bits);
  564. else
  565. ret &= bdi_congested(&q->backing_dev_info, bits);
  566. }
  567. }
  568. rcu_read_unlock();
  569. return ret;
  570. }
  571. EXPORT_SYMBOL_GPL(md_raid1_congested);
  572. static int raid1_congested(void *data, int bits)
  573. {
  574. struct mddev *mddev = data;
  575. return mddev_congested(mddev, bits) ||
  576. md_raid1_congested(mddev, bits);
  577. }
  578. static void flush_pending_writes(struct r1conf *conf)
  579. {
  580. /* Any writes that have been queued but are awaiting
  581. * bitmap updates get flushed here.
  582. */
  583. spin_lock_irq(&conf->device_lock);
  584. if (conf->pending_bio_list.head) {
  585. struct bio *bio;
  586. bio = bio_list_get(&conf->pending_bio_list);
  587. conf->pending_count = 0;
  588. spin_unlock_irq(&conf->device_lock);
  589. /* flush any pending bitmap writes to
  590. * disk before proceeding w/ I/O */
  591. bitmap_unplug(conf->mddev->bitmap);
  592. wake_up(&conf->wait_barrier);
  593. while (bio) { /* submit pending writes */
  594. struct bio *next = bio->bi_next;
  595. bio->bi_next = NULL;
  596. generic_make_request(bio);
  597. bio = next;
  598. }
  599. } else
  600. spin_unlock_irq(&conf->device_lock);
  601. }
  602. /* Barriers....
  603. * Sometimes we need to suspend IO while we do something else,
  604. * either some resync/recovery, or reconfigure the array.
  605. * To do this we raise a 'barrier'.
  606. * The 'barrier' is a counter that can be raised multiple times
  607. * to count how many activities are happening which preclude
  608. * normal IO.
  609. * We can only raise the barrier if there is no pending IO.
  610. * i.e. if nr_pending == 0.
  611. * We choose only to raise the barrier if no-one is waiting for the
  612. * barrier to go down. This means that as soon as an IO request
  613. * is ready, no other operations which require a barrier will start
  614. * until the IO request has had a chance.
  615. *
  616. * So: regular IO calls 'wait_barrier'. When that returns there
  617. * is no backgroup IO happening, It must arrange to call
  618. * allow_barrier when it has finished its IO.
  619. * backgroup IO calls must call raise_barrier. Once that returns
  620. * there is no normal IO happeing. It must arrange to call
  621. * lower_barrier when the particular background IO completes.
  622. */
  623. #define RESYNC_DEPTH 32
  624. static void raise_barrier(struct r1conf *conf)
  625. {
  626. spin_lock_irq(&conf->resync_lock);
  627. /* Wait until no block IO is waiting */
  628. wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
  629. conf->resync_lock, );
  630. /* block any new IO from starting */
  631. conf->barrier++;
  632. /* Now wait for all pending IO to complete */
  633. wait_event_lock_irq(conf->wait_barrier,
  634. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  635. conf->resync_lock, );
  636. spin_unlock_irq(&conf->resync_lock);
  637. }
  638. static void lower_barrier(struct r1conf *conf)
  639. {
  640. unsigned long flags;
  641. BUG_ON(conf->barrier <= 0);
  642. spin_lock_irqsave(&conf->resync_lock, flags);
  643. conf->barrier--;
  644. spin_unlock_irqrestore(&conf->resync_lock, flags);
  645. wake_up(&conf->wait_barrier);
  646. }
  647. static void wait_barrier(struct r1conf *conf)
  648. {
  649. spin_lock_irq(&conf->resync_lock);
  650. if (conf->barrier) {
  651. conf->nr_waiting++;
  652. /* Wait for the barrier to drop.
  653. * However if there are already pending
  654. * requests (preventing the barrier from
  655. * rising completely), and the
  656. * pre-process bio queue isn't empty,
  657. * then don't wait, as we need to empty
  658. * that queue to get the nr_pending
  659. * count down.
  660. */
  661. wait_event_lock_irq(conf->wait_barrier,
  662. !conf->barrier ||
  663. (conf->nr_pending &&
  664. current->bio_list &&
  665. !bio_list_empty(current->bio_list)),
  666. conf->resync_lock,
  667. );
  668. conf->nr_waiting--;
  669. }
  670. conf->nr_pending++;
  671. spin_unlock_irq(&conf->resync_lock);
  672. }
  673. static void allow_barrier(struct r1conf *conf)
  674. {
  675. unsigned long flags;
  676. spin_lock_irqsave(&conf->resync_lock, flags);
  677. conf->nr_pending--;
  678. spin_unlock_irqrestore(&conf->resync_lock, flags);
  679. wake_up(&conf->wait_barrier);
  680. }
  681. static void freeze_array(struct r1conf *conf)
  682. {
  683. /* stop syncio and normal IO and wait for everything to
  684. * go quite.
  685. * We increment barrier and nr_waiting, and then
  686. * wait until nr_pending match nr_queued+1
  687. * This is called in the context of one normal IO request
  688. * that has failed. Thus any sync request that might be pending
  689. * will be blocked by nr_pending, and we need to wait for
  690. * pending IO requests to complete or be queued for re-try.
  691. * Thus the number queued (nr_queued) plus this request (1)
  692. * must match the number of pending IOs (nr_pending) before
  693. * we continue.
  694. */
  695. spin_lock_irq(&conf->resync_lock);
  696. conf->barrier++;
  697. conf->nr_waiting++;
  698. wait_event_lock_irq(conf->wait_barrier,
  699. conf->nr_pending == conf->nr_queued+1,
  700. conf->resync_lock,
  701. flush_pending_writes(conf));
  702. spin_unlock_irq(&conf->resync_lock);
  703. }
  704. static void unfreeze_array(struct r1conf *conf)
  705. {
  706. /* reverse the effect of the freeze */
  707. spin_lock_irq(&conf->resync_lock);
  708. conf->barrier--;
  709. conf->nr_waiting--;
  710. wake_up(&conf->wait_barrier);
  711. spin_unlock_irq(&conf->resync_lock);
  712. }
  713. /* duplicate the data pages for behind I/O
  714. */
  715. static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
  716. {
  717. int i;
  718. struct bio_vec *bvec;
  719. struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
  720. GFP_NOIO);
  721. if (unlikely(!bvecs))
  722. return;
  723. bio_for_each_segment(bvec, bio, i) {
  724. bvecs[i] = *bvec;
  725. bvecs[i].bv_page = alloc_page(GFP_NOIO);
  726. if (unlikely(!bvecs[i].bv_page))
  727. goto do_sync_io;
  728. memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
  729. kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
  730. kunmap(bvecs[i].bv_page);
  731. kunmap(bvec->bv_page);
  732. }
  733. r1_bio->behind_bvecs = bvecs;
  734. r1_bio->behind_page_count = bio->bi_vcnt;
  735. set_bit(R1BIO_BehindIO, &r1_bio->state);
  736. return;
  737. do_sync_io:
  738. for (i = 0; i < bio->bi_vcnt; i++)
  739. if (bvecs[i].bv_page)
  740. put_page(bvecs[i].bv_page);
  741. kfree(bvecs);
  742. pr_debug("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
  743. }
  744. static void make_request(struct mddev *mddev, struct bio * bio)
  745. {
  746. struct r1conf *conf = mddev->private;
  747. struct mirror_info *mirror;
  748. struct r1bio *r1_bio;
  749. struct bio *read_bio;
  750. int i, disks;
  751. struct bitmap *bitmap;
  752. unsigned long flags;
  753. const int rw = bio_data_dir(bio);
  754. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  755. const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
  756. struct md_rdev *blocked_rdev;
  757. int plugged;
  758. int first_clone;
  759. int sectors_handled;
  760. int max_sectors;
  761. /*
  762. * Register the new request and wait if the reconstruction
  763. * thread has put up a bar for new requests.
  764. * Continue immediately if no resync is active currently.
  765. */
  766. md_write_start(mddev, bio); /* wait on superblock update early */
  767. if (bio_data_dir(bio) == WRITE &&
  768. bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
  769. bio->bi_sector < mddev->suspend_hi) {
  770. /* As the suspend_* range is controlled by
  771. * userspace, we want an interruptible
  772. * wait.
  773. */
  774. DEFINE_WAIT(w);
  775. for (;;) {
  776. flush_signals(current);
  777. prepare_to_wait(&conf->wait_barrier,
  778. &w, TASK_INTERRUPTIBLE);
  779. if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
  780. bio->bi_sector >= mddev->suspend_hi)
  781. break;
  782. schedule();
  783. }
  784. finish_wait(&conf->wait_barrier, &w);
  785. }
  786. wait_barrier(conf);
  787. bitmap = mddev->bitmap;
  788. /*
  789. * make_request() can abort the operation when READA is being
  790. * used and no empty request is available.
  791. *
  792. */
  793. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  794. r1_bio->master_bio = bio;
  795. r1_bio->sectors = bio->bi_size >> 9;
  796. r1_bio->state = 0;
  797. r1_bio->mddev = mddev;
  798. r1_bio->sector = bio->bi_sector;
  799. /* We might need to issue multiple reads to different
  800. * devices if there are bad blocks around, so we keep
  801. * track of the number of reads in bio->bi_phys_segments.
  802. * If this is 0, there is only one r1_bio and no locking
  803. * will be needed when requests complete. If it is
  804. * non-zero, then it is the number of not-completed requests.
  805. */
  806. bio->bi_phys_segments = 0;
  807. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  808. if (rw == READ) {
  809. /*
  810. * read balancing logic:
  811. */
  812. int rdisk;
  813. read_again:
  814. rdisk = read_balance(conf, r1_bio, &max_sectors);
  815. if (rdisk < 0) {
  816. /* couldn't find anywhere to read from */
  817. raid_end_bio_io(r1_bio);
  818. return;
  819. }
  820. mirror = conf->mirrors + rdisk;
  821. if (test_bit(WriteMostly, &mirror->rdev->flags) &&
  822. bitmap) {
  823. /* Reading from a write-mostly device must
  824. * take care not to over-take any writes
  825. * that are 'behind'
  826. */
  827. wait_event(bitmap->behind_wait,
  828. atomic_read(&bitmap->behind_writes) == 0);
  829. }
  830. r1_bio->read_disk = rdisk;
  831. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  832. md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
  833. max_sectors);
  834. r1_bio->bios[rdisk] = read_bio;
  835. read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
  836. read_bio->bi_bdev = mirror->rdev->bdev;
  837. read_bio->bi_end_io = raid1_end_read_request;
  838. read_bio->bi_rw = READ | do_sync;
  839. read_bio->bi_private = r1_bio;
  840. if (max_sectors < r1_bio->sectors) {
  841. /* could not read all from this device, so we will
  842. * need another r1_bio.
  843. */
  844. sectors_handled = (r1_bio->sector + max_sectors
  845. - bio->bi_sector);
  846. r1_bio->sectors = max_sectors;
  847. spin_lock_irq(&conf->device_lock);
  848. if (bio->bi_phys_segments == 0)
  849. bio->bi_phys_segments = 2;
  850. else
  851. bio->bi_phys_segments++;
  852. spin_unlock_irq(&conf->device_lock);
  853. /* Cannot call generic_make_request directly
  854. * as that will be queued in __make_request
  855. * and subsequent mempool_alloc might block waiting
  856. * for it. So hand bio over to raid1d.
  857. */
  858. reschedule_retry(r1_bio);
  859. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  860. r1_bio->master_bio = bio;
  861. r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  862. r1_bio->state = 0;
  863. r1_bio->mddev = mddev;
  864. r1_bio->sector = bio->bi_sector + sectors_handled;
  865. goto read_again;
  866. } else
  867. generic_make_request(read_bio);
  868. return;
  869. }
  870. /*
  871. * WRITE:
  872. */
  873. if (conf->pending_count >= max_queued_requests) {
  874. md_wakeup_thread(mddev->thread);
  875. wait_event(conf->wait_barrier,
  876. conf->pending_count < max_queued_requests);
  877. }
  878. /* first select target devices under rcu_lock and
  879. * inc refcount on their rdev. Record them by setting
  880. * bios[x] to bio
  881. * If there are known/acknowledged bad blocks on any device on
  882. * which we have seen a write error, we want to avoid writing those
  883. * blocks.
  884. * This potentially requires several writes to write around
  885. * the bad blocks. Each set of writes gets it's own r1bio
  886. * with a set of bios attached.
  887. */
  888. plugged = mddev_check_plugged(mddev);
  889. disks = conf->raid_disks * 2;
  890. retry_write:
  891. blocked_rdev = NULL;
  892. rcu_read_lock();
  893. max_sectors = r1_bio->sectors;
  894. for (i = 0; i < disks; i++) {
  895. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  896. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  897. atomic_inc(&rdev->nr_pending);
  898. blocked_rdev = rdev;
  899. break;
  900. }
  901. r1_bio->bios[i] = NULL;
  902. if (!rdev || test_bit(Faulty, &rdev->flags)) {
  903. if (i < conf->raid_disks)
  904. set_bit(R1BIO_Degraded, &r1_bio->state);
  905. continue;
  906. }
  907. atomic_inc(&rdev->nr_pending);
  908. if (test_bit(WriteErrorSeen, &rdev->flags)) {
  909. sector_t first_bad;
  910. int bad_sectors;
  911. int is_bad;
  912. is_bad = is_badblock(rdev, r1_bio->sector,
  913. max_sectors,
  914. &first_bad, &bad_sectors);
  915. if (is_bad < 0) {
  916. /* mustn't write here until the bad block is
  917. * acknowledged*/
  918. set_bit(BlockedBadBlocks, &rdev->flags);
  919. blocked_rdev = rdev;
  920. break;
  921. }
  922. if (is_bad && first_bad <= r1_bio->sector) {
  923. /* Cannot write here at all */
  924. bad_sectors -= (r1_bio->sector - first_bad);
  925. if (bad_sectors < max_sectors)
  926. /* mustn't write more than bad_sectors
  927. * to other devices yet
  928. */
  929. max_sectors = bad_sectors;
  930. rdev_dec_pending(rdev, mddev);
  931. /* We don't set R1BIO_Degraded as that
  932. * only applies if the disk is
  933. * missing, so it might be re-added,
  934. * and we want to know to recover this
  935. * chunk.
  936. * In this case the device is here,
  937. * and the fact that this chunk is not
  938. * in-sync is recorded in the bad
  939. * block log
  940. */
  941. continue;
  942. }
  943. if (is_bad) {
  944. int good_sectors = first_bad - r1_bio->sector;
  945. if (good_sectors < max_sectors)
  946. max_sectors = good_sectors;
  947. }
  948. }
  949. r1_bio->bios[i] = bio;
  950. }
  951. rcu_read_unlock();
  952. if (unlikely(blocked_rdev)) {
  953. /* Wait for this device to become unblocked */
  954. int j;
  955. for (j = 0; j < i; j++)
  956. if (r1_bio->bios[j])
  957. rdev_dec_pending(conf->mirrors[j].rdev, mddev);
  958. r1_bio->state = 0;
  959. allow_barrier(conf);
  960. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  961. wait_barrier(conf);
  962. goto retry_write;
  963. }
  964. if (max_sectors < r1_bio->sectors) {
  965. /* We are splitting this write into multiple parts, so
  966. * we need to prepare for allocating another r1_bio.
  967. */
  968. r1_bio->sectors = max_sectors;
  969. spin_lock_irq(&conf->device_lock);
  970. if (bio->bi_phys_segments == 0)
  971. bio->bi_phys_segments = 2;
  972. else
  973. bio->bi_phys_segments++;
  974. spin_unlock_irq(&conf->device_lock);
  975. }
  976. sectors_handled = r1_bio->sector + max_sectors - bio->bi_sector;
  977. atomic_set(&r1_bio->remaining, 1);
  978. atomic_set(&r1_bio->behind_remaining, 0);
  979. first_clone = 1;
  980. for (i = 0; i < disks; i++) {
  981. struct bio *mbio;
  982. if (!r1_bio->bios[i])
  983. continue;
  984. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  985. md_trim_bio(mbio, r1_bio->sector - bio->bi_sector, max_sectors);
  986. if (first_clone) {
  987. /* do behind I/O ?
  988. * Not if there are too many, or cannot
  989. * allocate memory, or a reader on WriteMostly
  990. * is waiting for behind writes to flush */
  991. if (bitmap &&
  992. (atomic_read(&bitmap->behind_writes)
  993. < mddev->bitmap_info.max_write_behind) &&
  994. !waitqueue_active(&bitmap->behind_wait))
  995. alloc_behind_pages(mbio, r1_bio);
  996. bitmap_startwrite(bitmap, r1_bio->sector,
  997. r1_bio->sectors,
  998. test_bit(R1BIO_BehindIO,
  999. &r1_bio->state));
  1000. first_clone = 0;
  1001. }
  1002. if (r1_bio->behind_bvecs) {
  1003. struct bio_vec *bvec;
  1004. int j;
  1005. /* Yes, I really want the '__' version so that
  1006. * we clear any unused pointer in the io_vec, rather
  1007. * than leave them unchanged. This is important
  1008. * because when we come to free the pages, we won't
  1009. * know the original bi_idx, so we just free
  1010. * them all
  1011. */
  1012. __bio_for_each_segment(bvec, mbio, j, 0)
  1013. bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
  1014. if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
  1015. atomic_inc(&r1_bio->behind_remaining);
  1016. }
  1017. r1_bio->bios[i] = mbio;
  1018. mbio->bi_sector = (r1_bio->sector +
  1019. conf->mirrors[i].rdev->data_offset);
  1020. mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1021. mbio->bi_end_io = raid1_end_write_request;
  1022. mbio->bi_rw = WRITE | do_flush_fua | do_sync;
  1023. mbio->bi_private = r1_bio;
  1024. atomic_inc(&r1_bio->remaining);
  1025. spin_lock_irqsave(&conf->device_lock, flags);
  1026. bio_list_add(&conf->pending_bio_list, mbio);
  1027. conf->pending_count++;
  1028. spin_unlock_irqrestore(&conf->device_lock, flags);
  1029. }
  1030. /* Mustn't call r1_bio_write_done before this next test,
  1031. * as it could result in the bio being freed.
  1032. */
  1033. if (sectors_handled < (bio->bi_size >> 9)) {
  1034. r1_bio_write_done(r1_bio);
  1035. /* We need another r1_bio. It has already been counted
  1036. * in bio->bi_phys_segments
  1037. */
  1038. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1039. r1_bio->master_bio = bio;
  1040. r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  1041. r1_bio->state = 0;
  1042. r1_bio->mddev = mddev;
  1043. r1_bio->sector = bio->bi_sector + sectors_handled;
  1044. goto retry_write;
  1045. }
  1046. r1_bio_write_done(r1_bio);
  1047. /* In case raid1d snuck in to freeze_array */
  1048. wake_up(&conf->wait_barrier);
  1049. if (do_sync || !bitmap || !plugged)
  1050. md_wakeup_thread(mddev->thread);
  1051. }
  1052. static void status(struct seq_file *seq, struct mddev *mddev)
  1053. {
  1054. struct r1conf *conf = mddev->private;
  1055. int i;
  1056. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  1057. conf->raid_disks - mddev->degraded);
  1058. rcu_read_lock();
  1059. for (i = 0; i < conf->raid_disks; i++) {
  1060. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1061. seq_printf(seq, "%s",
  1062. rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  1063. }
  1064. rcu_read_unlock();
  1065. seq_printf(seq, "]");
  1066. }
  1067. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1068. {
  1069. char b[BDEVNAME_SIZE];
  1070. struct r1conf *conf = mddev->private;
  1071. /*
  1072. * If it is not operational, then we have already marked it as dead
  1073. * else if it is the last working disks, ignore the error, let the
  1074. * next level up know.
  1075. * else mark the drive as failed
  1076. */
  1077. if (test_bit(In_sync, &rdev->flags)
  1078. && (conf->raid_disks - mddev->degraded) == 1) {
  1079. /*
  1080. * Don't fail the drive, act as though we were just a
  1081. * normal single drive.
  1082. * However don't try a recovery from this drive as
  1083. * it is very likely to fail.
  1084. */
  1085. conf->recovery_disabled = mddev->recovery_disabled;
  1086. return;
  1087. }
  1088. set_bit(Blocked, &rdev->flags);
  1089. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1090. unsigned long flags;
  1091. spin_lock_irqsave(&conf->device_lock, flags);
  1092. mddev->degraded++;
  1093. set_bit(Faulty, &rdev->flags);
  1094. spin_unlock_irqrestore(&conf->device_lock, flags);
  1095. /*
  1096. * if recovery is running, make sure it aborts.
  1097. */
  1098. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1099. } else
  1100. set_bit(Faulty, &rdev->flags);
  1101. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1102. printk(KERN_ALERT
  1103. "md/raid1:%s: Disk failure on %s, disabling device.\n"
  1104. "md/raid1:%s: Operation continuing on %d devices.\n",
  1105. mdname(mddev), bdevname(rdev->bdev, b),
  1106. mdname(mddev), conf->raid_disks - mddev->degraded);
  1107. }
  1108. static void print_conf(struct r1conf *conf)
  1109. {
  1110. int i;
  1111. printk(KERN_DEBUG "RAID1 conf printout:\n");
  1112. if (!conf) {
  1113. printk(KERN_DEBUG "(!conf)\n");
  1114. return;
  1115. }
  1116. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  1117. conf->raid_disks);
  1118. rcu_read_lock();
  1119. for (i = 0; i < conf->raid_disks; i++) {
  1120. char b[BDEVNAME_SIZE];
  1121. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1122. if (rdev)
  1123. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1124. i, !test_bit(In_sync, &rdev->flags),
  1125. !test_bit(Faulty, &rdev->flags),
  1126. bdevname(rdev->bdev,b));
  1127. }
  1128. rcu_read_unlock();
  1129. }
  1130. static void close_sync(struct r1conf *conf)
  1131. {
  1132. wait_barrier(conf);
  1133. allow_barrier(conf);
  1134. mempool_destroy(conf->r1buf_pool);
  1135. conf->r1buf_pool = NULL;
  1136. }
  1137. static int raid1_spare_active(struct mddev *mddev)
  1138. {
  1139. int i;
  1140. struct r1conf *conf = mddev->private;
  1141. int count = 0;
  1142. unsigned long flags;
  1143. /*
  1144. * Find all failed disks within the RAID1 configuration
  1145. * and mark them readable.
  1146. * Called under mddev lock, so rcu protection not needed.
  1147. */
  1148. for (i = 0; i < conf->raid_disks; i++) {
  1149. struct md_rdev *rdev = conf->mirrors[i].rdev;
  1150. struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
  1151. if (repl
  1152. && repl->recovery_offset == MaxSector
  1153. && !test_bit(Faulty, &repl->flags)
  1154. && !test_and_set_bit(In_sync, &repl->flags)) {
  1155. /* replacement has just become active */
  1156. if (!rdev ||
  1157. !test_and_clear_bit(In_sync, &rdev->flags))
  1158. count++;
  1159. if (rdev) {
  1160. /* Replaced device not technically
  1161. * faulty, but we need to be sure
  1162. * it gets removed and never re-added
  1163. */
  1164. set_bit(Faulty, &rdev->flags);
  1165. sysfs_notify_dirent_safe(
  1166. rdev->sysfs_state);
  1167. }
  1168. }
  1169. if (rdev
  1170. && !test_bit(Faulty, &rdev->flags)
  1171. && !test_and_set_bit(In_sync, &rdev->flags)) {
  1172. count++;
  1173. sysfs_notify_dirent_safe(rdev->sysfs_state);
  1174. }
  1175. }
  1176. spin_lock_irqsave(&conf->device_lock, flags);
  1177. mddev->degraded -= count;
  1178. spin_unlock_irqrestore(&conf->device_lock, flags);
  1179. print_conf(conf);
  1180. return count;
  1181. }
  1182. static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1183. {
  1184. struct r1conf *conf = mddev->private;
  1185. int err = -EEXIST;
  1186. int mirror = 0;
  1187. struct mirror_info *p;
  1188. int first = 0;
  1189. int last = conf->raid_disks - 1;
  1190. if (mddev->recovery_disabled == conf->recovery_disabled)
  1191. return -EBUSY;
  1192. if (rdev->raid_disk >= 0)
  1193. first = last = rdev->raid_disk;
  1194. for (mirror = first; mirror <= last; mirror++) {
  1195. p = conf->mirrors+mirror;
  1196. if (!p->rdev) {
  1197. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1198. rdev->data_offset << 9);
  1199. /* as we don't honour merge_bvec_fn, we must
  1200. * never risk violating it, so limit
  1201. * ->max_segments to one lying with a single
  1202. * page, as a one page request is never in
  1203. * violation.
  1204. */
  1205. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  1206. blk_queue_max_segments(mddev->queue, 1);
  1207. blk_queue_segment_boundary(mddev->queue,
  1208. PAGE_CACHE_SIZE - 1);
  1209. }
  1210. p->head_position = 0;
  1211. rdev->raid_disk = mirror;
  1212. err = 0;
  1213. /* As all devices are equivalent, we don't need a full recovery
  1214. * if this was recently any drive of the array
  1215. */
  1216. if (rdev->saved_raid_disk < 0)
  1217. conf->fullsync = 1;
  1218. rcu_assign_pointer(p->rdev, rdev);
  1219. break;
  1220. }
  1221. if (test_bit(WantReplacement, &p->rdev->flags) &&
  1222. p[conf->raid_disks].rdev == NULL) {
  1223. /* Add this device as a replacement */
  1224. clear_bit(In_sync, &rdev->flags);
  1225. set_bit(Replacement, &rdev->flags);
  1226. rdev->raid_disk = mirror;
  1227. err = 0;
  1228. conf->fullsync = 1;
  1229. rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
  1230. break;
  1231. }
  1232. }
  1233. md_integrity_add_rdev(rdev, mddev);
  1234. print_conf(conf);
  1235. return err;
  1236. }
  1237. static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1238. {
  1239. struct r1conf *conf = mddev->private;
  1240. int err = 0;
  1241. int number = rdev->raid_disk;
  1242. struct mirror_info *p = conf->mirrors+ number;
  1243. if (rdev != p->rdev)
  1244. p = conf->mirrors + conf->raid_disks + number;
  1245. print_conf(conf);
  1246. if (rdev == p->rdev) {
  1247. if (test_bit(In_sync, &rdev->flags) ||
  1248. atomic_read(&rdev->nr_pending)) {
  1249. err = -EBUSY;
  1250. goto abort;
  1251. }
  1252. /* Only remove non-faulty devices if recovery
  1253. * is not possible.
  1254. */
  1255. if (!test_bit(Faulty, &rdev->flags) &&
  1256. mddev->recovery_disabled != conf->recovery_disabled &&
  1257. mddev->degraded < conf->raid_disks) {
  1258. err = -EBUSY;
  1259. goto abort;
  1260. }
  1261. p->rdev = NULL;
  1262. synchronize_rcu();
  1263. if (atomic_read(&rdev->nr_pending)) {
  1264. /* lost the race, try later */
  1265. err = -EBUSY;
  1266. p->rdev = rdev;
  1267. goto abort;
  1268. } else if (conf->mirrors[conf->raid_disks + number].rdev) {
  1269. /* We just removed a device that is being replaced.
  1270. * Move down the replacement. We drain all IO before
  1271. * doing this to avoid confusion.
  1272. */
  1273. struct md_rdev *repl =
  1274. conf->mirrors[conf->raid_disks + number].rdev;
  1275. raise_barrier(conf);
  1276. clear_bit(Replacement, &repl->flags);
  1277. p->rdev = repl;
  1278. conf->mirrors[conf->raid_disks + number].rdev = NULL;
  1279. lower_barrier(conf);
  1280. clear_bit(WantReplacement, &rdev->flags);
  1281. } else
  1282. clear_bit(WantReplacement, &rdev->flags);
  1283. err = md_integrity_register(mddev);
  1284. }
  1285. abort:
  1286. print_conf(conf);
  1287. return err;
  1288. }
  1289. static void end_sync_read(struct bio *bio, int error)
  1290. {
  1291. struct r1bio *r1_bio = bio->bi_private;
  1292. update_head_pos(r1_bio->read_disk, r1_bio);
  1293. /*
  1294. * we have read a block, now it needs to be re-written,
  1295. * or re-read if the read failed.
  1296. * We don't do much here, just schedule handling by raid1d
  1297. */
  1298. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1299. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1300. if (atomic_dec_and_test(&r1_bio->remaining))
  1301. reschedule_retry(r1_bio);
  1302. }
  1303. static void end_sync_write(struct bio *bio, int error)
  1304. {
  1305. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1306. struct r1bio *r1_bio = bio->bi_private;
  1307. struct mddev *mddev = r1_bio->mddev;
  1308. struct r1conf *conf = mddev->private;
  1309. int mirror=0;
  1310. sector_t first_bad;
  1311. int bad_sectors;
  1312. mirror = find_bio_disk(r1_bio, bio);
  1313. if (!uptodate) {
  1314. sector_t sync_blocks = 0;
  1315. sector_t s = r1_bio->sector;
  1316. long sectors_to_go = r1_bio->sectors;
  1317. /* make sure these bits doesn't get cleared. */
  1318. do {
  1319. bitmap_end_sync(mddev->bitmap, s,
  1320. &sync_blocks, 1);
  1321. s += sync_blocks;
  1322. sectors_to_go -= sync_blocks;
  1323. } while (sectors_to_go > 0);
  1324. set_bit(WriteErrorSeen,
  1325. &conf->mirrors[mirror].rdev->flags);
  1326. if (!test_and_set_bit(WantReplacement,
  1327. &conf->mirrors[mirror].rdev->flags))
  1328. set_bit(MD_RECOVERY_NEEDED, &
  1329. mddev->recovery);
  1330. set_bit(R1BIO_WriteError, &r1_bio->state);
  1331. } else if (is_badblock(conf->mirrors[mirror].rdev,
  1332. r1_bio->sector,
  1333. r1_bio->sectors,
  1334. &first_bad, &bad_sectors) &&
  1335. !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
  1336. r1_bio->sector,
  1337. r1_bio->sectors,
  1338. &first_bad, &bad_sectors)
  1339. )
  1340. set_bit(R1BIO_MadeGood, &r1_bio->state);
  1341. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1342. int s = r1_bio->sectors;
  1343. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1344. test_bit(R1BIO_WriteError, &r1_bio->state))
  1345. reschedule_retry(r1_bio);
  1346. else {
  1347. put_buf(r1_bio);
  1348. md_done_sync(mddev, s, uptodate);
  1349. }
  1350. }
  1351. }
  1352. static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1353. int sectors, struct page *page, int rw)
  1354. {
  1355. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1356. /* success */
  1357. return 1;
  1358. if (rw == WRITE) {
  1359. set_bit(WriteErrorSeen, &rdev->flags);
  1360. if (!test_and_set_bit(WantReplacement,
  1361. &rdev->flags))
  1362. set_bit(MD_RECOVERY_NEEDED, &
  1363. rdev->mddev->recovery);
  1364. }
  1365. /* need to record an error - either for the block or the device */
  1366. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1367. md_error(rdev->mddev, rdev);
  1368. return 0;
  1369. }
  1370. static int fix_sync_read_error(struct r1bio *r1_bio)
  1371. {
  1372. /* Try some synchronous reads of other devices to get
  1373. * good data, much like with normal read errors. Only
  1374. * read into the pages we already have so we don't
  1375. * need to re-issue the read request.
  1376. * We don't need to freeze the array, because being in an
  1377. * active sync request, there is no normal IO, and
  1378. * no overlapping syncs.
  1379. * We don't need to check is_badblock() again as we
  1380. * made sure that anything with a bad block in range
  1381. * will have bi_end_io clear.
  1382. */
  1383. struct mddev *mddev = r1_bio->mddev;
  1384. struct r1conf *conf = mddev->private;
  1385. struct bio *bio = r1_bio->bios[r1_bio->read_disk];
  1386. sector_t sect = r1_bio->sector;
  1387. int sectors = r1_bio->sectors;
  1388. int idx = 0;
  1389. while(sectors) {
  1390. int s = sectors;
  1391. int d = r1_bio->read_disk;
  1392. int success = 0;
  1393. struct md_rdev *rdev;
  1394. int start;
  1395. if (s > (PAGE_SIZE>>9))
  1396. s = PAGE_SIZE >> 9;
  1397. do {
  1398. if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
  1399. /* No rcu protection needed here devices
  1400. * can only be removed when no resync is
  1401. * active, and resync is currently active
  1402. */
  1403. rdev = conf->mirrors[d].rdev;
  1404. if (sync_page_io(rdev, sect, s<<9,
  1405. bio->bi_io_vec[idx].bv_page,
  1406. READ, false)) {
  1407. success = 1;
  1408. break;
  1409. }
  1410. }
  1411. d++;
  1412. if (d == conf->raid_disks * 2)
  1413. d = 0;
  1414. } while (!success && d != r1_bio->read_disk);
  1415. if (!success) {
  1416. char b[BDEVNAME_SIZE];
  1417. int abort = 0;
  1418. /* Cannot read from anywhere, this block is lost.
  1419. * Record a bad block on each device. If that doesn't
  1420. * work just disable and interrupt the recovery.
  1421. * Don't fail devices as that won't really help.
  1422. */
  1423. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
  1424. " for block %llu\n",
  1425. mdname(mddev),
  1426. bdevname(bio->bi_bdev, b),
  1427. (unsigned long long)r1_bio->sector);
  1428. for (d = 0; d < conf->raid_disks * 2; d++) {
  1429. rdev = conf->mirrors[d].rdev;
  1430. if (!rdev || test_bit(Faulty, &rdev->flags))
  1431. continue;
  1432. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1433. abort = 1;
  1434. }
  1435. if (abort) {
  1436. conf->recovery_disabled =
  1437. mddev->recovery_disabled;
  1438. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1439. md_done_sync(mddev, r1_bio->sectors, 0);
  1440. put_buf(r1_bio);
  1441. return 0;
  1442. }
  1443. /* Try next page */
  1444. sectors -= s;
  1445. sect += s;
  1446. idx++;
  1447. continue;
  1448. }
  1449. start = d;
  1450. /* write it back and re-read */
  1451. while (d != r1_bio->read_disk) {
  1452. if (d == 0)
  1453. d = conf->raid_disks * 2;
  1454. d--;
  1455. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1456. continue;
  1457. rdev = conf->mirrors[d].rdev;
  1458. if (r1_sync_page_io(rdev, sect, s,
  1459. bio->bi_io_vec[idx].bv_page,
  1460. WRITE) == 0) {
  1461. r1_bio->bios[d]->bi_end_io = NULL;
  1462. rdev_dec_pending(rdev, mddev);
  1463. }
  1464. }
  1465. d = start;
  1466. while (d != r1_bio->read_disk) {
  1467. if (d == 0)
  1468. d = conf->raid_disks * 2;
  1469. d--;
  1470. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1471. continue;
  1472. rdev = conf->mirrors[d].rdev;
  1473. if (r1_sync_page_io(rdev, sect, s,
  1474. bio->bi_io_vec[idx].bv_page,
  1475. READ) != 0)
  1476. atomic_add(s, &rdev->corrected_errors);
  1477. }
  1478. sectors -= s;
  1479. sect += s;
  1480. idx ++;
  1481. }
  1482. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1483. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1484. return 1;
  1485. }
  1486. static int process_checks(struct r1bio *r1_bio)
  1487. {
  1488. /* We have read all readable devices. If we haven't
  1489. * got the block, then there is no hope left.
  1490. * If we have, then we want to do a comparison
  1491. * and skip the write if everything is the same.
  1492. * If any blocks failed to read, then we need to
  1493. * attempt an over-write
  1494. */
  1495. struct mddev *mddev = r1_bio->mddev;
  1496. struct r1conf *conf = mddev->private;
  1497. int primary;
  1498. int i;
  1499. for (primary = 0; primary < conf->raid_disks * 2; primary++)
  1500. if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
  1501. test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
  1502. r1_bio->bios[primary]->bi_end_io = NULL;
  1503. rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
  1504. break;
  1505. }
  1506. r1_bio->read_disk = primary;
  1507. for (i = 0; i < conf->raid_disks * 2; i++) {
  1508. int j;
  1509. int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
  1510. struct bio *pbio = r1_bio->bios[primary];
  1511. struct bio *sbio = r1_bio->bios[i];
  1512. int size;
  1513. if (r1_bio->bios[i]->bi_end_io != end_sync_read)
  1514. continue;
  1515. if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
  1516. for (j = vcnt; j-- ; ) {
  1517. struct page *p, *s;
  1518. p = pbio->bi_io_vec[j].bv_page;
  1519. s = sbio->bi_io_vec[j].bv_page;
  1520. if (memcmp(page_address(p),
  1521. page_address(s),
  1522. PAGE_SIZE))
  1523. break;
  1524. }
  1525. } else
  1526. j = 0;
  1527. if (j >= 0)
  1528. mddev->resync_mismatches += r1_bio->sectors;
  1529. if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
  1530. && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
  1531. /* No need to write to this device. */
  1532. sbio->bi_end_io = NULL;
  1533. rdev_dec_pending(conf->mirrors[i].rdev, mddev);
  1534. continue;
  1535. }
  1536. /* fixup the bio for reuse */
  1537. sbio->bi_vcnt = vcnt;
  1538. sbio->bi_size = r1_bio->sectors << 9;
  1539. sbio->bi_idx = 0;
  1540. sbio->bi_phys_segments = 0;
  1541. sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1542. sbio->bi_flags |= 1 << BIO_UPTODATE;
  1543. sbio->bi_next = NULL;
  1544. sbio->bi_sector = r1_bio->sector +
  1545. conf->mirrors[i].rdev->data_offset;
  1546. sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1547. size = sbio->bi_size;
  1548. for (j = 0; j < vcnt ; j++) {
  1549. struct bio_vec *bi;
  1550. bi = &sbio->bi_io_vec[j];
  1551. bi->bv_offset = 0;
  1552. if (size > PAGE_SIZE)
  1553. bi->bv_len = PAGE_SIZE;
  1554. else
  1555. bi->bv_len = size;
  1556. size -= PAGE_SIZE;
  1557. memcpy(page_address(bi->bv_page),
  1558. page_address(pbio->bi_io_vec[j].bv_page),
  1559. PAGE_SIZE);
  1560. }
  1561. }
  1562. return 0;
  1563. }
  1564. static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
  1565. {
  1566. struct r1conf *conf = mddev->private;
  1567. int i;
  1568. int disks = conf->raid_disks * 2;
  1569. struct bio *bio, *wbio;
  1570. bio = r1_bio->bios[r1_bio->read_disk];
  1571. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  1572. /* ouch - failed to read all of that. */
  1573. if (!fix_sync_read_error(r1_bio))
  1574. return;
  1575. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1576. if (process_checks(r1_bio) < 0)
  1577. return;
  1578. /*
  1579. * schedule writes
  1580. */
  1581. atomic_set(&r1_bio->remaining, 1);
  1582. for (i = 0; i < disks ; i++) {
  1583. wbio = r1_bio->bios[i];
  1584. if (wbio->bi_end_io == NULL ||
  1585. (wbio->bi_end_io == end_sync_read &&
  1586. (i == r1_bio->read_disk ||
  1587. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  1588. continue;
  1589. wbio->bi_rw = WRITE;
  1590. wbio->bi_end_io = end_sync_write;
  1591. atomic_inc(&r1_bio->remaining);
  1592. md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
  1593. generic_make_request(wbio);
  1594. }
  1595. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1596. /* if we're here, all write(s) have completed, so clean up */
  1597. md_done_sync(mddev, r1_bio->sectors, 1);
  1598. put_buf(r1_bio);
  1599. }
  1600. }
  1601. /*
  1602. * This is a kernel thread which:
  1603. *
  1604. * 1. Retries failed read operations on working mirrors.
  1605. * 2. Updates the raid superblock when problems encounter.
  1606. * 3. Performs writes following reads for array synchronising.
  1607. */
  1608. static void fix_read_error(struct r1conf *conf, int read_disk,
  1609. sector_t sect, int sectors)
  1610. {
  1611. struct mddev *mddev = conf->mddev;
  1612. while(sectors) {
  1613. int s = sectors;
  1614. int d = read_disk;
  1615. int success = 0;
  1616. int start;
  1617. struct md_rdev *rdev;
  1618. if (s > (PAGE_SIZE>>9))
  1619. s = PAGE_SIZE >> 9;
  1620. do {
  1621. /* Note: no rcu protection needed here
  1622. * as this is synchronous in the raid1d thread
  1623. * which is the thread that might remove
  1624. * a device. If raid1d ever becomes multi-threaded....
  1625. */
  1626. sector_t first_bad;
  1627. int bad_sectors;
  1628. rdev = conf->mirrors[d].rdev;
  1629. if (rdev &&
  1630. test_bit(In_sync, &rdev->flags) &&
  1631. is_badblock(rdev, sect, s,
  1632. &first_bad, &bad_sectors) == 0 &&
  1633. sync_page_io(rdev, sect, s<<9,
  1634. conf->tmppage, READ, false))
  1635. success = 1;
  1636. else {
  1637. d++;
  1638. if (d == conf->raid_disks * 2)
  1639. d = 0;
  1640. }
  1641. } while (!success && d != read_disk);
  1642. if (!success) {
  1643. /* Cannot read from anywhere - mark it bad */
  1644. struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
  1645. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1646. md_error(mddev, rdev);
  1647. break;
  1648. }
  1649. /* write it back and re-read */
  1650. start = d;
  1651. while (d != read_disk) {
  1652. if (d==0)
  1653. d = conf->raid_disks * 2;
  1654. d--;
  1655. rdev = conf->mirrors[d].rdev;
  1656. if (rdev &&
  1657. test_bit(In_sync, &rdev->flags))
  1658. r1_sync_page_io(rdev, sect, s,
  1659. conf->tmppage, WRITE);
  1660. }
  1661. d = start;
  1662. while (d != read_disk) {
  1663. char b[BDEVNAME_SIZE];
  1664. if (d==0)
  1665. d = conf->raid_disks * 2;
  1666. d--;
  1667. rdev = conf->mirrors[d].rdev;
  1668. if (rdev &&
  1669. test_bit(In_sync, &rdev->flags)) {
  1670. if (r1_sync_page_io(rdev, sect, s,
  1671. conf->tmppage, READ)) {
  1672. atomic_add(s, &rdev->corrected_errors);
  1673. printk(KERN_INFO
  1674. "md/raid1:%s: read error corrected "
  1675. "(%d sectors at %llu on %s)\n",
  1676. mdname(mddev), s,
  1677. (unsigned long long)(sect +
  1678. rdev->data_offset),
  1679. bdevname(rdev->bdev, b));
  1680. }
  1681. }
  1682. }
  1683. sectors -= s;
  1684. sect += s;
  1685. }
  1686. }
  1687. static void bi_complete(struct bio *bio, int error)
  1688. {
  1689. complete((struct completion *)bio->bi_private);
  1690. }
  1691. static int submit_bio_wait(int rw, struct bio *bio)
  1692. {
  1693. struct completion event;
  1694. rw |= REQ_SYNC;
  1695. init_completion(&event);
  1696. bio->bi_private = &event;
  1697. bio->bi_end_io = bi_complete;
  1698. submit_bio(rw, bio);
  1699. wait_for_completion(&event);
  1700. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  1701. }
  1702. static int narrow_write_error(struct r1bio *r1_bio, int i)
  1703. {
  1704. struct mddev *mddev = r1_bio->mddev;
  1705. struct r1conf *conf = mddev->private;
  1706. struct md_rdev *rdev = conf->mirrors[i].rdev;
  1707. int vcnt, idx;
  1708. struct bio_vec *vec;
  1709. /* bio has the data to be written to device 'i' where
  1710. * we just recently had a write error.
  1711. * We repeatedly clone the bio and trim down to one block,
  1712. * then try the write. Where the write fails we record
  1713. * a bad block.
  1714. * It is conceivable that the bio doesn't exactly align with
  1715. * blocks. We must handle this somehow.
  1716. *
  1717. * We currently own a reference on the rdev.
  1718. */
  1719. int block_sectors;
  1720. sector_t sector;
  1721. int sectors;
  1722. int sect_to_write = r1_bio->sectors;
  1723. int ok = 1;
  1724. if (rdev->badblocks.shift < 0)
  1725. return 0;
  1726. block_sectors = 1 << rdev->badblocks.shift;
  1727. sector = r1_bio->sector;
  1728. sectors = ((sector + block_sectors)
  1729. & ~(sector_t)(block_sectors - 1))
  1730. - sector;
  1731. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  1732. vcnt = r1_bio->behind_page_count;
  1733. vec = r1_bio->behind_bvecs;
  1734. idx = 0;
  1735. while (vec[idx].bv_page == NULL)
  1736. idx++;
  1737. } else {
  1738. vcnt = r1_bio->master_bio->bi_vcnt;
  1739. vec = r1_bio->master_bio->bi_io_vec;
  1740. idx = r1_bio->master_bio->bi_idx;
  1741. }
  1742. while (sect_to_write) {
  1743. struct bio *wbio;
  1744. if (sectors > sect_to_write)
  1745. sectors = sect_to_write;
  1746. /* Write at 'sector' for 'sectors'*/
  1747. wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
  1748. memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
  1749. wbio->bi_sector = r1_bio->sector;
  1750. wbio->bi_rw = WRITE;
  1751. wbio->bi_vcnt = vcnt;
  1752. wbio->bi_size = r1_bio->sectors << 9;
  1753. wbio->bi_idx = idx;
  1754. md_trim_bio(wbio, sector - r1_bio->sector, sectors);
  1755. wbio->bi_sector += rdev->data_offset;
  1756. wbio->bi_bdev = rdev->bdev;
  1757. if (submit_bio_wait(WRITE, wbio) == 0)
  1758. /* failure! */
  1759. ok = rdev_set_badblocks(rdev, sector,
  1760. sectors, 0)
  1761. && ok;
  1762. bio_put(wbio);
  1763. sect_to_write -= sectors;
  1764. sector += sectors;
  1765. sectors = block_sectors;
  1766. }
  1767. return ok;
  1768. }
  1769. static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
  1770. {
  1771. int m;
  1772. int s = r1_bio->sectors;
  1773. for (m = 0; m < conf->raid_disks * 2 ; m++) {
  1774. struct md_rdev *rdev = conf->mirrors[m].rdev;
  1775. struct bio *bio = r1_bio->bios[m];
  1776. if (bio->bi_end_io == NULL)
  1777. continue;
  1778. if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
  1779. test_bit(R1BIO_MadeGood, &r1_bio->state)) {
  1780. rdev_clear_badblocks(rdev, r1_bio->sector, s);
  1781. }
  1782. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
  1783. test_bit(R1BIO_WriteError, &r1_bio->state)) {
  1784. if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
  1785. md_error(conf->mddev, rdev);
  1786. }
  1787. }
  1788. put_buf(r1_bio);
  1789. md_done_sync(conf->mddev, s, 1);
  1790. }
  1791. static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
  1792. {
  1793. int m;
  1794. for (m = 0; m < conf->raid_disks * 2 ; m++)
  1795. if (r1_bio->bios[m] == IO_MADE_GOOD) {
  1796. struct md_rdev *rdev = conf->mirrors[m].rdev;
  1797. rdev_clear_badblocks(rdev,
  1798. r1_bio->sector,
  1799. r1_bio->sectors);
  1800. rdev_dec_pending(rdev, conf->mddev);
  1801. } else if (r1_bio->bios[m] != NULL) {
  1802. /* This drive got a write error. We need to
  1803. * narrow down and record precise write
  1804. * errors.
  1805. */
  1806. if (!narrow_write_error(r1_bio, m)) {
  1807. md_error(conf->mddev,
  1808. conf->mirrors[m].rdev);
  1809. /* an I/O failed, we can't clear the bitmap */
  1810. set_bit(R1BIO_Degraded, &r1_bio->state);
  1811. }
  1812. rdev_dec_pending(conf->mirrors[m].rdev,
  1813. conf->mddev);
  1814. }
  1815. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  1816. close_write(r1_bio);
  1817. raid_end_bio_io(r1_bio);
  1818. }
  1819. static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
  1820. {
  1821. int disk;
  1822. int max_sectors;
  1823. struct mddev *mddev = conf->mddev;
  1824. struct bio *bio;
  1825. char b[BDEVNAME_SIZE];
  1826. struct md_rdev *rdev;
  1827. clear_bit(R1BIO_ReadError, &r1_bio->state);
  1828. /* we got a read error. Maybe the drive is bad. Maybe just
  1829. * the block and we can fix it.
  1830. * We freeze all other IO, and try reading the block from
  1831. * other devices. When we find one, we re-write
  1832. * and check it that fixes the read error.
  1833. * This is all done synchronously while the array is
  1834. * frozen
  1835. */
  1836. if (mddev->ro == 0) {
  1837. freeze_array(conf);
  1838. fix_read_error(conf, r1_bio->read_disk,
  1839. r1_bio->sector, r1_bio->sectors);
  1840. unfreeze_array(conf);
  1841. } else
  1842. md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
  1843. bio = r1_bio->bios[r1_bio->read_disk];
  1844. bdevname(bio->bi_bdev, b);
  1845. read_more:
  1846. disk = read_balance(conf, r1_bio, &max_sectors);
  1847. if (disk == -1) {
  1848. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
  1849. " read error for block %llu\n",
  1850. mdname(mddev), b, (unsigned long long)r1_bio->sector);
  1851. raid_end_bio_io(r1_bio);
  1852. } else {
  1853. const unsigned long do_sync
  1854. = r1_bio->master_bio->bi_rw & REQ_SYNC;
  1855. if (bio) {
  1856. r1_bio->bios[r1_bio->read_disk] =
  1857. mddev->ro ? IO_BLOCKED : NULL;
  1858. bio_put(bio);
  1859. }
  1860. r1_bio->read_disk = disk;
  1861. bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
  1862. md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
  1863. r1_bio->bios[r1_bio->read_disk] = bio;
  1864. rdev = conf->mirrors[disk].rdev;
  1865. printk_ratelimited(KERN_ERR
  1866. "md/raid1:%s: redirecting sector %llu"
  1867. " to other mirror: %s\n",
  1868. mdname(mddev),
  1869. (unsigned long long)r1_bio->sector,
  1870. bdevname(rdev->bdev, b));
  1871. bio->bi_sector = r1_bio->sector + rdev->data_offset;
  1872. bio->bi_bdev = rdev->bdev;
  1873. bio->bi_end_io = raid1_end_read_request;
  1874. bio->bi_rw = READ | do_sync;
  1875. bio->bi_private = r1_bio;
  1876. if (max_sectors < r1_bio->sectors) {
  1877. /* Drat - have to split this up more */
  1878. struct bio *mbio = r1_bio->master_bio;
  1879. int sectors_handled = (r1_bio->sector + max_sectors
  1880. - mbio->bi_sector);
  1881. r1_bio->sectors = max_sectors;
  1882. spin_lock_irq(&conf->device_lock);
  1883. if (mbio->bi_phys_segments == 0)
  1884. mbio->bi_phys_segments = 2;
  1885. else
  1886. mbio->bi_phys_segments++;
  1887. spin_unlock_irq(&conf->device_lock);
  1888. generic_make_request(bio);
  1889. bio = NULL;
  1890. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1891. r1_bio->master_bio = mbio;
  1892. r1_bio->sectors = (mbio->bi_size >> 9)
  1893. - sectors_handled;
  1894. r1_bio->state = 0;
  1895. set_bit(R1BIO_ReadError, &r1_bio->state);
  1896. r1_bio->mddev = mddev;
  1897. r1_bio->sector = mbio->bi_sector + sectors_handled;
  1898. goto read_more;
  1899. } else
  1900. generic_make_request(bio);
  1901. }
  1902. }
  1903. static void raid1d(struct mddev *mddev)
  1904. {
  1905. struct r1bio *r1_bio;
  1906. unsigned long flags;
  1907. struct r1conf *conf = mddev->private;
  1908. struct list_head *head = &conf->retry_list;
  1909. struct blk_plug plug;
  1910. md_check_recovery(mddev);
  1911. blk_start_plug(&plug);
  1912. for (;;) {
  1913. if (atomic_read(&mddev->plug_cnt) == 0)
  1914. flush_pending_writes(conf);
  1915. spin_lock_irqsave(&conf->device_lock, flags);
  1916. if (list_empty(head)) {
  1917. spin_unlock_irqrestore(&conf->device_lock, flags);
  1918. break;
  1919. }
  1920. r1_bio = list_entry(head->prev, struct r1bio, retry_list);
  1921. list_del(head->prev);
  1922. conf->nr_queued--;
  1923. spin_unlock_irqrestore(&conf->device_lock, flags);
  1924. mddev = r1_bio->mddev;
  1925. conf = mddev->private;
  1926. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  1927. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1928. test_bit(R1BIO_WriteError, &r1_bio->state))
  1929. handle_sync_write_finished(conf, r1_bio);
  1930. else
  1931. sync_request_write(mddev, r1_bio);
  1932. } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1933. test_bit(R1BIO_WriteError, &r1_bio->state))
  1934. handle_write_finished(conf, r1_bio);
  1935. else if (test_bit(R1BIO_ReadError, &r1_bio->state))
  1936. handle_read_error(conf, r1_bio);
  1937. else
  1938. /* just a partial read to be scheduled from separate
  1939. * context
  1940. */
  1941. generic_make_request(r1_bio->bios[r1_bio->read_disk]);
  1942. cond_resched();
  1943. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  1944. md_check_recovery(mddev);
  1945. }
  1946. blk_finish_plug(&plug);
  1947. }
  1948. static int init_resync(struct r1conf *conf)
  1949. {
  1950. int buffs;
  1951. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1952. BUG_ON(conf->r1buf_pool);
  1953. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  1954. conf->poolinfo);
  1955. if (!conf->r1buf_pool)
  1956. return -ENOMEM;
  1957. conf->next_resync = 0;
  1958. return 0;
  1959. }
  1960. /*
  1961. * perform a "sync" on one "block"
  1962. *
  1963. * We need to make sure that no normal I/O request - particularly write
  1964. * requests - conflict with active sync requests.
  1965. *
  1966. * This is achieved by tracking pending requests and a 'barrier' concept
  1967. * that can be installed to exclude normal IO requests.
  1968. */
  1969. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1970. {
  1971. struct r1conf *conf = mddev->private;
  1972. struct r1bio *r1_bio;
  1973. struct bio *bio;
  1974. sector_t max_sector, nr_sectors;
  1975. int disk = -1;
  1976. int i;
  1977. int wonly = -1;
  1978. int write_targets = 0, read_targets = 0;
  1979. sector_t sync_blocks;
  1980. int still_degraded = 0;
  1981. int good_sectors = RESYNC_SECTORS;
  1982. int min_bad = 0; /* number of sectors that are bad in all devices */
  1983. if (!conf->r1buf_pool)
  1984. if (init_resync(conf))
  1985. return 0;
  1986. max_sector = mddev->dev_sectors;
  1987. if (sector_nr >= max_sector) {
  1988. /* If we aborted, we need to abort the
  1989. * sync on the 'current' bitmap chunk (there will
  1990. * only be one in raid1 resync.
  1991. * We can find the current addess in mddev->curr_resync
  1992. */
  1993. if (mddev->curr_resync < max_sector) /* aborted */
  1994. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1995. &sync_blocks, 1);
  1996. else /* completed sync */
  1997. conf->fullsync = 0;
  1998. bitmap_close_sync(mddev->bitmap);
  1999. close_sync(conf);
  2000. return 0;
  2001. }
  2002. if (mddev->bitmap == NULL &&
  2003. mddev->recovery_cp == MaxSector &&
  2004. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2005. conf->fullsync == 0) {
  2006. *skipped = 1;
  2007. return max_sector - sector_nr;
  2008. }
  2009. /* before building a request, check if we can skip these blocks..
  2010. * This call the bitmap_start_sync doesn't actually record anything
  2011. */
  2012. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2013. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2014. /* We can skip this block, and probably several more */
  2015. *skipped = 1;
  2016. return sync_blocks;
  2017. }
  2018. /*
  2019. * If there is non-resync activity waiting for a turn,
  2020. * and resync is going fast enough,
  2021. * then let it though before starting on this new sync request.
  2022. */
  2023. if (!go_faster && conf->nr_waiting)
  2024. msleep_interruptible(1000);
  2025. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2026. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  2027. raise_barrier(conf);
  2028. conf->next_resync = sector_nr;
  2029. rcu_read_lock();
  2030. /*
  2031. * If we get a correctably read error during resync or recovery,
  2032. * we might want to read from a different device. So we
  2033. * flag all drives that could conceivably be read from for READ,
  2034. * and any others (which will be non-In_sync devices) for WRITE.
  2035. * If a read fails, we try reading from something else for which READ
  2036. * is OK.
  2037. */
  2038. r1_bio->mddev = mddev;
  2039. r1_bio->sector = sector_nr;
  2040. r1_bio->state = 0;
  2041. set_bit(R1BIO_IsSync, &r1_bio->state);
  2042. for (i = 0; i < conf->raid_disks * 2; i++) {
  2043. struct md_rdev *rdev;
  2044. bio = r1_bio->bios[i];
  2045. /* take from bio_init */
  2046. bio->bi_next = NULL;
  2047. bio->bi_flags &= ~(BIO_POOL_MASK-1);
  2048. bio->bi_flags |= 1 << BIO_UPTODATE;
  2049. bio->bi_rw = READ;
  2050. bio->bi_vcnt = 0;
  2051. bio->bi_idx = 0;
  2052. bio->bi_phys_segments = 0;
  2053. bio->bi_size = 0;
  2054. bio->bi_end_io = NULL;
  2055. bio->bi_private = NULL;
  2056. rdev = rcu_dereference(conf->mirrors[i].rdev);
  2057. if (rdev == NULL ||
  2058. test_bit(Faulty, &rdev->flags)) {
  2059. if (i < conf->raid_disks)
  2060. still_degraded = 1;
  2061. } else if (!test_bit(In_sync, &rdev->flags)) {
  2062. bio->bi_rw = WRITE;
  2063. bio->bi_end_io = end_sync_write;
  2064. write_targets ++;
  2065. } else {
  2066. /* may need to read from here */
  2067. sector_t first_bad = MaxSector;
  2068. int bad_sectors;
  2069. if (is_badblock(rdev, sector_nr, good_sectors,
  2070. &first_bad, &bad_sectors)) {
  2071. if (first_bad > sector_nr)
  2072. good_sectors = first_bad - sector_nr;
  2073. else {
  2074. bad_sectors -= (sector_nr - first_bad);
  2075. if (min_bad == 0 ||
  2076. min_bad > bad_sectors)
  2077. min_bad = bad_sectors;
  2078. }
  2079. }
  2080. if (sector_nr < first_bad) {
  2081. if (test_bit(WriteMostly, &rdev->flags)) {
  2082. if (wonly < 0)
  2083. wonly = i;
  2084. } else {
  2085. if (disk < 0)
  2086. disk = i;
  2087. }
  2088. bio->bi_rw = READ;
  2089. bio->bi_end_io = end_sync_read;
  2090. read_targets++;
  2091. }
  2092. }
  2093. if (bio->bi_end_io) {
  2094. atomic_inc(&rdev->nr_pending);
  2095. bio->bi_sector = sector_nr + rdev->data_offset;
  2096. bio->bi_bdev = rdev->bdev;
  2097. bio->bi_private = r1_bio;
  2098. }
  2099. }
  2100. rcu_read_unlock();
  2101. if (disk < 0)
  2102. disk = wonly;
  2103. r1_bio->read_disk = disk;
  2104. if (read_targets == 0 && min_bad > 0) {
  2105. /* These sectors are bad on all InSync devices, so we
  2106. * need to mark them bad on all write targets
  2107. */
  2108. int ok = 1;
  2109. for (i = 0 ; i < conf->raid_disks * 2 ; i++)
  2110. if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
  2111. struct md_rdev *rdev =
  2112. rcu_dereference(conf->mirrors[i].rdev);
  2113. ok = rdev_set_badblocks(rdev, sector_nr,
  2114. min_bad, 0
  2115. ) && ok;
  2116. }
  2117. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2118. *skipped = 1;
  2119. put_buf(r1_bio);
  2120. if (!ok) {
  2121. /* Cannot record the badblocks, so need to
  2122. * abort the resync.
  2123. * If there are multiple read targets, could just
  2124. * fail the really bad ones ???
  2125. */
  2126. conf->recovery_disabled = mddev->recovery_disabled;
  2127. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  2128. return 0;
  2129. } else
  2130. return min_bad;
  2131. }
  2132. if (min_bad > 0 && min_bad < good_sectors) {
  2133. /* only resync enough to reach the next bad->good
  2134. * transition */
  2135. good_sectors = min_bad;
  2136. }
  2137. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  2138. /* extra read targets are also write targets */
  2139. write_targets += read_targets-1;
  2140. if (write_targets == 0 || read_targets == 0) {
  2141. /* There is nowhere to write, so all non-sync
  2142. * drives must be failed - so we are finished
  2143. */
  2144. sector_t rv = max_sector - sector_nr;
  2145. *skipped = 1;
  2146. put_buf(r1_bio);
  2147. return rv;
  2148. }
  2149. if (max_sector > mddev->resync_max)
  2150. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2151. if (max_sector > sector_nr + good_sectors)
  2152. max_sector = sector_nr + good_sectors;
  2153. nr_sectors = 0;
  2154. sync_blocks = 0;
  2155. do {
  2156. struct page *page;
  2157. int len = PAGE_SIZE;
  2158. if (sector_nr + (len>>9) > max_sector)
  2159. len = (max_sector - sector_nr) << 9;
  2160. if (len == 0)
  2161. break;
  2162. if (sync_blocks == 0) {
  2163. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2164. &sync_blocks, still_degraded) &&
  2165. !conf->fullsync &&
  2166. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  2167. break;
  2168. BUG_ON(sync_blocks < (PAGE_SIZE>>9));
  2169. if ((len >> 9) > sync_blocks)
  2170. len = sync_blocks<<9;
  2171. }
  2172. for (i = 0 ; i < conf->raid_disks * 2; i++) {
  2173. bio = r1_bio->bios[i];
  2174. if (bio->bi_end_io) {
  2175. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2176. if (bio_add_page(bio, page, len, 0) == 0) {
  2177. /* stop here */
  2178. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2179. while (i > 0) {
  2180. i--;
  2181. bio = r1_bio->bios[i];
  2182. if (bio->bi_end_io==NULL)
  2183. continue;
  2184. /* remove last page from this bio */
  2185. bio->bi_vcnt--;
  2186. bio->bi_size -= len;
  2187. bio->bi_flags &= ~(1<< BIO_SEG_VALID);
  2188. }
  2189. goto bio_full;
  2190. }
  2191. }
  2192. }
  2193. nr_sectors += len>>9;
  2194. sector_nr += len>>9;
  2195. sync_blocks -= (len>>9);
  2196. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  2197. bio_full:
  2198. r1_bio->sectors = nr_sectors;
  2199. /* For a user-requested sync, we read all readable devices and do a
  2200. * compare
  2201. */
  2202. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2203. atomic_set(&r1_bio->remaining, read_targets);
  2204. for (i = 0; i < conf->raid_disks * 2; i++) {
  2205. bio = r1_bio->bios[i];
  2206. if (bio->bi_end_io == end_sync_read) {
  2207. md_sync_acct(bio->bi_bdev, nr_sectors);
  2208. generic_make_request(bio);
  2209. }
  2210. }
  2211. } else {
  2212. atomic_set(&r1_bio->remaining, 1);
  2213. bio = r1_bio->bios[r1_bio->read_disk];
  2214. md_sync_acct(bio->bi_bdev, nr_sectors);
  2215. generic_make_request(bio);
  2216. }
  2217. return nr_sectors;
  2218. }
  2219. static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2220. {
  2221. if (sectors)
  2222. return sectors;
  2223. return mddev->dev_sectors;
  2224. }
  2225. static struct r1conf *setup_conf(struct mddev *mddev)
  2226. {
  2227. struct r1conf *conf;
  2228. int i;
  2229. struct mirror_info *disk;
  2230. struct md_rdev *rdev;
  2231. int err = -ENOMEM;
  2232. conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
  2233. if (!conf)
  2234. goto abort;
  2235. conf->mirrors = kzalloc(sizeof(struct mirror_info)
  2236. * mddev->raid_disks * 2,
  2237. GFP_KERNEL);
  2238. if (!conf->mirrors)
  2239. goto abort;
  2240. conf->tmppage = alloc_page(GFP_KERNEL);
  2241. if (!conf->tmppage)
  2242. goto abort;
  2243. conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  2244. if (!conf->poolinfo)
  2245. goto abort;
  2246. conf->poolinfo->raid_disks = mddev->raid_disks * 2;
  2247. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2248. r1bio_pool_free,
  2249. conf->poolinfo);
  2250. if (!conf->r1bio_pool)
  2251. goto abort;
  2252. conf->poolinfo->mddev = mddev;
  2253. err = -EINVAL;
  2254. spin_lock_init(&conf->device_lock);
  2255. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2256. int disk_idx = rdev->raid_disk;
  2257. if (disk_idx >= mddev->raid_disks
  2258. || disk_idx < 0)
  2259. continue;
  2260. if (test_bit(Replacement, &rdev->flags))
  2261. disk = conf->mirrors + conf->raid_disks + disk_idx;
  2262. else
  2263. disk = conf->mirrors + disk_idx;
  2264. if (disk->rdev)
  2265. goto abort;
  2266. disk->rdev = rdev;
  2267. disk->head_position = 0;
  2268. }
  2269. conf->raid_disks = mddev->raid_disks;
  2270. conf->mddev = mddev;
  2271. INIT_LIST_HEAD(&conf->retry_list);
  2272. spin_lock_init(&conf->resync_lock);
  2273. init_waitqueue_head(&conf->wait_barrier);
  2274. bio_list_init(&conf->pending_bio_list);
  2275. conf->pending_count = 0;
  2276. conf->recovery_disabled = mddev->recovery_disabled - 1;
  2277. err = -EIO;
  2278. conf->last_used = -1;
  2279. for (i = 0; i < conf->raid_disks * 2; i++) {
  2280. disk = conf->mirrors + i;
  2281. if (i < conf->raid_disks &&
  2282. disk[conf->raid_disks].rdev) {
  2283. /* This slot has a replacement. */
  2284. if (!disk->rdev) {
  2285. /* No original, just make the replacement
  2286. * a recovering spare
  2287. */
  2288. disk->rdev =
  2289. disk[conf->raid_disks].rdev;
  2290. disk[conf->raid_disks].rdev = NULL;
  2291. } else if (!test_bit(In_sync, &disk->rdev->flags))
  2292. /* Original is not in_sync - bad */
  2293. goto abort;
  2294. }
  2295. if (!disk->rdev ||
  2296. !test_bit(In_sync, &disk->rdev->flags)) {
  2297. disk->head_position = 0;
  2298. if (disk->rdev)
  2299. conf->fullsync = 1;
  2300. } else if (conf->last_used < 0)
  2301. /*
  2302. * The first working device is used as a
  2303. * starting point to read balancing.
  2304. */
  2305. conf->last_used = i;
  2306. }
  2307. if (conf->last_used < 0) {
  2308. printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
  2309. mdname(mddev));
  2310. goto abort;
  2311. }
  2312. err = -ENOMEM;
  2313. conf->thread = md_register_thread(raid1d, mddev, NULL);
  2314. if (!conf->thread) {
  2315. printk(KERN_ERR
  2316. "md/raid1:%s: couldn't allocate thread\n",
  2317. mdname(mddev));
  2318. goto abort;
  2319. }
  2320. return conf;
  2321. abort:
  2322. if (conf) {
  2323. if (conf->r1bio_pool)
  2324. mempool_destroy(conf->r1bio_pool);
  2325. kfree(conf->mirrors);
  2326. safe_put_page(conf->tmppage);
  2327. kfree(conf->poolinfo);
  2328. kfree(conf);
  2329. }
  2330. return ERR_PTR(err);
  2331. }
  2332. static int run(struct mddev *mddev)
  2333. {
  2334. struct r1conf *conf;
  2335. int i;
  2336. struct md_rdev *rdev;
  2337. if (mddev->level != 1) {
  2338. printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
  2339. mdname(mddev), mddev->level);
  2340. return -EIO;
  2341. }
  2342. if (mddev->reshape_position != MaxSector) {
  2343. printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
  2344. mdname(mddev));
  2345. return -EIO;
  2346. }
  2347. /*
  2348. * copy the already verified devices into our private RAID1
  2349. * bookkeeping area. [whatever we allocate in run(),
  2350. * should be freed in stop()]
  2351. */
  2352. if (mddev->private == NULL)
  2353. conf = setup_conf(mddev);
  2354. else
  2355. conf = mddev->private;
  2356. if (IS_ERR(conf))
  2357. return PTR_ERR(conf);
  2358. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2359. if (!mddev->gendisk)
  2360. continue;
  2361. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2362. rdev->data_offset << 9);
  2363. /* as we don't honour merge_bvec_fn, we must never risk
  2364. * violating it, so limit ->max_segments to 1 lying within
  2365. * a single page, as a one page request is never in violation.
  2366. */
  2367. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  2368. blk_queue_max_segments(mddev->queue, 1);
  2369. blk_queue_segment_boundary(mddev->queue,
  2370. PAGE_CACHE_SIZE - 1);
  2371. }
  2372. }
  2373. mddev->degraded = 0;
  2374. for (i=0; i < conf->raid_disks; i++)
  2375. if (conf->mirrors[i].rdev == NULL ||
  2376. !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
  2377. test_bit(Faulty, &conf->mirrors[i].rdev->flags))
  2378. mddev->degraded++;
  2379. if (conf->raid_disks - mddev->degraded == 1)
  2380. mddev->recovery_cp = MaxSector;
  2381. if (mddev->recovery_cp != MaxSector)
  2382. printk(KERN_NOTICE "md/raid1:%s: not clean"
  2383. " -- starting background reconstruction\n",
  2384. mdname(mddev));
  2385. printk(KERN_INFO
  2386. "md/raid1:%s: active with %d out of %d mirrors\n",
  2387. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2388. mddev->raid_disks);
  2389. /*
  2390. * Ok, everything is just fine now
  2391. */
  2392. mddev->thread = conf->thread;
  2393. conf->thread = NULL;
  2394. mddev->private = conf;
  2395. md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
  2396. if (mddev->queue) {
  2397. mddev->queue->backing_dev_info.congested_fn = raid1_congested;
  2398. mddev->queue->backing_dev_info.congested_data = mddev;
  2399. }
  2400. return md_integrity_register(mddev);
  2401. }
  2402. static int stop(struct mddev *mddev)
  2403. {
  2404. struct r1conf *conf = mddev->private;
  2405. struct bitmap *bitmap = mddev->bitmap;
  2406. /* wait for behind writes to complete */
  2407. if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
  2408. printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
  2409. mdname(mddev));
  2410. /* need to kick something here to make sure I/O goes? */
  2411. wait_event(bitmap->behind_wait,
  2412. atomic_read(&bitmap->behind_writes) == 0);
  2413. }
  2414. raise_barrier(conf);
  2415. lower_barrier(conf);
  2416. md_unregister_thread(&mddev->thread);
  2417. if (conf->r1bio_pool)
  2418. mempool_destroy(conf->r1bio_pool);
  2419. kfree(conf->mirrors);
  2420. kfree(conf->poolinfo);
  2421. kfree(conf);
  2422. mddev->private = NULL;
  2423. return 0;
  2424. }
  2425. static int raid1_resize(struct mddev *mddev, sector_t sectors)
  2426. {
  2427. /* no resync is happening, and there is enough space
  2428. * on all devices, so we can resize.
  2429. * We need to make sure resync covers any new space.
  2430. * If the array is shrinking we should possibly wait until
  2431. * any io in the removed space completes, but it hardly seems
  2432. * worth it.
  2433. */
  2434. md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
  2435. if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
  2436. return -EINVAL;
  2437. set_capacity(mddev->gendisk, mddev->array_sectors);
  2438. revalidate_disk(mddev->gendisk);
  2439. if (sectors > mddev->dev_sectors &&
  2440. mddev->recovery_cp > mddev->dev_sectors) {
  2441. mddev->recovery_cp = mddev->dev_sectors;
  2442. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2443. }
  2444. mddev->dev_sectors = sectors;
  2445. mddev->resync_max_sectors = sectors;
  2446. return 0;
  2447. }
  2448. static int raid1_reshape(struct mddev *mddev)
  2449. {
  2450. /* We need to:
  2451. * 1/ resize the r1bio_pool
  2452. * 2/ resize conf->mirrors
  2453. *
  2454. * We allocate a new r1bio_pool if we can.
  2455. * Then raise a device barrier and wait until all IO stops.
  2456. * Then resize conf->mirrors and swap in the new r1bio pool.
  2457. *
  2458. * At the same time, we "pack" the devices so that all the missing
  2459. * devices have the higher raid_disk numbers.
  2460. */
  2461. mempool_t *newpool, *oldpool;
  2462. struct pool_info *newpoolinfo;
  2463. struct mirror_info *newmirrors;
  2464. struct r1conf *conf = mddev->private;
  2465. int cnt, raid_disks;
  2466. unsigned long flags;
  2467. int d, d2, err;
  2468. /* Cannot change chunk_size, layout, or level */
  2469. if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
  2470. mddev->layout != mddev->new_layout ||
  2471. mddev->level != mddev->new_level) {
  2472. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2473. mddev->new_layout = mddev->layout;
  2474. mddev->new_level = mddev->level;
  2475. return -EINVAL;
  2476. }
  2477. err = md_allow_write(mddev);
  2478. if (err)
  2479. return err;
  2480. raid_disks = mddev->raid_disks + mddev->delta_disks;
  2481. if (raid_disks < conf->raid_disks) {
  2482. cnt=0;
  2483. for (d= 0; d < conf->raid_disks; d++)
  2484. if (conf->mirrors[d].rdev)
  2485. cnt++;
  2486. if (cnt > raid_disks)
  2487. return -EBUSY;
  2488. }
  2489. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  2490. if (!newpoolinfo)
  2491. return -ENOMEM;
  2492. newpoolinfo->mddev = mddev;
  2493. newpoolinfo->raid_disks = raid_disks * 2;
  2494. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2495. r1bio_pool_free, newpoolinfo);
  2496. if (!newpool) {
  2497. kfree(newpoolinfo);
  2498. return -ENOMEM;
  2499. }
  2500. newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks * 2,
  2501. GFP_KERNEL);
  2502. if (!newmirrors) {
  2503. kfree(newpoolinfo);
  2504. mempool_destroy(newpool);
  2505. return -ENOMEM;
  2506. }
  2507. raise_barrier(conf);
  2508. /* ok, everything is stopped */
  2509. oldpool = conf->r1bio_pool;
  2510. conf->r1bio_pool = newpool;
  2511. for (d = d2 = 0; d < conf->raid_disks; d++) {
  2512. struct md_rdev *rdev = conf->mirrors[d].rdev;
  2513. if (rdev && rdev->raid_disk != d2) {
  2514. sysfs_unlink_rdev(mddev, rdev);
  2515. rdev->raid_disk = d2;
  2516. sysfs_unlink_rdev(mddev, rdev);
  2517. if (sysfs_link_rdev(mddev, rdev))
  2518. printk(KERN_WARNING
  2519. "md/raid1:%s: cannot register rd%d\n",
  2520. mdname(mddev), rdev->raid_disk);
  2521. }
  2522. if (rdev)
  2523. newmirrors[d2++].rdev = rdev;
  2524. }
  2525. kfree(conf->mirrors);
  2526. conf->mirrors = newmirrors;
  2527. kfree(conf->poolinfo);
  2528. conf->poolinfo = newpoolinfo;
  2529. spin_lock_irqsave(&conf->device_lock, flags);
  2530. mddev->degraded += (raid_disks - conf->raid_disks);
  2531. spin_unlock_irqrestore(&conf->device_lock, flags);
  2532. conf->raid_disks = mddev->raid_disks = raid_disks;
  2533. mddev->delta_disks = 0;
  2534. conf->last_used = 0; /* just make sure it is in-range */
  2535. lower_barrier(conf);
  2536. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2537. md_wakeup_thread(mddev->thread);
  2538. mempool_destroy(oldpool);
  2539. return 0;
  2540. }
  2541. static void raid1_quiesce(struct mddev *mddev, int state)
  2542. {
  2543. struct r1conf *conf = mddev->private;
  2544. switch(state) {
  2545. case 2: /* wake for suspend */
  2546. wake_up(&conf->wait_barrier);
  2547. break;
  2548. case 1:
  2549. raise_barrier(conf);
  2550. break;
  2551. case 0:
  2552. lower_barrier(conf);
  2553. break;
  2554. }
  2555. }
  2556. static void *raid1_takeover(struct mddev *mddev)
  2557. {
  2558. /* raid1 can take over:
  2559. * raid5 with 2 devices, any layout or chunk size
  2560. */
  2561. if (mddev->level == 5 && mddev->raid_disks == 2) {
  2562. struct r1conf *conf;
  2563. mddev->new_level = 1;
  2564. mddev->new_layout = 0;
  2565. mddev->new_chunk_sectors = 0;
  2566. conf = setup_conf(mddev);
  2567. if (!IS_ERR(conf))
  2568. conf->barrier = 1;
  2569. return conf;
  2570. }
  2571. return ERR_PTR(-EINVAL);
  2572. }
  2573. static struct md_personality raid1_personality =
  2574. {
  2575. .name = "raid1",
  2576. .level = 1,
  2577. .owner = THIS_MODULE,
  2578. .make_request = make_request,
  2579. .run = run,
  2580. .stop = stop,
  2581. .status = status,
  2582. .error_handler = error,
  2583. .hot_add_disk = raid1_add_disk,
  2584. .hot_remove_disk= raid1_remove_disk,
  2585. .spare_active = raid1_spare_active,
  2586. .sync_request = sync_request,
  2587. .resize = raid1_resize,
  2588. .size = raid1_size,
  2589. .check_reshape = raid1_reshape,
  2590. .quiesce = raid1_quiesce,
  2591. .takeover = raid1_takeover,
  2592. };
  2593. static int __init raid_init(void)
  2594. {
  2595. return register_md_personality(&raid1_personality);
  2596. }
  2597. static void raid_exit(void)
  2598. {
  2599. unregister_md_personality(&raid1_personality);
  2600. }
  2601. module_init(raid_init);
  2602. module_exit(raid_exit);
  2603. MODULE_LICENSE("GPL");
  2604. MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
  2605. MODULE_ALIAS("md-personality-3"); /* RAID1 */
  2606. MODULE_ALIAS("md-raid1");
  2607. MODULE_ALIAS("md-level-1");
  2608. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);