Refactoring: struct lttng_channel
[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{
8cdc1a81 94 struct lttng_kernel_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 }
8cdc1a81 113 session->priv->file = session_file;
c0e31d2e 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,
8cdc1a81 262 struct lttng_kernel_ctx **ctx, struct lttng_kernel_session *session)
8070f5c0 263{
8070f5c0 264
8cdc1a81 265 if (session->priv->been_active)
8070f5c0
MD
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{
8cdc1a81 491 struct lttng_kernel_session *session = session_file->private_data;
88dfd899 492 const struct file_operations *fops = NULL;
5dbbdb43 493 const char *transport_name;
f7d06400 494 struct lttng_kernel_channel_buffer *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 }
f7d06400 562 chan->priv->parent.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 578static
8cdc1a81 579int lttng_abi_session_set_name(struct lttng_kernel_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
8cdc1a81 591 strcpy(session->priv->name, name->name);
7f859fbf
JR
592 return 0;
593}
594
1c88f269 595static
8cdc1a81 596int lttng_abi_session_set_creation_time(struct lttng_kernel_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
8cdc1a81 608 strcpy(session->priv->creation_time, time->iso8601);
1c88f269
JR
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{
8cdc1a81 792 struct lttng_kernel_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
8c71721f
MD
796 /*
797 * Handle backward compatibility. OLD commands have wrong
798 * directions, replace them by the correct direction.
799 */
800 switch (cmd) {
801 case LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_PID:
802 cmd = LTTNG_KERNEL_ABI_SESSION_TRACK_PID;
803 break;
804 case LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_PID:
805 cmd = LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID;
806 break;
807 case LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_ID:
808 cmd = LTTNG_KERNEL_ABI_SESSION_TRACK_ID;
809 break;
810 case LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_ID:
811 cmd = LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID;
812 break;
813 case LTTNG_KERNEL_ABI_OLD_SESSION_LIST_TRACKER_IDS:
814 cmd = LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS;
815 break;
816 case LTTNG_KERNEL_ABI_OLD_SESSION_SET_NAME:
817 cmd = LTTNG_KERNEL_ABI_SESSION_SET_NAME;
818 break;
819 case LTTNG_KERNEL_ABI_OLD_SESSION_SET_CREATION_TIME:
820 cmd = LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME;
821 break;
822 default:
823 /* Nothing to do. */
824 break;
825 }
826
ad1c05e1 827 switch (cmd) {
606828e4 828 case LTTNG_KERNEL_ABI_OLD_CHANNEL:
6dccd6c1 829 {
6dccd6c1 830 if (copy_from_user(&old_chan_param,
606828e4
MD
831 (struct lttng_kernel_abi_old_channel __user *) arg,
832 sizeof(struct lttng_kernel_abi_old_channel)))
6dccd6c1
JD
833 return -EFAULT;
834 chan_param.overwrite = old_chan_param.overwrite;
835 chan_param.subbuf_size = old_chan_param.subbuf_size;
836 chan_param.num_subbuf = old_chan_param.num_subbuf;
837 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
838 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
839 chan_param.output = old_chan_param.output;
840
841 return lttng_abi_create_channel(file, &chan_param,
842 PER_CPU_CHANNEL);
843 }
606828e4 844 case LTTNG_KERNEL_ABI_CHANNEL:
6dccd6c1 845 {
6dccd6c1 846 if (copy_from_user(&chan_param,
606828e4
MD
847 (struct lttng_kernel_abi_channel __user *) arg,
848 sizeof(struct lttng_kernel_abi_channel)))
6dccd6c1
JD
849 return -EFAULT;
850 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 851 PER_CPU_CHANNEL);
6dccd6c1 852 }
606828e4
MD
853 case LTTNG_KERNEL_ABI_OLD_SESSION_START:
854 case LTTNG_KERNEL_ABI_OLD_ENABLE:
855 case LTTNG_KERNEL_ABI_SESSION_START:
856 case LTTNG_KERNEL_ABI_ENABLE:
a90917c3 857 return lttng_session_enable(session);
606828e4
MD
858 case LTTNG_KERNEL_ABI_OLD_SESSION_STOP:
859 case LTTNG_KERNEL_ABI_OLD_DISABLE:
860 case LTTNG_KERNEL_ABI_SESSION_STOP:
861 case LTTNG_KERNEL_ABI_DISABLE:
a90917c3 862 return lttng_session_disable(session);
606828e4 863 case LTTNG_KERNEL_ABI_OLD_METADATA:
6dccd6c1 864 {
6dccd6c1 865 if (copy_from_user(&old_chan_param,
606828e4
MD
866 (struct lttng_kernel_abi_old_channel __user *) arg,
867 sizeof(struct lttng_kernel_abi_old_channel)))
6dccd6c1
JD
868 return -EFAULT;
869 chan_param.overwrite = old_chan_param.overwrite;
870 chan_param.subbuf_size = old_chan_param.subbuf_size;
871 chan_param.num_subbuf = old_chan_param.num_subbuf;
872 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
873 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
874 chan_param.output = old_chan_param.output;
875
876 return lttng_abi_create_channel(file, &chan_param,
877 METADATA_CHANNEL);
878 }
606828e4 879 case LTTNG_KERNEL_ABI_METADATA:
6dccd6c1 880 {
6dccd6c1 881 if (copy_from_user(&chan_param,
606828e4
MD
882 (struct lttng_kernel_abi_channel __user *) arg,
883 sizeof(struct lttng_kernel_abi_channel)))
6dccd6c1
JD
884 return -EFAULT;
885 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 886 METADATA_CHANNEL);
6dccd6c1 887 }
606828e4 888 case LTTNG_KERNEL_ABI_SESSION_TRACK_PID:
d1f652f8 889 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
606828e4 890 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID:
d1f652f8 891 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
606828e4 892 case LTTNG_KERNEL_ABI_SESSION_TRACK_ID:
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_track_id(session, tracker_type, tracker.id);
905 }
606828e4 906 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID:
d1f652f8 907 {
606828e4 908 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
909 enum tracker_type tracker_type;
910
911 if (copy_from_user(&tracker,
606828e4
MD
912 (struct lttng_kernel_abi_tracker_args __user *) arg,
913 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
914 return -EFAULT;
915 tracker_type = get_tracker_type(&tracker);
916 if (tracker_type == TRACKER_UNKNOWN)
917 return -EINVAL;
918 return lttng_session_untrack_id(session, tracker_type,
919 tracker.id);
920 }
606828e4 921 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_PIDS:
d1f652f8 922 return lttng_session_list_tracker_ids(session, TRACKER_PID);
606828e4 923 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS:
d1f652f8 924 {
606828e4 925 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
926 enum tracker_type tracker_type;
927
928 if (copy_from_user(&tracker,
606828e4
MD
929 (struct lttng_kernel_abi_tracker_args __user *) arg,
930 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
931 return -EFAULT;
932 tracker_type = get_tracker_type(&tracker);
933 if (tracker_type == TRACKER_UNKNOWN)
934 return -EINVAL;
935 return lttng_session_list_tracker_ids(session, tracker_type);
936 }
606828e4 937 case LTTNG_KERNEL_ABI_SESSION_METADATA_REGEN:
9616f0bf 938 return lttng_session_metadata_regenerate(session);
606828e4 939 case LTTNG_KERNEL_ABI_SESSION_STATEDUMP:
601252cf 940 return lttng_session_statedump(session);
606828e4 941 case LTTNG_KERNEL_ABI_SESSION_SET_NAME:
7f859fbf 942 {
606828e4 943 struct lttng_kernel_abi_session_name name;
7f859fbf
JR
944
945 if (copy_from_user(&name,
606828e4
MD
946 (struct lttng_kernel_abi_session_name __user *) arg,
947 sizeof(struct lttng_kernel_abi_session_name)))
7f859fbf
JR
948 return -EFAULT;
949 return lttng_abi_session_set_name(session, &name);
950 }
606828e4 951 case LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME:
1c88f269 952 {
606828e4 953 struct lttng_kernel_abi_session_creation_time time;
1c88f269
JR
954
955 if (copy_from_user(&time,
606828e4
MD
956 (struct lttng_kernel_abi_session_creation_time __user *) arg,
957 sizeof(struct lttng_kernel_abi_session_creation_time)))
1c88f269
JR
958 return -EFAULT;
959 return lttng_abi_session_set_creation_time(session, &time);
960 }
ad1c05e1
MD
961 default:
962 return -ENOIOCTLCMD;
963 }
964}
965
03037b98
MD
966/*
967 * Called when the last file reference is dropped.
968 *
969 * Big fat note: channels and events are invariant for the whole session after
970 * their creation. So this session destruction also destroys all channel and
971 * event structures specific to this session (they are not destroyed when their
972 * individual file is released).
973 */
ad1c05e1 974static
03037b98 975int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 976{
8cdc1a81 977 struct lttng_kernel_session *session = file->private_data;
c269fff4
MD
978
979 if (session)
a90917c3 980 lttng_session_destroy(session);
11b5a3c2 981 return 0;
ad1c05e1 982}
ad1c05e1
MD
983
984static const struct file_operations lttng_session_fops = {
a33c9927 985 .owner = THIS_MODULE,
03037b98 986 .release = lttng_session_release,
ad1c05e1
MD
987 .unlocked_ioctl = lttng_session_ioctl,
988#ifdef CONFIG_COMPAT
03037b98 989 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 990#endif
11b5a3c2 991};
ad1c05e1 992
db2511b4
MD
993/*
994 * When encountering empty buffer, flush current sub-buffer if non-empty
995 * and retry (if new data available to read after flush).
996 */
997static
998ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf,
999 size_t count, loff_t *ppos)
1000{
1001 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
1002 struct channel *chan = event_notifier_group->chan;
1003 struct lib_ring_buffer *buf = event_notifier_group->buf;
1004 ssize_t read_count = 0, len;
1005 size_t read_offset;
1006
1007 might_sleep();
1008 if (!lttng_access_ok(VERIFY_WRITE, user_buf, count))
1009 return -EFAULT;
1010
1011 /* Finish copy of previous record */
1012 if (*ppos != 0) {
1013 if (read_count < count) {
1014 len = chan->iter.len_left;
1015 read_offset = *ppos;
1016 goto skip_get_next;
1017 }
1018 }
1019
1020 while (read_count < count) {
1021 size_t copy_len, space_left;
1022
1023 len = lib_ring_buffer_get_next_record(chan, buf);
1024len_test:
1025 if (len < 0) {
1026 /*
1027 * Check if buffer is finalized (end of file).
1028 */
1029 if (len == -ENODATA) {
1030 /* A 0 read_count will tell about end of file */
1031 goto nodata;
1032 }
1033 if (filp->f_flags & O_NONBLOCK) {
1034 if (!read_count)
1035 read_count = -EAGAIN;
1036 goto nodata;
1037 } else {
1038 int error;
1039
1040 /*
1041 * No data available at the moment, return what
1042 * we got.
1043 */
1044 if (read_count)
1045 goto nodata;
1046
1047 /*
1048 * Wait for returned len to be >= 0 or -ENODATA.
1049 */
1050 error = wait_event_interruptible(
1051 event_notifier_group->read_wait,
1052 ((len = lib_ring_buffer_get_next_record(
1053 chan, buf)), len != -EAGAIN));
1054 CHAN_WARN_ON(chan, len == -EBUSY);
1055 if (error) {
1056 read_count = error;
1057 goto nodata;
1058 }
1059 CHAN_WARN_ON(chan, len < 0 && len != -ENODATA);
1060 goto len_test;
1061 }
1062 }
1063 read_offset = buf->iter.read_offset;
1064skip_get_next:
1065 space_left = count - read_count;
1066 if (len <= space_left) {
1067 copy_len = len;
1068 chan->iter.len_left = 0;
1069 *ppos = 0;
1070 } else {
1071 copy_len = space_left;
1072 chan->iter.len_left = len - copy_len;
1073 *ppos = read_offset + copy_len;
1074 }
1075 if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset,
1076 &user_buf[read_count],
1077 copy_len)) {
1078 /*
1079 * Leave the len_left and ppos values at their current
1080 * state, as we currently have a valid event to read.
1081 */
1082 return -EFAULT;
1083 }
1084 read_count += copy_len;
1085 }
31c02fb7 1086 goto put_record;
db2511b4
MD
1087
1088nodata:
1089 *ppos = 0;
1090 chan->iter.len_left = 0;
31c02fb7
MD
1091
1092put_record:
1093 lib_ring_buffer_put_current_record(buf);
db2511b4
MD
1094 return read_count;
1095}
1096
1097/*
1098 * If the ring buffer is non empty (even just a partial subbuffer), return that
1099 * there is data available. Perform a ring buffer flush if we encounter a
1100 * non-empty ring buffer which does not have any consumeable subbuffer available.
1101 */
1102static
1103unsigned int lttng_event_notifier_group_notif_poll(struct file *filp,
1104 poll_table *wait)
1105{
1106 unsigned int mask = 0;
1107 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
1108 struct channel *chan = event_notifier_group->chan;
1109 struct lib_ring_buffer *buf = event_notifier_group->buf;
1110 const struct lib_ring_buffer_config *config = &chan->backend.config;
1111 int finalized, disabled;
1112 unsigned long consumed, offset;
18f12d55 1113 size_t subbuffer_header_size = config->cb.subbuffer_header_size();
db2511b4
MD
1114
1115 if (filp->f_mode & FMODE_READ) {
1116 poll_wait_set_exclusive(wait);
1117 poll_wait(filp, &event_notifier_group->read_wait, wait);
1118
1119 finalized = lib_ring_buffer_is_finalized(config, buf);
1120 disabled = lib_ring_buffer_channel_is_disabled(chan);
1121
1122 /*
1123 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
1124 * finalized load before offsets loads.
1125 */
1126 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1127retry:
1128 if (disabled)
1129 return POLLERR;
1130
1131 offset = lib_ring_buffer_get_offset(config, buf);
1132 consumed = lib_ring_buffer_get_consumed(config, buf);
1133
1134 /*
1135 * If there is no buffer available to consume.
1136 */
1137 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) {
1138 /*
1139 * If there is a non-empty subbuffer, flush and try again.
1140 */
18f12d55 1141 if (subbuf_offset(offset, chan) > subbuffer_header_size) {
db2511b4
MD
1142 lib_ring_buffer_switch_remote(buf);
1143 goto retry;
1144 }
1145
1146 if (finalized)
1147 return POLLHUP;
1148 else {
1149 /*
1150 * The memory barriers
1151 * __wait_event()/wake_up_interruptible() take
1152 * care of "raw_spin_is_locked" memory ordering.
1153 */
1154 if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1155 goto retry;
1156 else
1157 return 0;
1158 }
1159 } else {
1160 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan)
1161 >= chan->backend.buf_size)
1162 return POLLPRI | POLLRDBAND;
1163 else
1164 return POLLIN | POLLRDNORM;
1165 }
1166 }
1167
1168 return mask;
1169}
1170
1171/**
1172 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
1173 * @inode: opened inode
1174 * @file: opened file
1175 *
1176 * Open implementation. Makes sure only one open instance of a buffer is
1177 * done at a given moment.
1178 */
1179static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file)
1180{
1181 struct lttng_event_notifier_group *event_notifier_group = inode->i_private;
1182 struct lib_ring_buffer *buf = event_notifier_group->buf;
1183
1184 file->private_data = event_notifier_group;
1185 return lib_ring_buffer_open(inode, file, buf);
1186}
1187
1188/**
1189 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1190 * @inode: opened inode
1191 * @file: opened file
1192 *
1193 * Release implementation.
1194 */
1195static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file)
1196{
1197 struct lttng_event_notifier_group *event_notifier_group = file->private_data;
1198 struct lib_ring_buffer *buf = event_notifier_group->buf;
1199 int ret;
1200
1201 ret = lib_ring_buffer_release(inode, file, buf);
1202 if (ret)
1203 return ret;
1204 fput(event_notifier_group->file);
1205 return 0;
1206}
1207
21f58fb7
FD
1208static const struct file_operations lttng_event_notifier_group_notif_fops = {
1209 .owner = THIS_MODULE,
db2511b4
MD
1210 .open = lttng_event_notifier_group_notif_open,
1211 .release = lttng_event_notifier_group_notif_release,
1212 .read = lttng_event_notifier_group_notif_read,
1213 .poll = lttng_event_notifier_group_notif_poll,
21f58fb7
FD
1214};
1215
d83004aa
JD
1216/**
1217 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1218 * @filp: the file
1219 * @wait: poll table
1220 *
1221 * Handles the poll operations for the metadata channels.
1222 */
ad1c05e1 1223static
d83004aa
JD
1224unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
1225 poll_table *wait)
1226{
1227 struct lttng_metadata_stream *stream = filp->private_data;
1228 struct lib_ring_buffer *buf = stream->priv;
1229 int finalized;
1230 unsigned int mask = 0;
1231
1232 if (filp->f_mode & FMODE_READ) {
1233 poll_wait_set_exclusive(wait);
1234 poll_wait(filp, &stream->read_wait, wait);
1235
1236 finalized = stream->finalized;
1237
1238 /*
1239 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1240 * ordering finalized load before offsets loads.
1241 */
1242 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1243
1244 if (finalized)
1245 mask |= POLLHUP;
1246
92d9f5e6 1247 mutex_lock(&stream->metadata_cache->lock);
d83004aa 1248 if (stream->metadata_cache->metadata_written >
f613e3e6 1249 stream->metadata_out)
d83004aa 1250 mask |= POLLIN;
92d9f5e6 1251 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
1252 }
1253
1254 return mask;
1255}
1256
f613e3e6
MD
1257static
1258void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
1259 unsigned int cmd, unsigned long arg)
1260{
1261 struct lttng_metadata_stream *stream = filp->private_data;
1262
1263 stream->metadata_out = stream->metadata_in;
1264}
1265
d1344afa
JD
1266/*
1267 * Reset the counter of how much metadata has been consumed to 0. That way,
1268 * the consumer receives the content of the metadata cache unchanged. This is
1269 * different from the metadata_regenerate where the offset from epoch is
1270 * resampled, here we want the exact same content as the last time the metadata
1271 * was generated. This command is only possible if all the metadata written
1272 * in the cache has been output to the metadata stream to avoid corrupting the
1273 * metadata file.
1274 *
1275 * Return 0 on success, a negative value on error.
1276 */
1277static
1278int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
1279{
1280 int ret;
1281 struct lttng_metadata_cache *cache = stream->metadata_cache;
1282
1283 mutex_lock(&cache->lock);
1284 if (stream->metadata_out != cache->metadata_written) {
1285 ret = -EBUSY;
1286 goto end;
1287 }
1288 stream->metadata_out = 0;
1289 stream->metadata_in = 0;
1290 wake_up_interruptible(&stream->read_wait);
1291 ret = 0;
1292
1293end:
1294 mutex_unlock(&cache->lock);
1295 return ret;
1296}
1297
d83004aa
JD
1298static
1299long lttng_metadata_ring_buffer_ioctl(struct file *filp,
1300 unsigned int cmd, unsigned long arg)
1301{
1302 int ret;
1303 struct lttng_metadata_stream *stream = filp->private_data;
1304 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
1305 unsigned int rb_cmd;
1306 bool coherent;
1307
606828e4
MD
1308 if (cmd == LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1309 rb_cmd = LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF;
8b97fd42
MD
1310 else
1311 rb_cmd = cmd;
d83004aa
JD
1312
1313 switch (cmd) {
606828e4 1314 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF:
d83004aa 1315 {
35097f36
JD
1316 struct lttng_metadata_stream *stream = filp->private_data;
1317 struct lib_ring_buffer *buf = stream->priv;
1318 struct channel *chan = buf->backend.chan;
1319
8b97fd42 1320 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1321 if (ret > 0) {
1322 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1323 ret = 0;
1324 } else if (ret < 0)
d83004aa
JD
1325 goto err;
1326 break;
1327 }
606828e4 1328 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF:
f613e3e6
MD
1329 {
1330 /*
1331 * Random access is not allowed for metadata channel.
1332 */
1333 return -ENOSYS;
1334 }
606828e4
MD
1335 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
1336 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH:
35097f36
JD
1337 {
1338 struct lttng_metadata_stream *stream = filp->private_data;
1339 struct lib_ring_buffer *buf = stream->priv;
1340 struct channel *chan = buf->backend.chan;
1341
1342 /*
1343 * Before doing the actual ring buffer flush, write up to one
1344 * packet of metadata in the ring buffer.
1345 */
8b97fd42 1346 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1347 if (ret < 0)
1348 goto err;
1349 break;
1350 }
606828e4 1351 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION:
9616f0bf
JD
1352 {
1353 struct lttng_metadata_stream *stream = filp->private_data;
1354
1355 return put_u64(stream->version, arg);
1356 }
606828e4 1357 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP:
d1344afa
JD
1358 {
1359 struct lttng_metadata_stream *stream = filp->private_data;
1360
1361 return lttng_metadata_cache_dump(stream);
1362 }
606828e4 1363 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1364 {
1365 struct lttng_metadata_stream *stream = filp->private_data;
1366 struct lib_ring_buffer *buf = stream->priv;
1367 struct channel *chan = buf->backend.chan;
1368
1369 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1370 if (ret > 0) {
1371 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1372 ret = 0;
1373 } else if (ret < 0) {
1374 goto err;
1375 }
1376 break;
1377 }
d83004aa
JD
1378 default:
1379 break;
1380 }
f613e3e6
MD
1381 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1382
d83004aa 1383 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1384 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1385 if (ret < 0)
1386 goto err;
d83004aa 1387
f613e3e6 1388 switch (cmd) {
606828e4 1389 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF:
f613e3e6
MD
1390 {
1391 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1392 cmd, arg);
1393 break;
1394 }
606828e4 1395 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1396 {
1397 return put_u32(coherent, arg);
1398 }
f613e3e6
MD
1399 default:
1400 break;
1401 }
d83004aa
JD
1402err:
1403 return ret;
1404}
1405
aeb9064d 1406#ifdef CONFIG_COMPAT
d83004aa
JD
1407static
1408long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
1409 unsigned int cmd, unsigned long arg)
1410{
1411 int ret;
1412 struct lttng_metadata_stream *stream = filp->private_data;
1413 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
1414 unsigned int rb_cmd;
1415 bool coherent;
1416
606828e4
MD
1417 if (cmd == LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1418 rb_cmd = LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF;
8b97fd42
MD
1419 else
1420 rb_cmd = cmd;
d83004aa
JD
1421
1422 switch (cmd) {
606828e4 1423 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF:
d83004aa 1424 {
35097f36
JD
1425 struct lttng_metadata_stream *stream = filp->private_data;
1426 struct lib_ring_buffer *buf = stream->priv;
1427 struct channel *chan = buf->backend.chan;
1428
8b97fd42 1429 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1430 if (ret > 0) {
1431 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1432 ret = 0;
1433 } else if (ret < 0)
d83004aa
JD
1434 goto err;
1435 break;
1436 }
606828e4 1437 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF:
f613e3e6
MD
1438 {
1439 /*
1440 * Random access is not allowed for metadata channel.
1441 */
1442 return -ENOSYS;
1443 }
606828e4
MD
1444 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
1445 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH:
96c55c2f
MD
1446 {
1447 struct lttng_metadata_stream *stream = filp->private_data;
1448 struct lib_ring_buffer *buf = stream->priv;
1449 struct channel *chan = buf->backend.chan;
1450
1451 /*
1452 * Before doing the actual ring buffer flush, write up to one
1453 * packet of metadata in the ring buffer.
1454 */
8b97fd42 1455 ret = lttng_metadata_output_channel(stream, chan, NULL);
96c55c2f
MD
1456 if (ret < 0)
1457 goto err;
1458 break;
1459 }
606828e4 1460 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION:
96c55c2f
MD
1461 {
1462 struct lttng_metadata_stream *stream = filp->private_data;
1463
1464 return put_u64(stream->version, arg);
1465 }
606828e4 1466 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP:
d1344afa
JD
1467 {
1468 struct lttng_metadata_stream *stream = filp->private_data;
1469
1470 return lttng_metadata_cache_dump(stream);
1471 }
606828e4 1472 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1473 {
1474 struct lttng_metadata_stream *stream = filp->private_data;
1475 struct lib_ring_buffer *buf = stream->priv;
1476 struct channel *chan = buf->backend.chan;
1477
1478 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1479 if (ret > 0) {
1480 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1481 ret = 0;
1482 } else if (ret < 0) {
1483 goto err;
1484 }
1485 break;
1486 }
d83004aa
JD
1487 default:
1488 break;
1489 }
f613e3e6
MD
1490 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1491
d83004aa 1492 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1493 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1494 if (ret < 0)
1495 goto err;
d83004aa 1496
f613e3e6 1497 switch (cmd) {
606828e4 1498 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF:
f613e3e6
MD
1499 {
1500 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1501 cmd, arg);
1502 break;
1503 }
606828e4 1504 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1505 {
1506 return put_u32(coherent, arg);
1507 }
f613e3e6
MD
1508 default:
1509 break;
1510 }
d83004aa
JD
1511err:
1512 return ret;
1513}
aeb9064d 1514#endif
d83004aa 1515
b3b8072b
MD
1516/*
1517 * This is not used by anonymous file descriptors. This code is left
1518 * there if we ever want to implement an inode with open() operation.
1519 */
d83004aa
JD
1520static
1521int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1522{
1523 struct lttng_metadata_stream *stream = inode->i_private;
1524 struct lib_ring_buffer *buf = stream->priv;
1525
1526 file->private_data = buf;
b3b8072b
MD
1527 /*
1528 * Since life-time of metadata cache differs from that of
1529 * session, we need to keep our own reference on the transport.
1530 */
1531 if (!try_module_get(stream->transport->owner)) {
5a15f70c 1532 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1533 return -EBUSY;
1534 }
d83004aa
JD
1535 return lib_ring_buffer_open(inode, file, buf);
1536}
1537
1538static
1539int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1540{
1541 struct lttng_metadata_stream *stream = file->private_data;
1542 struct lib_ring_buffer *buf = stream->priv;
1543
92143b2c
MD
1544 mutex_lock(&stream->metadata_cache->lock);
1545 list_del(&stream->list);
1546 mutex_unlock(&stream->metadata_cache->lock);
d83004aa 1547 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 1548 module_put(stream->transport->owner);
92143b2c 1549 kfree(stream);
d83004aa
JD
1550 return lib_ring_buffer_release(inode, file, buf);
1551}
1552
1553static
1554ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1555 struct pipe_inode_info *pipe, size_t len,
1556 unsigned int flags)
1557{
1558 struct lttng_metadata_stream *stream = in->private_data;
1559 struct lib_ring_buffer *buf = stream->priv;
1560
1561 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1562 flags, buf);
1563}
1564
1565static
1566int lttng_metadata_ring_buffer_mmap(struct file *filp,
1567 struct vm_area_struct *vma)
1568{
1569 struct lttng_metadata_stream *stream = filp->private_data;
1570 struct lib_ring_buffer *buf = stream->priv;
1571
1572 return lib_ring_buffer_mmap(filp, vma, buf);
1573}
1574
1575static
1576const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1577 .owner = THIS_MODULE,
1578 .open = lttng_metadata_ring_buffer_open,
1579 .release = lttng_metadata_ring_buffer_release,
1580 .poll = lttng_metadata_ring_buffer_poll,
1581 .splice_read = lttng_metadata_ring_buffer_splice_read,
1582 .mmap = lttng_metadata_ring_buffer_mmap,
1583 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1584 .llseek = vfs_lib_ring_buffer_no_llseek,
1585#ifdef CONFIG_COMPAT
1586 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1587#endif
1588};
1589
1590static
1591int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
a3fcd499 1592 const struct file_operations *fops, const char *name)
ad1c05e1 1593{
ad1c05e1 1594 int stream_fd, ret;
11b5a3c2 1595 struct file *stream_file;
ad1c05e1 1596
4ac10b76 1597 stream_fd = lttng_get_unused_fd();
ad1c05e1
MD
1598 if (stream_fd < 0) {
1599 ret = stream_fd;
1600 goto fd_error;
1601 }
a3fcd499 1602 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
c0e31d2e
MD
1603 if (IS_ERR(stream_file)) {
1604 ret = PTR_ERR(stream_file);
ad1c05e1
MD
1605 goto file_error;
1606 }
409453cb
MD
1607 /*
1608 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1609 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1610 * file descriptor, so we set FMODE_PREAD here.
1611 */
d7b6f197 1612 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 1613 fd_install(stream_fd, stream_file);
dda6a249
MD
1614 /*
1615 * The stream holds a reference to the channel within the generic ring
1616 * buffer library, so no need to hold a refcount on the channel and
1617 * session files here.
1618 */
ad1c05e1
MD
1619 return stream_fd;
1620
1621file_error:
1622 put_unused_fd(stream_fd);
d83004aa
JD
1623fd_error:
1624 return ret;
1625}
1626
1627static
1628int lttng_abi_open_stream(struct file *channel_file)
1629{
f7d06400 1630 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
d83004aa
JD
1631 struct lib_ring_buffer *buf;
1632 int ret;
1633 void *stream_priv;
1634
f7d06400 1635 buf = channel->ops->priv->buffer_read_open(channel->priv->rb_chan);
d83004aa
JD
1636 if (!buf)
1637 return -ENOENT;
1638
1639 stream_priv = buf;
1640 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1641 &lttng_stream_ring_buffer_file_operations,
1642 "[lttng_stream]");
d83004aa
JD
1643 if (ret < 0)
1644 goto fd_error;
1645
1646 return ret;
1647
1648fd_error:
4a399b76 1649 channel->ops->priv->buffer_read_close(buf);
d83004aa
JD
1650 return ret;
1651}
1652
1653static
1654int lttng_abi_open_metadata_stream(struct file *channel_file)
1655{
f7d06400
MD
1656 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
1657 struct lttng_kernel_session *session = channel->parent.session;
d83004aa
JD
1658 struct lib_ring_buffer *buf;
1659 int ret;
1660 struct lttng_metadata_stream *metadata_stream;
1661 void *stream_priv;
1662
f7d06400 1663 buf = channel->ops->priv->buffer_read_open(channel->priv->rb_chan);
d83004aa
JD
1664 if (!buf)
1665 return -ENOENT;
1666
1667 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1668 GFP_KERNEL);
b3b8072b
MD
1669 if (!metadata_stream) {
1670 ret = -ENOMEM;
1671 goto nomem;
1672 }
8cdc1a81 1673 metadata_stream->metadata_cache = session->priv->metadata_cache;
d83004aa
JD
1674 init_waitqueue_head(&metadata_stream->read_wait);
1675 metadata_stream->priv = buf;
1676 stream_priv = metadata_stream;
f7d06400 1677 metadata_stream->transport = channel->priv->transport;
8b97fd42
MD
1678 /* Initial state is an empty metadata, considered as incoherent. */
1679 metadata_stream->coherent = false;
b3b8072b
MD
1680
1681 /*
1682 * Since life-time of metadata cache differs from that of
1683 * session, we need to keep our own reference on the transport.
1684 */
1685 if (!try_module_get(metadata_stream->transport->owner)) {
5a15f70c 1686 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1687 ret = -EINVAL;
1688 goto notransport;
1689 }
1690
8cdc1a81 1691 if (!lttng_kref_get(&session->priv->metadata_cache->refcount)) {
901aaa5f 1692 ret = -EOVERFLOW;
9c1f4643 1693 goto kref_error;
901aaa5f
FD
1694 }
1695
d83004aa 1696 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1697 &lttng_metadata_ring_buffer_file_operations,
1698 "[lttng_metadata_stream]");
d83004aa
JD
1699 if (ret < 0)
1700 goto fd_error;
1701
8cdc1a81 1702 mutex_lock(&session->priv->metadata_cache->lock);
d83004aa 1703 list_add(&metadata_stream->list,
8cdc1a81
MD
1704 &session->priv->metadata_cache->metadata_stream);
1705 mutex_unlock(&session->priv->metadata_cache->lock);
d83004aa
JD
1706 return ret;
1707
ad1c05e1 1708fd_error:
8cdc1a81 1709 kref_put(&session->priv->metadata_cache->refcount, metadata_cache_destroy);
9c1f4643 1710kref_error:
b3b8072b
MD
1711 module_put(metadata_stream->transport->owner);
1712notransport:
1713 kfree(metadata_stream);
1714nomem:
4a399b76 1715 channel->ops->priv->buffer_read_close(buf);
ad1c05e1
MD
1716 return ret;
1717}
1718
21f58fb7
FD
1719static
1720int lttng_abi_open_event_notifier_group_stream(struct file *notif_file)
1721{
1722 struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data;
1723 struct channel *chan = event_notifier_group->chan;
1724 struct lib_ring_buffer *buf;
1725 int ret;
1726 void *stream_priv;
1727
4a399b76 1728 buf = event_notifier_group->ops->priv->buffer_read_open(chan);
21f58fb7
FD
1729 if (!buf)
1730 return -ENOENT;
1731
1732 /* The event_notifier notification fd holds a reference on the event_notifier group */
1733 if (!atomic_long_add_unless(&notif_file->f_count, 1, LONG_MAX)) {
1734 ret = -EOVERFLOW;
1735 goto refcount_error;
1736 }
1737 event_notifier_group->buf = buf;
1738 stream_priv = event_notifier_group;
1739 ret = lttng_abi_create_stream_fd(notif_file, stream_priv,
1740 &lttng_event_notifier_group_notif_fops,
1741 "[lttng_event_notifier_stream]");
1742 if (ret < 0)
1743 goto fd_error;
1744
1745 return ret;
1746
1747fd_error:
1748 atomic_long_dec(&notif_file->f_count);
1749refcount_error:
4a399b76 1750 event_notifier_group->ops->priv->buffer_read_close(buf);
21f58fb7
FD
1751 return ret;
1752}
1753
badfe9f5 1754static
606828e4 1755int lttng_abi_validate_event_param(struct lttng_kernel_abi_event *event_param)
badfe9f5
MD
1756{
1757 /* Limit ABI to implemented features. */
1758 switch (event_param->instrumentation) {
606828e4 1759 case LTTNG_KERNEL_ABI_SYSCALL:
badfe9f5 1760 switch (event_param->u.syscall.entryexit) {
606828e4
MD
1761 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: /* Fall-through */
1762 case LTTNG_KERNEL_ABI_SYSCALL_EXIT: /* Fall-through */
1763 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT:
badfe9f5
MD
1764 break;
1765 default:
1766 return -EINVAL;
1767 }
1768 switch (event_param->u.syscall.abi) {
606828e4 1769 case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL:
badfe9f5
MD
1770 break;
1771 default:
1772 return -EINVAL;
1773 }
1774 switch (event_param->u.syscall.match) {
606828e4 1775 case LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME:
badfe9f5
MD
1776 break;
1777 default:
1778 return -EINVAL;
1779 }
1780 break;
1781
606828e4 1782 case LTTNG_KERNEL_ABI_KRETPROBE:
88a82b17 1783 switch (event_param->u.kretprobe.entryexit) {
606828e4 1784 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT:
88a82b17 1785 break;
606828e4
MD
1786 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: /* Fall-through */
1787 case LTTNG_KERNEL_ABI_SYSCALL_EXIT: /* Fall-through */
88a82b17
MD
1788 default:
1789 return -EINVAL;
1790 }
1791 break;
1792
606828e4
MD
1793 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
1794 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
1795 case LTTNG_KERNEL_ABI_UPROBE:
badfe9f5
MD
1796 break;
1797
606828e4
MD
1798 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
1799 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
badfe9f5
MD
1800 default:
1801 return -EINVAL;
1802 }
1803 return 0;
1804}
1805
653fe716 1806static
c0e31d2e 1807int lttng_abi_create_event(struct file *channel_file,
606828e4 1808 struct lttng_kernel_abi_event *event_param)
653fe716 1809{
ef784b4d 1810 const struct file_operations *fops;
f7d06400 1811 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
653fe716 1812 int event_fd, ret;
11b5a3c2 1813 struct file *event_file;
3c997079 1814 void *priv;
653fe716 1815
606828e4 1816 event_param->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
6dccd6c1 1817 switch (event_param->instrumentation) {
606828e4
MD
1818 case LTTNG_KERNEL_ABI_KRETPROBE:
1819 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
7371f44c 1820 break;
606828e4
MD
1821 case LTTNG_KERNEL_ABI_KPROBE:
1822 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 1823 break;
606828e4 1824 case LTTNG_KERNEL_ABI_FUNCTION:
e884017c
MD
1825 WARN_ON_ONCE(1);
1826 /* Not implemented. */
e0a7a7c4
MD
1827 break;
1828 default:
1829 break;
1830 }
ef784b4d
MD
1831
1832 switch (event_param->instrumentation) {
1833 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
1834 case LTTNG_KERNEL_ABI_SYSCALL:
1835 fops = &lttng_event_recorder_enabler_fops;
1836 break;
1837 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
1838 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
1839 case LTTNG_KERNEL_ABI_UPROBE:
1840 fops = &lttng_event_recorder_event_fops;
1841 break;
1842
1843 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
1844 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
1845 default:
1846 return -EINVAL;
1847 }
1848
33a39a3c
MD
1849 event_fd = lttng_get_unused_fd();
1850 if (event_fd < 0) {
1851 ret = event_fd;
1852 goto fd_error;
1853 }
1854 event_file = anon_inode_getfile("[lttng_event]",
ef784b4d 1855 fops, NULL, O_RDWR);
33a39a3c
MD
1856 if (IS_ERR(event_file)) {
1857 ret = PTR_ERR(event_file);
1858 goto file_error;
1859 }
9c1f4643 1860 /* The event holds a reference on the channel */
98d7281c 1861 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
0d2c717f 1862 ret = -EOVERFLOW;
9c1f4643
MD
1863 goto refcount_error;
1864 }
badfe9f5
MD
1865 ret = lttng_abi_validate_event_param(event_param);
1866 if (ret)
1867 goto event_error;
684a1e4d
MD
1868
1869 switch (event_param->instrumentation) {
606828e4
MD
1870 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
1871 case LTTNG_KERNEL_ABI_SYSCALL:
684a1e4d 1872 {
b2bc0bc8 1873 struct lttng_event_enabler *event_enabler;
33a39a3c 1874
4993071a
PP
1875 if (strutils_is_star_glob_pattern(event_param->name)) {
1876 /*
1877 * If the event name is a star globbing pattern,
1878 * we create the special star globbing enabler.
1879 */
b2bc0bc8 1880 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
33a39a3c 1881 event_param, channel);
3c997079 1882 } else {
b2bc0bc8 1883 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
33a39a3c 1884 event_param, channel);
1ec65de1 1885 }
b2bc0bc8 1886 priv = event_enabler;
684a1e4d
MD
1887 break;
1888 }
1889
ef784b4d 1890 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
606828e4
MD
1891 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
1892 case LTTNG_KERNEL_ABI_UPROBE:
684a1e4d 1893 {
a67ba386 1894 struct lttng_kernel_event_recorder *event;
4ee2453d 1895
33a39a3c
MD
1896 /*
1897 * We tolerate no failure path after event creation. It
1898 * will stay invariant for the rest of the session.
1899 */
a67ba386
MD
1900 event = lttng_kernel_event_recorder_create(channel, event_param,
1901 NULL, event_param->instrumentation);
33a39a3c
MD
1902 WARN_ON_ONCE(!event);
1903 if (IS_ERR(event)) {
1904 ret = PTR_ERR(event);
1905 goto event_error;
80f87dd2 1906 }
33a39a3c 1907 priv = event;
684a1e4d
MD
1908 break;
1909 }
1910
ef784b4d 1911 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
606828e4 1912 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
684a1e4d
MD
1913 default:
1914 ret = -EINVAL;
1915 goto event_error;
03037b98 1916 }
33a39a3c
MD
1917 event_file->private_data = priv;
1918 fd_install(event_fd, event_file);
653fe716
MD
1919 return event_fd;
1920
03037b98 1921event_error:
9c1f4643
MD
1922 atomic_long_dec(&channel_file->f_count);
1923refcount_error:
c0e31d2e 1924 fput(event_file);
653fe716
MD
1925file_error:
1926 put_unused_fd(event_fd);
1927fd_error:
653fe716
MD
1928 return ret;
1929}
ad1c05e1 1930
dffef45d 1931static
ef784b4d 1932long lttng_event_notifier_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dffef45d 1933{
a67ba386 1934 struct lttng_kernel_event_notifier *event_notifier = file->private_data;
dffef45d
FD
1935
1936 switch (cmd) {
606828e4 1937 case LTTNG_KERNEL_ABI_ENABLE:
9d993668 1938 return lttng_event_enable(&event_notifier->parent);
606828e4 1939 case LTTNG_KERNEL_ABI_DISABLE:
9d993668 1940 return lttng_event_disable(&event_notifier->parent);
606828e4 1941 case LTTNG_KERNEL_ABI_FILTER:
ef784b4d 1942 return -EINVAL;
606828e4 1943 case LTTNG_KERNEL_ABI_CAPTURE:
ef784b4d 1944 return -EINVAL;
606828e4 1945 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
e2d5dbc7 1946 return lttng_event_add_callsite(&event_notifier->parent,
ef784b4d 1947 (struct lttng_kernel_abi_event_callsite __user *) arg);
dffef45d
FD
1948 default:
1949 return -ENOIOCTLCMD;
1950 }
1951}
1952
1953static
ef784b4d 1954long lttng_event_notifier_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dffef45d 1955{
ef784b4d 1956 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
dffef45d 1957
ef784b4d
MD
1958 switch (cmd) {
1959 case LTTNG_KERNEL_ABI_ENABLE:
1960 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
1961 case LTTNG_KERNEL_ABI_DISABLE:
1962 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
1963 case LTTNG_KERNEL_ABI_FILTER:
1964 return lttng_event_notifier_enabler_attach_filter_bytecode(
1965 event_notifier_enabler,
1966 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
1967 case LTTNG_KERNEL_ABI_CAPTURE:
1968 return lttng_event_notifier_enabler_attach_capture_bytecode(
1969 event_notifier_enabler,
1970 (struct lttng_kernel_abi_capture_bytecode __user *) arg);
1971 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
1972 return -EINVAL;
dffef45d 1973 default:
ef784b4d 1974 return -ENOIOCTLCMD;
dffef45d 1975 }
ef784b4d 1976}
dffef45d 1977
ef784b4d
MD
1978static
1979int lttng_event_notifier_event_release(struct inode *inode, struct file *file)
1980{
a67ba386 1981 struct lttng_kernel_event_notifier *event_notifier = file->private_data;
ef784b4d
MD
1982
1983 if (event_notifier)
a67ba386 1984 fput(event_notifier->priv->group->file);
dffef45d
FD
1985 return 0;
1986}
1987
ef784b4d
MD
1988static
1989int lttng_event_notifier_enabler_release(struct inode *inode, struct file *file)
1990{
1991 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
1992
1993 if (event_notifier_enabler)
1994 fput(event_notifier_enabler->group->file);
1995 return 0;
1996}
1997
1998static const struct file_operations lttng_event_notifier_event_fops = {
1999 .owner = THIS_MODULE,
2000 .release = lttng_event_notifier_event_release,
2001 .unlocked_ioctl = lttng_event_notifier_event_ioctl,
2002#ifdef CONFIG_COMPAT
2003 .compat_ioctl = lttng_event_notifier_event_ioctl,
2004#endif
2005};
2006
2007static const struct file_operations lttng_event_notifier_enabler_fops = {
dffef45d 2008 .owner = THIS_MODULE,
ef784b4d
MD
2009 .release = lttng_event_notifier_enabler_release,
2010 .unlocked_ioctl = lttng_event_notifier_enabler_ioctl,
dffef45d 2011#ifdef CONFIG_COMPAT
ef784b4d 2012 .compat_ioctl = lttng_event_notifier_enabler_ioctl,
dffef45d
FD
2013#endif
2014};
2015
2016static
2017int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
606828e4 2018 struct lttng_kernel_abi_event_notifier *event_notifier_param)
dffef45d
FD
2019{
2020 struct lttng_event_notifier_group *event_notifier_group =
2021 event_notifier_group_file->private_data;
ef784b4d 2022 const struct file_operations *fops;
dffef45d
FD
2023 int event_notifier_fd, ret;
2024 struct file *event_notifier_file;
2025 void *priv;
2026
2027 switch (event_notifier_param->event.instrumentation) {
606828e4
MD
2028 case LTTNG_KERNEL_ABI_TRACEPOINT:
2029 case LTTNG_KERNEL_ABI_UPROBE:
b01155ba 2030 break;
606828e4
MD
2031 case LTTNG_KERNEL_ABI_KPROBE:
2032 event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
2b16f0c9 2033 break;
606828e4 2034 case LTTNG_KERNEL_ABI_SYSCALL:
8a8ac9a8 2035 break;
606828e4 2036 case LTTNG_KERNEL_ABI_KRETPROBE:
9de67196 2037 /* Placing an event notifier on kretprobe is not supported. */
606828e4
MD
2038 case LTTNG_KERNEL_ABI_FUNCTION:
2039 case LTTNG_KERNEL_ABI_NOOP:
dffef45d
FD
2040 default:
2041 ret = -EINVAL;
2042 goto inval_instr;
2043 }
2044
ef784b4d
MD
2045 switch (event_notifier_param->event.instrumentation) {
2046 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
2047 case LTTNG_KERNEL_ABI_SYSCALL:
2048 fops = &lttng_event_notifier_enabler_fops;
2049 break;
2050 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
2051 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
2052 case LTTNG_KERNEL_ABI_UPROBE:
2053 fops = &lttng_event_notifier_event_fops;
2054 break;
2055
2056 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
2057 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
2058 default:
2059 ret = -EINVAL;
2060 goto inval_instr;
2061 }
2062
606828e4 2063 event_notifier_param->event.name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
dffef45d
FD
2064
2065 event_notifier_fd = lttng_get_unused_fd();
2066 if (event_notifier_fd < 0) {
2067 ret = event_notifier_fd;
2068 goto fd_error;
2069 }
2070
2071 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
ef784b4d 2072 fops, NULL, O_RDWR);
dffef45d
FD
2073 if (IS_ERR(event_notifier_file)) {
2074 ret = PTR_ERR(event_notifier_file);
2075 goto file_error;
2076 }
2077
2078 /* The event notifier holds a reference on the event notifier group. */
2079 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2080 ret = -EOVERFLOW;
2081 goto refcount_error;
2082 }
2083
56237619
MD
2084 ret = lttng_abi_validate_event_param(&event_notifier_param->event);
2085 if (ret)
2086 goto event_notifier_error;
2087
684a1e4d 2088 switch (event_notifier_param->event.instrumentation) {
606828e4
MD
2089 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
2090 case LTTNG_KERNEL_ABI_SYSCALL:
684a1e4d 2091 {
dffef45d
FD
2092 struct lttng_event_notifier_enabler *enabler;
2093
2094 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
2095 /*
2096 * If the event name is a star globbing pattern,
2097 * we create the special star globbing enabler.
2098 */
2099 enabler = lttng_event_notifier_enabler_create(
2100 event_notifier_group,
2101 LTTNG_ENABLER_FORMAT_STAR_GLOB,
2102 event_notifier_param);
2103 } else {
2104 enabler = lttng_event_notifier_enabler_create(
2105 event_notifier_group,
2106 LTTNG_ENABLER_FORMAT_NAME,
2107 event_notifier_param);
2108 }
2109 priv = enabler;
684a1e4d
MD
2110 break;
2111 }
2112
ef784b4d 2113 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
606828e4
MD
2114 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
2115 case LTTNG_KERNEL_ABI_UPROBE:
684a1e4d 2116 {
a67ba386 2117 struct lttng_kernel_event_notifier *event_notifier;
dffef45d
FD
2118
2119 /*
2120 * We tolerate no failure path after event notifier creation.
2121 * It will stay invariant for the rest of the session.
2122 */
2123 event_notifier = lttng_event_notifier_create(NULL,
99f52fcc
FD
2124 event_notifier_param->event.token,
2125 event_notifier_param->error_counter_index,
2126 event_notifier_group,
a67ba386 2127 event_notifier_param,
dffef45d
FD
2128 event_notifier_param->event.instrumentation);
2129 WARN_ON_ONCE(!event_notifier);
2130 if (IS_ERR(event_notifier)) {
2131 ret = PTR_ERR(event_notifier);
2132 goto event_notifier_error;
2133 }
2134 priv = event_notifier;
684a1e4d
MD
2135 break;
2136 }
2137
ef784b4d 2138 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
606828e4 2139 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
684a1e4d
MD
2140 default:
2141 ret = -EINVAL;
2142 goto event_notifier_error;
dffef45d
FD
2143 }
2144 event_notifier_file->private_data = priv;
2145 fd_install(event_notifier_fd, event_notifier_file);
2146 return event_notifier_fd;
2147
2148event_notifier_error:
2149 atomic_long_dec(&event_notifier_group_file->f_count);
2150refcount_error:
2151 fput(event_notifier_file);
2152file_error:
2153 put_unused_fd(event_notifier_fd);
2154fd_error:
2155inval_instr:
2156 return ret;
2157}
2158
99f52fcc
FD
2159static
2160long lttng_abi_event_notifier_group_create_error_counter(
2161 struct file *event_notifier_group_file,
606828e4 2162 const struct lttng_kernel_abi_counter_conf *error_counter_conf)
99f52fcc
FD
2163{
2164 int counter_fd, ret;
2165 char *counter_transport_name;
2166 size_t counter_len;
2167 struct lttng_counter *counter = NULL;
2168 struct file *counter_file;
2169 struct lttng_event_notifier_group *event_notifier_group =
2170 (struct lttng_event_notifier_group *) event_notifier_group_file->private_data;
2171
606828e4 2172 if (error_counter_conf->arithmetic != LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR) {
99f52fcc
FD
2173 printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2174 return -EINVAL;
2175 }
2176
2177 if (error_counter_conf->number_dimensions != 1) {
2178 printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n");
2179 return -EINVAL;
2180 }
2181
2182 switch (error_counter_conf->bitness) {
606828e4 2183 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_64:
99f52fcc
FD
2184 counter_transport_name = "counter-per-cpu-64-modular";
2185 break;
606828e4 2186 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_32:
99f52fcc
FD
2187 counter_transport_name = "counter-per-cpu-32-modular";
2188 break;
2189 default:
2190 return -EINVAL;
2191 }
2192
2193 /*
2194 * Lock sessions to provide mutual exclusion against concurrent
2195 * modification of event_notifier group, which would result in
2196 * overwriting the error counter if set concurrently.
2197 */
2198 lttng_lock_sessions();
2199
2200 if (event_notifier_group->error_counter) {
2201 printk(KERN_ERR "Error counter already created in event_notifier group\n");
2202 ret = -EBUSY;
2203 goto fd_error;
2204 }
2205
2206 counter_fd = lttng_get_unused_fd();
2207 if (counter_fd < 0) {
2208 ret = counter_fd;
2209 goto fd_error;
2210 }
2211
2212 counter_file = anon_inode_getfile("[lttng_counter]",
2213 &lttng_counter_fops,
2214 NULL, O_RDONLY);
2215 if (IS_ERR(counter_file)) {
2216 ret = PTR_ERR(counter_file);
2217 goto file_error;
2218 }
2219
2220 counter_len = error_counter_conf->dimensions[0].size;
2221
2222 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2223 ret = -EOVERFLOW;
2224 goto refcount_error;
2225 }
2226
2227 counter = lttng_kernel_counter_create(counter_transport_name,
2228 1, &counter_len);
2229 if (!counter) {
2230 ret = -EINVAL;
2231 goto counter_error;
2232 }
2233
99f52fcc 2234 event_notifier_group->error_counter_len = counter_len;
ab04d7b1
MD
2235 /*
2236 * store-release to publish error counter matches load-acquire
2237 * in record_error. Ensures the counter is created and the
2238 * error_counter_len is set before they are used.
2239 */
6657edec 2240 lttng_smp_store_release(&event_notifier_group->error_counter, counter);
99f52fcc
FD
2241
2242 counter->file = counter_file;
2243 counter->owner = event_notifier_group->file;
2244 counter_file->private_data = counter;
2245 /* Ownership transferred. */
2246 counter = NULL;
2247
2248 fd_install(counter_fd, counter_file);
2249 lttng_unlock_sessions();
2250
2251 return counter_fd;
2252
2253counter_error:
2254 atomic_long_dec(&event_notifier_group_file->f_count);
2255refcount_error:
2256 fput(counter_file);
2257file_error:
2258 put_unused_fd(counter_fd);
2259fd_error:
2260 lttng_unlock_sessions();
2261 return ret;
2262}
2263
750b05f2
FD
2264static
2265long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
2266 unsigned long arg)
2267{
2268 switch (cmd) {
606828e4 2269 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
21f58fb7
FD
2270 {
2271 return lttng_abi_open_event_notifier_group_stream(file);
2272 }
606828e4 2273 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE:
dffef45d 2274 {
606828e4 2275 struct lttng_kernel_abi_event_notifier uevent_notifier_param;
dffef45d
FD
2276
2277 if (copy_from_user(&uevent_notifier_param,
606828e4 2278 (struct lttng_kernel_abi_event_notifier __user *) arg,
dffef45d
FD
2279 sizeof(uevent_notifier_param)))
2280 return -EFAULT;
2281 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
2282 }
606828e4 2283 case LTTNG_KERNEL_ABI_COUNTER:
99f52fcc 2284 {
606828e4 2285 struct lttng_kernel_abi_counter_conf uerror_counter_conf;
99f52fcc
FD
2286
2287 if (copy_from_user(&uerror_counter_conf,
606828e4 2288 (struct lttng_kernel_abi_counter_conf __user *) arg,
99f52fcc
FD
2289 sizeof(uerror_counter_conf)))
2290 return -EFAULT;
2291 return lttng_abi_event_notifier_group_create_error_counter(file,
2292 &uerror_counter_conf);
2293 }
750b05f2
FD
2294 default:
2295 return -ENOIOCTLCMD;
2296 }
2297 return 0;
2298}
2299
2300static
2301int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
2302{
2303 struct lttng_event_notifier_group *event_notifier_group =
2304 file->private_data;
2305
2306 if (event_notifier_group)
2307 lttng_event_notifier_group_destroy(event_notifier_group);
2308 return 0;
2309}
2310
2311static const struct file_operations lttng_event_notifier_group_fops = {
2312 .owner = THIS_MODULE,
2313 .release = lttng_event_notifier_group_release,
2314 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
2315#ifdef CONFIG_COMPAT
2316 .compat_ioctl = lttng_event_notifier_group_ioctl,
2317#endif
2318};
2319
ad1c05e1
MD
2320/**
2321 * lttng_channel_ioctl - lttng syscall through ioctl
2322 *
c0e31d2e 2323 * @file: the file
ad1c05e1
MD
2324 * @cmd: the command
2325 * @arg: command arg
2326 *
2327 * This ioctl implements lttng commands:
606828e4 2328 * LTTNG_KERNEL_ABI_STREAM
ad1c05e1
MD
2329 * Returns an event stream file descriptor or failure.
2330 * (typically, one event stream records events from one CPU)
606828e4 2331 * LTTNG_KERNEL_ABI_EVENT
ad1c05e1 2332 * Returns an event file descriptor or failure.
606828e4 2333 * LTTNG_KERNEL_ABI_CONTEXT
8070f5c0 2334 * Prepend a context field to each event in the channel
606828e4 2335 * LTTNG_KERNEL_ABI_ENABLE
e64957da 2336 * Enable recording for events in this channel (weak enable)
606828e4 2337 * LTTNG_KERNEL_ABI_DISABLE
e64957da 2338 * Disable recording for events in this channel (strong disable)
baf20995 2339 *
baf20995
MD
2340 * Channel and event file descriptors also hold a reference on the session.
2341 */
ad1c05e1 2342static
c0e31d2e 2343long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 2344{
f7d06400 2345 struct lttng_kernel_channel_buffer *channel = file->private_data;
8070f5c0 2346
baf20995 2347 switch (cmd) {
606828e4
MD
2348 case LTTNG_KERNEL_ABI_OLD_STREAM:
2349 case LTTNG_KERNEL_ABI_STREAM:
c0e31d2e 2350 return lttng_abi_open_stream(file);
606828e4 2351 case LTTNG_KERNEL_ABI_OLD_EVENT:
6dccd6c1 2352 {
606828e4
MD
2353 struct lttng_kernel_abi_event *uevent_param;
2354 struct lttng_kernel_abi_old_event *old_uevent_param;
6dccd6c1
JD
2355 int ret;
2356
606828e4 2357 uevent_param = kmalloc(sizeof(struct lttng_kernel_abi_event),
6dccd6c1
JD
2358 GFP_KERNEL);
2359 if (!uevent_param) {
2360 ret = -ENOMEM;
2361 goto old_event_end;
2362 }
2363 old_uevent_param = kmalloc(
606828e4 2364 sizeof(struct lttng_kernel_abi_old_event),
6dccd6c1
JD
2365 GFP_KERNEL);
2366 if (!old_uevent_param) {
2367 ret = -ENOMEM;
2368 goto old_event_error_free_param;
2369 }
2370 if (copy_from_user(old_uevent_param,
606828e4
MD
2371 (struct lttng_kernel_abi_old_event __user *) arg,
2372 sizeof(struct lttng_kernel_abi_old_event))) {
6dccd6c1
JD
2373 ret = -EFAULT;
2374 goto old_event_error_free_old_param;
2375 }
2376
2377 memcpy(uevent_param->name, old_uevent_param->name,
2378 sizeof(uevent_param->name));
2379 uevent_param->instrumentation =
2380 old_uevent_param->instrumentation;
2381
2382 switch (old_uevent_param->instrumentation) {
606828e4 2383 case LTTNG_KERNEL_ABI_KPROBE:
6dccd6c1
JD
2384 uevent_param->u.kprobe.addr =
2385 old_uevent_param->u.kprobe.addr;
2386 uevent_param->u.kprobe.offset =
2387 old_uevent_param->u.kprobe.offset;
2388 memcpy(uevent_param->u.kprobe.symbol_name,
2389 old_uevent_param->u.kprobe.symbol_name,
2390 sizeof(uevent_param->u.kprobe.symbol_name));
2391 break;
606828e4 2392 case LTTNG_KERNEL_ABI_KRETPROBE:
6dccd6c1
JD
2393 uevent_param->u.kretprobe.addr =
2394 old_uevent_param->u.kretprobe.addr;
2395 uevent_param->u.kretprobe.offset =
2396 old_uevent_param->u.kretprobe.offset;
2397 memcpy(uevent_param->u.kretprobe.symbol_name,
2398 old_uevent_param->u.kretprobe.symbol_name,
2399 sizeof(uevent_param->u.kretprobe.symbol_name));
2400 break;
606828e4 2401 case LTTNG_KERNEL_ABI_FUNCTION:
e884017c
MD
2402 WARN_ON_ONCE(1);
2403 /* Not implemented. */
6dccd6c1
JD
2404 break;
2405 default:
2406 break;
2407 }
2408 ret = lttng_abi_create_event(file, uevent_param);
2409
2410old_event_error_free_old_param:
2411 kfree(old_uevent_param);
2412old_event_error_free_param:
2413 kfree(uevent_param);
2414old_event_end:
2415 return ret;
2416 }
606828e4 2417 case LTTNG_KERNEL_ABI_EVENT:
6dccd6c1 2418 {
606828e4 2419 struct lttng_kernel_abi_event uevent_param;
6dccd6c1
JD
2420
2421 if (copy_from_user(&uevent_param,
606828e4 2422 (struct lttng_kernel_abi_event __user *) arg,
6dccd6c1
JD
2423 sizeof(uevent_param)))
2424 return -EFAULT;
2425 return lttng_abi_create_event(file, &uevent_param);
2426 }
606828e4 2427 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
6dccd6c1 2428 {
606828e4
MD
2429 struct lttng_kernel_abi_context *ucontext_param;
2430 struct lttng_kernel_abi_old_context *old_ucontext_param;
6dccd6c1
JD
2431 int ret;
2432
606828e4 2433 ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_context),
6dccd6c1
JD
2434 GFP_KERNEL);
2435 if (!ucontext_param) {
2436 ret = -ENOMEM;
2437 goto old_ctx_end;
2438 }
606828e4 2439 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_old_context),
6dccd6c1
JD
2440 GFP_KERNEL);
2441 if (!old_ucontext_param) {
2442 ret = -ENOMEM;
2443 goto old_ctx_error_free_param;
2444 }
2445
2446 if (copy_from_user(old_ucontext_param,
606828e4
MD
2447 (struct lttng_kernel_abi_old_context __user *) arg,
2448 sizeof(struct lttng_kernel_abi_old_context))) {
6dccd6c1
JD
2449 ret = -EFAULT;
2450 goto old_ctx_error_free_old_param;
2451 }
2452 ucontext_param->ctx = old_ucontext_param->ctx;
2453 memcpy(ucontext_param->padding, old_ucontext_param->padding,
2454 sizeof(ucontext_param->padding));
2455 /* only type that uses the union */
606828e4 2456 if (old_ucontext_param->ctx == LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER) {
6dccd6c1
JD
2457 ucontext_param->u.perf_counter.type =
2458 old_ucontext_param->u.perf_counter.type;
2459 ucontext_param->u.perf_counter.config =
2460 old_ucontext_param->u.perf_counter.config;
2461 memcpy(ucontext_param->u.perf_counter.name,
2462 old_ucontext_param->u.perf_counter.name,
2463 sizeof(ucontext_param->u.perf_counter.name));
2464 }
2465
2466 ret = lttng_abi_add_context(file,
2467 ucontext_param,
f7d06400 2468 &channel->priv->ctx, channel->parent.session);
6dccd6c1
JD
2469
2470old_ctx_error_free_old_param:
2471 kfree(old_ucontext_param);
2472old_ctx_error_free_param:
2473 kfree(ucontext_param);
2474old_ctx_end:
2475 return ret;
2476 }
606828e4 2477 case LTTNG_KERNEL_ABI_CONTEXT:
6dccd6c1 2478 {
606828e4 2479 struct lttng_kernel_abi_context ucontext_param;
6dccd6c1
JD
2480
2481 if (copy_from_user(&ucontext_param,
606828e4 2482 (struct lttng_kernel_abi_context __user *) arg,
6dccd6c1
JD
2483 sizeof(ucontext_param)))
2484 return -EFAULT;
2485 return lttng_abi_add_context(file,
2486 &ucontext_param,
f7d06400 2487 &channel->priv->ctx, channel->parent.session);
6dccd6c1 2488 }
606828e4
MD
2489 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2490 case LTTNG_KERNEL_ABI_ENABLE:
a90917c3 2491 return lttng_channel_enable(channel);
606828e4
MD
2492 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2493 case LTTNG_KERNEL_ABI_DISABLE:
a90917c3 2494 return lttng_channel_disable(channel);
606828e4 2495 case LTTNG_KERNEL_ABI_SYSCALL_MASK:
12e579db 2496 return lttng_channel_syscall_mask(channel,
606828e4 2497 (struct lttng_kernel_abi_syscall_mask __user *) arg);
baf20995
MD
2498 default:
2499 return -ENOIOCTLCMD;
2500 }
2501}
2502
5dbbdb43
MD
2503/**
2504 * lttng_metadata_ioctl - lttng syscall through ioctl
2505 *
2506 * @file: the file
2507 * @cmd: the command
2508 * @arg: command arg
2509 *
2510 * This ioctl implements lttng commands:
606828e4 2511 * LTTNG_KERNEL_ABI_STREAM
5dbbdb43
MD
2512 * Returns an event stream file descriptor or failure.
2513 *
2514 * Channel and event file descriptors also hold a reference on the session.
2515 */
2516static
2517long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2518{
2519 switch (cmd) {
606828e4
MD
2520 case LTTNG_KERNEL_ABI_OLD_STREAM:
2521 case LTTNG_KERNEL_ABI_STREAM:
d83004aa 2522 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
2523 default:
2524 return -ENOIOCTLCMD;
2525 }
2526}
2527
653fe716
MD
2528/**
2529 * lttng_channel_poll - lttng stream addition/removal monitoring
2530 *
c0e31d2e 2531 * @file: the file
653fe716
MD
2532 * @wait: poll table
2533 */
c0e31d2e 2534unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 2535{
f7d06400 2536 struct lttng_kernel_channel_buffer *channel = file->private_data;
653fe716
MD
2537 unsigned int mask = 0;
2538
c0e31d2e 2539 if (file->f_mode & FMODE_READ) {
a33e44a6 2540 poll_wait_set_exclusive(wait);
f7d06400 2541 poll_wait(file, channel->ops->priv->get_hp_wait_queue(channel->priv->rb_chan),
24cedcfe 2542 wait);
653fe716 2543
f7d06400 2544 if (channel->ops->priv->is_disabled(channel->priv->rb_chan))
254ec7bc 2545 return POLLERR;
f7d06400 2546 if (channel->ops->priv->is_finalized(channel->priv->rb_chan))
653fe716 2547 return POLLHUP;
f7d06400 2548 if (channel->ops->priv->buffer_has_read_closed_stream(channel->priv->rb_chan))
653fe716 2549 return POLLIN | POLLRDNORM;
f71ecafa 2550 return 0;
653fe716
MD
2551 }
2552 return mask;
2553
2554}
2555
0a84a57f
MD
2556static
2557int lttng_channel_release(struct inode *inode, struct file *file)
2558{
f7d06400 2559 struct lttng_kernel_channel_buffer *channel = file->private_data;
c269fff4
MD
2560
2561 if (channel)
f7d06400 2562 fput(channel->parent.session->priv->file);
0a84a57f
MD
2563 return 0;
2564}
2565
d83004aa
JD
2566static
2567int lttng_metadata_channel_release(struct inode *inode, struct file *file)
2568{
f7d06400 2569 struct lttng_kernel_channel_buffer *channel = file->private_data;
d83004aa
JD
2570
2571 if (channel) {
f7d06400 2572 fput(channel->parent.session->priv->file);
a3381417 2573 lttng_metadata_channel_destroy(channel);
d83004aa
JD
2574 }
2575
2576 return 0;
2577}
2578
ad1c05e1 2579static const struct file_operations lttng_channel_fops = {
a33c9927 2580 .owner = THIS_MODULE,
03037b98 2581 .release = lttng_channel_release,
653fe716 2582 .poll = lttng_channel_poll,
ad1c05e1 2583 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 2584#ifdef CONFIG_COMPAT
03037b98 2585 .compat_ioctl = lttng_channel_ioctl,
baf20995 2586#endif
11b5a3c2 2587};
baf20995 2588
5dbbdb43 2589static const struct file_operations lttng_metadata_fops = {
a33c9927 2590 .owner = THIS_MODULE,
d83004aa 2591 .release = lttng_metadata_channel_release,
5dbbdb43
MD
2592 .unlocked_ioctl = lttng_metadata_ioctl,
2593#ifdef CONFIG_COMPAT
2594 .compat_ioctl = lttng_metadata_ioctl,
2595#endif
2596};
2597
8070f5c0 2598/**
ef784b4d 2599 * lttng_event_recorder_event_ioctl - lttng syscall through ioctl
8070f5c0
MD
2600 *
2601 * @file: the file
2602 * @cmd: the command
2603 * @arg: command arg
2604 *
2605 * This ioctl implements lttng commands:
606828e4 2606 * LTTNG_KERNEL_ABI_CONTEXT
8070f5c0 2607 * Prepend a context field to each record of this event
606828e4 2608 * LTTNG_KERNEL_ABI_ENABLE
e64957da 2609 * Enable recording for this event (weak enable)
606828e4 2610 * LTTNG_KERNEL_ABI_DISABLE
e64957da 2611 * Disable recording for this event (strong disable)
8070f5c0
MD
2612 */
2613static
ef784b4d 2614long lttng_event_recorder_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8070f5c0 2615{
e2d5dbc7 2616 struct lttng_kernel_event_recorder *event_recorder = file->private_data;
8070f5c0
MD
2617
2618 switch (cmd) {
606828e4 2619 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
6dccd6c1 2620 {
3c997079
MD
2621 /* Not implemented */
2622 return -ENOSYS;
6dccd6c1 2623 }
606828e4 2624 case LTTNG_KERNEL_ABI_CONTEXT:
6dccd6c1 2625 {
3c997079
MD
2626 /* Not implemented */
2627 return -ENOSYS;
6dccd6c1 2628 }
606828e4
MD
2629 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2630 case LTTNG_KERNEL_ABI_ENABLE:
9d993668 2631 return lttng_event_enable(&event_recorder->parent);
606828e4
MD
2632 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2633 case LTTNG_KERNEL_ABI_DISABLE:
9d993668 2634 return lttng_event_disable(&event_recorder->parent);
606828e4 2635 case LTTNG_KERNEL_ABI_FILTER:
ef784b4d 2636 return -EINVAL;
606828e4 2637 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
e2d5dbc7 2638 return lttng_event_add_callsite(&event_recorder->parent,
ef784b4d 2639 (struct lttng_kernel_abi_event_callsite __user *) arg);
8070f5c0
MD
2640 default:
2641 return -ENOIOCTLCMD;
2642 }
2643}
2644
ef784b4d
MD
2645/**
2646 * lttng_event_recorder_enabler_ioctl - lttng syscall through ioctl
2647 *
2648 * @file: the file
2649 * @cmd: the command
2650 * @arg: command arg
2651 *
2652 * This ioctl implements lttng commands:
2653 * LTTNG_KERNEL_ABI_CONTEXT
2654 * Prepend a context field to each record of this event
2655 * LTTNG_KERNEL_ABI_ENABLE
2656 * Enable recording for this event (weak enable)
2657 * LTTNG_KERNEL_ABI_DISABLE
2658 * Disable recording for this event (strong disable)
2659 */
0a84a57f 2660static
ef784b4d 2661long lttng_event_recorder_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0a84a57f 2662{
ef784b4d 2663 struct lttng_event_enabler *event_enabler = file->private_data;
3c997079 2664
ef784b4d
MD
2665 switch (cmd) {
2666 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
2667 {
2668 /* Not implemented */
2669 return -ENOSYS;
2670 }
2671 case LTTNG_KERNEL_ABI_CONTEXT:
2672 {
2673 /* Not implemented */
2674 return -ENOSYS;
2675 }
2676 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2677 case LTTNG_KERNEL_ABI_ENABLE:
2678 return lttng_event_enabler_enable(event_enabler);
2679 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2680 case LTTNG_KERNEL_ABI_DISABLE:
2681 return lttng_event_enabler_disable(event_enabler);
2682 case LTTNG_KERNEL_ABI_FILTER:
2683 return lttng_event_enabler_attach_filter_bytecode(
2684 event_enabler,
2685 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
2686 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
2687 return -EINVAL;
3c997079 2688 default:
ef784b4d 2689 return -ENOIOCTLCMD;
3c997079 2690 }
ef784b4d
MD
2691}
2692
2693static
2694int lttng_event_recorder_event_release(struct inode *inode, struct file *file)
2695{
a67ba386 2696 struct lttng_kernel_event_recorder *event = file->private_data;
ef784b4d
MD
2697
2698 if (event)
f7d06400 2699 fput(event->chan->priv->parent.file);
ef784b4d
MD
2700 return 0;
2701}
c269fff4 2702
ef784b4d
MD
2703static
2704int lttng_event_recorder_enabler_release(struct inode *inode, struct file *file)
2705{
2706 struct lttng_event_enabler *event_enabler = file->private_data;
2707
2708 if (event_enabler)
f7d06400 2709 fput(event_enabler->chan->priv->parent.file);
0a84a57f
MD
2710 return 0;
2711}
2712
ef784b4d
MD
2713static const struct file_operations lttng_event_recorder_event_fops = {
2714 .owner = THIS_MODULE,
2715 .release = lttng_event_recorder_event_release,
2716 .unlocked_ioctl = lttng_event_recorder_event_ioctl,
2717#ifdef CONFIG_COMPAT
2718 .compat_ioctl = lttng_event_recorder_event_ioctl,
2719#endif
2720};
2721
2722static const struct file_operations lttng_event_recorder_enabler_fops = {
a33c9927 2723 .owner = THIS_MODULE,
ef784b4d
MD
2724 .release = lttng_event_recorder_enabler_release,
2725 .unlocked_ioctl = lttng_event_recorder_enabler_ioctl,
8070f5c0 2726#ifdef CONFIG_COMPAT
ef784b4d 2727 .compat_ioctl = lttng_event_recorder_enabler_ioctl,
8070f5c0 2728#endif
11b5a3c2 2729};
0a84a57f 2730
3b731ab1
JD
2731static int put_u64(uint64_t val, unsigned long arg)
2732{
2733 return put_user(val, (uint64_t __user *) arg);
2734}
2735
8b97fd42
MD
2736static int put_u32(uint32_t val, unsigned long arg)
2737{
2738 return put_user(val, (uint32_t __user *) arg);
2739}
2740
ed8d02d6
JD
2741static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2742 unsigned int cmd, unsigned long arg)
2743{
3b731ab1
JD
2744 struct lib_ring_buffer *buf = filp->private_data;
2745 struct channel *chan = buf->backend.chan;
2746 const struct lib_ring_buffer_config *config = &chan->backend.config;
4a399b76 2747 const struct lttng_kernel_channel_buffer_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2748 int ret;
2749
2750 if (atomic_read(&chan->record_disabled))
2751 return -EIO;
2752
ed8d02d6 2753 switch (cmd) {
606828e4 2754 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN:
3b731ab1
JD
2755 {
2756 uint64_t ts;
2757
4a399b76 2758 ret = ops->priv->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2759 if (ret < 0)
2760 goto error;
2761 return put_u64(ts, arg);
2762 }
606828e4 2763 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END:
3b731ab1
JD
2764 {
2765 uint64_t ts;
2766
4a399b76 2767 ret = ops->priv->timestamp_end(config, buf, &ts);
3b731ab1
JD
2768 if (ret < 0)
2769 goto error;
2770 return put_u64(ts, arg);
2771 }
606828e4 2772 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED:
3b731ab1
JD
2773 {
2774 uint64_t ed;
2775
4a399b76 2776 ret = ops->priv->events_discarded(config, buf, &ed);
3b731ab1
JD
2777 if (ret < 0)
2778 goto error;
2779 return put_u64(ed, arg);
2780 }
606828e4 2781 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE:
3b731ab1
JD
2782 {
2783 uint64_t cs;
2784
4a399b76 2785 ret = ops->priv->content_size(config, buf, &cs);
3b731ab1
JD
2786 if (ret < 0)
2787 goto error;
2788 return put_u64(cs, arg);
2789 }
606828e4 2790 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE:
3b731ab1
JD
2791 {
2792 uint64_t ps;
2793
4a399b76 2794 ret = ops->priv->packet_size(config, buf, &ps);
3b731ab1
JD
2795 if (ret < 0)
2796 goto error;
2797 return put_u64(ps, arg);
2798 }
606828e4 2799 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID:
3b731ab1
JD
2800 {
2801 uint64_t si;
2802
4a399b76 2803 ret = ops->priv->stream_id(config, buf, &si);
3b731ab1
JD
2804 if (ret < 0)
2805 goto error;
2806 return put_u64(si, arg);
2807 }
606828e4 2808 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2348ca17
JD
2809 {
2810 uint64_t ts;
2811
4a399b76 2812 ret = ops->priv->current_timestamp(config, buf, &ts);
2348ca17
JD
2813 if (ret < 0)
2814 goto error;
2815 return put_u64(ts, arg);
2816 }
606828e4 2817 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM:
5b3cf4f9
JD
2818 {
2819 uint64_t seq;
2820
4a399b76 2821 ret = ops->priv->sequence_number(config, buf, &seq);
5b3cf4f9
JD
2822 if (ret < 0)
2823 goto error;
2824 return put_u64(seq, arg);
2825 }
606828e4 2826 case LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID:
5594698f
JD
2827 {
2828 uint64_t id;
2829
4a399b76 2830 ret = ops->priv->instance_id(config, buf, &id);
5594698f
JD
2831 if (ret < 0)
2832 goto error;
2833 return put_u64(id, arg);
2834 }
3b731ab1
JD
2835 default:
2836 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2837 cmd, arg);
ed8d02d6 2838 }
3b731ab1
JD
2839
2840error:
2841 return -ENOSYS;
ed8d02d6
JD
2842}
2843
2844#ifdef CONFIG_COMPAT
2845static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2846 unsigned int cmd, unsigned long arg)
2847{
3b731ab1
JD
2848 struct lib_ring_buffer *buf = filp->private_data;
2849 struct channel *chan = buf->backend.chan;
2850 const struct lib_ring_buffer_config *config = &chan->backend.config;
4a399b76 2851 const struct lttng_kernel_channel_buffer_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2852 int ret;
2853
2854 if (atomic_read(&chan->record_disabled))
2855 return -EIO;
2856
ed8d02d6 2857 switch (cmd) {
606828e4 2858 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
3b731ab1
JD
2859 {
2860 uint64_t ts;
2861
4a399b76 2862 ret = ops->priv->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2863 if (ret < 0)
2864 goto error;
2865 return put_u64(ts, arg);
2866 }
606828e4 2867 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
3b731ab1
JD
2868 {
2869 uint64_t ts;
2870
4a399b76 2871 ret = ops->priv->timestamp_end(config, buf, &ts);
3b731ab1
JD
2872 if (ret < 0)
2873 goto error;
2874 return put_u64(ts, arg);
2875 }
606828e4 2876 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
3b731ab1
JD
2877 {
2878 uint64_t ed;
2879
4a399b76 2880 ret = ops->priv->events_discarded(config, buf, &ed);
3b731ab1
JD
2881 if (ret < 0)
2882 goto error;
2883 return put_u64(ed, arg);
ed8d02d6 2884 }
606828e4 2885 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
3b731ab1
JD
2886 {
2887 uint64_t cs;
2888
4a399b76 2889 ret = ops->priv->content_size(config, buf, &cs);
3b731ab1
JD
2890 if (ret < 0)
2891 goto error;
2892 return put_u64(cs, arg);
2893 }
606828e4 2894 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
3b731ab1
JD
2895 {
2896 uint64_t ps;
2897
4a399b76 2898 ret = ops->priv->packet_size(config, buf, &ps);
3b731ab1
JD
2899 if (ret < 0)
2900 goto error;
2901 return put_u64(ps, arg);
2902 }
606828e4 2903 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_STREAM_ID:
3b731ab1
JD
2904 {
2905 uint64_t si;
2906
4a399b76 2907 ret = ops->priv->stream_id(config, buf, &si);
3b731ab1
JD
2908 if (ret < 0)
2909 goto error;
2910 return put_u64(si, arg);
2911 }
606828e4 2912 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2348ca17
JD
2913 {
2914 uint64_t ts;
2915
4a399b76 2916 ret = ops->priv->current_timestamp(config, buf, &ts);
2348ca17
JD
2917 if (ret < 0)
2918 goto error;
2919 return put_u64(ts, arg);
2920 }
606828e4 2921 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_SEQ_NUM:
5b3cf4f9
JD
2922 {
2923 uint64_t seq;
2924
4a399b76 2925 ret = ops->priv->sequence_number(config, buf, &seq);
5b3cf4f9
JD
2926 if (ret < 0)
2927 goto error;
2928 return put_u64(seq, arg);
2929 }
606828e4 2930 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_INSTANCE_ID:
5594698f
JD
2931 {
2932 uint64_t id;
2933
4a399b76 2934 ret = ops->priv->instance_id(config, buf, &id);
5594698f
JD
2935 if (ret < 0)
2936 goto error;
2937 return put_u64(id, arg);
2938 }
3b731ab1
JD
2939 default:
2940 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2941 cmd, arg);
2942 }
2943
2944error:
2945 return -ENOSYS;
ed8d02d6
JD
2946}
2947#endif /* CONFIG_COMPAT */
2948
2949static void lttng_stream_override_ring_buffer_fops(void)
2950{
2951 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2952 lttng_stream_ring_buffer_file_operations.open =
2953 lib_ring_buffer_file_operations.open;
2954 lttng_stream_ring_buffer_file_operations.release =
2955 lib_ring_buffer_file_operations.release;
2956 lttng_stream_ring_buffer_file_operations.poll =
2957 lib_ring_buffer_file_operations.poll;
2958 lttng_stream_ring_buffer_file_operations.splice_read =
2959 lib_ring_buffer_file_operations.splice_read;
2960 lttng_stream_ring_buffer_file_operations.mmap =
2961 lib_ring_buffer_file_operations.mmap;
2962 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2963 lttng_stream_ring_buffer_ioctl;
2964 lttng_stream_ring_buffer_file_operations.llseek =
2965 lib_ring_buffer_file_operations.llseek;
2966#ifdef CONFIG_COMPAT
2967 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2968 lttng_stream_ring_buffer_compat_ioctl;
2969#endif
2970}
2971
80996790 2972int __init lttng_abi_init(void)
baf20995
MD
2973{
2974 int ret = 0;
2975
263b6c88 2976 wrapper_vmalloc_sync_mappings();
2754583e 2977 lttng_clock_ref();
f771eda6
JD
2978
2979 ret = lttng_tp_mempool_init();
2980 if (ret) {
2981 goto error;
2982 }
2983
d29348f7 2984 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 2985 &lttng_proc_ops, NULL);
2470a237 2986
255e52a4 2987 if (!lttng_proc_dentry) {
5a15f70c 2988 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
2989 ret = -ENOMEM;
2990 goto error;
2991 }
ed8d02d6 2992 lttng_stream_override_ring_buffer_fops();
2754583e 2993 return 0;
ed8d02d6 2994
baf20995 2995error:
f771eda6 2996 lttng_tp_mempool_destroy();
2754583e 2997 lttng_clock_unref();
baf20995
MD
2998 return ret;
2999}
3000
e6e65fcd
MD
3001/* No __exit annotation because used by init error path too. */
3002void lttng_abi_exit(void)
baf20995 3003{
f771eda6 3004 lttng_tp_mempool_destroy();
2754583e 3005 lttng_clock_unref();
e6a17f26
MD
3006 if (lttng_proc_dentry)
3007 remove_proc_entry("lttng", NULL);
baf20995 3008}
This page took 0.222594 seconds and 4 git commands to generate.