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/module.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/raid/multipath.h>
26 #include <linux/buffer_head.h>
27 #include <asm/atomic.h>
29 #define MAJOR_NR MD_MAJOR
31 #define MD_PERSONALITY
33 #define MAX_WORK_PER_DISK 128
35 #define NR_RESERVED_BUFS 32
38 static mdk_personality_t multipath_personality;
39 static spinlock_t retry_list_lock = SPIN_LOCK_UNLOCKED;
40 struct multipath_bh *multipath_retry_list = NULL, **multipath_retry_tail;
43 static void *mp_pool_alloc(int gfp_flags, void *data)
45 struct multipath_bh *mpb;
46 mpb = kmalloc(sizeof(*mpb), gfp_flags);
48 memset(mpb, 0, sizeof(*mpb));
52 static void mp_pool_free(void *mpb, void *data)
57 static int multipath_map (mddev_t *mddev, mdk_rdev_t **rdevp)
59 multipath_conf_t *conf = mddev_to_conf(mddev);
60 int i, disks = conf->raid_disks;
63 * Later we do read balancing on the read side
64 * now we use the first available disk.
67 spin_lock_irq(&conf->device_lock);
68 for (i = 0; i < disks; i++) {
69 mdk_rdev_t *rdev = conf->multipaths[i].rdev;
70 if (rdev && rdev->in_sync) {
72 atomic_inc(&rdev->nr_pending);
73 spin_unlock_irq(&conf->device_lock);
77 spin_unlock_irq(&conf->device_lock);
79 printk(KERN_ERR "multipath_map(): no more operational IO paths?\n");
83 static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
86 mddev_t *mddev = mp_bh->mddev;
88 spin_lock_irqsave(&retry_list_lock, flags);
89 if (multipath_retry_list == NULL)
90 multipath_retry_tail = &multipath_retry_list;
91 *multipath_retry_tail = mp_bh;
92 multipath_retry_tail = &mp_bh->next_mp;
93 mp_bh->next_mp = NULL;
94 spin_unlock_irqrestore(&retry_list_lock, flags);
95 md_wakeup_thread(mddev->thread);
100 * multipath_end_bh_io() is called when we have finished servicing a multipathed
101 * operation and are ready to return a success/failure code to the buffer
104 static void multipath_end_bh_io (struct multipath_bh *mp_bh, int uptodate)
106 struct bio *bio = mp_bh->master_bio;
107 multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
109 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
110 mempool_free(mp_bh, conf->pool);
113 int multipath_end_request(struct bio *bio, unsigned int bytes_done, int error)
115 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
116 struct multipath_bh * mp_bh = (struct multipath_bh *)(bio->bi_private);
117 multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
118 mdk_rdev_t *rdev = conf->multipaths[mp_bh->path].rdev;
124 multipath_end_bh_io(mp_bh, uptodate);
129 char b[BDEVNAME_SIZE];
130 md_error (mp_bh->mddev, rdev);
131 printk(KERN_ERR "multipath: %s: rescheduling sector %llu\n",
132 bdevname(rdev->bdev,b),
133 (unsigned long long)bio->bi_sector);
134 multipath_reschedule_retry(mp_bh);
136 atomic_dec(&rdev->nr_pending);
141 * This routine returns the disk from which the requested read should
145 static int multipath_read_balance (multipath_conf_t *conf)
149 for (disk = 0; disk < conf->raid_disks; disk++) {
150 mdk_rdev_t *rdev = conf->multipaths[disk].rdev;
151 if (rdev && rdev->in_sync)
158 static void unplug_slaves(mddev_t *mddev)
160 multipath_conf_t *conf = mddev_to_conf(mddev);
163 for (i=0; i<mddev->raid_disks; i++) {
164 mdk_rdev_t *rdev = conf->multipaths[i].rdev;
165 if (rdev && !rdev->faulty) {
166 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
168 if (r_queue->unplug_fn)
169 r_queue->unplug_fn(r_queue);
173 static void multipath_unplug(request_queue_t *q)
175 unplug_slaves(q->queuedata);
179 static int multipath_make_request (request_queue_t *q, struct bio * bio)
181 mddev_t *mddev = q->queuedata;
182 multipath_conf_t *conf = mddev_to_conf(mddev);
183 struct multipath_bh * mp_bh;
184 struct multipath_info *multipath;
186 mp_bh = mempool_alloc(conf->pool, GFP_NOIO);
188 mp_bh->master_bio = bio;
189 mp_bh->mddev = mddev;
191 if (bio_data_dir(bio)==WRITE) {
192 disk_stat_inc(mddev->gendisk, writes);
193 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
195 disk_stat_inc(mddev->gendisk, reads);
196 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
199 * read balancing logic:
201 spin_lock_irq(&conf->device_lock);
202 mp_bh->path = multipath_read_balance(conf);
203 multipath = conf->multipaths + mp_bh->path;
204 atomic_inc(&multipath->rdev->nr_pending);
205 spin_unlock_irq(&conf->device_lock);
208 mp_bh->bio.bi_bdev = multipath->rdev->bdev;
209 mp_bh->bio.bi_rw |= (1 << BIO_RW_FAILFAST);
210 mp_bh->bio.bi_end_io = multipath_end_request;
211 mp_bh->bio.bi_private = mp_bh;
212 generic_make_request(&mp_bh->bio);
216 static void multipath_status (struct seq_file *seq, mddev_t *mddev)
218 multipath_conf_t *conf = mddev_to_conf(mddev);
221 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
222 conf->working_disks);
223 for (i = 0; i < conf->raid_disks; i++)
224 seq_printf (seq, "%s",
225 conf->multipaths[i].rdev &&
226 conf->multipaths[i].rdev->in_sync ? "U" : "_");
227 seq_printf (seq, "]");
232 * Careful, this can execute in IRQ contexts as well!
234 static void multipath_error (mddev_t *mddev, mdk_rdev_t *rdev)
236 multipath_conf_t *conf = mddev_to_conf(mddev);
238 if (conf->working_disks <= 1) {
240 * Uh oh, we can do nothing if this is our last path, but
241 * first check if this is a queued request for a device
242 * which has just failed.
245 "multipath: only one IO path left and IO error.\n");
246 /* leave it active... it's all we have */
249 * Mark disk as unusable
252 char b[BDEVNAME_SIZE];
256 conf->working_disks--;
257 printk(KERN_ALERT "multipath: IO failure on %s,"
258 " disabling IO path. \n Operation continuing"
259 " on %d IO paths.\n",
260 bdevname (rdev->bdev,b),
261 conf->working_disks);
266 static void print_multipath_conf (multipath_conf_t *conf)
269 struct multipath_info *tmp;
271 printk("MULTIPATH conf printout:\n");
273 printk("(conf==NULL)\n");
276 printk(" --- wd:%d rd:%d\n", conf->working_disks,
279 for (i = 0; i < conf->raid_disks; i++) {
280 char b[BDEVNAME_SIZE];
281 tmp = conf->multipaths + i;
283 printk(" disk%d, o:%d, dev:%s\n",
284 i,!tmp->rdev->faulty,
285 bdevname(tmp->rdev->bdev,b));
290 static int multipath_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
292 multipath_conf_t *conf = mddev->private;
295 struct multipath_info *p;
297 print_multipath_conf(conf);
298 spin_lock_irq(&conf->device_lock);
299 for (path=0; path<mddev->raid_disks; path++)
300 if ((p=conf->multipaths+path)->rdev == NULL) {
302 blk_queue_stack_limits(mddev->queue,
303 rdev->bdev->bd_disk->queue);
305 /* as we don't honour merge_bvec_fn, we must never risk
306 * violating it, so limit ->max_sector to one PAGE, as
307 * a one page request is never in violation.
308 * (Note: it is very unlikely that a device with
309 * merge_bvec_fn will be involved in multipath.)
311 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
312 mddev->queue->max_sectors > (PAGE_SIZE>>9))
313 mddev->queue->max_sectors = (PAGE_SIZE>>9);
315 conf->working_disks++;
316 rdev->raid_disk = path;
320 spin_unlock_irq(&conf->device_lock);
322 print_multipath_conf(conf);
326 static int multipath_remove_disk(mddev_t *mddev, int number)
328 multipath_conf_t *conf = mddev->private;
330 struct multipath_info *p = conf->multipaths + number;
332 print_multipath_conf(conf);
333 spin_lock_irq(&conf->device_lock);
336 if (p->rdev->in_sync ||
337 atomic_read(&p->rdev->nr_pending)) {
338 printk(KERN_ERR "hot-remove-disk, slot %d is identified" " but is still operational!\n", number);
348 spin_unlock_irq(&conf->device_lock);
350 print_multipath_conf(conf);
357 * This is a kernel thread which:
359 * 1. Retries failed read operations on working multipaths.
360 * 2. Updates the raid superblock when problems encounter.
361 * 3. Performs writes following reads for array syncronising.
364 static void multipathd (mddev_t *mddev)
366 struct multipath_bh *mp_bh;
371 md_check_recovery(mddev);
373 char b[BDEVNAME_SIZE];
374 spin_lock_irqsave(&retry_list_lock, flags);
375 mp_bh = multipath_retry_list;
378 multipath_retry_list = mp_bh->next_mp;
379 spin_unlock_irqrestore(&retry_list_lock, flags);
381 mddev = mp_bh->mddev;
383 bio->bi_sector = mp_bh->master_bio->bi_sector;
386 if (multipath_map (mddev, &rdev)<0) {
387 printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
388 " error for block %llu\n",
389 bdevname(bio->bi_bdev,b),
390 (unsigned long long)bio->bi_sector);
391 multipath_end_bh_io(mp_bh, 0);
393 printk(KERN_ERR "multipath: %s: redirecting sector %llu"
394 " to another IO path\n",
395 bdevname(bio->bi_bdev,b),
396 (unsigned long long)bio->bi_sector);
397 bio->bi_bdev = rdev->bdev;
398 generic_make_request(bio);
401 spin_unlock_irqrestore(&retry_list_lock, flags);
404 static int multipath_run (mddev_t *mddev)
406 multipath_conf_t *conf;
408 struct multipath_info *disk;
410 struct list_head *tmp;
412 if (mddev->level != LEVEL_MULTIPATH) {
413 printk("multipath: %s: raid level not set to multipath IO (%d)\n",
414 mdname(mddev), mddev->level);
418 * copy the already verified devices into our private MULTIPATH
419 * bookkeeping area. [whatever we allocate in multipath_run(),
420 * should be freed in multipath_stop()]
423 conf = kmalloc(sizeof(multipath_conf_t), GFP_KERNEL);
424 mddev->private = conf;
427 "multipath: couldn't allocate memory for %s\n",
431 memset(conf, 0, sizeof(*conf));
433 conf->multipaths = kmalloc(sizeof(struct multipath_info)*mddev->raid_disks,
435 if (!conf->multipaths) {
437 "multipath: couldn't allocate memory for %s\n",
441 memset(conf->multipaths, 0, sizeof(struct multipath_info)*mddev->raid_disks);
443 mddev->queue->unplug_fn = multipath_unplug;
445 conf->working_disks = 0;
446 ITERATE_RDEV(mddev,rdev,tmp) {
447 disk_idx = rdev->raid_disk;
449 disk_idx >= mddev->raid_disks)
452 disk = conf->multipaths + disk_idx;
455 blk_queue_stack_limits(mddev->queue,
456 rdev->bdev->bd_disk->queue);
457 /* as we don't honour merge_bvec_fn, we must never risk
458 * violating it, not that we ever expect a device with
459 * a merge_bvec_fn to be involved in multipath */
460 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
461 mddev->queue->max_sectors > (PAGE_SIZE>>9))
462 mddev->queue->max_sectors = (PAGE_SIZE>>9);
465 conf->working_disks++;
468 conf->raid_disks = mddev->raid_disks;
471 conf->device_lock = SPIN_LOCK_UNLOCKED;
473 if (!conf->working_disks) {
474 printk(KERN_ERR "multipath: no operational IO paths for %s\n",
478 mddev->degraded = conf->raid_disks = conf->working_disks;
480 conf->pool = mempool_create(NR_RESERVED_BUFS,
481 mp_pool_alloc, mp_pool_free,
483 if (conf->pool == NULL) {
485 "multipath: couldn't allocate memory for %s\n",
491 mddev->thread = md_register_thread(multipathd, mddev, "%s_multipath");
492 if (!mddev->thread) {
493 printk(KERN_ERR "multipath: couldn't allocate thread"
494 " for %s\n", mdname(mddev));
500 "multipath: array %s active with %d out of %d IO paths\n",
501 mdname(mddev), conf->working_disks, mddev->raid_disks);
503 * Ok, everything is just fine now
505 mddev->array_size = mddev->size;
510 mempool_destroy(conf->pool);
511 if (conf->multipaths)
512 kfree(conf->multipaths);
514 mddev->private = NULL;
520 static int multipath_stop (mddev_t *mddev)
522 multipath_conf_t *conf = mddev_to_conf(mddev);
524 md_unregister_thread(mddev->thread);
525 mempool_destroy(conf->pool);
526 kfree(conf->multipaths);
528 mddev->private = NULL;
532 static mdk_personality_t multipath_personality=
535 .owner = THIS_MODULE,
536 .make_request = multipath_make_request,
537 .run = multipath_run,
538 .stop = multipath_stop,
539 .status = multipath_status,
540 .error_handler = multipath_error,
541 .hot_add_disk = multipath_add_disk,
542 .hot_remove_disk= multipath_remove_disk,
545 static int __init multipath_init (void)
547 return register_md_personality (MULTIPATH, &multipath_personality);
550 static void __exit multipath_exit (void)
552 unregister_md_personality (MULTIPATH);
555 module_init(multipath_init);
556 module_exit(multipath_exit);
557 MODULE_LICENSE("GPL");
558 MODULE_ALIAS("md-personality-7"); /* MULTIPATH */