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

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