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