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
));
44 int lib_ring_buffer_open(struct inode
*inode
, struct file
*file
,
45 struct lib_ring_buffer
*buf
)
52 ret
= lib_ring_buffer_open_read(buf
);
56 ret
= nonseekable_open(inode
, file
);
62 lib_ring_buffer_release_read(buf
);
65 EXPORT_SYMBOL_GPL(lib_ring_buffer_open
);
68 * vfs_lib_ring_buffer_open - ring buffer open file operation
69 * @inode: opened inode
72 * Open implementation. Makes sure only one open instance of a buffer is
73 * done at a given moment.
76 int vfs_lib_ring_buffer_open(struct inode
*inode
, struct file
*file
)
78 struct lib_ring_buffer
*buf
= inode
->i_private
;
80 file
->private_data
= buf
;
81 return lib_ring_buffer_open(inode
, file
, buf
);
84 int lib_ring_buffer_release(struct inode
*inode
, struct file
*file
,
85 struct lib_ring_buffer
*buf
)
87 lib_ring_buffer_release_read(buf
);
91 EXPORT_SYMBOL_GPL(lib_ring_buffer_release
);
94 * vfs_lib_ring_buffer_release - ring buffer release file operation
95 * @inode: opened inode
98 * Release implementation.
101 int vfs_lib_ring_buffer_release(struct inode
*inode
, struct file
*file
)
103 struct lib_ring_buffer
*buf
= file
->private_data
;
105 return lib_ring_buffer_release(inode
, file
, buf
);
108 unsigned int lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
,
109 struct lib_ring_buffer
*buf
)
111 unsigned int mask
= 0;
112 struct channel
*chan
= buf
->backend
.chan
;
113 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
114 int finalized
, disabled
;
116 if (filp
->f_mode
& FMODE_READ
) {
117 poll_wait_set_exclusive(wait
);
118 poll_wait(filp
, &buf
->read_wait
, wait
);
120 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
121 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
124 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
125 * finalized load before offsets loads.
127 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
132 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
), chan
)
133 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
), chan
)
139 * The memory barriers
140 * __wait_event()/wake_up_interruptible() take
141 * care of "raw_spin_is_locked" memory ordering.
143 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
149 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
),
151 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
),
153 >= chan
->backend
.buf_size
)
154 return POLLPRI
| POLLRDBAND
;
156 return POLLIN
| POLLRDNORM
;
161 EXPORT_SYMBOL_GPL(lib_ring_buffer_poll
);
164 * vfs_lib_ring_buffer_poll - ring buffer poll file operation
168 * Poll implementation.
171 unsigned int vfs_lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
)
173 struct lib_ring_buffer
*buf
= filp
->private_data
;
175 return lib_ring_buffer_poll(filp
, wait
, buf
);
178 long lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
,
179 unsigned long arg
, struct lib_ring_buffer
*buf
)
181 struct channel
*chan
= buf
->backend
.chan
;
182 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
184 if (lib_ring_buffer_channel_is_disabled(chan
))
188 case RING_BUFFER_SNAPSHOT
:
189 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
190 &buf
->prod_snapshot
);
191 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
192 return put_ulong(buf
->cons_snapshot
, arg
);
193 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
194 return put_ulong(buf
->prod_snapshot
, arg
);
195 case RING_BUFFER_GET_SUBBUF
:
197 unsigned long uconsume
;
200 ret
= get_user(uconsume
, (unsigned long __user
*) arg
);
202 return ret
; /* will return -EFAULT */
203 ret
= lib_ring_buffer_get_subbuf(buf
, uconsume
);
205 /* Set file position to zero at each successful "get" */
210 case RING_BUFFER_PUT_SUBBUF
:
211 lib_ring_buffer_put_subbuf(buf
);
214 case RING_BUFFER_GET_NEXT_SUBBUF
:
218 ret
= lib_ring_buffer_get_next_subbuf(buf
);
220 /* Set file position to zero at each successful "get" */
225 case RING_BUFFER_PUT_NEXT_SUBBUF
:
226 lib_ring_buffer_put_next_subbuf(buf
);
228 case RING_BUFFER_GET_SUBBUF_SIZE
:
229 return put_ulong(lib_ring_buffer_get_read_data_size(config
, buf
),
231 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
235 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
236 size
= PAGE_ALIGN(size
);
237 return put_ulong(size
, arg
);
239 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
240 return put_ulong(chan
->backend
.subbuf_size
, arg
);
241 case RING_BUFFER_GET_MMAP_LEN
:
243 unsigned long mmap_buf_len
;
245 if (config
->output
!= RING_BUFFER_MMAP
)
247 mmap_buf_len
= chan
->backend
.buf_size
;
248 if (chan
->backend
.extra_reader_sb
)
249 mmap_buf_len
+= chan
->backend
.subbuf_size
;
250 if (mmap_buf_len
> INT_MAX
)
252 return put_ulong(mmap_buf_len
, arg
);
254 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
256 unsigned long sb_bindex
;
258 if (config
->output
!= RING_BUFFER_MMAP
)
260 sb_bindex
= subbuffer_id_get_index(config
,
261 buf
->backend
.buf_rsb
.id
);
262 return put_ulong(buf
->backend
.array
[sb_bindex
]->mmap_offset
,
265 case RING_BUFFER_FLUSH
:
266 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
272 EXPORT_SYMBOL_GPL(lib_ring_buffer_ioctl
);
275 * vfs_lib_ring_buffer_ioctl - control ring buffer reader synchronization
281 * This ioctl implements commands necessary for producer/consumer
282 * and flight recorder reader interaction :
283 * RING_BUFFER_GET_NEXT_SUBBUF
284 * Get the next sub-buffer that can be read. It never blocks.
285 * RING_BUFFER_PUT_NEXT_SUBBUF
286 * Release the currently read sub-buffer.
287 * RING_BUFFER_GET_SUBBUF_SIZE
288 * returns the size of the current sub-buffer.
289 * RING_BUFFER_GET_MAX_SUBBUF_SIZE
290 * returns the maximum size for sub-buffers.
291 * RING_BUFFER_GET_NUM_SUBBUF
292 * returns the number of reader-visible sub-buffers in the per cpu
293 * channel (for mmap).
294 * RING_BUFFER_GET_MMAP_READ_OFFSET
295 * returns the offset of the subbuffer belonging to the reader.
296 * Should only be used for mmap clients.
299 long vfs_lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
301 struct lib_ring_buffer
*buf
= filp
->private_data
;
303 return lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
307 long lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
308 unsigned long arg
, struct lib_ring_buffer
*buf
)
310 struct channel
*chan
= buf
->backend
.chan
;
311 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
313 if (lib_ring_buffer_channel_is_disabled(chan
))
317 case RING_BUFFER_COMPAT_SNAPSHOT
:
318 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
319 &buf
->prod_snapshot
);
320 case RING_BUFFER_COMPAT_SNAPSHOT_GET_CONSUMED
:
321 return compat_put_ulong(buf
->cons_snapshot
, arg
);
322 case RING_BUFFER_COMPAT_SNAPSHOT_GET_PRODUCED
:
323 return compat_put_ulong(buf
->prod_snapshot
, arg
);
324 case RING_BUFFER_COMPAT_GET_SUBBUF
:
327 unsigned long consume
;
330 ret
= get_user(uconsume
, (__u32 __user
*) arg
);
332 return ret
; /* will return -EFAULT */
333 consume
= buf
->cons_snapshot
;
334 consume
&= ~0xFFFFFFFFL
;
336 ret
= lib_ring_buffer_get_subbuf(buf
, consume
);
338 /* Set file position to zero at each successful "get" */
343 case RING_BUFFER_COMPAT_PUT_SUBBUF
:
344 lib_ring_buffer_put_subbuf(buf
);
347 case RING_BUFFER_COMPAT_GET_NEXT_SUBBUF
:
351 ret
= lib_ring_buffer_get_next_subbuf(buf
);
353 /* Set file position to zero at each successful "get" */
358 case RING_BUFFER_COMPAT_PUT_NEXT_SUBBUF
:
359 lib_ring_buffer_put_next_subbuf(buf
);
361 case RING_BUFFER_COMPAT_GET_SUBBUF_SIZE
:
363 unsigned long data_size
;
365 data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
366 if (data_size
> UINT_MAX
)
368 return compat_put_ulong(data_size
, arg
);
370 case RING_BUFFER_COMPAT_GET_PADDED_SUBBUF_SIZE
:
374 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
375 size
= PAGE_ALIGN(size
);
378 return compat_put_ulong(size
, arg
);
380 case RING_BUFFER_COMPAT_GET_MAX_SUBBUF_SIZE
:
381 if (chan
->backend
.subbuf_size
> UINT_MAX
)
383 return compat_put_ulong(chan
->backend
.subbuf_size
, arg
);
384 case RING_BUFFER_COMPAT_GET_MMAP_LEN
:
386 unsigned long mmap_buf_len
;
388 if (config
->output
!= RING_BUFFER_MMAP
)
390 mmap_buf_len
= chan
->backend
.buf_size
;
391 if (chan
->backend
.extra_reader_sb
)
392 mmap_buf_len
+= chan
->backend
.subbuf_size
;
393 if (mmap_buf_len
> UINT_MAX
)
395 return compat_put_ulong(mmap_buf_len
, arg
);
397 case RING_BUFFER_COMPAT_GET_MMAP_READ_OFFSET
:
399 unsigned long sb_bindex
, read_offset
;
401 if (config
->output
!= RING_BUFFER_MMAP
)
403 sb_bindex
= subbuffer_id_get_index(config
,
404 buf
->backend
.buf_rsb
.id
);
405 read_offset
= buf
->backend
.array
[sb_bindex
]->mmap_offset
;
406 if (read_offset
> UINT_MAX
)
408 return compat_put_ulong(read_offset
, arg
);
410 case RING_BUFFER_COMPAT_FLUSH
:
411 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
417 EXPORT_SYMBOL_GPL(lib_ring_buffer_compat_ioctl
);
420 long vfs_lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
423 struct lib_ring_buffer
*buf
= filp
->private_data
;
425 return lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
429 const struct file_operations lib_ring_buffer_file_operations
= {
430 .owner
= THIS_MODULE
,
431 .open
= vfs_lib_ring_buffer_open
,
432 .release
= vfs_lib_ring_buffer_release
,
433 .poll
= vfs_lib_ring_buffer_poll
,
434 .splice_read
= vfs_lib_ring_buffer_splice_read
,
435 .mmap
= vfs_lib_ring_buffer_mmap
,
436 .unlocked_ioctl
= vfs_lib_ring_buffer_ioctl
,
437 .llseek
= vfs_lib_ring_buffer_no_llseek
,
439 .compat_ioctl
= vfs_lib_ring_buffer_compat_ioctl
,
442 EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations
);
444 MODULE_LICENSE("GPL and additional rights");
445 MODULE_AUTHOR("Mathieu Desnoyers");
446 MODULE_DESCRIPTION("Ring Buffer Library VFS");