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