PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/hv/hv_snapshot.c

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