2023-01-25 20:00:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003 Sistina Software.
|
|
|
|
* Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Module Author: Heinz Mauelshagen
|
|
|
|
*
|
|
|
|
* This file is released under the GPL.
|
|
|
|
*
|
|
|
|
* Round-robin path selector.
|
|
|
|
*/
|
|
|
|
|
2008-10-21 16:44:59 +00:00
|
|
|
#include <linux/device-mapper.h>
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "dm-path-selector.h"
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
2011-07-03 17:58:33 +00:00
|
|
|
#include <linux/module.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-06-26 07:27:35 +00:00
|
|
|
#define DM_MSG_PREFIX "multipath round-robin"
|
2017-02-17 04:57:17 +00:00
|
|
|
#define RR_MIN_IO 1
|
|
|
|
#define RR_VERSION "1.2.0"
|
2006-06-26 07:27:35 +00:00
|
|
|
|
2023-01-26 14:48:30 +00:00
|
|
|
/*
|
|
|
|
*---------------------------------------------------------------
|
2005-04-16 22:20:36 +00:00
|
|
|
* Path-handling code, paths are held in lists
|
2023-01-26 14:48:30 +00:00
|
|
|
*---------------------------------------------------------------
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
struct path_info {
|
|
|
|
struct list_head list;
|
2006-12-08 10:36:33 +00:00
|
|
|
struct dm_path *path;
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int repeat_count;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:48:30 +00:00
|
|
|
/*
|
|
|
|
*---------------------------------------------------------------
|
2005-04-16 22:20:36 +00:00
|
|
|
* Round-robin selector
|
2023-01-26 14:48:30 +00:00
|
|
|
*---------------------------------------------------------------
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
struct selector {
|
|
|
|
struct list_head valid_paths;
|
|
|
|
struct list_head invalid_paths;
|
2016-02-15 19:25:00 +00:00
|
|
|
spinlock_t lock;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct selector *alloc_selector(void)
|
|
|
|
{
|
|
|
|
struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
|
|
|
|
|
2017-02-17 04:57:17 +00:00
|
|
|
if (s) {
|
|
|
|
INIT_LIST_HEAD(&s->valid_paths);
|
|
|
|
INIT_LIST_HEAD(&s->invalid_paths);
|
|
|
|
spin_lock_init(&s->lock);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-01-25 20:14:58 +00:00
|
|
|
static int rr_create(struct path_selector *ps, unsigned int argc, char **argv)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct selector *s;
|
|
|
|
|
|
|
|
s = alloc_selector();
|
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ps->context = s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rr_destroy(struct path_selector *ps)
|
|
|
|
{
|
2016-02-15 19:25:00 +00:00
|
|
|
struct selector *s = ps->context;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
free_paths(&s->valid_paths);
|
|
|
|
free_paths(&s->invalid_paths);
|
|
|
|
kfree(s);
|
|
|
|
ps->context = NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-08 10:36:33 +00:00
|
|
|
static int rr_status(struct path_selector *ps, struct dm_path *path,
|
2005-04-16 22:20:36 +00:00
|
|
|
status_type_t type, char *result, unsigned int maxlen)
|
|
|
|
{
|
|
|
|
struct path_info *pi;
|
|
|
|
int sz = 0;
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
DMEMIT("0 ");
|
|
|
|
else {
|
2023-01-25 21:57:42 +00:00
|
|
|
switch (type) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case STATUSTYPE_INFO:
|
|
|
|
break;
|
|
|
|
case STATUSTYPE_TABLE:
|
|
|
|
pi = path->pscontext;
|
|
|
|
DMEMIT("%u ", pi->repeat_count);
|
|
|
|
break;
|
2021-07-13 00:49:03 +00:00
|
|
|
|
|
|
|
case STATUSTYPE_IMA:
|
|
|
|
*result = '\0';
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called during initialisation to register each path with an
|
|
|
|
* optional repeat_count.
|
|
|
|
*/
|
2006-12-08 10:36:33 +00:00
|
|
|
static int rr_add_path(struct path_selector *ps, struct dm_path *path,
|
2005-04-16 22:20:36 +00:00
|
|
|
int argc, char **argv, char **error)
|
|
|
|
{
|
2016-02-15 19:25:00 +00:00
|
|
|
struct selector *s = ps->context;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct path_info *pi;
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int repeat_count = RR_MIN_IO;
|
dm: reject trailing characters in sccanf input
Device mapper uses sscanf to convert arguments to numbers. The problem is that
the way we use it ignores additional unmatched characters in the scanned string.
For example, this `if (sscanf(string, "%d", &number) == 1)' will match a number,
but also it will match number with some garbage appended, like "123abc".
As a result, device mapper accepts garbage after some numbers. For example
the command `dmsetup create vg1-new --table "0 16384 linear 254:1bla 34816bla"'
will pass without an error.
This patch fixes all sscanf uses in device mapper. It appends "%c" with
a pointer to a dummy character variable to every sscanf statement.
The construct `if (sscanf(string, "%d%c", &number, &dummy) == 1)' succeeds
only if string is a null-terminated number (optionally preceded by some
whitespace characters). If there is some character appended after the number,
sscanf matches "%c", writes the character to the dummy variable and returns 2.
We check the return value for 1 and consequently reject numbers with some
garbage appended.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
2012-03-28 17:41:26 +00:00
|
|
|
char dummy;
|
2016-02-15 19:25:00 +00:00
|
|
|
unsigned long flags;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
*error = "round-robin ps: incorrect number of arguments";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First path argument is number of I/Os before switching path */
|
dm: reject trailing characters in sccanf input
Device mapper uses sscanf to convert arguments to numbers. The problem is that
the way we use it ignores additional unmatched characters in the scanned string.
For example, this `if (sscanf(string, "%d", &number) == 1)' will match a number,
but also it will match number with some garbage appended, like "123abc".
As a result, device mapper accepts garbage after some numbers. For example
the command `dmsetup create vg1-new --table "0 16384 linear 254:1bla 34816bla"'
will pass without an error.
This patch fixes all sscanf uses in device mapper. It appends "%c" with
a pointer to a dummy character variable to every sscanf statement.
The construct `if (sscanf(string, "%d%c", &number, &dummy) == 1)' succeeds
only if string is a null-terminated number (optionally preceded by some
whitespace characters). If there is some character appended after the number,
sscanf matches "%c", writes the character to the dummy variable and returns 2.
We check the return value for 1 and consequently reject numbers with some
garbage appended.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
2012-03-28 17:41:26 +00:00
|
|
|
if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
*error = "round-robin ps: invalid repeat count";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:57:17 +00:00
|
|
|
if (repeat_count > 1) {
|
|
|
|
DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
|
|
|
|
repeat_count = 1;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
path->pscontext = pi;
|
|
|
|
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_lock_irqsave(&s->lock, flags);
|
2006-11-09 01:44:43 +00:00
|
|
|
list_add_tail(&pi->list, &s->valid_paths);
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_unlock_irqrestore(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-08 10:36:33 +00:00
|
|
|
static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2016-02-15 19:25:00 +00:00
|
|
|
unsigned long flags;
|
|
|
|
struct selector *s = ps->context;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct path_info *pi = p->pscontext;
|
|
|
|
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_lock_irqsave(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
list_move(&pi->list, &s->invalid_paths);
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_unlock_irqrestore(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-12-08 10:36:33 +00:00
|
|
|
static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2016-02-15 19:25:00 +00:00
|
|
|
unsigned long flags;
|
|
|
|
struct selector *s = ps->context;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct path_info *pi = p->pscontext;
|
|
|
|
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_lock_irqsave(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
list_move(&pi->list, &s->valid_paths);
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_unlock_irqrestore(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-18 02:29:17 +00:00
|
|
|
static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2016-02-15 19:25:00 +00:00
|
|
|
unsigned long flags;
|
|
|
|
struct selector *s = ps->context;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct path_info *pi = NULL;
|
|
|
|
|
2017-02-17 04:57:17 +00:00
|
|
|
spin_lock_irqsave(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-02-15 19:25:00 +00:00
|
|
|
spin_unlock_irqrestore(&s->lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-02-17 04:57:17 +00:00
|
|
|
return pi ? pi->path : NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct path_selector_type rr_ps = {
|
|
|
|
.name = "round-robin",
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
.table_args = 1,
|
|
|
|
.info_args = 0,
|
|
|
|
.create = rr_create,
|
|
|
|
.destroy = rr_destroy,
|
|
|
|
.status = rr_status,
|
|
|
|
.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)
|
2006-06-26 07:27:35 +00:00
|
|
|
DMERR("register failed %d", r);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2016-02-10 16:58:45 +00:00
|
|
|
DMINFO("version " RR_VERSION " loaded");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit dm_rr_exit(void)
|
|
|
|
{
|
|
|
|
int r = dm_unregister_path_selector(&rr_ps);
|
|
|
|
|
|
|
|
if (r < 0)
|
2007-07-12 16:27:01 +00:00
|
|
|
DMERR("unregister failed %d", r);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(dm_rr_init);
|
|
|
|
module_exit(dm_rr_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
|
2024-02-07 20:51:24 +00:00
|
|
|
MODULE_AUTHOR("Sistina Software <dm-devel@lists.linux.dev>");
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_LICENSE("GPL");
|