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