[Sheepdog] [PATCH RFC 10/10] sheepfs: export 'node' state

Liu Yuan namei.unix at gmail.com
Sat May 5 13:29:22 CEST 2012


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


Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/Makefile.am       |    3 +-
 sheep/sheepfs/core.c    |    4 ++
 sheep/sheepfs/node.c    |   71 +++++++++++++++++++++++++++++++++++++++++++++++
 sheep/sheepfs/sheepfs.h |    9 ++++++
 4 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100644 sheep/sheepfs/node.c

diff --git a/sheep/Makefile.am b/sheep/Makefile.am
index 2e2e17a..e65ac15 100644
--- a/sheep/Makefile.am
+++ b/sheep/Makefile.am
@@ -48,7 +48,8 @@ endif
 
 if BUILD_SHEEPFS
 sheep_SOURCES		+= sheepfs/core.c sheepfs/cluster.c sheepfs/VDI.c \
-			   sheepfs/shadow_file.c sheepfs/volume.c
+			   sheepfs/shadow_file.c sheepfs/volume.c \
+			   sheepfs/node.c
 endif
 
 sheep_LDADD	  	= ../lib/libsheepdog.a -lpthread \
diff --git a/sheep/sheepfs/core.c b/sheep/sheepfs/core.c
index 02e17d6..d0583aa 100644
--- a/sheep/sheepfs/core.c
+++ b/sheep/sheepfs/core.c
@@ -28,6 +28,8 @@ static struct sheepfs_file_operation {
 	[OP_VDI_LIST]     = { vdi_list_read, NULL, vdi_list_get_size },
 	[OP_VDI_MOUNT]    = { NULL, vdi_mount_write },
 	[OP_VDI_UNMOUNT]  = { NULL, vdi_unmount_write },
+	[OP_NODE_INFO]    = { node_info_read, NULL, node_info_get_size },
+	[OP_NODE_LIST]    = { node_list_read, NULL, node_list_get_size },
 	[OP_VOLUME]       = { volume_read, volume_write, volume_get_size,
 			      volume_sync, volume_open },
 };
@@ -212,6 +214,8 @@ static int create_sheepfs_layout(void)
 		return -1;
 	if (create_volume_layout() < 0)
 		return -1;
+	if (create_node_layout() < 0)
+		return -1;
 
 	return 0;
 }
diff --git a/sheep/sheepfs/node.c b/sheep/sheepfs/node.c
new file mode 100644
index 0000000..b09ee04
--- /dev/null
+++ b/sheep/sheepfs/node.c
@@ -0,0 +1,71 @@
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+
+#include "../sheep_priv.h"
+#include "../strbuf.h"
+#include "sheepfs.h"
+#include "logger.h"
+#include "net.h"
+
+#define PATH_NODE	"/node"
+#define PATH_NODE_INFO	"/node/info"
+#define PATH_NODE_LIST	"/node/list"
+
+
+int create_node_layout(void)
+{
+	if (shadow_dir_create(PATH_NODE) < 0)
+		return -1;
+
+	if (shadow_file_create(PATH_NODE_INFO) < 0)
+		return -1;
+	sheepfs_set_op(PATH_NODE_INFO, OP_NODE_INFO);
+
+	if (shadow_file_create(PATH_NODE_LIST) < 0)
+		return -1;
+	sheepfs_set_op(PATH_NODE_LIST, OP_NODE_LIST);
+
+	return 0;
+}
+
+int node_info_read(const char *path, char *buf, size_t size, off_t ignore)
+{
+	return shadow_file_read(path, buf, size, 0);
+}
+
+size_t node_info_get_size(const char *path)
+{
+	struct strbuf *buf = sheepfs_run_cmd("collie node info");
+	size_t len;
+
+	if (!buf)
+		return 0;
+
+	len = shadow_file_fill(path, buf->buf, buf->len);
+	strbuf_release(buf);
+	return len;
+}
+
+int node_list_read(const char *path, char *buf, size_t size, off_t ignore)
+{
+	return shadow_file_read(path, buf, size, 0);
+}
+
+size_t node_list_get_size(const char *path)
+{
+	struct strbuf *buf = sheepfs_run_cmd("collie node list");
+	size_t len;
+
+	if (!buf)
+		return 0;
+
+	len = shadow_file_fill(path, buf->buf, buf->len);
+	strbuf_release(buf);
+	return len;
+}
diff --git a/sheep/sheepfs/sheepfs.h b/sheep/sheepfs/sheepfs.h
index fcc00f3..bdb7209 100644
--- a/sheep/sheepfs/sheepfs.h
+++ b/sheep/sheepfs/sheepfs.h
@@ -9,6 +9,8 @@ enum sheepfs_opcode {
 	OP_VDI_LIST,
 	OP_VDI_MOUNT,
 	OP_VDI_UNMOUNT,
+	OP_NODE_INFO,
+	OP_NODE_LIST,
 	OP_VOLUME,
 };
 
@@ -54,4 +56,11 @@ extern size_t vdi_list_get_size(const char *path);
 extern int vdi_mount_write(const char *, const char *buf, size_t size, off_t);
 extern int vdi_unmount_write(const char *, const char *buf, size_t, off_t);
 
+/* node.c */
+extern int node_list_read(const char *path, char *buf, size_t size, off_t);
+extern size_t node_list_get_size(const char *path);
+extern int node_info_read(const char *path, char *buf, size_t size, off_t);
+extern size_t node_info_get_size(const char *path);
+extern int create_node_layout(void);
+
 #endif
-- 
1.7.8.2




More information about the sheepdog mailing list