Introduce event notifier lib ring buffer client
[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>
baf20995
MD
47
48/*
49 * This is LTTng's own personal way to create a system call as an external
80996790 50 * module. We use ioctl() on /proc/lttng.
baf20995
MD
51 */
52
e6a17f26 53static struct proc_dir_entry *lttng_proc_dentry;
059de147
MJ
54
55#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
56static const struct proc_ops lttng_proc_ops;
57#else
58static const struct file_operations lttng_proc_ops;
59#endif
60
ad1c05e1
MD
61static const struct file_operations lttng_session_fops;
62static const struct file_operations lttng_channel_fops;
5dbbdb43 63static const struct file_operations lttng_metadata_fops;
305d3e42 64static const struct file_operations lttng_event_fops;
ed8d02d6 65static struct file_operations lttng_stream_ring_buffer_file_operations;
baf20995 66
9616f0bf 67static int put_u64(uint64_t val, unsigned long arg);
8b97fd42 68static int put_u32(uint32_t val, unsigned long arg);
9616f0bf 69
a33c9927
MD
70/*
71 * Teardown management: opened file descriptors keep a refcount on the module,
72 * so it can only exit when all file descriptors are closed.
73 */
74
ad1c05e1 75static
baf20995
MD
76int lttng_abi_create_session(void)
77{
a90917c3 78 struct lttng_session *session;
c0e31d2e 79 struct file *session_file;
11b5a3c2 80 int session_fd, ret;
baf20995 81
a90917c3 82 session = lttng_session_create();
baf20995
MD
83 if (!session)
84 return -ENOMEM;
4ac10b76 85 session_fd = lttng_get_unused_fd();
baf20995
MD
86 if (session_fd < 0) {
87 ret = session_fd;
88 goto fd_error;
89 }
c0e31d2e 90 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 91 &lttng_session_fops,
baf20995 92 session, O_RDWR);
c0e31d2e
MD
93 if (IS_ERR(session_file)) {
94 ret = PTR_ERR(session_file);
baf20995
MD
95 goto file_error;
96 }
c0e31d2e
MD
97 session->file = session_file;
98 fd_install(session_fd, session_file);
baf20995
MD
99 return session_fd;
100
101file_error:
102 put_unused_fd(session_fd);
103fd_error:
a90917c3 104 lttng_session_destroy(session);
baf20995
MD
105 return ret;
106}
107
271b6681
MD
108static
109int lttng_abi_tracepoint_list(void)
110{
111 struct file *tracepoint_list_file;
112 int file_fd, ret;
113
4ac10b76 114 file_fd = lttng_get_unused_fd();
271b6681
MD
115 if (file_fd < 0) {
116 ret = file_fd;
117 goto fd_error;
118 }
30f18bf0 119
8ee099b6 120 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
271b6681
MD
121 &lttng_tracepoint_list_fops,
122 NULL, O_RDWR);
123 if (IS_ERR(tracepoint_list_file)) {
124 ret = PTR_ERR(tracepoint_list_file);
125 goto file_error;
126 }
30f18bf0
MD
127 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
128 if (ret < 0)
129 goto open_error;
271b6681
MD
130 fd_install(file_fd, tracepoint_list_file);
131 return file_fd;
132
30f18bf0
MD
133open_error:
134 fput(tracepoint_list_file);
271b6681
MD
135file_error:
136 put_unused_fd(file_fd);
137fd_error:
138 return ret;
139}
140
f127e61e
MD
141#ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
142static inline
143int lttng_abi_syscall_list(void)
144{
145 return -ENOSYS;
146}
147#else
148static
149int lttng_abi_syscall_list(void)
150{
151 struct file *syscall_list_file;
152 int file_fd, ret;
153
154 file_fd = lttng_get_unused_fd();
155 if (file_fd < 0) {
156 ret = file_fd;
157 goto fd_error;
158 }
159
160 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
161 &lttng_syscall_list_fops,
162 NULL, O_RDWR);
163 if (IS_ERR(syscall_list_file)) {
164 ret = PTR_ERR(syscall_list_file);
165 goto file_error;
166 }
167 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
168 if (ret < 0)
169 goto open_error;
170 fd_install(file_fd, syscall_list_file);
f127e61e
MD
171 return file_fd;
172
173open_error:
174 fput(syscall_list_file);
175file_error:
176 put_unused_fd(file_fd);
177fd_error:
178 return ret;
179}
180#endif
181
80c16bcf 182static
6dccd6c1 183void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
80c16bcf 184{
6dccd6c1
JD
185 v->major = LTTNG_MODULES_MAJOR_VERSION;
186 v->minor = LTTNG_MODULES_MINOR_VERSION;
187 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
80c16bcf
MD
188}
189
42cabb80
MD
190static
191void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v)
192{
193 v->major = LTTNG_MODULES_ABI_MAJOR_VERSION;
194 v->minor = LTTNG_MODULES_ABI_MINOR_VERSION;
195}
196
8070f5c0
MD
197static
198long lttng_abi_add_context(struct file *file,
6dccd6c1 199 struct lttng_kernel_context *context_param,
a90917c3 200 struct lttng_ctx **ctx, struct lttng_session *session)
8070f5c0 201{
8070f5c0
MD
202
203 if (session->been_active)
204 return -EPERM;
205
6dccd6c1 206 switch (context_param->ctx) {
12a313a5 207 case LTTNG_KERNEL_CONTEXT_PID:
8070f5c0 208 return lttng_add_pid_to_ctx(ctx);
a8ad3613
MD
209 case LTTNG_KERNEL_CONTEXT_PRIO:
210 return lttng_add_prio_to_ctx(ctx);
53f1f0ca
MD
211 case LTTNG_KERNEL_CONTEXT_NICE:
212 return lttng_add_nice_to_ctx(ctx);
b64bc438
MD
213 case LTTNG_KERNEL_CONTEXT_VPID:
214 return lttng_add_vpid_to_ctx(ctx);
215 case LTTNG_KERNEL_CONTEXT_TID:
216 return lttng_add_tid_to_ctx(ctx);
217 case LTTNG_KERNEL_CONTEXT_VTID:
218 return lttng_add_vtid_to_ctx(ctx);
219 case LTTNG_KERNEL_CONTEXT_PPID:
220 return lttng_add_ppid_to_ctx(ctx);
221 case LTTNG_KERNEL_CONTEXT_VPPID:
222 return lttng_add_vppid_to_ctx(ctx);
12a313a5 223 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
6dccd6c1
JD
224 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
225 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
226 context_param->u.perf_counter.config,
227 context_param->u.perf_counter.name,
c24a0d71 228 ctx);
a2563e83
MD
229 case LTTNG_KERNEL_CONTEXT_PROCNAME:
230 return lttng_add_procname_to_ctx(ctx);
975da2c0
JD
231 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
232 return lttng_add_hostname_to_ctx(ctx);
b3699d90
MD
233 case LTTNG_KERNEL_CONTEXT_CPU_ID:
234 return lttng_add_cpu_id_to_ctx(ctx);
79150a49
JD
235 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE:
236 return lttng_add_interruptible_to_ctx(ctx);
237 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE:
238 return lttng_add_need_reschedule_to_ctx(ctx);
239 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE:
240 return lttng_add_preemptible_to_ctx(ctx);
241 case LTTNG_KERNEL_CONTEXT_MIGRATABLE:
242 return lttng_add_migratable_to_ctx(ctx);
2fa2d39a
FG
243 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL:
244 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER:
245 return lttng_add_callstack_to_ctx(ctx, context_param->ctx);
a6cf40a4
MJ
246 case LTTNG_KERNEL_CONTEXT_CGROUP_NS:
247 return lttng_add_cgroup_ns_to_ctx(ctx);
248 case LTTNG_KERNEL_CONTEXT_IPC_NS:
249 return lttng_add_ipc_ns_to_ctx(ctx);
250 case LTTNG_KERNEL_CONTEXT_MNT_NS:
251 return lttng_add_mnt_ns_to_ctx(ctx);
252 case LTTNG_KERNEL_CONTEXT_NET_NS:
253 return lttng_add_net_ns_to_ctx(ctx);
254 case LTTNG_KERNEL_CONTEXT_PID_NS:
255 return lttng_add_pid_ns_to_ctx(ctx);
256 case LTTNG_KERNEL_CONTEXT_USER_NS:
257 return lttng_add_user_ns_to_ctx(ctx);
258 case LTTNG_KERNEL_CONTEXT_UTS_NS:
259 return lttng_add_uts_ns_to_ctx(ctx);
dc923e75
MJ
260 case LTTNG_KERNEL_CONTEXT_UID:
261 return lttng_add_uid_to_ctx(ctx);
262 case LTTNG_KERNEL_CONTEXT_EUID:
263 return lttng_add_euid_to_ctx(ctx);
264 case LTTNG_KERNEL_CONTEXT_SUID:
265 return lttng_add_suid_to_ctx(ctx);
266 case LTTNG_KERNEL_CONTEXT_GID:
267 return lttng_add_gid_to_ctx(ctx);
268 case LTTNG_KERNEL_CONTEXT_EGID:
269 return lttng_add_egid_to_ctx(ctx);
270 case LTTNG_KERNEL_CONTEXT_SGID:
271 return lttng_add_sgid_to_ctx(ctx);
272 case LTTNG_KERNEL_CONTEXT_VUID:
273 return lttng_add_vuid_to_ctx(ctx);
274 case LTTNG_KERNEL_CONTEXT_VEUID:
275 return lttng_add_veuid_to_ctx(ctx);
276 case LTTNG_KERNEL_CONTEXT_VSUID:
277 return lttng_add_vsuid_to_ctx(ctx);
278 case LTTNG_KERNEL_CONTEXT_VGID:
279 return lttng_add_vgid_to_ctx(ctx);
280 case LTTNG_KERNEL_CONTEXT_VEGID:
281 return lttng_add_vegid_to_ctx(ctx);
282 case LTTNG_KERNEL_CONTEXT_VSGID:
283 return lttng_add_vsgid_to_ctx(ctx);
876e2e92
MJ
284 case LTTNG_KERNEL_CONTEXT_TIME_NS:
285 return lttng_add_time_ns_to_ctx(ctx);
8070f5c0
MD
286 default:
287 return -EINVAL;
288 }
289}
290
ad1c05e1
MD
291/**
292 * lttng_ioctl - lttng syscall through ioctl
293 *
c0e31d2e 294 * @file: the file
ad1c05e1
MD
295 * @cmd: the command
296 * @arg: command arg
297 *
298 * This ioctl implements lttng commands:
38d024ae 299 * LTTNG_KERNEL_SESSION
ad1c05e1 300 * Returns a LTTng trace session file descriptor
271b6681
MD
301 * LTTNG_KERNEL_TRACER_VERSION
302 * Returns the LTTng kernel tracer version
303 * LTTNG_KERNEL_TRACEPOINT_LIST
304 * Returns a file descriptor listing available tracepoints
360f38ea
MD
305 * LTTNG_KERNEL_WAIT_QUIESCENT
306 * Returns after all previously running probes have completed
42cabb80
MD
307 * LTTNG_KERNEL_TRACER_ABI_VERSION
308 * Returns the LTTng kernel tracer ABI version
ad1c05e1
MD
309 *
310 * The returned session will be deleted when its file descriptor is closed.
311 */
312static
c0e31d2e 313long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
314{
315 switch (cmd) {
6dccd6c1 316 case LTTNG_KERNEL_OLD_SESSION:
38d024ae 317 case LTTNG_KERNEL_SESSION:
ad1c05e1 318 return lttng_abi_create_session();
6dccd6c1
JD
319 case LTTNG_KERNEL_OLD_TRACER_VERSION:
320 {
321 struct lttng_kernel_tracer_version v;
322 struct lttng_kernel_old_tracer_version oldv;
323 struct lttng_kernel_old_tracer_version *uversion =
324 (struct lttng_kernel_old_tracer_version __user *) arg;
325
326 lttng_abi_tracer_version(&v);
327 oldv.major = v.major;
328 oldv.minor = v.minor;
329 oldv.patchlevel = v.patchlevel;
330
331 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
332 return -EFAULT;
333 return 0;
334 }
80c16bcf 335 case LTTNG_KERNEL_TRACER_VERSION:
6dccd6c1
JD
336 {
337 struct lttng_kernel_tracer_version version;
338 struct lttng_kernel_tracer_version *uversion =
339 (struct lttng_kernel_tracer_version __user *) arg;
340
341 lttng_abi_tracer_version(&version);
42cabb80
MD
342
343 if (copy_to_user(uversion, &version, sizeof(version)))
344 return -EFAULT;
345 return 0;
346 }
347 case LTTNG_KERNEL_TRACER_ABI_VERSION:
348 {
349 struct lttng_kernel_tracer_abi_version version;
350 struct lttng_kernel_tracer_abi_version *uversion =
351 (struct lttng_kernel_tracer_abi_version __user *) arg;
352
353 lttng_abi_tracer_abi_version(&version);
354
6dccd6c1
JD
355 if (copy_to_user(uversion, &version, sizeof(version)))
356 return -EFAULT;
357 return 0;
358 }
359 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
271b6681
MD
360 case LTTNG_KERNEL_TRACEPOINT_LIST:
361 return lttng_abi_tracepoint_list();
2d2464bd
MD
362 case LTTNG_KERNEL_SYSCALL_LIST:
363 return lttng_abi_syscall_list();
6dccd6c1 364 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
5f7f9078
MD
365 case LTTNG_KERNEL_WAIT_QUIESCENT:
366 synchronize_trace();
367 return 0;
6dccd6c1
JD
368 case LTTNG_KERNEL_OLD_CALIBRATE:
369 {
370 struct lttng_kernel_old_calibrate __user *ucalibrate =
371 (struct lttng_kernel_old_calibrate __user *) arg;
372 struct lttng_kernel_old_calibrate old_calibrate;
373 struct lttng_kernel_calibrate calibrate;
374 int ret;
375
376 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
377 return -EFAULT;
378 calibrate.type = old_calibrate.type;
379 ret = lttng_calibrate(&calibrate);
380 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
381 return -EFAULT;
382 return ret;
383 }
57105fc2
MD
384 case LTTNG_KERNEL_CALIBRATE:
385 {
3db41b2c
MD
386 struct lttng_kernel_calibrate __user *ucalibrate =
387 (struct lttng_kernel_calibrate __user *) arg;
388 struct lttng_kernel_calibrate calibrate;
57105fc2
MD
389 int ret;
390
391 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
392 return -EFAULT;
393 ret = lttng_calibrate(&calibrate);
394 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
395 return -EFAULT;
396 return ret;
397 }
ad1c05e1
MD
398 default:
399 return -ENOIOCTLCMD;
400 }
401}
402
059de147
MJ
403#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
404static const struct proc_ops lttng_proc_ops = {
405 .proc_ioctl = lttng_ioctl,
406#ifdef CONFIG_COMPAT
407 .proc_compat_ioctl = lttng_ioctl,
408#endif /* CONFIG_COMPAT */
409};
410#else
411static const struct file_operations lttng_proc_ops = {
a33c9927 412 .owner = THIS_MODULE,
ad1c05e1
MD
413 .unlocked_ioctl = lttng_ioctl,
414#ifdef CONFIG_COMPAT
03037b98 415 .compat_ioctl = lttng_ioctl,
059de147 416#endif /* CONFIG_COMPAT */
11b5a3c2 417};
059de147 418#endif
ad1c05e1 419
5dbbdb43 420static
c0e31d2e 421int lttng_abi_create_channel(struct file *session_file,
6dccd6c1 422 struct lttng_kernel_channel *chan_param,
5dbbdb43 423 enum channel_type channel_type)
baf20995 424{
a90917c3 425 struct lttng_session *session = session_file->private_data;
88dfd899 426 const struct file_operations *fops = NULL;
5dbbdb43 427 const char *transport_name;
a90917c3 428 struct lttng_channel *chan;
c0e31d2e 429 struct file *chan_file;
baf20995 430 int chan_fd;
ad1c05e1 431 int ret = 0;
baf20995 432
4ac10b76 433 chan_fd = lttng_get_unused_fd();
baf20995
MD
434 if (chan_fd < 0) {
435 ret = chan_fd;
436 goto fd_error;
437 }
88dfd899
MD
438 switch (channel_type) {
439 case PER_CPU_CHANNEL:
440 fops = &lttng_channel_fops;
441 break;
442 case METADATA_CHANNEL:
443 fops = &lttng_metadata_fops;
444 break;
445 }
2470a237 446
c0e31d2e 447 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 448 fops,
03037b98 449 NULL, O_RDWR);
c0e31d2e
MD
450 if (IS_ERR(chan_file)) {
451 ret = PTR_ERR(chan_file);
baf20995
MD
452 goto file_error;
453 }
5dbbdb43
MD
454 switch (channel_type) {
455 case PER_CPU_CHANNEL:
6dccd6c1
JD
456 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
457 transport_name = chan_param->overwrite ?
96ba7208 458 "relay-overwrite" : "relay-discard";
6dccd6c1
JD
459 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
460 transport_name = chan_param->overwrite ?
96ba7208
JD
461 "relay-overwrite-mmap" : "relay-discard-mmap";
462 } else {
463 return -EINVAL;
464 }
5dbbdb43 465 break;
5dbbdb43 466 case METADATA_CHANNEL:
6dccd6c1 467 if (chan_param->output == LTTNG_KERNEL_SPLICE)
96ba7208 468 transport_name = "relay-metadata";
6dccd6c1 469 else if (chan_param->output == LTTNG_KERNEL_MMAP)
96ba7208
JD
470 transport_name = "relay-metadata-mmap";
471 else
472 return -EINVAL;
5dbbdb43
MD
473 break;
474 default:
475 transport_name = "<unknown>";
476 break;
477 }
98d7281c
MJ
478 if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) {
479 ret = -EOVERFLOW;
9c1f4643
MD
480 goto refcount_error;
481 }
03037b98
MD
482 /*
483 * We tolerate no failure path after channel creation. It will stay
484 * invariant for the rest of the session.
485 */
a90917c3 486 chan = lttng_channel_create(session, transport_name, NULL,
6dccd6c1
JD
487 chan_param->subbuf_size,
488 chan_param->num_subbuf,
489 chan_param->switch_timer_interval,
d83004aa
JD
490 chan_param->read_timer_interval,
491 channel_type);
03037b98 492 if (!chan) {
f3d01b96 493 ret = -EINVAL;
03037b98
MD
494 goto chan_error;
495 }
11b5a3c2 496 chan->file = chan_file;
c0e31d2e
MD
497 chan_file->private_data = chan;
498 fd_install(chan_fd, chan_file);
ad1c05e1 499
baf20995
MD
500 return chan_fd;
501
03037b98 502chan_error:
9c1f4643
MD
503 atomic_long_dec(&session_file->f_count);
504refcount_error:
c0e31d2e 505 fput(chan_file);
baf20995
MD
506file_error:
507 put_unused_fd(chan_fd);
508fd_error:
baf20995
MD
509 return ret;
510}
511
7f859fbf
JR
512static
513int lttng_abi_session_set_name(struct lttng_session *session,
514 struct lttng_kernel_session_name *name)
515{
516 size_t len;
517
518 len = strnlen(name->name, LTTNG_KERNEL_SESSION_NAME_LEN);
519
520 if (len == LTTNG_KERNEL_SESSION_NAME_LEN) {
521 /* Name is too long/malformed */
522 return -EINVAL;
523 }
524
525 strcpy(session->name, name->name);
526 return 0;
527}
528
1c88f269
JR
529static
530int lttng_abi_session_set_creation_time(struct lttng_session *session,
531 struct lttng_kernel_session_creation_time *time)
532{
533 size_t len;
534
535 len = strnlen(time->iso8601, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN);
536
537 if (len == LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN) {
538 /* Time is too long/malformed */
539 return -EINVAL;
540 }
541
542 strcpy(session->creation_time, time->iso8601);
543 return 0;
544}
545
d1f652f8
MD
546static
547enum tracker_type get_tracker_type(struct lttng_kernel_tracker_args *tracker)
548{
549 switch (tracker->type) {
550 case LTTNG_KERNEL_TRACKER_PID:
551 return TRACKER_PID;
552 case LTTNG_KERNEL_TRACKER_VPID:
553 return TRACKER_VPID;
554 case LTTNG_KERNEL_TRACKER_UID:
555 return TRACKER_UID;
556 case LTTNG_KERNEL_TRACKER_VUID:
557 return TRACKER_VUID;
558 case LTTNG_KERNEL_TRACKER_GID:
559 return TRACKER_GID;
560 case LTTNG_KERNEL_TRACKER_VGID:
561 return TRACKER_VGID;
562 default:
563 return TRACKER_UNKNOWN;
564 }
565}
566
baf20995 567/**
ad1c05e1 568 * lttng_session_ioctl - lttng session fd ioctl
baf20995 569 *
c0e31d2e 570 * @file: the file
baf20995
MD
571 * @cmd: the command
572 * @arg: command arg
573 *
574 * This ioctl implements lttng commands:
38d024ae 575 * LTTNG_KERNEL_CHANNEL
baf20995 576 * Returns a LTTng channel file descriptor
e64957da
MD
577 * LTTNG_KERNEL_ENABLE
578 * Enables tracing for a session (weak enable)
579 * LTTNG_KERNEL_DISABLE
580 * Disables tracing for a session (strong disable)
8070f5c0
MD
581 * LTTNG_KERNEL_METADATA
582 * Returns a LTTng metadata file descriptor
e0130fab 583 * LTTNG_KERNEL_SESSION_TRACK_PID
d1f652f8 584 * Add PID to session PID tracker
e0130fab 585 * LTTNG_KERNEL_SESSION_UNTRACK_PID
d1f652f8
MD
586 * Remove PID from session PID tracker
587 * LTTNG_KERNEL_SESSION_TRACK_ID
588 * Add ID to tracker
589 * LTTNG_KERNEL_SESSION_UNTRACK_ID
590 * Remove ID from tracker
ad1c05e1
MD
591 *
592 * The returned channel will be deleted when its file descriptor is closed.
593 */
594static
c0e31d2e 595long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 596{
a90917c3 597 struct lttng_session *session = file->private_data;
2d4c4d15
MD
598 struct lttng_kernel_channel chan_param;
599 struct lttng_kernel_old_channel old_chan_param;
c0e31d2e 600
ad1c05e1 601 switch (cmd) {
6dccd6c1
JD
602 case LTTNG_KERNEL_OLD_CHANNEL:
603 {
6dccd6c1
JD
604 if (copy_from_user(&old_chan_param,
605 (struct lttng_kernel_old_channel __user *) arg,
606 sizeof(struct lttng_kernel_old_channel)))
607 return -EFAULT;
608 chan_param.overwrite = old_chan_param.overwrite;
609 chan_param.subbuf_size = old_chan_param.subbuf_size;
610 chan_param.num_subbuf = old_chan_param.num_subbuf;
611 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
612 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
613 chan_param.output = old_chan_param.output;
614
615 return lttng_abi_create_channel(file, &chan_param,
616 PER_CPU_CHANNEL);
617 }
38d024ae 618 case LTTNG_KERNEL_CHANNEL:
6dccd6c1 619 {
6dccd6c1 620 if (copy_from_user(&chan_param,
38d024ae 621 (struct lttng_kernel_channel __user *) arg,
6dccd6c1
JD
622 sizeof(struct lttng_kernel_channel)))
623 return -EFAULT;
624 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 625 PER_CPU_CHANNEL);
6dccd6c1
JD
626 }
627 case LTTNG_KERNEL_OLD_SESSION_START:
628 case LTTNG_KERNEL_OLD_ENABLE:
38d024ae 629 case LTTNG_KERNEL_SESSION_START:
e64957da 630 case LTTNG_KERNEL_ENABLE:
a90917c3 631 return lttng_session_enable(session);
6dccd6c1
JD
632 case LTTNG_KERNEL_OLD_SESSION_STOP:
633 case LTTNG_KERNEL_OLD_DISABLE:
38d024ae 634 case LTTNG_KERNEL_SESSION_STOP:
e64957da 635 case LTTNG_KERNEL_DISABLE:
a90917c3 636 return lttng_session_disable(session);
6dccd6c1
JD
637 case LTTNG_KERNEL_OLD_METADATA:
638 {
6dccd6c1
JD
639 if (copy_from_user(&old_chan_param,
640 (struct lttng_kernel_old_channel __user *) arg,
641 sizeof(struct lttng_kernel_old_channel)))
642 return -EFAULT;
643 chan_param.overwrite = old_chan_param.overwrite;
644 chan_param.subbuf_size = old_chan_param.subbuf_size;
645 chan_param.num_subbuf = old_chan_param.num_subbuf;
646 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
647 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
648 chan_param.output = old_chan_param.output;
649
650 return lttng_abi_create_channel(file, &chan_param,
651 METADATA_CHANNEL);
652 }
38d024ae 653 case LTTNG_KERNEL_METADATA:
6dccd6c1 654 {
6dccd6c1
JD
655 if (copy_from_user(&chan_param,
656 (struct lttng_kernel_channel __user *) arg,
657 sizeof(struct lttng_kernel_channel)))
658 return -EFAULT;
659 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 660 METADATA_CHANNEL);
6dccd6c1 661 }
e0130fab 662 case LTTNG_KERNEL_SESSION_TRACK_PID:
d1f652f8 663 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
e0130fab 664 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
d1f652f8
MD
665 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
666 case LTTNG_KERNEL_SESSION_TRACK_ID:
667 {
668 struct lttng_kernel_tracker_args tracker;
669 enum tracker_type tracker_type;
670
671 if (copy_from_user(&tracker,
672 (struct lttng_kernel_tracker_args __user *) arg,
673 sizeof(struct lttng_kernel_tracker_args)))
674 return -EFAULT;
675 tracker_type = get_tracker_type(&tracker);
676 if (tracker_type == TRACKER_UNKNOWN)
677 return -EINVAL;
678 return lttng_session_track_id(session, tracker_type, tracker.id);
679 }
680 case LTTNG_KERNEL_SESSION_UNTRACK_ID:
681 {
682 struct lttng_kernel_tracker_args tracker;
683 enum tracker_type tracker_type;
684
685 if (copy_from_user(&tracker,
686 (struct lttng_kernel_tracker_args __user *) arg,
687 sizeof(struct lttng_kernel_tracker_args)))
688 return -EFAULT;
689 tracker_type = get_tracker_type(&tracker);
690 if (tracker_type == TRACKER_UNKNOWN)
691 return -EINVAL;
692 return lttng_session_untrack_id(session, tracker_type,
693 tracker.id);
694 }
7e6f9ef6 695 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
d1f652f8
MD
696 return lttng_session_list_tracker_ids(session, TRACKER_PID);
697 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS:
698 {
699 struct lttng_kernel_tracker_args tracker;
700 enum tracker_type tracker_type;
701
702 if (copy_from_user(&tracker,
703 (struct lttng_kernel_tracker_args __user *) arg,
704 sizeof(struct lttng_kernel_tracker_args)))
705 return -EFAULT;
706 tracker_type = get_tracker_type(&tracker);
707 if (tracker_type == TRACKER_UNKNOWN)
708 return -EINVAL;
709 return lttng_session_list_tracker_ids(session, tracker_type);
710 }
9616f0bf
JD
711 case LTTNG_KERNEL_SESSION_METADATA_REGEN:
712 return lttng_session_metadata_regenerate(session);
601252cf
MD
713 case LTTNG_KERNEL_SESSION_STATEDUMP:
714 return lttng_session_statedump(session);
7f859fbf
JR
715 case LTTNG_KERNEL_SESSION_SET_NAME:
716 {
717 struct lttng_kernel_session_name name;
718
719 if (copy_from_user(&name,
720 (struct lttng_kernel_session_name __user *) arg,
721 sizeof(struct lttng_kernel_session_name)))
722 return -EFAULT;
723 return lttng_abi_session_set_name(session, &name);
724 }
1c88f269
JR
725 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME:
726 {
727 struct lttng_kernel_session_creation_time time;
728
729 if (copy_from_user(&time,
730 (struct lttng_kernel_session_creation_time __user *) arg,
731 sizeof(struct lttng_kernel_session_creation_time)))
732 return -EFAULT;
733 return lttng_abi_session_set_creation_time(session, &time);
734 }
ad1c05e1
MD
735 default:
736 return -ENOIOCTLCMD;
737 }
738}
739
03037b98
MD
740/*
741 * Called when the last file reference is dropped.
742 *
743 * Big fat note: channels and events are invariant for the whole session after
744 * their creation. So this session destruction also destroys all channel and
745 * event structures specific to this session (they are not destroyed when their
746 * individual file is released).
747 */
ad1c05e1 748static
03037b98 749int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 750{
a90917c3 751 struct lttng_session *session = file->private_data;
c269fff4
MD
752
753 if (session)
a90917c3 754 lttng_session_destroy(session);
11b5a3c2 755 return 0;
ad1c05e1 756}
ad1c05e1
MD
757
758static const struct file_operations lttng_session_fops = {
a33c9927 759 .owner = THIS_MODULE,
03037b98 760 .release = lttng_session_release,
ad1c05e1
MD
761 .unlocked_ioctl = lttng_session_ioctl,
762#ifdef CONFIG_COMPAT
03037b98 763 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 764#endif
11b5a3c2 765};
ad1c05e1 766
d83004aa
JD
767/**
768 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
769 * @filp: the file
770 * @wait: poll table
771 *
772 * Handles the poll operations for the metadata channels.
773 */
ad1c05e1 774static
d83004aa
JD
775unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
776 poll_table *wait)
777{
778 struct lttng_metadata_stream *stream = filp->private_data;
779 struct lib_ring_buffer *buf = stream->priv;
780 int finalized;
781 unsigned int mask = 0;
782
783 if (filp->f_mode & FMODE_READ) {
784 poll_wait_set_exclusive(wait);
785 poll_wait(filp, &stream->read_wait, wait);
786
787 finalized = stream->finalized;
788
789 /*
790 * lib_ring_buffer_is_finalized() contains a smp_rmb()
791 * ordering finalized load before offsets loads.
792 */
793 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
794
795 if (finalized)
796 mask |= POLLHUP;
797
92d9f5e6 798 mutex_lock(&stream->metadata_cache->lock);
d83004aa 799 if (stream->metadata_cache->metadata_written >
f613e3e6 800 stream->metadata_out)
d83004aa 801 mask |= POLLIN;
92d9f5e6 802 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
803 }
804
805 return mask;
806}
807
f613e3e6
MD
808static
809void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
810 unsigned int cmd, unsigned long arg)
811{
812 struct lttng_metadata_stream *stream = filp->private_data;
813
814 stream->metadata_out = stream->metadata_in;
815}
816
d1344afa
JD
817/*
818 * Reset the counter of how much metadata has been consumed to 0. That way,
819 * the consumer receives the content of the metadata cache unchanged. This is
820 * different from the metadata_regenerate where the offset from epoch is
821 * resampled, here we want the exact same content as the last time the metadata
822 * was generated. This command is only possible if all the metadata written
823 * in the cache has been output to the metadata stream to avoid corrupting the
824 * metadata file.
825 *
826 * Return 0 on success, a negative value on error.
827 */
828static
829int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
830{
831 int ret;
832 struct lttng_metadata_cache *cache = stream->metadata_cache;
833
834 mutex_lock(&cache->lock);
835 if (stream->metadata_out != cache->metadata_written) {
836 ret = -EBUSY;
837 goto end;
838 }
839 stream->metadata_out = 0;
840 stream->metadata_in = 0;
841 wake_up_interruptible(&stream->read_wait);
842 ret = 0;
843
844end:
845 mutex_unlock(&cache->lock);
846 return ret;
847}
848
d83004aa
JD
849static
850long lttng_metadata_ring_buffer_ioctl(struct file *filp,
851 unsigned int cmd, unsigned long arg)
852{
853 int ret;
854 struct lttng_metadata_stream *stream = filp->private_data;
855 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
856 unsigned int rb_cmd;
857 bool coherent;
858
859 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
860 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
861 else
862 rb_cmd = cmd;
d83004aa
JD
863
864 switch (cmd) {
d83004aa
JD
865 case RING_BUFFER_GET_NEXT_SUBBUF:
866 {
35097f36
JD
867 struct lttng_metadata_stream *stream = filp->private_data;
868 struct lib_ring_buffer *buf = stream->priv;
869 struct channel *chan = buf->backend.chan;
870
8b97fd42 871 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
872 if (ret > 0) {
873 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
874 ret = 0;
875 } else if (ret < 0)
d83004aa
JD
876 goto err;
877 break;
878 }
f613e3e6
MD
879 case RING_BUFFER_GET_SUBBUF:
880 {
881 /*
882 * Random access is not allowed for metadata channel.
883 */
884 return -ENOSYS;
885 }
c6f05468 886 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
35097f36
JD
887 case RING_BUFFER_FLUSH:
888 {
889 struct lttng_metadata_stream *stream = filp->private_data;
890 struct lib_ring_buffer *buf = stream->priv;
891 struct channel *chan = buf->backend.chan;
892
893 /*
894 * Before doing the actual ring buffer flush, write up to one
895 * packet of metadata in the ring buffer.
896 */
8b97fd42 897 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
898 if (ret < 0)
899 goto err;
900 break;
901 }
9616f0bf
JD
902 case RING_BUFFER_GET_METADATA_VERSION:
903 {
904 struct lttng_metadata_stream *stream = filp->private_data;
905
906 return put_u64(stream->version, arg);
907 }
d1344afa
JD
908 case RING_BUFFER_METADATA_CACHE_DUMP:
909 {
910 struct lttng_metadata_stream *stream = filp->private_data;
911
912 return lttng_metadata_cache_dump(stream);
913 }
8b97fd42
MD
914 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
915 {
916 struct lttng_metadata_stream *stream = filp->private_data;
917 struct lib_ring_buffer *buf = stream->priv;
918 struct channel *chan = buf->backend.chan;
919
920 ret = lttng_metadata_output_channel(stream, chan, &coherent);
921 if (ret > 0) {
922 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
923 ret = 0;
924 } else if (ret < 0) {
925 goto err;
926 }
927 break;
928 }
d83004aa
JD
929 default:
930 break;
931 }
f613e3e6
MD
932 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
933
d83004aa 934 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 935 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
936 if (ret < 0)
937 goto err;
d83004aa 938
f613e3e6
MD
939 switch (cmd) {
940 case RING_BUFFER_PUT_NEXT_SUBBUF:
941 {
942 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
943 cmd, arg);
944 break;
945 }
8b97fd42
MD
946 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
947 {
948 return put_u32(coherent, arg);
949 }
f613e3e6
MD
950 default:
951 break;
952 }
d83004aa
JD
953err:
954 return ret;
955}
956
aeb9064d 957#ifdef CONFIG_COMPAT
d83004aa
JD
958static
959long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
960 unsigned int cmd, unsigned long arg)
961{
962 int ret;
963 struct lttng_metadata_stream *stream = filp->private_data;
964 struct lib_ring_buffer *buf = stream->priv;
8b97fd42
MD
965 unsigned int rb_cmd;
966 bool coherent;
967
968 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
969 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
970 else
971 rb_cmd = cmd;
d83004aa
JD
972
973 switch (cmd) {
d83004aa
JD
974 case RING_BUFFER_GET_NEXT_SUBBUF:
975 {
35097f36
JD
976 struct lttng_metadata_stream *stream = filp->private_data;
977 struct lib_ring_buffer *buf = stream->priv;
978 struct channel *chan = buf->backend.chan;
979
8b97fd42 980 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
981 if (ret > 0) {
982 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
983 ret = 0;
984 } else if (ret < 0)
d83004aa
JD
985 goto err;
986 break;
987 }
f613e3e6
MD
988 case RING_BUFFER_GET_SUBBUF:
989 {
990 /*
991 * Random access is not allowed for metadata channel.
992 */
993 return -ENOSYS;
994 }
c6f05468 995 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
96c55c2f
MD
996 case RING_BUFFER_FLUSH:
997 {
998 struct lttng_metadata_stream *stream = filp->private_data;
999 struct lib_ring_buffer *buf = stream->priv;
1000 struct channel *chan = buf->backend.chan;
1001
1002 /*
1003 * Before doing the actual ring buffer flush, write up to one
1004 * packet of metadata in the ring buffer.
1005 */
8b97fd42 1006 ret = lttng_metadata_output_channel(stream, chan, NULL);
96c55c2f
MD
1007 if (ret < 0)
1008 goto err;
1009 break;
1010 }
1011 case RING_BUFFER_GET_METADATA_VERSION:
1012 {
1013 struct lttng_metadata_stream *stream = filp->private_data;
1014
1015 return put_u64(stream->version, arg);
1016 }
d1344afa
JD
1017 case RING_BUFFER_METADATA_CACHE_DUMP:
1018 {
1019 struct lttng_metadata_stream *stream = filp->private_data;
1020
1021 return lttng_metadata_cache_dump(stream);
1022 }
8b97fd42
MD
1023 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1024 {
1025 struct lttng_metadata_stream *stream = filp->private_data;
1026 struct lib_ring_buffer *buf = stream->priv;
1027 struct channel *chan = buf->backend.chan;
1028
1029 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1030 if (ret > 0) {
1031 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1032 ret = 0;
1033 } else if (ret < 0) {
1034 goto err;
1035 }
1036 break;
1037 }
d83004aa
JD
1038 default:
1039 break;
1040 }
f613e3e6
MD
1041 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1042
d83004aa 1043 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1044 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1045 if (ret < 0)
1046 goto err;
d83004aa 1047
f613e3e6
MD
1048 switch (cmd) {
1049 case RING_BUFFER_PUT_NEXT_SUBBUF:
1050 {
1051 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1052 cmd, arg);
1053 break;
1054 }
8b97fd42
MD
1055 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1056 {
1057 return put_u32(coherent, arg);
1058 }
f613e3e6
MD
1059 default:
1060 break;
1061 }
d83004aa
JD
1062err:
1063 return ret;
1064}
aeb9064d 1065#endif
d83004aa 1066
b3b8072b
MD
1067/*
1068 * This is not used by anonymous file descriptors. This code is left
1069 * there if we ever want to implement an inode with open() operation.
1070 */
d83004aa
JD
1071static
1072int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1073{
1074 struct lttng_metadata_stream *stream = inode->i_private;
1075 struct lib_ring_buffer *buf = stream->priv;
1076
1077 file->private_data = buf;
b3b8072b
MD
1078 /*
1079 * Since life-time of metadata cache differs from that of
1080 * session, we need to keep our own reference on the transport.
1081 */
1082 if (!try_module_get(stream->transport->owner)) {
5a15f70c 1083 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1084 return -EBUSY;
1085 }
d83004aa
JD
1086 return lib_ring_buffer_open(inode, file, buf);
1087}
1088
1089static
1090int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1091{
1092 struct lttng_metadata_stream *stream = file->private_data;
1093 struct lib_ring_buffer *buf = stream->priv;
1094
92143b2c
MD
1095 mutex_lock(&stream->metadata_cache->lock);
1096 list_del(&stream->list);
1097 mutex_unlock(&stream->metadata_cache->lock);
d83004aa 1098 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 1099 module_put(stream->transport->owner);
92143b2c 1100 kfree(stream);
d83004aa
JD
1101 return lib_ring_buffer_release(inode, file, buf);
1102}
1103
1104static
1105ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1106 struct pipe_inode_info *pipe, size_t len,
1107 unsigned int flags)
1108{
1109 struct lttng_metadata_stream *stream = in->private_data;
1110 struct lib_ring_buffer *buf = stream->priv;
1111
1112 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1113 flags, buf);
1114}
1115
1116static
1117int lttng_metadata_ring_buffer_mmap(struct file *filp,
1118 struct vm_area_struct *vma)
1119{
1120 struct lttng_metadata_stream *stream = filp->private_data;
1121 struct lib_ring_buffer *buf = stream->priv;
1122
1123 return lib_ring_buffer_mmap(filp, vma, buf);
1124}
1125
1126static
1127const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1128 .owner = THIS_MODULE,
1129 .open = lttng_metadata_ring_buffer_open,
1130 .release = lttng_metadata_ring_buffer_release,
1131 .poll = lttng_metadata_ring_buffer_poll,
1132 .splice_read = lttng_metadata_ring_buffer_splice_read,
1133 .mmap = lttng_metadata_ring_buffer_mmap,
1134 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1135 .llseek = vfs_lib_ring_buffer_no_llseek,
1136#ifdef CONFIG_COMPAT
1137 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1138#endif
1139};
1140
1141static
1142int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
a3fcd499 1143 const struct file_operations *fops, const char *name)
ad1c05e1 1144{
ad1c05e1 1145 int stream_fd, ret;
11b5a3c2 1146 struct file *stream_file;
ad1c05e1 1147
4ac10b76 1148 stream_fd = lttng_get_unused_fd();
ad1c05e1
MD
1149 if (stream_fd < 0) {
1150 ret = stream_fd;
1151 goto fd_error;
1152 }
a3fcd499 1153 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
c0e31d2e
MD
1154 if (IS_ERR(stream_file)) {
1155 ret = PTR_ERR(stream_file);
ad1c05e1
MD
1156 goto file_error;
1157 }
409453cb
MD
1158 /*
1159 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1160 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1161 * file descriptor, so we set FMODE_PREAD here.
1162 */
d7b6f197 1163 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 1164 fd_install(stream_fd, stream_file);
dda6a249
MD
1165 /*
1166 * The stream holds a reference to the channel within the generic ring
1167 * buffer library, so no need to hold a refcount on the channel and
1168 * session files here.
1169 */
ad1c05e1
MD
1170 return stream_fd;
1171
1172file_error:
1173 put_unused_fd(stream_fd);
d83004aa
JD
1174fd_error:
1175 return ret;
1176}
1177
1178static
1179int lttng_abi_open_stream(struct file *channel_file)
1180{
1181 struct lttng_channel *channel = channel_file->private_data;
1182 struct lib_ring_buffer *buf;
1183 int ret;
1184 void *stream_priv;
1185
1186 buf = channel->ops->buffer_read_open(channel->chan);
1187 if (!buf)
1188 return -ENOENT;
1189
1190 stream_priv = buf;
1191 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1192 &lttng_stream_ring_buffer_file_operations,
1193 "[lttng_stream]");
d83004aa
JD
1194 if (ret < 0)
1195 goto fd_error;
1196
1197 return ret;
1198
1199fd_error:
1200 channel->ops->buffer_read_close(buf);
1201 return ret;
1202}
1203
1204static
1205int lttng_abi_open_metadata_stream(struct file *channel_file)
1206{
1207 struct lttng_channel *channel = channel_file->private_data;
1208 struct lttng_session *session = channel->session;
1209 struct lib_ring_buffer *buf;
1210 int ret;
1211 struct lttng_metadata_stream *metadata_stream;
1212 void *stream_priv;
1213
1214 buf = channel->ops->buffer_read_open(channel->chan);
1215 if (!buf)
1216 return -ENOENT;
1217
1218 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1219 GFP_KERNEL);
b3b8072b
MD
1220 if (!metadata_stream) {
1221 ret = -ENOMEM;
1222 goto nomem;
1223 }
d83004aa
JD
1224 metadata_stream->metadata_cache = session->metadata_cache;
1225 init_waitqueue_head(&metadata_stream->read_wait);
1226 metadata_stream->priv = buf;
1227 stream_priv = metadata_stream;
b3b8072b 1228 metadata_stream->transport = channel->transport;
8b97fd42
MD
1229 /* Initial state is an empty metadata, considered as incoherent. */
1230 metadata_stream->coherent = false;
b3b8072b
MD
1231
1232 /*
1233 * Since life-time of metadata cache differs from that of
1234 * session, we need to keep our own reference on the transport.
1235 */
1236 if (!try_module_get(metadata_stream->transport->owner)) {
5a15f70c 1237 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1238 ret = -EINVAL;
1239 goto notransport;
1240 }
1241
901aaa5f
FD
1242 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
1243 ret = -EOVERFLOW;
9c1f4643 1244 goto kref_error;
901aaa5f
FD
1245 }
1246
d83004aa 1247 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1248 &lttng_metadata_ring_buffer_file_operations,
1249 "[lttng_metadata_stream]");
d83004aa
JD
1250 if (ret < 0)
1251 goto fd_error;
1252
92143b2c 1253 mutex_lock(&session->metadata_cache->lock);
d83004aa
JD
1254 list_add(&metadata_stream->list,
1255 &session->metadata_cache->metadata_stream);
92143b2c 1256 mutex_unlock(&session->metadata_cache->lock);
d83004aa
JD
1257 return ret;
1258
ad1c05e1 1259fd_error:
9c1f4643
MD
1260 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1261kref_error:
b3b8072b
MD
1262 module_put(metadata_stream->transport->owner);
1263notransport:
1264 kfree(metadata_stream);
1265nomem:
11b5a3c2 1266 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
1267 return ret;
1268}
1269
badfe9f5
MD
1270static
1271int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param)
1272{
1273 /* Limit ABI to implemented features. */
1274 switch (event_param->instrumentation) {
1275 case LTTNG_KERNEL_SYSCALL:
1276 switch (event_param->u.syscall.entryexit) {
1277 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
1278 break;
1279 default:
1280 return -EINVAL;
1281 }
1282 switch (event_param->u.syscall.abi) {
1283 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
1284 break;
1285 default:
1286 return -EINVAL;
1287 }
1288 switch (event_param->u.syscall.match) {
1289 case LTTNG_SYSCALL_MATCH_NAME:
1290 break;
1291 default:
1292 return -EINVAL;
1293 }
1294 break;
1295
1296 case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */
1297 case LTTNG_KERNEL_KPROBE: /* Fallthrough */
1298 case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */
1299 case LTTNG_KERNEL_NOOP: /* Fallthrough */
1300 case LTTNG_KERNEL_UPROBE:
1301 break;
1302
1303 case LTTNG_KERNEL_FUNCTION: /* Fallthrough */
1304 default:
1305 return -EINVAL;
1306 }
1307 return 0;
1308}
1309
653fe716 1310static
c0e31d2e 1311int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 1312 struct lttng_kernel_event *event_param)
653fe716 1313{
a90917c3 1314 struct lttng_channel *channel = channel_file->private_data;
653fe716 1315 int event_fd, ret;
11b5a3c2 1316 struct file *event_file;
3c997079 1317 void *priv;
653fe716 1318
6dccd6c1
JD
1319 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1320 switch (event_param->instrumentation) {
7371f44c 1321 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 1322 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 1323 break;
ab2277d6 1324 case LTTNG_KERNEL_KPROBE:
6dccd6c1 1325 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 1326 break;
ab2277d6 1327 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
1328 WARN_ON_ONCE(1);
1329 /* Not implemented. */
e0a7a7c4
MD
1330 break;
1331 default:
1332 break;
1333 }
33a39a3c
MD
1334 event_fd = lttng_get_unused_fd();
1335 if (event_fd < 0) {
1336 ret = event_fd;
1337 goto fd_error;
1338 }
1339 event_file = anon_inode_getfile("[lttng_event]",
1340 &lttng_event_fops,
1341 NULL, O_RDWR);
1342 if (IS_ERR(event_file)) {
1343 ret = PTR_ERR(event_file);
1344 goto file_error;
1345 }
9c1f4643 1346 /* The event holds a reference on the channel */
98d7281c 1347 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
0d2c717f 1348 ret = -EOVERFLOW;
9c1f4643
MD
1349 goto refcount_error;
1350 }
badfe9f5
MD
1351 ret = lttng_abi_validate_event_param(event_param);
1352 if (ret)
1353 goto event_error;
33a39a3c
MD
1354 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1355 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
b2bc0bc8 1356 struct lttng_event_enabler *event_enabler;
33a39a3c 1357
4993071a
PP
1358 if (strutils_is_star_glob_pattern(event_param->name)) {
1359 /*
1360 * If the event name is a star globbing pattern,
1361 * we create the special star globbing enabler.
1362 */
b2bc0bc8 1363 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
33a39a3c 1364 event_param, channel);
3c997079 1365 } else {
b2bc0bc8 1366 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
33a39a3c 1367 event_param, channel);
1ec65de1 1368 }
b2bc0bc8 1369 priv = event_enabler;
33a39a3c
MD
1370 } else {
1371 struct lttng_event *event;
4ee2453d 1372
33a39a3c
MD
1373 /*
1374 * We tolerate no failure path after event creation. It
1375 * will stay invariant for the rest of the session.
1376 */
1377 event = lttng_event_create(channel, event_param,
1378 NULL, NULL,
1379 event_param->instrumentation);
1380 WARN_ON_ONCE(!event);
1381 if (IS_ERR(event)) {
1382 ret = PTR_ERR(event);
1383 goto event_error;
80f87dd2 1384 }
33a39a3c 1385 priv = event;
03037b98 1386 }
33a39a3c
MD
1387 event_file->private_data = priv;
1388 fd_install(event_fd, event_file);
653fe716
MD
1389 return event_fd;
1390
03037b98 1391event_error:
9c1f4643
MD
1392 atomic_long_dec(&channel_file->f_count);
1393refcount_error:
c0e31d2e 1394 fput(event_file);
653fe716
MD
1395file_error:
1396 put_unused_fd(event_fd);
1397fd_error:
653fe716
MD
1398 return ret;
1399}
ad1c05e1
MD
1400
1401/**
1402 * lttng_channel_ioctl - lttng syscall through ioctl
1403 *
c0e31d2e 1404 * @file: the file
ad1c05e1
MD
1405 * @cmd: the command
1406 * @arg: command arg
1407 *
1408 * This ioctl implements lttng commands:
38d024ae 1409 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
1410 * Returns an event stream file descriptor or failure.
1411 * (typically, one event stream records events from one CPU)
38d024ae 1412 * LTTNG_KERNEL_EVENT
ad1c05e1 1413 * Returns an event file descriptor or failure.
8070f5c0
MD
1414 * LTTNG_KERNEL_CONTEXT
1415 * Prepend a context field to each event in the channel
e64957da
MD
1416 * LTTNG_KERNEL_ENABLE
1417 * Enable recording for events in this channel (weak enable)
1418 * LTTNG_KERNEL_DISABLE
1419 * Disable recording for events in this channel (strong disable)
baf20995 1420 *
baf20995
MD
1421 * Channel and event file descriptors also hold a reference on the session.
1422 */
ad1c05e1 1423static
c0e31d2e 1424long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 1425{
a90917c3 1426 struct lttng_channel *channel = file->private_data;
8070f5c0 1427
baf20995 1428 switch (cmd) {
6dccd6c1 1429 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1430 case LTTNG_KERNEL_STREAM:
c0e31d2e 1431 return lttng_abi_open_stream(file);
6dccd6c1
JD
1432 case LTTNG_KERNEL_OLD_EVENT:
1433 {
1434 struct lttng_kernel_event *uevent_param;
1435 struct lttng_kernel_old_event *old_uevent_param;
1436 int ret;
1437
1438 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1439 GFP_KERNEL);
1440 if (!uevent_param) {
1441 ret = -ENOMEM;
1442 goto old_event_end;
1443 }
1444 old_uevent_param = kmalloc(
1445 sizeof(struct lttng_kernel_old_event),
1446 GFP_KERNEL);
1447 if (!old_uevent_param) {
1448 ret = -ENOMEM;
1449 goto old_event_error_free_param;
1450 }
1451 if (copy_from_user(old_uevent_param,
1452 (struct lttng_kernel_old_event __user *) arg,
1453 sizeof(struct lttng_kernel_old_event))) {
1454 ret = -EFAULT;
1455 goto old_event_error_free_old_param;
1456 }
1457
1458 memcpy(uevent_param->name, old_uevent_param->name,
1459 sizeof(uevent_param->name));
1460 uevent_param->instrumentation =
1461 old_uevent_param->instrumentation;
1462
1463 switch (old_uevent_param->instrumentation) {
1464 case LTTNG_KERNEL_KPROBE:
1465 uevent_param->u.kprobe.addr =
1466 old_uevent_param->u.kprobe.addr;
1467 uevent_param->u.kprobe.offset =
1468 old_uevent_param->u.kprobe.offset;
1469 memcpy(uevent_param->u.kprobe.symbol_name,
1470 old_uevent_param->u.kprobe.symbol_name,
1471 sizeof(uevent_param->u.kprobe.symbol_name));
1472 break;
1473 case LTTNG_KERNEL_KRETPROBE:
1474 uevent_param->u.kretprobe.addr =
1475 old_uevent_param->u.kretprobe.addr;
1476 uevent_param->u.kretprobe.offset =
1477 old_uevent_param->u.kretprobe.offset;
1478 memcpy(uevent_param->u.kretprobe.symbol_name,
1479 old_uevent_param->u.kretprobe.symbol_name,
1480 sizeof(uevent_param->u.kretprobe.symbol_name));
1481 break;
1482 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
1483 WARN_ON_ONCE(1);
1484 /* Not implemented. */
6dccd6c1
JD
1485 break;
1486 default:
1487 break;
1488 }
1489 ret = lttng_abi_create_event(file, uevent_param);
1490
1491old_event_error_free_old_param:
1492 kfree(old_uevent_param);
1493old_event_error_free_param:
1494 kfree(uevent_param);
1495old_event_end:
1496 return ret;
1497 }
38d024ae 1498 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
1499 {
1500 struct lttng_kernel_event uevent_param;
1501
1502 if (copy_from_user(&uevent_param,
1503 (struct lttng_kernel_event __user *) arg,
1504 sizeof(uevent_param)))
1505 return -EFAULT;
1506 return lttng_abi_create_event(file, &uevent_param);
1507 }
1508 case LTTNG_KERNEL_OLD_CONTEXT:
1509 {
1510 struct lttng_kernel_context *ucontext_param;
1511 struct lttng_kernel_old_context *old_ucontext_param;
1512 int ret;
1513
1514 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1515 GFP_KERNEL);
1516 if (!ucontext_param) {
1517 ret = -ENOMEM;
1518 goto old_ctx_end;
1519 }
1520 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1521 GFP_KERNEL);
1522 if (!old_ucontext_param) {
1523 ret = -ENOMEM;
1524 goto old_ctx_error_free_param;
1525 }
1526
1527 if (copy_from_user(old_ucontext_param,
1528 (struct lttng_kernel_old_context __user *) arg,
1529 sizeof(struct lttng_kernel_old_context))) {
1530 ret = -EFAULT;
1531 goto old_ctx_error_free_old_param;
1532 }
1533 ucontext_param->ctx = old_ucontext_param->ctx;
1534 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1535 sizeof(ucontext_param->padding));
1536 /* only type that uses the union */
1537 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1538 ucontext_param->u.perf_counter.type =
1539 old_ucontext_param->u.perf_counter.type;
1540 ucontext_param->u.perf_counter.config =
1541 old_ucontext_param->u.perf_counter.config;
1542 memcpy(ucontext_param->u.perf_counter.name,
1543 old_ucontext_param->u.perf_counter.name,
1544 sizeof(ucontext_param->u.perf_counter.name));
1545 }
1546
1547 ret = lttng_abi_add_context(file,
1548 ucontext_param,
1549 &channel->ctx, channel->session);
1550
1551old_ctx_error_free_old_param:
1552 kfree(old_ucontext_param);
1553old_ctx_error_free_param:
1554 kfree(ucontext_param);
1555old_ctx_end:
1556 return ret;
1557 }
8070f5c0 1558 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
1559 {
1560 struct lttng_kernel_context ucontext_param;
1561
1562 if (copy_from_user(&ucontext_param,
8070f5c0 1563 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
1564 sizeof(ucontext_param)))
1565 return -EFAULT;
1566 return lttng_abi_add_context(file,
1567 &ucontext_param,
8070f5c0 1568 &channel->ctx, channel->session);
6dccd6c1
JD
1569 }
1570 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1571 case LTTNG_KERNEL_ENABLE:
a90917c3 1572 return lttng_channel_enable(channel);
6dccd6c1 1573 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1574 case LTTNG_KERNEL_DISABLE:
a90917c3 1575 return lttng_channel_disable(channel);
12e579db
MD
1576 case LTTNG_KERNEL_SYSCALL_MASK:
1577 return lttng_channel_syscall_mask(channel,
1578 (struct lttng_kernel_syscall_mask __user *) arg);
baf20995
MD
1579 default:
1580 return -ENOIOCTLCMD;
1581 }
1582}
1583
5dbbdb43
MD
1584/**
1585 * lttng_metadata_ioctl - lttng syscall through ioctl
1586 *
1587 * @file: the file
1588 * @cmd: the command
1589 * @arg: command arg
1590 *
1591 * This ioctl implements lttng commands:
38d024ae 1592 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
1593 * Returns an event stream file descriptor or failure.
1594 *
1595 * Channel and event file descriptors also hold a reference on the session.
1596 */
1597static
1598long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1599{
1600 switch (cmd) {
6dccd6c1 1601 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1602 case LTTNG_KERNEL_STREAM:
d83004aa 1603 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
1604 default:
1605 return -ENOIOCTLCMD;
1606 }
1607}
1608
653fe716
MD
1609/**
1610 * lttng_channel_poll - lttng stream addition/removal monitoring
1611 *
c0e31d2e 1612 * @file: the file
653fe716
MD
1613 * @wait: poll table
1614 */
c0e31d2e 1615unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 1616{
a90917c3 1617 struct lttng_channel *channel = file->private_data;
653fe716
MD
1618 unsigned int mask = 0;
1619
c0e31d2e 1620 if (file->f_mode & FMODE_READ) {
a33e44a6 1621 poll_wait_set_exclusive(wait);
24cedcfe
MD
1622 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1623 wait);
653fe716 1624
254ec7bc
MD
1625 if (channel->ops->is_disabled(channel->chan))
1626 return POLLERR;
24cedcfe 1627 if (channel->ops->is_finalized(channel->chan))
653fe716 1628 return POLLHUP;
f71ecafa 1629 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 1630 return POLLIN | POLLRDNORM;
f71ecafa 1631 return 0;
653fe716
MD
1632 }
1633 return mask;
1634
1635}
1636
0a84a57f
MD
1637static
1638int lttng_channel_release(struct inode *inode, struct file *file)
1639{
a90917c3 1640 struct lttng_channel *channel = file->private_data;
c269fff4
MD
1641
1642 if (channel)
1643 fput(channel->session->file);
0a84a57f
MD
1644 return 0;
1645}
1646
d83004aa
JD
1647static
1648int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1649{
1650 struct lttng_channel *channel = file->private_data;
1651
1652 if (channel) {
d83004aa 1653 fput(channel->session->file);
a3381417 1654 lttng_metadata_channel_destroy(channel);
d83004aa
JD
1655 }
1656
1657 return 0;
1658}
1659
ad1c05e1 1660static const struct file_operations lttng_channel_fops = {
a33c9927 1661 .owner = THIS_MODULE,
03037b98 1662 .release = lttng_channel_release,
653fe716 1663 .poll = lttng_channel_poll,
ad1c05e1 1664 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 1665#ifdef CONFIG_COMPAT
03037b98 1666 .compat_ioctl = lttng_channel_ioctl,
baf20995 1667#endif
11b5a3c2 1668};
baf20995 1669
5dbbdb43 1670static const struct file_operations lttng_metadata_fops = {
a33c9927 1671 .owner = THIS_MODULE,
d83004aa 1672 .release = lttng_metadata_channel_release,
5dbbdb43
MD
1673 .unlocked_ioctl = lttng_metadata_ioctl,
1674#ifdef CONFIG_COMPAT
1675 .compat_ioctl = lttng_metadata_ioctl,
1676#endif
1677};
1678
8070f5c0
MD
1679/**
1680 * lttng_event_ioctl - lttng syscall through ioctl
1681 *
1682 * @file: the file
1683 * @cmd: the command
1684 * @arg: command arg
1685 *
1686 * This ioctl implements lttng commands:
8070f5c0
MD
1687 * LTTNG_KERNEL_CONTEXT
1688 * Prepend a context field to each record of this event
e64957da
MD
1689 * LTTNG_KERNEL_ENABLE
1690 * Enable recording for this event (weak enable)
1691 * LTTNG_KERNEL_DISABLE
1692 * Disable recording for this event (strong disable)
8070f5c0
MD
1693 */
1694static
1695long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1696{
3c997079 1697 struct lttng_event *event;
b2bc0bc8 1698 struct lttng_event_enabler *event_enabler;
3c997079 1699 enum lttng_event_type *evtype = file->private_data;
8070f5c0
MD
1700
1701 switch (cmd) {
6dccd6c1
JD
1702 case LTTNG_KERNEL_OLD_CONTEXT:
1703 {
3c997079
MD
1704 /* Not implemented */
1705 return -ENOSYS;
6dccd6c1 1706 }
8070f5c0 1707 case LTTNG_KERNEL_CONTEXT:
6dccd6c1 1708 {
3c997079
MD
1709 /* Not implemented */
1710 return -ENOSYS;
6dccd6c1
JD
1711 }
1712 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1713 case LTTNG_KERNEL_ENABLE:
3c997079
MD
1714 switch (*evtype) {
1715 case LTTNG_TYPE_EVENT:
1716 event = file->private_data;
1717 return lttng_event_enable(event);
1718 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
1719 event_enabler = file->private_data;
1720 return lttng_event_enabler_enable(event_enabler);
3c997079
MD
1721 default:
1722 WARN_ON_ONCE(1);
1723 return -ENOSYS;
1724 }
6dccd6c1 1725 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1726 case LTTNG_KERNEL_DISABLE:
3c997079
MD
1727 switch (*evtype) {
1728 case LTTNG_TYPE_EVENT:
1729 event = file->private_data;
1730 return lttng_event_disable(event);
1731 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
1732 event_enabler = file->private_data;
1733 return lttng_event_enabler_disable(event_enabler);
3c997079
MD
1734 default:
1735 WARN_ON_ONCE(1);
1736 return -ENOSYS;
1737 }
07dfc1d0
MD
1738 case LTTNG_KERNEL_FILTER:
1739 switch (*evtype) {
1740 case LTTNG_TYPE_EVENT:
1741 return -EINVAL;
1742 case LTTNG_TYPE_ENABLER:
1743 {
b2bc0bc8
FD
1744 event_enabler = file->private_data;
1745 return lttng_event_enabler_attach_bytecode(event_enabler,
07dfc1d0
MD
1746 (struct lttng_kernel_filter_bytecode __user *) arg);
1747 }
0586316f
MD
1748 default:
1749 WARN_ON_ONCE(1);
1750 return -ENOSYS;
07dfc1d0 1751 }
3aed4dca
FD
1752 case LTTNG_KERNEL_ADD_CALLSITE:
1753 switch (*evtype) {
1754 case LTTNG_TYPE_EVENT:
1755 event = file->private_data;
1756 return lttng_event_add_callsite(event,
1757 (struct lttng_kernel_event_callsite __user *) arg);
1758 case LTTNG_TYPE_ENABLER:
1759 return -EINVAL;
64c147c0
MD
1760 default:
1761 WARN_ON_ONCE(1);
1762 return -ENOSYS;
3aed4dca 1763 }
8070f5c0
MD
1764 default:
1765 return -ENOIOCTLCMD;
1766 }
1767}
1768
0a84a57f
MD
1769static
1770int lttng_event_release(struct inode *inode, struct file *file)
1771{
3c997079 1772 struct lttng_event *event;
b2bc0bc8 1773 struct lttng_event_enabler *event_enabler;
3c997079
MD
1774 enum lttng_event_type *evtype = file->private_data;
1775
1776 if (!evtype)
1777 return 0;
1778
1779 switch (*evtype) {
1780 case LTTNG_TYPE_EVENT:
1781 event = file->private_data;
1782 if (event)
1783 fput(event->chan->file);
1784 break;
1785 case LTTNG_TYPE_ENABLER:
b2bc0bc8
FD
1786 event_enabler = file->private_data;
1787 if (event_enabler)
1788 fput(event_enabler->chan->file);
3c997079
MD
1789 break;
1790 default:
1791 WARN_ON_ONCE(1);
1792 break;
1793 }
c269fff4 1794
0a84a57f
MD
1795 return 0;
1796}
1797
3b923e5b 1798/* TODO: filter control ioctl */
0a84a57f 1799static const struct file_operations lttng_event_fops = {
a33c9927 1800 .owner = THIS_MODULE,
0a84a57f 1801 .release = lttng_event_release,
8070f5c0
MD
1802 .unlocked_ioctl = lttng_event_ioctl,
1803#ifdef CONFIG_COMPAT
1804 .compat_ioctl = lttng_event_ioctl,
1805#endif
11b5a3c2 1806};
0a84a57f 1807
3b731ab1
JD
1808static int put_u64(uint64_t val, unsigned long arg)
1809{
1810 return put_user(val, (uint64_t __user *) arg);
1811}
1812
8b97fd42
MD
1813static int put_u32(uint32_t val, unsigned long arg)
1814{
1815 return put_user(val, (uint32_t __user *) arg);
1816}
1817
ed8d02d6
JD
1818static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1819 unsigned int cmd, unsigned long arg)
1820{
3b731ab1
JD
1821 struct lib_ring_buffer *buf = filp->private_data;
1822 struct channel *chan = buf->backend.chan;
1823 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 1824 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
1825 int ret;
1826
1827 if (atomic_read(&chan->record_disabled))
1828 return -EIO;
1829
ed8d02d6 1830 switch (cmd) {
3b731ab1
JD
1831 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1832 {
1833 uint64_t ts;
1834
dd5a0db3 1835 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
1836 if (ret < 0)
1837 goto error;
1838 return put_u64(ts, arg);
1839 }
1840 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1841 {
1842 uint64_t ts;
1843
dd5a0db3 1844 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
1845 if (ret < 0)
1846 goto error;
1847 return put_u64(ts, arg);
1848 }
1849 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1850 {
1851 uint64_t ed;
1852
dd5a0db3 1853 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
1854 if (ret < 0)
1855 goto error;
1856 return put_u64(ed, arg);
1857 }
1858 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1859 {
1860 uint64_t cs;
1861
dd5a0db3 1862 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
1863 if (ret < 0)
1864 goto error;
1865 return put_u64(cs, arg);
1866 }
1867 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1868 {
1869 uint64_t ps;
1870
dd5a0db3 1871 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
1872 if (ret < 0)
1873 goto error;
1874 return put_u64(ps, arg);
1875 }
1876 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1877 {
1878 uint64_t si;
1879
dd5a0db3 1880 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
1881 if (ret < 0)
1882 goto error;
1883 return put_u64(si, arg);
1884 }
2348ca17
JD
1885 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1886 {
1887 uint64_t ts;
1888
dd5a0db3 1889 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
1890 if (ret < 0)
1891 goto error;
1892 return put_u64(ts, arg);
1893 }
5b3cf4f9
JD
1894 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1895 {
1896 uint64_t seq;
1897
1898 ret = ops->sequence_number(config, buf, &seq);
1899 if (ret < 0)
1900 goto error;
1901 return put_u64(seq, arg);
1902 }
5594698f
JD
1903 case LTTNG_RING_BUFFER_INSTANCE_ID:
1904 {
1905 uint64_t id;
1906
1907 ret = ops->instance_id(config, buf, &id);
1908 if (ret < 0)
1909 goto error;
1910 return put_u64(id, arg);
1911 }
3b731ab1
JD
1912 default:
1913 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1914 cmd, arg);
ed8d02d6 1915 }
3b731ab1
JD
1916
1917error:
1918 return -ENOSYS;
ed8d02d6
JD
1919}
1920
1921#ifdef CONFIG_COMPAT
1922static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1923 unsigned int cmd, unsigned long arg)
1924{
3b731ab1
JD
1925 struct lib_ring_buffer *buf = filp->private_data;
1926 struct channel *chan = buf->backend.chan;
1927 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 1928 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
1929 int ret;
1930
1931 if (atomic_read(&chan->record_disabled))
1932 return -EIO;
1933
ed8d02d6 1934 switch (cmd) {
3b731ab1
JD
1935 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1936 {
1937 uint64_t ts;
1938
dd5a0db3 1939 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
1940 if (ret < 0)
1941 goto error;
1942 return put_u64(ts, arg);
1943 }
1944 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1945 {
1946 uint64_t ts;
1947
dd5a0db3 1948 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
1949 if (ret < 0)
1950 goto error;
1951 return put_u64(ts, arg);
1952 }
1953 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1954 {
1955 uint64_t ed;
1956
dd5a0db3 1957 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
1958 if (ret < 0)
1959 goto error;
1960 return put_u64(ed, arg);
ed8d02d6 1961 }
3b731ab1
JD
1962 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1963 {
1964 uint64_t cs;
1965
dd5a0db3 1966 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
1967 if (ret < 0)
1968 goto error;
1969 return put_u64(cs, arg);
1970 }
1971 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1972 {
1973 uint64_t ps;
1974
dd5a0db3 1975 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
1976 if (ret < 0)
1977 goto error;
1978 return put_u64(ps, arg);
1979 }
1980 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1981 {
1982 uint64_t si;
1983
dd5a0db3 1984 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
1985 if (ret < 0)
1986 goto error;
1987 return put_u64(si, arg);
1988 }
2348ca17
JD
1989 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1990 {
1991 uint64_t ts;
1992
dd5a0db3 1993 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
1994 if (ret < 0)
1995 goto error;
1996 return put_u64(ts, arg);
1997 }
5b3cf4f9
JD
1998 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1999 {
2000 uint64_t seq;
2001
2002 ret = ops->sequence_number(config, buf, &seq);
2003 if (ret < 0)
2004 goto error;
2005 return put_u64(seq, arg);
2006 }
5594698f
JD
2007 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
2008 {
2009 uint64_t id;
2010
2011 ret = ops->instance_id(config, buf, &id);
2012 if (ret < 0)
2013 goto error;
2014 return put_u64(id, arg);
2015 }
3b731ab1
JD
2016 default:
2017 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2018 cmd, arg);
2019 }
2020
2021error:
2022 return -ENOSYS;
ed8d02d6
JD
2023}
2024#endif /* CONFIG_COMPAT */
2025
2026static void lttng_stream_override_ring_buffer_fops(void)
2027{
2028 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2029 lttng_stream_ring_buffer_file_operations.open =
2030 lib_ring_buffer_file_operations.open;
2031 lttng_stream_ring_buffer_file_operations.release =
2032 lib_ring_buffer_file_operations.release;
2033 lttng_stream_ring_buffer_file_operations.poll =
2034 lib_ring_buffer_file_operations.poll;
2035 lttng_stream_ring_buffer_file_operations.splice_read =
2036 lib_ring_buffer_file_operations.splice_read;
2037 lttng_stream_ring_buffer_file_operations.mmap =
2038 lib_ring_buffer_file_operations.mmap;
2039 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2040 lttng_stream_ring_buffer_ioctl;
2041 lttng_stream_ring_buffer_file_operations.llseek =
2042 lib_ring_buffer_file_operations.llseek;
2043#ifdef CONFIG_COMPAT
2044 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2045 lttng_stream_ring_buffer_compat_ioctl;
2046#endif
2047}
2048
80996790 2049int __init lttng_abi_init(void)
baf20995
MD
2050{
2051 int ret = 0;
2052
263b6c88 2053 wrapper_vmalloc_sync_mappings();
2754583e 2054 lttng_clock_ref();
f771eda6
JD
2055
2056 ret = lttng_tp_mempool_init();
2057 if (ret) {
2058 goto error;
2059 }
2060
d29348f7 2061 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 2062 &lttng_proc_ops, NULL);
2470a237 2063
255e52a4 2064 if (!lttng_proc_dentry) {
5a15f70c 2065 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
2066 ret = -ENOMEM;
2067 goto error;
2068 }
ed8d02d6 2069 lttng_stream_override_ring_buffer_fops();
2754583e 2070 return 0;
ed8d02d6 2071
baf20995 2072error:
f771eda6 2073 lttng_tp_mempool_destroy();
2754583e 2074 lttng_clock_unref();
baf20995
MD
2075 return ret;
2076}
2077
e6e65fcd
MD
2078/* No __exit annotation because used by init error path too. */
2079void lttng_abi_exit(void)
baf20995 2080{
f771eda6 2081 lttng_tp_mempool_destroy();
2754583e 2082 lttng_clock_unref();
e6a17f26
MD
2083 if (lttng_proc_dentry)
2084 remove_proc_entry("lttng", NULL);
baf20995 2085}
This page took 0.155753 seconds and 4 git commands to generate.