blob: 152f9e65a22665f33208b535680bd68101fcda43 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 generic_make_request(&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
NeilBrown5c675f82014-12-15 12:56:56 +1100154static int multipath_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700155{
NeilBrown69724e22011-10-11 16:48:57 +1100156 struct mpconf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700157 int i, ret = 0;
158
159 rcu_read_lock();
160 for (i = 0; i < mddev->raid_disks ; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100161 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700162 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200163 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700164
Jan Karadc3b17c2017-02-02 15:56:50 +0100165 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700166 /* Just like multipath_map, we just check the
167 * first available device
168 */
169 break;
170 }
171 }
172 rcu_read_unlock();
173 return ret;
174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
177 * Careful, this can execute in IRQ contexts as well!
178 */
NeilBrownfd01b882011-10-11 16:47:53 +1100179static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
NeilBrown69724e22011-10-11 16:48:57 +1100181 struct mpconf *conf = mddev->private;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000182 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
NeilBrown92f861a2011-05-11 14:38:02 +1000184 if (conf->raid_disks - mddev->degraded <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
186 * Uh oh, we can do nothing if this is our last path, but
187 * first check if this is a queued request for a device
188 * which has just failed.
189 */
NeilBrown72796942016-11-02 14:16:49 +1100190 pr_warn("multipath: only one IO path left and IO error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* leave it active... it's all we have */
NeilBrown6f8d0c72011-05-11 14:38:44 +1000192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
NeilBrown6f8d0c72011-05-11 14:38:44 +1000194 /*
195 * Mark disk as unusable
196 */
197 if (test_and_clear_bit(In_sync, &rdev->flags)) {
198 unsigned long flags;
199 spin_lock_irqsave(&conf->device_lock, flags);
200 mddev->degraded++;
201 spin_unlock_irqrestore(&conf->device_lock, flags);
202 }
203 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -0800204 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown72796942016-11-02 14:16:49 +1100205 pr_err("multipath: IO failure on %s, disabling IO path.\n"
206 "multipath: Operation continuing on %d IO paths.\n",
NeilBrown6f8d0c72011-05-11 14:38:44 +1000207 bdevname(rdev->bdev, b),
208 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
NeilBrown69724e22011-10-11 16:48:57 +1100211static void print_multipath_conf (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int i;
214 struct multipath_info *tmp;
215
NeilBrown72796942016-11-02 14:16:49 +1100216 pr_debug("MULTIPATH conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!conf) {
NeilBrown72796942016-11-02 14:16:49 +1100218 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return;
220 }
NeilBrown72796942016-11-02 14:16:49 +1100221 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
222 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 for (i = 0; i < conf->raid_disks; i++) {
225 char b[BDEVNAME_SIZE];
226 tmp = conf->multipaths + i;
227 if (tmp->rdev)
NeilBrown72796942016-11-02 14:16:49 +1100228 pr_debug(" disk%d, o:%d, dev:%s\n",
229 i,!test_bit(Faulty, &tmp->rdev->flags),
230 bdevname(tmp->rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232}
233
NeilBrownfd01b882011-10-11 16:47:53 +1100234static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
NeilBrown69724e22011-10-11 16:48:57 +1100236 struct mpconf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +1000237 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int path;
239 struct multipath_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +1000240 int first = 0;
241 int last = mddev->raid_disks - 1;
242
243 if (rdev->raid_disk >= 0)
244 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 print_multipath_conf(conf);
247
Neil Brown6c2fce22008-06-28 08:31:31 +1000248 for (path = first; path <= last; path++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if ((p=conf->multipaths+path)->rdev == NULL) {
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000250 disk_stack_limits(mddev->gendisk, rdev->bdev,
251 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dan Williams1501efa2016-01-13 16:00:07 -0800253 err = md_integrity_add_rdev(rdev, mddev);
254 if (err)
255 break;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000256 spin_lock_irq(&conf->device_lock);
NeilBrown750a8f32006-10-28 10:38:31 -0700257 mddev->degraded--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 rdev->raid_disk = path;
NeilBrownb2d444d2005-11-08 21:39:31 -0800259 set_bit(In_sync, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +1000260 spin_unlock_irq(&conf->device_lock);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800261 rcu_assign_pointer(p->rdev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +1000262 err = 0;
263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 print_multipath_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +1000267
268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
NeilBrownb8321b62011-12-23 10:17:51 +1100271static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
NeilBrown69724e22011-10-11 16:48:57 +1100273 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +1100275 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct multipath_info *p = conf->multipaths + number;
277
278 print_multipath_conf(conf);
279
NeilBrownb8321b62011-12-23 10:17:51 +1100280 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800281 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 atomic_read(&rdev->nr_pending)) {
NeilBrown72796942016-11-02 14:16:49 +1100283 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 err = -EBUSY;
285 goto abort;
286 }
287 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +1000288 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
289 synchronize_rcu();
290 if (atomic_read(&rdev->nr_pending)) {
291 /* lost the race, try later */
292 err = -EBUSY;
293 p->rdev = rdev;
294 goto abort;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Martin K. Petersena91a2782011-03-17 11:11:05 +0100297 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299abort:
300
301 print_multipath_conf(conf);
302 return err;
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
306 * This is a kernel thread which:
307 *
308 * 1. Retries failed read operations on working multipaths.
309 * 2. Updates the raid superblock when problems encounter.
310 * 3. Performs writes following reads for array syncronising.
311 */
312
Shaohua Li4ed87312012-10-11 13:34:00 +1100313static void multipathd(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Shaohua Li4ed87312012-10-11 13:34:00 +1100315 struct mddev *mddev = thread->mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct multipath_bh *mp_bh;
317 struct bio *bio;
318 unsigned long flags;
NeilBrown69724e22011-10-11 16:48:57 +1100319 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct list_head *head = &conf->retry_list;
321
322 md_check_recovery(mddev);
323 for (;;) {
324 char b[BDEVNAME_SIZE];
325 spin_lock_irqsave(&conf->device_lock, flags);
326 if (list_empty(head))
327 break;
328 mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
329 list_del(head->prev);
330 spin_unlock_irqrestore(&conf->device_lock, flags);
331
332 bio = &mp_bh->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700333 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if ((mp_bh->path = multipath_map (conf))<0) {
NeilBrown72796942016-11-02 14:16:49 +1100336 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200337 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100338 (unsigned long long)bio->bi_iter.bi_sector);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200339 multipath_end_bh_io(mp_bh, BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 } else {
NeilBrown72796942016-11-02 14:16:49 +1100341 pr_err("multipath: %s: redirecting sector %llu to another IO path\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200342 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100343 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *bio = *(mp_bh->master_bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700345 bio->bi_iter.bi_sector +=
346 conf->multipaths[mp_bh->path].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200347 bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600348 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 bio->bi_end_io = multipath_end_request;
350 bio->bi_private = mp_bh;
351 generic_make_request(bio);
352 }
353 }
354 spin_unlock_irqrestore(&conf->device_lock, flags);
355}
356
NeilBrownfd01b882011-10-11 16:47:53 +1100357static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700358{
359 WARN_ONCE(sectors || raid_disks,
360 "%s does not support generic reshape\n", __func__);
361
362 return mddev->dev_sectors;
363}
364
NeilBrownfd01b882011-10-11 16:47:53 +1100365static int multipath_run (struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
NeilBrown69724e22011-10-11 16:48:57 +1100367 struct mpconf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int disk_idx;
369 struct multipath_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +1100370 struct md_rdev *rdev;
NeilBrown92f861a2011-05-11 14:38:02 +1000371 int working_disks;
Kent Overstreetafeee512018-05-20 18:25:52 -0400372 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Andre Noll0894cc32009-06-18 08:49:23 +1000374 if (md_check_no_bitmap(mddev))
375 return -EINVAL;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (mddev->level != LEVEL_MULTIPATH) {
NeilBrown72796942016-11-02 14:16:49 +1100378 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
379 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto out;
381 }
382 /*
383 * copy the already verified devices into our private MULTIPATH
384 * bookkeeping area. [whatever we allocate in multipath_run(),
NeilBrownafa0f552014-12-15 12:56:58 +1100385 * should be freed in multipath_free()]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387
NeilBrown69724e22011-10-11 16:48:57 +1100388 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 mddev->private = conf;
NeilBrown72796942016-11-02 14:16:49 +1100390 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Kees Cook6396bb22018-06-12 14:03:40 -0700393 conf->multipaths = kcalloc(mddev->raid_disks,
394 sizeof(struct multipath_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 GFP_KERNEL);
NeilBrown72796942016-11-02 14:16:49 +1100396 if (!conf->multipaths)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
NeilBrown92f861a2011-05-11 14:38:02 +1000399 working_disks = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100400 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 disk_idx = rdev->raid_disk;
402 if (disk_idx < 0 ||
403 disk_idx >= mddev->raid_disks)
404 continue;
405
406 disk = conf->multipaths + disk_idx;
407 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000408 disk_stack_limits(mddev->gendisk, rdev->bdev,
409 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
NeilBrownb2d444d2005-11-08 21:39:31 -0800411 if (!test_bit(Faulty, &rdev->flags))
NeilBrown92f861a2011-05-11 14:38:02 +1000412 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 conf->mddev = mddev;
417 spin_lock_init(&conf->device_lock);
418 INIT_LIST_HEAD(&conf->retry_list);
419
NeilBrown92f861a2011-05-11 14:38:02 +1000420 if (!working_disks) {
NeilBrown72796942016-11-02 14:16:49 +1100421 pr_warn("multipath: no operational IO paths for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 mdname(mddev));
423 goto out_free_conf;
424 }
NeilBrown92f861a2011-05-11 14:38:02 +1000425 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Kent Overstreetafeee512018-05-20 18:25:52 -0400427 ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS,
428 sizeof(struct multipath_bh));
429 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
NeilBrown72796942016-11-02 14:16:49 +1100432 mddev->thread = md_register_thread(multipathd, mddev,
433 "multipath");
434 if (!mddev->thread)
435 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
NeilBrown72796942016-11-02 14:16:49 +1100437 pr_info("multipath: array %s active with %d out of %d IO paths\n",
NeilBrown92f861a2011-05-11 14:38:02 +1000438 mdname(mddev), conf->raid_disks - mddev->degraded,
NeilBrown72796942016-11-02 14:16:49 +1100439 mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Ok, everything is just fine now
442 */
Dan Williams1f403622009-03-31 14:59:03 +1100443 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -0700444
Martin K. Petersena91a2782011-03-17 11:11:05 +0100445 if (md_integrity_register(mddev))
446 goto out_free_conf;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
449
450out_free_conf:
Kent Overstreetafeee512018-05-20 18:25:52 -0400451 mempool_exit(&conf->pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700452 kfree(conf->multipaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 kfree(conf);
454 mddev->private = NULL;
455out:
456 return -EIO;
457}
458
NeilBrownafa0f552014-12-15 12:56:58 +1100459static void multipath_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
NeilBrownafa0f552014-12-15 12:56:58 +1100461 struct mpconf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Kent Overstreetafeee512018-05-20 18:25:52 -0400463 mempool_exit(&conf->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 kfree(conf->multipaths);
465 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
NeilBrown84fc4b52011-10-11 16:49:58 +1100468static struct md_personality multipath_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 .name = "multipath",
NeilBrown2604b702006-01-06 00:20:36 -0800471 .level = LEVEL_MULTIPATH,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 .owner = THIS_MODULE,
473 .make_request = multipath_make_request,
474 .run = multipath_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100475 .free = multipath_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .status = multipath_status,
477 .error_handler = multipath_error,
478 .hot_add_disk = multipath_add_disk,
479 .hot_remove_disk= multipath_remove_disk,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700480 .size = multipath_size,
NeilBrown5c675f82014-12-15 12:56:56 +1100481 .congested = multipath_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482};
483
484static int __init multipath_init (void)
485{
NeilBrown2604b702006-01-06 00:20:36 -0800486 return register_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static void __exit multipath_exit (void)
490{
NeilBrown2604b702006-01-06 00:20:36 -0800491 unregister_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494module_init(multipath_init);
495module_exit(multipath_exit);
496MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100497MODULE_DESCRIPTION("simple multi-path personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
NeilBrownd9d166c2006-01-06 00:20:51 -0800499MODULE_ALIAS("md-multipath");
NeilBrown2604b702006-01-06 00:20:36 -0800500MODULE_ALIAS("md-level--4");