hv_snapshot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An implementation of host initiated guest snapshot.
  4. *
  5. * Copyright (C) 2013, Microsoft, Inc.
  6. * Author : K. Y. Srinivasan <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/net.h>
  10. #include <linux/nls.h>
  11. #include <linux/connector.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/hyperv.h>
  14. #include <asm/hyperv-tlfs.h>
  15. #include "hyperv_vmbus.h"
  16. #include "hv_utils_transport.h"
  17. #define VSS_MAJOR 5
  18. #define VSS_MINOR 0
  19. #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
  20. #define VSS_VER_COUNT 1
  21. static const int vss_versions[] = {
  22. VSS_VERSION
  23. };
  24. #define FW_VER_COUNT 1
  25. static const int fw_versions[] = {
  26. UTIL_FW_VERSION
  27. };
  28. /* See comment with struct hv_vss_msg regarding the max VMbus packet size */
  29. #define VSS_MAX_PKT_SIZE (HV_HYP_PAGE_SIZE * 2)
  30. /*
  31. * Timeout values are based on expecations from host
  32. */
  33. #define VSS_FREEZE_TIMEOUT (15 * 60)
  34. /*
  35. * Global state maintained for transaction that is being processed. For a class
  36. * of integration services, including the "VSS service", the specified protocol
  37. * is a "request/response" protocol which means that there can only be single
  38. * outstanding transaction from the host at any given point in time. We use
  39. * this to simplify memory management in this driver - we cache and process
  40. * only one message at a time.
  41. *
  42. * While the request/response protocol is guaranteed by the host, we further
  43. * ensure this by serializing packet processing in this driver - we do not
  44. * read additional packets from the VMBUs until the current packet is fully
  45. * handled.
  46. */
  47. static struct {
  48. int state; /* hvutil_device_state */
  49. int recv_len; /* number of bytes received. */
  50. struct vmbus_channel *recv_channel; /* chn we got the request */
  51. u64 recv_req_id; /* request ID. */
  52. struct hv_vss_msg *msg; /* current message */
  53. } vss_transaction;
  54. static void vss_respond_to_host(int error);
  55. /*
  56. * This state maintains the version number registered by the daemon.
  57. */
  58. static int dm_reg_value;
  59. static const char vss_devname[] = "vmbus/hv_vss";
  60. static __u8 *recv_buffer;
  61. static struct hvutil_transport *hvt;
  62. static void vss_timeout_func(struct work_struct *dummy);
  63. static void vss_handle_request(struct work_struct *dummy);
  64. static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
  65. static DECLARE_WORK(vss_handle_request_work, vss_handle_request);
  66. static void vss_poll_wrapper(void *channel)
  67. {
  68. /* Transaction is finished, reset the state here to avoid races. */
  69. vss_transaction.state = HVUTIL_READY;
  70. tasklet_schedule(&((struct vmbus_channel *)channel)->callback_event);
  71. }
  72. /*
  73. * Callback when data is received from user mode.
  74. */
  75. static void vss_timeout_func(struct work_struct *dummy)
  76. {
  77. /*
  78. * Timeout waiting for userspace component to reply happened.
  79. */
  80. pr_warn("VSS: timeout waiting for daemon to reply\n");
  81. vss_respond_to_host(HV_E_FAIL);
  82. hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
  83. }
  84. static void vss_register_done(void)
  85. {
  86. hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
  87. pr_debug("VSS: userspace daemon registered\n");
  88. }
  89. static int vss_handle_handshake(struct hv_vss_msg *vss_msg)
  90. {
  91. u32 our_ver = VSS_OP_REGISTER1;
  92. switch (vss_msg->vss_hdr.operation) {
  93. case VSS_OP_REGISTER:
  94. /* Daemon doesn't expect us to reply */
  95. dm_reg_value = VSS_OP_REGISTER;
  96. break;
  97. case VSS_OP_REGISTER1:
  98. /* Daemon expects us to reply with our own version */
  99. if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
  100. vss_register_done))
  101. return -EFAULT;
  102. dm_reg_value = VSS_OP_REGISTER1;
  103. break;
  104. default:
  105. return -EINVAL;
  106. }
  107. pr_info("VSS: userspace daemon ver. %d connected\n", dm_reg_value);
  108. return 0;
  109. }
  110. static int vss_on_msg(void *msg, int len)
  111. {
  112. struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg;
  113. if (len != sizeof(*vss_msg)) {
  114. pr_debug("VSS: Message size does not match length\n");
  115. return -EINVAL;
  116. }
  117. if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER ||
  118. vss_msg->vss_hdr.operation == VSS_OP_REGISTER1) {
  119. /*
  120. * Don't process registration messages if we're in the middle
  121. * of a transaction processing.
  122. */
  123. if (vss_transaction.state > HVUTIL_READY) {
  124. pr_debug("VSS: Got unexpected registration request\n");
  125. return -EINVAL;
  126. }
  127. return vss_handle_handshake(vss_msg);
  128. } else if (vss_transaction.state == HVUTIL_USERSPACE_REQ) {
  129. vss_transaction.state = HVUTIL_USERSPACE_RECV;
  130. if (vss_msg->vss_hdr.operation == VSS_OP_HOT_BACKUP)
  131. vss_transaction.msg->vss_cf.flags =
  132. VSS_HBU_NO_AUTO_RECOVERY;
  133. if (cancel_delayed_work_sync(&vss_timeout_work)) {
  134. vss_respond_to_host(vss_msg->error);
  135. /* Transaction is finished, reset the state. */
  136. hv_poll_channel(vss_transaction.recv_channel,
  137. vss_poll_wrapper);
  138. }
  139. } else {
  140. /* This is a spurious call! */
  141. pr_debug("VSS: Transaction not active\n");
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static void vss_send_op(void)
  147. {
  148. int op = vss_transaction.msg->vss_hdr.operation;
  149. int rc;
  150. struct hv_vss_msg *vss_msg;
  151. /* The transaction state is wrong. */
  152. if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED) {
  153. pr_debug("VSS: Unexpected attempt to send to daemon\n");
  154. return;
  155. }
  156. vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
  157. if (!vss_msg)
  158. return;
  159. vss_msg->vss_hdr.operation = op;
  160. vss_transaction.state = HVUTIL_USERSPACE_REQ;
  161. schedule_delayed_work(&vss_timeout_work, op == VSS_OP_FREEZE ?
  162. VSS_FREEZE_TIMEOUT * HZ : HV_UTIL_TIMEOUT * HZ);
  163. rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
  164. if (rc) {
  165. pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
  166. if (cancel_delayed_work_sync(&vss_timeout_work)) {
  167. vss_respond_to_host(HV_E_FAIL);
  168. vss_transaction.state = HVUTIL_READY;
  169. }
  170. }
  171. kfree(vss_msg);
  172. }
  173. static void vss_handle_request(struct work_struct *dummy)
  174. {
  175. switch (vss_transaction.msg->vss_hdr.operation) {
  176. /*
  177. * Initiate a "freeze/thaw" operation in the guest.
  178. * We respond to the host once the operation is complete.
  179. *
  180. * We send the message to the user space daemon and the operation is
  181. * performed in the daemon.
  182. */
  183. case VSS_OP_THAW:
  184. case VSS_OP_FREEZE:
  185. case VSS_OP_HOT_BACKUP:
  186. if (vss_transaction.state < HVUTIL_READY) {
  187. /* Userspace is not registered yet */
  188. pr_debug("VSS: Not ready for request.\n");
  189. vss_respond_to_host(HV_E_FAIL);
  190. return;
  191. }
  192. pr_debug("VSS: Received request for op code: %d\n",
  193. vss_transaction.msg->vss_hdr.operation);
  194. vss_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
  195. vss_send_op();
  196. return;
  197. case VSS_OP_GET_DM_INFO:
  198. vss_transaction.msg->dm_info.flags = 0;
  199. break;
  200. default:
  201. break;
  202. }
  203. vss_respond_to_host(0);
  204. hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
  205. }
  206. /*
  207. * Send a response back to the host.
  208. */
  209. static void
  210. vss_respond_to_host(int error)
  211. {
  212. struct icmsg_hdr *icmsghdrp;
  213. u32 buf_len;
  214. struct vmbus_channel *channel;
  215. u64 req_id;
  216. /*
  217. * Copy the global state for completing the transaction. Note that
  218. * only one transaction can be active at a time.
  219. */
  220. buf_len = vss_transaction.recv_len;
  221. channel = vss_transaction.recv_channel;
  222. req_id = vss_transaction.recv_req_id;
  223. icmsghdrp = (struct icmsg_hdr *)
  224. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  225. if (channel->onchannel_callback == NULL)
  226. /*
  227. * We have raced with util driver being unloaded;
  228. * silently return.
  229. */
  230. return;
  231. icmsghdrp->status = error;
  232. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  233. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  234. VM_PKT_DATA_INBAND, 0);
  235. }
  236. /*
  237. * This callback is invoked when we get a VSS message from the host.
  238. * The host ensures that only one VSS transaction can be active at a time.
  239. */
  240. void hv_vss_onchannelcallback(void *context)
  241. {
  242. struct vmbus_channel *channel = context;
  243. u32 recvlen;
  244. u64 requestid;
  245. struct hv_vss_msg *vss_msg;
  246. int vss_srv_version;
  247. struct icmsg_hdr *icmsghdrp;
  248. if (vss_transaction.state > HVUTIL_READY)
  249. return;
  250. if (vmbus_recvpacket(channel, recv_buffer, VSS_MAX_PKT_SIZE, &recvlen, &requestid)) {
  251. pr_err_ratelimited("VSS request received. Could not read into recv buf\n");
  252. return;
  253. }
  254. if (!recvlen)
  255. return;
  256. /* Ensure recvlen is big enough to read header data */
  257. if (recvlen < ICMSG_HDR) {
  258. pr_err_ratelimited("VSS request received. Packet length too small: %d\n",
  259. recvlen);
  260. return;
  261. }
  262. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[sizeof(struct vmbuspipe_hdr)];
  263. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  264. if (vmbus_prep_negotiate_resp(icmsghdrp,
  265. recv_buffer, recvlen,
  266. fw_versions, FW_VER_COUNT,
  267. vss_versions, VSS_VER_COUNT,
  268. NULL, &vss_srv_version)) {
  269. pr_info("VSS IC version %d.%d\n",
  270. vss_srv_version >> 16,
  271. vss_srv_version & 0xFFFF);
  272. }
  273. } else if (icmsghdrp->icmsgtype == ICMSGTYPE_VSS) {
  274. /* Ensure recvlen is big enough to contain hv_vss_msg */
  275. if (recvlen < ICMSG_HDR + sizeof(struct hv_vss_msg)) {
  276. pr_err_ratelimited("Invalid VSS msg. Packet length too small: %u\n",
  277. recvlen);
  278. return;
  279. }
  280. vss_msg = (struct hv_vss_msg *)&recv_buffer[ICMSG_HDR];
  281. /*
  282. * Stash away this global state for completing the
  283. * transaction; note transactions are serialized.
  284. */
  285. vss_transaction.recv_len = recvlen;
  286. vss_transaction.recv_req_id = requestid;
  287. vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
  288. schedule_work(&vss_handle_request_work);
  289. return;
  290. } else {
  291. pr_err_ratelimited("VSS request received. Invalid msg type: %d\n",
  292. icmsghdrp->icmsgtype);
  293. return;
  294. }
  295. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
  296. ICMSGHDRFLAG_RESPONSE;
  297. vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
  298. VM_PKT_DATA_INBAND, 0);
  299. }
  300. static void vss_on_reset(void)
  301. {
  302. if (cancel_delayed_work_sync(&vss_timeout_work))
  303. vss_respond_to_host(HV_E_FAIL);
  304. vss_transaction.state = HVUTIL_DEVICE_INIT;
  305. }
  306. int
  307. hv_vss_init(struct hv_util_service *srv)
  308. {
  309. if (vmbus_proto_version < VERSION_WIN8_1) {
  310. pr_warn("Integration service 'Backup (volume snapshot)'"
  311. " not supported on this host version.\n");
  312. return -ENOTSUPP;
  313. }
  314. recv_buffer = srv->recv_buffer;
  315. vss_transaction.recv_channel = srv->channel;
  316. vss_transaction.recv_channel->max_pkt_size = VSS_MAX_PKT_SIZE;
  317. /*
  318. * When this driver loads, the user level daemon that
  319. * processes the host requests may not yet be running.
  320. * Defer processing channel callbacks until the daemon
  321. * has registered.
  322. */
  323. vss_transaction.state = HVUTIL_DEVICE_INIT;
  324. hvt = hvutil_transport_init(vss_devname, CN_VSS_IDX, CN_VSS_VAL,
  325. vss_on_msg, vss_on_reset);
  326. if (!hvt) {
  327. pr_warn("VSS: Failed to initialize transport\n");
  328. return -EFAULT;
  329. }
  330. return 0;
  331. }
  332. static void hv_vss_cancel_work(void)
  333. {
  334. cancel_delayed_work_sync(&vss_timeout_work);
  335. cancel_work_sync(&vss_handle_request_work);
  336. }
  337. int hv_vss_pre_suspend(void)
  338. {
  339. struct vmbus_channel *channel = vss_transaction.recv_channel;
  340. struct hv_vss_msg *vss_msg;
  341. /*
  342. * Fake a THAW message for the user space daemon in case the daemon
  343. * has frozen the file systems. It doesn't matter if there is already
  344. * a message pending to be delivered to the user space since we force
  345. * vss_transaction.state to be HVUTIL_READY, so the user space daemon's
  346. * write() will fail with EINVAL (see vss_on_msg()), and the daemon
  347. * will reset the device by closing and re-opening it.
  348. */
  349. vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
  350. if (!vss_msg)
  351. return -ENOMEM;
  352. tasklet_disable(&channel->callback_event);
  353. vss_msg->vss_hdr.operation = VSS_OP_THAW;
  354. /* Cancel any possible pending work. */
  355. hv_vss_cancel_work();
  356. /* We don't care about the return value. */
  357. hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
  358. kfree(vss_msg);
  359. vss_transaction.state = HVUTIL_READY;
  360. /* tasklet_enable() will be called in hv_vss_pre_resume(). */
  361. return 0;
  362. }
  363. int hv_vss_pre_resume(void)
  364. {
  365. struct vmbus_channel *channel = vss_transaction.recv_channel;
  366. tasklet_enable(&channel->callback_event);
  367. return 0;
  368. }
  369. void hv_vss_deinit(void)
  370. {
  371. vss_transaction.state = HVUTIL_DEVICE_DYING;
  372. hv_vss_cancel_work();
  373. hvutil_transport_destroy(hvt);
  374. }