[sheepdog] [PATCH v2 5/9] sbd: add list interface to control file

Liu Yuan namei.unix at gmail.com
Sun May 25 09:53:17 CEST 2014


From: Liu Yuan <tailai.ly at taobao.com>

This is meant to output the mapping between sbdX and sheep vdi.

The output looks like below.

root at vm1:/home/test/sheepdog/sbd# cat /sys/bus/sbd/list
0 test
1 disk1
2 disk2
3 disk3

Also add a mutex to make sbd add/remove/list thread safe.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 sbd/sbd.h                |  6 ++++++
 sbd/sheep.c              |  6 ------
 sbd/sheep_block_device.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/sbd/sbd.h b/sbd/sbd.h
index dbabc7a..ccf9b89 100644
--- a/sbd/sbd.h
+++ b/sbd/sbd.h
@@ -23,6 +23,12 @@
 #define SBD_MINORS_PER_MAJOR 32
 #define SECTOR_SIZE 512
 
+/* FIXME I need this hack to compile DEFINE_MUTEX successfully */
+#ifdef __SPIN_LOCK_UNLOCKED
+# undef __SPIN_LOCK_UNLOCKED
+# define __SPIN_LOCK_UNLOCKED(lockname) __SPIN_LOCK_INITIALIZER(lockname)
+#endif
+
 struct sheep_vdi {
 	struct sd_inode *inode;
 	u32 vid;
diff --git a/sbd/sheep.c b/sbd/sheep.c
index daf281f..d438824 100644
--- a/sbd/sheep.c
+++ b/sbd/sheep.c
@@ -11,12 +11,6 @@
 
 #include "sbd.h"
 
-/* FIXME I need this hack to compile DEFINE_MUTEX successfully */
-#ifdef __SPIN_LOCK_UNLOCKED
-# undef __SPIN_LOCK_UNLOCKED
-# define __SPIN_LOCK_UNLOCKED(lockname) __SPIN_LOCK_INITIALIZER(lockname)
-#endif
-
 static DEFINE_MUTEX(socket_mutex);
 
 void socket_shutdown(struct socket *sock)
diff --git a/sbd/sheep_block_device.c b/sbd/sheep_block_device.c
index b953e36..7496dab 100644
--- a/sbd/sheep_block_device.c
+++ b/sbd/sheep_block_device.c
@@ -15,11 +15,40 @@
  * This file implements the glue functions to export sheep vdi as Linux block
  * device.
  *
+ * Instructions for use
+ * --------------------
+ *
+ * 1) Map a Linux block device to an existing sheep vdi.
+ *
+ *    In this example, we will try to connect to vdi test with sheep daemon IP
+ *    127.0.0.1 and port 7000.
+ *
+ *    $ echo "127.0.0.1 7000 test" > /sys/bus/sbd/add
+ *
+ * 2) List all active sbd<->vdi mappings.
+ *
+ *    In this example, we have already attached sheep disk1, disk2, disk3
+ *
+ *    $ cat /sys/bus/sbd/list
+ *    0 disk1
+ *    1 disk2
+ *    2 disk3
+ *
+ *    The columns, in order, are:
+ *    - blkdev unique id
+ *    - sheep vdi name
+ *
+ * 3) Remove an active sbd<->vdi mapping.
+ *
+ *    In this example, we remove the mapping with blkdev unique id 1.
+ *
+ *    $ echo 1 > /sys/bus/sbd/remove
  */
 
 #include "sbd.h"
 
 static LIST_HEAD(sbd_dev_list);
+static DEFINE_MUTEX(dev_list_mutex);
 
 static const struct block_device_operations sbd_bd_ops = {
 	.owner		= THIS_MODULE,
@@ -189,10 +218,12 @@ static ssize_t sbd_add(struct bus_type *bus, const char *buf,
 	rwlock_init(&dev->inflight_lock);
 	rwlock_init(&dev->blocking_lock);
 
+	mutex_lock(&dev_list_mutex);
 	list_for_each_entry(tmp, &sbd_dev_list, list) {
-		if (tmp->id > new_id)
+		if (tmp->id >= new_id)
 			new_id = tmp->id + 1;
 	}
+	mutex_unlock(&dev_list_mutex);
 
 	ret = sheep_setup_vdi(dev);
 	if (ret < 0)
@@ -216,7 +247,9 @@ static ssize_t sbd_add(struct bus_type *bus, const char *buf,
 	if (ret < 0)
 		goto err_unreg_blkdev;
 
+	mutex_lock(&dev_list_mutex);
 	list_add_tail(&dev->list, &sbd_dev_list);
+	mutex_unlock(&dev_list_mutex);
 
 	return count;
 err_unreg_blkdev:
@@ -285,9 +318,25 @@ static ssize_t sbd_remove(struct bus_type *bus, const char *buf,
 	return count;
 }
 
+static ssize_t sbd_list(struct bus_type *bus, char *buf)
+{
+	ssize_t ret = 0;
+	struct sbd_device *dev;
+
+	mutex_lock(&dev_list_mutex);
+	list_for_each_entry(dev, &sbd_dev_list, list) {
+		ret += sprintf(buf + ret, "%d %s\n", dev->id,
+			       dev->vdi.inode->name);
+	}
+	mutex_unlock(&dev_list_mutex);
+
+	return ret;
+}
+
 static struct bus_attribute sbd_bus_attrs[] = {
 	__ATTR(add, S_IWUSR, NULL, sbd_add),
 	__ATTR(remove, S_IWUSR, NULL, sbd_remove),
+	__ATTR(list, S_IRUSR, sbd_list, NULL),
 	__ATTR_NULL
 };
 
-- 
1.8.1.2




More information about the sheepdog mailing list