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

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


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


Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/sheepfs/cluster.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 sheep/sheepfs/core.c    |    9 +++++++--
 sheep/sheepfs/sheepfs.h |    6 ++++++
 3 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100644 sheep/sheepfs/cluster.c

diff --git a/sheep/sheepfs/cluster.c b/sheep/sheepfs/cluster.c
new file mode 100644
index 0000000..77a4acc
--- /dev/null
+++ b/sheep/sheepfs/cluster.c
@@ -0,0 +1,47 @@
+#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_CLUSTER		"/cluster"
+#define PATH_CLUSTER_INFO	"/cluster/info"
+
+int create_cluster_layout(void)
+{
+	if (shadow_dir_create(PATH_CLUSTER) < 0)
+		return -1;
+
+	if (shadow_file_create(PATH_CLUSTER_INFO) < 0)
+		return -1;
+	sheepfs_set_op(PATH_CLUSTER_INFO, OP_CLUSTER_INFO);
+
+	return 0;
+}
+
+int cluster_info_read(const char *path, char *buf, size_t size, off_t ignore)
+{
+	return shadow_file_read(path, buf, size, 0);
+}
+
+size_t cluster_info_get_size(const char *path)
+{
+	struct strbuf *buf = sheepfs_run_cmd("collie cluster info");
+	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/core.c b/sheep/sheepfs/core.c
index 880078c..e3b3b86 100644
--- a/sheep/sheepfs/core.c
+++ b/sheep/sheepfs/core.c
@@ -19,7 +19,9 @@ static struct sheepfs_file_operation {
 	int (*write)(const char *path, const char *buf, size_t size, off_t);
 	size_t (*get_size)(const char *path);
 } sheepfs_file_ops[] = {
-	[OP_NULL] = { NULL, NULL, NULL },
+	[OP_NULL]	  = { NULL, NULL, NULL },
+	[OP_CLUSTER_INFO] = { cluster_info_read, NULL,
+				cluster_info_get_size },
 };
 
 int sheepfs_set_op(const char *path, unsigned opcode)
@@ -167,8 +169,11 @@ static void sheepfs_main_loop(char *root)
 	return;
 }
 
-static int create_sheepfs_layout(char *path)
+static int create_sheepfs_layout(void)
 {
+	if (create_cluster_layout() < 0)
+		return -1;
+
 	return 0;
 }
 
diff --git a/sheep/sheepfs/sheepfs.h b/sheep/sheepfs/sheepfs.h
index 8b27a51..41212f8 100644
--- a/sheep/sheepfs/sheepfs.h
+++ b/sheep/sheepfs/sheepfs.h
@@ -3,6 +3,7 @@
 
 enum sheepfs_opcode {
 	OP_NULL = 0,
+	OP_CLUSTER_INFO,
 };
 
 extern char sheepfs_shadow[];
@@ -11,4 +12,9 @@ extern struct strbuf *sheepfs_run_cmd(const char *command);
 extern int sheepfs_init(const char *dir);
 extern int sheepfs_set_op(const char *path, unsigned opcode);
 
+/* cluster.c */
+extern int cluster_info_read(const char *path, char *buf, size_t size, off_t);
+extern size_t cluster_info_get_size(const char *path);
+extern int create_cluster_layout(void);
+
 #endif
-- 
1.7.8.2




More information about the sheepdog mailing list