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