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