blob: 881487de1e25af5e994c8770e255b74b3864f513 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * multipath.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5 *
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7 *
8 * MULTIPATH management functions.
9 *
10 * derived from raid1.c.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * You should have received a copy of the GNU General Public License
18 * (for example /usr/src/linux/COPYING); if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
NeilBrownbff61972009-03-31 14:33:13 +110022#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040023#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110024#include <linux/raid/md_u.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110027#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040028#include "md-multipath.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define MAX_WORK_PER_DISK 128
31
32#define NR_RESERVED_BUFS 32
33
NeilBrown69724e22011-10-11 16:48:57 +110034static int multipath_map (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 int i, disks = conf->raid_disks;
37
38 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +100039 * Later we do read balancing on the read side
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * now we use the first available disk.
41 */
42
43 rcu_read_lock();
44 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +110045 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +100046 if (rdev && test_bit(In_sync, &rdev->flags) &&
47 !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 atomic_inc(&rdev->nr_pending);
49 rcu_read_unlock();
50 return i;
51 }
52 }
53 rcu_read_unlock();
54
NeilBrown72796942016-11-02 14:16:49 +110055 pr_crit_ratelimited("multipath_map(): no more operational IO paths?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return (-1);
57}
58
59static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
60{
61 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +110062 struct mddev *mddev = mp_bh->mddev;
NeilBrown69724e22011-10-11 16:48:57 +110063 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 spin_lock_irqsave(&conf->device_lock, flags);
66 list_add(&mp_bh->retry_list, &conf->retry_list);
67 spin_unlock_irqrestore(&conf->device_lock, flags);
68 md_wakeup_thread(mddev->thread);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * multipath_end_bh_io() is called when we have finished servicing a multipathed
73 * operation and are ready to return a success/failure code to the buffer
74 * cache layer.
75 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020076static void multipath_end_bh_io(struct multipath_bh *mp_bh, blk_status_t status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 struct bio *bio = mp_bh->master_bio;
NeilBrown69724e22011-10-11 16:48:57 +110079 struct mpconf *conf = mp_bh->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020081 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020082 bio_endio(bio);
Kent Overstreetafeee512018-05-20 18:25:52 -040083 mempool_free(mp_bh, &conf->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020086static void multipath_end_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
H Hartley Sweeten7b928132010-03-08 16:02:40 +110088 struct multipath_bh *mp_bh = bio->bi_private;
NeilBrown69724e22011-10-11 16:48:57 +110089 struct mpconf *conf = mp_bh->mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +110090 struct md_rdev *rdev = conf->multipaths[mp_bh->path].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020092 if (!bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 multipath_end_bh_io(mp_bh, 0);
Jens Axboe1eff9d32016-08-05 15:35:16 -060094 else if (!(bio->bi_opf & REQ_RAHEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /*
96 * oops, IO error:
97 */
98 char b[BDEVNAME_SIZE];
99 md_error (mp_bh->mddev, rdev);
NeilBrown72796942016-11-02 14:16:49 +1100100 pr_info("multipath: %s: rescheduling sector %llu\n",
101 bdevname(rdev->bdev,b),
102 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 multipath_reschedule_retry(mp_bh);
104 } else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200105 multipath_end_bh_io(mp_bh, bio->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
NeilBrowncc27b0c2017-06-05 16:49:39 +1000109static bool multipath_make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
NeilBrown69724e22011-10-11 16:48:57 +1100111 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct multipath_bh * mp_bh;
113 struct multipath_info *multipath;
114
Jens Axboe1eff9d32016-08-05 15:35:16 -0600115 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200116 md_flush_request(mddev, bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000117 return true;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700118 }
119
Kent Overstreetafeee512018-05-20 18:25:52 -0400120 mp_bh = mempool_alloc(&conf->pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 mp_bh->master_bio = bio;
123 mp_bh->mddev = mddev;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 mp_bh->path = multipath_map(conf);
126 if (mp_bh->path < 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200127 bio_io_error(bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400128 mempool_free(mp_bh, &conf->pool);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000129 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 multipath = conf->multipaths + mp_bh->path;
132
Ming Lei3a83f462016-11-22 08:57:21 -0700133 bio_init(&mp_bh->bio, NULL, 0);
Ming Leifafcde32016-03-12 09:29:40 +0800134 __bio_clone_fast(&mp_bh->bio, bio);
135
Kent Overstreet4f024f32013-10-11 15:44:27 -0700136 mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200137 bio_set_dev(&mp_bh->bio, multipath->rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600138 mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 mp_bh->bio.bi_end_io = multipath_end_request;
140 mp_bh->bio.bi_private = mp_bh;
Shaohua Li26483812017-02-13 16:21:49 -0800141 mddev_check_writesame(mddev, &mp_bh->bio);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +0200142 mddev_check_write_zeroes(mddev, &mp_bh->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 generic_make_request(&mp_bh->bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000144 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
NeilBrown40cf2122016-06-02 16:19:52 +1000147static void multipath_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
NeilBrown69724e22011-10-11 16:48:57 +1100149 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int i;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
NeilBrown92f861a2011-05-11 14:38:02 +1000153 conf->raid_disks - mddev->degraded);
NeilBrown40cf2122016-06-02 16:19:52 +1000154 rcu_read_lock();
155 for (i = 0; i < conf->raid_disks; i++) {
156 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
157 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
158 }
159 rcu_read_unlock();
Markus Elfring3acdb7b2018-01-13 09:49:03 +0100160 seq_putc(seq, ']');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
NeilBrown5c675f82014-12-15 12:56:56 +1100163static int multipath_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700164{
NeilBrown69724e22011-10-11 16:48:57 +1100165 struct mpconf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700166 int i, ret = 0;
167
168 rcu_read_lock();
169 for (i = 0; i < mddev->raid_disks ; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100170 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700171 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200172 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700173
Jan Karadc3b17c2017-02-02 15:56:50 +0100174 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700175 /* Just like multipath_map, we just check the
176 * first available device
177 */
178 break;
179 }
180 }
181 rcu_read_unlock();
182 return ret;
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/*
186 * Careful, this can execute in IRQ contexts as well!
187 */
NeilBrownfd01b882011-10-11 16:47:53 +1100188static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
NeilBrown69724e22011-10-11 16:48:57 +1100190 struct mpconf *conf = mddev->private;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000191 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
NeilBrown92f861a2011-05-11 14:38:02 +1000193 if (conf->raid_disks - mddev->degraded <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
195 * Uh oh, we can do nothing if this is our last path, but
196 * first check if this is a queued request for a device
197 * which has just failed.
198 */
NeilBrown72796942016-11-02 14:16:49 +1100199 pr_warn("multipath: only one IO path left and IO error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* leave it active... it's all we have */
NeilBrown6f8d0c72011-05-11 14:38:44 +1000201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
NeilBrown6f8d0c72011-05-11 14:38:44 +1000203 /*
204 * Mark disk as unusable
205 */
206 if (test_and_clear_bit(In_sync, &rdev->flags)) {
207 unsigned long flags;
208 spin_lock_irqsave(&conf->device_lock, flags);
209 mddev->degraded++;
210 spin_unlock_irqrestore(&conf->device_lock, flags);
211 }
212 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -0800213 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown72796942016-11-02 14:16:49 +1100214 pr_err("multipath: IO failure on %s, disabling IO path.\n"
215 "multipath: Operation continuing on %d IO paths.\n",
NeilBrown6f8d0c72011-05-11 14:38:44 +1000216 bdevname(rdev->bdev, b),
217 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
NeilBrown69724e22011-10-11 16:48:57 +1100220static void print_multipath_conf (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int i;
223 struct multipath_info *tmp;
224
NeilBrown72796942016-11-02 14:16:49 +1100225 pr_debug("MULTIPATH conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!conf) {
NeilBrown72796942016-11-02 14:16:49 +1100227 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return;
229 }
NeilBrown72796942016-11-02 14:16:49 +1100230 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
231 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 for (i = 0; i < conf->raid_disks; i++) {
234 char b[BDEVNAME_SIZE];
235 tmp = conf->multipaths + i;
236 if (tmp->rdev)
NeilBrown72796942016-11-02 14:16:49 +1100237 pr_debug(" disk%d, o:%d, dev:%s\n",
238 i,!test_bit(Faulty, &tmp->rdev->flags),
239 bdevname(tmp->rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241}
242
NeilBrownfd01b882011-10-11 16:47:53 +1100243static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
NeilBrown69724e22011-10-11 16:48:57 +1100245 struct mpconf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +1000246 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int path;
248 struct multipath_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +1000249 int first = 0;
250 int last = mddev->raid_disks - 1;
251
252 if (rdev->raid_disk >= 0)
253 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 print_multipath_conf(conf);
256
Neil Brown6c2fce22008-06-28 08:31:31 +1000257 for (path = first; path <= last; path++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if ((p=conf->multipaths+path)->rdev == NULL) {
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000259 disk_stack_limits(mddev->gendisk, rdev->bdev,
260 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dan Williams1501efa2016-01-13 16:00:07 -0800262 err = md_integrity_add_rdev(rdev, mddev);
263 if (err)
264 break;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000265 spin_lock_irq(&conf->device_lock);
NeilBrown750a8f32006-10-28 10:38:31 -0700266 mddev->degraded--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 rdev->raid_disk = path;
NeilBrownb2d444d2005-11-08 21:39:31 -0800268 set_bit(In_sync, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +1000269 spin_unlock_irq(&conf->device_lock);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800270 rcu_assign_pointer(p->rdev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +1000271 err = 0;
272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 print_multipath_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +1000276
277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
NeilBrownb8321b62011-12-23 10:17:51 +1100280static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
NeilBrown69724e22011-10-11 16:48:57 +1100282 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +1100284 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct multipath_info *p = conf->multipaths + number;
286
287 print_multipath_conf(conf);
288
NeilBrownb8321b62011-12-23 10:17:51 +1100289 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800290 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 atomic_read(&rdev->nr_pending)) {
NeilBrown72796942016-11-02 14:16:49 +1100292 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 err = -EBUSY;
294 goto abort;
295 }
296 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +1000297 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
298 synchronize_rcu();
299 if (atomic_read(&rdev->nr_pending)) {
300 /* lost the race, try later */
301 err = -EBUSY;
302 p->rdev = rdev;
303 goto abort;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Martin K. Petersena91a2782011-03-17 11:11:05 +0100306 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308abort:
309
310 print_multipath_conf(conf);
311 return err;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*
315 * This is a kernel thread which:
316 *
317 * 1. Retries failed read operations on working multipaths.
318 * 2. Updates the raid superblock when problems encounter.
319 * 3. Performs writes following reads for array syncronising.
320 */
321
Shaohua Li4ed87312012-10-11 13:34:00 +1100322static void multipathd(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Shaohua Li4ed87312012-10-11 13:34:00 +1100324 struct mddev *mddev = thread->mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct multipath_bh *mp_bh;
326 struct bio *bio;
327 unsigned long flags;
NeilBrown69724e22011-10-11 16:48:57 +1100328 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct list_head *head = &conf->retry_list;
330
331 md_check_recovery(mddev);
332 for (;;) {
333 char b[BDEVNAME_SIZE];
334 spin_lock_irqsave(&conf->device_lock, flags);
335 if (list_empty(head))
336 break;
337 mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
338 list_del(head->prev);
339 spin_unlock_irqrestore(&conf->device_lock, flags);
340
341 bio = &mp_bh->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700342 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if ((mp_bh->path = multipath_map (conf))<0) {
NeilBrown72796942016-11-02 14:16:49 +1100345 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200346 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100347 (unsigned long long)bio->bi_iter.bi_sector);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200348 multipath_end_bh_io(mp_bh, BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
NeilBrown72796942016-11-02 14:16:49 +1100350 pr_err("multipath: %s: redirecting sector %llu to another IO path\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200351 bio_devname(bio, b),
NeilBrown72796942016-11-02 14:16:49 +1100352 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *bio = *(mp_bh->master_bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700354 bio->bi_iter.bi_sector +=
355 conf->multipaths[mp_bh->path].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200356 bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600357 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 bio->bi_end_io = multipath_end_request;
359 bio->bi_private = mp_bh;
360 generic_make_request(bio);
361 }
362 }
363 spin_unlock_irqrestore(&conf->device_lock, flags);
364}
365
NeilBrownfd01b882011-10-11 16:47:53 +1100366static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700367{
368 WARN_ONCE(sectors || raid_disks,
369 "%s does not support generic reshape\n", __func__);
370
371 return mddev->dev_sectors;
372}
373
NeilBrownfd01b882011-10-11 16:47:53 +1100374static int multipath_run (struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
NeilBrown69724e22011-10-11 16:48:57 +1100376 struct mpconf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int disk_idx;
378 struct multipath_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +1100379 struct md_rdev *rdev;
NeilBrown92f861a2011-05-11 14:38:02 +1000380 int working_disks;
Kent Overstreetafeee512018-05-20 18:25:52 -0400381 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Andre Noll0894cc32009-06-18 08:49:23 +1000383 if (md_check_no_bitmap(mddev))
384 return -EINVAL;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (mddev->level != LEVEL_MULTIPATH) {
NeilBrown72796942016-11-02 14:16:49 +1100387 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
388 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto out;
390 }
391 /*
392 * copy the already verified devices into our private MULTIPATH
393 * bookkeeping area. [whatever we allocate in multipath_run(),
NeilBrownafa0f552014-12-15 12:56:58 +1100394 * should be freed in multipath_free()]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396
NeilBrown69724e22011-10-11 16:48:57 +1100397 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 mddev->private = conf;
NeilBrown72796942016-11-02 14:16:49 +1100399 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Kees Cook6396bb22018-06-12 14:03:40 -0700402 conf->multipaths = kcalloc(mddev->raid_disks,
403 sizeof(struct multipath_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 GFP_KERNEL);
NeilBrown72796942016-11-02 14:16:49 +1100405 if (!conf->multipaths)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
NeilBrown92f861a2011-05-11 14:38:02 +1000408 working_disks = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100409 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 disk_idx = rdev->raid_disk;
411 if (disk_idx < 0 ||
412 disk_idx >= mddev->raid_disks)
413 continue;
414
415 disk = conf->multipaths + disk_idx;
416 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000417 disk_stack_limits(mddev->gendisk, rdev->bdev,
418 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
NeilBrownb2d444d2005-11-08 21:39:31 -0800420 if (!test_bit(Faulty, &rdev->flags))
NeilBrown92f861a2011-05-11 14:38:02 +1000421 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 conf->mddev = mddev;
426 spin_lock_init(&conf->device_lock);
427 INIT_LIST_HEAD(&conf->retry_list);
428
NeilBrown92f861a2011-05-11 14:38:02 +1000429 if (!working_disks) {
NeilBrown72796942016-11-02 14:16:49 +1100430 pr_warn("multipath: no operational IO paths for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 mdname(mddev));
432 goto out_free_conf;
433 }
NeilBrown92f861a2011-05-11 14:38:02 +1000434 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Kent Overstreetafeee512018-05-20 18:25:52 -0400436 ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS,
437 sizeof(struct multipath_bh));
438 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
NeilBrown72796942016-11-02 14:16:49 +1100441 mddev->thread = md_register_thread(multipathd, mddev,
442 "multipath");
443 if (!mddev->thread)
444 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
NeilBrown72796942016-11-02 14:16:49 +1100446 pr_info("multipath: array %s active with %d out of %d IO paths\n",
NeilBrown92f861a2011-05-11 14:38:02 +1000447 mdname(mddev), conf->raid_disks - mddev->degraded,
NeilBrown72796942016-11-02 14:16:49 +1100448 mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * Ok, everything is just fine now
451 */
Dan Williams1f403622009-03-31 14:59:03 +1100452 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -0700453
Martin K. Petersena91a2782011-03-17 11:11:05 +0100454 if (md_integrity_register(mddev))
455 goto out_free_conf;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458
459out_free_conf:
Kent Overstreetafeee512018-05-20 18:25:52 -0400460 mempool_exit(&conf->pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700461 kfree(conf->multipaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 kfree(conf);
463 mddev->private = NULL;
464out:
465 return -EIO;
466}
467
NeilBrownafa0f552014-12-15 12:56:58 +1100468static void multipath_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
NeilBrownafa0f552014-12-15 12:56:58 +1100470 struct mpconf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Kent Overstreetafeee512018-05-20 18:25:52 -0400472 mempool_exit(&conf->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 kfree(conf->multipaths);
474 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
NeilBrown84fc4b52011-10-11 16:49:58 +1100477static struct md_personality multipath_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 .name = "multipath",
NeilBrown2604b702006-01-06 00:20:36 -0800480 .level = LEVEL_MULTIPATH,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .owner = THIS_MODULE,
482 .make_request = multipath_make_request,
483 .run = multipath_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100484 .free = multipath_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .status = multipath_status,
486 .error_handler = multipath_error,
487 .hot_add_disk = multipath_add_disk,
488 .hot_remove_disk= multipath_remove_disk,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700489 .size = multipath_size,
NeilBrown5c675f82014-12-15 12:56:56 +1100490 .congested = multipath_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491};
492
493static int __init multipath_init (void)
494{
NeilBrown2604b702006-01-06 00:20:36 -0800495 return register_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498static void __exit multipath_exit (void)
499{
NeilBrown2604b702006-01-06 00:20:36 -0800500 unregister_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503module_init(multipath_init);
504module_exit(multipath_exit);
505MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100506MODULE_DESCRIPTION("simple multi-path personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
NeilBrownd9d166c2006-01-06 00:20:51 -0800508MODULE_ALIAS("md-multipath");
NeilBrown2604b702006-01-06 00:20:36 -0800509MODULE_ALIAS("md-level--4");