blob: b720d8f0f901ad042b0c9d9b06778ff146b67ffc [file] [log] [blame]
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -07001/*
2 * An implementation of the host initiated guest snapshot for Hyper-V.
3 *
4 *
5 * Copyright (C) 2013, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
7 *
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.
11 *
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
16 * details.
17 *
18 */
19
20
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <sys/poll.h>
Olaf Hering7b413b62013-04-24 07:48:52 -070024#include <sys/ioctl.h>
Olaf Hering7b413b62013-04-24 07:48:52 -070025#include <fcntl.h>
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070026#include <stdio.h>
Olaf Heringd3d1ee32013-04-24 07:48:51 -070027#include <mntent.h>
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070028#include <stdlib.h>
29#include <unistd.h>
30#include <string.h>
31#include <ctype.h>
32#include <errno.h>
33#include <arpa/inet.h>
Olaf Hering7b413b62013-04-24 07:48:52 -070034#include <linux/fs.h>
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070035#include <linux/connector.h>
36#include <linux/hyperv.h>
37#include <linux/netlink.h>
38#include <syslog.h>
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +020039#include <getopt.h>
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070040
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070041static struct sockaddr_nl addr;
42
43#ifndef SOL_NETLINK
44#define SOL_NETLINK 270
45#endif
46
47
Dexuan Cui4f689192014-09-25 21:52:04 -070048/* Don't use syslog() in the function since that can cause write to disk */
49static int vss_do_freeze(char *dir, unsigned int cmd)
Olaf Hering7b413b62013-04-24 07:48:52 -070050{
51 int ret, fd = open(dir, O_RDONLY);
52
53 if (fd < 0)
54 return 1;
Dexuan Cui4f689192014-09-25 21:52:04 -070055
Olaf Hering7b413b62013-04-24 07:48:52 -070056 ret = ioctl(fd, cmd, 0);
Dexuan Cui4f689192014-09-25 21:52:04 -070057
58 /*
59 * If a partition is mounted more than once, only the first
60 * FREEZE/THAW can succeed and the later ones will get
61 * EBUSY/EINVAL respectively: there could be 2 cases:
62 * 1) a user may mount the same partition to differnt directories
63 * by mistake or on purpose;
64 * 2) The subvolume of btrfs appears to have the same partition
65 * mounted more than once.
66 */
67 if (ret) {
68 if ((cmd == FIFREEZE && errno == EBUSY) ||
69 (cmd == FITHAW && errno == EINVAL)) {
70 close(fd);
71 return 0;
72 }
73 }
74
Olaf Hering7b413b62013-04-24 07:48:52 -070075 close(fd);
76 return !!ret;
77}
78
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070079static int vss_operate(int operation)
80{
Olaf Heringd3d1ee32013-04-24 07:48:51 -070081 char match[] = "/dev/";
82 FILE *mounts;
83 struct mntent *ent;
Olaf Hering7b413b62013-04-24 07:48:52 -070084 unsigned int cmd;
Olaf Heringd3d1ee32013-04-24 07:48:51 -070085 int error = 0, root_seen = 0;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070086
87 switch (operation) {
88 case VSS_OP_FREEZE:
Olaf Hering7b413b62013-04-24 07:48:52 -070089 cmd = FIFREEZE;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070090 break;
91 case VSS_OP_THAW:
Olaf Hering7b413b62013-04-24 07:48:52 -070092 cmd = FITHAW;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070093 break;
Olaf Heringeb8905b2013-04-24 07:48:48 -070094 default:
95 return -1;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070096 }
97
Olaf Heringd3d1ee32013-04-24 07:48:51 -070098 mounts = setmntent("/proc/mounts", "r");
99 if (mounts == NULL)
Olaf Heringeb8905b2013-04-24 07:48:48 -0700100 return -1;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700101
K. Y. Srinivasan0e272632013-04-24 07:48:54 -0700102 while ((ent = getmntent(mounts))) {
Olaf Heringd3d1ee32013-04-24 07:48:51 -0700103 if (strncmp(ent->mnt_fsname, match, strlen(match)))
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700104 continue;
Olaf Hering10b637b2013-04-24 07:48:53 -0700105 if (strcmp(ent->mnt_type, "iso9660") == 0)
106 continue;
K. Y. Srinivasanf33b2152014-02-12 08:40:22 -0800107 if (strcmp(ent->mnt_type, "vfat") == 0)
108 continue;
Olaf Heringd3d1ee32013-04-24 07:48:51 -0700109 if (strcmp(ent->mnt_dir, "/") == 0) {
110 root_seen = 1;
111 continue;
112 }
Dexuan Cui4f689192014-09-25 21:52:04 -0700113 error |= vss_do_freeze(ent->mnt_dir, cmd);
114 if (error && operation == VSS_OP_FREEZE)
115 goto err;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700116 }
Olaf Heringd3d1ee32013-04-24 07:48:51 -0700117 endmntent(mounts);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700118
Olaf Heringd3d1ee32013-04-24 07:48:51 -0700119 if (root_seen) {
Dexuan Cui4f689192014-09-25 21:52:04 -0700120 error |= vss_do_freeze("/", cmd);
121 if (error && operation == VSS_OP_FREEZE)
122 goto err;
Olaf Heringd3d1ee32013-04-24 07:48:51 -0700123 }
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700124
125 return error;
Dexuan Cui4f689192014-09-25 21:52:04 -0700126err:
127 endmntent(mounts);
128 vss_operate(VSS_OP_THAW);
129 return error;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700130}
131
132static int netlink_send(int fd, struct cn_msg *msg)
133{
Olaf Heringb4fb0ca2013-08-07 15:45:12 +0200134 struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE };
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700135 unsigned int size;
136 struct msghdr message;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700137 struct iovec iov[2];
138
Olaf Hering2bc41ea2013-08-07 15:07:21 +0200139 size = sizeof(struct cn_msg) + msg->len;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700140
Olaf Heringb4fb0ca2013-08-07 15:45:12 +0200141 nlh.nlmsg_pid = getpid();
142 nlh.nlmsg_len = NLMSG_LENGTH(size);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700143
Olaf Heringb4fb0ca2013-08-07 15:45:12 +0200144 iov[0].iov_base = &nlh;
145 iov[0].iov_len = sizeof(nlh);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700146
147 iov[1].iov_base = msg;
148 iov[1].iov_len = size;
149
150 memset(&message, 0, sizeof(message));
151 message.msg_name = &addr;
152 message.msg_namelen = sizeof(addr);
153 message.msg_iov = iov;
154 message.msg_iovlen = 2;
155
156 return sendmsg(fd, &message, 0);
157}
158
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200159void print_usage(char *argv[])
160{
161 fprintf(stderr, "Usage: %s [options]\n"
162 "Options are:\n"
163 " -n, --no-daemon stay in foreground, don't daemonize\n"
164 " -h, --help print this help\n", argv[0]);
165}
166
167int main(int argc, char *argv[])
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700168{
169 int fd, len, nl_group;
170 int error;
171 struct cn_msg *message;
172 struct pollfd pfd;
173 struct nlmsghdr *incoming_msg;
174 struct cn_msg *incoming_cn_msg;
175 int op;
176 struct hv_vss_msg *vss_msg;
Olaf Heringb4919a52013-08-01 14:34:26 +0200177 char *vss_recv_buffer;
178 size_t vss_recv_buffer_len;
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200179 int daemonize = 1, long_index = 0, opt;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700180
Vitaly Kuznetsov170f4be2014-10-22 18:07:11 +0200181 static struct option long_options[] = {
182 {"help", no_argument, 0, 'h' },
183 {"no-daemon", no_argument, 0, 'n' },
184 {0, 0, 0, 0 }
185 };
186
187 while ((opt = getopt_long(argc, argv, "hn", long_options,
188 &long_index)) != -1) {
189 switch (opt) {
190 case 'n':
191 daemonize = 0;
192 break;
193 case 'h':
194 default:
195 print_usage(argv);
196 exit(EXIT_FAILURE);
197 }
198 }
199
200 if (daemonize && daemon(1, 0))
Olaf Heringeb8905b2013-04-24 07:48:48 -0700201 return 1;
202
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700203 openlog("Hyper-V VSS", 0, LOG_USER);
204 syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
205
Olaf Hering269ce622013-08-06 20:55:38 +0200206 vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg);
Olaf Heringb4919a52013-08-01 14:34:26 +0200207 vss_recv_buffer = calloc(1, vss_recv_buffer_len);
Olaf Hering269ce622013-08-06 20:55:38 +0200208 if (!vss_recv_buffer) {
Olaf Heringb4919a52013-08-01 14:34:26 +0200209 syslog(LOG_ERR, "Failed to allocate netlink buffers");
210 exit(EXIT_FAILURE);
211 }
212
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700213 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
214 if (fd < 0) {
Tomas Hozza5c289262013-06-27 13:52:47 +0200215 syslog(LOG_ERR, "netlink socket creation failed; error:%d %s",
216 errno, strerror(errno));
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700217 exit(EXIT_FAILURE);
218 }
219 addr.nl_family = AF_NETLINK;
220 addr.nl_pad = 0;
221 addr.nl_pid = 0;
222 addr.nl_groups = 0;
223
224
225 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
226 if (error < 0) {
Tomas Hozza5c289262013-06-27 13:52:47 +0200227 syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno));
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700228 close(fd);
229 exit(EXIT_FAILURE);
230 }
231 nl_group = CN_VSS_IDX;
Tomas Hozzad12e1462013-06-27 13:52:48 +0200232 if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
233 syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno));
234 close(fd);
235 exit(EXIT_FAILURE);
236 }
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700237 /*
238 * Register ourselves with the kernel.
239 */
Olaf Hering269ce622013-08-06 20:55:38 +0200240 message = (struct cn_msg *)vss_recv_buffer;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700241 message->id.idx = CN_VSS_IDX;
242 message->id.val = CN_VSS_VAL;
243 message->ack = 0;
244 vss_msg = (struct hv_vss_msg *)message->data;
245 vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
246
247 message->len = sizeof(struct hv_vss_msg);
248
249 len = netlink_send(fd, message);
250 if (len < 0) {
Tomas Hozza5c289262013-06-27 13:52:47 +0200251 syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno));
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700252 close(fd);
253 exit(EXIT_FAILURE);
254 }
255
256 pfd.fd = fd;
257
258 while (1) {
259 struct sockaddr *addr_p = (struct sockaddr *) &addr;
260 socklen_t addr_l = sizeof(addr);
261 pfd.events = POLLIN;
262 pfd.revents = 0;
Tomas Hozza9561d472013-06-27 13:52:49 +0200263
264 if (poll(&pfd, 1, -1) < 0) {
265 syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
266 if (errno == EINVAL) {
267 close(fd);
268 exit(EXIT_FAILURE);
269 }
270 else
271 continue;
272 }
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700273
Olaf Heringb4919a52013-08-01 14:34:26 +0200274 len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0,
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700275 addr_p, &addr_l);
276
Olaf Hering5edf5ee2013-04-24 07:48:49 -0700277 if (len < 0) {
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700278 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
279 addr.nl_pid, errno, strerror(errno));
280 close(fd);
281 return -1;
282 }
283
Olaf Hering5edf5ee2013-04-24 07:48:49 -0700284 if (addr.nl_pid) {
K. Y. Srinivasan038336a2013-04-24 07:48:50 -0700285 syslog(LOG_WARNING,
286 "Received packet from untrusted pid:%u",
287 addr.nl_pid);
Olaf Hering5edf5ee2013-04-24 07:48:49 -0700288 continue;
289 }
290
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700291 incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
292
293 if (incoming_msg->nlmsg_type != NLMSG_DONE)
294 continue;
295
296 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
297 vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
298 op = vss_msg->vss_hdr.operation;
299 error = HV_S_OK;
300
301 switch (op) {
302 case VSS_OP_FREEZE:
303 case VSS_OP_THAW:
304 error = vss_operate(op);
Dexuan Cui4f689192014-09-25 21:52:04 -0700305 syslog(LOG_INFO, "VSS: op=%s: %s\n",
306 op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
307 error ? "failed" : "succeeded");
308
309 if (error) {
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700310 error = HV_E_FAIL;
Dexuan Cui4f689192014-09-25 21:52:04 -0700311 syslog(LOG_ERR, "op=%d failed!", op);
312 syslog(LOG_ERR, "report it with these files:");
313 syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
314 }
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700315 break;
316 default:
317 syslog(LOG_ERR, "Illegal op:%d\n", op);
318 }
319 vss_msg->error = error;
320 len = netlink_send(fd, incoming_cn_msg);
321 if (len < 0) {
Tomas Hozza5c289262013-06-27 13:52:47 +0200322 syslog(LOG_ERR, "net_link send failed; error:%d %s",
323 errno, strerror(errno));
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700324 exit(EXIT_FAILURE);
325 }
326 }
327
328}