xref: /linux/drivers/hv/hv_snapshot.c (revision 37cb0c76ac6c3bf3d82d25a7c95950f2dac3ca41)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * An implementation of host initiated guest snapshot.
4  *
5  * Copyright (C) 2013, Microsoft, Inc.
6  * Author : K. Y. Srinivasan <kys@microsoft.com>
7  */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/net.h>
11 #include <linux/nls.h>
12 #include <linux/connector.h>
13 #include <linux/workqueue.h>
14 #include <linux/hyperv.h>
15 #include <asm/hyperv-tlfs.h>
16 
17 #include "hyperv_vmbus.h"
18 #include "hv_utils_transport.h"
19 
20 #define VSS_MAJOR  5
21 #define VSS_MINOR  0
22 #define VSS_VERSION    (VSS_MAJOR << 16 | VSS_MINOR)
23 
24 #define VSS_VER_COUNT 1
25 static const int vss_versions[] = {
26 	VSS_VERSION
27 };
28 
29 #define FW_VER_COUNT 1
30 static const int fw_versions[] = {
31 	UTIL_FW_VERSION
32 };
33 
34 /* See comment with struct hv_vss_msg regarding the max VMbus packet size */
35 #define VSS_MAX_PKT_SIZE (HV_HYP_PAGE_SIZE * 2)
36 
37 /*
38  * Timeout values are based on expecations from host
39  */
40 #define VSS_FREEZE_TIMEOUT (15 * 60)
41 
42 /*
43  * Global state maintained for transaction that is being processed. For a class
44  * of integration services, including the "VSS service", the specified protocol
45  * is a "request/response" protocol which means that there can only be single
46  * outstanding transaction from the host at any given point in time. We use
47  * this to simplify memory management in this driver - we cache and process
48  * only one message at a time.
49  *
50  * While the request/response protocol is guaranteed by the host, we further
51  * ensure this by serializing packet processing in this driver - we do not
52  * read additional packets from the VMBUs until the current packet is fully
53  * handled.
54  */
55 
56 static struct {
57 	int state;   /* hvutil_device_state */
58 	int recv_len; /* number of bytes received. */
59 	struct vmbus_channel *recv_channel; /* chn we got the request */
60 	u64 recv_req_id; /* request ID. */
61 	struct hv_vss_msg  *msg; /* current message */
62 } vss_transaction;
63 
64 
65 static void vss_respond_to_host(int error);
66 
67 /*
68  * This state maintains the version number registered by the daemon.
69  */
70 static int dm_reg_value;
71 
72 static const char vss_devname[] = "vmbus/hv_vss";
73 static __u8 *recv_buffer;
74 static struct hvutil_transport *hvt;
75 
76 static void vss_timeout_func(struct work_struct *dummy);
77 static void vss_handle_request(struct work_struct *dummy);
78 
79 static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
80 static DECLARE_WORK(vss_handle_request_work, vss_handle_request);
81 
vss_poll_wrapper(void * channel)82 static void vss_poll_wrapper(void *channel)
83 {
84 	/* Transaction is finished, reset the state here to avoid races. */
85 	vss_transaction.state = HVUTIL_READY;
86 	tasklet_schedule(&((struct vmbus_channel *)channel)->callback_event);
87 }
88 
89 /*
90  * Callback when data is received from user mode.
91  */
92 
vss_timeout_func(struct work_struct * dummy)93 static void vss_timeout_func(struct work_struct *dummy)
94 {
95 	/*
96 	 * Timeout waiting for userspace component to reply happened.
97 	 */
98 	pr_warn("VSS: timeout waiting for daemon to reply\n");
99 	vss_respond_to_host(HV_E_FAIL);
100 
101 	hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
102 }
103 
vss_register_done(void)104 static void vss_register_done(void)
105 {
106 	hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
107 	pr_debug("VSS: userspace daemon registered\n");
108 }
109 
vss_handle_handshake(struct hv_vss_msg * vss_msg)110 static int vss_handle_handshake(struct hv_vss_msg *vss_msg)
111 {
112 	u32 our_ver = VSS_OP_REGISTER1;
113 
114 	switch (vss_msg->vss_hdr.operation) {
115 	case VSS_OP_REGISTER:
116 		/* Daemon doesn't expect us to reply */
117 		dm_reg_value = VSS_OP_REGISTER;
118 		break;
119 	case VSS_OP_REGISTER1:
120 		/* Daemon expects us to reply with our own version */
121 		if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
122 					  vss_register_done))
123 			return -EFAULT;
124 		dm_reg_value = VSS_OP_REGISTER1;
125 		break;
126 	default:
127 		return -EINVAL;
128 	}
129 	pr_info("VSS: userspace daemon ver. %d connected\n", dm_reg_value);
130 	return 0;
131 }
132 
vss_on_msg(void * msg,int len)133 static int vss_on_msg(void *msg, int len)
134 {
135 	struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg;
136 
137 	if (len != sizeof(*vss_msg)) {
138 		pr_debug("VSS: Message size does not match length\n");
139 		return -EINVAL;
140 	}
141 
142 	if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER ||
143 	    vss_msg->vss_hdr.operation == VSS_OP_REGISTER1) {
144 		/*
145 		 * Don't process registration messages if we're in the middle
146 		 * of a transaction processing.
147 		 */
148 		if (vss_transaction.state > HVUTIL_READY) {
149 			pr_debug("VSS: Got unexpected registration request\n");
150 			return -EINVAL;
151 		}
152 
153 		return vss_handle_handshake(vss_msg);
154 	} else if (vss_transaction.state == HVUTIL_USERSPACE_REQ) {
155 		vss_transaction.state = HVUTIL_USERSPACE_RECV;
156 
157 		if (vss_msg->vss_hdr.operation == VSS_OP_HOT_BACKUP)
158 			vss_transaction.msg->vss_cf.flags =
159 				VSS_HBU_NO_AUTO_RECOVERY;
160 
161 		if (cancel_delayed_work_sync(&vss_timeout_work)) {
162 			vss_respond_to_host(vss_msg->error);
163 			/* Transaction is finished, reset the state. */
164 			hv_poll_channel(vss_transaction.recv_channel,
165 					vss_poll_wrapper);
166 		}
167 	} else {
168 		/* This is a spurious call! */
169 		pr_debug("VSS: Transaction not active\n");
170 		return -EINVAL;
171 	}
172 	return 0;
173 }
174 
vss_send_op(void)175 static void vss_send_op(void)
176 {
177 	int op = vss_transaction.msg->vss_hdr.operation;
178 	int rc;
179 	struct hv_vss_msg *vss_msg;
180 
181 	/* The transaction state is wrong. */
182 	if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED) {
183 		pr_debug("VSS: Unexpected attempt to send to daemon\n");
184 		return;
185 	}
186 
187 	vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
188 	if (!vss_msg)
189 		return;
190 
191 	vss_msg->vss_hdr.operation = op;
192 
193 	vss_transaction.state = HVUTIL_USERSPACE_REQ;
194 
195 	schedule_delayed_work(&vss_timeout_work, op == VSS_OP_FREEZE ?
196 				secs_to_jiffies(VSS_FREEZE_TIMEOUT) :
197 				secs_to_jiffies(HV_UTIL_TIMEOUT));
198 
199 	rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
200 	if (rc) {
201 		pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
202 		if (cancel_delayed_work_sync(&vss_timeout_work)) {
203 			vss_respond_to_host(HV_E_FAIL);
204 			vss_transaction.state = HVUTIL_READY;
205 		}
206 	}
207 
208 	kfree(vss_msg);
209 }
210 
vss_handle_request(struct work_struct * dummy)211 static void vss_handle_request(struct work_struct *dummy)
212 {
213 	switch (vss_transaction.msg->vss_hdr.operation) {
214 	/*
215 	 * Initiate a "freeze/thaw" operation in the guest.
216 	 * We respond to the host once the operation is complete.
217 	 *
218 	 * We send the message to the user space daemon and the operation is
219 	 * performed in the daemon.
220 	 */
221 	case VSS_OP_THAW:
222 	case VSS_OP_FREEZE:
223 	case VSS_OP_HOT_BACKUP:
224 		if (vss_transaction.state < HVUTIL_READY) {
225 			/* Userspace is not registered yet */
226 			pr_debug("VSS: Not ready for request.\n");
227 			vss_respond_to_host(HV_E_FAIL);
228 			return;
229 		}
230 
231 		pr_debug("VSS: Received request for op code: %d\n",
232 			vss_transaction.msg->vss_hdr.operation);
233 		vss_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
234 		vss_send_op();
235 		return;
236 	case VSS_OP_GET_DM_INFO:
237 		vss_transaction.msg->dm_info.flags = 0;
238 		break;
239 	default:
240 		break;
241 	}
242 
243 	vss_respond_to_host(0);
244 	hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
245 }
246 
247 /*
248  * Send a response back to the host.
249  */
250 
251 static void
vss_respond_to_host(int error)252 vss_respond_to_host(int error)
253 {
254 	struct icmsg_hdr *icmsghdrp;
255 	u32	buf_len;
256 	struct vmbus_channel *channel;
257 	u64	req_id;
258 
259 	/*
260 	 * Copy the global state for completing the transaction. Note that
261 	 * only one transaction can be active at a time.
262 	 */
263 
264 	buf_len = vss_transaction.recv_len;
265 	channel = vss_transaction.recv_channel;
266 	req_id = vss_transaction.recv_req_id;
267 
268 	icmsghdrp = (struct icmsg_hdr *)
269 			&recv_buffer[sizeof(struct vmbuspipe_hdr)];
270 
271 	if (channel->onchannel_callback == NULL)
272 		/*
273 		 * We have raced with util driver being unloaded;
274 		 * silently return.
275 		 */
276 		return;
277 
278 	icmsghdrp->status = error;
279 
280 	icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
281 
282 	vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
283 				VM_PKT_DATA_INBAND, 0);
284 
285 }
286 
287 /*
288  * This callback is invoked when we get a VSS message from the host.
289  * The host ensures that only one VSS transaction can be active at a time.
290  */
291 
hv_vss_onchannelcallback(void * context)292 void hv_vss_onchannelcallback(void *context)
293 {
294 	struct vmbus_channel *channel = context;
295 	u32 recvlen;
296 	u64 requestid;
297 	struct hv_vss_msg *vss_msg;
298 	int vss_srv_version;
299 
300 	struct icmsg_hdr *icmsghdrp;
301 
302 	if (vss_transaction.state > HVUTIL_READY)
303 		return;
304 
305 	if (vmbus_recvpacket(channel, recv_buffer, VSS_MAX_PKT_SIZE, &recvlen, &requestid)) {
306 		pr_err_ratelimited("VSS request received. Could not read into recv buf\n");
307 		return;
308 	}
309 
310 	if (!recvlen)
311 		return;
312 
313 	/* Ensure recvlen is big enough to read header data */
314 	if (recvlen < ICMSG_HDR) {
315 		pr_err_ratelimited("VSS request received. Packet length too small: %d\n",
316 				   recvlen);
317 		return;
318 	}
319 
320 	icmsghdrp = (struct icmsg_hdr *)&recv_buffer[sizeof(struct vmbuspipe_hdr)];
321 
322 	if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
323 		if (vmbus_prep_negotiate_resp(icmsghdrp,
324 				recv_buffer, recvlen,
325 				fw_versions, FW_VER_COUNT,
326 				vss_versions, VSS_VER_COUNT,
327 				NULL, &vss_srv_version)) {
328 
329 			pr_info("VSS IC version %d.%d\n",
330 				vss_srv_version >> 16,
331 				vss_srv_version & 0xFFFF);
332 		}
333 	} else if (icmsghdrp->icmsgtype == ICMSGTYPE_VSS) {
334 		/* Ensure recvlen is big enough to contain hv_vss_msg */
335 		if (recvlen < ICMSG_HDR + sizeof(struct hv_vss_msg)) {
336 			pr_err_ratelimited("Invalid VSS msg. Packet length too small: %u\n",
337 					   recvlen);
338 			return;
339 		}
340 		vss_msg = (struct hv_vss_msg *)&recv_buffer[ICMSG_HDR];
341 
342 		/*
343 		 * Stash away this global state for completing the
344 		 * transaction; note transactions are serialized.
345 		 */
346 
347 		vss_transaction.recv_len = recvlen;
348 		vss_transaction.recv_req_id = requestid;
349 		vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
350 
351 		schedule_work(&vss_handle_request_work);
352 		return;
353 	} else {
354 		pr_err_ratelimited("VSS request received. Invalid msg type: %d\n",
355 				   icmsghdrp->icmsgtype);
356 		return;
357 	}
358 
359 	icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
360 		ICMSGHDRFLAG_RESPONSE;
361 	vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
362 			 VM_PKT_DATA_INBAND, 0);
363 }
364 
vss_on_reset(void)365 static void vss_on_reset(void)
366 {
367 	if (cancel_delayed_work_sync(&vss_timeout_work))
368 		vss_respond_to_host(HV_E_FAIL);
369 	vss_transaction.state = HVUTIL_DEVICE_INIT;
370 }
371 
372 int
hv_vss_init(struct hv_util_service * srv)373 hv_vss_init(struct hv_util_service *srv)
374 {
375 	if (vmbus_proto_version < VERSION_WIN8_1) {
376 		pr_warn("Integration service 'Backup (volume snapshot)'"
377 			" not supported on this host version.\n");
378 		return -ENOTSUPP;
379 	}
380 	recv_buffer = srv->recv_buffer;
381 	vss_transaction.recv_channel = srv->channel;
382 	vss_transaction.recv_channel->max_pkt_size = VSS_MAX_PKT_SIZE;
383 
384 	/*
385 	 * When this driver loads, the user level daemon that
386 	 * processes the host requests may not yet be running.
387 	 * Defer processing channel callbacks until the daemon
388 	 * has registered.
389 	 */
390 	vss_transaction.state = HVUTIL_DEVICE_INIT;
391 
392 	return 0;
393 }
394 
395 int
hv_vss_init_transport(void)396 hv_vss_init_transport(void)
397 {
398 	hvt = hvutil_transport_init(vss_devname, CN_VSS_IDX, CN_VSS_VAL,
399 				    vss_on_msg, vss_on_reset);
400 	if (!hvt) {
401 		pr_warn("VSS: Failed to initialize transport\n");
402 		return -EFAULT;
403 	}
404 
405 	return 0;
406 }
407 
hv_vss_cancel_work(void)408 static void hv_vss_cancel_work(void)
409 {
410 	cancel_delayed_work_sync(&vss_timeout_work);
411 	cancel_work_sync(&vss_handle_request_work);
412 }
413 
hv_vss_pre_suspend(void)414 int hv_vss_pre_suspend(void)
415 {
416 	struct vmbus_channel *channel = vss_transaction.recv_channel;
417 	struct hv_vss_msg *vss_msg;
418 
419 	/*
420 	 * Fake a THAW message for the user space daemon in case the daemon
421 	 * has frozen the file systems. It doesn't matter if there is already
422 	 * a message pending to be delivered to the user space since we force
423 	 * vss_transaction.state to be HVUTIL_READY, so the user space daemon's
424 	 * write() will fail with EINVAL (see vss_on_msg()), and the daemon
425 	 * will reset the device by closing and re-opening it.
426 	 */
427 	vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
428 	if (!vss_msg)
429 		return -ENOMEM;
430 
431 	tasklet_disable(&channel->callback_event);
432 
433 	vss_msg->vss_hdr.operation = VSS_OP_THAW;
434 
435 	/* Cancel any possible pending work. */
436 	hv_vss_cancel_work();
437 
438 	/* We don't care about the return value. */
439 	hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
440 
441 	kfree(vss_msg);
442 
443 	vss_transaction.state = HVUTIL_READY;
444 
445 	/* tasklet_enable() will be called in hv_vss_pre_resume(). */
446 	return 0;
447 }
448 
hv_vss_pre_resume(void)449 int hv_vss_pre_resume(void)
450 {
451 	struct vmbus_channel *channel = vss_transaction.recv_channel;
452 
453 	tasklet_enable(&channel->callback_event);
454 
455 	return 0;
456 }
457 
hv_vss_deinit(void)458 void hv_vss_deinit(void)
459 {
460 	vss_transaction.state = HVUTIL_DEVICE_DYING;
461 
462 	hv_vss_cancel_work();
463 
464 	hvutil_transport_destroy(hvt);
465 }
466