]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - tools/hv/hv_fcopy_daemon.c
Merge tag 'gfs2-for-5.6-2' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[linux.git] / tools / hv / hv_fcopy_daemon.c
index aea2d91ab36459103fa519a7b48f79921e6eb02e..16d629b22c2542824830a403faa5b78a6b179c51 100644 (file)
@@ -80,6 +80,8 @@ static int hv_start_fcopy(struct hv_start_fcopy *smsg)
 
        error = 0;
 done:
+       if (error)
+               target_fname[0] = '\0';
        return error;
 }
 
@@ -108,15 +110,29 @@ static int hv_copy_data(struct hv_do_fcopy *cpmsg)
        return ret;
 }
 
+/*
+ * Reset target_fname to "" in the two below functions for hibernation: if
+ * the fcopy operation is aborted by hibernation, the daemon should remove the
+ * partially-copied file; to achieve this, the hv_utils driver always fakes a
+ * CANCEL_FCOPY message upon suspend, and later when the VM resumes back,
+ * the daemon calls hv_copy_cancel() to remove the file; if a file is copied
+ * successfully before suspend, hv_copy_finished() must reset target_fname to
+ * avoid that the file can be incorrectly removed upon resume, since the faked
+ * CANCEL_FCOPY message is spurious in this case.
+ */
 static int hv_copy_finished(void)
 {
        close(target_fd);
+       target_fname[0] = '\0';
        return 0;
 }
 static int hv_copy_cancel(void)
 {
        close(target_fd);
-       unlink(target_fname);
+       if (strlen(target_fname) > 0) {
+               unlink(target_fname);
+               target_fname[0] = '\0';
+       }
        return 0;
 
 }
@@ -131,7 +147,7 @@ void print_usage(char *argv[])
 
 int main(int argc, char *argv[])
 {
-       int fcopy_fd;
+       int fcopy_fd = -1;
        int error;
        int daemonize = 1, long_index = 0, opt;
        int version = FCOPY_CURRENT_VERSION;
@@ -141,7 +157,7 @@ int main(int argc, char *argv[])
                struct hv_do_fcopy copy;
                __u32 kernel_modver;
        } buffer = { };
-       int in_handshake = 1;
+       int in_handshake;
 
        static struct option long_options[] = {
                {"help",        no_argument,       0,  'h' },
@@ -170,6 +186,12 @@ int main(int argc, char *argv[])
        openlog("HV_FCOPY", 0, LOG_USER);
        syslog(LOG_INFO, "starting; pid is:%d", getpid());
 
+reopen_fcopy_fd:
+       if (fcopy_fd != -1)
+               close(fcopy_fd);
+       /* Remove any possible partially-copied file on error */
+       hv_copy_cancel();
+       in_handshake = 1;
        fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
 
        if (fcopy_fd < 0) {
@@ -196,7 +218,7 @@ int main(int argc, char *argv[])
                len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
                if (len < 0) {
                        syslog(LOG_ERR, "pread failed: %s", strerror(errno));
-                       exit(EXIT_FAILURE);
+                       goto reopen_fcopy_fd;
                }
 
                if (in_handshake) {
@@ -231,9 +253,14 @@ int main(int argc, char *argv[])
 
                }
 
+               /*
+                * pwrite() may return an error due to the faked CANCEL_FCOPY
+                * message upon hibernation. Ignore the error by resetting the
+                * dev file, i.e. closing and re-opening it.
+                */
                if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
                        syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
-                       exit(EXIT_FAILURE);
+                       goto reopen_fcopy_fd;
                }
        }
 }