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