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

3feead
From f20c2a3298ceae7536c06bd08a5c571ebfa8cce4 Mon Sep 17 00:00:00 2001
3feead
From: Miroslav Rezanina <mrezanin@redhat.com>
3feead
Date: Thu, 6 May 2021 12:50:43 +0200
3feead
Subject: Update C files and scripts to kernel version 5.7-rc1
3feead
3feead
(cherry-picked from RHEL 8.4.0 commit b0a20fac0e74b0b3eecc20ffe74006e7877da352)
3feead
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
3feead
---
3feead
 hv_fcopy_daemon.c   | 37 +++++++++++++++++++++++++++++++-----
3feead
 hv_get_dhcp_info.sh |  2 +-
3feead
 hv_kvp_daemon.c     | 35 ++++++++++++++++++++--------------
3feead
 hv_set_ifconfig.sh  |  2 +-
3feead
 hv_vss_daemon.c     | 46 ++++++++++++++++++++++++++++++++++-----------
3feead
 5 files changed, 90 insertions(+), 32 deletions(-)
3feead
3feead
diff --git a/hv_fcopy_daemon.c b/hv_fcopy_daemon.c
3feead
index aea2d91..16d629b 100644
3feead
--- a/hv_fcopy_daemon.c
3feead
+++ b/hv_fcopy_daemon.c
3feead
@@ -80,6 +80,8 @@ static int hv_start_fcopy(struct hv_start_fcopy *smsg)
3feead
 
3feead
 	error = 0;
3feead
 done:
3feead
+	if (error)
3feead
+		target_fname[0] = '\0';
3feead
 	return error;
3feead
 }
3feead
 
3feead
@@ -108,15 +110,29 @@ static int hv_copy_data(struct hv_do_fcopy *cpmsg)
3feead
 	return ret;
3feead
 }
3feead
 
3feead
+/*
3feead
+ * Reset target_fname to "" in the two below functions for hibernation: if
3feead
+ * the fcopy operation is aborted by hibernation, the daemon should remove the
3feead
+ * partially-copied file; to achieve this, the hv_utils driver always fakes a
3feead
+ * CANCEL_FCOPY message upon suspend, and later when the VM resumes back,
3feead
+ * the daemon calls hv_copy_cancel() to remove the file; if a file is copied
3feead
+ * successfully before suspend, hv_copy_finished() must reset target_fname to
3feead
+ * avoid that the file can be incorrectly removed upon resume, since the faked
3feead
+ * CANCEL_FCOPY message is spurious in this case.
3feead
+ */
3feead
 static int hv_copy_finished(void)
3feead
 {
3feead
 	close(target_fd);
3feead
+	target_fname[0] = '\0';
3feead
 	return 0;
3feead
 }
3feead
 static int hv_copy_cancel(void)
3feead
 {
3feead
 	close(target_fd);
3feead
-	unlink(target_fname);
3feead
+	if (strlen(target_fname) > 0) {
3feead
+		unlink(target_fname);
3feead
+		target_fname[0] = '\0';
3feead
+	}
3feead
 	return 0;
3feead
 
3feead
 }
3feead
@@ -131,7 +147,7 @@ void print_usage(char *argv[])
3feead
 
3feead
 int main(int argc, char *argv[])
3feead
 {
3feead
-	int fcopy_fd;
3feead
+	int fcopy_fd = -1;
3feead
 	int error;
3feead
 	int daemonize = 1, long_index = 0, opt;
3feead
 	int version = FCOPY_CURRENT_VERSION;
3feead
@@ -141,7 +157,7 @@ int main(int argc, char *argv[])
3feead
 		struct hv_do_fcopy copy;
3feead
 		__u32 kernel_modver;
3feead
 	} buffer = { };
3feead
-	int in_handshake = 1;
3feead
+	int in_handshake;
3feead
 
3feead
 	static struct option long_options[] = {
3feead
 		{"help",	no_argument,	   0,  'h' },
3feead
@@ -170,6 +186,12 @@ int main(int argc, char *argv[])
3feead
 	openlog("HV_FCOPY", 0, LOG_USER);
3feead
 	syslog(LOG_INFO, "starting; pid is:%d", getpid());
3feead
 
3feead
+reopen_fcopy_fd:
3feead
+	if (fcopy_fd != -1)
3feead
+		close(fcopy_fd);
3feead
+	/* Remove any possible partially-copied file on error */
3feead
+	hv_copy_cancel();
3feead
+	in_handshake = 1;
3feead
 	fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
3feead
 
3feead
 	if (fcopy_fd < 0) {
3feead
@@ -196,7 +218,7 @@ int main(int argc, char *argv[])
3feead
 		len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
3feead
 		if (len < 0) {
3feead
 			syslog(LOG_ERR, "pread failed: %s", strerror(errno));
3feead
-			exit(EXIT_FAILURE);
3feead
+			goto reopen_fcopy_fd;
3feead
 		}
3feead
 
3feead
 		if (in_handshake) {
3feead
@@ -231,9 +253,14 @@ int main(int argc, char *argv[])
3feead
 
3feead
 		}
3feead
 
3feead
+		/*
3feead
+		 * pwrite() may return an error due to the faked CANCEL_FCOPY
3feead
+		 * message upon hibernation. Ignore the error by resetting the
3feead
+		 * dev file, i.e. closing and re-opening it.
3feead
+		 */
3feead
 		if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
3feead
 			syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
3feead
-			exit(EXIT_FAILURE);
3feead
+			goto reopen_fcopy_fd;
3feead
 		}
3feead
 	}
