[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[lvm-devel] [LVM2 PATCH 1/2] lvrename: factor out core routine



Hi,

This patch factors out core renaming function from lvrename()
as a preparation for mirrored LV renaming.

No functional change except for the order of the validity check.
It used to be:
  - check if the new name is not yet used in the VG
  - check if the old name really exists in the VG
This order is now reversed.
As the VG is locked, it should have no problem.

Also the following cleanups are included:
  - converting "stack; return 0;" to "return_0"
  - removing duplicated mirrored LV check
    (there were 2 identical checks)

Applicable to the CVS head.

Thanks,
-- 
Jun'ichi Nomura, NEC Corporation of America
Factor out core renaming function from lvrename()
as a preparation for mirrored LV renaming.

No functional change except for the order of the validity check.
It used to be:
  - check if the new name is not yet used in the VG
  - check if the old name really exists in the VG
This order is now reversed.

As the VG is locked, it should have no problem.

Also the following cleanups are included:
  - converting "stack; return 0;" to "return_0"
  - removing duplicated mirrored LV check (there were 2 identical checks)

Index: LVM2.work/tools/lvrename.c
===================================================================
--- LVM2.work.orig/tools/lvrename.c
+++ LVM2.work/tools/lvrename.c
@@ -16,6 +16,71 @@
 #include "tools.h"
 #include "lvm-types.h"
 
+
+/*
+ * Core of LV renaming routine.
+ * VG must be locked by caller.
+ * Returns 0 on failure, 1 on success.
+ */
+static int rename_lv(struct cmd_context *cmd, struct logical_volume *lv,
+		     char *newname)
+{
+	struct volume_group *vg = lv->vg;
+
+	if (find_lv_in_vg(vg, newname)) {
+		log_error("Logical volume \"%s\" already exists in "
+			  "volume group \"%s\"", newname, vg->name);
+		return 0;
+	}
+
+	if (lv->status & LOCKED) {
+		log_error("Cannot rename locked LV %s", lv->name);
+		return 0;
+	}
+
+	if ((lv->status & MIRRORED) ||
+	    (lv->status & MIRROR_LOG) ||
+	    (lv->status & MIRROR_IMAGE)) {
+		log_error("Mirrored LV, \"%s\" cannot be renamed: %s",
+			  lv->name, strerror(ENOSYS));
+		return 0;
+	}
+
+	if (!archive(vg))
+		return_0;
+
+	if (!(lv->name = dm_pool_strdup(cmd->mem, newname))) {
+		log_error("Failed to allocate space for new name");
+		return 0;
+	}
+
+	log_verbose("Writing out updated volume group");
+	if (!vg_write(vg))
+		return_0;
+
+	backup(vg);
+
+	if (!suspend_lv(cmd, lv)) {
+		stack;
+		vg_revert(vg);
+		return 0;
+	}
+
+	if (!vg_commit(vg)) {
+		stack;
+		resume_lv(cmd, lv);
+		return 0;
+	}
+
+	resume_lv(cmd, lv);
+
+	return 1;
+}
+
+/*
+ * lvrename command implementation.
+ * Check arguments and call rename_lv() to execute the request.
+ */
 int lvrename(struct cmd_context *cmd, int argc, char **argv)
 {
 	size_t maxlen;
@@ -24,7 +89,6 @@ int lvrename(struct cmd_context *cmd, in
 	char *st;
 
 	struct volume_group *vg;
-	struct logical_volume *lv;
 	struct lv_list *lvl;
 
 	if (argc == 3) {
@@ -103,72 +167,14 @@ int lvrename(struct cmd_context *cmd, in
 				    CORRECT_INCONSISTENT)))
 		return ECMD_FAILED;
 
-	if (find_lv_in_vg(vg, lv_name_new)) {
-		log_error("Logical volume \"%s\" already exists in "
-			  "volume group \"%s\"", lv_name_new, vg_name);
-		goto error;
-	}
-
 	if (!(lvl = find_lv_in_vg(vg, lv_name_old))) {
 		log_error("Existing logical volume \"%s\" not found in "
 			  "volume group \"%s\"", lv_name_old, vg_name);
 		goto error;
 	}
 
-	lv = lvl->lv;
-
-	if (lv->status & LOCKED) {
-		log_error("Cannot rename locked LV %s", lv->name);
-		goto error;
-	}
-
-	if ((lv->status & MIRRORED) ||
-	    (lv->status & MIRROR_LOG) ||
-	    (lv->status & MIRROR_IMAGE)) {
-		log_error("Mirrored LV, \"%s\" cannot be renamed: %s",
-			  lv->name, strerror(ENOSYS));
+	if (!rename_lv(cmd, lvl->lv, lv_name_new))
 		goto error;
-	}
-
-	if ((lv->status & MIRRORED) ||
-	    (lv->status & MIRROR_LOG) ||
-	    (lv->status & MIRROR_IMAGE)) {
-		log_error("Mirrored LV, \"%s\" cannot be renamed: %s",
-			  lv->name, strerror(ENOSYS));
-		goto error;
-	}
-
-	if (!archive(lv->vg)) {
-		stack;
-		goto error;
-	}
-
-	if (!(lv->name = dm_pool_strdup(cmd->mem, lv_name_new))) {
-		log_error("Failed to allocate space for new name");
-		goto error;
-	}
-
-	log_verbose("Writing out updated volume group");
-	if (!vg_write(vg)) {
-		stack;
-		goto error;
-	}
-
-	backup(lv->vg);
-
-	if (!suspend_lv(cmd, lv)) {
-		stack;
-		vg_revert(vg);
-		goto error;
-	}
-
-	if (!vg_commit(vg)) {
-		stack;
-		resume_lv(cmd, lv);
-		goto error;
-	}
-
-	resume_lv(cmd, lv);
 
 	unlock_vg(cmd, vg_name);
 

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]