[sheepdog] [PATCH 2/2] sheep: implement atomic create and write mechanism for config
Hitoshi Mitake
mitake.hitoshi at gmail.com
Mon Apr 15 17:16:58 CEST 2013
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
sheep/config.c | 33 ++++++++++++++++++++++++++++++---
1 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/sheep/config.c b/sheep/config.c
index 49916e7..a2ffd8c 100644
--- a/sheep/config.c
+++ b/sheep/config.c
@@ -29,17 +29,18 @@ static struct sheepdog_config {
uint64_t space;
} config;
-char *config_path;
+char *config_path, *tmp_config_path;
#define CONFIG_PATH "/config"
+#define TMP_CONFIG_PATH "/config.tmp"
static int write_config(void)
{
int fd, ret;
- fd = open(config_path, O_RDWR | O_CREAT | O_DSYNC, def_fmode);
+ fd = open(tmp_config_path, O_WRONLY | O_CREAT | O_SYNC, def_fmode);
if (fd < 0) {
- sd_eprintf("failed to open config file, %m");
+ sd_eprintf("failed to temporal file for config, %m");
return SD_RES_EIO;
}
@@ -51,13 +52,35 @@ static int write_config(void)
ret = SD_RES_SUCCESS;
close(fd);
+ ret = rename(tmp_config_path, config_path);
+ if (ret < 0) {
+ sd_eprintf("failed to rename, %m");
+ ret = SD_RES_EIO;
+ }
+
+ unlink(tmp_config_path);
+
return ret;
}
+static void check_tmp_config(void)
+{
+ int fd;
+
+ fd = open(tmp_config_path, O_RDONLY);
+ if (fd < 0 && errno == ENOENT)
+ return;
+
+ sd_iprintf("temporal file for config exists");
+ unlink(tmp_config_path);
+}
+
int init_config_file(void)
{
int fd, ret;
+ check_tmp_config();
+
fd = open(config_path, O_RDONLY);
if (fd < 0) {
if (errno != ENOENT) {
@@ -119,6 +142,10 @@ void init_config_path(const char *base_path)
config_path = xzalloc(len);
snprintf(config_path, len, "%s" CONFIG_PATH, base_path);
+
+ len = strlen(base_path) + strlen(TMP_CONFIG_PATH) + 1;
+ tmp_config_path = xzalloc(len);
+ snprintf(tmp_config_path, len, "%s" TMP_CONFIG_PATH, base_path);
}
int set_cluster_ctime(uint64_t ct)
--
1.7.5.1
More information about the sheepdog
mailing list