3feead
 }
3feead
diff --git a/hv_get_dhcp_info.sh b/hv_get_dhcp_info.sh
3feead
index c38686c..2f2a3c7 100644
3feead
--- a/hv_get_dhcp_info.sh
3feead
+++ b/hv_get_dhcp_info.sh
3feead
@@ -13,7 +13,7 @@
3feead
 #	the script prints the string "Disabled" to stdout.
3feead
 #
3feead
 # Each Distro is expected to implement this script in a distro specific
3feead
-# fashion. For instance on Distros that ship with Network Manager enabled,
3feead
+# fashion. For instance, on Distros that ship with Network Manager enabled,
3feead
 # this script can be based on the Network Manager APIs for retrieving DHCP
3feead
 # information.
3feead
 
3feead
diff --git a/hv_kvp_daemon.c b/hv_kvp_daemon.c
3feead
index e9ef4ca..0e5f14a 100644
3feead
--- a/hv_kvp_daemon.c
3feead
+++ b/hv_kvp_daemon.c
3feead
@@ -76,7 +76,7 @@ enum {
3feead
 	DNS
3feead
 };
3feead
 
3feead
-static int in_hand_shake = 1;
3feead
+static int in_hand_shake;
3feead
 
3feead
 static char *os_name = "";
3feead
 static char *os_major = "";
3feead
@@ -1360,7 +1360,7 @@ void print_usage(char *argv[])
3feead
 
3feead
 int main(int argc, char *argv[])
3feead
 {
3feead
-	int kvp_fd, len;
3feead
+	int kvp_fd = -1, len;
3feead
 	int error;
3feead
 	struct pollfd pfd;
3feead
 	char    *p;
3feead
@@ -1400,14 +1400,6 @@ int main(int argc, char *argv[])
3feead
 	openlog("KVP", 0, LOG_USER);
3feead
 	syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
3feead
 
3feead
-	kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
3feead
-
3feead
-	if (kvp_fd < 0) {
3feead
-		syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
3feead
-			errno, strerror(errno));
3feead
-		exit(EXIT_FAILURE);
3feead
-	}
3feead
-
3feead
 	/*
3feead
 	 * Retrieve OS release information.
3feead
 	 */
3feead
@@ -1423,6 +1415,18 @@ int main(int argc, char *argv[])
3feead
 		exit(EXIT_FAILURE);
3feead
 	}
3feead
 
3feead
+reopen_kvp_fd:
3feead
+	if (kvp_fd != -1)
3feead
+		close(kvp_fd);
3feead
+	in_hand_shake = 1;
3feead
+	kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
3feead
+
3feead
+	if (kvp_fd < 0) {
3feead
+		syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
3feead
+		       errno, strerror(errno));
3feead
+		exit(EXIT_FAILURE);
3feead
+	}
3feead
+
3feead
 	/*
3feead
 	 * Register ourselves with the kernel.
3feead
 	 */
3feead
@@ -1457,8 +1461,7 @@ int main(int argc, char *argv[])
3feead
 			syslog(LOG_ERR, "read failed; error:%d %s",
3feead
 			       errno, strerror(errno));
3feead
 
3feead
-			close(kvp_fd);
3feead
-			return EXIT_FAILURE;
3feead
+			goto reopen_kvp_fd;
3feead
 		}
3feead
 
3feead
 		/*
3feead
@@ -1617,13 +1620,17 @@ int main(int argc, char *argv[])
3feead
 			break;
3feead
 		}
3feead
 
3feead
-		/* Send the value back to the kernel. */
3feead
+		/*
3feead
+		 * Send the value back to the kernel. Note: the write() may
3feead
+		 * return an error due to hibernation; we can ignore the error
3feead
+		 * by resetting the dev file, i.e. closing and re-opening it.
3feead
+		 */
3feead
 kvp_done:
3feead
 		len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
3feead
 		if (len != sizeof(struct hv_kvp_msg)) {
3feead
 			syslog(LOG_ERR, "write failed; error: %d %s", errno,
3feead
 			       strerror(errno));
3feead
-			exit(EXIT_FAILURE);
3feead
+			goto reopen_kvp_fd;
3feead
 		}
3feead
 	}
3feead
 
3feead
diff --git a/hv_set_ifconfig.sh b/hv_set_ifconfig.sh
3feead
index 18b27cc..3dd064c 100644
3feead
--- a/hv_set_ifconfig.sh
3feead
+++ b/hv_set_ifconfig.sh
3feead
@@ -12,7 +12,7 @@
3feead
 # be used to configure the interface.
3feead
 #
3feead
 # Each Distro is expected to implement this script in a distro specific
