Drivers: hv: fcopy: rename fcopy_work -> fcopy_timeout_work
[firefly-linux-kernel-4.4.55.git] / drivers / hv / hv_fcopy.c
1 /*
2  * An implementation of file copy service.
3  *
4  * Copyright (C) 2014, Microsoft, Inc.
5  *
6  * Author : K. Y. Srinivasan <ksrinivasan@novell.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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/semaphore.h>
23 #include <linux/fs.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>
31
32 #include "hyperv_vmbus.h"
33
34 #define WIN8_SRV_MAJOR          1
35 #define WIN8_SRV_MINOR          1
36 #define WIN8_SRV_VERSION        (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
37
38 /*
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.
45  *
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
49  * handled.
50  *
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.
55  */
56
57 static struct {
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;
66 } fcopy_transaction;
67
68 static bool opened; /* currently device opened */
69
70 /*
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.
74  */
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;
81
82 static void fcopy_timeout_func(struct work_struct *dummy)
83 {
84         /*
85          * If the timer fires, the user-mode component has not responded;
86          * process the pending transaction.
87          */
88         fcopy_respond_to_host(HV_E_FAIL);
89
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.
94          *
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.
97          */
98         if (down_trylock(&fcopy_transaction.read_sema))
99                 ;
100
101         hv_poll_channel(fcopy_transaction.fcopy_context,
102                         hv_fcopy_onchannelcallback);
103 }
104
105 static int fcopy_handle_handshake(u32 version)
106 {
107         switch (version) {
108         case FCOPY_CURRENT_VERSION:
109                 break;
110         default:
111                 /*
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.
116                  */
117                 return -EINVAL;
118         }
119         pr_info("FCP: user-mode registering done. Daemon version: %d\n",
120                 version);
121         fcopy_transaction.active = false;
122         hv_poll_channel(fcopy_transaction.fcopy_context,
123                         hv_fcopy_onchannelcallback);
124         in_hand_shake = false;
125         return 0;
126 }
127
128 static void fcopy_send_data(void)
129 {
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;
133
134         /*
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!
143          */
144
145         switch (operation) {
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;
150
151                 utf16s_to_utf8s((wchar_t *)smsg_in->file_name, W_MAX_PATH,
152                                 UTF16_LITTLE_ENDIAN,
153                                 (__u8 *)smsg_out->file_name, W_MAX_PATH - 1);
154
155                 utf16s_to_utf8s((wchar_t *)smsg_in->path_name, W_MAX_PATH,
156                                 UTF16_LITTLE_ENDIAN,
157                                 (__u8 *)smsg_out->path_name, W_MAX_PATH - 1);
158
159                 smsg_out->copy_flags = smsg_in->copy_flags;
160                 smsg_out->file_size = smsg_in->file_size;
161                 break;
162
163         default:
164                 break;
165         }
166         up(&fcopy_transaction.read_sema);
167         return;
168 }
169
170 /*
171  * Send a response back to the host.
172  */
173
174 static void
175 fcopy_respond_to_host(int error)
176 {
177         struct icmsg_hdr *icmsghdr;
178         u32 buf_len;
179         struct vmbus_channel *channel;
180         u64 req_id;
181
182         /*
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.
188          */
189
190         buf_len = fcopy_transaction.recv_len;
191         channel = fcopy_transaction.recv_channel;
192         req_id = fcopy_transaction.recv_req_id;
193
194         fcopy_transaction.active = false;
195
196         icmsghdr = (struct icmsg_hdr *)
197                         &recv_buffer[sizeof(struct vmbuspipe_hdr)];
198
199         if (channel->onchannel_callback == NULL)
200                 /*
201                  * We have raced with util driver being unloaded;
202                  * silently return.
203                  */
204                 return;
205
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);
210 }
211
212 void hv_fcopy_onchannelcallback(void *context)
213 {
214         struct vmbus_channel *channel = context;
215         u32 recvlen;
216         u64 requestid;
217         struct hv_fcopy_hdr *fcopy_msg;
218         struct icmsg_hdr *icmsghdr;
219         struct icmsg_negotiate *negop = NULL;
220         int util_fw_version;
221         int fcopy_srv_version;
222
223         if (fcopy_transaction.active) {
224                 /*
225                  * We will defer processing this callback once
226                  * the current transaction is complete.
227                  */
228                 fcopy_transaction.fcopy_context = context;
229                 return;
230         }
231         fcopy_transaction.fcopy_context = NULL;
232
233         vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
234                          &requestid);
235         if (recvlen <= 0)
236                 return;
237
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);
245         } else {
246                 fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[
247                                 sizeof(struct vmbuspipe_hdr) +
248                                 sizeof(struct icmsg_hdr)];
249
250                 /*
251                  * Stash away this global state for completing the
252                  * transaction; note transactions are serialized.
253                  */
254
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;
260
261                 /*
262                  * Send the information to the user-level daemon.
263                  */
264                 schedule_delayed_work(&fcopy_timeout_work, 5*HZ);
265                 fcopy_send_data();
266                 return;
267         }
268         icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
269         vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
270                         VM_PKT_DATA_INBAND, 0);
271 }
272
273 /*
274  * Create a char device that can support read/write for passing
275  * the payload.
276  */
277
278 static ssize_t fcopy_read(struct file *file, char __user *buf,
279                 size_t count, loff_t *ppos)
280 {
281         void *src;
282         size_t copy_size;
283         int operation;
284
285         /*
286          * Wait until there is something to be read.
287          */
288         if (down_interruptible(&fcopy_transaction.read_sema))
289                 return -EINTR;
290
291         /*
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.
295          */
296         if (!opened)
297                 return -ENODEV;
298
299         operation = fcopy_transaction.fcopy_msg->operation;
300
301         if (operation == START_FILE_COPY) {
302                 src = &fcopy_transaction.message;
303                 copy_size = sizeof(struct hv_start_fcopy);
304                 if (count < copy_size)
305                         return 0;
306         } else {
307                 src = fcopy_transaction.fcopy_msg;
308                 copy_size = sizeof(struct hv_do_fcopy);
309                 if (count < copy_size)
310                         return 0;
311         }
312         if (copy_to_user(buf, src, copy_size))
313                 return -EFAULT;
314
315         return copy_size;
316 }
317
318 static ssize_t fcopy_write(struct file *file, const char __user *buf,
319                         size_t count, loff_t *ppos)
320 {
321         int response = 0;
322
323         if (count != sizeof(int))
324                 return -EINVAL;
325
326         if (copy_from_user(&response, buf, sizeof(int)))
327                 return -EFAULT;
328
329         if (in_hand_shake) {
330                 if (fcopy_handle_handshake(response))
331                         return -EINVAL;
332                 return sizeof(int);
333         }
334
335         /*
336          * Complete the transaction by forwarding the result
337          * to the host. But first, cancel the timeout.
338          */
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);
343         }
344
345         return sizeof(int);
346 }
347
348 static int fcopy_open(struct inode *inode, struct file *f)
349 {
350         /*
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.
354          */
355         if (opened)
356                 return -EBUSY;
357
358         /*
359          * The daemon is alive; setup the state.
360          */
361         opened = true;
362         return 0;
363 }
364
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.
369  *
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.
372  */
373 static int fcopy_release(struct inode *inode, struct file *f)
374 {
375         /*
376          * The daemon has exited; reset the state.
377          */
378         in_hand_shake = true;
379         opened = false;
380
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))
384                         ;
385                 fcopy_respond_to_host(HV_E_FAIL);
386         }
387         return 0;
388 }
389
390
391 static const struct file_operations fcopy_fops = {
392         .read           = fcopy_read,
393         .write          = fcopy_write,
394         .release        = fcopy_release,
395         .open           = fcopy_open,
396 };
397
398 static struct miscdevice fcopy_misc = {
399         .minor          = MISC_DYNAMIC_MINOR,
400         .name           = "vmbus/hv_fcopy",
401         .fops           = &fcopy_fops,
402 };
403
404 static int fcopy_dev_init(void)
405 {
406         return misc_register(&fcopy_misc);
407 }
408
409 static void fcopy_dev_deinit(void)
410 {
411
412         /*
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.
417          */
418         opened = false;
419         /*
420          * Signal the semaphore as the device is
421          * going away.
422          */
423         up(&fcopy_transaction.read_sema);
424         misc_deregister(&fcopy_misc);
425 }
426
427 int hv_fcopy_init(struct hv_util_service *srv)
428 {
429         recv_buffer = srv->recv_buffer;
430
431         /*
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
435          * has registered.
436          */
437         fcopy_transaction.active = true;
438         sema_init(&fcopy_transaction.read_sema, 0);
439
440         return fcopy_dev_init();
441 }
442
443 void hv_fcopy_deinit(void)
444 {
445         cancel_delayed_work_sync(&fcopy_timeout_work);
446         fcopy_dev_deinit();
447 }