2 * multipath.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * MULTIPATH management functions.
10 * derived from raid1.c.
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)
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.
22 #include <linux/blkdev.h>
23 #include <linux/module.h>
24 #include <linux/raid/md_u.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
28 #include "multipath.h"
30 #define MAX_WORK_PER_DISK 128
32 #define NR_RESERVED_BUFS 32
34 static int multipath_map (struct mpconf
*conf
)
36 int i
, disks
= conf
->raid_disks
;
39 * Later we do read balancing on the read side
40 * now we use the first available disk.
44 for (i
= 0; i
< disks
; i
++) {
45 struct md_rdev
*rdev
= rcu_dereference(conf
->multipaths
[i
].rdev
);
46 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
47 !test_bit(Faulty
, &rdev
->flags
)) {
48 atomic_inc(&rdev
->nr_pending
);
55 pr_crit_ratelimited("multipath_map(): no more operational IO paths?\n");
59 static void multipath_reschedule_retry (struct multipath_bh
*mp_bh
)
62 struct mddev
*mddev
= mp_bh
->mddev
;
63 struct mpconf
*conf
= mddev
->private;
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
);
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
76 static void multipath_end_bh_io(struct multipath_bh
*mp_bh
, blk_status_t status
)
78 struct bio
*bio
= mp_bh
->master_bio
;
79 struct mpconf
*conf
= mp_bh
->mddev
->private;
81 bio
->bi_status
= status
;
83 mempool_free(mp_bh
, conf
->pool
);
86 static void multipath_end_request(struct bio
*bio
)
88 struct multipath_bh
*mp_bh
= bio
->bi_private
;
89 struct mpconf
*conf
= mp_bh
->mddev
->private;
90 struct md_rdev
*rdev
= conf
->multipaths
[mp_bh
->path
].rdev
;
93 multipath_end_bh_io(mp_bh
, 0);
94 else if (!(bio
->bi_opf
& REQ_RAHEAD
)) {
98 char b
[BDEVNAME_SIZE
];
99 md_error (mp_bh
->mddev
, rdev
);
100 pr_info("multipath: %s: rescheduling sector %llu\n",
101 bdevname(rdev
->bdev
,b
),
102 (unsigned long long)bio
->bi_iter
.bi_sector
);
103 multipath_reschedule_retry(mp_bh
);
105 multipath_end_bh_io(mp_bh
, bio
->bi_status
);
106 rdev_dec_pending(rdev
, conf
->mddev
);
109 static bool multipath_make_request(struct mddev
*mddev
, struct bio
* bio
)
111 struct mpconf
*conf
= mddev
->private;
112 struct multipath_bh
* mp_bh
;
113 struct multipath_info
*multipath
;
115 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
116 md_flush_request(mddev
, bio
);
120 mp_bh
= mempool_alloc(conf
->pool
, GFP_NOIO
);
122 mp_bh
->master_bio
= bio
;
123 mp_bh
->mddev
= mddev
;
125 mp_bh
->path
= multipath_map(conf
);
126 if (mp_bh
->path
< 0) {
128 mempool_free(mp_bh
, conf
->pool
);
131 multipath
= conf
->multipaths
+ mp_bh
->path
;
133 bio_init(&mp_bh
->bio
, NULL
, 0);
134 __bio_clone_fast(&mp_bh
->bio
, bio
);
136 mp_bh
->bio
.bi_iter
.bi_sector
+= multipath
->rdev
->data_offset
;
137 bio_set_dev(&mp_bh
->bio
, multipath
->rdev
->bdev
);
138 mp_bh
->bio
.bi_opf
|= REQ_FAILFAST_TRANSPORT
;
139 mp_bh
->bio
.bi_end_io
= multipath_end_request
;
140 mp_bh
->bio
.bi_private
= mp_bh
;
141 mddev_check_writesame(mddev
, &mp_bh
->bio
);
142 mddev_check_write_zeroes(mddev
, &mp_bh
->bio
);
143 generic_make_request(&mp_bh
->bio
);
147 static void multipath_status(struct seq_file
*seq
, struct mddev
*mddev
)
149 struct mpconf
*conf
= mddev
->private;
152 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
,
153 conf
->raid_disks
- mddev
->degraded
);
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" : "_");
160 seq_printf (seq
, "]");
163 static int multipath_congested(struct mddev
*mddev
, int bits
)
165 struct mpconf
*conf
= mddev
->private;
169 for (i
= 0; i
< mddev
->raid_disks
; i
++) {
170 struct md_rdev
*rdev
= rcu_dereference(conf
->multipaths
[i
].rdev
);
171 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
172 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
174 ret
|= bdi_congested(q
->backing_dev_info
, bits
);
175 /* Just like multipath_map, we just check the
176 * first available device
186 * Careful, this can execute in IRQ contexts as well!
188 static void multipath_error (struct mddev
*mddev
, struct md_rdev
*rdev
)
190 struct mpconf
*conf
= mddev
->private;
191 char b
[BDEVNAME_SIZE
];
193 if (conf
->raid_disks
- mddev
->degraded
<= 1) {
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.
199 pr_warn("multipath: only one IO path left and IO error.\n");
200 /* leave it active... it's all we have */
204 * Mark disk as unusable
206 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
208 spin_lock_irqsave(&conf
->device_lock
, flags
);
210 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
212 set_bit(Faulty
, &rdev
->flags
);
213 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
214 pr_err("multipath: IO failure on %s, disabling IO path.\n"
215 "multipath: Operation continuing on %d IO paths.\n",
216 bdevname(rdev
->bdev
, b
),
217 conf
->raid_disks
- mddev
->degraded
);
220 static void print_multipath_conf (struct mpconf
*conf
)
223 struct multipath_info
*tmp
;
225 pr_debug("MULTIPATH conf printout:\n");
227 pr_debug("(conf==NULL)\n");
230 pr_debug(" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
233 for (i
= 0; i
< conf
->raid_disks
; i
++) {
234 char b
[BDEVNAME_SIZE
];
235 tmp
= conf
->multipaths
+ i
;
237 pr_debug(" disk%d, o:%d, dev:%s\n",
238 i
,!test_bit(Faulty
, &tmp
->rdev
->flags
),
239 bdevname(tmp
->rdev
->bdev
,b
));
243 static int multipath_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
245 struct mpconf
*conf
= mddev
->private;
246 struct request_queue
*q
;
249 struct multipath_info
*p
;
251 int last
= mddev
->raid_disks
- 1;
253 if (rdev
->raid_disk
>= 0)
254 first
= last
= rdev
->raid_disk
;
256 print_multipath_conf(conf
);
258 for (path
= first
; path
<= last
; path
++)
259 if ((p
=conf
->multipaths
+path
)->rdev
== NULL
) {
260 q
= rdev
->bdev
->bd_disk
->queue
;
261 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
262 rdev
->data_offset
<< 9);
264 err
= md_integrity_add_rdev(rdev
, mddev
);
267 spin_lock_irq(&conf
->device_lock
);
269 rdev
->raid_disk
= path
;
270 set_bit(In_sync
, &rdev
->flags
);
271 spin_unlock_irq(&conf
->device_lock
);
272 rcu_assign_pointer(p
->rdev
, rdev
);
277 print_multipath_conf(conf
);
282 static int multipath_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
284 struct mpconf
*conf
= mddev
->private;
286 int number
= rdev
->raid_disk
;
287 struct multipath_info
*p
= conf
->multipaths
+ number
;
289 print_multipath_conf(conf
);
291 if (rdev
== p
->rdev
) {
292 if (test_bit(In_sync
, &rdev
->flags
) ||
293 atomic_read(&rdev
->nr_pending
)) {
294 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number
);
299 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
301 if (atomic_read(&rdev
->nr_pending
)) {
302 /* lost the race, try later */
308 err
= md_integrity_register(mddev
);
312 print_multipath_conf(conf
);
317 * This is a kernel thread which:
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.
324 static void multipathd(struct md_thread
*thread
)
326 struct mddev
*mddev
= thread
->mddev
;
327 struct multipath_bh
*mp_bh
;
330 struct mpconf
*conf
= mddev
->private;
331 struct list_head
*head
= &conf
->retry_list
;
333 md_check_recovery(mddev
);
335 char b
[BDEVNAME_SIZE
];
336 spin_lock_irqsave(&conf
->device_lock
, flags
);
337 if (list_empty(head
))
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
);
344 bio
->bi_iter
.bi_sector
= mp_bh
->master_bio
->bi_iter
.bi_sector
;
346 if ((mp_bh
->path
= multipath_map (conf
))<0) {
347 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n",
349 (unsigned long long)bio
->bi_iter
.bi_sector
);
350 multipath_end_bh_io(mp_bh
, BLK_STS_IOERR
);
352 pr_err("multipath: %s: redirecting sector %llu to another IO path\n",
354 (unsigned long long)bio
->bi_iter
.bi_sector
);
355 *bio
= *(mp_bh
->master_bio
);
356 bio
->bi_iter
.bi_sector
+=
357 conf
->multipaths
[mp_bh
->path
].rdev
->data_offset
;
358 bio_set_dev(bio
, conf
->multipaths
[mp_bh
->path
].rdev
->bdev
);
359 bio
->bi_opf
|= REQ_FAILFAST_TRANSPORT
;
360 bio
->bi_end_io
= multipath_end_request
;
361 bio
->bi_private
= mp_bh
;
362 generic_make_request(bio
);
365 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
368 static sector_t
multipath_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
370 WARN_ONCE(sectors
|| raid_disks
,
371 "%s does not support generic reshape\n", __func__
);
373 return mddev
->dev_sectors
;
376 static int multipath_run (struct mddev
*mddev
)
380 struct multipath_info
*disk
;
381 struct md_rdev
*rdev
;
384 if (md_check_no_bitmap(mddev
))
387 if (mddev
->level
!= LEVEL_MULTIPATH
) {
388 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n",
389 mdname(mddev
), mddev
->level
);
393 * copy the already verified devices into our private MULTIPATH
394 * bookkeeping area. [whatever we allocate in multipath_run(),
395 * should be freed in multipath_free()]
398 conf
= kzalloc(sizeof(struct mpconf
), GFP_KERNEL
);
399 mddev
->private = conf
;
403 conf
->multipaths
= kzalloc(sizeof(struct multipath_info
)*mddev
->raid_disks
,
405 if (!conf
->multipaths
)
409 rdev_for_each(rdev
, mddev
) {
410 disk_idx
= rdev
->raid_disk
;
412 disk_idx
>= mddev
->raid_disks
)
415 disk
= conf
->multipaths
+ disk_idx
;
417 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
418 rdev
->data_offset
<< 9);
420 if (!test_bit(Faulty
, &rdev
->flags
))
424 conf
->raid_disks
= mddev
->raid_disks
;
426 spin_lock_init(&conf
->device_lock
);
427 INIT_LIST_HEAD(&conf
->retry_list
);
429 if (!working_disks
) {
430 pr_warn("multipath: no operational IO paths for %s\n",
434 mddev
->degraded
= conf
->raid_disks
- working_disks
;
436 conf
->pool
= mempool_create_kmalloc_pool(NR_RESERVED_BUFS
,
437 sizeof(struct multipath_bh
));
438 if (conf
->pool
== NULL
)
441 mddev
->thread
= md_register_thread(multipathd
, mddev
,
446 pr_info("multipath: array %s active with %d out of %d IO paths\n",
447 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
,
450 * Ok, everything is just fine now
452 md_set_array_sectors(mddev
, multipath_size(mddev
, 0, 0));
454 if (md_integrity_register(mddev
))
460 mempool_destroy(conf
->pool
);
461 kfree(conf
->multipaths
);
463 mddev
->private = NULL
;
468 static void multipath_free(struct mddev
*mddev
, void *priv
)
470 struct mpconf
*conf
= priv
;
472 mempool_destroy(conf
->pool
);
473 kfree(conf
->multipaths
);
477 static struct md_personality multipath_personality
=
480 .level
= LEVEL_MULTIPATH
,
481 .owner
= THIS_MODULE
,
482 .make_request
= multipath_make_request
,
483 .run
= multipath_run
,
484 .free
= multipath_free
,
485 .status
= multipath_status
,
486 .error_handler
= multipath_error
,
487 .hot_add_disk
= multipath_add_disk
,
488 .hot_remove_disk
= multipath_remove_disk
,
489 .size
= multipath_size
,
490 .congested
= multipath_congested
,
493 static int __init
multipath_init (void)
495 return register_md_personality (&multipath_personality
);
498 static void __exit
multipath_exit (void)
500 unregister_md_personality (&multipath_personality
);
503 module_init(multipath_init
);
504 module_exit(multipath_exit
);
505 MODULE_LICENSE("GPL");
506 MODULE_DESCRIPTION("simple multi-path personality for MD");
507 MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
508 MODULE_ALIAS("md-multipath");
509 MODULE_ALIAS("md-level--4");