[Sheepdog] [PATCH v5 16/17] farm: add format() support

Liu Yuan namei.unix at gmail.com
Fri Dec 30 14:07:11 CET 2011


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

- add a format() hook to store driver interface.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/farm/farm.c    |   28 ++++++++++++++++++++++++++++
 sheep/group.c        |    7 ++++++-
 sheep/ops.c          |    6 +++++-
 sheep/sheep_priv.h   |    3 +++
 sheep/simple_store.c |   23 +++++++++++++++++++++++
 sheep/store.c        |   11 ++---------
 6 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c
index c0fd65f..d4b0bd4 100644
--- a/sheep/farm/farm.c
+++ b/sheep/farm/farm.c
@@ -22,6 +22,7 @@ char farm_dir[PATH_MAX];
 static int def_open_flags = O_DSYNC | O_RDWR;
 extern char *obj_path;
 extern mode_t def_fmode;
+extern mode_t def_dmode;
 
 static int create_directory(char *p)
 {
@@ -558,6 +559,32 @@ out:
 	return ret;
 }
 
+static int farm_format(struct siocb *iocb)
+{
+	char path[PATH_MAX];
+	unsigned ret;
+	char name[] = "farm";
+
+	dprintf("try get a clean store\n");
+	snprintf(path, sizeof(path), "%s", obj_path);
+	ret = rmdir_r(path);
+	if (ret && ret != -ENOENT) {
+		eprintf("failed to remove %s: %s\n", path, strerror(-ret));
+		return SD_RES_EIO;
+	}
+	if (mkdir(path, def_dmode) < 0) {
+		eprintf("%m\n");
+		return SD_RES_EIO;
+	}
+
+	if (store_file_write(name, ARRAY_SIZE(name)) < 0)
+		return SD_RES_EIO;
+
+	trunk_reset();
+
+	return SD_RES_SUCCESS;
+}
+
 struct store_driver farm = {
 	.name = "farm",
 	.init = farm_init,
@@ -573,6 +600,7 @@ struct store_driver farm = {
 	.snapshot = farm_snapshot,
 	.restore = farm_restore,
 	.get_snap_file = farm_get_snap_file,
+	.format = farm_format,
 };
 
 add_store_driver(farm);
diff --git a/sheep/group.c b/sheep/group.c
index 239f311..9aecf85 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -568,8 +568,13 @@ static void update_cluster_info(struct join_message *msg,
 
 	if (!sd_store && strlen((char *)msg->store)) {
 		sd_store = find_store_driver((char *)msg->store);
-		if (sd_store)
+		if (sd_store) {
+			const char *name = sd_store->name;
 			sd_store->init(obj_path);
+			if (store_file_write((void *)name, strlen(name) + 1) < 0)
+				panic("failed to write store file\n");
+		} else
+				panic("backend store %s not supported\n", msg->store);
 	}
 
 join_finished:
diff --git a/sheep/ops.c b/sheep/ops.c
index 5270ee4..edb0b22 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -135,11 +135,15 @@ static int cluster_make_fs(const struct sd_req *req, struct sd_rsp *rsp,
 	const struct sd_so_req *hdr = (const struct sd_so_req *)req;
 	int i, latest_epoch, ret;
 	uint64_t ctime;
+	struct siocb iocb = { 0 };
 
 	sd_store = find_store_driver(index_to_name(hdr->index));
 	if (!sd_store)
 		return SD_RES_NO_STORE;
 
+	latest_epoch = get_latest_epoch();
+	iocb.epoch = latest_epoch;
+	sd_store->format(&iocb);
 	sd_store->init(obj_path);
 	sys->nr_sobjs = hdr->copies;
 	sys->flags = hdr->flags;
@@ -149,9 +153,9 @@ static int cluster_make_fs(const struct sd_req *req, struct sd_rsp *rsp,
 	ctime = hdr->ctime;
 	set_cluster_ctime(ctime);
 
-	latest_epoch = get_latest_epoch();
 	for (i = 1; i <= latest_epoch; i++)
 		remove_epoch(i);
+
 	memset(sys->vdi_inuse, 0, sizeof(sys->vdi_inuse));
 
 	sys->epoch = 1;
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 6535305..df3db78 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -171,6 +171,7 @@ struct store_driver {
 	int (*write)(uint64_t oid, struct siocb *);
 	int (*read)(uint64_t oid, struct siocb *);
 	int (*close)(uint64_t oid, struct siocb *);
+	int (*format)(struct siocb*);
 	/* Operations in recovery */
 	int (*get_objlist)(struct siocb *);
 	int (*link)(uint64_t oid, struct siocb *, int tgt_epoch);
@@ -309,6 +310,8 @@ int remove_object(struct sd_vnode *e,
 void del_sheep_fd(int fd);
 int get_sheep_fd(uint8_t *addr, uint16_t port, int node_idx, uint32_t epoch);
 
+int rmdir_r(char *dir_path);
+
 /* Operations */
 
 struct sd_op_template *get_sd_op(uint8_t opcode);
diff --git a/sheep/simple_store.c b/sheep/simple_store.c
index a5711c1..224bcd1 100644
--- a/sheep/simple_store.c
+++ b/sheep/simple_store.c
@@ -239,6 +239,28 @@ out:
 	return ret;
 }
 
+static int simple_store_format(struct siocb *iocb)
+{
+	char path[PATH_MAX];
+	unsigned epoch = iocb->epoch, ret, i;
+	char name[] = "simple";
+
+	dprintf("epoch %u\n", epoch);
+	for (i = 1; i <= epoch; i++) {
+		snprintf(path, sizeof(path), "%s%08u", obj_path, i);
+		ret = rmdir_r(path);
+		if (ret && ret != -ENOENT) {
+			eprintf("failed to remove %s: %s\n", path, strerror(-ret));
+			return SD_RES_EIO;
+		}
+	}
+
+	if (store_file_write(name, ARRAY_SIZE(name)) < 0)
+		return SD_RES_EIO;
+
+	return SD_RES_SUCCESS;
+}
+
 struct store_driver simple_store = {
 	.name = "simple",
 	.init = simple_store_init,
@@ -249,6 +271,7 @@ struct store_driver simple_store = {
 	.get_objlist = simple_store_get_objlist,
 	.link = simple_store_link,
 	.atomic_put = simple_store_atomic_put,
+	.format = simple_store_format,
 };
 
 add_store_driver(simple_store);
diff --git a/sheep/store.c b/sheep/store.c
index 00e568e..7c46f9d 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -40,7 +40,7 @@ static char *mnt_path;
 static char *jrnl_path;
 static char *config_path;
 
-static mode_t def_dmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
+mode_t def_dmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
 mode_t def_fmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
 
 struct store_driver *sd_store;
@@ -891,7 +891,7 @@ int get_latest_epoch(void)
 }
 
 /* remove directory recursively */
-static int rmdir_r(char *dir_path)
+int rmdir_r(char *dir_path)
 {
 	int ret;
 	struct stat s;
@@ -948,13 +948,6 @@ int remove_epoch(int epoch)
 		return SD_RES_EIO;
 	}
 
-	snprintf(path, sizeof(path), "%s%08u", obj_path, epoch);
-	ret = rmdir_r(path);
-	if (ret && ret != -ENOENT) {
-		eprintf("failed to remove %s: %s\n", path, strerror(-ret));
-		return SD_RES_EIO;
-	}
-
 	snprintf(path, sizeof(path), "%s%08u/", jrnl_path, epoch);
 	ret = rmdir_r(path);
 	if (ret && ret != -ENOENT) {
-- 
1.7.8.rc3




More information about the sheepdog mailing list