hv_snapshot.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * An implementation of host initiated guest snapshot.
  3. *
  4. *
  5. * Copyright (C) 2013, Microsoft, Inc.
  6. * Author : K. Y. Srinivasan <kys@microsoft.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/net.h>
  21. #include <linux/nls.h>
  22. #include <linux/connector.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/hyperv.h>
  25. #include "hyperv_vmbus.h"
  26. #include "hv_utils_transport.h"
  27. #define VSS_MAJOR 5
  28. #define VSS_MINOR 0
  29. #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
  30. #define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000))
  31. /*
  32. * Global state maintained for transaction that is being processed. For a class
  33. * of integration services, including the "VSS service", the specified protocol
  34. * is a "request/response" protocol which means that there can only be single
  35. * outstanding transaction from the host at any given point in time. We use
  36. * this to simplify memory management in this driver - we cache and process
  37. * only one message at a time.
  38. *
  39. * While the request/response protocol is guaranteed by the host, we further
  40. * ensure this by serializing packet processing in this driver - we do not
  41. * read additional packets from the VMBUs until the current packet is fully
  42. * handled.
  43. */
  44. static struct {
  45. int state; /* hvutil_device_state */
  46. int recv_len; /* number of bytes received. */
  47. struct vmbus_channel *recv_channel; /* chn we got the request */
  48. u64 recv_req_id; /* request ID. */
  49. struct hv_vss_msg *msg; /* current message */
  50. } vss_transaction;
  51. static void vss_respond_to_host(int error);
  52. /*
  53. * This state maintains the version number registered by the daemon.
  54. */
  55. static int dm_reg_value;
  56. static const char vss_devname[] = "vmbus/hv_vss";
  57. static __u8 *recv_buffer;
  58. static struct hvutil_transport *hvt;
  59. static struct completion release_event;
  60. static void vss_send_op(struct work_struct *dummy);
  61. static void vss_timeout_func(struct work_struct *dummy);
  62. static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
  63. static DECLARE_WORK(vss_send_op_work, vss_send_op);
  64. static void vss_poll_wrapper(void *channel)
  65. {
  66. /* Transaction is finished, reset the state here to avoid races. */
  67. vss_transaction.state = HVUTIL_READY;
  68. hv_vss_onchannelcallback(channel);
  69. }
  70. /*
  71. * Callback when data is received from user mode.
  72. */
  73. static void vss_timeout_func(struct work_struct *dummy)
  74. {
  75. /*
  76. * Timeout waiting for userspace component to reply happened.
  77. */
  78. pr_warn("VSS: timeout waiting for daemon to reply\n");
  79. vss_respond_to_host(HV_E_FAIL);
  80. hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
  81. }
  82. static int vss_handle_handshake(struct hv_vss_msg *vss_msg)
  83. {
  84. u32 our_ver = VSS_OP_REGISTER1;
  85. switch (vss_msg->vss_hdr.operation) {
  86. case VSS_OP_REGISTER:
  87. /* Daemon doesn't expect us to reply */
  88. dm_reg_value = VSS_OP_REGISTER;
  89. break;
  90. case VSS_OP_REGISTER1:
  91. /* Daemon expects us to reply with our own version*/
  92. if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver)))
  93. return -EFAULT;
  94. dm_reg_value = VSS_OP_REGISTER1;
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
  100. pr_debug("VSS: userspace daemon ver. %d registered\n", dm_reg_value);
  101. return 0;
  102. }
  103. static int vss_on_msg(void *msg, int len)
  104. {
  105. struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg;
  106. if (len != sizeof(*vss_msg))
  107. return -EINVAL;
  108. if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER ||
  109. vss_msg->vss_hdr.operation == VSS_OP_REGISTER1) {
  110. /*
  111. * Don't process registration messages if we're in the middle
  112. * of a transaction processing.
  113. */
  114. if (vss_transaction.state > HVUTIL_READY)
  115. return -EINVAL;
  116. return vss_handle_handshake(vss_msg);
  117. } else if (vss_transaction.state == HVUTIL_USERSPACE_REQ) {
  118. vss_transaction.state = HVUTIL_USERSPACE_RECV;
  119. if (cancel_delayed_work_sync(&vss_timeout_work)) {
  120. vss_respond_to_host(vss_msg->error);
  121. /* Transaction is finished, reset the state. */
  122. hv_poll_channel(vss_transaction.recv_channel,
  123. vss_poll_wrapper);
  124. }
  125. } else {
  126. /* This is a spurious call! */
  127. pr_warn("VSS: Transaction not active\n");
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static void vss_send_op(struct work_struct *dummy)
  133. {
  134. int op = vss_transaction.msg->vss_hdr.operation;
  135. int rc;
  136. struct hv_vss_msg *vss_msg;
  137. /* The transaction state is wrong. */
  138. if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
  139. return;
  140. vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
  141. if (!vss_msg)
  142. return;
  143. vss_msg->vss_hdr.operation = op;
  144. vss_transaction.state = HVUTIL_USERSPACE_REQ;
  145. rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg));
  146. if (rc) {
  147. pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
  148. if (cancel_delayed_work_sync(&vss_timeout_work)) {
  149. vss_respond_to_host(HV_E_FAIL);
  150. vss_transaction.state = HVUTIL_READY;
  151. }
  152. }
  153. kfree(vss_msg);
  154. return;
  155. }
  156. /*
  157. * Send a response back to the host.
  158. */
  159. static void
  160. vss_respond_to_host(int error)
  161. {
  162. struct icmsg_hdr *icmsghdrp;
  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.
  169. */
  170. buf_len = vss_transaction.recv_len;
  171. channel = vss_transaction.recv_channel;
  172. req_id = vss_transaction.recv_req_id;
  173. icmsghdrp = (struct icmsg_hdr *)
  174. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  175. if (channel->onchannel_callback == NULL)
  176. /*
  177. * We have raced with util driver being unloaded;
  178. * silently return.
  179. */
  180. return;
  181. icmsghdrp->status = error;
  182. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  183. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  184. VM_PKT_DATA_INBAND, 0);
  185. }
  186. /*
  187. * This callback is invoked when we get a VSS message from the host.
  188. * The host ensures that only one VSS transaction can be active at a time.
  189. */
  190. void hv_vss_onchannelcallback(void *context)
  191. {
  192. struct vmbus_channel *channel = context;
  193. u32 recvlen;
  194. u64 requestid;
  195. struct hv_vss_msg *vss_msg;
  196. struct icmsg_hdr *icmsghdrp;
  197. struct icmsg_negotiate *negop = NULL;
  198. if (vss_transaction.state > HVUTIL_READY)
  199. return;
  200. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
  201. &requestid);
  202. if (recvlen > 0) {
  203. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  204. sizeof(struct vmbuspipe_hdr)];
  205. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  206. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  207. recv_buffer, UTIL_FW_VERSION,
  208. VSS_VERSION);
  209. } else {
  210. vss_msg = (struct hv_vss_msg *)&recv_buffer[
  211. sizeof(struct vmbuspipe_hdr) +
  212. sizeof(struct icmsg_hdr)];
  213. /*
  214. * Stash away this global state for completing the
  215. * transaction; note transactions are serialized.
  216. */
  217. vss_transaction.recv_len = recvlen;
  218. vss_transaction.recv_req_id = requestid;
  219. vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
  220. switch (vss_msg->vss_hdr.operation) {
  221. /*
  222. * Initiate a "freeze/thaw"
  223. * operation in the guest.
  224. * We respond to the host once
  225. * the operation is complete.
  226. *
  227. * We send the message to the
  228. * user space daemon and the
  229. * operation is performed in
  230. * the daemon.
  231. */
  232. case VSS_OP_FREEZE:
  233. case VSS_OP_THAW:
  234. if (vss_transaction.state < HVUTIL_READY) {
  235. /* Userspace is not registered yet */
  236. vss_respond_to_host(HV_E_FAIL);
  237. return;
  238. }
  239. vss_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
  240. schedule_work(&vss_send_op_work);
  241. schedule_delayed_work(&vss_timeout_work,
  242. VSS_USERSPACE_TIMEOUT);
  243. return;
  244. case VSS_OP_HOT_BACKUP:
  245. vss_msg->vss_cf.flags =
  246. VSS_HBU_NO_AUTO_RECOVERY;
  247. vss_respond_to_host(0);
  248. return;
  249. case VSS_OP_GET_DM_INFO:
  250. vss_msg->dm_info.flags = 0;
  251. vss_respond_to_host(0);
  252. return;
  253. default:
  254. vss_respond_to_host(0);
  255. return;
  256. }
  257. }
  258. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  259. | ICMSGHDRFLAG_RESPONSE;
  260. vmbus_sendpacket(channel, recv_buffer,
  261. recvlen, requestid,
  262. VM_PKT_DATA_INBAND, 0);
  263. }
  264. }
  265. static void vss_on_reset(void)
  266. {
  267. if (cancel_delayed_work_sync(&vss_timeout_work))
  268. vss_respond_to_host(HV_E_FAIL);
  269. vss_transaction.state = HVUTIL_DEVICE_INIT;
  270. complete(&release_event);
  271. }
  272. int
  273. hv_vss_init(struct hv_util_service *srv)
  274. {
  275. init_completion(&release_event);
  276. if (vmbus_proto_version < VERSION_WIN8_1) {
  277. pr_warn("Integration service 'Backup (volume snapshot)'"
  278. " not supported on this host version.\n");
  279. return -ENOTSUPP;
  280. }
  281. recv_buffer = srv->recv_buffer;
  282. vss_transaction.recv_channel = srv->channel;
  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. vss_transaction.state = HVUTIL_DEVICE_INIT;
  290. hvt = hvutil_transport_init(vss_devname, CN_VSS_IDX, CN_VSS_VAL,
  291. vss_on_msg, vss_on_reset);
  292. if (!hvt)
  293. return -EFAULT;
  294. return 0;
  295. }
  296. void hv_vss_deinit(void)
  297. {
  298. vss_transaction.state = HVUTIL_DEVICE_DYING;
  299. cancel_delayed_work_sync(&vss_timeout_work);
  300. cancel_work_sync(&vss_send_op_work);
  301. hvutil_transport_destroy(hvt);
  302. wait_for_completion(&release_event);
  303. }