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 different 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) {
229 if (daemonize
&& daemon(1, 0))
232 openlog("Hyper-V VSS", 0, LOG_USER
);
233 syslog(LOG_INFO
, "VSS starting; pid is:%d", getpid());
235 vss_fd
= open("/dev/vmbus/hv_vss", O_RDWR
);
237 syslog(LOG_ERR
, "open /dev/vmbus/hv_vss failed; error: %d %s",
238 errno
, strerror(errno
));
242 * Register ourselves with the kernel.
244 vss_msg
->vss_hdr
.operation
= VSS_OP_REGISTER1
;
246 len
= write(vss_fd
, vss_msg
, sizeof(struct hv_vss_msg
));
248 syslog(LOG_ERR
, "registration to kernel failed; error: %d %s",
249 errno
, strerror(errno
));
260 if (poll(&pfd
, 1, -1) < 0) {
261 syslog(LOG_ERR
, "poll failed; error:%d %s", errno
, strerror(errno
));
262 if (errno
== EINVAL
) {
270 len
= read(vss_fd
, vss_msg
, sizeof(struct hv_vss_msg
));
273 if (len
!= sizeof(kernel_modver
)) {
274 syslog(LOG_ERR
, "invalid version negotiation");
277 kernel_modver
= *(__u32
*)vss_msg
;
279 syslog(LOG_INFO
, "VSS: kernel module version: %d",
284 if (len
!= sizeof(struct hv_vss_msg
)) {
285 syslog(LOG_ERR
, "read failed; error:%d %s",
286 errno
, strerror(errno
));
291 op
= vss_msg
->vss_hdr
.operation
;
297 error
= vss_operate(op
);
298 syslog(LOG_INFO
, "VSS: op=%s: %s\n",
299 op
== VSS_OP_FREEZE
? "FREEZE" : "THAW",
300 error
? "failed" : "succeeded");
304 syslog(LOG_ERR
, "op=%d failed!", op
);
305 syslog(LOG_ERR
, "report it with these files:");
306 syslog(LOG_ERR
, "/etc/fstab and /proc/mounts");
309 case VSS_OP_HOT_BACKUP
:
310 syslog(LOG_INFO
, "VSS: op=CHECK HOT BACKUP\n");
313 syslog(LOG_ERR
, "Illegal op:%d\n", op
);
315 vss_msg
->error
= error
;
316 len
= write(vss_fd
, vss_msg
, sizeof(struct hv_vss_msg
));
317 if (len
!= sizeof(struct hv_vss_msg
)) {
318 syslog(LOG_ERR
, "write failed; error: %d %s", errno
,
321 if (op
== VSS_OP_FREEZE
)
322 vss_operate(VSS_OP_THAW
);