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