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