1 // SPDX-License-Identifier: GPL-2.0-only
3 * An implementation of host to guest copy functionality for Linux.
5 * Copyright (C) 2014, Microsoft, Inc.
7 * Author : K. Y. Srinivasan <kys@microsoft.com>
11 #include <sys/types.h>
17 #include <linux/hyperv.h>
18 #include <linux/limits.h>
25 static char target_fname[PATH_MAX];
26 static unsigned long long filesize;
28 static int hv_start_fcopy(struct hv_start_fcopy *smsg)
30 int error = HV_E_FAIL;
34 p = (char *)smsg->path_name;
35 snprintf(target_fname, sizeof(target_fname), "%s/%s",
36 (char *)smsg->path_name, (char *)smsg->file_name);
38 syslog(LOG_INFO, "Target file name: %s", target_fname);
40 * Check to see if the path is already in place; if not,
43 while ((q = strchr(p, '/')) != NULL) {
49 if (access((char *)smsg->path_name, F_OK)) {
50 if (smsg->copy_flags & CREATE_PATH) {
51 if (mkdir((char *)smsg->path_name, 0755)) {
52 syslog(LOG_ERR, "Failed to create %s",
53 (char *)smsg->path_name);
57 syslog(LOG_ERR, "Invalid path: %s",
58 (char *)smsg->path_name);
66 if (!access(target_fname, F_OK)) {
67 syslog(LOG_INFO, "File: %s exists", target_fname);
68 if (!(smsg->copy_flags & OVER_WRITE)) {
69 error = HV_ERROR_ALREADY_EXISTS;
74 target_fd = open(target_fname,
75 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
76 if (target_fd == -1) {
77 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
86 static int hv_copy_data(struct hv_do_fcopy *cpmsg)
88 ssize_t bytes_written;
91 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
94 filesize += cpmsg->size;
95 if (bytes_written != cpmsg->size) {
98 ret = HV_ERROR_DISK_FULL;
104 syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
105 filesize, (long)bytes_written, strerror(errno));
111 static int hv_copy_finished(void)
116 static int hv_copy_cancel(void)
119 unlink(target_fname);
124 void print_usage(char *argv[])
126 fprintf(stderr, "Usage: %s [options]\n"
128 " -n, --no-daemon stay in foreground, don't daemonize\n"
129 " -h, --help print this help\n", argv[0]);
132 int main(int argc, char *argv[])
136 int daemonize = 1, long_index = 0, opt;
137 int version = FCOPY_CURRENT_VERSION;
139 struct hv_fcopy_hdr hdr;
140 struct hv_start_fcopy start;
141 struct hv_do_fcopy copy;
144 int in_handshake = 1;
146 static struct option long_options[] = {
147 {"help", no_argument, 0, 'h' },
148 {"no-daemon", no_argument, 0, 'n' },
152 while ((opt = getopt_long(argc, argv, "hn", long_options,
153 &long_index)) != -1) {
165 if (daemonize && daemon(1, 0)) {
166 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
170 openlog("HV_FCOPY", 0, LOG_USER);
171 syslog(LOG_INFO, "starting; pid is:%d", getpid());
173 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
176 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
177 errno, strerror(errno));
182 * Register with the kernel.
184 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
185 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
191 * In this loop we process fcopy messages after the
192 * handshake is complete.
196 len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
198 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
203 if (len != sizeof(buffer.kernel_modver)) {
204 syslog(LOG_ERR, "invalid version negotiation");
208 syslog(LOG_INFO, "kernel module version: %u",
209 buffer.kernel_modver);
213 switch (buffer.hdr.operation) {
214 case START_FILE_COPY:
215 error = hv_start_fcopy(&buffer.start);
218 error = hv_copy_data(&buffer.copy);
221 error = hv_copy_finished();
224 error = hv_copy_cancel();
229 syslog(LOG_ERR, "Unknown operation: %d",
230 buffer.hdr.operation);
234 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
235 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));