2 * An implementation of file copy service.
4 * Copyright (C) 2014, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/semaphore.h>
24 #include <linux/nls.h>
25 #include <linux/workqueue.h>
26 #include <linux/cdev.h>
27 #include <linux/hyperv.h>
28 #include <linux/sched.h>
29 #include <linux/uaccess.h>
30 #include <linux/miscdevice.h>
32 #include "hyperv_vmbus.h"
34 #define WIN8_SRV_MAJOR 1
35 #define WIN8_SRV_MINOR 1
36 #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
39 * Global state maintained for transaction that is being processed.
40 * For a class of integration services, including the "file copy service",
41 * the specified protocol is a "request/response" protocol which means that
42 * there can only be single outstanding transaction from the host at any
43 * given point in time. We use this to simplify memory management in this
44 * driver - we cache and process only one message at a time.
46 * While the request/response protocol is guaranteed by the host, we further
47 * ensure this by serializing packet processing in this driver - we do not
48 * read additional packets from the VMBUs until the current packet is fully
51 * The transaction "active" state is set when we receive a request from the
52 * host and we cleanup this state when the transaction is completed - when we
53 * respond to the host with our response. When the transaction active state is
54 * set, we defer handling incoming packets.
58 bool active
; /* transaction status - active or not */
59 int recv_len
; /* number of bytes received. */
60 struct hv_fcopy_hdr
*fcopy_msg
; /* current message */
61 struct hv_start_fcopy message
; /* sent to daemon */
62 struct vmbus_channel
*recv_channel
; /* chn we got the request */
63 u64 recv_req_id
; /* request ID. */
64 void *fcopy_context
; /* for the channel callback */
65 struct semaphore read_sema
;
68 static bool opened
; /* currently device opened */
71 * Before we can accept copy messages from the host, we need
72 * to handshake with the user level daemon. This state tracks
73 * if we are in the handshake phase.
75 static bool in_hand_shake
= true;
76 static void fcopy_send_data(void);
77 static void fcopy_respond_to_host(int error
);
78 static void fcopy_work_func(struct work_struct
*dummy
);
79 static DECLARE_DELAYED_WORK(fcopy_work
, fcopy_work_func
);
80 static u8
*recv_buffer
;
82 static void fcopy_work_func(struct work_struct
*dummy
)
85 * If the timer fires, the user-mode component has not responded;
86 * process the pending transaction.
88 fcopy_respond_to_host(HV_E_FAIL
);
90 /* In the case the user-space daemon crashes, hangs or is killed, we
91 * need to down the semaphore, otherwise, after the daemon starts next
92 * time, the obsolete data in fcopy_transaction.message or
93 * fcopy_transaction.fcopy_msg will be used immediately.
95 * NOTE: fcopy_read() happens to get the semaphore (very rare)? We're
96 * still OK, because we've reported the failure to the host.
98 if (down_trylock(&fcopy_transaction
.read_sema
))
103 static int fcopy_handle_handshake(u32 version
)
106 case FCOPY_CURRENT_VERSION
:
110 * For now we will fail the registration.
111 * If and when we have multiple versions to
112 * deal with, we will be backward compatible.
113 * We will add this code when needed.
117 pr_info("FCP: user-mode registering done. Daemon version: %d\n",
119 fcopy_transaction
.active
= false;
120 if (fcopy_transaction
.fcopy_context
)
121 hv_fcopy_onchannelcallback(fcopy_transaction
.fcopy_context
);
122 in_hand_shake
= false;
126 static void fcopy_send_data(void)
128 struct hv_start_fcopy
*smsg_out
= &fcopy_transaction
.message
;
129 int operation
= fcopy_transaction
.fcopy_msg
->operation
;
130 struct hv_start_fcopy
*smsg_in
;
133 * The strings sent from the host are encoded in
134 * in utf16; convert it to utf8 strings.
135 * The host assures us that the utf16 strings will not exceed
136 * the max lengths specified. We will however, reserve room
137 * for the string terminating character - in the utf16s_utf8s()
138 * function we limit the size of the buffer where the converted
139 * string is placed to W_MAX_PATH -1 to guarantee
140 * that the strings can be properly terminated!
144 case START_FILE_COPY
:
145 memset(smsg_out
, 0, sizeof(struct hv_start_fcopy
));
146 smsg_out
->hdr
.operation
= operation
;
147 smsg_in
= (struct hv_start_fcopy
*)fcopy_transaction
.fcopy_msg
;
149 utf16s_to_utf8s((wchar_t *)smsg_in
->file_name
, W_MAX_PATH
,
151 (__u8
*)smsg_out
->file_name
, W_MAX_PATH
- 1);
153 utf16s_to_utf8s((wchar_t *)smsg_in
->path_name
, W_MAX_PATH
,
155 (__u8
*)smsg_out
->path_name
, W_MAX_PATH
- 1);
157 smsg_out
->copy_flags
= smsg_in
->copy_flags
;
158 smsg_out
->file_size
= smsg_in
->file_size
;
164 up(&fcopy_transaction
.read_sema
);
169 * Send a response back to the host.
173 fcopy_respond_to_host(int error
)
175 struct icmsg_hdr
*icmsghdr
;
177 struct vmbus_channel
*channel
;
181 * Copy the global state for completing the transaction. Note that
182 * only one transaction can be active at a time. This is guaranteed
183 * by the file copy protocol implemented by the host. Furthermore,
184 * the "transaction active" state we maintain ensures that there can
185 * only be one active transaction at a time.
188 buf_len
= fcopy_transaction
.recv_len
;
189 channel
= fcopy_transaction
.recv_channel
;
190 req_id
= fcopy_transaction
.recv_req_id
;
192 fcopy_transaction
.active
= false;
194 icmsghdr
= (struct icmsg_hdr
*)
195 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
197 if (channel
->onchannel_callback
== NULL
)
199 * We have raced with util driver being unloaded;
204 icmsghdr
->status
= error
;
205 icmsghdr
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
206 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
207 VM_PKT_DATA_INBAND
, 0);
210 void hv_fcopy_onchannelcallback(void *context
)
212 struct vmbus_channel
*channel
= context
;
215 struct hv_fcopy_hdr
*fcopy_msg
;
216 struct icmsg_hdr
*icmsghdr
;
217 struct icmsg_negotiate
*negop
= NULL
;
219 int fcopy_srv_version
;
221 if (fcopy_transaction
.active
) {
223 * We will defer processing this callback once
224 * the current transaction is complete.
226 fcopy_transaction
.fcopy_context
= context
;
230 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 2, &recvlen
,
235 icmsghdr
= (struct icmsg_hdr
*)&recv_buffer
[
236 sizeof(struct vmbuspipe_hdr
)];
237 if (icmsghdr
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
238 util_fw_version
= UTIL_FW_VERSION
;
239 fcopy_srv_version
= WIN8_SRV_VERSION
;
240 vmbus_prep_negotiate_resp(icmsghdr
, negop
, recv_buffer
,
241 util_fw_version
, fcopy_srv_version
);
243 fcopy_msg
= (struct hv_fcopy_hdr
*)&recv_buffer
[
244 sizeof(struct vmbuspipe_hdr
) +
245 sizeof(struct icmsg_hdr
)];
248 * Stash away this global state for completing the
249 * transaction; note transactions are serialized.
252 fcopy_transaction
.active
= true;
253 fcopy_transaction
.recv_len
= recvlen
;
254 fcopy_transaction
.recv_channel
= channel
;
255 fcopy_transaction
.recv_req_id
= requestid
;
256 fcopy_transaction
.fcopy_msg
= fcopy_msg
;
259 * Send the information to the user-level daemon.
261 schedule_delayed_work(&fcopy_work
, 5*HZ
);
265 icmsghdr
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
266 vmbus_sendpacket(channel
, recv_buffer
, recvlen
, requestid
,
267 VM_PKT_DATA_INBAND
, 0);
271 * Create a char device that can support read/write for passing
275 static ssize_t
fcopy_read(struct file
*file
, char __user
*buf
,
276 size_t count
, loff_t
*ppos
)
283 * Wait until there is something to be read.
285 if (down_interruptible(&fcopy_transaction
.read_sema
))
289 * The channel may be rescinded and in this case, we will wakeup the
290 * the thread blocked on the semaphore and we will use the opened
291 * state to correctly handle this case.
296 operation
= fcopy_transaction
.fcopy_msg
->operation
;
298 if (operation
== START_FILE_COPY
) {
299 src
= &fcopy_transaction
.message
;
300 copy_size
= sizeof(struct hv_start_fcopy
);
301 if (count
< copy_size
)
304 src
= fcopy_transaction
.fcopy_msg
;
305 copy_size
= sizeof(struct hv_do_fcopy
);
306 if (count
< copy_size
)
309 if (copy_to_user(buf
, src
, copy_size
))
315 static ssize_t
fcopy_write(struct file
*file
, const char __user
*buf
,
316 size_t count
, loff_t
*ppos
)
320 if (count
!= sizeof(int))
323 if (copy_from_user(&response
, buf
, sizeof(int)))
327 if (fcopy_handle_handshake(response
))
333 * Complete the transaction by forwarding the result
334 * to the host. But first, cancel the timeout.
336 if (cancel_delayed_work_sync(&fcopy_work
))
337 fcopy_respond_to_host(response
);
342 static int fcopy_open(struct inode
*inode
, struct file
*f
)
345 * The user level daemon that will open this device is
346 * really an extension of this driver. We can have only
347 * active open at a time.
353 * The daemon is alive; setup the state.
359 /* XXX: there are still some tricky corner cases, e.g.,
360 * 1) In a SMP guest, when fcopy_release() runs between
361 * schedule_delayed_work() and fcopy_send_data(), there is
362 * still a chance an obsolete message will be queued.
364 * 2) When the fcopy daemon is running, if we unload the driver,
365 * we'll notice a kernel oops when we kill the daemon later.
367 static int fcopy_release(struct inode
*inode
, struct file
*f
)
370 * The daemon has exited; reset the state.
372 in_hand_shake
= true;
375 if (cancel_delayed_work_sync(&fcopy_work
)) {
376 /* We haven't up()-ed the semaphore(very rare)? */
377 if (down_trylock(&fcopy_transaction
.read_sema
))
379 fcopy_respond_to_host(HV_E_FAIL
);
385 static const struct file_operations fcopy_fops
= {
387 .write
= fcopy_write
,
388 .release
= fcopy_release
,
392 static struct miscdevice fcopy_misc
= {
393 .minor
= MISC_DYNAMIC_MINOR
,
394 .name
= "vmbus/hv_fcopy",
398 static int fcopy_dev_init(void)
400 return misc_register(&fcopy_misc
);
403 static void fcopy_dev_deinit(void)
407 * The device is going away - perhaps because the
408 * host has rescinded the channel. Setup state so that
409 * user level daemon can gracefully exit if it is blocked
410 * on the read semaphore.
414 * Signal the semaphore as the device is
417 up(&fcopy_transaction
.read_sema
);
418 misc_deregister(&fcopy_misc
);
421 int hv_fcopy_init(struct hv_util_service
*srv
)
423 recv_buffer
= srv
->recv_buffer
;
426 * When this driver loads, the user level daemon that
427 * processes the host requests may not yet be running.
428 * Defer processing channel callbacks until the daemon
431 fcopy_transaction
.active
= true;
432 sema_init(&fcopy_transaction
.read_sema
, 0);
434 return fcopy_dev_init();
437 void hv_fcopy_deinit(void)
439 cancel_delayed_work_sync(&fcopy_work
);