146f51
/*
146f51
 * An implementation of the host initiated guest snapshot for Hyper-V.
146f51
 *
146f51
 *
146f51
 * Copyright (C) 2013, Microsoft, Inc.
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
146f51
#include <sys/types.h>
146f51
#include <sys/poll.h>
146f51
#include <sys/ioctl.h>
146f51
#include <sys/stat.h>
146f51
#include <sys/sysmacros.h>
146f51
#include <fcntl.h>
146f51
#include <stdio.h>
146f51
#include <mntent.h>
146f51
#include <stdlib.h>
146f51
#include <unistd.h>
146f51
#include <string.h>
146f51
#include <ctype.h>
146f51
#include <errno.h>
146f51
#include <linux/fs.h>
146f51
#include <linux/major.h>
146f51
#include <linux/hyperv.h>
146f51
#include <syslog.h>
146f51
#include <getopt.h>
146f51
146f51
/* Don't use syslog() in the function since that can cause write to disk */
146f51
static int vss_do_freeze(char *dir, unsigned int cmd)
146f51
{
146f51
	int ret, fd = open(dir, O_RDONLY);
146f51
146f51
	if (fd < 0)
146f51
		return 1;
146f51
146f51
	ret = ioctl(fd, cmd, 0);
146f51
146f51
	/*
146f51
	 * If a partition is mounted more than once, only the first
146f51
	 * FREEZE/THAW can succeed and the later ones will get
146f51
	 * EBUSY/EINVAL respectively: there could be 2 cases:
146f51
	 * 1) a user may mount the same partition to differnt directories
146f51
	 *  by mistake or on purpose;
146f51
	 * 2) The subvolume of btrfs appears to have the same partition
146f51
	 * mounted more than once.
146f51
	 */
146f51
	if (ret) {
146f51
		if ((cmd == FIFREEZE && errno == EBUSY) ||
146f51
		    (cmd == FITHAW && errno == EINVAL)) {
146f51
			close(fd);
146f51
			return 0;
146f51
		}
146f51
	}
146f51
146f51
	close(fd);
146f51
	return !!ret;
146f51
}
146f51
146f51
static int vss_operate(int operation)
146f51
{
146f51
	char match[] = "/dev/";
146f51
	FILE *mounts;
146f51
	struct mntent *ent;
146f51
	struct stat sb;
146f51
	char errdir[1024] = {0};
146f51
	unsigned int cmd;
146f51
	int error = 0, root_seen = 0, save_errno = 0;
146f51
146f51
	switch (operation) {
146f51
	case VSS_OP_FREEZE:
146f51
		cmd = FIFREEZE;
146f51
		break;
146f51
	case VSS_OP_THAW:
146f51
		cmd = FITHAW;
146f51
		break;
146f51
	default:
146f51
		return -1;
146f51
	}
146f51
146f51
	mounts = setmntent("/proc/mounts", "r");
146f51
	if (mounts == NULL)
146f51
		return -1;
146f51
146f51
	while ((ent = getmntent(mounts))) {
146f51
		if (strncmp(ent->mnt_fsname, match, strlen(match)))
146f51
			continue;
146f51
		if (stat(ent->mnt_fsname, &sb) == -1)
146f51
			continue;
146f51
		if (S_ISBLK(sb.st_mode) && major(sb.st_rdev) == LOOP_MAJOR)
146f51
			continue;
146f51
		if (hasmntopt(ent, MNTOPT_RO) != NULL)
146f51
			continue;
146f51
		if (strcmp(ent->mnt_type, "vfat") == 0)
146f51
			continue;
146f51
		if (strcmp(ent->mnt_dir, "/") == 0) {
146f51
			root_seen = 1;
146f51
			continue;
146f51
		}
146f51
		error |= vss_do_freeze(ent->mnt_dir, cmd);
146f51
		if (error && operation == VSS_OP_FREEZE)
146f51
			goto err;
146f51
	}
146f51
146f51
	endmntent(mounts);
146f51
146f51
	if (root_seen) {
146f51
		error |= vss_do_freeze("/", cmd);
146f51
		if (error && operation == VSS_OP_FREEZE)
146f51
			goto err;
146f51
	}
146f51
146f51
	goto out;
146f51
err:
146f51
	save_errno = errno;
146f51
	if (ent) {
146f51
		strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
146f51
		endmntent(mounts);
146f51
	}
146f51
	vss_operate(VSS_OP_THAW);
146f51
	/* Call syslog after we thaw all filesystems */
146f51
	if (ent)
146f51
		syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
146f51
		       errdir, save_errno, strerror(save_errno));
