blob: 785f4e95148cf13a48458c00147162ef503d6ef6 [file] [log] [blame]
K. Y. Srinivasan013254762014-02-16 11:34:30 -08001/*
2 * An implementation of host to guest copy functionality for Linux.
3 *
4 * Copyright (C) 2014, Microsoft, Inc.
5 *
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
7 *
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.
11 *
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
16 * details.
17 */
18
19
20#include <sys/types.h>
K. Y. Srinivasan013254762014-02-16 11:34:30 -080021#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
K. Y. Srinivasan013254762014-02-16 11:34:30 -080024#include <errno.h>
25#include <linux/hyperv.h>
Dexuan Cui1330fc32018-03-04 22:17:14 -070026#include <linux/limits.h>
K. Y. Srinivasan013254762014-02-16 11:34:30 -080027#include <syslog.h>
28#include <sys/stat.h>
29#include <fcntl.h>
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +020030#include <getopt.h>
K. Y. Srinivasan013254762014-02-16 11:34:30 -080031
32static int target_fd;
Dexuan Cui1330fc32018-03-04 22:17:14 -070033static char target_fname[PATH_MAX];
Olaf Heringb4ed5d12015-12-14 16:01:34 -080034static unsigned long long filesize;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080035
36static int hv_start_fcopy(struct hv_start_fcopy *smsg)
37{
38 int error = HV_E_FAIL;
39 char *q, *p;
40
Olaf Heringb4ed5d12015-12-14 16:01:34 -080041 filesize = 0;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080042 p = (char *)smsg->path_name;
43 snprintf(target_fname, sizeof(target_fname), "%s/%s",
Vitaly Kuznetsovaba474b2015-01-09 22:18:54 -080044 (char *)smsg->path_name, (char *)smsg->file_name);
K. Y. Srinivasan013254762014-02-16 11:34:30 -080045
46 syslog(LOG_INFO, "Target file name: %s", target_fname);
47 /*
48 * Check to see if the path is already in place; if not,
49 * create if required.
50 */
51 while ((q = strchr(p, '/')) != NULL) {
52 if (q == p) {
53 p++;
54 continue;
55 }
56 *q = '\0';
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);
62 goto done;
63 }
64 } else {
65 syslog(LOG_ERR, "Invalid path: %s",
66 (char *)smsg->path_name);
67 goto done;
68 }
69 }
70 p = q + 1;
71 *q = '/';
72 }
73
74 if (!access(target_fname, F_OK)) {
75 syslog(LOG_INFO, "File: %s exists", target_fname);
K. Y. Srinivasan314672a2014-04-09 17:24:16 -070076 if (!(smsg->copy_flags & OVER_WRITE)) {
77 error = HV_ERROR_ALREADY_EXISTS;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080078 goto done;
K. Y. Srinivasan314672a2014-04-09 17:24:16 -070079 }
K. Y. Srinivasan013254762014-02-16 11:34:30 -080080 }
81
Yue Zhange013ac32014-06-27 18:19:48 -070082 target_fd = open(target_fname,
83 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
K. Y. Srinivasan013254762014-02-16 11:34:30 -080084 if (target_fd == -1) {
85 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
86 goto done;
87 }
88
89 error = 0;
90done:
91 return error;
92}
93
94static int hv_copy_data(struct hv_do_fcopy *cpmsg)
95{
96 ssize_t bytes_written;
Olaf Heringb4ed5d12015-12-14 16:01:34 -080097 int ret = 0;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080098
99 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
100 cpmsg->offset);
101
Olaf Heringb4ed5d12015-12-14 16:01:34 -0800102 filesize += cpmsg->size;
103 if (bytes_written != cpmsg->size) {
104 switch (errno) {
105 case ENOSPC:
106 ret = HV_ERROR_DISK_FULL;
107 break;
108 default:
109 ret = HV_E_FAIL;
110 break;
111 }
112 syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
113 filesize, (long)bytes_written, strerror(errno));
114 }
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800115
Olaf Heringb4ed5d12015-12-14 16:01:34 -0800116 return ret;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800117}
118
119static int hv_copy_finished(void)
120{
121 close(target_fd);
122 return 0;
123}
124static int hv_copy_cancel(void)
125{
126 close(target_fd);
127 unlink(target_fname);
128 return 0;
129
130}
131
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200132void print_usage(char *argv[])
133{
134 fprintf(stderr, "Usage: %s [options]\n"
135 "Options are:\n"
136 " -n, --no-daemon stay in foreground, don't daemonize\n"
137 " -h, --help print this help\n", argv[0]);
138}
139
140int main(int argc, char *argv[])
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800141{
Olaf Hering3f2baa82017-08-10 15:45:16 -0700142 int fcopy_fd;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800143 int error;
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200144 int daemonize = 1, long_index = 0, opt;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800145 int version = FCOPY_CURRENT_VERSION;
Olaf Hering3f2baa82017-08-10 15:45:16 -0700146 union {
147 struct hv_fcopy_hdr hdr;
148 struct hv_start_fcopy start;
149 struct hv_do_fcopy copy;
150 __u32 kernel_modver;
151 } buffer = { };
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700152 int in_handshake = 1;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800153
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200154 static struct option long_options[] = {
155 {"help", no_argument, 0, 'h' },
156 {"no-daemon", no_argument, 0, 'n' },
157 {0, 0, 0, 0 }
158 };
159
160 while ((opt = getopt_long(argc, argv, "hn", long_options,
161 &long_index)) != -1) {
162 switch (opt) {
163 case 'n':
164 daemonize = 0;
165 break;
166 case 'h':
167 default:
168 print_usage(argv);
169 exit(EXIT_FAILURE);
170 }
171 }
172
173 if (daemonize && daemon(1, 0)) {
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800174 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
175 exit(EXIT_FAILURE);
176 }
177
178 openlog("HV_FCOPY", 0, LOG_USER);
Olaf Hering6dfb8672015-12-14 16:01:35 -0800179 syslog(LOG_INFO, "starting; pid is:%d", getpid());
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800180
181 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
182
183 if (fcopy_fd < 0) {
184 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
185 errno, strerror(errno));
186 exit(EXIT_FAILURE);
187 }
188
189 /*
190 * Register with the kernel.
191 */
192 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
193 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
194 exit(EXIT_FAILURE);
195 }
196
197 while (1) {
198 /*
199 * In this loop we process fcopy messages after the
200 * handshake is complete.
201 */
Olaf Hering3f2baa82017-08-10 15:45:16 -0700202 ssize_t len;
203
204 len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800205 if (len < 0) {
206 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
207 exit(EXIT_FAILURE);
208 }
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700209
210 if (in_handshake) {
Olaf Hering3f2baa82017-08-10 15:45:16 -0700211 if (len != sizeof(buffer.kernel_modver)) {
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700212 syslog(LOG_ERR, "invalid version negotiation");
213 exit(EXIT_FAILURE);
214 }
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700215 in_handshake = 0;
Olaf Hering3f2baa82017-08-10 15:45:16 -0700216 syslog(LOG_INFO, "kernel module version: %u",
217 buffer.kernel_modver);
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700218 continue;
219 }
220
Olaf Hering3f2baa82017-08-10 15:45:16 -0700221 switch (buffer.hdr.operation) {
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800222 case START_FILE_COPY:
Olaf Hering3f2baa82017-08-10 15:45:16 -0700223 error = hv_start_fcopy(&buffer.start);
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800224 break;
225 case WRITE_TO_FILE:
Olaf Hering3f2baa82017-08-10 15:45:16 -0700226 error = hv_copy_data(&buffer.copy);
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800227 break;
228 case COMPLETE_FCOPY:
229 error = hv_copy_finished();
230 break;
231 case CANCEL_FCOPY:
232 error = hv_copy_cancel();
233 break;
234
235 default:
236 syslog(LOG_ERR, "Unknown operation: %d",
Olaf Hering3f2baa82017-08-10 15:45:16 -0700237 buffer.hdr.operation);
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800238
239 }
240
241 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
242 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
243 exit(EXIT_FAILURE);
244 }
245 }
246}