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