[Sheepdog] [PATCH v2 05/15] sheepfs: add 'vdi' entry
Liu Yuan
namei.unix at gmail.com
Mon May 14 11:47:30 CEST 2012
From: Liu Yuan <tailai.ly at taobao.com>
Support 'collie vdi list', mount & unmount (used by volume)
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
sheepfs/Makefile.am | 2 +-
sheepfs/core.c | 6 +++-
sheepfs/sheepfs.h | 9 ++++++
sheepfs/vdi.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 88 insertions(+), 2 deletions(-)
create mode 100644 sheepfs/vdi.c
diff --git a/sheepfs/Makefile.am b/sheepfs/Makefile.am
index 2dfbbe4..17ac0fe 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 cluster.c
+sheepfs_SOURCES = core.c cluster.c vdi.c
sheepfs_LDADD = ../lib/libsheepdog.a $(fuse_LIBS) $(LIBS)
sheepfs_DEPENDENCIES = ../lib/libsheepdog.a
diff --git a/sheepfs/core.c b/sheepfs/core.c
index 3499862..c042245 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -47,9 +47,11 @@ 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 },
+ [OP_VDI_LIST] = { vdi_list_read, NULL, vdi_list_get_size },
+ [OP_VDI_MOUNT] = { NULL, vdi_mount_write, NULL },
};
int sheepfs_set_op(const char *path, unsigned opcode)
@@ -200,6 +202,8 @@ static int create_sheepfs_layout(void)
{
if (create_cluster_layout() < 0)
return -1;
+ if (create_vdi_layout() < 0)
+ return -1;
return 0;
}
diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h
index 128e69c..4b20807 100644
--- a/sheepfs/sheepfs.h
+++ b/sheepfs/sheepfs.h
@@ -4,6 +4,8 @@
enum sheepfs_opcode {
OP_NULL = 0,
OP_CLUSTER_INFO,
+ OP_VDI_LIST,
+ OP_VDI_MOUNT,
};
extern char sheepfs_shadow[];
@@ -16,4 +18,11 @@ 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);
+/* vdi.c */
+extern int create_vdi_layout(void);
+extern int vdi_list_read(const char *path, char *buf, size_t size, off_t);
+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);
+
#endif
diff --git a/sheepfs/vdi.c b/sheepfs/vdi.c
new file mode 100644
index 0000000..8eefd59
--- /dev/null
+++ b/sheepfs/vdi.c
@@ -0,0 +1,73 @@
+/*
+ * 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_VDI "/vdi"
+#define PATH_VDI_LIST "/vdi/list"
+#define PATH_VDI_MOUNT "/vdi/mount"
+
+int create_vdi_layout(void)
+{
+ if (shadow_dir_create(PATH_VDI) < 0)
+ return -1;
+
+ if (shadow_file_create(PATH_VDI_LIST) < 0)
+ return -1;
+ if (sheepfs_set_op(PATH_VDI_LIST, OP_VDI_LIST) < 0)
+ return -1;
+
+ if (shadow_file_create(PATH_VDI_MOUNT) < 0)
+ return -1;
+ if (sheepfs_set_op(PATH_VDI_MOUNT, OP_VDI_MOUNT) < 0)
+ return -1;
+
+ return 0;
+}
+
+int vdi_list_read(const char *path, char *buf, size_t size, off_t ignore)
+{
+ return shadow_file_read(path, buf, size, 0);
+}
+
+size_t vdi_list_get_size(const char *path)
+{
+ struct strbuf *buf = sheepfs_run_cmd("collie vdi list");
+ size_t len;
+
+ if (!buf)
+ return 0;
+
+ len = shadow_file_write(path, buf->buf, buf->len);
+ strbuf_release(buf);
+ return len;
+}
+
+int vdi_mount_write(const char *path, const char *buf, size_t size,
+ off_t ignore)
+{
+ if (volume_create_entry(buf) < 0)
+ return -EIO;
+ return size;
+}
--
1.7.8.2
More information about the sheepdog
mailing list