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