From: Liu Yuan <tailai.ly at taobao.com> Signed-off-by: Liu Yuan <tailai.ly at taobao.com> --- sheep/sheepfs/VDI.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ sheep/sheepfs/core.c | 6 ++++- sheep/sheepfs/sheepfs.h | 9 +++++++ 3 files changed, 76 insertions(+), 1 deletions(-) create mode 100644 sheep/sheepfs/VDI.c diff --git a/sheep/sheepfs/VDI.c b/sheep/sheepfs/VDI.c new file mode 100644 index 0000000..00c5b7d --- /dev/null +++ b/sheep/sheepfs/VDI.c @@ -0,0 +1,62 @@ +#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_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_fill(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; +} diff --git a/sheep/sheepfs/core.c b/sheep/sheepfs/core.c index e3b3b86..b07200c 100644 --- a/sheep/sheepfs/core.c +++ b/sheep/sheepfs/core.c @@ -19,9 +19,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) @@ -173,6 +175,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/sheep/sheepfs/sheepfs.h b/sheep/sheepfs/sheepfs.h index 41212f8..b06b91b 100644 --- a/sheep/sheepfs/sheepfs.h +++ b/sheep/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[]; @@ -17,4 +19,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 -- 1.7.8.2 |