146f51
/*
146f51
 * An implementation of host to guest copy functionality for Linux.
146f51
 *
146f51
 * Copyright (C) 2014, Microsoft, Inc.
146f51
 *
146f51
 * Author : K. Y. Srinivasan <kys@microsoft.com>
146f51
 *
146f51
 * This program is free software; you can redistribute it and/or modify it
146f51
 * under the terms of the GNU General Public License version 2 as published
146f51
 * by the Free Software Foundation.
146f51
 *
146f51
 * This program is distributed in the hope that it will be useful, but
146f51
 * WITHOUT ANY WARRANTY; without even the implied warranty of
146f51
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
146f51
 * NON INFRINGEMENT.  See the GNU General Public License for more
146f51
 * details.
146f51
 */
146f51
146f51
146f51
#include <sys/types.h>
146f51
#include <stdio.h>
146f51
#include <stdlib.h>
146f51
#include <unistd.h>
146f51
#include <string.h>
146f51
#include <errno.h>
146f51
#include <linux/hyperv.h>
146f51
#include <linux/limits.h>
146f51
#include <syslog.h>
146f51
#include <sys/stat.h>
146f51
#include <fcntl.h>
146f51
#include <getopt.h>
146f51
146f51
static int target_fd;
146f51
static char target_fname[PATH_MAX];
146f51
static unsigned long long filesize;
146f51
146f51
static int hv_start_fcopy(struct hv_start_fcopy *smsg)
146f51
{
146f51
	int error = HV_E_FAIL;
146f51
	char *q, *p;
146f51
146f51
	filesize = 0;
146f51
	p = (char *)smsg->path_name;
146f51
	snprintf(target_fname, sizeof(target_fname), "%s/%s",
146f51
		 (char *)smsg->path_name, (char *)smsg->file_name);
146f51
146f51
	syslog(LOG_INFO, "Target file name: %s", target_fname);
146f51
	/*
146f51
	 * Check to see if the path is already in place; if not,
146f51
	 * create if required.
146f51
	 */
146f51
	while ((q = strchr(p, '/')) != NULL) {
146f51
		if (q == p) {
146f51
			p++;
146f51
			continue;
146f51
		}
146f51
		*q = '\0';
146f51
		if (access((char *)smsg->path_name, F_OK)) {
146f51
			if (smsg->copy_flags & CREATE_PATH) {
146f51
				if (mkdir((char *)smsg->path_name, 0755)) {
146f51
					syslog(LOG_ERR, "Failed to create %s",
146f51
						(char *)smsg->path_name);
146f51
					goto done;
146f51
				}
146f51
			} else {
146f51
				syslog(LOG_ERR, "Invalid path: %s",
146f51
					(char *)smsg->path_name);
146f51
				goto done;
146f51
			}
146f51
		}
146f51
		p = q + 1;
146f51
		*q = '/';
146f51
	}
146f51
146f51
	if (!access(target_fname, F_OK)) {
146f51
		syslog(LOG_INFO, "File: %s exists", target_fname);
146f51
		if (!(smsg->copy_flags & OVER_WRITE)) {
146f51
			error = HV_ERROR_ALREADY_EXISTS;
146f51
			goto done;
146f51
		}
146f51
	}
146f51
146f51
	target_fd = open(target_fname,
146f51
			 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
146f51
	if (target_fd == -1) {
146f51
		syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
146f51
		goto done;
146f51
	}
146f51
146f51
	error = 0;
146f51
done:
146f51
	return error;
146f51
}
146f51
146f51
static int hv_copy_data(struct hv_do_fcopy *cpmsg)
146f51
{
146f51
	ssize_t bytes_written;
146f51
	int ret = 0;
146f51
146f51
	bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
146f51
				cpmsg->offset);
146f51
146f51
	filesize += cpmsg->size;
146f51
	if (bytes_written != cpmsg->size) {
146f51
		switch (errno) {
146f51
		case ENOSPC:
146f51
			ret = HV_ERROR_DISK_FULL;
146f51
			break;
146f51
		default:
146f51
			ret = HV_E_FAIL;
146f51
			break;
146f51
		}
146f51
		syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
146f51
		       filesize, (long)bytes_written, strerror(errno));
146f51
	}
146f51
146f51
	return ret;
