libnvdimm/altmap: Track namespace boundaries in altmap
[linux-2.6-block.git] / drivers / md / md-multipath.c
CommitLineData
af1a8899 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
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.
1da177e4
LT
12 */
13
bff61975 14#include <linux/blkdev.h>
056075c7 15#include <linux/module.h>
bff61975 16#include <linux/raid/md_u.h>
bff61975 17#include <linux/seq_file.h>
5a0e3ad6 18#include <linux/slab.h>
43b2e5d8 19#include "md.h"
935fe098 20#include "md-multipath.h"
1da177e4
LT
21
22#define MAX_WORK_PER_DISK 128
23
24#define NR_RESERVED_BUFS 32
25
69724e28 26static int multipath_map (struct mpconf *conf)
1da177e4
LT
27{
28 int i, disks = conf->raid_disks;
29
30 /*
f72ffdd6 31 * Later we do read balancing on the read side
1da177e4
LT
32 * now we use the first available disk.
33 */
34
35 rcu_read_lock();
36 for (i = 0; i < disks; i++) {
3cb03002 37 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
f5b67ae8
N
38 if (rdev && test_bit(In_sync, &rdev->flags) &&
39 !test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
40 atomic_inc(&rdev->nr_pending);
41 rcu_read_unlock();
42 return i;
43 }
44 }
45 rcu_read_unlock();
46
7279694d 47 pr_crit_ratelimited("multipath_map(): no more operational IO paths?\n");
1da177e4
LT
48 return (-1);
49}
50
51static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
52{
53 unsigned long flags;
fd01b88c 54 struct mddev *mddev = mp_bh->mddev;
69724e28 55 struct mpconf *conf = mddev->private;
1da177e4
LT
56
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
1da177e4
LT
63/*
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 */
4e4cbee9 68static void multipath_end_bh_io(struct multipath_bh *mp_bh, blk_status_t status)
1da177e4
LT
69{
70 struct bio *bio = mp_bh->master_bio;
69724e28 71 struct mpconf *conf = mp_bh->mddev->private;
1da177e4 72
4e4cbee9 73 bio->bi_status = status;
4246a0b6 74 bio_endio(bio);
afeee514 75 mempool_free(mp_bh, &conf->pool);
1da177e4
LT
76}
77
4246a0b6 78static void multipath_end_request(struct bio *bio)
1da177e4 79{
7b92813c 80 struct multipath_bh *mp_bh = bio->bi_private;
69724e28 81 struct mpconf *conf = mp_bh->mddev->private;
3cb03002 82 struct md_rdev *rdev = conf->multipaths[mp_bh->path].rdev;
1da177e4 83
4e4cbee9 84 if (!bio->bi_status)
1da177e4 85 multipath_end_bh_io(mp_bh, 0);
1eff9d32 86 else if (!(bio->bi_opf & REQ_RAHEAD)) {
1da177e4
LT
87 /*
88 * oops, IO error:
89 */
90 char b[BDEVNAME_SIZE];
91 md_error (mp_bh->mddev, rdev);
7279694d
N
92 pr_info("multipath: %s: rescheduling sector %llu\n",
93 bdevname(rdev->bdev,b),
94 (unsigned long long)bio->bi_iter.bi_sector);
1da177e4
LT
95 multipath_reschedule_retry(mp_bh);
96 } else
4e4cbee9 97 multipath_end_bh_io(mp_bh, bio->bi_status);
1da177e4 98 rdev_dec_pending(rdev, conf->mddev);
1da177e4
LT
99}
100
cc27b0c7 101static bool multipath_make_request(struct mddev *mddev, struct bio * bio)
1da177e4 102{
69724e28 103 struct mpconf *conf = mddev->private;
1da177e4
LT
104 struct multipath_bh * mp_bh;
105 struct multipath_info *multipath;
106
1eff9d32 107 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
e9c7469b 108 md_flush_request(mddev, bio);
cc27b0c7 109 return true;
e5dcdd80
N
110 }
111
afeee514 112 mp_bh = mempool_alloc(&conf->pool, GFP_NOIO);
1da177e4
LT
113
114 mp_bh->master_bio = bio;
115 mp_bh->mddev = mddev;
116
1da177e4
LT
117 mp_bh->path = multipath_map(conf);
118 if (mp_bh->path < 0) {
4246a0b6 119 bio_io_error(bio);
afeee514 120 mempool_free(mp_bh, &conf->pool);
cc27b0c7 121 return true;
1da177e4
LT
122 }
123 multipath = conf->multipaths + mp_bh->path;
124
3a83f467 125 bio_init(&mp_bh->bio, NULL, 0);
fafcde3a
ML
126 __bio_clone_fast(&mp_bh->bio, bio);
127
4f024f37 128 mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset;
74d46992 129 bio_set_dev(&mp_bh->bio, multipath->rdev->bdev);
1eff9d32 130 mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT;
1da177e4
LT
131 mp_bh->bio.bi_end_io = multipath_end_request;
132 mp_bh->bio.bi_private = mp_bh;
26483819 133 mddev_check_writesame(mddev, &mp_bh->bio);
3deff1a7 134 mddev_check_write_zeroes(mddev, &mp_bh->bio);
1da177e4 135 generic_make_request(&mp_bh->bio);
cc27b0c7 136 return true;
1da177e4
LT
137}
138
40cf2123 139static void multipath_status(struct seq_file *seq, struct mddev *mddev)
1da177e4 140{
69724e28 141 struct mpconf *conf = mddev->private;
1da177e4 142 int i;
f72ffdd6 143
1da177e4 144 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
92f861a7 145 conf->raid_disks - mddev->degraded);
40cf2123
N
146 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();
3acdb7b5 152 seq_putc(seq, ']');
1da177e4
LT
153}
154
5c675f83 155static int multipath_congested(struct mddev *mddev, int bits)
0d129228 156{
69724e28 157 struct mpconf *conf = mddev->private;
0d129228
N
158 int i, ret = 0;
159
160 rcu_read_lock();
161 for (i = 0; i < mddev->raid_disks ; i++) {
3cb03002 162 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
0d129228 163 if (rdev && !test_bit(Faulty, &rdev->flags)) {
165125e1 164 struct request_queue *q = bdev_get_queue(rdev->bdev);
0d129228 165
dc3b17cc 166 ret |= bdi_congested(q->backing_dev_info, bits);
0d129228
N
167 /* 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}
1da177e4
LT
176
177/*
178 * Careful, this can execute in IRQ contexts as well!
179 */
fd01b88c 180static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
1da177e4 181{
69724e28 182 struct mpconf *conf = mddev->private;
6f8d0c77 183 char b[BDEVNAME_SIZE];
1da177e4 184
92f861a7 185 if (conf->raid_disks - mddev->degraded <= 1) {
1da177e4
LT
186 /*
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 */
7279694d 191 pr_warn("multipath: only one IO path left and IO error.\n");
1da177e4 192 /* leave it active... it's all we have */
6f8d0c77
N
193 return;
194 }
195 /*
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);
1da177e4 203 }
6f8d0c77 204 set_bit(Faulty, &rdev->flags);
2953079c 205 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7279694d
N
206 pr_err("multipath: IO failure on %s, disabling IO path.\n"
207 "multipath: Operation continuing on %d IO paths.\n",
6f8d0c77
N
208 bdevname(rdev->bdev, b),
209 conf->raid_disks - mddev->degraded);
1da177e4
LT
210}
211
69724e28 212static void print_multipath_conf (struct mpconf *conf)
1da177e4
LT
213{
214 int i;
215 struct multipath_info *tmp;
216
7279694d 217 pr_debug("MULTIPATH conf printout:\n");
1da177e4 218 if (!conf) {
7279694d 219 pr_debug("(conf==NULL)\n");
1da177e4
LT
220 return;
221 }
7279694d
N
222 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
223 conf->raid_disks);
1da177e4
LT
224
225 for (i = 0; i < conf->raid_disks; i++) {
226 char b[BDEVNAME_SIZE];
227 tmp = conf->multipaths + i;
228 if (tmp->rdev)
7279694d
N
229 pr_debug(" disk%d, o:%d, dev:%s\n",
230 i,!test_bit(Faulty, &tmp->rdev->flags),
231 bdevname(tmp->rdev->bdev,b));
1da177e4
LT
232 }
233}
234
fd01b88c 235static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 236{
69724e28 237 struct mpconf *conf = mddev->private;
199050ea 238 int err = -EEXIST;
1da177e4
LT
239 int path;
240 struct multipath_info *p;
6c2fce2e
NB
241 int first = 0;
242 int last = mddev->raid_disks - 1;
243
244 if (rdev->raid_disk >= 0)
245 first = last = rdev->raid_disk;
1da177e4
LT
246
247 print_multipath_conf(conf);
248
6c2fce2e 249 for (path = first; path <= last; path++)
1da177e4 250 if ((p=conf->multipaths+path)->rdev == NULL) {
8f6c2e4b
MP
251 disk_stack_limits(mddev->gendisk, rdev->bdev,
252 rdev->data_offset << 9);
1da177e4 253
1501efad
DW
254 err = md_integrity_add_rdev(rdev, mddev);
255 if (err)
256 break;
6f8d0c77 257 spin_lock_irq(&conf->device_lock);
750a8f3e 258 mddev->degraded--;
1da177e4 259 rdev->raid_disk = path;
b2d444d7 260 set_bit(In_sync, &rdev->flags);
6f8d0c77 261 spin_unlock_irq(&conf->device_lock);
d6065f7b 262 rcu_assign_pointer(p->rdev, rdev);
199050ea
NB
263 err = 0;
264 break;
1da177e4
LT
265 }
266
267 print_multipath_conf(conf);
199050ea
NB
268
269 return err;
1da177e4
LT
270}
271
b8321b68 272static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 273{
69724e28 274 struct mpconf *conf = mddev->private;
1da177e4 275 int err = 0;
b8321b68 276 int number = rdev->raid_disk;
1da177e4
LT
277 struct multipath_info *p = conf->multipaths + number;
278
279 print_multipath_conf(conf);
280
b8321b68 281 if (rdev == p->rdev) {
b2d444d7 282 if (test_bit(In_sync, &rdev->flags) ||
1da177e4 283 atomic_read(&rdev->nr_pending)) {
7279694d 284 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number);
1da177e4
LT
285 err = -EBUSY;
286 goto abort;
287 }
288 p->rdev = NULL;
d787be40
N
289 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 }
1da177e4 297 }
a91a2785 298 err = md_integrity_register(mddev);
1da177e4
LT
299 }
300abort:
301
302 print_multipath_conf(conf);
303 return err;
304}
305
1da177e4
LT
306/*
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
4ed8731d 314static void multipathd(struct md_thread *thread)
1da177e4 315{
4ed8731d 316 struct mddev *mddev = thread->mddev;
1da177e4
LT
317 struct multipath_bh *mp_bh;
318 struct bio *bio;
319 unsigned long flags;
69724e28 320 struct mpconf *conf = mddev->private;
1da177e4
LT
321 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;
4f024f37 334 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
f72ffdd6 335
1da177e4 336 if ((mp_bh->path = multipath_map (conf))<0) {
7279694d 337 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n",
74d46992 338 bio_devname(bio, b),
7279694d 339 (unsigned long long)bio->bi_iter.bi_sector);
4e4cbee9 340 multipath_end_bh_io(mp_bh, BLK_STS_IOERR);
1da177e4 341 } else {
7279694d 342 pr_err("multipath: %s: redirecting sector %llu to another IO path\n",
74d46992 343 bio_devname(bio, b),
7279694d 344 (unsigned long long)bio->bi_iter.bi_sector);
1da177e4 345 *bio = *(mp_bh->master_bio);
4f024f37
KO
346 bio->bi_iter.bi_sector +=
347 conf->multipaths[mp_bh->path].rdev->data_offset;
74d46992 348 bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev);
1eff9d32 349 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
1da177e4
LT
350 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
fd01b88c 358static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce
DW
359{
360 WARN_ONCE(sectors || raid_disks,
361 "%s does not support generic reshape\n", __func__);
362
363 return mddev->dev_sectors;
364}
365
fd01b88c 366static int multipath_run (struct mddev *mddev)
1da177e4 367{
69724e28 368 struct mpconf *conf;
1da177e4
LT
369 int disk_idx;
370 struct multipath_info *disk;
3cb03002 371 struct md_rdev *rdev;
92f861a7 372 int working_disks;
afeee514 373 int ret;
1da177e4 374
0894cc30
AN
375 if (md_check_no_bitmap(mddev))
376 return -EINVAL;
377
1da177e4 378 if (mddev->level != LEVEL_MULTIPATH) {
7279694d
N
379 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
380 mdname(mddev), mddev->level);
1da177e4
LT
381 goto out;
382 }
383 /*
384 * copy the already verified devices into our private MULTIPATH
385 * bookkeeping area. [whatever we allocate in multipath_run(),
afa0f557 386 * should be freed in multipath_free()]
1da177e4
LT
387 */
388
69724e28 389 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
1da177e4 390 mddev->private = conf;
7279694d 391 if (!conf)
1da177e4 392 goto out;
1da177e4 393
6396bb22
KC
394 conf->multipaths = kcalloc(mddev->raid_disks,
395 sizeof(struct multipath_info),
1da177e4 396 GFP_KERNEL);
7279694d 397 if (!conf->multipaths)
1da177e4 398 goto out_free_conf;
1da177e4 399
92f861a7 400 working_disks = 0;
dafb20fa 401 rdev_for_each(rdev, mddev) {
1da177e4
LT
402 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;
8f6c2e4b
MP
409 disk_stack_limits(mddev->gendisk, rdev->bdev,
410 rdev->data_offset << 9);
1da177e4 411
b2d444d7 412 if (!test_bit(Faulty, &rdev->flags))
92f861a7 413 working_disks++;
1da177e4
LT
414 }
415
416 conf->raid_disks = mddev->raid_disks;
1da177e4
LT
417 conf->mddev = mddev;
418 spin_lock_init(&conf->device_lock);
419 INIT_LIST_HEAD(&conf->retry_list);
420
92f861a7 421 if (!working_disks) {
7279694d 422 pr_warn("multipath: no operational IO paths for %s\n",
1da177e4
LT
423 mdname(mddev));
424 goto out_free_conf;
425 }
92f861a7 426 mddev->degraded = conf->raid_disks - working_disks;
1da177e4 427
afeee514
KO
428 ret = mempool_init_kmalloc_pool(&conf->pool, NR_RESERVED_BUFS,
429 sizeof(struct multipath_bh));
430 if (ret)
1da177e4 431 goto out_free_conf;
1da177e4 432
7279694d
N
433 mddev->thread = md_register_thread(multipathd, mddev,
434 "multipath");
435 if (!mddev->thread)
436 goto out_free_conf;
1da177e4 437
7279694d 438 pr_info("multipath: array %s active with %d out of %d IO paths\n",
92f861a7 439 mdname(mddev), conf->raid_disks - mddev->degraded,
7279694d 440 mddev->raid_disks);
1da177e4
LT
441 /*
442 * Ok, everything is just fine now
443 */
1f403624 444 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
7a5febe9 445
a91a2785
MP
446 if (md_integrity_register(mddev))
447 goto out_free_conf;
448
1da177e4
LT
449 return 0;
450
451out_free_conf:
afeee514 452 mempool_exit(&conf->pool);
990a8baf 453 kfree(conf->multipaths);
1da177e4
LT
454 kfree(conf);
455 mddev->private = NULL;
456out:
457 return -EIO;
458}
459
afa0f557 460static void multipath_free(struct mddev *mddev, void *priv)
1da177e4 461{
afa0f557 462 struct mpconf *conf = priv;
1da177e4 463
afeee514 464 mempool_exit(&conf->pool);
1da177e4
LT
465 kfree(conf->multipaths);
466 kfree(conf);
1da177e4
LT
467}
468
84fc4b56 469static struct md_personality multipath_personality =
1da177e4
LT
470{
471 .name = "multipath",
2604b703 472 .level = LEVEL_MULTIPATH,
1da177e4
LT
473 .owner = THIS_MODULE,
474 .make_request = multipath_make_request,
475 .run = multipath_run,
afa0f557 476 .free = multipath_free,
1da177e4
LT
477 .status = multipath_status,
478 .error_handler = multipath_error,
479 .hot_add_disk = multipath_add_disk,
480 .hot_remove_disk= multipath_remove_disk,
80c3a6ce 481 .size = multipath_size,
5c675f83 482 .congested = multipath_congested,
1da177e4
LT
483};
484
485static int __init multipath_init (void)
486{
2604b703 487 return register_md_personality (&multipath_personality);
1da177e4
LT
488}
489
490static void __exit multipath_exit (void)
491{
2604b703 492 unregister_md_personality (&multipath_personality);
1da177e4
LT
493}
494
495module_init(multipath_init);
496module_exit(multipath_exit);
497MODULE_LICENSE("GPL");
0efb9e61 498MODULE_DESCRIPTION("simple multi-path personality for MD");
1da177e4 499MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
d9d166c2 500MODULE_ALIAS("md-multipath");
2604b703 501MODULE_ALIAS("md-level--4");