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