Skip to contentSkip to conversation

: Enforce an alphabetical lock ordering for vgname locks.

PATCH New Dave Wysochanski 2 participants 19 messages Aug 13, 2009 – Sep 2, 2009

lore
Share & tools
lore
#0 lore raw

v2 cover : Enforce an alphabetical lock ordering for vgname locks.

This patch series is still WIP but implements the alphabetical lock ordering. Added from v1:
- vgsplit refactor

Still todo
- vgmerge refactor
- liblvm enforcement of rules
- any cleanup

#1 lore raw

PATCH v2 1/9 Enforce an alphabetical lock ordering for vgname locks.

You can test this with a simple vgsplit - e.g.:
vgsplit vg2 vg1 - should fail now
vgsplit vg1 vg2 - should pass

TODO
- fix vgsplit and vgmerge to obey the ordering rules
- cleanup patch if possible

---
 lib/cache/lvmcache.c  |   38 +++++++++++++++++++++++++++++++++++++-
 lib/cache/lvmcache.h  |    1 +
 lib/locking/locking.c |    6 ++++++
 3 files changed, 44 insertions(+), 1 deletions(-)
lib/cache/lvmcache.c +37 -1
index a7889d8..3db9bb9 100644
@@ -186,6 +186,42 @@ void lvmcache_drop_metadata(const char *vgname)
 		_drop_metadata(vgname);
 }
 
