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