Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * An implementation of host initiated guest snapshot.
0004  *
0005  * Copyright (C) 2013, Microsoft, Inc.
0006  * Author : K. Y. Srinivasan <kys@microsoft.com>
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 /* See comment with struct hv_vss_msg regarding the max VMbus packet size */
0035 #define VSS_MAX_PKT_SIZE (HV_HYP_PAGE_SIZE * 2)
0036 
0037 /*
0038  * Timeout values are based on expecations from host
0039  */
0040 #define VSS_FREEZE_TIMEOUT (15 * 60)
0041 
0042 /*
0043  * Global state maintained for transaction that is being processed. For a class
0044  * of integration services, including the "VSS service", the specified protocol
0045  * is a "request/response" protocol which means that there can only be single
0046  * outstanding transaction from the host at any given point in time. We use
0047  * this to simplify memory management in this driver - we cache and process
0048  * only one message at a time.
0049  *
0050  * While the request/response protocol is guaranteed by the host, we further
0051  * ensure this by serializing packet processing in this driver - we do not
0052  * read additional packets from the VMBUs until the current packet is fully
0053  * handled.
0054  */
0055 
0056 static struct {
0057     int state;   /* hvutil_device_state */
0058     int recv_len; /* number of bytes received. */
0059     struct vmbus_channel *recv_channel; /* chn we got the request */
0060     u64 recv_req_id; /* request ID. */
0061     struct hv_vss_msg  *msg; /* current message */
0062 } vss_transaction;
0063 
0064 
0065 static void vss_respond_to_host(int error);
0066 
0067 /*
0068  * This state maintains the version number registered by the daemon.
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     /* Transaction is finished, reset the state here to avoid races. */
0085     vss_transaction.state = HVUTIL_READY;
0086     tasklet_schedule(&((struct vmbus_channel *)channel)->callback_event);
0087 }
0088 
0089 /*
0090  * Callback when data is received from user mode.
0091  */
0092 
0093 static void vss_timeout_func(struct work_struct *dummy)
0094 {
0095     /*
0096      * Timeout waiting for userspace component to reply happened.
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         /* Daemon doesn't expect us to reply */
0117         dm_reg_value = VSS_OP_REGISTER;
0118         break;
0119     case VSS_OP_REGISTER1:
0120         /* Daemon expects us to reply with our own version */
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          * Don't process registration messages if we're in the middle
0146          * of a transaction processing.
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             /* Transaction is finished, reset the state. */
0164             hv_poll_channel(vss_transaction.recv_channel,
0165                     vss_poll_wrapper);
0166         }
0167     } else {
0168         /* This is a spurious call! */
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     /* The transaction state is wrong. */
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      * Initiate a "freeze/thaw" operation in the guest.
0215      * We respond to the host once the operation is complete.
0216      *
0217      * We send the message to the user space daemon and the operation is
0218      * performed in the daemon.
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             /* Userspace is not registered yet */
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  * Send a response back to the host.
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      * Copy the global state for completing the transaction. Note that
0260      * only one transaction can be active at a time.
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          * We have raced with util driver being unloaded;
0273          * silently return.
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  * This callback is invoked when we get a VSS message from the host.
0288  * The host ensures that only one VSS transaction can be active at a time.
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     /* Ensure recvlen is big enough to read header data */
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         /* Ensure recvlen is big enough to contain hv_vss_msg */
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          * Stash away this global state for completing the
0343          * transaction; note transactions are serialized.
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      * When this driver loads, the user level daemon that
0385      * processes the host requests may not yet be running.
0386      * Defer processing channel callbacks until the daemon
0387      * has registered.
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      * Fake a THAW message for the user space daemon in case the daemon
0414      * has frozen the file systems. It doesn't matter if there is already
0415      * a message pending to be delivered to the user space since we force
0416      * vss_transaction.state to be HVUTIL_READY, so the user space daemon's
0417      * write() will fail with EINVAL (see vss_on_msg()), and the daemon
0418      * will reset the device by closing and re-opening it.
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     /* Cancel any possible pending work. */
0429     hv_vss_cancel_work();
0430 
0431     /* We don't care about the return value. */
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     /* tasklet_enable() will be called in hv_vss_pre_resume(). */
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 }