hv_fcopy_daemon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An implementation of host to guest copy functionality for Linux.
  4. *
  5. * Copyright (C) 2014, Microsoft, Inc.
  6. *
  7. * Author : K. Y. Srinivasan <[email protected]>
  8. */
  9. #include <sys/types.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <linux/hyperv.h>
  16. #include <linux/limits.h>
  17. #include <syslog.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <getopt.h>
  21. static int target_fd;
  22. static char target_fname[PATH_MAX];
  23. static unsigned long long filesize;
  24. static int hv_start_fcopy(struct hv_start_fcopy *smsg)
  25. {
  26. int error = HV_E_FAIL;
  27. char *q, *p;
  28. filesize = 0;
  29. p = (char *)smsg->path_name;
  30. snprintf(target_fname, sizeof(target_fname), "%s/%s",
  31. (char *)smsg->path_name, (char *)smsg->file_name);
  32. syslog(LOG_INFO, "Target file name: %s", target_fname);
  33. /*
  34. * Check to see if the path is already in place; if not,
  35. * create if required.
  36. */
  37. while ((q = strchr(p, '/')) != NULL) {
  38. if (q == p) {
  39. p++;
  40. continue;
  41. }
  42. *q = '\0';
  43. if (access((char *)smsg->path_name, F_OK)) {
  44. if (smsg->copy_flags & CREATE_PATH) {
  45. if (mkdir((char *)smsg->path_name, 0755)) {
  46. syslog(LOG_ERR, "Failed to create %s",
  47. (char *)smsg->path_name);
  48. goto done;
  49. }
  50. } else {
  51. syslog(LOG_ERR, "Invalid path: %s",
  52. (char *)smsg->path_name);
  53. goto done;
  54. }
  55. }
  56. p = q + 1;
  57. *q = '/';
  58. }
  59. if (!access(target_fname, F_OK)) {
  60. syslog(LOG_INFO, "File: %s exists", target_fname);
  61. if (!(smsg->copy_flags & OVER_WRITE)) {
  62. error = HV_ERROR_ALREADY_EXISTS;
  63. goto done;
  64. }
  65. }
  66. target_fd = open(target_fname,
  67. O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
  68. if (target_fd == -1) {
  69. syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
  70. goto done;
  71. }
  72. error = 0;
  73. done:
  74. if (error)
  75. target_fname[0] = '\0';
  76. return error;
  77. }
  78. static int hv_copy_data(struct hv_do_fcopy *cpmsg)
  79. {
  80. ssize_t bytes_written;
  81. int ret = 0;
  82. bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
  83. cpmsg->offset);
  84. filesize += cpmsg->size;
  85. if (bytes_written != cpmsg->size) {
  86. switch (errno) {
  87. case ENOSPC:
  88. ret = HV_ERROR_DISK_FULL;
  89. break;
  90. default:
  91. ret = HV_E_FAIL;
  92. break;
  93. }
  94. syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
  95. filesize, (long)bytes_written, strerror(errno));
  96. }
  97. return ret;
  98. }
  99. /*
  100. * Reset target_fname to "" in the two below functions for hibernation: if
  101. * the fcopy operation is aborted by hibernation, the daemon should remove the
  102. * partially-copied file; to achieve this, the hv_utils driver always fakes a
  103. * CANCEL_FCOPY message upon suspend, and later when the VM resumes back,
  104. * the daemon calls hv_copy_cancel() to remove the file; if a file is copied
  105. * successfully before suspend, hv_copy_finished() must reset target_fname to
  106. * avoid that the file can be incorrectly removed upon resume, since the faked
  107. * CANCEL_FCOPY message is spurious in this case.
  108. */
  109. static int hv_copy_finished(void)
  110. {
  111. close(target_fd);
  112. target_fname[0] = '\0';
  113. return 0;
  114. }
  115. static int hv_copy_cancel(void)
  116. {
  117. close(target_fd);
  118. if (strlen(target_fname) > 0) {
  119. unlink(target_fname);
  120. target_fname[0] = '\0';
  121. }
  122. return 0;
  123. }
  124. void print_usage(char *argv[])
  125. {
  126. fprintf(stderr, "Usage: %s [options]\n"
  127. "Options are:\n"
  128. " -n, --no-daemon stay in foreground, don't daemonize\n"
  129. " -h, --help print this help\n", argv[0]);
  130. }
  131. int main(int argc, char *argv[])
  132. {
  133. int fcopy_fd = -1;
  134. int error;
  135. int daemonize = 1, long_index = 0, opt;
  136. int version = FCOPY_CURRENT_VERSION;
  137. union {
  138. struct hv_fcopy_hdr hdr;
  139. struct hv_start_fcopy start;
  140. struct hv_do_fcopy copy;
  141. __u32 kernel_modver;
  142. } buffer = { };
  143. int in_handshake;
  144. static struct option long_options[] = {
  145. {"help", no_argument, 0, 'h' },
  146. {"no-daemon", no_argument, 0, 'n' },
  147. {0, 0, 0, 0 }
  148. };
  149. while ((opt = getopt_long(argc, argv, "hn", long_options,
  150. &long_index)) != -1) {
  151. switch (opt) {
  152. case 'n':
  153. daemonize = 0;
  154. break;
  155. case 'h':
  156. default:
  157. print_usage(argv);
  158. exit(EXIT_FAILURE);
  159. }
  160. }
  161. if (daemonize && daemon(1, 0)) {
  162. syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
  163. exit(EXIT_FAILURE);
  164. }
  165. openlog("HV_FCOPY", 0, LOG_USER);
  166. syslog(LOG_INFO, "starting; pid is:%d", getpid());
  167. reopen_fcopy_fd:
  168. if (fcopy_fd != -1)
  169. close(fcopy_fd);
  170. /* Remove any possible partially-copied file on error */
  171. hv_copy_cancel();
  172. in_handshake = 1;
  173. fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
  174. if (fcopy_fd < 0) {
  175. syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
  176. errno, strerror(errno));
  177. exit(EXIT_FAILURE);
  178. }
  179. /*
  180. * Register with the kernel.
  181. */
  182. if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
  183. syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
  184. exit(EXIT_FAILURE);
  185. }
  186. while (1) {
  187. /*
  188. * In this loop we process fcopy messages after the
  189. * handshake is complete.
  190. */
  191. ssize_t len;
  192. len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
  193. if (len < 0) {
  194. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  195. goto reopen_fcopy_fd;
  196. }
  197. if (in_handshake) {
  198. if (len != sizeof(buffer.kernel_modver)) {
  199. syslog(LOG_ERR, "invalid version negotiation");
  200. exit(EXIT_FAILURE);
  201. }
  202. in_handshake = 0;
  203. syslog(LOG_INFO, "kernel module version: %u",
  204. buffer.kernel_modver);
  205. continue;
  206. }
  207. switch (buffer.hdr.operation) {
  208. case START_FILE_COPY:
  209. error = hv_start_fcopy(&buffer.start);
  210. break;
  211. case WRITE_TO_FILE:
  212. error = hv_copy_data(&buffer.copy);
  213. break;
  214. case COMPLETE_FCOPY:
  215. error = hv_copy_finished();
  216. break;
  217. case CANCEL_FCOPY:
  218. error = hv_copy_cancel();
  219. break;
  220. default:
  221. error = HV_E_FAIL;
  222. syslog(LOG_ERR, "Unknown operation: %d",
  223. buffer.hdr.operation);
  224. }
  225. /*
  226. * pwrite() may return an error due to the faked CANCEL_FCOPY
  227. * message upon hibernation. Ignore the error by resetting the
  228. * dev file, i.e. closing and re-opening it.
  229. */
  230. if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
  231. syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
  232. goto reopen_fcopy_fd;
  233. }
  234. }
  235. }