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