blob: 6780938d29914e0ed4aef76fc33d9fa44e2be5ed [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
Jens Axboe1eff9d32016-08-05 15:35:16 -0600107 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200108 md_flush_request(mddev, bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000109 return true;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700110 }
111
Kent Overstreetafeee512018-05-20 18:25:52 -0400112 mp_bh = mempool_alloc(&conf->pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 mp_bh->master_bio = bio;
115 mp_bh->mddev = mddev;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 mp_bh->path = multipath_map(conf);
118 if (mp_bh->path < 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200119 bio_io_error(bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400120 mempool_free(mp_bh, &conf->pool);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000121 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 multipath = conf->multipaths + mp_bh->path;
124
Ming Lei3a83f462016-11-22 08:57:21 -0700125 bio_init(&mp_bh->bio, NULL, 0);
Ming Leifafcde32016-03-12 09:29:40 +0800126 __bio_clone_fast(&mp_bh->bio, bio);
127
Kent Overstreet4f024f32013-10-11 15:44:27 -0700128 mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200129 bio_set_dev(&mp_bh->bio, multipath->rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600130 mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mp_bh->bio.bi_end_io = multipath_end_request;
132 mp_bh->bio.bi_private = mp_bh;
Shaohua Li26483812017-02-13 16:21:49 -0800133 mddev_check_writesame(mddev, &mp_bh->bio);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +0200134 mddev_check_write_zeroes(mddev, &mp_bh->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 generic_make_request(&mp_bh->bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000136 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
NeilBrown40cf2122016-06-02 16:19:52 +1000139static void multipath_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
NeilBrown69724e22011-10-11 16:48:57 +1100141 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int i;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
NeilBrown92f861a2011-05-11 14:38:02 +1000145 conf->raid_disks - mddev->degraded);
NeilBrown40cf2122016-06-02 16:19:52 +1000146 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 Elfring3acdb7b2018-01-13 09:49:03 +0100152 seq_putc(seq, ']');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
NeilBrown5c675f82014-12-15 12:56:56 +1100155static int multipath_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700156{
NeilBrown69724e22011-10-11 16:48:57 +1100157 struct mpconf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700158 int i, ret = 0;
159
160 rcu_read_lock();
161 for (i = 0; i < mddev->raid_disks ; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100162 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700163 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200164 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700165
Jan Karadc3b17c2017-02-02 15:56:50 +0100166 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700167 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*
178 * Careful, this can execute in IRQ contexts as well!
179 */
NeilBrownfd01b882011-10-11 16:47:53 +1100180static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
NeilBrown69724e22011-10-11 16:48:57 +1100182 struct mpconf *conf = mddev->private;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000183 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
NeilBrown92f861a2011-05-11 14:38:02 +1000185 if (conf->raid_disks - mddev->degraded <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /*
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 */
NeilBrown72796942016-11-02 14:16:49 +1100191 pr_warn("multipath: only one IO path left and IO error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* leave it active... it's all we have */
NeilBrown6f8d0c72011-05-11 14:38:44 +1000193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
NeilBrown6f8d0c72011-05-11 14:38:44 +1000195 /*
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 Li29530792016-12-08 15:48:19 -0800205 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown72796942016-11-02 14:16:49 +1100206 pr_err("multipath: IO failure on %s, disabling IO path.\n"
207 "multipath: Operation continuing on %d IO paths.\n",
NeilBrown6f8d0c72011-05-11 14:38:44 +1000208 bdevname(rdev->bdev, b),
209 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
NeilBrown69724e22011-10-11 16:48:57 +1100212static void print_multipath_conf (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int i;
215 struct multipath_info *tmp;
216
NeilBrown72796942016-11-02 14:16:49 +1100217 pr_debug("MULTIPATH conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!conf) {
NeilBrown72796942016-11-02 14:16:49 +1100219 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return;
221 }
NeilBrown72796942016-11-02 14:16:49 +1100222 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
223 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 for (i = 0; i < conf->raid_disks; i++) {
226 char b[BDEVNAME_SIZE];
227 tmp = conf->multipaths + i;
228 if (tmp->rdev)
NeilBrown72796942016-11-02 14:16:49 +1100229 pr_debug(" disk%d, o:%d, dev:%s\n",
230 i,!test_bit(Faulty, &tmp->rdev->flags),
231 bdevname(tmp->rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233}
234
NeilBrownfd01b882011-10-11 16:47:53 +1100235static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
NeilBrown69724e22011-10-11 16:48:57 +1100237 struct mpconf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +1000238 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int path;
240 struct multipath_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +1000241 int first = 0;
242 int last = mddev->raid_disks - 1;
243
244 if (rdev->raid_disk >= 0)
245 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 print_multipath_conf(conf);
248
Neil Brown6c2fce22008-06-28 08:31:31 +1000249 for (path = first; path <= last; path++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if ((p=conf->multipaths+path)->rdev == NULL) {
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000251 disk_stack_limits(mddev->gendisk, rdev->bdev,
252 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dan Williams1501efa2016-01-13 16:00:07 -0800254 err = md_integrity_add_rdev(rdev, mddev);
255 if (err)
256 break;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000257 spin_lock_irq(&conf->device_lock);
NeilBrown750a8f32006-10-28 10:38:31 -0700258 mddev->degraded--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 rdev->raid_disk = path;
NeilBrownb2d444d2005-11-08 21:39:31 -0800260 set_bit(In_sync, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +1000261 spin_unlock_irq(&conf->device_lock);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800262 rcu_assign_pointer(p->rdev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +1000263 err = 0;
264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 print_multipath_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +1000268
269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
NeilBrownb8321b62011-12-23 10:17:51 +1100272static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
NeilBrown69724e22011-10-11 16:48:57 +1100274 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +1100276 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct multipath_info *p = conf->multipaths + number;
278
279 print_multipath_conf(conf);
280
NeilBrownb8321b62011-12-23 10:17:51 +1100281 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800282 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 atomic_read(&rdev->nr_pending)) {
NeilBrown72796942016-11-02 14:16:49 +1100284 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 err = -EBUSY;
286 goto abort;
287 }
288 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +1000289 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 Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Martin K. Petersena91a2782011-03-17 11:11:05 +0100298 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300abort:
301
302 print_multipath_conf(conf);
303 return err;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
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 Li4ed87312012-10-11 13:34:00 +1100314static void multipathd(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Shaohua Li4ed87312012-10-11 13:34:00 +1100316 struct mddev *mddev = thread->mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct multipath_bh *mp_bh;
318 struct bio *bio;
319 unsigned long flags;
NeilBrown69724e22011-10-11 16:48:57 +1100320 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 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 Overstreet4f024f32013-10-11 15:44:27 -0700334 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if ((mp_bh->path = multipath_map (conf))<0) {
NeilBrown72796942016-11-02 14:16:49 +1100337 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200338 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100339 (unsigned long long)bio->bi_iter.bi_sector);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200340 multipath_end_bh_io(mp_bh, BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 } else {
NeilBrown72796942016-11-02 14:16:49 +1100342 pr_err("multipath: %s: redirecting sector %llu to another IO path\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200343 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100344 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *bio = *(mp_bh->master_bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700346 bio->bi_iter.bi_sector +=
347 conf->multipaths[mp_bh->path].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200348 bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600349 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 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
NeilBrownfd01b882011-10-11 16:47:53 +1100358static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700359{
360 WARN_ONCE(sectors || raid_disks,
361 "%s does not support generic reshape\n", __func__);
362
363 return mddev->dev_sectors;
364}
365
NeilBrownfd01b882011-10-11 16:47:53 +1100366static int multipath_run (struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
NeilBrown69724e22011-10-11 16:48:57 +1100368 struct mpconf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int disk_idx;
370 struct multipath_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +1100371 struct md_rdev *rdev;
NeilBrown92f861a2011-05-11 14:38:02 +1000372 int working_disks;
Kent Overstreetafeee512018-05-20 18:25:52 -0400373 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Andre Noll0894cc32009-06-18 08:49:23 +1000375 if (md_check_no_bitmap(mddev))
376 return -EINVAL;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (mddev->level != LEVEL_MULTIPATH) {
NeilBrown72796942016-11-02 14:16:49 +1100379 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
380 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto out;
382 }
383 /*
384 * copy the already verified devices into our private MULTIPATH
385 * bookkeeping area. [whatever we allocate in multipath_run(),
NeilBrownafa0f552014-12-15 12:56:58 +1100386 * should be freed in multipath_free()]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388
NeilBrown69724e22011-10-11 16:48:57 +1100389 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 mddev->private = conf;
NeilBrown72796942016-11-02 14:16:49 +1100391 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Kees Cook6396bb22018-06-12 14:03:40 -0700394 conf->multipaths = kcalloc(mddev->raid_disks,
395 sizeof(struct multipath_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 GFP_KERNEL);
NeilBrown72796942016-11-02 14:16:49 +1100397 if (!conf->multipaths)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
NeilBrown92f861a2011-05-11 14:38:02 +1000400 working_disks = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100401 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 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. Petersen8f6c2e42009-07-01 11:13:45 +1000409 disk_stack_limits(mddev->gendisk, rdev->bdev,
410 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
NeilBrownb2d444d2005-11-08 21:39:31 -0800412 if (!test_bit(Faulty, &rdev->flags))
NeilBrown92f861a2011-05-11 14:38:02 +1000413 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 conf->mddev = mddev;
418 spin_lock_init(&conf->device_lock);
419 INIT_LIST_HEAD(&conf->retry_list);
420
NeilBrown92f861a2011-05-11 14:38:02 +1000421 if (!working_disks) {
NeilBrown72796942016-11-02 14:16:49 +1100422 pr_warn("multipath: no operational IO paths for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 mdname(mddev));
424 goto out_free_conf;
425 }
NeilBrown92f861a2011-05-11 14:38:02 +1000426 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Kent Overstreetafeee512018-05-20 18:25:52 -0400428 ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS,
429 sizeof(struct multipath_bh));
430 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
NeilBrown72796942016-11-02 14:16:49 +1100433 mddev->thread = md_register_thread(multipathd, mddev,
434 "multipath");
435 if (!mddev->thread)
436 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
NeilBrown72796942016-11-02 14:16:49 +1100438 pr_info("multipath: array %s active with %d out of %d IO paths\n",
NeilBrown92f861a2011-05-11 14:38:02 +1000439 mdname(mddev), conf->raid_disks - mddev->degraded,
NeilBrown72796942016-11-02 14:16:49 +1100440 mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Ok, everything is just fine now
443 */
Dan Williams1f403622009-03-31 14:59:03 +1100444 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -0700445
Martin K. Petersena91a2782011-03-17 11:11:05 +0100446 if (md_integrity_register(mddev))
447 goto out_free_conf;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450
451out_free_conf:
Kent Overstreetafeee512018-05-20 18:25:52 -0400452 mempool_exit(&conf->pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700453 kfree(conf->multipaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 kfree(conf);
455 mddev->private = NULL;
456out:
457 return -EIO;
458}
459
NeilBrownafa0f552014-12-15 12:56:58 +1100460static void multipath_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
NeilBrownafa0f552014-12-15 12:56:58 +1100462 struct mpconf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Kent Overstreetafeee512018-05-20 18:25:52 -0400464 mempool_exit(&conf->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 kfree(conf->multipaths);
466 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
NeilBrown84fc4b52011-10-11 16:49:58 +1100469static struct md_personality multipath_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 .name = "multipath",
NeilBrown2604b702006-01-06 00:20:36 -0800472 .level = LEVEL_MULTIPATH,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .owner = THIS_MODULE,
474 .make_request = multipath_make_request,
475 .run = multipath_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100476 .free = multipath_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 .status = multipath_status,
478 .error_handler = multipath_error,
479 .hot_add_disk = multipath_add_disk,
480 .hot_remove_disk= multipath_remove_disk,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700481 .size = multipath_size,
NeilBrown5c675f82014-12-15 12:56:56 +1100482 .congested = multipath_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483};
484
485static int __init multipath_init (void)
486{
NeilBrown2604b702006-01-06 00:20:36 -0800487 return register_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490static void __exit multipath_exit (void)
491{
NeilBrown2604b702006-01-06 00:20:36 -0800492 unregister_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495module_init(multipath_init);
496module_exit(multipath_exit);
497MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100498MODULE_DESCRIPTION("simple multi-path personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
NeilBrownd9d166c2006-01-06 00:20:51 -0800500MODULE_ALIAS("md-multipath");
NeilBrown2604b702006-01-06 00:20:36 -0800501MODULE_ALIAS("md-level--4");