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