blob: 26ae609a94488b1059515dfcb059c3aa3d3af41a [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>
26#include <syslog.h>
27#include <sys/stat.h>
28#include <fcntl.h>
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +020029#include <getopt.h>
K. Y. Srinivasan013254762014-02-16 11:34:30 -080030
31static int target_fd;
32static char target_fname[W_MAX_PATH];
Olaf Heringb4ed5d12015-12-14 16:01:34 -080033static unsigned long long filesize;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080034
35static int hv_start_fcopy(struct hv_start_fcopy *smsg)
36{
37 int error = HV_E_FAIL;
38 char *q, *p;
39
Olaf Heringb4ed5d12015-12-14 16:01:34 -080040 filesize = 0;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080041 p = (char *)smsg->path_name;
42 snprintf(target_fname, sizeof(target_fname), "%s/%s",
Vitaly Kuznetsovaba474b2015-01-09 22:18:54 -080043 (char *)smsg->path_name, (char *)smsg->file_name);
K. Y. Srinivasan013254762014-02-16 11:34:30 -080044
45 syslog(LOG_INFO, "Target file name: %s", target_fname);
46 /*
47 * Check to see if the path is already in place; if not,
48 * create if required.
49 */
50 while ((q = strchr(p, '/')) != NULL) {
51 if (q == p) {
52 p++;
53 continue;
54 }
55 *q = '\0';
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);
61 goto done;
62 }
63 } else {
64 syslog(LOG_ERR, "Invalid path: %s",
65 (char *)smsg->path_name);
66 goto done;
67 }
68 }
69 p = q + 1;
70 *q = '/';
71 }
72
73 if (!access(target_fname, F_OK)) {
74 syslog(LOG_INFO, "File: %s exists", target_fname);
K. Y. Srinivasan314672a2014-04-09 17:24:16 -070075 if (!(smsg->copy_flags & OVER_WRITE)) {
76 error = HV_ERROR_ALREADY_EXISTS;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080077 goto done;
K. Y. Srinivasan314672a2014-04-09 17:24:16 -070078 }
K. Y. Srinivasan013254762014-02-16 11:34:30 -080079 }
80
Yue Zhange013ac32014-06-27 18:19:48 -070081 target_fd = open(target_fname,
82 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
K. Y. Srinivasan013254762014-02-16 11:34:30 -080083 if (target_fd == -1) {
84 syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
85 goto done;
86 }
87
88 error = 0;
89done:
90 return error;
91}
92
93static int hv_copy_data(struct hv_do_fcopy *cpmsg)
94{
95 ssize_t bytes_written;
Olaf Heringb4ed5d12015-12-14 16:01:34 -080096 int ret = 0;
K. Y. Srinivasan013254762014-02-16 11:34:30 -080097
98 bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
99 cpmsg->offset);
100
Olaf Heringb4ed5d12015-12-14 16:01:34 -0800101 filesize += cpmsg->size;
102 if (bytes_written != cpmsg->size) {
103 switch (errno) {
104 case ENOSPC:
105 ret = HV_ERROR_DISK_FULL;
106 break;
107 default:
108 ret = HV_E_FAIL;
109 break;
110 }
111 syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
112 filesize, (long)bytes_written, strerror(errno));
113 }
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800114
Olaf Heringb4ed5d12015-12-14 16:01:34 -0800115 return ret;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800116}
117
118static int hv_copy_finished(void)
119{
120 close(target_fd);
121 return 0;
122}
123static int hv_copy_cancel(void)
124{
125 close(target_fd);
126 unlink(target_fname);
127 return 0;
128
129}
130
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200131void print_usage(char *argv[])
132{
133 fprintf(stderr, "Usage: %s [options]\n"
134 "Options are:\n"
135 " -n, --no-daemon stay in foreground, don't daemonize\n"
136 " -h, --help print this help\n", argv[0]);
137}
138
139int main(int argc, char *argv[])
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800140{
Vitaly Kuznetsovaba474b2015-01-09 22:18:54 -0800141 int fcopy_fd, len;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800142 int error;
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200143 int daemonize = 1, long_index = 0, opt;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800144 int version = FCOPY_CURRENT_VERSION;
145 char *buffer[4096 * 2];
146 struct hv_fcopy_hdr *in_msg;
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700147 int in_handshake = 1;
148 __u32 kernel_modver;
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800149
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200150 static struct option long_options[] = {
151 {"help", no_argument, 0, 'h' },
152 {"no-daemon", no_argument, 0, 'n' },
153 {0, 0, 0, 0 }
154 };
155
156 while ((opt = getopt_long(argc, argv, "hn", long_options,
157 &long_index)) != -1) {
158 switch (opt) {
159 case 'n':
160 daemonize = 0;
161 break;
162 case 'h':
163 default:
164 print_usage(argv);
165 exit(EXIT_FAILURE);
166 }
167 }
168
169 if (daemonize && daemon(1, 0)) {
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800170 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
171 exit(EXIT_FAILURE);
172 }
173
174 openlog("HV_FCOPY", 0, LOG_USER);
Olaf Hering6dfb8672015-12-14 16:01:35 -0800175 syslog(LOG_INFO, "starting; pid is:%d", getpid());
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800176
177 fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
178
179 if (fcopy_fd < 0) {
180 syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
181 errno, strerror(errno));
182 exit(EXIT_FAILURE);
183 }
184
185 /*
186 * Register with the kernel.
187 */
188 if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
189 syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
190 exit(EXIT_FAILURE);
191 }
192
193 while (1) {
194 /*
195 * In this loop we process fcopy messages after the
196 * handshake is complete.
197 */
198 len = pread(fcopy_fd, buffer, (4096 * 2), 0);
199 if (len < 0) {
200 syslog(LOG_ERR, "pread failed: %s", strerror(errno));
201 exit(EXIT_FAILURE);
202 }
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700203
204 if (in_handshake) {
205 if (len != sizeof(kernel_modver)) {
206 syslog(LOG_ERR, "invalid version negotiation");
207 exit(EXIT_FAILURE);
208 }
209 kernel_modver = *(__u32 *)buffer;
210 in_handshake = 0;
Olaf Hering6dfb8672015-12-14 16:01:35 -0800211 syslog(LOG_INFO, "kernel module version: %d",
Vitaly Kuznetsova4d1ee52015-04-11 18:07:58 -0700212 kernel_modver);
213 continue;
214 }
215
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800216 in_msg = (struct hv_fcopy_hdr *)buffer;
217
218 switch (in_msg->operation) {
219 case START_FILE_COPY:
220 error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
221 break;
222 case WRITE_TO_FILE:
223 error = hv_copy_data((struct hv_do_fcopy *)in_msg);
224 break;
225 case COMPLETE_FCOPY:
226 error = hv_copy_finished();
227 break;
228 case CANCEL_FCOPY:
229 error = hv_copy_cancel();
230 break;
231
232 default:
233 syslog(LOG_ERR, "Unknown operation: %d",
234 in_msg->operation);
235
236 }
237
238 if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
239 syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
240 exit(EXIT_FAILURE);
241 }
242 }
243}