hv_vss_daemon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <fcntl.h>
  23. #include <stdio.h>
  24. #include <mntent.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <linux/fs.h>
  31. #include <linux/hyperv.h>
  32. #include <syslog.h>
  33. #include <getopt.h>
  34. /* Don't use syslog() in the function since that can cause write to disk */
  35. static int vss_do_freeze(char *dir, unsigned int cmd)
  36. {
  37. int ret, fd = open(dir, O_RDONLY);
  38. if (fd < 0)
  39. return 1;
  40. ret = ioctl(fd, cmd, 0);
  41. /*
  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 differnt directories
  46. * by mistake or on purpose;
  47. * 2) The subvolume of btrfs appears to have the same partition
  48. * mounted more than once.
  49. */
  50. if (ret) {
  51. if ((cmd == FIFREEZE && errno == EBUSY) ||
  52. (cmd == FITHAW && errno == EINVAL)) {
  53. close(fd);
  54. return 0;
  55. }
  56. }
  57. close(fd);
  58. return !!ret;
  59. }
  60. static int vss_operate(int operation)
  61. {
  62. char match[] = "/dev/";
  63. FILE *mounts;
  64. struct mntent *ent;
  65. char errdir[1024] = {0};
  66. unsigned int cmd;
  67. int error = 0, root_seen = 0, save_errno = 0;
  68. switch (operation) {
  69. case VSS_OP_FREEZE:
  70. cmd = FIFREEZE;
  71. break;
  72. case VSS_OP_THAW:
  73. cmd = FITHAW;
  74. break;
  75. default:
  76. return -1;
  77. }
  78. mounts = setmntent("/proc/mounts", "r");
  79. if (mounts == NULL)
  80. return -1;
  81. while ((ent = getmntent(mounts))) {
  82. if (strncmp(ent->mnt_fsname, match, strlen(match)))
  83. continue;
  84. if (hasmntopt(ent, MNTOPT_RO) != NULL)
  85. continue;
  86. if (strcmp(ent->mnt_type, "vfat") == 0)
  87. continue;
  88. if (strcmp(ent->mnt_dir, "/") == 0) {
  89. root_seen = 1;
  90. continue;
  91. }
  92. error |= vss_do_freeze(ent->mnt_dir, cmd);
  93. if (error && operation == VSS_OP_FREEZE)
  94. goto err;
  95. }
  96. endmntent(mounts);
  97. if (root_seen) {
  98. error |= vss_do_freeze("/", cmd);
  99. if (error && operation == VSS_OP_FREEZE)
  100. goto err;
  101. }
  102. goto out;
  103. err:
  104. save_errno = errno;
  105. if (ent) {
  106. strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
  107. endmntent(mounts);
  108. }
  109. vss_operate(VSS_OP_THAW);
  110. /* Call syslog after we thaw all filesystems */
  111. if (ent)
  112. syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
  113. errdir, save_errno, strerror(save_errno));
  114. else
  115. syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
  116. strerror(save_errno));
  117. out:
  118. return error;
  119. }
  120. void print_usage(char *argv[])
  121. {
  122. fprintf(stderr, "Usage: %s [options]\n"
  123. "Options are:\n"
  124. " -n, --no-daemon stay in foreground, don't daemonize\n"
  125. " -h, --help print this help\n", argv[0]);
  126. }
  127. int main(int argc, char *argv[])
  128. {
  129. int vss_fd, len;
  130. int error;
  131. struct pollfd pfd;
  132. int op;
  133. struct hv_vss_msg vss_msg[1];
  134. int daemonize = 1, long_index = 0, opt;
  135. int in_handshake = 1;
  136. __u32 kernel_modver;
  137. static struct option long_options[] = {
  138. {"help", no_argument, 0, 'h' },
  139. {"no-daemon", no_argument, 0, 'n' },
  140. {0, 0, 0, 0 }
  141. };
  142. while ((opt = getopt_long(argc, argv, "hn", long_options,
  143. &long_index)) != -1) {
  144. switch (opt) {
  145. case 'n':
  146. daemonize = 0;
  147. break;
  148. case 'h':
  149. default:
  150. print_usage(argv);
  151. exit(EXIT_FAILURE);
  152. }
  153. }
  154. if (daemonize && daemon(1, 0))
  155. return 1;
  156. openlog("Hyper-V VSS", 0, LOG_USER);
  157. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  158. vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
  159. if (vss_fd < 0) {
  160. syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
  161. errno, strerror(errno));
  162. exit(EXIT_FAILURE);
  163. }
  164. /*
  165. * Register ourselves with the kernel.
  166. */
  167. vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
  168. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  169. if (len < 0) {
  170. syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
  171. errno, strerror(errno));
  172. close(vss_fd);
  173. exit(EXIT_FAILURE);
  174. }
  175. pfd.fd = vss_fd;
  176. while (1) {
  177. pfd.events = POLLIN;
  178. pfd.revents = 0;
  179. if (poll(&pfd, 1, -1) < 0) {
  180. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  181. if (errno == EINVAL) {
  182. close(vss_fd);
  183. exit(EXIT_FAILURE);
  184. }
  185. else
  186. continue;
  187. }
  188. len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  189. if (in_handshake) {
  190. if (len != sizeof(kernel_modver)) {
  191. syslog(LOG_ERR, "invalid version negotiation");
  192. exit(EXIT_FAILURE);
  193. }
  194. kernel_modver = *(__u32 *)vss_msg;
  195. in_handshake = 0;
  196. syslog(LOG_INFO, "VSS: kernel module version: %d",
  197. kernel_modver);
  198. continue;
  199. }
  200. if (len != sizeof(struct hv_vss_msg)) {
  201. syslog(LOG_ERR, "read failed; error:%d %s",
  202. errno, strerror(errno));
  203. close(vss_fd);
  204. return EXIT_FAILURE;
  205. }
  206. op = vss_msg->vss_hdr.operation;
  207. error = HV_S_OK;
  208. switch (op) {
  209. case VSS_OP_FREEZE:
  210. case VSS_OP_THAW:
  211. error = vss_operate(op);
  212. syslog(LOG_INFO, "VSS: op=%s: %s\n",
  213. op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
  214. error ? "failed" : "succeeded");
  215. if (error) {
  216. error = HV_E_FAIL;
  217. syslog(LOG_ERR, "op=%d failed!", op);
  218. syslog(LOG_ERR, "report it with these files:");
  219. syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
  220. }
  221. break;
  222. default:
  223. syslog(LOG_ERR, "Illegal op:%d\n", op);
  224. }
  225. vss_msg->error = error;
  226. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  227. if (len != sizeof(struct hv_vss_msg)) {
  228. syslog(LOG_ERR, "write failed; error: %d %s", errno,
  229. strerror(errno));
  230. exit(EXIT_FAILURE);
  231. }
  232. }
  233. close(vss_fd);
  234. exit(0);
  235. }