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 * This is not used by anonymous file descriptors. This code is left
46 * there if we ever want to implement an inode with open() operation.
48 int lib_ring_buffer_open(struct inode
*inode
, struct file
*file
,
49 struct lib_ring_buffer
*buf
)
56 ret
= lib_ring_buffer_open_read(buf
);
60 ret
= nonseekable_open(inode
, file
);
66 lib_ring_buffer_release_read(buf
);
69 EXPORT_SYMBOL_GPL(lib_ring_buffer_open
);
72 * vfs_lib_ring_buffer_open - ring buffer open file operation
73 * @inode: opened inode
76 * Open implementation. Makes sure only one open instance of a buffer is
77 * done at a given moment.
80 int vfs_lib_ring_buffer_open(struct inode
*inode
, struct file
*file
)
82 struct lib_ring_buffer
*buf
= inode
->i_private
;
84 file
->private_data
= buf
;
85 return lib_ring_buffer_open(inode
, file
, buf
);
88 int lib_ring_buffer_release(struct inode
*inode
, struct file
*file
,
89 struct lib_ring_buffer
*buf
)
91 lib_ring_buffer_release_read(buf
);
95 EXPORT_SYMBOL_GPL(lib_ring_buffer_release
);
98 * vfs_lib_ring_buffer_release - ring buffer release file operation
99 * @inode: opened inode
102 * Release implementation.
105 int vfs_lib_ring_buffer_release(struct inode
*inode
, struct file
*file
)
107 struct lib_ring_buffer
*buf
= file
->private_data
;
109 return lib_ring_buffer_release(inode
, file
, buf
);
112 unsigned int lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
,
113 struct lib_ring_buffer
*buf
)
115 unsigned int mask
= 0;
116 struct channel
*chan
= buf
->backend
.chan
;
117 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
118 int finalized
, disabled
;
120 if (filp
->f_mode
& FMODE_READ
) {
121 poll_wait_set_exclusive(wait
);
122 poll_wait(filp
, &buf
->read_wait
, wait
);
124 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
125 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
128 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
129 * finalized load before offsets loads.
131 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
136 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
), chan
)
137 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
), chan
)
143 * The memory barriers
144 * __wait_event()/wake_up_interruptible() take
145 * care of "raw_spin_is_locked" memory ordering.
147 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
153 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
),
155 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
),
157 >= chan
->backend
.buf_size
)
158 return POLLPRI
| POLLRDBAND
;
160 return POLLIN
| POLLRDNORM
;
165 EXPORT_SYMBOL_GPL(lib_ring_buffer_poll
);
168 * vfs_lib_ring_buffer_poll - ring buffer poll file operation
172 * Poll implementation.
175 unsigned int vfs_lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
)
177 struct lib_ring_buffer
*buf
= filp
->private_data
;
179 return lib_ring_buffer_poll(filp
, wait
, buf
);
182 long lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
,
183 unsigned long arg
, struct lib_ring_buffer
*buf
)
185 struct channel
*chan
= buf
->backend
.chan
;
186 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
188 if (lib_ring_buffer_channel_is_disabled(chan
))
192 case RING_BUFFER_SNAPSHOT
:
193 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
194 &buf
->prod_snapshot
);
195 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
196 return put_ulong(buf
->cons_snapshot
, arg
);
197 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
198 return put_ulong(buf
->prod_snapshot
, arg
);
199 case RING_BUFFER_GET_SUBBUF
:
201 unsigned long uconsume
;
204 ret
= get_user(uconsume
, (unsigned long __user
*) arg
);
206 return ret
; /* will return -EFAULT */
207 ret
= lib_ring_buffer_get_subbuf(buf
, uconsume
);
209 /* Set file position to zero at each successful "get" */
214 case RING_BUFFER_PUT_SUBBUF
:
215 lib_ring_buffer_put_subbuf(buf
);
218 case RING_BUFFER_GET_NEXT_SUBBUF
:
222 ret
= lib_ring_buffer_get_next_subbuf(buf
);
224 /* Set file position to zero at each successful "get" */
229 case RING_BUFFER_PUT_NEXT_SUBBUF
:
230 lib_ring_buffer_put_next_subbuf(buf
);
232 case RING_BUFFER_GET_SUBBUF_SIZE
:
233 return put_ulong(lib_ring_buffer_get_read_data_size(config
, buf
),
235 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
239 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
240 size
= PAGE_ALIGN(size
);
241 return put_ulong(size
, arg
);
243 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
244 return put_ulong(chan
->backend
.subbuf_size
, arg
);
245 case RING_BUFFER_GET_MMAP_LEN
:
247 unsigned long mmap_buf_len
;
249 if (config
->output
!= RING_BUFFER_MMAP
)
251 mmap_buf_len
= chan
->backend
.buf_size
;
252 if (chan
->backend
.extra_reader_sb
)
253 mmap_buf_len
+= chan
->backend
.subbuf_size
;
254 if (mmap_buf_len
> INT_MAX
)
256 return put_ulong(mmap_buf_len
, arg
);
258 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
260 unsigned long sb_bindex
;
262 if (config
->output
!= RING_BUFFER_MMAP
)
264 sb_bindex
= subbuffer_id_get_index(config
,
265 buf
->backend
.buf_rsb
.id
);
266 return put_ulong(buf
->backend
.array
[sb_bindex
]->mmap_offset
,
269 case RING_BUFFER_FLUSH
:
270 lib_ring_buffer_switch_remote(buf
);
276 EXPORT_SYMBOL_GPL(lib_ring_buffer_ioctl
);
279 * vfs_lib_ring_buffer_ioctl - control ring buffer reader synchronization
285 * This ioctl implements commands necessary for producer/consumer
286 * and flight recorder reader interaction :
287 * RING_BUFFER_GET_NEXT_SUBBUF
288 * Get the next sub-buffer that can be read. It never blocks.
289 * RING_BUFFER_PUT_NEXT_SUBBUF
290 * Release the currently read sub-buffer.
291 * RING_BUFFER_GET_SUBBUF_SIZE
292 * returns the size of the current sub-buffer.
293 * RING_BUFFER_GET_MAX_SUBBUF_SIZE
294 * returns the maximum size for sub-buffers.
295 * RING_BUFFER_GET_NUM_SUBBUF
296 * returns the number of reader-visible sub-buffers in the per cpu
297 * channel (for mmap).
298 * RING_BUFFER_GET_MMAP_READ_OFFSET
299 * returns the offset of the subbuffer belonging to the reader.
300 * Should only be used for mmap clients.
303 long vfs_lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
305 struct lib_ring_buffer
*buf
= filp
->private_data
;
307 return lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
311 long lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
312 unsigned long arg
, struct lib_ring_buffer
*buf
)
314 struct channel
*chan
= buf
->backend
.chan
;
315 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
317 if (lib_ring_buffer_channel_is_disabled(chan
))
321 case RING_BUFFER_COMPAT_SNAPSHOT
:
322 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
323 &buf
->prod_snapshot
);
324 case RING_BUFFER_COMPAT_SNAPSHOT_GET_CONSUMED
:
325 return compat_put_ulong(buf
->cons_snapshot
, arg
);
326 case RING_BUFFER_COMPAT_SNAPSHOT_GET_PRODUCED
:
327 return compat_put_ulong(buf
->prod_snapshot
, arg
);
328 case RING_BUFFER_COMPAT_GET_SUBBUF
:
331 unsigned long consume
;
334 ret
= get_user(uconsume
, (__u32 __user
*) arg
);
336 return ret
; /* will return -EFAULT */
337 consume
= buf
->cons_snapshot
;
338 consume
&= ~0xFFFFFFFFL
;
340 ret
= lib_ring_buffer_get_subbuf(buf
, consume
);
342 /* Set file position to zero at each successful "get" */
347 case RING_BUFFER_COMPAT_PUT_SUBBUF
:
348 lib_ring_buffer_put_subbuf(buf
);
351 case RING_BUFFER_COMPAT_GET_NEXT_SUBBUF
:
355 ret
= lib_ring_buffer_get_next_subbuf(buf
);
357 /* Set file position to zero at each successful "get" */
362 case RING_BUFFER_COMPAT_PUT_NEXT_SUBBUF
:
363 lib_ring_buffer_put_next_subbuf(buf
);
365 case RING_BUFFER_COMPAT_GET_SUBBUF_SIZE
:
367 unsigned long data_size
;
369 data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
370 if (data_size
> UINT_MAX
)
372 return compat_put_ulong(data_size
, arg
);
374 case RING_BUFFER_COMPAT_GET_PADDED_SUBBUF_SIZE
:
378 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
379 size
= PAGE_ALIGN(size
);
382 return compat_put_ulong(size
, arg
);
384 case RING_BUFFER_COMPAT_GET_MAX_SUBBUF_SIZE
:
385 if (chan
->backend
.subbuf_size
> UINT_MAX
)
387 return compat_put_ulong(chan
->backend
.subbuf_size
, arg
);
388 case RING_BUFFER_COMPAT_GET_MMAP_LEN
:
390 unsigned long mmap_buf_len
;
392 if (config
->output
!= RING_BUFFER_MMAP
)
394 mmap_buf_len
= chan
->backend
.buf_size
;
395 if (chan
->backend
.extra_reader_sb
)
396 mmap_buf_len
+= chan
->backend
.subbuf_size
;
397 if (mmap_buf_len
> UINT_MAX
)
399 return compat_put_ulong(mmap_buf_len
, arg
);
401 case RING_BUFFER_COMPAT_GET_MMAP_READ_OFFSET
:
403 unsigned long sb_bindex
, read_offset
;
405 if (config
->output
!= RING_BUFFER_MMAP
)
407 sb_bindex
= subbuffer_id_get_index(config
,
408 buf
->backend
.buf_rsb
.id
);
409 read_offset
= buf
->backend
.array
[sb_bindex
]->mmap_offset
;
410 if (read_offset
> UINT_MAX
)
412 return compat_put_ulong(read_offset
, arg
);
414 case RING_BUFFER_COMPAT_FLUSH
:
415 lib_ring_buffer_switch_remote(buf
);
421 EXPORT_SYMBOL_GPL(lib_ring_buffer_compat_ioctl
);
424 long vfs_lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
427 struct lib_ring_buffer
*buf
= filp
->private_data
;
429 return lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
433 const struct file_operations lib_ring_buffer_file_operations
= {
434 .owner
= THIS_MODULE
,
435 .open
= vfs_lib_ring_buffer_open
,
436 .release
= vfs_lib_ring_buffer_release
,
437 .poll
= vfs_lib_ring_buffer_poll
,
438 .splice_read
= vfs_lib_ring_buffer_splice_read
,
439 .mmap
= vfs_lib_ring_buffer_mmap
,
440 .unlocked_ioctl
= vfs_lib_ring_buffer_ioctl
,
441 .llseek
= vfs_lib_ring_buffer_no_llseek
,
443 .compat_ioctl
= vfs_lib_ring_buffer_compat_ioctl
,
446 EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations
);
448 MODULE_LICENSE("GPL and additional rights");
449 MODULE_AUTHOR("Mathieu Desnoyers");
450 MODULE_DESCRIPTION("Ring Buffer Library VFS");