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