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