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

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