]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/hv/hv_fcopy_daemon.c
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>
32 static char target_fname
[W_MAX_PATH
];
33 static unsigned long long filesize
;
35 static int hv_start_fcopy(struct hv_start_fcopy
*smsg
)
37 int error
= HV_E_FAIL
;
41 p
= (char *)smsg
->path_name
;
42 snprintf(target_fname
, sizeof(target_fname
), "%s/%s",
43 (char *)smsg
->path_name
, (char *)smsg
->file_name
);
45 syslog(LOG_INFO
, "Target file name: %s", target_fname
);
47 * Check to see if the path is already in place; if not,
50 while ((q
= strchr(p
, '/')) != NULL
) {
56 if (access((char *)smsg
->path_name
, F_OK
)) {
57 if (smsg
->copy_flags
& CREATE_PATH
) {
58 if (mkdir((char *)smsg
->path_name
, 0755)) {
59 syslog(LOG_ERR
, "Failed to create %s",
60 (char *)smsg
->path_name
);
64 syslog(LOG_ERR
, "Invalid path: %s",
65 (char *)smsg
->path_name
);
73 if (!access(target_fname
, F_OK
)) {
74 syslog(LOG_INFO
, "File: %s exists", target_fname
);
75 if (!(smsg
->copy_flags
& OVER_WRITE
)) {
76 error
= HV_ERROR_ALREADY_EXISTS
;
81 target_fd
= open(target_fname
,
82 O_RDWR
| O_CREAT
| O_TRUNC
| O_CLOEXEC
, 0744);
83 if (target_fd
== -1) {
84 syslog(LOG_INFO
, "Open Failed: %s", strerror(errno
));
93 static int hv_copy_data(struct hv_do_fcopy
*cpmsg
)
95 ssize_t bytes_written
;
98 bytes_written
= pwrite(target_fd
, cpmsg
->data
, cpmsg
->size
,
101 filesize
+= cpmsg
->size
;
102 if (bytes_written
!= cpmsg
->size
) {
105 ret
= HV_ERROR_DISK_FULL
;
111 syslog(LOG_ERR
, "pwrite failed to write %llu bytes: %ld (%s)",
112 filesize
, (long)bytes_written
, strerror(errno
));
118 static int hv_copy_finished(void)
123 static int hv_copy_cancel(void)
126 unlink(target_fname
);
131 void print_usage(char *argv
[])
133 fprintf(stderr
, "Usage: %s [options]\n"
135 " -n, --no-daemon stay in foreground, don't daemonize\n"
136 " -h, --help print this help\n", argv
[0]);
139 int main(int argc
, char *argv
[])
143 int daemonize
= 1, long_index
= 0, opt
;
144 int version
= FCOPY_CURRENT_VERSION
;
145 char *buffer
[4096 * 2];
146 struct hv_fcopy_hdr
*in_msg
;
147 int in_handshake
= 1;
150 static struct option long_options
[] = {
151 {"help", no_argument
, 0, 'h' },
152 {"no-daemon", no_argument
, 0, 'n' },
156 while ((opt
= getopt_long(argc
, argv
, "hn", long_options
,
157 &long_index
)) != -1) {
169 if (daemonize
&& daemon(1, 0)) {
170 syslog(LOG_ERR
, "daemon() failed; error: %s", strerror(errno
));
174 openlog("HV_FCOPY", 0, LOG_USER
);
175 syslog(LOG_INFO
, "starting; pid is:%d", getpid());
177 fcopy_fd
= open("/dev/vmbus/hv_fcopy", O_RDWR
);
180 syslog(LOG_ERR
, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
181 errno
, strerror(errno
));
186 * Register with the kernel.
188 if ((write(fcopy_fd
, &version
, sizeof(int))) != sizeof(int)) {
189 syslog(LOG_ERR
, "Registration failed: %s", strerror(errno
));
195 * In this loop we process fcopy messages after the
196 * handshake is complete.
198 len
= pread(fcopy_fd
, buffer
, (4096 * 2), 0);
200 syslog(LOG_ERR
, "pread failed: %s", strerror(errno
));
205 if (len
!= sizeof(kernel_modver
)) {
206 syslog(LOG_ERR
, "invalid version negotiation");
209 kernel_modver
= *(__u32
*)buffer
;
211 syslog(LOG_INFO
, "kernel module version: %d",
216 in_msg
= (struct hv_fcopy_hdr
*)buffer
;
218 switch (in_msg
->operation
) {
219 case START_FILE_COPY
:
220 error
= hv_start_fcopy((struct hv_start_fcopy
*)in_msg
);
223 error
= hv_copy_data((struct hv_do_fcopy
*)in_msg
);
226 error
= hv_copy_finished();
229 error
= hv_copy_cancel();
233 syslog(LOG_ERR
, "Unknown operation: %d",
238 if (pwrite(fcopy_fd
, &error
, sizeof(int), 0) != sizeof(int)) {
239 syslog(LOG_ERR
, "pwrite failed: %s", strerror(errno
));