hv_fcopy.c 11 KB

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