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