raid1.c 40 KB

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