hv_fcopy.c 9.8 KB

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