multipath.c 14 KB

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