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