Blame SOURCES/hvd-Update-C-files-and-scripts-to-kernel-version-5.7-rc1.patch

f32269
From b0a20fac0e74b0b3eecc20ffe74006e7877da352 Mon Sep 17 00:00:00 2001
f32269
From: Mohammed Gamal <mgamal@redhat.com>
f32269
Date: Wed, 15 Apr 2020 12:00:14 +0200
f32269
Subject: [PATCH 1/2] Update C files and scripts to kernel version 5.7-rc1
f32269
f32269
RH-Author: Mohammed Gamal <mgamal@redhat.com>
f32269
Message-id: <20200414183955.194006-2-mgamal@redhat.com>
f32269
Patchwork-id: 94689
f32269
O-Subject: [RHEL8.3 virt hyperv-daemons PATCH v5 1/2] Update C files and scripts to kernel version 5.7-rc1
f32269
Bugzilla: 1816750
f32269
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
f32269
RH-Acked-by: Cathy Avery <cavery@redhat.com>
f32269
f32269
Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
f32269
---
f32269
 hv_fcopy_daemon.c   |  38 ++++++++++++--
f32269
 hv_get_dhcp_info.sh |   2 +-
f32269
 hv_kvp_daemon.c     |  63 ++++++++++++++---------
f32269
 hv_set_ifconfig.sh  |   2 +-
f32269
 hv_vss_daemon.c     | 118 ++++++++++++++++++++++++++++++++++++++------
f32269
 lsvmbus             |  75 +++++++++++++++-------------
f32269
 6 files changed, 220 insertions(+), 78 deletions(-)
f32269
f32269
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
f32269
---
f32269
 hv_fcopy_daemon.c   |  38 ++++++++++++++---
f32269
 hv_get_dhcp_info.sh |   2 +-
f32269
 hv_kvp_daemon.c     |  63 ++++++++++++++++++----------
f32269
 hv_set_ifconfig.sh  |   2 +-
f32269
 hv_vss_daemon.c     | 118 +++++++++++++++++++++++++++++++++++++++++++++-------
f32269
 lsvmbus             |  75 ++++++++++++++++++---------------
f32269
 6 files changed, 220 insertions(+), 78 deletions(-)
f32269
f32269
diff --git a/hv_fcopy_daemon.c b/hv_fcopy_daemon.c
f32269
index d78aed8..f40ddaf 100644
f32269
--- a/hv_fcopy_daemon.c
f32269
+++ b/hv_fcopy_daemon.c
f32269
@@ -89,6 +89,8 @@ static int hv_start_fcopy(struct hv_start_fcopy *smsg)
f32269
 
f32269
 	error = 0;
f32269
 done:
f32269
+	if (error)
f32269
+		target_fname[0] = '\0';
f32269
 	return error;
f32269
 }
f32269
 
f32269
@@ -117,15 +119,29 @@ static int hv_copy_data(struct hv_do_fcopy *cpmsg)
f32269
 	return ret;
f32269
 }
f32269
 
f32269
+/*
f32269
+ * Reset target_fname to "" in the two below functions for hibernation: if
f32269
+ * the fcopy operation is aborted by hibernation, the daemon should remove the
f32269
+ * partially-copied file; to achieve this, the hv_utils driver always fakes a
f32269
+ * CANCEL_FCOPY message upon suspend, and later when the VM resumes back,
f32269
+ * the daemon calls hv_copy_cancel() to remove the file; if a file is copied
f32269
+ * successfully before suspend, hv_copy_finished() must reset target_fname to
f32269
+ * avoid that the file can be incorrectly removed upon resume, since the faked
f32269
+ * CANCEL_FCOPY message is spurious in this case.
f32269
+ */
f32269
 static int hv_copy_finished(void)
f32269
 {
f32269
 	close(target_fd);
f32269
+	target_fname[0] = '\0';
f32269
 	return 0;
f32269
 }
f32269
 static int hv_copy_cancel(void)
f32269
 {
f32269
 	close(target_fd);
f32269
-	unlink(target_fname);
f32269
+	if (strlen(target_fname) > 0) {
f32269
+		unlink(target_fname);
f32269
+		target_fname[0] = '\0';
f32269
+	}
f32269
 	return 0;
f32269
 
f32269
 }
f32269
@@ -140,7 +156,7 @@ void print_usage(char *argv[])
f32269
 
