Skip to contentSkip to conversation

Add infrastructure for cleaner pv handling, v7

PATCH New Dave Wysochanski 1 participant 7 messages May 11, 2010

lore
Share & tools
lore
#0 lore raw

cover Add infrastructure for cleaner pv handling, v7

This patchset replaces the last patchset submitted on Wed, 28 Apr 2010 11:55:37.

Quoted 1 line

From now on, a user that wants to get/set a PV property and

knows only the pvname must first lookup the vgname, then obtain a vg handle, and finally use the pv handle to get/set the property.

Early patches add internal functions to lookup a vgname from a device name and pvid, respecting the order of lookup. In cases where we lookup by pvid or there are no mdas, we may need to do increasingly expensive scans to provide the answer. Once the internal functions are given, pvchange is reworked to use this new functionality, and lvm2app interfaces are added to lookup a vgname from a pvname and pvid, which will be useful for udisks and other applications that may not know the vgname but want information on a specific device/PV. The new functionality is tested by running the nightly tests and some lvm2app specific tests.

#1 lore raw

PATCH 1/6 Add pvid_from_devname() lvmcache function.

Add supporting function for mappings from devname -> pvid -> vgname.

---
 lib/cache/lvmcache.c |   19 +++++++++++++++++++
 lib/cache/lvmcache.h |    2 ++
 2 files changed, 21 insertions(+), 0 deletions(-)
lib/cache/lvmcache.c +19 -0
index 0e31a4e..2a5f2d7 100644
@@ -747,6 +747,25 @@ struct device *device_from_pvid(struct cmd_context *cmd, struct id *pvid,
 	return NULL;
 }
 
+const char *pvid_from_devname(struct cmd_context *cmd,
+			      const char *devname)
+{
+	struct device *dev;
+	struct label *label;
+
+	if (!(dev = dev_cache_get(devname, cmd->filter))) {
+		log_error("%s: Couldn't find device.  Check your filters?",
+			  devname);
+		return NULL;
+	}
+
+	if (!(label_read(dev, &label, UINT64_C(0))))
+		return NULL;
+
+	return dev->pvid;
+}
+
+
 static int _free_vginfo(struct lvmcache_vginfo *vginfo)
 {
 	struct lvmcache_vginfo *primary_vginfo, *vginfo2;
lib/cache/lvmcache.h +2 -0
index 8eeb495..5b0ca73 100644
@@ -95,6 +95,8 @@ struct lvmcache_info *info_from_pvid(const char *pvid, int valid_only);
 const char *vgname_from_vgid(struct dm_pool *mem, const char *vgid);
 struct device *device_from_pvid(struct cmd_context *cmd, struct id *pvid,
 				unsigned *scan_done_once);
+const char *pvid_from_devname(struct cmd_context *cmd,
+			      const char *dev_name);
 int vgs_locked(void);
 int vgname_is_locked(const char *vgname);
 
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/6 Add lvmcache function to lookup a vgname from a pvid.

---
 lib/cache/lvmcache.c |   25 +++++++++++++++++++++++++
 lib/cache/lvmcache.h |    1 +
 2 files changed, 26 insertions(+), 0 deletions(-)
lib/cache/lvmcache.c +25 -0
index 2a5f2d7..bcff43d 100644
@@ -499,6 +499,31 @@ struct lvmcache_info *info_from_pvid(const char *pvid, int valid_only)
 	return info;
 }
 
