hv_fcopy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 <[email protected]>
  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. * 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. if (vmbus_recvpacket(channel, recv_buffer, HV_HYP_PAGE_SIZE * 2, &recvlen, &requestid)) {
  206. pr_err_ratelimited("Fcopy request received. Could not read into recv buf\n");
  207. return;
  208. }
  209. if (!recvlen)
  210. return;
  211. /* Ensure recvlen is big enough to read header data */
  212. if (recvlen < ICMSG_HDR) {
  213. pr_err_ratelimited("Fcopy request received. Packet length too small: %d\n",
  214. recvlen);
  215. return;
  216. }
  217. icmsghdr = (struct icmsg_hdr *)&recv_buffer[
  218. sizeof(struct vmbuspipe_hdr)];
  219. if (icmsghdr->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  220. if (vmbus_prep_negotiate_resp(icmsghdr,
  221. recv_buffer, recvlen,
  222. fw_versions, FW_VER_COUNT,
  223. fcopy_versions, FCOPY_VER_COUNT,
  224. NULL, &fcopy_srv_version)) {
  225. pr_info("FCopy IC version %d.%d\n",
  226. fcopy_srv_version >> 16,
  227. fcopy_srv_version & 0xFFFF);
  228. }
  229. } else if (icmsghdr->icmsgtype == ICMSGTYPE_FCOPY) {
  230. /* Ensure recvlen is big enough to contain hv_fcopy_hdr */
  231. if (recvlen < ICMSG_HDR + sizeof(struct hv_fcopy_hdr)) {
  232. pr_err_ratelimited("Invalid Fcopy hdr. Packet length too small: %u\n",
  233. recvlen);
  234. return;
  235. }
  236. fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[ICMSG_HDR];
  237. /*
  238. * Stash away this global state for completing the
  239. * transaction; note transactions are serialized.
  240. */
  241. fcopy_transaction.recv_len = recvlen;
  242. fcopy_transaction.recv_req_id = requestid;
  243. fcopy_transaction.fcopy_msg = fcopy_msg;
  244. if (fcopy_transaction.state < HVUTIL_READY) {
  245. /* Userspace is not registered yet */
  246. fcopy_respond_to_host(HV_E_FAIL);
  247. return;
  248. }
  249. fcopy_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
  250. /*
  251. * Send the information to the user-level daemon.
  252. */
  253. schedule_work(&fcopy_send_work);
  254. schedule_delayed_work(&fcopy_timeout_work,
  255. HV_UTIL_TIMEOUT * HZ);
  256. return;
  257. } else {
  258. pr_err_ratelimited("Fcopy request received. Invalid msg type: %d\n",
  259. icmsghdr->icmsgtype);
  260. return;
  261. }
  262. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  263. vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
  264. VM_PKT_DATA_INBAND, 0);
  265. }
  266. /* Callback when data is received from userspace */
  267. static int fcopy_on_msg(void *msg, int len)
  268. {
  269. int *val = (int *)msg;
  270. if (len != sizeof(int))
  271. return -EINVAL;
  272. if (fcopy_transaction.state == HVUTIL_DEVICE_INIT)
  273. return fcopy_handle_handshake(*val);
  274. if (fcopy_transaction.state != HVUTIL_USERSPACE_REQ)
  275. return -EINVAL;
  276. /*
  277. * Complete the transaction by forwarding the result
  278. * to the host. But first, cancel the timeout.
  279. */
  280. if (cancel_delayed_work_sync(&fcopy_timeout_work)) {
  281. fcopy_transaction.state = HVUTIL_USERSPACE_RECV;
  282. fcopy_respond_to_host(*val);
  283. hv_poll_channel(fcopy_transaction.recv_channel,
  284. fcopy_poll_wrapper);
  285. }
  286. return 0;
  287. }
  288. static void fcopy_on_reset(void)
  289. {
  290. /*
  291. * The daemon has exited; reset the state.
  292. */
  293. fcopy_transaction.state = HVUTIL_DEVICE_INIT;
  294. if (cancel_delayed_work_sync(&fcopy_timeout_work))
  295. fcopy_respond_to_host(HV_E_FAIL);
  296. }
  297. int hv_fcopy_init(struct hv_util_service *srv)
  298. {
  299. recv_buffer = srv->recv_buffer;
  300. fcopy_transaction.recv_channel = srv->channel;
  301. fcopy_transaction.recv_channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2;
  302. /*
  303. * When this driver loads, the user level daemon that
  304. * processes the host requests may not yet be running.
  305. * Defer processing channel callbacks until the daemon
  306. * has registered.
  307. */
  308. fcopy_transaction.state = HVUTIL_DEVICE_INIT;
  309. hvt = hvutil_transport_init(fcopy_devname, 0, 0,
  310. fcopy_on_msg, fcopy_on_reset);
  311. if (!hvt)
  312. return -EFAULT;
  313. return 0;
  314. }
  315. static void hv_fcopy_cancel_work(void)
  316. {
  317. cancel_delayed_work_sync(&fcopy_timeout_work);
  318. cancel_work_sync(&fcopy_send_work);
  319. }
  320. int hv_fcopy_pre_suspend(void)
  321. {
  322. struct vmbus_channel *channel = fcopy_transaction.recv_channel;
  323. struct hv_fcopy_hdr *fcopy_msg;
  324. /*
  325. * Fake a CANCEL_FCOPY message for the user space daemon in case the
  326. * daemon is in the middle of copying some file. It doesn't matter if
  327. * there is already a message pending to be delivered to the user
  328. * space since we force fcopy_transaction.state to be HVUTIL_READY, so
  329. * the user space daemon's write() will fail with EINVAL (see
  330. * fcopy_on_msg()), and the daemon will reset the device by closing
  331. * and re-opening it.
  332. */
  333. fcopy_msg = kzalloc(sizeof(*fcopy_msg), GFP_KERNEL);
  334. if (!fcopy_msg)
  335. return -ENOMEM;
  336. tasklet_disable(&channel->callback_event);
  337. fcopy_msg->operation = CANCEL_FCOPY;
  338. hv_fcopy_cancel_work();
  339. /* We don't care about the return value. */
  340. hvutil_transport_send(hvt, fcopy_msg, sizeof(*fcopy_msg), NULL);
  341. kfree(fcopy_msg);
  342. fcopy_transaction.state = HVUTIL_READY;
  343. /* tasklet_enable() will be called in hv_fcopy_pre_resume(). */
  344. return 0;
  345. }
  346. int hv_fcopy_pre_resume(void)
  347. {
  348. struct vmbus_channel *channel = fcopy_transaction.recv_channel;
  349. tasklet_enable(&channel->callback_event);
  350. return 0;
  351. }
  352. void hv_fcopy_deinit(void)
  353. {
  354. fcopy_transaction.state = HVUTIL_DEVICE_DYING;
  355. hv_fcopy_cancel_work();
  356. hvutil_transport_destroy(hvt);
  357. }