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