146f51
	else
146f51
		syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
146f51
		       strerror(save_errno));
146f51
out:
146f51
	return error;
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 vss_fd, len;
146f51
	int error;
146f51
	struct pollfd pfd;
146f51
	int	op;
146f51
	struct hv_vss_msg vss_msg[1];
146f51
	int daemonize = 1, long_index = 0, opt;
146f51
	int in_handshake = 1;
146f51
	__u32 kernel_modver;
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
		return 1;
146f51
146f51
	openlog("Hyper-V VSS", 0, LOG_USER);
146f51
	syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
146f51
146f51
	vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
146f51
	if (vss_fd < 0) {
146f51
		syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
146f51
		       errno, strerror(errno));
146f51
		exit(EXIT_FAILURE);
146f51
	}
146f51
	/*
146f51
	 * Register ourselves with the kernel.
146f51
	 */
146f51
	vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
146f51
146f51
	len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
146f51
	if (len < 0) {
146f51
		syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
146f51
		       errno, strerror(errno));
146f51
		close(vss_fd);
146f51
		exit(EXIT_FAILURE);
146f51
	}
146f51
146f51
	pfd.fd = vss_fd;
146f51
146f51
	while (1) {
146f51
		pfd.events = POLLIN;
146f51
		pfd.revents = 0;
146f51
146f51
		if (poll(&pfd, 1, -1) < 0) {
146f51
			syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
146f51
			if (errno == EINVAL) {
146f51
				close(vss_fd);
146f51
				exit(EXIT_FAILURE);
146f51
			}
146f51
			else
146f51
				continue;
146f51
		}
146f51
146f51
		len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
146f51
146f51
		if (in_handshake) {
146f51
			if (len != sizeof(kernel_modver)) {
146f51
				syslog(LOG_ERR, "invalid version negotiation");
146f51
				exit(EXIT_FAILURE);
146f51
			}
146f51
			kernel_modver = *(__u32 *)vss_msg;
146f51
			in_handshake = 0;
146f51
			syslog(LOG_INFO, "VSS: kernel module version: %d",
146f51
			       kernel_modver);
146f51
			continue;
146f51
		}
146f51
146f51
		if (len != sizeof(struct hv_vss_msg)) {
146f51
			syslog(LOG_ERR, "read failed; error:%d %s",
146f51
			       errno, strerror(errno));
146f51
			close(vss_fd);
146f51
			return EXIT_FAILURE;
146f51
		}
146f51
146f51
		op = vss_msg->vss_hdr.operation;
146f51
		error =  HV_S_OK;
146f51
146f51
		switch (op) {
146f51
		case VSS_OP_FREEZE:
146f51
		case VSS_OP_THAW:
146f51
			error = vss_operate(op);
146f51
			syslog(LOG_INFO, "VSS: op=%s: %s\n",
146f51
				op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
146f51
				error ? "failed" : "succeeded");
146f51
146f51
			if (error) {
146f51
				error = HV_E_FAIL;
146f51
				syslog(LOG_ERR, "op=%d failed!", op);
146f51
				syslog(LOG_ERR, "report it with these files:");
146f51
				syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
146f51
			}
146f51
			break;
146f51
		case VSS_OP_HOT_BACKUP:
146f51
			syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
146f51
			break;
146f51
		default:
146f51
			syslog(LOG_ERR, "Illegal op:%d\n", op);
146f51
		}
146f51
		vss_msg->error = error;
146f51
		len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
146f51
		if (len != sizeof(struct hv_vss_msg)) {
146f51
			syslog(LOG_ERR, "write failed; error: %d %s", errno,
146f51
			       strerror(errno));
146f51
146f51
			if (op == VSS_OP_FREEZE)
146f51
				vss_operate(VSS_OP_THAW);
146f51
		}
146f51
	}
146f51
146f51
	close(vss_fd);
146f51
	exit(0);
146f51
}