Blame SOURCES/hv_vss_daemon.c

b19ffd
/*
b19ffd
 * An implementation of the host initiated guest snapshot for Hyper-V.
b19ffd
 *
b19ffd
 *
b19ffd
 * Copyright (C) 2013, Microsoft, Inc.
b19ffd
 * Author : K. Y. Srinivasan <kys@microsoft.com>
b19ffd
 *
b19ffd
 * This program is free software; you can redistribute it and/or modify it
b19ffd
 * under the terms of the GNU General Public License version 2 as published
b19ffd
 * by the Free Software Foundation.
b19ffd
 *
b19ffd
 * This program is distributed in the hope that it will be useful, but
b19ffd
 * WITHOUT ANY WARRANTY; without even the implied warranty of
b19ffd
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
b19ffd
 * NON INFRINGEMENT.  See the GNU General Public License for more
b19ffd
 * details.
b19ffd
 *
b19ffd
 */
b19ffd
b19ffd
b19ffd
#include <sys/types.h>
b19ffd
#include <sys/poll.h>
b19ffd
#include <sys/ioctl.h>
b19ffd
#include <sys/stat.h>
b19ffd
#include <sys/sysmacros.h>
b19ffd
#include <fcntl.h>
b19ffd
#include <stdio.h>
b19ffd
#include <mntent.h>
b19ffd
#include <stdlib.h>
b19ffd
#include <unistd.h>
b19ffd
#include <string.h>
b19ffd
#include <ctype.h>
b19ffd
#include <errno.h>
b19ffd
#include <linux/fs.h>
b19ffd
#include <linux/major.h>
b19ffd
#include <linux/hyperv.h>
b19ffd
#include <syslog.h>
b19ffd
#include <getopt.h>
b19ffd
b19ffd
/* Don't use syslog() in the function since that can cause write to disk */
b19ffd
static int vss_do_freeze(char *dir, unsigned int cmd)
b19ffd
{
b19ffd
	int ret, fd = open(dir, O_RDONLY);
b19ffd
b19ffd
	if (fd < 0)
b19ffd
		return 1;
b19ffd
b19ffd
	ret = ioctl(fd, cmd, 0);
b19ffd
b19ffd
	/*
b19ffd
	 * If a partition is mounted more than once, only the first
b19ffd
	 * FREEZE/THAW can succeed and the later ones will get
b19ffd
	 * EBUSY/EINVAL respectively: there could be 2 cases:
b19ffd
	 * 1) a user may mount the same partition to differnt directories
b19ffd
	 *  by mistake or on purpose;
b19ffd
	 * 2) The subvolume of btrfs appears to have the same partition
b19ffd
	 * mounted more than once.
b19ffd
	 */
b19ffd
	if (ret) {
b19ffd
		if ((cmd == FIFREEZE && errno == EBUSY) ||
b19ffd
		    (cmd == FITHAW && errno == EINVAL)) {
b19ffd
			close(fd);
b19ffd
			return 0;
b19ffd
		}
b19ffd
	}
b19ffd
b19ffd
	close(fd);
b19ffd
	return !!ret;
b19ffd
}
b19ffd
b19ffd
static int vss_operate(int operation)
b19ffd
{
b19ffd
	char match[] = "/dev/";
b19ffd
	FILE *mounts;
b19ffd
	struct mntent *ent;
b19ffd
	struct stat sb;
b19ffd
	char errdir[1024] = {0};
b19ffd
	unsigned int cmd;
b19ffd
	int error = 0, root_seen = 0, save_errno = 0;
b19ffd
b19ffd
	switch (operation) {
b19ffd
	case VSS_OP_FREEZE:
b19ffd
		cmd = FIFREEZE;
b19ffd
		break;
b19ffd
	case VSS_OP_THAW:
b19ffd
		cmd = FITHAW;
b19ffd
		break;
b19ffd
	default:
b19ffd
		return -1;
b19ffd
	}
b19ffd
b19ffd
	mounts = setmntent("/proc/mounts", "r");
b19ffd
	if (mounts == NULL)
b19ffd
		return -1;
b19ffd
b19ffd
	while ((ent = getmntent(mounts))) {
b19ffd
		if (strncmp(ent->mnt_fsname, match, strlen(match)))
b19ffd
			continue;
b19ffd
		if (stat(ent->mnt_fsname, &sb) == -1)
b19ffd
			continue;
b19ffd
		if (S_ISBLK(sb.st_mode) && major(sb.st_rdev) == LOOP_MAJOR)
b19ffd
			continue;
b19ffd
		if (hasmntopt(ent, MNTOPT_RO) != NULL)
b19ffd
			continue;
b19ffd
		if (strcmp(ent->mnt_type, "vfat") == 0)
b19ffd
			continue;
b19ffd
		if (strcmp(ent->mnt_dir, "/") == 0) {
b19ffd
			root_seen = 1;
b19ffd
			continue;
b19ffd
		}
b19ffd
		error |= vss_do_freeze(ent->mnt_dir, cmd);
b19ffd
		if (error && operation == VSS_OP_FREEZE)
b19ffd
			goto err;
b19ffd
	}
b19ffd
b19ffd
	endmntent(mounts);
b19ffd
b19ffd
	if (root_seen) {
b19ffd
		error |= vss_do_freeze("/", cmd);
b19ffd
		if (error && operation == VSS_OP_FREEZE)
b19ffd
			goto err;
b19ffd
	}
b19ffd
b19ffd
	goto out;
b19ffd
err:
b19ffd
	save_errno = errno;
b19ffd
	if (ent) {
b19ffd
		strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
b19ffd
		endmntent(mounts);
b19ffd
	}
b19ffd
	vss_operate(VSS_OP_THAW);
b19ffd
	/* Call syslog after we thaw all filesystems */
b19ffd
	if (ent)
b19ffd
		syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
b19ffd
		       errdir, save_errno, strerror(save_errno));
