Skip to contentSkip to conversation

Refactor vgremove and move orphan lock inside library calls

PATCH New Dave Wysochanski 1 participant 5 messages Jul 9, 2009

lore
Share & tools
lore
#0 lore raw

cover Refactor vgremove and move orphan lock inside library calls

The following 4 patches refactor vgremove and move the orphan lock inside a few library calls. After these patches we will have vgcreate, vgremove, and vgextend library calls.

Passes testsuite and applies on:
commit ac959238aadad88428428decc7adcce663589c98

Author: Milan Broz <[email protected]>
Date:   Thu Jul 9 11:29:41 2009 +0000

Fix and precise metadata read errors for segment areas.

#1 lore raw

PATCH 1/4 Remove unnecessary parameters from vg_remove_single().

Use vg_t instead of struct volume_group.
Should be no functional change.

---
 lib/metadata/metadata-exported.h |    4 +---
 lib/metadata/metadata.c          |    7 +++----
 tools/vgremove.c                 |    3 +--
 3 files changed, 5 insertions(+), 9 deletions(-)
lib/metadata/metadata-exported.h +1 -3
index 5aa0d56..6e085d8 100644
@@ -425,9 +425,7 @@ uint32_t pv_list_extents_free(const struct dm_list *pvh);
 
 vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
 int vg_remove(struct volume_group *vg);
-int vg_remove_single(struct cmd_context *cmd, const char *vg_name,
-		     struct volume_group *vg,
-		     force_t force);
+int vg_remove_single(vg_t *vg, force_t force);
 int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
 	      const char *new_name);
 int vg_extend(struct volume_group *vg, int pv_count, char **pv_names);
lib/metadata/metadata.c +3 -4
index 0c155fd..71cb6ea 100644
@@ -359,15 +359,14 @@ static int remove_lvs_in_vg(struct cmd_context *cmd,
 	return 1;
 }
 