f32269
 int main(int argc, char *argv[])
f32269
 {
f32269
-	int fcopy_fd;
f32269
+	int fcopy_fd = -1;
f32269
 	int error;
f32269
 	int daemonize = 1, long_index = 0, opt;
f32269
 	int version = FCOPY_CURRENT_VERSION;
f32269
@@ -150,7 +166,7 @@ int main(int argc, char *argv[])
f32269
 		struct hv_do_fcopy copy;
f32269
 		__u32 kernel_modver;
f32269
 	} buffer = { };
f32269
-	int in_handshake = 1;
f32269
+	int in_handshake;
f32269
 
f32269
 	static struct option long_options[] = {
f32269
 		{"help",	no_argument,	   0,  'h' },
f32269
@@ -179,6 +195,12 @@ int main(int argc, char *argv[])
f32269
 	openlog("HV_FCOPY", 0, LOG_USER);
f32269
 	syslog(LOG_INFO, "starting; pid is:%d", getpid());
f32269
 
f32269
+reopen_fcopy_fd:
f32269
+	if (fcopy_fd != -1)
f32269
+		close(fcopy_fd);
f32269
+	/* Remove any possible partially-copied file on error */
f32269
+	hv_copy_cancel();
f32269
+	in_handshake = 1;
f32269
 	fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
f32269
 
f32269
 	if (fcopy_fd < 0) {
f32269
@@ -205,7 +227,7 @@ int main(int argc, char *argv[])
f32269
 		len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
f32269
 		if (len < 0) {
f32269
 			syslog(LOG_ERR, "pread failed: %s", strerror(errno));
f32269
-			exit(EXIT_FAILURE);
f32269
+			goto reopen_fcopy_fd;
f32269
 		}
f32269
 
f32269
 		if (in_handshake) {
f32269
@@ -234,14 +256,20 @@ int main(int argc, char *argv[])
f32269
 			break;
f32269
 
f32269
 		default:
f32269
+			error = HV_E_FAIL;
f32269
 			syslog(LOG_ERR, "Unknown operation: %d",
f32269
 				buffer.hdr.operation);
f32269
 
f32269
 		}
f32269
 
f32269
+		/*
f32269
+		 * pwrite() may return an error due to the faked CANCEL_FCOPY
f32269
+		 * message upon hibernation. Ignore the error by resetting the
f32269
+		 * dev file, i.e. closing and re-opening it.
f32269
+		 */
f32269
 		if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
f32269
 			syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
f32269
-			exit(EXIT_FAILURE);
f32269
+			goto reopen_fcopy_fd;
f32269
 		}
f32269
 	}
f32269
 }
f32269
diff --git a/hv_get_dhcp_info.sh b/hv_get_dhcp_info.sh
f32269
index c38686c..2f2a3c7 100644
f32269
--- a/hv_get_dhcp_info.sh
f32269
+++ b/hv_get_dhcp_info.sh
f32269
@@ -13,7 +13,7 @@
f32269
 #	the script prints the string "Disabled" to stdout.
f32269
 #
f32269
 # Each Distro is expected to implement this script in a distro specific
f32269
-# fashion. For instance on Distros that ship with Network Manager enabled,
f32269
+# fashion. For instance, on Distros that ship with Network Manager enabled,
f32269
 # this script can be based on the Network Manager APIs for retrieving DHCP
f32269
 # information.
f32269
 
f32269
diff --git a/hv_kvp_daemon.c b/hv_kvp_daemon.c
f32269
index dbf6e8b..ee9c1bb 100644
f32269
--- a/hv_kvp_daemon.c
f32269
+++ b/hv_kvp_daemon.c
f32269
@@ -76,7 +76,7 @@ enum {
f32269
 	DNS
f32269
 };
f32269
 
f32269
-static int in_hand_shake = 1;
f32269
+static int in_hand_shake;
f32269
 
f32269
 static char *os_name = "";
f32269
 static char *os_major = "";
f32269
@@ -286,7 +286,7 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
f32269
 		 * Found a match; just move the remaining
f32269
 		 * entries up.
f32269
 		 */
