hv_snapshot.c 10 KB

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