Tools: hv: vss: use misc char device to communicate with kernel
[deliverable/linux.git] / drivers / hv / hv_snapshot.c
CommitLineData
96dd86fa
S
1/*
2 * An implementation of host initiated guest snapshot.
3 *
4 *
5 * Copyright (C) 2013, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
7 *
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.
11 *
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.
17 *
18 */
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
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>
26
3647a83d 27#include "hyperv_vmbus.h"
6472f80a 28#include "hv_utils_transport.h"
3647a83d 29
6741335b
S
30#define VSS_MAJOR 5
31#define VSS_MINOR 0
3a491605 32#define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
6741335b 33
64914207 34#define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000))
96dd86fa
S
35
36/*
086a6f68
VK
37 * Global state maintained for transaction that is being processed. For a class
38 * of integration services, including the "VSS service", the specified protocol
39 * is a "request/response" protocol which means that there can only be single
40 * outstanding transaction from the host at any given point in time. We use
41 * this to simplify memory management in this driver - we cache and process
42 * only one message at a time.
96dd86fa 43 *
086a6f68
VK
44 * While the request/response protocol is guaranteed by the host, we further
45 * ensure this by serializing packet processing in this driver - we do not
46 * read additional packets from the VMBUs until the current packet is fully
47 * handled.
96dd86fa
S
48 */
49
50static struct {
086a6f68 51 int state; /* hvutil_device_state */
96dd86fa
S
52 int recv_len; /* number of bytes received. */
53 struct vmbus_channel *recv_channel; /* chn we got the request */
54 u64 recv_req_id; /* request ID. */
55 struct hv_vss_msg *msg; /* current message */
38c06c29 56 void *vss_context; /* for the channel callback */
96dd86fa
S
57} vss_transaction;
58
59
60static void vss_respond_to_host(int error);
61
6472f80a 62static const char vss_devname[] = "vmbus/hv_vss";
96dd86fa 63static __u8 *recv_buffer;
6472f80a 64static struct hvutil_transport *hvt;
96dd86fa
S
65
66static void vss_send_op(struct work_struct *dummy);
64914207
VK
67static void vss_timeout_func(struct work_struct *dummy);
68
69static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
96dd86fa
S
70static DECLARE_WORK(vss_send_op_work, vss_send_op);
71
72/*
73 * Callback when data is received from user mode.
74 */
75
64914207
VK
76static void vss_timeout_func(struct work_struct *dummy)
77{
78 /*
79 * Timeout waiting for userspace component to reply happened.
80 */
81 pr_warn("VSS: timeout waiting for daemon to reply\n");
82 vss_respond_to_host(HV_E_FAIL);
38c06c29 83
086a6f68
VK
84 /* Transaction is finished, reset the state. */
85 if (vss_transaction.state > HVUTIL_READY)
86 vss_transaction.state = HVUTIL_READY;
87
38c06c29
VK
88 hv_poll_channel(vss_transaction.vss_context,
89 hv_vss_onchannelcallback);
64914207
VK
90}
91
6472f80a 92static int vss_on_msg(void *msg, int len)
96dd86fa 93{
6472f80a 94 struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg;
96dd86fa 95
6472f80a
VK
96 if (len != sizeof(*vss_msg))
97 return -EINVAL;
96dd86fa 98
086a6f68
VK
99 /*
100 * Don't process registration messages if we're in the middle of
101 * a transaction processing.
102 */
103 if (vss_transaction.state > HVUTIL_READY &&
104 vss_msg->vss_hdr.operation == VSS_OP_REGISTER)
6472f80a 105 return -EINVAL;
086a6f68
VK
106
107 if (vss_transaction.state == HVUTIL_DEVICE_INIT &&
108 vss_msg->vss_hdr.operation == VSS_OP_REGISTER) {
96dd86fa 109 pr_info("VSS daemon registered\n");
086a6f68
VK
110 vss_transaction.state = HVUTIL_READY;
111 } else if (vss_transaction.state == HVUTIL_USERSPACE_REQ) {
112 vss_transaction.state = HVUTIL_USERSPACE_RECV;
113 if (cancel_delayed_work_sync(&vss_timeout_work)) {
114 vss_respond_to_host(vss_msg->error);
115 /* Transaction is finished, reset the state. */
116 vss_transaction.state = HVUTIL_READY;
117 hv_poll_channel(vss_transaction.vss_context,
118 hv_vss_onchannelcallback);
119 }
120 } else {
121 /* This is a spurious call! */
122 pr_warn("VSS: Transaction not active\n");
6472f80a 123 return -EINVAL;
96dd86fa 124 }
6472f80a 125 return 0;
96dd86fa
S
126}
127
128
129static void vss_send_op(struct work_struct *dummy)
130{
131 int op = vss_transaction.msg->vss_hdr.operation;
8d9560eb 132 int rc;
96dd86fa
S
133 struct hv_vss_msg *vss_msg;
134
086a6f68
VK
135 /* The transaction state is wrong. */
136 if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
137 return;
138
6472f80a
VK
139 vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
140 if (!vss_msg)
96dd86fa
S
141 return;
142
96dd86fa 143 vss_msg->vss_hdr.operation = op;
96dd86fa 144
086a6f68 145 vss_transaction.state = HVUTIL_USERSPACE_REQ;
6472f80a 146 rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg));
8d9560eb
VK
147 if (rc) {
148 pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
086a6f68 149 if (cancel_delayed_work_sync(&vss_timeout_work)) {
8d9560eb 150 vss_respond_to_host(HV_E_FAIL);
086a6f68
VK
151 vss_transaction.state = HVUTIL_READY;
152 }
8d9560eb 153 }
086a6f68 154
6472f80a 155 kfree(vss_msg);
96dd86fa
S
156
157 return;
158}
159
160/*
161 * Send a response back to the host.
162 */
163
164static void
165vss_respond_to_host(int error)
166{
167 struct icmsg_hdr *icmsghdrp;
168 u32 buf_len;
169 struct vmbus_channel *channel;
170 u64 req_id;
171
96dd86fa
S
172 /*
173 * Copy the global state for completing the transaction. Note that
174 * only one transaction can be active at a time.
175 */
176
177 buf_len = vss_transaction.recv_len;
178 channel = vss_transaction.recv_channel;
179 req_id = vss_transaction.recv_req_id;
96dd86fa
S
180
181 icmsghdrp = (struct icmsg_hdr *)
182 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
183
184 if (channel->onchannel_callback == NULL)
185 /*
186 * We have raced with util driver being unloaded;
187 * silently return.
188 */
189 return;
190
191 icmsghdrp->status = error;
192
193 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
194
195 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
196 VM_PKT_DATA_INBAND, 0);
197
198}
199
200/*
201 * This callback is invoked when we get a VSS message from the host.
202 * The host ensures that only one VSS transaction can be active at a time.
203 */
204
205void hv_vss_onchannelcallback(void *context)
206{
207 struct vmbus_channel *channel = context;
208 u32 recvlen;
209 u64 requestid;
210 struct hv_vss_msg *vss_msg;
211
212
213 struct icmsg_hdr *icmsghdrp;
214 struct icmsg_negotiate *negop = NULL;
215
086a6f68 216 if (vss_transaction.state > HVUTIL_READY) {
96dd86fa
S
217 /*
218 * We will defer processing this callback once
219 * the current transaction is complete.
220 */
38c06c29 221 vss_transaction.vss_context = context;
96dd86fa
S
222 return;
223 }
38c06c29 224 vss_transaction.vss_context = NULL;
96dd86fa
S
225
226 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
227 &requestid);
228
229 if (recvlen > 0) {
230 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
231 sizeof(struct vmbuspipe_hdr)];
232
233 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
234 vmbus_prep_negotiate_resp(icmsghdrp, negop,
3a491605
S
235 recv_buffer, UTIL_FW_VERSION,
236 VSS_VERSION);
96dd86fa
S
237 } else {
238 vss_msg = (struct hv_vss_msg *)&recv_buffer[
239 sizeof(struct vmbuspipe_hdr) +
240 sizeof(struct icmsg_hdr)];
241
242 /*
243 * Stash away this global state for completing the
244 * transaction; note transactions are serialized.
245 */
246
247 vss_transaction.recv_len = recvlen;
248 vss_transaction.recv_channel = channel;
249 vss_transaction.recv_req_id = requestid;
96dd86fa
S
250 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
251
252 switch (vss_msg->vss_hdr.operation) {
253 /*
254 * Initiate a "freeze/thaw"
255 * operation in the guest.
256 * We respond to the host once
257 * the operation is complete.
258 *
259 * We send the message to the
260 * user space daemon and the
261 * operation is performed in
262 * the daemon.
263 */
264 case VSS_OP_FREEZE:
265 case VSS_OP_THAW:
086a6f68
VK
266 if (vss_transaction.state < HVUTIL_READY) {
267 /* Userspace is not registered yet */
268 vss_respond_to_host(HV_E_FAIL);
269 return;
270 }
271 vss_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
96dd86fa 272 schedule_work(&vss_send_op_work);
64914207
VK
273 schedule_delayed_work(&vss_timeout_work,
274 VSS_USERSPACE_TIMEOUT);
96dd86fa
S
275 return;
276
277 case VSS_OP_HOT_BACKUP:
278 vss_msg->vss_cf.flags =
279 VSS_HBU_NO_AUTO_RECOVERY;
280 vss_respond_to_host(0);
281 return;
282
283 case VSS_OP_GET_DM_INFO:
284 vss_msg->dm_info.flags = 0;
285 vss_respond_to_host(0);
286 return;
287
288 default:
289 vss_respond_to_host(0);
290 return;
291
292 }
293
294 }
295
296 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
297 | ICMSGHDRFLAG_RESPONSE;
298
299 vmbus_sendpacket(channel, recv_buffer,
300 recvlen, requestid,
301 VM_PKT_DATA_INBAND, 0);
302 }
303
304}
305
6472f80a
VK
306static void vss_on_reset(void)
307{
308 if (cancel_delayed_work_sync(&vss_timeout_work))
309 vss_respond_to_host(HV_E_FAIL);
310 vss_transaction.state = HVUTIL_DEVICE_INIT;
311}
312
96dd86fa
S
313int
314hv_vss_init(struct hv_util_service *srv)
315{
96dd86fa
S
316 recv_buffer = srv->recv_buffer;
317
318 /*
319 * When this driver loads, the user level daemon that
320 * processes the host requests may not yet be running.
321 * Defer processing channel callbacks until the daemon
322 * has registered.
323 */
086a6f68
VK
324 vss_transaction.state = HVUTIL_DEVICE_INIT;
325
6472f80a
VK
326 hvt = hvutil_transport_init(vss_devname, CN_VSS_IDX, CN_VSS_VAL,
327 vss_on_msg, vss_on_reset);
328 if (!hvt)
329 return -EFAULT;
330
96dd86fa
S
331 return 0;
332}
333
334void hv_vss_deinit(void)
335{
086a6f68 336 vss_transaction.state = HVUTIL_DEVICE_DYING;
64914207 337 cancel_delayed_work_sync(&vss_timeout_work);
96dd86fa 338 cancel_work_sync(&vss_send_op_work);
6472f80a 339 hvutil_transport_destroy(hvt);
96dd86fa 340}
This page took 0.136631 seconds and 5 git commands to generate.