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