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