raid1.c 63 KB

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