From: "Balasubramanian, Vijayakumar (STSD)" <[email-masked]>

This patch provides a "least pending IO" dynamic load balancing policy for
bio-based device-mapper multipath.

This load balancing policy considers the number of unserviced requests pending
on a path and selects the path with least count for pending service request.

We find this policy more useful especially when the SAN environment has
heterogeneous components. E.g. when there is one 8GB HBA and one 2GB HBA
connected to the same server, 8GB HBA could be utilized better with this
algorithm.

Signed-off-by: Sakshi Chaitanya Veni <[email-masked]>
Signed-off-by: Vijayakumar Balasubramanian <[email-masked]>
Signed-off-by: Senthil Kumar V <[email-masked]>
---
 drivers/md/Makefile           |    2 
 drivers/md/dm-least-pending.c |  229 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 230 insertions(+), 1 deletion(-)

Index: linux-2.6.28-rc9/drivers/md/Makefile
===================================================================
--- linux-2.6.28-rc9.orig/drivers/md/Makefile	2008-12-22 18:28:17.000000000 +0000
+++ linux-2.6.28-rc9/drivers/md/Makefile	2008-12-22 18:29:12.000000000 +0000
@@ -33,7 +33,7 @@ obj-$(CONFIG_BLK_DEV_MD)	+= md-mod.o
 obj-$(CONFIG_BLK_DEV_DM)	+= dm-mod.o
 obj-$(CONFIG_DM_CRYPT)		+= dm-crypt.o
 obj-$(CONFIG_DM_DELAY)		+= dm-delay.o
-obj-$(CONFIG_DM_MULTIPATH)	+= dm-multipath.o dm-round-robin.o
+obj-$(CONFIG_DM_MULTIPATH)	+= dm-multipath.o dm-round-robin.o dm-least-pending.o
 obj-$(CONFIG_DM_SNAPSHOT)	+= dm-snapshot.o
 obj-$(CONFIG_DM_MIRROR)		+= dm-mirror.o dm-log.o dm-region-hash.o
 obj-$(CONFIG_DM_ZERO)		+= dm-zero.o
