Thomas Gleixner | af1a889 | 2019-05-20 19:08:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 14 | #include <linux/blkdev.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 16 | #include <linux/raid/md_u.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 19 | #include "md.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 20 | #include "md-multipath.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define MAX_WORK_PER_DISK 128 |
| 23 | |
| 24 | #define NR_RESERVED_BUFS 32 |
| 25 | |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 26 | static int multipath_map (struct mpconf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | int i, disks = conf->raid_disks; |
| 29 | |
| 30 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 31 | * Later we do read balancing on the read side |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | * now we use the first available disk. |
| 33 | */ |
| 34 | |
| 35 | rcu_read_lock(); |
| 36 | for (i = 0; i < disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 37 | struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev); |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 38 | if (rdev && test_bit(In_sync, &rdev->flags) && |
| 39 | !test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | atomic_inc(&rdev->nr_pending); |
| 41 | rcu_read_unlock(); |
| 42 | return i; |
| 43 | } |
| 44 | } |
| 45 | rcu_read_unlock(); |
| 46 | |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 47 | pr_crit_ratelimited("multipath_map(): no more operational IO paths?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return (-1); |
| 49 | } |
| 50 | |
| 51 | static void multipath_reschedule_retry (struct multipath_bh *mp_bh) |
| 52 | { |
| 53 | unsigned long flags; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 54 | struct mddev *mddev = mp_bh->mddev; |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 55 | struct mpconf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* |
| 64 | * multipath_end_bh_io() is called when we have finished servicing a multipathed |
| 65 | * operation and are ready to return a success/failure code to the buffer |
| 66 | * cache layer. |
| 67 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 68 | static void multipath_end_bh_io(struct multipath_bh *mp_bh, blk_status_t status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | struct bio *bio = mp_bh->master_bio; |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 71 | struct mpconf *conf = mp_bh->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 73 | bio->bi_status = status; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 74 | bio_endio(bio); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 75 | mempool_free(mp_bh, &conf->pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 78 | static void multipath_end_request(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 80 | struct multipath_bh *mp_bh = bio->bi_private; |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 81 | struct mpconf *conf = mp_bh->mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 82 | struct md_rdev *rdev = conf->multipaths[mp_bh->path].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 84 | if (!bio->bi_status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | multipath_end_bh_io(mp_bh, 0); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 86 | else if (!(bio->bi_opf & REQ_RAHEAD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
| 88 | * oops, IO error: |
| 89 | */ |
| 90 | char b[BDEVNAME_SIZE]; |
| 91 | md_error (mp_bh->mddev, rdev); |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 92 | pr_info("multipath: %s: rescheduling sector %llu\n", |
| 93 | bdevname(rdev->bdev,b), |
| 94 | (unsigned long long)bio->bi_iter.bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | multipath_reschedule_retry(mp_bh); |
| 96 | } else |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 97 | multipath_end_bh_io(mp_bh, bio->bi_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 101 | static bool multipath_make_request(struct mddev *mddev, struct bio * bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 103 | struct mpconf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct multipath_bh * mp_bh; |
| 105 | struct multipath_info *multipath; |
| 106 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 107 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 108 | md_flush_request(mddev, bio); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 109 | return true; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 112 | mp_bh = mempool_alloc(&conf->pool, GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | mp_bh->master_bio = bio; |
| 115 | mp_bh->mddev = mddev; |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | mp_bh->path = multipath_map(conf); |
| 118 | if (mp_bh->path < 0) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 119 | bio_io_error(bio); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 120 | mempool_free(mp_bh, &conf->pool); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 121 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | multipath = conf->multipaths + mp_bh->path; |
| 124 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 125 | bio_init(&mp_bh->bio, NULL, 0); |
Ming Lei | fafcde3 | 2016-03-12 09:29:40 +0800 | [diff] [blame] | 126 | __bio_clone_fast(&mp_bh->bio, bio); |
| 127 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 128 | mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 129 | bio_set_dev(&mp_bh->bio, multipath->rdev->bdev); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 130 | mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | mp_bh->bio.bi_end_io = multipath_end_request; |
| 132 | mp_bh->bio.bi_private = mp_bh; |
Shaohua Li | 2648381 | 2017-02-13 16:21:49 -0800 | [diff] [blame] | 133 | mddev_check_writesame(mddev, &mp_bh->bio); |
Christoph Hellwig | 3deff1a | 2017-04-05 19:21:03 +0200 | [diff] [blame] | 134 | mddev_check_write_zeroes(mddev, &mp_bh->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | generic_make_request(&mp_bh->bio); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 136 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
NeilBrown | 40cf212 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 139 | static void multipath_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 141 | struct mpconf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | int i; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 145 | conf->raid_disks - mddev->degraded); |
NeilBrown | 40cf212 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 146 | rcu_read_lock(); |
| 147 | for (i = 0; i < conf->raid_disks; i++) { |
| 148 | struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev); |
| 149 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 150 | } |
| 151 | rcu_read_unlock(); |
Markus Elfring | 3acdb7b | 2018-01-13 09:49:03 +0100 | [diff] [blame] | 152 | seq_putc(seq, ']'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 155 | static int multipath_congested(struct mddev *mddev, int bits) |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 156 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 157 | struct mpconf *conf = mddev->private; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 158 | int i, ret = 0; |
| 159 | |
| 160 | rcu_read_lock(); |
| 161 | for (i = 0; i < mddev->raid_disks ; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 162 | struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 163 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 164 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 165 | |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 166 | ret |= bdi_congested(q->backing_dev_info, bits); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 167 | /* Just like multipath_map, we just check the |
| 168 | * first available device |
| 169 | */ |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | rcu_read_unlock(); |
| 174 | return ret; |
| 175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * Careful, this can execute in IRQ contexts as well! |
| 179 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 180 | static void multipath_error (struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 182 | struct mpconf *conf = mddev->private; |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 183 | char b[BDEVNAME_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 185 | if (conf->raid_disks - mddev->degraded <= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Uh oh, we can do nothing if this is our last path, but |
| 188 | * first check if this is a queued request for a device |
| 189 | * which has just failed. |
| 190 | */ |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 191 | pr_warn("multipath: only one IO path left and IO error.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* leave it active... it's all we have */ |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 193 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 195 | /* |
| 196 | * Mark disk as unusable |
| 197 | */ |
| 198 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 199 | unsigned long flags; |
| 200 | spin_lock_irqsave(&conf->device_lock, flags); |
| 201 | mddev->degraded++; |
| 202 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 203 | } |
| 204 | set_bit(Faulty, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 205 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 206 | pr_err("multipath: IO failure on %s, disabling IO path.\n" |
| 207 | "multipath: Operation continuing on %d IO paths.\n", |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 208 | bdevname(rdev->bdev, b), |
| 209 | conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 212 | static void print_multipath_conf (struct mpconf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | int i; |
| 215 | struct multipath_info *tmp; |
| 216 | |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 217 | pr_debug("MULTIPATH conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (!conf) { |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 219 | pr_debug("(conf==NULL)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return; |
| 221 | } |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 222 | pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
| 223 | conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | for (i = 0; i < conf->raid_disks; i++) { |
| 226 | char b[BDEVNAME_SIZE]; |
| 227 | tmp = conf->multipaths + i; |
| 228 | if (tmp->rdev) |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 229 | pr_debug(" disk%d, o:%d, dev:%s\n", |
| 230 | i,!test_bit(Faulty, &tmp->rdev->flags), |
| 231 | bdevname(tmp->rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 235 | static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 237 | struct mpconf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 238 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | int path; |
| 240 | struct multipath_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 241 | int first = 0; |
| 242 | int last = mddev->raid_disks - 1; |
| 243 | |
| 244 | if (rdev->raid_disk >= 0) |
| 245 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | print_multipath_conf(conf); |
| 248 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 249 | for (path = first; path <= last; path++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if ((p=conf->multipaths+path)->rdev == NULL) { |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 251 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 252 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 254 | err = md_integrity_add_rdev(rdev, mddev); |
| 255 | if (err) |
| 256 | break; |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 257 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 750a8f3 | 2006-10-28 10:38:31 -0700 | [diff] [blame] | 258 | mddev->degraded--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | rdev->raid_disk = path; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 260 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 261 | spin_unlock_irq(&conf->device_lock); |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 262 | rcu_assign_pointer(p->rdev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 263 | err = 0; |
| 264 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | print_multipath_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 268 | |
| 269 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 272 | static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 274 | struct mpconf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 276 | int number = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | struct multipath_info *p = conf->multipaths + number; |
| 278 | |
| 279 | print_multipath_conf(conf); |
| 280 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 281 | if (rdev == p->rdev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 282 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | atomic_read(&rdev->nr_pending)) { |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 284 | pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | err = -EBUSY; |
| 286 | goto abort; |
| 287 | } |
| 288 | p->rdev = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 289 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 290 | synchronize_rcu(); |
| 291 | if (atomic_read(&rdev->nr_pending)) { |
| 292 | /* lost the race, try later */ |
| 293 | err = -EBUSY; |
| 294 | p->rdev = rdev; |
| 295 | goto abort; |
| 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 298 | err = md_integrity_register(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | abort: |
| 301 | |
| 302 | print_multipath_conf(conf); |
| 303 | return err; |
| 304 | } |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | /* |
| 307 | * This is a kernel thread which: |
| 308 | * |
| 309 | * 1. Retries failed read operations on working multipaths. |
| 310 | * 2. Updates the raid superblock when problems encounter. |
| 311 | * 3. Performs writes following reads for array syncronising. |
| 312 | */ |
| 313 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 314 | static void multipathd(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 316 | struct mddev *mddev = thread->mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | struct multipath_bh *mp_bh; |
| 318 | struct bio *bio; |
| 319 | unsigned long flags; |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 320 | struct mpconf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | struct list_head *head = &conf->retry_list; |
| 322 | |
| 323 | md_check_recovery(mddev); |
| 324 | for (;;) { |
| 325 | char b[BDEVNAME_SIZE]; |
| 326 | spin_lock_irqsave(&conf->device_lock, flags); |
| 327 | if (list_empty(head)) |
| 328 | break; |
| 329 | mp_bh = list_entry(head->prev, struct multipath_bh, retry_list); |
| 330 | list_del(head->prev); |
| 331 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 332 | |
| 333 | bio = &mp_bh->bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 334 | bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if ((mp_bh->path = multipath_map (conf))<0) { |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 337 | pr_err("multipath: %s: unrecoverable IO read error for block %llu\n", |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 338 | bio_devname(bio, b), |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 339 | (unsigned long long)bio->bi_iter.bi_sector); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 340 | multipath_end_bh_io(mp_bh, BLK_STS_IOERR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } else { |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 342 | pr_err("multipath: %s: redirecting sector %llu to another IO path\n", |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 343 | bio_devname(bio, b), |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 344 | (unsigned long long)bio->bi_iter.bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | *bio = *(mp_bh->master_bio); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 346 | bio->bi_iter.bi_sector += |
| 347 | conf->multipaths[mp_bh->path].rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 348 | bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 349 | bio->bi_opf |= REQ_FAILFAST_TRANSPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | bio->bi_end_io = multipath_end_request; |
| 351 | bio->bi_private = mp_bh; |
| 352 | generic_make_request(bio); |
| 353 | } |
| 354 | } |
| 355 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 356 | } |
| 357 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 358 | static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 359 | { |
| 360 | WARN_ONCE(sectors || raid_disks, |
| 361 | "%s does not support generic reshape\n", __func__); |
| 362 | |
| 363 | return mddev->dev_sectors; |
| 364 | } |
| 365 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 366 | static int multipath_run (struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 368 | struct mpconf *conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | int disk_idx; |
| 370 | struct multipath_info *disk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 371 | struct md_rdev *rdev; |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 372 | int working_disks; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 373 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 375 | if (md_check_no_bitmap(mddev)) |
| 376 | return -EINVAL; |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | if (mddev->level != LEVEL_MULTIPATH) { |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 379 | pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n", |
| 380 | mdname(mddev), mddev->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | goto out; |
| 382 | } |
| 383 | /* |
| 384 | * copy the already verified devices into our private MULTIPATH |
| 385 | * bookkeeping area. [whatever we allocate in multipath_run(), |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 386 | * should be freed in multipath_free()] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | */ |
| 388 | |
NeilBrown | 69724e2 | 2011-10-11 16:48:57 +1100 | [diff] [blame] | 389 | conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | mddev->private = conf; |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 391 | if (!conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 394 | conf->multipaths = kcalloc(mddev->raid_disks, |
| 395 | sizeof(struct multipath_info), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | GFP_KERNEL); |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 397 | if (!conf->multipaths) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | goto out_free_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 400 | working_disks = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 401 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | disk_idx = rdev->raid_disk; |
| 403 | if (disk_idx < 0 || |
| 404 | disk_idx >= mddev->raid_disks) |
| 405 | continue; |
| 406 | |
| 407 | disk = conf->multipaths + disk_idx; |
| 408 | disk->rdev = rdev; |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 409 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 410 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 412 | if (!test_bit(Faulty, &rdev->flags)) |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 413 | working_disks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | conf->raid_disks = mddev->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | conf->mddev = mddev; |
| 418 | spin_lock_init(&conf->device_lock); |
| 419 | INIT_LIST_HEAD(&conf->retry_list); |
| 420 | |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 421 | if (!working_disks) { |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 422 | pr_warn("multipath: no operational IO paths for %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | mdname(mddev)); |
| 424 | goto out_free_conf; |
| 425 | } |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 426 | mddev->degraded = conf->raid_disks - working_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 428 | ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS, |
| 429 | sizeof(struct multipath_bh)); |
| 430 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | goto out_free_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 433 | mddev->thread = md_register_thread(multipathd, mddev, |
| 434 | "multipath"); |
| 435 | if (!mddev->thread) |
| 436 | goto out_free_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 438 | pr_info("multipath: array %s active with %d out of %d IO paths\n", |
NeilBrown | 92f861a | 2011-05-11 14:38:02 +1000 | [diff] [blame] | 439 | mdname(mddev), conf->raid_disks - mddev->degraded, |
NeilBrown | 7279694 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 440 | mddev->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | /* |
| 442 | * Ok, everything is just fine now |
| 443 | */ |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 444 | md_set_array_sectors(mddev, multipath_size(mddev, 0, 0)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 445 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 446 | if (md_integrity_register(mddev)) |
| 447 | goto out_free_conf; |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | |
| 451 | out_free_conf: |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 452 | mempool_exit(&conf->pool); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 453 | kfree(conf->multipaths); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | kfree(conf); |
| 455 | mddev->private = NULL; |
| 456 | out: |
| 457 | return -EIO; |
| 458 | } |
| 459 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 460 | static void multipath_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 462 | struct mpconf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 464 | mempool_exit(&conf->pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | kfree(conf->multipaths); |
| 466 | kfree(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 469 | static struct md_personality multipath_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | .name = "multipath", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 472 | .level = LEVEL_MULTIPATH, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | .owner = THIS_MODULE, |
| 474 | .make_request = multipath_make_request, |
| 475 | .run = multipath_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 476 | .free = multipath_free, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | .status = multipath_status, |
| 478 | .error_handler = multipath_error, |
| 479 | .hot_add_disk = multipath_add_disk, |
| 480 | .hot_remove_disk= multipath_remove_disk, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 481 | .size = multipath_size, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 482 | .congested = multipath_congested, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | static int __init multipath_init (void) |
| 486 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 487 | return register_md_personality (&multipath_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void __exit multipath_exit (void) |
| 491 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 492 | unregister_md_personality (&multipath_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | module_init(multipath_init); |
| 496 | module_exit(multipath_exit); |
| 497 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 498 | MODULE_DESCRIPTION("simple multi-path personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | MODULE_ALIAS("md-personality-7"); /* MULTIPATH */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 500 | MODULE_ALIAS("md-multipath"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 501 | MODULE_ALIAS("md-level--4"); |