f32269
-		if (i == num_records) {
f32269
+		if (i == (num_records - 1)) {
f32269
 			kvp_file_info[pool].num_records--;
f32269
 			kvp_update_file(pool);
f32269
 			return 0;
f32269
@@ -700,7 +700,7 @@ static void kvp_get_ipconfig_info(char *if_name,
f32269
 
f32269
 
f32269
 	/*
f32269
-	 * Gather the DNS  state.
f32269
+	 * Gather the DNS state.
f32269
 	 * Since there is no standard way to get this information
f32269
 	 * across various distributions of interest; we just invoke
f32269
 	 * an external script that needs to be ported across distros
f32269
@@ -809,7 +809,7 @@ kvp_get_ip_info(int family, char *if_name, int op,
f32269
 	int sn_offset = 0;
f32269
 	int error = 0;
f32269
 	char *buffer;
f32269
-	struct hv_kvp_ipaddr_value *ip_buffer;
f32269
+	struct hv_kvp_ipaddr_value *ip_buffer = NULL;
f32269
 	char cidr_mask[5]; /* /xyz */
f32269
 	int weight;
f32269
 	int i;
f32269
@@ -1051,7 +1051,7 @@ static int parse_ip_val_buffer(char *in_buf, int *offset,
f32269
 	char *start;
f32269
 
f32269
 	/*
f32269
-	 * in_buf has sequence of characters that are seperated by
f32269
+	 * in_buf has sequence of characters that are separated by
f32269
 	 * the character ';'. The last sequence does not have the
f32269
 	 * terminating ";" character.
f32269
 	 */
f32269
@@ -1178,6 +1178,7 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
f32269
 	FILE *file;
f32269
 	char cmd[PATH_MAX];
f32269
 	char *mac_addr;
f32269
+	int str_len;
f32269
 
f32269
 	/*
f32269
 	 * Set the configuration for the specified interface with
f32269
@@ -1301,8 +1302,18 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
f32269
 	 * invoke the external script to do its magic.
f32269
 	 */
f32269
 
f32269
-	snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
f32269
-		 "hv_set_ifconfig", if_file);
f32269
+	str_len = snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
f32269
+			   "hv_set_ifconfig", if_file);
f32269
+	/*
f32269
+	 * This is a little overcautious, but it's necessary to suppress some
f32269
+	 * false warnings from gcc 8.0.1.
f32269
+	 */
f32269
+	if (str_len <= 0 || (unsigned int)str_len >= sizeof(cmd)) {
f32269
+		syslog(LOG_ERR, "Cmd '%s' (len=%d) may be too long",
f32269
+		       cmd, str_len);
f32269
+		return HV_E_FAIL;
f32269
+	}
f32269
+
f32269
 	if (system(cmd)) {
f32269
 		syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
f32269
 				cmd, errno, strerror(errno));
f32269
@@ -1349,7 +1360,7 @@ void print_usage(char *argv[])
f32269
 
f32269
 int main(int argc, char *argv[])
f32269
 {
f32269
-	int kvp_fd, len;
f32269
+	int kvp_fd = -1, len;
f32269
 	int error;
f32269
 	struct pollfd pfd;
f32269
 	char    *p;
f32269
@@ -1375,6 +1386,8 @@ int main(int argc, char *argv[])
f32269
 			daemonize = 0;
f32269
 			break;
f32269
 		case 'h':
f32269
+			print_usage(argv);
f32269
+			exit(0);
f32269
 		default:
f32269
 			print_usage(argv);
f32269
 			exit(EXIT_FAILURE);
f32269
@@ -1387,14 +1400,6 @@ int main(int argc, char *argv[])
f32269
 	openlog("KVP", 0, LOG_USER);
f32269
 	syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
f32269
 
f32269
-	kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
f32269
-
f32269
-	if (kvp_fd < 0) {
f32269
-		syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
f32269
-			errno, strerror(errno));
f32269
-		exit(EXIT_FAILURE);
f32269
-	}
f32269
-
f32269
 	/*
f32269
 	 * Retrieve OS release information.
f32269
 	 */
f32269
@@ -1410,6 +1415,18 @@ int main(int argc, char *argv[])
f32269
 		exit(EXIT_FAILURE);
f32269
 	}
f32269
 
f32269
+reopen_kvp_fd:
f32269
+	if (kvp_fd != -1)
f32269
+		close(kvp_fd);
f32269
+	in_hand_shake = 1;
f32269
+	kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
f32269
+
f32269
+	if (kvp_fd < 0) {
f32269
+		syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
f32269
+		       errno, strerror(errno));
f32269
+		exit(EXIT_FAILURE);
f32269
+	}
f32269
+
f32269
 	/*
f32269
 	 * Register ourselves with the kernel.
f32269
 	 */