-/* FIXME: remove redundant vg_name */
-int vg_remove_single(struct cmd_context *cmd, const char *vg_name,
-		     struct volume_group *vg,
-		     force_t force __attribute((unused)))
+int vg_remove_single(vg_t *vg, force_t force)
 {
 	struct physical_volume *pv;
 	struct pv_list *pvl;
 	unsigned lv_count;
 	int ret = 1;
+	const char *vg_name = vg->name;
+	struct cmd_context *cmd = vg->cmd;
 
 	if (vg_read_error(vg) || vg_missing_pv_count(vg)) {
 		log_error("Volume group \"%s\" not found, is inconsistent "
tools/vgremove.c +1 -2
index ff35720..8977ea5 100644
@@ -19,8 +19,7 @@ static int vgremove_single(struct cmd_context *cmd, const char *vg_name,
 			   struct volume_group *vg,
 			   void *handle __attribute((unused)))
 {
-	if (!vg_remove_single(cmd, vg_name, vg,
-			      arg_count(cmd, force_ARG)))
+	if (!vg_remove_single(vg, arg_count(cmd, force_ARG)))
 		return ECMD_FAILED;
 
 	return ECMD_PROCESSED;
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/4 Remove force parameter from vg_remove_single, now the liblvm function.

Move check for active LVs outside of library function. The vgremove liblvm function function will fail if there are active LVs. It will be the application's responsibility to check this condition and remove the LVs individually before calling vgremove. Note also that we've duplicated the EXPORTED_VG check in vgremove_single (tools) and vg_remove_single (library). Duplication seemed the only option here since we don't want to do the automatic removal of LVs (in the tools) if the vg is exported, and we still need to protect the library call from removal if the vg is exported.

We still need to deal with the ORPHAN lock but vg_remove_single is now very close to our liblvm function.

TODO: Refactor lvremove in a similar way.

---
 lib/metadata/metadata-exported.h |    6 +++++-
 lib/metadata/metadata.c          |   23 ++++-------------------
 tools/vgremove.c                 |   24 +++++++++++++++++++++++-
 3 files changed, 32 insertions(+), 21 deletions(-)
lib/metadata/metadata-exported.h +5 -1
index 6e085d8..f4a2170 100644
@@ -425,7 +425,7 @@ uint32_t pv_list_extents_free(const struct dm_list *pvh);
 
 vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
 int vg_remove(struct volume_group *vg);
-int vg_remove_single(vg_t *vg, force_t force);
+int vg_remove_single(vg_t *vg);
 int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
 	      const char *new_name);
 int vg_extend(struct volume_group *vg, int pv_count, char **pv_names);
@@ -436,6 +436,10 @@ int vg_set_alloc_policy(vg_t *vg, alloc_policy_t alloc);
 int vg_split_mdas(struct cmd_context *cmd, struct volume_group *vg_from,
 		  struct volume_group *vg_to);
 
+/* FIXME: refactor / unexport when lvremove liblvm refactoring dones */
+int remove_lvs_in_vg(struct cmd_context *cmd,
+		     struct volume_group *vg,
+		     force_t force);
 /*
  * vg_release() must be called on every struct volume_group allocated
  * by vg_create() or vg_read_internal() to free it when no longer required.
lib/metadata/metadata.c +4 -19
index 71cb6ea..5257801 100644
@@ -343,9 +343,9 @@ int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
 	return 1;
 }
 
-static int remove_lvs_in_vg(struct cmd_context *cmd,
-			    struct volume_group *vg,
-			    force_t force)
+int remove_lvs_in_vg(struct cmd_context *cmd,
+		     struct volume_group *vg,
+		     force_t force)
 {
 	struct dm_list *lst;
 	struct lv_list *lvl;
@@ -359,7 +359,7 @@ static int remove_lvs_in_vg(struct cmd_context *cmd,
 	return 1;
 }
 
-int vg_remove_single(vg_t *vg, force_t force)
+int vg_remove_single(vg_t *vg)
 {
 	struct physical_volume *pv;
 	struct pv_list *pvl;
@@ -382,21 +382,6 @@ int vg_remove_single(vg_t *vg, force_t force)
 	lv_count = vg_visible_lvs(vg);
 
 	if (lv_count) {
-		if ((force == PROMPT) &&
-		    (yes_no_prompt("Do you really want to remove volume "
-				   "group \"%s\" containing %u "
-				   "logical volumes? [y/n]: ",
-				   vg_name, lv_count) == 'n')) {
-			log_print("Volume group \"%s\" not removed", vg_name);
-			return 0;
-		}
-		if (!remove_lvs_in_vg(cmd, vg, force))
-			return 0;
-	}
-
-	lv_count = vg_visible_lvs(vg);
-
-	if (lv_count) {
 		log_error("Volume group \"%s\" still contains %u "
 			  "logical volume(s)", vg_name, lv_count);
 		return 0;
tools/vgremove.c +23 -1
index 8977ea5..1bfff1e 100644
@@ -19,7 +19,29 @@ static int vgremove_single(struct cmd_context *cmd, const char *vg_name,
 			   struct volume_group *vg,
 			   void *handle __attribute((unused)))
 {
-	if (!vg_remove_single(vg, arg_count(cmd, force_ARG)))
+	unsigned lv_count;
+	force_t force;
+
+	if (!vg_check_status(vg, EXPORTED_VG))
+		return ECMD_FAILED;
+
+	lv_count = vg_visible_lvs(vg);
+
+	force = arg_count(cmd, force_ARG);
+	if (lv_count) {
+		if ((force == PROMPT) &&
+		    (yes_no_prompt("Do you really want to remove volume "
+				   "group \"%s\" containing %u "
+				   "logical volumes? [y/n]: ",
+				   vg_name, lv_count) == 'n')) {
+			log_print("Volume group \"%s\" not removed", vg_name);
+			return ECMD_FAILED;
+		}
+		if (!remove_lvs_in_vg(cmd, vg, force))
+			return ECMD_FAILED;
+	}
+
+	if (!vg_remove_single(vg))
 		return ECMD_FAILED;
 
 	return ECMD_PROCESSED;
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/4 Move orphan lock inside vg_remove_single.

Move the vg orphan lock inside vg_remove_single, now a complete liblvm function.

---
 lib/metadata/metadata.c |    7 +++++++
 tools/vgremove.c        |    7 -------
 2 files changed, 7 insertions(+), 7 deletions(-)
lib/metadata/metadata.c +7 -0
index 5257801..bbfce76 100644
@@ -390,8 +390,14 @@ int vg_remove_single(vg_t *vg)
 	if (!archive(vg))
 		return 0;
 
+	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
+		log_error("Can't get lock for orphan PVs");
+		return 0;
+	}
+
 	if (!vg_remove(vg)) {
 		log_error("vg_remove %s failed", vg_name);
+		unlock_vg(cmd, VG_ORPHANS);
 		return 0;
 	}
 
@@ -425,6 +431,7 @@ int vg_remove_single(vg_t *vg)
 	else
 		log_error("Volume group \"%s\" not properly removed", vg_name);
 
+	unlock_vg(cmd, VG_ORPHANS);
 	return ret;
 }
 
tools/vgremove.c +0 -7
index 1bfff1e..92c10fe 100644
@@ -56,16 +56,9 @@ int vgremove(struct cmd_context *cmd, int argc, char **argv)
 		return EINVALID_CMD_LINE;
 	}
 
-	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
-		log_error("Can't get lock for orphan PVs");
-		return ECMD_FAILED;
-	}
-
 	ret = process_each_vg(cmd, argc, argv,
 			      READ_FOR_UPDATE,
 			      NULL, &vgremove_single);
 
-	unlock_vg(cmd, VG_ORPHANS);
-
 	return ret;
 }
Signature · 1 line
1.6.0.6
#4 lore raw

PATCH 4/4 Move orphan lock obtain/release inside vg_extend().

With this change we now have vgcreate/vgextend liblvm functions.

---
 lib/metadata/metadata.c |   12 ++++++++++--
 tools/vgcreate.c        |    8 --------
 tools/vgextend.c        |    7 -------
 3 files changed, 10 insertions(+), 17 deletions(-)
lib/metadata/metadata.c +10 -2
index bbfce76..5ff28d5 100644
@@ -439,6 +439,13 @@ int vg_extend(struct volume_group *vg, int pv_count, char **pv_names)
 {
 	int i;
 	struct physical_volume *pv;
+	struct cmd_context *cmd = vg->cmd;
+
+	/* FIXME: orphan lock needs tied to vg handle or inside library call */
+	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
+		log_error("Can't get lock for orphan PVs");
+		return 0;
+	}
 
 	/* attach each pv */
 	for (i = 0; i < pv_count; i++) {
@@ -447,18 +454,19 @@ int vg_extend(struct volume_group *vg, int pv_count, char **pv_names)
 				  "physical volume", pv_names[i]);
 			goto bad;
 		}
-		
 		if (!add_pv_to_vg(vg, pv_names[i], pv))
 			goto bad;
 	}
 
 /* FIXME Decide whether to initialise and add new mdahs to format instance */
 
