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