[sheepdog] [PATCH v4 RESEND 2/8] sheepdev: use proc entry to control adding/removing devices

levin li levin108 at gmail.com
Wed Jan 23 09:16:11 CET 2013


From: levin li <xingke.lwp at taobao.com>

Add a proc entry /proc/sheep for sheep device controlling, by writing to
this entry to add or remove device

For example:

Add a sheep VDI as a block device:
echo "add 127.0.0.1 linux:tag" > /proc/sheep

Remove a sheep device:
echo "del linux:tag" > /proc/sheep

Signed-off-by: levin li <xingke.lwp at taobao.com>
---
 sheepdev/Kbuild     |  11 ++++
 sheepdev/device.c   |  52 +++++++++++++++++++
 sheepdev/proc.c     | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 sheepdev/sheepdev.h |  88 ++++++++++++++++++++++++++++++++
 4 files changed, 295 insertions(+)
 create mode 100644 sheepdev/Kbuild
 create mode 100644 sheepdev/device.c
 create mode 100644 sheepdev/proc.c
 create mode 100644 sheepdev/sheepdev.h

diff --git a/sheepdev/Kbuild b/sheepdev/Kbuild
new file mode 100644
index 0000000..0a2c060
--- /dev/null
+++ b/sheepdev/Kbuild
@@ -0,0 +1,11 @@
+
+MODULE_NAME=sheepdev
+
+ccflags-y := -I$(PWD)/../include
+
+obj-m := $(MODULE_NAME).o
+
+sheepdev-objs := device.o proc.o
+
+device.o : sheepdev.h sheepdog_proto.h
+proc.o : sheepdev.h
diff --git a/sheepdev/device.c b/sheepdev/device.c
new file mode 100644
index 0000000..15e149a
--- /dev/null
+++ b/sheepdev/device.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2013 Taobao Inc.
+ *
+ * Levin Li <xingke.lwp at taobao.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/wait.h>
+#include <linux/sched.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/genhd.h>
+#include <linux/blkdev.h>
+#include <linux/hdreg.h>
+#include <linux/proc_fs.h>
+#include <linux/kthread.h>
+#include "sheepdev.h"
+
+static int __init sheep_module_init(void)
+{
+	int ret;
+
+	DBPRT("Block device driver for Sheepdog\n");
+
+	ret = sheep_proc_init();
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static void __exit sheep_module_exit(void)
+{
+	sheep_proc_destroy();
+
+	DBPRT("Sheepdog Block Device Removed.\n");
+}
+
+module_init(sheep_module_init);
+module_exit(sheep_module_exit);
+
+MODULE_LICENSE("GPL");
diff --git a/sheepdev/proc.c b/sheepdev/proc.c
new file mode 100644
index 0000000..a2efbe0
--- /dev/null
+++ b/sheepdev/proc.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2012 Taobao Inc.
+ *
+ * Levin Li <xingke.lwp at taobao.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "sheepdev.h"
+
+static struct proc_dir_entry *sheep_proc_entry;
+
+#define MAX_CMD_LEN 64
+#define ADDR_LEN 16
+
+static int process_add_command(char *buf)
+{
+	int ret = 0;
+	char addr[ADDR_LEN];
+	char snapshot_tag[SD_MAX_VDI_TAG_LEN];
+	char vdiname[SD_MAX_VDI_LEN];
+	int snapshot_id = 0, port;
+	char *token, *cur = buf;
+	char *subtoken, *subcur;
+
+	token = strsep(&cur, " ");
+	if (!token)
+		return -EINVAL;
+
+	subcur = token;
+	subtoken = strsep(&subcur, ":");
+	strncpy(addr, subtoken, ADDR_LEN);
+	if (!subcur)
+		port = SD_LISTEN_PORT;
+	else
+		port = simple_strtol(subcur, NULL, 10);
+
+	token = strsep(&cur, " ");
+	if (!token)
+		return -EINVAL;
+
+	subcur = token;
+	token = strsep(&subcur, "\n");
+	subcur = token;
+	subtoken = strsep(&subcur, ":");
+	strncpy(vdiname, subtoken, SD_MAX_VDI_LEN);
+
+	memset(snapshot_tag, 0, sizeof(snapshot_tag));
+	if (subcur) {
+		char *p;
+		snapshot_id = simple_strtol(subcur, &p, 10);
+		if (subcur == p) {
+			snapshot_id = 0;
+			strncpy(snapshot_tag, subcur, SD_MAX_VDI_TAG_LEN);
+		}
+	}
+
+	return ret;
+}
+
+static int process_del_command(char *buf)
+{
+	char snapshot_tag[SD_MAX_VDI_TAG_LEN];
+	char vdiname[SD_MAX_VDI_LEN];
+	char *token, *cur = buf;
+	int snapshot_id = 0, ret = 0;
+
+	token = strsep(&cur, ":");
+	if (!token)
+		return -EINVAL;
+
+	strncpy(vdiname, token, SD_MAX_VDI_LEN);
+
+	memset(snapshot_tag, 0, sizeof(snapshot_tag));
+	if (cur) {
+		char *p;
+		snapshot_id = simple_strtol(cur, &p, 10);
+		if (cur == p) {
+			snapshot_id = 0;
+			strncpy(snapshot_tag, cur, SD_MAX_VDI_TAG_LEN);
+		}
+	}
+
+	return ret;
+}
+
+static ssize_t sheep_proc_write(struct file *filp, const char __user *buf,
+				size_t len, loff_t *offset)
+{
+	char *kern_buf, *token, *cur;
+	int ret;
+
+	kern_buf = kmalloc(len, GFP_KERNEL);
+	memset(kern_buf, 0, len);
+	if (!kern_buf)
+		return -ENOMEM;
+
+	if (copy_from_user(kern_buf, buf, len)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	cur = kern_buf;
+	token = strsep(&cur, " ");
+
+	if (!token)
+		ret = -EINVAL;
+	else if (strcmp(token, "add") == 0)
+		ret = process_add_command(cur);
+	else if (strcmp(token, "del") == 0)
+		ret = process_del_command(cur);
+	else
+		ret = -EINVAL;
+
+out:
+	kfree(kern_buf);
+	return ret ? ret : len;
+}
+
+static const struct file_operations sheep_proc_fops = {
+	.write = sheep_proc_write,
+};
+
+int sheep_proc_init(void)
+{
+	/* create proc entry for sheep control */
+	sheep_proc_entry = create_proc_entry(PROC_ENTRY_NAME,
+					     S_IFREG | S_IRUGO | S_IWUGO, NULL);
+	if (!sheep_proc_entry)
+		return -ENOMEM;
+
+	sheep_proc_entry->proc_fops = &sheep_proc_fops;
+
+	return 0;
+}
+
+void sheep_proc_destroy(void)
+{
+	remove_proc_entry(PROC_ENTRY_NAME, NULL);
+}
diff --git a/sheepdev/sheepdev.h b/sheepdev/sheepdev.h
new file mode 100644
index 0000000..23c93ff
--- /dev/null
+++ b/sheepdev/sheepdev.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2013 Taobao Inc.
+ *
+ * Levin Li <xingke.lwp at taobao.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __SHEEP_H_
+#define __SHEEP_H_
+
+#include <linux/socket.h>
+#include <linux/net.h>
+#include <net/sock.h>
+#include <linux/tcp.h>
+#include <linux/socket.h>
+#include <linux/slab.h>
+#include <linux/in.h>
+#include <linux/inet.h>
+#include <linux/list.h>
+#include <linux/rbtree.h>
+#include <asm/atomic.h>
+#include <linux/string.h>
+#include <net/inet_common.h>
+#include "sheepdog_proto.h"
+
+#define SHEEP_OBJECT_SIZE (4 * 1024 * 1024)
+
+#define SHEEP_BLKDEV_NAME "sheep"
+#define PROC_ENTRY_NAME "sheep"
+#define KERNEL_SECTOR_SIZE 512
+#define SHEEP_BLKDEV_MINORS 1024
+
+#define DBPRT(fmt, args...) printk(KERN_DEBUG "sheep: " fmt, ##args)
+
+struct sheepdev {
+	struct gendisk *disk;
+	struct socket *sock;
+
+	/* VDI related */
+	char ip_addr[16];
+	char vdiname[SD_MAX_VDI_LEN];
+	char snapshot_tag[SD_MAX_VDI_TAG_LEN];
+	unsigned int snapshot_id;
+	unsigned int port;
+
+	unsigned int minor;
+	unsigned int req_id;
+	unsigned int vid;
+	unsigned long size;
+	unsigned long sectors;
+	atomic_t struct_refcnt;
+	unsigned int device_refcnt;
+
+	spinlock_t dev_lock;
+	spinlock_t que_lock;
+	rwlock_t creating_lock;
+	rwlock_t sock_lock;
+
+	struct rb_root obj_state_tree;
+	struct task_struct *req_thread;
+	struct task_struct *fin_thread;
+
+	wait_queue_head_t req_wait;
+	wait_queue_head_t fin_wait;
+	wait_queue_head_t creating_wait;
+
+	struct list_head pending_list;
+	struct list_head finish_list;
+	struct list_head dev_list;
+	struct list_head deletion_list;
+
+	struct list_head sd_req_list;
+	spinlock_t sd_req_lock;
+
+	struct sheepdog_inode *inode;
+};
+
+/* proc.c */
+int sheep_proc_init(void);
+void sheep_proc_destroy(void);
+
+#endif
-- 
1.7.11.7




More information about the sheepdog mailing list