Round-robin path selector. --- diff/drivers/md/Makefile 2004-10-19 16:55:12.000000000 +0100 +++ source/drivers/md/Makefile 2004-10-19 16:57:53.000000000 +0100 @@ -28,7 +28,7 @@ obj-$(CONFIG_BLK_DEV_MD) += md.o obj-$(CONFIG_BLK_DEV_DM) += dm-mod.o obj-$(CONFIG_DM_CRYPT) += dm-crypt.o -obj-$(CONFIG_DM_MULTIPATH) += dm-multipath.o +obj-$(CONFIG_DM_MULTIPATH) += dm-multipath.o dm-round-robin.o obj-$(CONFIG_DM_SNAPSHOT) += dm-snapshot.o obj-$(CONFIG_DM_MIRROR) += dm-mirror.o obj-$(CONFIG_DM_ZERO) += dm-zero.o --- diff/drivers/md/dm-round-robin.c 1970-01-01 01:00:00.000000000 +0100 +++ source/drivers/md/dm-round-robin.c 2004-10-19 16:57:53.000000000 +0100 @@ -0,0 +1,239 @@ +/* + * Copyright (C) 2003 Sistina Software. + * Copyright (C) 2004 Red Hat, Inc. All rights reserved. + * + * Module Author: Heinz Mauelshagen + * + * This file is released under the GPL. + * + * Round-robin path selector. + */ + +#include "dm.h" +#include "dm-path-selector.h" + +#include + +/*----------------------------------------------------------------- + * Path-handling code, paths are held in lists + *---------------------------------------------------------------*/ +struct path_info { + struct list_head list; + struct path *path; + unsigned repeat_count; +}; + +static struct path_info *path_lookup(struct list_head *head, struct path *p) +{ + struct path_info *pi; + + list_for_each_entry (pi, head, list) + if (pi->path == p) + return pi; + + return NULL; +} + +static void free_paths(struct list_head *paths) +{ + struct path_info *pi, *next; + + list_for_each_entry_safe (pi, next, paths, list) { + list_del(&pi->list); + kfree(pi); + } +} + +/*----------------------------------------------------------------- + * Round-robin selector + *---------------------------------------------------------------*/ + +#define RR_MIN_IO 1000 + +struct selector { + spinlock_t lock; + + struct list_head valid_paths; + struct list_head invalid_paths; +}; + +static struct selector *alloc_selector(void) +{ + struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL); + + if (s) { + s->lock = SPIN_LOCK_UNLOCKED; + + INIT_LIST_HEAD(&s->valid_paths); + INIT_LIST_HEAD(&s->invalid_paths); + } + + return s; +} + +static int rr_ctr(struct path_selector *ps) +{ + struct selector *s; + + s = alloc_selector(); + if (!s) + return -ENOMEM; + + ps->context = s; + return 0; +} + +static void rr_dtr(struct path_selector *ps) +{ + struct selector *s = (struct selector *) ps->context; + + free_paths(&s->valid_paths); + free_paths(&s->invalid_paths); + kfree(s); + ps->context = NULL; +} + +static int rr_add_path(struct path_selector *ps, struct path *path, + struct block_device *bdev, + int argc, char **argv, char **error) +{ + struct selector *s = (struct selector *) ps->context; + struct path_info *pi; + unsigned repeat_count = RR_MIN_IO; + + if (argc > 1) { + *error = "round-robin ps: incorrect number of arguments"; + return -EINVAL; + } + + /* First path argument is number of I/Os before switching path */ + if ((argc == 1) && (sscanf(argv[0], "%u", &repeat_count) != 1)) { + *error = "round-robin ps: invalid repeat count"; + return -EINVAL; + } + + /* allocate the path */ + pi = kmalloc(sizeof(*pi), GFP_KERNEL); + if (!pi) { + *error = "round-robin ps: Error allocating path context"; + return -ENOMEM; + } + + pi->path = path; + pi->repeat_count = repeat_count; + + spin_lock(&s->lock); + list_add(&pi->list, &s->valid_paths); + spin_unlock(&s->lock); + + return 0; +} + +static void rr_fail_path(struct path_selector *ps, struct path *p) +{ + unsigned long flags; + struct selector *s = (struct selector *) ps->context; + struct path_info *pi; + + /* + * This function will be called infrequently so we don't + * mind the expense of these searches. + */ + spin_lock_irqsave(&s->lock, flags); + pi = path_lookup(&s->valid_paths, p); + if (!pi) + pi = path_lookup(&s->invalid_paths, p); + + if (pi) + list_move(&pi->list, &s->invalid_paths); + else + DMWARN("asked to change the state of an unknown path"); + + spin_unlock_irqrestore(&s->lock, flags); +} + +static int rr_reinstate_path(struct path_selector *ps, struct path *p) +{ + int r = 0; + unsigned long flags; + struct selector *s = (struct selector *) ps->context; + struct path_info *pi; + + /* + * This function will be called infrequently so we don't + * mind the expense of these searches. + */ + spin_lock_irqsave(&s->lock, flags); + pi = path_lookup(&s->invalid_paths, p); + if (!pi) + pi = path_lookup(&s->valid_paths, p); + + if (pi) + list_move(&pi->list, &s->valid_paths); + else { + DMWARN("asked to reinstate an unknown path"); + r = -EINVAL; + } + + spin_unlock_irqrestore(&s->lock, flags); + + return r; +} + +static struct path *rr_select_path(struct path_selector *ps, + unsigned *repeat_count) +{ + unsigned long flags; + struct selector *s = (struct selector *) ps->context; + struct path_info *pi = NULL; + + spin_lock_irqsave(&s->lock, flags); + if (!list_empty(&s->valid_paths)) { + pi = list_entry(s->valid_paths.next, struct path_info, list); + list_move_tail(&pi->list, &s->valid_paths); + *repeat_count = pi->repeat_count; + } + spin_unlock_irqrestore(&s->lock, flags); + + return pi ? pi->path : NULL; +} + +static struct path_selector_type rr_ps = { + .name = "round-robin", + .module = THIS_MODULE, + .table_args = 0, + .info_args = 0, + .ctr = rr_ctr, + .dtr = rr_dtr, + .add_path = rr_add_path, + .fail_path = rr_fail_path, + .reinstate_path = rr_reinstate_path, + .select_path = rr_select_path, +}; + +static int __init dm_rr_init(void) +{ + int r = dm_register_path_selector(&rr_ps); + + if (r < 0) + DMERR("round-robin: register failed %d", r); + + DMINFO("dm-round-robin version 0.2.1 loaded"); + + return r; +} + +static void __exit dm_rr_exit(void) +{ + int r = dm_unregister_path_selector(&rr_ps); + + if (r < 0) + DMERR("round-robin: unregister failed %d", r); +} + +module_init(dm_rr_init); +module_exit(dm_rr_exit); + +MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector"); +MODULE_AUTHOR("Sistina Software "); +MODULE_LICENSE("GPL");