Skip to contentSkip to conversation

Use vgid as well as vgname in lvmcache_update_vg().

PATCH New Dave Wysochanski 2 participants 8 messages Feb 7, 2009 – Feb 17, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use vgid as well as vgname in lvmcache_update_vg().

With duplicate vgnames in a system, vg metadata could get exported to the wrong lvmcache_vginfo structure. Use the vg_uuid as well as the name to lookup the vginfo structure before we store the metadata.

---
 lib/cache/lvmcache.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
lib/cache/lvmcache.c +1 -1
index d24e9a2..083aab1 100644
@@ -1068,7 +1068,7 @@ int lvmcache_update_vg(struct volume_group *vg, unsigned precommitted)
 
 	/* store text representation of vg to cache */
 	if (vg->cmd->current_settings.cache_vgmetadata &&
-	    (vginfo = vginfo_from_vgname(vg->name, NULL)))
+	    (vginfo = vginfo_from_vgname(vg->name, (char *)&vg->id)))
 		_store_metadata(vginfo, vg, precommitted);
 
 	return 1;
Signature · 1 line
1.5.5.1
#1 lore raw

PATCH 1/1 Use vgid as well as vgname in lvmcache_update_vg().

Quoted 4 lines · Dave Wysochanski

With duplicate vgnames in a system, vg metadata could get exported to the wrong lvmcache_vginfo structure. Use the vg_uuid as well as the name to lookup the vginfo structure before we store the metadata.

Did you construct any test case where this actually goes wrong?

Alasdair

Signature · 1 line
agk at redhat.com
#2 lore raw

PATCH 1/1 Use vgid as well as vgname in lvmcache_update_vg().

Quoted 8 lines · Alasdair G Kergon
Quoted 4 lines · Dave Wysochanski

With duplicate vgnames in a system, vg metadata could get exported to the wrong lvmcache_vginfo structure. Use the vg_uuid as well as the name to lookup the vginfo structure before we store the metadata.

Did you construct any test case where this actually goes wrong?

I saw the problem when testing one series of liblvm patches that I posted to lvm-devel. I'm not sure I can hit the problem with the existing tools.

The scenario I was testing involved duplicate vgnames in the system. I tried to open both VGs (different vgids) but noticed that I could only ever open one of them. I traced the bug to this piece of code and submitted the patch.

I've reproduced the problem with my patches but I'm looking at the code more carefully and trying to remember it all.

#3 lore raw

cover Test case for demonstrating lvmcache bug with duplicate vgnames

In reply to Alasdair G Kergon

The following 3 patches should apply cleanly and demonstrate the lvmcache bug. The first patch just adds a couple accessor functions to display vgname and uuid. The second patch is the fix for the bug.
The last patch contains the test code that demonstrates the bug. Applying the first and third patches you should see the bug. Then apply the second patch and rebuild and you should see the bug is removed.

#4 lore raw

PATCH 1/3 Add lvm_vg_name() and lvm_vg_uuid().

---
 lib/metadata/metadata-exported.h |    2 ++
 lib/metadata/metadata.c          |   19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 0 deletions(-)
lib/metadata/metadata-exported.h +2 -0
index a1c45ce..d279b31 100644
@@ -621,6 +621,8 @@ uint32_t pv_pe_size(const pv_t *pv);
 uint64_t pv_pe_start(const pv_t *pv);
 uint32_t pv_pe_count(const pv_t *pv);
 uint32_t pv_pe_alloc_count(const pv_t *pv);
+const char *lvm_vg_name(const vg_t *vg);
+const char *lvm_vg_uuid(vg_t *vg);
 
 int vg_missing_pv_count(const vg_t *vg);
 uint32_t vg_status(const vg_t *vg);
lib/metadata/metadata.c +19 -0
index a2b5f2f..e699ae9 100644
@@ -2673,6 +2673,25 @@ vg_t *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,
 	return vg_read(cmd, vg_name, vgid, flags | READ_FOR_UPDATE);
 }
 
+const char *lvm_vg_name(const vg_t *vg)
+{
+	return vg->name;
+}
+
+/* FIXME: signedness + null termination problem - export proper uuid type */
+const char *lvm_vg_uuid(vg_t *vg)
+{
+	char *value;
+
+	/* FIXME: leak - this is really bad */
+	if (!(value = dm_pool_zalloc(vg->cmd->mem, sizeof(vg->id.uuid)+1)))
+		return NULL;
+	strncpy(value, vg->id.uuid, sizeof(vg->id.uuid));
+	value[sizeof(vg->id.uuid)] = '\0';
+
+	return value;
+}
+
 /*
  * Test the validity of a VG handle returned by vg_read() or vg_read_for_update().
  *
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 2/3 Update lvmcache_update_vg to use vgid as well as vgname.

With duplicate vgnames in a system, vg metadata could get exported to the wrong lvmcache_vginfo structure. Use the vg_uuid as well as the name to lookup the vginfo structure before we store the metadata.

---
 lib/cache/lvmcache.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
lib/cache/lvmcache.c +1 -1
index d24e9a2..083aab1 100644
@@ -1068,7 +1068,7 @@ int lvmcache_update_vg(struct volume_group *vg, unsigned precommitted)
 
 	/* store text representation of vg to cache */
 	if (vg->cmd->current_settings.cache_vgmetadata &&
-	    (vginfo = vginfo_from_vgname(vg->name, NULL)))
+	    (vginfo = vginfo_from_vgname(vg->name, (char *)&vg->id)))
 		_store_metadata(vginfo, vg, precommitted);
 
 	return 1;
