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. conf->recovery_disabled = mddev->recovery_disabled;
  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_safe(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 (mddev->recovery_disabled == conf->recovery_disabled)
  926. return -EBUSY;
  927. if (rdev->badblocks.count)
  928. return -EINVAL;
  929. if (rdev->raid_disk >= 0)
  930. first = last = rdev->raid_disk;
  931. for (mirror = first; mirror <= last; mirror++)
  932. if ( !(p=conf->mirrors+mirror)->rdev) {
  933. disk_stack_limits(mddev->gendisk, rdev->bdev,
  934. rdev->data_offset << 9);
  935. /* as we don't honour merge_bvec_fn, we must
  936. * never risk violating it, so limit
  937. * ->max_segments to one lying with a single
  938. * page, as a one page request is never in
  939. * violation.
  940. */
  941. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  942. blk_queue_max_segments(mddev->queue, 1);
  943. blk_queue_segment_boundary(mddev->queue,
  944. PAGE_CACHE_SIZE - 1);
  945. }
  946. p->head_position = 0;
  947. rdev->raid_disk = mirror;
  948. err = 0;
  949. /* As all devices are equivalent, we don't need a full recovery
  950. * if this was recently any drive of the array
  951. */
  952. if (rdev->saved_raid_disk < 0)
  953. conf->fullsync = 1;
  954. rcu_assign_pointer(p->rdev, rdev);
  955. break;
  956. }
  957. md_integrity_add_rdev(rdev, mddev);
  958. print_conf(conf);
  959. return err;
  960. }
  961. static int raid1_remove_disk(mddev_t *mddev, int number)
  962. {
  963. conf_t *conf = mddev->private;
  964. int err = 0;
  965. mdk_rdev_t *rdev;
  966. mirror_info_t *p = conf->mirrors+ number;
  967. print_conf(conf);
  968. rdev = p->rdev;
  969. if (rdev) {
  970. if (test_bit(In_sync, &rdev->flags) ||
  971. atomic_read(&rdev->nr_pending)) {
  972. err = -EBUSY;
  973. goto abort;
  974. }
  975. /* Only remove non-faulty devices if recovery
  976. * is not possible.
  977. */
  978. if (!test_bit(Faulty, &rdev->flags) &&
  979. mddev->recovery_disabled != conf->recovery_disabled &&
  980. mddev->degraded < conf->raid_disks) {
  981. err = -EBUSY;
  982. goto abort;
  983. }
  984. p->rdev = NULL;
  985. synchronize_rcu();
  986. if (atomic_read(&rdev->nr_pending)) {
  987. /* lost the race, try later */
  988. err = -EBUSY;
  989. p->rdev = rdev;
  990. goto abort;
  991. }
  992. err = md_integrity_register(mddev);
  993. }
  994. abort:
  995. print_conf(conf);
  996. return err;
  997. }
  998. static void end_sync_read(struct bio *bio, int error)
  999. {
  1000. r1bio_t *r1_bio = bio->bi_private;
  1001. int i;
  1002. for (i=r1_bio->mddev->raid_disks; i--; )
  1003. if (r1_bio->bios[i] == bio)
  1004. break;
  1005. BUG_ON(i < 0);
  1006. update_head_pos(i, r1_bio);
  1007. /*
  1008. * we have read a block, now it needs to be re-written,
  1009. * or re-read if the read failed.
  1010. * We don't do much here, just schedule handling by raid1d
  1011. */
  1012. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1013. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1014. if (atomic_dec_and_test(&r1_bio->remaining))
  1015. reschedule_retry(r1_bio);
  1016. }
  1017. static void end_sync_write(struct bio *bio, int error)
  1018. {
  1019. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1020. r1bio_t *r1_bio = bio->bi_private;
  1021. mddev_t *mddev = r1_bio->mddev;
  1022. conf_t *conf = mddev->private;
  1023. int i;
  1024. int mirror=0;
  1025. for (i = 0; i < conf->raid_disks; i++)
  1026. if (r1_bio->bios[i] == bio) {
  1027. mirror = i;
  1028. break;
  1029. }
  1030. if (!uptodate) {
  1031. sector_t sync_blocks = 0;
  1032. sector_t s = r1_bio->sector;
  1033. long sectors_to_go = r1_bio->sectors;
  1034. /* make sure these bits doesn't get cleared. */
  1035. do {
  1036. bitmap_end_sync(mddev->bitmap, s,
  1037. &sync_blocks, 1);
  1038. s += sync_blocks;
  1039. sectors_to_go -= sync_blocks;
  1040. } while (sectors_to_go > 0);
  1041. md_error(mddev, conf->mirrors[mirror].rdev);
  1042. }
  1043. update_head_pos(mirror, r1_bio);
  1044. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1045. sector_t s = r1_bio->sectors;
  1046. put_buf(r1_bio);
  1047. md_done_sync(mddev, s, uptodate);
  1048. }
  1049. }
  1050. static int fix_sync_read_error(r1bio_t *r1_bio)
  1051. {
  1052. /* Try some synchronous reads of other devices to get
  1053. * good data, much like with normal read errors. Only
  1054. * read into the pages we already have so we don't
  1055. * need to re-issue the read request.
  1056. * We don't need to freeze the array, because being in an
  1057. * active sync request, there is no normal IO, and
  1058. * no overlapping syncs.
  1059. */
  1060. mddev_t *mddev = r1_bio->mddev;
  1061. conf_t *conf = mddev->private;
  1062. struct bio *bio = r1_bio->bios[r1_bio->read_disk];
  1063. sector_t sect = r1_bio->sector;
  1064. int sectors = r1_bio->sectors;
  1065. int idx = 0;
  1066. while(sectors) {
  1067. int s = sectors;
  1068. int d = r1_bio->read_disk;
  1069. int success = 0;
  1070. mdk_rdev_t *rdev;
  1071. int start;
  1072. if (s > (PAGE_SIZE>>9))
  1073. s = PAGE_SIZE >> 9;
  1074. do {
  1075. if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
  1076. /* No rcu protection needed here devices
  1077. * can only be removed when no resync is
  1078. * active, and resync is currently active
  1079. */
  1080. rdev = conf->mirrors[d].rdev;
  1081. if (sync_page_io(rdev, sect, s<<9,
  1082. bio->bi_io_vec[idx].bv_page,
  1083. READ, false)) {
  1084. success = 1;
  1085. break;
  1086. }
  1087. }
  1088. d++;
  1089. if (d == conf->raid_disks)
  1090. d = 0;
  1091. } while (!success && d != r1_bio->read_disk);
  1092. if (!success) {
  1093. char b[BDEVNAME_SIZE];
  1094. /* Cannot read from anywhere, array is toast */
  1095. md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
  1096. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
  1097. " for block %llu\n",
  1098. mdname(mddev),
  1099. bdevname(bio->bi_bdev, b),
  1100. (unsigned long long)r1_bio->sector);
  1101. md_done_sync(mddev, r1_bio->sectors, 0);
  1102. put_buf(r1_bio);
  1103. return 0;
  1104. }
  1105. start = d;
  1106. /* write it back and re-read */
  1107. while (d != r1_bio->read_disk) {
  1108. if (d == 0)
  1109. d = conf->raid_disks;
  1110. d--;
  1111. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1112. continue;
  1113. rdev = conf->mirrors[d].rdev;
  1114. if (sync_page_io(rdev, sect, 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. }
  1121. }
  1122. d = start;
  1123. while (d != r1_bio->read_disk) {
  1124. if (d == 0)
  1125. d = conf->raid_disks;
  1126. d--;
  1127. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1128. continue;
  1129. rdev = conf->mirrors[d].rdev;
  1130. if (sync_page_io(rdev, sect, s<<9,
  1131. bio->bi_io_vec[idx].bv_page,
  1132. READ, false) == 0)
  1133. md_error(mddev, rdev);
  1134. else
  1135. atomic_add(s, &rdev->corrected_errors);
  1136. }
  1137. sectors -= s;
  1138. sect += s;
  1139. idx ++;
  1140. }
  1141. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1142. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1143. return 1;
  1144. }
  1145. static int process_checks(r1bio_t *r1_bio)
  1146. {
  1147. /* We have read all readable devices. If we haven't
  1148. * got the block, then there is no hope left.
  1149. * If we have, then we want to do a comparison
  1150. * and skip the write if everything is the same.
  1151. * If any blocks failed to read, then we need to
  1152. * attempt an over-write
  1153. */
  1154. mddev_t *mddev = r1_bio->mddev;
  1155. conf_t *conf = mddev->private;
  1156. int primary;
  1157. int i;
  1158. for (primary = 0; primary < conf->raid_disks; primary++)
  1159. if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
  1160. test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
  1161. r1_bio->bios[primary]->bi_end_io = NULL;
  1162. rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
  1163. break;
  1164. }
  1165. r1_bio->read_disk = primary;
  1166. for (i = 0; i < conf->raid_disks; i++) {
  1167. int j;
  1168. int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
  1169. struct bio *pbio = r1_bio->bios[primary];
  1170. struct bio *sbio = r1_bio->bios[i];
  1171. int size;
  1172. if (r1_bio->bios[i]->bi_end_io != end_sync_read)
  1173. continue;
  1174. if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
  1175. for (j = vcnt; j-- ; ) {
  1176. struct page *p, *s;
  1177. p = pbio->bi_io_vec[j].bv_page;
  1178. s = sbio->bi_io_vec[j].bv_page;
  1179. if (memcmp(page_address(p),
  1180. page_address(s),
  1181. PAGE_SIZE))
  1182. break;
  1183. }
  1184. } else
  1185. j = 0;
  1186. if (j >= 0)
  1187. mddev->resync_mismatches += r1_bio->sectors;
  1188. if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
  1189. && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
  1190. /* No need to write to this device. */
  1191. sbio->bi_end_io = NULL;
  1192. rdev_dec_pending(conf->mirrors[i].rdev, mddev);
  1193. continue;
  1194. }
  1195. /* fixup the bio for reuse */
  1196. sbio->bi_vcnt = vcnt;
  1197. sbio->bi_size = r1_bio->sectors << 9;
  1198. sbio->bi_idx = 0;
  1199. sbio->bi_phys_segments = 0;
  1200. sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1201. sbio->bi_flags |= 1 << BIO_UPTODATE;
  1202. sbio->bi_next = NULL;
  1203. sbio->bi_sector = r1_bio->sector +
  1204. conf->mirrors[i].rdev->data_offset;
  1205. sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1206. size = sbio->bi_size;
  1207. for (j = 0; j < vcnt ; j++) {
  1208. struct bio_vec *bi;
  1209. bi = &sbio->bi_io_vec[j];
  1210. bi->bv_offset = 0;
  1211. if (size > PAGE_SIZE)
  1212. bi->bv_len = PAGE_SIZE;
  1213. else
  1214. bi->bv_len = size;
  1215. size -= PAGE_SIZE;
  1216. memcpy(page_address(bi->bv_page),
  1217. page_address(pbio->bi_io_vec[j].bv_page),
  1218. PAGE_SIZE);
  1219. }
  1220. }
  1221. return 0;
  1222. }
  1223. static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
  1224. {
  1225. conf_t *conf = mddev->private;
  1226. int i;
  1227. int disks = conf->raid_disks;
  1228. struct bio *bio, *wbio;
  1229. bio = r1_bio->bios[r1_bio->read_disk];
  1230. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  1231. /* ouch - failed to read all of that. */
  1232. if (!fix_sync_read_error(r1_bio))
  1233. return;
  1234. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1235. if (process_checks(r1_bio) < 0)
  1236. return;
  1237. /*
  1238. * schedule writes
  1239. */
  1240. atomic_set(&r1_bio->remaining, 1);
  1241. for (i = 0; i < disks ; i++) {
  1242. wbio = r1_bio->bios[i];
  1243. if (wbio->bi_end_io == NULL ||
  1244. (wbio->bi_end_io == end_sync_read &&
  1245. (i == r1_bio->read_disk ||
  1246. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  1247. continue;
  1248. wbio->bi_rw = WRITE;
  1249. wbio->bi_end_io = end_sync_write;
  1250. atomic_inc(&r1_bio->remaining);
  1251. md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
  1252. generic_make_request(wbio);
  1253. }
  1254. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1255. /* if we're here, all write(s) have completed, so clean up */
  1256. md_done_sync(mddev, r1_bio->sectors, 1);
  1257. put_buf(r1_bio);
  1258. }
  1259. }
  1260. /*
  1261. * This is a kernel thread which:
  1262. *
  1263. * 1. Retries failed read operations on working mirrors.
  1264. * 2. Updates the raid superblock when problems encounter.
  1265. * 3. Performs writes following reads for array syncronising.
  1266. */
  1267. static void fix_read_error(conf_t *conf, int read_disk,
  1268. sector_t sect, int sectors)
  1269. {
  1270. mddev_t *mddev = conf->mddev;
  1271. while(sectors) {
  1272. int s = sectors;
  1273. int d = read_disk;
  1274. int success = 0;
  1275. int start;
  1276. mdk_rdev_t *rdev;
  1277. if (s > (PAGE_SIZE>>9))
  1278. s = PAGE_SIZE >> 9;
  1279. do {
  1280. /* Note: no rcu protection needed here
  1281. * as this is synchronous in the raid1d thread
  1282. * which is the thread that might remove
  1283. * a device. If raid1d ever becomes multi-threaded....
  1284. */
  1285. rdev = conf->mirrors[d].rdev;
  1286. if (rdev &&
  1287. test_bit(In_sync, &rdev->flags) &&
  1288. sync_page_io(rdev, sect, s<<9,
  1289. conf->tmppage, READ, false))
  1290. success = 1;
  1291. else {
  1292. d++;
  1293. if (d == conf->raid_disks)
  1294. d = 0;
  1295. }
  1296. } while (!success && d != read_disk);
  1297. if (!success) {
  1298. /* Cannot read from anywhere -- bye bye array */
  1299. md_error(mddev, conf->mirrors[read_disk].rdev);
  1300. break;
  1301. }
  1302. /* write it back and re-read */
  1303. start = d;
  1304. while (d != read_disk) {
  1305. if (d==0)
  1306. d = conf->raid_disks;
  1307. d--;
  1308. rdev = conf->mirrors[d].rdev;
  1309. if (rdev &&
  1310. test_bit(In_sync, &rdev->flags)) {
  1311. if (sync_page_io(rdev, sect, s<<9,
  1312. conf->tmppage, WRITE, false)
  1313. == 0)
  1314. /* Well, this device is dead */
  1315. md_error(mddev, rdev);
  1316. }
  1317. }
  1318. d = start;
  1319. while (d != read_disk) {
  1320. char b[BDEVNAME_SIZE];
  1321. if (d==0)
  1322. d = conf->raid_disks;
  1323. d--;
  1324. rdev = conf->mirrors[d].rdev;
  1325. if (rdev &&
  1326. test_bit(In_sync, &rdev->flags)) {
  1327. if (sync_page_io(rdev, sect, s<<9,
  1328. conf->tmppage, READ, false)
  1329. == 0)
  1330. /* Well, this device is dead */
  1331. md_error(mddev, rdev);
  1332. else {
  1333. atomic_add(s, &rdev->corrected_errors);
  1334. printk(KERN_INFO
  1335. "md/raid1:%s: read error corrected "
  1336. "(%d sectors at %llu on %s)\n",
  1337. mdname(mddev), s,
  1338. (unsigned long long)(sect +
  1339. rdev->data_offset),
  1340. bdevname(rdev->bdev, b));
  1341. }
  1342. }
  1343. }
  1344. sectors -= s;
  1345. sect += s;
  1346. }
  1347. }
  1348. static void raid1d(mddev_t *mddev)
  1349. {
  1350. r1bio_t *r1_bio;
  1351. struct bio *bio;
  1352. unsigned long flags;
  1353. conf_t *conf = mddev->private;
  1354. struct list_head *head = &conf->retry_list;
  1355. mdk_rdev_t *rdev;
  1356. struct blk_plug plug;
  1357. md_check_recovery(mddev);
  1358. blk_start_plug(&plug);
  1359. for (;;) {
  1360. char b[BDEVNAME_SIZE];
  1361. if (atomic_read(&mddev->plug_cnt) == 0)
  1362. flush_pending_writes(conf);
  1363. spin_lock_irqsave(&conf->device_lock, flags);
  1364. if (list_empty(head)) {
  1365. spin_unlock_irqrestore(&conf->device_lock, flags);
  1366. break;
  1367. }
  1368. r1_bio = list_entry(head->prev, r1bio_t, retry_list);
  1369. list_del(head->prev);
  1370. conf->nr_queued--;
  1371. spin_unlock_irqrestore(&conf->device_lock, flags);
  1372. mddev = r1_bio->mddev;
  1373. conf = mddev->private;
  1374. if (test_bit(R1BIO_IsSync, &r1_bio->state))
  1375. sync_request_write(mddev, r1_bio);
  1376. else {
  1377. int disk;
  1378. /* we got a read error. Maybe the drive is bad. Maybe just
  1379. * the block and we can fix it.
  1380. * We freeze all other IO, and try reading the block from
  1381. * other devices. When we find one, we re-write
  1382. * and check it that fixes the read error.
  1383. * This is all done synchronously while the array is
  1384. * frozen
  1385. */
  1386. if (mddev->ro == 0) {
  1387. freeze_array(conf);
  1388. fix_read_error(conf, r1_bio->read_disk,
  1389. r1_bio->sector,
  1390. r1_bio->sectors);
  1391. unfreeze_array(conf);
  1392. } else
  1393. md_error(mddev,
  1394. conf->mirrors[r1_bio->read_disk].rdev);
  1395. bio = r1_bio->bios[r1_bio->read_disk];
  1396. if ((disk=read_balance(conf, r1_bio)) == -1) {
  1397. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
  1398. " read error for block %llu\n",
  1399. mdname(mddev),
  1400. bdevname(bio->bi_bdev,b),
  1401. (unsigned long long)r1_bio->sector);
  1402. raid_end_bio_io(r1_bio);
  1403. } else {
  1404. const unsigned long do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC;
  1405. r1_bio->bios[r1_bio->read_disk] =
  1406. mddev->ro ? IO_BLOCKED : NULL;
  1407. r1_bio->read_disk = disk;
  1408. bio_put(bio);
  1409. bio = bio_clone_mddev(r1_bio->master_bio,
  1410. GFP_NOIO, mddev);
  1411. r1_bio->bios[r1_bio->read_disk] = bio;
  1412. rdev = conf->mirrors[disk].rdev;
  1413. printk_ratelimited(
  1414. KERN_ERR
  1415. "md/raid1:%s: redirecting sector %llu"
  1416. " to other mirror: %s\n",
  1417. mdname(mddev),
  1418. (unsigned long long)r1_bio->sector,
  1419. bdevname(rdev->bdev, b));
  1420. bio->bi_sector = r1_bio->sector + rdev->data_offset;
  1421. bio->bi_bdev = rdev->bdev;
  1422. bio->bi_end_io = raid1_end_read_request;
  1423. bio->bi_rw = READ | do_sync;
  1424. bio->bi_private = r1_bio;
  1425. generic_make_request(bio);
  1426. }
  1427. }
  1428. cond_resched();
  1429. }
  1430. blk_finish_plug(&plug);
  1431. }
  1432. static int init_resync(conf_t *conf)
  1433. {
  1434. int buffs;
  1435. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1436. BUG_ON(conf->r1buf_pool);
  1437. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  1438. conf->poolinfo);
  1439. if (!conf->r1buf_pool)
  1440. return -ENOMEM;
  1441. conf->next_resync = 0;
  1442. return 0;
  1443. }
  1444. /*
  1445. * perform a "sync" on one "block"
  1446. *
  1447. * We need to make sure that no normal I/O request - particularly write
  1448. * requests - conflict with active sync requests.
  1449. *
  1450. * This is achieved by tracking pending requests and a 'barrier' concept
  1451. * that can be installed to exclude normal IO requests.
  1452. */
  1453. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1454. {
  1455. conf_t *conf = mddev->private;
  1456. r1bio_t *r1_bio;
  1457. struct bio *bio;
  1458. sector_t max_sector, nr_sectors;
  1459. int disk = -1;
  1460. int i;
  1461. int wonly = -1;
  1462. int write_targets = 0, read_targets = 0;
  1463. sector_t sync_blocks;
  1464. int still_degraded = 0;
  1465. if (!conf->r1buf_pool)
  1466. if (init_resync(conf))
  1467. return 0;
  1468. max_sector = mddev->dev_sectors;
  1469. if (sector_nr >= max_sector) {
  1470. /* If we aborted, we need to abort the
  1471. * sync on the 'current' bitmap chunk (there will
  1472. * only be one in raid1 resync.
  1473. * We can find the current addess in mddev->curr_resync
  1474. */
  1475. if (mddev->curr_resync < max_sector) /* aborted */
  1476. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1477. &sync_blocks, 1);
  1478. else /* completed sync */
  1479. conf->fullsync = 0;
  1480. bitmap_close_sync(mddev->bitmap);
  1481. close_sync(conf);
  1482. return 0;
  1483. }
  1484. if (mddev->bitmap == NULL &&
  1485. mddev->recovery_cp == MaxSector &&
  1486. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  1487. conf->fullsync == 0) {
  1488. *skipped = 1;
  1489. return max_sector - sector_nr;
  1490. }
  1491. /* before building a request, check if we can skip these blocks..
  1492. * This call the bitmap_start_sync doesn't actually record anything
  1493. */
  1494. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1495. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1496. /* We can skip this block, and probably several more */
  1497. *skipped = 1;
  1498. return sync_blocks;
  1499. }
  1500. /*
  1501. * If there is non-resync activity waiting for a turn,
  1502. * and resync is going fast enough,
  1503. * then let it though before starting on this new sync request.
  1504. */
  1505. if (!go_faster && conf->nr_waiting)
  1506. msleep_interruptible(1000);
  1507. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  1508. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  1509. raise_barrier(conf);
  1510. conf->next_resync = sector_nr;
  1511. rcu_read_lock();
  1512. /*
  1513. * If we get a correctably read error during resync or recovery,
  1514. * we might want to read from a different device. So we
  1515. * flag all drives that could conceivably be read from for READ,
  1516. * and any others (which will be non-In_sync devices) for WRITE.
  1517. * If a read fails, we try reading from something else for which READ
  1518. * is OK.
  1519. */
  1520. r1_bio->mddev = mddev;
  1521. r1_bio->sector = sector_nr;
  1522. r1_bio->state = 0;
  1523. set_bit(R1BIO_IsSync, &r1_bio->state);
  1524. for (i=0; i < conf->raid_disks; i++) {
  1525. mdk_rdev_t *rdev;
  1526. bio = r1_bio->bios[i];
  1527. /* take from bio_init */
  1528. bio->bi_next = NULL;
  1529. bio->bi_flags &= ~(BIO_POOL_MASK-1);
  1530. bio->bi_flags |= 1 << BIO_UPTODATE;
  1531. bio->bi_comp_cpu = -1;
  1532. bio->bi_rw = READ;
  1533. bio->bi_vcnt = 0;
  1534. bio->bi_idx = 0;
  1535. bio->bi_phys_segments = 0;
  1536. bio->bi_size = 0;
  1537. bio->bi_end_io = NULL;
  1538. bio->bi_private = NULL;
  1539. rdev = rcu_dereference(conf->mirrors[i].rdev);
  1540. if (rdev == NULL ||
  1541. test_bit(Faulty, &rdev->flags)) {
  1542. still_degraded = 1;
  1543. continue;
  1544. } else if (!test_bit(In_sync, &rdev->flags)) {
  1545. bio->bi_rw = WRITE;
  1546. bio->bi_end_io = end_sync_write;
  1547. write_targets ++;
  1548. } else {
  1549. /* may need to read from here */
  1550. bio->bi_rw = READ;
  1551. bio->bi_end_io = end_sync_read;
  1552. if (test_bit(WriteMostly, &rdev->flags)) {
  1553. if (wonly < 0)
  1554. wonly = i;
  1555. } else {
  1556. if (disk < 0)
  1557. disk = i;
  1558. }
  1559. read_targets++;
  1560. }
  1561. atomic_inc(&rdev->nr_pending);
  1562. bio->bi_sector = sector_nr + rdev->data_offset;
  1563. bio->bi_bdev = rdev->bdev;
  1564. bio->bi_private = r1_bio;
  1565. }
  1566. rcu_read_unlock();
  1567. if (disk < 0)
  1568. disk = wonly;
  1569. r1_bio->read_disk = disk;
  1570. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  1571. /* extra read targets are also write targets */
  1572. write_targets += read_targets-1;
  1573. if (write_targets == 0 || read_targets == 0) {
  1574. /* There is nowhere to write, so all non-sync
  1575. * drives must be failed - so we are finished
  1576. */
  1577. sector_t rv = max_sector - sector_nr;
  1578. *skipped = 1;
  1579. put_buf(r1_bio);
  1580. return rv;
  1581. }
  1582. if (max_sector > mddev->resync_max)
  1583. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  1584. nr_sectors = 0;
  1585. sync_blocks = 0;
  1586. do {
  1587. struct page *page;
  1588. int len = PAGE_SIZE;
  1589. if (sector_nr + (len>>9) > max_sector)
  1590. len = (max_sector - sector_nr) << 9;
  1591. if (len == 0)
  1592. break;
  1593. if (sync_blocks == 0) {
  1594. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1595. &sync_blocks, still_degraded) &&
  1596. !conf->fullsync &&
  1597. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1598. break;
  1599. BUG_ON(sync_blocks < (PAGE_SIZE>>9));
  1600. if ((len >> 9) > sync_blocks)
  1601. len = sync_blocks<<9;
  1602. }
  1603. for (i=0 ; i < conf->raid_disks; i++) {
  1604. bio = r1_bio->bios[i];
  1605. if (bio->bi_end_io) {
  1606. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  1607. if (bio_add_page(bio, page, len, 0) == 0) {
  1608. /* stop here */
  1609. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1610. while (i > 0) {
  1611. i--;
  1612. bio = r1_bio->bios[i];
  1613. if (bio->bi_end_io==NULL)
  1614. continue;
  1615. /* remove last page from this bio */
  1616. bio->bi_vcnt--;
  1617. bio->bi_size -= len;
  1618. bio->bi_flags &= ~(1<< BIO_SEG_VALID);
  1619. }
  1620. goto bio_full;
  1621. }
  1622. }
  1623. }
  1624. nr_sectors += len>>9;
  1625. sector_nr += len>>9;
  1626. sync_blocks -= (len>>9);
  1627. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  1628. bio_full:
  1629. r1_bio->sectors = nr_sectors;
  1630. /* For a user-requested sync, we read all readable devices and do a
  1631. * compare
  1632. */
  1633. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1634. atomic_set(&r1_bio->remaining, read_targets);
  1635. for (i=0; i<conf->raid_disks; i++) {
  1636. bio = r1_bio->bios[i];
  1637. if (bio->bi_end_io == end_sync_read) {
  1638. md_sync_acct(bio->bi_bdev, nr_sectors);
  1639. generic_make_request(bio);
  1640. }
  1641. }
  1642. } else {
  1643. atomic_set(&r1_bio->remaining, 1);
  1644. bio = r1_bio->bios[r1_bio->read_disk];
  1645. md_sync_acct(bio->bi_bdev, nr_sectors);
  1646. generic_make_request(bio);
  1647. }
  1648. return nr_sectors;
  1649. }
  1650. static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  1651. {
  1652. if (sectors)
  1653. return sectors;
  1654. return mddev->dev_sectors;
  1655. }
  1656. static conf_t *setup_conf(mddev_t *mddev)
  1657. {
  1658. conf_t *conf;
  1659. int i;
  1660. mirror_info_t *disk;
  1661. mdk_rdev_t *rdev;
  1662. int err = -ENOMEM;
  1663. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  1664. if (!conf)
  1665. goto abort;
  1666. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1667. GFP_KERNEL);
  1668. if (!conf->mirrors)
  1669. goto abort;
  1670. conf->tmppage = alloc_page(GFP_KERNEL);
  1671. if (!conf->tmppage)
  1672. goto abort;
  1673. conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  1674. if (!conf->poolinfo)
  1675. goto abort;
  1676. conf->poolinfo->raid_disks = mddev->raid_disks;
  1677. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1678. r1bio_pool_free,
  1679. conf->poolinfo);
  1680. if (!conf->r1bio_pool)
  1681. goto abort;
  1682. conf->poolinfo->mddev = mddev;
  1683. spin_lock_init(&conf->device_lock);
  1684. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1685. int disk_idx = rdev->raid_disk;
  1686. if (disk_idx >= mddev->raid_disks
  1687. || disk_idx < 0)
  1688. continue;
  1689. disk = conf->mirrors + disk_idx;
  1690. disk->rdev = rdev;
  1691. disk->head_position = 0;
  1692. }
  1693. conf->raid_disks = mddev->raid_disks;
  1694. conf->mddev = mddev;
  1695. INIT_LIST_HEAD(&conf->retry_list);
  1696. spin_lock_init(&conf->resync_lock);
  1697. init_waitqueue_head(&conf->wait_barrier);
  1698. bio_list_init(&conf->pending_bio_list);
  1699. conf->last_used = -1;
  1700. for (i = 0; i < conf->raid_disks; i++) {
  1701. disk = conf->mirrors + i;
  1702. if (!disk->rdev ||
  1703. !test_bit(In_sync, &disk->rdev->flags)) {
  1704. disk->head_position = 0;
  1705. if (disk->rdev)
  1706. conf->fullsync = 1;
  1707. } else if (conf->last_used < 0)
  1708. /*
  1709. * The first working device is used as a
  1710. * starting point to read balancing.
  1711. */
  1712. conf->last_used = i;
  1713. }
  1714. err = -EIO;
  1715. if (conf->last_used < 0) {
  1716. printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
  1717. mdname(mddev));
  1718. goto abort;
  1719. }
  1720. err = -ENOMEM;
  1721. conf->thread = md_register_thread(raid1d, mddev, NULL);
  1722. if (!conf->thread) {
  1723. printk(KERN_ERR
  1724. "md/raid1:%s: couldn't allocate thread\n",
  1725. mdname(mddev));
  1726. goto abort;
  1727. }
  1728. return conf;
  1729. abort:
  1730. if (conf) {
  1731. if (conf->r1bio_pool)
  1732. mempool_destroy(conf->r1bio_pool);
  1733. kfree(conf->mirrors);
  1734. safe_put_page(conf->tmppage);
  1735. kfree(conf->poolinfo);
  1736. kfree(conf);
  1737. }
  1738. return ERR_PTR(err);
  1739. }
  1740. static int run(mddev_t *mddev)
  1741. {
  1742. conf_t *conf;
  1743. int i;
  1744. mdk_rdev_t *rdev;
  1745. if (mddev->level != 1) {
  1746. printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
  1747. mdname(mddev), mddev->level);
  1748. return -EIO;
  1749. }
  1750. if (mddev->reshape_position != MaxSector) {
  1751. printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
  1752. mdname(mddev));
  1753. return -EIO;
  1754. }
  1755. /*
  1756. * copy the already verified devices into our private RAID1
  1757. * bookkeeping area. [whatever we allocate in run(),
  1758. * should be freed in stop()]
  1759. */
  1760. if (mddev->private == NULL)
  1761. conf = setup_conf(mddev);
  1762. else
  1763. conf = mddev->private;
  1764. if (IS_ERR(conf))
  1765. return PTR_ERR(conf);
  1766. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1767. if (!mddev->gendisk)
  1768. continue;
  1769. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1770. rdev->data_offset << 9);
  1771. /* as we don't honour merge_bvec_fn, we must never risk
  1772. * violating it, so limit ->max_segments to 1 lying within
  1773. * a single page, as a one page request is never in violation.
  1774. */
  1775. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  1776. blk_queue_max_segments(mddev->queue, 1);
  1777. blk_queue_segment_boundary(mddev->queue,
  1778. PAGE_CACHE_SIZE - 1);
  1779. }
  1780. if (rdev->badblocks.count) {
  1781. printk(KERN_ERR "md/raid1: Cannot handle bad blocks yet\n");
  1782. return -EINVAL;
  1783. }
  1784. }
  1785. mddev->degraded = 0;
  1786. for (i=0; i < conf->raid_disks; i++)
  1787. if (conf->mirrors[i].rdev == NULL ||
  1788. !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
  1789. test_bit(Faulty, &conf->mirrors[i].rdev->flags))
  1790. mddev->degraded++;
  1791. if (conf->raid_disks - mddev->degraded == 1)
  1792. mddev->recovery_cp = MaxSector;
  1793. if (mddev->recovery_cp != MaxSector)
  1794. printk(KERN_NOTICE "md/raid1:%s: not clean"
  1795. " -- starting background reconstruction\n",
  1796. mdname(mddev));
  1797. printk(KERN_INFO
  1798. "md/raid1:%s: active with %d out of %d mirrors\n",
  1799. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1800. mddev->raid_disks);
  1801. /*
  1802. * Ok, everything is just fine now
  1803. */
  1804. mddev->thread = conf->thread;
  1805. conf->thread = NULL;
  1806. mddev->private = conf;
  1807. md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
  1808. if (mddev->queue) {
  1809. mddev->queue->backing_dev_info.congested_fn = raid1_congested;
  1810. mddev->queue->backing_dev_info.congested_data = mddev;
  1811. }
  1812. return md_integrity_register(mddev);
  1813. }
  1814. static int stop(mddev_t *mddev)
  1815. {
  1816. conf_t *conf = mddev->private;
  1817. struct bitmap *bitmap = mddev->bitmap;
  1818. /* wait for behind writes to complete */
  1819. if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
  1820. printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
  1821. mdname(mddev));
  1822. /* need to kick something here to make sure I/O goes? */
  1823. wait_event(bitmap->behind_wait,
  1824. atomic_read(&bitmap->behind_writes) == 0);
  1825. }
  1826. raise_barrier(conf);
  1827. lower_barrier(conf);
  1828. md_unregister_thread(mddev->thread);
  1829. mddev->thread = NULL;
  1830. if (conf->r1bio_pool)
  1831. mempool_destroy(conf->r1bio_pool);
  1832. kfree(conf->mirrors);
  1833. kfree(conf->poolinfo);
  1834. kfree(conf);
  1835. mddev->private = NULL;
  1836. return 0;
  1837. }
  1838. static int raid1_resize(mddev_t *mddev, sector_t sectors)
  1839. {
  1840. /* no resync is happening, and there is enough space
  1841. * on all devices, so we can resize.
  1842. * We need to make sure resync covers any new space.
  1843. * If the array is shrinking we should possibly wait until
  1844. * any io in the removed space completes, but it hardly seems
  1845. * worth it.
  1846. */
  1847. md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
  1848. if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
  1849. return -EINVAL;
  1850. set_capacity(mddev->gendisk, mddev->array_sectors);
  1851. revalidate_disk(mddev->gendisk);
  1852. if (sectors > mddev->dev_sectors &&
  1853. mddev->recovery_cp > mddev->dev_sectors) {
  1854. mddev->recovery_cp = mddev->dev_sectors;
  1855. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1856. }
  1857. mddev->dev_sectors = sectors;
  1858. mddev->resync_max_sectors = sectors;
  1859. return 0;
  1860. }
  1861. static int raid1_reshape(mddev_t *mddev)
  1862. {
  1863. /* We need to:
  1864. * 1/ resize the r1bio_pool
  1865. * 2/ resize conf->mirrors
  1866. *
  1867. * We allocate a new r1bio_pool if we can.
  1868. * Then raise a device barrier and wait until all IO stops.
  1869. * Then resize conf->mirrors and swap in the new r1bio pool.
  1870. *
  1871. * At the same time, we "pack" the devices so that all the missing
  1872. * devices have the higher raid_disk numbers.
  1873. */
  1874. mempool_t *newpool, *oldpool;
  1875. struct pool_info *newpoolinfo;
  1876. mirror_info_t *newmirrors;
  1877. conf_t *conf = mddev->private;
  1878. int cnt, raid_disks;
  1879. unsigned long flags;
  1880. int d, d2, err;
  1881. /* Cannot change chunk_size, layout, or level */
  1882. if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
  1883. mddev->layout != mddev->new_layout ||
  1884. mddev->level != mddev->new_level) {
  1885. mddev->new_chunk_sectors = mddev->chunk_sectors;
  1886. mddev->new_layout = mddev->layout;
  1887. mddev->new_level = mddev->level;
  1888. return -EINVAL;
  1889. }
  1890. err = md_allow_write(mddev);
  1891. if (err)
  1892. return err;
  1893. raid_disks = mddev->raid_disks + mddev->delta_disks;
  1894. if (raid_disks < conf->raid_disks) {
  1895. cnt=0;
  1896. for (d= 0; d < conf->raid_disks; d++)
  1897. if (conf->mirrors[d].rdev)
  1898. cnt++;
  1899. if (cnt > raid_disks)
  1900. return -EBUSY;
  1901. }
  1902. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  1903. if (!newpoolinfo)
  1904. return -ENOMEM;
  1905. newpoolinfo->mddev = mddev;
  1906. newpoolinfo->raid_disks = raid_disks;
  1907. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1908. r1bio_pool_free, newpoolinfo);
  1909. if (!newpool) {
  1910. kfree(newpoolinfo);
  1911. return -ENOMEM;
  1912. }
  1913. newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
  1914. if (!newmirrors) {
  1915. kfree(newpoolinfo);
  1916. mempool_destroy(newpool);
  1917. return -ENOMEM;
  1918. }
  1919. raise_barrier(conf);
  1920. /* ok, everything is stopped */
  1921. oldpool = conf->r1bio_pool;
  1922. conf->r1bio_pool = newpool;
  1923. for (d = d2 = 0; d < conf->raid_disks; d++) {
  1924. mdk_rdev_t *rdev = conf->mirrors[d].rdev;
  1925. if (rdev && rdev->raid_disk != d2) {
  1926. sysfs_unlink_rdev(mddev, rdev);
  1927. rdev->raid_disk = d2;
  1928. sysfs_unlink_rdev(mddev, rdev);
  1929. if (sysfs_link_rdev(mddev, rdev))
  1930. printk(KERN_WARNING
  1931. "md/raid1:%s: cannot register rd%d\n",
  1932. mdname(mddev), rdev->raid_disk);
  1933. }
  1934. if (rdev)
  1935. newmirrors[d2++].rdev = rdev;
  1936. }
  1937. kfree(conf->mirrors);
  1938. conf->mirrors = newmirrors;
  1939. kfree(conf->poolinfo);
  1940. conf->poolinfo = newpoolinfo;
  1941. spin_lock_irqsave(&conf->device_lock, flags);
  1942. mddev->degraded += (raid_disks - conf->raid_disks);
  1943. spin_unlock_irqrestore(&conf->device_lock, flags);
  1944. conf->raid_disks = mddev->raid_disks = raid_disks;
  1945. mddev->delta_disks = 0;
  1946. conf->last_used = 0; /* just make sure it is in-range */
  1947. lower_barrier(conf);
  1948. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1949. md_wakeup_thread(mddev->thread);
  1950. mempool_destroy(oldpool);
  1951. return 0;
  1952. }
  1953. static void raid1_quiesce(mddev_t *mddev, int state)
  1954. {
  1955. conf_t *conf = mddev->private;
  1956. switch(state) {
  1957. case 2: /* wake for suspend */
  1958. wake_up(&conf->wait_barrier);
  1959. break;
  1960. case 1:
  1961. raise_barrier(conf);
  1962. break;
  1963. case 0:
  1964. lower_barrier(conf);
  1965. break;
  1966. }
  1967. }
  1968. static void *raid1_takeover(mddev_t *mddev)
  1969. {
  1970. /* raid1 can take over:
  1971. * raid5 with 2 devices, any layout or chunk size
  1972. */
  1973. if (mddev->level == 5 && mddev->raid_disks == 2) {
  1974. conf_t *conf;
  1975. mddev->new_level = 1;
  1976. mddev->new_layout = 0;
  1977. mddev->new_chunk_sectors = 0;
  1978. conf = setup_conf(mddev);
  1979. if (!IS_ERR(conf))
  1980. conf->barrier = 1;
  1981. return conf;
  1982. }
  1983. return ERR_PTR(-EINVAL);
  1984. }
  1985. static struct mdk_personality raid1_personality =
  1986. {
  1987. .name = "raid1",
  1988. .level = 1,
  1989. .owner = THIS_MODULE,
  1990. .make_request = make_request,
  1991. .run = run,
  1992. .stop = stop,
  1993. .status = status,
  1994. .error_handler = error,
  1995. .hot_add_disk = raid1_add_disk,
  1996. .hot_remove_disk= raid1_remove_disk,
  1997. .spare_active = raid1_spare_active,
  1998. .sync_request = sync_request,
  1999. .resize = raid1_resize,
  2000. .size = raid1_size,
  2001. .check_reshape = raid1_reshape,
  2002. .quiesce = raid1_quiesce,
  2003. .takeover = raid1_takeover,
  2004. };
  2005. static int __init raid_init(void)
  2006. {
  2007. return register_md_personality(&raid1_personality);
  2008. }
  2009. static void raid_exit(void)
  2010. {
  2011. unregister_md_personality(&raid1_personality);
  2012. }
  2013. module_init(raid_init);
  2014. module_exit(raid_exit);
  2015. MODULE_LICENSE("GPL");
  2016. MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
  2017. MODULE_ALIAS("md-personality-3"); /* RAID1 */
  2018. MODULE_ALIAS("md-raid1");
  2019. MODULE_ALIAS("md-level-1");