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