raid1.c 60 KB

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