raid1.c 49 KB

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