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