Namespace all logging statements
[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,
1143 const struct file_operations *fops)
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 }
d83004aa
JD
1153 stream_file = anon_inode_getfile("[lttng_stream]", fops,
1154 stream_priv, O_RDWR);
c0e31d2e
MD
1155 if (IS_ERR(stream_file)) {
1156 ret = PTR_ERR(stream_file);
ad1c05e1
MD
1157 goto file_error;
1158 }
409453cb
MD
1159 /*
1160 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1161 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1162 * file descriptor, so we set FMODE_PREAD here.
1163 */
d7b6f197 1164 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 1165 fd_install(stream_fd, stream_file);
dda6a249
MD
1166 /*
1167 * The stream holds a reference to the channel within the generic ring
1168 * buffer library, so no need to hold a refcount on the channel and
1169 * session files here.
1170 */
ad1c05e1
MD
1171 return stream_fd;
1172
1173file_error:
1174 put_unused_fd(stream_fd);
d83004aa
JD
1175fd_error:
1176 return ret;
1177}
1178
1179static
1180int lttng_abi_open_stream(struct file *channel_file)
1181{
1182 struct lttng_channel *channel = channel_file->private_data;
1183 struct lib_ring_buffer *buf;
1184 int ret;
1185 void *stream_priv;
1186
1187 buf = channel->ops->buffer_read_open(channel->chan);
1188 if (!buf)
1189 return -ENOENT;
1190
1191 stream_priv = buf;
1192 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
ed8d02d6 1193 &lttng_stream_ring_buffer_file_operations);
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
JD
1247 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1248 &lttng_metadata_ring_buffer_file_operations);
1249 if (ret < 0)
1250 goto fd_error;
1251
92143b2c 1252 mutex_lock(&session->metadata_cache->lock);
d83004aa
JD
1253 list_add(&metadata_stream->list,
1254 &session->metadata_cache->metadata_stream);
92143b2c 1255 mutex_unlock(&session->metadata_cache->lock);
d83004aa
JD
1256 return ret;
1257
ad1c05e1 1258fd_error:
9c1f4643
MD
1259 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1260kref_error:
b3b8072b
MD
1261 module_put(metadata_stream->transport->owner);
1262notransport:
1263 kfree(metadata_stream);
1264nomem:
11b5a3c2 1265 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
1266 return ret;
1267}
1268
653fe716 1269static
c0e31d2e 1270int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 1271 struct lttng_kernel_event *event_param)
653fe716 1272{
a90917c3 1273 struct lttng_channel *channel = channel_file->private_data;
653fe716 1274 int event_fd, ret;
11b5a3c2 1275 struct file *event_file;
3c997079 1276 void *priv;
653fe716 1277
6dccd6c1
JD
1278 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1279 switch (event_param->instrumentation) {
7371f44c 1280 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 1281 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 1282 break;
ab2277d6 1283 case LTTNG_KERNEL_KPROBE:
6dccd6c1 1284 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 1285 break;
ab2277d6 1286 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
1287 WARN_ON_ONCE(1);
1288 /* Not implemented. */
e0a7a7c4
MD
1289 break;
1290 default:
1291 break;
1292 }
33a39a3c
MD
1293 event_fd = lttng_get_unused_fd();
1294 if (event_fd < 0) {
1295 ret = event_fd;
1296 goto fd_error;
1297 }
1298 event_file = anon_inode_getfile("[lttng_event]",
1299 &lttng_event_fops,
1300 NULL, O_RDWR);
1301 if (IS_ERR(event_file)) {
1302 ret = PTR_ERR(event_file);
1303 goto file_error;
1304 }
9c1f4643 1305 /* The event holds a reference on the channel */
98d7281c 1306 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
0d2c717f 1307 ret = -EOVERFLOW;
9c1f4643
MD
1308 goto refcount_error;
1309 }
33a39a3c
MD
1310 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1311 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1312 struct lttng_enabler *enabler;
1313
4993071a
PP
1314 if (strutils_is_star_glob_pattern(event_param->name)) {
1315 /*
1316 * If the event name is a star globbing pattern,
1317 * we create the special star globbing enabler.
1318 */
1319 enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB,
33a39a3c 1320 event_param, channel);
3c997079 1321 } else {
33a39a3c
MD
1322 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
1323 event_param, channel);
1ec65de1 1324 }
33a39a3c
MD
1325 priv = enabler;
1326 } else {
1327 struct lttng_event *event;
4ee2453d 1328
33a39a3c
MD
1329 /*
1330 * We tolerate no failure path after event creation. It
1331 * will stay invariant for the rest of the session.
1332 */
1333 event = lttng_event_create(channel, event_param,
1334 NULL, NULL,
1335 event_param->instrumentation);
1336 WARN_ON_ONCE(!event);
1337 if (IS_ERR(event)) {
1338 ret = PTR_ERR(event);
1339 goto event_error;
80f87dd2 1340 }
33a39a3c 1341 priv = event;
03037b98 1342 }
33a39a3c
MD
1343 event_file->private_data = priv;
1344 fd_install(event_fd, event_file);
653fe716
MD
1345 return event_fd;
1346
03037b98 1347event_error:
9c1f4643
MD
1348 atomic_long_dec(&channel_file->f_count);
1349refcount_error:
c0e31d2e 1350 fput(event_file);
653fe716
MD
1351file_error:
1352 put_unused_fd(event_fd);
1353fd_error:
653fe716
MD
1354 return ret;
1355}
ad1c05e1
MD
1356
1357/**
1358 * lttng_channel_ioctl - lttng syscall through ioctl
1359 *
c0e31d2e 1360 * @file: the file
ad1c05e1
MD
1361 * @cmd: the command
1362 * @arg: command arg
1363 *
1364 * This ioctl implements lttng commands:
38d024ae 1365 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
1366 * Returns an event stream file descriptor or failure.
1367 * (typically, one event stream records events from one CPU)
38d024ae 1368 * LTTNG_KERNEL_EVENT
ad1c05e1 1369 * Returns an event file descriptor or failure.
8070f5c0
MD
1370 * LTTNG_KERNEL_CONTEXT
1371 * Prepend a context field to each event in the channel
e64957da
MD
1372 * LTTNG_KERNEL_ENABLE
1373 * Enable recording for events in this channel (weak enable)
1374 * LTTNG_KERNEL_DISABLE
1375 * Disable recording for events in this channel (strong disable)
baf20995 1376 *
baf20995
MD
1377 * Channel and event file descriptors also hold a reference on the session.
1378 */
ad1c05e1 1379static
c0e31d2e 1380long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 1381{
a90917c3 1382 struct lttng_channel *channel = file->private_data;
8070f5c0 1383
baf20995 1384 switch (cmd) {
6dccd6c1 1385 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1386 case LTTNG_KERNEL_STREAM:
c0e31d2e 1387 return lttng_abi_open_stream(file);
6dccd6c1
JD
1388 case LTTNG_KERNEL_OLD_EVENT:
1389 {
1390 struct lttng_kernel_event *uevent_param;
1391 struct lttng_kernel_old_event *old_uevent_param;
1392 int ret;
1393
1394 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1395 GFP_KERNEL);
1396 if (!uevent_param) {
1397 ret = -ENOMEM;
1398 goto old_event_end;
1399 }
1400 old_uevent_param = kmalloc(
1401 sizeof(struct lttng_kernel_old_event),
1402 GFP_KERNEL);
1403 if (!old_uevent_param) {
1404 ret = -ENOMEM;
1405 goto old_event_error_free_param;
1406 }
1407 if (copy_from_user(old_uevent_param,
1408 (struct lttng_kernel_old_event __user *) arg,
1409 sizeof(struct lttng_kernel_old_event))) {
1410 ret = -EFAULT;
1411 goto old_event_error_free_old_param;
1412 }
1413
1414 memcpy(uevent_param->name, old_uevent_param->name,
1415 sizeof(uevent_param->name));
1416 uevent_param->instrumentation =
1417 old_uevent_param->instrumentation;
1418
1419 switch (old_uevent_param->instrumentation) {
1420 case LTTNG_KERNEL_KPROBE:
1421 uevent_param->u.kprobe.addr =
1422 old_uevent_param->u.kprobe.addr;
1423 uevent_param->u.kprobe.offset =
1424 old_uevent_param->u.kprobe.offset;
1425 memcpy(uevent_param->u.kprobe.symbol_name,
1426 old_uevent_param->u.kprobe.symbol_name,
1427 sizeof(uevent_param->u.kprobe.symbol_name));
1428 break;
1429 case LTTNG_KERNEL_KRETPROBE:
1430 uevent_param->u.kretprobe.addr =
1431 old_uevent_param->u.kretprobe.addr;
1432 uevent_param->u.kretprobe.offset =
1433 old_uevent_param->u.kretprobe.offset;
1434 memcpy(uevent_param->u.kretprobe.symbol_name,
1435 old_uevent_param->u.kretprobe.symbol_name,
1436 sizeof(uevent_param->u.kretprobe.symbol_name));
1437 break;
1438 case LTTNG_KERNEL_FUNCTION:
e884017c
MD
1439 WARN_ON_ONCE(1);
1440 /* Not implemented. */
6dccd6c1
JD
1441 break;
1442 default:
1443 break;
1444 }
1445 ret = lttng_abi_create_event(file, uevent_param);
1446
1447old_event_error_free_old_param:
1448 kfree(old_uevent_param);
1449old_event_error_free_param:
1450 kfree(uevent_param);
1451old_event_end:
1452 return ret;
1453 }
38d024ae 1454 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
1455 {
1456 struct lttng_kernel_event uevent_param;
1457
1458 if (copy_from_user(&uevent_param,
1459 (struct lttng_kernel_event __user *) arg,
1460 sizeof(uevent_param)))
1461 return -EFAULT;
1462 return lttng_abi_create_event(file, &uevent_param);
1463 }
1464 case LTTNG_KERNEL_OLD_CONTEXT:
1465 {
1466 struct lttng_kernel_context *ucontext_param;
1467 struct lttng_kernel_old_context *old_ucontext_param;
1468 int ret;
1469
1470 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1471 GFP_KERNEL);
1472 if (!ucontext_param) {
1473 ret = -ENOMEM;
1474 goto old_ctx_end;
1475 }
1476 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1477 GFP_KERNEL);
1478 if (!old_ucontext_param) {
1479 ret = -ENOMEM;
1480 goto old_ctx_error_free_param;
1481 }
1482
1483 if (copy_from_user(old_ucontext_param,
1484 (struct lttng_kernel_old_context __user *) arg,
1485 sizeof(struct lttng_kernel_old_context))) {
1486 ret = -EFAULT;
1487 goto old_ctx_error_free_old_param;
1488 }
1489 ucontext_param->ctx = old_ucontext_param->ctx;
1490 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1491 sizeof(ucontext_param->padding));
1492 /* only type that uses the union */
1493 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1494 ucontext_param->u.perf_counter.type =
1495 old_ucontext_param->u.perf_counter.type;
1496 ucontext_param->u.perf_counter.config =
1497 old_ucontext_param->u.perf_counter.config;
1498 memcpy(ucontext_param->u.perf_counter.name,
1499 old_ucontext_param->u.perf_counter.name,
1500 sizeof(ucontext_param->u.perf_counter.name));
1501 }
1502
1503 ret = lttng_abi_add_context(file,
1504 ucontext_param,
1505 &channel->ctx, channel->session);
1506
1507old_ctx_error_free_old_param:
1508 kfree(old_ucontext_param);
1509old_ctx_error_free_param:
1510 kfree(ucontext_param);
1511old_ctx_end:
1512 return ret;
1513 }
8070f5c0 1514 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
1515 {
1516 struct lttng_kernel_context ucontext_param;
1517
1518 if (copy_from_user(&ucontext_param,
8070f5c0 1519 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
1520 sizeof(ucontext_param)))
1521 return -EFAULT;
1522 return lttng_abi_add_context(file,
1523 &ucontext_param,
8070f5c0 1524 &channel->ctx, channel->session);
6dccd6c1
JD
1525 }
1526 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1527 case LTTNG_KERNEL_ENABLE:
a90917c3 1528 return lttng_channel_enable(channel);
6dccd6c1 1529 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1530 case LTTNG_KERNEL_DISABLE:
a90917c3 1531 return lttng_channel_disable(channel);
12e579db
MD
1532 case LTTNG_KERNEL_SYSCALL_MASK:
1533 return lttng_channel_syscall_mask(channel,
1534 (struct lttng_kernel_syscall_mask __user *) arg);
baf20995
MD
1535 default:
1536 return -ENOIOCTLCMD;
1537 }
1538}
1539
5dbbdb43
MD
1540/**
1541 * lttng_metadata_ioctl - lttng syscall through ioctl
1542 *
1543 * @file: the file
1544 * @cmd: the command
1545 * @arg: command arg
1546 *
1547 * This ioctl implements lttng commands:
38d024ae 1548 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
1549 * Returns an event stream file descriptor or failure.
1550 *
1551 * Channel and event file descriptors also hold a reference on the session.
1552 */
1553static
1554long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1555{
1556 switch (cmd) {
6dccd6c1 1557 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1558 case LTTNG_KERNEL_STREAM:
d83004aa 1559 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
1560 default:
1561 return -ENOIOCTLCMD;
1562 }
1563}
1564
653fe716
MD
1565/**
1566 * lttng_channel_poll - lttng stream addition/removal monitoring
1567 *
c0e31d2e 1568 * @file: the file
653fe716
MD
1569 * @wait: poll table
1570 */
c0e31d2e 1571unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 1572{
a90917c3 1573 struct lttng_channel *channel = file->private_data;
653fe716
MD
1574 unsigned int mask = 0;
1575
c0e31d2e 1576 if (file->f_mode & FMODE_READ) {
a33e44a6 1577 poll_wait_set_exclusive(wait);
24cedcfe
MD
1578 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1579 wait);
653fe716 1580
254ec7bc
MD
1581 if (channel->ops->is_disabled(channel->chan))
1582 return POLLERR;
24cedcfe 1583 if (channel->ops->is_finalized(channel->chan))
653fe716 1584 return POLLHUP;
f71ecafa 1585 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 1586 return POLLIN | POLLRDNORM;
f71ecafa 1587 return 0;
653fe716
MD
1588 }
1589 return mask;
1590
1591}
1592
0a84a57f
MD
1593static
1594int lttng_channel_release(struct inode *inode, struct file *file)
1595{
a90917c3 1596 struct lttng_channel *channel = file->private_data;
c269fff4
MD
1597
1598 if (channel)
1599 fput(channel->session->file);
0a84a57f
MD
1600 return 0;
1601}
1602
d83004aa
JD
1603static
1604int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1605{
1606 struct lttng_channel *channel = file->private_data;
1607
1608 if (channel) {
d83004aa 1609 fput(channel->session->file);
a3381417 1610 lttng_metadata_channel_destroy(channel);
d83004aa
JD
1611 }
1612
1613 return 0;
1614}
1615
ad1c05e1 1616static const struct file_operations lttng_channel_fops = {
a33c9927 1617 .owner = THIS_MODULE,
03037b98 1618 .release = lttng_channel_release,
653fe716 1619 .poll = lttng_channel_poll,
ad1c05e1 1620 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 1621#ifdef CONFIG_COMPAT
03037b98 1622 .compat_ioctl = lttng_channel_ioctl,
baf20995 1623#endif
11b5a3c2 1624};
baf20995 1625
5dbbdb43 1626static const struct file_operations lttng_metadata_fops = {
a33c9927 1627 .owner = THIS_MODULE,
d83004aa 1628 .release = lttng_metadata_channel_release,
5dbbdb43
MD
1629 .unlocked_ioctl = lttng_metadata_ioctl,
1630#ifdef CONFIG_COMPAT
1631 .compat_ioctl = lttng_metadata_ioctl,
1632#endif
1633};
1634
8070f5c0
MD
1635/**
1636 * lttng_event_ioctl - lttng syscall through ioctl
1637 *
1638 * @file: the file
1639 * @cmd: the command
1640 * @arg: command arg
1641 *
1642 * This ioctl implements lttng commands:
8070f5c0
MD
1643 * LTTNG_KERNEL_CONTEXT
1644 * Prepend a context field to each record of this event
e64957da
MD
1645 * LTTNG_KERNEL_ENABLE
1646 * Enable recording for this event (weak enable)
1647 * LTTNG_KERNEL_DISABLE
1648 * Disable recording for this event (strong disable)
8070f5c0
MD
1649 */
1650static
1651long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1652{
3c997079
MD
1653 struct lttng_event *event;
1654 struct lttng_enabler *enabler;
1655 enum lttng_event_type *evtype = file->private_data;
8070f5c0
MD
1656
1657 switch (cmd) {
6dccd6c1
JD
1658 case LTTNG_KERNEL_OLD_CONTEXT:
1659 {
3c997079
MD
1660 /* Not implemented */
1661 return -ENOSYS;
6dccd6c1 1662 }
8070f5c0 1663 case LTTNG_KERNEL_CONTEXT:
6dccd6c1 1664 {
3c997079
MD
1665 /* Not implemented */
1666 return -ENOSYS;
6dccd6c1
JD
1667 }
1668 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1669 case LTTNG_KERNEL_ENABLE:
3c997079
MD
1670 switch (*evtype) {
1671 case LTTNG_TYPE_EVENT:
1672 event = file->private_data;
1673 return lttng_event_enable(event);
1674 case LTTNG_TYPE_ENABLER:
1675 enabler = file->private_data;
1676 return lttng_enabler_enable(enabler);
1677 default:
1678 WARN_ON_ONCE(1);
1679 return -ENOSYS;
1680 }
6dccd6c1 1681 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1682 case LTTNG_KERNEL_DISABLE:
3c997079
MD
1683 switch (*evtype) {
1684 case LTTNG_TYPE_EVENT:
1685 event = file->private_data;
1686 return lttng_event_disable(event);
1687 case LTTNG_TYPE_ENABLER:
1688 enabler = file->private_data;
1689 return lttng_enabler_disable(enabler);
1690 default:
1691 WARN_ON_ONCE(1);
1692 return -ENOSYS;
1693 }
07dfc1d0
MD
1694 case LTTNG_KERNEL_FILTER:
1695 switch (*evtype) {
1696 case LTTNG_TYPE_EVENT:
1697 return -EINVAL;
1698 case LTTNG_TYPE_ENABLER:
1699 {
1700 enabler = file->private_data;
1701 return lttng_enabler_attach_bytecode(enabler,
1702 (struct lttng_kernel_filter_bytecode __user *) arg);
1703 }
0586316f
MD
1704 default:
1705 WARN_ON_ONCE(1);
1706 return -ENOSYS;
07dfc1d0 1707 }
3aed4dca
FD
1708 case LTTNG_KERNEL_ADD_CALLSITE:
1709 switch (*evtype) {
1710 case LTTNG_TYPE_EVENT:
1711 event = file->private_data;
1712 return lttng_event_add_callsite(event,
1713 (struct lttng_kernel_event_callsite __user *) arg);
1714 case LTTNG_TYPE_ENABLER:
1715 return -EINVAL;
64c147c0
MD
1716 default:
1717 WARN_ON_ONCE(1);
1718 return -ENOSYS;
3aed4dca 1719 }
8070f5c0
MD
1720 default:
1721 return -ENOIOCTLCMD;
1722 }
1723}
1724
0a84a57f
MD
1725static
1726int lttng_event_release(struct inode *inode, struct file *file)
1727{
3c997079
MD
1728 struct lttng_event *event;
1729 struct lttng_enabler *enabler;
1730 enum lttng_event_type *evtype = file->private_data;
1731
1732 if (!evtype)
1733 return 0;
1734
1735 switch (*evtype) {
1736 case LTTNG_TYPE_EVENT:
1737 event = file->private_data;
1738 if (event)
1739 fput(event->chan->file);
1740 break;
1741 case LTTNG_TYPE_ENABLER:
1742 enabler = file->private_data;
1743 if (enabler)
1744 fput(enabler->chan->file);
1745 break;
1746 default:
1747 WARN_ON_ONCE(1);
1748 break;
1749 }
c269fff4 1750
0a84a57f
MD
1751 return 0;
1752}
1753
3b923e5b 1754/* TODO: filter control ioctl */
0a84a57f 1755static const struct file_operations lttng_event_fops = {
a33c9927 1756 .owner = THIS_MODULE,
0a84a57f 1757 .release = lttng_event_release,
8070f5c0
MD
1758 .unlocked_ioctl = lttng_event_ioctl,
1759#ifdef CONFIG_COMPAT
1760 .compat_ioctl = lttng_event_ioctl,
1761#endif
11b5a3c2 1762};
0a84a57f 1763
3b731ab1
JD
1764static int put_u64(uint64_t val, unsigned long arg)
1765{
1766 return put_user(val, (uint64_t __user *) arg);
1767}
1768
8b97fd42
MD
1769static int put_u32(uint32_t val, unsigned long arg)
1770{
1771 return put_user(val, (uint32_t __user *) arg);
1772}
1773
ed8d02d6
JD
1774static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1775 unsigned int cmd, unsigned long arg)
1776{
3b731ab1
JD
1777 struct lib_ring_buffer *buf = filp->private_data;
1778 struct channel *chan = buf->backend.chan;
1779 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 1780 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
1781 int ret;
1782
1783 if (atomic_read(&chan->record_disabled))
1784 return -EIO;
1785
ed8d02d6 1786 switch (cmd) {
3b731ab1
JD
1787 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1788 {
1789 uint64_t ts;
1790
dd5a0db3 1791 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
1792 if (ret < 0)
1793 goto error;
1794 return put_u64(ts, arg);
1795 }
1796 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1797 {
1798 uint64_t ts;
1799
dd5a0db3 1800 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
1801 if (ret < 0)
1802 goto error;
1803 return put_u64(ts, arg);
1804 }
1805 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1806 {
1807 uint64_t ed;
1808
dd5a0db3 1809 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
1810 if (ret < 0)
1811 goto error;
1812 return put_u64(ed, arg);
1813 }
1814 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1815 {
1816 uint64_t cs;
1817
dd5a0db3 1818 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
1819 if (ret < 0)
1820 goto error;
1821 return put_u64(cs, arg);
1822 }
1823 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1824 {
1825 uint64_t ps;
1826
dd5a0db3 1827 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
1828 if (ret < 0)
1829 goto error;
1830 return put_u64(ps, arg);
1831 }
1832 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1833 {
1834 uint64_t si;
1835
dd5a0db3 1836 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
1837 if (ret < 0)
1838 goto error;
1839 return put_u64(si, arg);
1840 }
2348ca17
JD
1841 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1842 {
1843 uint64_t ts;
1844
dd5a0db3 1845 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
1846 if (ret < 0)
1847 goto error;
1848 return put_u64(ts, arg);
1849 }
5b3cf4f9
JD
1850 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1851 {
1852 uint64_t seq;
1853
1854 ret = ops->sequence_number(config, buf, &seq);
1855 if (ret < 0)
1856 goto error;
1857 return put_u64(seq, arg);
1858 }
5594698f
JD
1859 case LTTNG_RING_BUFFER_INSTANCE_ID:
1860 {
1861 uint64_t id;
1862
1863 ret = ops->instance_id(config, buf, &id);
1864 if (ret < 0)
1865 goto error;
1866 return put_u64(id, arg);
1867 }
3b731ab1
JD
1868 default:
1869 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1870 cmd, arg);
ed8d02d6 1871 }
3b731ab1
JD
1872
1873error:
1874 return -ENOSYS;
ed8d02d6
JD
1875}
1876
1877#ifdef CONFIG_COMPAT
1878static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1879 unsigned int cmd, unsigned long arg)
1880{
3b731ab1
JD
1881 struct lib_ring_buffer *buf = filp->private_data;
1882 struct channel *chan = buf->backend.chan;
1883 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 1884 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
1885 int ret;
1886
1887 if (atomic_read(&chan->record_disabled))
1888 return -EIO;
1889
ed8d02d6 1890 switch (cmd) {
3b731ab1
JD
1891 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1892 {
1893 uint64_t ts;
1894
dd5a0db3 1895 ret = ops->timestamp_begin(config, buf, &ts);
3b731ab1
JD
1896 if (ret < 0)
1897 goto error;
1898 return put_u64(ts, arg);
1899 }
1900 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1901 {
1902 uint64_t ts;
1903
dd5a0db3 1904 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
1905 if (ret < 0)
1906 goto error;
1907 return put_u64(ts, arg);
1908 }
1909 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1910 {
1911 uint64_t ed;
1912
dd5a0db3 1913 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
1914 if (ret < 0)
1915 goto error;
1916 return put_u64(ed, arg);
ed8d02d6 1917 }
3b731ab1
JD
1918 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1919 {
1920 uint64_t cs;
1921
dd5a0db3 1922 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
1923 if (ret < 0)
1924 goto error;
1925 return put_u64(cs, arg);
1926 }
1927 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1928 {
1929 uint64_t ps;
1930
dd5a0db3 1931 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
1932 if (ret < 0)
1933 goto error;
1934 return put_u64(ps, arg);
1935 }
1936 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1937 {
1938 uint64_t si;
1939
dd5a0db3 1940 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
1941 if (ret < 0)
1942 goto error;
1943 return put_u64(si, arg);
1944 }
2348ca17
JD
1945 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1946 {
1947 uint64_t ts;
1948
dd5a0db3 1949 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
1950 if (ret < 0)
1951 goto error;
1952 return put_u64(ts, arg);
1953 }
5b3cf4f9
JD
1954 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1955 {
1956 uint64_t seq;
1957
1958 ret = ops->sequence_number(config, buf, &seq);
1959 if (ret < 0)
1960 goto error;
1961 return put_u64(seq, arg);
1962 }
5594698f
JD
1963 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
1964 {
1965 uint64_t id;
1966
1967 ret = ops->instance_id(config, buf, &id);
1968 if (ret < 0)
1969 goto error;
1970 return put_u64(id, arg);
1971 }
3b731ab1
JD
1972 default:
1973 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1974 cmd, arg);
1975 }
1976
1977error:
1978 return -ENOSYS;
ed8d02d6
JD
1979}
1980#endif /* CONFIG_COMPAT */
1981
1982static void lttng_stream_override_ring_buffer_fops(void)
1983{
1984 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1985 lttng_stream_ring_buffer_file_operations.open =
1986 lib_ring_buffer_file_operations.open;
1987 lttng_stream_ring_buffer_file_operations.release =
1988 lib_ring_buffer_file_operations.release;
1989 lttng_stream_ring_buffer_file_operations.poll =
1990 lib_ring_buffer_file_operations.poll;
1991 lttng_stream_ring_buffer_file_operations.splice_read =
1992 lib_ring_buffer_file_operations.splice_read;
1993 lttng_stream_ring_buffer_file_operations.mmap =
1994 lib_ring_buffer_file_operations.mmap;
1995 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1996 lttng_stream_ring_buffer_ioctl;
1997 lttng_stream_ring_buffer_file_operations.llseek =
1998 lib_ring_buffer_file_operations.llseek;
1999#ifdef CONFIG_COMPAT
2000 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2001 lttng_stream_ring_buffer_compat_ioctl;
2002#endif
2003}
2004
80996790 2005int __init lttng_abi_init(void)
baf20995
MD
2006{
2007 int ret = 0;
2008
263b6c88 2009 wrapper_vmalloc_sync_mappings();
2754583e 2010 lttng_clock_ref();
f771eda6
JD
2011
2012 ret = lttng_tp_mempool_init();
2013 if (ret) {
2014 goto error;
2015 }
2016
d29348f7 2017 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 2018 &lttng_proc_ops, NULL);
2470a237 2019
255e52a4 2020 if (!lttng_proc_dentry) {
5a15f70c 2021 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
2022 ret = -ENOMEM;
2023 goto error;
2024 }
ed8d02d6 2025 lttng_stream_override_ring_buffer_fops();
2754583e 2026 return 0;
ed8d02d6 2027
baf20995 2028error:
f771eda6 2029 lttng_tp_mempool_destroy();
2754583e 2030 lttng_clock_unref();
baf20995
MD
2031 return ret;
2032}
2033
e6e65fcd
MD
2034/* No __exit annotation because used by init error path too. */
2035void lttng_abi_exit(void)
baf20995 2036{
f771eda6 2037 lttng_tp_mempool_destroy();
2754583e 2038 lttng_clock_unref();
e6a17f26
MD
2039 if (lttng_proc_dentry)
2040 remove_proc_entry("lttng", NULL);
baf20995 2041}
This page took 0.144816 seconds and 4 git commands to generate.