Update PREAD fix
[lttng-modules.git] / ltt-debugfs-abi.c
1 /*
2 * ltt-debugfs-abi.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng debugfs ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns a file descriptor or failure.
10 * - channel creation, returns a file descriptor or failure.
11 * - Operates on a session file descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns a file descriptor or failure.
14 * - Operates on a channel file descriptor.
15 * - stream notifier get, returns a file descriptor or failure.
16 * - Operates on a channel file descriptor.
17 * - event creation, returns a file descriptor or failure.
18 * - Operates on a channel file descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
23 */
24
25 #include <linux/module.h>
26 #include <linux/debugfs.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/file.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
31 #include <linux/ringbuffer/vfs.h>
32 #include "ltt-debugfs-abi.h"
33 #include "ltt-events.h"
34
35 /*
36 * This is LTTng's own personal way to create a system call as an external
37 * module. We use ioctl() on /sys/kernel/debug/lttng.
38 */
39
40 static struct dentry *lttng_dentry;
41 static const struct file_operations lttng_fops;
42 static const struct file_operations lttng_session_fops;
43 static const struct file_operations lttng_channel_fops;
44 static const struct file_operations lttng_event_fops;
45
46 static
47 int lttng_abi_create_session(void)
48 {
49 struct ltt_session *session;
50 struct file *session_file;
51 int session_fd, ret;
52
53 session = ltt_session_create();
54 if (!session)
55 return -ENOMEM;
56 session_fd = get_unused_fd();
57 if (session_fd < 0) {
58 ret = session_fd;
59 goto fd_error;
60 }
61 session_file = anon_inode_getfile("[lttng_session]",
62 &lttng_session_fops,
63 session, O_RDWR);
64 if (IS_ERR(session_file)) {
65 ret = PTR_ERR(session_file);
66 goto file_error;
67 }
68 session->file = session_file;
69 fd_install(session_fd, session_file);
70 return session_fd;
71
72 file_error:
73 put_unused_fd(session_fd);
74 fd_error:
75 ltt_session_destroy(session);
76 return ret;
77 }
78
79 /**
80 * lttng_ioctl - lttng syscall through ioctl
81 *
82 * @file: the file
83 * @cmd: the command
84 * @arg: command arg
85 *
86 * This ioctl implements lttng commands:
87 * LTTNG_SESSION
88 * Returns a LTTng trace session file descriptor
89 *
90 * The returned session will be deleted when its file descriptor is closed.
91 */
92 static
93 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
94 {
95 switch (cmd) {
96 case LTTNG_SESSION:
97 return lttng_abi_create_session();
98 default:
99 return -ENOIOCTLCMD;
100 }
101 }
102
103 static const struct file_operations lttng_fops = {
104 .unlocked_ioctl = lttng_ioctl,
105 #ifdef CONFIG_COMPAT
106 .compat_ioctl = lttng_ioctl,
107 #endif
108 };
109
110 int lttng_abi_create_channel(struct file *session_file,
111 struct lttng_channel __user *uchan_param)
112 {
113 struct ltt_session *session = session_file->private_data;
114 struct ltt_channel *chan;
115 struct file *chan_file;
116 struct lttng_channel chan_param;
117 int chan_fd;
118 int ret = 0;
119
120 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
121 return -EFAULT;
122 chan_fd = get_unused_fd();
123 if (chan_fd < 0) {
124 ret = chan_fd;
125 goto fd_error;
126 }
127 chan_file = anon_inode_getfile("[lttng_channel]",
128 &lttng_channel_fops,
129 NULL, O_RDWR);
130 if (IS_ERR(chan_file)) {
131 ret = PTR_ERR(chan_file);
132 goto file_error;
133 }
134 /*
135 * We tolerate no failure path after channel creation. It will stay
136 * invariant for the rest of the session.
137 */
138 chan = ltt_channel_create(session, chan_param.overwrite, NULL,
139 chan_param.subbuf_size,
140 chan_param.num_subbuf,
141 chan_param.switch_timer_interval,
142 chan_param.read_timer_interval);
143 if (!chan) {
144 ret = -EINVAL;
145 goto chan_error;
146 }
147 chan->file = chan_file;
148 chan_file->private_data = chan;
149 fd_install(chan_fd, chan_file);
150 /* The channel created holds a reference on the session */
151 atomic_long_inc(&session_file->f_count);
152
153 return chan_fd;
154
155 chan_error:
156 fput(chan_file);
157 file_error:
158 put_unused_fd(chan_fd);
159 fd_error:
160 return ret;
161 }
162
163 /**
164 * lttng_session_ioctl - lttng session fd ioctl
165 *
166 * @file: the file
167 * @cmd: the command
168 * @arg: command arg
169 *
170 * This ioctl implements lttng commands:
171 * LTTNG_CHANNEL
172 * Returns a LTTng channel file descriptor
173 *
174 * The returned channel will be deleted when its file descriptor is closed.
175 */
176 static
177 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
178 {
179 struct ltt_session *session = file->private_data;
180
181 switch (cmd) {
182 case LTTNG_CHANNEL:
183 return lttng_abi_create_channel(file, (struct lttng_channel __user *)arg);
184 case LTTNG_SESSION_START:
185 return ltt_session_start(session);
186 case LTTNG_SESSION_STOP:
187 return ltt_session_stop(session);
188 default:
189 return -ENOIOCTLCMD;
190 }
191 }
192
193 /*
194 * Called when the last file reference is dropped.
195 *
196 * Big fat note: channels and events are invariant for the whole session after
197 * their creation. So this session destruction also destroys all channel and
198 * event structures specific to this session (they are not destroyed when their
199 * individual file is released).
200 */
201 static
202 int lttng_session_release(struct inode *inode, struct file *file)
203 {
204 struct ltt_session *session = file->private_data;
205
206 if (session)
207 ltt_session_destroy(session);
208 return 0;
209 }
210
211 static const struct file_operations lttng_session_fops = {
212 .release = lttng_session_release,
213 .unlocked_ioctl = lttng_session_ioctl,
214 #ifdef CONFIG_COMPAT
215 .compat_ioctl = lttng_session_ioctl,
216 #endif
217 };
218
219 static
220 int lttng_abi_open_stream(struct file *channel_file)
221 {
222 struct ltt_channel *channel = channel_file->private_data;
223 struct lib_ring_buffer *buf;
224 int stream_fd, ret;
225 struct file *stream_file;
226
227 buf = channel->ops->buffer_read_open(channel->chan);
228 if (!buf)
229 return -ENOENT;
230
231 stream_fd = get_unused_fd();
232 if (stream_fd < 0) {
233 ret = stream_fd;
234 goto fd_error;
235 }
236 stream_file = anon_inode_getfile("[lttng_stream]",
237 &lib_ring_buffer_file_operations,
238 buf, O_RDWR);
239 if (IS_ERR(stream_file)) {
240 ret = PTR_ERR(stream_file);
241 goto file_error;
242 }
243 /*
244 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
245 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
246 * file descriptor, so we set FMODE_PREAD here.
247 */
248 stream_file->f_mode |= FMODE_PREAD;
249 fd_install(stream_fd, stream_file);
250 /*
251 * The stream holds a reference to the channel within the generic ring
252 * buffer library, so no need to hold a refcount on the channel and
253 * session files here.
254 */
255 return stream_fd;
256
257 file_error:
258 put_unused_fd(stream_fd);
259 fd_error:
260 channel->ops->buffer_read_close(buf);
261 return ret;
262 }
263
264 static
265 int lttng_abi_create_event(struct file *channel_file,
266 struct lttng_event __user *uevent_param)
267 {
268 struct ltt_channel *channel = channel_file->private_data;
269 struct ltt_event *event;
270 char *event_name;
271 struct lttng_event event_param;
272 int event_fd, ret;
273 struct file *event_file;
274 void *probe;
275
276 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
277 return -EFAULT;
278 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
279 if (!event_name)
280 return -ENOMEM;
281 if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) {
282 ret = -EFAULT;
283 goto name_error;
284 }
285 event_name[PATH_MAX - 1] = '\0';
286
287 probe = ltt_probe_get(event_name);
288 if (!probe) {
289 ret = -ENOENT;
290 goto probe_error;
291 }
292 event_fd = get_unused_fd();
293 if (event_fd < 0) {
294 ret = event_fd;
295 goto fd_error;
296 }
297 event_file = anon_inode_getfile("[lttng_event]",
298 &lttng_event_fops,
299 NULL, O_RDWR);
300 if (IS_ERR(event_file)) {
301 ret = PTR_ERR(event_file);
302 goto file_error;
303 }
304 /*
305 * We tolerate no failure path after event creation. It will stay
306 * invariant for the rest of the session.
307 */
308 event = ltt_event_create(channel, event_name, event_param.itype,
309 probe, NULL);
310 if (!event) {
311 goto event_error;
312 ret = -EEXIST;
313 }
314 event_file->private_data = event;
315 fd_install(event_fd, event_file);
316 /* The event holds a reference on the channel */
317 atomic_long_inc(&channel_file->f_count);
318 kfree(event_name);
319 return event_fd;
320
321 event_error:
322 fput(event_file);
323 file_error:
324 put_unused_fd(event_fd);
325 fd_error:
326 ltt_probe_put(probe);
327 probe_error:
328 name_error:
329 kfree(event_name);
330 return ret;
331 }
332
333 /**
334 * lttng_channel_ioctl - lttng syscall through ioctl
335 *
336 * @file: the file
337 * @cmd: the command
338 * @arg: command arg
339 *
340 * This ioctl implements lttng commands:
341 * LTTNG_STREAM
342 * Returns an event stream file descriptor or failure.
343 * (typically, one event stream records events from one CPU)
344 * LTTNG_EVENT
345 * Returns an event file descriptor or failure.
346 *
347 * The returned session will be deleted when its file descriptor is closed.
348 * Channel and event file descriptors also hold a reference on the session.
349 */
350 static
351 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
352 {
353 switch (cmd) {
354 case LTTNG_STREAM:
355 return lttng_abi_open_stream(file);
356 case LTTNG_EVENT:
357 return lttng_abi_create_event(file, (struct lttng_event __user *)arg);
358 default:
359 return -ENOIOCTLCMD;
360 }
361 }
362
363 /* TODO: poll */
364 #if 0
365 /**
366 * lttng_channel_poll - lttng stream addition/removal monitoring
367 *
368 * @file: the file
369 * @wait: poll table
370 */
371 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
372 {
373 struct ltt_channel *channel = file->private_data;
374 unsigned int mask = 0;
375
376 if (file->f_mode & FMODE_READ) {
377 poll_wait_set_exclusive(wait);
378 poll_wait(file, &channel->notify_wait, wait);
379
380 /* TODO: identify when the channel is being finalized. */
381 if (finalized)
382 return POLLHUP;
383 else
384 return POLLIN | POLLRDNORM;
385 }
386 return mask;
387
388 }
389 #endif //0
390
391 static
392 int lttng_channel_release(struct inode *inode, struct file *file)
393 {
394 struct ltt_channel *channel = file->private_data;
395
396 if (channel)
397 fput(channel->session->file);
398 return 0;
399 }
400
401 static const struct file_operations lttng_channel_fops = {
402 .release = lttng_channel_release,
403 /* TODO */
404 #if 0
405 .poll = lttng_channel_poll,
406 #endif //0
407 .unlocked_ioctl = lttng_channel_ioctl,
408 #ifdef CONFIG_COMPAT
409 .compat_ioctl = lttng_channel_ioctl,
410 #endif
411 };
412
413 static
414 int lttng_event_release(struct inode *inode, struct file *file)
415 {
416 struct ltt_event *event = file->private_data;
417
418 if (event)
419 fput(event->chan->file);
420 return 0;
421 }
422
423 /* TODO: filter control ioctl */
424 static const struct file_operations lttng_event_fops = {
425 .release = lttng_event_release,
426 };
427
428 int __init ltt_debugfs_abi_init(void)
429 {
430 int ret = 0;
431
432 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
433 &lttng_fops);
434 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
435 printk(KERN_ERR "Error creating LTTng control file\n");
436 ret = -ENOMEM;
437 goto error;
438 }
439 error:
440 return ret;
441 }
442
443 void __exit ltt_debugfs_abi_exit(void)
444 {
445 debugfs_remove(lttng_dentry);
446 }
This page took 0.046087 seconds and 5 git commands to generate.