7cbebd47fcf8bd7f6b80001ed3969229a53b9f23
[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/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/file.h>
28 #include <linux/fdtable.h>
29 #include <linux/syscalls.h>
30
31 #include "eventfd_link.h"
32
33
34 /*
35  * get_files_struct is copied from fs/file.c
36  */
37 struct files_struct *
38 get_files_struct(struct task_struct *task)
39 {
40         struct files_struct *files;
41
42         task_lock(task);
43         files = task->files;
44         if (files)
45                 atomic_inc(&files->count);
46         task_unlock(task);
47
48         return files;
49 }
50
51 /*
52  * put_files_struct is extracted from fs/file.c
53  */
54 void
55 put_files_struct(struct files_struct *files)
56 {
57         if (atomic_dec_and_test(&files->count))
58                 BUG();
59 }
60
61 static struct file *
62 fget_from_files(struct files_struct *files, unsigned fd)
63 {
64         struct file *file;
65
66         rcu_read_lock();
67         file = fcheck_files(files, fd);
68         if (file) {
69                 if (file->f_mode & FMODE_PATH ||
70                         !atomic_long_inc_not_zero(&file->f_count)) {
71
72                         file = NULL;
73                 }
74         }
75         rcu_read_unlock();
76
77         return file;
78 }
79
80 static long
81 eventfd_link_ioctl_copy(unsigned long arg)
82 {
83         void __user *argp = (void __user *) arg;
84         struct task_struct *task_target = NULL;
85         struct file *file;
86         struct files_struct *files;
87         struct fdtable *fdt;
88         struct eventfd_copy eventfd_copy;
89         long ret = -EFAULT;
90
91         if (copy_from_user(&eventfd_copy, argp, sizeof(struct eventfd_copy)))
92                 goto out;
93
94         /*
95          * Find the task struct for the target pid
96          */
97         ret = -ESRCH;
98
99         task_target =
100                 get_pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
101         if (task_target == NULL) {
102                 pr_info("Unable to find pid %d\n", eventfd_copy.target_pid);
103                 goto out;
104         }
105
106         ret = -ESTALE;
107         files = get_files_struct(current);
108         if (files == NULL) {
109                 pr_info("Failed to get current files struct\n");
110                 goto out_task;
111         }
112
113         ret = -EBADF;
114         file = fget_from_files(files, eventfd_copy.source_fd);
115
116         if (file == NULL) {
117                 pr_info("Failed to get fd %d from source\n",
118                         eventfd_copy.source_fd);
119                 put_files_struct(files);
120                 goto out_task;
121         }
122
123         /*
124          * Release the existing eventfd in the source process
125          */
126         spin_lock(&files->file_lock);
127         fput(file);
128         filp_close(file, files);
129         fdt = files_fdtable(files);
130         fdt->fd[eventfd_copy.source_fd] = NULL;
131         spin_unlock(&files->file_lock);
132
133         put_files_struct(files);
134
135         /*
136          * Find the file struct associated with the target fd.
137          */
138
139         ret = -ESTALE;
140         files = get_files_struct(task_target);
141         if (files == NULL) {
142                 pr_info("Failed to get target files struct\n");
143                 goto out_task;
144         }
145
146         ret = -EBADF;
147         file = fget_from_files(files, eventfd_copy.target_fd);
148         put_files_struct(files);
149
150         if (file == NULL) {
151                 pr_info("Failed to get fd %d from target\n",
152                         eventfd_copy.target_fd);
153                 goto out_task;
154         }
155
156         /*
157          * Install the file struct from the target process into the
158          * file desciptor of the source process,
159          */
160
161         fd_install(eventfd_copy.source_fd, file);
162         ret = 0;
163
164 out_task:
165         put_task_struct(task_target);
166 out:
167         return ret;
168 }
169
170 static long
171 eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
172 {
173         long ret = -ENOIOCTLCMD;
174
175         switch (ioctl) {
176         case EVENTFD_COPY:
177                 ret = eventfd_link_ioctl_copy(arg);
178                 break;
179         }
180
181         return ret;
182 }
183
184 static const struct file_operations eventfd_link_fops = {
185         .owner = THIS_MODULE,
186         .unlocked_ioctl = eventfd_link_ioctl,
187 };
188
189
190 static struct miscdevice eventfd_link_misc = {
191         .name = "eventfd-link",
192         .fops = &eventfd_link_fops,
193 };
194
195 static int __init
196 eventfd_link_init(void)
197 {
198         return misc_register(&eventfd_link_misc);
199 }
200
201 module_init(eventfd_link_init);
202
203 static void __exit
204 eventfd_link_exit(void)
205 {
206         misc_deregister(&eventfd_link_misc);
207 }
208
209 module_exit(eventfd_link_exit);
210
211 MODULE_VERSION("0.0.1");
212 MODULE_LICENSE("GPL v2");
213 MODULE_AUTHOR("Anthony Fee");
214 MODULE_DESCRIPTION("Link eventfd");
215 MODULE_ALIAS("devname:eventfd-link");