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