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