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