Handle release of parent files (header)
[lttng-modules.git] / ltt-debugfs-abi.c
CommitLineData
baf20995
MD
1/*
2 * ltt-debugfs-abi.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng debugfs ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns a file descriptor or failure.
ad1c05e1
MD
10 * - channel creation, returns a file descriptor or failure.
11 * - Operates on a session file descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns a file descriptor or failure.
14 * - Operates on a channel file descriptor.
15 * - stream notifier get, returns a file descriptor or failure.
16 * - Operates on a channel file descriptor.
17 * - event creation, returns a file descriptor or failure.
18 * - Operates on a channel file descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
baf20995
MD
23 */
24
25#include <linux/debugfs.h>
ad1c05e1 26#include "ltt-events.h"
baf20995
MD
27
28/*
29 * This is LTTng's own personal way to create a system call as an external
30 * module. We use ioctl() on /sys/kernel/debug/lttng.
31 */
32
33static struct dentry *lttng_dentry;
ad1c05e1
MD
34static const struct file_operations lttng_fops;
35static const struct file_operations lttng_session_fops;
36static const struct file_operations lttng_channel_fops;
305d3e42 37static const struct file_operations lttng_event_fops;
baf20995
MD
38
39/*
40 * LTTng DebugFS ABI structures.
41 */
42
43struct lttng_channel {
baf20995
MD
44 int overwrite; /* 1: overwrite, 0: discard */
45 u64 subbuf_size;
46 u64 num_subbuf;
47 unsigned int switch_timer_interval;
48 unsigned int read_timer_interval;
49};
50
51struct lttng_event {
baf20995
MD
52 enum instrum_type itype;
53 char name[];
54};
55
ad1c05e1 56static
baf20995
MD
57int lttng_abi_create_session(void)
58{
59 struct ltt_session *session;
03037b98 60 struct file *session_filp;
baf20995
MD
61 int session_fd;
62
653fe716 63 session = ltt_session_create();
baf20995
MD
64 if (!session)
65 return -ENOMEM;
66 session_fd = get_unused_fd_flags(O_RDWR);
67 if (session_fd < 0) {
68 ret = session_fd;
69 goto fd_error;
70 }
03037b98 71 session_filp = anon_inode_getfile("[lttng_session]",
ad1c05e1 72 &lttng_session_fops,
baf20995 73 session, O_RDWR);
03037b98
MD
74 if (IS_ERR(session_filp)) {
75 ret = PTR_ERR(session_filp);
baf20995
MD
76 goto file_error;
77 }
0a84a57f 78 session->file = session_filp;
03037b98 79 fd_install(session_fd, session_filp);
baf20995
MD
80 return session_fd;
81
82file_error:
83 put_unused_fd(session_fd);
84fd_error:
85 ltt_session_destroy(session);
86 return ret;
87}
88
ad1c05e1
MD
89/**
90 * lttng_ioctl - lttng syscall through ioctl
91 *
92 * @filp: the file
93 * @cmd: the command
94 * @arg: command arg
95 *
96 * This ioctl implements lttng commands:
97 * LTTNG_SESSION
98 * Returns a LTTng trace session file descriptor
99 *
100 * The returned session will be deleted when its file descriptor is closed.
101 */
102static
103long lttng_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
104{
105 switch (cmd) {
106 case LTTNG_SESSION:
107 return lttng_abi_create_session();
108 default:
109 return -ENOIOCTLCMD;
110 }
111}
112
ad1c05e1
MD
113static const struct file_operations lttng_fops = {
114 .unlocked_ioctl = lttng_ioctl,
115#ifdef CONFIG_COMPAT
03037b98 116 .compat_ioctl = lttng_ioctl,
ad1c05e1
MD
117#endif
118}
119
120int lttng_abi_create_channel(struct file *session_filp,
121 struct lttng_channel __user *uchan_param)
baf20995 122{
ad1c05e1 123 struct ltt_session *session = session_filp->private_data;
baf20995 124 struct ltt_channel *chan;
ad1c05e1 125 struct file *chan_filp;
baf20995
MD
126 struct lttng_channel chan_param;
127 int chan_fd;
ad1c05e1 128 int ret = 0;
baf20995 129
653fe716 130 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 131 return -EFAULT;
baf20995
MD
132 chan_fd = get_unused_fd_flags(O_RDWR);
133 if (chan_fd < 0) {
134 ret = chan_fd;
135 goto fd_error;
136 }
ad1c05e1
MD
137 chan_filp = anon_inode_getfile("[lttng_channel]",
138 &lttng_channel_fops,
03037b98 139 NULL, O_RDWR);
ad1c05e1
MD
140 if (IS_ERR(chan_filp)) {
141 ret = PTR_ERR(chan_filp);
baf20995
MD
142 goto file_error;
143 }
03037b98
MD
144 /*
145 * We tolerate no failure path after channel creation. It will stay
146 * invariant for the rest of the session.
147 */
148 chan = ltt_channel_create(session, chan_param->overwrite, NULL,
149 chan_param->subbuf_size,
150 chan_param->num_subbuf,
151 chan_param->switch_timer_interval,
152 chan_param->read_timer_interval);
153 if (!chan) {
154 ret = -ENOMEM;
155 goto chan_error;
156 }
0a84a57f 157 channel->file = chan_filp;
03037b98
MD
158 chan_filp->private_data = chan;
159 fd_install(chan_fd, chan_filp);
ad1c05e1
MD
160 /* The channel created holds a reference on the session */
161 atomic_inc(&session_filp->f_count);
162
baf20995
MD
163 return chan_fd;
164
03037b98
MD
165chan_error:
166 fput(chan_filp);
baf20995
MD
167file_error:
168 put_unused_fd(chan_fd);
169fd_error:
baf20995
MD
170 return ret;
171}
172
173/**
ad1c05e1 174 * lttng_session_ioctl - lttng session fd ioctl
baf20995
MD
175 *
176 * @filp: the file
177 * @cmd: the command
178 * @arg: command arg
179 *
180 * This ioctl implements lttng commands:
baf20995
MD
181 * LTTNG_CHANNEL
182 * Returns a LTTng channel file descriptor
ad1c05e1
MD
183 *
184 * The returned channel will be deleted when its file descriptor is closed.
185 */
186static
187long lttng_session_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
188{
189 switch (cmd) {
190 case LTTNG_CHANNEL:
191 return lttng_abi_create_channel(filp, (struct lttng_channel __user *)arg);
192 default:
193 return -ENOIOCTLCMD;
194 }
195}
196
03037b98
MD
197/*
198 * Called when the last file reference is dropped.
199 *
200 * Big fat note: channels and events are invariant for the whole session after
201 * their creation. So this session destruction also destroys all channel and
202 * event structures specific to this session (they are not destroyed when their
203 * individual file is released).
204 */
ad1c05e1 205static
03037b98 206int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 207{
03037b98
MD
208 struct ltt_session *session = file->private_data;
209 return ltt_session_destroy(session);
ad1c05e1 210}
ad1c05e1
MD
211
212static const struct file_operations lttng_session_fops = {
03037b98 213 .release = lttng_session_release,
ad1c05e1
MD
214 .unlocked_ioctl = lttng_session_ioctl,
215#ifdef CONFIG_COMPAT
03037b98 216 .compat_ioctl = lttng_session_ioctl,
ad1c05e1
MD
217#endif
218}
219
220static
221int lttng_abi_open_stream(struct file *channel_filp)
222{
223 struct ltt_channel *channel = channel_filp->private_data;
224 struct lib_ring_buffer *buf;
225 int stream_fd, ret;
226
227 buf = ltt_buffer_read_open(channel->chan);
228 if (!buf)
229 return -ENOENT;
230
231 stream_fd = get_unused_fd_flags(O_RDWR);
232 if (stream_fd < 0) {
233 ret = stream_fd;
234 goto fd_error;
235 }
236 stream_filp = anon_inode_getfile("[lttng_stream]",
7f57c73c 237 &lib_ring_buffer_file_operations,
ad1c05e1
MD
238 buf, O_RDWR);
239 if (IS_ERR(stream_filp)) {
240 ret = PTR_ERR(stream_filp);
241 goto file_error;
242 }
03037b98 243 fd_install(stream_fd, stream_filp);
ad1c05e1
MD
244 /* The stream holds a reference on the channel */
245 atomic_inc(&channel_filp->f_count);
246 return stream_fd;
247
248file_error:
249 put_unused_fd(stream_fd);
250fd_error:
251 ltt_buffer_read_close(buf);
252 return ret;
253}
254
653fe716
MD
255static
256int lttng_abi_create_event(struct file *channel_filp,
257 struct lttng_event __user *uevent_param)
258{
259 struct ltt_channel *channel = channel_filp->private_data;
260 struct ltt_event *event;
261 char *event_name;
262 struct lttng_event event_param;
263 int event_fd, ret;
264
265 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
266 return -EFAULT;
267 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
268 if (!event_name)
269 return -ENOMEM;
270 if (strncpy_from_user(event_name, &uevent_param->name, PATH_MAX)) {
271 ret = -EFAULT;
272 goto name_error;
273 }
274 event_name[PATH_MAX - 1] = '\0';
653fe716
MD
275 event_fd = get_unused_fd_flags(O_RDWR);
276 if (event_fd < 0) {
277 ret = event_fd;
278 goto fd_error;
279 }
280 event_filp = anon_inode_getfile("[lttng_event]",
305d3e42 281 &lttng_event_fops, /* TODO: filter */
03037b98 282 NULL, O_RDWR);
653fe716
MD
283 if (IS_ERR(event_filp)) {
284 ret = PTR_ERR(event_filp);
285 goto file_error;
286 }
03037b98
MD
287 /*
288 * We tolerate no failure path after event creation. It will stay
289 * invariant for the rest of the session.
290 */
291 event = ltt_event_create(channel, event_param->itype, event_name, NULL);
292 if (!event) {
293 goto event_error;
294 ret = -EEXIST;
295 }
296 event_filp->private_data = event;
297 fd_install(event_fd, event_filp);
653fe716
MD
298 /* The event holds a reference on the channel */
299 atomic_inc(&channel_filp->f_count);
03037b98 300 kfree(event_name);
653fe716
MD
301 return event_fd;
302
03037b98
MD
303event_error:
304 fput(event_filp);
653fe716
MD
305file_error:
306 put_unused_fd(event_fd);
307fd_error:
653fe716
MD
308name_error:
309 kfree(event_name);
310 return ret;
311}
ad1c05e1
MD
312
313/**
314 * lttng_channel_ioctl - lttng syscall through ioctl
315 *
316 * @filp: the file
317 * @cmd: the command
318 * @arg: command arg
319 *
320 * This ioctl implements lttng commands:
321 * LTTNG_STREAM
322 * Returns an event stream file descriptor or failure.
323 * (typically, one event stream records events from one CPU)
baf20995 324 * LTTNG_EVENT
ad1c05e1 325 * Returns an event file descriptor or failure.
baf20995
MD
326 *
327 * The returned session will be deleted when its file descriptor is closed.
328 * Channel and event file descriptors also hold a reference on the session.
329 */
ad1c05e1
MD
330static
331long lttng_channel_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
baf20995 332{
baf20995 333 switch (cmd) {
ad1c05e1
MD
334 case LTTNG_STREAM:
335 return lttng_abi_open_stream(filp);
baf20995 336 case LTTNG_EVENT:
ad1c05e1 337 return lttng_abi_create_event(filp, (struct lttng_event __user *)arg);
baf20995
MD
338 default:
339 return -ENOIOCTLCMD;
340 }
341}
342
653fe716
MD
343/**
344 * lttng_channel_poll - lttng stream addition/removal monitoring
345 *
346 * @filp: the file
347 * @wait: poll table
348 */
349unsigned int lttng_channel_poll(struct file *filp, poll_table *wait)
350{
351 struct ltt_channel *channel = filp->private_data;
352 unsigned int mask = 0;
353
354 if (filp->f_mode & FMODE_READ) {
355 poll_wait_set_exclusive(wait);
356 poll_wait(filp, &channel->notify_wait, wait);
357
358 /* TODO: identify when the channel is being finalized. */
359 if (finalized)
360 return POLLHUP;
361 else
362 return POLLIN | POLLRDNORM;
363 }
364 return mask;
365
366}
367
0a84a57f
MD
368static
369int lttng_channel_release(struct inode *inode, struct file *file)
370{
371 struct ltt_channel *channel = file->private_data;
372 fput(channel->session->file);
373 return 0;
374}
375
ad1c05e1 376static const struct file_operations lttng_channel_fops = {
03037b98 377 .release = lttng_channel_release,
653fe716 378 .poll = lttng_channel_poll,
ad1c05e1 379 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 380#ifdef CONFIG_COMPAT
03037b98 381 .compat_ioctl = lttng_channel_ioctl,
baf20995
MD
382#endif
383}
384
0a84a57f
MD
385static
386int lttng_event_release(struct inode *inode, struct file *file)
387{
388 struct ltt_event *event = file->private_data;
389 fput(event->chan->file);
390 return 0;
391}
392
393static const struct file_operations lttng_event_fops = {
394 .release = lttng_event_release,
395}
396
baf20995
MD
397static int __init ltt_debugfs_abi_init(void)
398{
399 int ret = 0;
400
401 lttng_dentry = debugfs_create_file("lttng", NULL);
402 if (IS_ERR(lttng_dentry) || !lttng_dentry)
403 printk(KERN_ERR "Error creating LTTng control file\n");
404 ret = -ENOMEM;
405 goto error;
406 }
407error:
408 return ret;
409}
410
411static void __exit ltt_debugfs_abi_exit(void)
412{
413 debugfs_remote(lttng_dentry);
414}
This page took 0.039658 seconds and 4 git commands to generate.