f32269
@@ -1443,9 +1460,7 @@ int main(int argc, char *argv[])
f32269
 		if (len != sizeof(struct hv_kvp_msg)) {
f32269
 			syslog(LOG_ERR, "read failed; error:%d %s",
f32269
 			       errno, strerror(errno));
f32269
-
f32269
-			close(kvp_fd);
f32269
-			return EXIT_FAILURE;
f32269
+			goto reopen_kvp_fd;
f32269
 		}
f32269
 
f32269
 		/*
f32269
@@ -1479,7 +1494,7 @@ int main(int argc, char *argv[])
f32269
 		case KVP_OP_GET_IP_INFO:
f32269
 			kvp_ip_val = &hv_msg->body.kvp_ip_val;
f32269
 
f32269
-			error =  kvp_mac_to_ip(kvp_ip_val);
f32269
+			error = kvp_mac_to_ip(kvp_ip_val);
f32269
 
f32269
 			if (error)
f32269
 				hv_msg->error = error;
f32269
@@ -1604,13 +1619,17 @@ int main(int argc, char *argv[])
f32269
 			break;
f32269
 		}
f32269
 
f32269
-		/* Send the value back to the kernel. */
f32269
+		/*
f32269
+		 * Send the value back to the kernel. Note: the write() may
f32269
+		 * return an error due to hibernation; we can ignore the error
f32269
+		 * by resetting the dev file, i.e. closing and re-opening it.
f32269
+		 */
f32269
 kvp_done:
f32269
 		len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
f32269
 		if (len != sizeof(struct hv_kvp_msg)) {
f32269
 			syslog(LOG_ERR, "write failed; error: %d %s", errno,
f32269
 			       strerror(errno));
f32269
-			exit(EXIT_FAILURE);
f32269
+			goto reopen_kvp_fd;
f32269
 		}
f32269
 	}
f32269
 
f32269
diff --git a/hv_set_ifconfig.sh b/hv_set_ifconfig.sh
f32269
index 18b27cc..3dd064c 100644
f32269
--- a/hv_set_ifconfig.sh
f32269
+++ b/hv_set_ifconfig.sh
f32269
@@ -12,7 +12,7 @@
f32269
 # be used to configure the interface.
f32269
 #
f32269
 # Each Distro is expected to implement this script in a distro specific
f32269
-# fashion. For instance on Distros that ship with Network Manager enabled,
f32269
+# fashion. For instance, on Distros that ship with Network Manager enabled,
f32269
 # this script can be based on the Network Manager APIs for configuring the
f32269
 # interface.
f32269
 #
f32269
diff --git a/hv_vss_daemon.c b/hv_vss_daemon.c
f32269
index 34031a2..8fe0a5c 100644
f32269
--- a/hv_vss_daemon.c
f32269
+++ b/hv_vss_daemon.c
f32269
@@ -36,6 +36,10 @@
f32269
 #include <linux/hyperv.h>
f32269
 #include <syslog.h>
f32269
 #include <getopt.h>
f32269
+#include <stdbool.h>
f32269
+#include <dirent.h>
f32269
+
f32269
+static bool fs_frozen;
f32269
 
f32269
 /* Don't use syslog() in the function since that can cause write to disk */
f32269
 static int vss_do_freeze(char *dir, unsigned int cmd)
f32269
@@ -51,7 +55,7 @@ static int vss_do_freeze(char *dir, unsigned int cmd)
f32269
 	 * If a partition is mounted more than once, only the first
f32269
 	 * FREEZE/THAW can succeed and the later ones will get
f32269
 	 * EBUSY/EINVAL respectively: there could be 2 cases:
f32269
-	 * 1) a user may mount the same partition to differnt directories
f32269
+	 * 1) a user may mount the same partition to different directories
f32269
 	 *  by mistake or on purpose;
f32269
 	 * 2) The subvolume of btrfs appears to have the same partition
f32269
 	 * mounted more than once.
f32269
@@ -68,6 +72,55 @@ static int vss_do_freeze(char *dir, unsigned int cmd)
f32269
 	return !!ret;
f32269
 }
f32269
 
