blob: 6a7855ab285226cb1be68ac43e9e764b50a93c39 [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"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "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
55 printk(KERN_ERR "multipath_map(): no more operational IO paths?\n");
56 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 */
76static void multipath_end_bh_io (struct multipath_bh *mp_bh, int err)
77{
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 Hellwig4246a0b2015-07-20 15:29:37 +020081 bio->bi_error = err;
82 bio_endio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 mempool_free(mp_bh, conf->pool);
84}
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 Hellwig4246a0b2015-07-20 15:29:37 +020092 if (!bio->bi_error)
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);
NeilBrownf72ffdd2014-09-30 14:23:59 +1000100 printk(KERN_ERR "multipath: %s: rescheduling sector %llu\n",
101 bdevname(rdev->bdev,b),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700102 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 multipath_reschedule_retry(mp_bh);
104 } else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200105 multipath_end_bh_io(mp_bh, bio->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700109static void 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);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200117 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700118 }
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 mp_bh = mempool_alloc(conf->pool, GFP_NOIO);
121
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 mempool_free(mp_bh, conf->pool);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200129 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 multipath = conf->multipaths + mp_bh->path;
132
Ming Leifafcde32016-03-12 09:29:40 +0800133 bio_init(&mp_bh->bio);
134 __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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 mp_bh->bio.bi_bdev = 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;
141 generic_make_request(&mp_bh->bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200142 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
NeilBrown40cf2122016-06-02 16:19:52 +1000145static void multipath_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
NeilBrown69724e22011-10-11 16:48:57 +1100147 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int i;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
NeilBrown92f861a2011-05-11 14:38:02 +1000151 conf->raid_disks - mddev->degraded);
NeilBrown40cf2122016-06-02 16:19:52 +1000152 rcu_read_lock();
153 for (i = 0; i < conf->raid_disks; i++) {
154 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
155 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
156 }
157 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 seq_printf (seq, "]");
159}
160
NeilBrown5c675f82014-12-15 12:56:56 +1100161static int multipath_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700162{
NeilBrown69724e22011-10-11 16:48:57 +1100163 struct mpconf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700164 int i, ret = 0;
165
166 rcu_read_lock();
167 for (i = 0; i < mddev->raid_disks ; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100168 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700169 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200170 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700171
Jan Kara1cdb7f92017-02-02 15:56:50 +0100172 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700173 /* Just like multipath_map, we just check the
174 * first available device
175 */
176 break;
177 }
178 }
179 rcu_read_unlock();
180 return ret;
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/*
184 * Careful, this can execute in IRQ contexts as well!
185 */
NeilBrownfd01b882011-10-11 16:47:53 +1100186static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
NeilBrown69724e22011-10-11 16:48:57 +1100188 struct mpconf *conf = mddev->private;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000189 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
NeilBrown92f861a2011-05-11 14:38:02 +1000191 if (conf->raid_disks - mddev->degraded <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Uh oh, we can do nothing if this is our last path, but
194 * first check if this is a queued request for a device
195 * which has just failed.
196 */
NeilBrownf72ffdd2014-09-30 14:23:59 +1000197 printk(KERN_ALERT
NeilBrown6f8d0c72011-05-11 14:38:44 +1000198 "multipath: only one IO path left and IO error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* leave it active... it's all we have */
NeilBrown6f8d0c72011-05-11 14:38:44 +1000200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
NeilBrown6f8d0c72011-05-11 14:38:44 +1000202 /*
203 * Mark disk as unusable
204 */
205 if (test_and_clear_bit(In_sync, &rdev->flags)) {
206 unsigned long flags;
207 spin_lock_irqsave(&conf->device_lock, flags);
208 mddev->degraded++;
209 spin_unlock_irqrestore(&conf->device_lock, flags);
210 }
211 set_bit(Faulty, &rdev->flags);
212 set_bit(MD_CHANGE_DEVS, &mddev->flags);
213 printk(KERN_ALERT "multipath: IO failure on %s,"
214 " disabling IO path.\n"
215 "multipath: Operation continuing"
216 " on %d IO paths.\n",
217 bdevname(rdev->bdev, b),
218 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
NeilBrown69724e22011-10-11 16:48:57 +1100221static void print_multipath_conf (struct mpconf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int i;
224 struct multipath_info *tmp;
225
226 printk("MULTIPATH conf printout:\n");
227 if (!conf) {
228 printk("(conf==NULL)\n");
229 return;
230 }
NeilBrown92f861a2011-05-11 14:38:02 +1000231 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 conf->raid_disks);
233
234 for (i = 0; i < conf->raid_disks; i++) {
235 char b[BDEVNAME_SIZE];
236 tmp = conf->multipaths + i;
237 if (tmp->rdev)
238 printk(" disk%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800239 i,!test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 bdevname(tmp->rdev->bdev,b));
241 }
242}
243
NeilBrownfd01b882011-10-11 16:47:53 +1100244static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrown69724e22011-10-11 16:48:57 +1100246 struct mpconf *conf = mddev->private;
Jesper Juhl352d7682006-01-09 20:54:44 -0800247 struct request_queue *q;
Neil Brown199050e2008-06-28 08:31:33 +1000248 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int path;
250 struct multipath_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +1000251 int first = 0;
252 int last = mddev->raid_disks - 1;
253
254 if (rdev->raid_disk >= 0)
255 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 print_multipath_conf(conf);
258
Neil Brown6c2fce22008-06-28 08:31:31 +1000259 for (path = first; path <= last; path++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if ((p=conf->multipaths+path)->rdev == NULL) {
Jesper Juhl352d7682006-01-09 20:54:44 -0800261 q = rdev->bdev->bd_disk->queue;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000262 disk_stack_limits(mddev->gendisk, rdev->bdev,
263 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Dan Williams1501efa2016-01-13 16:00:07 -0800265 err = md_integrity_add_rdev(rdev, mddev);
266 if (err)
267 break;
NeilBrown6f8d0c72011-05-11 14:38:44 +1000268 spin_lock_irq(&conf->device_lock);
NeilBrown750a8f32006-10-28 10:38:31 -0700269 mddev->degraded--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 rdev->raid_disk = path;
NeilBrownb2d444d2005-11-08 21:39:31 -0800271 set_bit(In_sync, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +1000272 spin_unlock_irq(&conf->device_lock);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800273 rcu_assign_pointer(p->rdev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +1000274 err = 0;
275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
278 print_multipath_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +1000279
280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
NeilBrownb8321b62011-12-23 10:17:51 +1100283static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
NeilBrown69724e22011-10-11 16:48:57 +1100285 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +1100287 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct multipath_info *p = conf->multipaths + number;
289
290 print_multipath_conf(conf);
291
NeilBrownb8321b62011-12-23 10:17:51 +1100292 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800293 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 atomic_read(&rdev->nr_pending)) {
NeilBrowndfc70642008-05-23 13:04:39 -0700295 printk(KERN_ERR "hot-remove-disk, slot %d is identified"
296 " but is still operational!\n", number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 err = -EBUSY;
298 goto abort;
299 }
300 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +1000301 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
302 synchronize_rcu();
303 if (atomic_read(&rdev->nr_pending)) {
304 /* lost the race, try later */
305 err = -EBUSY;
306 p->rdev = rdev;
307 goto abort;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Martin K. Petersena91a2782011-03-17 11:11:05 +0100310 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312abort:
313
314 print_multipath_conf(conf);
315 return err;
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * This is a kernel thread which:
320 *
321 * 1. Retries failed read operations on working multipaths.
322 * 2. Updates the raid superblock when problems encounter.
323 * 3. Performs writes following reads for array syncronising.
324 */
325
Shaohua Li4ed87312012-10-11 13:34:00 +1100326static void multipathd(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Shaohua Li4ed87312012-10-11 13:34:00 +1100328 struct mddev *mddev = thread->mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct multipath_bh *mp_bh;
330 struct bio *bio;
331 unsigned long flags;
NeilBrown69724e22011-10-11 16:48:57 +1100332 struct mpconf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct list_head *head = &conf->retry_list;
334
335 md_check_recovery(mddev);
336 for (;;) {
337 char b[BDEVNAME_SIZE];
338 spin_lock_irqsave(&conf->device_lock, flags);
339 if (list_empty(head))
340 break;
341 mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
342 list_del(head->prev);
343 spin_unlock_irqrestore(&conf->device_lock, flags);
344
345 bio = &mp_bh->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700346 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
NeilBrownf72ffdd2014-09-30 14:23:59 +1000347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if ((mp_bh->path = multipath_map (conf))<0) {
349 printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
350 " error for block %llu\n",
351 bdevname(bio->bi_bdev,b),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700352 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 multipath_end_bh_io(mp_bh, -EIO);
354 } else {
355 printk(KERN_ERR "multipath: %s: redirecting sector %llu"
356 " to another IO path\n",
357 bdevname(bio->bi_bdev,b),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700358 (unsigned long long)bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *bio = *(mp_bh->master_bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700360 bio->bi_iter.bi_sector +=
361 conf->multipaths[mp_bh->path].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600363 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 bio->bi_end_io = multipath_end_request;
365 bio->bi_private = mp_bh;
366 generic_make_request(bio);
367 }
368 }
369 spin_unlock_irqrestore(&conf->device_lock, flags);
370}
371
NeilBrownfd01b882011-10-11 16:47:53 +1100372static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700373{
374 WARN_ONCE(sectors || raid_disks,
375 "%s does not support generic reshape\n", __func__);
376
377 return mddev->dev_sectors;
378}
379
NeilBrownfd01b882011-10-11 16:47:53 +1100380static int multipath_run (struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
NeilBrown69724e22011-10-11 16:48:57 +1100382 struct mpconf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int disk_idx;
384 struct multipath_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +1100385 struct md_rdev *rdev;
NeilBrown92f861a2011-05-11 14:38:02 +1000386 int working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Andre Noll0894cc32009-06-18 08:49:23 +1000388 if (md_check_no_bitmap(mddev))
389 return -EINVAL;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (mddev->level != LEVEL_MULTIPATH) {
392 printk("multipath: %s: raid level not set to multipath IO (%d)\n",
393 mdname(mddev), mddev->level);
394 goto out;
395 }
396 /*
397 * copy the already verified devices into our private MULTIPATH
398 * bookkeeping area. [whatever we allocate in multipath_run(),
NeilBrownafa0f552014-12-15 12:56:58 +1100399 * should be freed in multipath_free()]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
401
NeilBrown69724e22011-10-11 16:48:57 +1100402 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 mddev->private = conf;
404 if (!conf) {
NeilBrownf72ffdd2014-09-30 14:23:59 +1000405 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "multipath: couldn't allocate memory for %s\n",
407 mdname(mddev));
408 goto out;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
NeilBrown9ffae0c2006-01-06 00:20:32 -0800411 conf->multipaths = kzalloc(sizeof(struct multipath_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 GFP_KERNEL);
413 if (!conf->multipaths) {
NeilBrownf72ffdd2014-09-30 14:23:59 +1000414 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 "multipath: couldn't allocate memory for %s\n",
416 mdname(mddev));
417 goto out_free_conf;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
NeilBrown92f861a2011-05-11 14:38:02 +1000420 working_disks = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100421 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 disk_idx = rdev->raid_disk;
423 if (disk_idx < 0 ||
424 disk_idx >= mddev->raid_disks)
425 continue;
426
427 disk = conf->multipaths + disk_idx;
428 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000429 disk_stack_limits(mddev->gendisk, rdev->bdev,
430 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
NeilBrownb2d444d2005-11-08 21:39:31 -0800432 if (!test_bit(Faulty, &rdev->flags))
NeilBrown92f861a2011-05-11 14:38:02 +1000433 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 conf->mddev = mddev;
438 spin_lock_init(&conf->device_lock);
439 INIT_LIST_HEAD(&conf->retry_list);
440
NeilBrown92f861a2011-05-11 14:38:02 +1000441 if (!working_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 printk(KERN_ERR "multipath: no operational IO paths for %s\n",
443 mdname(mddev));
444 goto out_free_conf;
445 }
NeilBrown92f861a2011-05-11 14:38:02 +1000446 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Sage Weilbbba8092009-09-21 17:02:55 -0700448 conf->pool = mempool_create_kmalloc_pool(NR_RESERVED_BUFS,
Matthew Dobson26b6e052006-03-26 01:37:48 -0800449 sizeof(struct multipath_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (conf->pool == NULL) {
NeilBrownf72ffdd2014-09-30 14:23:59 +1000451 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 "multipath: couldn't allocate memory for %s\n",
453 mdname(mddev));
454 goto out_free_conf;
455 }
456
457 {
NeilBrown02326052012-07-03 15:56:52 +1000458 mddev->thread = md_register_thread(multipathd, mddev,
459 "multipath");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!mddev->thread) {
461 printk(KERN_ERR "multipath: couldn't allocate thread"
462 " for %s\n", mdname(mddev));
463 goto out_free_conf;
464 }
465 }
466
NeilBrownf72ffdd2014-09-30 14:23:59 +1000467 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 "multipath: array %s active with %d out of %d IO paths\n",
NeilBrown92f861a2011-05-11 14:38:02 +1000469 mdname(mddev), conf->raid_disks - mddev->degraded,
470 mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Ok, everything is just fine now
473 */
Dan Williams1f403622009-03-31 14:59:03 +1100474 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -0700475
Martin K. Petersena91a2782011-03-17 11:11:05 +0100476 if (md_integrity_register(mddev))
477 goto out_free_conf;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480
481out_free_conf:
Julia Lawall644df1a2015-09-13 14:15:10 +0200482 mempool_destroy(conf->pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700483 kfree(conf->multipaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 kfree(conf);
485 mddev->private = NULL;
486out:
487 return -EIO;
488}
489
NeilBrownafa0f552014-12-15 12:56:58 +1100490static void multipath_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
NeilBrownafa0f552014-12-15 12:56:58 +1100492 struct mpconf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 mempool_destroy(conf->pool);
495 kfree(conf->multipaths);
496 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
NeilBrown84fc4b52011-10-11 16:49:58 +1100499static struct md_personality multipath_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 .name = "multipath",
NeilBrown2604b702006-01-06 00:20:36 -0800502 .level = LEVEL_MULTIPATH,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .owner = THIS_MODULE,
504 .make_request = multipath_make_request,
505 .run = multipath_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100506 .free = multipath_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .status = multipath_status,
508 .error_handler = multipath_error,
509 .hot_add_disk = multipath_add_disk,
510 .hot_remove_disk= multipath_remove_disk,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700511 .size = multipath_size,
NeilBrown5c675f82014-12-15 12:56:56 +1100512 .congested = multipath_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513};
514
515static int __init multipath_init (void)
516{
NeilBrown2604b702006-01-06 00:20:36 -0800517 return register_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static void __exit multipath_exit (void)
521{
NeilBrown2604b702006-01-06 00:20:36 -0800522 unregister_md_personality (&multipath_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525module_init(multipath_init);
526module_exit(multipath_exit);
527MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100528MODULE_DESCRIPTION("simple multi-path personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
NeilBrownd9d166c2006-01-06 00:20:51 -0800530MODULE_ALIAS("md-multipath");
NeilBrown2604b702006-01-06 00:20:36 -0800531MODULE_ALIAS("md-level--4");