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