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