2 * An implementation of host to guest copy functionality for Linux.
4 * Copyright (C) 2014, Microsoft, Inc.
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>
26 #include <linux/hyperv.h>
27 #include <linux/limits.h>
34 static char target_fname[PATH_MAX];
35 static unsigned long long filesize;
37 static int hv_start_fcopy(struct hv_start_fcopy *smsg)
39 int error = HV_E_FAIL;
43 p = (char *)smsg->path_name;
44 snprintf(target_fname, sizeof(target_fname), "%s/%s",
45 (char *)smsg->path_name, (char *)smsg->file_name);
47 syslog(LOG_INFO, "Target file name: %s", target_fname);
49 * Check to see if the path is already in place; if not,
52 while ((q = strchr(p, '/')) != NULL) {
58 if (access((char *)smsg->path_name, F_OK)) {
59 if (smsg->copy_flags & CREATE_PATH) {
60 if (mkdir((char *)smsg->path_name, 0755)) {
61 syslog(LOG_ERR, "Failed to create %s",
62 (char *)smsg->path_name);
66 syslog(LOG_ERR, "Invalid path: %s",
67 (char *)smsg->path_name);
75 if (!access(target_fname, F_OK)) {
76 syslog(LOG_INFO, "File: %s exists", target_fname);
77 if (!(smsg->copy_flags & OVER_WRITE)) {
78 error = HV_ERROR_ALREADY_EXISTS;
83 target_fd = open(target_fname,
84 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
85 if (target_fd == -1) {
86 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
95 static int hv_copy_data(struct hv_do_fcopy *cpmsg)
97 ssize_t bytes_written;
100 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
103 filesize += cpmsg->size;
104 if (bytes_written != cpmsg->size) {
107 ret = HV_ERROR_DISK_FULL;
113 syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
114 filesize, (long)bytes_written, strerror(errno));
120 static int hv_copy_finished(void)
125 static int hv_copy_cancel(void)
128 unlink(target_fname);
133 void print_usage(char *argv[])
135 fprintf(stderr, "Usage: %s [options]\n"
137 " -n, --no-daemon stay in foreground, don't daemonize\n"
138 " -h, --help print this help\n", argv[0]);
141 int main(int argc, char *argv[])
145 int daemonize = 1, long_index = 0, opt;
146 int version = FCOPY_CURRENT_VERSION;
148 struct hv_fcopy_hdr hdr;
149 struct hv_start_fcopy start;
150 struct hv_do_fcopy copy;
153 int in_handshake = 1;
155 static struct option long_options[] = {
156 {"help", no_argument, 0, 'h' },
157 {"no-daemon", no_argument, 0, 'n' },
161 while ((opt = getopt_long(argc, argv, "hn", long_options,
162 &long_index)) != -1) {
174 if (daemonize && daemon(1, 0)) {
175 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
179 openlog("HV_FCOPY", 0, LOG_USER);
180 syslog(LOG_INFO, "starting; pid is:%d", getpid());
182 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
185 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
186 errno, strerror(errno));
191 * Register with the kernel.
193 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
194 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
200 * In this loop we process fcopy messages after the
201 * handshake is complete.
205 len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
207 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
212 if (len != sizeof(buffer.kernel_modver)) {
213 syslog(LOG_ERR, "invalid version negotiation");
217 syslog(LOG_INFO, "kernel module version: %u",
218 buffer.kernel_modver);
222 switch (buffer.hdr.operation) {
223 case START_FILE_COPY:
224 error = hv_start_fcopy(&buffer.start);
227 error = hv_copy_data(&buffer.copy);
230 error = hv_copy_finished();
233 error = hv_copy_cancel();
238 syslog(LOG_ERR, "Unknown operation: %d",
239 buffer.hdr.operation);
243 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
244 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));