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