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