+char *lvmcache_vgname_from_pvid(struct cmd_context *cmd, const char *pvid)
+{
+	struct lvmcache_info *info;
+	char *vgname;
+
+	if (!device_from_pvid(cmd, pvid, NULL)) {
+		char buffer[64] __attribute((aligned(8)));
+
+		if (!id_write_format(pvid, buffer, sizeof(buffer)))
+			buffer[0] = '\0';
+		log_error("Couldn't find device with uuid %s.", buffer);
+		return NULL;
+	}
+
+	info = info_from_pvid(pvid, 0);
+	if (!info)
+		return_NULL;
+
+	if (!(vgname = dm_pool_strdup(cmd->mem, info->vginfo->vgname))) {
+		log_errno(ENOMEM, "vgname allocation failed");
+		return NULL;
+	}
+	return vgname;
+}
+
 static void _rescan_entry(struct lvmcache_info *info)
 {
 	struct label *label;
lib/cache/lvmcache.h +1 -0
index 5b0ca73..55c2e7a 100644
@@ -97,6 +97,7 @@ struct device *device_from_pvid(struct cmd_context *cmd, struct id *pvid,
 				unsigned *scan_done_once);
 const char *pvid_from_devname(struct cmd_context *cmd,
 			      const char *dev_name);
+char *lvmcache_vgname_from_pvid(struct cmd_context *cmd, const char *pvid);
 int vgs_locked(void);
 int vgname_is_locked(const char *vgname);
 
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/6 Add find_vgname_from_{pvname|pvid} functions.

Some commands start with a pvname, but we'd like to force users to start with a vg handle to obtain a pv handle. Our best option seems to be providing a way to look up the vgname from the pvname, and then require them to use vg_read/vg_open.

In addition to the pvname lookup function, this patch also provides a lookup by pvid. The lookup by pvid can be used in conjunction with lvmcache_get_pvids to process all pvs in the system.

The pvid find function first calls lvmcache_vgname_from_pvid, which may cause the label to be read if it is not in the cache. If the vgname is returned is an orphan, we then check to see if there are metadata areas, and if not, we scan every PV on the system by calling scan_vgs_for_pvs(). In most cases we should not need to do this, and by using the info->mdas count, we avoid calling pv_read() as prior code did. So this patch is a bit cleaner and should allow us to refactor more of the pv code.

---
 lib/metadata/metadata-exported.h |    4 +++
 lib/metadata/metadata.c          |   46 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 0 deletions(-)
lib/metadata/metadata-exported.h +4 -0
index 3c5f8a3..b3d6a06 100644
@@ -624,6 +624,10 @@ struct logical_volume *find_lv(const struct volume_group *vg,
 struct physical_volume *find_pv_by_name(struct cmd_context *cmd,
 					const char *pv_name);
 
+const char *find_vgname_from_pvname(struct cmd_context *cmd,
+				    const char *pvname);
+const char *find_vgname_from_pvid(struct cmd_context *cmd,
+				  const char *pvid);
 /* Find LV segment containing given LE */
 struct lv_segment *first_seg(const struct logical_volume *lv);
 
lib/metadata/metadata.c +46 -0

52 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#4 lore raw

PATCH 4/6 Update pvchange to always obtain a vg handle for each pv to process.

Earlier patches added some infrastructure to lookup a vgname from a pvname. We now can cleanup some of the pvchange and other code by requiring callers that want to modify some pv property:
1) lookup the vgname by the pvname
2) use the vgname to obtain a vg handle
3) get the pv handle from the vg handle

This should work going forward and be a much cleaner interface, as we move away from pvs as standalone objects.

---
 tools/pvchange.c |  122 ++++++++++++++++++++++++-----------------------------
 1 files changed, 55 insertions(+), 67 deletions(-)
tools/pvchange.c +55 -67
index cba9dd2..cfec448 100644
@@ -17,13 +17,10 @@
 
 /* FIXME Locking.  PVs in VG. */
 
-static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
+static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg,
+			    struct physical_volume *pv,
 			    void *handle __attribute((unused)))
 {
-	struct volume_group *vg = NULL;
-	const char *vg_name = NULL;
-	struct pv_list *pvl;
-	uint64_t sector;
 	uint32_t orig_pe_alloc_count;
 	/* FIXME Next three only required for format1. */
 	uint32_t orig_pe_count, orig_pe_size;
@@ -53,21 +50,6 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
 
 	/* If in a VG, must change using volume group. */
 	if (!is_orphan(pv)) {
-		vg_name = pv_vg_name(pv);
-
-		log_verbose("Finding volume group %s of physical volume %s",
-			    vg_name, pv_name);
-		vg = vg_read_for_update(cmd, vg_name, NULL, 0);
-		if (vg_read_error(vg)) {
-			vg_release(vg);
-			return_0;
-		}
-
-		if (!(pvl = find_pv_in_vg(vg, pv_name))) {
-			log_error("Unable to find \"%s\" in volume group \"%s\"",
-				  pv_name, vg->name);
-			goto out;
-		}
 		if (tagarg && !(vg->fid->fmt->features & FMT_TAGS)) {
 			log_error("Volume group containing %s does not "
 				  "support tags", pv_name);
@@ -78,7 +60,6 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
 				  "logical volumes", pv_name);
 			goto out;
 		}
-		pv = pvl->pv;
 		if (!archive(vg))
 			goto out;
 	} else {
@@ -87,19 +68,6 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
 				  "in volume group", pv_name);
 			return 0;
 		}
-
-		vg_name = VG_ORPHANS;
-
-		if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
-			log_error("Can't get lock for orphans");
-			return 0;
-		}
-
-		if (!(pv = pv_read(cmd, pv_name, NULL, &sector, 1, 0))) {
-			unlock_vg(cmd, vg_name);
-			log_error("Unable to read PV \"%s\"", pv_name);
-			return 0;
-		}
 	}
 
 	if (arg_count(cmd, allocatable_ARG)) {
@@ -201,7 +169,6 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
 	log_print("Physical volume \"%s\" changed", pv_name);
 	r = 1;
 out:
-	unlock_and_release_vg(cmd, vg, vg_name);
 	return r;
 
 }
