Blame hv_fcopy_daemon.c

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