hv_fcopy.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * An implementation of file copy service.
  3. *
  4. * Copyright (C) 2014, Microsoft, Inc.
  5. *
  6. * Author : K. Y. Srinivasan <ksrinivasan@novell.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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/nls.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/hyperv.h>
  23. #include <linux/sched.h>
  24. #include "hyperv_vmbus.h"
  25. #include "hv_utils_transport.h"
  26. #define WIN8_SRV_MAJOR 1
  27. #define WIN8_SRV_MINOR 1
  28. #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
  29. #define FCOPY_VER_COUNT 1
  30. static const int fcopy_versions[] = {
  31. WIN8_SRV_VERSION
  32. };
  33. #define FW_VER_COUNT 1
  34. static const int fw_versions[] = {
  35. UTIL_FW_VERSION
  36. };
  37. /*
  38. * Global state maintained for transaction that is being processed.
  39. * For a class of integration services, including the "file copy service",
  40. * the specified protocol is a "request/response" protocol which means that
  41. * there can only be single outstanding transaction from the host at any
  42. * given point in time. We use this to simplify memory management in this
  43. * driver - we cache and process only one message at a time.
  44. *
  45. * While the request/response protocol is guaranteed by the host, we further
  46. * ensure this by serializing packet processing in this driver - we do not
  47. * read additional packets from the VMBUs until the current packet is fully
  48. * handled.
  49. */
  50. static struct {
  51. int state; /* hvutil_device_state */
  52. int recv_len; /* number of bytes received. */
  53. struct hv_fcopy_hdr *fcopy_msg; /* current message */
  54. struct vmbus_channel *recv_channel; /* chn we got the request */
  55. u64 recv_req_id; /* request ID. */
  56. } fcopy_transaction;
  57. static void fcopy_respond_to_host(int error);
  58. static void fcopy_send_data(struct work_struct *dummy);
  59. static void fcopy_timeout_func(struct work_struct *dummy);
  60. static DECLARE_DELAYED_WORK(fcopy_timeout_work, fcopy_timeout_func);
  61. static DECLARE_WORK(fcopy_send_work, fcopy_send_data);
  62. static const char fcopy_devname[] = "vmbus/hv_fcopy";
  63. static u8 *recv_buffer;
  64. static struct hvutil_transport *hvt;
  65. /*
  66. * This state maintains the version number registered by the daemon.
  67. */
  68. static int dm_reg_value;
  69. static void fcopy_poll_wrapper(void *channel)
  70. {
  71. /* Transaction is finished, reset the state here to avoid races. */
  72. fcopy_transaction.state = HVUTIL_READY;
  73. hv_fcopy_onchannelcallback(channel);
  74. }
  75. static void fcopy_timeout_func(struct work_struct *dummy)
  76. {
  77. /*
  78. * If the timer fires, the user-mode component has not responded;
  79. * process the pending transaction.
  80. */
  81. fcopy_respond_to_host(HV_E_FAIL);
  82. hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
  83. }
  84. static void fcopy_register_done(void)
  85. {
  86. pr_debug("FCP: userspace daemon registered\n");
  87. hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
  88. }
  89. static int fcopy_handle_handshake(u32 version)
  90. {
  91. u32 our_ver = FCOPY_CURRENT_VERSION;
  92. switch (version) {
  93. case FCOPY_VERSION_0:
  94. /* Daemon doesn't expect us to reply */
  95. dm_reg_value = version;
  96. break;
  97. case FCOPY_VERSION_1:
  98. /* Daemon expects us to reply with our own version */
  99. if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
  100. fcopy_register_done))
  101. return -EFAULT;
  102. dm_reg_value = version;
  103. break;
  104. default:
  105. /*
  106. * For now we will fail the registration.
  107. * If and when we have multiple versions to
  108. * deal with, we will be backward compatible.
  109. * We will add this code when needed.
  110. */
  111. return -EINVAL;
  112. }
  113. pr_debug("FCP: userspace daemon ver. %d connected\n", version);
  114. return 0;
  115. }
  116. static void fcopy_send_data(struct work_struct *dummy)
  117. {
  118. struct hv_start_fcopy *smsg_out = NULL;
  119. int operation = fcopy_transaction.fcopy_msg->operation;
  120. struct hv_start_fcopy *smsg_in;
  121. void *out_src;
  122. int rc, out_len;
  123. /*
  124. * The strings sent from the host are encoded in
  125. * in utf16; convert it to utf8 strings.
  126. * The host assures us that the utf16 strings will not exceed
  127. * the max lengths specified. We will however, reserve room
  128. * for the string terminating character - in the utf16s_utf8s()
  129. * function we limit the size of the buffer where the converted
  130. * string is placed to W_MAX_PATH -1 to guarantee
  131. * that the strings can be properly terminated!
  132. */
  133. switch (operation) {
  134. case START_FILE_COPY:
  135. out_len = sizeof(struct hv_start_fcopy);
  136. smsg_out = kzalloc(sizeof(*smsg_out), GFP_KERNEL);
  137. if (!smsg_out)
  138. return;
  139. smsg_out->hdr.operation = operation;
  140. smsg_in = (struct hv_start_fcopy *)fcopy_transaction.fcopy_msg;
  141. utf16s_to_utf8s((wchar_t *)smsg_in->file_name, W_MAX_PATH,
  142. UTF16_LITTLE_ENDIAN,
  143. (__u8 *)&smsg_out->file_name, W_MAX_PATH - 1);
  144. utf16s_to_utf8s((wchar_t *)smsg_in->path_name, W_MAX_PATH,
  145. UTF16_LITTLE_ENDIAN,
  146. (__u8 *)&smsg_out->path_name, W_MAX_PATH - 1);
  147. smsg_out->copy_flags = smsg_in->copy_flags;
  148. smsg_out->file_size = smsg_in->file_size;
  149. out_src = smsg_out;
  150. break;
  151. default:
  152. out_src = fcopy_transaction.fcopy_msg;
  153. out_len = fcopy_transaction.recv_len;
  154. break;
  155. }
  156. fcopy_transaction.state = HVUTIL_USERSPACE_REQ;
  157. rc = hvutil_transport_send(hvt, out_src, out_len, NULL);
  158. if (rc) {
  159. pr_debug("FCP: failed to communicate to the daemon: %d\n", rc);
  160. if (cancel_delayed_work_sync(&fcopy_timeout_work)) {
  161. fcopy_respond_to_host(HV_E_FAIL);
  162. fcopy_transaction.state = HVUTIL_READY;
  163. }
  164. }
  165. kfree(smsg_out);
  166. }
  167. /*
  168. * Send a response back to the host.
  169. */
  170. static void
  171. fcopy_respond_to_host(int error)
  172. {
  173. struct icmsg_hdr *icmsghdr;
  174. u32 buf_len;
  175. struct vmbus_channel *channel;
  176. u64 req_id;
  177. /*
  178. * Copy the global state for completing the transaction. Note that
  179. * only one transaction can be active at a time. This is guaranteed
  180. * by the file copy protocol implemented by the host. Furthermore,
  181. * the "transaction active" state we maintain ensures that there can
  182. * only be one active transaction at a time.
  183. */
  184. buf_len = fcopy_transaction.recv_len;
  185. channel = fcopy_transaction.recv_channel;
  186. req_id = fcopy_transaction.recv_req_id;
  187. icmsghdr = (struct icmsg_hdr *)
  188. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  189. if (channel->onchannel_callback == NULL)
  190. /*
  191. * We have raced with util driver being unloaded;
  192. * silently return.
  193. */
  194. return;
  195. icmsghdr->status = error;
  196. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  197. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  198. VM_PKT_DATA_INBAND, 0);
  199. }
  200. void hv_fcopy_onchannelcallback(void *context)
  201. {
  202. struct vmbus_channel *channel = context;
  203. u32 recvlen;
  204. u64 requestid;
  205. struct hv_fcopy_hdr *fcopy_msg;
  206. struct icmsg_hdr *icmsghdr;
  207. int fcopy_srv_version;
  208. if (fcopy_transaction.state > HVUTIL_READY)
  209. return;
  210. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
  211. &requestid);
  212. if (recvlen <= 0)
  213. return;
  214. icmsghdr = (struct icmsg_hdr *)&recv_buffer[
  215. sizeof(struct vmbuspipe_hdr)];
  216. if (icmsghdr->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  217. if (vmbus_prep_negotiate_resp(icmsghdr, recv_buffer,
  218. fw_versions, FW_VER_COUNT,
  219. fcopy_versions, FCOPY_VER_COUNT,
  220. NULL, &fcopy_srv_version)) {
  221. pr_info("FCopy IC version %d.%d\n",
  222. fcopy_srv_version >> 16,
  223. fcopy_srv_version & 0xFFFF);
  224. }
  225. } else {
  226. fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[
  227. sizeof(struct vmbuspipe_hdr) +
  228. sizeof(struct icmsg_hdr)];
  229. /*
  230. * Stash away this global state for completing the
  231. * transaction; note transactions are serialized.
  232. */
  233. fcopy_transaction.recv_len = recvlen;
  234. fcopy_transaction.recv_req_id = requestid;
  235. fcopy_transaction.fcopy_msg = fcopy_msg;
  236. if (fcopy_transaction.state < HVUTIL_READY) {
  237. /* Userspace is not registered yet */
  238. fcopy_respond_to_host(HV_E_FAIL);
  239. return;
  240. }
  241. fcopy_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
  242. /*
  243. * Send the information to the user-level daemon.
  244. */
  245. schedule_work(&fcopy_send_work);
  246. schedule_delayed_work(&fcopy_timeout_work,
  247. HV_UTIL_TIMEOUT * HZ);
  248. return;
  249. }
  250. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  251. vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
  252. VM_PKT_DATA_INBAND, 0);
  253. }
  254. /* Callback when data is received from userspace */
  255. static int fcopy_on_msg(void *msg, int len)
  256. {
  257. int *val = (int *)msg;
  258. if (len != sizeof(int))
  259. return -EINVAL;
  260. if (fcopy_transaction.state == HVUTIL_DEVICE_INIT)
  261. return fcopy_handle_handshake(*val);
  262. if (fcopy_transaction.state != HVUTIL_USERSPACE_REQ)
  263. return -EINVAL;
  264. /*
  265. * Complete the transaction by forwarding the result
  266. * to the host. But first, cancel the timeout.
  267. */
  268. if (cancel_delayed_work_sync(&fcopy_timeout_work)) {
  269. fcopy_transaction.state = HVUTIL_USERSPACE_RECV;
  270. fcopy_respond_to_host(*val);
  271. hv_poll_channel(fcopy_transaction.recv_channel,
  272. fcopy_poll_wrapper);
  273. }
  274. return 0;
  275. }
  276. static void fcopy_on_reset(void)
  277. {
  278. /*
  279. * The daemon has exited; reset the state.
  280. */
  281. fcopy_transaction.state = HVUTIL_DEVICE_INIT;
  282. if (cancel_delayed_work_sync(&fcopy_timeout_work))
  283. fcopy_respond_to_host(HV_E_FAIL);
  284. }
  285. int hv_fcopy_init(struct hv_util_service *srv)
  286. {
  287. recv_buffer = srv->recv_buffer;
  288. fcopy_transaction.recv_channel = srv->channel;
  289. /*
  290. * When this driver loads, the user level daemon that
  291. * processes the host requests may not yet be running.
  292. * Defer processing channel callbacks until the daemon
  293. * has registered.
  294. */
  295. fcopy_transaction.state = HVUTIL_DEVICE_INIT;
  296. hvt = hvutil_transport_init(fcopy_devname, 0, 0,
  297. fcopy_on_msg, fcopy_on_reset);
  298. if (!hvt)
  299. return -EFAULT;
  300. return 0;
  301. }
  302. void hv_fcopy_deinit(void)
  303. {
  304. fcopy_transaction.state = HVUTIL_DEVICE_DYING;
  305. cancel_delayed_work_sync(&fcopy_timeout_work);
  306. hvutil_transport_destroy(hvt);
  307. }