raid1.c 54 KB

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