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 printk(KERN_ERR "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, int err)
78 struct bio *bio = mp_bh->master_bio;
79 struct mpconf *conf = mp_bh->mddev->private;
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_rw & REQ_RAHEAD)) {
98 char b[BDEVNAME_SIZE];
99 md_error (mp_bh->mddev, rdev);
100 printk(KERN_ERR "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_error);
106 rdev_dec_pending(rdev, conf->mddev);
109 static void 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_rw & 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);
134 __bio_clone_fast(&mp_bh->bio, bio);
136 mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset;
137 mp_bh->bio.bi_bdev = multipath->rdev->bdev;
138 mp_bh->bio.bi_rw |= REQ_FAILFAST_TRANSPORT;
139 mp_bh->bio.bi_end_io = multipath_end_request;
140 mp_bh->bio.bi_private = mp_bh;
141 generic_make_request(&mp_bh->bio);
145 static void multipath_status(struct seq_file *seq, struct mddev *mddev)
147 struct mpconf *conf = mddev->private;
150 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
151 conf->raid_disks - mddev->degraded);
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" : "_");
158 seq_printf (seq, "]");
161 static int multipath_congested(struct mddev *mddev, int bits)
163 struct mpconf *conf = mddev->private;
167 for (i = 0; i < mddev->raid_disks ; i++) {
168 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev);
169 if (rdev && !test_bit(Faulty, &rdev->flags)) {
170 struct request_queue *q = bdev_get_queue(rdev->bdev);
172 ret |= bdi_congested(&q->backing_dev_info, bits);
173 /* Just like multipath_map, we just check the
174 * first available device
184 * Careful, this can execute in IRQ contexts as well!
186 static void multipath_error (struct mddev *mddev, struct md_rdev *rdev)
188 struct mpconf *conf = mddev->private;
189 char b[BDEVNAME_SIZE];
191 if (conf->raid_disks - mddev->degraded <= 1) {
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.
198 "multipath: only one IO path left and IO error.\n");
199 /* leave it active... it's all we have */
203 * Mark disk as unusable
205 if (test_and_clear_bit(In_sync, &rdev->flags)) {
207 spin_lock_irqsave(&conf->device_lock, flags);
209 spin_unlock_irqrestore(&conf->device_lock, flags);
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);
221 static void print_multipath_conf (struct mpconf *conf)
224 struct multipath_info *tmp;
226 printk("MULTIPATH conf printout:\n");
228 printk("(conf==NULL)\n");
231 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
234 for (i = 0; i < conf->raid_disks; i++) {
235 char b[BDEVNAME_SIZE];
236 tmp = conf->multipaths + i;
238 printk(" disk%d, o:%d, dev:%s\n",
239 i,!test_bit(Faulty, &tmp->rdev->flags),
240 bdevname(tmp->rdev->bdev,b));
244 static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev)
246 struct mpconf *conf = mddev->private;
247 struct request_queue *q;
250 struct multipath_info *p;
252 int last = mddev->raid_disks - 1;
254 if (rdev->raid_disk >= 0)
255 first = last = rdev->raid_disk;
257 print_multipath_conf(conf);
259 for (path = first; path <= last; path++)
260 if ((p=conf->multipaths+path)->rdev == NULL) {
261 q = rdev->bdev->bd_disk->queue;
262 disk_stack_limits(mddev->gendisk, rdev->bdev,
263 rdev->data_offset << 9);
265 err = md_integrity_add_rdev(rdev, mddev);
268 spin_lock_irq(&conf->device_lock);
270 rdev->raid_disk = path;
271 set_bit(In_sync, &rdev->flags);
272 spin_unlock_irq(&conf->device_lock);
273 rcu_assign_pointer(p->rdev, rdev);
278 print_multipath_conf(conf);
283 static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
285 struct mpconf *conf = mddev->private;
287 int number = rdev->raid_disk;
288 struct multipath_info *p = conf->multipaths + number;
290 print_multipath_conf(conf);
292 if (rdev == p->rdev) {
293 if (test_bit(In_sync, &rdev->flags) ||
294 atomic_read(&rdev->nr_pending)) {
295 printk(KERN_ERR "hot-remove-disk, slot %d is identified"
296 " but is still operational!\n", number);
301 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
303 if (atomic_read(&rdev->nr_pending)) {
304 /* lost the race, try later */
310 err = md_integrity_register(mddev);
314 print_multipath_conf(conf);
319 * This is a kernel thread which:
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.
326 static void multipathd(struct md_thread *thread)
328 struct mddev *mddev = thread->mddev;
329 struct multipath_bh *mp_bh;
332 struct mpconf *conf = mddev->private;
333 struct list_head *head = &conf->retry_list;
335 md_check_recovery(mddev);
337 char b[BDEVNAME_SIZE];
338 spin_lock_irqsave(&conf->device_lock, flags);
339 if (list_empty(head))
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);
346 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector;
348 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),
352 (unsigned long long)bio->bi_iter.bi_sector);
353 multipath_end_bh_io(mp_bh, -EIO);
355 printk(KERN_ERR "multipath: %s: redirecting sector %llu"
356 " to another IO path\n",
357 bdevname(bio->bi_bdev,b),
358 (unsigned long long)bio->bi_iter.bi_sector);
359 *bio = *(mp_bh->master_bio);
360 bio->bi_iter.bi_sector +=
361 conf->multipaths[mp_bh->path].rdev->data_offset;
362 bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev;
363 bio->bi_rw |= REQ_FAILFAST_TRANSPORT;
364 bio->bi_end_io = multipath_end_request;
365 bio->bi_private = mp_bh;
366 generic_make_request(bio);
369 spin_unlock_irqrestore(&conf->device_lock, flags);
372 static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks)
374 WARN_ONCE(sectors || raid_disks,
375 "%s does not support generic reshape\n", __func__);
377 return mddev->dev_sectors;
380 static int multipath_run (struct mddev *mddev)
384 struct multipath_info *disk;
385 struct md_rdev *rdev;
388 if (md_check_no_bitmap(mddev))
391 if (mddev->level != LEVEL_MULTIPATH) {
392 printk("multipath: %s: raid level not set to multipath IO (%d)\n",
393 mdname(mddev), mddev->level);
397 * copy the already verified devices into our private MULTIPATH
398 * bookkeeping area. [whatever we allocate in multipath_run(),
399 * should be freed in multipath_free()]
402 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL);
403 mddev->private = conf;
406 "multipath: couldn't allocate memory for %s\n",
411 conf->multipaths = kzalloc(sizeof(struct multipath_info)*mddev->raid_disks,
413 if (!conf->multipaths) {
415 "multipath: couldn't allocate memory for %s\n",
421 rdev_for_each(rdev, mddev) {
422 disk_idx = rdev->raid_disk;
424 disk_idx >= mddev->raid_disks)
427 disk = conf->multipaths + disk_idx;
429 disk_stack_limits(mddev->gendisk, rdev->bdev,
430 rdev->data_offset << 9);
432 if (!test_bit(Faulty, &rdev->flags))
436 conf->raid_disks = mddev->raid_disks;
438 spin_lock_init(&conf->device_lock);
439 INIT_LIST_HEAD(&conf->retry_list);
441 if (!working_disks) {
442 printk(KERN_ERR "multipath: no operational IO paths for %s\n",
446 mddev->degraded = conf->raid_disks - working_disks;
448 conf->pool = mempool_create_kmalloc_pool(NR_RESERVED_BUFS,
449 sizeof(struct multipath_bh));
450 if (conf->pool == NULL) {
452 "multipath: couldn't allocate memory for %s\n",
458 mddev->thread = md_register_thread(multipathd, mddev,
460 if (!mddev->thread) {
461 printk(KERN_ERR "multipath: couldn't allocate thread"
462 " for %s\n", mdname(mddev));
468 "multipath: array %s active with %d out of %d IO paths\n",
469 mdname(mddev), conf->raid_disks - mddev->degraded,
472 * Ok, everything is just fine now
474 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
476 if (md_integrity_register(mddev))
482 mempool_destroy(conf->pool);
483 kfree(conf->multipaths);
485 mddev->private = NULL;
490 static void multipath_free(struct mddev *mddev, void *priv)
492 struct mpconf *conf = priv;
494 mempool_destroy(conf->pool);
495 kfree(conf->multipaths);
499 static struct md_personality multipath_personality =
502 .level = LEVEL_MULTIPATH,
503 .owner = THIS_MODULE,
504 .make_request = multipath_make_request,
505 .run = multipath_run,
506 .free = multipath_free,
507 .status = multipath_status,
508 .error_handler = multipath_error,
509 .hot_add_disk = multipath_add_disk,
510 .hot_remove_disk= multipath_remove_disk,
511 .size = multipath_size,
512 .congested = multipath_congested,
515 static int __init multipath_init (void)
517 return register_md_personality (&multipath_personality);
520 static void __exit multipath_exit (void)
522 unregister_md_personality (&multipath_personality);
525 module_init(multipath_init);
526 module_exit(multipath_exit);
527 MODULE_LICENSE("GPL");
528 MODULE_DESCRIPTION("simple multi-path personality for MD");
529 MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
530 MODULE_ALIAS("md-multipath");
531 MODULE_ALIAS("md-level--4");