4 * Copyright (C) 2009-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring Buffer VFS file operations.
8 * Dual LGPL v2.1/GPL v2 license.
11 #include <linux/module.h>
13 #include <linux/compat.h>
15 #include "../../wrapper/ringbuffer/backend.h"
16 #include "../../wrapper/ringbuffer/frontend.h"
17 #include "../../wrapper/ringbuffer/vfs.h"
18 #include "../../wrapper/poll.h"
20 static int put_ulong(unsigned long val
, unsigned long arg
)
22 return put_user(val
, (unsigned long __user
*)arg
);
26 static int compat_put_ulong(compat_ulong_t val
, unsigned long arg
)
28 return put_user(val
, (compat_ulong_t __user
*)compat_ptr(arg
));
33 * lib_ring_buffer_open - ring buffer open file operation
34 * @inode: opened inode
37 * Open implementation. Makes sure only one open instance of a buffer is
38 * done at a given moment.
40 int lib_ring_buffer_open(struct inode
*inode
, struct file
*file
)
42 struct lib_ring_buffer
*buf
= inode
->i_private
;
45 ret
= lib_ring_buffer_open_read(buf
);
49 file
->private_data
= buf
;
50 ret
= nonseekable_open(inode
, file
);
56 lib_ring_buffer_release_read(buf
);
61 * lib_ring_buffer_release - ring buffer release file operation
62 * @inode: opened inode
65 * Release implementation.
67 int lib_ring_buffer_release(struct inode
*inode
, struct file
*file
)
69 struct lib_ring_buffer
*buf
= file
->private_data
;
71 lib_ring_buffer_release_read(buf
);
77 * lib_ring_buffer_poll - ring buffer poll file operation
81 * Poll implementation.
83 unsigned int lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
)
85 unsigned int mask
= 0;
86 struct lib_ring_buffer
*buf
= filp
->private_data
;
87 struct channel
*chan
= buf
->backend
.chan
;
88 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
89 int finalized
, disabled
;
91 if (filp
->f_mode
& FMODE_READ
) {
92 poll_wait_set_exclusive(wait
);
93 poll_wait(filp
, &buf
->read_wait
, wait
);
95 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
96 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
99 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
100 * finalized load before offsets loads.
102 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
107 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
), chan
)
108 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
), chan
)
114 * The memory barriers
115 * __wait_event()/wake_up_interruptible() take
116 * care of "raw_spin_is_locked" memory ordering.
118 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
124 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
),
126 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
),
128 >= chan
->backend
.buf_size
)
129 return POLLPRI
| POLLRDBAND
;
131 return POLLIN
| POLLRDNORM
;
138 * lib_ring_buffer_ioctl - control ring buffer reader synchronization
144 * This ioctl implements commands necessary for producer/consumer
145 * and flight recorder reader interaction :
146 * RING_BUFFER_GET_NEXT_SUBBUF
147 * Get the next sub-buffer that can be read. It never blocks.
148 * RING_BUFFER_PUT_NEXT_SUBBUF
149 * Release the currently read sub-buffer.
150 * RING_BUFFER_GET_SUBBUF_SIZE
151 * returns the size of the current sub-buffer.
152 * RING_BUFFER_GET_MAX_SUBBUF_SIZE
153 * returns the maximum size for sub-buffers.
154 * RING_BUFFER_GET_NUM_SUBBUF
155 * returns the number of reader-visible sub-buffers in the per cpu
156 * channel (for mmap).
157 * RING_BUFFER_GET_MMAP_READ_OFFSET
158 * returns the offset of the subbuffer belonging to the reader.
159 * Should only be used for mmap clients.
161 long lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
163 struct lib_ring_buffer
*buf
= filp
->private_data
;
164 struct channel
*chan
= buf
->backend
.chan
;
165 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
167 if (lib_ring_buffer_channel_is_disabled(chan
))
171 case RING_BUFFER_SNAPSHOT
:
172 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
173 &buf
->prod_snapshot
);
174 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
175 return put_ulong(buf
->cons_snapshot
, arg
);
176 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
177 return put_ulong(buf
->prod_snapshot
, arg
);
178 case RING_BUFFER_GET_SUBBUF
:
180 unsigned long uconsume
;
183 ret
= get_user(uconsume
, (unsigned long __user
*) arg
);
185 return ret
; /* will return -EFAULT */
186 ret
= lib_ring_buffer_get_subbuf(buf
, uconsume
);
188 /* Set file position to zero at each successful "get" */
193 case RING_BUFFER_PUT_SUBBUF
:
194 lib_ring_buffer_put_subbuf(buf
);
197 case RING_BUFFER_GET_NEXT_SUBBUF
:
201 ret
= lib_ring_buffer_get_next_subbuf(buf
);
203 /* Set file position to zero at each successful "get" */
208 case RING_BUFFER_PUT_NEXT_SUBBUF
:
209 lib_ring_buffer_put_next_subbuf(buf
);
211 case RING_BUFFER_GET_SUBBUF_SIZE
:
212 return put_ulong(lib_ring_buffer_get_read_data_size(config
, buf
),
214 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
218 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
219 size
= PAGE_ALIGN(size
);
220 return put_ulong(size
, arg
);
222 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
223 return put_ulong(chan
->backend
.subbuf_size
, arg
);
224 case RING_BUFFER_GET_MMAP_LEN
:
226 unsigned long mmap_buf_len
;
228 if (config
->output
!= RING_BUFFER_MMAP
)
230 mmap_buf_len
= chan
->backend
.buf_size
;
231 if (chan
->backend
.extra_reader_sb
)
232 mmap_buf_len
+= chan
->backend
.subbuf_size
;
233 if (mmap_buf_len
> INT_MAX
)
235 return put_ulong(mmap_buf_len
, arg
);
237 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
239 unsigned long sb_bindex
;
241 if (config
->output
!= RING_BUFFER_MMAP
)
243 sb_bindex
= subbuffer_id_get_index(config
,
244 buf
->backend
.buf_rsb
.id
);
245 return put_ulong(buf
->backend
.array
[sb_bindex
]->mmap_offset
,
248 case RING_BUFFER_FLUSH
:
249 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
257 long lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
260 struct lib_ring_buffer
*buf
= filp
->private_data
;
261 struct channel
*chan
= buf
->backend
.chan
;
262 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
264 if (lib_ring_buffer_channel_is_disabled(chan
))
268 case RING_BUFFER_SNAPSHOT
:
269 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
270 &buf
->prod_snapshot
);
271 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
272 return compat_put_ulong(buf
->cons_snapshot
, arg
);
273 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
274 return compat_put_ulong(buf
->prod_snapshot
, arg
);
275 case RING_BUFFER_GET_SUBBUF
:
278 unsigned long consume
;
281 ret
= get_user(uconsume
, (__u32 __user
*) arg
);
283 return ret
; /* will return -EFAULT */
284 consume
= buf
->cons_snapshot
;
285 consume
&= ~0xFFFFFFFFL
;
287 ret
= lib_ring_buffer_get_subbuf(buf
, consume
);
289 /* Set file position to zero at each successful "get" */
294 case RING_BUFFER_PUT_SUBBUF
:
295 lib_ring_buffer_put_subbuf(buf
);
298 case RING_BUFFER_GET_NEXT_SUBBUF
:
302 ret
= lib_ring_buffer_get_next_subbuf(buf
);
304 /* Set file position to zero at each successful "get" */
309 case RING_BUFFER_PUT_NEXT_SUBBUF
:
310 lib_ring_buffer_put_next_subbuf(buf
);
312 case RING_BUFFER_GET_SUBBUF_SIZE
:
314 unsigned long data_size
;
316 data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
317 if (data_size
> UINT_MAX
)
319 return put_ulong(data_size
, arg
);
321 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
325 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
326 size
= PAGE_ALIGN(size
);
329 return put_ulong(size
, arg
);
331 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
332 if (chan
->backend
.subbuf_size
> UINT_MAX
)
334 return put_ulong(chan
->backend
.subbuf_size
, arg
);
335 case RING_BUFFER_GET_MMAP_LEN
:
337 unsigned long mmap_buf_len
;
339 if (config
->output
!= RING_BUFFER_MMAP
)
341 mmap_buf_len
= chan
->backend
.buf_size
;
342 if (chan
->backend
.extra_reader_sb
)
343 mmap_buf_len
+= chan
->backend
.subbuf_size
;
344 if (mmap_buf_len
> UINT_MAX
)
346 return put_ulong(mmap_buf_len
, arg
);
348 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
350 unsigned long sb_bindex
, read_offset
;
352 if (config
->output
!= RING_BUFFER_MMAP
)
354 sb_bindex
= subbuffer_id_get_index(config
,
355 buf
->backend
.buf_rsb
.id
);
356 read_offset
= buf
->backend
.array
[sb_bindex
]->mmap_offset
;
357 if (read_offset
> UINT_MAX
)
359 return put_ulong(read_offset
, arg
);
361 case RING_BUFFER_FLUSH
:
362 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
370 const struct file_operations lib_ring_buffer_file_operations
= {
371 .owner
= THIS_MODULE
,
372 .open
= lib_ring_buffer_open
,
373 .release
= lib_ring_buffer_release
,
374 .poll
= lib_ring_buffer_poll
,
375 .splice_read
= lib_ring_buffer_splice_read
,
376 .mmap
= lib_ring_buffer_mmap
,
377 .unlocked_ioctl
= lib_ring_buffer_ioctl
,
378 .llseek
= lib_ring_buffer_no_llseek
,
380 .compat_ioctl
= lib_ring_buffer_compat_ioctl
,
383 EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations
);
385 MODULE_LICENSE("GPL and additional rights");
386 MODULE_AUTHOR("Mathieu Desnoyers");
387 MODULE_DESCRIPTION("Ring Buffer Library VFS");