3feead
-# fashion. For instance on Distros that ship with Network Manager enabled,
3feead
+# fashion. For instance, on Distros that ship with Network Manager enabled,
3feead
 # this script can be based on the Network Manager APIs for configuring the
3feead
 # interface.
3feead
 #
3feead
diff --git a/hv_vss_daemon.c b/hv_vss_daemon.c
3feead
index 92902a8..29a1e48 100644
3feead
--- a/hv_vss_daemon.c
3feead
+++ b/hv_vss_daemon.c
3feead
@@ -28,6 +28,8 @@
3feead
 #include <stdbool.h>
3feead
 #include <dirent.h>
3feead
 
3feead
+static bool fs_frozen;
3feead
+
3feead
 /* Don't use syslog() in the function since that can cause write to disk */
3feead
 static int vss_do_freeze(char *dir, unsigned int cmd)
3feead
 {
3feead
@@ -155,16 +157,22 @@ static int vss_operate(int operation)
3feead
 			continue;
3feead
 		}
3feead
 		error |= vss_do_freeze(ent->mnt_dir, cmd);
3feead
-		if (error && operation == VSS_OP_FREEZE)
3feead
-			goto err;
3feead
+		if (operation == VSS_OP_FREEZE) {
3feead
+			if (error)
3feead
+				goto err;
3feead
+			fs_frozen = true;
3feead
+		}
3feead
 	}
3feead
 
3feead
 	endmntent(mounts);
3feead
 
3feead
 	if (root_seen) {
3feead
 		error |= vss_do_freeze("/", cmd);
3feead
-		if (error && operation == VSS_OP_FREEZE)
3feead
-			goto err;
3feead
+		if (operation == VSS_OP_FREEZE) {
3feead
+			if (error)
3feead
+				goto err;
3feead
+			fs_frozen = true;
3feead
+		}
3feead
 	}
3feead
 
3feead
 	goto out;
3feead
@@ -175,6 +183,7 @@ err:
3feead
 		endmntent(mounts);
3feead
 	}
3feead
 	vss_operate(VSS_OP_THAW);
3feead
+	fs_frozen = false;
3feead
 	/* Call syslog after we thaw all filesystems */
3feead
 	if (ent)
3feead
 		syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
3feead
@@ -196,13 +205,13 @@ void print_usage(char *argv[])
3feead
 
3feead
 int main(int argc, char *argv[])
3feead
 {
3feead
-	int vss_fd, len;
3feead
+	int vss_fd = -1, len;
3feead
 	int error;
3feead
 	struct pollfd pfd;
3feead
 	int	op;
3feead
 	struct hv_vss_msg vss_msg[1];
3feead
 	int daemonize = 1, long_index = 0, opt;
3feead
-	int in_handshake = 1;
3feead
+	int in_handshake;
3feead
 	__u32 kernel_modver;
3feead
 
3feead
 	static struct option long_options[] = {
3feead
@@ -232,6 +241,18 @@ int main(int argc, char *argv[])
3feead
 	openlog("Hyper-V VSS", 0, LOG_USER);
3feead
 	syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
3feead
 
3feead
+reopen_vss_fd:
3feead
+	if (vss_fd != -1)
3feead
+		close(vss_fd);
3feead
+	if (fs_frozen) {
3feead
+		if (vss_operate(VSS_OP_THAW) || fs_frozen) {
3feead
+			syslog(LOG_ERR, "failed to thaw file system: err=%d",
3feead
+			       errno);
3feead
+			exit(EXIT_FAILURE);
3feead
+		}
3feead
+	}
3feead
+
3feead
+	in_handshake = 1;
3feead
 	vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
3feead
 	if (vss_fd < 0) {
3feead
 		syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
3feead
@@ -247,8 +268,7 @@ int main(int argc, char *argv[])
3feead
 	if (len < 0) {
3feead
 		syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
3feead
 		       errno, strerror(errno));
3feead
-		close(vss_fd);
3feead
-		exit(EXIT_FAILURE);
3feead
+		goto reopen_vss_fd;
3feead
 	}
3feead
 
3feead
 	pfd.fd = vss_fd;
3feead
@@ -312,14 +332,18 @@ int main(int argc, char *argv[])
3feead
 		default:
3feead
 			syslog(LOG_ERR, "Illegal op:%d\n", op);
3feead
 		}
3feead
+
3feead
+		/*
3feead
+		 * The write() may return an error due to the faked VSS_OP_THAW
3feead
+		 * message upon hibernation. Ignore the error by resetting the
3feead
+		 * dev file, i.e. closing and re-opening it.
3feead
+		 */
3feead
 		vss_msg->error = error;
3feead
 		len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
3feead
 		if (len != sizeof(struct hv_vss_msg)) {
3feead
 			syslog(LOG_ERR, "write failed; error: %d %s", errno,
3feead
 			       strerror(errno));
3feead
-
3feead
-			if (op == VSS_OP_FREEZE)
3feead
-				vss_operate(VSS_OP_THAW);
3feead
+			goto reopen_vss_fd;
3feead
 		}
3feead
 	}
3feead
 
3feead
-- 
3feead
2.27.0
3feead