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