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