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