md-multipath.c 13 KB

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