Fix: don't access packet header for stream_id and stream_instance_id getters
[deliverable/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
495 switch (cmd) {
496 case LTTNG_KERNEL_OLD_CHANNEL:
497 {
498 struct lttng_kernel_channel chan_param;
499 struct lttng_kernel_old_channel old_chan_param;
500
501 if (copy_from_user(&old_chan_param,
502 (struct lttng_kernel_old_channel __user *) arg,
503 sizeof(struct lttng_kernel_old_channel)))
504 return -EFAULT;
505 chan_param.overwrite = old_chan_param.overwrite;
506 chan_param.subbuf_size = old_chan_param.subbuf_size;
507 chan_param.num_subbuf = old_chan_param.num_subbuf;
508 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
509 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
510 chan_param.output = old_chan_param.output;
511
512 return lttng_abi_create_channel(file, &chan_param,
513 PER_CPU_CHANNEL);
514 }
515 case LTTNG_KERNEL_CHANNEL:
516 {
517 struct lttng_kernel_channel chan_param;
518
519 if (copy_from_user(&chan_param,
520 (struct lttng_kernel_channel __user *) arg,
521 sizeof(struct lttng_kernel_channel)))
522 return -EFAULT;
523 return lttng_abi_create_channel(file, &chan_param,
524 PER_CPU_CHANNEL);
525 }
526 case LTTNG_KERNEL_OLD_SESSION_START:
527 case LTTNG_KERNEL_OLD_ENABLE:
528 case LTTNG_KERNEL_SESSION_START:
529 case LTTNG_KERNEL_ENABLE:
530 return lttng_session_enable(session);
531 case LTTNG_KERNEL_OLD_SESSION_STOP:
532 case LTTNG_KERNEL_OLD_DISABLE:
533 case LTTNG_KERNEL_SESSION_STOP:
534 case LTTNG_KERNEL_DISABLE:
535 return lttng_session_disable(session);
536 case LTTNG_KERNEL_OLD_METADATA:
537 {
538 struct lttng_kernel_channel chan_param;
539 struct lttng_kernel_old_channel old_chan_param;
540
541 if (copy_from_user(&old_chan_param,
542 (struct lttng_kernel_old_channel __user *) arg,
543 sizeof(struct lttng_kernel_old_channel)))
544 return -EFAULT;
545 chan_param.overwrite = old_chan_param.overwrite;
546 chan_param.subbuf_size = old_chan_param.subbuf_size;
547 chan_param.num_subbuf = old_chan_param.num_subbuf;
548 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
549 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
550 chan_param.output = old_chan_param.output;
551
552 return lttng_abi_create_channel(file, &chan_param,
553 METADATA_CHANNEL);
554 }
555 case LTTNG_KERNEL_METADATA:
556 {
557 struct lttng_kernel_channel chan_param;
558
559 if (copy_from_user(&chan_param,
560 (struct lttng_kernel_channel __user *) arg,
561 sizeof(struct lttng_kernel_channel)))
562 return -EFAULT;
563 return lttng_abi_create_channel(file, &chan_param,
564 METADATA_CHANNEL);
565 }
566 case LTTNG_KERNEL_SESSION_TRACK_PID:
567 return lttng_session_track_pid(session, (int) arg);
568 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
569 return lttng_session_untrack_pid(session, (int) arg);
570 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
571 return lttng_session_list_tracker_pids(session);
572 case LTTNG_KERNEL_SESSION_METADATA_REGEN:
573 return lttng_session_metadata_regenerate(session);
574 case LTTNG_KERNEL_SESSION_STATEDUMP:
575 return lttng_session_statedump(session);
576 default:
577 return -ENOIOCTLCMD;
578 }
579 }
580
581 /*
582 * Called when the last file reference is dropped.
583 *
584 * Big fat note: channels and events are invariant for the whole session after
585 * their creation. So this session destruction also destroys all channel and
586 * event structures specific to this session (they are not destroyed when their
587 * individual file is released).
588 */
589 static
590 int lttng_session_release(struct inode *inode, struct file *file)
591 {
592 struct lttng_session *session = file->private_data;
593
594 if (session)
595 lttng_session_destroy(session);
596 return 0;
597 }
598
599 static const struct file_operations lttng_session_fops = {
600 .owner = THIS_MODULE,
601 .release = lttng_session_release,
602 .unlocked_ioctl = lttng_session_ioctl,
603 #ifdef CONFIG_COMPAT
604 .compat_ioctl = lttng_session_ioctl,
605 #endif
606 };
607
608 /**
609 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
610 * @filp: the file
611 * @wait: poll table
612 *
613 * Handles the poll operations for the metadata channels.
614 */
615 static
616 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
617 poll_table *wait)
618 {
619 struct lttng_metadata_stream *stream = filp->private_data;
620 struct lib_ring_buffer *buf = stream->priv;
621 int finalized;
622 unsigned int mask = 0;
623
624 if (filp->f_mode & FMODE_READ) {
625 poll_wait_set_exclusive(wait);
626 poll_wait(filp, &stream->read_wait, wait);
627
628 finalized = stream->finalized;
629
630 /*
631 * lib_ring_buffer_is_finalized() contains a smp_rmb()
632 * ordering finalized load before offsets loads.
633 */
634 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
635
636 if (finalized)
637 mask |= POLLHUP;
638
639 mutex_lock(&stream->metadata_cache->lock);
640 if (stream->metadata_cache->metadata_written >
641 stream->metadata_out)
642 mask |= POLLIN;
643 mutex_unlock(&stream->metadata_cache->lock);
644 }
645
646 return mask;
647 }
648
649 static
650 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
651 unsigned int cmd, unsigned long arg)
652 {
653 struct lttng_metadata_stream *stream = filp->private_data;
654
655 stream->metadata_out = stream->metadata_in;
656 }
657
658 static
659 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
660 unsigned int cmd, unsigned long arg)
661 {
662 int ret;
663 struct lttng_metadata_stream *stream = filp->private_data;
664 struct lib_ring_buffer *buf = stream->priv;
665
666 switch (cmd) {
667 case RING_BUFFER_GET_NEXT_SUBBUF:
668 {
669 struct lttng_metadata_stream *stream = filp->private_data;
670 struct lib_ring_buffer *buf = stream->priv;
671 struct channel *chan = buf->backend.chan;
672
673 ret = lttng_metadata_output_channel(stream, chan);
674 if (ret > 0) {
675 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
676 ret = 0;
677 } else if (ret < 0)
678 goto err;
679 break;
680 }
681 case RING_BUFFER_GET_SUBBUF:
682 {
683 /*
684 * Random access is not allowed for metadata channel.
685 */
686 return -ENOSYS;
687 }
688 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
689 case RING_BUFFER_FLUSH:
690 {
691 struct lttng_metadata_stream *stream = filp->private_data;
692 struct lib_ring_buffer *buf = stream->priv;
693 struct channel *chan = buf->backend.chan;
694
695 /*
696 * Before doing the actual ring buffer flush, write up to one
697 * packet of metadata in the ring buffer.
698 */
699 ret = lttng_metadata_output_channel(stream, chan);
700 if (ret < 0)
701 goto err;
702 break;
703 }
704 case RING_BUFFER_GET_METADATA_VERSION:
705 {
706 struct lttng_metadata_stream *stream = filp->private_data;
707
708 return put_u64(stream->version, arg);
709 }
710 default:
711 break;
712 }
713 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
714
715 /* Performing lib ring buffer ioctl after our own. */
716 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
717 if (ret < 0)
718 goto err;
719
720 switch (cmd) {
721 case RING_BUFFER_PUT_NEXT_SUBBUF:
722 {
723 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
724 cmd, arg);
725 break;
726 }
727 default:
728 break;
729 }
730 err:
731 return ret;
732 }
733
734 #ifdef CONFIG_COMPAT
735 static
736 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
737 unsigned int cmd, unsigned long arg)
738 {
739 int ret;
740 struct lttng_metadata_stream *stream = filp->private_data;
741 struct lib_ring_buffer *buf = stream->priv;
742
743 switch (cmd) {
744 case RING_BUFFER_GET_NEXT_SUBBUF:
745 {
746 struct lttng_metadata_stream *stream = filp->private_data;
747 struct lib_ring_buffer *buf = stream->priv;
748 struct channel *chan = buf->backend.chan;
749
750 ret = lttng_metadata_output_channel(stream, chan);
751 if (ret > 0) {
752 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
753 ret = 0;
754 } else if (ret < 0)
755 goto err;
756 break;
757 }
758 case RING_BUFFER_GET_SUBBUF:
759 {
760 /*
761 * Random access is not allowed for metadata channel.
762 */
763 return -ENOSYS;
764 }
765 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
766 case RING_BUFFER_FLUSH:
767 {
768 struct lttng_metadata_stream *stream = filp->private_data;
769 struct lib_ring_buffer *buf = stream->priv;
770 struct channel *chan = buf->backend.chan;
771
772 /*
773 * Before doing the actual ring buffer flush, write up to one
774 * packet of metadata in the ring buffer.
775 */
776 ret = lttng_metadata_output_channel(stream, chan);
777 if (ret < 0)
778 goto err;
779 break;
780 }
781 case RING_BUFFER_GET_METADATA_VERSION:
782 {
783 struct lttng_metadata_stream *stream = filp->private_data;
784
785 return put_u64(stream->version, arg);
786 }
787 default:
788 break;
789 }
790 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
791
792 /* Performing lib ring buffer ioctl after our own. */
793 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
794 if (ret < 0)
795 goto err;
796
797 switch (cmd) {
798 case RING_BUFFER_PUT_NEXT_SUBBUF:
799 {
800 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
801 cmd, arg);
802 break;
803 }
804 default:
805 break;
806 }
807 err:
808 return ret;
809 }
810 #endif
811
812 /*
813 * This is not used by anonymous file descriptors. This code is left
814 * there if we ever want to implement an inode with open() operation.
815 */
816 static
817 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
818 {
819 struct lttng_metadata_stream *stream = inode->i_private;
820 struct lib_ring_buffer *buf = stream->priv;
821
822 file->private_data = buf;
823 /*
824 * Since life-time of metadata cache differs from that of
825 * session, we need to keep our own reference on the transport.
826 */
827 if (!try_module_get(stream->transport->owner)) {
828 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
829 return -EBUSY;
830 }
831 return lib_ring_buffer_open(inode, file, buf);
832 }
833
834 static
835 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
836 {
837 struct lttng_metadata_stream *stream = file->private_data;
838 struct lib_ring_buffer *buf = stream->priv;
839
840 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
841 module_put(stream->transport->owner);
842 return lib_ring_buffer_release(inode, file, buf);
843 }
844
845 static
846 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
847 struct pipe_inode_info *pipe, size_t len,
848 unsigned int flags)
849 {
850 struct lttng_metadata_stream *stream = in->private_data;
851 struct lib_ring_buffer *buf = stream->priv;
852
853 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
854 flags, buf);
855 }
856
857 static
858 int lttng_metadata_ring_buffer_mmap(struct file *filp,
859 struct vm_area_struct *vma)
860 {
861 struct lttng_metadata_stream *stream = filp->private_data;
862 struct lib_ring_buffer *buf = stream->priv;
863
864 return lib_ring_buffer_mmap(filp, vma, buf);
865 }
866
867 static
868 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
869 .owner = THIS_MODULE,
870 .open = lttng_metadata_ring_buffer_open,
871 .release = lttng_metadata_ring_buffer_release,
872 .poll = lttng_metadata_ring_buffer_poll,
873 .splice_read = lttng_metadata_ring_buffer_splice_read,
874 .mmap = lttng_metadata_ring_buffer_mmap,
875 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
876 .llseek = vfs_lib_ring_buffer_no_llseek,
877 #ifdef CONFIG_COMPAT
878 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
879 #endif
880 };
881
882 static
883 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
884 const struct file_operations *fops)
885 {
886 int stream_fd, ret;
887 struct file *stream_file;
888
889 stream_fd = lttng_get_unused_fd();
890 if (stream_fd < 0) {
891 ret = stream_fd;
892 goto fd_error;
893 }
894 stream_file = anon_inode_getfile("[lttng_stream]", fops,
895 stream_priv, O_RDWR);
896 if (IS_ERR(stream_file)) {
897 ret = PTR_ERR(stream_file);
898 goto file_error;
899 }
900 /*
901 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
902 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
903 * file descriptor, so we set FMODE_PREAD here.
904 */
905 stream_file->f_mode |= FMODE_PREAD;
906 fd_install(stream_fd, stream_file);
907 /*
908 * The stream holds a reference to the channel within the generic ring
909 * buffer library, so no need to hold a refcount on the channel and
910 * session files here.
911 */
912 return stream_fd;
913
914 file_error:
915 put_unused_fd(stream_fd);
916 fd_error:
917 return ret;
918 }
919
920 static
921 int lttng_abi_open_stream(struct file *channel_file)
922 {
923 struct lttng_channel *channel = channel_file->private_data;
924 struct lib_ring_buffer *buf;
925 int ret;
926 void *stream_priv;
927
928 buf = channel->ops->buffer_read_open(channel->chan);
929 if (!buf)
930 return -ENOENT;
931
932 stream_priv = buf;
933 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
934 &lttng_stream_ring_buffer_file_operations);
935 if (ret < 0)
936 goto fd_error;
937
938 return ret;
939
940 fd_error:
941 channel->ops->buffer_read_close(buf);
942 return ret;
943 }
944
945 static
946 int lttng_abi_open_metadata_stream(struct file *channel_file)
947 {
948 struct lttng_channel *channel = channel_file->private_data;
949 struct lttng_session *session = channel->session;
950 struct lib_ring_buffer *buf;
951 int ret;
952 struct lttng_metadata_stream *metadata_stream;
953 void *stream_priv;
954
955 buf = channel->ops->buffer_read_open(channel->chan);
956 if (!buf)
957 return -ENOENT;
958
959 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
960 GFP_KERNEL);
961 if (!metadata_stream) {
962 ret = -ENOMEM;
963 goto nomem;
964 }
965 metadata_stream->metadata_cache = session->metadata_cache;
966 init_waitqueue_head(&metadata_stream->read_wait);
967 metadata_stream->priv = buf;
968 stream_priv = metadata_stream;
969 metadata_stream->transport = channel->transport;
970
971 /*
972 * Since life-time of metadata cache differs from that of
973 * session, we need to keep our own reference on the transport.
974 */
975 if (!try_module_get(metadata_stream->transport->owner)) {
976 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
977 ret = -EINVAL;
978 goto notransport;
979 }
980
981 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
982 ret = -EOVERFLOW;
983 goto kref_error;
984 }
985
986 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
987 &lttng_metadata_ring_buffer_file_operations);
988 if (ret < 0)
989 goto fd_error;
990
991 list_add(&metadata_stream->list,
992 &session->metadata_cache->metadata_stream);
993 return ret;
994
995 fd_error:
996 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
997 kref_error:
998 module_put(metadata_stream->transport->owner);
999 notransport:
1000 kfree(metadata_stream);
1001 nomem:
1002 channel->ops->buffer_read_close(buf);
1003 return ret;
1004 }
1005
1006 static
1007 int lttng_abi_create_event(struct file *channel_file,
1008 struct lttng_kernel_event *event_param)
1009 {
1010 struct lttng_channel *channel = channel_file->private_data;
1011 int event_fd, ret;
1012 struct file *event_file;
1013 void *priv;
1014
1015 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1016 switch (event_param->instrumentation) {
1017 case LTTNG_KERNEL_KRETPROBE:
1018 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1019 break;
1020 case LTTNG_KERNEL_KPROBE:
1021 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1022 break;
1023 case LTTNG_KERNEL_FUNCTION:
1024 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1025 break;
1026 default:
1027 break;
1028 }
1029 event_fd = lttng_get_unused_fd();
1030 if (event_fd < 0) {
1031 ret = event_fd;
1032 goto fd_error;
1033 }
1034 event_file = anon_inode_getfile("[lttng_event]",
1035 &lttng_event_fops,
1036 NULL, O_RDWR);
1037 if (IS_ERR(event_file)) {
1038 ret = PTR_ERR(event_file);
1039 goto file_error;
1040 }
1041 /* The event holds a reference on the channel */
1042 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
1043 ret = -EOVERFLOW;
1044 goto refcount_error;
1045 }
1046 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1047 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1048 struct lttng_enabler *enabler;
1049
1050 if (strutils_is_star_glob_pattern(event_param->name)) {
1051 /*
1052 * If the event name is a star globbing pattern,
1053 * we create the special star globbing enabler.
1054 */
1055 enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB,
1056 event_param, channel);
1057 } else {
1058 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
1059 event_param, channel);
1060 }
1061 priv = enabler;
1062 } else {
1063 struct lttng_event *event;
1064
1065 /*
1066 * We tolerate no failure path after event creation. It
1067 * will stay invariant for the rest of the session.
1068 */
1069 event = lttng_event_create(channel, event_param,
1070 NULL, NULL,
1071 event_param->instrumentation);
1072 WARN_ON_ONCE(!event);
1073 if (IS_ERR(event)) {
1074 ret = PTR_ERR(event);
1075 goto event_error;
1076 }
1077 priv = event;
1078 }
1079 event_file->private_data = priv;
1080 fd_install(event_fd, event_file);
1081 return event_fd;
1082
1083 event_error:
1084 atomic_long_dec(&channel_file->f_count);
1085 refcount_error:
1086 fput(event_file);
1087 file_error:
1088 put_unused_fd(event_fd);
1089 fd_error:
1090 return ret;
1091 }
1092
1093 /**
1094 * lttng_channel_ioctl - lttng syscall through ioctl
1095 *
1096 * @file: the file
1097 * @cmd: the command
1098 * @arg: command arg
1099 *
1100 * This ioctl implements lttng commands:
1101 * LTTNG_KERNEL_STREAM
1102 * Returns an event stream file descriptor or failure.
1103 * (typically, one event stream records events from one CPU)
1104 * LTTNG_KERNEL_EVENT
1105 * Returns an event file descriptor or failure.
1106 * LTTNG_KERNEL_CONTEXT
1107 * Prepend a context field to each event in the channel
1108 * LTTNG_KERNEL_ENABLE
1109 * Enable recording for events in this channel (weak enable)
1110 * LTTNG_KERNEL_DISABLE
1111 * Disable recording for events in this channel (strong disable)
1112 *
1113 * Channel and event file descriptors also hold a reference on the session.
1114 */
1115 static
1116 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1117 {
1118 struct lttng_channel *channel = file->private_data;
1119
1120 switch (cmd) {
1121 case LTTNG_KERNEL_OLD_STREAM:
1122 case LTTNG_KERNEL_STREAM:
1123 return lttng_abi_open_stream(file);
1124 case LTTNG_KERNEL_OLD_EVENT:
1125 {
1126 struct lttng_kernel_event *uevent_param;
1127 struct lttng_kernel_old_event *old_uevent_param;
1128 int ret;
1129
1130 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1131 GFP_KERNEL);
1132 if (!uevent_param) {
1133 ret = -ENOMEM;
1134 goto old_event_end;
1135 }
1136 old_uevent_param = kmalloc(
1137 sizeof(struct lttng_kernel_old_event),
1138 GFP_KERNEL);
1139 if (!old_uevent_param) {
1140 ret = -ENOMEM;
1141 goto old_event_error_free_param;
1142 }
1143 if (copy_from_user(old_uevent_param,
1144 (struct lttng_kernel_old_event __user *) arg,
1145 sizeof(struct lttng_kernel_old_event))) {
1146 ret = -EFAULT;
1147 goto old_event_error_free_old_param;
1148 }
1149
1150 memcpy(uevent_param->name, old_uevent_param->name,
1151 sizeof(uevent_param->name));
1152 uevent_param->instrumentation =
1153 old_uevent_param->instrumentation;
1154
1155 switch (old_uevent_param->instrumentation) {
1156 case LTTNG_KERNEL_KPROBE:
1157 uevent_param->u.kprobe.addr =
1158 old_uevent_param->u.kprobe.addr;
1159 uevent_param->u.kprobe.offset =
1160 old_uevent_param->u.kprobe.offset;
1161 memcpy(uevent_param->u.kprobe.symbol_name,
1162 old_uevent_param->u.kprobe.symbol_name,
1163 sizeof(uevent_param->u.kprobe.symbol_name));
1164 break;
1165 case LTTNG_KERNEL_KRETPROBE:
1166 uevent_param->u.kretprobe.addr =
1167 old_uevent_param->u.kretprobe.addr;
1168 uevent_param->u.kretprobe.offset =
1169 old_uevent_param->u.kretprobe.offset;
1170 memcpy(uevent_param->u.kretprobe.symbol_name,
1171 old_uevent_param->u.kretprobe.symbol_name,
1172 sizeof(uevent_param->u.kretprobe.symbol_name));
1173 break;
1174 case LTTNG_KERNEL_FUNCTION:
1175 memcpy(uevent_param->u.ftrace.symbol_name,
1176 old_uevent_param->u.ftrace.symbol_name,
1177 sizeof(uevent_param->u.ftrace.symbol_name));
1178 break;
1179 default:
1180 break;
1181 }
1182 ret = lttng_abi_create_event(file, uevent_param);
1183
1184 old_event_error_free_old_param:
1185 kfree(old_uevent_param);
1186 old_event_error_free_param:
1187 kfree(uevent_param);
1188 old_event_end:
1189 return ret;
1190 }
1191 case LTTNG_KERNEL_EVENT:
1192 {
1193 struct lttng_kernel_event uevent_param;
1194
1195 if (copy_from_user(&uevent_param,
1196 (struct lttng_kernel_event __user *) arg,
1197 sizeof(uevent_param)))
1198 return -EFAULT;
1199 return lttng_abi_create_event(file, &uevent_param);
1200 }
1201 case LTTNG_KERNEL_OLD_CONTEXT:
1202 {
1203 struct lttng_kernel_context *ucontext_param;
1204 struct lttng_kernel_old_context *old_ucontext_param;
1205 int ret;
1206
1207 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1208 GFP_KERNEL);
1209 if (!ucontext_param) {
1210 ret = -ENOMEM;
1211 goto old_ctx_end;
1212 }
1213 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1214 GFP_KERNEL);
1215 if (!old_ucontext_param) {
1216 ret = -ENOMEM;
1217 goto old_ctx_error_free_param;
1218 }
1219
1220 if (copy_from_user(old_ucontext_param,
1221 (struct lttng_kernel_old_context __user *) arg,
1222 sizeof(struct lttng_kernel_old_context))) {
1223 ret = -EFAULT;
1224 goto old_ctx_error_free_old_param;
1225 }
1226 ucontext_param->ctx = old_ucontext_param->ctx;
1227 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1228 sizeof(ucontext_param->padding));
1229 /* only type that uses the union */
1230 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1231 ucontext_param->u.perf_counter.type =
1232 old_ucontext_param->u.perf_counter.type;
1233 ucontext_param->u.perf_counter.config =
1234 old_ucontext_param->u.perf_counter.config;
1235 memcpy(ucontext_param->u.perf_counter.name,
1236 old_ucontext_param->u.perf_counter.name,
1237 sizeof(ucontext_param->u.perf_counter.name));
1238 }
1239
1240 ret = lttng_abi_add_context(file,
1241 ucontext_param,
1242 &channel->ctx, channel->session);
1243
1244 old_ctx_error_free_old_param:
1245 kfree(old_ucontext_param);
1246 old_ctx_error_free_param:
1247 kfree(ucontext_param);
1248 old_ctx_end:
1249 return ret;
1250 }
1251 case LTTNG_KERNEL_CONTEXT:
1252 {
1253 struct lttng_kernel_context ucontext_param;
1254
1255 if (copy_from_user(&ucontext_param,
1256 (struct lttng_kernel_context __user *) arg,
1257 sizeof(ucontext_param)))
1258 return -EFAULT;
1259 return lttng_abi_add_context(file,
1260 &ucontext_param,
1261 &channel->ctx, channel->session);
1262 }
1263 case LTTNG_KERNEL_OLD_ENABLE:
1264 case LTTNG_KERNEL_ENABLE:
1265 return lttng_channel_enable(channel);
1266 case LTTNG_KERNEL_OLD_DISABLE:
1267 case LTTNG_KERNEL_DISABLE:
1268 return lttng_channel_disable(channel);
1269 case LTTNG_KERNEL_SYSCALL_MASK:
1270 return lttng_channel_syscall_mask(channel,
1271 (struct lttng_kernel_syscall_mask __user *) arg);
1272 default:
1273 return -ENOIOCTLCMD;
1274 }
1275
1276 }
1277
1278 /**
1279 * lttng_metadata_ioctl - lttng syscall through ioctl
1280 *
1281 * @file: the file
1282 * @cmd: the command
1283 * @arg: command arg
1284 *
1285 * This ioctl implements lttng commands:
1286 * LTTNG_KERNEL_STREAM
1287 * Returns an event stream file descriptor or failure.
1288 *
1289 * Channel and event file descriptors also hold a reference on the session.
1290 */
1291 static
1292 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1293 {
1294 switch (cmd) {
1295 case LTTNG_KERNEL_OLD_STREAM:
1296 case LTTNG_KERNEL_STREAM:
1297 return lttng_abi_open_metadata_stream(file);
1298 default:
1299 return -ENOIOCTLCMD;
1300 }
1301 }
1302
1303 /**
1304 * lttng_channel_poll - lttng stream addition/removal monitoring
1305 *
1306 * @file: the file
1307 * @wait: poll table
1308 */
1309 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1310 {
1311 struct lttng_channel *channel = file->private_data;
1312 unsigned int mask = 0;
1313
1314 if (file->f_mode & FMODE_READ) {
1315 poll_wait_set_exclusive(wait);
1316 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1317 wait);
1318
1319 if (channel->ops->is_disabled(channel->chan))
1320 return POLLERR;
1321 if (channel->ops->is_finalized(channel->chan))
1322 return POLLHUP;
1323 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1324 return POLLIN | POLLRDNORM;
1325 return 0;
1326 }
1327 return mask;
1328
1329 }
1330
1331 static
1332 int lttng_channel_release(struct inode *inode, struct file *file)
1333 {
1334 struct lttng_channel *channel = file->private_data;
1335
1336 if (channel)
1337 fput(channel->session->file);
1338 return 0;
1339 }
1340
1341 static
1342 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1343 {
1344 struct lttng_channel *channel = file->private_data;
1345
1346 if (channel) {
1347 fput(channel->session->file);
1348 lttng_metadata_channel_destroy(channel);
1349 }
1350
1351 return 0;
1352 }
1353
1354 static const struct file_operations lttng_channel_fops = {
1355 .owner = THIS_MODULE,
1356 .release = lttng_channel_release,
1357 .poll = lttng_channel_poll,
1358 .unlocked_ioctl = lttng_channel_ioctl,
1359 #ifdef CONFIG_COMPAT
1360 .compat_ioctl = lttng_channel_ioctl,
1361 #endif
1362 };
1363
1364 static const struct file_operations lttng_metadata_fops = {
1365 .owner = THIS_MODULE,
1366 .release = lttng_metadata_channel_release,
1367 .unlocked_ioctl = lttng_metadata_ioctl,
1368 #ifdef CONFIG_COMPAT
1369 .compat_ioctl = lttng_metadata_ioctl,
1370 #endif
1371 };
1372
1373 /**
1374 * lttng_event_ioctl - lttng syscall through ioctl
1375 *
1376 * @file: the file
1377 * @cmd: the command
1378 * @arg: command arg
1379 *
1380 * This ioctl implements lttng commands:
1381 * LTTNG_KERNEL_CONTEXT
1382 * Prepend a context field to each record of this event
1383 * LTTNG_KERNEL_ENABLE
1384 * Enable recording for this event (weak enable)
1385 * LTTNG_KERNEL_DISABLE
1386 * Disable recording for this event (strong disable)
1387 */
1388 static
1389 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1390 {
1391 struct lttng_event *event;
1392 struct lttng_enabler *enabler;
1393 enum lttng_event_type *evtype = file->private_data;
1394
1395 switch (cmd) {
1396 case LTTNG_KERNEL_OLD_CONTEXT:
1397 {
1398 /* Not implemented */
1399 return -ENOSYS;
1400 }
1401 case LTTNG_KERNEL_CONTEXT:
1402 {
1403 /* Not implemented */
1404 return -ENOSYS;
1405 }
1406 case LTTNG_KERNEL_OLD_ENABLE:
1407 case LTTNG_KERNEL_ENABLE:
1408 switch (*evtype) {
1409 case LTTNG_TYPE_EVENT:
1410 event = file->private_data;
1411 return lttng_event_enable(event);
1412 case LTTNG_TYPE_ENABLER:
1413 enabler = file->private_data;
1414 return lttng_enabler_enable(enabler);
1415 default:
1416 WARN_ON_ONCE(1);
1417 return -ENOSYS;
1418 }
1419 case LTTNG_KERNEL_OLD_DISABLE:
1420 case LTTNG_KERNEL_DISABLE:
1421 switch (*evtype) {
1422 case LTTNG_TYPE_EVENT:
1423 event = file->private_data;
1424 return lttng_event_disable(event);
1425 case LTTNG_TYPE_ENABLER:
1426 enabler = file->private_data;
1427 return lttng_enabler_disable(enabler);
1428 default:
1429 WARN_ON_ONCE(1);
1430 return -ENOSYS;
1431 }
1432 case LTTNG_KERNEL_FILTER:
1433 switch (*evtype) {
1434 case LTTNG_TYPE_EVENT:
1435 return -EINVAL;
1436 case LTTNG_TYPE_ENABLER:
1437 {
1438 enabler = file->private_data;
1439 return lttng_enabler_attach_bytecode(enabler,
1440 (struct lttng_kernel_filter_bytecode __user *) arg);
1441 }
1442
1443 }
1444 default:
1445 return -ENOIOCTLCMD;
1446 }
1447 }
1448
1449 static
1450 int lttng_event_release(struct inode *inode, struct file *file)
1451 {
1452 struct lttng_event *event;
1453 struct lttng_enabler *enabler;
1454 enum lttng_event_type *evtype = file->private_data;
1455
1456 if (!evtype)
1457 return 0;
1458
1459 switch (*evtype) {
1460 case LTTNG_TYPE_EVENT:
1461 event = file->private_data;
1462 if (event)
1463 fput(event->chan->file);
1464 break;
1465 case LTTNG_TYPE_ENABLER:
1466 enabler = file->private_data;
1467 if (enabler)
1468 fput(enabler->chan->file);
1469 break;
1470 default:
1471 WARN_ON_ONCE(1);
1472 break;
1473 }
1474
1475 return 0;
1476 }
1477
1478 /* TODO: filter control ioctl */
1479 static const struct file_operations lttng_event_fops = {
1480 .owner = THIS_MODULE,
1481 .release = lttng_event_release,
1482 .unlocked_ioctl = lttng_event_ioctl,
1483 #ifdef CONFIG_COMPAT
1484 .compat_ioctl = lttng_event_ioctl,
1485 #endif
1486 };
1487
1488 static int put_u64(uint64_t val, unsigned long arg)
1489 {
1490 return put_user(val, (uint64_t __user *) arg);
1491 }
1492
1493 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1494 unsigned int cmd, unsigned long arg)
1495 {
1496 struct lib_ring_buffer *buf = filp->private_data;
1497 struct channel *chan = buf->backend.chan;
1498 const struct lib_ring_buffer_config *config = &chan->backend.config;
1499 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1500 int ret;
1501
1502 if (atomic_read(&chan->record_disabled))
1503 return -EIO;
1504
1505 switch (cmd) {
1506 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1507 {
1508 uint64_t ts;
1509
1510 ret = ops->timestamp_begin(config, buf, &ts);
1511 if (ret < 0)
1512 goto error;
1513 return put_u64(ts, arg);
1514 }
1515 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1516 {
1517 uint64_t ts;
1518
1519 ret = ops->timestamp_end(config, buf, &ts);
1520 if (ret < 0)
1521 goto error;
1522 return put_u64(ts, arg);
1523 }
1524 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1525 {
1526 uint64_t ed;
1527
1528 ret = ops->events_discarded(config, buf, &ed);
1529 if (ret < 0)
1530 goto error;
1531 return put_u64(ed, arg);
1532 }
1533 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1534 {
1535 uint64_t cs;
1536
1537 ret = ops->content_size(config, buf, &cs);
1538 if (ret < 0)
1539 goto error;
1540 return put_u64(cs, arg);
1541 }
1542 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1543 {
1544 uint64_t ps;
1545
1546 ret = ops->packet_size(config, buf, &ps);
1547 if (ret < 0)
1548 goto error;
1549 return put_u64(ps, arg);
1550 }
1551 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1552 {
1553 uint64_t si;
1554
1555 ret = ops->stream_id(config, buf, &si);
1556 if (ret < 0)
1557 goto error;
1558 return put_u64(si, arg);
1559 }
1560 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1561 {
1562 uint64_t ts;
1563
1564 ret = ops->current_timestamp(config, buf, &ts);
1565 if (ret < 0)
1566 goto error;
1567 return put_u64(ts, arg);
1568 }
1569 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1570 {
1571 uint64_t seq;
1572
1573 ret = ops->sequence_number(config, buf, &seq);
1574 if (ret < 0)
1575 goto error;
1576 return put_u64(seq, arg);
1577 }
1578 case LTTNG_RING_BUFFER_INSTANCE_ID:
1579 {
1580 uint64_t id;
1581
1582 ret = ops->instance_id(config, buf, &id);
1583 if (ret < 0)
1584 goto error;
1585 return put_u64(id, arg);
1586 }
1587 default:
1588 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1589 cmd, arg);
1590 }
1591
1592 error:
1593 return -ENOSYS;
1594 }
1595
1596 #ifdef CONFIG_COMPAT
1597 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1598 unsigned int cmd, unsigned long arg)
1599 {
1600 struct lib_ring_buffer *buf = filp->private_data;
1601 struct channel *chan = buf->backend.chan;
1602 const struct lib_ring_buffer_config *config = &chan->backend.config;
1603 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1604 int ret;
1605
1606 if (atomic_read(&chan->record_disabled))
1607 return -EIO;
1608
1609 switch (cmd) {
1610 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1611 {
1612 uint64_t ts;
1613
1614 ret = ops->timestamp_begin(config, buf, &ts);
1615 if (ret < 0)
1616 goto error;
1617 return put_u64(ts, arg);
1618 }
1619 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1620 {
1621 uint64_t ts;
1622
1623 ret = ops->timestamp_end(config, buf, &ts);
1624 if (ret < 0)
1625 goto error;
1626 return put_u64(ts, arg);
1627 }
1628 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1629 {
1630 uint64_t ed;
1631
1632 ret = ops->events_discarded(config, buf, &ed);
1633 if (ret < 0)
1634 goto error;
1635 return put_u64(ed, arg);
1636 }
1637 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1638 {
1639 uint64_t cs;
1640
1641 ret = ops->content_size(config, buf, &cs);
1642 if (ret < 0)
1643 goto error;
1644 return put_u64(cs, arg);
1645 }
1646 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1647 {
1648 uint64_t ps;
1649
1650 ret = ops->packet_size(config, buf, &ps);
1651 if (ret < 0)
1652 goto error;
1653 return put_u64(ps, arg);
1654 }
1655 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1656 {
1657 uint64_t si;
1658
1659 ret = ops->stream_id(config, buf, &si);
1660 if (ret < 0)
1661 goto error;
1662 return put_u64(si, arg);
1663 }
1664 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1665 {
1666 uint64_t ts;
1667
1668 ret = ops->current_timestamp(config, buf, &ts);
1669 if (ret < 0)
1670 goto error;
1671 return put_u64(ts, arg);
1672 }
1673 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1674 {
1675 uint64_t seq;
1676
1677 ret = ops->sequence_number(config, buf, &seq);
1678 if (ret < 0)
1679 goto error;
1680 return put_u64(seq, arg);
1681 }
1682 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
1683 {
1684 uint64_t id;
1685
1686 ret = ops->instance_id(config, buf, &id);
1687 if (ret < 0)
1688 goto error;
1689 return put_u64(id, arg);
1690 }
1691 default:
1692 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1693 cmd, arg);
1694 }
1695
1696 error:
1697 return -ENOSYS;
1698 }
1699 #endif /* CONFIG_COMPAT */
1700
1701 static void lttng_stream_override_ring_buffer_fops(void)
1702 {
1703 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1704 lttng_stream_ring_buffer_file_operations.open =
1705 lib_ring_buffer_file_operations.open;
1706 lttng_stream_ring_buffer_file_operations.release =
1707 lib_ring_buffer_file_operations.release;
1708 lttng_stream_ring_buffer_file_operations.poll =
1709 lib_ring_buffer_file_operations.poll;
1710 lttng_stream_ring_buffer_file_operations.splice_read =
1711 lib_ring_buffer_file_operations.splice_read;
1712 lttng_stream_ring_buffer_file_operations.mmap =
1713 lib_ring_buffer_file_operations.mmap;
1714 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1715 lttng_stream_ring_buffer_ioctl;
1716 lttng_stream_ring_buffer_file_operations.llseek =
1717 lib_ring_buffer_file_operations.llseek;
1718 #ifdef CONFIG_COMPAT
1719 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1720 lttng_stream_ring_buffer_compat_ioctl;
1721 #endif
1722 }
1723
1724 int __init lttng_abi_init(void)
1725 {
1726 int ret = 0;
1727
1728 wrapper_vmalloc_sync_all();
1729 lttng_clock_ref();
1730
1731 ret = lttng_tp_mempool_init();
1732 if (ret) {
1733 goto error;
1734 }
1735
1736 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1737 &lttng_fops, NULL);
1738
1739 if (!lttng_proc_dentry) {
1740 printk(KERN_ERR "Error creating LTTng control file\n");
1741 ret = -ENOMEM;
1742 goto error;
1743 }
1744 lttng_stream_override_ring_buffer_fops();
1745 return 0;
1746
1747 error:
1748 lttng_tp_mempool_destroy();
1749 lttng_clock_unref();
1750 return ret;
1751 }
1752
1753 /* No __exit annotation because used by init error path too. */
1754 void lttng_abi_exit(void)
1755 {
1756 lttng_tp_mempool_destroy();
1757 lttng_clock_unref();
1758 if (lttng_proc_dentry)
1759 remove_proc_entry("lttng", NULL);
1760 }
This page took 0.077811 seconds and 5 git commands to generate.