+static const char * _compare_lock_name(struct dm_hash_node *n,
+				       const char *vgname)
+{
+	char *vgname2;
+
+	if (!_lock_hash || !dm_hash_get_data(_lock_hash, n))
+		return NULL;
+
+	vgname2 = dm_hash_get_key(_lock_hash, n);
+	/* We don't care about special locks */
+	if (*vgname2 == '#')
+		return NULL;
+
+	if (strcmp(vgname, vgname2) < 0)
+		return vgname2;
+	return NULL;
+}
+
+/*
+ * Enforce alphabetical order of vgname locks
+ */
+int lvmcache_verify_lock_order(const char *vgname)
+{
+	struct dm_hash_node *n;
+	const char *vgname2;
+
+	dm_hash_iterate(n, _lock_hash)
+		if ((vgname2 = _compare_lock_name(n, vgname))) {
+			log_errno(EDEADLK, "Invalid vgname lock order "
+				  "attempted with %s - %s already locked",
+				  vgname, vgname2);
+			return 0;
+		}
+	return 1;
+}
+
 void lvmcache_lock_vgname(const char *vgname, int read_only __attribute((unused)))
 {
 	if (!_lock_hash && !lvmcache_init()) {
@@ -196,7 +232,7 @@ void lvmcache_lock_vgname(const char *vgname, int read_only __attribute((unused)
 	if (dm_hash_lookup(_lock_hash, vgname))
 		log_error("Internal error: Nested locking attempted on VG %s.",
 			  vgname);
-		
+
 	if (!dm_hash_insert(_lock_hash, vgname, (void *) 1))
 		log_error("Cache locking failure for %s", vgname);
 
lib/cache/lvmcache.h +1 -0
index 09edb3c..f1f1065 100644
@@ -84,6 +84,7 @@ int lvmcache_update_vg(struct volume_group *vg, unsigned precommitted);
 
 void lvmcache_lock_vgname(const char *vgname, int read_only);
 void lvmcache_unlock_vgname(const char *vgname);
+int lvmcache_verify_lock_order(const char *vgname);
 
 /* Queries */
 const struct format_type *fmt_from_vgname(const char *vgname, const char *vgid);
lib/locking/locking.c +6 -0
index 40e8601..1217974 100644
@@ -387,6 +387,12 @@ int lock_vol(struct cmd_context *cmd, const char *vol, uint32_t flags)
 		if (!_blocking_supported || vgs_locked())
 			flags |= LCK_NONBLOCK;
 
+		if (vol[0] != '#' &&
+		    ((flags & LCK_TYPE_MASK) != LCK_UNLOCK) &&
+		    (!(flags & LCK_CACHE)) &&
+		    !lvmcache_verify_lock_order(vol))
+			return 0;
+
 		/* Lock VG to change on-disk metadata. */
 		/* If LVM1 driver knows about the VG, it can't be accessed. */
 		if (!check_lvm1_vg_inactive(cmd, vol))
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH v2 2/9 Refactor vgsplit to move existing_vg logic in a separate 'if' statement.

This will aid in future refactorings and allow for us to reorder the source and destination vg based on alphabetical names.
Should be no functional change.

---
 tools/vgsplit.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
tools/vgsplit.c +6 -3
index 35c425a..fd4099f 100644
@@ -290,6 +290,11 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 			goto bad2;
 		}
 
+	} else if (vg_read_error(vg_to) == SUCCESS) {
+		existing_vg = 0;
+	}
+
+	if (existing_vg) {
 		if (new_vg_option_specified(cmd)) {
 			log_error("Volume group \"%s\" exists, but new VG "
 				    "option specified", vg_name_to);
@@ -297,9 +302,7 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 		}
 		if (!vgs_are_compatible(cmd, vg_from,vg_to))
 			goto_bad;
-	} else if (vg_read_error(vg_to) == SUCCESS) {
-		existing_vg = 0;
-
+	} else {
 		vp_def.vg_name = NULL;
 		vp_def.extent_size = vg_from->extent_size;
 		vp_def.max_pv = vg_from->max_pv;
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH v2 3/9 Refactor vgsplit - remove bad2 label.

No functional change.

---
 tools/vgsplit.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
tools/vgsplit.c +2 -2
index fd4099f..8607548 100644
@@ -287,7 +287,8 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 		if (vg_read_error(vg_to)) {
 			vg_release(vg_to);
 			stack;
-			goto bad2;
+			unlock_and_release_vg(cmd, vg_from, vg_name_from);
+			return ECMD_FAILED;
 		}
 
 	} else if (vg_read_error(vg_to) == SUCCESS) {
@@ -427,7 +428,6 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 
 bad:
 	unlock_and_release_vg(cmd, vg_to, vg_name_to);
-bad2:
 	unlock_and_release_vg(cmd, vg_from, vg_name_from);
 	return r;
 }
Signature · 1 line
1.6.0.6
#4 lore raw

PATCH v2 4/9 Refactor vgsplit - add 'lock_vg_from_first' flag.

Introduce 'lock_vg_from_first' flag to retain which vg was locked first. Should be no functional change.

---
 tools/vgsplit.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
tools/vgsplit.c +8 -2
index 8607548..1a9e45e 100644
@@ -218,6 +218,7 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 	int existing_vg = 0;
 	int r = ECMD_FAILED;
 	const char *lv_name;
+	int lock_vg_from_first = 1;
 
 	if ((arg_count(cmd, name_ARG) + argc) < 3) {
 		log_error("Existing VG, new VG and either physical volumes "
@@ -427,7 +428,12 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 	r = ECMD_PROCESSED;
 
 bad:
-	unlock_and_release_vg(cmd, vg_to, vg_name_to);
-	unlock_and_release_vg(cmd, vg_from, vg_name_from);
+	if (lock_vg_from_first) {
+		unlock_and_release_vg(cmd, vg_to, vg_name_to);
+		unlock_and_release_vg(cmd, vg_from, vg_name_from);
+	} else {
+		unlock_and_release_vg(cmd, vg_from, vg_name_from);
+		unlock_and_release_vg(cmd, vg_to, vg_name_to);
+	}
 	return r;
 }
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH v2 5/9 Refactor vgsplit - create _vgsplit_to function which creates or opens dest vg.

Move the creating/opening of the destination vg into its own function so later we can reorder the source / destination vg opening based on the alphabetical lock order rule.

Should be no functional change but code is a bit tricky.

---
 tools/vgsplit.c |   79 +++++++++++++++++++++++++++++++++---------------------
 1 files changed, 48 insertions(+), 31 deletions(-)
tools/vgsplit.c +48 -31
index 1a9e45e..508d547 100644
@@ -198,6 +198,52 @@ static int _move_mirrors(struct volume_group *vg_from,
 }
 
 /*
+ * Create or open the destination of the vgsplit operation.
+ * Returns
+ * - non-NULL: VG handle w/VG lock held
+ * - NULL: no VG lock held
+ */
+static struct volume_group *_vgsplit_to(struct cmd_context *cmd,
+					const char *vg_name_to,
+					int *existing_vg)
+{
+	struct volume_group *vg_to = NULL;
+
+	log_verbose("Checking for new volume group \"%s\"", vg_name_to);
+	/*
+	 * First try to create a new VG.  If we cannot create it,
+	 * and we get FAILED_EXIST (we will not be holding a lock),
+	 * a VG must already exist with this name.  We then try to
+	 * read the existing VG - the vgsplit will be into an existing VG.
+	 *
+	 * Otherwise, if the lock was successful, it must be the case that
+	 * we obtained a WRITE lock and could not find the vgname in the
+	 * system.  Thus, the split will be into a new VG.
+	 */
+	vg_to = vg_create(cmd, vg_name_to);
+	if (vg_read_error(vg_to) == FAILED_LOCKING) {
+		log_error("Can't get lock for %s", vg_name_to);
+		vg_release(vg_to);
+		return NULL;
+	}
+	if (vg_read_error(vg_to) == FAILED_EXIST) {
+		*existing_vg = 1;
+		vg_release(vg_to);
+		vg_to = vg_read_for_update(cmd, vg_name_to, NULL, 0);
+
+		if (vg_read_error(vg_to)) {
+			vg_release(vg_to);
+			stack;
+			return NULL;
+		}
+
+	} else if (vg_read_error(vg_to) == SUCCESS) {
+		*existing_vg = 0;
+	}
+	return vg_to;
+}
+
+/*
  * Has the user given an option related to a new vg as the split destination?
  */
 static int new_vg_option_specified(struct cmd_context *cmd)
@@ -262,40 +308,11 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 	 */
 	cmd->fmt = vg_from->fid->fmt;
 
-	log_verbose("Checking for new volume group \"%s\"", vg_name_to);
-	/*
-	 * First try to create a new VG.  If we cannot create it,
-	 * and we get FAILED_EXIST (we will not be holding a lock),
-	 * a VG must already exist with this name.  We then try to
-	 * read the existing VG - the vgsplit will be into an existing VG.
-	 *
-	 * Otherwise, if the lock was successful, it must be the case that
-	 * we obtained a WRITE lock and could not find the vgname in the
-	 * system.  Thus, the split will be into a new VG.
-	 */
-	vg_to = vg_create(cmd, vg_name_to);
-	if (vg_read_error(vg_to) == FAILED_LOCKING) {
-		log_error("Can't get lock for %s", vg_name_to);
-		vg_release(vg_to);
+	vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
+	if (!vg_to) {
 		unlock_and_release_vg(cmd, vg_from, vg_name_from);
 		return ECMD_FAILED;
 	}
-	if (vg_read_error(vg_to) == FAILED_EXIST) {
-		existing_vg = 1;
-		vg_release(vg_to);
-		vg_to = vg_read_for_update(cmd, vg_name_to, NULL, 0);
-
-		if (vg_read_error(vg_to)) {
-			vg_release(vg_to);
-			stack;
-			unlock_and_release_vg(cmd, vg_from, vg_name_from);
-			return ECMD_FAILED;
-		}
-
-	} else if (vg_read_error(vg_to) == SUCCESS) {
-		existing_vg = 0;
-	}
-
 	if (existing_vg) {
 		if (new_vg_option_specified(cmd)) {
 			log_error("Volume group \"%s\" exists, but new VG "
Signature · 1 line
1.6.0.6
#6 lore raw

PATCH v2 6/9 Refactor vgsplit - create _vgsplit_from function to open source vg.

Should be no functional change and allows future reorder of source and dest vgs.

---
 tools/vgsplit.c |   29 +++++++++++++++++++++++------
 1 files changed, 23 insertions(+), 6 deletions(-)
tools/vgsplit.c +23 -6
index 508d547..bfcd7f5 100644
@@ -244,6 +244,27 @@ static struct volume_group *_vgsplit_to(struct cmd_context *cmd,
 }
 
 /*
+ * Open the source of the vgsplit operation.
+ * Returns
+ * - non-NULL: VG handle w/VG lock held
+ * - NULL: no VG lock held
+ */
+static struct volume_group *_vgsplit_from(struct cmd_context *cmd,
+					  const char *vg_name_from)
+{
+	struct volume_group *vg_from;
+
+	log_verbose("Checking for volume group \"%s\"", vg_name_from);
+
+	vg_from = vg_read_for_update(cmd, vg_name_from, NULL, 0);
+	if (vg_read_error(vg_from)) {
+		vg_release(vg_from);
+		return NULL;
+	}
+	return vg_from;
+}
+
+/*
  * Has the user given an option related to a new vg as the split destination?
  */
 static int new_vg_option_specified(struct cmd_context *cmd)
@@ -293,13 +314,9 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 		return ECMD_FAILED;
 	}
 
-	log_verbose("Checking for volume group \"%s\"", vg_name_from);
-
-	vg_from = vg_read_for_update(cmd, vg_name_from, NULL, 0);
-	if (vg_read_error(vg_from)) {
-		vg_release(vg_from);
+	vg_from = _vgsplit_from(cmd, vg_name_from);
+	if (!vg_from)
 		return ECMD_FAILED;
-	}
 
 	/*
 	 * Set metadata format of original VG.
Signature · 1 line
1.6.0.6
#7 lore raw

PATCH v2 7/9 Refactor vgsplit: _vgsplit_from or _vgsplit_to failure cleanup depends on order.

Should be no functional change.

---
 tools/vgsplit.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
tools/vgsplit.c +6 -2
index bfcd7f5..7fe39e9 100644
@@ -315,8 +315,11 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 	}
 
 	vg_from = _vgsplit_from(cmd, vg_name_from);
-	if (!vg_from)
+	if (!vg_from) {
+		if (!lock_vg_from_first)
+			unlock_and_release_vg(cmd, vg_to, vg_name_to);
 		return ECMD_FAILED;
+	}
 
 	/*
 	 * Set metadata format of original VG.
@@ -327,7 +330,8 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 
 	vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
 	if (!vg_to) {
-		unlock_and_release_vg(cmd, vg_from, vg_name_from);
+		if (lock_vg_from_first)
+			unlock_and_release_vg(cmd, vg_from, vg_name_from);
 		return ECMD_FAILED;
 	}
 	if (existing_vg) {
Signature · 1 line
1.6.0.6
#8 lore raw

PATCH v2 8/9 Refactor vgsplit - reorder _vgsplit_from and _vgsplit_to based on flag.

Should be no functional change but there is one bug. If we open the destination first, we cannot know the 'fmt'. So in this case we rely on the user specifying the value in the --metadatatype variable if the destination is a new VG. This should at least be noted in the man page or perhaps a warning should be printed. Other options don't seem attractive:
- open vg_from a second time
- defer actual creation until later but just obtain the locks in the right order

---
 tools/vgsplit.c |   45 ++++++++++++++++++++++++++++-----------------
 1 files changed, 28 insertions(+), 17 deletions(-)
tools/vgsplit.c +28 -17
index 7fe39e9..ef1ab2c 100644
@@ -314,26 +314,37 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 		return ECMD_FAILED;
 	}
 
-	vg_from = _vgsplit_from(cmd, vg_name_from);
-	if (!vg_from) {
-		if (!lock_vg_from_first)
+	if (lock_vg_from_first) {
+		vg_from = _vgsplit_from(cmd, vg_name_from);
+		if (!vg_from)
+			return ECMD_FAILED;
+		/*
+		 * Set metadata format of original VG.
+		 * NOTE: We must set the format before calling vg_create()
+		 * since vg_create() calls the per-format constructor.
+		 */
+		cmd->fmt = vg_from->fid->fmt;
+
+		vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
+		if (!vg_to) {
+			unlock_and_release_vg(cmd, vg_from, vg_name_from);
+			return ECMD_FAILED;
+		}
+	} else {
+		/* FIXME: in this case we don't know the vg_from->fid->fmt */
+		if (!arg_count(cmd, metadatatype_ARG))
+			log_warn("Unable to set format based on "
+				 "source VG format");
+		vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
+		if (!vg_to)
+			return ECMD_FAILED;
+		vg_from = _vgsplit_from(cmd, vg_name_from);
+		if (!vg_from) {
 			unlock_and_release_vg(cmd, vg_to, vg_name_to);
-		return ECMD_FAILED;
+			return ECMD_FAILED;
+		}
 	}
 
-	/*
-	 * Set metadata format of original VG.
-	 * NOTE: We must set the format before calling vg_create()
-	 * since vg_create() calls the per-format constructor.
-	 */
-	cmd->fmt = vg_from->fid->fmt;
-
-	vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
-	if (!vg_to) {
-		if (lock_vg_from_first)
-			unlock_and_release_vg(cmd, vg_from, vg_name_from);
-		return ECMD_FAILED;
-	}
 	if (existing_vg) {
 		if (new_vg_option_specified(cmd)) {
 			log_error("Volume group \"%s\" exists, but new VG "
Signature · 1 line
1.6.0.6
#9 lore raw

PATCH v2 9/9 Change vgsplit behavior to open/lock first vg based on alphabetical ordering.

If the destination vgname comes before the source vgname, we must open the destination first because of the locking rules. Thus, do a strcmp and set the flag based on the comparison.

---
 tools/vgsplit.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
tools/vgsplit.c +3 -0
index ef1ab2c..a8e525d 100644
@@ -314,6 +314,9 @@ int vgsplit(struct cmd_context *cmd, int argc, char **argv)
 		return ECMD_FAILED;
 	}
 
+	if (strcmp(vg_name_to, vg_name_from) < 0)
+		lock_vg_from_first = 0;
+
 	if (lock_vg_from_first) {
 		vg_from = _vgsplit_from(cmd, vg_name_from);
 		if (!vg_from)
Signature · 1 line
1.6.0.6
#10 lore raw

PATCH v2 8/9 Refactor vgsplit - reorder _vgsplit_from and _vgsplit_to based on flag.

In reply to Dave Wysochanski
Quoted 4 lines · Dave Wysochanski

Should be no functional change but there is one bug. If we open the destination first, we cannot know the 'fmt'. So in this case we rely on the user specifying the value in the --metadatatype variable if the destination is a new VG.

That's OK because for most people now, it's always lvm2 format and the default is correct, and everyone else quickly learns they need to use that option sometimes.

tools/vgsplit.c int vgsplit(struct cmd_context *cmd, int argc, char **argv) · line 337

Quoted 4 lines
tools/vgsplit.c +4 -0
+		/* FIXME: in this case we don't know the vg_from->fid->fmt */
+		if (!arg_count(cmd, metadatatype_ARG))
+			log_warn("Unable to set format based on "
+				 "source VG format");

As elsewhere in the code, delete those lines and just use cmd->fmt directly.

Later, once you *do* know the correct format, verify it matches and if it doesn't, abort with an error telling people to use that option.

Alasdair

#11 lore raw

PATCH v2 1/9 Enforce an alphabetical lock ordering for vgname locks.

In reply to Dave Wysochanski

lib/cache/lvmcache.c void lvmcache_drop_metadata(const char *vgname) · line 187

Quoted 5 lines · Dave Wysochanski
lib/cache/lvmcache.c +0 -0
@@ -186,6 +186,42 @@ void lvmcache_drop_metadata(const char *vgname)
 		_drop_metadata(vgname);
 }

Please document what this function returns here. (Normally you'd expect a compare fn to return an integer.)

lib/cache/lvmcache.c void lvmcache_drop_metadata(const char *vgname) · line 208

Quoted 6 lines
lib/cache/lvmcache.c +5 -0
+static const char * _compare_lock_name(struct dm_hash_node *n,
+				       const char *vgname)
+{
 
+/*
+ * Enforce alphabetical order of vgname locks

* Ensure VG locks are acquired in alphabetical order.

lib/cache/lvmcache.c void lvmcache_drop_metadata(const char *vgname) · line 219

Quoted 7 lines
lib/cache/lvmcache.c +6 -0
+ */
+int lvmcache_verify_lock_order(const char *vgname)
 
+		if ((vgname2 = _compare_lock_name(n, vgname))) {
+			log_errno(EDEADLK, "Invalid vgname lock order "
+				  "attempted with %s - %s already locked",
+				  vgname, vgname2);
Internal error: VG lock %s must be requested before %s, not after.

lib/locking/locking.c int lock_vol(struct cmd_context *cmd, const char *vol, uint32_t flags) · line 392

Quoted 9 lines
lib/locking/locking.c +5 -0
@@ -387,6 +387,12 @@ int lock_vol(struct cmd_context *cmd, const char *vol, uint32_t flags)
 
+		if (vol[0] != '#' &&
+		    ((flags & LCK_TYPE_MASK) != LCK_UNLOCK) &&
+		    (!(flags & LCK_CACHE)) &&
+		    !lvmcache_verify_lock_order(vol))
+			return 0;

return_0;

Alasdair

#12 lore raw

PATCH 10/13 Refactor vgmerge - introduce lock_vg_from_first flag.

In reply to Dave Wysochanski

Should be no functional change.

---
 tools/vgmerge.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
tools/vgmerge.c +8 -2
index 37f6bd7..39032fb 100644
@@ -21,6 +21,7 @@ static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
 	struct volume_group *vg_to, *vg_from;
 	struct lv_list *lvl1, *lvl2;
 	int r = ECMD_FAILED;
+	int lock_vg_from_first = 0;
 
 	if (!strcmp(vg_name_to, vg_name_from)) {
 		log_error("Duplicate volume group name \"%s\"", vg_name_from);
@@ -116,8 +117,13 @@ static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
 		  vg_from->name, vg_to->name);
 	r = ECMD_PROCESSED;
 bad:
-	unlock_and_release_vg(cmd, vg_from, vg_name_from);
-	unlock_and_release_vg(cmd, vg_to, vg_name_to);
+	if (lock_vg_from_first) {
+		unlock_and_release_vg(cmd, vg_to, vg_name_to);
+		unlock_and_release_vg(cmd, vg_from, vg_name_from);
+	} else {
+		unlock_and_release_vg(cmd, vg_from, vg_name_from);
+		unlock_and_release_vg(cmd, vg_to, vg_name_to);
+	}
 	return r;
 }
 
Signature · 1 line
1.6.0.6
#13 lore raw

PATCH 11/13 Refactor vgmerge - create _vgmerge_from and _vgmerge_to.

These functions are really identical but for clarity I made them separate functions in this patch.

Should be no functional change.

---
 tools/vgmerge.c |   40 +++++++++++++++++++++++++++++++---------
 1 files changed, 31 insertions(+), 9 deletions(-)
tools/vgmerge.c +31 -9
index 39032fb..4dbcd61 100644
@@ -15,6 +15,33 @@
 
 #include "tools.h"
 
+static struct volume_group *_vgmerge_from(struct cmd_context *cmd,
+					  const char *vg_name_from)
+{
+	struct volume_group *vg_from;
+	log_verbose("Checking for volume group \"%s\"", vg_name_from);
+	vg_from = vg_read_for_update(cmd, vg_name_from, NULL, 0);
+	if (vg_read_error(vg_from)) {
+		vg_release(vg_from);
+		return NULL;
+	}
+	return vg_from;
+}
+
+static struct volume_group *_vgmerge_to(struct cmd_context *cmd,
+					const char *vg_name_to)
+{
+	struct volume_group *vg_to;
+
+	log_verbose("Checking for volume group \"%s\"", vg_name_to);
+	vg_to = vg_read_for_update(cmd, vg_name_to, NULL, 0);
+	if (vg_read_error(vg_to)) {
+		vg_release(vg_to);
+		return NULL;
+	}
+	return vg_to;
+}
+
 static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
 			   const char *vg_name_from)
 {
@@ -28,17 +55,12 @@ static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
 		return ECMD_FAILED;
 	}
 
-	log_verbose("Checking for volume group \"%s\"", vg_name_to);
-	vg_to = vg_read_for_update(cmd, vg_name_to, NULL, 0);
-	if (vg_read_error(vg_to)) {
-		vg_release(vg_to);
+	vg_to = _vgmerge_to(cmd, vg_name_to);
+	if (!vg_to)
 		return ECMD_FAILED;
-	}
 
-	log_verbose("Checking for volume group \"%s\"", vg_name_from);
-	vg_from = vg_read_for_update(cmd, vg_name_from, NULL, 0);
-	if (vg_read_error(vg_from)) {
-		vg_release(vg_from);
+	vg_from = _vgmerge_from(cmd, vg_name_from);
+	if (!vg_from) {
 		unlock_and_release_vg(cmd, vg_to, vg_name_to);
 		return ECMD_FAILED;
 	}
Signature · 1 line
1.6.0.6
#14 lore raw

PATCH 12/13 Refactor vgmerge - combine _vgmerge_to and _vgmerge_from into _vgmerge_vg_read.

These functions are identical so should be no functional change.

---
 tools/vgmerge.c |   34 ++++++++++------------------------
 1 files changed, 10 insertions(+), 24 deletions(-)
tools/vgmerge.c +10 -24
index 4dbcd61..9e38e6c 100644
@@ -15,31 +15,17 @@
 
 #include "tools.h"
 
-static struct volume_group *_vgmerge_from(struct cmd_context *cmd,
-					  const char *vg_name_from)
+static struct volume_group *_vgmerge_vg_read(struct cmd_context *cmd,
+					     const char *vg_name)
 {
-	struct volume_group *vg_from;
-	log_verbose("Checking for volume group \"%s\"", vg_name_from);
-	vg_from = vg_read_for_update(cmd, vg_name_from, NULL, 0);
-	if (vg_read_error(vg_from)) {
-		vg_release(vg_from);
+	struct volume_group *vg;
+	log_verbose("Checking for volume group \"%s\"", vg_name);
+	vg = vg_read_for_update(cmd, vg_name, NULL, 0);
+	if (vg_read_error(vg)) {
+		vg_release(vg);
 		return NULL;
 	}
-	return vg_from;
-}
-
-static struct volume_group *_vgmerge_to(struct cmd_context *cmd,
-					const char *vg_name_to)
-{
-	struct volume_group *vg_to;
-
-	log_verbose("Checking for volume group \"%s\"", vg_name_to);
-	vg_to = vg_read_for_update(cmd, vg_name_to, NULL, 0);
-	if (vg_read_error(vg_to)) {
-		vg_release(vg_to);
-		return NULL;
-	}
-	return vg_to;
+	return vg;
 }
 
 static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
@@ -55,11 +41,11 @@ static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
 		return ECMD_FAILED;
 	}
 
-	vg_to = _vgmerge_to(cmd, vg_name_to);
+	vg_to = _vgmerge_vg_read(cmd, vg_name_to);
 	if (!vg_to)
 		return ECMD_FAILED;
 
-	vg_from = _vgmerge_from(cmd, vg_name_from);
+	vg_from = _vgmerge_vg_read(cmd, vg_name_from);
 	if (!vg_from) {
 		unlock_and_release_vg(cmd, vg_to, vg_name_to);
 		return ECMD_FAILED;
Signature · 1 line
1.6.0.6
#15 lore raw

PATCH 13/13 Change vgmerge behavior to open/lock first vg based on alphabetical ordering.

This enforces our alphabetical lock ordering rules for vgmerge.

---
 tools/vgmerge.c |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)
tools/vgmerge.c +21 -7
index 9e38e6c..e147d27 100644
@@ -41,14 +41,28 @@ static int _vgmerge_single(struct cmd_context *cmd, const char *vg_name_to,
 		return ECMD_FAILED;
 	}
 
-	vg_to = _vgmerge_vg_read(cmd, vg_name_to);
-	if (!vg_to)
-		return ECMD_FAILED;
+	if (strcmp(vg_name_to, vg_name_from) > 0)
+		lock_vg_from_first = 1;
 
-	vg_from = _vgmerge_vg_read(cmd, vg_name_from);
-	if (!vg_from) {
-		unlock_and_release_vg(cmd, vg_to, vg_name_to);
-		return ECMD_FAILED;
+	if (lock_vg_from_first) {
+		vg_from = _vgmerge_vg_read(cmd, vg_name_from);
+		if (!vg_from)
+			return ECMD_FAILED;
+		vg_to = _vgmerge_vg_read(cmd, vg_name_to);
+		if (!vg_to) {
+			unlock_and_release_vg(cmd, vg_from, vg_name_from);
+			return ECMD_FAILED;
+		}
+	} else {
+		vg_to = _vgmerge_vg_read(cmd, vg_name_to);
+		if (!vg_to)
+			return ECMD_FAILED;
+
+		vg_from = _vgmerge_vg_read(cmd, vg_name_from);
+		if (!vg_from) {
+			unlock_and_release_vg(cmd, vg_to, vg_name_to);
+			return ECMD_FAILED;
+		}
 	}
 
 	if (!vgs_are_compatible(cmd, vg_from, vg_to))
Signature · 1 line
1.6.0.6
#16 lore raw

PATCH 1/1 : Prototype vgsplit and vgmerge for alphabetical VG lock

In reply to Dave Wysochanski

This patch just shows how liblvm would be restricted and vgsplit/vgmerge defined for the new alphabetical lock ordering. Note that this is one option for implementing the restriction - the other one is to allow multiple VGs open but require the application to open in the correct order. I suppose a 3rd option would be implicitly releasing / reacquiring VG locks, but then we'd have to handle the fallout from locking failure and/or blocking.

#17 lore raw

PATCH 14/14 Update lvm2app.h prototype to describe restrictions for alphabetical locks.

If we move to the alphabetical lock ordering mandate within LVM, this will impact liblvm. One way to make liblvm comply is to only allow an application to create/open a single VG at any time. The other option is to allow an application to have multiple VGs open, and fail the calls if they get the ordering wrong. I suppose a third approach is a slight variation on the second one, with liblvm handling lock release and reobtain with the correct order. This patch takes the first approach.

The implications of taking this approach is now we have a hybrid object/cmdline liblvm model for some operations. Operations involving more than 1 VG are probably not in the 20% of the 80/20 rule, so perhaps not a big deal. We will have to deal with all the commandline options in these calls. Fortunately, commandline options that would create further complexity are limited to vgsplit (vgmerge is trivial). Thus, we impose further restrictions on vgsplit:
1. You must split into an existing VG. For the most part this is fine, but it does cause a problem if the new VG was to only contain devices from the source VG. We could actually support this if we allowed multiple VGs to be open at a time, and allowed the vgcreate call to go through, followed by a vgsplit. Then we'd have to handle implicit lock re-ordering though. With this patch's approach, split into a new VG is just not supported.
2. The -n option will be supported in a separate liblvm call(s). For this one, we could provide an API to return a list of devices associated with one or more LVs, or we could just leave this to the application for now.

A final word of caution if we take this approach. At this point it is unclear to me that vgsplit/vgmerge would be the only multi-VG operations we'd ever want to support. So we could find ourselves creating another non-object based function in the future. While I'm not really keen on trying to plan/forsee the unknowable, this seems to be a real possibility to consider with this design approach, unless we are fairly certain vgsplit/vgmerge would cover the multi-vg operations.

---
 liblvm/lvm2app.h |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
liblvm/lvm2app.h +50 -0
index 02b5d76..05e4f5e 100644
@@ -61,6 +61,13 @@
  * close the VG and re-open with WRITE permission. It should then check
  * whether the original "vg_seqno" obtained with READ permission matches
  * the new one obtained with WRITE permission.
+ *
+ * Limitations
+ * 1. Only one VG allowed open at any time.
+ * The LVM library currently enforces the restriction of a single VG open at
+ * a time.  This rule is enforced by failing any function that opens or
+ * creates a VG.  The list includes lvm_vg_open, lvm_vg_create, lvm_vg_split,
+ * and lvm_vg_merge.
  */
 
 /**
@@ -462,6 +469,49 @@ int lvm_vg_extend(vg_t vg, const char *device);
 int lvm_vg_reduce(vg_t vg, const char *device);
 
 /**
+ * Split a VG into a second VG by moving devices (and any LVs) to the new VG.
+ *
+ * This function will split an existing VG into a second VG by moving devices
+ * from the source VG to the destination.  Note that no storage is copied
+ * during the move, so whole LVs must be moved at a time.
+ *
+ * \param   vg_name_src
+ * VG handle obtained from lvm_vg_create or lvm_vg_open.
+ *
+ * \param   vg_name_dest
+ * Name of new volume group to split into.  The VG may be a new or existing VG.
+ *
+ * \param   devices
+ * Array of device names to move from vg_name_src to vg_name_dest.
+ *
+ * \param   device_count
+ * Count of devices to move.
+ *
+ * \return
+ * 0 (success) or -1 (failure).
+ */
+int lvm_vg_split(const char *vg_name_src, const char *vg_name_dest,
+		 const char **devices, uint32_t device_count);
+
+/**
+ * Merge two VGs into a single VG.
+ *
+ * This function merges an existing source VG into a destination VG by moving
+ * devices from the source to the destination.
+ *
+ * \param   vg_name_src
+ * VG handle obtained from lvm_vg_create or lvm_vg_open.
+ *
+ * \param   vg_name_dest
+ * Name of new volume group to split into.  The VG may be a new or existing VG.
+ *
+ * \return
+ * 0 (success) or -1 (failure).
+ */
+int lvm_vg_merge(const char *vg_name_src, const char *vg_name_dest);
+
+
+/**
  * Set the extent size of a VG.
  *
  * This function requires calling lvm_vg_write to commit the change to disk.
Signature · 1 line
1.6.0.6
#18 lore raw

v2 cover : Enforce an alphabetical lock ordering for vgname locks.

In reply to Dave Wysochanski
Quoted 1 line · Dave Wysochanski

This patch series is still WIP but implements the alphabetical lock ordering.

Please add the requirement to the locking documentation in locking.h.

* LCK_VG:
...
* If more than one lock needs to be held simultaneously, they must be
* acquired in alphabetical order of 'vol' (to avoid deadlocks).

Alasdair