md-multipath.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. md_error (mp_bh->mddev, rdev);
  78. pr_info("multipath: %pg: rescheduling sector %llu\n",
  79. rdev->bdev,
  80. (unsigned long long)bio->bi_iter.bi_sector);
  81. multipath_reschedule_retry(mp_bh);
  82. } else
  83. multipath_end_bh_io(mp_bh, bio->bi_status);
  84. rdev_dec_pending(rdev, conf->mddev);
  85. }
  86. static bool multipath_make_request(struct mddev *mddev, struct bio * bio)
  87. {
  88. struct mpconf *conf = mddev->private;
  89. struct multipath_bh * mp_bh;
  90. struct multipath_info *multipath;
  91. if (unlikely(bio->bi_opf & REQ_PREFLUSH)
  92. && md_flush_request(mddev, bio))
  93. return true;
  94. mp_bh = mempool_alloc(&conf->pool, GFP_NOIO);
  95. mp_bh->master_bio = bio;
  96. mp_bh->mddev = mddev;
  97. mp_bh->path = multipath_map(conf);
  98. if (mp_bh->path < 0) {
  99. bio_io_error(bio);
  100. mempool_free(mp_bh, &conf->pool);
  101. return true;
  102. }
  103. multipath = conf->multipaths + mp_bh->path;
  104. bio_init_clone(multipath->rdev->bdev, &mp_bh->bio, bio, GFP_NOIO);
  105. mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset;
  106. mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT;
  107. mp_bh->bio.bi_end_io = multipath_end_request;
  108. mp_bh->bio.bi_private = mp_bh;
  109. mddev_check_write_zeroes(mddev, &mp_bh->bio);
  110. submit_bio_noacct(&mp_bh->bio);
  111. return true;
  112. }
  113. static void multipath_status(struct seq_file *seq, struct mddev *mddev)
  114. {
  115. struct mpconf *conf = mddev->private;
  116. int i;
  117. seq_printf (seq, " [%d/%d] [", conf->raid_disks,
  118. conf->raid_disks - mddev->degraded);
  119. rcu_read_lock();
  120. for (i = 0; i < conf->raid_disks; i++) {
  121. struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
  122. seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  123. }
  124. rcu_read_unlock();
  125. seq_putc(seq, ']');
  126. }
  127. /*
  128. * Careful, this can execute in IRQ contexts as well!
  129. */
  130. static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
  131. {
  132. struct mpconf *conf = mddev->private;
  133. if (conf->raid_disks - mddev->degraded <= 1) {
  134. /*
  135. * Uh oh, we can do nothing if this is our last path, but
  136. * first check if this is a queued request for a device
  137. * which has just failed.
  138. */
  139. pr_warn("multipath: only one IO path left and IO error.\n");
  140. /* leave it active... it's all we have */
  141. return;
  142. }
  143. /*
  144. * Mark disk as unusable
  145. */
  146. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  147. unsigned long flags;
  148. spin_lock_irqsave(&conf->device_lock, flags);
  149. mddev->degraded++;
  150. spin_unlock_irqrestore(&conf->device_lock, flags);
  151. }
  152. set_bit(Faulty, &rdev->flags);
  153. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  154. pr_err("multipath: IO failure on %pg, disabling IO path.\n"
  155. "multipath: Operation continuing on %d IO paths.\n",
  156. rdev->bdev,
  157. conf->raid_disks - mddev->degraded);
  158. }
  159. static void print_multipath_conf (struct mpconf *conf)
  160. {
  161. int i;
  162. struct multipath_info *tmp;
  163. pr_debug("MULTIPATH conf printout:\n");
  164. if (!conf) {
  165. pr_debug("(conf==NULL)\n");
  166. return;
  167. }
  168. pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  169. conf->raid_disks);
  170. for (i = 0; i < conf->raid_disks; i++) {
  171. tmp = conf->multipaths + i;
  172. if (tmp->rdev)
  173. pr_debug(" disk%d, o:%d, dev:%pg\n",
  174. i,!test_bit(Faulty, &tmp->rdev->flags),
  175. tmp->rdev->bdev);
  176. }
  177. }
  178. static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  179. {
  180. struct mpconf *conf = mddev->private;
  181. int err = -EEXIST;
  182. int path;
  183. struct multipath_info *p;
  184. int first = 0;
  185. int last = mddev->raid_disks - 1;
  186. if (rdev->raid_disk >= 0)
  187. first = last = rdev->raid_disk;
  188. print_multipath_conf(conf);
  189. for (path = first; path <= last; path++)
  190. if ((p=conf->multipaths+path)->rdev == NULL) {
  191. disk_stack_limits(mddev->gendisk, rdev->bdev,
  192. rdev->data_offset << 9);
  193. err = md_integrity_add_rdev(rdev, mddev);
  194. if (err)
  195. break;
  196. spin_lock_irq(&conf->device_lock);
  197. mddev->degraded--;
  198. rdev->raid_disk = path;
  199. set_bit(In_sync, &rdev->flags);
  200. spin_unlock_irq(&conf->device_lock);
  201. rcu_assign_pointer(p->rdev, rdev);
  202. err = 0;
  203. break;
  204. }
  205. print_multipath_conf(conf);
  206. return err;
  207. }
  208. static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  209. {
  210. struct mpconf *conf = mddev->private;
  211. int err = 0;
  212. int number = rdev->raid_disk;
  213. struct multipath_info *p = conf->multipaths + number;
  214. print_multipath_conf(conf);
  215. if (rdev == p->rdev) {
  216. if (test_bit(In_sync, &rdev->flags) ||
  217. atomic_read(&rdev->nr_pending)) {
  218. pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number);
  219. err = -EBUSY;
  220. goto abort;
  221. }
  222. p->rdev = NULL;
  223. if (!test_bit(RemoveSynchronized, &rdev->flags)) {
  224. synchronize_rcu();
  225. if (atomic_read(&rdev->nr_pending)) {
  226. /* lost the race, try later */
  227. err = -EBUSY;
  228. p->rdev = rdev;
  229. goto abort;
  230. }
  231. }
  232. err = md_integrity_register(mddev);
  233. }
  234. abort:
  235. print_multipath_conf(conf);
  236. return err;
  237. }
  238. /*
  239. * This is a kernel thread which:
  240. *
  241. * 1. Retries failed read operations on working multipaths.
  242. * 2. Updates the raid superblock when problems encounter.
  243. * 3. Performs writes following reads for array syncronising.
  244. */
  245. static void multipathd(struct md_thread *thread)
  246. {
  247. struct mddev *mddev = thread->mddev;
  248. struct multipath_bh *mp_bh;
  249. struct bio *bio;
  250. unsigned long flags;
  251. struct mpconf *conf = mddev->private;
  252. struct list_head *head = &conf->retry_list;
  253. md_check_recovery(mddev);
  254. for (;;) {
  255. spin_lock_irqsave(&conf->device_lock, flags);
  256. if (list_empty(head))
  257. break;
  258. mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
  259. list_del(head->prev);
  260. spin_unlock_irqrestore(&conf->device_lock, flags);
  261. bio = &mp_bh->bio;
  262. bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
  263. if ((mp_bh->path = multipath_map (conf))<0) {
  264. pr_err("multipath: %pg: unrecoverable IO read error for block %llu\n",
  265. bio->bi_bdev,
  266. (unsigned long long)bio->bi_iter.bi_sector);
  267. multipath_end_bh_io(mp_bh, BLK_STS_IOERR);
  268. } else {
  269. pr_err("multipath: %pg: redirecting sector %llu to another IO path\n",
  270. bio->bi_bdev,
  271. (unsigned long long)bio->bi_iter.bi_sector);
  272. *bio = *(mp_bh->master_bio);
  273. bio->bi_iter.bi_sector +=
  274. conf->multipaths[mp_bh->path].rdev->data_offset;
  275. bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev);
  276. bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
  277. bio->bi_end_io = multipath_end_request;
  278. bio->bi_private = mp_bh;
  279. submit_bio_noacct(bio);
  280. }
  281. }
  282. spin_unlock_irqrestore(&conf->device_lock, flags);
  283. }
  284. static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  285. {
  286. WARN_ONCE(sectors || raid_disks,
  287. "%s does not support generic reshape\n", __func__);
  288. return mddev->dev_sectors;
  289. }
  290. static int multipath_run (struct mddev *mddev)
  291. {
  292. struct mpconf *conf;
  293. int disk_idx;
  294. struct multipath_info *disk;
  295. struct md_rdev *rdev;
  296. int working_disks;
  297. int ret;
  298. if (md_check_no_bitmap(mddev))
  299. return -EINVAL;
  300. if (mddev->level != LEVEL_MULTIPATH) {
  301. pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
  302. mdname(mddev), mddev->level);
  303. goto out;
  304. }
  305. /*
  306. * copy the already verified devices into our private MULTIPATH
  307. * bookkeeping area. [whatever we allocate in multipath_run(),
  308. * should be freed in multipath_free()]
  309. */
  310. conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
  311. mddev->private = conf;
  312. if (!conf)
  313. goto out;
  314. conf->multipaths = kcalloc(mddev->raid_disks,
  315. sizeof(struct multipath_info),
  316. GFP_KERNEL);
  317. if (!conf->multipaths)
  318. goto out_free_conf;
  319. working_disks = 0;
  320. rdev_for_each(rdev, mddev) {
  321. disk_idx = rdev->raid_disk;
  322. if (disk_idx < 0 ||
  323. disk_idx >= mddev->raid_disks)
  324. continue;
  325. disk = conf->multipaths + disk_idx;
  326. disk->rdev = rdev;
  327. disk_stack_limits(mddev->gendisk, rdev->bdev,
  328. rdev->data_offset << 9);
  329. if (!test_bit(Faulty, &rdev->flags))
  330. working_disks++;
  331. }
  332. conf->raid_disks = mddev->raid_disks;
  333. conf->mddev = mddev;
  334. spin_lock_init(&conf->device_lock);
  335. INIT_LIST_HEAD(&conf->retry_list);
  336. if (!working_disks) {
  337. pr_warn("multipath: no operational IO paths for %s\n",
  338. mdname(mddev));
  339. goto out_free_conf;
  340. }
  341. mddev->degraded = conf->raid_disks - working_disks;
  342. ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS,
  343. sizeof(struct multipath_bh));
  344. if (ret)
  345. goto out_free_conf;
  346. mddev->thread = md_register_thread(multipathd, mddev,
  347. "multipath");
  348. if (!mddev->thread)
  349. goto out_free_conf;
  350. pr_info("multipath: array %s active with %d out of %d IO paths\n",
  351. mdname(mddev), conf->raid_disks - mddev->degraded,
  352. mddev->raid_disks);
  353. /*
  354. * Ok, everything is just fine now
  355. */
  356. md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
  357. if (md_integrity_register(mddev))
  358. goto out_free_conf;
  359. return 0;
  360. out_free_conf:
  361. mempool_exit(&conf->pool);
  362. kfree(conf->multipaths);
  363. kfree(conf);
  364. mddev->private = NULL;
  365. out:
  366. return -EIO;
  367. }
  368. static void multipath_free(struct mddev *mddev, void *priv)
  369. {
  370. struct mpconf *conf = priv;
  371. mempool_exit(&conf->pool);
  372. kfree(conf->multipaths);
  373. kfree(conf);
  374. }
  375. static struct md_personality multipath_personality =
  376. {
  377. .name = "multipath",
  378. .level = LEVEL_MULTIPATH,
  379. .owner = THIS_MODULE,
  380. .make_request = multipath_make_request,
  381. .run = multipath_run,
  382. .free = multipath_free,
  383. .status = multipath_status,
  384. .error_handler = multipath_error,
  385. .hot_add_disk = multipath_add_disk,
  386. .hot_remove_disk= multipath_remove_disk,
  387. .size = multipath_size,
  388. };
  389. static int __init multipath_init (void)
  390. {
  391. return register_md_personality (&multipath_personality);
  392. }
  393. static void __exit multipath_exit (void)
  394. {
  395. unregister_md_personality (&multipath_personality);
  396. }
  397. module_init(multipath_init);
  398. module_exit(multipath_exit);
  399. MODULE_LICENSE("GPL");
  400. MODULE_DESCRIPTION("simple multi-path personality for MD (deprecated)");
  401. MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
  402. MODULE_ALIAS("md-multipath");
  403. MODULE_ALIAS("md-level--4");