@@ -212,12 +179,12 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv)
 	int done = 0;
 	int total = 0;
 
-	struct physical_volume *pv;
-	char *pv_name;
+	struct volume_group *vg;
+	const char *pv_name, *vg_name;
 
 	struct pv_list *pvl;
-	struct dm_list *pvslist;
-	struct dm_list mdas;
+	struct dm_list *vgnames;
+	struct str_list *sll;
 
 	if (arg_count(cmd, allocatable_ARG) + arg_count(cmd, addtag_ARG) +
 	    arg_count(cmd, deltag_ARG) + arg_count(cmd, uuid_ARG) != 1) {
@@ -240,50 +207,71 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv)
 		log_verbose("Using physical volume(s) on command line");
 		for (; opt < argc; opt++) {
 			pv_name = argv[opt];
-			dm_list_init(&mdas);
-			if (!(pv = pv_read(cmd, pv_name, &mdas, NULL, 1, 0))) {
+			vg_name = find_vgname_from_pvname(cmd, pv_name);
+			if (!vg_name) {
 				log_error("Failed to read physical volume %s",
 					  pv_name);
 				continue;
 			}
-			/*
-			 * If a PV has no MDAs it may appear to be an
-			 * orphan until the metadata is read off
-			 * another PV in the same VG.  Detecting this
-			 * means checking every VG by scanning every
-			 * PV on the system.
-			 */
-			if (is_orphan(pv) && !dm_list_size(&mdas)) {
-				if (!scan_vgs_for_pvs(cmd)) {
-					log_error("Rescan for PVs without "
-						  "metadata areas failed.");
-					continue;
-				}
-				if (!(pv = pv_read(cmd, pv_name,
-						   NULL, NULL, 1, 0))) {
-					log_error("Failed to read "
-						  "physical volume %s",
-						  pv_name);
-					continue;
-				}
+			vg = vg_read_for_update(cmd, vg_name, NULL, 0);
+			if (vg_read_error(vg)) {
+				vg_release(vg);
+				stack;
+				continue;
+			}
+			pvl = find_pv_in_vg(vg, pv_name);
+			if (!pvl || !pvl->pv) {
+				log_error("Unable to find %s in %s",
+					  pv_name, vg_name);
+				continue;
 			}
 
 			total++;
-			done += _pvchange_single(cmd, pv, NULL);
+			done += _pvchange_single(cmd, vg,
+						 pvl->pv, NULL);
+			/* FIXME: we should be using #orphans_lvm2 everwhere */
+			if (is_orphan_vg(vg->name))
+				vg_name = VG_ORPHANS;
+			unlock_and_release_vg(cmd, vg, vg_name);
 		}
 	} else {
 		log_verbose("Scanning for physical volume names");
-		if (!(pvslist = get_pvs(cmd))) {
-			stack;
+		/* FIXME: share code with toollib */
+		/*
+		 * Take the global lock here so the lvmcache remains
+		 * consistent across orphan/non-orphan vg locks.  If we don't
+		 * take the lock here, pvs with 0 mdas in a non-orphan VG will
+		 * be processed twice.
+		 */
+		if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_WRITE)) {
+			log_error("Unable to obtain global lock.");
 			return ECMD_FAILED;
 		}
 
-		dm_list_iterate_items(pvl, pvslist) {
-			total++;
-			done += _pvchange_single(cmd, pvl->pv, NULL);
+		if ((vgnames = get_vgnames(cmd, 1)) &&
+		    !dm_list_empty(vgnames)) {
+			dm_list_iterate_items(sll, vgnames) {
+				vg = vg_read_for_update(cmd, sll->str, NULL, 0);
+				if (vg_read_error(vg)) {
+					vg_release(vg);
+					stack;
+					continue;
+				}
+				dm_list_iterate_items(pvl, &vg->pvs) {
+					total++;
+					done += _pvchange_single(cmd, vg,
+								 pvl->pv,
+								 NULL);
+				}
+				/* FIXME: we should be using #orphans_lvm2 everwhere */
+				if (is_orphan_vg(vg->name))
+					sll->str = VG_ORPHANS;
+				unlock_and_release_vg(cmd, vg, sll->str);
+			}
 		}
 	}
 
+	unlock_vg(cmd, VG_GLOBAL);
 	log_print("%d physical volume%s changed / %d physical volume%s "
 		  "not changed",
 		  done, done == 1 ? "" : "s",
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 5/6 Add lvm2app interfaces to lookup a vgname from a pvid and pvname.

lvm2app forces applications to start with a volume group name, open the volume group, then operate on individual pvs. In some cases the application may want to start with a device name rather than the volume group name. Today, if an application wants to do this, it must iterate through all the volume groups to find the volume group that the specific device is attached to. These new interfaces allow the application to avoid such overhead. Bump the lvm2app version number to 3.

---
 VERSION                  |    2 +-
 liblvm/.exported_symbols |    2 ++
 liblvm/lvm2app.h         |   41 ++++++++++++++++++++++++++++++++++++++++-
 liblvm/lvm_base.c        |   19 +++++++++++++++++++
 4 files changed, 62 insertions(+), 2 deletions(-)
VERSION +1 -1
index d1ca44f..718913f 100644
@@ -1 +1 @@
-2.02.65(2)-cvs (2010-04-30)
+2.02.65(3)-cvs (2010-04-30)
liblvm/.exported_symbols +2 -0
index 9b61d45..6f53de3 100644
@@ -48,3 +48,5 @@ lvm_list_vg_names
 lvm_list_vg_uuids
 lvm_vg_create_lv_linear
 lvm_vg_remove_lv
+lvm_vgname_from_pvid
+lvm_vgname_from_device
liblvm/lvm2app.h +40 -1

51 lines not on this page · raw message

liblvm/lvm_base.c +19 -0
index 1b2ed9f..08ef14f 100644
@@ -17,6 +17,7 @@
 #include "toolcontext.h"
 #include "locking.h"
 #include "lvm-version.h"
+#include "metadata-exported.h"
 
 const char *lvm_library_get_version(void)
 {
@@ -98,3 +99,21 @@ const char *lvm_errmsg(lvm_t libh)
 {
 	return stored_errmsg();
 }
+
+const char *lvm_vgname_from_pvid(lvm_t libh, const char *pvid)
+{
+	struct cmd_context *cmd = (struct cmd_context *)libh;
+	char uuid[64] __attribute((aligned(8)));
+
+	if (!id_read_format(uuid, pvid)) {
+		log_error(INTERNAL_ERROR "Unable to convert uuid");
+		return NULL;
+	}
+	return find_vgname_from_pvid(cmd, uuid);
+}
+
+const char *lvm_vgname_from_device(lvm_t libh, const char *device)
+{
+	struct cmd_context *cmd = (struct cmd_context *)libh;
+	return find_vgname_from_pvname(cmd, device);
+}
Signature · 1 line
1.6.0.6
#6 lore raw

PATCH 6/6 Test lvm_vgname_from_{pvid|device}.

---
 test/api/test.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)
test/api/test.c +40 -0
index 8775dfb..f89840c 100644
@@ -97,6 +97,10 @@ static void _show_help(void)
 	       "Add/remove a tag from a VG\n");
 	printf("'lv_{add|remove}_tag vgname lvname tag': "
 	       "Add/remove a tag from a LV\n");
+	printf("'vgname_from_devname device': "
+	       "Lookup a vgname from a device name\n");
+	printf("'vgname_from_pvid pvid': "
+	       "Lookup a vgname from a pvid\n");
 	printf("'quit': exit the program\n");
 }
 
