raid1.c 60 KB

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