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