hv_fcopy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/semaphore.h>
  21. #include <linux/fs.h>
  22. #include <linux/nls.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/cdev.h>
  25. #include <linux/hyperv.h>
  26. #include <linux/sched.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/miscdevice.h>
  29. #include "hyperv_vmbus.h"
  30. #define WIN8_SRV_MAJOR 1
  31. #define WIN8_SRV_MINOR 1
  32. #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
  33. /*
  34. * Global state maintained for transaction that is being processed.
  35. * For a class of integration services, including the "file copy service",
  36. * the specified protocol is a "request/response" protocol which means that
  37. * there can only be single outstanding transaction from the host at any
  38. * given point in time. We use this to simplify memory management in this
  39. * driver - we cache and process only one message at a time.
  40. *
  41. * While the request/response protocol is guaranteed by the host, we further
  42. * ensure this by serializing packet processing in this driver - we do not
  43. * read additional packets from the VMBUs until the current packet is fully
  44. * handled.
  45. *
  46. * The transaction "active" state is set when we receive a request from the
  47. * host and we cleanup this state when the transaction is completed - when we
  48. * respond to the host with our response. When the transaction active state is
  49. * set, we defer handling incoming packets.
  50. */
  51. static struct {
  52. bool active; /* transaction status - active or not */
  53. int recv_len; /* number of bytes received. */
  54. struct hv_fcopy_hdr *fcopy_msg; /* current message */
  55. struct hv_start_fcopy message; /* sent to daemon */
  56. struct vmbus_channel *recv_channel; /* chn we got the request */
  57. u64 recv_req_id; /* request ID. */
  58. void *fcopy_context; /* for the channel callback */
  59. struct semaphore read_sema;
  60. } fcopy_transaction;
  61. static bool opened; /* currently device opened */
  62. /*
  63. * Before we can accept copy messages from the host, we need
  64. * to handshake with the user level daemon. This state tracks
  65. * if we are in the handshake phase.
  66. */
  67. static bool in_hand_shake = true;
  68. static void fcopy_send_data(void);
  69. static void fcopy_respond_to_host(int error);
  70. static void fcopy_work_func(struct work_struct *dummy);
  71. static DECLARE_DELAYED_WORK(fcopy_work, fcopy_work_func);
  72. static u8 *recv_buffer;
  73. static void fcopy_work_func(struct work_struct *dummy)
  74. {
  75. /*
  76. * If the timer fires, the user-mode component has not responded;
  77. * process the pending transaction.
  78. */
  79. fcopy_respond_to_host(HV_E_FAIL);
  80. }
  81. static int fcopy_handle_handshake(u32 version)
  82. {
  83. switch (version) {
  84. case FCOPY_CURRENT_VERSION:
  85. break;
  86. default:
  87. /*
  88. * For now we will fail the registration.
  89. * If and when we have multiple versions to
  90. * deal with, we will be backward compatible.
  91. * We will add this code when needed.
  92. */
  93. return -EINVAL;
  94. }
  95. pr_info("FCP: user-mode registering done. Daemon version: %d\n",
  96. version);
  97. fcopy_transaction.active = false;
  98. if (fcopy_transaction.fcopy_context)
  99. hv_fcopy_onchannelcallback(fcopy_transaction.fcopy_context);
  100. in_hand_shake = false;
  101. return 0;
  102. }
  103. static void fcopy_send_data(void)
  104. {
  105. struct hv_start_fcopy *smsg_out = &fcopy_transaction.message;
  106. int operation = fcopy_transaction.fcopy_msg->operation;
  107. struct hv_start_fcopy *smsg_in;
  108. /*
  109. * The strings sent from the host are encoded in
  110. * in utf16; convert it to utf8 strings.
  111. * The host assures us that the utf16 strings will not exceed
  112. * the max lengths specified. We will however, reserve room
  113. * for the string terminating character - in the utf16s_utf8s()
  114. * function we limit the size of the buffer where the converted
  115. * string is placed to W_MAX_PATH -1 to guarantee
  116. * that the strings can be properly terminated!
  117. */
  118. switch (operation) {
  119. case START_FILE_COPY:
  120. memset(smsg_out, 0, sizeof(struct hv_start_fcopy));
  121. smsg_out->hdr.operation = operation;
  122. smsg_in = (struct hv_start_fcopy *)fcopy_transaction.fcopy_msg;
  123. utf16s_to_utf8s((wchar_t *)smsg_in->file_name, W_MAX_PATH,
  124. UTF16_LITTLE_ENDIAN,
  125. (__u8 *)smsg_out->file_name, W_MAX_PATH - 1);
  126. utf16s_to_utf8s((wchar_t *)smsg_in->path_name, W_MAX_PATH,
  127. UTF16_LITTLE_ENDIAN,
  128. (__u8 *)smsg_out->path_name, W_MAX_PATH - 1);
  129. smsg_out->copy_flags = smsg_in->copy_flags;
  130. smsg_out->file_size = smsg_in->file_size;
  131. break;
  132. default:
  133. break;
  134. }
  135. up(&fcopy_transaction.read_sema);
  136. return;
  137. }
  138. /*
  139. * Send a response back to the host.
  140. */
  141. static void
  142. fcopy_respond_to_host(int error)
  143. {
  144. struct icmsg_hdr *icmsghdr;
  145. u32 buf_len;
  146. struct vmbus_channel *channel;
  147. u64 req_id;
  148. /*
  149. * Copy the global state for completing the transaction. Note that
  150. * only one transaction can be active at a time. This is guaranteed
  151. * by the file copy protocol implemented by the host. Furthermore,
  152. * the "transaction active" state we maintain ensures that there can
  153. * only be one active transaction at a time.
  154. */
  155. buf_len = fcopy_transaction.recv_len;
  156. channel = fcopy_transaction.recv_channel;
  157. req_id = fcopy_transaction.recv_req_id;
  158. fcopy_transaction.active = false;
  159. icmsghdr = (struct icmsg_hdr *)
  160. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  161. if (channel->onchannel_callback == NULL)
  162. /*
  163. * We have raced with util driver being unloaded;
  164. * silently return.
  165. */
  166. return;
  167. icmsghdr->status = error;
  168. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  169. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  170. VM_PKT_DATA_INBAND, 0);
  171. }
  172. void hv_fcopy_onchannelcallback(void *context)
  173. {
  174. struct vmbus_channel *channel = context;
  175. u32 recvlen;
  176. u64 requestid;
  177. struct hv_fcopy_hdr *fcopy_msg;
  178. struct icmsg_hdr *icmsghdr;
  179. struct icmsg_negotiate *negop = NULL;
  180. int util_fw_version;
  181. int fcopy_srv_version;
  182. if (fcopy_transaction.active) {
  183. /*
  184. * We will defer processing this callback once
  185. * the current transaction is complete.
  186. */
  187. fcopy_transaction.fcopy_context = context;
  188. return;
  189. }
  190. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
  191. &requestid);
  192. if (recvlen <= 0)
  193. return;
  194. icmsghdr = (struct icmsg_hdr *)&recv_buffer[
  195. sizeof(struct vmbuspipe_hdr)];
  196. if (icmsghdr->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  197. util_fw_version = UTIL_FW_VERSION;
  198. fcopy_srv_version = WIN8_SRV_VERSION;
  199. vmbus_prep_negotiate_resp(icmsghdr, negop, recv_buffer,
  200. util_fw_version, fcopy_srv_version);
  201. } else {
  202. fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[
  203. sizeof(struct vmbuspipe_hdr) +
  204. sizeof(struct icmsg_hdr)];
  205. /*
  206. * Stash away this global state for completing the
  207. * transaction; note transactions are serialized.
  208. */
  209. fcopy_transaction.active = true;
  210. fcopy_transaction.recv_len = recvlen;
  211. fcopy_transaction.recv_channel = channel;
  212. fcopy_transaction.recv_req_id = requestid;
  213. fcopy_transaction.fcopy_msg = fcopy_msg;
  214. /*
  215. * Send the information to the user-level daemon.
  216. */
  217. schedule_delayed_work(&fcopy_work, 5*HZ);
  218. fcopy_send_data();
  219. return;
  220. }
  221. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  222. vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
  223. VM_PKT_DATA_INBAND, 0);
  224. }
  225. /*
  226. * Create a char device that can support read/write for passing
  227. * the payload.
  228. */
  229. static ssize_t fcopy_read(struct file *file, char __user *buf,
  230. size_t count, loff_t *ppos)
  231. {
  232. void *src;
  233. size_t copy_size;
  234. int operation;
  235. /*
  236. * Wait until there is something to be read.
  237. */
  238. if (down_interruptible(&fcopy_transaction.read_sema))
  239. return -EINTR;
  240. /*
  241. * The channel may be rescinded and in this case, we will wakeup the
  242. * the thread blocked on the semaphore and we will use the opened
  243. * state to correctly handle this case.
  244. */
  245. if (!opened)
  246. return -ENODEV;
  247. operation = fcopy_transaction.fcopy_msg->operation;
  248. if (operation == START_FILE_COPY) {
  249. src = &fcopy_transaction.message;
  250. copy_size = sizeof(struct hv_start_fcopy);
  251. if (count < copy_size)
  252. return 0;
  253. } else {
  254. src = fcopy_transaction.fcopy_msg;
  255. copy_size = sizeof(struct hv_do_fcopy);
  256. if (count < copy_size)
  257. return 0;
  258. }
  259. if (copy_to_user(buf, src, copy_size))
  260. return -EFAULT;
  261. return copy_size;
  262. }
  263. static ssize_t fcopy_write(struct file *file, const char __user *buf,
  264. size_t count, loff_t *ppos)
  265. {
  266. int response = 0;
  267. if (count != sizeof(int))
  268. return -EINVAL;
  269. if (copy_from_user(&response, buf, sizeof(int)))
  270. return -EFAULT;
  271. if (in_hand_shake) {
  272. if (fcopy_handle_handshake(response))
  273. return -EINVAL;
  274. return sizeof(int);
  275. }
  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_work))
  281. fcopy_respond_to_host(response);
  282. return sizeof(int);
  283. }
  284. static int fcopy_open(struct inode *inode, struct file *f)
  285. {
  286. /*
  287. * The user level daemon that will open this device is
  288. * really an extension of this driver. We can have only
  289. * active open at a time.
  290. */
  291. if (opened)
  292. return -EBUSY;
  293. /*
  294. * The daemon is alive; setup the state.
  295. */
  296. opened = true;
  297. return 0;
  298. }
  299. static int fcopy_release(struct inode *inode, struct file *f)
  300. {
  301. /*
  302. * The daemon has exited; reset the state.
  303. */
  304. in_hand_shake = true;
  305. opened = false;
  306. return 0;
  307. }
  308. static const struct file_operations fcopy_fops = {
  309. .read = fcopy_read,
  310. .write = fcopy_write,
  311. .release = fcopy_release,
  312. .open = fcopy_open,
  313. };
  314. static struct miscdevice fcopy_misc = {
  315. .minor = MISC_DYNAMIC_MINOR,
  316. .name = "vmbus/hv_fcopy",
  317. .fops = &fcopy_fops,
  318. };
  319. static int fcopy_dev_init(void)
  320. {
  321. return misc_register(&fcopy_misc);
  322. }
  323. static void fcopy_dev_deinit(void)
  324. {
  325. /*
  326. * The device is going away - perhaps because the
  327. * host has rescinded the channel. Setup state so that
  328. * user level daemon can gracefully exit if it is blocked
  329. * on the read semaphore.
  330. */
  331. opened = false;
  332. /*
  333. * Signal the semaphore as the device is
  334. * going away.
  335. */
  336. up(&fcopy_transaction.read_sema);
  337. misc_deregister(&fcopy_misc);
  338. }
  339. int hv_fcopy_init(struct hv_util_service *srv)
  340. {
  341. recv_buffer = srv->recv_buffer;
  342. /*
  343. * When this driver loads, the user level daemon that
  344. * processes the host requests may not yet be running.
  345. * Defer processing channel callbacks until the daemon
  346. * has registered.
  347. */
  348. fcopy_transaction.active = true;
  349. sema_init(&fcopy_transaction.read_sema, 0);
  350. return fcopy_dev_init();
  351. }
  352. void hv_fcopy_deinit(void)
  353. {
  354. cancel_delayed_work_sync(&fcopy_work);
  355. fcopy_dev_deinit();
  356. }