3feead
// SPDX-License-Identifier: GPL-2.0-only
3feead
/*
3feead
 * An implementation of the host initiated guest snapshot for Hyper-V.
3feead
 *
3feead
 * Copyright (C) 2013, Microsoft, Inc.
3feead
 * Author : K. Y. Srinivasan <kys@microsoft.com>
3feead
 */
3feead
3feead
3feead
#include <sys/types.h>
3feead
#include <sys/poll.h>
3feead
#include <sys/ioctl.h>
3feead
#include <sys/stat.h>
3feead
#include <sys/sysmacros.h>
3feead
#include <fcntl.h>
3feead
#include <stdio.h>
3feead
#include <mntent.h>
3feead
#include <stdlib.h>
3feead
#include <unistd.h>
3feead
#include <string.h>
3feead
#include <ctype.h>
3feead
#include <errno.h>
3feead
#include <linux/fs.h>
3feead
#include <linux/major.h>
3feead
#include <linux/hyperv.h>
3feead
#include <syslog.h>
3feead
#include <getopt.h>
3feead
#include <stdbool.h>
3feead
#include <dirent.h>
3feead
3feead
/* Don't use syslog() in the function since that can cause write to disk */
3feead
static int vss_do_freeze(char *dir, unsigned int cmd)
3feead
{
3feead
	int ret, fd = open(dir, O_RDONLY);
3feead
3feead
	if (fd < 0)
3feead
		return 1;
3feead
3feead
	ret = ioctl(fd, cmd, 0);
3feead
3feead
	/*
3feead
	 * If a partition is mounted more than once, only the first
3feead
	 * FREEZE/THAW can succeed and the later ones will get
3feead
	 * EBUSY/EINVAL respectively: there could be 2 cases:
3feead
	 * 1) a user may mount the same partition to different directories
3feead
	 *  by mistake or on purpose;
3feead
	 * 2) The subvolume of btrfs appears to have the same partition
3feead
	 * mounted more than once.
3feead
	 */
3feead
	if (ret) {
3feead
		if ((cmd == FIFREEZE && errno == EBUSY) ||
3feead
		    (cmd == FITHAW && errno == EINVAL)) {
3feead
			close(fd);
3feead
			return 0;
3feead
		}
3feead
	}
3feead
3feead
	close(fd);
3feead
	return !!ret;
3feead
}
3feead
3feead
static bool is_dev_loop(const char *blkname)
3feead
{
3feead
	char *buffer;
3feead
	DIR *dir;
3feead
	struct dirent *entry;
3feead
	bool ret = false;
3feead
3feead
	buffer = malloc(PATH_MAX);
3feead
	if (!buffer) {
3feead
		syslog(LOG_ERR, "Can't allocate memory!");
3feead
		exit(1);
3feead
	}
3feead
3feead
	snprintf(buffer, PATH_MAX, "%s/loop", blkname);
3feead
	if (!access(buffer, R_OK | X_OK)) {
3feead
		ret = true;
3feead
		goto free_buffer;
3feead
	} else if (errno != ENOENT) {
3feead
		syslog(LOG_ERR, "Can't access: %s; error:%d %s!",
3feead
		       buffer, errno, strerror(errno));
3feead
	}
3feead
3feead
	snprintf(buffer, PATH_MAX, "%s/slaves", blkname);
3feead
	dir = opendir(buffer);
3feead
	if (!dir) {
3feead
		if (errno != ENOENT)
3feead
			syslog(LOG_ERR, "Can't opendir: %s; error:%d %s!",
3feead
			       buffer, errno, strerror(errno));
3feead
		goto free_buffer;
3feead
	}
3feead
3feead
	while ((entry = readdir(dir)) != NULL) {
3feead
		if (strcmp(entry->d_name, ".") == 0 ||
3feead
		    strcmp(entry->d_name, "..") == 0)
3feead
			continue;
3feead
3feead
		snprintf(buffer, PATH_MAX, "%s/slaves/%s", blkname,
3feead
			 entry->d_name);
3feead
		if (is_dev_loop(buffer)) {
3feead
			ret = true;
3feead
			break;
3feead
		}
3feead
	}
3feead
	closedir(dir);
3feead
free_buffer:
3feead
	free(buffer);
3feead
	return ret;
3feead
}
3feead
3feead
static int vss_operate(int operation)
3feead
{
3feead
	char match[] = "/dev/";
3feead
	FILE *mounts;
3feead
	struct mntent *ent;
3feead
	struct stat sb;
3feead
	char errdir[1024] = {0};
3feead
	char blkdir[23]; /* /sys/dev/block/XXX:XXX */
3feead
	unsigned int cmd;
3feead
	int error = 0, root_seen = 0, save_errno = 0;
3feead
3feead
	switch (operation) {
3feead
	case VSS_OP_FREEZE:
3feead
		cmd = FIFREEZE;
3feead
		break;
3feead
	case VSS_OP_THAW:
3feead
		cmd = FITHAW;
3feead
		break;
3feead
	default:
3feead
		return -1;
3feead
	}
3feead
3feead
	mounts = setmntent("/proc/mounts", "r");
3feead
	if (mounts == NULL)
3feead
		return -1;
3feead
3feead
	while ((ent = getmntent(mounts))) {
3feead
		if (strncmp(ent->mnt_fsname, match, strlen(match)))
3feead
			continue;
3feead
		if (stat(ent->mnt_fsname, &sb)) {
3feead
			syslog(LOG_ERR, "Can't stat: %s; error:%d %s!",
3feead
			       ent->mnt_fsname, errno, strerror(errno));
3feead
		} else {
3feead
			sprintf(blkdir, "/sys/dev/block/%d:%d",
3feead
				major(sb.st_rdev), minor(sb.st_rdev));
3feead
			if (is_dev_loop(blkdir))
3feead
				continue;
3feead
		}
3feead
		if (hasmntopt(ent, MNTOPT_RO) != NULL)
3feead
			continue;
3feead
		if (strcmp(ent->mnt_type, "vfat") == 0)
3feead
			continue;
3feead
		if (strcmp(ent->mnt_dir, "/") == 0) {
3feead
			root_seen = 1;
3feead
			continue;
3feead
		}
3feead
		error |= vss_do_freeze(ent->mnt_dir, cmd);
3feead
		if (error && operation == VSS_OP_FREEZE)
3feead
			goto err;
3feead
	}
3feead
3feead
	endmntent(mounts);
3feead
3feead
	if (root_seen) {
3feead
		error |= vss_do_freeze("/", cmd);
3feead
		if (error && operation == VSS_OP_FREEZE)
3feead
			goto err;
3feead
	}
3feead
3feead
	goto out;
3feead
err:
3feead
	save_errno = errno;
3feead
	if (ent) {
3feead
		strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
3feead
		endmntent(mounts);
3feead
	}
3feead
	vss_operate(VSS_OP_THAW);
3feead
	/* Call syslog after we thaw all filesystems */
3feead
	if (ent)
3feead
		syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
3feead
		       errdir, save_errno, strerror(save_errno));
