blob: 9d5e0d1efdb51bd235f87bcdd914d2c5656d6d45 [file] [log] [blame]
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -07001/*
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
K. Y. Srinivasan67413352013-07-02 10:31:30 -070027#define VSS_MAJOR 5
28#define VSS_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070029#define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
K. Y. Srinivasan67413352013-07-02 10:31:30 -070030
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010031#define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000))
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070032
33/*
34 * Global state maintained for transaction that is being processed.
35 * Note that only one transaction can be active at any point in time.
36 *
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.
40 */
41
42static 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;
49
50
51static void vss_respond_to_host(int error);
52
53static struct cb_id vss_id = { CN_VSS_IDX, CN_VSS_VAL };
54static const char vss_name[] = "vss_kernel_module";
55static __u8 *recv_buffer;
56
57static void vss_send_op(struct work_struct *dummy);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010058static void vss_timeout_func(struct work_struct *dummy);
59
60static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070061static DECLARE_WORK(vss_send_op_work, vss_send_op);
62
63/*
64 * Callback when data is received from user mode.
65 */
66
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010067static void vss_timeout_func(struct work_struct *dummy)
68{
69 /*
70 * Timeout waiting for userspace component to reply happened.
71 */
72 pr_warn("VSS: timeout waiting for daemon to reply\n");
73 vss_respond_to_host(HV_E_FAIL);
74}
75
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070076static void
77vss_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
78{
79 struct hv_vss_msg *vss_msg;
80
81 vss_msg = (struct hv_vss_msg *)msg->data;
82
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;
89
90 }
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010091 if (cancel_delayed_work_sync(&vss_timeout_work))
92 vss_respond_to_host(vss_msg->error);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070093}
94
95
96static void vss_send_op(struct work_struct *dummy)
97{
98 int op = vss_transaction.msg->vss_hdr.operation;
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +010099 int rc;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700100 struct cn_msg *msg;
101 struct hv_vss_msg *vss_msg;
102
103 msg = kzalloc(sizeof(*msg) + sizeof(*vss_msg), GFP_ATOMIC);
104 if (!msg)
105 return;
106
107 vss_msg = (struct hv_vss_msg *)msg->data;
108
109 msg->id.idx = CN_VSS_IDX;
110 msg->id.val = CN_VSS_VAL;
111
112 vss_msg->vss_hdr.operation = op;
113 msg->len = sizeof(struct hv_vss_msg);
114
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100115 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);
120 }
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700121 kfree(msg);
122
123 return;
124}
125
126/*
127 * Send a response back to the host.
128 */
129
130static void
131vss_respond_to_host(int error)
132{
133 struct icmsg_hdr *icmsghdrp;
134 u32 buf_len;
135 struct vmbus_channel *channel;
136 u64 req_id;
137
138 /*
139 * If a transaction is not active; log and return.
140 */
141
142 if (!vss_transaction.active) {
143 /*
144 * This is a spurious call!
145 */
146 pr_warn("VSS: Transaction not active\n");
147 return;
148 }
149 /*
150 * Copy the global state for completing the transaction. Note that
151 * only one transaction can be active at a time.
152 */
153
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;
158
159 icmsghdrp = (struct icmsg_hdr *)
160 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
161
162 if (channel->onchannel_callback == NULL)
163 /*
164 * We have raced with util driver being unloaded;
165 * silently return.
166 */
167 return;
168
169 icmsghdrp->status = error;
170
171 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
172
173 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
174 VM_PKT_DATA_INBAND, 0);
175
176}
177
178/*
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.
181 */
182
183void hv_vss_onchannelcallback(void *context)
184{
185 struct vmbus_channel *channel = context;
186 u32 recvlen;
187 u64 requestid;
188 struct hv_vss_msg *vss_msg;
189
190
191 struct icmsg_hdr *icmsghdrp;
192 struct icmsg_negotiate *negop = NULL;
193
194 if (vss_transaction.active) {
195 /*
196 * We will defer processing this callback once
197 * the current transaction is complete.
198 */
199 vss_transaction.recv_channel = channel;
200 return;
201 }
202
203 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
204 &requestid);
205
206 if (recvlen > 0) {
207 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
208 sizeof(struct vmbuspipe_hdr)];
209
210 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
211 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700212 recv_buffer, UTIL_FW_VERSION,
213 VSS_VERSION);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700214 } else {
215 vss_msg = (struct hv_vss_msg *)&recv_buffer[
216 sizeof(struct vmbuspipe_hdr) +
217 sizeof(struct icmsg_hdr)];
218
219 /*
220 * Stash away this global state for completing the
221 * transaction; note transactions are serialized.
222 */
223
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;
229
230 switch (vss_msg->vss_hdr.operation) {
231 /*
232 * Initiate a "freeze/thaw"
233 * operation in the guest.
234 * We respond to the host once
235 * the operation is complete.
236 *
237 * We send the message to the
238 * user space daemon and the
239 * operation is performed in
240 * the daemon.
241 */
242 case VSS_OP_FREEZE:
243 case VSS_OP_THAW:
244 schedule_work(&vss_send_op_work);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +0100245 schedule_delayed_work(&vss_timeout_work,
246 VSS_USERSPACE_TIMEOUT);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700247 return;
248
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;
254
255 case VSS_OP_GET_DM_INFO:
256 vss_msg->dm_info.flags = 0;
257 vss_respond_to_host(0);
258 return;
259
260 default:
261 vss_respond_to_host(0);
262 return;
263
264 }
265
266 }
267
268 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
269 | ICMSGHDRFLAG_RESPONSE;
270
271 vmbus_sendpacket(channel, recv_buffer,
272 recvlen, requestid,
273 VM_PKT_DATA_INBAND, 0);
274 }
275
276}
277
278int
279hv_vss_init(struct hv_util_service *srv)
280{
281 int err;
282
283 err = cn_add_callback(&vss_id, vss_name, vss_cn_callback);
284 if (err)
285 return err;
286 recv_buffer = srv->recv_buffer;
287
288 /*
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.
293 */
294 vss_transaction.active = true;
295 return 0;
296}
297
298void hv_vss_deinit(void)
299{
300 cn_del_callback(&vss_id);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +0100301 cancel_delayed_work_sync(&vss_timeout_work);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700302 cancel_work_sync(&vss_send_op_work);
303}