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_timeout_func(struct work_struct
*dummy
);
79 static DECLARE_DELAYED_WORK(fcopy_timeout_work
, fcopy_timeout_func
);
80 static u8
*recv_buffer
;
82 static void fcopy_timeout_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
))
101 hv_poll_channel(fcopy_transaction
.fcopy_context
,
102 hv_fcopy_onchannelcallback
);
105 static int fcopy_handle_handshake(u32 version
)
108 case FCOPY_CURRENT_VERSION
:
112 * For now we will fail the registration.
113 * If and when we have multiple versions to
114 * deal with, we will be backward compatible.
115 * We will add this code when needed.
119 pr_info("FCP: user-mode registering done. Daemon version: %d\n",
121 fcopy_transaction
.active
= false;
122 hv_poll_channel(fcopy_transaction
.fcopy_context
,
123 hv_fcopy_onchannelcallback
);
124 in_hand_shake
= false;
128 static void fcopy_send_data(void)
130 struct hv_start_fcopy
*smsg_out
= &fcopy_transaction
.message
;
131 int operation
= fcopy_transaction
.fcopy_msg
->operation
;
132 struct hv_start_fcopy
*smsg_in
;
135 * The strings sent from the host are encoded in
136 * in utf16; convert it to utf8 strings.
137 * The host assures us that the utf16 strings will not exceed
138 * the max lengths specified. We will however, reserve room
139 * for the string terminating character - in the utf16s_utf8s()
140 * function we limit the size of the buffer where the converted
141 * string is placed to W_MAX_PATH -1 to guarantee
142 * that the strings can be properly terminated!
146 case START_FILE_COPY
:
147 memset(smsg_out
, 0, sizeof(struct hv_start_fcopy
));
148 smsg_out
->hdr
.operation
= operation
;
149 smsg_in
= (struct hv_start_fcopy
*)fcopy_transaction
.fcopy_msg
;
151 utf16s_to_utf8s((wchar_t *)smsg_in
->file_name
, W_MAX_PATH
,
153 (__u8
*)smsg_out
->file_name
, W_MAX_PATH
- 1);
155 utf16s_to_utf8s((wchar_t *)smsg_in
->path_name
, W_MAX_PATH
,
157 (__u8
*)smsg_out
->path_name
, W_MAX_PATH
- 1);
159 smsg_out
->copy_flags
= smsg_in
->copy_flags
;
160 smsg_out
->file_size
= smsg_in
->file_size
;
166 up(&fcopy_transaction
.read_sema
);
171 * Send a response back to the host.
175 fcopy_respond_to_host(int error
)
177 struct icmsg_hdr
*icmsghdr
;
179 struct vmbus_channel
*channel
;
183 * Copy the global state for completing the transaction. Note that
184 * only one transaction can be active at a time. This is guaranteed
185 * by the file copy protocol implemented by the host. Furthermore,
186 * the "transaction active" state we maintain ensures that there can
187 * only be one active transaction at a time.
190 buf_len
= fcopy_transaction
.recv_len
;
191 channel
= fcopy_transaction
.recv_channel
;
192 req_id
= fcopy_transaction
.recv_req_id
;
194 fcopy_transaction
.active
= false;
196 icmsghdr
= (struct icmsg_hdr
*)
197 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
199 if (channel
->onchannel_callback
== NULL
)
201 * We have raced with util driver being unloaded;
206 icmsghdr
->status
= error
;
207 icmsghdr
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
208 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
209 VM_PKT_DATA_INBAND
, 0);
212 void hv_fcopy_onchannelcallback(void *context
)
214 struct vmbus_channel
*channel
= context
;
217 struct hv_fcopy_hdr
*fcopy_msg
;
218 struct icmsg_hdr
*icmsghdr
;
219 struct icmsg_negotiate
*negop
= NULL
;
221 int fcopy_srv_version
;
223 if (fcopy_transaction
.active
) {
225 * We will defer processing this callback once
226 * the current transaction is complete.
228 fcopy_transaction
.fcopy_context
= context
;
231 fcopy_transaction
.fcopy_context
= NULL
;
233 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 2, &recvlen
,
238 icmsghdr
= (struct icmsg_hdr
*)&recv_buffer
[
239 sizeof(struct vmbuspipe_hdr
)];
240 if (icmsghdr
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
241 util_fw_version
= UTIL_FW_VERSION
;
242 fcopy_srv_version
= WIN8_SRV_VERSION
;
243 vmbus_prep_negotiate_resp(icmsghdr
, negop
, recv_buffer
,
244 util_fw_version
, fcopy_srv_version
);
246 fcopy_msg
= (struct hv_fcopy_hdr
*)&recv_buffer
[
247 sizeof(struct vmbuspipe_hdr
) +
248 sizeof(struct icmsg_hdr
)];
251 * Stash away this global state for completing the
252 * transaction; note transactions are serialized.
255 fcopy_transaction
.active
= true;
256 fcopy_transaction
.recv_len
= recvlen
;
257 fcopy_transaction
.recv_channel
= channel
;
258 fcopy_transaction
.recv_req_id
= requestid
;
259 fcopy_transaction
.fcopy_msg
= fcopy_msg
;
262 * Send the information to the user-level daemon.
264 schedule_delayed_work(&fcopy_timeout_work
, 5*HZ
);
268 icmsghdr
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
269 vmbus_sendpacket(channel
, recv_buffer
, recvlen
, requestid
,
270 VM_PKT_DATA_INBAND
, 0);
274 * Create a char device that can support read/write for passing
278 static ssize_t
fcopy_read(struct file
*file
, char __user
*buf
,
279 size_t count
, loff_t
*ppos
)
286 * Wait until there is something to be read.
288 if (down_interruptible(&fcopy_transaction
.read_sema
))
292 * The channel may be rescinded and in this case, we will wakeup the
293 * the thread blocked on the semaphore and we will use the opened
294 * state to correctly handle this case.
299 operation
= fcopy_transaction
.fcopy_msg
->operation
;
301 if (operation
== START_FILE_COPY
) {
302 src
= &fcopy_transaction
.message
;
303 copy_size
= sizeof(struct hv_start_fcopy
);
304 if (count
< copy_size
)
307 src
= fcopy_transaction
.fcopy_msg
;
308 copy_size
= sizeof(struct hv_do_fcopy
);
309 if (count
< copy_size
)
312 if (copy_to_user(buf
, src
, copy_size
))
318 static ssize_t
fcopy_write(struct file
*file
, const char __user
*buf
,
319 size_t count
, loff_t
*ppos
)
323 if (count
!= sizeof(int))
326 if (copy_from_user(&response
, buf
, sizeof(int)))
330 if (fcopy_handle_handshake(response
))
336 * Complete the transaction by forwarding the result
337 * to the host. But first, cancel the timeout.
339 if (cancel_delayed_work_sync(&fcopy_timeout_work
))
340 fcopy_respond_to_host(response
);
341 hv_poll_channel(fcopy_transaction
.fcopy_context
,
342 hv_fcopy_onchannelcallback
);
348 static int fcopy_open(struct inode
*inode
, struct file
*f
)
351 * The user level daemon that will open this device is
352 * really an extension of this driver. We can have only
353 * active open at a time.
359 * The daemon is alive; setup the state.
365 /* XXX: there are still some tricky corner cases, e.g.,
366 * 1) In a SMP guest, when fcopy_release() runs between
367 * schedule_delayed_work() and fcopy_send_data(), there is
368 * still a chance an obsolete message will be queued.
370 * 2) When the fcopy daemon is running, if we unload the driver,
371 * we'll notice a kernel oops when we kill the daemon later.
373 static int fcopy_release(struct inode
*inode
, struct file
*f
)
376 * The daemon has exited; reset the state.
378 in_hand_shake
= true;
381 if (cancel_delayed_work_sync(&fcopy_timeout_work
)) {
382 /* We haven't up()-ed the semaphore(very rare)? */
383 if (down_trylock(&fcopy_transaction
.read_sema
))
385 fcopy_respond_to_host(HV_E_FAIL
);
391 static const struct file_operations fcopy_fops
= {
393 .write
= fcopy_write
,
394 .release
= fcopy_release
,
398 static struct miscdevice fcopy_misc
= {
399 .minor
= MISC_DYNAMIC_MINOR
,
400 .name
= "vmbus/hv_fcopy",
404 static int fcopy_dev_init(void)
406 return misc_register(&fcopy_misc
);
409 static void fcopy_dev_deinit(void)
413 * The device is going away - perhaps because the
414 * host has rescinded the channel. Setup state so that
415 * user level daemon can gracefully exit if it is blocked
416 * on the read semaphore.
420 * Signal the semaphore as the device is
423 up(&fcopy_transaction
.read_sema
);
424 misc_deregister(&fcopy_misc
);
427 int hv_fcopy_init(struct hv_util_service
*srv
)
429 recv_buffer
= srv
->recv_buffer
;
432 * When this driver loads, the user level daemon that
433 * processes the host requests may not yet be running.
434 * Defer processing channel callbacks until the daemon
437 fcopy_transaction
.active
= true;
438 sema_init(&fcopy_transaction
.read_sema
, 0);
440 return fcopy_dev_init();
443 void hv_fcopy_deinit(void)
445 cancel_delayed_work_sync(&fcopy_timeout_work
);