K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 1 | /* |
| 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. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <linux/hyperv.h> |
Dexuan Cui | 1330fc3 | 2018-03-04 22:17:14 -0700 | [diff] [blame^] | 26 | #include <linux/limits.h> |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 27 | #include <syslog.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 30 | #include <getopt.h> |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 31 | |
| 32 | static int target_fd; |
Dexuan Cui | 1330fc3 | 2018-03-04 22:17:14 -0700 | [diff] [blame^] | 33 | static char target_fname[PATH_MAX]; |
Olaf Hering | b4ed5d1 | 2015-12-14 16:01:34 -0800 | [diff] [blame] | 34 | static unsigned long long filesize; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 35 | |
| 36 | static int hv_start_fcopy(struct hv_start_fcopy *smsg) |
| 37 | { |
| 38 | int error = HV_E_FAIL; |
| 39 | char *q, *p; |
| 40 | |
Olaf Hering | b4ed5d1 | 2015-12-14 16:01:34 -0800 | [diff] [blame] | 41 | filesize = 0; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 42 | p = (char *)smsg->path_name; |
| 43 | snprintf(target_fname, sizeof(target_fname), "%s/%s", |
Vitaly Kuznetsov | aba474b | 2015-01-09 22:18:54 -0800 | [diff] [blame] | 44 | (char *)smsg->path_name, (char *)smsg->file_name); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 45 | |
| 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. Srinivasan | 314672a | 2014-04-09 17:24:16 -0700 | [diff] [blame] | 76 | if (!(smsg->copy_flags & OVER_WRITE)) { |
| 77 | error = HV_ERROR_ALREADY_EXISTS; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 78 | goto done; |
K. Y. Srinivasan | 314672a | 2014-04-09 17:24:16 -0700 | [diff] [blame] | 79 | } |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Yue Zhang | e013ac3 | 2014-06-27 18:19:48 -0700 | [diff] [blame] | 82 | target_fd = open(target_fname, |
| 83 | O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 84 | if (target_fd == -1) { |
| 85 | syslog(LOG_INFO, "Open Failed: %s", strerror(errno)); |
| 86 | goto done; |
| 87 | } |
| 88 | |
| 89 | error = 0; |
| 90 | done: |
| 91 | return error; |
| 92 | } |
| 93 | |
| 94 | static int hv_copy_data(struct hv_do_fcopy *cpmsg) |
| 95 | { |
| 96 | ssize_t bytes_written; |
Olaf Hering | b4ed5d1 | 2015-12-14 16:01:34 -0800 | [diff] [blame] | 97 | int ret = 0; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 98 | |
| 99 | bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size, |
| 100 | cpmsg->offset); |
| 101 | |
Olaf Hering | b4ed5d1 | 2015-12-14 16:01:34 -0800 | [diff] [blame] | 102 | 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. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 115 | |
Olaf Hering | b4ed5d1 | 2015-12-14 16:01:34 -0800 | [diff] [blame] | 116 | return ret; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int hv_copy_finished(void) |
| 120 | { |
| 121 | close(target_fd); |
| 122 | return 0; |
| 123 | } |
| 124 | static int hv_copy_cancel(void) |
| 125 | { |
| 126 | close(target_fd); |
| 127 | unlink(target_fname); |
| 128 | return 0; |
| 129 | |
| 130 | } |
| 131 | |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 132 | void 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 | |
| 140 | int main(int argc, char *argv[]) |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 141 | { |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 142 | int fcopy_fd; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 143 | int error; |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 144 | int daemonize = 1, long_index = 0, opt; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 145 | int version = FCOPY_CURRENT_VERSION; |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 146 | 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 Kuznetsov | a4d1ee5 | 2015-04-11 18:07:58 -0700 | [diff] [blame] | 152 | int in_handshake = 1; |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 153 | |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 154 | 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. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 174 | syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno)); |
| 175 | exit(EXIT_FAILURE); |
| 176 | } |
| 177 | |
| 178 | openlog("HV_FCOPY", 0, LOG_USER); |
Olaf Hering | 6dfb867 | 2015-12-14 16:01:35 -0800 | [diff] [blame] | 179 | syslog(LOG_INFO, "starting; pid is:%d", getpid()); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 180 | |
| 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 Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 202 | ssize_t len; |
| 203 | |
| 204 | len = pread(fcopy_fd, &buffer, sizeof(buffer), 0); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 205 | if (len < 0) { |
| 206 | syslog(LOG_ERR, "pread failed: %s", strerror(errno)); |
| 207 | exit(EXIT_FAILURE); |
| 208 | } |
Vitaly Kuznetsov | a4d1ee5 | 2015-04-11 18:07:58 -0700 | [diff] [blame] | 209 | |
| 210 | if (in_handshake) { |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 211 | if (len != sizeof(buffer.kernel_modver)) { |
Vitaly Kuznetsov | a4d1ee5 | 2015-04-11 18:07:58 -0700 | [diff] [blame] | 212 | syslog(LOG_ERR, "invalid version negotiation"); |
| 213 | exit(EXIT_FAILURE); |
| 214 | } |
Vitaly Kuznetsov | a4d1ee5 | 2015-04-11 18:07:58 -0700 | [diff] [blame] | 215 | in_handshake = 0; |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 216 | syslog(LOG_INFO, "kernel module version: %u", |
| 217 | buffer.kernel_modver); |
Vitaly Kuznetsov | a4d1ee5 | 2015-04-11 18:07:58 -0700 | [diff] [blame] | 218 | continue; |
| 219 | } |
| 220 | |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 221 | switch (buffer.hdr.operation) { |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 222 | case START_FILE_COPY: |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 223 | error = hv_start_fcopy(&buffer.start); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 224 | break; |
| 225 | case WRITE_TO_FILE: |
Olaf Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 226 | error = hv_copy_data(&buffer.copy); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 227 | 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 Hering | 3f2baa8 | 2017-08-10 15:45:16 -0700 | [diff] [blame] | 237 | buffer.hdr.operation); |
K. Y. Srinivasan | 01325476 | 2014-02-16 11:34:30 -0800 | [diff] [blame] | 238 | |
| 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 | } |