2 * An implementation of host to guest copy functionality for Linux.
4 * Copyright (C) 2014, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
20 #include <sys/types.h>
25 #include <linux/hyperv.h>
26 #include <linux/limits.h>
33 static char target_fname[PATH_MAX];
34 static unsigned long long filesize;
36 static int hv_start_fcopy(struct hv_start_fcopy *smsg)
38 int error = HV_E_FAIL;
42 p = (char *)smsg->path_name;
43 snprintf(target_fname, sizeof(target_fname), "%s/%s",
44 (char *)smsg->path_name, (char *)smsg->file_name);
46 syslog(LOG_INFO, "Target file name: %s", target_fname);
48 * Check to see if the path is already in place; if not,
51 while ((q = strchr(p, '/')) != NULL) {
57 if (access((char *)smsg->path_name, F_OK)) {
58 if (smsg->copy_flags & CREATE_PATH) {
59 if (mkdir((char *)smsg->path_name, 0755)) {
60 syslog(LOG_ERR, "Failed to create %s",
61 (char *)smsg->path_name);
65 syslog(LOG_ERR, "Invalid path: %s",
66 (char *)smsg->path_name);
74 if (!access(target_fname, F_OK)) {
75 syslog(LOG_INFO, "File: %s exists", target_fname);
76 if (!(smsg->copy_flags & OVER_WRITE)) {
77 error = HV_ERROR_ALREADY_EXISTS;
82 target_fd = open(target_fname,
83 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
84 if (target_fd == -1) {
85 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
94 static int hv_copy_data(struct hv_do_fcopy *cpmsg)
96 ssize_t bytes_written;
99 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
102 filesize += cpmsg->size;
103 if (bytes_written != cpmsg->size) {
106 ret = HV_ERROR_DISK_FULL;
112 syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
113 filesize, (long)bytes_written, strerror(errno));
119 static int hv_copy_finished(void)
124 static int hv_copy_cancel(void)
127 unlink(target_fname);
132 void print_usage(char *argv[])
134 fprintf(stderr, "Usage: %s [options]\n"
136 " -n, --no-daemon stay in foreground, don't daemonize\n"
137 " -h, --help print this help\n", argv[0]);
140 int main(int argc, char *argv[])
144 int daemonize = 1, long_index = 0, opt;
145 int version = FCOPY_CURRENT_VERSION;
147 struct hv_fcopy_hdr hdr;
148 struct hv_start_fcopy start;
149 struct hv_do_fcopy copy;
152 int in_handshake = 1;
154 static struct option long_options[] = {
155 {"help", no_argument, 0, 'h' },
156 {"no-daemon", no_argument, 0, 'n' },
160 while ((opt = getopt_long(argc, argv, "hn", long_options,
161 &long_index)) != -1) {
173 if (daemonize && daemon(1, 0)) {
174 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
178 openlog("HV_FCOPY", 0, LOG_USER);
179 syslog(LOG_INFO, "starting; pid is:%d", getpid());
181 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
184 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
185 errno, strerror(errno));
190 * Register with the kernel.
192 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
193 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
199 * In this loop we process fcopy messages after the
200 * handshake is complete.
204 len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
206 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
211 if (len != sizeof(buffer.kernel_modver)) {
212 syslog(LOG_ERR, "invalid version negotiation");
216 syslog(LOG_INFO, "kernel module version: %u",
217 buffer.kernel_modver);
221 switch (buffer.hdr.operation) {
222 case START_FILE_COPY:
223 error = hv_start_fcopy(&buffer.start);
226 error = hv_copy_data(&buffer.copy);
229 error = hv_copy_finished();
232 error = hv_copy_cancel();
237 syslog(LOG_ERR, "Unknown operation: %d",
238 buffer.hdr.operation);
242 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
243 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));