0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/net.h>
0011 #include <linux/nls.h>
0012 #include <linux/connector.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/hyperv.h>
0015 #include <asm/hyperv-tlfs.h>
0016
0017 #include "hyperv_vmbus.h"
0018 #include "hv_utils_transport.h"
0019
0020 #define VSS_MAJOR 5
0021 #define VSS_MINOR 0
0022 #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
0023
0024 #define VSS_VER_COUNT 1
0025 static const int vss_versions[] = {
0026 VSS_VERSION
0027 };
0028
0029 #define FW_VER_COUNT 1
0030 static const int fw_versions[] = {
0031 UTIL_FW_VERSION
0032 };
0033
0034
0035 #define VSS_MAX_PKT_SIZE (HV_HYP_PAGE_SIZE * 2)
0036
0037
0038
0039
0040 #define VSS_FREEZE_TIMEOUT (15 * 60)
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 static struct {
0057 int state;
0058 int recv_len;
0059 struct vmbus_channel *recv_channel;
0060 u64 recv_req_id;
0061 struct hv_vss_msg *msg;
0062 } vss_transaction;
0063
0064
0065 static void vss_respond_to_host(int error);
0066
0067
0068
0069
0070 static int dm_reg_value;
0071
0072 static const char vss_devname[] = "vmbus/hv_vss";
0073 static __u8 *recv_buffer;
0074 static struct hvutil_transport *hvt;
0075
0076 static void vss_timeout_func(struct work_struct *dummy);
0077 static void vss_handle_request(struct work_struct *dummy);
0078
0079 static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
0080 static DECLARE_WORK(vss_handle_request_work, vss_handle_request);
0081
0082 static void vss_poll_wrapper(void *channel)
0083 {
0084
0085 vss_transaction.state = HVUTIL_READY;
0086 tasklet_schedule(&((struct vmbus_channel *)channel)->callback_event);
0087 }
0088
0089
0090
0091
0092
0093 static void vss_timeout_func(struct work_struct *dummy)
0094 {
0095
0096
0097
0098 pr_warn("VSS: timeout waiting for daemon to reply\n");
0099 vss_respond_to_host(HV_E_FAIL);
0100
0101 hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
0102 }
0103
0104 static void vss_register_done(void)
0105 {
0106 hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
0107 pr_debug("VSS: userspace daemon registered\n");
0108 }
0109
0110 static int vss_handle_handshake(struct hv_vss_msg *vss_msg)
0111 {
0112 u32 our_ver = VSS_OP_REGISTER1;
0113
0114 switch (vss_msg->vss_hdr.operation) {
0115 case VSS_OP_REGISTER:
0116
0117 dm_reg_value = VSS_OP_REGISTER;
0118 break;
0119 case VSS_OP_REGISTER1:
0120
0121 if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
0122 vss_register_done))
0123 return -EFAULT;
0124 dm_reg_value = VSS_OP_REGISTER1;
0125 break;
0126 default:
0127 return -EINVAL;
0128 }
0129 pr_info("VSS: userspace daemon ver. %d connected\n", dm_reg_value);
0130 return 0;
0131 }
0132
0133 static int vss_on_msg(void *msg, int len)
0134 {
0135 struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg;
0136
0137 if (len != sizeof(*vss_msg)) {
0138 pr_debug("VSS: Message size does not match length\n");
0139 return -EINVAL;
0140 }
0141
0142 if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER ||
0143 vss_msg->vss_hdr.operation == VSS_OP_REGISTER1) {
0144
0145
0146
0147
0148 if (vss_transaction.state > HVUTIL_READY) {
0149 pr_debug("VSS: Got unexpected registration request\n");
0150 return -EINVAL;
0151 }
0152
0153 return vss_handle_handshake(vss_msg);
0154 } else if (vss_transaction.state == HVUTIL_USERSPACE_REQ) {
0155 vss_transaction.state = HVUTIL_USERSPACE_RECV;
0156
0157 if (vss_msg->vss_hdr.operation == VSS_OP_HOT_BACKUP)
0158 vss_transaction.msg->vss_cf.flags =
0159 VSS_HBU_NO_AUTO_RECOVERY;
0160
0161 if (cancel_delayed_work_sync(&vss_timeout_work)) {
0162 vss_respond_to_host(vss_msg->error);
0163
0164 hv_poll_channel(vss_transaction.recv_channel,
0165 vss_poll_wrapper);
0166 }
0167 } else {
0168
0169 pr_debug("VSS: Transaction not active\n");
0170 return -EINVAL;
0171 }
0172 return 0;
0173 }
0174
0175 static void vss_send_op(void)
0176 {
0177 int op = vss_transaction.msg->vss_hdr.operation;
0178 int rc;
0179 struct hv_vss_msg *vss_msg;
0180
0181
0182 if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED) {
0183 pr_debug("VSS: Unexpected attempt to send to daemon\n");
0184 return;
0185 }
0186
0187 vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
0188 if (!vss_msg)
0189 return;
0190
0191 vss_msg->vss_hdr.operation = op;
0192
0193 vss_transaction.state = HVUTIL_USERSPACE_REQ;
0194
0195 schedule_delayed_work(&vss_timeout_work, op == VSS_OP_FREEZE ?
0196 VSS_FREEZE_TIMEOUT * HZ : HV_UTIL_TIMEOUT * HZ);
0197
0198 rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
0199 if (rc) {
0200 pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
0201 if (cancel_delayed_work_sync(&vss_timeout_work)) {
0202 vss_respond_to_host(HV_E_FAIL);
0203 vss_transaction.state = HVUTIL_READY;
0204 }
0205 }
0206
0207 kfree(vss_msg);
0208 }
0209
0210 static void vss_handle_request(struct work_struct *dummy)
0211 {
0212 switch (vss_transaction.msg->vss_hdr.operation) {
0213
0214
0215
0216
0217
0218
0219
0220 case VSS_OP_THAW:
0221 case VSS_OP_FREEZE:
0222 case VSS_OP_HOT_BACKUP:
0223 if (vss_transaction.state < HVUTIL_READY) {
0224
0225 pr_debug("VSS: Not ready for request.\n");
0226 vss_respond_to_host(HV_E_FAIL);
0227 return;
0228 }
0229
0230 pr_debug("VSS: Received request for op code: %d\n",
0231 vss_transaction.msg->vss_hdr.operation);
0232 vss_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
0233 vss_send_op();
0234 return;
0235 case VSS_OP_GET_DM_INFO:
0236 vss_transaction.msg->dm_info.flags = 0;
0237 break;
0238 default:
0239 break;
0240 }
0241
0242 vss_respond_to_host(0);
0243 hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
0244 }
0245
0246
0247
0248
0249
0250 static void
0251 vss_respond_to_host(int error)
0252 {
0253 struct icmsg_hdr *icmsghdrp;
0254 u32 buf_len;
0255 struct vmbus_channel *channel;
0256 u64 req_id;
0257
0258
0259
0260
0261
0262
0263 buf_len = vss_transaction.recv_len;
0264 channel = vss_transaction.recv_channel;
0265 req_id = vss_transaction.recv_req_id;
0266
0267 icmsghdrp = (struct icmsg_hdr *)
0268 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
0269
0270 if (channel->onchannel_callback == NULL)
0271
0272
0273
0274
0275 return;
0276
0277 icmsghdrp->status = error;
0278
0279 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
0280
0281 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
0282 VM_PKT_DATA_INBAND, 0);
0283
0284 }
0285
0286
0287
0288
0289
0290
0291 void hv_vss_onchannelcallback(void *context)
0292 {
0293 struct vmbus_channel *channel = context;
0294 u32 recvlen;
0295 u64 requestid;
0296 struct hv_vss_msg *vss_msg;
0297 int vss_srv_version;
0298
0299 struct icmsg_hdr *icmsghdrp;
0300
0301 if (vss_transaction.state > HVUTIL_READY)
0302 return;
0303
0304 if (vmbus_recvpacket(channel, recv_buffer, VSS_MAX_PKT_SIZE, &recvlen, &requestid)) {
0305 pr_err_ratelimited("VSS request received. Could not read into recv buf\n");
0306 return;
0307 }
0308
0309 if (!recvlen)
0310 return;
0311
0312
0313 if (recvlen < ICMSG_HDR) {
0314 pr_err_ratelimited("VSS request received. Packet length too small: %d\n",
0315 recvlen);
0316 return;
0317 }
0318
0319 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[sizeof(struct vmbuspipe_hdr)];
0320
0321 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
0322 if (vmbus_prep_negotiate_resp(icmsghdrp,
0323 recv_buffer, recvlen,
0324 fw_versions, FW_VER_COUNT,
0325 vss_versions, VSS_VER_COUNT,
0326 NULL, &vss_srv_version)) {
0327
0328 pr_info("VSS IC version %d.%d\n",
0329 vss_srv_version >> 16,
0330 vss_srv_version & 0xFFFF);
0331 }
0332 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_VSS) {
0333
0334 if (recvlen < ICMSG_HDR + sizeof(struct hv_vss_msg)) {
0335 pr_err_ratelimited("Invalid VSS msg. Packet length too small: %u\n",
0336 recvlen);
0337 return;
0338 }
0339 vss_msg = (struct hv_vss_msg *)&recv_buffer[ICMSG_HDR];
0340
0341
0342
0343
0344
0345
0346 vss_transaction.recv_len = recvlen;
0347 vss_transaction.recv_req_id = requestid;
0348 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
0349
0350 schedule_work(&vss_handle_request_work);
0351 return;
0352 } else {
0353 pr_err_ratelimited("VSS request received. Invalid msg type: %d\n",
0354 icmsghdrp->icmsgtype);
0355 return;
0356 }
0357
0358 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
0359 ICMSGHDRFLAG_RESPONSE;
0360 vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
0361 VM_PKT_DATA_INBAND, 0);
0362 }
0363
0364 static void vss_on_reset(void)
0365 {
0366 if (cancel_delayed_work_sync(&vss_timeout_work))
0367 vss_respond_to_host(HV_E_FAIL);
0368 vss_transaction.state = HVUTIL_DEVICE_INIT;
0369 }
0370
0371 int
0372 hv_vss_init(struct hv_util_service *srv)
0373 {
0374 if (vmbus_proto_version < VERSION_WIN8_1) {
0375 pr_warn("Integration service 'Backup (volume snapshot)'"
0376 " not supported on this host version.\n");
0377 return -ENOTSUPP;
0378 }
0379 recv_buffer = srv->recv_buffer;
0380 vss_transaction.recv_channel = srv->channel;
0381 vss_transaction.recv_channel->max_pkt_size = VSS_MAX_PKT_SIZE;
0382
0383
0384
0385
0386
0387
0388
0389 vss_transaction.state = HVUTIL_DEVICE_INIT;
0390
0391 hvt = hvutil_transport_init(vss_devname, CN_VSS_IDX, CN_VSS_VAL,
0392 vss_on_msg, vss_on_reset);
0393 if (!hvt) {
0394 pr_warn("VSS: Failed to initialize transport\n");
0395 return -EFAULT;
0396 }
0397
0398 return 0;
0399 }
0400
0401 static void hv_vss_cancel_work(void)
0402 {
0403 cancel_delayed_work_sync(&vss_timeout_work);
0404 cancel_work_sync(&vss_handle_request_work);
0405 }
0406
0407 int hv_vss_pre_suspend(void)
0408 {
0409 struct vmbus_channel *channel = vss_transaction.recv_channel;
0410 struct hv_vss_msg *vss_msg;
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420 vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
0421 if (!vss_msg)
0422 return -ENOMEM;
0423
0424 tasklet_disable(&channel->callback_event);
0425
0426 vss_msg->vss_hdr.operation = VSS_OP_THAW;
0427
0428
0429 hv_vss_cancel_work();
0430
0431
0432 hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
0433
0434 kfree(vss_msg);
0435
0436 vss_transaction.state = HVUTIL_READY;
0437
0438
0439 return 0;
0440 }
0441
0442 int hv_vss_pre_resume(void)
0443 {
0444 struct vmbus_channel *channel = vss_transaction.recv_channel;
0445
0446 tasklet_enable(&channel->callback_event);
0447
0448 return 0;
0449 }
0450
0451 void hv_vss_deinit(void)
0452 {
0453 vss_transaction.state = HVUTIL_DEVICE_DYING;
0454
0455 hv_vss_cancel_work();
0456
0457 hvutil_transport_destroy(hvt);
0458 }