raid1.c 62 KB

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