hv_vss_daemon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. default:
  158. print_usage(argv);
  159. exit(EXIT_FAILURE);
  160. }
  161. }
  162. if (daemonize && daemon(1, 0))
  163. return 1;
  164. openlog("Hyper-V VSS", 0, LOG_USER);
  165. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  166. vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
  167. if (vss_fd < 0) {
  168. syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
  169. errno, strerror(errno));
  170. exit(EXIT_FAILURE);
  171. }
  172. /*
  173. * Register ourselves with the kernel.
  174. */
  175. vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
  176. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  177. if (len < 0) {
  178. syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
  179. errno, strerror(errno));
  180. close(vss_fd);
  181. exit(EXIT_FAILURE);
  182. }
  183. pfd.fd = vss_fd;
  184. while (1) {
  185. pfd.events = POLLIN;
  186. pfd.revents = 0;
  187. if (poll(&pfd, 1, -1) < 0) {
  188. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  189. if (errno == EINVAL) {
  190. close(vss_fd);
  191. exit(EXIT_FAILURE);
  192. }
  193. else
  194. continue;
  195. }
  196. len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  197. if (in_handshake) {
  198. if (len != sizeof(kernel_modver)) {
  199. syslog(LOG_ERR, "invalid version negotiation");
  200. exit(EXIT_FAILURE);
  201. }
  202. kernel_modver = *(__u32 *)vss_msg;
  203. in_handshake = 0;
  204. syslog(LOG_INFO, "VSS: kernel module version: %d",
  205. kernel_modver);
  206. continue;
  207. }
  208. if (len != sizeof(struct hv_vss_msg)) {
  209. syslog(LOG_ERR, "read failed; error:%d %s",
  210. errno, strerror(errno));
  211. close(vss_fd);
  212. return EXIT_FAILURE;
  213. }
  214. op = vss_msg->vss_hdr.operation;
  215. error = HV_S_OK;
  216. switch (op) {
  217. case VSS_OP_FREEZE:
  218. case VSS_OP_THAW:
  219. error = vss_operate(op);
  220. syslog(LOG_INFO, "VSS: op=%s: %s\n",
  221. op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
  222. error ? "failed" : "succeeded");
  223. if (error) {
  224. error = HV_E_FAIL;
  225. syslog(LOG_ERR, "op=%d failed!", op);
  226. syslog(LOG_ERR, "report it with these files:");
  227. syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
  228. }
  229. break;
  230. case VSS_OP_HOT_BACKUP:
  231. syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
  232. break;
  233. default:
  234. syslog(LOG_ERR, "Illegal op:%d\n", op);
  235. }
  236. vss_msg->error = error;
  237. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  238. if (len != sizeof(struct hv_vss_msg)) {
  239. syslog(LOG_ERR, "write failed; error: %d %s", errno,
  240. strerror(errno));
  241. if (op == VSS_OP_FREEZE)
  242. vss_operate(VSS_OP_THAW);
  243. }
  244. }
  245. close(vss_fd);
  246. exit(0);
  247. }