raid1.c 59 KB

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