2 * An implementation of host initiated guest snapshot.
5 * Copyright (C) 2013, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/net.h>
22 #include <linux/nls.h>
23 #include <linux/connector.h>
24 #include <linux/workqueue.h>
25 #include <linux/hyperv.h>
29 #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
34 * Global state maintained for transaction that is being processed.
35 * Note that only one transaction can be active at any point in time.
37 * This state is set when we receive a request from the host; we
38 * cleanup this state when the transaction is completed - when we respond
39 * to the host with the key value.
43 bool active; /* transaction status - active or not */
44 int recv_len; /* number of bytes received. */
45 struct vmbus_channel *recv_channel; /* chn we got the request */
46 u64 recv_req_id; /* request ID. */
47 struct hv_vss_msg *msg; /* current message */
51 static void vss_respond_to_host(int error);
53 static struct cb_id vss_id = { CN_VSS_IDX, CN_VSS_VAL };
54 static const char vss_name[] = "vss_kernel_module";
55 static __u8 *recv_buffer;
57 static void vss_send_op(struct work_struct *dummy);
58 static DECLARE_WORK(vss_send_op_work, vss_send_op);
61 * Callback when data is received from user mode.
65 vss_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
67 struct hv_vss_msg *vss_msg;
69 vss_msg = (struct hv_vss_msg *)msg->data;
71 if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER) {
72 pr_info("VSS daemon registered\n");
73 vss_transaction.active = false;
74 if (vss_transaction.recv_channel != NULL)
75 hv_vss_onchannelcallback(vss_transaction.recv_channel);
79 vss_respond_to_host(vss_msg->error);
83 static void vss_send_op(struct work_struct *dummy)
85 int op = vss_transaction.msg->vss_hdr.operation;
87 struct hv_vss_msg *vss_msg;
89 msg = kzalloc(sizeof(*msg) + sizeof(*vss_msg), GFP_ATOMIC);
93 vss_msg = (struct hv_vss_msg *)msg->data;
95 msg->id.idx = CN_VSS_IDX;
96 msg->id.val = CN_VSS_VAL;
98 vss_msg->vss_hdr.operation = op;
99 msg->len = sizeof(struct hv_vss_msg);
101 cn_netlink_send(msg, 0, GFP_ATOMIC);
108 * Send a response back to the host.
112 vss_respond_to_host(int error)
114 struct icmsg_hdr *icmsghdrp;
116 struct vmbus_channel *channel;
120 * If a transaction is not active; log and return.
123 if (!vss_transaction.active) {
125 * This is a spurious call!
127 pr_warn("VSS: Transaction not active\n");
131 * Copy the global state for completing the transaction. Note that
132 * only one transaction can be active at a time.
135 buf_len = vss_transaction.recv_len;
136 channel = vss_transaction.recv_channel;
137 req_id = vss_transaction.recv_req_id;
138 vss_transaction.active = false;
140 icmsghdrp = (struct icmsg_hdr *)
141 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
143 if (channel->onchannel_callback == NULL)
145 * We have raced with util driver being unloaded;
150 icmsghdrp->status = error;
152 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
154 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
155 VM_PKT_DATA_INBAND, 0);
160 * This callback is invoked when we get a VSS message from the host.
161 * The host ensures that only one VSS transaction can be active at a time.
164 void hv_vss_onchannelcallback(void *context)
166 struct vmbus_channel *channel = context;
169 struct hv_vss_msg *vss_msg;
172 struct icmsg_hdr *icmsghdrp;
173 struct icmsg_negotiate *negop = NULL;
175 if (vss_transaction.active) {
177 * We will defer processing this callback once
178 * the current transaction is complete.
180 vss_transaction.recv_channel = channel;
184 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
188 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
189 sizeof(struct vmbuspipe_hdr)];
191 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
192 vmbus_prep_negotiate_resp(icmsghdrp, negop,
193 recv_buffer, UTIL_FW_VERSION,
196 vss_msg = (struct hv_vss_msg *)&recv_buffer[
197 sizeof(struct vmbuspipe_hdr) +
198 sizeof(struct icmsg_hdr)];
201 * Stash away this global state for completing the
202 * transaction; note transactions are serialized.
205 vss_transaction.recv_len = recvlen;
206 vss_transaction.recv_channel = channel;
207 vss_transaction.recv_req_id = requestid;
208 vss_transaction.active = true;
209 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
211 switch (vss_msg->vss_hdr.operation) {
213 * Initiate a "freeze/thaw"
214 * operation in the guest.
215 * We respond to the host once
216 * the operation is complete.
218 * We send the message to the
219 * user space daemon and the
220 * operation is performed in
225 schedule_work(&vss_send_op_work);
228 case VSS_OP_HOT_BACKUP:
229 vss_msg->vss_cf.flags =
230 VSS_HBU_NO_AUTO_RECOVERY;
231 vss_respond_to_host(0);
234 case VSS_OP_GET_DM_INFO:
235 vss_msg->dm_info.flags = 0;
236 vss_respond_to_host(0);
240 vss_respond_to_host(0);
247 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
248 | ICMSGHDRFLAG_RESPONSE;
250 vmbus_sendpacket(channel, recv_buffer,
252 VM_PKT_DATA_INBAND, 0);
258 hv_vss_init(struct hv_util_service *srv)
262 err = cn_add_callback(&vss_id, vss_name, vss_cn_callback);
265 recv_buffer = srv->recv_buffer;
268 * When this driver loads, the user level daemon that
269 * processes the host requests may not yet be running.
270 * Defer processing channel callbacks until the daemon
273 vss_transaction.active = true;
277 void hv_vss_deinit(void)
279 cn_del_callback(&vss_id);
280 cancel_work_sync(&vss_send_op_work);