2 * 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>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
35 #include <linux/connector.h>
36 #include <linux/hyperv.h>
37 #include <linux/netlink.h>
40 static struct sockaddr_nl addr;
43 #define SOL_NETLINK 270
47 /* Don't use syslog() in the function since that can cause write to disk */
48 static int vss_do_freeze(char *dir, unsigned int cmd)
50 int ret, fd = open(dir, O_RDONLY);
55 ret = ioctl(fd, cmd, 0);
58 * If a partition is mounted more than once, only the first
59 * FREEZE/THAW can succeed and the later ones will get
60 * EBUSY/EINVAL respectively: there could be 2 cases:
61 * 1) a user may mount the same partition to differnt directories
62 * by mistake or on purpose;
63 * 2) The subvolume of btrfs appears to have the same partition
64 * mounted more than once.
67 if ((cmd == FIFREEZE && errno == EBUSY) ||
68 (cmd == FITHAW && errno == EINVAL)) {
78 static int vss_operate(int operation)
80 char match[] = "/dev/";
84 int error = 0, root_seen = 0;
97 mounts = setmntent("/proc/mounts", "r");
101 while ((ent = getmntent(mounts))) {
102 if (strncmp(ent->mnt_fsname, match, strlen(match)))
104 if (strcmp(ent->mnt_type, "iso9660") == 0)
106 if (strcmp(ent->mnt_type, "vfat") == 0)
108 if (strcmp(ent->mnt_dir, "/") == 0) {
112 error |= vss_do_freeze(ent->mnt_dir, cmd);
113 if (error && operation == VSS_OP_FREEZE)
119 error |= vss_do_freeze("/", cmd);
120 if (error && operation == VSS_OP_FREEZE)
127 vss_operate(VSS_OP_THAW);
131 static int netlink_send(int fd, struct cn_msg *msg)
133 struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE };
135 struct msghdr message;
138 size = sizeof(struct cn_msg) + msg->len;
140 nlh.nlmsg_pid = getpid();
141 nlh.nlmsg_len = NLMSG_LENGTH(size);
143 iov[0].iov_base = &nlh;
144 iov[0].iov_len = sizeof(nlh);
146 iov[1].iov_base = msg;
147 iov[1].iov_len = size;
149 memset(&message, 0, sizeof(message));
150 message.msg_name = &addr;
151 message.msg_namelen = sizeof(addr);
152 message.msg_iov = iov;
153 message.msg_iovlen = 2;
155 return sendmsg(fd, &message, 0);
160 int fd, len, nl_group;
162 struct cn_msg *message;
164 struct nlmsghdr *incoming_msg;
165 struct cn_msg *incoming_cn_msg;
167 struct hv_vss_msg *vss_msg;
168 char *vss_recv_buffer;
169 size_t vss_recv_buffer_len;
174 openlog("Hyper-V VSS", 0, LOG_USER);
175 syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
177 vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg);
178 vss_recv_buffer = calloc(1, vss_recv_buffer_len);
179 if (!vss_recv_buffer) {
180 syslog(LOG_ERR, "Failed to allocate netlink buffers");
184 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
186 syslog(LOG_ERR, "netlink socket creation failed; error:%d %s",
187 errno, strerror(errno));
190 addr.nl_family = AF_NETLINK;
196 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
198 syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno));
202 nl_group = CN_VSS_IDX;
203 if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
204 syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno));
209 * Register ourselves with the kernel.
211 message = (struct cn_msg *)vss_recv_buffer;
212 message->id.idx = CN_VSS_IDX;
213 message->id.val = CN_VSS_VAL;
215 vss_msg = (struct hv_vss_msg *)message->data;
216 vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
218 message->len = sizeof(struct hv_vss_msg);
220 len = netlink_send(fd, message);
222 syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno));
230 struct sockaddr *addr_p = (struct sockaddr *) &addr;
231 socklen_t addr_l = sizeof(addr);
235 if (poll(&pfd, 1, -1) < 0) {
236 syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
237 if (errno == EINVAL) {
245 len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0,
249 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
250 addr.nl_pid, errno, strerror(errno));
257 "Received packet from untrusted pid:%u",
262 incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
264 if (incoming_msg->nlmsg_type != NLMSG_DONE)
267 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
268 vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
269 op = vss_msg->vss_hdr.operation;
275 error = vss_operate(op);
276 syslog(LOG_INFO, "VSS: op=%s: %s\n",
277 op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
278 error ? "failed" : "succeeded");
282 syslog(LOG_ERR, "op=%d failed!", op);
283 syslog(LOG_ERR, "report it with these files:");
284 syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
288 syslog(LOG_ERR, "Illegal op:%d\n", op);
290 vss_msg->error = error;
291 len = netlink_send(fd, incoming_cn_msg);
293 syslog(LOG_ERR, "net_link send failed; error:%d %s",
294 errno, strerror(errno));