4 * Ring Buffer VFS file operations.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <linux/module.h>
25 #include <linux/compat.h>
27 #include "../../wrapper/ringbuffer/backend.h"
28 #include "../../wrapper/ringbuffer/frontend.h"
29 #include "../../wrapper/ringbuffer/vfs.h"
30 #include "../../wrapper/poll.h"
32 static int put_ulong(unsigned long val
, unsigned long arg
)
34 return put_user(val
, (unsigned long __user
*)arg
);
38 static int compat_put_ulong(compat_ulong_t val
, unsigned long arg
)
40 return put_user(val
, (compat_ulong_t __user
*)compat_ptr(arg
));
45 * lib_ring_buffer_open - ring buffer open file operation
46 * @inode: opened inode
49 * Open implementation. Makes sure only one open instance of a buffer is
50 * done at a given moment.
52 int lib_ring_buffer_open(struct inode
*inode
, struct file
*file
)
54 struct lib_ring_buffer
*buf
= inode
->i_private
;
60 ret
= lib_ring_buffer_open_read(buf
);
64 file
->private_data
= buf
;
65 ret
= nonseekable_open(inode
, file
);
71 lib_ring_buffer_release_read(buf
);
76 * lib_ring_buffer_release - ring buffer release file operation
77 * @inode: opened inode
80 * Release implementation.
82 int lib_ring_buffer_release(struct inode
*inode
, struct file
*file
)
84 struct lib_ring_buffer
*buf
= file
->private_data
;
86 lib_ring_buffer_release_read(buf
);
92 * lib_ring_buffer_poll - ring buffer poll file operation
96 * Poll implementation.
98 unsigned int lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
)
100 unsigned int mask
= 0;
101 struct lib_ring_buffer
*buf
= filp
->private_data
;
102 struct channel
*chan
= buf
->backend
.chan
;
103 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
104 int finalized
, disabled
;
106 if (filp
->f_mode
& FMODE_READ
) {
107 poll_wait_set_exclusive(wait
);
108 poll_wait(filp
, &buf
->read_wait
, wait
);
110 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
111 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
114 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
115 * finalized load before offsets loads.
117 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
122 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
), chan
)
123 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
), chan
)
129 * The memory barriers
130 * __wait_event()/wake_up_interruptible() take
131 * care of "raw_spin_is_locked" memory ordering.
133 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
139 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
),
141 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
),
143 >= chan
->backend
.buf_size
)
144 return POLLPRI
| POLLRDBAND
;
146 return POLLIN
| POLLRDNORM
;
153 * lib_ring_buffer_ioctl - control ring buffer reader synchronization
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.
176 long lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
178 struct lib_ring_buffer
*buf
= filp
->private_data
;
179 struct channel
*chan
= buf
->backend
.chan
;
180 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
182 if (lib_ring_buffer_channel_is_disabled(chan
))
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
:
195 unsigned long uconsume
;
198 ret
= get_user(uconsume
, (unsigned long __user
*) arg
);
200 return ret
; /* will return -EFAULT */
201 ret
= lib_ring_buffer_get_subbuf(buf
, uconsume
);
203 /* Set file position to zero at each successful "get" */
208 case RING_BUFFER_PUT_SUBBUF
:
209 lib_ring_buffer_put_subbuf(buf
);
212 case RING_BUFFER_GET_NEXT_SUBBUF
:
216 ret
= lib_ring_buffer_get_next_subbuf(buf
);
218 /* Set file position to zero at each successful "get" */
223 case RING_BUFFER_PUT_NEXT_SUBBUF
:
224 lib_ring_buffer_put_next_subbuf(buf
);
226 case RING_BUFFER_GET_SUBBUF_SIZE
:
227 return put_ulong(lib_ring_buffer_get_read_data_size(config
, buf
),
229 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
233 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
234 size
= PAGE_ALIGN(size
);
235 return put_ulong(size
, arg
);
237 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
238 return put_ulong(chan
->backend
.subbuf_size
, arg
);
239 case RING_BUFFER_GET_MMAP_LEN
:
241 unsigned long mmap_buf_len
;
243 if (config
->output
!= RING_BUFFER_MMAP
)
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
)
250 return put_ulong(mmap_buf_len
, arg
);
252 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
254 unsigned long sb_bindex
;
256 if (config
->output
!= RING_BUFFER_MMAP
)
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
,
263 case RING_BUFFER_FLUSH
:
264 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
272 long lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
275 struct lib_ring_buffer
*buf
= filp
->private_data
;
276 struct channel
*chan
= buf
->backend
.chan
;
277 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
279 if (lib_ring_buffer_channel_is_disabled(chan
))
283 case RING_BUFFER_COMPAT_SNAPSHOT
:
284 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
285 &buf
->prod_snapshot
);
286 case RING_BUFFER_COMPAT_SNAPSHOT_GET_CONSUMED
:
287 return compat_put_ulong(buf
->cons_snapshot
, arg
);
288 case RING_BUFFER_COMPAT_SNAPSHOT_GET_PRODUCED
:
289 return compat_put_ulong(buf
->prod_snapshot
, arg
);
290 case RING_BUFFER_COMPAT_GET_SUBBUF
:
293 unsigned long consume
;
296 ret
= get_user(uconsume
, (__u32 __user
*) arg
);
298 return ret
; /* will return -EFAULT */
299 consume
= buf
->cons_snapshot
;
300 consume
&= ~0xFFFFFFFFL
;
302 ret
= lib_ring_buffer_get_subbuf(buf
, consume
);
304 /* Set file position to zero at each successful "get" */
309 case RING_BUFFER_COMPAT_PUT_SUBBUF
:
310 lib_ring_buffer_put_subbuf(buf
);
313 case RING_BUFFER_COMPAT_GET_NEXT_SUBBUF
:
317 ret
= lib_ring_buffer_get_next_subbuf(buf
);
319 /* Set file position to zero at each successful "get" */
324 case RING_BUFFER_COMPAT_PUT_NEXT_SUBBUF
:
325 lib_ring_buffer_put_next_subbuf(buf
);
327 case RING_BUFFER_COMPAT_GET_SUBBUF_SIZE
:
329 unsigned long data_size
;
331 data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
332 if (data_size
> UINT_MAX
)
334 return put_ulong(data_size
, arg
);
336 case RING_BUFFER_COMPAT_GET_PADDED_SUBBUF_SIZE
:
340 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
341 size
= PAGE_ALIGN(size
);
344 return put_ulong(size
, arg
);
346 case RING_BUFFER_COMPAT_GET_MAX_SUBBUF_SIZE
:
347 if (chan
->backend
.subbuf_size
> UINT_MAX
)
349 return put_ulong(chan
->backend
.subbuf_size
, arg
);
350 case RING_BUFFER_COMPAT_GET_MMAP_LEN
:
352 unsigned long mmap_buf_len
;
354 if (config
->output
!= RING_BUFFER_MMAP
)
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
)
361 return put_ulong(mmap_buf_len
, arg
);
363 case RING_BUFFER_COMPAT_GET_MMAP_READ_OFFSET
:
365 unsigned long sb_bindex
, read_offset
;
367 if (config
->output
!= RING_BUFFER_MMAP
)
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
)
374 return put_ulong(read_offset
, arg
);
376 case RING_BUFFER_COMPAT_FLUSH
:
377 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
385 const struct file_operations lib_ring_buffer_file_operations
= {
386 .owner
= THIS_MODULE
,
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
,
395 .compat_ioctl
= lib_ring_buffer_compat_ioctl
,
398 EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations
);
400 MODULE_LICENSE("GPL and additional rights");
401 MODULE_AUTHOR("Mathieu Desnoyers");
402 MODULE_DESCRIPTION("Ring Buffer Library VFS");