SoW-2020-0002: Trace Hit Counters
[deliverable/lttng-modules.git] / src / lttng-abi.c
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
9f36eaed 2 *
e8951e63 3 * lttng-abi.c
baf20995 4 *
e8951e63 5 * LTTng ABI
baf20995 6 *
886d51a3
MD
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
baf20995
MD
9 * Mimic system calls for:
10 * - session creation, returns a file descriptor or failure.
ad1c05e1
MD
11 * - channel creation, returns a file descriptor or failure.
12 * - Operates on a session file descriptor
13 * - Takes all channel options as parameters.
14 * - stream get, returns a file descriptor or failure.
15 * - Operates on a channel file descriptor.
16 * - stream notifier get, returns a file descriptor or failure.
17 * - Operates on a channel file descriptor.
18 * - event creation, returns a file descriptor or failure.
19 * - Operates on a channel file descriptor
20 * - Takes an event name as parameter
21 * - Takes an instrumentation source as parameter
22 * - e.g. tracepoints, dynamic_probes...
23 * - Takes instrumentation source specific arguments.
baf20995
MD
24 */
25
11b5a3c2 26#include <linux/module.h>
e6a17f26 27#include <linux/proc_fs.h>
11b5a3c2
MD
28#include <linux/anon_inodes.h>
29#include <linux/file.h>
30#include <linux/uaccess.h>
31#include <linux/slab.h>
abc0446a 32#include <linux/err.h>
263b6c88 33#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
24591303
MD
34#include <ringbuffer/vfs.h>
35#include <ringbuffer/backend.h>
36#include <ringbuffer/frontend.h>
241ae9a8
MD
37#include <wrapper/poll.h>
38#include <wrapper/file.h>
39#include <wrapper/kref.h>
6657edec 40#include <wrapper/barrier.h>
2df37e95
MD
41#include <lttng/string-utils.h>
42#include <lttng/abi.h>
43#include <lttng/abi-old.h>
44#include <lttng/events.h>
45#include <lttng/tracer.h>
46#include <lttng/tp-mempool.h>
24591303 47#include <ringbuffer/frontend_types.h>
db2511b4 48#include <ringbuffer/iterator.h>
baf20995
MD
49
50/*
51 * This is LTTng's own personal way to create a system call as an external
80996790 52 * module. We use ioctl() on /proc/lttng.
baf20995
MD
53 */
54
e6a17f26 55static struct proc_dir_entry *lttng_proc_dentry;
059de147 56
5f4c791e 57#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
059de147
MJ
58static const struct proc_ops lttng_proc_ops;
59#else
60static const struct file_operations lttng_proc_ops;
61#endif
62
ad1c05e1 63static const struct file_operations lttng_session_fops;
750b05f2 64static const struct file_operations lttng_event_notifier_group_fops;
ad1c05e1 65static const struct file_operations lttng_channel_fops;
5dbbdb43 66static const struct file_operations lttng_metadata_fops;
305d3e42 67static const struct file_operations lttng_event_fops;
ed8d02d6 68static struct file_operations lttng_stream_ring_buffer_file_operations;
baf20995 69
9616f0bf 70static int put_u64(uint64_t val, unsigned long arg);
8b97fd42 71static int put_u32(uint32_t val, unsigned long arg);
9616f0bf 72
99f52fcc
FD
73static int validate_zeroed_padding(char *p, size_t len)
74{
75 size_t i;
76
77 for (i = 0; i < len; i++) {
78 if (p[i])
79 return -1;
80 }
81 return 0;
82}
83
a33c9927
MD
84/*
85 * Teardown management: opened file descriptors keep a refcount on the module,
86 * so it can only exit when all file descriptors are closed.
87 */
88
ad1c05e1 89static
baf20995
MD
90int lttng_abi_create_session(void)
91{
a90917c3 92 struct lttng_session *session;
c0e31d2e 93 struct file *session_file;
11b5a3c2 94 int session_fd, ret;
baf20995 95
a90917c3 96 session = lttng_session_create();
baf20995
MD
97 if (!session)
98 return -ENOMEM;
4ac10b76 99 session_fd = lttng_get_unused_fd();
baf20995
MD
100 if (session_fd < 0) {
101 ret = session_fd;
102 goto fd_error;
103 }
c0e31d2e 104 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 105 &lttng_session_fops,
baf20995 106 session, O_RDWR);
c0e31d2e
MD
107 if (IS_ERR(session_file)) {
108 ret = PTR_ERR(session_file);
baf20995
MD
109 goto file_error;
110 }
c0e31d2e
MD
111 session->file = session_file;
112 fd_install(session_fd, session_file);
baf20995
MD
113 return session_fd;
114
115file_error:
116 put_unused_fd(session_fd);
117fd_error:
a90917c3 118 lttng_session_destroy(session);
baf20995
MD
119 return ret;
120}
121
21f58fb7
FD
122void event_notifier_send_notification_work_wakeup(struct irq_work *entry)
123{
124 struct lttng_event_notifier_group *event_notifier_group =
125 container_of(entry, struct lttng_event_notifier_group,
126 wakeup_pending);
127 wake_up_interruptible(&event_notifier_group->read_wait);
128}
129
750b05f2
FD
130static
131int lttng_abi_create_event_notifier_group(void)
132{
133 struct lttng_event_notifier_group *event_notifier_group;
134 struct file *event_notifier_group_file;
135 int event_notifier_group_fd, ret;
136
137 event_notifier_group = lttng_event_notifier_group_create();
138 if (!event_notifier_group)
139 return -ENOMEM;
140
141 event_notifier_group_fd = lttng_get_unused_fd();
142 if (event_notifier_group_fd < 0) {
143 ret = event_notifier_group_fd;
144 goto fd_error;
145 }
146 event_notifier_group_file = anon_inode_getfile("[lttng_event_notifier_group]",
147 &lttng_event_notifier_group_fops,
148 event_notifier_group, O_RDWR);
149 if (IS_ERR(event_notifier_group_file)) {
150 ret = PTR_ERR(event_notifier_group_file);
151 goto file_error;
152 }
153
154 event_notifier_group->file = event_notifier_group_file;
21f58fb7
FD
155 init_waitqueue_head(&event_notifier_group->read_wait);
156 init_irq_work(&event_notifier_group->wakeup_pending,
157 event_notifier_send_notification_work_wakeup);
750b05f2
FD
158 fd_install(event_notifier_group_fd, event_notifier_group_file);
159 return event_notifier_group_fd;
160
161file_error:
162 put_unused_fd(event_notifier_group_fd);
163fd_error:
164 lttng_event_notifier_group_destroy(event_notifier_group);
165 return ret;
166}
167
271b6681
MD
168static
169int lttng_abi_tracepoint_list(void)
170{
171 struct file *tracepoint_list_file;
172 int file_fd, ret;
173
4ac10b76 174 file_fd = lttng_get_unused_fd();
271b6681
MD
175 if (file_fd < 0) {
176 ret = file_fd;
177 goto fd_error;
178 }
30f18bf0 179
8ee099b6 180 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
271b6681
MD
181 &lttng_tracepoint_list_fops,
182 NULL, O_RDWR);
183 if (IS_ERR(tracepoint_list_file)) {
184 ret = PTR_ERR(tracepoint_list_file);
185 goto file_error;
186 }
30f18bf0
MD
187 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
188 if (ret < 0)
189 goto open_error;
271b6681
MD
190 fd_install(file_fd, tracepoint_list_file);
191 return file_fd;
192
30f18bf0
MD
193open_error:
194 fput(tracepoint_list_file);
271b6681
MD
195file_error:
196 put_unused_fd(file_fd);
197fd_error:
198 return ret;
199}
200
f127e61e
MD
201#ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
202static inline
203int lttng_abi_syscall_list(void)
204{
205 return -ENOSYS;
206}
207#else
208static
209int lttng_abi_syscall_list(void)
210{
211 struct file *syscall_list_file;
212 int file_fd, ret;
213
214 file_fd = lttng_get_unused_fd();
215 if (file_fd < 0) {
216 ret = file_fd;
217 goto fd_error;
218 }
219
220 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
221 &lttng_syscall_list_fops,
222 NULL, O_RDWR);
223 if (IS_ERR(syscall_list_file)) {
224 ret = PTR_ERR(syscall_list_file);
225 goto file_error;
226 }
227 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
228 if (ret < 0)
229 goto open_error;
230 fd_install(file_fd, syscall_list_file);
f127e61e
MD
231 return file_fd;
232
233open_error:
234 fput(syscall_list_file);
235file_error:
236 put_unused_fd(file_fd);
237fd_error:
238 return ret;
239}
240#endif
241
80c16bcf 242static
6dccd6c1 243void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
80c16bcf 244{
6dccd6c1
JD
245 v->major = LTTNG_MODULES_MAJOR_VERSION;
246 v->minor = LTTNG_MODULES_MINOR_VERSION;
247 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
80c16bcf
MD
248}
249
42cabb80
MD
250static
251void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v)
252{
253 v->major = LTTNG_MODULES_ABI_MAJOR_VERSION;
254 v->minor = LTTNG_MODULES_ABI_MINOR_VERSION;
255}
256
8070f5c0
MD
257static
258long lttng_abi_add_context(struct file *file,
6dccd6c1 259 struct lttng_kernel_context *context_param,
a90917c3 260 struct lttng_ctx **ctx, struct lttng_session *session)
8070f5c0 261{
8070f5c0
MD
262
263 if (session->been_active)
264 return -EPERM;
265
6dccd6c1 266 switch (context_param->ctx) {
12a313a5 267 case LTTNG_KERNEL_CONTEXT_PID:
8070f5c0 268 return lttng_add_pid_to_ctx(ctx);
a8ad3613
MD
269 case LTTNG_KERNEL_CONTEXT_PRIO:
270 return lttng_add_prio_to_ctx(ctx);
53f1f0ca
MD
271 case LTTNG_KERNEL_CONTEXT_NICE:
272 return lttng_add_nice_to_ctx(ctx);
b64bc438
MD
273 case LTTNG_KERNEL_CONTEXT_VPID:
274 return lttng_add_vpid_to_ctx(ctx);
275 case LTTNG_KERNEL_CONTEXT_TID:
276 return lttng_add_tid_to_ctx(ctx);
277 case LTTNG_KERNEL_CONTEXT_VTID:
278 return lttng_add_vtid_to_ctx(ctx);
279 case LTTNG_KERNEL_CONTEXT_PPID:
280 return lttng_add_ppid_to_ctx(ctx);
281 case LTTNG_KERNEL_CONTEXT_VPPID:
282 return lttng_add_vppid_to_ctx(ctx);
12a313a5 283 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
6dccd6c1
JD
284 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
285 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
286 context_param->u.perf_counter.config,
287 context_param->u.perf_counter.name,
c24a0d71 288 ctx);
a2563e83
MD
289 case LTTNG_KERNEL_CONTEXT_PROCNAME:
290 return lttng_add_procname_to_ctx(ctx);
975da2c0
JD
291 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
292 return lttng_add_hostname_to_ctx(ctx);
b3699d90
MD
293 case LTTNG_KERNEL_CONTEXT_CPU_ID:
294 return lttng_add_cpu_id_to_ctx(ctx);
79150a49
JD
295 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE:
296 return lttng_add_interruptible_to_ctx(ctx);
297 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE:
298 return lttng_add_need_reschedule_to_ctx(ctx);
299 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE:
300 return lttng_add_preemptible_to_ctx(ctx);
301 case LTTNG_KERNEL_CONTEXT_MIGRATABLE:
302 return lttng_add_migratable_to_ctx(ctx);
2fa2d39a
FG
303 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL:
304 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER:
305 return lttng_add_callstack_to_ctx(ctx, context_param->ctx);
a6cf40a4
MJ
306 case LTTNG_KERNEL_CONTEXT_CGROUP_NS:
307 return lttng_add_cgroup_ns_to_ctx(ctx);
308 case LTTNG_KERNEL_CONTEXT_IPC_NS:
309 return lttng_add_ipc_ns_to_ctx(ctx);
310 case LTTNG_KERNEL_CONTEXT_MNT_NS:
311 return lttng_add_mnt_ns_to_ctx(ctx);
312 case LTTNG_KERNEL_CONTEXT_NET_NS:
313 return lttng_add_net_ns_to_ctx(ctx);
314 case LTTNG_KERNEL_CONTEXT_PID_NS:
315 return lttng_add_pid_ns_to_ctx(ctx);
316 case LTTNG_KERNEL_CONTEXT_USER_NS:
317 return lttng_add_user_ns_to_ctx(ctx);
318 case LTTNG_KERNEL_CONTEXT_UTS_NS:
319 return lttng_add_uts_ns_to_ctx(ctx);
dc923e75
MJ
320 case LTTNG_KERNEL_CONTEXT_UID:
321 return lttng_add_uid_to_ctx(ctx);
322 case LTTNG_KERNEL_CONTEXT_EUID:
323 return lttng_add_euid_to_ctx(ctx);
324 case LTTNG_KERNEL_CONTEXT_SUID:
325 return lttng_add_suid_to_ctx(ctx);
326 case LTTNG_KERNEL_CONTEXT_GID:
327 return lttng_add_gid_to_ctx(ctx);
328 case LTTNG_KERNEL_CONTEXT_EGID:
329 return lttng_add_egid_to_ctx(ctx);
330 case LTTNG_KERNEL_CONTEXT_SGID:
331 return lttng_add_sgid_to_ctx(ctx);
332 case LTTNG_KERNEL_CONTEXT_VUID:
333 return lttng_add_vuid_to_ctx(ctx);
334 case LTTNG_KERNEL_CONTEXT_VEUID:
335 return lttng_add_veuid_to_ctx(ctx);
336 case LTTNG_KERNEL_CONTEXT_VSUID:
337 return lttng_add_vsuid_to_ctx(ctx);
338 case LTTNG_KERNEL_CONTEXT_VGID:
339 return lttng_add_vgid_to_ctx(ctx);
340 case LTTNG_KERNEL_CONTEXT_VEGID:
341 return lttng_add_vegid_to_ctx(ctx);
342 case LTTNG_KERNEL_CONTEXT_VSGID:
343 return lttng_add_vsgid_to_ctx(ctx);
876e2e92
MJ
344 case LTTNG_KERNEL_CONTEXT_TIME_NS:
345 return lttng_add_time_ns_to_ctx(ctx);
8070f5c0
MD
346 default:
347 return -EINVAL;
348 }
349}
350
ad1c05e1
MD
351/**
352 * lttng_ioctl - lttng syscall through ioctl
353 *
c0e31d2e 354 * @file: the file
ad1c05e1
MD
355 * @cmd: the command
356 * @arg: command arg
357 *
358 * This ioctl implements lttng commands:
38d024ae 359 * LTTNG_KERNEL_SESSION
ad1c05e1 360 * Returns a LTTng trace session file descriptor
271b6681
MD
361 * LTTNG_KERNEL_TRACER_VERSION
362 * Returns the LTTng kernel tracer version
363 * LTTNG_KERNEL_TRACEPOINT_LIST
364 * Returns a file descriptor listing available tracepoints
360f38ea
MD
365 * LTTNG_KERNEL_WAIT_QUIESCENT
366 * Returns after all previously running probes have completed
42cabb80
MD
367 * LTTNG_KERNEL_TRACER_ABI_VERSION
368 * Returns the LTTng kernel tracer ABI version
750b05f2
FD
369 * LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
370 * Returns a LTTng event notifier group file descriptor
ad1c05e1
MD
371 *
372 * The returned session will be deleted when its file descriptor is closed.
373 */
374static
c0e31d2e 375long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
376{
377 switch (cmd) {
6dccd6c1 378 case LTTNG_KERNEL_OLD_SESSION:
38d024ae 379 case LTTNG_KERNEL_SESSION:
ad1c05e1 380 return lttng_abi_create_session();
750b05f2
FD
381 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE:
382 return lttng_abi_create_event_notifier_group();
6dccd6c1
JD
383 case LTTNG_KERNEL_OLD_TRACER_VERSION:
384 {
385 struct lttng_kernel_tracer_version v;
386 struct lttng_kernel_old_tracer_version oldv;
387 struct lttng_kernel_old_tracer_version *uversion =
388 (struct lttng_kernel_old_tracer_version __user *) arg;
389
390 lttng_abi_tracer_version(&v);
391 oldv.major = v.major;
392 oldv.minor = v.minor;
393 oldv.patchlevel = v.patchlevel;
394
395 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
396 return -EFAULT;
397 return 0;
398 }
80c16bcf 399 case LTTNG_KERNEL_TRACER_VERSION:
6dccd6c1
JD
400 {
401 struct lttng_kernel_tracer_version version;
402 struct lttng_kernel_tracer_version *uversion =
403 (struct lttng_kernel_tracer_version __user *) arg;
404
405 lttng_abi_tracer_version(&version);
42cabb80
MD
406
407 if (copy_to_user(uversion, &version, sizeof(version)))
408 return -EFAULT;
409 return 0;
410 }
411 case LTTNG_KERNEL_TRACER_ABI_VERSION:
412 {
413 struct lttng_kernel_tracer_abi_version version;
414 struct lttng_kernel_tracer_abi_version *uversion =
415 (struct lttng_kernel_tracer_abi_version __user *) arg;
416
417 lttng_abi_tracer_abi_version(&version);
418
6dccd6c1
JD
419 if (copy_to_user(uversion, &version, sizeof(version)))
420 return -EFAULT;
421 return 0;
422 }
423 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
271b6681
MD
424 case LTTNG_KERNEL_TRACEPOINT_LIST:
425 return lttng_abi_tracepoint_list();
2d2464bd
MD
426 case LTTNG_KERNEL_SYSCALL_LIST:
427 return lttng_abi_syscall_list();
6dccd6c1 428 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
5f7f9078
MD
429 case LTTNG_KERNEL_WAIT_QUIESCENT:
430 synchronize_trace();
431 return 0;
6dccd6c1
JD
432 case LTTNG_KERNEL_OLD_CALIBRATE:
433 {
434 struct lttng_kernel_old_calibrate __user *ucalibrate =
435 (struct lttng_kernel_old_calibrate __user *) arg;
436 struct lttng_kernel_old_calibrate old_calibrate;
437 struct lttng_kernel_calibrate calibrate;
438 int ret;
439
440 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
441 return -EFAULT;
442 calibrate.type = old_calibrate.type;
443 ret = lttng_calibrate(&calibrate);
444 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
445 return -EFAULT;
446 return ret;
447 }
57105fc2
MD
448 case LTTNG_KERNEL_CALIBRATE:
449 {
3db41b2c
MD
450 struct lttng_kernel_calibrate __user *ucalibrate =
451 (struct lttng_kernel_calibrate __user *) arg;
452 struct lttng_kernel_calibrate calibrate;
57105fc2
MD
453 int ret;
454
455 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
456 return -EFAULT;
457 ret = lttng_calibrate(&calibrate);
458 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
459 return -EFAULT;
460 return ret;
461 }
ad1c05e1
MD
462 default:
463 return -ENOIOCTLCMD;
464 }
465}
466
5f4c791e 467#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
059de147
MJ
468static const struct proc_ops lttng_proc_ops = {
469 .proc_ioctl = lttng_ioctl,
470#ifdef CONFIG_COMPAT
471 .proc_compat_ioctl = lttng_ioctl,
472#endif /* CONFIG_COMPAT */
473};
474#else
475static const struct file_operations lttng_proc_ops = {
a33c9927 476 .owner = THIS_MODULE,
ad1c05e1
MD
477 .unlocked_ioctl = lttng_ioctl,
478#ifdef CONFIG_COMPAT
03037b98 479 .compat_ioctl = lttng_ioctl,
059de147 480#endif /* CONFIG_COMPAT */
11b5a3c2 481};
059de147 482#endif
ad1c05e1 483
5dbbdb43 484static
c0e31d2e 485int lttng_abi_create_channel(struct file *session_file,
6dccd6c1 486 struct lttng_kernel_channel *chan_param,
5dbbdb43 487 enum channel_type channel_type)
baf20995 488{
a90917c3 489 struct lttng_session *session = session_file->private_data;
88dfd899 490 const struct file_operations *fops = NULL;
5dbbdb43 491 const char *transport_name;
a90917c3 492 struct lttng_channel *chan;
0c023c01 493 struct lttng_event_container *container;
c0e31d2e 494 struct file *chan_file;
baf20995 495 int chan_fd;
ad1c05e1 496 int ret = 0;
baf20995 497
4ac10b76 498 chan_fd = lttng_get_unused_fd();
baf20995
MD
499 if (chan_fd < 0) {
500 ret = chan_fd;
501 goto fd_error;
502 }
88dfd899
MD
503 switch (channel_type) {
504 case PER_CPU_CHANNEL:
505 fops = &lttng_channel_fops;
506 break;
507 case METADATA_CHANNEL:
508 fops = &lttng_metadata_fops;
509 break;
510 }
2470a237 511
c0e31d2e 512 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 513 fops,
03037b98 514 NULL, O_RDWR);
c0e31d2e
MD
515 if (IS_ERR(chan_file)) {
516 ret = PTR_ERR(chan_file);
baf20995
MD
517 goto file_error;
518 }
5dbbdb43
MD
519 switch (channel_type) {
520 case PER_CPU_CHANNEL:
6dccd6c1
JD
521 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
522 transport_name = chan_param->overwrite ?
96ba7208 523 "relay-overwrite" : "relay-discard";
6dccd6c1
JD
524 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
525 transport_name = chan_param->overwrite ?
96ba7208
JD
526 "relay-overwrite-mmap" : "relay-discard-mmap";
527 } else {
528 return -EINVAL;
529 }
5dbbdb43 530 break;
5dbbdb43 531 case METADATA_CHANNEL:
6dccd6c1 532 if (chan_param->output == LTTNG_KERNEL_SPLICE)
96ba7208 533 transport_name = "relay-metadata";
6dccd6c1 534 else if (chan_param->output == LTTNG_KERNEL_MMAP)
96ba7208
JD
535 transport_name = "relay-metadata-mmap";
536 else
537 return -EINVAL;
5dbbdb43
MD
538 break;
539 default:
540 transport_name = "<unknown>";
541 break;
542 }
98d7281c
MJ
543 if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) {
544 ret = -EOVERFLOW;
9c1f4643
MD
545 goto refcount_error;
546 }
03037b98
MD
547 /*
548 * We tolerate no failure path after channel creation. It will stay
549 * invariant for the rest of the session.
550 */
a90917c3 551 chan = lttng_channel_create(session, transport_name, NULL,
6dccd6c1
JD
552 chan_param->subbuf_size,
553 chan_param->num_subbuf,
554 chan_param->switch_timer_interval,
d83004aa
JD
555 chan_param->read_timer_interval,
556 channel_type);
03037b98 557 if (!chan) {
f3d01b96 558 ret = -EINVAL;
03037b98
MD
559 goto chan_error;
560 }
0c023c01
MD
561 container = lttng_channel_get_event_container(chan);
562 container->file = chan_file;
c0e31d2e
MD
563 chan_file->private_data = chan;
564 fd_install(chan_fd, chan_file);
ad1c05e1 565
baf20995
MD
566 return chan_fd;
567
03037b98 568chan_error:
9c1f4643
MD
569 atomic_long_dec(&session_file->f_count);
570refcount_error:
c0e31d2e 571 fput(chan_file);
baf20995
MD
572file_error:
573 put_unused_fd(chan_fd);
574fd_error:
baf20995
MD
575 return ret;
576}
577
7f859fbf
JR
578static
579int lttng_abi_session_set_name(struct lttng_session *session,
580 struct lttng_kernel_session_name *name)
581{
582 size_t len;
583
584 len = strnlen(name->name, LTTNG_KERNEL_SESSION_NAME_LEN);
585
586 if (len == LTTNG_KERNEL_SESSION_NAME_LEN) {
587 /* Name is too long/malformed */
588 return -EINVAL;
589 }
590
591 strcpy(session->name, name->name);
592 return 0;
593}
594
1c88f269
JR
595static
596int lttng_abi_session_set_creation_time(struct lttng_session *session,
597 struct lttng_kernel_session_creation_time *time)
598{
599 size_t len;
600
601 len = strnlen(time->iso8601, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN);
602
603 if (len == LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN) {
604 /* Time is too long/malformed */
605 return -EINVAL;
606 }
607
608 strcpy(session->creation_time, time->iso8601);
609 return 0;
610}
611
0c023c01
MD
612static
613int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param)
614{
615 /* Limit ABI to implemented features. */
616 switch (event_param->instrumentation) {
617 case LTTNG_KERNEL_SYSCALL:
618 switch (event_param->u.syscall.entryexit) {
619 case LTTNG_KERNEL_SYSCALL_ENTRY:
620 case LTTNG_KERNEL_SYSCALL_EXIT:
621 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
622 break;
623 default:
624 return -EINVAL;
625 }
626 switch (event_param->u.syscall.abi) {
627 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
628 break;
629 default:
630 return -EINVAL;
631 }
632 switch (event_param->u.syscall.match) {
633 case LTTNG_KERNEL_SYSCALL_MATCH_NAME:
634 break;
635 default:
636 return -EINVAL;
637 }
638 break;
639
640 case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */
641 case LTTNG_KERNEL_KPROBE: /* Fallthrough */
642 case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */
643 case LTTNG_KERNEL_NOOP: /* Fallthrough */
644 case LTTNG_KERNEL_UPROBE:
645 break;
646
647 case LTTNG_KERNEL_FUNCTION: /* Fallthrough */
648 default:
649 return -EINVAL;
650 }
651 return 0;
652}
653
654static
655int lttng_abi_create_event(struct file *event_container_file,
656 struct lttng_event_container *container,
657 struct lttng_kernel_event *event_param,
658 const struct lttng_counter_key *key)
659{
660 int event_fd, ret;
661 struct file *event_file;
662 void *priv;
663
664 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
665 switch (event_param->instrumentation) {
666 case LTTNG_KERNEL_KRETPROBE:
667 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
668 break;
669 case LTTNG_KERNEL_KPROBE:
670 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
671 break;
672 case LTTNG_KERNEL_FUNCTION:
673 WARN_ON_ONCE(1);
674 /* Not implemented. */
675 break;
676 default:
677 break;
678 }
679 event_fd = lttng_get_unused_fd();
680 if (event_fd < 0) {
681 ret = event_fd;
682 goto fd_error;
683 }
684 event_file = anon_inode_getfile("[lttng_event]",
685 &lttng_event_fops,
686 NULL, O_RDWR);
687 if (IS_ERR(event_file)) {
688 ret = PTR_ERR(event_file);
689 goto file_error;
690 }
691 /* The event holds a reference on the container */
692 if (!atomic_long_add_unless(&event_container_file->f_count, 1, LONG_MAX)) {
693 ret = -EOVERFLOW;
694 goto refcount_error;
695 }
696 ret = lttng_abi_validate_event_param(event_param);
697 if (ret)
698 goto event_error;
699 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
700 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
701 struct lttng_event_enabler *event_enabler;
702
703 if (strutils_is_star_glob_pattern(event_param->name)) {
704 /*
705 * If the event name is a star globbing pattern,
706 * we create the special star globbing enabler.
707 */
708 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
709 event_param, key, container);
710 } else {
711 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
712 event_param, key, container);
713 }
714 priv = event_enabler;
715 } else {
716 struct lttng_event *event;
717
718 /*
719 * We tolerate no failure path after event creation. It
720 * will stay invariant for the rest of the session.
721 */
722 event = lttng_event_create(container, event_param, key,
723 NULL, NULL,
724 event_param->instrumentation, event_param->token);
725 if (IS_ERR(event)) {
726 ret = PTR_ERR(event);
727 goto event_error;
728 }
729 priv = event;
730 }
731 event_file->private_data = priv;
732 fd_install(event_fd, event_file);
733 return event_fd;
734
735event_error:
736 atomic_long_dec(&event_container_file->f_count);
737refcount_error:
738 fput(event_file);
739file_error:
740 put_unused_fd(event_fd);
741fd_error:
742 return ret;
743}
744
99f52fcc
FD
745static
746int lttng_counter_release(struct inode *inode, struct file *file)
747{
748 struct lttng_counter *counter = file->private_data;
749
750 if (counter) {
751 /*
752 * Do not destroy the counter itself. Wait of the owner
753 * (event_notifier group) to be destroyed.
754 */
755 fput(counter->owner);
756 }
757
758 return 0;
759}
760
0c023c01
MD
761static
762int copy_counter_key(struct lttng_counter_key *key,
763 const struct lttng_kernel_counter_key *ukey)
764{
765 size_t i, j, nr_dimensions;
766
767 nr_dimensions = ukey->nr_dimensions;
768 if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX)
769 return -EINVAL;
770 key->nr_dimensions = nr_dimensions;
771 for (i = 0; i < nr_dimensions; i++) {
772 const struct lttng_kernel_counter_key_dimension *udim =
773 &ukey->key_dimensions[i];
774 struct lttng_counter_key_dimension *dim =
775 &key->key_dimensions[i];
776 size_t nr_key_tokens;
777
778 nr_key_tokens = udim->nr_key_tokens;
779 if (!nr_key_tokens || nr_key_tokens > LTTNG_NR_KEY_TOKEN)
780 return -EINVAL;
781 dim->nr_key_tokens = nr_key_tokens;
782 for (j = 0; j < nr_key_tokens; j++) {
783 const struct lttng_kernel_key_token *utoken =
784 &udim->key_tokens[j];
785 struct lttng_key_token *token =
786 &dim->key_tokens[j];
787
788 switch (utoken->type) {
789 case LTTNG_KERNEL_KEY_TOKEN_STRING:
790 {
791 long ret;
792
793 token->type = LTTNG_KEY_TOKEN_STRING;
794 ret = strncpy_from_user(token->arg.string,
795 (char __user *)(unsigned long)utoken->arg.string_ptr,
796 LTTNG_KEY_TOKEN_STRING_LEN_MAX);
797 if (ret < 0)
798 return -EFAULT;
799 if (!ret || ret == LTTNG_KEY_TOKEN_STRING_LEN_MAX)
800 return -EINVAL;
801 break;
802 }
803 case LTTNG_KERNEL_KEY_TOKEN_EVENT_NAME:
804 token->type = LTTNG_KEY_TOKEN_EVENT_NAME;
805 break;
806 case LTTNG_KERNEL_KEY_TOKEN_PROVIDER_NAME:
807 printk(KERN_ERR "LTTng: Provider name token not supported.\n");
808 /* Fallthrough */
809 default:
810 return -EINVAL;
811 }
812 }
813 }
814 return 0;
815}
816
99f52fcc
FD
817static
818long lttng_counter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
819{
820 struct lttng_counter *counter = file->private_data;
0c023c01 821 struct lttng_event_container *container = lttng_counter_get_event_container(counter);
99f52fcc
FD
822 size_t indexes[LTTNG_KERNEL_COUNTER_DIMENSION_MAX] = { 0 };
823 int i;
824
825 switch (cmd) {
826 case LTTNG_KERNEL_COUNTER_READ:
827 {
828 struct lttng_kernel_counter_read local_counter_read;
829 struct lttng_kernel_counter_read __user *ucounter_read =
830 (struct lttng_kernel_counter_read __user *) arg;
831 bool overflow, underflow;
832 int64_t value;
833 int32_t cpu;
834 int ret;
835
836 if (copy_from_user(&local_counter_read, ucounter_read,
837 sizeof(local_counter_read)))
838 return -EFAULT;
839 if (validate_zeroed_padding(local_counter_read.padding,
840 sizeof(local_counter_read.padding)))
841 return -EINVAL;
842
843 /* Cast all indexes into size_t. */
844 for (i = 0; i < local_counter_read.index.number_dimensions; i++)
845 indexes[i] = (size_t) local_counter_read.index.dimension_indexes[i];
846 cpu = local_counter_read.cpu;
847
848 ret = lttng_kernel_counter_read(counter, indexes, cpu, &value,
849 &overflow, &underflow);
850 if (ret)
851 return ret;
852 local_counter_read.value.value = value;
853 local_counter_read.value.overflow = overflow;
854 local_counter_read.value.underflow = underflow;
855
856 if (copy_to_user(&ucounter_read->value, &local_counter_read.value,
857 sizeof(local_counter_read.value)))
858 return -EFAULT;
859
860 return 0;
861 }
862 case LTTNG_KERNEL_COUNTER_AGGREGATE:
863 {
864 struct lttng_kernel_counter_aggregate local_counter_aggregate;
865 struct lttng_kernel_counter_aggregate __user *ucounter_aggregate =
866 (struct lttng_kernel_counter_aggregate __user *) arg;
867 bool overflow, underflow;
868 int64_t value;
869 int ret;
870
871 if (copy_from_user(&local_counter_aggregate, ucounter_aggregate,
872 sizeof(local_counter_aggregate)))
873 return -EFAULT;
874 if (validate_zeroed_padding(local_counter_aggregate.padding,
875 sizeof(local_counter_aggregate.padding)))
876 return -EINVAL;
877
878 /* Cast all indexes into size_t. */
879 for (i = 0; i < local_counter_aggregate.index.number_dimensions; i++)
880 indexes[i] = (size_t) local_counter_aggregate.index.dimension_indexes[i];
881
882 ret = lttng_kernel_counter_aggregate(counter, indexes, &value,
883 &overflow, &underflow);
884 if (ret)
885 return ret;
886 local_counter_aggregate.value.value = value;
887 local_counter_aggregate.value.overflow = overflow;
888 local_counter_aggregate.value.underflow = underflow;
889
890 if (copy_to_user(&ucounter_aggregate->value, &local_counter_aggregate.value,
891 sizeof(local_counter_aggregate.value)))
892 return -EFAULT;
893
894 return 0;
895 }
896 case LTTNG_KERNEL_COUNTER_CLEAR:
897 {
898 struct lttng_kernel_counter_clear local_counter_clear;
899 struct lttng_kernel_counter_clear __user *ucounter_clear =
900 (struct lttng_kernel_counter_clear __user *) arg;
901
902 if (copy_from_user(&local_counter_clear, ucounter_clear,
903 sizeof(local_counter_clear)))
904 return -EFAULT;
905 if (validate_zeroed_padding(local_counter_clear.padding,
906 sizeof(local_counter_clear.padding)))
907 return -EINVAL;
908
909 /* Cast all indexes into size_t. */
910 for (i = 0; i < local_counter_clear.index.number_dimensions; i++)
911 indexes[i] = (size_t) local_counter_clear.index.dimension_indexes[i];
912
913 return lttng_kernel_counter_clear(counter, indexes);
914 }
0c023c01
MD
915 case LTTNG_KERNEL_COUNTER_EVENT:
916 {
917 struct lttng_kernel_counter_event *ucounter_event_param;
918 struct lttng_counter_key *key;
919 int ret;
920
921 key = kzalloc(sizeof(*key), GFP_KERNEL);
922 if (!key)
923 return -ENOMEM;
924 ucounter_event_param = kzalloc(sizeof(*ucounter_event_param), GFP_KERNEL);
925 if (!ucounter_event_param) {
926 ret = -ENOMEM;
927 goto free_key;
928 }
929 if (copy_from_user(ucounter_event_param,
930 (struct lttng_kernel_counter_event __user *) arg,
931 sizeof(*ucounter_event_param))) {
932 ret = -EFAULT;
933 goto free_param;
934 }
935 ret = copy_counter_key(key, &ucounter_event_param->key);
936 if (ret)
937 goto free_param;
938 ret = lttng_abi_create_event(file, container, &ucounter_event_param->event, key);
939 free_param:
940 kfree(ucounter_event_param);
941 free_key:
942 kfree(key);
943 return ret;
944 }
945 case LTTNG_KERNEL_ENABLE:
946 return lttng_event_container_enable(container);
947 case LTTNG_KERNEL_DISABLE:
948 return lttng_event_container_disable(container);
949 case LTTNG_KERNEL_SYSCALL_MASK:
950 return lttng_event_container_syscall_mask(container,
951 (struct lttng_kernel_syscall_mask __user *) arg);
952 case LTTNG_KERNEL_COUNTER_MAP_NR_DESCRIPTORS:
953 {
954 uint64_t __user *user_nr_descriptors = (uint64_t __user *) arg;
955 uint64_t nr_descriptors;
956
957 mutex_lock(&counter->map.lock);
958 nr_descriptors = counter->map.nr_descriptors;
959 mutex_unlock(&counter->map.lock);
960 return put_user(nr_descriptors, user_nr_descriptors);
961 }
962 case LTTNG_KERNEL_COUNTER_MAP_DESCRIPTOR:
963 {
964 struct lttng_kernel_counter_map_descriptor __user *user_descriptor =
965 (struct lttng_kernel_counter_map_descriptor __user *) arg;
966 struct lttng_kernel_counter_map_descriptor local_descriptor;
967 struct lttng_counter_map_descriptor *kernel_descriptor;
968 int ret;
969
970 if (copy_from_user(&local_descriptor, user_descriptor,
971 sizeof(local_descriptor)))
972 return -EFAULT;
973 if (validate_zeroed_padding(local_descriptor.padding,
974 sizeof(local_descriptor.padding)))
975 return -EINVAL;
976
977 mutex_lock(&counter->map.lock);
978 if (local_descriptor.descriptor_index >= counter->map.nr_descriptors) {
979 ret = -EOVERFLOW;
980 goto map_descriptor_error_unlock;
981 }
982 kernel_descriptor = &counter->map.descriptors[local_descriptor.descriptor_index];
983 local_descriptor.user_token = kernel_descriptor->user_token;
984 local_descriptor.array_index = kernel_descriptor->array_index;
985 memcpy(local_descriptor.key, kernel_descriptor->key, LTTNG_KERNEL_COUNTER_KEY_LEN);
986 mutex_unlock(&counter->map.lock);
987
988 if (copy_to_user(user_descriptor, &local_descriptor,
989 sizeof(local_descriptor)))
990 return -EFAULT;
991
992 return 0;
993
994 map_descriptor_error_unlock:
995 mutex_unlock(&counter->map.lock);
996 return ret;
997 }
99f52fcc
FD
998 default:
999 WARN_ON_ONCE(1);
1000 return -ENOSYS;
1001 }
1002}
1003
1004static const struct file_operations lttng_counter_fops = {
1005 .owner = THIS_MODULE,
1006 .release = lttng_counter_release,
1007 .unlocked_ioctl = lttng_counter_ioctl,
1008#ifdef CONFIG_COMPAT
1009 .compat_ioctl = lttng_counter_ioctl,
1010#endif
1011};
1012
0c023c01
MD
1013static
1014long lttng_abi_session_create_counter(
1015 struct lttng_session *session,
1016 const struct lttng_kernel_counter_conf *counter_conf)
1017{
1018 int counter_fd, ret, i;
1019 char *counter_transport_name;
1020 struct lttng_event_container *container;
1021 struct lttng_counter *counter;
1022 struct file *counter_file;
1023 size_t dimension_sizes[LTTNG_KERNEL_COUNTER_DIMENSION_MAX] = { 0 };
1024 size_t number_dimensions;
1025
1026 counter_fd = lttng_get_unused_fd();
1027 if (counter_fd < 0) {
1028 ret = counter_fd;
1029 goto fd_error;
1030 }
1031
1032 counter_file = anon_inode_getfile("[lttng_counter]",
1033 &lttng_counter_fops,
1034 NULL, O_RDONLY);
1035 if (IS_ERR(counter_file)) {
1036 ret = PTR_ERR(counter_file);
1037 goto file_error;
1038 }
1039
1040 if (counter_conf->arithmetic != LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR) {
1041 printk(KERN_ERR "LTTng: Map: Error counter of the wrong type.\n");
1042 return -EINVAL;
1043 }
1044
1045 switch (counter_conf->bitness) {
1046 case LTTNG_KERNEL_COUNTER_BITNESS_64:
1047 counter_transport_name = "counter-per-cpu-64-modular";
1048 break;
1049 case LTTNG_KERNEL_COUNTER_BITNESS_32:
1050 counter_transport_name = "counter-per-cpu-32-modular";
1051 break;
1052 default:
1053 return -EINVAL;
1054 }
1055
1056 number_dimensions = (size_t) counter_conf->number_dimensions;
1057
1058 for (i = 0; i < counter_conf->number_dimensions; i++) {
1059 if (counter_conf->dimensions[i].has_underflow)
1060 return -EINVAL;
1061 if (counter_conf->dimensions[i].has_overflow)
1062 return -EINVAL;
1063 dimension_sizes[i] = counter_conf->dimensions[i].size;
1064 }
1065
1066 if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) {
1067 ret = -EOVERFLOW;
1068 goto refcount_error;
1069 }
1070
1071 counter = lttng_session_create_counter(session,
1072 counter_transport_name,
1073 number_dimensions, dimension_sizes,
1074 counter_conf->coalesce_hits);
1075 if (!counter) {
1076 ret = -EINVAL;
1077 goto counter_error;
1078 }
1079
1080 counter->owner = session->file;
1081 container = lttng_counter_get_event_container(counter);
1082 container->file = counter_file;
1083 counter_file->private_data = counter;
1084
1085 fd_install(counter_fd, counter_file);
1086
1087 return counter_fd;
1088
1089counter_error:
1090 atomic_long_dec(&session->file->f_count);
1091refcount_error:
1092 fput(counter_file);
1093file_error:
1094 put_unused_fd(counter_fd);
1095fd_error:
1096 return ret;
1097}
99f52fcc 1098
d1f652f8
MD
1099static
1100enum tracker_type get_tracker_type(struct lttng_kernel_tracker_args *tracker)
1101{
1102 switch (tracker->type) {
1103 case LTTNG_KERNEL_TRACKER_PID:
1104 return TRACKER_PID;
1105 case LTTNG_KERNEL_TRACKER_VPID:
1106 return TRACKER_VPID;
1107 case LTTNG_KERNEL_TRACKER_UID:
1108 return TRACKER_UID;
1109 case LTTNG_KERNEL_TRACKER_VUID:
1110 return TRACKER_VUID;
1111 case LTTNG_KERNEL_TRACKER_GID:
1112 return TRACKER_GID;
1113 case LTTNG_KERNEL_TRACKER_VGID:
1114 return TRACKER_VGID;
1115 default:
1116 return TRACKER_UNKNOWN;
1117 }
1118}
1119
baf20995 1120/**
ad1c05e1 1121 * lttng_session_ioctl - lttng session fd ioctl
baf20995 1122 *
c0e31d2e 1123 * @file: the file
baf20995
MD
1124 * @cmd: the command
1125 * @arg: command arg
1126 *
1127 * This ioctl implements lttng commands:
38d024ae 1128 * LTTNG_KERNEL_CHANNEL
baf20995 1129 * Returns a LTTng channel file descriptor
e64957da
MD
1130 * LTTNG_KERNEL_ENABLE
1131 * Enables tracing for a session (weak enable)
1132 * LTTNG_KERNEL_DISABLE
1133 * Disables tracing for a session (strong disable)
8070f5c0
MD
1134 * LTTNG_KERNEL_METADATA
1135 * Returns a LTTng metadata file descriptor
e0130fab 1136 * LTTNG_KERNEL_SESSION_TRACK_PID
d1f652f8 1137 * Add PID to session PID tracker
e0130fab 1138 * LTTNG_KERNEL_SESSION_UNTRACK_PID
d1f652f8
MD
1139 * Remove PID from session PID tracker
1140 * LTTNG_KERNEL_SESSION_TRACK_ID
1141 * Add ID to tracker
1142 * LTTNG_KERNEL_SESSION_UNTRACK_ID
1143 * Remove ID from tracker
ad1c05e1
MD
1144 *
1145 * The returned channel will be deleted when its file descriptor is closed.
1146 */
1147static
c0e31d2e 1148long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 1149{
a90917c3 1150 struct lttng_session *session = file->private_data;
2d4c4d15
MD
1151 struct lttng_kernel_channel chan_param;
1152 struct lttng_kernel_old_channel old_chan_param;
c0e31d2e 1153
ad1c05e1 1154 switch (cmd) {
6dccd6c1
JD
1155 case LTTNG_KERNEL_OLD_CHANNEL:
1156 {
6dccd6c1
JD
1157 if (copy_from_user(&old_chan_param,
1158 (struct lttng_kernel_old_channel __user *) arg,
1159 sizeof(struct lttng_kernel_old_channel)))
1160 return -EFAULT;
1161 chan_param.overwrite = old_chan_param.overwrite;
1162 chan_param.subbuf_size = old_chan_param.subbuf_size;
1163 chan_param.num_subbuf = old_chan_param.num_subbuf;
1164 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
1165 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
1166 chan_param.output = old_chan_param.output;
1167
1168 return lttng_abi_create_channel(file, &chan_param,
1169 PER_CPU_CHANNEL);
1170 }
38d024ae 1171 case LTTNG_KERNEL_CHANNEL:
6dccd6c1 1172 {
6dccd6c1 1173 if (copy_from_user(&chan_param,
38d024ae 1174 (struct lttng_kernel_channel __user *) arg,
6dccd6c1
JD
1175 sizeof(struct lttng_kernel_channel)))
1176 return -EFAULT;
1177 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 1178 PER_CPU_CHANNEL);
6dccd6c1
JD
1179 }
1180 case LTTNG_KERNEL_OLD_SESSION_START:
1181 case LTTNG_KERNEL_OLD_ENABLE:
38d024ae 1182 case LTTNG_KERNEL_SESSION_START:
e64957da 1183 case LTTNG_KERNEL_ENABLE:
a90917c3 1184 return lttng_session_enable(session);
6dccd6c1
JD
1185 case LTTNG_KERNEL_OLD_SESSION_STOP:
1186 case LTTNG_KERNEL_OLD_DISABLE:
38d024ae 1187 case LTTNG_KERNEL_SESSION_STOP:
e64957da 1188 case LTTNG_KERNEL_DISABLE:
a90917c3 1189 return lttng_session_disable(session);
6dccd6c1
JD
1190 case LTTNG_KERNEL_OLD_METADATA:
1191 {
6dccd6c1
JD
1192 if (copy_from_user(&old_chan_param,
1193 (struct lttng_kernel_old_channel __user *) arg,
1194 sizeof(struct lttng_kernel_old_channel)))
1195 return -EFAULT;
1196 chan_param.overwrite = old_chan_param.overwrite;
1197 chan_param.subbuf_size = old_chan_param.subbuf_size;
1198 chan_param.num_subbuf = old_chan_param.num_subbuf;
1199 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
1200 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
1201 chan_param.output = old_chan_param.output;
1202
1203 return lttng_abi_create_channel(file, &chan_param,
1204 METADATA_CHANNEL);
1205 }
38d024ae 1206 case LTTNG_KERNEL_METADATA:
6dccd6c1 1207 {
6dccd6c1
JD
1208 if (copy_from_user(&chan_param,
1209 (struct lttng_kernel_channel __user *) arg,
1210 sizeof(struct lttng_kernel_channel)))
1211 return -EFAULT;
1212 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 1213 METADATA_CHANNEL);
6dccd6c1 1214 }
e0130fab 1215 case LTTNG_KERNEL_SESSION_TRACK_PID:
d1f652f8 1216 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
e0130fab 1217 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
d1f652f8
MD
1218 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
1219 case LTTNG_KERNEL_SESSION_TRACK_ID:
1220 {
1221 struct lttng_kernel_tracker_args tracker;
1222 enum tracker_type tracker_type;
1223
1224 if (copy_from_user(&tracker,
1225 (struct lttng_kernel_tracker_args __user *) arg,
1226 sizeof(struct lttng_kernel_tracker_args)))
1227 return -EFAULT;
1228 tracker_type = get_tracker_type(&tracker);
1229 if (tracker_type == TRACKER_UNKNOWN)
1230 return -EINVAL;
1231 return lttng_session_track_id(session, tracker_type, tracker.id);
1232 }
1233 case LTTNG_KERNEL_SESSION_UNTRACK_ID:
1234 {
1235 struct lttng_kernel_tracker_args tracker;
1236 enum tracker_type tracker_type;
1237
1238 if (copy_from_user(&tracker,
1239 (struct lttng_kernel_tracker_args __user *) arg,
1240 sizeof(struct lttng_kernel_tracker_args)))
1241 return -EFAULT;
1242 tracker_type = get_tracker_type(&tracker);
1243 if (tracker_type == TRACKER_UNKNOWN)
1244 return -EINVAL;
1245 return lttng_session_untrack_id(session, tracker_type,
1246 tracker.id);
1247 }
7e6f9ef6 1248 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
d1f652f8
MD
1249 return lttng_session_list_tracker_ids(session, TRACKER_PID);
1250 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS:
1251 {
1252 struct lttng_kernel_tracker_args tracker;
1253 enum tracker_type tracker_type;
1254
1255 if (copy_from_user(&tracker,
1256 (struct lttng_kernel_tracker_args __user *) arg,
1257 sizeof(struct lttng_kernel_tracker_args)))
1258 return -EFAULT;
1259 tracker_type = get_tracker_type(&tracker);
1260 if (tracker_type == TRACKER_UNKNOWN)
1261 return -EINVAL;
1262 return lttng_session_list_tracker_ids(session, tracker_type);
1263 }
9616f0bf
JD
1264 case LTTNG_KERNEL_SESSION_METADATA_REGEN:
1265 return lttng_session_metadata_regenerate(session);
601252cf
MD
1266 case LTTNG_KERNEL_SESSION_STATEDUMP:
1267 return lttng_session_statedump(session);
7f859fbf
JR
1268 case LTTNG_KERNEL_SESSION_SET_NAME:
1269 {
1270 struct lttng_kernel_session_name name;
1271
1272 if (copy_from_user(&name,
1273 (struct lttng_kernel_session_name __user *) arg,
1274 sizeof(struct lttng_kernel_session_name)))
1275 return -EFAULT;
1276 return lttng_abi_session_set_name(session, &name);
1277 }
1c88f269
JR
1278 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME:
1279 {
1280 struct lttng_kernel_session_creation_time time;
1281
1282 if (copy_from_user(&time,
1283 (struct lttng_kernel_session_creation_time __user *) arg,
1284 sizeof(struct lttng_kernel_session_creation_time)))
1285 return -EFAULT;
1286 return lttng_abi_session_set_creation_time(session, &time);
1287 }
0c023c01
MD
1288 case LTTNG_KERNEL_COUNTER:
1289 {
1290 struct lttng_kernel_counter_conf ucounter_conf;
1291
1292 if (copy_from_user(&ucounter_conf,
1293 (struct lttng_kernel_counter_conf __user *) arg,
1294 sizeof(ucounter_conf)))
1295 return -EFAULT;
1296 return lttng_abi_session_create_counter(session,
1297 &ucounter_conf);
1298 }
ad1c05e1
MD
1299 default:
1300 return -ENOIOCTLCMD;
1301 }
1302}
1303
03037b98
MD
1304/*
1305 * Called when the last file reference is dropped.
1306 *
1307 * Big fat note: channels and events are invariant for the whole session after
1308 * their creation. So this session destruction also destroys all channel and
1309 * event structures specific to this session (they are not destroyed when their
1310 * individual file is released).
1311 */
ad1c05e1 1312static
03037b98 1313int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 1314{
a90917c3 1315 struct lttng_session *session = file->private_data;
c269fff4
MD
1316
1317 if (session)
a90917c3 1318 lttng_session_destroy(session);
11b5a3c2 1319 return 0;
ad1c05e1 1320}
ad1c05e1
MD
1321
1322static const struct file_operations lttng_session_fops = {
a33c9927 1323 .owner = THIS_MODULE,
03037b98 1324 .release = lttng_session_release,
ad1c05e1
MD
1325 .unlocked_ioctl = lttng_session_ioctl,
1326#ifdef CONFIG_COMPAT
03037b98 1327 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 1328#endif
11b5a3c2 1329};
ad1c05e1 1330
db2511b4
MD
1331/*
1332 * When encountering empty buffer, flush current sub-buffer if non-empty
1333 * and retry (if new data available to read after flush).
1334 */
1335static
1336ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf,
1337 size_t count, loff_t *ppos)
1338{
1339 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
1340 struct channel *chan = event_notifier_group->chan;
1341 struct lib_ring_buffer *buf = event_notifier_group->buf;
1342 ssize_t read_count = 0, len;
1343 size_t read_offset;
1344
1345 might_sleep();
1346 if (!lttng_access_ok(VERIFY_WRITE, user_buf, count))
1347 return -EFAULT;
1348
1349 /* Finish copy of previous record */
1350 if (*ppos != 0) {
1351 if (read_count < count) {
1352 len = chan->iter.len_left;
1353 read_offset = *ppos;
1354 goto skip_get_next;
1355 }
1356 }
1357
1358 while (read_count < count) {
1359 size_t copy_len, space_left;
1360
1361 len = lib_ring_buffer_get_next_record(chan, buf);
1362len_test:
1363 if (len < 0) {
1364 /*
1365 * Check if buffer is finalized (end of file).
1366 */
1367 if (len == -ENODATA) {
1368 /* A 0 read_count will tell about end of file */
1369 goto nodata;
1370 }
1371 if (filp->f_flags & O_NONBLOCK) {
1372 if (!read_count)
1373 read_count = -EAGAIN;
1374 goto nodata;
1375 } else {
1376 int error;
1377
1378 /*
1379 * No data available at the moment, return what
1380 * we got.
1381 */
1382 if (read_count)
1383 goto nodata;
1384
1385 /*
1386 * Wait for returned len to be >= 0 or -ENODATA.
1387 */
1388 error = wait_event_interruptible(
1389 event_notifier_group->read_wait,
1390 ((len = lib_ring_buffer_get_next_record(
1391 chan, buf)), len != -EAGAIN));
1392 CHAN_WARN_ON(chan, len == -EBUSY);
1393 if (error) {
1394 read_count = error;
1395 goto nodata;
1396 }
1397 CHAN_WARN_ON(chan, len < 0 && len != -ENODATA);
1398 goto len_test;
1399 }
1400 }
1401 read_offset = buf->iter.read_offset;
1402skip_get_next:
1403 space_left = count - read_count;
1404 if (len <= space_left) {
1405 copy_len = len;
1406 chan->iter.len_left = 0;
1407 *ppos = 0;
1408 } else {
1409 copy_len = space_left;
1410 chan->iter.len_left = len - copy_len;
1411 *ppos = read_offset + copy_len;
1412 }
1413 if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset,
1414 &user_buf[read_count],
1415 copy_len)) {
1416 /*
1417 * Leave the len_left and ppos values at their current
1418 * state, as we currently have a valid event to read.
1419 */
1420 return -EFAULT;
1421 }
1422 read_count += copy_len;
1423 }
31c02fb7 1424 goto put_record;
db2511b4
MD
1425
1426nodata:
1427 *ppos = 0;
1428 chan->iter.len_left = 0;
31c02fb7
MD
1429
1430put_record:
1431 lib_ring_buffer_put_current_record(buf);
db2511b4
MD
1432 return read_count;
1433}
1434
1435/*
1436 * If the ring buffer is non empty (even just a partial subbuffer), return that
1437 * there is data available. Perform a ring buffer flush if we encounter a
1438 * non-empty ring buffer which does not have any consumeable subbuffer available.
1439 */
1440static
1441unsigned int lttng_event_notifier_group_notif_poll(struct file *filp,
1442 poll_table *wait)
1443{
1444 unsigned int mask = 0;
1445 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
1446 struct channel *chan = event_notifier_group->chan;
1447 struct lib_ring_buffer *buf = event_notifier_group->buf;
1448 const struct lib_ring_buffer_config *config = &chan->backend.config;
1449 int finalized, disabled;
1450 unsigned long consumed, offset;
18f12d55 1451 size_t subbuffer_header_size = config->cb.subbuffer_header_size();
db2511b4
MD
1452
1453 if (filp->f_mode & FMODE_READ) {
1454 poll_wait_set_exclusive(wait);
1455 poll_wait(filp, &event_notifier_group->read_wait, wait);
1456
1457 finalized = lib_ring_buffer_is_finalized(config, buf);
1458 disabled = lib_ring_buffer_channel_is_disabled(chan);
1459
1460 /*
1461 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
1462 * finalized load before offsets loads.
1463 */
1464 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1465retry:
1466 if (disabled)
1467 return POLLERR;
1468
1469 offset = lib_ring_buffer_get_offset(config, buf);
1470 consumed = lib_ring_buffer_get_consumed(config, buf);
1471
1472 /*
1473 * If there is no buffer available to consume.
1474 */
1475 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) {
1476 /*
1477 * If there is a non-empty subbuffer, flush and try again.
1478 */
18f12d55 1479 if (subbuf_offset(offset, chan) > subbuffer_header_size) {
db2511b4
MD
1480 lib_ring_buffer_switch_remote(buf);
1481 goto retry;
1482 }
1483
1484 if (finalized)
1485 return POLLHUP;
1486 else {
1487 /*
1488 * The memory barriers
1489 * __wait_event()/wake_up_interruptible() take
1490 * care of "raw_spin_is_locked" memory ordering.
1491 */
1492 if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1493 goto retry;
1494 else
1495 return 0;
1496 }
1497 } else {
1498 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan)
1499 >= chan->backend.buf_size)
1500 return POLLPRI | POLLRDBAND;
1501 else
1502 return POLLIN | POLLRDNORM;
1503 }
1504 }
1505
1506 return mask;
1507}
1508
1509/**
1510 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
1511 * @inode: opened inode
1512 * @file: opened file
1513 *
1514 * Open implementation. Makes sure only one open instance of a buffer is
1515 * done at a given moment.
1516 */
1517static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file)
1518{
1519 struct lttng_event_notifier_group *event_notifier_group = inode->i_private;
1520 struct lib_ring_buffer *buf = event_notifier_group->buf;
1521
1522 file->private_data = event_notifier_group;
1523 return lib_ring_buffer_open(inode, file, buf);
1524}
1525
1526/**
1527 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1528 * @inode: opened inode
1529 * @file: opened file
1530 *
1531 * Release implementation.
1532 */
1533static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file)
1534{
1535 struct lttng_event_notifier_group *event_notifier_group = file->private_data;
1536 struct lib_ring_buffer *buf = event_notifier_group->buf;
1537 int ret;
1538
1539 ret = lib_ring_buffer_release(inode, file, buf);
1540 if (ret)
1541 return ret;
1542 fput(event_notifier_group->file);
1543 return 0;
1544}
1545
21f58fb7
FD
1546static const struct file_operations lttng_event_notifier_group_notif_fops = {
1547 .owner = THIS_MODULE,
db2511b4
MD
1548 .open = lttng_event_notifier_group_notif_open,
1549 .release = lttng_event_notifier_group_notif_release,
1550 .read = lttng_event_notifier_group_notif_read,
1551 .poll = lttng_event_notifier_group_notif_poll,
21f58fb7
FD
1552};
1553
d83004aa
JD
1554/**
1555 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1556 * @filp: the file
1557 * @wait: poll table
1558 *
1559 * Handles the poll operations for the metadata channels.
1560 */
ad1c05e1 1561static
d83004aa
JD
1562unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
1563 poll_table *wait)
1564{
1565 struct lttng_metadata_stream *stream = filp->private_data;
1566 struct lib_ring_buffer *buf = stream->priv;
1567 int finalized;
1568 unsigned int mask = 0;
1569
1570 if (filp->f_mode & FMODE_READ) {
1571 poll_wait_set_exclusive(wait);
1572 poll_wait(filp, &stream->read_wait, wait);
1573
1574 finalized = stream->finalized;
1575
1576 /*
1577 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1578 * ordering finalized load before offsets loads.
1579 */
1580 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1581
1582 if (finalized)
1583 mask |= POLLHUP;
1584
92d9f5e6 1585 mutex_lock(&stream->metadata_cache->lock);
d83004aa 1586 if (stream->metadata_cache->metadata_written >
f613e3e6 1587 stream->metadata_out)
d83004aa 1588 mask |= POLLIN;
92d9f5e6 1589 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
1590 }
1591
1592 return mask;
1593}
1594
f613e3e6
MD
1595static
1596void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
1597 unsigned int cmd, unsigned long arg)
1598{
1599 struct lttng_metadata_stream *stream = filp->private_data;
1600
1601 stream->metadata_out = stream->metadata_in;
1602}
1603
d1344afa
JD
1604/*
1605 * Reset the counter of how much metadata has been consumed to 0. That way,
1606 * the consumer receives the content of the metadata cache unchanged. This is
1607 * different from the metadata_regenerate where the offset from epoch is
1608 * resampled, here we want the exact same content as the last time the metadata
1609 * was generated. This command is only possible if all the metadata written
1610 * in the cache has been output to the metadata stream to avoid corrupting the
1611 * metadata file.
1612 *
1613 * Return 0 on success, a negative value on error.
1614 */
1615static
1616int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
1617{
1618 int ret;
1619 struct lttng_metadata_cache *cache = stream->metadata_cache;
1620
1621 mutex_lock(&cache->lock);
1622 if (stream->metadata_out != cache->metadata_written) {
1623 ret = -EBUSY;
1624 goto end;
1625 }
1626 stream->metadata_out = 0;
1627 stream->metadata_in = 0;
1628 wake_up_interruptible(&stream->read_wait);
1629 ret = 0;
1630
1631end:
1632 mutex_unlock(&cache->lock);
1633 return ret;
1634}
1635
d83004aa
JD
1636static
1637long lttng_metadata_ring_buffer_ioctl(struct file *filp,
1638 unsigned int cmd, unsigned long arg)
1639{
1640 int ret;
1641 struct lttng_metadata_stream *stream = filp->private_data;
1642 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
1643 unsigned int rb_cmd;
1644 bool coherent;
1645
1646 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1647 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
1648 else
1649 rb_cmd = cmd;
d83004aa
JD
1650
1651 switch (cmd) {
d83004aa
JD
1652 case RING_BUFFER_GET_NEXT_SUBBUF:
1653 {
35097f36
JD
1654 struct lttng_metadata_stream *stream = filp->private_data;
1655 struct lib_ring_buffer *buf = stream->priv;
1656 struct channel *chan = buf->backend.chan;
1657
8b97fd42 1658 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1659 if (ret > 0) {
1660 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1661 ret = 0;
1662 } else if (ret < 0)
d83004aa
JD
1663 goto err;
1664 break;
1665 }
f613e3e6
MD
1666 case RING_BUFFER_GET_SUBBUF:
1667 {
1668 /*
1669 * Random access is not allowed for metadata channel.
1670 */
1671 return -ENOSYS;
1672 }
c6f05468 1673 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
35097f36
JD
1674 case RING_BUFFER_FLUSH:
1675 {
1676 struct lttng_metadata_stream *stream = filp->private_data;
1677 struct lib_ring_buffer *buf = stream->priv;
1678 struct channel *chan = buf->backend.chan;
1679
1680 /*
1681 * Before doing the actual ring buffer flush, write up to one
1682 * packet of metadata in the ring buffer.
1683 */
8b97fd42 1684 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1685 if (ret < 0)
1686 goto err;
1687 break;
1688 }
9616f0bf
JD
1689 case RING_BUFFER_GET_METADATA_VERSION:
1690 {
1691 struct lttng_metadata_stream *stream = filp->private_data;
1692
1693 return put_u64(stream->version, arg);
1694 }
d1344afa
JD
1695 case RING_BUFFER_METADATA_CACHE_DUMP:
1696 {
1697 struct lttng_metadata_stream *stream = filp->private_data;
1698
1699 return lttng_metadata_cache_dump(stream);
1700 }
8b97fd42
MD
1701 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1702 {
1703 struct lttng_metadata_stream *stream = filp->private_data;
1704 struct lib_ring_buffer *buf = stream->priv;
1705 struct channel *chan = buf->backend.chan;
1706
1707 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1708 if (ret > 0) {
1709 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1710 ret = 0;
1711 } else if (ret < 0) {
1712 goto err;
1713 }
1714 break;
1715 }
d83004aa
JD
1716 default:
1717 break;
1718 }
f613e3e6
MD
1719 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1720
d83004aa 1721 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1722 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1723 if (ret < 0)
1724 goto err;
d83004aa 1725
f613e3e6
MD
1726 switch (cmd) {
1727 case RING_BUFFER_PUT_NEXT_SUBBUF:
1728 {
1729 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1730 cmd, arg);
1731 break;
1732 }
8b97fd42
MD
1733 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1734 {
1735 return put_u32(coherent, arg);
1736 }
f613e3e6
MD
1737 default:
1738 break;
1739 }
d83004aa
JD
1740err:
1741 return ret;
1742}
1743
aeb9064d 1744#ifdef CONFIG_COMPAT
d83004aa
JD
1745static
1746long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
1747 unsigned int cmd, unsigned long arg)
1748{
1749 int ret;
1750 struct lttng_metadata_stream *stream = filp->private_data;
1751 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
1752 unsigned int rb_cmd;
1753 bool coherent;
1754
1755 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1756 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
1757 else
1758 rb_cmd = cmd;
d83004aa
JD
1759
1760 switch (cmd) {
d83004aa
JD
1761 case RING_BUFFER_GET_NEXT_SUBBUF:
1762 {
35097f36
JD
1763 struct lttng_metadata_stream *stream = filp->private_data;
1764 struct lib_ring_buffer *buf = stream->priv;
1765 struct channel *chan = buf->backend.chan;
1766
8b97fd42 1767 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1768 if (ret > 0) {
1769 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1770 ret = 0;
1771 } else if (ret < 0)
d83004aa
JD
1772 goto err;
1773 break;
1774 }
f613e3e6
MD
1775 case RING_BUFFER_GET_SUBBUF:
1776 {
1777 /*
1778 * Random access is not allowed for metadata channel.
1779 */
1780 return -ENOSYS;
1781 }
c6f05468 1782 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
96c55c2f
MD
1783 case RING_BUFFER_FLUSH:
1784 {
1785 struct lttng_metadata_stream *stream = filp->private_data;
1786 struct lib_ring_buffer *buf = stream->priv;
1787 struct channel *chan = buf->backend.chan;
1788
1789 /*
1790 * Before doing the actual ring buffer flush, write up to one
1791 * packet of metadata in the ring buffer.
1792 */
8b97fd42 1793 ret = lttng_metadata_output_channel(stream, chan, NULL);
96c55c2f
MD
1794 if (ret < 0)
1795 goto err;
1796 break;
1797 }
1798 case RING_BUFFER_GET_METADATA_VERSION:
1799 {
1800 struct lttng_metadata_stream *stream = filp->private_data;
1801
1802 return put_u64(stream->version, arg);
1803 }
d1344afa
JD
1804 case RING_BUFFER_METADATA_CACHE_DUMP:
1805 {
1806 struct lttng_metadata_stream *stream = filp->private_data;
1807
1808 return lttng_metadata_cache_dump(stream);
1809 }
8b97fd42
MD
1810 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1811 {
1812 struct lttng_metadata_stream *stream = filp->private_data;
1813 struct lib_ring_buffer *buf = stream->priv;
1814 struct channel *chan = buf->backend.chan;
1815
1816 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1817 if (ret > 0) {
1818 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1819 ret = 0;
1820 } else if (ret < 0) {
1821 goto err;
1822 }
1823 break;
1824 }
d83004aa
JD
1825 default:
1826 break;
1827 }
f613e3e6
MD
1828 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1829
d83004aa 1830 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1831 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1832 if (ret < 0)
1833 goto err;
d83004aa 1834
f613e3e6
MD
1835 switch (cmd) {
1836 case RING_BUFFER_PUT_NEXT_SUBBUF:
1837 {
1838 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1839 cmd, arg);
1840 break;
1841 }
8b97fd42
MD
1842 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1843 {
1844 return put_u32(coherent, arg);
1845 }
f613e3e6
MD
1846 default:
1847 break;
1848 }
d83004aa
JD
1849err:
1850 return ret;
1851}
aeb9064d 1852#endif
d83004aa 1853
b3b8072b
MD
1854/*
1855 * This is not used by anonymous file descriptors. This code is left
1856 * there if we ever want to implement an inode with open() operation.
1857 */
d83004aa
JD
1858static
1859int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1860{
1861 struct lttng_metadata_stream *stream = inode->i_private;
1862 struct lib_ring_buffer *buf = stream->priv;
1863
1864 file->private_data = buf;
b3b8072b
MD
1865 /*
1866 * Since life-time of metadata cache differs from that of
1867 * session, we need to keep our own reference on the transport.
1868 */
1869 if (!try_module_get(stream->transport->owner)) {
5a15f70c 1870 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1871 return -EBUSY;
1872 }
d83004aa
JD
1873 return lib_ring_buffer_open(inode, file, buf);
1874}
1875
1876static
1877int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1878{
1879 struct lttng_metadata_stream *stream = file->private_data;
1880 struct lib_ring_buffer *buf = stream->priv;
1881
92143b2c
MD
1882 mutex_lock(&stream->metadata_cache->lock);
1883 list_del(&stream->list);
1884 mutex_unlock(&stream->metadata_cache->lock);
d83004aa 1885 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 1886 module_put(stream->transport->owner);
92143b2c 1887 kfree(stream);
d83004aa
JD
1888 return lib_ring_buffer_release(inode, file, buf);
1889}
1890
1891static
1892ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1893 struct pipe_inode_info *pipe, size_t len,
1894 unsigned int flags)
1895{
1896 struct lttng_metadata_stream *stream = in->private_data;
1897 struct lib_ring_buffer *buf = stream->priv;
1898
1899 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1900 flags, buf);
1901}
1902
1903static
1904int lttng_metadata_ring_buffer_mmap(struct file *filp,
1905 struct vm_area_struct *vma)
1906{
1907 struct lttng_metadata_stream *stream = filp->private_data;
1908 struct lib_ring_buffer *buf = stream->priv;
1909
1910 return lib_ring_buffer_mmap(filp, vma, buf);
1911}
1912
1913static
1914const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1915 .owner = THIS_MODULE,
1916 .open = lttng_metadata_ring_buffer_open,
1917 .release = lttng_metadata_ring_buffer_release,
1918 .poll = lttng_metadata_ring_buffer_poll,
1919 .splice_read = lttng_metadata_ring_buffer_splice_read,
1920 .mmap = lttng_metadata_ring_buffer_mmap,
1921 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1922 .llseek = vfs_lib_ring_buffer_no_llseek,
1923#ifdef CONFIG_COMPAT
1924 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1925#endif
1926};
1927
1928static
0c023c01 1929int lttng_abi_create_stream_fd(struct file *file, void *stream_priv,
a3fcd499 1930 const struct file_operations *fops, const char *name)
ad1c05e1 1931{
ad1c05e1 1932 int stream_fd, ret;
11b5a3c2 1933 struct file *stream_file;
ad1c05e1 1934
4ac10b76 1935 stream_fd = lttng_get_unused_fd();
ad1c05e1
MD
1936 if (stream_fd < 0) {
1937 ret = stream_fd;
1938 goto fd_error;
1939 }
a3fcd499 1940 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
c0e31d2e
MD
1941 if (IS_ERR(stream_file)) {
1942 ret = PTR_ERR(stream_file);
ad1c05e1
MD
1943 goto file_error;
1944 }
409453cb
MD
1945 /*
1946 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1947 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1948 * file descriptor, so we set FMODE_PREAD here.
1949 */
d7b6f197 1950 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 1951 fd_install(stream_fd, stream_file);
dda6a249
MD
1952 /*
1953 * The stream holds a reference to the channel within the generic ring
1954 * buffer library, so no need to hold a refcount on the channel and
1955 * session files here.
1956 */
ad1c05e1
MD
1957 return stream_fd;
1958
1959file_error:
1960 put_unused_fd(stream_fd);
d83004aa
JD
1961fd_error:
1962 return ret;
1963}
1964
1965static
0c023c01 1966int lttng_abi_open_stream(struct file *file)
d83004aa 1967{
0c023c01 1968 struct lttng_channel *channel = file->private_data;
d83004aa
JD
1969 struct lib_ring_buffer *buf;
1970 int ret;
1971 void *stream_priv;
1972
1973 buf = channel->ops->buffer_read_open(channel->chan);
1974 if (!buf)
1975 return -ENOENT;
1976
1977 stream_priv = buf;
0c023c01 1978 ret = lttng_abi_create_stream_fd(file, stream_priv,
a3fcd499
MD
1979 &lttng_stream_ring_buffer_file_operations,
1980 "[lttng_stream]");
d83004aa
JD
1981 if (ret < 0)
1982 goto fd_error;
1983
1984 return ret;
1985
1986fd_error:
1987 channel->ops->buffer_read_close(buf);
1988 return ret;
1989}
1990
1991static
0c023c01 1992int lttng_abi_open_metadata_stream(struct file *file)
d83004aa 1993{
0c023c01
MD
1994 struct lttng_channel *channel = file->private_data;
1995 struct lttng_event_container *container = lttng_channel_get_event_container(channel);
1996 struct lttng_session *session = container->session;
d83004aa
JD
1997 struct lib_ring_buffer *buf;
1998 int ret;
1999 struct lttng_metadata_stream *metadata_stream;
2000 void *stream_priv;
2001
2002 buf = channel->ops->buffer_read_open(channel->chan);
2003 if (!buf)
2004 return -ENOENT;
2005
2006 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
2007 GFP_KERNEL);
b3b8072b
MD
2008 if (!metadata_stream) {
2009 ret = -ENOMEM;
2010 goto nomem;
2011 }
d83004aa
JD
2012 metadata_stream->metadata_cache = session->metadata_cache;
2013 init_waitqueue_head(&metadata_stream->read_wait);
2014 metadata_stream->priv = buf;
2015 stream_priv = metadata_stream;
b3b8072b 2016 metadata_stream->transport = channel->transport;
8b97fd42
MD
2017 /* Initial state is an empty metadata, considered as incoherent. */
2018 metadata_stream->coherent = false;
b3b8072b
MD
2019
2020 /*
2021 * Since life-time of metadata cache differs from that of
2022 * session, we need to keep our own reference on the transport.
2023 */
2024 if (!try_module_get(metadata_stream->transport->owner)) {
5a15f70c 2025 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
2026 ret = -EINVAL;
2027 goto notransport;
2028 }
2029
901aaa5f
FD
2030 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
2031 ret = -EOVERFLOW;
9c1f4643 2032 goto kref_error;
901aaa5f
FD
2033 }
2034
0c023c01 2035 ret = lttng_abi_create_stream_fd(file, stream_priv,
a3fcd499
MD
2036 &lttng_metadata_ring_buffer_file_operations,
2037 "[lttng_metadata_stream]");
d83004aa
JD
2038 if (ret < 0)
2039 goto fd_error;
2040
92143b2c 2041 mutex_lock(&session->metadata_cache->lock);
d83004aa
JD
2042 list_add(&metadata_stream->list,
2043 &session->metadata_cache->metadata_stream);
92143b2c 2044 mutex_unlock(&session->metadata_cache->lock);
d83004aa
JD
2045 return ret;
2046
ad1c05e1 2047fd_error:
9c1f4643
MD
2048 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
2049kref_error:
b3b8072b
MD
2050 module_put(metadata_stream->transport->owner);
2051notransport:
2052 kfree(metadata_stream);
2053nomem:
11b5a3c2 2054 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
2055 return ret;
2056}
2057
21f58fb7
FD
2058static
2059int lttng_abi_open_event_notifier_group_stream(struct file *notif_file)
2060{
2061 struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data;
2062 struct channel *chan = event_notifier_group->chan;
2063 struct lib_ring_buffer *buf;
2064 int ret;
2065 void *stream_priv;
2066
2067 buf = event_notifier_group->ops->buffer_read_open(chan);
2068 if (!buf)
2069 return -ENOENT;
2070
2071 /* The event_notifier notification fd holds a reference on the event_notifier group */
2072 if (!atomic_long_add_unless(&notif_file->f_count, 1, LONG_MAX)) {
2073 ret = -EOVERFLOW;
2074 goto refcount_error;
2075 }
2076 event_notifier_group->buf = buf;
2077 stream_priv = event_notifier_group;
2078 ret = lttng_abi_create_stream_fd(notif_file, stream_priv,
2079 &lttng_event_notifier_group_notif_fops,
2080 "[lttng_event_notifier_stream]");
2081 if (ret < 0)
2082 goto fd_error;
2083
2084 return ret;
2085
2086fd_error:
2087 atomic_long_dec(&notif_file->f_count);
2088refcount_error:
2089 event_notifier_group->ops->buffer_read_close(buf);
2090 return ret;
2091}
2092
dffef45d
FD
2093static
2094long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2095{
2b16f0c9 2096 struct lttng_event_notifier *event_notifier;
dffef45d
FD
2097 struct lttng_event_notifier_enabler *event_notifier_enabler;
2098 enum lttng_event_type *evtype = file->private_data;
2099
2100 switch (cmd) {
2101 case LTTNG_KERNEL_ENABLE:
2102 switch (*evtype) {
2103 case LTTNG_TYPE_EVENT:
2b16f0c9
FD
2104 event_notifier = file->private_data;
2105 return lttng_event_notifier_enable(event_notifier);
dffef45d
FD
2106 case LTTNG_TYPE_ENABLER:
2107 event_notifier_enabler = file->private_data;
2108 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
2109 default:
2110 WARN_ON_ONCE(1);
2111 return -ENOSYS;
2112 }
2113 case LTTNG_KERNEL_DISABLE:
2114 switch (*evtype) {
2115 case LTTNG_TYPE_EVENT:
2b16f0c9
FD
2116 event_notifier = file->private_data;
2117 return lttng_event_notifier_disable(event_notifier);
dffef45d
FD
2118 case LTTNG_TYPE_ENABLER:
2119 event_notifier_enabler = file->private_data;
2120 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
2121 default:
2122 WARN_ON_ONCE(1);
2123 return -ENOSYS;
2124 }
2125 case LTTNG_KERNEL_FILTER:
2126 switch (*evtype) {
2127 case LTTNG_TYPE_EVENT:
2128 return -EINVAL;
2129 case LTTNG_TYPE_ENABLER:
2130 event_notifier_enabler = file->private_data;
183e8b3a
FD
2131 return lttng_event_notifier_enabler_attach_filter_bytecode(
2132 event_notifier_enabler,
dffef45d
FD
2133 (struct lttng_kernel_filter_bytecode __user *) arg);
2134 default:
2135 WARN_ON_ONCE(1);
2136 return -ENOSYS;
2137 }
99d223ad
FD
2138
2139 case LTTNG_KERNEL_CAPTURE:
2140 switch (*evtype) {
2141 case LTTNG_TYPE_EVENT:
2142 return -EINVAL;
2143 case LTTNG_TYPE_ENABLER:
2144 event_notifier_enabler = file->private_data;
2145 return lttng_event_notifier_enabler_attach_capture_bytecode(
2146 event_notifier_enabler,
2147 (struct lttng_kernel_capture_bytecode __user *) arg);
2148 default:
2149 WARN_ON_ONCE(1);
2150 return -ENOSYS;
2151 }
9de67196
FD
2152 case LTTNG_KERNEL_ADD_CALLSITE:
2153 switch (*evtype) {
2154 case LTTNG_TYPE_EVENT:
2155 event_notifier = file->private_data;
2156 return lttng_event_notifier_add_callsite(event_notifier,
2157 (struct lttng_kernel_event_callsite __user *) arg);
2158 case LTTNG_TYPE_ENABLER:
2159 return -EINVAL;
2160 default:
2161 WARN_ON_ONCE(1);
2162 return -ENOSYS;
2163 }
dffef45d
FD
2164 default:
2165 return -ENOIOCTLCMD;
2166 }
2167}
2168
2169static
2170int lttng_event_notifier_release(struct inode *inode, struct file *file)
2171{
2172 struct lttng_event_notifier *event_notifier;
2173 struct lttng_event_notifier_enabler *event_notifier_enabler;
2174 enum lttng_event_type *evtype = file->private_data;
2175
2176 if (!evtype)
2177 return 0;
2178
2179 switch (*evtype) {
2180 case LTTNG_TYPE_EVENT:
2181 event_notifier = file->private_data;
2182 if (event_notifier)
2183 fput(event_notifier->group->file);
2184 break;
2185 case LTTNG_TYPE_ENABLER:
2186 event_notifier_enabler = file->private_data;
2187 if (event_notifier_enabler)
2188 fput(event_notifier_enabler->group->file);
2189 break;
2190 default:
2191 WARN_ON_ONCE(1);
2192 break;
2193 }
2194
2195 return 0;
2196}
2197
2198static const struct file_operations lttng_event_notifier_fops = {
2199 .owner = THIS_MODULE,
2200 .release = lttng_event_notifier_release,
2201 .unlocked_ioctl = lttng_event_notifier_ioctl,
2202#ifdef CONFIG_COMPAT
2203 .compat_ioctl = lttng_event_notifier_ioctl,
2204#endif
2205};
2206
2207static
2208int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
2209 struct lttng_kernel_event_notifier *event_notifier_param)
2210{
2211 struct lttng_event_notifier_group *event_notifier_group =
2212 event_notifier_group_file->private_data;
2213 int event_notifier_fd, ret;
2214 struct file *event_notifier_file;
2215 void *priv;
2216
2217 switch (event_notifier_param->event.instrumentation) {
2218 case LTTNG_KERNEL_TRACEPOINT:
9de67196 2219 case LTTNG_KERNEL_UPROBE:
b01155ba 2220 break;
dffef45d 2221 case LTTNG_KERNEL_KPROBE:
2b16f0c9
FD
2222 event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
2223 break;
8a8ac9a8
FD
2224 case LTTNG_KERNEL_SYSCALL:
2225 break;
dffef45d 2226 case LTTNG_KERNEL_KRETPROBE:
9de67196 2227 /* Placing an event notifier on kretprobe is not supported. */
dffef45d
FD
2228 case LTTNG_KERNEL_FUNCTION:
2229 case LTTNG_KERNEL_NOOP:
dffef45d
FD
2230 default:
2231 ret = -EINVAL;
2232 goto inval_instr;
2233 }
2234
2235 event_notifier_param->event.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
2236
2237 event_notifier_fd = lttng_get_unused_fd();
2238 if (event_notifier_fd < 0) {
2239 ret = event_notifier_fd;
2240 goto fd_error;
2241 }
2242
2243 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
2244 &lttng_event_notifier_fops,
2245 NULL, O_RDWR);
2246 if (IS_ERR(event_notifier_file)) {
2247 ret = PTR_ERR(event_notifier_file);
2248 goto file_error;
2249 }
2250
2251 /* The event notifier holds a reference on the event notifier group. */
2252 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2253 ret = -EOVERFLOW;
2254 goto refcount_error;
2255 }
2256
2257 if (event_notifier_param->event.instrumentation == LTTNG_KERNEL_TRACEPOINT
2258 || event_notifier_param->event.instrumentation == LTTNG_KERNEL_SYSCALL) {
2259 struct lttng_event_notifier_enabler *enabler;
2260
2261 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
2262 /*
2263 * If the event name is a star globbing pattern,
2264 * we create the special star globbing enabler.
2265 */
2266 enabler = lttng_event_notifier_enabler_create(
2267 event_notifier_group,
2268 LTTNG_ENABLER_FORMAT_STAR_GLOB,
2269 event_notifier_param);
2270 } else {
2271 enabler = lttng_event_notifier_enabler_create(
2272 event_notifier_group,
2273 LTTNG_ENABLER_FORMAT_NAME,
2274 event_notifier_param);
2275 }
2276 priv = enabler;
2277 } else {
2278 struct lttng_event_notifier *event_notifier;
2279
2280 /*
2281 * We tolerate no failure path after event notifier creation.
2282 * It will stay invariant for the rest of the session.
2283 */
2284 event_notifier = lttng_event_notifier_create(NULL,
99f52fcc
FD
2285 event_notifier_param->event.token,
2286 event_notifier_param->error_counter_index,
2287 event_notifier_group,
dffef45d
FD
2288 event_notifier_param, NULL,
2289 event_notifier_param->event.instrumentation);
2290 WARN_ON_ONCE(!event_notifier);
2291 if (IS_ERR(event_notifier)) {
2292 ret = PTR_ERR(event_notifier);
2293 goto event_notifier_error;
2294 }
2295 priv = event_notifier;
2296 }
2297 event_notifier_file->private_data = priv;
2298 fd_install(event_notifier_fd, event_notifier_file);
2299 return event_notifier_fd;
2300
2301event_notifier_error:
2302 atomic_long_dec(&event_notifier_group_file->f_count);
2303refcount_error:
2304 fput(event_notifier_file);
2305file_error:
2306 put_unused_fd(event_notifier_fd);
2307fd_error:
2308inval_instr:
2309 return ret;
2310}
2311
99f52fcc
FD
2312static
2313long lttng_abi_event_notifier_group_create_error_counter(
2314 struct file *event_notifier_group_file,
2315 const struct lttng_kernel_counter_conf *error_counter_conf)
2316{
2317 int counter_fd, ret;
2318 char *counter_transport_name;
2319 size_t counter_len;
0c023c01
MD
2320 struct lttng_counter *counter;
2321 struct lttng_event_container *container;
99f52fcc
FD
2322 struct file *counter_file;
2323 struct lttng_event_notifier_group *event_notifier_group =
2324 (struct lttng_event_notifier_group *) event_notifier_group_file->private_data;
2325
2326 if (error_counter_conf->arithmetic != LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR) {
2327 printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2328 return -EINVAL;
2329 }
2330
2331 if (error_counter_conf->number_dimensions != 1) {
2332 printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n");
2333 return -EINVAL;
2334 }
2335
2336 switch (error_counter_conf->bitness) {
2337 case LTTNG_KERNEL_COUNTER_BITNESS_64:
2338 counter_transport_name = "counter-per-cpu-64-modular";
2339 break;
2340 case LTTNG_KERNEL_COUNTER_BITNESS_32:
2341 counter_transport_name = "counter-per-cpu-32-modular";
2342 break;
2343 default:
2344 return -EINVAL;
2345 }
2346
99f52fcc
FD
2347 counter_fd = lttng_get_unused_fd();
2348 if (counter_fd < 0) {
2349 ret = counter_fd;
2350 goto fd_error;
2351 }
2352
2353 counter_file = anon_inode_getfile("[lttng_counter]",
2354 &lttng_counter_fops,
2355 NULL, O_RDONLY);
2356 if (IS_ERR(counter_file)) {
2357 ret = PTR_ERR(counter_file);
2358 goto file_error;
2359 }
2360
2361 counter_len = error_counter_conf->dimensions[0].size;
2362
2363 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2364 ret = -EOVERFLOW;
2365 goto refcount_error;
2366 }
2367
0c023c01
MD
2368 ret = lttng_event_notifier_group_set_error_counter(event_notifier_group,
2369 counter_transport_name, counter_len);
2370 if (ret)
99f52fcc 2371 goto counter_error;
99f52fcc 2372
0c023c01
MD
2373 counter = event_notifier_group->error_counter;
2374 container = lttng_counter_get_event_container(counter);
2375 container->file = counter_file;
99f52fcc
FD
2376 counter->owner = event_notifier_group->file;
2377 counter_file->private_data = counter;
99f52fcc
FD
2378
2379 fd_install(counter_fd, counter_file);
99f52fcc
FD
2380
2381 return counter_fd;
2382
2383counter_error:
2384 atomic_long_dec(&event_notifier_group_file->f_count);
2385refcount_error:
2386 fput(counter_file);
2387file_error:
2388 put_unused_fd(counter_fd);
2389fd_error:
99f52fcc
FD
2390 return ret;
2391}
2392
750b05f2
FD
2393static
2394long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
2395 unsigned long arg)
2396{
2397 switch (cmd) {
21f58fb7
FD
2398 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
2399 {
2400 return lttng_abi_open_event_notifier_group_stream(file);
2401 }
dffef45d
FD
2402 case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE:
2403 {
2404 struct lttng_kernel_event_notifier uevent_notifier_param;
2405
2406 if (copy_from_user(&uevent_notifier_param,
2407 (struct lttng_kernel_event_notifier __user *) arg,
2408 sizeof(uevent_notifier_param)))
2409 return -EFAULT;
2410 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
2411 }
99f52fcc
FD
2412 case LTTNG_KERNEL_COUNTER:
2413 {
2414 struct lttng_kernel_counter_conf uerror_counter_conf;
2415
2416 if (copy_from_user(&uerror_counter_conf,
2417 (struct lttng_kernel_counter_conf __user *) arg,
2418 sizeof(uerror_counter_conf)))
2419 return -EFAULT;
2420 return lttng_abi_event_notifier_group_create_error_counter(file,
2421 &uerror_counter_conf);
2422 }
750b05f2
FD
2423 default:
2424 return -ENOIOCTLCMD;
2425 }
2426 return 0;
2427}
2428
2429static
2430int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
2431{
2432 struct lttng_event_notifier_group *event_notifier_group =
2433 file->private_data;
2434
2435 if (event_notifier_group)
2436 lttng_event_notifier_group_destroy(event_notifier_group);
2437 return 0;
2438}
2439
2440static const struct file_operations lttng_event_notifier_group_fops = {
2441 .owner = THIS_MODULE,
2442 .release = lttng_event_notifier_group_release,
2443 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
2444#ifdef CONFIG_COMPAT
2445 .compat_ioctl = lttng_event_notifier_group_ioctl,
2446#endif
2447};
2448
ad1c05e1
MD
2449/**
2450 * lttng_channel_ioctl - lttng syscall through ioctl
2451 *
c0e31d2e 2452 * @file: the file
ad1c05e1
MD
2453 * @cmd: the command
2454 * @arg: command arg
2455 *
2456 * This ioctl implements lttng commands:
38d024ae 2457 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
2458 * Returns an event stream file descriptor or failure.
2459 * (typically, one event stream records events from one CPU)
38d024ae 2460 * LTTNG_KERNEL_EVENT
ad1c05e1 2461 * Returns an event file descriptor or failure.
8070f5c0
MD
2462 * LTTNG_KERNEL_CONTEXT
2463 * Prepend a context field to each event in the channel
e64957da
MD
2464 * LTTNG_KERNEL_ENABLE
2465 * Enable recording for events in this channel (weak enable)
2466 * LTTNG_KERNEL_DISABLE
2467 * Disable recording for events in this channel (strong disable)
baf20995 2468 *
baf20995
MD
2469 * Channel and event file descriptors also hold a reference on the session.
2470 */
ad1c05e1 2471static
c0e31d2e 2472long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 2473{
a90917c3 2474 struct lttng_channel *channel = file->private_data;
0c023c01 2475 struct lttng_event_container *container = lttng_channel_get_event_container(channel);
8070f5c0 2476
baf20995 2477 switch (cmd) {
6dccd6c1 2478 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 2479 case LTTNG_KERNEL_STREAM:
c0e31d2e 2480 return lttng_abi_open_stream(file);
6dccd6c1
JD
2481 case LTTNG_KERNEL_OLD_EVENT:
2482 {
2483 struct lttng_kernel_event *uevent_param;
2484 struct lttng_kernel_old_event *old_uevent_param;
2485 int ret;
2486
2487 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
2488 GFP_KERNEL);
2489 if (!uevent_param) {
2490 ret = -ENOMEM;
2491 goto old_event_end;
2492 }
2493 old_uevent_param = kmalloc(
2494 sizeof(struct lttng_kernel_old_event),
2495 GFP_KERNEL);
2496 if (!old_uevent_param) {
2497 ret = -ENOMEM;
2498 goto old_event_error_free_param;
2499 }
2500 if (copy_from_user(old_uevent_param,
2501 (struct lttng_kernel_old_event __user *) arg,
2502 sizeof(struct lttng_kernel_old_event))) {
2503 ret = -EFAULT;
2504 goto old_event_error_free_old_param;
2505 }
2506
2507 memcpy(uevent_param->name, old_uevent_param->name,
2508 sizeof(uevent_param->name));
2509 uevent_param->instrumentation =
2510 old_uevent_param->instrumentation;
2511
2512 switch (old_uevent_param->instrumentation) {
2513 case LTTNG_KERNEL_KPROBE:
2514 uevent_param->u.kprobe.addr =
2515 old_uevent_param->u.kprobe.addr;
2516 uevent_param->u.kprobe.offset =
2517 old_uevent_param->u.kprobe.offset;
2518 memcpy(uevent_param->u.kprobe.symbol_name,
2519 old_uevent_param->u.kprobe.symbol_name,
2520 sizeof(uevent_param->u.kprobe.symbol_name));
2521 break;
2522 case LTTNG_KERNEL_KRETPROBE:
2523 uevent_param->u.kretprobe.addr =
2524 old_uevent_param->u.kretprobe.addr;
2525 uevent_param->u.kretprobe.offset =
2526 old_uevent_param->u.kretprobe.offset;
2527 memcpy(uevent_param->u.kretprobe.symbol_name,
2528 old_uevent_param->u.kretprobe.symbol_name,
2529 sizeof(uevent_param->u.kretprobe.symbol_name));
2530 break;
2531 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
2532 WARN_ON_ONCE(1);
2533 /* Not implemented. */
6dccd6c1
JD
2534 break;
2535 default:
2536 break;
2537 }
0c023c01 2538 ret = lttng_abi_create_event(file, container, uevent_param, NULL);
6dccd6c1
JD
2539
2540old_event_error_free_old_param:
2541 kfree(old_uevent_param);
2542old_event_error_free_param:
2543 kfree(uevent_param);
2544old_event_end:
2545 return ret;
2546 }
38d024ae 2547 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
2548 {
2549 struct lttng_kernel_event uevent_param;
2550
2551 if (copy_from_user(&uevent_param,
2552 (struct lttng_kernel_event __user *) arg,
2553 sizeof(uevent_param)))
2554 return -EFAULT;
0c023c01 2555 return lttng_abi_create_event(file, container, &uevent_param, NULL);
6dccd6c1
JD
2556 }
2557 case LTTNG_KERNEL_OLD_CONTEXT:
2558 {
2559 struct lttng_kernel_context *ucontext_param;
2560 struct lttng_kernel_old_context *old_ucontext_param;
2561 int ret;
2562
2563 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
2564 GFP_KERNEL);
2565 if (!ucontext_param) {
2566 ret = -ENOMEM;
2567 goto old_ctx_end;
2568 }
2569 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
2570 GFP_KERNEL);
2571 if (!old_ucontext_param) {
2572 ret = -ENOMEM;
2573 goto old_ctx_error_free_param;
2574 }
2575
2576 if (copy_from_user(old_ucontext_param,
2577 (struct lttng_kernel_old_context __user *) arg,
2578 sizeof(struct lttng_kernel_old_context))) {
2579 ret = -EFAULT;
2580 goto old_ctx_error_free_old_param;
2581 }
2582 ucontext_param->ctx = old_ucontext_param->ctx;
2583 memcpy(ucontext_param->padding, old_ucontext_param->padding,
2584 sizeof(ucontext_param->padding));
2585 /* only type that uses the union */
2586 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
2587 ucontext_param->u.perf_counter.type =
2588 old_ucontext_param->u.perf_counter.type;
2589 ucontext_param->u.perf_counter.config =
2590 old_ucontext_param->u.perf_counter.config;
2591 memcpy(ucontext_param->u.perf_counter.name,
2592 old_ucontext_param->u.perf_counter.name,
2593 sizeof(ucontext_param->u.perf_counter.name));
2594 }
2595
2596 ret = lttng_abi_add_context(file,
2597 ucontext_param,
0c023c01 2598 &channel->ctx, container->session);
6dccd6c1
JD
2599
2600old_ctx_error_free_old_param:
2601 kfree(old_ucontext_param);
2602old_ctx_error_free_param:
2603 kfree(ucontext_param);
2604old_ctx_end:
2605 return ret;
2606 }
8070f5c0 2607 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
2608 {
2609 struct lttng_kernel_context ucontext_param;
2610
2611 if (copy_from_user(&ucontext_param,
8070f5c0 2612 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
2613 sizeof(ucontext_param)))
2614 return -EFAULT;
2615 return lttng_abi_add_context(file,
2616 &ucontext_param,
0c023c01 2617 &channel->ctx, container->session);
6dccd6c1
JD
2618 }
2619 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 2620 case LTTNG_KERNEL_ENABLE:
0c023c01 2621 return lttng_event_container_enable(container);
6dccd6c1 2622 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 2623 case LTTNG_KERNEL_DISABLE:
0c023c01 2624 return lttng_event_container_disable(container);
12e579db 2625 case LTTNG_KERNEL_SYSCALL_MASK:
0c023c01 2626 return lttng_event_container_syscall_mask(container,
12e579db 2627 (struct lttng_kernel_syscall_mask __user *) arg);
baf20995
MD
2628 default:
2629 return -ENOIOCTLCMD;
2630 }
2631}
2632
5dbbdb43
MD
2633/**
2634 * lttng_metadata_ioctl - lttng syscall through ioctl
2635 *
2636 * @file: the file
2637 * @cmd: the command
2638 * @arg: command arg
2639 *
2640 * This ioctl implements lttng commands:
38d024ae 2641 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
2642 * Returns an event stream file descriptor or failure.
2643 *
2644 * Channel and event file descriptors also hold a reference on the session.
2645 */
2646static
2647long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2648{
2649 switch (cmd) {
6dccd6c1 2650 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 2651 case LTTNG_KERNEL_STREAM:
d83004aa 2652 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
2653 default:
2654 return -ENOIOCTLCMD;
2655 }
2656}
2657
653fe716
MD
2658/**
2659 * lttng_channel_poll - lttng stream addition/removal monitoring
2660 *
c0e31d2e 2661 * @file: the file
653fe716
MD
2662 * @wait: poll table
2663 */
c0e31d2e 2664unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 2665{
a90917c3 2666 struct lttng_channel *channel = file->private_data;
653fe716
MD
2667 unsigned int mask = 0;
2668
c0e31d2e 2669 if (file->f_mode & FMODE_READ) {
a33e44a6 2670 poll_wait_set_exclusive(wait);
24cedcfe
MD
2671 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
2672 wait);
653fe716 2673
254ec7bc
MD
2674 if (channel->ops->is_disabled(channel->chan))
2675 return POLLERR;
24cedcfe 2676 if (channel->ops->is_finalized(channel->chan))
653fe716 2677 return POLLHUP;
f71ecafa 2678 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 2679 return POLLIN | POLLRDNORM;
f71ecafa 2680 return 0;
653fe716
MD
2681 }
2682 return mask;
2683
2684}
2685
0a84a57f
MD
2686static
2687int lttng_channel_release(struct inode *inode, struct file *file)
2688{
0c023c01
MD
2689 struct lttng_channel *chan = file->private_data;
2690
2691 if (chan) {
2692 struct lttng_event_container *container = lttng_channel_get_event_container(chan);
c269fff4 2693
0c023c01
MD
2694 fput(container->session->file);
2695 }
0a84a57f
MD
2696 return 0;
2697}
2698
d83004aa
JD
2699static
2700int lttng_metadata_channel_release(struct inode *inode, struct file *file)
2701{
0c023c01
MD
2702 struct lttng_channel *chan = file->private_data;
2703
2704 if (chan) {
2705 struct lttng_event_container *container = lttng_channel_get_event_container(chan);
d83004aa 2706
0c023c01
MD
2707 fput(container->session->file);
2708 lttng_metadata_channel_destroy(chan);
d83004aa
JD
2709 }
2710
2711 return 0;
2712}
2713
ad1c05e1 2714static const struct file_operations lttng_channel_fops = {
a33c9927 2715 .owner = THIS_MODULE,
03037b98 2716 .release = lttng_channel_release,
653fe716 2717 .poll = lttng_channel_poll,
ad1c05e1 2718 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 2719#ifdef CONFIG_COMPAT
03037b98 2720 .compat_ioctl = lttng_channel_ioctl,
baf20995 2721#endif
11b5a3c2 2722};
baf20995 2723
5dbbdb43 2724static const struct file_operations lttng_metadata_fops = {
a33c9927 2725 .owner = THIS_MODULE,
d83004aa 2726 .release = lttng_metadata_channel_release,
5dbbdb43
MD
2727 .unlocked_ioctl = lttng_metadata_ioctl,
2728#ifdef CONFIG_COMPAT
2729 .compat_ioctl = lttng_metadata_ioctl,
2730#endif
2731};
2732
8070f5c0
MD
2733/**
2734 * lttng_event_ioctl - lttng syscall through ioctl
2735 *
2736 * @file: the file
2737 * @cmd: the command
2738 * @arg: command arg
2739 *
2740 * This ioctl implements lttng commands:
8070f5c0
MD
2741 * LTTNG_KERNEL_CONTEXT
2742 * Prepend a context field to each record of this event
e64957da
MD
2743 * LTTNG_KERNEL_ENABLE
2744 * Enable recording for this event (weak enable)
2745 * LTTNG_KERNEL_DISABLE
2746 * Disable recording for this event (strong disable)
8070f5c0
MD
2747 */
2748static
2749long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2750{
3c997079 2751 struct lttng_event *event;
b2bc0bc8 2752 struct lttng_event_enabler *event_enabler;
3c997079 2753 enum lttng_event_type *evtype = file->private_data;
8070f5c0
MD
2754
2755 switch (cmd) {
6dccd6c1
JD
2756 case LTTNG_KERNEL_OLD_CONTEXT:
2757 {
3c997079
MD
2758 /* Not implemented */
2759 return -ENOSYS;
6dccd6c1 2760 }
8070f5c0 2761 case LTTNG_KERNEL_CONTEXT:
6dccd6c1 2762 {
3c997079
MD
2763 /* Not implemented */
2764 return -ENOSYS;
6dccd6c1
JD
2765 }
2766 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 2767 case LTTNG_KERNEL_ENABLE:
3c997079
MD
2768 switch (*evtype) {
2769 case LTTNG_TYPE_EVENT:
2770 event = file->private_data;
2771 return lttng_event_enable(event);
2772 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
2773 event_enabler = file->private_data;
2774 return lttng_event_enabler_enable(event_enabler);
3c997079
MD
2775 default:
2776 WARN_ON_ONCE(1);
2777 return -ENOSYS;
2778 }
6dccd6c1 2779 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 2780 case LTTNG_KERNEL_DISABLE:
3c997079
MD
2781 switch (*evtype) {
2782 case LTTNG_TYPE_EVENT:
2783 event = file->private_data;
2784 return lttng_event_disable(event);
2785 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
2786 event_enabler = file->private_data;
2787 return lttng_event_enabler_disable(event_enabler);
3c997079
MD
2788 default:
2789 WARN_ON_ONCE(1);
2790 return -ENOSYS;
2791 }
07dfc1d0
MD
2792 case LTTNG_KERNEL_FILTER:
2793 switch (*evtype) {
2794 case LTTNG_TYPE_EVENT:
2795 return -EINVAL;
2796 case LTTNG_TYPE_ENABLER:
2797 {
b2bc0bc8 2798 event_enabler = file->private_data;
183e8b3a
FD
2799 return lttng_event_enabler_attach_filter_bytecode(
2800 event_enabler,
07dfc1d0
MD
2801 (struct lttng_kernel_filter_bytecode __user *) arg);
2802 }
0586316f
MD
2803 default:
2804 WARN_ON_ONCE(1);
2805 return -ENOSYS;
07dfc1d0 2806 }
3aed4dca
FD
2807 case LTTNG_KERNEL_ADD_CALLSITE:
2808 switch (*evtype) {
2809 case LTTNG_TYPE_EVENT:
2810 event = file->private_data;
2811 return lttng_event_add_callsite(event,
2812 (struct lttng_kernel_event_callsite __user *) arg);
2813 case LTTNG_TYPE_ENABLER:
2814 return -EINVAL;
64c147c0
MD
2815 default:
2816 WARN_ON_ONCE(1);
2817 return -ENOSYS;
3aed4dca 2818 }
8070f5c0
MD
2819 default:
2820 return -ENOIOCTLCMD;
2821 }
2822}
2823
0a84a57f
MD
2824static
2825int lttng_event_release(struct inode *inode, struct file *file)
2826{
3c997079 2827 struct lttng_event *event;
b2bc0bc8 2828 struct lttng_event_enabler *event_enabler;
3c997079
MD
2829 enum lttng_event_type *evtype = file->private_data;
2830
2831 if (!evtype)
2832 return 0;
2833
2834 switch (*evtype) {
2835 case LTTNG_TYPE_EVENT:
2836 event = file->private_data;
2837 if (event)
0c023c01 2838 fput(event->container->file);
3c997079
MD
2839 break;
2840 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
2841 event_enabler = file->private_data;
2842 if (event_enabler)
0c023c01 2843 fput(event_enabler->container->file);
3c997079
MD
2844 break;
2845 default:
2846 WARN_ON_ONCE(1);
2847 break;
2848 }
c269fff4 2849
0a84a57f
MD
2850 return 0;
2851}
2852
3b923e5b 2853/* TODO: filter control ioctl */
0a84a57f 2854static const struct file_operations lttng_event_fops = {
a33c9927 2855 .owner = THIS_MODULE,
0a84a57f 2856 .release = lttng_event_release,
8070f5c0
MD
2857 .unlocked_ioctl = lttng_event_ioctl,
2858#ifdef CONFIG_COMPAT
2859 .compat_ioctl = lttng_event_ioctl,
2860#endif
11b5a3c2 2861};
0a84a57f 2862
3b731ab1
JD
2863static int put_u64(uint64_t val, unsigned long arg)
2864{
2865 return put_user(val, (uint64_t __user *) arg);
2866}
2867
8b97fd42
MD
2868static int put_u32(uint32_t val, unsigned long arg)
2869{
2870 return put_user(val, (uint32_t __user *) arg);
2871}
2872
ed8d02d6
JD
2873static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2874 unsigned int cmd, unsigned long arg)
2875{
3b731ab1
JD
2876 struct lib_ring_buffer *buf = filp->private_data;
2877 struct channel *chan = buf->backend.chan;
2878 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 2879 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2880 int ret;
2881
2882 if (atomic_read(&chan->record_disabled))
2883 return -EIO;
2884
ed8d02d6 2885 switch (cmd) {
3b731ab1
JD
2886 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
2887 {
2888 uint64_t ts;
2889
dd5a0db3 2890 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2891 if (ret < 0)
2892 goto error;
2893 return put_u64(ts, arg);
2894 }
2895 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
2896 {
2897 uint64_t ts;
2898
dd5a0db3 2899 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
2900 if (ret < 0)
2901 goto error;
2902 return put_u64(ts, arg);
2903 }
2904 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
2905 {
2906 uint64_t ed;
2907
dd5a0db3 2908 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
2909 if (ret < 0)
2910 goto error;
2911 return put_u64(ed, arg);
2912 }
2913 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
2914 {
2915 uint64_t cs;
2916
dd5a0db3 2917 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
2918 if (ret < 0)
2919 goto error;
2920 return put_u64(cs, arg);
2921 }
2922 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
2923 {
2924 uint64_t ps;
2925
dd5a0db3 2926 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
2927 if (ret < 0)
2928 goto error;
2929 return put_u64(ps, arg);
2930 }
2931 case LTTNG_RING_BUFFER_GET_STREAM_ID:
2932 {
2933 uint64_t si;
2934
dd5a0db3 2935 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
2936 if (ret < 0)
2937 goto error;
2938 return put_u64(si, arg);
2939 }
2348ca17
JD
2940 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2941 {
2942 uint64_t ts;
2943
dd5a0db3 2944 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
2945 if (ret < 0)
2946 goto error;
2947 return put_u64(ts, arg);
2948 }
5b3cf4f9
JD
2949 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
2950 {
2951 uint64_t seq;
2952
2953 ret = ops->sequence_number(config, buf, &seq);
2954 if (ret < 0)
2955 goto error;
2956 return put_u64(seq, arg);
2957 }
5594698f
JD
2958 case LTTNG_RING_BUFFER_INSTANCE_ID:
2959 {
2960 uint64_t id;
2961
2962 ret = ops->instance_id(config, buf, &id);
2963 if (ret < 0)
2964 goto error;
2965 return put_u64(id, arg);
2966 }
3b731ab1
JD
2967 default:
2968 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2969 cmd, arg);
ed8d02d6 2970 }
3b731ab1
JD
2971
2972error:
2973 return -ENOSYS;
ed8d02d6
JD
2974}
2975
2976#ifdef CONFIG_COMPAT
2977static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2978 unsigned int cmd, unsigned long arg)
2979{
3b731ab1
JD
2980 struct lib_ring_buffer *buf = filp->private_data;
2981 struct channel *chan = buf->backend.chan;
2982 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 2983 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2984 int ret;
2985
2986 if (atomic_read(&chan->record_disabled))
2987 return -EIO;
2988
ed8d02d6 2989 switch (cmd) {
3b731ab1
JD
2990 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
2991 {
2992 uint64_t ts;
2993
dd5a0db3 2994 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2995 if (ret < 0)
2996 goto error;
2997 return put_u64(ts, arg);
2998 }
2999 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
3000 {
3001 uint64_t ts;
3002
dd5a0db3 3003 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
3004 if (ret < 0)
3005 goto error;
3006 return put_u64(ts, arg);
3007 }
3008 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
3009 {
3010 uint64_t ed;
3011
dd5a0db3 3012 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
3013 if (ret < 0)
3014 goto error;
3015 return put_u64(ed, arg);
ed8d02d6 3016 }
3b731ab1
JD
3017 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
3018 {
3019 uint64_t cs;
3020
dd5a0db3 3021 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
3022 if (ret < 0)
3023 goto error;
3024 return put_u64(cs, arg);
3025 }
3026 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
3027 {
3028 uint64_t ps;
3029
dd5a0db3 3030 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
3031 if (ret < 0)
3032 goto error;
3033 return put_u64(ps, arg);
3034 }
3035 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
3036 {
3037 uint64_t si;
3038
dd5a0db3 3039 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
3040 if (ret < 0)
3041 goto error;
3042 return put_u64(si, arg);
3043 }
2348ca17
JD
3044 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
3045 {
3046 uint64_t ts;
3047
dd5a0db3 3048 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
3049 if (ret < 0)
3050 goto error;
3051 return put_u64(ts, arg);
3052 }
5b3cf4f9
JD
3053 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
3054 {
3055 uint64_t seq;
3056
3057 ret = ops->sequence_number(config, buf, &seq);
3058 if (ret < 0)
3059 goto error;
3060 return put_u64(seq, arg);
3061 }
5594698f
JD
3062 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
3063 {
3064 uint64_t id;
3065
3066 ret = ops->instance_id(config, buf, &id);
3067 if (ret < 0)
3068 goto error;
3069 return put_u64(id, arg);
3070 }
3b731ab1
JD
3071 default:
3072 return lib_ring_buffer_file_operations.compat_ioctl(filp,
3073 cmd, arg);
3074 }
3075
3076error:
3077 return -ENOSYS;
ed8d02d6
JD
3078}
3079#endif /* CONFIG_COMPAT */
3080
3081static void lttng_stream_override_ring_buffer_fops(void)
3082{
3083 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
3084 lttng_stream_ring_buffer_file_operations.open =
3085 lib_ring_buffer_file_operations.open;
3086 lttng_stream_ring_buffer_file_operations.release =
3087 lib_ring_buffer_file_operations.release;
3088 lttng_stream_ring_buffer_file_operations.poll =
3089 lib_ring_buffer_file_operations.poll;
3090 lttng_stream_ring_buffer_file_operations.splice_read =
3091 lib_ring_buffer_file_operations.splice_read;
3092 lttng_stream_ring_buffer_file_operations.mmap =
3093 lib_ring_buffer_file_operations.mmap;
3094 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
3095 lttng_stream_ring_buffer_ioctl;
3096 lttng_stream_ring_buffer_file_operations.llseek =
3097 lib_ring_buffer_file_operations.llseek;
3098#ifdef CONFIG_COMPAT
3099 lttng_stream_ring_buffer_file_operations.compat_ioctl =
3100 lttng_stream_ring_buffer_compat_ioctl;
3101#endif
3102}
3103
80996790 3104int __init lttng_abi_init(void)
baf20995
MD
3105{
3106 int ret = 0;
3107
263b6c88 3108 wrapper_vmalloc_sync_mappings();
2754583e 3109 lttng_clock_ref();
f771eda6
JD
3110
3111 ret = lttng_tp_mempool_init();
3112 if (ret) {
3113 goto error;
3114 }
3115
d29348f7 3116 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 3117 &lttng_proc_ops, NULL);
2470a237 3118
255e52a4 3119 if (!lttng_proc_dentry) {
5a15f70c 3120 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
3121 ret = -ENOMEM;
3122 goto error;
3123 }
ed8d02d6 3124 lttng_stream_override_ring_buffer_fops();
2754583e 3125 return 0;
ed8d02d6 3126
baf20995 3127error:
f771eda6 3128 lttng_tp_mempool_destroy();
2754583e 3129 lttng_clock_unref();
baf20995
MD
3130 return ret;
3131}
3132
e6e65fcd
MD
3133/* No __exit annotation because used by init error path too. */
3134void lttng_abi_exit(void)
baf20995 3135{
f771eda6 3136 lttng_tp_mempool_destroy();
2754583e 3137 lttng_clock_unref();
e6a17f26
MD
3138 if (lttng_proc_dentry)
3139 remove_proc_entry("lttng", NULL);
baf20995 3140}
This page took 0.196031 seconds and 5 git commands to generate.