@@ -576,6 +580,38 @@ static void _lv_tag(char **argv, int argc, int add)
 	printf("%s tag %s to LV %s\n",
 	       add ? "adding":"removing", argv[3], argv[2]);
 }
+static void _vgname_from_pvid(char **argv, int argc, lvm_t libh)
+{
+	const char *vgname;
+
+	if (argc < 1) {
+		printf("Please enter pvid\n");
+		return;
+	}
+	if (!(vgname = lvm_vgname_from_pvid(libh, argv[1]))) {
+		printf("Error ");
+	} else {
+		printf("Success ");
+	}
+	printf("looking up vgname=%s from PVID=%s\n",
+	       vgname, argv[1]);
+}
+static void _vgname_from_devname(char **argv, int argc, lvm_t libh)
+{
+	const char *vgname;
+
+	if (argc < 1) {
+		printf("Please enter device\n");
+		return;
+	}
+	if (!(vgname = lvm_vgname_from_device(libh, argv[1]))) {
+		printf("Error ");
+	} else {
+		printf("Success ");
+	}
+	printf("looking up vgname=%s from device name=%s\n",
+	       vgname, argv[1]);
+}
 static void _lvs_in_vg(char **argv, int argc)
 {
 	struct dm_list *lvs;
@@ -766,6 +802,10 @@ static int lvmapi_test_shell(lvm_t libh)
 			_lv_tag(argv, argc, 0);
 		} else if (!strcmp(argv[0], "lv_get_tags")) {
 			_lv_get_tags(argv, argc);
+		} else if (!strcmp(argv[0], "vgname_from_devname")) {
+			_vgname_from_devname(argv, argc, libh);
+		} else if (!strcmp(argv[0], "vgname_from_pvid")) {
+			_vgname_from_pvid(argv, argc, libh);
 		} else {
 			printf ("Unrecognized command %s\n", argv[0]);
 		}
Signature · 1 line
1.6.0.6