Skip to contentSkip to conversation

Add infrastructure for cleaner pv handling

PATCH New Dave Wysochanski 1 participant 4 messages Feb 13, 2010

lore
Share & tools
lore
#0 lore raw

cover Add infrastructure for cleaner pv handling

This patchset adds some infrastructure to move us more towards the goal of removing the standalone PV object.

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.

The last patch reworks pvchange to use this new functionality. It passes the testsuite but does still have one bug. A setup with a VG containing a PV with metadatacopies == 0 and pvchange -u --all end up failing because the PV is on both the orphan VG and the real VG. I think this is a bug internally unrelated to this patchset but I have yet to track it down. It should be fixed before we move the tools to this new method.

#1 lore raw

PATCH 1/3 Add a couple of lvmcache functions to aid in lookups by pvname.

It would be nice to clean up some of the pv handling and move away from the pv as a standalone object. To this end, I introduce a couple helper functions from lvmcache which allow us to lookup a vgname from a pvname and an info struct from a pvname. These functions will be used later in a library function that we will export and will aid in cleaning up the pv handling.

---
 lib/cache/lvmcache.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/cache/lvmcache.h |    2 +
 2 files changed, 62 insertions(+), 0 deletions(-)
lib/cache/lvmcache.c +60 -0
index 35ee05b..074d591 100644
@@ -499,6 +499,66 @@ struct lvmcache_info *info_from_pvid(const char *pvid, int valid_only)
 	return info;
 }
 
+struct lvmcache_info *info_from_pvname(const char *pvname, int valid_only)
+{
+	struct dm_hash_node *n;
+	struct lvmcache_info *info;
+	char *id;
+
+	if (!_pvid_hash | !pvname)
+		return_NULL;
+
+	/* FIXME: Might want to use another hash for PV names */
+	dm_hash_iterate(n, _pvid_hash) {
+		if (!dm_hash_get_data(_pvid_hash, n))
+			return_NULL;
+
+		id = dm_hash_get_key(_pvid_hash, n);
+		info = info_from_pvid(id, 1);
+
+		if (!strcmp(dev_name(info->dev), pvname)) {
+			if (valid_only && !_info_is_valid(info))
+				return NULL;
+			else
+				return info;
+		}
+	}
+
+	return NULL;
+}
+
+char *lvmcache_vgname_from_pvname(struct cmd_context *cmd, const char *pvname)
+{
+	struct dm_hash_node *n;
+	struct lvmcache_info *info;
+	char *id;
+	char *vgname;
+
+	if (!_pvid_hash | !pvname)
+		return_NULL;
+
+	/* FIXME: Might want to use another hash for PV names */
+	dm_hash_iterate(n, _pvid_hash) {
+		if (!dm_hash_get_data(_pvid_hash, n))
+			return_NULL;
+
+		id = dm_hash_get_key(_pvid_hash, n);
+		info = info_from_pvid(id, 1);
+
+		if (info->vginfo &&
+		    !strcmp(dev_name(info->dev), pvname)) {
+			if (!(vgname = dm_pool_alloc(cmd->mem, NAME_LEN+1))) {
+				log_errno(ENOMEM, "pvname allocation failed");
+				return NULL;
+			}
+			strncpy(vgname, info->vginfo->vgname, NAME_LEN);
+			return vgname;
+		}
+	}
+
+	return NULL;
+}
+
 static void _rescan_entry(struct lvmcache_info *info)
 {
 	struct label *label;
lib/cache/lvmcache.h +2 -0
index 2c14b6c..10d1b7f 100644
@@ -94,6 +94,8 @@ struct lvmcache_vginfo *vginfo_from_vgid(const char *vgid);
 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);
+char *lvmcache_vgname_from_pvname(struct cmd_context *cmd, const char *pvname);
+struct lvmcache_info *info_from_pvname(const char *pvname, int valid_only);
 int vgs_locked(void);
 int vgname_is_locked(const char *vgname);
 
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/3 Add find_vgname_from_pvname() function using lvmcache 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 requiring them to use vg_read/vg_open. This find function first does an intial scan, then asks lvmcache for the vgname. If the vgname is an orphan, we then check to see if there's 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 IMO and should allow us to refactor more of the pv code.

---
 lib/metadata/metadata-exported.h |    1 +
 lib/metadata/metadata.c          |   42 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 0 deletions(-)
lib/metadata/metadata-exported.h +1 -0
index f8329d5..597b913 100644
@@ -605,6 +605,7 @@ 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);
 
+char *find_vgname_from_pvname(struct cmd_context *cmd, const char *pvname);
 /* Find LV segment containing given LE */
 struct lv_segment *first_seg(const struct logical_volume *lv);
 
lib/metadata/metadata.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/3 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 |  116 +++++++++++++++++++++--------------------------------
 1 files changed, 46 insertions(+), 70 deletions(-)
tools/pvchange.c +46 -70
index cba9dd2..0cf1c98 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,47 +207,56 @@ 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;
-			return ECMD_FAILED;
-		}
-
-		dm_list_iterate_items(pvl, pvslist) {
-			total++;
-			done += _pvchange_single(cmd, pvl->pv, NULL);
+		/* FIXME: share code with toollib */
+		if ((vgnames = get_vgnames(cmd, 0, 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))
+					vg_name = VG_ORPHANS;
+				unlock_and_release_vg(cmd, vg, vg_name);
+			}
 		}
 	}
 
Signature · 1 line
1.6.0.6