Commit | Line | Data |
---|---|---|
96dd86fa S |
1 | /* |
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> | |
7b413b65 | 24 | #include <sys/ioctl.h> |
7b413b65 | 25 | #include <fcntl.h> |
96dd86fa | 26 | #include <stdio.h> |
d3d1ee3a | 27 | #include <mntent.h> |
96dd86fa S |
28 | #include <stdlib.h> |
29 | #include <unistd.h> | |
30 | #include <string.h> | |
31 | #include <ctype.h> | |
32 | #include <errno.h> | |
33 | #include <arpa/inet.h> | |
7b413b65 | 34 | #include <linux/fs.h> |
96dd86fa S |
35 | #include <linux/connector.h> |
36 | #include <linux/hyperv.h> | |
37 | #include <linux/netlink.h> | |
38 | #include <syslog.h> | |
170f4bea | 39 | #include <getopt.h> |
96dd86fa | 40 | |
96dd86fa S |
41 | static struct sockaddr_nl addr; |
42 | ||
43 | #ifndef SOL_NETLINK | |
44 | #define SOL_NETLINK 270 | |
45 | #endif | |
46 | ||
47 | ||
4f689190 DC |
48 | /* Don't use syslog() in the function since that can cause write to disk */ |
49 | static int vss_do_freeze(char *dir, unsigned int cmd) | |
7b413b65 OH |
50 | { |
51 | int ret, fd = open(dir, O_RDONLY); | |
52 | ||
53 | if (fd < 0) | |
54 | return 1; | |
4f689190 | 55 | |
7b413b65 | 56 | ret = ioctl(fd, cmd, 0); |
4f689190 DC |
57 | |
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 | ||
7b413b65 OH |
75 | close(fd); |
76 | return !!ret; | |
77 | } | |
78 | ||
96dd86fa S |
79 | static int vss_operate(int operation) |
80 | { | |
d3d1ee3a OH |
81 | char match[] = "/dev/"; |
82 | FILE *mounts; | |
83 | struct mntent *ent; | |
7b413b65 | 84 | unsigned int cmd; |
7a401744 | 85 | int error = 0, root_seen = 0, save_errno = 0; |
96dd86fa S |
86 | |
87 | switch (operation) { | |
88 | case VSS_OP_FREEZE: | |
7b413b65 | 89 | cmd = FIFREEZE; |
96dd86fa S |
90 | break; |
91 | case VSS_OP_THAW: | |
7b413b65 | 92 | cmd = FITHAW; |
96dd86fa | 93 | break; |
eb8905b8 OH |
94 | default: |
95 | return -1; | |
96dd86fa S |
96 | } |
97 | ||
d3d1ee3a OH |
98 | mounts = setmntent("/proc/mounts", "r"); |
99 | if (mounts == NULL) | |
eb8905b8 | 100 | return -1; |
96dd86fa | 101 | |
0e272639 | 102 | while ((ent = getmntent(mounts))) { |
d3d1ee3a | 103 | if (strncmp(ent->mnt_fsname, match, strlen(match))) |
96dd86fa | 104 | continue; |
9e5db05a | 105 | if (hasmntopt(ent, MNTOPT_RO) != NULL) |
10b637b4 | 106 | continue; |
f33b2155 S |
107 | if (strcmp(ent->mnt_type, "vfat") == 0) |
108 | continue; | |
d3d1ee3a OH |
109 | if (strcmp(ent->mnt_dir, "/") == 0) { |
110 | root_seen = 1; | |
111 | continue; | |
112 | } | |
4f689190 DC |
113 | error |= vss_do_freeze(ent->mnt_dir, cmd); |
114 | if (error && operation == VSS_OP_FREEZE) | |
115 | goto err; | |
96dd86fa | 116 | } |
96dd86fa | 117 | |
d3d1ee3a | 118 | if (root_seen) { |
4f689190 DC |
119 | error |= vss_do_freeze("/", cmd); |
120 | if (error && operation == VSS_OP_FREEZE) | |
121 | goto err; | |
d3d1ee3a | 122 | } |
96dd86fa | 123 | |
7a401744 | 124 | goto out; |
4f689190 | 125 | err: |
7a401744 | 126 | save_errno = errno; |
4f689190 | 127 | vss_operate(VSS_OP_THAW); |
7a401744 VK |
128 | /* Call syslog after we thaw all filesystems */ |
129 | if (ent) | |
130 | syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s", | |
131 | ent->mnt_dir, save_errno, strerror(save_errno)); | |
132 | else | |
133 | syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno, | |
134 | strerror(save_errno)); | |
135 | out: | |
136 | endmntent(mounts); | |
96dd86fa S |
137 | return error; |
138 | } | |
139 | ||
140 | static int netlink_send(int fd, struct cn_msg *msg) | |
141 | { | |
b4fb0ca2 | 142 | struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE }; |
96dd86fa S |
143 | unsigned int size; |
144 | struct msghdr message; | |
96dd86fa S |
145 | struct iovec iov[2]; |
146 | ||
2bc41ea3 | 147 | size = sizeof(struct cn_msg) + msg->len; |
96dd86fa | 148 | |
b4fb0ca2 OH |
149 | nlh.nlmsg_pid = getpid(); |
150 | nlh.nlmsg_len = NLMSG_LENGTH(size); | |
96dd86fa | 151 | |
b4fb0ca2 OH |
152 | iov[0].iov_base = &nlh; |
153 | iov[0].iov_len = sizeof(nlh); | |
96dd86fa S |
154 | |
155 | iov[1].iov_base = msg; | |
156 | iov[1].iov_len = size; | |
157 | ||
158 | memset(&message, 0, sizeof(message)); | |
159 | message.msg_name = &addr; | |
160 | message.msg_namelen = sizeof(addr); | |
161 | message.msg_iov = iov; | |
162 | message.msg_iovlen = 2; | |
163 | ||
164 | return sendmsg(fd, &message, 0); | |
165 | } | |
166 | ||
170f4bea VK |
167 | void print_usage(char *argv[]) |
168 | { | |
169 | fprintf(stderr, "Usage: %s [options]\n" | |
170 | "Options are:\n" | |
171 | " -n, --no-daemon stay in foreground, don't daemonize\n" | |
172 | " -h, --help print this help\n", argv[0]); | |
173 | } | |
174 | ||
175 | int main(int argc, char *argv[]) | |
96dd86fa S |
176 | { |
177 | int fd, len, nl_group; | |
178 | int error; | |
179 | struct cn_msg *message; | |
180 | struct pollfd pfd; | |
181 | struct nlmsghdr *incoming_msg; | |
182 | struct cn_msg *incoming_cn_msg; | |
183 | int op; | |
184 | struct hv_vss_msg *vss_msg; | |
b4919a5f OH |
185 | char *vss_recv_buffer; |
186 | size_t vss_recv_buffer_len; | |
170f4bea VK |
187 | int daemonize = 1, long_index = 0, opt; |
188 | ||
189 | static struct option long_options[] = { | |
190 | {"help", no_argument, 0, 'h' }, | |
191 | {"no-daemon", no_argument, 0, 'n' }, | |
192 | {0, 0, 0, 0 } | |
193 | }; | |
194 | ||
195 | while ((opt = getopt_long(argc, argv, "hn", long_options, | |
196 | &long_index)) != -1) { | |
197 | switch (opt) { | |
198 | case 'n': | |
199 | daemonize = 0; | |
200 | break; | |
201 | case 'h': | |
202 | default: | |
203 | print_usage(argv); | |
204 | exit(EXIT_FAILURE); | |
205 | } | |
206 | } | |
96dd86fa | 207 | |
170f4bea | 208 | if (daemonize && daemon(1, 0)) |
eb8905b8 OH |
209 | return 1; |
210 | ||
96dd86fa S |
211 | openlog("Hyper-V VSS", 0, LOG_USER); |
212 | syslog(LOG_INFO, "VSS starting; pid is:%d", getpid()); | |
213 | ||
269ce62b | 214 | vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg); |
b4919a5f | 215 | vss_recv_buffer = calloc(1, vss_recv_buffer_len); |
269ce62b | 216 | if (!vss_recv_buffer) { |
b4919a5f OH |
217 | syslog(LOG_ERR, "Failed to allocate netlink buffers"); |
218 | exit(EXIT_FAILURE); | |
219 | } | |
220 | ||
96dd86fa S |
221 | fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); |
222 | if (fd < 0) { | |
5c289269 TH |
223 | syslog(LOG_ERR, "netlink socket creation failed; error:%d %s", |
224 | errno, strerror(errno)); | |
96dd86fa S |
225 | exit(EXIT_FAILURE); |
226 | } | |
227 | addr.nl_family = AF_NETLINK; | |
228 | addr.nl_pad = 0; | |
229 | addr.nl_pid = 0; | |
230 | addr.nl_groups = 0; | |
231 | ||
232 | ||
233 | error = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); | |
234 | if (error < 0) { | |
5c289269 | 235 | syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno)); |
96dd86fa S |
236 | close(fd); |
237 | exit(EXIT_FAILURE); | |
238 | } | |
239 | nl_group = CN_VSS_IDX; | |
d12e1469 TH |
240 | if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) { |
241 | syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno)); | |
242 | close(fd); | |
243 | exit(EXIT_FAILURE); | |
244 | } | |
96dd86fa S |
245 | /* |
246 | * Register ourselves with the kernel. | |
247 | */ | |
269ce62b | 248 | message = (struct cn_msg *)vss_recv_buffer; |
96dd86fa S |
249 | message->id.idx = CN_VSS_IDX; |
250 | message->id.val = CN_VSS_VAL; | |
251 | message->ack = 0; | |
252 | vss_msg = (struct hv_vss_msg *)message->data; | |
253 | vss_msg->vss_hdr.operation = VSS_OP_REGISTER; | |
254 | ||
255 | message->len = sizeof(struct hv_vss_msg); | |
256 | ||
257 | len = netlink_send(fd, message); | |
258 | if (len < 0) { | |
5c289269 | 259 | syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno)); |
96dd86fa S |
260 | close(fd); |
261 | exit(EXIT_FAILURE); | |
262 | } | |
263 | ||
264 | pfd.fd = fd; | |
265 | ||
266 | while (1) { | |
267 | struct sockaddr *addr_p = (struct sockaddr *) &addr; | |
268 | socklen_t addr_l = sizeof(addr); | |
269 | pfd.events = POLLIN; | |
270 | pfd.revents = 0; | |
9561d479 TH |
271 | |
272 | if (poll(&pfd, 1, -1) < 0) { | |
273 | syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno)); | |
274 | if (errno == EINVAL) { | |
275 | close(fd); | |
276 | exit(EXIT_FAILURE); | |
277 | } | |
278 | else | |
279 | continue; | |
280 | } | |
96dd86fa | 281 | |
b4919a5f | 282 | len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0, |
96dd86fa S |
283 | addr_p, &addr_l); |
284 | ||
5edf5ee4 | 285 | if (len < 0) { |
96dd86fa S |
286 | syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s", |
287 | addr.nl_pid, errno, strerror(errno)); | |
288 | close(fd); | |
289 | return -1; | |
290 | } | |
291 | ||
5edf5ee4 | 292 | if (addr.nl_pid) { |
038336a5 S |
293 | syslog(LOG_WARNING, |
294 | "Received packet from untrusted pid:%u", | |
295 | addr.nl_pid); | |
5edf5ee4 OH |
296 | continue; |
297 | } | |
298 | ||
96dd86fa S |
299 | incoming_msg = (struct nlmsghdr *)vss_recv_buffer; |
300 | ||
301 | if (incoming_msg->nlmsg_type != NLMSG_DONE) | |
302 | continue; | |
303 | ||
304 | incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg); | |
305 | vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data; | |
306 | op = vss_msg->vss_hdr.operation; | |
307 | error = HV_S_OK; | |
308 | ||
309 | switch (op) { | |
310 | case VSS_OP_FREEZE: | |
311 | case VSS_OP_THAW: | |
312 | error = vss_operate(op); | |
4f689190 DC |
313 | syslog(LOG_INFO, "VSS: op=%s: %s\n", |
314 | op == VSS_OP_FREEZE ? "FREEZE" : "THAW", | |
315 | error ? "failed" : "succeeded"); | |
316 | ||
317 | if (error) { | |
96dd86fa | 318 | error = HV_E_FAIL; |
4f689190 DC |
319 | syslog(LOG_ERR, "op=%d failed!", op); |
320 | syslog(LOG_ERR, "report it with these files:"); | |
321 | syslog(LOG_ERR, "/etc/fstab and /proc/mounts"); | |
322 | } | |
96dd86fa S |
323 | break; |
324 | default: | |
325 | syslog(LOG_ERR, "Illegal op:%d\n", op); | |
326 | } | |
327 | vss_msg->error = error; | |
328 | len = netlink_send(fd, incoming_cn_msg); | |
329 | if (len < 0) { | |
5c289269 TH |
330 | syslog(LOG_ERR, "net_link send failed; error:%d %s", |
331 | errno, strerror(errno)); | |
96dd86fa S |
332 | exit(EXIT_FAILURE); |
333 | } | |
334 | } | |
335 | ||
336 | } |