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