From: Liu Yuan <tailai.ly at taobao.com> Sheep needs to set system VDI bitmap when starting up. And VDI object is stored in the underlying backend stores which have different layouts. Signed-off-by: Liu Yuan <tailai.ly at taobao.com> --- sheep/farm/farm.c | 37 +++++++++++++++++++++++++++++++++++++ sheep/simple_store.c | 43 +++++++++++++++++++++++++++++++++++++++++-- sheep/store.c | 46 ++-------------------------------------------- 3 files changed, 80 insertions(+), 46 deletions(-) diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c index 0cb453e..7a980ec 100644 --- a/sheep/farm/farm.c +++ b/sheep/farm/farm.c @@ -185,6 +185,40 @@ static int farm_close(uint64_t oid, struct siocb *iocb) return SD_RES_SUCCESS; } +static int init_sys_vdi_bitmap(char *path) +{ + DIR *dir; + struct dirent *dent; + + dir = opendir(path); + if (!dir) { + vprintf(SDOG_ERR, "failed to open the working directory: %m\n"); + return -1; + } + + vprintf(SDOG_INFO, "found the working directory %s\n", path); + while ((dent = readdir(dir))) { + uint64_t oid; + + if (!strcmp(dent->d_name, ".")) + continue; + + oid = strtoull(dent->d_name, NULL, 16); + if (oid == 0 || oid == ULLONG_MAX) + continue; + + if (!is_vdi_obj(oid)) + continue; + + vprintf(SDOG_DEBUG, "found the VDI object %" PRIx64 "\n", oid); + + set_bit(oid_to_vid(oid), sys->vdi_inuse); + } + closedir(dir); + + return 0; +} + static int farm_init(char *p) { dprintf("use farm store driver\n"); @@ -197,6 +231,9 @@ static int farm_init(char *p) if (snap_init() < 0) goto err; + if (init_sys_vdi_bitmap(p) < 0) + goto err; + return SD_RES_SUCCESS; err: return SD_RES_EIO; diff --git a/sheep/simple_store.c b/sheep/simple_store.c index 2f9d959..3597926 100644 --- a/sheep/simple_store.c +++ b/sheep/simple_store.c @@ -36,8 +36,47 @@ struct store_driver store; static int simple_store_init(char *path) { - eprintf("Use simple store driver\n"); - return 0; + int epoch, latest_epoch; + DIR *dir; + struct dirent *dent; + char p[PATH_MAX]; + + eprintf("use simple store driver\n"); + strcpy(p, path); + latest_epoch = get_latest_epoch(); + for (epoch = 1; epoch <= latest_epoch; epoch++) { + snprintf(p, PATH_MAX, "%s/%08u", path, epoch); + dir = opendir(p); + if (!dir) { + if (errno == ENOENT) + continue; + + vprintf(SDOG_ERR, "failed to open the epoch directory: %m\n"); + return SD_RES_EIO; + } + + vprintf(SDOG_INFO, "found the object directory %s\n", path); + while ((dent = readdir(dir))) { + uint64_t oid; + + if (!strcmp(dent->d_name, ".") || + !strcmp(dent->d_name, "..")) + continue; + + oid = strtoull(dent->d_name, NULL, 16); + if (oid == 0 || oid == ULLONG_MAX) + continue; + + if (!is_vdi_obj(oid)) + continue; + + vprintf(SDOG_DEBUG, "found the VDI object %" PRIx64 "\n", oid); + + set_bit(oid_to_vid(oid), sys->vdi_inuse); + } + closedir(dir); + } + return SD_RES_SUCCESS; } static int store_write_last_sector(uint64_t oid, struct siocb *iocb) diff --git a/sheep/store.c b/sheep/store.c index eac31c7..65ac1b6 100644 --- a/sheep/store.c +++ b/sheep/store.c @@ -1889,54 +1889,12 @@ static int init_obj_path(const char *base_path) static int init_epoch_path(const char *base_path) { - int new, ret; - uint32_t epoch, latest_epoch; - DIR *dir; - char path[1024]; - struct dirent *dent; - uint64_t oid; + int new; epoch_path = zalloc(strlen(base_path) + strlen(EPOCH_PATH) + 1); sprintf(epoch_path, "%s" EPOCH_PATH, base_path); - ret = init_path(epoch_path, &new); - if (new || ret) - return ret; - - latest_epoch = get_latest_epoch(); - - for (epoch = 1; epoch <= latest_epoch; epoch++) { - snprintf(path, sizeof(path), "%s/%08u", obj_path, epoch); - - vprintf(SDOG_INFO, "found the object directory %s\n", path); - - dir = opendir(path); - if (!dir) { - if (errno == ENOENT) - continue; - - vprintf(SDOG_ERR, "failed to open the epoch directory: %m\n"); - return SD_RES_EIO; - } - - while ((dent = readdir(dir))) { - if (!strcmp(dent->d_name, ".") || - !strcmp(dent->d_name, "..")) - continue; - - oid = strtoull(dent->d_name, NULL, 16); - - if (!is_vdi_obj(oid)) - continue; - - vprintf(SDOG_DEBUG, "found the VDI object %" PRIx64 "\n", oid); - - set_bit(oid_to_vid(oid), sys->vdi_inuse); - } - closedir(dir); - } - - return 0; + return init_path(epoch_path, &new); } static int init_mnt_path(const char *base_path) -- 1.7.8.2 |