b19ffd
	else
b19ffd
		syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
b19ffd
		       strerror(save_errno));
b19ffd
out:
b19ffd
	return error;
b19ffd
}
b19ffd
b19ffd
void print_usage(char *argv[])
b19ffd
{
b19ffd
	fprintf(stderr, "Usage: %s [options]\n"
b19ffd
		"Options are:\n"
b19ffd
		"  -n, --no-daemon        stay in foreground, don't daemonize\n"
b19ffd
		"  -h, --help             print this help\n", argv[0]);
b19ffd
}
b19ffd
b19ffd
int main(int argc, char *argv[])
b19ffd
{
b19ffd
	int vss_fd, len;
b19ffd
	int error;
b19ffd
	struct pollfd pfd;
b19ffd
	int	op;
b19ffd
	struct hv_vss_msg vss_msg[1];
b19ffd
	int daemonize = 1, long_index = 0, opt;
b19ffd
	int in_handshake = 1;
b19ffd
	__u32 kernel_modver;
b19ffd
b19ffd
	static struct option long_options[] = {
b19ffd
		{"help",	no_argument,	   0,  'h' },
b19ffd
		{"no-daemon",	no_argument,	   0,  'n' },
b19ffd
		{0,		0,		   0,  0   }
b19ffd
	};
b19ffd
b19ffd
	while ((opt = getopt_long(argc, argv, "hn", long_options,
b19ffd
				  &long_index)) != -1) {
b19ffd
		switch (opt) {
b19ffd
		case 'n':
b19ffd
			daemonize = 0;
b19ffd
			break;
b19ffd
		case 'h':
b19ffd
		default:
b19ffd
			print_usage(argv);
b19ffd
			exit(EXIT_FAILURE);
b19ffd
		}
b19ffd
	}
b19ffd
b19ffd
	if (daemonize && daemon(1, 0))
b19ffd
		return 1;
b19ffd
b19ffd
	openlog("Hyper-V VSS", 0, LOG_USER);
b19ffd
	syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
b19ffd
b19ffd
	vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
b19ffd
	if (vss_fd < 0) {
b19ffd
		syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
b19ffd
		       errno, strerror(errno));
b19ffd
		exit(EXIT_FAILURE);
b19ffd
	}
