From 65927e86669e702b8b76f8af81c040c666bc3260 Mon Sep 17 00:00:00 2001
From: miaoguanqin <miaoguanqin@huawei.com>
Date: Thu, 8 Sep 2022 19:59:44 +0800
Subject: [PATCH] vgremove: PVID file leakage in /run/lvm/pvs_online

We found PVID file leakage problem when exec the following test;
pvcreate /dev/sdb
vgcreate -s 1G docker /dev/sdb
lvcreate -L 10M docker
pvscan --cache --listvg --checkcomplete --vgonline --udevoutput --journal=output /dev/sdb
vgremove -ff docker

pvcreate operation generates a new PVID, which is used to create PVID pvid file
when exec pvscan. However vgremove does not delete the old PVID file.

Here, we will delete all PVID files of each PV in current vg, when exec vgremove.

Signed-off-by: miaoguanqin <miaoguanqin@huawei.com>
---
 lib/metadata/metadata.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c
index 30b2c17..ea02629 100644
--- a/lib/metadata/metadata.c
+++ b/lib/metadata/metadata.c
@@ -38,6 +38,29 @@
 #include <time.h>
 #include <math.h>
 
+static const char *_pvs_online_dir = DEFAULT_RUN_DIR "/pvs_online";
+
+static int remove_pvid_file(struct physical_volume *pv)
+{
+	char pvid[ID_LEN + 1] __attribute__((aligned(8))) = { 0 };
+	char path[PATH_MAX] __attribute__((aligned(8)))={ 0 };
+
+	memcpy(pvid, &pv->id.uuid, ID_LEN);
+
+	if (dm_snprintf(path, sizeof(path), "%s/%s", _pvs_online_dir, pvid) < 0) {
+		log_warn("WARNING:pvid file path is %s", path);
+		return 0;
+	}
+
+	log_warn("unlink pvid file, path is %s", path);
+
+	if (unlink(path) && (errno != ENOENT)) {
+		log_warn("WARNING:unlink pvid file path error, path is %s", path);
+		return 0;
+	}
+	return 1;
+}
+
 static struct physical_volume *_pv_read(struct cmd_context *cmd,
 					const struct format_type *fmt,
 					struct volume_group *vg,
@@ -665,6 +688,15 @@ int vg_remove_direct(struct volume_group *vg)
 				  " from volume group \"%s\"",
 				  pv_dev_name(pv), vg->name);
 			ret = 0;
+			continue;
+		}
+
+		/* Remove pvid files */
+		if (!remove_pvid_file(pv)) {
+			log_error("Failed to remove pvid files \"%s\""
+			      " from volume group \"%s\"",
+			      _pvs_online_dir, vg->name);
+			ret = 0;
 		}
 	}
 
-- 
2.33.0