f32269
+static bool is_dev_loop(const char *blkname)
f32269
+{
f32269
+	char *buffer;
f32269
+	DIR *dir;
f32269
+	struct dirent *entry;
f32269
+	bool ret = false;
f32269
+
f32269
+	buffer = malloc(PATH_MAX);
f32269
+	if (!buffer) {
f32269
+		syslog(LOG_ERR, "Can't allocate memory!");
f32269
+		exit(1);
f32269
+	}
f32269
+
f32269
+	snprintf(buffer, PATH_MAX, "%s/loop", blkname);
f32269
+	if (!access(buffer, R_OK | X_OK)) {
f32269
+		ret = true;
f32269
+		goto free_buffer;
f32269
+	} else if (errno != ENOENT) {
f32269
+		syslog(LOG_ERR, "Can't access: %s; error:%d %s!",
f32269
+		       buffer, errno, strerror(errno));
f32269
+	}
f32269
+
f32269
+	snprintf(buffer, PATH_MAX, "%s/slaves", blkname);
f32269
+	dir = opendir(buffer);
f32269
+	if (!dir) {
f32269
+		if (errno != ENOENT)
f32269
+			syslog(LOG_ERR, "Can't opendir: %s; error:%d %s!",
f32269
+			       buffer, errno, strerror(errno));
f32269
+		goto free_buffer;
f32269
+	}
f32269
+
f32269
+	while ((entry = readdir(dir)) != NULL) {
f32269
+		if (strcmp(entry->d_name, ".") == 0 ||
f32269
+		    strcmp(entry->d_name, "..") == 0)
f32269
+			continue;
f32269
+
f32269
+		snprintf(buffer, PATH_MAX, "%s/slaves/%s", blkname,
f32269
+			 entry->d_name);
f32269
+		if (is_dev_loop(buffer)) {
f32269
+			ret = true;
f32269
+			break;
f32269
+		}
f32269
+	}
f32269
+	closedir(dir);
f32269
+free_buffer:
f32269
+	free(buffer);
f32269
+	return ret;
f32269
+}
f32269
+
f32269
 static int vss_operate(int operation)
f32269
 {
f32269
 	char match[] = "/dev/";
f32269
@@ -75,6 +128,7 @@ static int vss_operate(int operation)
f32269
 	struct mntent *ent;
f32269
 	struct stat sb;
f32269
 	char errdir[1024] = {0};
f32269
+	char blkdir[23]; /* /sys/dev/block/XXX:XXX */
f32269
 	unsigned int cmd;
f32269
 	int error = 0, root_seen = 0, save_errno = 0;
f32269
 
f32269
@@ -96,10 +150,15 @@ static int vss_operate(int operation)
f32269
 	while ((ent = getmntent(mounts))) {
f32269
 		if (strncmp(ent->mnt_fsname, match, strlen(match)))
f32269
 			continue;
f32269
-		if (stat(ent->mnt_fsname, &sb) == -1)
f32269
-			continue;
f32269
-		if (S_ISBLK(sb.st_mode) && major(sb.st_rdev) == LOOP_MAJOR)
f32269
-			continue;
f32269
+		if (stat(ent->mnt_fsname, &sb)) {
f32269
+			syslog(LOG_ERR, "Can't stat: %s; error:%d %s!",
f32269
+			       ent->mnt_fsname, errno, strerror(errno));
f32269
+		} else {
f32269
+			sprintf(blkdir, "/sys/dev/block/%d:%d",
f32269
+				major(sb.st_rdev), minor(sb.st_rdev));
f32269
+			if (is_dev_loop(blkdir))
f32269
+				continue;
f32269
+		}
f32269
 		if (hasmntopt(ent, MNTOPT_RO) != NULL)
f32269
 			continue;
f32269
 		if (strcmp(ent->mnt_type, "vfat") == 0)
f32269
@@ -109,18 +168,27 @@ static int vss_operate(int operation)
f32269
 			continue;
f32269
 		}
f32269
 		error |= vss_do_freeze(ent->mnt_dir, cmd);
f32269
-		if (error && operation == VSS_OP_FREEZE)
f32269
-			goto err;
f32269
+		if (operation == VSS_OP_FREEZE) {
f32269
+			if (error)
f32269
+				goto err;
f32269
+			fs_frozen = true;
f32269
+		}
f32269
 	}
f32269
 
f32269
 	endmntent(mounts);
f32269
 
