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