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