Fix: quote event name in metadata
[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
f613e3e6
MD
551static
552void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
553 unsigned int cmd, unsigned long arg)
554{
555 struct lttng_metadata_stream *stream = filp->private_data;
556
557 stream->metadata_out = stream->metadata_in;
558}
559
d83004aa
JD
560static
561long lttng_metadata_ring_buffer_ioctl(struct file *filp,
562 unsigned int cmd, unsigned long arg)
563{
564 int ret;
565 struct lttng_metadata_stream *stream = filp->private_data;
566 struct lib_ring_buffer *buf = stream->priv;
567
568 switch (cmd) {
d83004aa
JD
569 case RING_BUFFER_GET_NEXT_SUBBUF:
570 {
35097f36
JD
571 struct lttng_metadata_stream *stream = filp->private_data;
572 struct lib_ring_buffer *buf = stream->priv;
573 struct channel *chan = buf->backend.chan;
574
575 ret = lttng_metadata_output_channel(stream, chan);
576 if (ret > 0) {
577 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
578 ret = 0;
579 } else if (ret < 0)
d83004aa
JD
580 goto err;
581 break;
582 }
f613e3e6
MD
583 case RING_BUFFER_GET_SUBBUF:
584 {
585 /*
586 * Random access is not allowed for metadata channel.
587 */
588 return -ENOSYS;
589 }
35097f36
JD
590 case RING_BUFFER_FLUSH:
591 {
592 struct lttng_metadata_stream *stream = filp->private_data;
593 struct lib_ring_buffer *buf = stream->priv;
594 struct channel *chan = buf->backend.chan;
595
596 /*
597 * Before doing the actual ring buffer flush, write up to one
598 * packet of metadata in the ring buffer.
599 */
600 ret = lttng_metadata_output_channel(stream, chan);
601 if (ret < 0)
602 goto err;
603 break;
604 }
d83004aa
JD
605 default:
606 break;
607 }
f613e3e6
MD
608 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
609
d83004aa 610 /* Performing lib ring buffer ioctl after our own. */
f613e3e6
MD
611 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
612 if (ret < 0)
613 goto err;
d83004aa 614
f613e3e6
MD
615 switch (cmd) {
616 case RING_BUFFER_PUT_NEXT_SUBBUF:
617 {
618 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
619 cmd, arg);
620 break;
621 }
622 default:
623 break;
624 }
d83004aa
JD
625err:
626 return ret;
627}
628
aeb9064d 629#ifdef CONFIG_COMPAT
d83004aa
JD
630static
631long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
632 unsigned int cmd, unsigned long arg)
633{
634 int ret;
635 struct lttng_metadata_stream *stream = filp->private_data;
636 struct lib_ring_buffer *buf = stream->priv;
637
638 switch (cmd) {
d83004aa
JD
639 case RING_BUFFER_GET_NEXT_SUBBUF:
640 {
35097f36
JD
641 struct lttng_metadata_stream *stream = filp->private_data;
642 struct lib_ring_buffer *buf = stream->priv;
643 struct channel *chan = buf->backend.chan;
644
645 ret = lttng_metadata_output_channel(stream, chan);
646 if (ret > 0) {
647 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
648 ret = 0;
649 } else if (ret < 0)
d83004aa
JD
650 goto err;
651 break;
652 }
f613e3e6
MD
653 case RING_BUFFER_GET_SUBBUF:
654 {
655 /*
656 * Random access is not allowed for metadata channel.
657 */
658 return -ENOSYS;
659 }
d83004aa
JD
660 default:
661 break;
662 }
f613e3e6
MD
663 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
664
d83004aa 665 /* Performing lib ring buffer ioctl after our own. */
f613e3e6
MD
666 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
667 if (ret < 0)
668 goto err;
d83004aa 669
f613e3e6
MD
670 switch (cmd) {
671 case RING_BUFFER_PUT_NEXT_SUBBUF:
672 {
673 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
674 cmd, arg);
675 break;
676 }
677 default:
678 break;
679 }
d83004aa
JD
680err:
681 return ret;
682}
aeb9064d 683#endif
d83004aa 684
b3b8072b
MD
685/*
686 * This is not used by anonymous file descriptors. This code is left
687 * there if we ever want to implement an inode with open() operation.
688 */
d83004aa
JD
689static
690int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
691{
692 struct lttng_metadata_stream *stream = inode->i_private;
693 struct lib_ring_buffer *buf = stream->priv;
694
695 file->private_data = buf;
b3b8072b
MD
696 /*
697 * Since life-time of metadata cache differs from that of
698 * session, we need to keep our own reference on the transport.
699 */
700 if (!try_module_get(stream->transport->owner)) {
701 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
702 return -EBUSY;
703 }
d83004aa
JD
704 return lib_ring_buffer_open(inode, file, buf);
705}
706
707static
708int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
709{
710 struct lttng_metadata_stream *stream = file->private_data;
711 struct lib_ring_buffer *buf = stream->priv;
712
713 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 714 module_put(stream->transport->owner);
d83004aa
JD
715 return lib_ring_buffer_release(inode, file, buf);
716}
717
718static
719ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
720 struct pipe_inode_info *pipe, size_t len,
721 unsigned int flags)
722{
723 struct lttng_metadata_stream *stream = in->private_data;
724 struct lib_ring_buffer *buf = stream->priv;
725
726 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
727 flags, buf);
728}
729
730static
731int lttng_metadata_ring_buffer_mmap(struct file *filp,
732 struct vm_area_struct *vma)
733{
734 struct lttng_metadata_stream *stream = filp->private_data;
735 struct lib_ring_buffer *buf = stream->priv;
736
737 return lib_ring_buffer_mmap(filp, vma, buf);
738}
739
740static
741const struct file_operations lttng_metadata_ring_buffer_file_operations = {
742 .owner = THIS_MODULE,
743 .open = lttng_metadata_ring_buffer_open,
744 .release = lttng_metadata_ring_buffer_release,
745 .poll = lttng_metadata_ring_buffer_poll,
746 .splice_read = lttng_metadata_ring_buffer_splice_read,
747 .mmap = lttng_metadata_ring_buffer_mmap,
748 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
749 .llseek = vfs_lib_ring_buffer_no_llseek,
750#ifdef CONFIG_COMPAT
751 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
752#endif
753};
754
755static
756int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
757 const struct file_operations *fops)
ad1c05e1 758{
ad1c05e1 759 int stream_fd, ret;
11b5a3c2 760 struct file *stream_file;
ad1c05e1 761
1c25284c 762 stream_fd = get_unused_fd();
ad1c05e1
MD
763 if (stream_fd < 0) {
764 ret = stream_fd;
765 goto fd_error;
766 }
d83004aa
JD
767 stream_file = anon_inode_getfile("[lttng_stream]", fops,
768 stream_priv, O_RDWR);
c0e31d2e
MD
769 if (IS_ERR(stream_file)) {
770 ret = PTR_ERR(stream_file);
ad1c05e1
MD
771 goto file_error;
772 }
409453cb
MD
773 /*
774 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
775 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
776 * file descriptor, so we set FMODE_PREAD here.
777 */
d7b6f197 778 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 779 fd_install(stream_fd, stream_file);
dda6a249
MD
780 /*
781 * The stream holds a reference to the channel within the generic ring
782 * buffer library, so no need to hold a refcount on the channel and
783 * session files here.
784 */
ad1c05e1
MD
785 return stream_fd;
786
787file_error:
788 put_unused_fd(stream_fd);
d83004aa
JD
789fd_error:
790 return ret;
791}
792
793static
794int lttng_abi_open_stream(struct file *channel_file)
795{
796 struct lttng_channel *channel = channel_file->private_data;
797 struct lib_ring_buffer *buf;
798 int ret;
799 void *stream_priv;
800
801 buf = channel->ops->buffer_read_open(channel->chan);
802 if (!buf)
803 return -ENOENT;
804
805 stream_priv = buf;
806 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
ed8d02d6 807 &lttng_stream_ring_buffer_file_operations);
d83004aa
JD
808 if (ret < 0)
809 goto fd_error;
810
811 return ret;
812
813fd_error:
814 channel->ops->buffer_read_close(buf);
815 return ret;
816}
817
818static
819int lttng_abi_open_metadata_stream(struct file *channel_file)
820{
821 struct lttng_channel *channel = channel_file->private_data;
822 struct lttng_session *session = channel->session;
823 struct lib_ring_buffer *buf;
824 int ret;
825 struct lttng_metadata_stream *metadata_stream;
826 void *stream_priv;
827
828 buf = channel->ops->buffer_read_open(channel->chan);
829 if (!buf)
830 return -ENOENT;
831
832 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
833 GFP_KERNEL);
b3b8072b
MD
834 if (!metadata_stream) {
835 ret = -ENOMEM;
836 goto nomem;
837 }
d83004aa
JD
838 metadata_stream->metadata_cache = session->metadata_cache;
839 init_waitqueue_head(&metadata_stream->read_wait);
840 metadata_stream->priv = buf;
841 stream_priv = metadata_stream;
b3b8072b
MD
842 metadata_stream->transport = channel->transport;
843
844 /*
845 * Since life-time of metadata cache differs from that of
846 * session, we need to keep our own reference on the transport.
847 */
848 if (!try_module_get(metadata_stream->transport->owner)) {
849 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
850 ret = -EINVAL;
851 goto notransport;
852 }
853
d83004aa
JD
854 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
855 &lttng_metadata_ring_buffer_file_operations);
856 if (ret < 0)
857 goto fd_error;
858
859 kref_get(&session->metadata_cache->refcount);
860 list_add(&metadata_stream->list,
861 &session->metadata_cache->metadata_stream);
862 return ret;
863
ad1c05e1 864fd_error:
b3b8072b
MD
865 module_put(metadata_stream->transport->owner);
866notransport:
867 kfree(metadata_stream);
868nomem:
11b5a3c2 869 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
870 return ret;
871}
872
653fe716 873static
c0e31d2e 874int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 875 struct lttng_kernel_event *event_param)
653fe716 876{
a90917c3
MD
877 struct lttng_channel *channel = channel_file->private_data;
878 struct lttng_event *event;
653fe716 879 int event_fd, ret;
11b5a3c2 880 struct file *event_file;
653fe716 881
6dccd6c1
JD
882 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
883 switch (event_param->instrumentation) {
7371f44c 884 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 885 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 886 break;
ab2277d6 887 case LTTNG_KERNEL_KPROBE:
6dccd6c1 888 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 889 break;
ab2277d6 890 case LTTNG_KERNEL_FUNCTION:
6dccd6c1 891 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4
MD
892 break;
893 default:
894 break;
895 }
6dccd6c1 896 switch (event_param->instrumentation) {
1ec65de1
MD
897 default:
898 event_fd = get_unused_fd();
899 if (event_fd < 0) {
900 ret = event_fd;
901 goto fd_error;
902 }
903 event_file = anon_inode_getfile("[lttng_event]",
904 &lttng_event_fops,
905 NULL, O_RDWR);
906 if (IS_ERR(event_file)) {
907 ret = PTR_ERR(event_file);
908 goto file_error;
909 }
910 /*
911 * We tolerate no failure path after event creation. It
912 * will stay invariant for the rest of the session.
913 */
6dccd6c1 914 event = lttng_event_create(channel, event_param, NULL, NULL);
1ec65de1
MD
915 if (!event) {
916 ret = -EINVAL;
917 goto event_error;
918 }
919 event_file->private_data = event;
920 fd_install(event_fd, event_file);
921 /* The event holds a reference on the channel */
922 atomic_long_inc(&channel_file->f_count);
923 break;
43880ee8
MD
924 case LTTNG_KERNEL_SYSCALL:
925 /*
926 * Only all-syscall tracing supported for now.
927 */
6dccd6c1 928 if (event_param->name[0] != '\0')
43880ee8 929 return -EINVAL;
1ec65de1
MD
930 ret = lttng_syscalls_register(channel, NULL);
931 if (ret)
932 goto fd_error;
933 event_fd = 0;
934 break;
03037b98 935 }
653fe716
MD
936 return event_fd;
937
03037b98 938event_error:
c0e31d2e 939 fput(event_file);
653fe716
MD
940file_error:
941 put_unused_fd(event_fd);
942fd_error:
653fe716
MD
943 return ret;
944}
ad1c05e1
MD
945
946/**
947 * lttng_channel_ioctl - lttng syscall through ioctl
948 *
c0e31d2e 949 * @file: the file
ad1c05e1
MD
950 * @cmd: the command
951 * @arg: command arg
952 *
953 * This ioctl implements lttng commands:
38d024ae 954 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
955 * Returns an event stream file descriptor or failure.
956 * (typically, one event stream records events from one CPU)
38d024ae 957 * LTTNG_KERNEL_EVENT
ad1c05e1 958 * Returns an event file descriptor or failure.
8070f5c0
MD
959 * LTTNG_KERNEL_CONTEXT
960 * Prepend a context field to each event in the channel
e64957da
MD
961 * LTTNG_KERNEL_ENABLE
962 * Enable recording for events in this channel (weak enable)
963 * LTTNG_KERNEL_DISABLE
964 * Disable recording for events in this channel (strong disable)
baf20995 965 *
baf20995
MD
966 * Channel and event file descriptors also hold a reference on the session.
967 */
ad1c05e1 968static
c0e31d2e 969long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 970{
a90917c3 971 struct lttng_channel *channel = file->private_data;
8070f5c0 972
baf20995 973 switch (cmd) {
6dccd6c1 974 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 975 case LTTNG_KERNEL_STREAM:
c0e31d2e 976 return lttng_abi_open_stream(file);
6dccd6c1
JD
977 case LTTNG_KERNEL_OLD_EVENT:
978 {
979 struct lttng_kernel_event *uevent_param;
980 struct lttng_kernel_old_event *old_uevent_param;
981 int ret;
982
983 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
984 GFP_KERNEL);
985 if (!uevent_param) {
986 ret = -ENOMEM;
987 goto old_event_end;
988 }
989 old_uevent_param = kmalloc(
990 sizeof(struct lttng_kernel_old_event),
991 GFP_KERNEL);
992 if (!old_uevent_param) {
993 ret = -ENOMEM;
994 goto old_event_error_free_param;
995 }
996 if (copy_from_user(old_uevent_param,
997 (struct lttng_kernel_old_event __user *) arg,
998 sizeof(struct lttng_kernel_old_event))) {
999 ret = -EFAULT;
1000 goto old_event_error_free_old_param;
1001 }
1002
1003 memcpy(uevent_param->name, old_uevent_param->name,
1004 sizeof(uevent_param->name));
1005 uevent_param->instrumentation =
1006 old_uevent_param->instrumentation;
1007
1008 switch (old_uevent_param->instrumentation) {
1009 case LTTNG_KERNEL_KPROBE:
1010 uevent_param->u.kprobe.addr =
1011 old_uevent_param->u.kprobe.addr;
1012 uevent_param->u.kprobe.offset =
1013 old_uevent_param->u.kprobe.offset;
1014 memcpy(uevent_param->u.kprobe.symbol_name,
1015 old_uevent_param->u.kprobe.symbol_name,
1016 sizeof(uevent_param->u.kprobe.symbol_name));
1017 break;
1018 case LTTNG_KERNEL_KRETPROBE:
1019 uevent_param->u.kretprobe.addr =
1020 old_uevent_param->u.kretprobe.addr;
1021 uevent_param->u.kretprobe.offset =
1022 old_uevent_param->u.kretprobe.offset;
1023 memcpy(uevent_param->u.kretprobe.symbol_name,
1024 old_uevent_param->u.kretprobe.symbol_name,
1025 sizeof(uevent_param->u.kretprobe.symbol_name));
1026 break;
1027 case LTTNG_KERNEL_FUNCTION:
1028 memcpy(uevent_param->u.ftrace.symbol_name,
1029 old_uevent_param->u.ftrace.symbol_name,
1030 sizeof(uevent_param->u.ftrace.symbol_name));
1031 break;
1032 default:
1033 break;
1034 }
1035 ret = lttng_abi_create_event(file, uevent_param);
1036
1037old_event_error_free_old_param:
1038 kfree(old_uevent_param);
1039old_event_error_free_param:
1040 kfree(uevent_param);
1041old_event_end:
1042 return ret;
1043 }
38d024ae 1044 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
1045 {
1046 struct lttng_kernel_event uevent_param;
1047
1048 if (copy_from_user(&uevent_param,
1049 (struct lttng_kernel_event __user *) arg,
1050 sizeof(uevent_param)))
1051 return -EFAULT;
1052 return lttng_abi_create_event(file, &uevent_param);
1053 }
1054 case LTTNG_KERNEL_OLD_CONTEXT:
1055 {
1056 struct lttng_kernel_context *ucontext_param;
1057 struct lttng_kernel_old_context *old_ucontext_param;
1058 int ret;
1059
1060 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1061 GFP_KERNEL);
1062 if (!ucontext_param) {
1063 ret = -ENOMEM;
1064 goto old_ctx_end;
1065 }
1066 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1067 GFP_KERNEL);
1068 if (!old_ucontext_param) {
1069 ret = -ENOMEM;
1070 goto old_ctx_error_free_param;
1071 }
1072
1073 if (copy_from_user(old_ucontext_param,
1074 (struct lttng_kernel_old_context __user *) arg,
1075 sizeof(struct lttng_kernel_old_context))) {
1076 ret = -EFAULT;
1077 goto old_ctx_error_free_old_param;
1078 }
1079 ucontext_param->ctx = old_ucontext_param->ctx;
1080 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1081 sizeof(ucontext_param->padding));
1082 /* only type that uses the union */
1083 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1084 ucontext_param->u.perf_counter.type =
1085 old_ucontext_param->u.perf_counter.type;
1086 ucontext_param->u.perf_counter.config =
1087 old_ucontext_param->u.perf_counter.config;
1088 memcpy(ucontext_param->u.perf_counter.name,
1089 old_ucontext_param->u.perf_counter.name,
1090 sizeof(ucontext_param->u.perf_counter.name));
1091 }
1092
1093 ret = lttng_abi_add_context(file,
1094 ucontext_param,
1095 &channel->ctx, channel->session);
1096
1097old_ctx_error_free_old_param:
1098 kfree(old_ucontext_param);
1099old_ctx_error_free_param:
1100 kfree(ucontext_param);
1101old_ctx_end:
1102 return ret;
1103 }
8070f5c0 1104 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
1105 {
1106 struct lttng_kernel_context ucontext_param;
1107
1108 if (copy_from_user(&ucontext_param,
8070f5c0 1109 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
1110 sizeof(ucontext_param)))
1111 return -EFAULT;
1112 return lttng_abi_add_context(file,
1113 &ucontext_param,
8070f5c0 1114 &channel->ctx, channel->session);
6dccd6c1
JD
1115 }
1116 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1117 case LTTNG_KERNEL_ENABLE:
a90917c3 1118 return lttng_channel_enable(channel);
6dccd6c1 1119 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1120 case LTTNG_KERNEL_DISABLE:
a90917c3 1121 return lttng_channel_disable(channel);
baf20995
MD
1122 default:
1123 return -ENOIOCTLCMD;
1124 }
6dccd6c1 1125
baf20995
MD
1126}
1127
5dbbdb43
MD
1128/**
1129 * lttng_metadata_ioctl - lttng syscall through ioctl
1130 *
1131 * @file: the file
1132 * @cmd: the command
1133 * @arg: command arg
1134 *
1135 * This ioctl implements lttng commands:
38d024ae 1136 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
1137 * Returns an event stream file descriptor or failure.
1138 *
1139 * Channel and event file descriptors also hold a reference on the session.
1140 */
1141static
1142long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1143{
1144 switch (cmd) {
6dccd6c1 1145 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1146 case LTTNG_KERNEL_STREAM:
d83004aa 1147 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
1148 default:
1149 return -ENOIOCTLCMD;
1150 }
1151}
1152
653fe716
MD
1153/**
1154 * lttng_channel_poll - lttng stream addition/removal monitoring
1155 *
c0e31d2e 1156 * @file: the file
653fe716
MD
1157 * @wait: poll table
1158 */
c0e31d2e 1159unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 1160{
a90917c3 1161 struct lttng_channel *channel = file->private_data;
653fe716
MD
1162 unsigned int mask = 0;
1163
c0e31d2e 1164 if (file->f_mode & FMODE_READ) {
a33e44a6 1165 poll_wait_set_exclusive(wait);
24cedcfe
MD
1166 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1167 wait);
653fe716 1168
254ec7bc
MD
1169 if (channel->ops->is_disabled(channel->chan))
1170 return POLLERR;
24cedcfe 1171 if (channel->ops->is_finalized(channel->chan))
653fe716 1172 return POLLHUP;
f71ecafa 1173 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 1174 return POLLIN | POLLRDNORM;
f71ecafa 1175 return 0;
653fe716
MD
1176 }
1177 return mask;
1178
1179}
1180
0a84a57f
MD
1181static
1182int lttng_channel_release(struct inode *inode, struct file *file)
1183{
a90917c3 1184 struct lttng_channel *channel = file->private_data;
c269fff4
MD
1185
1186 if (channel)
1187 fput(channel->session->file);
0a84a57f
MD
1188 return 0;
1189}
1190
d83004aa
JD
1191static
1192int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1193{
1194 struct lttng_channel *channel = file->private_data;
1195
1196 if (channel) {
1197 lttng_metadata_channel_destroy(channel);
1198 fput(channel->session->file);
1199 }
1200
1201 return 0;
1202}
1203
ad1c05e1 1204static const struct file_operations lttng_channel_fops = {
a33c9927 1205 .owner = THIS_MODULE,
03037b98 1206 .release = lttng_channel_release,
653fe716 1207 .poll = lttng_channel_poll,
ad1c05e1 1208 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 1209#ifdef CONFIG_COMPAT
03037b98 1210 .compat_ioctl = lttng_channel_ioctl,
baf20995 1211#endif
11b5a3c2 1212};
baf20995 1213
5dbbdb43 1214static const struct file_operations lttng_metadata_fops = {
a33c9927 1215 .owner = THIS_MODULE,
d83004aa 1216 .release = lttng_metadata_channel_release,
5dbbdb43
MD
1217 .unlocked_ioctl = lttng_metadata_ioctl,
1218#ifdef CONFIG_COMPAT
1219 .compat_ioctl = lttng_metadata_ioctl,
1220#endif
1221};
1222
8070f5c0
MD
1223/**
1224 * lttng_event_ioctl - lttng syscall through ioctl
1225 *
1226 * @file: the file
1227 * @cmd: the command
1228 * @arg: command arg
1229 *
1230 * This ioctl implements lttng commands:
8070f5c0
MD
1231 * LTTNG_KERNEL_CONTEXT
1232 * Prepend a context field to each record of this event
e64957da
MD
1233 * LTTNG_KERNEL_ENABLE
1234 * Enable recording for this event (weak enable)
1235 * LTTNG_KERNEL_DISABLE
1236 * Disable recording for this event (strong disable)
8070f5c0
MD
1237 */
1238static
1239long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1240{
a90917c3 1241 struct lttng_event *event = file->private_data;
8070f5c0
MD
1242
1243 switch (cmd) {
6dccd6c1
JD
1244 case LTTNG_KERNEL_OLD_CONTEXT:
1245 {
1246 struct lttng_kernel_context *ucontext_param;
1247 struct lttng_kernel_old_context *old_ucontext_param;
1248 int ret;
1249
1250 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1251 GFP_KERNEL);
1252 if (!ucontext_param) {
1253 ret = -ENOMEM;
1254 goto old_ctx_end;
1255 }
1256 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1257 GFP_KERNEL);
1258 if (!old_ucontext_param) {
1259 ret = -ENOMEM;
1260 goto old_ctx_error_free_param;
1261 }
1262
1263 if (copy_from_user(old_ucontext_param,
1264 (struct lttng_kernel_old_context __user *) arg,
1265 sizeof(struct lttng_kernel_old_context))) {
1266 ret = -EFAULT;
1267 goto old_ctx_error_free_old_param;
1268 }
1269 ucontext_param->ctx = old_ucontext_param->ctx;
1270 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1271 sizeof(ucontext_param->padding));
1272 /* only type that uses the union */
1273 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1274 ucontext_param->u.perf_counter.type =
1275 old_ucontext_param->u.perf_counter.type;
1276 ucontext_param->u.perf_counter.config =
1277 old_ucontext_param->u.perf_counter.config;
1278 memcpy(ucontext_param->u.perf_counter.name,
1279 old_ucontext_param->u.perf_counter.name,
1280 sizeof(ucontext_param->u.perf_counter.name));
1281 }
1282
1283 ret = lttng_abi_add_context(file,
1284 ucontext_param,
1285 &event->ctx, event->chan->session);
1286
1287old_ctx_error_free_old_param:
1288 kfree(old_ucontext_param);
1289old_ctx_error_free_param:
1290 kfree(ucontext_param);
1291old_ctx_end:
1292 return ret;
1293 }
8070f5c0 1294 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
1295 {
1296 struct lttng_kernel_context ucontext_param;
1297
1298 if (copy_from_user(&ucontext_param,
1299 (struct lttng_kernel_context __user *) arg,
1300 sizeof(ucontext_param)))
1301 return -EFAULT;
8070f5c0 1302 return lttng_abi_add_context(file,
6dccd6c1 1303 &ucontext_param,
8070f5c0 1304 &event->ctx, event->chan->session);
6dccd6c1
JD
1305 }
1306 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1307 case LTTNG_KERNEL_ENABLE:
a90917c3 1308 return lttng_event_enable(event);
6dccd6c1 1309 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1310 case LTTNG_KERNEL_DISABLE:
a90917c3 1311 return lttng_event_disable(event);
8070f5c0
MD
1312 default:
1313 return -ENOIOCTLCMD;
1314 }
1315}
1316
0a84a57f
MD
1317static
1318int lttng_event_release(struct inode *inode, struct file *file)
1319{
a90917c3 1320 struct lttng_event *event = file->private_data;
c269fff4 1321
aa7c23a9 1322 if (event)
c269fff4 1323 fput(event->chan->file);
0a84a57f
MD
1324 return 0;
1325}
1326
3b923e5b 1327/* TODO: filter control ioctl */
0a84a57f 1328static const struct file_operations lttng_event_fops = {
a33c9927 1329 .owner = THIS_MODULE,
0a84a57f 1330 .release = lttng_event_release,
8070f5c0
MD
1331 .unlocked_ioctl = lttng_event_ioctl,
1332#ifdef CONFIG_COMPAT
1333 .compat_ioctl = lttng_event_ioctl,
1334#endif
11b5a3c2 1335};
0a84a57f 1336
3b731ab1
JD
1337static int put_u64(uint64_t val, unsigned long arg)
1338{
1339 return put_user(val, (uint64_t __user *) arg);
1340}
1341
ed8d02d6
JD
1342static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1343 unsigned int cmd, unsigned long arg)
1344{
3b731ab1
JD
1345 struct lib_ring_buffer *buf = filp->private_data;
1346 struct channel *chan = buf->backend.chan;
1347 const struct lib_ring_buffer_config *config = &chan->backend.config;
1348 struct lttng_channel *lttng_chan = channel_get_private(chan);
1349 int ret;
1350
1351 if (atomic_read(&chan->record_disabled))
1352 return -EIO;
1353
ed8d02d6 1354 switch (cmd) {
3b731ab1
JD
1355 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1356 {
1357 uint64_t ts;
1358
1359 if (!lttng_chan->ops)
1360 goto error;
1361 ret = lttng_chan->ops->timestamp_begin(config, buf, &ts);
1362 if (ret < 0)
1363 goto error;
1364 return put_u64(ts, arg);
1365 }
1366 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1367 {
1368 uint64_t ts;
1369
1370 if (!lttng_chan->ops)
1371 goto error;
1372 ret = lttng_chan->ops->timestamp_end(config, buf, &ts);
1373 if (ret < 0)
1374 goto error;
1375 return put_u64(ts, arg);
1376 }
1377 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1378 {
1379 uint64_t ed;
1380
1381 if (!lttng_chan->ops)
1382 goto error;
1383 ret = lttng_chan->ops->events_discarded(config, buf, &ed);
1384 if (ret < 0)
1385 goto error;
1386 return put_u64(ed, arg);
1387 }
1388 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1389 {
1390 uint64_t cs;
1391
1392 if (!lttng_chan->ops)
1393 goto error;
1394 ret = lttng_chan->ops->content_size(config, buf, &cs);
1395 if (ret < 0)
1396 goto error;
1397 return put_u64(cs, arg);
1398 }
1399 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1400 {
1401 uint64_t ps;
1402
1403 if (!lttng_chan->ops)
1404 goto error;
1405 ret = lttng_chan->ops->packet_size(config, buf, &ps);
1406 if (ret < 0)
1407 goto error;
1408 return put_u64(ps, arg);
1409 }
1410 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1411 {
1412 uint64_t si;
1413
1414 if (!lttng_chan->ops)
1415 goto error;
1416 ret = lttng_chan->ops->stream_id(config, buf, &si);
1417 if (ret < 0)
1418 goto error;
1419 return put_u64(si, arg);
1420 }
2348ca17
JD
1421 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1422 {
1423 uint64_t ts;
1424
1425 if (!lttng_chan->ops)
1426 goto error;
1427 ret = lttng_chan->ops->current_timestamp(config, buf, &ts);
1428 if (ret < 0)
1429 goto error;
1430 return put_u64(ts, arg);
1431 }
3b731ab1
JD
1432 default:
1433 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1434 cmd, arg);
ed8d02d6 1435 }
3b731ab1
JD
1436
1437error:
1438 return -ENOSYS;
ed8d02d6
JD
1439}
1440
1441#ifdef CONFIG_COMPAT
1442static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1443 unsigned int cmd, unsigned long arg)
1444{
3b731ab1
JD
1445 struct lib_ring_buffer *buf = filp->private_data;
1446 struct channel *chan = buf->backend.chan;
1447 const struct lib_ring_buffer_config *config = &chan->backend.config;
1448 struct lttng_channel *lttng_chan = channel_get_private(chan);
1449 int ret;
1450
1451 if (atomic_read(&chan->record_disabled))
1452 return -EIO;
1453
ed8d02d6 1454 switch (cmd) {
3b731ab1
JD
1455 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1456 {
1457 uint64_t ts;
1458
1459 if (!lttng_chan->ops)
1460 goto error;
1461 ret = lttng_chan->ops->timestamp_begin(config, buf, &ts);
1462 if (ret < 0)
1463 goto error;
1464 return put_u64(ts, arg);
1465 }
1466 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1467 {
1468 uint64_t ts;
1469
1470 if (!lttng_chan->ops)
1471 goto error;
1472 ret = lttng_chan->ops->timestamp_end(config, buf, &ts);
1473 if (ret < 0)
1474 goto error;
1475 return put_u64(ts, arg);
1476 }
1477 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1478 {
1479 uint64_t ed;
1480
1481 if (!lttng_chan->ops)
1482 goto error;
1483 ret = lttng_chan->ops->events_discarded(config, buf, &ed);
1484 if (ret < 0)
1485 goto error;
1486 return put_u64(ed, arg);
ed8d02d6 1487 }
3b731ab1
JD
1488 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1489 {
1490 uint64_t cs;
1491
1492 if (!lttng_chan->ops)
1493 goto error;
1494 ret = lttng_chan->ops->content_size(config, buf, &cs);
1495 if (ret < 0)
1496 goto error;
1497 return put_u64(cs, arg);
1498 }
1499 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1500 {
1501 uint64_t ps;
1502
1503 if (!lttng_chan->ops)
1504 goto error;
1505 ret = lttng_chan->ops->packet_size(config, buf, &ps);
1506 if (ret < 0)
1507 goto error;
1508 return put_u64(ps, arg);
1509 }
1510 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1511 {
1512 uint64_t si;
1513
1514 if (!lttng_chan->ops)
1515 goto error;
1516 ret = lttng_chan->ops->stream_id(config, buf, &si);
1517 if (ret < 0)
1518 goto error;
1519 return put_u64(si, arg);
1520 }
2348ca17
JD
1521 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1522 {
1523 uint64_t ts;
1524
1525 if (!lttng_chan->ops)
1526 goto error;
1527 ret = lttng_chan->ops->current_timestamp(config, buf, &ts);
1528 if (ret < 0)
1529 goto error;
1530 return put_u64(ts, arg);
1531 }
3b731ab1
JD
1532 default:
1533 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1534 cmd, arg);
1535 }
1536
1537error:
1538 return -ENOSYS;
ed8d02d6
JD
1539}
1540#endif /* CONFIG_COMPAT */
1541
1542static void lttng_stream_override_ring_buffer_fops(void)
1543{
1544 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1545 lttng_stream_ring_buffer_file_operations.open =
1546 lib_ring_buffer_file_operations.open;
1547 lttng_stream_ring_buffer_file_operations.release =
1548 lib_ring_buffer_file_operations.release;
1549 lttng_stream_ring_buffer_file_operations.poll =
1550 lib_ring_buffer_file_operations.poll;
1551 lttng_stream_ring_buffer_file_operations.splice_read =
1552 lib_ring_buffer_file_operations.splice_read;
1553 lttng_stream_ring_buffer_file_operations.mmap =
1554 lib_ring_buffer_file_operations.mmap;
1555 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1556 lttng_stream_ring_buffer_ioctl;
1557 lttng_stream_ring_buffer_file_operations.llseek =
1558 lib_ring_buffer_file_operations.llseek;
1559#ifdef CONFIG_COMPAT
1560 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1561 lttng_stream_ring_buffer_compat_ioctl;
1562#endif
1563}
1564
80996790 1565int __init lttng_abi_init(void)
baf20995
MD
1566{
1567 int ret = 0;
1568
6d2a620c 1569 wrapper_vmalloc_sync_all();
d29348f7 1570 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
e6a17f26
MD
1571 &lttng_fops, NULL);
1572
255e52a4 1573 if (!lttng_proc_dentry) {
baf20995
MD
1574 printk(KERN_ERR "Error creating LTTng control file\n");
1575 ret = -ENOMEM;
1576 goto error;
1577 }
ed8d02d6
JD
1578 lttng_stream_override_ring_buffer_fops();
1579
baf20995
MD
1580error:
1581 return ret;
1582}
1583
80996790 1584void __exit lttng_abi_exit(void)
baf20995 1585{
e6a17f26
MD
1586 if (lttng_proc_dentry)
1587 remove_proc_entry("lttng", NULL);
baf20995 1588}
This page took 0.101932 seconds and 4 git commands to generate.