PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/hv/hv_vss_daemon.c

https://gitlab.com/kush/linux
C | 337 lines | 266 code | 39 blank | 32 comment | 69 complexity | 5fd1cae1d8acba1d978c7a8b6854ad17 MD5 | raw file
  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. #include <sys/types.h>
  20. #include <sys/poll.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/stat.h>
  23. #include <sys/sysmacros.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <mntent.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <linux/fs.h>
  33. #include <linux/major.h>
  34. #include <linux/hyperv.h>
  35. #include <syslog.h>
  36. #include <getopt.h>
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. /* Don't use syslog() in the function since that can cause write to disk */
  40. static int vss_do_freeze(char *dir, unsigned int cmd)
  41. {
  42. int ret, fd = open(dir, O_RDONLY);
  43. if (fd < 0)
  44. return 1;
  45. ret = ioctl(fd, cmd, 0);
  46. /*
  47. * If a partition is mounted more than once, only the first
  48. * FREEZE/THAW can succeed and the later ones will get
  49. * EBUSY/EINVAL respectively: there could be 2 cases:
  50. * 1) a user may mount the same partition to differnt directories
  51. * by mistake or on purpose;
  52. * 2) The subvolume of btrfs appears to have the same partition
  53. * mounted more than once.
  54. */
  55. if (ret) {
  56. if ((cmd == FIFREEZE && errno == EBUSY) ||
  57. (cmd == FITHAW && errno == EINVAL)) {
  58. close(fd);
  59. return 0;
  60. }
  61. }
  62. close(fd);
  63. return !!ret;
  64. }
  65. static bool is_dev_loop(const char *blkname)
  66. {
  67. char *buffer;
  68. DIR *dir;
  69. struct dirent *entry;
  70. bool ret = false;
  71. buffer = malloc(PATH_MAX);
  72. if (!buffer) {
  73. syslog(LOG_ERR, "Can't allocate memory!");
  74. exit(1);
  75. }
  76. snprintf(buffer, PATH_MAX, "%s/loop", blkname);
  77. if (!access(buffer, R_OK | X_OK)) {
  78. ret = true;
  79. goto free_buffer;
  80. } else if (errno != ENOENT) {
  81. syslog(LOG_ERR, "Can't access: %s; error:%d %s!",
  82. buffer, errno, strerror(errno));
  83. }
  84. snprintf(buffer, PATH_MAX, "%s/slaves", blkname);
  85. dir = opendir(buffer);
  86. if (!dir) {
  87. if (errno != ENOENT)
  88. syslog(LOG_ERR, "Can't opendir: %s; error:%d %s!",
  89. buffer, errno, strerror(errno));
  90. goto free_buffer;
  91. }
  92. while ((entry = readdir(dir)) != NULL) {
  93. if (strcmp(entry->d_name, ".") == 0 ||
  94. strcmp(entry->d_name, "..") == 0)
  95. continue;
  96. snprintf(buffer, PATH_MAX, "%s/slaves/%s", blkname,
  97. entry->d_name);
  98. if (is_dev_loop(buffer)) {
  99. ret = true;
  100. break;
  101. }
  102. }
  103. closedir(dir);
  104. free_buffer:
  105. free(buffer);
  106. return ret;
  107. }
  108. static int vss_operate(int operation)
  109. {
  110. char match[] = "/dev/";
  111. FILE *mounts;
  112. struct mntent *ent;
  113. struct stat sb;
  114. char errdir[1024] = {0};
  115. char blkdir[23]; /* /sys/dev/block/XXX:XXX */
  116. unsigned int cmd;
  117. int error = 0, root_seen = 0, save_errno = 0;
  118. switch (operation) {
  119. case VSS_OP_FREEZE:
  120. cmd = FIFREEZE;
  121. break;
  122. case VSS_OP_THAW:
  123. cmd = FITHAW;
  124. break;
  125. default:
  126. return -1;
  127. }
  128. mounts = setmntent("/proc/mounts", "r");
  129. if (mounts == NULL)
  130. return -1;
  131. while ((ent = getmntent(mounts))) {
  132. if (strncmp(ent->mnt_fsname, match, strlen(match)))
  133. continue;
  134. if (stat(ent->mnt_fsname, &sb)) {
  135. syslog(LOG_ERR, "Can't stat: %s; error:%d %s!",
  136. ent->mnt_fsname, errno, strerror(errno));
  137. } else {
  138. sprintf(blkdir, "/sys/dev/block/%d:%d",
  139. major(sb.st_rdev), minor(sb.st_rdev));
  140. if (is_dev_loop(blkdir))
  141. continue;
  142. }
  143. if (hasmntopt(ent, MNTOPT_RO) != NULL)
  144. continue;
  145. if (strcmp(ent->mnt_type, "vfat") == 0)
  146. continue;
  147. if (strcmp(ent->mnt_dir, "/") == 0) {
  148. root_seen = 1;
  149. continue;
  150. }
  151. error |= vss_do_freeze(ent->mnt_dir, cmd);
  152. if (error && operation == VSS_OP_FREEZE)
  153. goto err;
  154. }
  155. endmntent(mounts);
  156. if (root_seen) {
  157. error |= vss_do_freeze("/", cmd);
  158. if (error && operation == VSS_OP_FREEZE)
  159. goto err;
  160. }
  161. goto out;
  162. err:
  163. save_errno = errno;
  164. if (ent) {
  165. strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
  166. endmntent(mounts);
  167. }
  168. vss_operate(VSS_OP_THAW);
  169. /* Call syslog after we thaw all filesystems */
  170. if (ent)
  171. syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
  172. errdir, save_errno, strerror(save_errno));
  173. else
  174. syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
  175. strerror(save_errno));
  176. out:
  177. return error;
  178. }
  179. void print_usage(char *argv[])
  180. {
  181. fprintf(stderr, "Usage: %s [options]\n"
  182. "Options are:\n"
  183. " -n, --no-daemon stay in foreground, don't daemonize\n"
  184. " -h, --help print this help\n", argv[0]);
  185. }
  186. int main(int argc, char *argv[])
  187. {
  188. int vss_fd, len;
  189. int error;
  190. struct pollfd pfd;
  191. int op;
  192. struct hv_vss_msg vss_msg[1];
  193. int daemonize = 1, long_index = 0, opt;
  194. int in_handshake = 1;
  195. __u32 kernel_modver;
  196. static struct option long_options[] = {
  197. {"help", no_argument, 0, 'h' },
  198. {"no-daemon", no_argument, 0, 'n' },
  199. {0, 0, 0, 0 }
  200. };
  201. while ((opt = getopt_long(argc, argv, "hn", long_options,
  202. &long_index)) != -1) {
  203. switch (opt) {
  204. case 'n':
  205. daemonize = 0;
  206. break;
  207. case 'h':
  208. default:
  209. print_usage(argv);
  210. exit(EXIT_FAILURE);
  211. }
  212. }
  213. if (daemonize && daemon(1, 0))
  214. return 1;
  215. openlog("Hyper-V VSS", 0, LOG_USER);
  216. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  217. vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
  218. if (vss_fd < 0) {
  219. syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
  220. errno, strerror(errno));
  221. exit(EXIT_FAILURE);
  222. }
  223. /*
  224. * Register ourselves with the kernel.
  225. */
  226. vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
  227. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  228. if (len < 0) {
  229. syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
  230. errno, strerror(errno));
  231. close(vss_fd);
  232. exit(EXIT_FAILURE);
  233. }
  234. pfd.fd = vss_fd;
  235. while (1) {
  236. pfd.events = POLLIN;
  237. pfd.revents = 0;
  238. if (poll(&pfd, 1, -1) < 0) {
  239. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  240. if (errno == EINVAL) {
  241. close(vss_fd);
  242. exit(EXIT_FAILURE);
  243. }
  244. else
  245. continue;
  246. }
  247. len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  248. if (in_handshake) {
  249. if (len != sizeof(kernel_modver)) {
  250. syslog(LOG_ERR, "invalid version negotiation");
  251. exit(EXIT_FAILURE);
  252. }
  253. kernel_modver = *(__u32 *)vss_msg;
  254. in_handshake = 0;
  255. syslog(LOG_INFO, "VSS: kernel module version: %d",
  256. kernel_modver);
  257. continue;
  258. }
  259. if (len != sizeof(struct hv_vss_msg)) {
  260. syslog(LOG_ERR, "read failed; error:%d %s",
  261. errno, strerror(errno));
  262. close(vss_fd);
  263. return EXIT_FAILURE;
  264. }
  265. op = vss_msg->vss_hdr.operation;
  266. error = HV_S_OK;
  267. switch (op) {
  268. case VSS_OP_FREEZE:
  269. case VSS_OP_THAW:
  270. error = vss_operate(op);
  271. syslog(LOG_INFO, "VSS: op=%s: %s\n",
  272. op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
  273. error ? "failed" : "succeeded");
  274. if (error) {
  275. error = HV_E_FAIL;
  276. syslog(LOG_ERR, "op=%d failed!", op);
  277. syslog(LOG_ERR, "report it with these files:");
  278. syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
  279. }
  280. break;
  281. case VSS_OP_HOT_BACKUP:
  282. syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
  283. break;
  284. default:
  285. syslog(LOG_ERR, "Illegal op:%d\n", op);
  286. }
  287. vss_msg->error = error;
  288. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  289. if (len != sizeof(struct hv_vss_msg)) {
  290. syslog(LOG_ERR, "write failed; error: %d %s", errno,
  291. strerror(errno));
  292. if (op == VSS_OP_FREEZE)
  293. vss_operate(VSS_OP_THAW);
  294. }
  295. }
  296. close(vss_fd);
  297. exit(0);
  298. }