[sheepdog] [PATCH v3 04/15] sheepfs: add 'cluster' entry
Liu Yuan
namei.unix at gmail.com
Mon May 21 17:25:48 CEST 2012
From: Liu Yuan <tailai.ly at taobao.com>
This export cluster state as collie cluster command.
You can read the those info by simply ordinary read/write. For e.g,
tailai.ly at taobao:~/sheepdog$ cat sheepfs_dir/cluster/info
Cluster status: running
Cluster created at Mon May 14 15:45:37 2012
Epoch Time Version
2012-05-14 15:45:38 1 [127.0.0.1:7000, 127.0.0.1:7001, 127.0.0.1:7002]
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
include/net.h | 2 ++
sheepfs/Makefile.am | 2 +-
sheepfs/cluster.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++
sheepfs/core.c | 10 ++++++---
sheepfs/sheepfs.h | 6 ++++++
5 files changed, 75 insertions(+), 4 deletions(-)
create mode 100644 sheepfs/cluster.c
diff --git a/include/net.h b/include/net.h
index 698b55e..0286ea5 100644
--- a/include/net.h
+++ b/include/net.h
@@ -4,6 +4,8 @@
#include <sys/socket.h>
#include <arpa/inet.h>
+#include "sheepdog_proto.h"
+
#define DEFAULT_SOCKET_TIMEOUT 5 /* seconds */
enum conn_state {
diff --git a/sheepfs/Makefile.am b/sheepfs/Makefile.am
index c6c6741..2dfbbe4 100644
--- a/sheepfs/Makefile.am
+++ b/sheepfs/Makefile.am
@@ -23,7 +23,7 @@ INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
sbin_PROGRAMS = sheepfs
-sheepfs_SOURCES = core.c
+sheepfs_SOURCES = core.c cluster.c
sheepfs_LDADD = ../lib/libsheepdog.a $(fuse_LIBS) $(LIBS)
sheepfs_DEPENDENCIES = ../lib/libsheepdog.a
diff --git a/sheepfs/cluster.c b/sheepfs/cluster.c
new file mode 100644
index 0000000..c4a481b
--- /dev/null
+++ b/sheepfs/cluster.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2012 Taobao Inc.
+ *
+ * Liu Yuan <namei.unix at gmail.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 <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 <syslog.h>
+
+#include "strbuf.h"
+#include "sheepfs.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;
+ if (sheepfs_set_op(PATH_CLUSTER_INFO, OP_CLUSTER_INFO) < 0)
+ return -1;
+
+ 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_write(path, buf->buf, buf->len);
+ strbuf_release(buf);
+ return len;
+}
diff --git a/sheepfs/core.c b/sheepfs/core.c
index 9074ae5..6b49271 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -34,20 +34,21 @@ static int sheepfs_fg;
static struct option const long_options[] = {
{"debug", no_argument, NULL, 'd'},
- {"directio", no_argument, NULL, 'D'},
{"help", no_argument, NULL, 'h'},
{"foreground", no_argument, NULL, 'f'},
{NULL, 0, NULL, 0},
};
-static const char *short_options = "dDhf";
+static const char *short_options = "dhf";
static struct sheepfs_file_operation {
int (*read)(const char *path, char *buf, size_t size, off_t);
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)
@@ -196,6 +197,9 @@ static int sheepfs_main_loop(char *mountpoint)
static int create_sheepfs_layout(void)
{
+ if (create_cluster_layout() < 0)
+ return -1;
+
return 0;
}
diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h
index d1b2a14..128e69c 100644
--- a/sheepfs/sheepfs.h
+++ b/sheepfs/sheepfs.h
@@ -3,6 +3,7 @@
enum sheepfs_opcode {
OP_NULL = 0,
+ OP_CLUSTER_INFO,
};
extern char sheepfs_shadow[];
@@ -10,4 +11,9 @@ extern char sheepfs_shadow[];
extern struct strbuf *sheepfs_run_cmd(const char *command);
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.10.2
More information about the sheepdog
mailing list