hv_vss_daemon.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. print_usage(argv);
  209. exit(0);
  210. default:
  211. print_usage(argv);
  212. exit(EXIT_FAILURE);
  213. }
  214. }
  215. if (daemonize && daemon(1, 0))
  216. return 1;
  217. openlog("Hyper-V VSS", 0, LOG_USER);
  218. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  219. vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
  220. if (vss_fd < 0) {
  221. syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
  222. errno, strerror(errno));
  223. exit(EXIT_FAILURE);
  224. }
  225. /*
  226. * Register ourselves with the kernel.
  227. */
  228. vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
  229. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  230. if (len < 0) {
  231. syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
  232. errno, strerror(errno));
  233. close(vss_fd);
  234. exit(EXIT_FAILURE);
  235. }
  236. pfd.fd = vss_fd;
  237. while (1) {
  238. pfd.events = POLLIN;
  239. pfd.revents = 0;
  240. if (poll(&pfd, 1, -1) < 0) {
  241. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  242. if (errno == EINVAL) {
  243. close(vss_fd);
  244. exit(EXIT_FAILURE);
  245. }
  246. else
  247. continue;
  248. }
  249. len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  250. if (in_handshake) {
  251. if (len != sizeof(kernel_modver)) {
  252. syslog(LOG_ERR, "invalid version negotiation");
  253. exit(EXIT_FAILURE);
  254. }
  255. kernel_modver = *(__u32 *)vss_msg;
  256. in_handshake = 0;
  257. syslog(LOG_INFO, "VSS: kernel module version: %d",
  258. kernel_modver);
  259. continue;
  260. }
  261. if (len != sizeof(struct hv_vss_msg)) {
  262. syslog(LOG_ERR, "read failed; error:%d %s",
  263. errno, strerror(errno));
  264. close(vss_fd);
  265. return EXIT_FAILURE;
  266. }
  267. op = vss_msg->vss_hdr.operation;
  268. error = HV_S_OK;
  269. switch (op) {
  270. case VSS_OP_FREEZE:
  271. case VSS_OP_THAW:
  272. error = vss_operate(op);
  273. syslog(LOG_INFO, "VSS: op=%s: %s\n",
  274. op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
  275. error ? "failed" : "succeeded");
  276. if (error) {
  277. error = HV_E_FAIL;
  278. syslog(LOG_ERR, "op=%d failed!", op);
  279. syslog(LOG_ERR, "report it with these files:");
  280. syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
  281. }
  282. break;
  283. case VSS_OP_HOT_BACKUP:
  284. syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
  285. break;
  286. default:
  287. syslog(LOG_ERR, "Illegal op:%d\n", op);
  288. }
  289. vss_msg->error = error;
  290. len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
  291. if (len != sizeof(struct hv_vss_msg)) {
  292. syslog(LOG_ERR, "write failed; error: %d %s", errno,
  293. strerror(errno));
  294. if (op == VSS_OP_FREEZE)
  295. vss_operate(VSS_OP_THAW);
  296. }
  297. }
  298. close(vss_fd);
  299. exit(0);
  300. }