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>
21 #include <sys/socket.h>
23 #include <linux/types.h>
24 #include <linux/kdev_t.h>
31 #include <linux/hyperv.h>
39 static char target_fname
[W_MAX_PATH
];
40 static unsigned long long filesize
;
42 static int hv_start_fcopy(struct hv_start_fcopy
*smsg
)
44 int error
= HV_E_FAIL
;
48 p
= (char *)smsg
->path_name
;
49 snprintf(target_fname
, sizeof(target_fname
), "%s/%s",
50 (char *)smsg
->path_name
, (char *)smsg
->file_name
);
52 syslog(LOG_INFO
, "Target file name: %s", target_fname
);
54 * Check to see if the path is already in place; if not,
57 while ((q
= strchr(p
, '/')) != NULL
) {
63 if (access((char *)smsg
->path_name
, F_OK
)) {
64 if (smsg
->copy_flags
& CREATE_PATH
) {
65 if (mkdir((char *)smsg
->path_name
, 0755)) {
66 syslog(LOG_ERR
, "Failed to create %s",
67 (char *)smsg
->path_name
);
71 syslog(LOG_ERR
, "Invalid path: %s",
72 (char *)smsg
->path_name
);
80 if (!access(target_fname
, F_OK
)) {
81 syslog(LOG_INFO
, "File: %s exists", target_fname
);
82 if (!(smsg
->copy_flags
& OVER_WRITE
)) {
83 error
= HV_ERROR_ALREADY_EXISTS
;
88 target_fd
= open(target_fname
,
89 O_RDWR
| O_CREAT
| O_TRUNC
| O_CLOEXEC
, 0744);
90 if (target_fd
== -1) {
91 syslog(LOG_INFO
, "Open Failed: %s", strerror(errno
));
100 static int hv_copy_data(struct hv_do_fcopy
*cpmsg
)
102 ssize_t bytes_written
;
105 bytes_written
= pwrite(target_fd
, cpmsg
->data
, cpmsg
->size
,
108 filesize
+= cpmsg
->size
;
109 if (bytes_written
!= cpmsg
->size
) {
112 ret
= HV_ERROR_DISK_FULL
;
118 syslog(LOG_ERR
, "pwrite failed to write %llu bytes: %ld (%s)",
119 filesize
, (long)bytes_written
, strerror(errno
));
125 static int hv_copy_finished(void)
130 static int hv_copy_cancel(void)
133 unlink(target_fname
);
138 void print_usage(char *argv
[])
140 fprintf(stderr
, "Usage: %s [options]\n"
142 " -n, --no-daemon stay in foreground, don't daemonize\n"
143 " -h, --help print this help\n", argv
[0]);
146 int main(int argc
, char *argv
[])
150 int daemonize
= 1, long_index
= 0, opt
;
151 int version
= FCOPY_CURRENT_VERSION
;
152 char *buffer
[4096 * 2];
153 struct hv_fcopy_hdr
*in_msg
;
154 int in_handshake
= 1;
157 static struct option long_options
[] = {
158 {"help", no_argument
, 0, 'h' },
159 {"no-daemon", no_argument
, 0, 'n' },
163 while ((opt
= getopt_long(argc
, argv
, "hn", long_options
,
164 &long_index
)) != -1) {
176 if (daemonize
&& daemon(1, 0)) {
177 syslog(LOG_ERR
, "daemon() failed; error: %s", strerror(errno
));
181 openlog("HV_FCOPY", 0, LOG_USER
);
182 syslog(LOG_INFO
, "HV_FCOPY starting; pid is:%d", getpid());
184 fcopy_fd
= open("/dev/vmbus/hv_fcopy", O_RDWR
);
187 syslog(LOG_ERR
, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
188 errno
, strerror(errno
));
193 * Register with the kernel.
195 if ((write(fcopy_fd
, &version
, sizeof(int))) != sizeof(int)) {
196 syslog(LOG_ERR
, "Registration failed: %s", strerror(errno
));
202 * In this loop we process fcopy messages after the
203 * handshake is complete.
205 len
= pread(fcopy_fd
, buffer
, (4096 * 2), 0);
207 syslog(LOG_ERR
, "pread failed: %s", strerror(errno
));
212 if (len
!= sizeof(kernel_modver
)) {
213 syslog(LOG_ERR
, "invalid version negotiation");
216 kernel_modver
= *(__u32
*)buffer
;
218 syslog(LOG_INFO
, "HV_FCOPY: kernel module version: %d",
223 in_msg
= (struct hv_fcopy_hdr
*)buffer
;
225 switch (in_msg
->operation
) {
226 case START_FILE_COPY
:
227 error
= hv_start_fcopy((struct hv_start_fcopy
*)in_msg
);
230 error
= hv_copy_data((struct hv_do_fcopy
*)in_msg
);
233 error
= hv_copy_finished();
236 error
= hv_copy_cancel();
240 syslog(LOG_ERR
, "Unknown operation: %d",
245 if (pwrite(fcopy_fd
, &error
, sizeof(int), 0) != sizeof(int)) {
246 syslog(LOG_ERR
, "pwrite failed: %s", strerror(errno
));