f32269
 	if (root_seen) {
f32269
 		error |= vss_do_freeze("/", cmd);
f32269
-		if (error && operation == VSS_OP_FREEZE)
f32269
-			goto err;
f32269
+		if (operation == VSS_OP_FREEZE) {
f32269
+			if (error)
f32269
+				goto err;
f32269
+			fs_frozen = true;
f32269
+		}
f32269
 	}
f32269
 
f32269
+	if (operation == VSS_OP_THAW && !error)
f32269
+		fs_frozen = false;
f32269
+
f32269
 	goto out;
f32269
 err:
f32269
 	save_errno = errno;
f32269
@@ -129,6 +197,7 @@ err:
f32269
 		endmntent(mounts);
f32269
 	}
f32269
 	vss_operate(VSS_OP_THAW);
f32269
+	fs_frozen = false;
f32269
 	/* Call syslog after we thaw all filesystems */
f32269
 	if (ent)
f32269
 		syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
f32269
@@ -150,13 +219,13 @@ void print_usage(char *argv[])
f32269
 
f32269
 int main(int argc, char *argv[])
f32269
 {
f32269
-	int vss_fd, len;
f32269
+	int vss_fd = -1, len;
f32269
 	int error;
f32269
 	struct pollfd pfd;
f32269
 	int	op;
f32269
 	struct hv_vss_msg vss_msg[1];
f32269
 	int daemonize = 1, long_index = 0, opt;
f32269
-	int in_handshake = 1;
f32269
+	int in_handshake;
f32269
 	__u32 kernel_modver;
f32269
 
f32269
 	static struct option long_options[] = {
f32269
@@ -172,6 +241,8 @@ int main(int argc, char *argv[])
f32269
 			daemonize = 0;
f32269
 			break;
f32269
 		case 'h':
f32269
+			print_usage(argv);
f32269
+			exit(0);
f32269
 		default:
f32269
 			print_usage(argv);
f32269
 			exit(EXIT_FAILURE);
f32269
@@ -184,6 +255,18 @@ int main(int argc, char *argv[])
f32269
 	openlog("Hyper-V VSS", 0, LOG_USER);
f32269
 	syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
f32269
 
f32269
+reopen_vss_fd:
f32269
+	if (vss_fd != -1)
f32269
+		close(vss_fd);
f32269
+	if (fs_frozen) {
f32269
+		if (vss_operate(VSS_OP_THAW) || fs_frozen) {
f32269
+			syslog(LOG_ERR, "failed to thaw file system: err=%d",
f32269
+			       errno);
f32269
+			exit(EXIT_FAILURE);
f32269
+		}
f32269
+	}
f32269
+
f32269
+	in_handshake = 1;
f32269
 	vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
f32269
 	if (vss_fd < 0) {
f32269
 		syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
f32269
@@ -236,8 +319,7 @@ int main(int argc, char *argv[])
f32269
 		if (len != sizeof(struct hv_vss_msg)) {
f32269
 			syslog(LOG_ERR, "read failed; error:%d %s",
f32269
 			       errno, strerror(errno));
f32269
-			close(vss_fd);
f32269
-			return EXIT_FAILURE;
f32269
+			goto reopen_vss_fd;
f32269
 		}
f32269
 
f32269
 		op = vss_msg->vss_hdr.operation;
f32269
@@ -264,14 +346,18 @@ int main(int argc, char *argv[])
f32269
 		default:
f32269
 			syslog(LOG_ERR, "Illegal op:%d\n", op);
f32269
 		}
f32269
+
f32269
+		/*
f32269
+		 * The write() may return an error due to the faked VSS_OP_THAW
f32269
+		 * message upon hibernation. Ignore the error by resetting the
f32269
+		 * dev file, i.e. closing and re-opening it.
f32269
+		 */
f32269
 		vss_msg->error = error;
f32269
 		len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
f32269
 		if (len != sizeof(struct hv_vss_msg)) {
f32269
 			syslog(LOG_ERR, "write failed; error: %d %s", errno,
f32269
 			       strerror(errno));
f32269
-
f32269
-			if (op == VSS_OP_FREEZE)
f32269
-				vss_operate(VSS_OP_THAW);
f32269
+			goto reopen_vss_fd;
f32269
 		}
f32269
 	}
f32269
 
f32269
diff --git a/lsvmbus b/lsvmbus
f32269
index 55e7374..099f2c4 100644
f32269
--- a/lsvmbus
f32269
+++ b/lsvmbus
f32269
@@ -4,10 +4,10 @@
f32269
 import os
f32269
 from optparse import OptionParser
f32269
 
f32269
+help_msg = "print verbose messages. Try -vv, -vvv for  more verbose messages"
f32269
 parser = OptionParser()
f32269
-parser.add_option("-v", "--verbose", dest="verbose",
f32269
-		   help="print verbose messages. Try -vv, -vvv for \
f32269
-			more verbose messages", action="count")
f32269
+parser.add_option(
f32269
+	"-v", "--verbose", dest="verbose", help=help_msg, action="count")
f32269
 
f32269
 (options, args) = parser.parse_args()
f32269
 
f32269
@@ -21,27 +21,28 @@ if not os.path.isdir(vmbus_sys_path):
f32269
 	exit(-1)
f32269
 
f32269
 vmbus_dev_dict = {
f32269
-	'{0e0b6031-5213-4934-818b-38d90ced39db}' : '[Operating system shutdown]',
f32269
-	'{9527e630-d0ae-497b-adce-e80ab0175caf}' : '[Time Synchronization]',
f32269
-	'{57164f39-9115-4e78-ab55-382f3bd5422d}' : '[Heartbeat]',
f32269
-	'{a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}' : '[Data Exchange]',
f32269
-	'{35fa2e29-ea23-4236-96ae-3a6ebacba440}' : '[Backup (volume checkpoint)]',
f32269
-	'{34d14be3-dee4-41c8-9ae7-6b174977c192}' : '[Guest services]',
f32269
-	'{525074dc-8985-46e2-8057-a307dc18a502}' : '[Dynamic Memory]',
f32269
-	'{cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}' : 'Synthetic mouse',
f32269
-	'{f912ad6d-2b17-48ea-bd65-f927a61c7684}' : 'Synthetic keyboard',
f32269
-	'{da0a7802-e377-4aac-8e77-0558eb1073f8}' : 'Synthetic framebuffer adapter',
f32269
-	'{f8615163-df3e-46c5-913f-f2d2f965ed0e}' : 'Synthetic network adapter',
f32269
-	'{32412632-86cb-44a2-9b5c-50d1417354f5}' : 'Synthetic IDE Controller',
f32269
-	'{ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}' : 'Synthetic SCSI Controller',
f32269
-	'{2f9bcc4a-0069-4af3-b76b-6fd0be528cda}' : 'Synthetic fiber channel adapter',
f32269
-	'{8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}' : 'Synthetic RDMA adapter',
f32269
-	'{44c4f61d-4444-4400-9d52-802e27ede19f}' : 'PCI Express pass-through',
f32269
-	'{276aacf4-ac15-426c-98dd-7521ad3f01fe}' : '[Reserved system device]',
f32269
-	'{f8e65716-3cb3-4a06-9a60-1889c5cccab5}' : '[Reserved system device]',
f32269
-	'{3375baf4-9e15-4b30-b765-67acb10d607b}' : '[Reserved system device]',
f32269
+	'{0e0b6031-5213-4934-818b-38d90ced39db}': '[Operating system shutdown]',
f32269
+	'{9527e630-d0ae-497b-adce-e80ab0175caf}': '[Time Synchronization]',
f32269
+	'{57164f39-9115-4e78-ab55-382f3bd5422d}': '[Heartbeat]',
f32269
+	'{a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}': '[Data Exchange]',
f32269
+	'{35fa2e29-ea23-4236-96ae-3a6ebacba440}': '[Backup (volume checkpoint)]',
f32269
+	'{34d14be3-dee4-41c8-9ae7-6b174977c192}': '[Guest services]',
f32269
+	'{525074dc-8985-46e2-8057-a307dc18a502}': '[Dynamic Memory]',
f32269
+	'{cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}': 'Synthetic mouse',
f32269
+	'{f912ad6d-2b17-48ea-bd65-f927a61c7684}': 'Synthetic keyboard',
f32269
+	'{da0a7802-e377-4aac-8e77-0558eb1073f8}': 'Synthetic framebuffer adapter',
f32269
+	'{f8615163-df3e-46c5-913f-f2d2f965ed0e}': 'Synthetic network adapter',
f32269
+	'{32412632-86cb-44a2-9b5c-50d1417354f5}': 'Synthetic IDE Controller',
f32269
+	'{ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}': 'Synthetic SCSI Controller',
f32269
+	'{2f9bcc4a-0069-4af3-b76b-6fd0be528cda}': 'Synthetic fiber channel adapter',
f32269
+	'{8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}': 'Synthetic RDMA adapter',
f32269
+	'{44c4f61d-4444-4400-9d52-802e27ede19f}': 'PCI Express pass-through',
f32269
+	'{276aacf4-ac15-426c-98dd-7521ad3f01fe}': '[Reserved system device]',
f32269
+	'{f8e65716-3cb3-4a06-9a60-1889c5cccab5}': '[Reserved system device]',
f32269
+	'{3375baf4-9e15-4b30-b765-67acb10d607b}': '[Reserved system device]',
f32269
 }
f32269
 
f32269
+
f32269
 def get_vmbus_dev_attr(dev_name, attr):
f32269
 	try:
f32269
 		f = open('%s/%s/%s' % (vmbus_sys_path, dev_name, attr), 'r')
f32269
@@ -52,6 +53,7 @@ def get_vmbus_dev_attr(dev_name, attr):
f32269
 
f32269
 	return lines
f32269
 
f32269
+
f32269
 class VMBus_Dev:
f32269
 	pass
f32269
 
f32269
@@ -66,12 +68,13 @@ for f in os.listdir(vmbus_sys_path):
f32269
 
f32269
 	chn_vp_mapping = get_vmbus_dev_attr(f, 'channel_vp_mapping')
f32269
 	chn_vp_mapping = [c.strip() for c in chn_vp_mapping]
f32269
-	chn_vp_mapping = sorted(chn_vp_mapping,
f32269
-		key = lambda c : int(c.split(':')[0]))
f32269
+	chn_vp_mapping = sorted(
f32269
+		chn_vp_mapping, key=lambda c: int(c.split(':')[0]))
f32269
 
f32269
-	chn_vp_mapping = ['\tRel_ID=%s, target_cpu=%s' %
f32269
-				(c.split(':')[0], c.split(':')[1])
f32269
-					for c in chn_vp_mapping]
f32269
+	chn_vp_mapping = [
f32269
+		'\tRel_ID=%s, target_cpu=%s' %
f32269
+		(c.split(':')[0], c.split(':')[1]) for c in chn_vp_mapping
f32269
+	]
f32269
 	d = VMBus_Dev()
f32269
 	d.sysfs_path = '%s/%s' % (vmbus_sys_path, f)
f32269
 	d.vmbus_id = vmbus_id
f32269
@@ -85,7 +88,7 @@ for f in os.listdir(vmbus_sys_path):
f32269
 	vmbus_dev_list.append(d)
f32269
 
f32269
 
f32269
-vmbus_dev_list  = sorted(vmbus_dev_list, key = lambda d : int(d.vmbus_id))
f32269
+vmbus_dev_list = sorted(vmbus_dev_list, key=lambda d: int(d.vmbus_id))
f32269
 
f32269
 format0 = '%2s: %s'
f32269
 format1 = '%2s: Class_ID = %s - %s\n%s'
f32269
@@ -95,9 +98,15 @@ for d in vmbus_dev_list:
f32269
 	if verbose == 0:
f32269
 		print(('VMBUS ID ' + format0) % (d.vmbus_id, d.dev_desc))
f32269
 	elif verbose == 1:
f32269
-		print (('VMBUS ID ' + format1) %	\
f32269
-			(d.vmbus_id, d.class_id, d.dev_desc, d.chn_vp_mapping))
f32269
+		print(
f32269
+			('VMBUS ID ' + format1) %
f32269
+			(d.vmbus_id, d.class_id, d.dev_desc, d.chn_vp_mapping)
f32269
+		)
f32269
 	else:
f32269
-		print (('VMBUS ID ' + format2) % \
f32269
-			(d.vmbus_id, d.class_id, d.dev_desc, \
f32269
-			d.device_id, d.sysfs_path, d.chn_vp_mapping))
f32269
+		print(
f32269
+			('VMBUS ID ' + format2) %
f32269
+			(
f32269
+				d.vmbus_id, d.class_id, d.dev_desc,
f32269
+				d.device_id, d.sysfs_path, d.chn_vp_mapping
f32269
+			)
f32269
+		)
f32269
-- 
f32269
1.8.3.1
f32269