Index: linux-2.6.28-rc9/drivers/md/dm-least-pending.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.28-rc9/drivers/md/dm-least-pending.c	2008-12-22 18:29:12.000000000 +0000
@@ -0,0 +1,229 @@
+/*
+ * (C) Copyright 2008 Hewlett-Packard Development Company, L.P
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-path-selector.h"
+
+#include <linux/slab.h>
+
+#define DM_MSG_PREFIX "multipath least-pending"
+
+/*-----------------------------------------------------------------
+ * Path-handling code, paths are held in lists
+ *---------------------------------------------------------------*/
+struct path_info {
+	struct list_head list;
+	struct dm_path *path;
+	unsigned io_count;
+};
+
+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);
+	}
+}
+
+/*-----------------------------------------------------------------
+ * Least-pending selector
+ *---------------------------------------------------------------*/
+
+struct selector {
+	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) {
+		INIT_LIST_HEAD(&s->valid_paths);
+		INIT_LIST_HEAD(&s->invalid_paths);
+	}
+
+	return s;
+}
+
+static int lpp_create(struct path_selector *ps, unsigned argc, char **argv)
+{
+	struct selector *s;
+
+	s = alloc_selector();
+	if (!s)
+		return -ENOMEM;
+
+	ps->context = s;
+	return 0;
+}
+
+static void lpp_destroy(struct path_selector *ps)
+{
+	struct selector *s = ps->context;
+
+	free_paths(&s->valid_paths);
+	free_paths(&s->invalid_paths);
+	kfree(s);
+	ps->context = NULL;
+}
+
+static int lpp_status(struct path_selector *ps, struct dm_path *path,
+		     status_type_t type, char *result, unsigned int maxlen)
+{
+	struct path_info *pi;
+	int sz = 0;
+
+	if (!path)
+		DMEMIT("0 ");
+	else {
+		pi = path->pscontext;
+		switch (type) {
+		case STATUSTYPE_INFO:
+			DMEMIT("%u ", pi->io_count);
+			break;
+		case STATUSTYPE_TABLE:
+			break;
+		}
+	}
+
+	return sz;
+}
+
+/*
+ * Called during initialisation to register each path.
+ */
+static int lpp_add_path(struct path_selector *ps, struct dm_path *path,
+		       int argc, char **argv, char **error)
+{
+	struct selector *s = ps->context;
+	struct path_info *pi;
+
+	if (argc) {
+		*error = "least-pending ps: no arguments expected";
+		return -EINVAL;
+	}
+
+	/* allocate the path */
+	pi = kmalloc(sizeof(*pi), GFP_KERNEL);
+	if (!pi) {
+		*error = "least-pending ps: Error allocating path context";
+		return -ENOMEM;
+	}
+
+	pi->path = path;
+	pi->io_count = 0;
+
+	path->pscontext = pi;
+
+	list_add(&pi->list, &s->valid_paths);
+
+	return 0;
+}
+
+static void lpp_fail_path(struct path_selector *ps, struct dm_path *p)
+{
+	struct selector *s = ps->context;
+	struct path_info *pi = p->pscontext;
+
+	pi->io_count = 0;
+
+	list_move(&pi->list, &s->invalid_paths);
+}
+
+static int lpp_reinstate_path(struct path_selector *ps, struct dm_path *p)
+{
+	struct selector *s = ps->context;
+	struct path_info *pi = p->pscontext;
+
+	list_move(&pi->list, &s->valid_paths);
+
+	return 0;
+}
+
+static struct dm_path *lpp_select_path(struct path_selector *ps,
+				       unsigned *repeat_count)
+{
+	struct selector *s = ps->context;
+	struct path_info *pi, *next, *least_io_path = NULL;
+	struct list_head *paths;
+
+	if (list_empty(&s->valid_paths))
+		return NULL;
+
+	paths = &s->valid_paths;
+
+	list_for_each_entry_safe(pi, next, paths, list) {
+		if (!least_io_path || least_io_path->io_count < pi->io_count)
+			least_io_path = pi;
+		if (!least_io_path->io_count)
+			break;
+	}
+
+	if (!least_io_path)
+		return NULL;
+
+	least_io_path->io_count++;
+	*repeat_count = 1;
+
+	return least_io_path->path;
+}
+
+static int lpp_end_io(struct path_selector *ps, struct dm_path *path)
+{
+	struct path_info *pi = NULL;
+
+	pi = path->pscontext;
+	if (!pi)
+		return 1;
+
+	pi->io_count--;
+
+	return 0;
+}
+
+static struct path_selector_type lpp_ps = {
+	.name = "least-pending",
+	.module = THIS_MODULE,
+	.table_args = 0,
+	.info_args = 1,
+	.create = lpp_create,
+	.destroy = lpp_destroy,
+	.status = lpp_status,
+	.add_path = lpp_add_path,
+	.fail_path = lpp_fail_path,
+	.reinstate_path = lpp_reinstate_path,
+	.select_path = lpp_select_path,
+	.end_io	= lpp_end_io,
+};
+
+static int __init dm_lpp_init(void)
+{
+	int r = dm_register_path_selector(&lpp_ps);
+
+	if (r < 0)
+		DMERR("register failed %d", r);
+
+	DMINFO("version 1.0.0 loaded");
+
+	return r;
+}
+
+static void __exit dm_lpp_exit(void)
+{
+	int r = dm_unregister_path_selector(&lpp_ps);
+
+	if (r < 0)
+		DMERR("unregister failed %d", r);
+}
+
+module_init(dm_lpp_init);
+module_exit(dm_lpp_exit);
+
+MODULE_DESCRIPTION(DM_NAME " least-pending multipath path selector");
+MODULE_AUTHOR("Sakshi Chaitanya Veni <[email-masked]>");
+MODULE_LICENSE("GPL");