From 7ece0b7b04059f79e25cd7bf44218eb4688dd8e3 Mon Sep 17 00:00:00 2001 From: Avra Sengupta Date: Fri, 22 Jan 2016 16:59:53 +0530 Subject: snapshot: Fix parent volinfo corruption in snapshot restore Backport of http://review.gluster.org/#/c/13282/ Remove parent_volinfo irrespective of success or failure of glusterd_lvm_snapshot_remove(). This prevents the duplication of the volinfo after the execution of glusterd_snapshot_restore(). When commit fails on originator node, we don't perform commit on the other nodes, as a result we don't have a backup of /var/lib/glusterd/vols/ in the GLUSTERD_TRASH in other nodes. But in the postvalidate we try to restore from GLUSTED_TRASH and end up cleaning up /var/lib/glusterd/vols/. Hence moved glusterd_snapshot_backup_vol() from commit to prevalidate, so that the backup is always available when a cleanup is needed. Change-Id: Icd471b23faf02bad680b9a1aadc4a0175f7cce8b BUG: 1301030 Signed-off-by: Avra Sengupta Reviewed-on: http://review.gluster.org/13282 Smoke: Gluster Build System NetBSD-regression: NetBSD Build System CentOS-regression: Gluster Build System Reviewed-by: Rajesh Joseph (cherry picked from commit 3f78b6b3dc6376945a1b1a7c4af16103901ab746) Reviewed-on: http://review.gluster.org/13548 --- xlators/mgmt/glusterd/src/glusterd-snapshot.c | 102 +++++++++++++++++++++----- 1 file changed, 83 insertions(+), 19 deletions(-) diff --git a/xlators/mgmt/glusterd/src/glusterd-snapshot.c b/xlators/mgmt/glusterd/src/glusterd-snapshot.c index 8b814acd455..f66ee1ed045 100644 --- a/xlators/mgmt/glusterd/src/glusterd-snapshot.c +++ b/xlators/mgmt/glusterd/src/glusterd-snapshot.c @@ -853,17 +853,6 @@ glusterd_snapshot_restore (dict_t *dict, char **op_errstr, dict_t *rsp_dict) goto out; } - /* Take backup of the volinfo folder */ - ret = glusterd_snapshot_backup_vol (parent_volinfo); - if (ret) { - gf_msg (this->name, GF_LOG_ERROR, 0, - GD_MSG_VOL_OP_FAILED, - "Failed to backup " - "volume backend files for %s volume", - parent_volinfo->volname); - goto out; - } - if (is_origin_glusterd (dict) == _gf_true) { /* From origin glusterd check if * * any peers with snap bricks is down */ @@ -902,7 +891,6 @@ glusterd_snapshot_restore (dict_t *dict, char **op_errstr, dict_t *rsp_dict) gf_msg (this->name, GF_LOG_ERROR, 0, GD_MSG_LVM_REMOVE_FAILED, "Failed to remove LVM backend"); - goto out; } } @@ -911,6 +899,9 @@ glusterd_snapshot_restore (dict_t *dict, char **op_errstr, dict_t *rsp_dict) */ cds_list_del_init (&parent_volinfo->vol_list); glusterd_volinfo_unref (parent_volinfo); + + if (ret) + goto out; } ret = 0; @@ -1048,6 +1039,17 @@ glusterd_snapshot_restore_prevalidate (dict_t *dict, char **op_errstr, ret = -1; goto out; } + + /* Take backup of the volinfo folder */ + ret = glusterd_snapshot_backup_vol (volinfo); + if (ret) { + gf_msg (this->name, GF_LOG_ERROR, 0, + GD_MSG_VOL_OP_FAILED, + "Failed to backup " + "volume backend files for %s volume", + volinfo->volname); + goto out; + } } /* Get brickinfo for snap_volumes */ @@ -1055,6 +1057,7 @@ glusterd_snapshot_restore_prevalidate (dict_t *dict, char **op_errstr, cds_list_for_each_entry (volinfo, &snap->volumes, vol_list) { volcount++; brick_count = 0; + cds_list_for_each_entry (brickinfo, &volinfo->bricks, brick_list) { brick_count++; @@ -8500,6 +8503,64 @@ out: return ret; } +/* This function is called to remove the trashpath, in cases + * when the restore operation is successful and we don't need + * the backup, and incases when the restore op is failed before + * commit, and we don't need to revert the backup. + * + * @param volname name of the volume which is being restored + * + * @return 0 on success or -1 on failure + */ +int +glusterd_remove_trashpath (char *volname) +{ + int ret = -1; + char delete_path[PATH_MAX] = {0,}; + xlator_t *this = NULL; + glusterd_conf_t *priv = NULL; + struct stat stbuf = {0, }; + + this = THIS; + GF_ASSERT (this); + priv = this->private; + + GF_ASSERT (volname); + + snprintf (delete_path, sizeof (delete_path), + "%s/"GLUSTERD_TRASH"/vols-%s.deleted", priv->workdir, + volname); + + ret = lstat (delete_path, &stbuf); + if (ret) { + /* If the trash dir does not exist, return * + * without failure * + */ + if (errno == ENOENT) { + ret = 0; + goto out; + } else { + gf_msg (this->name, GF_LOG_ERROR, errno, + GD_MSG_DIR_OP_FAILED, "Failed to lstat " + "backup dir (%s)", delete_path); + goto out; + } + } + + /* Delete the backup copy of volume folder */ + ret = recursive_rmdir (delete_path); + if (ret) { + gf_msg (this->name, GF_LOG_ERROR, errno, + GD_MSG_DIR_OP_FAILED, "Failed to remove " + "backup dir (%s)", delete_path); + goto out; + } + + ret = 0; +out: + return ret; +} + /* This function is called if snapshot restore operation * is successful. It will cleanup the backup files created * during the restore operation. @@ -8516,7 +8577,6 @@ glusterd_snapshot_restore_cleanup (dict_t *rsp_dict, glusterd_snap_t *snap) { int ret = -1; - char delete_path[PATH_MAX] = {0,}; xlator_t *this = NULL; glusterd_conf_t *priv = NULL; @@ -8528,10 +8588,6 @@ glusterd_snapshot_restore_cleanup (dict_t *rsp_dict, GF_ASSERT (volname); GF_ASSERT (snap); - snprintf (delete_path, sizeof (delete_path), - "%s/"GLUSTERD_TRASH"/vols-%s.deleted", priv->workdir, - volname); - /* Now delete the snap entry. */ ret = glusterd_snap_remove (rsp_dict, snap, _gf_false, _gf_true, _gf_false); @@ -8543,11 +8599,11 @@ glusterd_snapshot_restore_cleanup (dict_t *rsp_dict, } /* Delete the backup copy of volume folder */ - ret = recursive_rmdir (delete_path); + ret = glusterd_remove_trashpath(volname); if (ret) { gf_msg (this->name, GF_LOG_ERROR, errno, GD_MSG_DIR_OP_FAILED, "Failed to remove " - "backup dir (%s)", delete_path); + "backup dir"); goto out; } @@ -8777,6 +8833,14 @@ glusterd_snapshot_restore_postop (dict_t *dict, int32_t op_ret, ret = dict_get_int32 (dict, "cleanup", &cleanup); /* Perform cleanup only when required */ if (ret || (0 == cleanup)) { + /* Delete the backup copy of volume folder */ + ret = glusterd_remove_trashpath(volinfo->volname); + if (ret) { + gf_msg (this->name, GF_LOG_ERROR, errno, + GD_MSG_DIR_OP_FAILED, + "Failed to remove backup dir"); + goto out; + } ret = 0; goto out; } -- cgit