raid1.c 58 KB

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