b83ed3d211defb747447469fc40bba06d816242b
[deliverable/lttng-modules.git] / lttng-abi.c
1 /*
2 * lttng-abi.c
3 *
4 * LTTng ABI
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
38 */
39
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include <linux/err.h>
47 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
48 #include "wrapper/ringbuffer/vfs.h"
49 #include "wrapper/ringbuffer/backend.h"
50 #include "wrapper/ringbuffer/frontend.h"
51 #include "wrapper/poll.h"
52 #include "lttng-abi.h"
53 #include "lttng-abi-old.h"
54 #include "lttng-events.h"
55 #include "lttng-tracer.h"
56 #include "lib/ringbuffer/frontend_types.h"
57
58 /*
59 * This is LTTng's own personal way to create a system call as an external
60 * module. We use ioctl() on /proc/lttng.
61 */
62
63 static struct proc_dir_entry *lttng_proc_dentry;
64 static const struct file_operations lttng_fops;
65 static const struct file_operations lttng_session_fops;
66 static const struct file_operations lttng_channel_fops;
67 static const struct file_operations lttng_metadata_fops;
68 static const struct file_operations lttng_event_fops;
69 static struct file_operations lttng_stream_ring_buffer_file_operations;
70
71 /*
72 * Teardown management: opened file descriptors keep a refcount on the module,
73 * so it can only exit when all file descriptors are closed.
74 */
75
76 static
77 int lttng_abi_create_session(void)
78 {
79 struct lttng_session *session;
80 struct file *session_file;
81 int session_fd, ret;
82
83 session = lttng_session_create();
84 if (!session)
85 return -ENOMEM;
86 session_fd = get_unused_fd();
87 if (session_fd < 0) {
88 ret = session_fd;
89 goto fd_error;
90 }
91 session_file = anon_inode_getfile("[lttng_session]",
92 &lttng_session_fops,
93 session, O_RDWR);
94 if (IS_ERR(session_file)) {
95 ret = PTR_ERR(session_file);
96 goto file_error;
97 }
98 session->file = session_file;
99 fd_install(session_fd, session_file);
100 return session_fd;
101
102 file_error:
103 put_unused_fd(session_fd);
104 fd_error:
105 lttng_session_destroy(session);
106 return ret;
107 }
108
109 static
110 int lttng_abi_tracepoint_list(void)
111 {
112 struct file *tracepoint_list_file;
113 int file_fd, ret;
114
115 file_fd = get_unused_fd();
116 if (file_fd < 0) {
117 ret = file_fd;
118 goto fd_error;
119 }
120
121 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
122 &lttng_tracepoint_list_fops,
123 NULL, O_RDWR);
124 if (IS_ERR(tracepoint_list_file)) {
125 ret = PTR_ERR(tracepoint_list_file);
126 goto file_error;
127 }
128 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
129 if (ret < 0)
130 goto open_error;
131 fd_install(file_fd, tracepoint_list_file);
132 if (file_fd < 0) {
133 ret = file_fd;
134 goto fd_error;
135 }
136 return file_fd;
137
138 open_error:
139 fput(tracepoint_list_file);
140 file_error:
141 put_unused_fd(file_fd);
142 fd_error:
143 return ret;
144 }
145
146 static
147 int lttng_abi_syscall_list(void)
148 {
149 struct file *syscall_list_file;
150 int file_fd, ret;
151
152 file_fd = get_unused_fd();
153 if (file_fd < 0) {
154 ret = file_fd;
155 goto fd_error;
156 }
157
158 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
159 &lttng_syscall_list_fops,
160 NULL, O_RDWR);
161 if (IS_ERR(syscall_list_file)) {
162 ret = PTR_ERR(syscall_list_file);
163 goto file_error;
164 }
165 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
166 if (ret < 0)
167 goto open_error;
168 fd_install(file_fd, syscall_list_file);
169 if (file_fd < 0) {
170 ret = file_fd;
171 goto fd_error;
172 }
173 return file_fd;
174
175 open_error:
176 fput(syscall_list_file);
177 file_error:
178 put_unused_fd(file_fd);
179 fd_error:
180 return ret;
181 }
182
183 static
184 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
185 {
186 v->major = LTTNG_MODULES_MAJOR_VERSION;
187 v->minor = LTTNG_MODULES_MINOR_VERSION;
188 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
189 }
190
191 static
192 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v)
193 {
194 v->major = LTTNG_MODULES_ABI_MAJOR_VERSION;
195 v->minor = LTTNG_MODULES_ABI_MINOR_VERSION;
196 }
197
198 static
199 long lttng_abi_add_context(struct file *file,
200 struct lttng_kernel_context *context_param,
201 struct lttng_ctx **ctx, struct lttng_session *session)
202 {
203
204 if (session->been_active)
205 return -EPERM;
206
207 switch (context_param->ctx) {
208 case LTTNG_KERNEL_CONTEXT_PID:
209 return lttng_add_pid_to_ctx(ctx);
210 case LTTNG_KERNEL_CONTEXT_PRIO:
211 return lttng_add_prio_to_ctx(ctx);
212 case LTTNG_KERNEL_CONTEXT_NICE:
213 return lttng_add_nice_to_ctx(ctx);
214 case LTTNG_KERNEL_CONTEXT_VPID:
215 return lttng_add_vpid_to_ctx(ctx);
216 case LTTNG_KERNEL_CONTEXT_TID:
217 return lttng_add_tid_to_ctx(ctx);
218 case LTTNG_KERNEL_CONTEXT_VTID:
219 return lttng_add_vtid_to_ctx(ctx);
220 case LTTNG_KERNEL_CONTEXT_PPID:
221 return lttng_add_ppid_to_ctx(ctx);
222 case LTTNG_KERNEL_CONTEXT_VPPID:
223 return lttng_add_vppid_to_ctx(ctx);
224 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
225 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
226 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
227 context_param->u.perf_counter.config,
228 context_param->u.perf_counter.name,
229 ctx);
230 case LTTNG_KERNEL_CONTEXT_PROCNAME:
231 return lttng_add_procname_to_ctx(ctx);
232 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
233 return lttng_add_hostname_to_ctx(ctx);
234 default:
235 return -EINVAL;
236 }
237 }
238
239 /**
240 * lttng_ioctl - lttng syscall through ioctl
241 *
242 * @file: the file
243 * @cmd: the command
244 * @arg: command arg
245 *
246 * This ioctl implements lttng commands:
247 * LTTNG_KERNEL_SESSION
248 * Returns a LTTng trace session file descriptor
249 * LTTNG_KERNEL_TRACER_VERSION
250 * Returns the LTTng kernel tracer version
251 * LTTNG_KERNEL_TRACEPOINT_LIST
252 * Returns a file descriptor listing available tracepoints
253 * LTTNG_KERNEL_WAIT_QUIESCENT
254 * Returns after all previously running probes have completed
255 * LTTNG_KERNEL_TRACER_ABI_VERSION
256 * Returns the LTTng kernel tracer ABI version
257 *
258 * The returned session will be deleted when its file descriptor is closed.
259 */
260 static
261 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
262 {
263 switch (cmd) {
264 case LTTNG_KERNEL_OLD_SESSION:
265 case LTTNG_KERNEL_SESSION:
266 return lttng_abi_create_session();
267 case LTTNG_KERNEL_OLD_TRACER_VERSION:
268 {
269 struct lttng_kernel_tracer_version v;
270 struct lttng_kernel_old_tracer_version oldv;
271 struct lttng_kernel_old_tracer_version *uversion =
272 (struct lttng_kernel_old_tracer_version __user *) arg;
273
274 lttng_abi_tracer_version(&v);
275 oldv.major = v.major;
276 oldv.minor = v.minor;
277 oldv.patchlevel = v.patchlevel;
278
279 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
280 return -EFAULT;
281 return 0;
282 }
283 case LTTNG_KERNEL_TRACER_VERSION:
284 {
285 struct lttng_kernel_tracer_version version;
286 struct lttng_kernel_tracer_version *uversion =
287 (struct lttng_kernel_tracer_version __user *) arg;
288
289 lttng_abi_tracer_version(&version);
290
291 if (copy_to_user(uversion, &version, sizeof(version)))
292 return -EFAULT;
293 return 0;
294 }
295 case LTTNG_KERNEL_TRACER_ABI_VERSION:
296 {
297 struct lttng_kernel_tracer_abi_version version;
298 struct lttng_kernel_tracer_abi_version *uversion =
299 (struct lttng_kernel_tracer_abi_version __user *) arg;
300
301 lttng_abi_tracer_abi_version(&version);
302
303 if (copy_to_user(uversion, &version, sizeof(version)))
304 return -EFAULT;
305 return 0;
306 }
307 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
308 case LTTNG_KERNEL_TRACEPOINT_LIST:
309 return lttng_abi_tracepoint_list();
310 case LTTNG_KERNEL_SYSCALL_LIST:
311 return lttng_abi_syscall_list();
312 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
313 case LTTNG_KERNEL_WAIT_QUIESCENT:
314 synchronize_trace();
315 return 0;
316 case LTTNG_KERNEL_OLD_CALIBRATE:
317 {
318 struct lttng_kernel_old_calibrate __user *ucalibrate =
319 (struct lttng_kernel_old_calibrate __user *) arg;
320 struct lttng_kernel_old_calibrate old_calibrate;
321 struct lttng_kernel_calibrate calibrate;
322 int ret;
323
324 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
325 return -EFAULT;
326 calibrate.type = old_calibrate.type;
327 ret = lttng_calibrate(&calibrate);
328 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
329 return -EFAULT;
330 return ret;
331 }
332 case LTTNG_KERNEL_CALIBRATE:
333 {
334 struct lttng_kernel_calibrate __user *ucalibrate =
335 (struct lttng_kernel_calibrate __user *) arg;
336 struct lttng_kernel_calibrate calibrate;
337 int ret;
338
339 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
340 return -EFAULT;
341 ret = lttng_calibrate(&calibrate);
342 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
343 return -EFAULT;
344 return ret;
345 }
346 default:
347 return -ENOIOCTLCMD;
348 }
349 }
350
351 static const struct file_operations lttng_fops = {
352 .owner = THIS_MODULE,
353 .unlocked_ioctl = lttng_ioctl,
354 #ifdef CONFIG_COMPAT
355 .compat_ioctl = lttng_ioctl,
356 #endif
357 };
358
359 static
360 int lttng_abi_create_channel(struct file *session_file,
361 struct lttng_kernel_channel *chan_param,
362 enum channel_type channel_type)
363 {
364 struct lttng_session *session = session_file->private_data;
365 const struct file_operations *fops = NULL;
366 const char *transport_name;
367 struct lttng_channel *chan;
368 struct file *chan_file;
369 int chan_fd;
370 int ret = 0;
371
372 chan_fd = get_unused_fd();
373 if (chan_fd < 0) {
374 ret = chan_fd;
375 goto fd_error;
376 }
377 switch (channel_type) {
378 case PER_CPU_CHANNEL:
379 fops = &lttng_channel_fops;
380 break;
381 case METADATA_CHANNEL:
382 fops = &lttng_metadata_fops;
383 break;
384 }
385
386 chan_file = anon_inode_getfile("[lttng_channel]",
387 fops,
388 NULL, O_RDWR);
389 if (IS_ERR(chan_file)) {
390 ret = PTR_ERR(chan_file);
391 goto file_error;
392 }
393 switch (channel_type) {
394 case PER_CPU_CHANNEL:
395 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
396 transport_name = chan_param->overwrite ?
397 "relay-overwrite" : "relay-discard";
398 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
399 transport_name = chan_param->overwrite ?
400 "relay-overwrite-mmap" : "relay-discard-mmap";
401 } else {
402 return -EINVAL;
403 }
404 break;
405 case METADATA_CHANNEL:
406 if (chan_param->output == LTTNG_KERNEL_SPLICE)
407 transport_name = "relay-metadata";
408 else if (chan_param->output == LTTNG_KERNEL_MMAP)
409 transport_name = "relay-metadata-mmap";
410 else
411 return -EINVAL;
412 break;
413 default:
414 transport_name = "<unknown>";
415 break;
416 }
417 /*
418 * We tolerate no failure path after channel creation. It will stay
419 * invariant for the rest of the session.
420 */
421 chan = lttng_channel_create(session, transport_name, NULL,
422 chan_param->subbuf_size,
423 chan_param->num_subbuf,
424 chan_param->switch_timer_interval,
425 chan_param->read_timer_interval,
426 channel_type);
427 if (!chan) {
428 ret = -EINVAL;
429 goto chan_error;
430 }
431 chan->file = chan_file;
432 chan_file->private_data = chan;
433 fd_install(chan_fd, chan_file);
434 atomic_long_inc(&session_file->f_count);
435
436 return chan_fd;
437
438 chan_error:
439 fput(chan_file);
440 file_error:
441 put_unused_fd(chan_fd);
442 fd_error:
443 return ret;
444 }
445
446 /**
447 * lttng_session_ioctl - lttng session fd ioctl
448 *
449 * @file: the file
450 * @cmd: the command
451 * @arg: command arg
452 *
453 * This ioctl implements lttng commands:
454 * LTTNG_KERNEL_CHANNEL
455 * Returns a LTTng channel file descriptor
456 * LTTNG_KERNEL_ENABLE
457 * Enables tracing for a session (weak enable)
458 * LTTNG_KERNEL_DISABLE
459 * Disables tracing for a session (strong disable)
460 * LTTNG_KERNEL_METADATA
461 * Returns a LTTng metadata file descriptor
462 *
463 * The returned channel will be deleted when its file descriptor is closed.
464 */
465 static
466 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
467 {
468 struct lttng_session *session = file->private_data;
469
470 switch (cmd) {
471 case LTTNG_KERNEL_OLD_CHANNEL:
472 {
473 struct lttng_kernel_channel chan_param;
474 struct lttng_kernel_old_channel old_chan_param;
475
476 if (copy_from_user(&old_chan_param,
477 (struct lttng_kernel_old_channel __user *) arg,
478 sizeof(struct lttng_kernel_old_channel)))
479 return -EFAULT;
480 chan_param.overwrite = old_chan_param.overwrite;
481 chan_param.subbuf_size = old_chan_param.subbuf_size;
482 chan_param.num_subbuf = old_chan_param.num_subbuf;
483 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
484 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
485 chan_param.output = old_chan_param.output;
486
487 return lttng_abi_create_channel(file, &chan_param,
488 PER_CPU_CHANNEL);
489 }
490 case LTTNG_KERNEL_CHANNEL:
491 {
492 struct lttng_kernel_channel chan_param;
493
494 if (copy_from_user(&chan_param,
495 (struct lttng_kernel_channel __user *) arg,
496 sizeof(struct lttng_kernel_channel)))
497 return -EFAULT;
498 return lttng_abi_create_channel(file, &chan_param,
499 PER_CPU_CHANNEL);
500 }
501 case LTTNG_KERNEL_OLD_SESSION_START:
502 case LTTNG_KERNEL_OLD_ENABLE:
503 case LTTNG_KERNEL_SESSION_START:
504 case LTTNG_KERNEL_ENABLE:
505 return lttng_session_enable(session);
506 case LTTNG_KERNEL_OLD_SESSION_STOP:
507 case LTTNG_KERNEL_OLD_DISABLE:
508 case LTTNG_KERNEL_SESSION_STOP:
509 case LTTNG_KERNEL_DISABLE:
510 return lttng_session_disable(session);
511 case LTTNG_KERNEL_OLD_METADATA:
512 {
513 struct lttng_kernel_channel chan_param;
514 struct lttng_kernel_old_channel old_chan_param;
515
516 if (copy_from_user(&old_chan_param,
517 (struct lttng_kernel_old_channel __user *) arg,
518 sizeof(struct lttng_kernel_old_channel)))
519 return -EFAULT;
520 chan_param.overwrite = old_chan_param.overwrite;
521 chan_param.subbuf_size = old_chan_param.subbuf_size;
522 chan_param.num_subbuf = old_chan_param.num_subbuf;
523 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
524 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
525 chan_param.output = old_chan_param.output;
526
527 return lttng_abi_create_channel(file, &chan_param,
528 METADATA_CHANNEL);
529 }
530 case LTTNG_KERNEL_METADATA:
531 {
532 struct lttng_kernel_channel chan_param;
533
534 if (copy_from_user(&chan_param,
535 (struct lttng_kernel_channel __user *) arg,
536 sizeof(struct lttng_kernel_channel)))
537 return -EFAULT;
538 return lttng_abi_create_channel(file, &chan_param,
539 METADATA_CHANNEL);
540 }
541 default:
542 return -ENOIOCTLCMD;
543 }
544 }
545
546 /*
547 * Called when the last file reference is dropped.
548 *
549 * Big fat note: channels and events are invariant for the whole session after
550 * their creation. So this session destruction also destroys all channel and
551 * event structures specific to this session (they are not destroyed when their
552 * individual file is released).
553 */
554 static
555 int lttng_session_release(struct inode *inode, struct file *file)
556 {
557 struct lttng_session *session = file->private_data;
558
559 if (session)
560 lttng_session_destroy(session);
561 return 0;
562 }
563
564 static const struct file_operations lttng_session_fops = {
565 .owner = THIS_MODULE,
566 .release = lttng_session_release,
567 .unlocked_ioctl = lttng_session_ioctl,
568 #ifdef CONFIG_COMPAT
569 .compat_ioctl = lttng_session_ioctl,
570 #endif
571 };
572
573 /**
574 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
575 * @filp: the file
576 * @wait: poll table
577 *
578 * Handles the poll operations for the metadata channels.
579 */
580 static
581 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
582 poll_table *wait)
583 {
584 struct lttng_metadata_stream *stream = filp->private_data;
585 struct lib_ring_buffer *buf = stream->priv;
586 int finalized;
587 unsigned int mask = 0;
588
589 if (filp->f_mode & FMODE_READ) {
590 poll_wait_set_exclusive(wait);
591 poll_wait(filp, &stream->read_wait, wait);
592
593 finalized = stream->finalized;
594
595 /*
596 * lib_ring_buffer_is_finalized() contains a smp_rmb()
597 * ordering finalized load before offsets loads.
598 */
599 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
600
601 if (finalized)
602 mask |= POLLHUP;
603
604 if (stream->metadata_cache->metadata_written >
605 stream->metadata_out)
606 mask |= POLLIN;
607 }
608
609 return mask;
610 }
611
612 static
613 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
614 unsigned int cmd, unsigned long arg)
615 {
616 struct lttng_metadata_stream *stream = filp->private_data;
617
618 stream->metadata_out = stream->metadata_in;
619 }
620
621 static
622 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
623 unsigned int cmd, unsigned long arg)
624 {
625 int ret;
626 struct lttng_metadata_stream *stream = filp->private_data;
627 struct lib_ring_buffer *buf = stream->priv;
628
629 switch (cmd) {
630 case RING_BUFFER_GET_NEXT_SUBBUF:
631 {
632 struct lttng_metadata_stream *stream = filp->private_data;
633 struct lib_ring_buffer *buf = stream->priv;
634 struct channel *chan = buf->backend.chan;
635
636 ret = lttng_metadata_output_channel(stream, chan);
637 if (ret > 0) {
638 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
639 ret = 0;
640 } else if (ret < 0)
641 goto err;
642 break;
643 }
644 case RING_BUFFER_GET_SUBBUF:
645 {
646 /*
647 * Random access is not allowed for metadata channel.
648 */
649 return -ENOSYS;
650 }
651 case RING_BUFFER_FLUSH:
652 {
653 struct lttng_metadata_stream *stream = filp->private_data;
654 struct lib_ring_buffer *buf = stream->priv;
655 struct channel *chan = buf->backend.chan;
656
657 /*
658 * Before doing the actual ring buffer flush, write up to one
659 * packet of metadata in the ring buffer.
660 */
661 ret = lttng_metadata_output_channel(stream, chan);
662 if (ret < 0)
663 goto err;
664 break;
665 }
666 default:
667 break;
668 }
669 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
670
671 /* Performing lib ring buffer ioctl after our own. */
672 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
673 if (ret < 0)
674 goto err;
675
676 switch (cmd) {
677 case RING_BUFFER_PUT_NEXT_SUBBUF:
678 {
679 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
680 cmd, arg);
681 break;
682 }
683 default:
684 break;
685 }
686 err:
687 return ret;
688 }
689
690 #ifdef CONFIG_COMPAT
691 static
692 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
693 unsigned int cmd, unsigned long arg)
694 {
695 int ret;
696 struct lttng_metadata_stream *stream = filp->private_data;
697 struct lib_ring_buffer *buf = stream->priv;
698
699 switch (cmd) {
700 case RING_BUFFER_GET_NEXT_SUBBUF:
701 {
702 struct lttng_metadata_stream *stream = filp->private_data;
703 struct lib_ring_buffer *buf = stream->priv;
704 struct channel *chan = buf->backend.chan;
705
706 ret = lttng_metadata_output_channel(stream, chan);
707 if (ret > 0) {
708 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
709 ret = 0;
710 } else if (ret < 0)
711 goto err;
712 break;
713 }
714 case RING_BUFFER_GET_SUBBUF:
715 {
716 /*
717 * Random access is not allowed for metadata channel.
718 */
719 return -ENOSYS;
720 }
721 default:
722 break;
723 }
724 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
725
726 /* Performing lib ring buffer ioctl after our own. */
727 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
728 if (ret < 0)
729 goto err;
730
731 switch (cmd) {
732 case RING_BUFFER_PUT_NEXT_SUBBUF:
733 {
734 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
735 cmd, arg);
736 break;
737 }
738 default:
739 break;
740 }
741 err:
742 return ret;
743 }
744 #endif
745
746 /*
747 * This is not used by anonymous file descriptors. This code is left
748 * there if we ever want to implement an inode with open() operation.
749 */
750 static
751 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
752 {
753 struct lttng_metadata_stream *stream = inode->i_private;
754 struct lib_ring_buffer *buf = stream->priv;
755
756 file->private_data = buf;
757 /*
758 * Since life-time of metadata cache differs from that of
759 * session, we need to keep our own reference on the transport.
760 */
761 if (!try_module_get(stream->transport->owner)) {
762 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
763 return -EBUSY;
764 }
765 return lib_ring_buffer_open(inode, file, buf);
766 }
767
768 static
769 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
770 {
771 struct lttng_metadata_stream *stream = file->private_data;
772 struct lib_ring_buffer *buf = stream->priv;
773
774 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
775 module_put(stream->transport->owner);
776 return lib_ring_buffer_release(inode, file, buf);
777 }
778
779 static
780 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
781 struct pipe_inode_info *pipe, size_t len,
782 unsigned int flags)
783 {
784 struct lttng_metadata_stream *stream = in->private_data;
785 struct lib_ring_buffer *buf = stream->priv;
786
787 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
788 flags, buf);
789 }
790
791 static
792 int lttng_metadata_ring_buffer_mmap(struct file *filp,
793 struct vm_area_struct *vma)
794 {
795 struct lttng_metadata_stream *stream = filp->private_data;
796 struct lib_ring_buffer *buf = stream->priv;
797
798 return lib_ring_buffer_mmap(filp, vma, buf);
799 }
800
801 static
802 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
803 .owner = THIS_MODULE,
804 .open = lttng_metadata_ring_buffer_open,
805 .release = lttng_metadata_ring_buffer_release,
806 .poll = lttng_metadata_ring_buffer_poll,
807 .splice_read = lttng_metadata_ring_buffer_splice_read,
808 .mmap = lttng_metadata_ring_buffer_mmap,
809 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
810 .llseek = vfs_lib_ring_buffer_no_llseek,
811 #ifdef CONFIG_COMPAT
812 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
813 #endif
814 };
815
816 static
817 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
818 const struct file_operations *fops)
819 {
820 int stream_fd, ret;
821 struct file *stream_file;
822
823 stream_fd = get_unused_fd();
824 if (stream_fd < 0) {
825 ret = stream_fd;
826 goto fd_error;
827 }
828 stream_file = anon_inode_getfile("[lttng_stream]", fops,
829 stream_priv, O_RDWR);
830 if (IS_ERR(stream_file)) {
831 ret = PTR_ERR(stream_file);
832 goto file_error;
833 }
834 /*
835 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
836 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
837 * file descriptor, so we set FMODE_PREAD here.
838 */
839 stream_file->f_mode |= FMODE_PREAD;
840 fd_install(stream_fd, stream_file);
841 /*
842 * The stream holds a reference to the channel within the generic ring
843 * buffer library, so no need to hold a refcount on the channel and
844 * session files here.
845 */
846 return stream_fd;
847
848 file_error:
849 put_unused_fd(stream_fd);
850 fd_error:
851 return ret;
852 }
853
854 static
855 int lttng_abi_open_stream(struct file *channel_file)
856 {
857 struct lttng_channel *channel = channel_file->private_data;
858 struct lib_ring_buffer *buf;
859 int ret;
860 void *stream_priv;
861
862 buf = channel->ops->buffer_read_open(channel->chan);
863 if (!buf)
864 return -ENOENT;
865
866 stream_priv = buf;
867 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
868 &lttng_stream_ring_buffer_file_operations);
869 if (ret < 0)
870 goto fd_error;
871
872 return ret;
873
874 fd_error:
875 channel->ops->buffer_read_close(buf);
876 return ret;
877 }
878
879 static
880 int lttng_abi_open_metadata_stream(struct file *channel_file)
881 {
882 struct lttng_channel *channel = channel_file->private_data;
883 struct lttng_session *session = channel->session;
884 struct lib_ring_buffer *buf;
885 int ret;
886 struct lttng_metadata_stream *metadata_stream;
887 void *stream_priv;
888
889 buf = channel->ops->buffer_read_open(channel->chan);
890 if (!buf)
891 return -ENOENT;
892
893 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
894 GFP_KERNEL);
895 if (!metadata_stream) {
896 ret = -ENOMEM;
897 goto nomem;
898 }
899 metadata_stream->metadata_cache = session->metadata_cache;
900 init_waitqueue_head(&metadata_stream->read_wait);
901 metadata_stream->priv = buf;
902 stream_priv = metadata_stream;
903 metadata_stream->transport = channel->transport;
904 mutex_init(&metadata_stream->lock);
905
906 /*
907 * Since life-time of metadata cache differs from that of
908 * session, we need to keep our own reference on the transport.
909 */
910 if (!try_module_get(metadata_stream->transport->owner)) {
911 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
912 ret = -EINVAL;
913 goto notransport;
914 }
915
916 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
917 &lttng_metadata_ring_buffer_file_operations);
918 if (ret < 0)
919 goto fd_error;
920
921 kref_get(&session->metadata_cache->refcount);
922 list_add(&metadata_stream->list,
923 &session->metadata_cache->metadata_stream);
924 return ret;
925
926 fd_error:
927 module_put(metadata_stream->transport->owner);
928 notransport:
929 kfree(metadata_stream);
930 nomem:
931 channel->ops->buffer_read_close(buf);
932 return ret;
933 }
934
935 static
936 int lttng_abi_create_event(struct file *channel_file,
937 struct lttng_kernel_event *event_param)
938 {
939 struct lttng_channel *channel = channel_file->private_data;
940 struct lttng_event *event;
941 int event_fd, ret;
942 struct file *event_file;
943
944 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
945 switch (event_param->instrumentation) {
946 case LTTNG_KERNEL_KRETPROBE:
947 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
948 break;
949 case LTTNG_KERNEL_KPROBE:
950 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
951 break;
952 case LTTNG_KERNEL_FUNCTION:
953 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
954 break;
955 default:
956 break;
957 }
958 switch (event_param->instrumentation) {
959 default:
960 event_fd = get_unused_fd();
961 if (event_fd < 0) {
962 ret = event_fd;
963 goto fd_error;
964 }
965 event_file = anon_inode_getfile("[lttng_event]",
966 &lttng_event_fops,
967 NULL, O_RDWR);
968 if (IS_ERR(event_file)) {
969 ret = PTR_ERR(event_file);
970 goto file_error;
971 }
972 /*
973 * We tolerate no failure path after event creation. It
974 * will stay invariant for the rest of the session.
975 */
976 event = lttng_event_create(channel, event_param, NULL, NULL);
977 WARN_ON_ONCE(!event);
978 if (IS_ERR(event)) {
979 ret = PTR_ERR(event);
980 goto event_error;
981 }
982 event_file->private_data = event;
983 fd_install(event_fd, event_file);
984 /* The event holds a reference on the channel */
985 atomic_long_inc(&channel_file->f_count);
986 break;
987 case LTTNG_KERNEL_SYSCALL:
988 ret = lttng_syscalls_register(channel, NULL);
989 if (ret)
990 goto fd_error;
991 event_fd = 0;
992 if (event_param->u.syscall.enable) {
993 ret = lttng_syscall_filter_enable(channel,
994 event_param->name[0] == '\0' ?
995 NULL : event_param->name);
996 if (ret)
997 goto fd_error;
998
999 } else {
1000 ret = lttng_syscall_filter_disable(channel,
1001 event_param->name[0] == '\0' ?
1002 NULL : event_param->name);
1003 if (ret)
1004 goto fd_error;
1005 }
1006 break;
1007 }
1008 return event_fd;
1009
1010 event_error:
1011 fput(event_file);
1012 file_error:
1013 put_unused_fd(event_fd);
1014 fd_error:
1015 return ret;
1016 }
1017
1018 /**
1019 * lttng_channel_ioctl - lttng syscall through ioctl
1020 *
1021 * @file: the file
1022 * @cmd: the command
1023 * @arg: command arg
1024 *
1025 * This ioctl implements lttng commands:
1026 * LTTNG_KERNEL_STREAM
1027 * Returns an event stream file descriptor or failure.
1028 * (typically, one event stream records events from one CPU)
1029 * LTTNG_KERNEL_EVENT
1030 * Returns an event file descriptor or failure.
1031 * LTTNG_KERNEL_CONTEXT
1032 * Prepend a context field to each event in the channel
1033 * LTTNG_KERNEL_ENABLE
1034 * Enable recording for events in this channel (weak enable)
1035 * LTTNG_KERNEL_DISABLE
1036 * Disable recording for events in this channel (strong disable)
1037 *
1038 * Channel and event file descriptors also hold a reference on the session.
1039 */
1040 static
1041 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1042 {
1043 struct lttng_channel *channel = file->private_data;
1044
1045 switch (cmd) {
1046 case LTTNG_KERNEL_OLD_STREAM:
1047 case LTTNG_KERNEL_STREAM:
1048 return lttng_abi_open_stream(file);
1049 case LTTNG_KERNEL_OLD_EVENT:
1050 {
1051 struct lttng_kernel_event *uevent_param;
1052 struct lttng_kernel_old_event *old_uevent_param;
1053 int ret;
1054
1055 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1056 GFP_KERNEL);
1057 if (!uevent_param) {
1058 ret = -ENOMEM;
1059 goto old_event_end;
1060 }
1061 old_uevent_param = kmalloc(
1062 sizeof(struct lttng_kernel_old_event),
1063 GFP_KERNEL);
1064 if (!old_uevent_param) {
1065 ret = -ENOMEM;
1066 goto old_event_error_free_param;
1067 }
1068 if (copy_from_user(old_uevent_param,
1069 (struct lttng_kernel_old_event __user *) arg,
1070 sizeof(struct lttng_kernel_old_event))) {
1071 ret = -EFAULT;
1072 goto old_event_error_free_old_param;
1073 }
1074
1075 memcpy(uevent_param->name, old_uevent_param->name,
1076 sizeof(uevent_param->name));
1077 uevent_param->instrumentation =
1078 old_uevent_param->instrumentation;
1079
1080 switch (old_uevent_param->instrumentation) {
1081 case LTTNG_KERNEL_KPROBE:
1082 uevent_param->u.kprobe.addr =
1083 old_uevent_param->u.kprobe.addr;
1084 uevent_param->u.kprobe.offset =
1085 old_uevent_param->u.kprobe.offset;
1086 memcpy(uevent_param->u.kprobe.symbol_name,
1087 old_uevent_param->u.kprobe.symbol_name,
1088 sizeof(uevent_param->u.kprobe.symbol_name));
1089 break;
1090 case LTTNG_KERNEL_KRETPROBE:
1091 uevent_param->u.kretprobe.addr =
1092 old_uevent_param->u.kretprobe.addr;
1093 uevent_param->u.kretprobe.offset =
1094 old_uevent_param->u.kretprobe.offset;
1095 memcpy(uevent_param->u.kretprobe.symbol_name,
1096 old_uevent_param->u.kretprobe.symbol_name,
1097 sizeof(uevent_param->u.kretprobe.symbol_name));
1098 break;
1099 case LTTNG_KERNEL_FUNCTION:
1100 memcpy(uevent_param->u.ftrace.symbol_name,
1101 old_uevent_param->u.ftrace.symbol_name,
1102 sizeof(uevent_param->u.ftrace.symbol_name));
1103 break;
1104 default:
1105 break;
1106 }
1107 ret = lttng_abi_create_event(file, uevent_param);
1108
1109 old_event_error_free_old_param:
1110 kfree(old_uevent_param);
1111 old_event_error_free_param:
1112 kfree(uevent_param);
1113 old_event_end:
1114 return ret;
1115 }
1116 case LTTNG_KERNEL_EVENT:
1117 {
1118 struct lttng_kernel_event uevent_param;
1119
1120 if (copy_from_user(&uevent_param,
1121 (struct lttng_kernel_event __user *) arg,
1122 sizeof(uevent_param)))
1123 return -EFAULT;
1124 return lttng_abi_create_event(file, &uevent_param);
1125 }
1126 case LTTNG_KERNEL_OLD_CONTEXT:
1127 {
1128 struct lttng_kernel_context *ucontext_param;
1129 struct lttng_kernel_old_context *old_ucontext_param;
1130 int ret;
1131
1132 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1133 GFP_KERNEL);
1134 if (!ucontext_param) {
1135 ret = -ENOMEM;
1136 goto old_ctx_end;
1137 }
1138 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1139 GFP_KERNEL);
1140 if (!old_ucontext_param) {
1141 ret = -ENOMEM;
1142 goto old_ctx_error_free_param;
1143 }
1144
1145 if (copy_from_user(old_ucontext_param,
1146 (struct lttng_kernel_old_context __user *) arg,
1147 sizeof(struct lttng_kernel_old_context))) {
1148 ret = -EFAULT;
1149 goto old_ctx_error_free_old_param;
1150 }
1151 ucontext_param->ctx = old_ucontext_param->ctx;
1152 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1153 sizeof(ucontext_param->padding));
1154 /* only type that uses the union */
1155 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1156 ucontext_param->u.perf_counter.type =
1157 old_ucontext_param->u.perf_counter.type;
1158 ucontext_param->u.perf_counter.config =
1159 old_ucontext_param->u.perf_counter.config;
1160 memcpy(ucontext_param->u.perf_counter.name,
1161 old_ucontext_param->u.perf_counter.name,
1162 sizeof(ucontext_param->u.perf_counter.name));
1163 }
1164
1165 ret = lttng_abi_add_context(file,
1166 ucontext_param,
1167 &channel->ctx, channel->session);
1168
1169 old_ctx_error_free_old_param:
1170 kfree(old_ucontext_param);
1171 old_ctx_error_free_param:
1172 kfree(ucontext_param);
1173 old_ctx_end:
1174 return ret;
1175 }
1176 case LTTNG_KERNEL_CONTEXT:
1177 {
1178 struct lttng_kernel_context ucontext_param;
1179
1180 if (copy_from_user(&ucontext_param,
1181 (struct lttng_kernel_context __user *) arg,
1182 sizeof(ucontext_param)))
1183 return -EFAULT;
1184 return lttng_abi_add_context(file,
1185 &ucontext_param,
1186 &channel->ctx, channel->session);
1187 }
1188 case LTTNG_KERNEL_OLD_ENABLE:
1189 case LTTNG_KERNEL_ENABLE:
1190 return lttng_channel_enable(channel);
1191 case LTTNG_KERNEL_OLD_DISABLE:
1192 case LTTNG_KERNEL_DISABLE:
1193 return lttng_channel_disable(channel);
1194 case LTTNG_KERNEL_SYSCALL_MASK:
1195 return lttng_channel_syscall_mask(channel,
1196 (struct lttng_kernel_syscall_mask __user *) arg);
1197 default:
1198 return -ENOIOCTLCMD;
1199 }
1200
1201 }
1202
1203 /**
1204 * lttng_metadata_ioctl - lttng syscall through ioctl
1205 *
1206 * @file: the file
1207 * @cmd: the command
1208 * @arg: command arg
1209 *
1210 * This ioctl implements lttng commands:
1211 * LTTNG_KERNEL_STREAM
1212 * Returns an event stream file descriptor or failure.
1213 *
1214 * Channel and event file descriptors also hold a reference on the session.
1215 */
1216 static
1217 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1218 {
1219 switch (cmd) {
1220 case LTTNG_KERNEL_OLD_STREAM:
1221 case LTTNG_KERNEL_STREAM:
1222 return lttng_abi_open_metadata_stream(file);
1223 default:
1224 return -ENOIOCTLCMD;
1225 }
1226 }
1227
1228 /**
1229 * lttng_channel_poll - lttng stream addition/removal monitoring
1230 *
1231 * @file: the file
1232 * @wait: poll table
1233 */
1234 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1235 {
1236 struct lttng_channel *channel = file->private_data;
1237 unsigned int mask = 0;
1238
1239 if (file->f_mode & FMODE_READ) {
1240 poll_wait_set_exclusive(wait);
1241 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1242 wait);
1243
1244 if (channel->ops->is_disabled(channel->chan))
1245 return POLLERR;
1246 if (channel->ops->is_finalized(channel->chan))
1247 return POLLHUP;
1248 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1249 return POLLIN | POLLRDNORM;
1250 return 0;
1251 }
1252 return mask;
1253
1254 }
1255
1256 static
1257 int lttng_channel_release(struct inode *inode, struct file *file)
1258 {
1259 struct lttng_channel *channel = file->private_data;
1260
1261 if (channel)
1262 fput(channel->session->file);
1263 return 0;
1264 }
1265
1266 static
1267 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1268 {
1269 struct lttng_channel *channel = file->private_data;
1270
1271 if (channel) {
1272 lttng_metadata_channel_destroy(channel);
1273 fput(channel->session->file);
1274 }
1275
1276 return 0;
1277 }
1278
1279 static const struct file_operations lttng_channel_fops = {
1280 .owner = THIS_MODULE,
1281 .release = lttng_channel_release,
1282 .poll = lttng_channel_poll,
1283 .unlocked_ioctl = lttng_channel_ioctl,
1284 #ifdef CONFIG_COMPAT
1285 .compat_ioctl = lttng_channel_ioctl,
1286 #endif
1287 };
1288
1289 static const struct file_operations lttng_metadata_fops = {
1290 .owner = THIS_MODULE,
1291 .release = lttng_metadata_channel_release,
1292 .unlocked_ioctl = lttng_metadata_ioctl,
1293 #ifdef CONFIG_COMPAT
1294 .compat_ioctl = lttng_metadata_ioctl,
1295 #endif
1296 };
1297
1298 /**
1299 * lttng_event_ioctl - lttng syscall through ioctl
1300 *
1301 * @file: the file
1302 * @cmd: the command
1303 * @arg: command arg
1304 *
1305 * This ioctl implements lttng commands:
1306 * LTTNG_KERNEL_CONTEXT
1307 * Prepend a context field to each record of this event
1308 * LTTNG_KERNEL_ENABLE
1309 * Enable recording for this event (weak enable)
1310 * LTTNG_KERNEL_DISABLE
1311 * Disable recording for this event (strong disable)
1312 */
1313 static
1314 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1315 {
1316 struct lttng_event *event = file->private_data;
1317
1318 switch (cmd) {
1319 case LTTNG_KERNEL_OLD_CONTEXT:
1320 {
1321 struct lttng_kernel_context *ucontext_param;
1322 struct lttng_kernel_old_context *old_ucontext_param;
1323 int ret;
1324
1325 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1326 GFP_KERNEL);
1327 if (!ucontext_param) {
1328 ret = -ENOMEM;
1329 goto old_ctx_end;
1330 }
1331 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1332 GFP_KERNEL);
1333 if (!old_ucontext_param) {
1334 ret = -ENOMEM;
1335 goto old_ctx_error_free_param;
1336 }
1337
1338 if (copy_from_user(old_ucontext_param,
1339 (struct lttng_kernel_old_context __user *) arg,
1340 sizeof(struct lttng_kernel_old_context))) {
1341 ret = -EFAULT;
1342 goto old_ctx_error_free_old_param;
1343 }
1344 ucontext_param->ctx = old_ucontext_param->ctx;
1345 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1346 sizeof(ucontext_param->padding));
1347 /* only type that uses the union */
1348 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1349 ucontext_param->u.perf_counter.type =
1350 old_ucontext_param->u.perf_counter.type;
1351 ucontext_param->u.perf_counter.config =
1352 old_ucontext_param->u.perf_counter.config;
1353 memcpy(ucontext_param->u.perf_counter.name,
1354 old_ucontext_param->u.perf_counter.name,
1355 sizeof(ucontext_param->u.perf_counter.name));
1356 }
1357
1358 ret = lttng_abi_add_context(file,
1359 ucontext_param,
1360 &event->ctx, event->chan->session);
1361
1362 old_ctx_error_free_old_param:
1363 kfree(old_ucontext_param);
1364 old_ctx_error_free_param:
1365 kfree(ucontext_param);
1366 old_ctx_end:
1367 return ret;
1368 }
1369 case LTTNG_KERNEL_CONTEXT:
1370 {
1371 struct lttng_kernel_context ucontext_param;
1372
1373 if (copy_from_user(&ucontext_param,
1374 (struct lttng_kernel_context __user *) arg,
1375 sizeof(ucontext_param)))
1376 return -EFAULT;
1377 return lttng_abi_add_context(file,
1378 &ucontext_param,
1379 &event->ctx, event->chan->session);
1380 }
1381 case LTTNG_KERNEL_OLD_ENABLE:
1382 case LTTNG_KERNEL_ENABLE:
1383 return lttng_event_enable(event);
1384 case LTTNG_KERNEL_OLD_DISABLE:
1385 case LTTNG_KERNEL_DISABLE:
1386 return lttng_event_disable(event);
1387 default:
1388 return -ENOIOCTLCMD;
1389 }
1390 }
1391
1392 static
1393 int lttng_event_release(struct inode *inode, struct file *file)
1394 {
1395 struct lttng_event *event = file->private_data;
1396
1397 if (event)
1398 fput(event->chan->file);
1399 return 0;
1400 }
1401
1402 /* TODO: filter control ioctl */
1403 static const struct file_operations lttng_event_fops = {
1404 .owner = THIS_MODULE,
1405 .release = lttng_event_release,
1406 .unlocked_ioctl = lttng_event_ioctl,
1407 #ifdef CONFIG_COMPAT
1408 .compat_ioctl = lttng_event_ioctl,
1409 #endif
1410 };
1411
1412 static int put_u64(uint64_t val, unsigned long arg)
1413 {
1414 return put_user(val, (uint64_t __user *) arg);
1415 }
1416
1417 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1418 unsigned int cmd, unsigned long arg)
1419 {
1420 struct lib_ring_buffer *buf = filp->private_data;
1421 struct channel *chan = buf->backend.chan;
1422 const struct lib_ring_buffer_config *config = &chan->backend.config;
1423 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1424 int ret;
1425
1426 if (atomic_read(&chan->record_disabled))
1427 return -EIO;
1428
1429 switch (cmd) {
1430 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1431 {
1432 uint64_t ts;
1433
1434 ret = ops->timestamp_begin(config, buf, &ts);
1435 if (ret < 0)
1436 goto error;
1437 return put_u64(ts, arg);
1438 }
1439 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1440 {
1441 uint64_t ts;
1442
1443 ret = ops->timestamp_end(config, buf, &ts);
1444 if (ret < 0)
1445 goto error;
1446 return put_u64(ts, arg);
1447 }
1448 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1449 {
1450 uint64_t ed;
1451
1452 ret = ops->events_discarded(config, buf, &ed);
1453 if (ret < 0)
1454 goto error;
1455 return put_u64(ed, arg);
1456 }
1457 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1458 {
1459 uint64_t cs;
1460
1461 ret = ops->content_size(config, buf, &cs);
1462 if (ret < 0)
1463 goto error;
1464 return put_u64(cs, arg);
1465 }
1466 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1467 {
1468 uint64_t ps;
1469
1470 ret = ops->packet_size(config, buf, &ps);
1471 if (ret < 0)
1472 goto error;
1473 return put_u64(ps, arg);
1474 }
1475 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1476 {
1477 uint64_t si;
1478
1479 ret = ops->stream_id(config, buf, &si);
1480 if (ret < 0)
1481 goto error;
1482 return put_u64(si, arg);
1483 }
1484 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1485 {
1486 uint64_t ts;
1487
1488 ret = ops->current_timestamp(config, buf, &ts);
1489 if (ret < 0)
1490 goto error;
1491 return put_u64(ts, arg);
1492 }
1493 default:
1494 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1495 cmd, arg);
1496 }
1497
1498 error:
1499 return -ENOSYS;
1500 }
1501
1502 #ifdef CONFIG_COMPAT
1503 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1504 unsigned int cmd, unsigned long arg)
1505 {
1506 struct lib_ring_buffer *buf = filp->private_data;
1507 struct channel *chan = buf->backend.chan;
1508 const struct lib_ring_buffer_config *config = &chan->backend.config;
1509 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1510 int ret;
1511
1512 if (atomic_read(&chan->record_disabled))
1513 return -EIO;
1514
1515 switch (cmd) {
1516 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1517 {
1518 uint64_t ts;
1519
1520 ret = ops->timestamp_begin(config, buf, &ts);
1521 if (ret < 0)
1522 goto error;
1523 return put_u64(ts, arg);
1524 }
1525 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1526 {
1527 uint64_t ts;
1528
1529 ret = ops->timestamp_end(config, buf, &ts);
1530 if (ret < 0)
1531 goto error;
1532 return put_u64(ts, arg);
1533 }
1534 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1535 {
1536 uint64_t ed;
1537
1538 ret = ops->events_discarded(config, buf, &ed);
1539 if (ret < 0)
1540 goto error;
1541 return put_u64(ed, arg);
1542 }
1543 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1544 {
1545 uint64_t cs;
1546
1547 ret = ops->content_size(config, buf, &cs);
1548 if (ret < 0)
1549 goto error;
1550 return put_u64(cs, arg);
1551 }
1552 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1553 {
1554 uint64_t ps;
1555
1556 ret = ops->packet_size(config, buf, &ps);
1557 if (ret < 0)
1558 goto error;
1559 return put_u64(ps, arg);
1560 }
1561 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1562 {
1563 uint64_t si;
1564
1565 ret = ops->stream_id(config, buf, &si);
1566 if (ret < 0)
1567 goto error;
1568 return put_u64(si, arg);
1569 }
1570 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1571 {
1572 uint64_t ts;
1573
1574 ret = ops->current_timestamp(config, buf, &ts);
1575 if (ret < 0)
1576 goto error;
1577 return put_u64(ts, arg);
1578 }
1579 default:
1580 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1581 cmd, arg);
1582 }
1583
1584 error:
1585 return -ENOSYS;
1586 }
1587 #endif /* CONFIG_COMPAT */
1588
1589 static void lttng_stream_override_ring_buffer_fops(void)
1590 {
1591 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1592 lttng_stream_ring_buffer_file_operations.open =
1593 lib_ring_buffer_file_operations.open;
1594 lttng_stream_ring_buffer_file_operations.release =
1595 lib_ring_buffer_file_operations.release;
1596 lttng_stream_ring_buffer_file_operations.poll =
1597 lib_ring_buffer_file_operations.poll;
1598 lttng_stream_ring_buffer_file_operations.splice_read =
1599 lib_ring_buffer_file_operations.splice_read;
1600 lttng_stream_ring_buffer_file_operations.mmap =
1601 lib_ring_buffer_file_operations.mmap;
1602 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1603 lttng_stream_ring_buffer_ioctl;
1604 lttng_stream_ring_buffer_file_operations.llseek =
1605 lib_ring_buffer_file_operations.llseek;
1606 #ifdef CONFIG_COMPAT
1607 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1608 lttng_stream_ring_buffer_compat_ioctl;
1609 #endif
1610 }
1611
1612 int __init lttng_abi_init(void)
1613 {
1614 int ret = 0;
1615
1616 wrapper_vmalloc_sync_all();
1617 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1618 &lttng_fops, NULL);
1619
1620 if (!lttng_proc_dentry) {
1621 printk(KERN_ERR "Error creating LTTng control file\n");
1622 ret = -ENOMEM;
1623 goto error;
1624 }
1625 lttng_stream_override_ring_buffer_fops();
1626
1627 error:
1628 return ret;
1629 }
1630
1631 /* No __exit annotation because used by init error path too. */
1632 void lttng_abi_exit(void)
1633 {
1634 if (lttng_proc_dentry)
1635 remove_proc_entry("lttng", NULL);
1636 }
This page took 0.068796 seconds and 5 git commands to generate.