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