blob: 776bbe542db55eeed565f5aa208713ed5215904a [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
NeilBrownbff61972009-03-31 14:33:13 +110014#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040015#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110016#include <linux/raid/md_u.h>
NeilBrownbff61972009-03-31 14:33:13 +110017#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110019#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040020#include "md-multipath.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define MAX_WORK_PER_DISK 128
23
24#define NR_RESERVED_BUFS 32
25
NeilBrown69724e22011-10-11 16:48:57 +110026static int multipath_map (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 int i, disks = conf->raid_disks;
29
30 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +100031 * Later we do read balancing on the read side
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * now we use the first available disk.
33 */
34
35 rcu_read_lock();
36 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +110037 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +100038 if (rdev && test_bit(In_sync, &rdev->flags) &&
39 !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 atomic_inc(&rdev->nr_pending);
41 rcu_read_unlock();
42 return i;
43 }
44 }
45 rcu_read_unlock();
46
NeilBrown72796942016-11-02 14:16:49 +110047 pr_crit_ratelimited("multipath_map(): no more operational IO paths?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return (-1);
49}
50
51static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
52{
53 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +110054 struct mddev *mddev = mp_bh->mddev;
NeilBrown69724e22011-10-11 16:48:57 +110055 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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 Torvalds1da177e2005-04-16 15:20:36 -070063/*
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 Hellwig4e4cbee2017-06-03 09:38:06 +020068static void multipath_end_bh_io(struct multipath_bh *mp_bh, blk_status_t status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct bio *bio = mp_bh->master_bio;
NeilBrown69724e22011-10-11 16:48:57 +110071 struct mpconf *conf = mp_bh->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020073 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020074 bio_endio(bio);
Kent Overstreetafeee512018-05-20 18:25:52 -040075 mempool_free(mp_bh, &conf->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020078static void multipath_end_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
H Hartley Sweeten7b928132010-03-08 16:02:40 +110080 struct multipath_bh *mp_bh = bio->bi_private;
NeilBrown69724e22011-10-11 16:48:57 +110081 struct mpconf *conf = mp_bh->mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +110082 struct md_rdev *rdev = conf->multipaths[mp_bh->path].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020084 if (!bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 multipath_end_bh_io(mp_bh, 0);
Jens Axboe1eff9d32016-08-05 15:35:16 -060086 else if (!(bio->bi_opf & REQ_RAHEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /*
88 * oops, IO error:
89 */
90 char b[BDEVNAME_SIZE];
91 md_error (mp_bh->mddev, rdev);
NeilBrown72796942016-11-02 14:16:49 +110092 pr_info("multipath: %s: rescheduling sector %llu\n",
93 bdevname(rdev->bdev,b),
94 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 multipath_reschedule_retry(mp_bh);
96 } else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020097 multipath_end_bh_io(mp_bh, bio->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
NeilBrowncc27b0c2017-06-05 16:49:39 +1000101static bool multipath_make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
NeilBrown69724e22011-10-11 16:48:57 +1100103 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct multipath_bh * mp_bh;
105 struct multipath_info *multipath;
106
David Jeffery775d7832019-09-16 13:15:14 -0400107 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
108 && md_flush_request(mddev, bio))
NeilBrowncc27b0c2017-06-05 16:49:39 +1000109 return true;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700110
Kent Overstreetafeee512018-05-20 18:25:52 -0400111 mp_bh = mempool_alloc(&conf->pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 mp_bh->master_bio = bio;
114 mp_bh->mddev = mddev;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 mp_bh->path = multipath_map(conf);
117 if (mp_bh->path < 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200118 bio_io_error(bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400119 mempool_free(mp_bh, &conf->pool);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000120 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 multipath = conf->multipaths + mp_bh->path;
123
Ming Lei3a83f462016-11-22 08:57:21 -0700124 bio_init(&mp_bh->bio, NULL, 0);
Ming Leifafcde32016-03-12 09:29:40 +0800125 __bio_clone_fast(&mp_bh->bio, bio);
126
Kent Overstreet4f024f32013-10-11 15:44:27 -0700127 mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200128 bio_set_dev(&mp_bh->bio, multipath->rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600129 mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 mp_bh->bio.bi_end_io = multipath_end_request;
131 mp_bh->bio.bi_private = mp_bh;
Shaohua Li26483812017-02-13 16:21:49 -0800132 mddev_check_writesame(mddev, &mp_bh->bio);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +0200133 mddev_check_write_zeroes(mddev, &mp_bh->bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200134 submit_bio_noacct(&mp_bh->bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000135 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
NeilBrown40cf2122016-06-02 16:19:52 +1000138static void multipath_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
NeilBrown69724e22011-10-11 16:48:57 +1100140 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int i;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
NeilBrown92f861a2011-05-11 14:38:02 +1000144 conf->raid_disks - mddev->degraded);
NeilBrown40cf2122016-06-02 16:19:52 +1000145 rcu_read_lock();
146 for (i = 0; i < conf->raid_disks; i++) {
147 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
148 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
149 }
150 rcu_read_unlock();
Markus Elfring3acdb7b2018-01-13 09:49:03 +0100151 seq_putc(seq, ']');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * Careful, this can execute in IRQ contexts as well!
156 */
NeilBrownfd01b882011-10-11 16:47:53 +1100157static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
NeilBrown69724e22011-10-11 16:48:57 +1100159 struct mpconf *conf = mddev->private;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000160 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
NeilBrown92f861a2011-05-11 14:38:02 +1000162 if (conf->raid_disks - mddev->degraded <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /*
164 * Uh oh, we can do nothing if this is our last path, but
165 * first check if this is a queued request for a device
166 * which has just failed.
167 */
NeilBrown72796942016-11-02 14:16:49 +1100168 pr_warn("multipath: only one IO path left and IO error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* leave it active... it's all we have */
NeilBrown6f8d0c72011-05-11 14:38:44 +1000170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
NeilBrown6f8d0c72011-05-11 14:38:44 +1000172 /*
173 * Mark disk as unusable
174 */
175 if (test_and_clear_bit(In_sync, &rdev->flags)) {
176 unsigned long flags;
177 spin_lock_irqsave(&conf->device_lock, flags);
178 mddev->degraded++;
179 spin_unlock_irqrestore(&conf->device_lock, flags);
180 }
181 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -0800182 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown72796942016-11-02 14:16:49 +1100183 pr_err("multipath: IO failure on %s, disabling IO path.\n"
184 "multipath: Operation continuing on %d IO paths.\n",
NeilBrown6f8d0c72011-05-11 14:38:44 +1000185 bdevname(rdev->bdev, b),
186 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
NeilBrown69724e22011-10-11 16:48:57 +1100189static void print_multipath_conf (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int i;
192 struct multipath_info *tmp;
193
NeilBrown72796942016-11-02 14:16:49 +1100194 pr_debug("MULTIPATH conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!conf) {
NeilBrown72796942016-11-02 14:16:49 +1100196 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return;
198 }
NeilBrown72796942016-11-02 14:16:49 +1100199 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
200 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 for (i = 0; i < conf->raid_disks; i++) {
203 char b[BDEVNAME_SIZE];
204 tmp = conf->multipaths + i;
205 if (tmp->rdev)
NeilBrown72796942016-11-02 14:16:49 +1100206 pr_debug(" disk%d, o:%d, dev:%s\n",
207 i,!test_bit(Faulty, &tmp->rdev->flags),
208 bdevname(tmp->rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210}
211
NeilBrownfd01b882011-10-11 16:47:53 +1100212static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
NeilBrown69724e22011-10-11 16:48:57 +1100214 struct mpconf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +1000215 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int path;
217 struct multipath_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +1000218 int first = 0;
219 int last = mddev->raid_disks - 1;
220
221 if (rdev->raid_disk >= 0)
222 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 print_multipath_conf(conf);
225
Neil Brown6c2fce22008-06-28 08:31:31 +1000226 for (path = first; path <= last; path++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if ((p=conf->multipaths+path)->rdev == NULL) {
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000228 disk_stack_limits(mddev->gendisk, rdev->bdev,
229 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dan Williams1501efa2016-01-13 16:00:07 -0800231 err = md_integrity_add_rdev(rdev, mddev);
232 if (err)
233 break;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000234 spin_lock_irq(&conf->device_lock);
NeilBrown750a8f32006-10-28 10:38:31 -0700235 mddev->degraded--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 rdev->raid_disk = path;
NeilBrownb2d444d2005-11-08 21:39:31 -0800237 set_bit(In_sync, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +1000238 spin_unlock_irq(&conf->device_lock);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800239 rcu_assign_pointer(p->rdev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +1000240 err = 0;
241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
244 print_multipath_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +1000245
246 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
NeilBrownb8321b62011-12-23 10:17:51 +1100249static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
NeilBrown69724e22011-10-11 16:48:57 +1100251 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +1100253 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct multipath_info *p = conf->multipaths + number;
255
256 print_multipath_conf(conf);
257
NeilBrownb8321b62011-12-23 10:17:51 +1100258 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800259 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 atomic_read(&rdev->nr_pending)) {
NeilBrown72796942016-11-02 14:16:49 +1100261 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 err = -EBUSY;
263 goto abort;
264 }
265 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +1000266 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
267 synchronize_rcu();
268 if (atomic_read(&rdev->nr_pending)) {
269 /* lost the race, try later */
270 err = -EBUSY;
271 p->rdev = rdev;
272 goto abort;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Martin K. Petersena91a2782011-03-17 11:11:05 +0100275 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277abort:
278
279 print_multipath_conf(conf);
280 return err;
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
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
Shaohua Li4ed87312012-10-11 13:34:00 +1100291static void multipathd(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Shaohua Li4ed87312012-10-11 13:34:00 +1100293 struct mddev *mddev = thread->mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct multipath_bh *mp_bh;
295 struct bio *bio;
296 unsigned long flags;
NeilBrown69724e22011-10-11 16:48:57 +1100297 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct list_head *head = &conf->retry_list;
299
300 md_check_recovery(mddev);
301 for (;;) {
302 char b[BDEVNAME_SIZE];
303 spin_lock_irqsave(&conf->device_lock, flags);
304 if (list_empty(head))
305 break;
306 mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
307 list_del(head->prev);
308 spin_unlock_irqrestore(&conf->device_lock, flags);
309
310 bio = &mp_bh->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700311 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if ((mp_bh->path = multipath_map (conf))<0) {
NeilBrown72796942016-11-02 14:16:49 +1100314 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200315 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100316 (unsigned long long)bio->bi_iter.bi_sector);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200317 multipath_end_bh_io(mp_bh, BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else {
NeilBrown72796942016-11-02 14:16:49 +1100319 pr_err("multipath: %s: redirecting sector %llu to another IO path\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200320 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100321 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *bio = *(mp_bh->master_bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700323 bio->bi_iter.bi_sector +=
324 conf->multipaths[mp_bh->path].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200325 bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600326 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 bio->bi_end_io = multipath_end_request;
328 bio->bi_private = mp_bh;
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200329 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
332 spin_unlock_irqrestore(&conf->device_lock, flags);
333}
334
NeilBrownfd01b882011-10-11 16:47:53 +1100335static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700336{
337 WARN_ONCE(sectors || raid_disks,
338 "%s does not support generic reshape\n", __func__);
339
340 return mddev->dev_sectors;
341}
342
NeilBrownfd01b882011-10-11 16:47:53 +1100343static int multipath_run (struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
NeilBrown69724e22011-10-11 16:48:57 +1100345 struct mpconf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int disk_idx;
347 struct multipath_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +1100348 struct md_rdev *rdev;
NeilBrown92f861a2011-05-11 14:38:02 +1000349 int working_disks;
Kent Overstreetafeee512018-05-20 18:25:52 -0400350 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Andre Noll0894cc32009-06-18 08:49:23 +1000352 if (md_check_no_bitmap(mddev))
353 return -EINVAL;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (mddev->level != LEVEL_MULTIPATH) {
NeilBrown72796942016-11-02 14:16:49 +1100356 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
357 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto out;
359 }
360 /*
361 * copy the already verified devices into our private MULTIPATH
362 * bookkeeping area. [whatever we allocate in multipath_run(),
NeilBrownafa0f552014-12-15 12:56:58 +1100363 * should be freed in multipath_free()]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365
NeilBrown69724e22011-10-11 16:48:57 +1100366 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 mddev->private = conf;
NeilBrown72796942016-11-02 14:16:49 +1100368 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Kees Cook6396bb22018-06-12 14:03:40 -0700371 conf->multipaths = kcalloc(mddev->raid_disks,
372 sizeof(struct multipath_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 GFP_KERNEL);
NeilBrown72796942016-11-02 14:16:49 +1100374 if (!conf->multipaths)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
NeilBrown92f861a2011-05-11 14:38:02 +1000377 working_disks = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100378 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 disk_idx = rdev->raid_disk;
380 if (disk_idx < 0 ||
381 disk_idx >= mddev->raid_disks)
382 continue;
383
384 disk = conf->multipaths + disk_idx;
385 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000386 disk_stack_limits(mddev->gendisk, rdev->bdev,
387 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
NeilBrownb2d444d2005-11-08 21:39:31 -0800389 if (!test_bit(Faulty, &rdev->flags))
NeilBrown92f861a2011-05-11 14:38:02 +1000390 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 conf->mddev = mddev;
395 spin_lock_init(&conf->device_lock);
396 INIT_LIST_HEAD(&conf->retry_list);
397
NeilBrown92f861a2011-05-11 14:38:02 +1000398 if (!working_disks) {
NeilBrown72796942016-11-02 14:16:49 +1100399 pr_warn("multipath: no operational IO paths for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 mdname(mddev));
401 goto out_free_conf;
402 }
NeilBrown92f861a2011-05-11 14:38:02 +1000403 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Kent Overstreetafeee512018-05-20 18:25:52 -0400405 ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS,
406 sizeof(struct multipath_bh));
407 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
NeilBrown72796942016-11-02 14:16:49 +1100410 mddev->thread = md_register_thread(multipathd, mddev,
411 "multipath");
412 if (!mddev->thread)
413 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
NeilBrown72796942016-11-02 14:16:49 +1100415 pr_info("multipath: array %s active with %d out of %d IO paths\n",
NeilBrown92f861a2011-05-11 14:38:02 +1000416 mdname(mddev), conf->raid_disks - mddev->degraded,
NeilBrown72796942016-11-02 14:16:49 +1100417 mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * Ok, everything is just fine now
420 */
Dan Williams1f403622009-03-31 14:59:03 +1100421 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -0700422
Martin K. Petersena91a2782011-03-17 11:11:05 +0100423 if (md_integrity_register(mddev))
424 goto out_free_conf;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427
428out_free_conf:
Kent Overstreetafeee512018-05-20 18:25:52 -0400429 mempool_exit(&conf->pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700430 kfree(conf->multipaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 kfree(conf);
432 mddev->private = NULL;
433out:
434 return -EIO;
435}
436
NeilBrownafa0f552014-12-15 12:56:58 +1100437static void multipath_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
NeilBrownafa0f552014-12-15 12:56:58 +1100439 struct mpconf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Kent Overstreetafeee512018-05-20 18:25:52 -0400441 mempool_exit(&conf->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kfree(conf->multipaths);
443 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
NeilBrown84fc4b52011-10-11 16:49:58 +1100446static struct md_personality multipath_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 .name = "multipath",
NeilBrown2604b702006-01-06 00:20:36 -0800449 .level = LEVEL_MULTIPATH,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .owner = THIS_MODULE,
451 .make_request = multipath_make_request,
452 .run = multipath_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100453 .free = multipath_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 .status = multipath_status,
455 .error_handler = multipath_error,
456 .hot_add_disk = multipath_add_disk,
457 .hot_remove_disk= multipath_remove_disk,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700458 .size = multipath_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459};
460
461static int __init multipath_init (void)
462{
NeilBrown2604b702006-01-06 00:20:36 -0800463 return register_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static void __exit multipath_exit (void)
467{
NeilBrown2604b702006-01-06 00:20:36 -0800468 unregister_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471module_init(multipath_init);
472module_exit(multipath_exit);
473MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100474MODULE_DESCRIPTION("simple multi-path personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
NeilBrownd9d166c2006-01-06 00:20:51 -0800476MODULE_ALIAS("md-multipath");
NeilBrown2604b702006-01-06 00:20:36 -0800477MODULE_ALIAS("md-level--4");