Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / drivers / hv / hv_snapshot.c
blob0c354622437681d1b6dd157f718bb27cf8dbdbed
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)
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 DECLARE_WORK(vss_send_op_work, vss_send_op);
61 * Callback when data is received from user mode.
64 static void
65 vss_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
67 struct hv_vss_msg *vss_msg;
69 vss_msg = (struct hv_vss_msg *)msg->data;
71 if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER) {
72 pr_info("VSS daemon registered\n");
73 vss_transaction.active = false;
74 if (vss_transaction.recv_channel != NULL)
75 hv_vss_onchannelcallback(vss_transaction.recv_channel);
76 return;
79 vss_respond_to_host(vss_msg->error);
83 static void vss_send_op(struct work_struct *dummy)
85 int op = vss_transaction.msg->vss_hdr.operation;
86 struct cn_msg *msg;
87 struct hv_vss_msg *vss_msg;
89 msg = kzalloc(sizeof(*msg) + sizeof(*vss_msg), GFP_ATOMIC);
90 if (!msg)
91 return;
93 vss_msg = (struct hv_vss_msg *)msg->data;
95 msg->id.idx = CN_VSS_IDX;
96 msg->id.val = CN_VSS_VAL;
98 vss_msg->vss_hdr.operation = op;
99 msg->len = sizeof(struct hv_vss_msg);
101 cn_netlink_send(msg, 0, GFP_ATOMIC);
102 kfree(msg);
104 return;
108 * Send a response back to the host.
111 static void
112 vss_respond_to_host(int error)
114 struct icmsg_hdr *icmsghdrp;
115 u32 buf_len;
116 struct vmbus_channel *channel;
117 u64 req_id;
120 * If a transaction is not active; log and return.
123 if (!vss_transaction.active) {
125 * This is a spurious call!
127 pr_warn("VSS: Transaction not active\n");
128 return;
131 * Copy the global state for completing the transaction. Note that
132 * only one transaction can be active at a time.
135 buf_len = vss_transaction.recv_len;
136 channel = vss_transaction.recv_channel;
137 req_id = vss_transaction.recv_req_id;
138 vss_transaction.active = false;
140 icmsghdrp = (struct icmsg_hdr *)
141 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
143 if (channel->onchannel_callback == NULL)
145 * We have raced with util driver being unloaded;
146 * silently return.
148 return;
150 icmsghdrp->status = error;
152 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
154 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
155 VM_PKT_DATA_INBAND, 0);
160 * This callback is invoked when we get a VSS message from the host.
161 * The host ensures that only one VSS transaction can be active at a time.
164 void hv_vss_onchannelcallback(void *context)
166 struct vmbus_channel *channel = context;
167 u32 recvlen;
168 u64 requestid;
169 struct hv_vss_msg *vss_msg;
172 struct icmsg_hdr *icmsghdrp;
173 struct icmsg_negotiate *negop = NULL;
175 if (vss_transaction.active) {
177 * We will defer processing this callback once
178 * the current transaction is complete.
180 vss_transaction.recv_channel = channel;
181 return;
184 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
185 &requestid);
187 if (recvlen > 0) {
188 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
189 sizeof(struct vmbuspipe_hdr)];
191 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
192 vmbus_prep_negotiate_resp(icmsghdrp, negop,
193 recv_buffer, UTIL_FW_VERSION,
194 VSS_VERSION);
195 } else {
196 vss_msg = (struct hv_vss_msg *)&recv_buffer[
197 sizeof(struct vmbuspipe_hdr) +
198 sizeof(struct icmsg_hdr)];
201 * Stash away this global state for completing the
202 * transaction; note transactions are serialized.
205 vss_transaction.recv_len = recvlen;
206 vss_transaction.recv_channel = channel;
207 vss_transaction.recv_req_id = requestid;
208 vss_transaction.active = true;
209 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
211 switch (vss_msg->vss_hdr.operation) {
213 * Initiate a "freeze/thaw"
214 * operation in the guest.
215 * We respond to the host once
216 * the operation is complete.
218 * We send the message to the
219 * user space daemon and the
220 * operation is performed in
221 * the daemon.
223 case VSS_OP_FREEZE:
224 case VSS_OP_THAW:
225 schedule_work(&vss_send_op_work);
226 return;
228 case VSS_OP_HOT_BACKUP:
229 vss_msg->vss_cf.flags =
230 VSS_HBU_NO_AUTO_RECOVERY;
231 vss_respond_to_host(0);
232 return;
234 case VSS_OP_GET_DM_INFO:
235 vss_msg->dm_info.flags = 0;
236 vss_respond_to_host(0);
237 return;
239 default:
240 vss_respond_to_host(0);
241 return;
247 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
248 | ICMSGHDRFLAG_RESPONSE;
250 vmbus_sendpacket(channel, recv_buffer,
251 recvlen, requestid,
252 VM_PKT_DATA_INBAND, 0);
258 hv_vss_init(struct hv_util_service *srv)
260 int err;
262 err = cn_add_callback(&vss_id, vss_name, vss_cn_callback);
263 if (err)
264 return err;
265 recv_buffer = srv->recv_buffer;
268 * When this driver loads, the user level daemon that
269 * processes the host requests may not yet be running.
270 * Defer processing channel callbacks until the daemon
271 * has registered.
273 vss_transaction.active = true;
274 return 0;
277 void hv_vss_deinit(void)
279 cn_del_callback(&vss_id);
280 cancel_work_sync(&vss_send_op_work);