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