From: Liu Yuan <tailai...@taobao.com>

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

Signed-off-by: Liu Yuan <tailai...@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 4b1eedc..0cb453e 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)
 {
@@ -559,6 +560,32 @@ out:
        return ret;
 }
 
+static int farm_format(struct siocb *iocb)
+{
+       char path[PATH_MAX];
+       unsigned ret;
+       const uint8_t 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 (set_cluster_store(name) < 0)
+               return SD_RES_EIO;
+
+       trunk_reset();
+
+       return SD_RES_SUCCESS;
+}
+
 struct store_driver farm = {
        .name = "farm",
        .init = farm_init,
@@ -574,6 +601,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 41242c9..5f38445 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -575,8 +575,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 uint8_t *name = (uint8_t *)sd_store->name;
                        sd_store->init(obj_path);
+                       if (set_cluster_store(name) != SD_RES_SUCCESS)
+                               panic("failed to store into config 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 58a1956..4a672be 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(data);
        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 df641b3..750d0ca 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);
@@ -299,6 +300,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..2f9d959 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;
+       const uint8_t 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 (set_cluster_store(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 ccdb925..eac31c7 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -41,7 +41,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;
@@ -892,7 +892,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;
@@ -949,13 +949,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.2

-- 
sheepdog mailing list
sheepdog@lists.wpkg.org
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to