diff options
author | shishir gowda <sgowda@redhat.com> | 2013-10-22 16:55:55 +0530 |
---|---|---|
committer | shishir gowda <sgowda@redhat.com> | 2013-11-15 12:38:59 +0530 |
commit | b1e9fab5de8ab45987b2eed9bfd12a1eb188707b (patch) | |
tree | 60fa4bd2e49e296937c09869cee995220fd9fda6 /xlators/mgmt/glusterd/src/glusterd-store.c | |
parent | cc4fa72926f9ac517365d91ae6144530dc67c001 (diff) |
mgmt/glusterd:store for CG
Change-Id: I6ec888a5553ad29ded032c02c80dd940b2aae007
Signed-off-by: shishir gowda <sgowda@redhat.com>
Diffstat (limited to 'xlators/mgmt/glusterd/src/glusterd-store.c')
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-store.c | 386 |
1 files changed, 385 insertions, 1 deletions
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c index 50e680064..b1ce059c3 100644 --- a/xlators/mgmt/glusterd/src/glusterd-store.c +++ b/xlators/mgmt/glusterd/src/glusterd-store.c @@ -732,6 +732,17 @@ glusterd_store_create_volume_dir (glusterd_volinfo_t *volinfo) } static void +glusterd_store_vol_snaps_cg_dirpath_set (char *cgdirpath, size_t len) +{ + glusterd_conf_t *priv = NULL; + + priv = THIS->private; + GF_ASSERT (priv); + + snprintf (cgdirpath, len, "%s/%s", priv->workdir, + GLUSTERD_VOL_SNAP_CG_DIR_PREFIX); +} +static void glusterd_store_vol_snaps_dirpath_set (glusterd_volinfo_t *volinfo, char *snapdirpath, size_t len) { @@ -779,7 +790,19 @@ glusterd_store_create_snaps_dir (glusterd_volinfo_t *volinfo) return ret; } +/* creates GLUSTERD_VOLUME_DIR_PREFIX/cg directory */ +static int32_t +glusterd_store_create_snaps_cg_dir () +{ + int32_t ret = -1; + char cgdirpath[PATH_MAX] = {0,}; + glusterd_store_vol_snaps_cg_dirpath_set (cgdirpath, + sizeof (cgdirpath)); + ret = gf_store_mkdir (cgdirpath); + gf_log (THIS->name, GF_LOG_DEBUG, "Returning with %d", ret); + return ret; +} /* creates GLUSTERD_VOLUME_DIR_PREFIX/<volname>/snaps/<snap-name> directory */ static int32_t glusterd_store_create_snap_vol_dir (glusterd_volinfo_t *volinfo, char *snap_name) @@ -912,6 +935,34 @@ glusterd_store_create_snap_vol_shandle_on_absence (glusterd_volinfo_t *volinfo, ret = gf_store_handle_create_on_absence (&snapinfo->shandle, volfpath); return ret; } + +static void +glusterd_store_snap_cgfpath_set (char *cg_name, + char *cgfpath, size_t len) +{ + char cgdirpath[PATH_MAX] = {0,}; + GF_ASSERT (cg_name); + GF_ASSERT (cgfpath); + GF_ASSERT (len <= PATH_MAX); + + glusterd_store_vol_snaps_cg_dirpath_set (cgdirpath, + sizeof (cgdirpath)); + snprintf (cgfpath, len, "%s/%s.info", cgdirpath, cg_name); +} + +int32_t +glusterd_store_create_snap_cg_shandle_on_absence (glusterd_snap_cg_t *cg) +{ + char cgfpath[PATH_MAX] = {0}; + int32_t ret = 0; + + GF_ASSERT (cg); + + glusterd_store_snap_cgfpath_set (cg->cg_name, cgfpath, + sizeof (cgfpath)); + ret = gf_store_handle_create_on_absence (&cg->shandle, cgfpath); + return ret; +} int32_t glusterd_store_create_rbstate_shandle_on_absence (glusterd_volinfo_t *volinfo) { @@ -1178,6 +1229,78 @@ out: } int32_t +glusterd_store_snap_cg_write (int fd, glusterd_snap_cg_t *cg) +{ + int ret = -1; + char buf[PATH_MAX] = {0, }; + uint64_t count = 0; + + GF_ASSERT (fd > 0); + GF_ASSERT (cg); + + ret = gf_store_save_value (fd, GLUSTERD_STORE_KEY_SNAP_NAME, + cg->cg_name); + if (ret) + goto out; + + snprintf (buf, sizeof (buf), "%"PRIu64, cg->volume_count); + ret = gf_store_save_value (fd, GLUSTERD_STORE_KEY_CG_VOL_COUNT, buf); + if (ret) + goto out; + + ret = gf_store_save_value (fd, GLUSTERD_STORE_KEY_SNAP_CG_ID, + uuid_utoa(cg->cg_id)); + if (ret) + goto out; + + snprintf (buf, sizeof (buf), "%d", cg->cg_status); + ret = gf_store_save_value (fd, GLUSTERD_STORE_KEY_SNAP_STATUS, buf); + if (ret) + goto out; + while (count < cg->volume_count) { + ret = gf_store_save_value (fd, GLUSTERD_STORE_KEY_VOL_ID, + uuid_utoa (cg->volumes[count].volume_id)); + if (ret) + goto out; + + count++; + } +out: + gf_log (THIS->name, GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +int32_t +glusterd_store_perform_snap_cg_store (glusterd_snap_cg_t *cg) +{ + int fd = -1; + int32_t ret = -1; + GF_ASSERT (cg); + + ret = glusterd_store_create_snap_cg_shandle_on_absence (cg); + if (ret) { + gf_log (THIS->name, GF_LOG_ERROR, "Failed to create " + " shandle for cg %s ", cg->cg_name); + goto out; + } + fd = gf_store_mkstemp (cg->shandle); + if (fd <= 0) { + ret = -1; + goto out; + } + + ret = glusterd_store_snap_cg_write (fd, cg); + if (ret) + goto out; +out: + if (ret && (fd > 0)) + gf_store_unlink_tmppath (cg->shandle); + if (fd > 0) + close (fd); + gf_log (THIS->name, GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int32_t glusterd_store_perform_snap_volume_store (glusterd_volinfo_t *volinfo, glusterd_volinfo_t *snap_volinfo) { @@ -1219,7 +1342,6 @@ out: gf_log (THIS->name, GF_LOG_DEBUG, "Returning %d", ret); return ret; } - int32_t glusterd_store_snap_volume (glusterd_volinfo_t *volinfo, glusterd_snap_t *snap) { @@ -1318,8 +1440,59 @@ out: return ret; } +int32_t +glusterd_store_snap_cg (glusterd_snap_cg_t *cg) +{ + int32_t ret = -1; + + GF_ASSERT (cg); + + + LOCK (&cg->lock); + { + ret = glusterd_store_perform_snap_cg_store (cg); + if (ret) { + gf_log (THIS->name, GF_LOG_ERROR, + "Failed store snap cg (%s)", cg->cg_name); + goto unlock; + } + } +unlock: + UNLOCK (&cg->lock); + + return 0; + +} +int32_t +glusterd_store_perform_snap_cgs_store (glusterd_conf_t *priv) +{ + int32_t ret = -1; + glusterd_snap_cg_t *entry = NULL; + glusterd_snap_cg_t *tmp = NULL; + + GF_ASSERT (priv); + ret = glusterd_store_create_snaps_cg_dir (priv); + if (ret) + goto out; + +// LOCK (&priv->lock); + { + list_for_each_entry_safe (entry, tmp, &priv->snap_cg, + cg_list) { + ret = glusterd_store_snap_cg (entry); + if (ret) + goto unlock; + } + } +unlock: +// UNLOCK (&priv->lock); + +out: + gf_log (THIS->name, GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} int32_t glusterd_store_perform_node_state_store (glusterd_volinfo_t *volinfo) @@ -1541,7 +1714,73 @@ out: return ret; } +int32_t +glusterd_store_delete_snap_cg (glusterd_snap_cg_t *cg) +{ + char pathname[PATH_MAX] = {0,}; + int32_t ret = 0; + glusterd_conf_t *priv = NULL; + char path[PATH_MAX] = {0,}; + char delete_path[PATH_MAX] = {0,}; + char trashdir[PATH_MAX] = {0,}; + xlator_t *this = NULL; + gf_boolean_t rename_fail = _gf_false; + + this = THIS; + GF_ASSERT (this); + + GF_ASSERT (cg); + priv = this->private; + + GF_ASSERT (priv); + + glusterd_store_snap_cgfpath_set (cg->cg_name, pathname, sizeof (path)); + snprintf (delete_path, sizeof (delete_path), + "%s/"GLUSTERD_TRASH"/%s/%s.deleted", priv->workdir, + GLUSTERD_VOL_SNAP_CG_DIR_PREFIX, cg->cg_name); + + snprintf (trashdir, sizeof (trashdir), "%s/"GLUSTERD_TRASH"/%s", + priv->workdir, GLUSTERD_VOL_SNAP_CG_DIR_PREFIX); + + ret = mkdir (trashdir, 0777); + if (ret && errno != EEXIST) { + gf_log (this->name, GF_LOG_ERROR, "Failed to create trash " + "directory, reason : %s", strerror (errno)); + ret = -1; + goto out; + } + + ret = rename (pathname, delete_path); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, "Failed to rename %s cg " + "info file", cg->cg_name); + rename_fail = _gf_true; + goto out; + } + + ret = unlink (delete_path); + if (ret) { + gf_log (this->name, GF_LOG_DEBUG, " Failed to remove " + "%s. Reason : %s", delete_path, strerror (errno)); + } + + ret = rmdir (trashdir); + if (ret) { + gf_log (this->name, GF_LOG_DEBUG, "Failed to rmdir: %s, Reason:" + " %s", trashdir, strerror (errno)); + } + +out: + if (!ret && cg->shandle) { + gf_store_handle_destroy (cg->shandle); + cg->shandle = NULL; + } + ret = (rename_fail == _gf_true) ? -1: 0; + + gf_log (this->name, GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} int32_t glusterd_store_delete_volume (glusterd_volinfo_t *volinfo, glusterd_volinfo_t *snapinfo) @@ -2549,6 +2788,109 @@ out: } int32_t +glusterd_store_retrieve_snap_cg (char *cg_name, glusterd_conf_t *priv) +{ + int32_t ret = -1; + gf_store_iter_t *iter = NULL; + char *key = NULL; + char *value = NULL; + char cg_path[PATH_MAX] = {0,}; + char path[PATH_MAX] = {0,}; + gf_store_op_errno_t op_errno = GD_STORE_SUCCESS; + glusterd_snap_cg_t *cg = NULL; + uint64_t vol_count = 0; + gf_store_handle_t *tmp_shandle = NULL; + uint64_t count = 0; + glusterd_volinfo_t *volinfo = NULL; + glusterd_volinfo_t *tmpinfo = NULL; + uuid_t vol_id = {0, }; + + GF_ASSERT (cg_name); + GF_ASSERT (priv); + + glusterd_store_snap_cgfpath_set (cg_name, path, sizeof (path)); + ret = gf_store_handle_retrieve (path, &tmp_shandle); + if (ret) + goto out; + + ret = gf_store_iter_new (tmp_shandle, &iter); + if (ret) + goto out; + ret = gf_store_iter_get_matching (iter, + GLUSTERD_STORE_KEY_CG_VOL_COUNT, + &value); + if (ret) + goto out; + + vol_count = atoi (value); + + GF_FREE (value); + value = NULL; + + cg = glusterd_new_snap_cg_object (vol_count); + if (!cg) { + gf_log (THIS->name, GF_LOG_ERROR, + "Failed to create snap cg object"); + goto out; + } + + cg->shandle = tmp_shandle; + + GLUSTERD_GET_SNAP_CG_DIR (cg_path, priv); + snprintf (path, sizeof (path), "%s/%s", cg_path, + cg_name); + cg->volume_count = atoi (value); + + ret = gf_store_iter_get_next (iter, &key, &value, &op_errno); + while (!ret) { + + if (!strncmp(key, GLUSTERD_STORE_KEY_SNAP_NAME, + sizeof (*key))) { + strncpy (cg->cg_name, value, sizeof (*value)); + } else if (!strncmp(key, GLUSTERD_STORE_KEY_SNAP_STATUS, + sizeof (*key))) { + cg->cg_status = atoi (value); + } else if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_CG_ID, + sizeof (*key))) { + uuid_parse (value, cg->cg_id); + } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_ID, + sizeof (*key))) { + uuid_parse (key, vol_id); + ret = glusterd_volinfo_find_by_volume_id (vol_id, &volinfo); + if (ret) + break; + if (count < vol_count) { + tmpinfo = &cg->volumes[count]; + tmpinfo = volinfo; + count++; + } + } + + GF_FREE (value); + value = NULL; + } + + ret = glusterd_add_snap_cg (priv, cg); + if (ret) { + gf_log (THIS->name, GF_LOG_ERROR, "Failed to add %s to" + " cg_list", cg_name); + goto out; + } + + if (op_errno != GD_STORE_EOF) + goto out; + + ret = gf_store_iter_destroy (iter); + + if (ret) + goto out; + +out: + gf_log ("", GF_LOG_DEBUG, "Returning with %d", ret); + + return ret; +} +int32_t glusterd_store_retrieve_snap_list (char *volname) { int32_t ret = -1; @@ -2670,6 +3012,44 @@ out: return ret; } + +int32_t +glusterd_store_retrieve_snap_cgs (xlator_t *this) +{ + int32_t ret = 0; + char path[PATH_MAX] = {0,}; + glusterd_conf_t *priv = NULL; + DIR *dir = NULL; + struct dirent *entry = NULL; + + GF_ASSERT (this); + priv = this->private; + + GF_ASSERT (priv); + + snprintf (path, PATH_MAX, "%s/%s", priv->workdir, + GLUSTERD_VOL_SNAP_CG_DIR_PREFIX); + + dir = opendir (path); + + if (!dir) { + gf_log ("", GF_LOG_ERROR, "Unable to open dir %s", path); + ret = -1; + goto out; + } + + glusterd_for_each_entry (entry, dir); + + while (entry) { + ret = glusterd_store_retrieve_snap_cg (entry->d_name, priv); + if (ret) + goto out; + glusterd_for_each_entry (entry, dir); + } +out: + return 0; +} + int32_t glusterd_store_retrieve_volumes (xlator_t *this) { @@ -3155,6 +3535,10 @@ glusterd_restore () if (ret) goto out; + ret = glusterd_store_retrieve_snap_cgs (this); + if (ret) + goto out; + ret = glusterd_store_retrieve_peers (this); if (ret) goto out; |