lttng-abi.c: fix whitespaces
[deliverable/lttng-modules.git] / lttng-abi.c
1 /*
2 * lttng-abi.c
3 *
4 * LTTng ABI
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
38 */
39
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include <linux/err.h>
47 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
48 #include <wrapper/ringbuffer/vfs.h>
49 #include <wrapper/ringbuffer/backend.h>
50 #include <wrapper/ringbuffer/frontend.h>
51 #include <wrapper/poll.h>
52 #include <wrapper/file.h>
53 #include <wrapper/kref.h>
54 #include <lttng-string-utils.h>
55 #include <lttng-abi.h>
56 #include <lttng-abi-old.h>
57 #include <lttng-events.h>
58 #include <lttng-tracer.h>
59 #include <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:
688 {
689 struct lttng_metadata_stream *stream = filp->private_data;
690 struct lib_ring_buffer *buf = stream->priv;
691 struct channel *chan = buf->backend.chan;
692
693 /*
694 * Before doing the actual ring buffer flush, write up to one
695 * packet of metadata in the ring buffer.
696 */
697 ret = lttng_metadata_output_channel(stream, chan);
698 if (ret < 0)
699 goto err;
700 break;
701 }
702 case RING_BUFFER_GET_METADATA_VERSION:
703 {
704 struct lttng_metadata_stream *stream = filp->private_data;
705
706 return put_u64(stream->version, arg);
707 }
708 case RING_BUFFER_SNAPSHOT:
709 {
710 /*
711 * Force the buffer to quiescent so the ring buffer
712 * don't attempt to perform a SWITCH_FLUSH, which would
713 * desynchronize the client accounting of the amount of
714 * data available in the buffer from the ring buffer
715 * view.
716 */
717 buf->quiescent = true;
718 break;
719 }
720 default:
721 break;
722 }
723 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
724
725 /* Performing lib ring buffer ioctl after our own. */
726 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
727 if (ret < 0)
728 goto err;
729
730 switch (cmd) {
731 case RING_BUFFER_PUT_NEXT_SUBBUF:
732 {
733 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
734 cmd, arg);
735 break;
736 }
737 default:
738 break;
739 }
740 err:
741 return ret;
742 }
743
744 #ifdef CONFIG_COMPAT
745 static
746 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
747 unsigned int cmd, unsigned long arg)
748 {
749 int ret;
750 struct lttng_metadata_stream *stream = filp->private_data;
751 struct lib_ring_buffer *buf = stream->priv;
752
753 switch (cmd) {
754 case RING_BUFFER_GET_NEXT_SUBBUF:
755 {
756 struct lttng_metadata_stream *stream = filp->private_data;
757 struct lib_ring_buffer *buf = stream->priv;
758 struct channel *chan = buf->backend.chan;
759
760 ret = lttng_metadata_output_channel(stream, chan);
761 if (ret > 0) {
762 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
763 ret = 0;
764 } else if (ret < 0)
765 goto err;
766 break;
767 }
768 case RING_BUFFER_GET_SUBBUF:
769 {
770 /*
771 * Random access is not allowed for metadata channel.
772 */
773 return -ENOSYS;
774 }
775 case RING_BUFFER_FLUSH:
776 {
777 struct lttng_metadata_stream *stream = filp->private_data;
778 struct lib_ring_buffer *buf = stream->priv;
779 struct channel *chan = buf->backend.chan;
780
781 /*
782 * Before doing the actual ring buffer flush, write up to one
783 * packet of metadata in the ring buffer.
784 */
785 ret = lttng_metadata_output_channel(stream, chan);
786 if (ret < 0)
787 goto err;
788 break;
789 }
790 case RING_BUFFER_GET_METADATA_VERSION:
791 {
792 struct lttng_metadata_stream *stream = filp->private_data;
793
794 return put_u64(stream->version, arg);
795 }
796 case RING_BUFFER_SNAPSHOT:
797 {
798 /*
799 * Force the buffer to quiescent so the ring buffer
800 * don't attempt to perform a SWITCH_FLUSH, which would
801 * desynchronize the client accounting of the amount of
802 * data available in the buffer from the ring buffer
803 * view.
804 */
805 buf->quiescent = true;
806 break;
807 }
808 default:
809 break;
810 }
811 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
812
813 /* Performing lib ring buffer ioctl after our own. */
814 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
815 if (ret < 0)
816 goto err;
817
818 switch (cmd) {
819 case RING_BUFFER_PUT_NEXT_SUBBUF:
820 {
821 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
822 cmd, arg);
823 break;
824 }
825 default:
826 break;
827 }
828 err:
829 return ret;
830 }
831 #endif
832
833 /*
834 * This is not used by anonymous file descriptors. This code is left
835 * there if we ever want to implement an inode with open() operation.
836 */
837 static
838 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
839 {
840 struct lttng_metadata_stream *stream = inode->i_private;
841 struct lib_ring_buffer *buf = stream->priv;
842
843 file->private_data = buf;
844 /*
845 * Since life-time of metadata cache differs from that of
846 * session, we need to keep our own reference on the transport.
847 */
848 if (!try_module_get(stream->transport->owner)) {
849 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
850 return -EBUSY;
851 }
852 return lib_ring_buffer_open(inode, file, buf);
853 }
854
855 static
856 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
857 {
858 struct lttng_metadata_stream *stream = file->private_data;
859 struct lib_ring_buffer *buf = stream->priv;
860
861 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
862 module_put(stream->transport->owner);
863 return lib_ring_buffer_release(inode, file, buf);
864 }
865
866 static
867 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
868 struct pipe_inode_info *pipe, size_t len,
869 unsigned int flags)
870 {
871 struct lttng_metadata_stream *stream = in->private_data;
872 struct lib_ring_buffer *buf = stream->priv;
873
874 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
875 flags, buf);
876 }
877
878 static
879 int lttng_metadata_ring_buffer_mmap(struct file *filp,
880 struct vm_area_struct *vma)
881 {
882 struct lttng_metadata_stream *stream = filp->private_data;
883 struct lib_ring_buffer *buf = stream->priv;
884
885 return lib_ring_buffer_mmap(filp, vma, buf);
886 }
887
888 static
889 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
890 .owner = THIS_MODULE,
891 .open = lttng_metadata_ring_buffer_open,
892 .release = lttng_metadata_ring_buffer_release,
893 .poll = lttng_metadata_ring_buffer_poll,
894 .splice_read = lttng_metadata_ring_buffer_splice_read,
895 .mmap = lttng_metadata_ring_buffer_mmap,
896 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
897 .llseek = vfs_lib_ring_buffer_no_llseek,
898 #ifdef CONFIG_COMPAT
899 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
900 #endif
901 };
902
903 static
904 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
905 const struct file_operations *fops)
906 {
907 int stream_fd, ret;
908 struct file *stream_file;
909
910 stream_fd = lttng_get_unused_fd();
911 if (stream_fd < 0) {
912 ret = stream_fd;
913 goto fd_error;
914 }
915 stream_file = anon_inode_getfile("[lttng_stream]", fops,
916 stream_priv, O_RDWR);
917 if (IS_ERR(stream_file)) {
918 ret = PTR_ERR(stream_file);
919 goto file_error;
920 }
921 /*
922 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
923 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
924 * file descriptor, so we set FMODE_PREAD here.
925 */
926 stream_file->f_mode |= FMODE_PREAD;
927 fd_install(stream_fd, stream_file);
928 /*
929 * The stream holds a reference to the channel within the generic ring
930 * buffer library, so no need to hold a refcount on the channel and
931 * session files here.
932 */
933 return stream_fd;
934
935 file_error:
936 put_unused_fd(stream_fd);
937 fd_error:
938 return ret;
939 }
940
941 static
942 int lttng_abi_open_stream(struct file *channel_file)
943 {
944 struct lttng_channel *channel = channel_file->private_data;
945 struct lib_ring_buffer *buf;
946 int ret;
947 void *stream_priv;
948
949 buf = channel->ops->buffer_read_open(channel->chan);
950 if (!buf)
951 return -ENOENT;
952
953 stream_priv = buf;
954 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
955 &lttng_stream_ring_buffer_file_operations);
956 if (ret < 0)
957 goto fd_error;
958
959 return ret;
960
961 fd_error:
962 channel->ops->buffer_read_close(buf);
963 return ret;
964 }
965
966 static
967 int lttng_abi_open_metadata_stream(struct file *channel_file)
968 {
969 struct lttng_channel *channel = channel_file->private_data;
970 struct lttng_session *session = channel->session;
971 struct lib_ring_buffer *buf;
972 int ret;
973 struct lttng_metadata_stream *metadata_stream;
974 void *stream_priv;
975
976 buf = channel->ops->buffer_read_open(channel->chan);
977 if (!buf)
978 return -ENOENT;
979
980 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
981 GFP_KERNEL);
982 if (!metadata_stream) {
983 ret = -ENOMEM;
984 goto nomem;
985 }
986 metadata_stream->metadata_cache = session->metadata_cache;
987 init_waitqueue_head(&metadata_stream->read_wait);
988 metadata_stream->priv = buf;
989 stream_priv = metadata_stream;
990 metadata_stream->transport = channel->transport;
991
992 /*
993 * Since life-time of metadata cache differs from that of
994 * session, we need to keep our own reference on the transport.
995 */
996 if (!try_module_get(metadata_stream->transport->owner)) {
997 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
998 ret = -EINVAL;
999 goto notransport;
1000 }
1001
1002 if (!lttng_kref_get(&session->metadata_cache->refcount))
1003 goto kref_error;
1004 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1005 &lttng_metadata_ring_buffer_file_operations);
1006 if (ret < 0)
1007 goto fd_error;
1008
1009 list_add(&metadata_stream->list,
1010 &session->metadata_cache->metadata_stream);
1011 return ret;
1012
1013 fd_error:
1014 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1015 kref_error:
1016 module_put(metadata_stream->transport->owner);
1017 notransport:
1018 kfree(metadata_stream);
1019 nomem:
1020 channel->ops->buffer_read_close(buf);
1021 return ret;
1022 }
1023
1024 static
1025 int lttng_abi_create_event(struct file *channel_file,
1026 struct lttng_kernel_event *event_param)
1027 {
1028 struct lttng_channel *channel = channel_file->private_data;
1029 int event_fd, ret;
1030 struct file *event_file;
1031 void *priv;
1032
1033 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1034 switch (event_param->instrumentation) {
1035 case LTTNG_KERNEL_KRETPROBE:
1036 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1037 break;
1038 case LTTNG_KERNEL_KPROBE:
1039 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1040 break;
1041 case LTTNG_KERNEL_FUNCTION:
1042 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1043 break;
1044 default:
1045 break;
1046 }
1047 event_fd = lttng_get_unused_fd();
1048 if (event_fd < 0) {
1049 ret = event_fd;
1050 goto fd_error;
1051 }
1052 event_file = anon_inode_getfile("[lttng_event]",
1053 &lttng_event_fops,
1054 NULL, O_RDWR);
1055 if (IS_ERR(event_file)) {
1056 ret = PTR_ERR(event_file);
1057 goto file_error;
1058 }
1059 /* The event holds a reference on the channel */
1060 if (atomic_long_add_unless(&channel_file->f_count,
1061 1, INT_MAX) == INT_MAX) {
1062 ret = -EOVERFLOW;
1063 goto refcount_error;
1064 }
1065 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1066 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1067 struct lttng_enabler *enabler;
1068
1069 if (strutils_is_star_glob_pattern(event_param->name)) {
1070 /*
1071 * If the event name is a star globbing pattern,
1072 * we create the special star globbing enabler.
1073 */
1074 enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB,
1075 event_param, channel);
1076 } else {
1077 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
1078 event_param, channel);
1079 }
1080 priv = enabler;
1081 } else {
1082 struct lttng_event *event;
1083
1084 /*
1085 * We tolerate no failure path after event creation. It
1086 * will stay invariant for the rest of the session.
1087 */
1088 event = lttng_event_create(channel, event_param,
1089 NULL, NULL,
1090 event_param->instrumentation);
1091 WARN_ON_ONCE(!event);
1092 if (IS_ERR(event)) {
1093 ret = PTR_ERR(event);
1094 goto event_error;
1095 }
1096 priv = event;
1097 }
1098 event_file->private_data = priv;
1099 fd_install(event_fd, event_file);
1100 return event_fd;
1101
1102 event_error:
1103 atomic_long_dec(&channel_file->f_count);
1104 refcount_error:
1105 fput(event_file);
1106 file_error:
1107 put_unused_fd(event_fd);
1108 fd_error:
1109 return ret;
1110 }
1111
1112 /**
1113 * lttng_channel_ioctl - lttng syscall through ioctl
1114 *
1115 * @file: the file
1116 * @cmd: the command
1117 * @arg: command arg
1118 *
1119 * This ioctl implements lttng commands:
1120 * LTTNG_KERNEL_STREAM
1121 * Returns an event stream file descriptor or failure.
1122 * (typically, one event stream records events from one CPU)
1123 * LTTNG_KERNEL_EVENT
1124 * Returns an event file descriptor or failure.
1125 * LTTNG_KERNEL_CONTEXT
1126 * Prepend a context field to each event in the channel
1127 * LTTNG_KERNEL_ENABLE
1128 * Enable recording for events in this channel (weak enable)
1129 * LTTNG_KERNEL_DISABLE
1130 * Disable recording for events in this channel (strong disable)
1131 *
1132 * Channel and event file descriptors also hold a reference on the session.
1133 */
1134 static
1135 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1136 {
1137 struct lttng_channel *channel = file->private_data;
1138
1139 switch (cmd) {
1140 case LTTNG_KERNEL_OLD_STREAM:
1141 case LTTNG_KERNEL_STREAM:
1142 return lttng_abi_open_stream(file);
1143 case LTTNG_KERNEL_OLD_EVENT:
1144 {
1145 struct lttng_kernel_event *uevent_param;
1146 struct lttng_kernel_old_event *old_uevent_param;
1147 int ret;
1148
1149 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1150 GFP_KERNEL);
1151 if (!uevent_param) {
1152 ret = -ENOMEM;
1153 goto old_event_end;
1154 }
1155 old_uevent_param = kmalloc(
1156 sizeof(struct lttng_kernel_old_event),
1157 GFP_KERNEL);
1158 if (!old_uevent_param) {
1159 ret = -ENOMEM;
1160 goto old_event_error_free_param;
1161 }
1162 if (copy_from_user(old_uevent_param,
1163 (struct lttng_kernel_old_event __user *) arg,
1164 sizeof(struct lttng_kernel_old_event))) {
1165 ret = -EFAULT;
1166 goto old_event_error_free_old_param;
1167 }
1168
1169 memcpy(uevent_param->name, old_uevent_param->name,
1170 sizeof(uevent_param->name));
1171 uevent_param->instrumentation =
1172 old_uevent_param->instrumentation;
1173
1174 switch (old_uevent_param->instrumentation) {
1175 case LTTNG_KERNEL_KPROBE:
1176 uevent_param->u.kprobe.addr =
1177 old_uevent_param->u.kprobe.addr;
1178 uevent_param->u.kprobe.offset =
1179 old_uevent_param->u.kprobe.offset;
1180 memcpy(uevent_param->u.kprobe.symbol_name,
1181 old_uevent_param->u.kprobe.symbol_name,
1182 sizeof(uevent_param->u.kprobe.symbol_name));
1183 break;
1184 case LTTNG_KERNEL_KRETPROBE:
1185 uevent_param->u.kretprobe.addr =
1186 old_uevent_param->u.kretprobe.addr;
1187 uevent_param->u.kretprobe.offset =
1188 old_uevent_param->u.kretprobe.offset;
1189 memcpy(uevent_param->u.kretprobe.symbol_name,
1190 old_uevent_param->u.kretprobe.symbol_name,
1191 sizeof(uevent_param->u.kretprobe.symbol_name));
1192 break;
1193 case LTTNG_KERNEL_FUNCTION:
1194 memcpy(uevent_param->u.ftrace.symbol_name,
1195 old_uevent_param->u.ftrace.symbol_name,
1196 sizeof(uevent_param->u.ftrace.symbol_name));
1197 break;
1198 default:
1199 break;
1200 }
1201 ret = lttng_abi_create_event(file, uevent_param);
1202
1203 old_event_error_free_old_param:
1204 kfree(old_uevent_param);
1205 old_event_error_free_param:
1206 kfree(uevent_param);
1207 old_event_end:
1208 return ret;
1209 }
1210 case LTTNG_KERNEL_EVENT:
1211 {
1212 struct lttng_kernel_event uevent_param;
1213
1214 if (copy_from_user(&uevent_param,
1215 (struct lttng_kernel_event __user *) arg,
1216 sizeof(uevent_param)))
1217 return -EFAULT;
1218 return lttng_abi_create_event(file, &uevent_param);
1219 }
1220 case LTTNG_KERNEL_OLD_CONTEXT:
1221 {
1222 struct lttng_kernel_context *ucontext_param;
1223 struct lttng_kernel_old_context *old_ucontext_param;
1224 int ret;
1225
1226 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1227 GFP_KERNEL);
1228 if (!ucontext_param) {
1229 ret = -ENOMEM;
1230 goto old_ctx_end;
1231 }
1232 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1233 GFP_KERNEL);
1234 if (!old_ucontext_param) {
1235 ret = -ENOMEM;
1236 goto old_ctx_error_free_param;
1237 }
1238
1239 if (copy_from_user(old_ucontext_param,
1240 (struct lttng_kernel_old_context __user *) arg,
1241 sizeof(struct lttng_kernel_old_context))) {
1242 ret = -EFAULT;
1243 goto old_ctx_error_free_old_param;
1244 }
1245 ucontext_param->ctx = old_ucontext_param->ctx;
1246 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1247 sizeof(ucontext_param->padding));
1248 /* only type that uses the union */
1249 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1250 ucontext_param->u.perf_counter.type =
1251 old_ucontext_param->u.perf_counter.type;
1252 ucontext_param->u.perf_counter.config =
1253 old_ucontext_param->u.perf_counter.config;
1254 memcpy(ucontext_param->u.perf_counter.name,
1255 old_ucontext_param->u.perf_counter.name,
1256 sizeof(ucontext_param->u.perf_counter.name));
1257 }
1258
1259 ret = lttng_abi_add_context(file,
1260 ucontext_param,
1261 &channel->ctx, channel->session);
1262
1263 old_ctx_error_free_old_param:
1264 kfree(old_ucontext_param);
1265 old_ctx_error_free_param:
1266 kfree(ucontext_param);
1267 old_ctx_end:
1268 return ret;
1269 }
1270 case LTTNG_KERNEL_CONTEXT:
1271 {
1272 struct lttng_kernel_context ucontext_param;
1273
1274 if (copy_from_user(&ucontext_param,
1275 (struct lttng_kernel_context __user *) arg,
1276 sizeof(ucontext_param)))
1277 return -EFAULT;
1278 return lttng_abi_add_context(file,
1279 &ucontext_param,
1280 &channel->ctx, channel->session);
1281 }
1282 case LTTNG_KERNEL_OLD_ENABLE:
1283 case LTTNG_KERNEL_ENABLE:
1284 return lttng_channel_enable(channel);
1285 case LTTNG_KERNEL_OLD_DISABLE:
1286 case LTTNG_KERNEL_DISABLE:
1287 return lttng_channel_disable(channel);
1288 case LTTNG_KERNEL_SYSCALL_MASK:
1289 return lttng_channel_syscall_mask(channel,
1290 (struct lttng_kernel_syscall_mask __user *) arg);
1291 default:
1292 return -ENOIOCTLCMD;
1293 }
1294
1295 }
1296
1297 /**
1298 * lttng_metadata_ioctl - lttng syscall through ioctl
1299 *
1300 * @file: the file
1301 * @cmd: the command
1302 * @arg: command arg
1303 *
1304 * This ioctl implements lttng commands:
1305 * LTTNG_KERNEL_STREAM
1306 * Returns an event stream file descriptor or failure.
1307 *
1308 * Channel and event file descriptors also hold a reference on the session.
1309 */
1310 static
1311 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1312 {
1313 switch (cmd) {
1314 case LTTNG_KERNEL_OLD_STREAM:
1315 case LTTNG_KERNEL_STREAM:
1316 return lttng_abi_open_metadata_stream(file);
1317 default:
1318 return -ENOIOCTLCMD;
1319 }
1320 }
1321
1322 /**
1323 * lttng_channel_poll - lttng stream addition/removal monitoring
1324 *
1325 * @file: the file
1326 * @wait: poll table
1327 */
1328 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1329 {
1330 struct lttng_channel *channel = file->private_data;
1331 unsigned int mask = 0;
1332
1333 if (file->f_mode & FMODE_READ) {
1334 poll_wait_set_exclusive(wait);
1335 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1336 wait);
1337
1338 if (channel->ops->is_disabled(channel->chan))
1339 return POLLERR;
1340 if (channel->ops->is_finalized(channel->chan))
1341 return POLLHUP;
1342 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1343 return POLLIN | POLLRDNORM;
1344 return 0;
1345 }
1346 return mask;
1347
1348 }
1349
1350 static
1351 int lttng_channel_release(struct inode *inode, struct file *file)
1352 {
1353 struct lttng_channel *channel = file->private_data;
1354
1355 if (channel)
1356 fput(channel->session->file);
1357 return 0;
1358 }
1359
1360 static
1361 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1362 {
1363 struct lttng_channel *channel = file->private_data;
1364
1365 if (channel) {
1366 fput(channel->session->file);
1367 lttng_metadata_channel_destroy(channel);
1368 }
1369
1370 return 0;
1371 }
1372
1373 static const struct file_operations lttng_channel_fops = {
1374 .owner = THIS_MODULE,
1375 .release = lttng_channel_release,
1376 .poll = lttng_channel_poll,
1377 .unlocked_ioctl = lttng_channel_ioctl,
1378 #ifdef CONFIG_COMPAT
1379 .compat_ioctl = lttng_channel_ioctl,
1380 #endif
1381 };
1382
1383 static const struct file_operations lttng_metadata_fops = {
1384 .owner = THIS_MODULE,
1385 .release = lttng_metadata_channel_release,
1386 .unlocked_ioctl = lttng_metadata_ioctl,
1387 #ifdef CONFIG_COMPAT
1388 .compat_ioctl = lttng_metadata_ioctl,
1389 #endif
1390 };
1391
1392 /**
1393 * lttng_event_ioctl - lttng syscall through ioctl
1394 *
1395 * @file: the file
1396 * @cmd: the command
1397 * @arg: command arg
1398 *
1399 * This ioctl implements lttng commands:
1400 * LTTNG_KERNEL_CONTEXT
1401 * Prepend a context field to each record of this event
1402 * LTTNG_KERNEL_ENABLE
1403 * Enable recording for this event (weak enable)
1404 * LTTNG_KERNEL_DISABLE
1405 * Disable recording for this event (strong disable)
1406 */
1407 static
1408 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1409 {
1410 struct lttng_event *event;
1411 struct lttng_enabler *enabler;
1412 enum lttng_event_type *evtype = file->private_data;
1413
1414 switch (cmd) {
1415 case LTTNG_KERNEL_OLD_CONTEXT:
1416 {
1417 /* Not implemented */
1418 return -ENOSYS;
1419 }
1420 case LTTNG_KERNEL_CONTEXT:
1421 {
1422 /* Not implemented */
1423 return -ENOSYS;
1424 }
1425 case LTTNG_KERNEL_OLD_ENABLE:
1426 case LTTNG_KERNEL_ENABLE:
1427 switch (*evtype) {
1428 case LTTNG_TYPE_EVENT:
1429 event = file->private_data;
1430 return lttng_event_enable(event);
1431 case LTTNG_TYPE_ENABLER:
1432 enabler = file->private_data;
1433 return lttng_enabler_enable(enabler);
1434 default:
1435 WARN_ON_ONCE(1);
1436 return -ENOSYS;
1437 }
1438 case LTTNG_KERNEL_OLD_DISABLE:
1439 case LTTNG_KERNEL_DISABLE:
1440 switch (*evtype) {
1441 case LTTNG_TYPE_EVENT:
1442 event = file->private_data;
1443 return lttng_event_disable(event);
1444 case LTTNG_TYPE_ENABLER:
1445 enabler = file->private_data;
1446 return lttng_enabler_disable(enabler);
1447 default:
1448 WARN_ON_ONCE(1);
1449 return -ENOSYS;
1450 }
1451 case LTTNG_KERNEL_FILTER:
1452 switch (*evtype) {
1453 case LTTNG_TYPE_EVENT:
1454 return -EINVAL;
1455 case LTTNG_TYPE_ENABLER:
1456 {
1457 enabler = file->private_data;
1458 return lttng_enabler_attach_bytecode(enabler,
1459 (struct lttng_kernel_filter_bytecode __user *) arg);
1460 }
1461
1462 }
1463 default:
1464 return -ENOIOCTLCMD;
1465 }
1466 }
1467
1468 static
1469 int lttng_event_release(struct inode *inode, struct file *file)
1470 {
1471 struct lttng_event *event;
1472 struct lttng_enabler *enabler;
1473 enum lttng_event_type *evtype = file->private_data;
1474
1475 if (!evtype)
1476 return 0;
1477
1478 switch (*evtype) {
1479 case LTTNG_TYPE_EVENT:
1480 event = file->private_data;
1481 if (event)
1482 fput(event->chan->file);
1483 break;
1484 case LTTNG_TYPE_ENABLER:
1485 enabler = file->private_data;
1486 if (enabler)
1487 fput(enabler->chan->file);
1488 break;
1489 default:
1490 WARN_ON_ONCE(1);
1491 break;
1492 }
1493
1494 return 0;
1495 }
1496
1497 /* TODO: filter control ioctl */
1498 static const struct file_operations lttng_event_fops = {
1499 .owner = THIS_MODULE,
1500 .release = lttng_event_release,
1501 .unlocked_ioctl = lttng_event_ioctl,
1502 #ifdef CONFIG_COMPAT
1503 .compat_ioctl = lttng_event_ioctl,
1504 #endif
1505 };
1506
1507 static int put_u64(uint64_t val, unsigned long arg)
1508 {
1509 return put_user(val, (uint64_t __user *) arg);
1510 }
1511
1512 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1513 unsigned int cmd, unsigned long arg)
1514 {
1515 struct lib_ring_buffer *buf = filp->private_data;
1516 struct channel *chan = buf->backend.chan;
1517 const struct lib_ring_buffer_config *config = &chan->backend.config;
1518 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1519 int ret;
1520
1521 if (atomic_read(&chan->record_disabled))
1522 return -EIO;
1523
1524 switch (cmd) {
1525 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1526 {
1527 uint64_t ts;
1528
1529 ret = ops->timestamp_begin(config, buf, &ts);
1530 if (ret < 0)
1531 goto error;
1532 return put_u64(ts, arg);
1533 }
1534 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1535 {
1536 uint64_t ts;
1537
1538 ret = ops->timestamp_end(config, buf, &ts);
1539 if (ret < 0)
1540 goto error;
1541 return put_u64(ts, arg);
1542 }
1543 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1544 {
1545 uint64_t ed;
1546
1547 ret = ops->events_discarded(config, buf, &ed);
1548 if (ret < 0)
1549 goto error;
1550 return put_u64(ed, arg);
1551 }
1552 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1553 {
1554 uint64_t cs;
1555
1556 ret = ops->content_size(config, buf, &cs);
1557 if (ret < 0)
1558 goto error;
1559 return put_u64(cs, arg);
1560 }
1561 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1562 {
1563 uint64_t ps;
1564
1565 ret = ops->packet_size(config, buf, &ps);
1566 if (ret < 0)
1567 goto error;
1568 return put_u64(ps, arg);
1569 }
1570 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1571 {
1572 uint64_t si;
1573
1574 ret = ops->stream_id(config, buf, &si);
1575 if (ret < 0)
1576 goto error;
1577 return put_u64(si, arg);
1578 }
1579 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1580 {
1581 uint64_t ts;
1582
1583 ret = ops->current_timestamp(config, buf, &ts);
1584 if (ret < 0)
1585 goto error;
1586 return put_u64(ts, arg);
1587 }
1588 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1589 {
1590 uint64_t seq;
1591
1592 ret = ops->sequence_number(config, buf, &seq);
1593 if (ret < 0)
1594 goto error;
1595 return put_u64(seq, arg);
1596 }
1597 case LTTNG_RING_BUFFER_INSTANCE_ID:
1598 {
1599 uint64_t id;
1600
1601 ret = ops->instance_id(config, buf, &id);
1602 if (ret < 0)
1603 goto error;
1604 return put_u64(id, arg);
1605 }
1606 default:
1607 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1608 cmd, arg);
1609 }
1610
1611 error:
1612 return -ENOSYS;
1613 }
1614
1615 #ifdef CONFIG_COMPAT
1616 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1617 unsigned int cmd, unsigned long arg)
1618 {
1619 struct lib_ring_buffer *buf = filp->private_data;
1620 struct channel *chan = buf->backend.chan;
1621 const struct lib_ring_buffer_config *config = &chan->backend.config;
1622 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1623 int ret;
1624
1625 if (atomic_read(&chan->record_disabled))
1626 return -EIO;
1627
1628 switch (cmd) {
1629 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1630 {
1631 uint64_t ts;
1632
1633 ret = ops->timestamp_begin(config, buf, &ts);
1634 if (ret < 0)
1635 goto error;
1636 return put_u64(ts, arg);
1637 }
1638 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1639 {
1640 uint64_t ts;
1641
1642 ret = ops->timestamp_end(config, buf, &ts);
1643 if (ret < 0)
1644 goto error;
1645 return put_u64(ts, arg);
1646 }
1647 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1648 {
1649 uint64_t ed;
1650
1651 ret = ops->events_discarded(config, buf, &ed);
1652 if (ret < 0)
1653 goto error;
1654 return put_u64(ed, arg);
1655 }
1656 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1657 {
1658 uint64_t cs;
1659
1660 ret = ops->content_size(config, buf, &cs);
1661 if (ret < 0)
1662 goto error;
1663 return put_u64(cs, arg);
1664 }
1665 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1666 {
1667 uint64_t ps;
1668
1669 ret = ops->packet_size(config, buf, &ps);
1670 if (ret < 0)
1671 goto error;
1672 return put_u64(ps, arg);
1673 }
1674 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1675 {
1676 uint64_t si;
1677
1678 ret = ops->stream_id(config, buf, &si);
1679 if (ret < 0)
1680 goto error;
1681 return put_u64(si, arg);
1682 }
1683 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1684 {
1685 uint64_t ts;
1686
1687 ret = ops->current_timestamp(config, buf, &ts);
1688 if (ret < 0)
1689 goto error;
1690 return put_u64(ts, arg);
1691 }
1692 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1693 {
1694 uint64_t seq;
1695
1696 ret = ops->sequence_number(config, buf, &seq);
1697 if (ret < 0)
1698 goto error;
1699 return put_u64(seq, arg);
1700 }
1701 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
1702 {
1703 uint64_t id;
1704
1705 ret = ops->instance_id(config, buf, &id);
1706 if (ret < 0)
1707 goto error;
1708 return put_u64(id, arg);
1709 }
1710 default:
1711 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1712 cmd, arg);
1713 }
1714
1715 error:
1716 return -ENOSYS;
1717 }
1718 #endif /* CONFIG_COMPAT */
1719
1720 static void lttng_stream_override_ring_buffer_fops(void)
1721 {
1722 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1723 lttng_stream_ring_buffer_file_operations.open =
1724 lib_ring_buffer_file_operations.open;
1725 lttng_stream_ring_buffer_file_operations.release =
1726 lib_ring_buffer_file_operations.release;
1727 lttng_stream_ring_buffer_file_operations.poll =
1728 lib_ring_buffer_file_operations.poll;
1729 lttng_stream_ring_buffer_file_operations.splice_read =
1730 lib_ring_buffer_file_operations.splice_read;
1731 lttng_stream_ring_buffer_file_operations.mmap =
1732 lib_ring_buffer_file_operations.mmap;
1733 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1734 lttng_stream_ring_buffer_ioctl;
1735 lttng_stream_ring_buffer_file_operations.llseek =
1736 lib_ring_buffer_file_operations.llseek;
1737 #ifdef CONFIG_COMPAT
1738 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1739 lttng_stream_ring_buffer_compat_ioctl;
1740 #endif
1741 }
1742
1743 int __init lttng_abi_init(void)
1744 {
1745 int ret = 0;
1746
1747 wrapper_vmalloc_sync_all();
1748 lttng_clock_ref();
1749 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1750 &lttng_fops, NULL);
1751
1752 if (!lttng_proc_dentry) {
1753 printk(KERN_ERR "Error creating LTTng control file\n");
1754 ret = -ENOMEM;
1755 goto error;
1756 }
1757 lttng_stream_override_ring_buffer_fops();
1758 return 0;
1759
1760 error:
1761 lttng_clock_unref();
1762 return ret;
1763 }
1764
1765 /* No __exit annotation because used by init error path too. */
1766 void lttng_abi_exit(void)
1767 {
1768 lttng_clock_unref();
1769 if (lttng_proc_dentry)
1770 remove_proc_entry("lttng", NULL);
1771 }
This page took 0.091764 seconds and 5 git commands to generate.