raid1.c 44 KB

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