hv_vss_daemon.c 6.4 KB

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