vhost: fix code style
[dpdk.git] / lib / librte_vhost / eventfd_link / eventfd_link.c
1 /*-
2  * GPL LICENSE SUMMARY
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of version 2 of the GNU General Public License as
8  *   published by the Free Software Foundation.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  *   WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *   General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *   The full GNU General Public License is included in this distribution
19  *   in the file called LICENSE.GPL.
20  *
21  *   Contact Information:
22  *   Intel Corporation
23  */
24
25 #include <linux/eventfd.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/rcupdate.h>
30 #include <linux/file.h>
31 #include <linux/slab.h>
32 #include <linux/fs.h>
33 #include <linux/mmu_context.h>
34 #include <linux/sched.h>
35 #include <asm/mmu_context.h>
36 #include <linux/fdtable.h>
37
38 #include "eventfd_link.h"
39
40
41 /*
42  * get_files_struct is copied from fs/file.c
43  */
44 struct files_struct *
45 get_files_struct(struct task_struct *task)
46 {
47         struct files_struct *files;
48
49         task_lock(task);
50         files = task->files;
51         if (files)
52                 atomic_inc(&files->count);
53         task_unlock(task);
54
55         return files;
56 }
57
58 /*
59  * put_files_struct is extracted from fs/file.c
60  */
61 void
62 put_files_struct(struct files_struct *files)
63 {
64         if (atomic_dec_and_test(&files->count))
65                 BUG();
66 }
67
68
69 static long
70 eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
71 {
72         void __user *argp = (void __user *) arg;
73         struct task_struct *task_target = NULL;
74         struct file *file;
75         struct files_struct *files;
76         struct fdtable *fdt;
77         struct eventfd_copy eventfd_copy;
78
79         switch (ioctl) {
80         case EVENTFD_COPY:
81                 if (copy_from_user(&eventfd_copy, argp,
82                         sizeof(struct eventfd_copy)))
83                         return -EFAULT;
84
85                 /*
86                  * Find the task struct for the target pid
87                  */
88                 task_target =
89                         pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
90                 if (task_target == NULL) {
91                         pr_debug("Failed to get mem ctx for target pid\n");
92                         return -EFAULT;
93                 }
94
95                 files = get_files_struct(current);
96                 if (files == NULL) {
97                         pr_debug("Failed to get files struct\n");
98                         return -EFAULT;
99                 }
100
101                 rcu_read_lock();
102                 file = fcheck_files(files, eventfd_copy.source_fd);
103                 if (file) {
104                         if (file->f_mode & FMODE_PATH ||
105                                 !atomic_long_inc_not_zero(&file->f_count))
106                                 file = NULL;
107                 }
108                 rcu_read_unlock();
109                 put_files_struct(files);
110
111                 if (file == NULL) {
112                         pr_debug("Failed to get file from source pid\n");
113                         return 0;
114                 }
115
116                 /*
117                  * Release the existing eventfd in the source process
118                  */
119                 spin_lock(&files->file_lock);
120                 filp_close(file, files);
121                 fdt = files_fdtable(files);
122                 fdt->fd[eventfd_copy.source_fd] = NULL;
123                 spin_unlock(&files->file_lock);
124
125                 /*
126                  * Find the file struct associated with the target fd.
127                  */
128
129                 files = get_files_struct(task_target);
130                 if (files == NULL) {
131                         pr_debug("Failed to get files struct\n");
132                         return -EFAULT;
133                 }
134
135                 rcu_read_lock();
136                 file = fcheck_files(files, eventfd_copy.target_fd);
137                 if (file) {
138                         if (file->f_mode & FMODE_PATH ||
139                                 !atomic_long_inc_not_zero(&file->f_count))
140                                         file = NULL;
141                 }
142                 rcu_read_unlock();
143                 put_files_struct(files);
144
145                 if (file == NULL) {
146                         pr_debug("Failed to get file from target pid\n");
147                         return 0;
148                 }
149
150                 /*
151                  * Install the file struct from the target process into the
152                  * file desciptor of the source process,
153                  */
154
155                 fd_install(eventfd_copy.source_fd, file);
156
157                 return 0;
158
159         default:
160                 return -ENOIOCTLCMD;
161         }
162 }
163
164 static const struct file_operations eventfd_link_fops = {
165         .owner = THIS_MODULE,
166         .unlocked_ioctl = eventfd_link_ioctl,
167 };
168
169
170 static struct miscdevice eventfd_link_misc = {
171         .name = "eventfd-link",
172         .fops = &eventfd_link_fops,
173 };
174
175 static int __init
176 eventfd_link_init(void)
177 {
178         return misc_register(&eventfd_link_misc);
179 }
180
181 module_init(eventfd_link_init);
182
183 static void __exit
184 eventfd_link_exit(void)
185 {
186         misc_deregister(&eventfd_link_misc);
187 }
188
189 module_exit(eventfd_link_exit);
190
191 MODULE_VERSION("0.0.1");
192 MODULE_LICENSE("GPL v2");
193 MODULE_AUTHOR("Anthony Fee");
194 MODULE_DESCRIPTION("Link eventfd");
195 MODULE_ALIAS("devname:eventfd-link");