blob: 21e51be74e6c4a15a915796ab38d368170bdf5f6 [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;
99 struct cn_msg *msg;
100 struct hv_vss_msg *vss_msg;
101
102 msg = kzalloc(sizeof(*msg) + sizeof(*vss_msg), GFP_ATOMIC);
103 if (!msg)
104 return;
105
106 vss_msg = (struct hv_vss_msg *)msg->data;
107
108 msg->id.idx = CN_VSS_IDX;
109 msg->id.val = CN_VSS_VAL;
110
111 vss_msg->vss_hdr.operation = op;
112 msg->len = sizeof(struct hv_vss_msg);
113
David Friesac8f7332014-01-15 22:29:19 -0600114 cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700115 kfree(msg);
116
117 return;
118}
119
120/*
121 * Send a response back to the host.
122 */
123
124static void
125vss_respond_to_host(int error)
126{
127 struct icmsg_hdr *icmsghdrp;
128 u32 buf_len;
129 struct vmbus_channel *channel;
130 u64 req_id;
131
132 /*
133 * If a transaction is not active; log and return.
134 */
135
136 if (!vss_transaction.active) {
137 /*
138 * This is a spurious call!
139 */
140 pr_warn("VSS: Transaction not active\n");
141 return;
142 }
143 /*
144 * Copy the global state for completing the transaction. Note that
145 * only one transaction can be active at a time.
146 */
147
148 buf_len = vss_transaction.recv_len;
149 channel = vss_transaction.recv_channel;
150 req_id = vss_transaction.recv_req_id;
151 vss_transaction.active = false;
152
153 icmsghdrp = (struct icmsg_hdr *)
154 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
155
156 if (channel->onchannel_callback == NULL)
157 /*
158 * We have raced with util driver being unloaded;
159 * silently return.
160 */
161 return;
162
163 icmsghdrp->status = error;
164
165 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
166
167 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
168 VM_PKT_DATA_INBAND, 0);
169
170}
171
172/*
173 * This callback is invoked when we get a VSS message from the host.
174 * The host ensures that only one VSS transaction can be active at a time.
175 */
176
177void hv_vss_onchannelcallback(void *context)
178{
179 struct vmbus_channel *channel = context;
180 u32 recvlen;
181 u64 requestid;
182 struct hv_vss_msg *vss_msg;
183
184
185 struct icmsg_hdr *icmsghdrp;
186 struct icmsg_negotiate *negop = NULL;
187
188 if (vss_transaction.active) {
189 /*
190 * We will defer processing this callback once
191 * the current transaction is complete.
192 */
193 vss_transaction.recv_channel = channel;
194 return;
195 }
196
197 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
198 &requestid);
199
200 if (recvlen > 0) {
201 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
202 sizeof(struct vmbuspipe_hdr)];
203
204 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
205 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700206 recv_buffer, UTIL_FW_VERSION,
207 VSS_VERSION);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700208 } else {
209 vss_msg = (struct hv_vss_msg *)&recv_buffer[
210 sizeof(struct vmbuspipe_hdr) +
211 sizeof(struct icmsg_hdr)];
212
213 /*
214 * Stash away this global state for completing the
215 * transaction; note transactions are serialized.
216 */
217
218 vss_transaction.recv_len = recvlen;
219 vss_transaction.recv_channel = channel;
220 vss_transaction.recv_req_id = requestid;
221 vss_transaction.active = true;
222 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
223
224 switch (vss_msg->vss_hdr.operation) {
225 /*
226 * Initiate a "freeze/thaw"
227 * operation in the guest.
228 * We respond to the host once
229 * the operation is complete.
230 *
231 * We send the message to the
232 * user space daemon and the
233 * operation is performed in
234 * the daemon.
235 */
236 case VSS_OP_FREEZE:
237 case VSS_OP_THAW:
238 schedule_work(&vss_send_op_work);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +0100239 schedule_delayed_work(&vss_timeout_work,
240 VSS_USERSPACE_TIMEOUT);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700241 return;
242
243 case VSS_OP_HOT_BACKUP:
244 vss_msg->vss_cf.flags =
245 VSS_HBU_NO_AUTO_RECOVERY;
246 vss_respond_to_host(0);
247 return;
248
249 case VSS_OP_GET_DM_INFO:
250 vss_msg->dm_info.flags = 0;
251 vss_respond_to_host(0);
252 return;
253
254 default:
255 vss_respond_to_host(0);
256 return;
257
258 }
259
260 }
261
262 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
263 | ICMSGHDRFLAG_RESPONSE;
264
265 vmbus_sendpacket(channel, recv_buffer,
266 recvlen, requestid,
267 VM_PKT_DATA_INBAND, 0);
268 }
269
270}
271
272int
273hv_vss_init(struct hv_util_service *srv)
274{
275 int err;
276
277 err = cn_add_callback(&vss_id, vss_name, vss_cn_callback);
278 if (err)
279 return err;
280 recv_buffer = srv->recv_buffer;
281
282 /*
283 * When this driver loads, the user level daemon that
284 * processes the host requests may not yet be running.
285 * Defer processing channel callbacks until the daemon
286 * has registered.
287 */
288 vss_transaction.active = true;
289 return 0;
290}
291
292void hv_vss_deinit(void)
293{
294 cn_del_callback(&vss_id);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +0100295 cancel_delayed_work_sync(&vss_timeout_work);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700296 cancel_work_sync(&vss_send_op_work);
297}