+	unlock_vg(cmd, VG_ORPHANS);
 	return 1;
-	
+
       bad:
 	log_error("Unable to add physical volume '%s' to "
 		  "volume group '%s'.", pv_names[i], vg->name);
+	unlock_vg(cmd, VG_ORPHANS);
 	return 0;
 }
 
tools/vgcreate.c +0 -8
index 7cfd0f2..c41c50f 100644
@@ -46,12 +46,6 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv)
 	if (validate_vg_create_params(cmd, &vp_new))
 	    return EINVALID_CMD_LINE;
 
-	/* FIXME: orphan lock needs tied to vg handle or inside library call */
-	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
-		log_error("Can't get lock for orphan PVs");
-		return ECMD_FAILED;
-	}
-
 	/* Create the new VG */
 	vg = vg_create(cmd, vp_new.vg_name);
 	if (vg_read_error(vg))
@@ -113,7 +107,6 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv)
 	}
 
 	unlock_vg(cmd, vp_new.vg_name);
-	unlock_vg(cmd, VG_ORPHANS);
 
 	backup(vg);
 
@@ -126,6 +119,5 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv)
 bad:
 	vg_release(vg);
 	unlock_vg(cmd, vp_new.vg_name);
-	unlock_vg(cmd, VG_ORPHANS);
 	return ECMD_FAILED;
 }
tools/vgextend.c +0 -7
index a08d4db..728d9f4 100644
@@ -36,17 +36,11 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
 	argc--;
 	argv++;
 
-	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
-		log_error("Can't get lock for orphan PVs");
-		return ECMD_FAILED;
-	}
-
 	log_verbose("Checking for volume group \"%s\"", vg_name);
 	vg = vg_read_for_update(cmd, vg_name, NULL,
 				READ_REQUIRE_RESIZEABLE);
 	if (vg_read_error(vg)) {
 		vg_release(vg);
-		unlock_vg(cmd, VG_ORPHANS);
 		return ECMD_FAILED;
 	}
 /********** FIXME
@@ -77,6 +71,5 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
 
 error:
 	unlock_and_release_vg(cmd, vg, vg_name);
-	unlock_vg(cmd, VG_ORPHANS);
 	return r;
 }
Signature · 1 line
1.6.0.6