3feead
	else
3feead
		syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
3feead
		       strerror(save_errno));
3feead
out:
3feead
	return error;
3feead
}
3feead
3feead
void print_usage(char *argv[])
3feead
{
3feead
	fprintf(stderr, "Usage: %s [options]\n"
3feead
		"Options are:\n"
3feead
		"  -n, --no-daemon        stay in foreground, don't daemonize\n"
3feead
		"  -h, --help             print this help\n", argv[0]);
3feead
}
3feead
3feead
int main(int argc, char *argv[])
3feead
{
3feead
	int vss_fd, len;
3feead
	int error;
3feead
	struct pollfd pfd;
3feead
	int	op;
3feead
	struct hv_vss_msg vss_msg[1];
3feead
	int daemonize = 1, long_index = 0, opt;
3feead
	int in_handshake = 1;
3feead
	__u32 kernel_modver;
3feead
3feead
	static struct option long_options[] = {
3feead
		{"help",	no_argument,	   0,  'h' },
3feead
		{"no-daemon",	no_argument,	   0,  'n' },
3feead
		{0,		0,		   0,  0   }
3feead
	};
3feead
3feead
	while ((opt = getopt_long(argc, argv, "hn", long_options,
3feead
				  &long_index)) != -1) {
3feead
		switch (opt) {
3feead
		case 'n':
3feead
			daemonize = 0;
3feead
			break;
3feead
		case 'h':
3feead
			print_usage(argv);
3feead
			exit(0);
3feead
		default:
3feead
			print_usage(argv);
3feead
			exit(EXIT_FAILURE);
3feead
		}
3feead
	}
3feead
3feead
	if (daemonize && daemon(1, 0))
3feead
		return 1;
3feead
3feead
	openlog("Hyper-V VSS", 0, LOG_USER);
3feead
	syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
3feead
3feead
	vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
3feead
	if (vss_fd < 0) {
3feead
		syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
3feead
		       errno, strerror(errno));
3feead
		exit(EXIT_FAILURE);
3feead
	}
