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