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