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