hv_vss_daemon.c 7.2 KB

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