Fix cleanup: move lttng-tracer-core.h include to lib ringbuffer config.h
[lttng-modules.git] / lib / ringbuffer / ring_buffer_vfs.c
CommitLineData
f3bc08c5
MD
1/*
2 * ring_buffer_vfs.c
3 *
f3bc08c5
MD
4 * Ring Buffer VFS file operations.
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
f3bc08c5
MD
21 */
22
23#include <linux/module.h>
24#include <linux/fs.h>
25#include <linux/compat.h>
26
27#include "../../wrapper/ringbuffer/backend.h"
28#include "../../wrapper/ringbuffer/frontend.h"
29#include "../../wrapper/ringbuffer/vfs.h"
30#include "../../wrapper/poll.h"
31
32static int put_ulong(unsigned long val, unsigned long arg)
33{
34 return put_user(val, (unsigned long __user *)arg);
35}
36
37#ifdef CONFIG_COMPAT
38static int compat_put_ulong(compat_ulong_t val, unsigned long arg)
39{
40 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
41}
42#endif
43
44/**
45 * lib_ring_buffer_open - ring buffer open file operation
46 * @inode: opened inode
47 * @file: opened file
48 *
49 * Open implementation. Makes sure only one open instance of a buffer is
50 * done at a given moment.
51 */
52int lib_ring_buffer_open(struct inode *inode, struct file *file)
53{
54 struct lib_ring_buffer *buf = inode->i_private;
55 int ret;
56
733ce41d
MD
57 if (!buf)
58 return -EINVAL;
59
f3bc08c5
MD
60 ret = lib_ring_buffer_open_read(buf);
61 if (ret)
62 return ret;
63
64 file->private_data = buf;
65 ret = nonseekable_open(inode, file);
66 if (ret)
67 goto release_read;
68 return 0;
69
70release_read:
71 lib_ring_buffer_release_read(buf);
72 return ret;
73}
74
75/**
76 * lib_ring_buffer_release - ring buffer release file operation
77 * @inode: opened inode
78 * @file: opened file
79 *
80 * Release implementation.
81 */
82int lib_ring_buffer_release(struct inode *inode, struct file *file)
83{
84 struct lib_ring_buffer *buf = file->private_data;
85
86 lib_ring_buffer_release_read(buf);
87
88 return 0;
89}
90
91/**
92 * lib_ring_buffer_poll - ring buffer poll file operation
93 * @filp: the file
94 * @wait: poll table
95 *
96 * Poll implementation.
97 */
98unsigned int lib_ring_buffer_poll(struct file *filp, poll_table *wait)
99{
100 unsigned int mask = 0;
101 struct lib_ring_buffer *buf = filp->private_data;
102 struct channel *chan = buf->backend.chan;
5a8fd222 103 const struct lib_ring_buffer_config *config = &chan->backend.config;
254ec7bc 104 int finalized, disabled;
f3bc08c5
MD
105
106 if (filp->f_mode & FMODE_READ) {
a33e44a6 107 poll_wait_set_exclusive(wait);
f3bc08c5
MD
108 poll_wait(filp, &buf->read_wait, wait);
109
110 finalized = lib_ring_buffer_is_finalized(config, buf);
254ec7bc
MD
111 disabled = lib_ring_buffer_channel_is_disabled(chan);
112
f3bc08c5
MD
113 /*
114 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
115 * finalized load before offsets loads.
116 */
117 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
118retry:
254ec7bc
MD
119 if (disabled)
120 return POLLERR;
121
f3bc08c5
MD
122 if (subbuf_trunc(lib_ring_buffer_get_offset(config, buf), chan)
123 - subbuf_trunc(lib_ring_buffer_get_consumed(config, buf), chan)
124 == 0) {
125 if (finalized)
126 return POLLHUP;
127 else {
128 /*
129 * The memory barriers
130 * __wait_event()/wake_up_interruptible() take
131 * care of "raw_spin_is_locked" memory ordering.
132 */
133 if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
134 goto retry;
135 else
136 return 0;
137 }
138 } else {
139 if (subbuf_trunc(lib_ring_buffer_get_offset(config, buf),
140 chan)
141 - subbuf_trunc(lib_ring_buffer_get_consumed(config, buf),
142 chan)
143 >= chan->backend.buf_size)
144 return POLLPRI | POLLRDBAND;
145 else
146 return POLLIN | POLLRDNORM;
147 }
148 }
149 return mask;
150}
151
152/**
153 * lib_ring_buffer_ioctl - control ring buffer reader synchronization
154 *
155 * @filp: the file
156 * @cmd: the command
157 * @arg: command arg
158 *
159 * This ioctl implements commands necessary for producer/consumer
160 * and flight recorder reader interaction :
161 * RING_BUFFER_GET_NEXT_SUBBUF
162 * Get the next sub-buffer that can be read. It never blocks.
163 * RING_BUFFER_PUT_NEXT_SUBBUF
164 * Release the currently read sub-buffer.
165 * RING_BUFFER_GET_SUBBUF_SIZE
166 * returns the size of the current sub-buffer.
167 * RING_BUFFER_GET_MAX_SUBBUF_SIZE
168 * returns the maximum size for sub-buffers.
169 * RING_BUFFER_GET_NUM_SUBBUF
170 * returns the number of reader-visible sub-buffers in the per cpu
171 * channel (for mmap).
172 * RING_BUFFER_GET_MMAP_READ_OFFSET
173 * returns the offset of the subbuffer belonging to the reader.
174 * Should only be used for mmap clients.
175 */
176long lib_ring_buffer_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
177{
178 struct lib_ring_buffer *buf = filp->private_data;
179 struct channel *chan = buf->backend.chan;
5a8fd222 180 const struct lib_ring_buffer_config *config = &chan->backend.config;
f3bc08c5 181
254ec7bc
MD
182 if (lib_ring_buffer_channel_is_disabled(chan))
183 return -EIO;
184
f3bc08c5
MD
185 switch (cmd) {
186 case RING_BUFFER_SNAPSHOT:
187 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
188 &buf->prod_snapshot);
189 case RING_BUFFER_SNAPSHOT_GET_CONSUMED:
190 return put_ulong(buf->cons_snapshot, arg);
191 case RING_BUFFER_SNAPSHOT_GET_PRODUCED:
192 return put_ulong(buf->prod_snapshot, arg);
193 case RING_BUFFER_GET_SUBBUF:
194 {
195 unsigned long uconsume;
196 long ret;
197
198 ret = get_user(uconsume, (unsigned long __user *) arg);
199 if (ret)
200 return ret; /* will return -EFAULT */
201 ret = lib_ring_buffer_get_subbuf(buf, uconsume);
202 if (!ret) {
203 /* Set file position to zero at each successful "get" */
204 filp->f_pos = 0;
205 }
206 return ret;
207 }
208 case RING_BUFFER_PUT_SUBBUF:
209 lib_ring_buffer_put_subbuf(buf);
210 return 0;
211
212 case RING_BUFFER_GET_NEXT_SUBBUF:
213 {
214 long ret;
215
216 ret = lib_ring_buffer_get_next_subbuf(buf);
217 if (!ret) {
218 /* Set file position to zero at each successful "get" */
219 filp->f_pos = 0;
220 }
221 return ret;
222 }
223 case RING_BUFFER_PUT_NEXT_SUBBUF:
224 lib_ring_buffer_put_next_subbuf(buf);
225 return 0;
226 case RING_BUFFER_GET_SUBBUF_SIZE:
227 return put_ulong(lib_ring_buffer_get_read_data_size(config, buf),
228 arg);
229 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE:
230 {
231 unsigned long size;
232
233 size = lib_ring_buffer_get_read_data_size(config, buf);
234 size = PAGE_ALIGN(size);
235 return put_ulong(size, arg);
236 }
237 case RING_BUFFER_GET_MAX_SUBBUF_SIZE:
238 return put_ulong(chan->backend.subbuf_size, arg);
239 case RING_BUFFER_GET_MMAP_LEN:
240 {
241 unsigned long mmap_buf_len;
242
243 if (config->output != RING_BUFFER_MMAP)
244 return -EINVAL;
245 mmap_buf_len = chan->backend.buf_size;
246 if (chan->backend.extra_reader_sb)
247 mmap_buf_len += chan->backend.subbuf_size;
248 if (mmap_buf_len > INT_MAX)
249 return -EFBIG;
250 return put_ulong(mmap_buf_len, arg);
251 }
252 case RING_BUFFER_GET_MMAP_READ_OFFSET:
253 {
254 unsigned long sb_bindex;
255
256 if (config->output != RING_BUFFER_MMAP)
257 return -EINVAL;
258 sb_bindex = subbuffer_id_get_index(config,
259 buf->backend.buf_rsb.id);
260 return put_ulong(buf->backend.array[sb_bindex]->mmap_offset,
261 arg);
262 }
5c055266
MD
263 case RING_BUFFER_FLUSH:
264 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
265 return 0;
f3bc08c5
MD
266 default:
267 return -ENOIOCTLCMD;
268 }
269}
270
271#ifdef CONFIG_COMPAT
272long lib_ring_buffer_compat_ioctl(struct file *filp, unsigned int cmd,
273 unsigned long arg)
274{
275 struct lib_ring_buffer *buf = filp->private_data;
276 struct channel *chan = buf->backend.chan;
5a8fd222 277 const struct lib_ring_buffer_config *config = &chan->backend.config;
254ec7bc
MD
278
279 if (lib_ring_buffer_channel_is_disabled(chan))
280 return -EIO;
f3bc08c5
MD
281
282 switch (cmd) {
283 case RING_BUFFER_SNAPSHOT:
284 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
285 &buf->prod_snapshot);
286 case RING_BUFFER_SNAPSHOT_GET_CONSUMED:
287 return compat_put_ulong(buf->cons_snapshot, arg);
288 case RING_BUFFER_SNAPSHOT_GET_PRODUCED:
289 return compat_put_ulong(buf->prod_snapshot, arg);
290 case RING_BUFFER_GET_SUBBUF:
291 {
292 __u32 uconsume;
293 unsigned long consume;
294 long ret;
295
296 ret = get_user(uconsume, (__u32 __user *) arg);
297 if (ret)
298 return ret; /* will return -EFAULT */
299 consume = buf->cons_snapshot;
300 consume &= ~0xFFFFFFFFL;
301 consume |= uconsume;
302 ret = lib_ring_buffer_get_subbuf(buf, consume);
303 if (!ret) {
304 /* Set file position to zero at each successful "get" */
305 filp->f_pos = 0;
306 }
307 return ret;
308 }
309 case RING_BUFFER_PUT_SUBBUF:
310 lib_ring_buffer_put_subbuf(buf);
311 return 0;
312
313 case RING_BUFFER_GET_NEXT_SUBBUF:
314 {
315 long ret;
316
317 ret = lib_ring_buffer_get_next_subbuf(buf);
318 if (!ret) {
319 /* Set file position to zero at each successful "get" */
320 filp->f_pos = 0;
321 }
322 return ret;
323 }
324 case RING_BUFFER_PUT_NEXT_SUBBUF:
325 lib_ring_buffer_put_next_subbuf(buf);
326 return 0;
327 case RING_BUFFER_GET_SUBBUF_SIZE:
328 {
329 unsigned long data_size;
330
331 data_size = lib_ring_buffer_get_read_data_size(config, buf);
332 if (data_size > UINT_MAX)
333 return -EFBIG;
334 return put_ulong(data_size, arg);
335 }
336 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE:
337 {
338 unsigned long size;
339
340 size = lib_ring_buffer_get_read_data_size(config, buf);
341 size = PAGE_ALIGN(size);
342 if (size > UINT_MAX)
343 return -EFBIG;
344 return put_ulong(size, arg);
345 }
346 case RING_BUFFER_GET_MAX_SUBBUF_SIZE:
347 if (chan->backend.subbuf_size > UINT_MAX)
348 return -EFBIG;
349 return put_ulong(chan->backend.subbuf_size, arg);
350 case RING_BUFFER_GET_MMAP_LEN:
351 {
352 unsigned long mmap_buf_len;
353
354 if (config->output != RING_BUFFER_MMAP)
355 return -EINVAL;
356 mmap_buf_len = chan->backend.buf_size;
357 if (chan->backend.extra_reader_sb)
358 mmap_buf_len += chan->backend.subbuf_size;
359 if (mmap_buf_len > UINT_MAX)
360 return -EFBIG;
361 return put_ulong(mmap_buf_len, arg);
362 }
363 case RING_BUFFER_GET_MMAP_READ_OFFSET:
364 {
365 unsigned long sb_bindex, read_offset;
366
367 if (config->output != RING_BUFFER_MMAP)
368 return -EINVAL;
369 sb_bindex = subbuffer_id_get_index(config,
370 buf->backend.buf_rsb.id);
371 read_offset = buf->backend.array[sb_bindex]->mmap_offset;
372 if (read_offset > UINT_MAX)
373 return -EINVAL;
374 return put_ulong(read_offset, arg);
375 }
5c055266
MD
376 case RING_BUFFER_FLUSH:
377 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
378 return 0;
f3bc08c5
MD
379 default:
380 return -ENOIOCTLCMD;
381 }
382}
383#endif
384
385const struct file_operations lib_ring_buffer_file_operations = {
a33c9927 386 .owner = THIS_MODULE,
f3bc08c5
MD
387 .open = lib_ring_buffer_open,
388 .release = lib_ring_buffer_release,
389 .poll = lib_ring_buffer_poll,
390 .splice_read = lib_ring_buffer_splice_read,
391 .mmap = lib_ring_buffer_mmap,
392 .unlocked_ioctl = lib_ring_buffer_ioctl,
393 .llseek = lib_ring_buffer_no_llseek,
394#ifdef CONFIG_COMPAT
395 .compat_ioctl = lib_ring_buffer_compat_ioctl,
396#endif
397};
398EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations);
399
400MODULE_LICENSE("GPL and additional rights");
401MODULE_AUTHOR("Mathieu Desnoyers");
402MODULE_DESCRIPTION("Ring Buffer Library VFS");
This page took 0.040225 seconds and 4 git commands to generate.