b19ffd
	/*
b19ffd
	 * Register ourselves with the kernel.
b19ffd
	 */
b19ffd
	vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
b19ffd
b19ffd
	len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
b19ffd
	if (len < 0) {
b19ffd
		syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
b19ffd
		       errno, strerror(errno));
b19ffd
		close(vss_fd);
b19ffd
		exit(EXIT_FAILURE);
b19ffd
	}
b19ffd
b19ffd
	pfd.fd = vss_fd;
b19ffd
b19ffd
	while (1) {
b19ffd
		pfd.events = POLLIN;
b19ffd
		pfd.revents = 0;
b19ffd
b19ffd
		if (poll(&pfd, 1, -1) < 0) {
b19ffd
			syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
b19ffd
			if (errno == EINVAL) {
b19ffd
				close(vss_fd);
b19ffd
				exit(EXIT_FAILURE);
b19ffd
			}
b19ffd
			else
b19ffd
				continue;
b19ffd
		}
b19ffd
b19ffd
		len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
b19ffd
b19ffd
		if (in_handshake) {
b19ffd
			if (len != sizeof(kernel_modver)) {
b19ffd
				syslog(LOG_ERR, "invalid version negotiation");
b19ffd
				exit(EXIT_FAILURE);
b19ffd
			}
b19ffd
			kernel_modver = *(__u32 *)vss_msg;
b19ffd
			in_handshake = 0;
b19ffd
			syslog(LOG_INFO, "VSS: kernel module version: %d",
b19ffd
			       kernel_modver);
b19ffd
			continue;
b19ffd
		}
b19ffd
b19ffd
		if (len != sizeof(struct hv_vss_msg)) {
b19ffd
			syslog(LOG_ERR, "read failed; error:%d %s",
b19ffd
			       errno, strerror(errno));
b19ffd
			close(vss_fd);
b19ffd
			return EXIT_FAILURE;
b19ffd
		}
b19ffd
b19ffd
		op = vss_msg->vss_hdr.operation;
b19ffd
		error =  HV_S_OK;
b19ffd
b19ffd
		switch (op) {
b19ffd
		case VSS_OP_FREEZE:
b19ffd
		case VSS_OP_THAW:
b19ffd
			error = vss_operate(op);
b19ffd
			syslog(LOG_INFO, "VSS: op=%s: %s\n",
b19ffd
				op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
b19ffd
				error ? "failed" : "succeeded");
b19ffd
b19ffd
			if (error) {
b19ffd
				error = HV_E_FAIL;
b19ffd
				syslog(LOG_ERR, "op=%d failed!", op);
b19ffd
				syslog(LOG_ERR, "report it with these files:");
b19ffd
				syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
b19ffd
			}
b19ffd
			break;
b19ffd
		case VSS_OP_HOT_BACKUP:
b19ffd
			syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
b19ffd
			break;
b19ffd
		default:
b19ffd
			syslog(LOG_ERR, "Illegal op:%d\n", op);
b19ffd
		}
b19ffd
		vss_msg->error = error;
b19ffd
		len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
b19ffd
		if (len != sizeof(struct hv_vss_msg)) {
b19ffd
			syslog(LOG_ERR, "write failed; error: %d %s", errno,
b19ffd
			       strerror(errno));
b19ffd
b19ffd
			if (op == VSS_OP_FREEZE)
b19ffd
				vss_operate(VSS_OP_THAW);
b19ffd
		}
b19ffd
	}
b19ffd
b19ffd
	close(vss_fd);
b19ffd
	exit(0);
b19ffd
}