146f51
}
146f51
146f51
static int hv_copy_finished(void)
146f51
{
146f51
	close(target_fd);
146f51
	return 0;
146f51
}
146f51
static int hv_copy_cancel(void)
146f51
{
146f51
	close(target_fd);
146f51
	unlink(target_fname);
146f51
	return 0;
146f51
146f51
}
146f51
146f51
void print_usage(char *argv[])
146f51
{
146f51
	fprintf(stderr, "Usage: %s [options]\n"
146f51
		"Options are:\n"
146f51
		"  -n, --no-daemon        stay in foreground, don't daemonize\n"
146f51
		"  -h, --help             print this help\n", argv[0]);
146f51
}
146f51
146f51
int main(int argc, char *argv[])
146f51
{
146f51
	int fcopy_fd;
146f51
	int error;
146f51
	int daemonize = 1, long_index = 0, opt;
146f51
	int version = FCOPY_CURRENT_VERSION;
146f51
	union {
146f51
		struct hv_fcopy_hdr hdr;
146f51
		struct hv_start_fcopy start;
146f51
		struct hv_do_fcopy copy;
146f51
		__u32 kernel_modver;
146f51
	} buffer = { };
146f51
	int in_handshake = 1;
146f51
146f51
	static struct option long_options[] = {
146f51
		{"help",	no_argument,	   0,  'h' },
146f51
		{"no-daemon",	no_argument,	   0,  'n' },
146f51
		{0,		0,		   0,  0   }
146f51
	};
146f51
146f51
	while ((opt = getopt_long(argc, argv, "hn", long_options,
146f51
				  &long_index)) != -1) {
146f51
		switch (opt) {
146f51
		case 'n':
146f51
			daemonize = 0;
146f51
			break;
146f51
		case 'h':
146f51
		default:
146f51
			print_usage(argv);
146f51
			exit(EXIT_FAILURE);
146f51
		}
146f51
	}
146f51
146f51
	if (daemonize && daemon(1, 0)) {
146f51
		syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
146f51
		exit(EXIT_FAILURE);
146f51
	}
146f51
146f51
	openlog("HV_FCOPY", 0, LOG_USER);
146f51
	syslog(LOG_INFO, "starting; pid is:%d", getpid());
146f51
146f51
	fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
146f51
146f51
	if (fcopy_fd < 0) {
146f51
		syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
146f51
			errno, strerror(errno));
146f51
		exit(EXIT_FAILURE);
146f51
	}
146f51
146f51
	/*
146f51
	 * Register with the kernel.
146f51
	 */
146f51
	if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
146f51
		syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
146f51
		exit(EXIT_FAILURE);
146f51
	}
146f51
146f51
	while (1) {
146f51
		/*
146f51
		 * In this loop we process fcopy messages after the
146f51
		 * handshake is complete.
146f51
		 */
146f51
		ssize_t len;
146f51
146f51
		len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
146f51
		if (len < 0) {
146f51
			syslog(LOG_ERR, "pread failed: %s", strerror(errno));
146f51
			exit(EXIT_FAILURE);
146f51
		}
146f51
146f51
		if (in_handshake) {
146f51
			if (len != sizeof(buffer.kernel_modver)) {
146f51
				syslog(LOG_ERR, "invalid version negotiation");
146f51
				exit(EXIT_FAILURE);
146f51
			}
146f51
			in_handshake = 0;
146f51
			syslog(LOG_INFO, "kernel module version: %u",
146f51
			       buffer.kernel_modver);
146f51
			continue;
146f51
		}
146f51
146f51
		switch (buffer.hdr.operation) {
146f51
		case START_FILE_COPY:
146f51
			error = hv_start_fcopy(&buffer.start);
146f51
			break;
146f51
		case WRITE_TO_FILE:
146f51
			error = hv_copy_data(&buffer.copy);
146f51
			break;
146f51
		case COMPLETE_FCOPY:
146f51
			error = hv_copy_finished();
146f51
			break;
146f51
		case CANCEL_FCOPY:
146f51
			error = hv_copy_cancel();
146f51
			break;
146f51
146f51
		default:
146f51
			syslog(LOG_ERR, "Unknown operation: %d",
146f51
				buffer.hdr.operation);
146f51
146f51
		}
146f51
146f51
		if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
146f51
			syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
146f51
			exit(EXIT_FAILURE);
146f51
		}
146f51
	}
146f51
}