Signature · 1 line
1.6.0.6
#6 lore raw

PATCH 3/3 Add duplicate vg test.

This test demonstrates a bug in vg_read() when duplicate vgs are in the system (duplicate vgnames but differing vgids). To demonstrate, you should have 2 such vgs visible in the system. Then compile and run as follows.

Compile (from test/api directory):
gcc ./duplicate-vg-test.c -I../../include -I../../libdm -L../../lib -L../../libdm -llvm -ldevmapper

Run:
./a.out vg1 Y6mFZP5CcjZcpzkjZKqgbBnRAbyGYbH1 vg1 pWSu1RugZH0fIpUzlZimuOIreBC7joCe

You should see output such as the following which demonstrates the bug (note actual/expected uuid
on VG2 indicating the wrong VG has been opened):
WARNING: Duplicate VG name vg1: Existing pWSu1R-ugZH-0fIp-UzlZ-imuO-IreB-C7joCe (created here) takes precedence over Y6mFZP-5Ccj-Zcpz-kjZK-qgbB-nRAb-yGYbH1
VG1 - name: vg1/vg1 (actual/expected) UUID: Y6mFZP5CcjZcpzkjZKqgbBnRAbyGYbH1/Y6mFZP5CcjZcpzkjZKqgbBnRAbyGYbH1 (actual/expected)
VG2 - name: vg1/vg1 (actual/expected) UUID: Y6mFZP5CcjZcpzkjZKqgbBnRAbyGYbH1/pWSu1RugZH0fIpUzlZimuOIreBC7joCe (actual/expected)

Applying the "Update lvmcache_update_vg to use vgid as well as vgname" patch
will produce the expected output:
WARNING: Duplicate VG name vg1: Existing pWSu1R-ugZH-0fIp-UzlZ-imuO-IreB-C7joCe (created here) takes precedence over Y6mFZP-5Ccj-Zcpz-kjZK-qgbB-nRAb-yGYbH1
VG1 - name: vg1/vg1 (actual/expected) UUID: Y6mFZP5CcjZcpzkjZKqgbBnRAbyGYbH1/Y6mFZP5CcjZcpzkjZKqgbBnRAbyGYbH1 (actual/expected)
VG2 - name: vg1/vg1 (actual/expected) UUID: pWSu1RugZH0fIpUzlZimuOIreBC7joCe/pWSu1RugZH0fIpUzlZimuOIreBC7joCe (actual/expected)

---
 test/api/duplicate-vg-test.c |   69 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
 create mode 100644 test/api/duplicate-vg-test.c
test/api/duplicate-vg-test.c new file +69 -0
new file mode 100644
index 0000000..7466f68
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
+ * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License v.2.1.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "libdevmapper.h"
+#include "lvm-types.h"
+#include "metadata.h"
+#include "metadata-exported.h"
+#include "lvm2.h"
+#include "locking.h"
+
+void _test_duplicate_vgs(void *h, int argc, char *argv[])
+{
+	vg_t *vg1, *vg2;
+
+	if (argc <= 4) {
+		printf("Enter vg1name, vg1uuid, vg2name, vg2uuid\n");
+		return;
+	}
+	if (!(vg1 = vg_read(h, argv[1], argv[2], 0))) {
+		printf("Error opening vg1, name=%s, uuid=%s\n",
+			argv[1], argv[2]);
+		return;
+	}
+	if (!(vg2 = vg_read(h, argv[3], argv[4], 0))) {
+		printf("Error opening vg1, name=%s, uuid=%s\n",
+			argv[3], argv[4]);
+		return;
+	}
+	
+	printf("VG1 - name: %s/%s (actual/expected) UUID: %s/%s (actual/expected)\n",
+		lvm_vg_name(vg1), argv[1], lvm_vg_uuid(vg1), argv[2]);
+	printf("VG2 - name: %s/%s (actual/expected) UUID: %s/%s (actual/expected)\n",
+		lvm_vg_name(vg2), argv[3], lvm_vg_uuid(vg2), argv[4]);
+	unlock_vg(h, lvm_vg_name(vg1));
+	unlock_vg(h, lvm_vg_name(vg2));
+}
+
+int main (int argc, char *argv[])
+{
+	void *h;
+
+	h = lvm2_create();
+	if (!h) {
+		printf("Unable to open lvm library instance\n");
+		return 1;
+	}
+	init_locking(1, h);
+
+	_test_duplicate_vgs(h, argc, argv);
+
+	if (h)
+		lvm2_destroy(h);
+	return 0;
+}
+
Signature · 1 line
1.6.0.6
#7 lore raw

cover Test case for demonstrating lvmcache bug with duplicate vgnames

In reply to Dave Wysochanski

Thanks. I've checked in a variation on this, which ensures that the VG metadata is indexed only by the VG uuid it contains. (This should already have been updated in the cache earlier.) I've also added the vgid to the debug message issued when the cache is updated.

Alasdair

Signature · 1 line
agk at redhat.com