hv_fcopy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. /* In the case the user-space daemon crashes, hangs or is killed, we
  81. * need to down the semaphore, otherwise, after the daemon starts next
  82. * time, the obsolete data in fcopy_transaction.message or
  83. * fcopy_transaction.fcopy_msg will be used immediately.
  84. *
  85. * NOTE: fcopy_read() happens to get the semaphore (very rare)? We're
  86. * still OK, because we've reported the failure to the host.
  87. */
  88. if (down_trylock(&fcopy_transaction.read_sema))
  89. ;
  90. }
  91. static int fcopy_handle_handshake(u32 version)
  92. {
  93. switch (version) {
  94. case FCOPY_CURRENT_VERSION:
  95. break;
  96. default:
  97. /*
  98. * For now we will fail the registration.
  99. * If and when we have multiple versions to
  100. * deal with, we will be backward compatible.
  101. * We will add this code when needed.
  102. */
  103. return -EINVAL;
  104. }
  105. pr_info("FCP: user-mode registering done. Daemon version: %d\n",
  106. version);
  107. fcopy_transaction.active = false;
  108. if (fcopy_transaction.fcopy_context)
  109. hv_fcopy_onchannelcallback(fcopy_transaction.fcopy_context);
  110. in_hand_shake = false;
  111. return 0;
  112. }
  113. static void fcopy_send_data(void)
  114. {
  115. struct hv_start_fcopy *smsg_out = &fcopy_transaction.message;
  116. int operation = fcopy_transaction.fcopy_msg->operation;
  117. struct hv_start_fcopy *smsg_in;
  118. /*
  119. * The strings sent from the host are encoded in
  120. * in utf16; convert it to utf8 strings.
  121. * The host assures us that the utf16 strings will not exceed
  122. * the max lengths specified. We will however, reserve room
  123. * for the string terminating character - in the utf16s_utf8s()
  124. * function we limit the size of the buffer where the converted
  125. * string is placed to W_MAX_PATH -1 to guarantee
  126. * that the strings can be properly terminated!
  127. */
  128. switch (operation) {
  129. case START_FILE_COPY:
  130. memset(smsg_out, 0, sizeof(struct hv_start_fcopy));
  131. smsg_out->hdr.operation = operation;
  132. smsg_in = (struct hv_start_fcopy *)fcopy_transaction.fcopy_msg;
  133. utf16s_to_utf8s((wchar_t *)smsg_in->file_name, W_MAX_PATH,
  134. UTF16_LITTLE_ENDIAN,
  135. (__u8 *)smsg_out->file_name, W_MAX_PATH - 1);
  136. utf16s_to_utf8s((wchar_t *)smsg_in->path_name, W_MAX_PATH,
  137. UTF16_LITTLE_ENDIAN,
  138. (__u8 *)smsg_out->path_name, W_MAX_PATH - 1);
  139. smsg_out->copy_flags = smsg_in->copy_flags;
  140. smsg_out->file_size = smsg_in->file_size;
  141. break;
  142. default:
  143. break;
  144. }
  145. up(&fcopy_transaction.read_sema);
  146. return;
  147. }
  148. /*
  149. * Send a response back to the host.
  150. */
  151. static void
  152. fcopy_respond_to_host(int error)
  153. {
  154. struct icmsg_hdr *icmsghdr;
  155. u32 buf_len;
  156. struct vmbus_channel *channel;
  157. u64 req_id;
  158. /*
  159. * Copy the global state for completing the transaction. Note that
  160. * only one transaction can be active at a time. This is guaranteed
  161. * by the file copy protocol implemented by the host. Furthermore,
  162. * the "transaction active" state we maintain ensures that there can
  163. * only be one active transaction at a time.
  164. */
  165. buf_len = fcopy_transaction.recv_len;
  166. channel = fcopy_transaction.recv_channel;
  167. req_id = fcopy_transaction.recv_req_id;
  168. fcopy_transaction.active = false;
  169. icmsghdr = (struct icmsg_hdr *)
  170. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  171. if (channel->onchannel_callback == NULL)
  172. /*
  173. * We have raced with util driver being unloaded;
  174. * silently return.
  175. */
  176. return;
  177. icmsghdr->status = error;
  178. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  179. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  180. VM_PKT_DATA_INBAND, 0);
  181. }
  182. void hv_fcopy_onchannelcallback(void *context)
  183. {
  184. struct vmbus_channel *channel = context;
  185. u32 recvlen;
  186. u64 requestid;
  187. struct hv_fcopy_hdr *fcopy_msg;
  188. struct icmsg_hdr *icmsghdr;
  189. struct icmsg_negotiate *negop = NULL;
  190. int util_fw_version;
  191. int fcopy_srv_version;
  192. if (fcopy_transaction.active) {
  193. /*
  194. * We will defer processing this callback once
  195. * the current transaction is complete.
  196. */
  197. fcopy_transaction.fcopy_context = context;
  198. return;
  199. }
  200. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
  201. &requestid);
  202. if (recvlen <= 0)
  203. return;
  204. icmsghdr = (struct icmsg_hdr *)&recv_buffer[
  205. sizeof(struct vmbuspipe_hdr)];
  206. if (icmsghdr->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  207. util_fw_version = UTIL_FW_VERSION;
  208. fcopy_srv_version = WIN8_SRV_VERSION;
  209. vmbus_prep_negotiate_resp(icmsghdr, negop, recv_buffer,
  210. util_fw_version, fcopy_srv_version);
  211. } else {
  212. fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[
  213. sizeof(struct vmbuspipe_hdr) +
  214. sizeof(struct icmsg_hdr)];
  215. /*
  216. * Stash away this global state for completing the
  217. * transaction; note transactions are serialized.
  218. */
  219. fcopy_transaction.active = true;
  220. fcopy_transaction.recv_len = recvlen;
  221. fcopy_transaction.recv_channel = channel;
  222. fcopy_transaction.recv_req_id = requestid;
  223. fcopy_transaction.fcopy_msg = fcopy_msg;
  224. /*
  225. * Send the information to the user-level daemon.
  226. */
  227. schedule_delayed_work(&fcopy_work, 5*HZ);
  228. fcopy_send_data();
  229. return;
  230. }
  231. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  232. vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
  233. VM_PKT_DATA_INBAND, 0);
  234. }
  235. /*
  236. * Create a char device that can support read/write for passing
  237. * the payload.
  238. */
  239. static ssize_t fcopy_read(struct file *file, char __user *buf,
  240. size_t count, loff_t *ppos)
  241. {
  242. void *src;
  243. size_t copy_size;
  244. int operation;
  245. /*
  246. * Wait until there is something to be read.
  247. */
  248. if (down_interruptible(&fcopy_transaction.read_sema))
  249. return -EINTR;
  250. /*
  251. * The channel may be rescinded and in this case, we will wakeup the
  252. * the thread blocked on the semaphore and we will use the opened
  253. * state to correctly handle this case.
  254. */
  255. if (!opened)
  256. return -ENODEV;
  257. operation = fcopy_transaction.fcopy_msg->operation;
  258. if (operation == START_FILE_COPY) {
  259. src = &fcopy_transaction.message;
  260. copy_size = sizeof(struct hv_start_fcopy);
  261. if (count < copy_size)
  262. return 0;
  263. } else {
  264. src = fcopy_transaction.fcopy_msg;
  265. copy_size = sizeof(struct hv_do_fcopy);
  266. if (count < copy_size)
  267. return 0;
  268. }
  269. if (copy_to_user(buf, src, copy_size))
  270. return -EFAULT;
  271. return copy_size;
  272. }
  273. static ssize_t fcopy_write(struct file *file, const char __user *buf,
  274. size_t count, loff_t *ppos)
  275. {
  276. int response = 0;
  277. if (count != sizeof(int))
  278. return -EINVAL;
  279. if (copy_from_user(&response, buf, sizeof(int)))
  280. return -EFAULT;
  281. if (in_hand_shake) {
  282. if (fcopy_handle_handshake(response))
  283. return -EINVAL;
  284. return sizeof(int);
  285. }
  286. /*
  287. * Complete the transaction by forwarding the result
  288. * to the host. But first, cancel the timeout.
  289. */
  290. if (cancel_delayed_work_sync(&fcopy_work))
  291. fcopy_respond_to_host(response);
  292. return sizeof(int);
  293. }
  294. static int fcopy_open(struct inode *inode, struct file *f)
  295. {
  296. /*
  297. * The user level daemon that will open this device is
  298. * really an extension of this driver. We can have only
  299. * active open at a time.
  300. */
  301. if (opened)
  302. return -EBUSY;
  303. /*
  304. * The daemon is alive; setup the state.
  305. */
  306. opened = true;
  307. return 0;
  308. }
  309. /* XXX: there are still some tricky corner cases, e.g.,
  310. * 1) In a SMP guest, when fcopy_release() runs between
  311. * schedule_delayed_work() and fcopy_send_data(), there is
  312. * still a chance an obsolete message will be queued.
  313. *
  314. * 2) When the fcopy daemon is running, if we unload the driver,
  315. * we'll notice a kernel oops when we kill the daemon later.
  316. */
  317. static int fcopy_release(struct inode *inode, struct file *f)
  318. {
  319. /*
  320. * The daemon has exited; reset the state.
  321. */
  322. in_hand_shake = true;
  323. opened = false;
  324. if (cancel_delayed_work_sync(&fcopy_work)) {
  325. /* We haven't up()-ed the semaphore(very rare)? */
  326. if (down_trylock(&fcopy_transaction.read_sema))
  327. ;
  328. fcopy_respond_to_host(HV_E_FAIL);
  329. }
  330. return 0;
  331. }
  332. static const struct file_operations fcopy_fops = {
  333. .read = fcopy_read,
  334. .write = fcopy_write,
  335. .release = fcopy_release,
  336. .open = fcopy_open,
  337. };
  338. static struct miscdevice fcopy_misc = {
  339. .minor = MISC_DYNAMIC_MINOR,
  340. .name = "vmbus/hv_fcopy",
  341. .fops = &fcopy_fops,
  342. };
  343. static int fcopy_dev_init(void)
  344. {
  345. return misc_register(&fcopy_misc);
  346. }
  347. static void fcopy_dev_deinit(void)
  348. {
  349. /*
  350. * The device is going away - perhaps because the
  351. * host has rescinded the channel. Setup state so that
  352. * user level daemon can gracefully exit if it is blocked
  353. * on the read semaphore.
  354. */
  355. opened = false;
  356. /*
  357. * Signal the semaphore as the device is
  358. * going away.
  359. */
  360. up(&fcopy_transaction.read_sema);
  361. misc_deregister(&fcopy_misc);
  362. }
  363. int hv_fcopy_init(struct hv_util_service *srv)
  364. {
  365. recv_buffer = srv->recv_buffer;
  366. /*
  367. * When this driver loads, the user level daemon that
  368. * processes the host requests may not yet be running.
  369. * Defer processing channel callbacks until the daemon
  370. * has registered.
  371. */
  372. fcopy_transaction.active = true;
  373. sema_init(&fcopy_transaction.read_sema, 0);
  374. return fcopy_dev_init();
  375. }
  376. void hv_fcopy_deinit(void)
  377. {
  378. cancel_delayed_work_sync(&fcopy_work);
  379. fcopy_dev_deinit();
  380. }