Linux 4.1.16
[linux/fpc-iii.git] / drivers / hv / hv_snapshot.c
blob9d5e0d1efdb51bd235f87bcdd914d2c5656d6d45
1 /*
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
16 * details.
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>
27 #define VSS_MAJOR 5
28 #define VSS_MINOR 0
29 #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
31 #define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000))
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.
42 static struct {
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 */
48 } vss_transaction;
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 void vss_timeout_func(struct work_struct *dummy);
60 static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
61 static DECLARE_WORK(vss_send_op_work, vss_send_op);
64 * Callback when data is received from user mode.
67 static void vss_timeout_func(struct work_struct *dummy)
70 * Timeout waiting for userspace component to reply happened.
72 pr_warn("VSS: timeout waiting for daemon to reply\n");
73 vss_respond_to_host(HV_E_FAIL);
76 static void
77 vss_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
79 struct hv_vss_msg *vss_msg;
81 vss_msg = (struct hv_vss_msg *)msg->data;
83 if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER) {
84 pr_info("VSS daemon registered\n");
85 vss_transaction.active = false;
86 if (vss_transaction.recv_channel != NULL)
87 hv_vss_onchannelcallback(vss_transaction.recv_channel);
88 return;
91 if (cancel_delayed_work_sync(&vss_timeout_work))
92 vss_respond_to_host(vss_msg->error);
96 static void vss_send_op(struct work_struct *dummy)
98 int op = vss_transaction.msg->vss_hdr.operation;
99 int rc;
100 struct cn_msg *msg;
101 struct hv_vss_msg *vss_msg;
103 msg = kzalloc(sizeof(*msg) + sizeof(*vss_msg), GFP_ATOMIC);
104 if (!msg)
105 return;
107 vss_msg = (struct hv_vss_msg *)msg->data;
109 msg->id.idx = CN_VSS_IDX;
110 msg->id.val = CN_VSS_VAL;
112 vss_msg->vss_hdr.operation = op;
113 msg->len = sizeof(struct hv_vss_msg);
115 rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
116 if (rc) {
117 pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
118 if (cancel_delayed_work_sync(&vss_timeout_work))
119 vss_respond_to_host(HV_E_FAIL);
121 kfree(msg);
123 return;
127 * Send a response back to the host.
130 static void
131 vss_respond_to_host(int error)
133 struct icmsg_hdr *icmsghdrp;
134 u32 buf_len;
135 struct vmbus_channel *channel;
136 u64 req_id;
139 * If a transaction is not active; log and return.
142 if (!vss_transaction.active) {
144 * This is a spurious call!
146 pr_warn("VSS: Transaction not active\n");
147 return;
150 * Copy the global state for completing the transaction. Note that
151 * only one transaction can be active at a time.
154 buf_len = vss_transaction.recv_len;
155 channel = vss_transaction.recv_channel;
156 req_id = vss_transaction.recv_req_id;
157 vss_transaction.active = false;
159 icmsghdrp = (struct icmsg_hdr *)
160 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
162 if (channel->onchannel_callback == NULL)
164 * We have raced with util driver being unloaded;
165 * silently return.
167 return;
169 icmsghdrp->status = error;
171 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
173 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
174 VM_PKT_DATA_INBAND, 0);
179 * This callback is invoked when we get a VSS message from the host.
180 * The host ensures that only one VSS transaction can be active at a time.
183 void hv_vss_onchannelcallback(void *context)
185 struct vmbus_channel *channel = context;
186 u32 recvlen;
187 u64 requestid;
188 struct hv_vss_msg *vss_msg;
191 struct icmsg_hdr *icmsghdrp;
192 struct icmsg_negotiate *negop = NULL;
194 if (vss_transaction.active) {
196 * We will defer processing this callback once
197 * the current transaction is complete.
199 vss_transaction.recv_channel = channel;
200 return;
203 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
204 &requestid);
206 if (recvlen > 0) {
207 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
208 sizeof(struct vmbuspipe_hdr)];
210 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
211 vmbus_prep_negotiate_resp(icmsghdrp, negop,
212 recv_buffer, UTIL_FW_VERSION,
213 VSS_VERSION);
214 } else {
215 vss_msg = (struct hv_vss_msg *)&recv_buffer[
216 sizeof(struct vmbuspipe_hdr) +
217 sizeof(struct icmsg_hdr)];
220 * Stash away this global state for completing the
221 * transaction; note transactions are serialized.
224 vss_transaction.recv_len = recvlen;
225 vss_transaction.recv_channel = channel;
226 vss_transaction.recv_req_id = requestid;
227 vss_transaction.active = true;
228 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
230 switch (vss_msg->vss_hdr.operation) {
232 * Initiate a "freeze/thaw"
233 * operation in the guest.
234 * We respond to the host once
235 * the operation is complete.
237 * We send the message to the
238 * user space daemon and the
239 * operation is performed in
240 * the daemon.
242 case VSS_OP_FREEZE:
243 case VSS_OP_THAW:
244 schedule_work(&vss_send_op_work);
245 schedule_delayed_work(&vss_timeout_work,
246 VSS_USERSPACE_TIMEOUT);
247 return;
249 case VSS_OP_HOT_BACKUP:
250 vss_msg->vss_cf.flags =
251 VSS_HBU_NO_AUTO_RECOVERY;
252 vss_respond_to_host(0);
253 return;
255 case VSS_OP_GET_DM_INFO:
256 vss_msg->dm_info.flags = 0;
257 vss_respond_to_host(0);
258 return;
260 default:
261 vss_respond_to_host(0);
262 return;
268 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
269 | ICMSGHDRFLAG_RESPONSE;
271 vmbus_sendpacket(channel, recv_buffer,
272 recvlen, requestid,
273 VM_PKT_DATA_INBAND, 0);
279 hv_vss_init(struct hv_util_service *srv)
281 int err;
283 err = cn_add_callback(&vss_id, vss_name, vss_cn_callback);
284 if (err)
285 return err;
286 recv_buffer = srv->recv_buffer;
289 * When this driver loads, the user level daemon that
290 * processes the host requests may not yet be running.
291 * Defer processing channel callbacks until the daemon
292 * has registered.
294 vss_transaction.active = true;
295 return 0;
298 void hv_vss_deinit(void)
300 cn_del_callback(&vss_id);
301 cancel_delayed_work_sync(&vss_timeout_work);
302 cancel_work_sync(&vss_send_op_work);