multipath.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * multipath.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. * MULTIPATH management functions.
  9. *
  10. * derived from raid1.c.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * (for example /usr/src/linux/COPYING); if not, write to the Free
  19. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/raid/multipath.h>
  25. #include <linux/buffer_head.h>
  26. #include <asm/atomic.h>
  27. #define MAJOR_NR MD_MAJOR
  28. #define MD_DRIVER
  29. #define MD_PERSONALITY
  30. #define MAX_WORK_PER_DISK 128
  31. #define NR_RESERVED_BUFS 32
  32. static int multipath_map (multipath_conf_t *conf)
  33. {
  34. int i, disks = conf->raid_disks;
  35. /*
  36. * Later we do read balancing on the read side
  37. * now we use the first available disk.
  38. */
  39. rcu_read_lock();
  40. for (i = 0; i < disks; i++) {
  41. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  42. if (rdev && test_bit(In_sync, &rdev->flags)) {
  43. atomic_inc(&rdev->nr_pending);
  44. rcu_read_unlock();
  45. return i;
  46. }
  47. }
  48. rcu_read_unlock();
  49. printk(KERN_ERR "multipath_map(): no more operational IO paths?\n");
  50. return (-1);
  51. }
  52. static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
  53. {
  54. unsigned long flags;
  55. mddev_t *mddev = mp_bh->mddev;
  56. multipath_conf_t *conf = mddev_to_conf(mddev);
  57. spin_lock_irqsave(&conf->device_lock, flags);
  58. list_add(&mp_bh->retry_list, &conf->retry_list);
  59. spin_unlock_irqrestore(&conf->device_lock, flags);
  60. md_wakeup_thread(mddev->thread);
  61. }
  62. /*
  63. * multipath_end_bh_io() is called when we have finished servicing a multipathed
  64. * operation and are ready to return a success/failure code to the buffer
  65. * cache layer.
  66. */
  67. static void multipath_end_bh_io (struct multipath_bh *mp_bh, int err)
  68. {
  69. struct bio *bio = mp_bh->master_bio;
  70. multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
  71. bio_endio(bio, err);
  72. mempool_free(mp_bh, conf->pool);
  73. }
  74. static void multipath_end_request(struct bio *bio, int error)
  75. {
  76. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  77. struct multipath_bh * mp_bh = (struct multipath_bh *)(bio->bi_private);
  78. multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
  79. mdk_rdev_t *rdev = conf->multipaths[mp_bh->path].rdev;
  80. if (uptodate)
  81. multipath_end_bh_io(mp_bh, 0);
  82. else if (!bio_rw_ahead(bio)) {
  83. /*
  84. * oops, IO error:
  85. */
  86. char b[BDEVNAME_SIZE];
  87. md_error (mp_bh->mddev, rdev);
  88. printk(KERN_ERR "multipath: %s: rescheduling sector %llu\n",
  89. bdevname(rdev->bdev,b),
  90. (unsigned long long)bio->bi_sector);
  91. multipath_reschedule_retry(mp_bh);
  92. } else
  93. multipath_end_bh_io(mp_bh, error);
  94. rdev_dec_pending(rdev, conf->mddev);
  95. }
  96. static void unplug_slaves(mddev_t *mddev)
  97. {
  98. multipath_conf_t *conf = mddev_to_conf(mddev);
  99. int i;
  100. rcu_read_lock();
  101. for (i=0; i<mddev->raid_disks; i++) {
  102. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  103. if (rdev && !test_bit(Faulty, &rdev->flags)
  104. && atomic_read(&rdev->nr_pending)) {
  105. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  106. atomic_inc(&rdev->nr_pending);
  107. rcu_read_unlock();
  108. if (r_queue->unplug_fn)
  109. r_queue->unplug_fn(r_queue);
  110. rdev_dec_pending(rdev, mddev);
  111. rcu_read_lock();
  112. }
  113. }
  114. rcu_read_unlock();
  115. }
  116. static void multipath_unplug(struct request_queue *q)
  117. {
  118. unplug_slaves(q->queuedata);
  119. }
  120. static int multipath_make_request (struct request_queue *q, struct bio * bio)
  121. {
  122. mddev_t *mddev = q->queuedata;
  123. multipath_conf_t *conf = mddev_to_conf(mddev);
  124. struct multipath_bh * mp_bh;
  125. struct multipath_info *multipath;
  126. const int rw = bio_data_dir(bio);
  127. if (unlikely(bio_barrier(bio))) {
  128. bio_endio(bio, -EOPNOTSUPP);
  129. return 0;
  130. }
  131. mp_bh = mempool_alloc(conf->pool, GFP_NOIO);
  132. mp_bh->master_bio = bio;
  133. mp_bh->mddev = mddev;
  134. disk_stat_inc(mddev->gendisk, ios[rw]);
  135. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
  136. mp_bh->path = multipath_map(conf);
  137. if (mp_bh->path < 0) {
  138. bio_endio(bio, -EIO);
  139. mempool_free(mp_bh, conf->pool);
  140. return 0;
  141. }
  142. multipath = conf->multipaths + mp_bh->path;
  143. mp_bh->bio = *bio;
  144. mp_bh->bio.bi_sector += multipath->rdev->data_offset;
  145. mp_bh->bio.bi_bdev = multipath->rdev->bdev;
  146. mp_bh->bio.bi_rw |= (1 << BIO_RW_FAILFAST);
  147. mp_bh->bio.bi_end_io = multipath_end_request;
  148. mp_bh->bio.bi_private = mp_bh;
  149. generic_make_request(&mp_bh->bio);
  150. return 0;
  151. }
  152. static void multipath_status (struct seq_file *seq, mddev_t *mddev)
  153. {
  154. multipath_conf_t *conf = mddev_to_conf(mddev);
  155. int i;
  156. seq_printf (seq, " [%d/%d] [", conf->raid_disks,
  157. conf->working_disks);
  158. for (i = 0; i < conf->raid_disks; i++)
  159. seq_printf (seq, "%s",
  160. conf->multipaths[i].rdev &&
  161. test_bit(In_sync, &conf->multipaths[i].rdev->flags) ? "U" : "_");
  162. seq_printf (seq, "]");
  163. }
  164. static int multipath_issue_flush(struct request_queue *q, struct gendisk *disk,
  165. sector_t *error_sector)
  166. {
  167. mddev_t *mddev = q->queuedata;
  168. multipath_conf_t *conf = mddev_to_conf(mddev);
  169. int i, ret = 0;
  170. rcu_read_lock();
  171. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  172. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  173. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  174. struct block_device *bdev = rdev->bdev;
  175. struct request_queue *r_queue = bdev_get_queue(bdev);
  176. if (!r_queue->issue_flush_fn)
  177. ret = -EOPNOTSUPP;
  178. else {
  179. atomic_inc(&rdev->nr_pending);
  180. rcu_read_unlock();
  181. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  182. error_sector);
  183. rdev_dec_pending(rdev, mddev);
  184. rcu_read_lock();
  185. }
  186. }
  187. }
  188. rcu_read_unlock();
  189. return ret;
  190. }
  191. static int multipath_congested(void *data, int bits)
  192. {
  193. mddev_t *mddev = data;
  194. multipath_conf_t *conf = mddev_to_conf(mddev);
  195. int i, ret = 0;
  196. rcu_read_lock();
  197. for (i = 0; i < mddev->raid_disks ; i++) {
  198. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  199. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  200. struct request_queue *q = bdev_get_queue(rdev->bdev);
  201. ret |= bdi_congested(&q->backing_dev_info, bits);
  202. /* Just like multipath_map, we just check the
  203. * first available device
  204. */
  205. break;
  206. }
  207. }
  208. rcu_read_unlock();
  209. return ret;
  210. }
  211. /*
  212. * Careful, this can execute in IRQ contexts as well!
  213. */
  214. static void multipath_error (mddev_t *mddev, mdk_rdev_t *rdev)
  215. {
  216. multipath_conf_t *conf = mddev_to_conf(mddev);
  217. if (conf->working_disks <= 1) {
  218. /*
  219. * Uh oh, we can do nothing if this is our last path, but
  220. * first check if this is a queued request for a device
  221. * which has just failed.
  222. */
  223. printk(KERN_ALERT
  224. "multipath: only one IO path left and IO error.\n");
  225. /* leave it active... it's all we have */
  226. } else {
  227. /*
  228. * Mark disk as unusable
  229. */
  230. if (!test_bit(Faulty, &rdev->flags)) {
  231. char b[BDEVNAME_SIZE];
  232. clear_bit(In_sync, &rdev->flags);
  233. set_bit(Faulty, &rdev->flags);
  234. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  235. conf->working_disks--;
  236. mddev->degraded++;
  237. printk(KERN_ALERT "multipath: IO failure on %s,"
  238. " disabling IO path. \n Operation continuing"
  239. " on %d IO paths.\n",
  240. bdevname (rdev->bdev,b),
  241. conf->working_disks);
  242. }
  243. }
  244. }
  245. static void print_multipath_conf (multipath_conf_t *conf)
  246. {
  247. int i;
  248. struct multipath_info *tmp;
  249. printk("MULTIPATH conf printout:\n");
  250. if (!conf) {
  251. printk("(conf==NULL)\n");
  252. return;
  253. }
  254. printk(" --- wd:%d rd:%d\n", conf->working_disks,
  255. conf->raid_disks);
  256. for (i = 0; i < conf->raid_disks; i++) {
  257. char b[BDEVNAME_SIZE];
  258. tmp = conf->multipaths + i;
  259. if (tmp->rdev)
  260. printk(" disk%d, o:%d, dev:%s\n",
  261. i,!test_bit(Faulty, &tmp->rdev->flags),
  262. bdevname(tmp->rdev->bdev,b));
  263. }
  264. }
  265. static int multipath_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  266. {
  267. multipath_conf_t *conf = mddev->private;
  268. struct request_queue *q;
  269. int found = 0;
  270. int path;
  271. struct multipath_info *p;
  272. print_multipath_conf(conf);
  273. for (path=0; path<mddev->raid_disks; path++)
  274. if ((p=conf->multipaths+path)->rdev == NULL) {
  275. q = rdev->bdev->bd_disk->queue;
  276. blk_queue_stack_limits(mddev->queue, q);
  277. /* as we don't honour merge_bvec_fn, we must never risk
  278. * violating it, so limit ->max_sector to one PAGE, as
  279. * a one page request is never in violation.
  280. * (Note: it is very unlikely that a device with
  281. * merge_bvec_fn will be involved in multipath.)
  282. */
  283. if (q->merge_bvec_fn &&
  284. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  285. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  286. conf->working_disks++;
  287. mddev->degraded--;
  288. rdev->raid_disk = path;
  289. set_bit(In_sync, &rdev->flags);
  290. rcu_assign_pointer(p->rdev, rdev);
  291. found = 1;
  292. }
  293. print_multipath_conf(conf);
  294. return found;
  295. }
  296. static int multipath_remove_disk(mddev_t *mddev, int number)
  297. {
  298. multipath_conf_t *conf = mddev->private;
  299. int err = 0;
  300. mdk_rdev_t *rdev;
  301. struct multipath_info *p = conf->multipaths + number;
  302. print_multipath_conf(conf);
  303. rdev = p->rdev;
  304. if (rdev) {
  305. if (test_bit(In_sync, &rdev->flags) ||
  306. atomic_read(&rdev->nr_pending)) {
  307. printk(KERN_ERR "hot-remove-disk, slot %d is identified" " but is still operational!\n", number);
  308. err = -EBUSY;
  309. goto abort;
  310. }
  311. p->rdev = NULL;
  312. synchronize_rcu();
  313. if (atomic_read(&rdev->nr_pending)) {
  314. /* lost the race, try later */
  315. err = -EBUSY;
  316. p->rdev = rdev;
  317. }
  318. }
  319. abort:
  320. print_multipath_conf(conf);
  321. return err;
  322. }
  323. /*
  324. * This is a kernel thread which:
  325. *
  326. * 1. Retries failed read operations on working multipaths.
  327. * 2. Updates the raid superblock when problems encounter.
  328. * 3. Performs writes following reads for array syncronising.
  329. */
  330. static void multipathd (mddev_t *mddev)
  331. {
  332. struct multipath_bh *mp_bh;
  333. struct bio *bio;
  334. unsigned long flags;
  335. multipath_conf_t *conf = mddev_to_conf(mddev);
  336. struct list_head *head = &conf->retry_list;
  337. md_check_recovery(mddev);
  338. for (;;) {
  339. char b[BDEVNAME_SIZE];
  340. spin_lock_irqsave(&conf->device_lock, flags);
  341. if (list_empty(head))
  342. break;
  343. mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
  344. list_del(head->prev);
  345. spin_unlock_irqrestore(&conf->device_lock, flags);
  346. bio = &mp_bh->bio;
  347. bio->bi_sector = mp_bh->master_bio->bi_sector;
  348. if ((mp_bh->path = multipath_map (conf))<0) {
  349. printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
  350. " error for block %llu\n",
  351. bdevname(bio->bi_bdev,b),
  352. (unsigned long long)bio->bi_sector);
  353. multipath_end_bh_io(mp_bh, -EIO);
  354. } else {
  355. printk(KERN_ERR "multipath: %s: redirecting sector %llu"
  356. " to another IO path\n",
  357. bdevname(bio->bi_bdev,b),
  358. (unsigned long long)bio->bi_sector);
  359. *bio = *(mp_bh->master_bio);
  360. bio->bi_sector += conf->multipaths[mp_bh->path].rdev->data_offset;
  361. bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev;
  362. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  363. bio->bi_end_io = multipath_end_request;
  364. bio->bi_private = mp_bh;
  365. generic_make_request(bio);
  366. }
  367. }
  368. spin_unlock_irqrestore(&conf->device_lock, flags);
  369. }
  370. static int multipath_run (mddev_t *mddev)
  371. {
  372. multipath_conf_t *conf;
  373. int disk_idx;
  374. struct multipath_info *disk;
  375. mdk_rdev_t *rdev;
  376. struct list_head *tmp;
  377. if (mddev->level != LEVEL_MULTIPATH) {
  378. printk("multipath: %s: raid level not set to multipath IO (%d)\n",
  379. mdname(mddev), mddev->level);
  380. goto out;
  381. }
  382. /*
  383. * copy the already verified devices into our private MULTIPATH
  384. * bookkeeping area. [whatever we allocate in multipath_run(),
  385. * should be freed in multipath_stop()]
  386. */
  387. conf = kzalloc(sizeof(multipath_conf_t), GFP_KERNEL);
  388. mddev->private = conf;
  389. if (!conf) {
  390. printk(KERN_ERR
  391. "multipath: couldn't allocate memory for %s\n",
  392. mdname(mddev));
  393. goto out;
  394. }
  395. conf->multipaths = kzalloc(sizeof(struct multipath_info)*mddev->raid_disks,
  396. GFP_KERNEL);
  397. if (!conf->multipaths) {
  398. printk(KERN_ERR
  399. "multipath: couldn't allocate memory for %s\n",
  400. mdname(mddev));
  401. goto out_free_conf;
  402. }
  403. conf->working_disks = 0;
  404. ITERATE_RDEV(mddev,rdev,tmp) {
  405. disk_idx = rdev->raid_disk;
  406. if (disk_idx < 0 ||
  407. disk_idx >= mddev->raid_disks)
  408. continue;
  409. disk = conf->multipaths + disk_idx;
  410. disk->rdev = rdev;
  411. blk_queue_stack_limits(mddev->queue,
  412. rdev->bdev->bd_disk->queue);
  413. /* as we don't honour merge_bvec_fn, we must never risk
  414. * violating it, not that we ever expect a device with
  415. * a merge_bvec_fn to be involved in multipath */
  416. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  417. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  418. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  419. if (!test_bit(Faulty, &rdev->flags))
  420. conf->working_disks++;
  421. }
  422. conf->raid_disks = mddev->raid_disks;
  423. conf->mddev = mddev;
  424. spin_lock_init(&conf->device_lock);
  425. INIT_LIST_HEAD(&conf->retry_list);
  426. if (!conf->working_disks) {
  427. printk(KERN_ERR "multipath: no operational IO paths for %s\n",
  428. mdname(mddev));
  429. goto out_free_conf;
  430. }
  431. mddev->degraded = conf->raid_disks - conf->working_disks;
  432. conf->pool = mempool_create_kzalloc_pool(NR_RESERVED_BUFS,
  433. sizeof(struct multipath_bh));
  434. if (conf->pool == NULL) {
  435. printk(KERN_ERR
  436. "multipath: couldn't allocate memory for %s\n",
  437. mdname(mddev));
  438. goto out_free_conf;
  439. }
  440. {
  441. mddev->thread = md_register_thread(multipathd, mddev, "%s_multipath");
  442. if (!mddev->thread) {
  443. printk(KERN_ERR "multipath: couldn't allocate thread"
  444. " for %s\n", mdname(mddev));
  445. goto out_free_conf;
  446. }
  447. }
  448. printk(KERN_INFO
  449. "multipath: array %s active with %d out of %d IO paths\n",
  450. mdname(mddev), conf->working_disks, mddev->raid_disks);
  451. /*
  452. * Ok, everything is just fine now
  453. */
  454. mddev->array_size = mddev->size;
  455. mddev->queue->unplug_fn = multipath_unplug;
  456. mddev->queue->issue_flush_fn = multipath_issue_flush;
  457. mddev->queue->backing_dev_info.congested_fn = multipath_congested;
  458. mddev->queue->backing_dev_info.congested_data = mddev;
  459. return 0;
  460. out_free_conf:
  461. if (conf->pool)
  462. mempool_destroy(conf->pool);
  463. kfree(conf->multipaths);
  464. kfree(conf);
  465. mddev->private = NULL;
  466. out:
  467. return -EIO;
  468. }
  469. static int multipath_stop (mddev_t *mddev)
  470. {
  471. multipath_conf_t *conf = mddev_to_conf(mddev);
  472. md_unregister_thread(mddev->thread);
  473. mddev->thread = NULL;
  474. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  475. mempool_destroy(conf->pool);
  476. kfree(conf->multipaths);
  477. kfree(conf);
  478. mddev->private = NULL;
  479. return 0;
  480. }
  481. static struct mdk_personality multipath_personality =
  482. {
  483. .name = "multipath",
  484. .level = LEVEL_MULTIPATH,
  485. .owner = THIS_MODULE,
  486. .make_request = multipath_make_request,
  487. .run = multipath_run,
  488. .stop = multipath_stop,
  489. .status = multipath_status,
  490. .error_handler = multipath_error,
  491. .hot_add_disk = multipath_add_disk,
  492. .hot_remove_disk= multipath_remove_disk,
  493. };
  494. static int __init multipath_init (void)
  495. {
  496. return register_md_personality (&multipath_personality);
  497. }
  498. static void __exit multipath_exit (void)
  499. {
  500. unregister_md_personality (&multipath_personality);
  501. }
  502. module_init(multipath_init);
  503. module_exit(multipath_exit);
  504. MODULE_LICENSE("GPL");
  505. MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
  506. MODULE_ALIAS("md-multipath");
  507. MODULE_ALIAS("md-level--4");