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