raid1.c 56 KB

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