1 // SPDX-License-Identifier: GPL-2.0-only
3 * An implementation of the host initiated guest snapshot for Hyper-V.
5 * Copyright (C) 2013, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
10 #include <sys/types.h>
12 #include <sys/ioctl.h>
14 #include <sys/sysmacros.h>
24 #include <linux/major.h>
25 #include <linux/hyperv.h>
31 /* Don't use syslog() in the function since that can cause write to disk */
32 static int vss_do_freeze(char *dir, unsigned int cmd)
34 int ret, fd = open(dir, O_RDONLY);
39 ret = ioctl(fd, cmd, 0);
42 * If a partition is mounted more than once, only the first
43 * FREEZE/THAW can succeed and the later ones will get
44 * EBUSY/EINVAL respectively: there could be 2 cases:
45 * 1) a user may mount the same partition to differnt directories
46 * by mistake or on purpose;
47 * 2) The subvolume of btrfs appears to have the same partition
48 * mounted more than once.
51 if ((cmd == FIFREEZE && errno == EBUSY) ||
52 (cmd == FITHAW && errno == EINVAL)) {
62 static bool is_dev_loop(const char *blkname)
69 buffer = malloc(PATH_MAX);
71 syslog(LOG_ERR, "Can't allocate memory!");
75 snprintf(buffer, PATH_MAX, "%s/loop", blkname);
76 if (!access(buffer, R_OK | X_OK)) {
79 } else if (errno != ENOENT) {
80 syslog(LOG_ERR, "Can't access: %s; error:%d %s!",
81 buffer, errno, strerror(errno));
84 snprintf(buffer, PATH_MAX, "%s/slaves", blkname);
85 dir = opendir(buffer);
88 syslog(LOG_ERR, "Can't opendir: %s; error:%d %s!",
89 buffer, errno, strerror(errno));
93 while ((entry = readdir(dir)) != NULL) {
94 if (strcmp(entry->d_name, ".") == 0 ||
95 strcmp(entry->d_name, "..") == 0)
98 snprintf(buffer, PATH_MAX, "%s/slaves/%s", blkname,
100 if (is_dev_loop(buffer)) {
111 static int vss_operate(int operation)
113 char match[] = "/dev/";
117 char errdir[1024] = {0};
118 char blkdir[23]; /* /sys/dev/block/XXX:XXX */
120 int error = 0, root_seen = 0, save_errno = 0;
133 mounts = setmntent("/proc/mounts", "r");
137 while ((ent = getmntent(mounts))) {
138 if (strncmp(ent->mnt_fsname, match, strlen(match)))
140 if (stat(ent->mnt_fsname, &sb)) {
141 syslog(LOG_ERR, "Can't stat: %s; error:%d %s!",
142 ent->mnt_fsname, errno, strerror(errno));
144 sprintf(blkdir, "/sys/dev/block/%d:%d",
145 major(sb.st_rdev), minor(sb.st_rdev));
146 if (is_dev_loop(blkdir))
149 if (hasmntopt(ent, MNTOPT_RO) != NULL)
151 if (strcmp(ent->mnt_type, "vfat") == 0)
153 if (strcmp(ent->mnt_dir, "/") == 0) {
157 error |= vss_do_freeze(ent->mnt_dir, cmd);
158 if (error && operation == VSS_OP_FREEZE)
165 error |= vss_do_freeze("/", cmd);
166 if (error && operation == VSS_OP_FREEZE)
174 strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
177 vss_operate(VSS_OP_THAW);
178 /* Call syslog after we thaw all filesystems */
180 syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
181 errdir, save_errno, strerror(save_errno));
183 syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
184 strerror(save_errno));
189 void print_usage(char *argv[])
191 fprintf(stderr, "Usage: %s [options]\n"
193 " -n, --no-daemon stay in foreground, don't daemonize\n"
194 " -h, --help print this help\n", argv[0]);
197 int main(int argc, char *argv[])
203 struct hv_vss_msg vss_msg[1];
204 int daemonize = 1, long_index = 0, opt;
205 int in_handshake = 1;
208 static struct option long_options[] = {
209 {"help", no_argument, 0, 'h' },
210 {"no-daemon", no_argument, 0, 'n' },
214 while ((opt = getopt_long(argc, argv, "hn", long_options,
215 &long_index)) != -1) {
227 if (daemonize && daemon(1, 0))
230 openlog("Hyper-V VSS", 0, LOG_USER);
231 syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
233 vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
235 syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
236 errno, strerror(errno));
240 * Register ourselves with the kernel.
242 vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
244 len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
246 syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
247 errno, strerror(errno));
258 if (poll(&pfd, 1, -1) < 0) {
259 syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
260 if (errno == EINVAL) {
268 len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
271 if (len != sizeof(kernel_modver)) {
272 syslog(LOG_ERR, "invalid version negotiation");
275 kernel_modver = *(__u32 *)vss_msg;
277 syslog(LOG_INFO, "VSS: kernel module version: %d",
282 if (len != sizeof(struct hv_vss_msg)) {
283 syslog(LOG_ERR, "read failed; error:%d %s",
284 errno, strerror(errno));
289 op = vss_msg->vss_hdr.operation;
295 error = vss_operate(op);
296 syslog(LOG_INFO, "VSS: op=%s: %s\n",
297 op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
298 error ? "failed" : "succeeded");
302 syslog(LOG_ERR, "op=%d failed!", op);
303 syslog(LOG_ERR, "report it with these files:");
304 syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
307 case VSS_OP_HOT_BACKUP:
308 syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
311 syslog(LOG_ERR, "Illegal op:%d\n", op);
313 vss_msg->error = error;
314 len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
315 if (len != sizeof(struct hv_vss_msg)) {
316 syslog(LOG_ERR, "write failed; error: %d %s", errno,
319 if (op == VSS_OP_FREEZE)
320 vss_operate(VSS_OP_THAW);