kni: fix build on SLES 12
[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                 fput(file);
121                 filp_close(file, files);
122                 fdt = files_fdtable(files);
123                 fdt->fd[eventfd_copy.source_fd] = NULL;
124                 spin_unlock(&files->file_lock);
125
126                 /*
127                  * Find the file struct associated with the target fd.
128                  */
129
130                 files = get_files_struct(task_target);
131                 if (files == NULL) {
132                         pr_debug("Failed to get files struct\n");
133                         return -EFAULT;
134                 }
135
136                 rcu_read_lock();
137                 file = fcheck_files(files, eventfd_copy.target_fd);
138                 if (file) {
139                         if (file->f_mode & FMODE_PATH ||
140                                 !atomic_long_inc_not_zero(&file->f_count))
141                                         file = NULL;
142                 }
143                 rcu_read_unlock();
144                 put_files_struct(files);
145
146                 if (file == NULL) {
147                         pr_debug("Failed to get file from target pid\n");
148                         return 0;
149                 }
150
151                 /*
152                  * Install the file struct from the target process into the
153                  * file desciptor of the source process,
154                  */
155
156                 fd_install(eventfd_copy.source_fd, file);
157
158                 return 0;
159
160         default:
161                 return -ENOIOCTLCMD;
162         }
163 }
164
165 static const struct file_operations eventfd_link_fops = {
166         .owner = THIS_MODULE,
167         .unlocked_ioctl = eventfd_link_ioctl,
168 };
169
170
171 static struct miscdevice eventfd_link_misc = {
172         .name = "eventfd-link",
173         .fops = &eventfd_link_fops,
174 };
175
176 static int __init
177 eventfd_link_init(void)
178 {
179         return misc_register(&eventfd_link_misc);
180 }
181
182 module_init(eventfd_link_init);
183
184 static void __exit
185 eventfd_link_exit(void)
186 {
187         misc_deregister(&eventfd_link_misc);
188 }
189
190 module_exit(eventfd_link_exit);
191
192 MODULE_VERSION("0.0.1");
193 MODULE_LICENSE("GPL v2");
194 MODULE_AUTHOR("Anthony Fee");
195 MODULE_DESCRIPTION("Link eventfd");
196 MODULE_ALIAS("devname:eventfd-link");