3feead
	/*
3feead
	 * Register ourselves with the kernel.
3feead
	 */
3feead
	vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
3feead
3feead
	len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
3feead
	if (len < 0) {
3feead
		syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
3feead
		       errno, strerror(errno));
3feead
		close(vss_fd);
3feead
		exit(EXIT_FAILURE);
3feead
	}
3feead
3feead
	pfd.fd = vss_fd;
3feead
3feead
	while (1) {
3feead
		pfd.events = POLLIN;
3feead
		pfd.revents = 0;
3feead
3feead
		if (poll(&pfd, 1, -1) < 0) {
3feead
			syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
3feead
			if (errno == EINVAL) {
3feead
				close(vss_fd);
3feead
				exit(EXIT_FAILURE);
3feead
			}
3feead
			else
3feead
				continue;
3feead
		}
3feead
3feead
		len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
3feead
3feead
		if (in_handshake) {
3feead
			if (len != sizeof(kernel_modver)) {
3feead
				syslog(LOG_ERR, "invalid version negotiation");
3feead
				exit(EXIT_FAILURE);
3feead
			}
3feead
			kernel_modver = *(__u32 *)vss_msg;
3feead
			in_handshake = 0;
3feead
			syslog(LOG_INFO, "VSS: kernel module version: %d",
3feead
			       kernel_modver);
3feead
			continue;
3feead
		}
3feead
3feead
		if (len != sizeof(struct hv_vss_msg)) {
3feead
			syslog(LOG_ERR, "read failed; error:%d %s",
3feead
			       errno, strerror(errno));
3feead
			close(vss_fd);
3feead
			return EXIT_FAILURE;
3feead
		}
3feead
3feead
		op = vss_msg->vss_hdr.operation;
3feead
		error =  HV_S_OK;
3feead
3feead
		switch (op) {
3feead
		case VSS_OP_FREEZE:
3feead
		case VSS_OP_THAW:
3feead
			error = vss_operate(op);
3feead
			syslog(LOG_INFO, "VSS: op=%s: %s\n",
3feead
				op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
3feead
				error ? "failed" : "succeeded");
3feead
3feead
			if (error) {
3feead
				error = HV_E_FAIL;
3feead
				syslog(LOG_ERR, "op=%d failed!", op);
3feead
				syslog(LOG_ERR, "report it with these files:");
3feead
				syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
3feead
			}
3feead
			break;
3feead
		case VSS_OP_HOT_BACKUP:
3feead
			syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
3feead
			break;
3feead
		default:
3feead
			syslog(LOG_ERR, "Illegal op:%d\n", op);
3feead
		}
3feead
		vss_msg->error = error;
3feead
		len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
3feead
		if (len != sizeof(struct hv_vss_msg)) {
3feead
			syslog(LOG_ERR, "write failed; error: %d %s", errno,
3feead
			       strerror(errno));
3feead
3feead
			if (op == VSS_OP_FREEZE)
3feead
				vss_operate(VSS_OP_THAW);
3feead
		}
3feead
	}
3feead
3feead
	close(vss_fd);
3feead
	exit(0);
3feead
}