Implement lttng_event_notifier_group_notif_fops read, poll, open, release ABI
[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 }
912 return read_count;
913
914nodata:
915 *ppos = 0;
916 chan->iter.len_left = 0;
917 return read_count;
918}
919
920/*
921 * If the ring buffer is non empty (even just a partial subbuffer), return that
922 * there is data available. Perform a ring buffer flush if we encounter a
923 * non-empty ring buffer which does not have any consumeable subbuffer available.
924 */
925static
926unsigned int lttng_event_notifier_group_notif_poll(struct file *filp,
927 poll_table *wait)
928{
929 unsigned int mask = 0;
930 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
931 struct channel *chan = event_notifier_group->chan;
932 struct lib_ring_buffer *buf = event_notifier_group->buf;
933 const struct lib_ring_buffer_config *config = &chan->backend.config;
934 int finalized, disabled;
935 unsigned long consumed, offset;
936
937 if (filp->f_mode & FMODE_READ) {
938 poll_wait_set_exclusive(wait);
939 poll_wait(filp, &event_notifier_group->read_wait, wait);
940
941 finalized = lib_ring_buffer_is_finalized(config, buf);
942 disabled = lib_ring_buffer_channel_is_disabled(chan);
943
944 /*
945 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
946 * finalized load before offsets loads.
947 */
948 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
949retry:
950 if (disabled)
951 return POLLERR;
952
953 offset = lib_ring_buffer_get_offset(config, buf);
954 consumed = lib_ring_buffer_get_consumed(config, buf);
955
956 /*
957 * If there is no buffer available to consume.
958 */
959 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) {
960 /*
961 * If there is a non-empty subbuffer, flush and try again.
962 */
963 if (subbuf_offset(offset, chan) != 0) {
964 lib_ring_buffer_switch_remote(buf);
965 goto retry;
966 }
967
968 if (finalized)
969 return POLLHUP;
970 else {
971 /*
972 * The memory barriers
973 * __wait_event()/wake_up_interruptible() take
974 * care of "raw_spin_is_locked" memory ordering.
975 */
976 if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
977 goto retry;
978 else
979 return 0;
980 }
981 } else {
982 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan)
983 >= chan->backend.buf_size)
984 return POLLPRI | POLLRDBAND;
985 else
986 return POLLIN | POLLRDNORM;
987 }
988 }
989
990 return mask;
991}
992
993/**
994 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
995 * @inode: opened inode
996 * @file: opened file
997 *
998 * Open implementation. Makes sure only one open instance of a buffer is
999 * done at a given moment.
1000 */
1001static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file)
1002{
1003 struct lttng_event_notifier_group *event_notifier_group = inode->i_private;
1004 struct lib_ring_buffer *buf = event_notifier_group->buf;
1005
1006 file->private_data = event_notifier_group;
1007 return lib_ring_buffer_open(inode, file, buf);
1008}
1009
1010/**
1011 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1012 * @inode: opened inode
1013 * @file: opened file
1014 *
1015 * Release implementation.
1016 */
1017static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file)
1018{
1019 struct lttng_event_notifier_group *event_notifier_group = file->private_data;
1020 struct lib_ring_buffer *buf = event_notifier_group->buf;
1021 int ret;
1022
1023 ret = lib_ring_buffer_release(inode, file, buf);
1024 if (ret)
1025 return ret;
1026 fput(event_notifier_group->file);
1027 return 0;
1028}
1029
21f58fb7
FD
1030static const struct file_operations lttng_event_notifier_group_notif_fops = {
1031 .owner = THIS_MODULE,
db2511b4
MD
1032 .open = lttng_event_notifier_group_notif_open,
1033 .release = lttng_event_notifier_group_notif_release,
1034 .read = lttng_event_notifier_group_notif_read,
1035 .poll = lttng_event_notifier_group_notif_poll,
21f58fb7
FD
1036};
1037
d83004aa
JD
1038/**
1039 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1040 * @filp: the file
1041 * @wait: poll table
1042 *
1043 * Handles the poll operations for the metadata channels.
1044 */
ad1c05e1 1045static
d83004aa
JD
1046unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
1047 poll_table *wait)
1048{
1049 struct lttng_metadata_stream *stream = filp->private_data;
1050 struct lib_ring_buffer *buf = stream->priv;
1051 int finalized;
1052 unsigned int mask = 0;
1053
1054 if (filp->f_mode & FMODE_READ) {
1055 poll_wait_set_exclusive(wait);
1056 poll_wait(filp, &stream->read_wait, wait);
1057
1058 finalized = stream->finalized;
1059
1060 /*
1061 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1062 * ordering finalized load before offsets loads.
1063 */
1064 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1065
1066 if (finalized)
1067 mask |= POLLHUP;
1068
92d9f5e6 1069 mutex_lock(&stream->metadata_cache->lock);
d83004aa 1070 if (stream->metadata_cache->metadata_written >
f613e3e6 1071 stream->metadata_out)
d83004aa 1072 mask |= POLLIN;
92d9f5e6 1073 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
1074 }
1075
1076 return mask;
1077}
1078
f613e3e6
MD
1079static
1080void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
1081 unsigned int cmd, unsigned long arg)
1082{
1083 struct lttng_metadata_stream *stream = filp->private_data;
1084
1085 stream->metadata_out = stream->metadata_in;
1086}
1087
d1344afa
JD
1088/*
1089 * Reset the counter of how much metadata has been consumed to 0. That way,
1090 * the consumer receives the content of the metadata cache unchanged. This is
1091 * different from the metadata_regenerate where the offset from epoch is
1092 * resampled, here we want the exact same content as the last time the metadata
1093 * was generated. This command is only possible if all the metadata written
1094 * in the cache has been output to the metadata stream to avoid corrupting the
1095 * metadata file.
1096 *
1097 * Return 0 on success, a negative value on error.
1098 */
1099static
1100int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
1101{
1102 int ret;
1103 struct lttng_metadata_cache *cache = stream->metadata_cache;
1104
1105 mutex_lock(&cache->lock);
1106 if (stream->metadata_out != cache->metadata_written) {
1107 ret = -EBUSY;
1108 goto end;
1109 }
1110 stream->metadata_out = 0;
1111 stream->metadata_in = 0;
1112 wake_up_interruptible(&stream->read_wait);
1113 ret = 0;
1114
1115end:
1116 mutex_unlock(&cache->lock);
1117 return ret;
1118}
1119
d83004aa
JD
1120static
1121long lttng_metadata_ring_buffer_ioctl(struct file *filp,
1122 unsigned int cmd, unsigned long arg)
1123{
1124 int ret;
1125 struct lttng_metadata_stream *stream = filp->private_data;
1126 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
1127 unsigned int rb_cmd;
1128 bool coherent;
1129
1130 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1131 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
1132 else
1133 rb_cmd = cmd;
d83004aa
JD
1134
1135 switch (cmd) {
d83004aa
JD
1136 case RING_BUFFER_GET_NEXT_SUBBUF:
1137 {
35097f36
JD
1138 struct lttng_metadata_stream *stream = filp->private_data;
1139 struct lib_ring_buffer *buf = stream->priv;
1140 struct channel *chan = buf->backend.chan;
1141
8b97fd42 1142 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1143 if (ret > 0) {
1144 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1145 ret = 0;
1146 } else if (ret < 0)
d83004aa
JD
1147 goto err;
1148 break;
1149 }
f613e3e6
MD
1150 case RING_BUFFER_GET_SUBBUF:
1151 {
1152 /*
1153 * Random access is not allowed for metadata channel.
1154 */
1155 return -ENOSYS;
1156 }
c6f05468 1157 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
35097f36
JD
1158 case RING_BUFFER_FLUSH:
1159 {
1160 struct lttng_metadata_stream *stream = filp->private_data;
1161 struct lib_ring_buffer *buf = stream->priv;
1162 struct channel *chan = buf->backend.chan;
1163
1164 /*
1165 * Before doing the actual ring buffer flush, write up to one
1166 * packet of metadata in the ring buffer.
1167 */
8b97fd42 1168 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1169 if (ret < 0)
1170 goto err;
1171 break;
1172 }
9616f0bf
JD
1173 case RING_BUFFER_GET_METADATA_VERSION:
1174 {
1175 struct lttng_metadata_stream *stream = filp->private_data;
1176
1177 return put_u64(stream->version, arg);
1178 }
d1344afa
JD
1179 case RING_BUFFER_METADATA_CACHE_DUMP:
1180 {
1181 struct lttng_metadata_stream *stream = filp->private_data;
1182
1183 return lttng_metadata_cache_dump(stream);
1184 }
8b97fd42
MD
1185 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1186 {
1187 struct lttng_metadata_stream *stream = filp->private_data;
1188 struct lib_ring_buffer *buf = stream->priv;
1189 struct channel *chan = buf->backend.chan;
1190
1191 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1192 if (ret > 0) {
1193 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1194 ret = 0;
1195 } else if (ret < 0) {
1196 goto err;
1197 }
1198 break;
1199 }
d83004aa
JD
1200 default:
1201 break;
1202 }
f613e3e6
MD
1203 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1204
d83004aa 1205 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1206 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1207 if (ret < 0)
1208 goto err;
d83004aa 1209
f613e3e6
MD
1210 switch (cmd) {
1211 case RING_BUFFER_PUT_NEXT_SUBBUF:
1212 {
1213 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1214 cmd, arg);
1215 break;
1216 }
8b97fd42
MD
1217 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1218 {
1219 return put_u32(coherent, arg);
1220 }
f613e3e6
MD
1221 default:
1222 break;
1223 }
d83004aa
JD
1224err:
1225 return ret;
1226}
1227
aeb9064d 1228#ifdef CONFIG_COMPAT
d83004aa
JD
1229static
1230long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
1231 unsigned int cmd, unsigned long arg)
1232{
1233 int ret;
1234 struct lttng_metadata_stream *stream = filp->private_data;
1235 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
1236 unsigned int rb_cmd;
1237 bool coherent;
1238
1239 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1240 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
1241 else
1242 rb_cmd = cmd;
d83004aa
JD
1243
1244 switch (cmd) {
d83004aa
JD
1245 case RING_BUFFER_GET_NEXT_SUBBUF:
1246 {
35097f36
JD
1247 struct lttng_metadata_stream *stream = filp->private_data;
1248 struct lib_ring_buffer *buf = stream->priv;
1249 struct channel *chan = buf->backend.chan;
1250
8b97fd42 1251 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1252 if (ret > 0) {
1253 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1254 ret = 0;
1255 } else if (ret < 0)
d83004aa
JD
1256 goto err;
1257 break;
1258 }
f613e3e6
MD
1259 case RING_BUFFER_GET_SUBBUF:
1260 {
1261 /*
1262 * Random access is not allowed for metadata channel.
1263 */
1264 return -ENOSYS;
1265 }
c6f05468 1266 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
96c55c2f
MD
1267 case RING_BUFFER_FLUSH:
1268 {
1269 struct lttng_metadata_stream *stream = filp->private_data;
1270 struct lib_ring_buffer *buf = stream->priv;
1271 struct channel *chan = buf->backend.chan;
1272
1273 /*
1274 * Before doing the actual ring buffer flush, write up to one
1275 * packet of metadata in the ring buffer.
1276 */
8b97fd42 1277 ret = lttng_metadata_output_channel(stream, chan, NULL);
96c55c2f
MD
1278 if (ret < 0)
1279 goto err;
1280 break;
1281 }
1282 case RING_BUFFER_GET_METADATA_VERSION:
1283 {
1284 struct lttng_metadata_stream *stream = filp->private_data;
1285
1286 return put_u64(stream->version, arg);
1287 }
d1344afa
JD
1288 case RING_BUFFER_METADATA_CACHE_DUMP:
1289 {
1290 struct lttng_metadata_stream *stream = filp->private_data;
1291
1292 return lttng_metadata_cache_dump(stream);
1293 }
8b97fd42
MD
1294 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1295 {
1296 struct lttng_metadata_stream *stream = filp->private_data;
1297 struct lib_ring_buffer *buf = stream->priv;
1298 struct channel *chan = buf->backend.chan;
1299
1300 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1301 if (ret > 0) {
1302 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1303 ret = 0;
1304 } else if (ret < 0) {
1305 goto err;
1306 }
1307 break;
1308 }
d83004aa
JD
1309 default:
1310 break;
1311 }
f613e3e6
MD
1312 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1313
d83004aa 1314 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1315 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1316 if (ret < 0)
1317 goto err;
d83004aa 1318
f613e3e6
MD
1319 switch (cmd) {
1320 case RING_BUFFER_PUT_NEXT_SUBBUF:
1321 {
1322 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1323 cmd, arg);
1324 break;
1325 }
8b97fd42
MD
1326 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1327 {
1328 return put_u32(coherent, arg);
1329 }
f613e3e6
MD
1330 default:
1331 break;
1332 }
d83004aa
JD
1333err:
1334 return ret;
1335}
aeb9064d 1336#endif
d83004aa 1337
b3b8072b
MD
1338/*
1339 * This is not used by anonymous file descriptors. This code is left
1340 * there if we ever want to implement an inode with open() operation.
1341 */
d83004aa
JD
1342static
1343int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1344{
1345 struct lttng_metadata_stream *stream = inode->i_private;
1346 struct lib_ring_buffer *buf = stream->priv;
1347
1348 file->private_data = buf;
b3b8072b
MD
1349 /*
1350 * Since life-time of metadata cache differs from that of
1351 * session, we need to keep our own reference on the transport.
1352 */
1353 if (!try_module_get(stream->transport->owner)) {
5a15f70c 1354 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1355 return -EBUSY;
1356 }
d83004aa
JD
1357 return lib_ring_buffer_open(inode, file, buf);
1358}
1359
1360static
1361int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1362{
1363 struct lttng_metadata_stream *stream = file->private_data;
1364 struct lib_ring_buffer *buf = stream->priv;
1365
92143b2c
MD
1366 mutex_lock(&stream->metadata_cache->lock);
1367 list_del(&stream->list);
1368 mutex_unlock(&stream->metadata_cache->lock);
d83004aa 1369 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 1370 module_put(stream->transport->owner);
92143b2c 1371 kfree(stream);
d83004aa
JD
1372 return lib_ring_buffer_release(inode, file, buf);
1373}
1374
1375static
1376ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1377 struct pipe_inode_info *pipe, size_t len,
1378 unsigned int flags)
1379{
1380 struct lttng_metadata_stream *stream = in->private_data;
1381 struct lib_ring_buffer *buf = stream->priv;
1382
1383 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1384 flags, buf);
1385}
1386
1387static
1388int lttng_metadata_ring_buffer_mmap(struct file *filp,
1389 struct vm_area_struct *vma)
1390{
1391 struct lttng_metadata_stream *stream = filp->private_data;
1392 struct lib_ring_buffer *buf = stream->priv;
1393
1394 return lib_ring_buffer_mmap(filp, vma, buf);
1395}
1396
1397static
1398const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1399 .owner = THIS_MODULE,
1400 .open = lttng_metadata_ring_buffer_open,
1401 .release = lttng_metadata_ring_buffer_release,
1402 .poll = lttng_metadata_ring_buffer_poll,
1403 .splice_read = lttng_metadata_ring_buffer_splice_read,
1404 .mmap = lttng_metadata_ring_buffer_mmap,
1405 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1406 .llseek = vfs_lib_ring_buffer_no_llseek,
1407#ifdef CONFIG_COMPAT
1408 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1409#endif
1410};
1411
1412static
1413int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
a3fcd499 1414 const struct file_operations *fops, const char *name)
ad1c05e1 1415{
ad1c05e1 1416 int stream_fd, ret;
11b5a3c2 1417 struct file *stream_file;
ad1c05e1 1418
4ac10b76 1419 stream_fd = lttng_get_unused_fd();
ad1c05e1
MD
1420 if (stream_fd < 0) {
1421 ret = stream_fd;
1422 goto fd_error;
1423 }
a3fcd499 1424 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
c0e31d2e
MD
1425 if (IS_ERR(stream_file)) {
1426 ret = PTR_ERR(stream_file);
ad1c05e1
MD
1427 goto file_error;
1428 }
409453cb
MD
1429 /*
1430 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1431 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1432 * file descriptor, so we set FMODE_PREAD here.
1433 */
d7b6f197 1434 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 1435 fd_install(stream_fd, stream_file);
dda6a249
MD
1436 /*
1437 * The stream holds a reference to the channel within the generic ring
1438 * buffer library, so no need to hold a refcount on the channel and
1439 * session files here.
1440 */
ad1c05e1
MD
1441 return stream_fd;
1442
1443file_error:
1444 put_unused_fd(stream_fd);
d83004aa
JD
1445fd_error:
1446 return ret;
1447}
1448
1449static
1450int lttng_abi_open_stream(struct file *channel_file)
1451{
1452 struct lttng_channel *channel = channel_file->private_data;
1453 struct lib_ring_buffer *buf;
1454 int ret;
1455 void *stream_priv;
1456
1457 buf = channel->ops->buffer_read_open(channel->chan);
1458 if (!buf)
1459 return -ENOENT;
1460
1461 stream_priv = buf;
1462 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1463 &lttng_stream_ring_buffer_file_operations,
1464 "[lttng_stream]");
d83004aa
JD
1465 if (ret < 0)
1466 goto fd_error;
1467
1468 return ret;
1469
1470fd_error:
1471 channel->ops->buffer_read_close(buf);
1472 return ret;
1473}
1474
1475static
1476int lttng_abi_open_metadata_stream(struct file *channel_file)
1477{
1478 struct lttng_channel *channel = channel_file->private_data;
1479 struct lttng_session *session = channel->session;
1480 struct lib_ring_buffer *buf;
1481 int ret;
1482 struct lttng_metadata_stream *metadata_stream;
1483 void *stream_priv;
1484
1485 buf = channel->ops->buffer_read_open(channel->chan);
1486 if (!buf)
1487 return -ENOENT;
1488
1489 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1490 GFP_KERNEL);
b3b8072b
MD
1491 if (!metadata_stream) {
1492 ret = -ENOMEM;
1493 goto nomem;
1494 }
d83004aa
JD
1495 metadata_stream->metadata_cache = session->metadata_cache;
1496 init_waitqueue_head(&metadata_stream->read_wait);
1497 metadata_stream->priv = buf;
1498 stream_priv = metadata_stream;
b3b8072b 1499 metadata_stream->transport = channel->transport;
8b97fd42
MD
1500 /* Initial state is an empty metadata, considered as incoherent. */
1501 metadata_stream->coherent = false;
b3b8072b
MD
1502
1503 /*
1504 * Since life-time of metadata cache differs from that of
1505 * session, we need to keep our own reference on the transport.
1506 */
1507 if (!try_module_get(metadata_stream->transport->owner)) {
5a15f70c 1508 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1509 ret = -EINVAL;
1510 goto notransport;
1511 }
1512
901aaa5f
FD
1513 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
1514 ret = -EOVERFLOW;
9c1f4643 1515 goto kref_error;
901aaa5f
FD
1516 }
1517
d83004aa 1518 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1519 &lttng_metadata_ring_buffer_file_operations,
1520 "[lttng_metadata_stream]");
d83004aa
JD
1521 if (ret < 0)
1522 goto fd_error;
1523
92143b2c 1524 mutex_lock(&session->metadata_cache->lock);
d83004aa
JD
1525 list_add(&metadata_stream->list,
1526 &session->metadata_cache->metadata_stream);
92143b2c 1527 mutex_unlock(&session->metadata_cache->lock);
d83004aa
JD
1528 return ret;
1529
ad1c05e1 1530fd_error:
9c1f4643
MD
1531 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1532kref_error:
b3b8072b
MD
1533 module_put(metadata_stream->transport->owner);
1534notransport:
1535 kfree(metadata_stream);
1536nomem:
11b5a3c2 1537 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
1538 return ret;
1539}
1540
21f58fb7
FD
1541static
1542int lttng_abi_open_event_notifier_group_stream(struct file *notif_file)
1543{
1544 struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data;
1545 struct channel *chan = event_notifier_group->chan;
1546 struct lib_ring_buffer *buf;
1547 int ret;
1548 void *stream_priv;
1549
1550 buf = event_notifier_group->ops->buffer_read_open(chan);
1551 if (!buf)
1552 return -ENOENT;
1553
1554 /* The event_notifier notification fd holds a reference on the event_notifier group */
1555 if (!atomic_long_add_unless(&notif_file->f_count, 1, LONG_MAX)) {
1556 ret = -EOVERFLOW;
1557 goto refcount_error;
1558 }
1559 event_notifier_group->buf = buf;
1560 stream_priv = event_notifier_group;
1561 ret = lttng_abi_create_stream_fd(notif_file, stream_priv,
1562 &lttng_event_notifier_group_notif_fops,
1563 "[lttng_event_notifier_stream]");
1564 if (ret < 0)
1565 goto fd_error;
1566
1567 return ret;
1568
1569fd_error:
1570 atomic_long_dec(&notif_file->f_count);
1571refcount_error:
1572 event_notifier_group->ops->buffer_read_close(buf);
1573 return ret;
1574}
1575
badfe9f5
MD
1576static
1577int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param)
1578{
1579 /* Limit ABI to implemented features. */
1580 switch (event_param->instrumentation) {
1581 case LTTNG_KERNEL_SYSCALL:
1582 switch (event_param->u.syscall.entryexit) {
1583 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
1584 break;
1585 default:
1586 return -EINVAL;
1587 }
1588 switch (event_param->u.syscall.abi) {
1589 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
1590 break;
1591 default:
1592 return -EINVAL;
1593 }
1594 switch (event_param->u.syscall.match) {
1595 case LTTNG_SYSCALL_MATCH_NAME:
1596 break;
1597 default:
1598 return -EINVAL;
1599 }
1600 break;
1601
1602 case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */
1603 case LTTNG_KERNEL_KPROBE: /* Fallthrough */
1604 case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */
1605 case LTTNG_KERNEL_NOOP: /* Fallthrough */
1606 case LTTNG_KERNEL_UPROBE:
1607 break;
1608
1609 case LTTNG_KERNEL_FUNCTION: /* Fallthrough */
1610 default:
1611 return -EINVAL;
1612 }
1613 return 0;
1614}
1615
653fe716 1616static
c0e31d2e 1617int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 1618 struct lttng_kernel_event *event_param)
653fe716 1619{
a90917c3 1620 struct lttng_channel *channel = channel_file->private_data;
653fe716 1621 int event_fd, ret;
11b5a3c2 1622 struct file *event_file;
3c997079 1623 void *priv;
653fe716 1624
6dccd6c1
JD
1625 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1626 switch (event_param->instrumentation) {
7371f44c 1627 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 1628 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 1629 break;
ab2277d6 1630 case LTTNG_KERNEL_KPROBE:
6dccd6c1 1631 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 1632 break;
ab2277d6 1633 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
1634 WARN_ON_ONCE(1);
1635 /* Not implemented. */
e0a7a7c4
MD
1636 break;
1637 default:
1638 break;
1639 }
33a39a3c
MD
1640 event_fd = lttng_get_unused_fd();
1641 if (event_fd < 0) {
1642 ret = event_fd;
1643 goto fd_error;
1644 }
1645 event_file = anon_inode_getfile("[lttng_event]",
1646 &lttng_event_fops,
1647 NULL, O_RDWR);
1648 if (IS_ERR(event_file)) {
1649 ret = PTR_ERR(event_file);
1650 goto file_error;
1651 }
9c1f4643 1652 /* The event holds a reference on the channel */
98d7281c 1653 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
0d2c717f 1654 ret = -EOVERFLOW;
9c1f4643
MD
1655 goto refcount_error;
1656 }
badfe9f5
MD
1657 ret = lttng_abi_validate_event_param(event_param);
1658 if (ret)
1659 goto event_error;
33a39a3c
MD
1660 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1661 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
b2bc0bc8 1662 struct lttng_event_enabler *event_enabler;
33a39a3c 1663
4993071a
PP
1664 if (strutils_is_star_glob_pattern(event_param->name)) {
1665 /*
1666 * If the event name is a star globbing pattern,
1667 * we create the special star globbing enabler.
1668 */
b2bc0bc8 1669 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
33a39a3c 1670 event_param, channel);
3c997079 1671 } else {
b2bc0bc8 1672 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
33a39a3c 1673 event_param, channel);
1ec65de1 1674 }
b2bc0bc8 1675 priv = event_enabler;
33a39a3c
MD
1676 } else {
1677 struct lttng_event *event;
4ee2453d 1678
33a39a3c
MD
1679 /*
1680 * We tolerate no failure path after event creation. It
1681 * will stay invariant for the rest of the session.
1682 */
1683 event = lttng_event_create(channel, event_param,
1684 NULL, NULL,
1685 event_param->instrumentation);
1686 WARN_ON_ONCE(!event);
1687 if (IS_ERR(event)) {
1688 ret = PTR_ERR(event);
1689 goto event_error;
80f87dd2 1690 }
33a39a3c 1691 priv = event;
03037b98 1692 }
33a39a3c
MD
1693 event_file->private_data = priv;
1694 fd_install(event_fd, event_file);
653fe716
MD
1695 return event_fd;
1696
03037b98 1697event_error:
9c1f4643
MD
1698 atomic_long_dec(&channel_file->f_count);
1699refcount_error:
c0e31d2e 1700 fput(event_file);
653fe716
MD
1701file_error:
1702 put_unused_fd(event_fd);
1703fd_error:
653fe716
MD
1704 return ret;
1705}
ad1c05e1 1706
dffef45d
FD
1707static
1708long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1709{
1710 struct lttng_event_notifier_enabler *event_notifier_enabler;
1711 enum lttng_event_type *evtype = file->private_data;
1712
1713 switch (cmd) {
1714 case LTTNG_KERNEL_ENABLE:
1715 switch (*evtype) {
1716 case LTTNG_TYPE_EVENT:
1717 return -EINVAL;
1718 case LTTNG_TYPE_ENABLER:
1719 event_notifier_enabler = file->private_data;
1720 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
1721 default:
1722 WARN_ON_ONCE(1);
1723 return -ENOSYS;
1724 }
1725 case LTTNG_KERNEL_DISABLE:
1726 switch (*evtype) {
1727 case LTTNG_TYPE_EVENT:
1728 return -EINVAL;
1729 case LTTNG_TYPE_ENABLER:
1730 event_notifier_enabler = file->private_data;
1731 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
1732 default:
1733 WARN_ON_ONCE(1);
1734 return -ENOSYS;
1735 }
1736 case LTTNG_KERNEL_FILTER:
1737 switch (*evtype) {
1738 case LTTNG_TYPE_EVENT:
1739 return -EINVAL;
1740 case LTTNG_TYPE_ENABLER:
1741 event_notifier_enabler = file->private_data;
1742 return lttng_event_notifier_enabler_attach_bytecode(event_notifier_enabler,
1743 (struct lttng_kernel_filter_bytecode __user *) arg);
1744 default:
1745 WARN_ON_ONCE(1);
1746 return -ENOSYS;
1747 }
1748 default:
1749 return -ENOIOCTLCMD;
1750 }
1751}
1752
1753static
1754int lttng_event_notifier_release(struct inode *inode, struct file *file)
1755{
1756 struct lttng_event_notifier *event_notifier;
1757 struct lttng_event_notifier_enabler *event_notifier_enabler;
1758 enum lttng_event_type *evtype = file->private_data;
1759
1760 if (!evtype)
1761 return 0;
1762
1763 switch (*evtype) {
1764 case LTTNG_TYPE_EVENT:
1765 event_notifier = file->private_data;
1766 if (event_notifier)
1767 fput(event_notifier->group->file);
1768 break;
1769 case LTTNG_TYPE_ENABLER:
1770 event_notifier_enabler = file->private_data;
1771 if (event_notifier_enabler)
1772 fput(event_notifier_enabler->group->file);
1773 break;
1774 default:
1775 WARN_ON_ONCE(1);
1776 break;
1777 }
1778
1779 return 0;
1780}
1781
1782static const struct file_operations lttng_event_notifier_fops = {
1783 .owner = THIS_MODULE,
1784 .release = lttng_event_notifier_release,
1785 .unlocked_ioctl = lttng_event_notifier_ioctl,
1786#ifdef CONFIG_COMPAT
1787 .compat_ioctl = lttng_event_notifier_ioctl,
1788#endif
1789};
1790
1791static
1792int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
1793 struct lttng_kernel_event_notifier *event_notifier_param)
1794{
1795 struct lttng_event_notifier_group *event_notifier_group =
1796 event_notifier_group_file->private_data;
1797 int event_notifier_fd, ret;
1798 struct file *event_notifier_file;
1799 void *priv;
1800
1801 switch (event_notifier_param->event.instrumentation) {
1802 case LTTNG_KERNEL_TRACEPOINT:
1803 case LTTNG_KERNEL_KPROBE:
1804 case LTTNG_KERNEL_UPROBE:
1805 case LTTNG_KERNEL_KRETPROBE:
1806 case LTTNG_KERNEL_FUNCTION:
1807 case LTTNG_KERNEL_NOOP:
1808 case LTTNG_KERNEL_SYSCALL:
1809 default:
1810 ret = -EINVAL;
1811 goto inval_instr;
1812 }
1813
1814 event_notifier_param->event.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1815
1816 event_notifier_fd = lttng_get_unused_fd();
1817 if (event_notifier_fd < 0) {
1818 ret = event_notifier_fd;
1819 goto fd_error;
1820 }
1821
1822 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
1823 &lttng_event_notifier_fops,
1824 NULL, O_RDWR);
1825 if (IS_ERR(event_notifier_file)) {
1826 ret = PTR_ERR(event_notifier_file);
1827 goto file_error;
1828 }
1829
1830 /* The event notifier holds a reference on the event notifier group. */
1831 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
1832 ret = -EOVERFLOW;
1833 goto refcount_error;
1834 }
1835
1836 if (event_notifier_param->event.instrumentation == LTTNG_KERNEL_TRACEPOINT
1837 || event_notifier_param->event.instrumentation == LTTNG_KERNEL_SYSCALL) {
1838 struct lttng_event_notifier_enabler *enabler;
1839
1840 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
1841 /*
1842 * If the event name is a star globbing pattern,
1843 * we create the special star globbing enabler.
1844 */
1845 enabler = lttng_event_notifier_enabler_create(
1846 event_notifier_group,
1847 LTTNG_ENABLER_FORMAT_STAR_GLOB,
1848 event_notifier_param);
1849 } else {
1850 enabler = lttng_event_notifier_enabler_create(
1851 event_notifier_group,
1852 LTTNG_ENABLER_FORMAT_NAME,
1853 event_notifier_param);
1854 }
1855 priv = enabler;
1856 } else {
1857 struct lttng_event_notifier *event_notifier;
1858
1859 /*
1860 * We tolerate no failure path after event notifier creation.
1861 * It will stay invariant for the rest of the session.
1862 */
1863 event_notifier = lttng_event_notifier_create(NULL,
1864 event_notifier_param->event.token, event_notifier_group,
1865 event_notifier_param, NULL,
1866 event_notifier_param->event.instrumentation);
1867 WARN_ON_ONCE(!event_notifier);
1868 if (IS_ERR(event_notifier)) {
1869 ret = PTR_ERR(event_notifier);
1870 goto event_notifier_error;
1871 }
1872 priv = event_notifier;
1873 }
1874 event_notifier_file->private_data = priv;
1875 fd_install(event_notifier_fd, event_notifier_file);
1876 return event_notifier_fd;
1877
1878event_notifier_error:
1879 atomic_long_dec(&event_notifier_group_file->f_count);
1880refcount_error:
1881 fput(event_notifier_file);
1882file_error:
1883 put_unused_fd(event_notifier_fd);
1884fd_error:
1885inval_instr:
1886 return ret;
1887}
1888
750b05f2
FD
1889static
1890long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
1891 unsigned long arg)
1892{
1893 switch (cmd) {
21f58fb7
FD
1894 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
1895 {
1896 return lttng_abi_open_event_notifier_group_stream(file);
1897 }
dffef45d
FD
1898 case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE:
1899 {
1900 struct lttng_kernel_event_notifier uevent_notifier_param;
1901
1902 if (copy_from_user(&uevent_notifier_param,
1903 (struct lttng_kernel_event_notifier __user *) arg,
1904 sizeof(uevent_notifier_param)))
1905 return -EFAULT;
1906 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
1907 }
750b05f2
FD
1908 default:
1909 return -ENOIOCTLCMD;
1910 }
1911 return 0;
1912}
1913
1914static
1915int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
1916{
1917 struct lttng_event_notifier_group *event_notifier_group =
1918 file->private_data;
1919
1920 if (event_notifier_group)
1921 lttng_event_notifier_group_destroy(event_notifier_group);
1922 return 0;
1923}
1924
1925static const struct file_operations lttng_event_notifier_group_fops = {
1926 .owner = THIS_MODULE,
1927 .release = lttng_event_notifier_group_release,
1928 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
1929#ifdef CONFIG_COMPAT
1930 .compat_ioctl = lttng_event_notifier_group_ioctl,
1931#endif
1932};
1933
ad1c05e1
MD
1934/**
1935 * lttng_channel_ioctl - lttng syscall through ioctl
1936 *
c0e31d2e 1937 * @file: the file
ad1c05e1
MD
1938 * @cmd: the command
1939 * @arg: command arg
1940 *
1941 * This ioctl implements lttng commands:
38d024ae 1942 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
1943 * Returns an event stream file descriptor or failure.
1944 * (typically, one event stream records events from one CPU)
38d024ae 1945 * LTTNG_KERNEL_EVENT
ad1c05e1 1946 * Returns an event file descriptor or failure.
8070f5c0
MD
1947 * LTTNG_KERNEL_CONTEXT
1948 * Prepend a context field to each event in the channel
e64957da
MD
1949 * LTTNG_KERNEL_ENABLE
1950 * Enable recording for events in this channel (weak enable)
1951 * LTTNG_KERNEL_DISABLE
1952 * Disable recording for events in this channel (strong disable)
baf20995 1953 *
baf20995
MD
1954 * Channel and event file descriptors also hold a reference on the session.
1955 */
ad1c05e1 1956static
c0e31d2e 1957long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 1958{
a90917c3 1959 struct lttng_channel *channel = file->private_data;
8070f5c0 1960
baf20995 1961 switch (cmd) {
6dccd6c1 1962 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1963 case LTTNG_KERNEL_STREAM:
c0e31d2e 1964 return lttng_abi_open_stream(file);
6dccd6c1
JD
1965 case LTTNG_KERNEL_OLD_EVENT:
1966 {
1967 struct lttng_kernel_event *uevent_param;
1968 struct lttng_kernel_old_event *old_uevent_param;
1969 int ret;
1970
1971 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1972 GFP_KERNEL);
1973 if (!uevent_param) {
1974 ret = -ENOMEM;
1975 goto old_event_end;
1976 }
1977 old_uevent_param = kmalloc(
1978 sizeof(struct lttng_kernel_old_event),
1979 GFP_KERNEL);
1980 if (!old_uevent_param) {
1981 ret = -ENOMEM;
1982 goto old_event_error_free_param;
1983 }
1984 if (copy_from_user(old_uevent_param,
1985 (struct lttng_kernel_old_event __user *) arg,
1986 sizeof(struct lttng_kernel_old_event))) {
1987 ret = -EFAULT;
1988 goto old_event_error_free_old_param;
1989 }
1990
1991 memcpy(uevent_param->name, old_uevent_param->name,
1992 sizeof(uevent_param->name));
1993 uevent_param->instrumentation =
1994 old_uevent_param->instrumentation;
1995
1996 switch (old_uevent_param->instrumentation) {
1997 case LTTNG_KERNEL_KPROBE:
1998 uevent_param->u.kprobe.addr =
1999 old_uevent_param->u.kprobe.addr;
2000 uevent_param->u.kprobe.offset =
2001 old_uevent_param->u.kprobe.offset;
2002 memcpy(uevent_param->u.kprobe.symbol_name,
2003 old_uevent_param->u.kprobe.symbol_name,
2004 sizeof(uevent_param->u.kprobe.symbol_name));
2005 break;
2006 case LTTNG_KERNEL_KRETPROBE:
2007 uevent_param->u.kretprobe.addr =
2008 old_uevent_param->u.kretprobe.addr;
2009 uevent_param->u.kretprobe.offset =
2010 old_uevent_param->u.kretprobe.offset;
2011 memcpy(uevent_param->u.kretprobe.symbol_name,
2012 old_uevent_param->u.kretprobe.symbol_name,
2013 sizeof(uevent_param->u.kretprobe.symbol_name));
2014 break;
2015 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
2016 WARN_ON_ONCE(1);
2017 /* Not implemented. */
6dccd6c1
JD
2018 break;
2019 default:
2020 break;
2021 }
2022 ret = lttng_abi_create_event(file, uevent_param);
2023
2024old_event_error_free_old_param:
2025 kfree(old_uevent_param);
2026old_event_error_free_param:
2027 kfree(uevent_param);
2028old_event_end:
2029 return ret;
2030 }
38d024ae 2031 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
2032 {
2033 struct lttng_kernel_event uevent_param;
2034
2035 if (copy_from_user(&uevent_param,
2036 (struct lttng_kernel_event __user *) arg,
2037 sizeof(uevent_param)))
2038 return -EFAULT;
2039 return lttng_abi_create_event(file, &uevent_param);
2040 }
2041 case LTTNG_KERNEL_OLD_CONTEXT:
2042 {
2043 struct lttng_kernel_context *ucontext_param;
2044 struct lttng_kernel_old_context *old_ucontext_param;
2045 int ret;
2046
2047 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
2048 GFP_KERNEL);
2049 if (!ucontext_param) {
2050 ret = -ENOMEM;
2051 goto old_ctx_end;
2052 }
2053 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
2054 GFP_KERNEL);
2055 if (!old_ucontext_param) {
2056 ret = -ENOMEM;
2057 goto old_ctx_error_free_param;
2058 }
2059
2060 if (copy_from_user(old_ucontext_param,
2061 (struct lttng_kernel_old_context __user *) arg,
2062 sizeof(struct lttng_kernel_old_context))) {
2063 ret = -EFAULT;
2064 goto old_ctx_error_free_old_param;
2065 }
2066 ucontext_param->ctx = old_ucontext_param->ctx;
2067 memcpy(ucontext_param->padding, old_ucontext_param->padding,
2068 sizeof(ucontext_param->padding));
2069 /* only type that uses the union */
2070 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
2071 ucontext_param->u.perf_counter.type =
2072 old_ucontext_param->u.perf_counter.type;
2073 ucontext_param->u.perf_counter.config =
2074 old_ucontext_param->u.perf_counter.config;
2075 memcpy(ucontext_param->u.perf_counter.name,
2076 old_ucontext_param->u.perf_counter.name,
2077 sizeof(ucontext_param->u.perf_counter.name));
2078 }
2079
2080 ret = lttng_abi_add_context(file,
2081 ucontext_param,
2082 &channel->ctx, channel->session);
2083
2084old_ctx_error_free_old_param:
2085 kfree(old_ucontext_param);
2086old_ctx_error_free_param:
2087 kfree(ucontext_param);
2088old_ctx_end:
2089 return ret;
2090 }
8070f5c0 2091 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
2092 {
2093 struct lttng_kernel_context ucontext_param;
2094
2095 if (copy_from_user(&ucontext_param,
8070f5c0 2096 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
2097 sizeof(ucontext_param)))
2098 return -EFAULT;
2099 return lttng_abi_add_context(file,
2100 &ucontext_param,
8070f5c0 2101 &channel->ctx, channel->session);
6dccd6c1
JD
2102 }
2103 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 2104 case LTTNG_KERNEL_ENABLE:
a90917c3 2105 return lttng_channel_enable(channel);
6dccd6c1 2106 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 2107 case LTTNG_KERNEL_DISABLE:
a90917c3 2108 return lttng_channel_disable(channel);
12e579db
MD
2109 case LTTNG_KERNEL_SYSCALL_MASK:
2110 return lttng_channel_syscall_mask(channel,
2111 (struct lttng_kernel_syscall_mask __user *) arg);
baf20995
MD
2112 default:
2113 return -ENOIOCTLCMD;
2114 }
2115}
2116
5dbbdb43
MD
2117/**
2118 * lttng_metadata_ioctl - lttng syscall through ioctl
2119 *
2120 * @file: the file
2121 * @cmd: the command
2122 * @arg: command arg
2123 *
2124 * This ioctl implements lttng commands:
38d024ae 2125 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
2126 * Returns an event stream file descriptor or failure.
2127 *
2128 * Channel and event file descriptors also hold a reference on the session.
2129 */
2130static
2131long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2132{
2133 switch (cmd) {
6dccd6c1 2134 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 2135 case LTTNG_KERNEL_STREAM:
d83004aa 2136 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
2137 default:
2138 return -ENOIOCTLCMD;
2139 }
2140}
2141
653fe716
MD
2142/**
2143 * lttng_channel_poll - lttng stream addition/removal monitoring
2144 *
c0e31d2e 2145 * @file: the file
653fe716
MD
2146 * @wait: poll table
2147 */
c0e31d2e 2148unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 2149{
a90917c3 2150 struct lttng_channel *channel = file->private_data;
653fe716
MD
2151 unsigned int mask = 0;
2152
c0e31d2e 2153 if (file->f_mode & FMODE_READ) {
a33e44a6 2154 poll_wait_set_exclusive(wait);
24cedcfe
MD
2155 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
2156 wait);
653fe716 2157
254ec7bc
MD
2158 if (channel->ops->is_disabled(channel->chan))
2159 return POLLERR;
24cedcfe 2160 if (channel->ops->is_finalized(channel->chan))
653fe716 2161 return POLLHUP;
f71ecafa 2162 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 2163 return POLLIN | POLLRDNORM;
f71ecafa 2164 return 0;
653fe716
MD
2165 }
2166 return mask;
2167
2168}
2169
0a84a57f
MD
2170static
2171int lttng_channel_release(struct inode *inode, struct file *file)
2172{
a90917c3 2173 struct lttng_channel *channel = file->private_data;
c269fff4
MD
2174
2175 if (channel)
2176 fput(channel->session->file);
0a84a57f
MD
2177 return 0;
2178}
2179
d83004aa
JD
2180static
2181int lttng_metadata_channel_release(struct inode *inode, struct file *file)
2182{
2183 struct lttng_channel *channel = file->private_data;
2184
2185 if (channel) {
d83004aa 2186 fput(channel->session->file);
a3381417 2187 lttng_metadata_channel_destroy(channel);
d83004aa
JD
2188 }
2189
2190 return 0;
2191}
2192
ad1c05e1 2193static const struct file_operations lttng_channel_fops = {
a33c9927 2194 .owner = THIS_MODULE,
03037b98 2195 .release = lttng_channel_release,
653fe716 2196 .poll = lttng_channel_poll,
ad1c05e1 2197 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 2198#ifdef CONFIG_COMPAT
03037b98 2199 .compat_ioctl = lttng_channel_ioctl,
baf20995 2200#endif
11b5a3c2 2201};
baf20995 2202
5dbbdb43 2203static const struct file_operations lttng_metadata_fops = {
a33c9927 2204 .owner = THIS_MODULE,
d83004aa 2205 .release = lttng_metadata_channel_release,
5dbbdb43
MD
2206 .unlocked_ioctl = lttng_metadata_ioctl,
2207#ifdef CONFIG_COMPAT
2208 .compat_ioctl = lttng_metadata_ioctl,
2209#endif
2210};
2211
8070f5c0
MD
2212/**
2213 * lttng_event_ioctl - lttng syscall through ioctl
2214 *
2215 * @file: the file
2216 * @cmd: the command
2217 * @arg: command arg
2218 *
2219 * This ioctl implements lttng commands:
8070f5c0
MD
2220 * LTTNG_KERNEL_CONTEXT
2221 * Prepend a context field to each record of this event
e64957da
MD
2222 * LTTNG_KERNEL_ENABLE
2223 * Enable recording for this event (weak enable)
2224 * LTTNG_KERNEL_DISABLE
2225 * Disable recording for this event (strong disable)
8070f5c0
MD
2226 */
2227static
2228long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2229{
3c997079 2230 struct lttng_event *event;
b2bc0bc8 2231 struct lttng_event_enabler *event_enabler;
3c997079 2232 enum lttng_event_type *evtype = file->private_data;
8070f5c0
MD
2233
2234 switch (cmd) {
6dccd6c1
JD
2235 case LTTNG_KERNEL_OLD_CONTEXT:
2236 {
3c997079
MD
2237 /* Not implemented */
2238 return -ENOSYS;
6dccd6c1 2239 }
8070f5c0 2240 case LTTNG_KERNEL_CONTEXT:
6dccd6c1 2241 {
3c997079
MD
2242 /* Not implemented */
2243 return -ENOSYS;
6dccd6c1
JD
2244 }
2245 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 2246 case LTTNG_KERNEL_ENABLE:
3c997079
MD
2247 switch (*evtype) {
2248 case LTTNG_TYPE_EVENT:
2249 event = file->private_data;
2250 return lttng_event_enable(event);
2251 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
2252 event_enabler = file->private_data;
2253 return lttng_event_enabler_enable(event_enabler);
3c997079
MD
2254 default:
2255 WARN_ON_ONCE(1);
2256 return -ENOSYS;
2257 }
6dccd6c1 2258 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 2259 case LTTNG_KERNEL_DISABLE:
3c997079
MD
2260 switch (*evtype) {
2261 case LTTNG_TYPE_EVENT:
2262 event = file->private_data;
2263 return lttng_event_disable(event);
2264 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
2265 event_enabler = file->private_data;
2266 return lttng_event_enabler_disable(event_enabler);
3c997079
MD
2267 default:
2268 WARN_ON_ONCE(1);
2269 return -ENOSYS;
2270 }
07dfc1d0
MD
2271 case LTTNG_KERNEL_FILTER:
2272 switch (*evtype) {
2273 case LTTNG_TYPE_EVENT:
2274 return -EINVAL;
2275 case LTTNG_TYPE_ENABLER:
2276 {
b2bc0bc8
FD
2277 event_enabler = file->private_data;
2278 return lttng_event_enabler_attach_bytecode(event_enabler,
07dfc1d0
MD
2279 (struct lttng_kernel_filter_bytecode __user *) arg);
2280 }
0586316f
MD
2281 default:
2282 WARN_ON_ONCE(1);
2283 return -ENOSYS;
07dfc1d0 2284 }
3aed4dca
FD
2285 case LTTNG_KERNEL_ADD_CALLSITE:
2286 switch (*evtype) {
2287 case LTTNG_TYPE_EVENT:
2288 event = file->private_data;
2289 return lttng_event_add_callsite(event,
2290 (struct lttng_kernel_event_callsite __user *) arg);
2291 case LTTNG_TYPE_ENABLER:
2292 return -EINVAL;
64c147c0
MD
2293 default:
2294 WARN_ON_ONCE(1);
2295 return -ENOSYS;
3aed4dca 2296 }
8070f5c0
MD
2297 default:
2298 return -ENOIOCTLCMD;
2299 }
2300}
2301
0a84a57f
MD
2302static
2303int lttng_event_release(struct inode *inode, struct file *file)
2304{
3c997079 2305 struct lttng_event *event;
b2bc0bc8 2306 struct lttng_event_enabler *event_enabler;
3c997079
MD
2307 enum lttng_event_type *evtype = file->private_data;
2308
2309 if (!evtype)
2310 return 0;
2311
2312 switch (*evtype) {
2313 case LTTNG_TYPE_EVENT:
2314 event = file->private_data;
2315 if (event)
2316 fput(event->chan->file);
2317 break;
2318 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
2319 event_enabler = file->private_data;
2320 if (event_enabler)
2321 fput(event_enabler->chan->file);
3c997079
MD
2322 break;
2323 default:
2324 WARN_ON_ONCE(1);
2325 break;
2326 }
c269fff4 2327
0a84a57f
MD
2328 return 0;
2329}
2330
3b923e5b 2331/* TODO: filter control ioctl */
0a84a57f 2332static const struct file_operations lttng_event_fops = {
a33c9927 2333 .owner = THIS_MODULE,
0a84a57f 2334 .release = lttng_event_release,
8070f5c0
MD
2335 .unlocked_ioctl = lttng_event_ioctl,
2336#ifdef CONFIG_COMPAT
2337 .compat_ioctl = lttng_event_ioctl,
2338#endif
11b5a3c2 2339};
0a84a57f 2340
3b731ab1
JD
2341static int put_u64(uint64_t val, unsigned long arg)
2342{
2343 return put_user(val, (uint64_t __user *) arg);
2344}
2345
8b97fd42
MD
2346static int put_u32(uint32_t val, unsigned long arg)
2347{
2348 return put_user(val, (uint32_t __user *) arg);
2349}
2350
ed8d02d6
JD
2351static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2352 unsigned int cmd, unsigned long arg)
2353{
3b731ab1
JD
2354 struct lib_ring_buffer *buf = filp->private_data;
2355 struct channel *chan = buf->backend.chan;
2356 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 2357 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2358 int ret;
2359
2360 if (atomic_read(&chan->record_disabled))
2361 return -EIO;
2362
ed8d02d6 2363 switch (cmd) {
3b731ab1
JD
2364 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
2365 {
2366 uint64_t ts;
2367
dd5a0db3 2368 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2369 if (ret < 0)
2370 goto error;
2371 return put_u64(ts, arg);
2372 }
2373 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
2374 {
2375 uint64_t ts;
2376
dd5a0db3 2377 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
2378 if (ret < 0)
2379 goto error;
2380 return put_u64(ts, arg);
2381 }
2382 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
2383 {
2384 uint64_t ed;
2385
dd5a0db3 2386 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
2387 if (ret < 0)
2388 goto error;
2389 return put_u64(ed, arg);
2390 }
2391 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
2392 {
2393 uint64_t cs;
2394
dd5a0db3 2395 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
2396 if (ret < 0)
2397 goto error;
2398 return put_u64(cs, arg);
2399 }
2400 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
2401 {
2402 uint64_t ps;
2403
dd5a0db3 2404 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
2405 if (ret < 0)
2406 goto error;
2407 return put_u64(ps, arg);
2408 }
2409 case LTTNG_RING_BUFFER_GET_STREAM_ID:
2410 {
2411 uint64_t si;
2412
dd5a0db3 2413 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
2414 if (ret < 0)
2415 goto error;
2416 return put_u64(si, arg);
2417 }
2348ca17
JD
2418 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2419 {
2420 uint64_t ts;
2421
dd5a0db3 2422 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
2423 if (ret < 0)
2424 goto error;
2425 return put_u64(ts, arg);
2426 }
5b3cf4f9
JD
2427 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
2428 {
2429 uint64_t seq;
2430
2431 ret = ops->sequence_number(config, buf, &seq);
2432 if (ret < 0)
2433 goto error;
2434 return put_u64(seq, arg);
2435 }
5594698f
JD
2436 case LTTNG_RING_BUFFER_INSTANCE_ID:
2437 {
2438 uint64_t id;
2439
2440 ret = ops->instance_id(config, buf, &id);
2441 if (ret < 0)
2442 goto error;
2443 return put_u64(id, arg);
2444 }
3b731ab1
JD
2445 default:
2446 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2447 cmd, arg);
ed8d02d6 2448 }
3b731ab1
JD
2449
2450error:
2451 return -ENOSYS;
ed8d02d6
JD
2452}
2453
2454#ifdef CONFIG_COMPAT
2455static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2456 unsigned int cmd, unsigned long arg)
2457{
3b731ab1
JD
2458 struct lib_ring_buffer *buf = filp->private_data;
2459 struct channel *chan = buf->backend.chan;
2460 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 2461 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2462 int ret;
2463
2464 if (atomic_read(&chan->record_disabled))
2465 return -EIO;
2466
ed8d02d6 2467 switch (cmd) {
3b731ab1
JD
2468 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
2469 {
2470 uint64_t ts;
2471
dd5a0db3 2472 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2473 if (ret < 0)
2474 goto error;
2475 return put_u64(ts, arg);
2476 }
2477 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
2478 {
2479 uint64_t ts;
2480
dd5a0db3 2481 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
2482 if (ret < 0)
2483 goto error;
2484 return put_u64(ts, arg);
2485 }
2486 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
2487 {
2488 uint64_t ed;
2489
dd5a0db3 2490 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
2491 if (ret < 0)
2492 goto error;
2493 return put_u64(ed, arg);
ed8d02d6 2494 }
3b731ab1
JD
2495 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
2496 {
2497 uint64_t cs;
2498
dd5a0db3 2499 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
2500 if (ret < 0)
2501 goto error;
2502 return put_u64(cs, arg);
2503 }
2504 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
2505 {
2506 uint64_t ps;
2507
dd5a0db3 2508 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
2509 if (ret < 0)
2510 goto error;
2511 return put_u64(ps, arg);
2512 }
2513 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
2514 {
2515 uint64_t si;
2516
dd5a0db3 2517 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
2518 if (ret < 0)
2519 goto error;
2520 return put_u64(si, arg);
2521 }
2348ca17
JD
2522 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2523 {
2524 uint64_t ts;
2525
dd5a0db3 2526 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
2527 if (ret < 0)
2528 goto error;
2529 return put_u64(ts, arg);
2530 }
5b3cf4f9
JD
2531 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
2532 {
2533 uint64_t seq;
2534
2535 ret = ops->sequence_number(config, buf, &seq);
2536 if (ret < 0)
2537 goto error;
2538 return put_u64(seq, arg);
2539 }
5594698f
JD
2540 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
2541 {
2542 uint64_t id;
2543
2544 ret = ops->instance_id(config, buf, &id);
2545 if (ret < 0)
2546 goto error;
2547 return put_u64(id, arg);
2548 }
3b731ab1
JD
2549 default:
2550 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2551 cmd, arg);
2552 }
2553
2554error:
2555 return -ENOSYS;
ed8d02d6
JD
2556}
2557#endif /* CONFIG_COMPAT */
2558
2559static void lttng_stream_override_ring_buffer_fops(void)
2560{
2561 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2562 lttng_stream_ring_buffer_file_operations.open =
2563 lib_ring_buffer_file_operations.open;
2564 lttng_stream_ring_buffer_file_operations.release =
2565 lib_ring_buffer_file_operations.release;
2566 lttng_stream_ring_buffer_file_operations.poll =
2567 lib_ring_buffer_file_operations.poll;
2568 lttng_stream_ring_buffer_file_operations.splice_read =
2569 lib_ring_buffer_file_operations.splice_read;
2570 lttng_stream_ring_buffer_file_operations.mmap =
2571 lib_ring_buffer_file_operations.mmap;
2572 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2573 lttng_stream_ring_buffer_ioctl;
2574 lttng_stream_ring_buffer_file_operations.llseek =
2575 lib_ring_buffer_file_operations.llseek;
2576#ifdef CONFIG_COMPAT
2577 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2578 lttng_stream_ring_buffer_compat_ioctl;
2579#endif
2580}
2581
80996790 2582int __init lttng_abi_init(void)
baf20995
MD
2583{
2584 int ret = 0;
2585
263b6c88 2586 wrapper_vmalloc_sync_mappings();
2754583e 2587 lttng_clock_ref();
f771eda6
JD
2588
2589 ret = lttng_tp_mempool_init();
2590 if (ret) {
2591 goto error;
2592 }
2593
d29348f7 2594 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 2595 &lttng_proc_ops, NULL);
2470a237 2596
255e52a4 2597 if (!lttng_proc_dentry) {
5a15f70c 2598 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
2599 ret = -ENOMEM;
2600 goto error;
2601 }
ed8d02d6 2602 lttng_stream_override_ring_buffer_fops();
2754583e 2603 return 0;
ed8d02d6 2604
baf20995 2605error:
f771eda6 2606 lttng_tp_mempool_destroy();
2754583e 2607 lttng_clock_unref();
baf20995
MD
2608 return ret;
2609}
2610
e6e65fcd
MD
2611/* No __exit annotation because used by init error path too. */
2612void lttng_abi_exit(void)
baf20995 2613{
f771eda6 2614 lttng_tp_mempool_destroy();
2754583e 2615 lttng_clock_unref();
e6a17f26
MD
2616 if (lttng_proc_dentry)
2617 remove_proc_entry("lttng", NULL);
baf20995 2618}
This page took 0.167752 seconds and 4 git commands to generate.