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>
31 #include <lttng-tracer.h>
33 static int put_ulong(unsigned long val
, unsigned long arg
)
35 return put_user(val
, (unsigned long __user
*)arg
);
39 static int compat_put_ulong(compat_ulong_t val
, unsigned long arg
)
41 return put_user(val
, (compat_ulong_t __user
*)compat_ptr(arg
));
46 * This is not used by anonymous file descriptors. This code is left
47 * there if we ever want to implement an inode with open() operation.
49 int lib_ring_buffer_open(struct inode
*inode
, struct file
*file
,
50 struct lib_ring_buffer
*buf
)
57 ret
= lib_ring_buffer_open_read(buf
);
61 ret
= nonseekable_open(inode
, file
);
67 lib_ring_buffer_release_read(buf
);
70 EXPORT_SYMBOL_GPL(lib_ring_buffer_open
);
73 * vfs_lib_ring_buffer_open - ring buffer open file operation
74 * @inode: opened inode
77 * Open implementation. Makes sure only one open instance of a buffer is
78 * done at a given moment.
81 int vfs_lib_ring_buffer_open(struct inode
*inode
, struct file
*file
)
83 struct lib_ring_buffer
*buf
= inode
->i_private
;
85 file
->private_data
= buf
;
86 return lib_ring_buffer_open(inode
, file
, buf
);
89 int lib_ring_buffer_release(struct inode
*inode
, struct file
*file
,
90 struct lib_ring_buffer
*buf
)
92 lib_ring_buffer_release_read(buf
);
96 EXPORT_SYMBOL_GPL(lib_ring_buffer_release
);
99 * vfs_lib_ring_buffer_release - ring buffer release file operation
100 * @inode: opened inode
103 * Release implementation.
106 int vfs_lib_ring_buffer_release(struct inode
*inode
, struct file
*file
)
108 struct lib_ring_buffer
*buf
= file
->private_data
;
110 return lib_ring_buffer_release(inode
, file
, buf
);
113 unsigned int lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
,
114 struct lib_ring_buffer
*buf
)
116 unsigned int mask
= 0;
117 struct channel
*chan
= buf
->backend
.chan
;
118 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
119 int finalized
, disabled
;
121 if (filp
->f_mode
& FMODE_READ
) {
122 poll_wait_set_exclusive(wait
);
123 poll_wait(filp
, &buf
->read_wait
, wait
);
125 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
126 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
129 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
130 * finalized load before offsets loads.
132 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
137 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
), chan
)
138 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
), chan
)
144 * The memory barriers
145 * __wait_event()/wake_up_interruptible() take
146 * care of "raw_spin_is_locked" memory ordering.
148 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
154 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
),
156 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
),
158 >= chan
->backend
.buf_size
)
159 return POLLPRI
| POLLRDBAND
;
161 return POLLIN
| POLLRDNORM
;
166 EXPORT_SYMBOL_GPL(lib_ring_buffer_poll
);
169 * vfs_lib_ring_buffer_poll - ring buffer poll file operation
173 * Poll implementation.
176 unsigned int vfs_lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
)
178 struct lib_ring_buffer
*buf
= filp
->private_data
;
180 return lib_ring_buffer_poll(filp
, wait
, buf
);
183 long lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
,
184 unsigned long arg
, struct lib_ring_buffer
*buf
)
186 struct channel
*chan
= buf
->backend
.chan
;
187 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
189 if (lib_ring_buffer_channel_is_disabled(chan
))
193 case RING_BUFFER_SNAPSHOT
:
195 * First, ensure we perform a "final" flush onto the
196 * stream. This will ensure we create a packet of
197 * padding if we encounter an empty packet. This ensures
198 * the time-stamps right before the snapshot is used as
199 * end of packet timestamp.
202 lib_ring_buffer_switch_remote_empty(buf
);
203 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
204 &buf
->prod_snapshot
);
205 case RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS
:
206 return lib_ring_buffer_snapshot_sample_positions(buf
,
207 &buf
->cons_snapshot
, &buf
->prod_snapshot
);
208 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
209 return put_ulong(buf
->cons_snapshot
, arg
);
210 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
211 return put_ulong(buf
->prod_snapshot
, arg
);
212 case RING_BUFFER_GET_SUBBUF
:
214 unsigned long uconsume
;
217 ret
= get_user(uconsume
, (unsigned long __user
*) arg
);
219 return ret
; /* will return -EFAULT */
220 ret
= lib_ring_buffer_get_subbuf(buf
, uconsume
);
222 /* Set file position to zero at each successful "get" */
227 case RING_BUFFER_PUT_SUBBUF
:
228 lib_ring_buffer_put_subbuf(buf
);
231 case RING_BUFFER_GET_NEXT_SUBBUF
:
235 ret
= lib_ring_buffer_get_next_subbuf(buf
);
237 /* Set file position to zero at each successful "get" */
242 case RING_BUFFER_PUT_NEXT_SUBBUF
:
243 lib_ring_buffer_put_next_subbuf(buf
);
245 case RING_BUFFER_GET_SUBBUF_SIZE
:
246 return put_ulong(lib_ring_buffer_get_read_data_size(config
, buf
),
248 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
252 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
253 size
= PAGE_ALIGN(size
);
254 return put_ulong(size
, arg
);
256 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
257 return put_ulong(chan
->backend
.subbuf_size
, arg
);
258 case RING_BUFFER_GET_MMAP_LEN
:
260 unsigned long mmap_buf_len
;
262 if (config
->output
!= RING_BUFFER_MMAP
)
264 mmap_buf_len
= chan
->backend
.buf_size
;
265 if (chan
->backend
.extra_reader_sb
)
266 mmap_buf_len
+= chan
->backend
.subbuf_size
;
267 if (mmap_buf_len
> INT_MAX
)
269 return put_ulong(mmap_buf_len
, arg
);
271 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
273 unsigned long sb_bindex
;
275 if (config
->output
!= RING_BUFFER_MMAP
)
277 sb_bindex
= subbuffer_id_get_index(config
,
278 buf
->backend
.buf_rsb
.id
);
279 return put_ulong(buf
->backend
.array
[sb_bindex
]->mmap_offset
,
282 case RING_BUFFER_FLUSH
:
283 lib_ring_buffer_switch_remote(buf
);
289 EXPORT_SYMBOL_GPL(lib_ring_buffer_ioctl
);
292 * vfs_lib_ring_buffer_ioctl - control ring buffer reader synchronization
298 * This ioctl implements commands necessary for producer/consumer
299 * and flight recorder reader interaction :
300 * RING_BUFFER_GET_NEXT_SUBBUF
301 * Get the next sub-buffer that can be read. It never blocks.
302 * RING_BUFFER_PUT_NEXT_SUBBUF
303 * Release the currently read sub-buffer.
304 * RING_BUFFER_GET_SUBBUF_SIZE
305 * returns the size of the current sub-buffer.
306 * RING_BUFFER_GET_MAX_SUBBUF_SIZE
307 * returns the maximum size for sub-buffers.
308 * RING_BUFFER_GET_NUM_SUBBUF
309 * returns the number of reader-visible sub-buffers in the per cpu
310 * channel (for mmap).
311 * RING_BUFFER_GET_MMAP_READ_OFFSET
312 * returns the offset of the subbuffer belonging to the reader.
313 * Should only be used for mmap clients.
316 long vfs_lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
318 struct lib_ring_buffer
*buf
= filp
->private_data
;
320 return lib_ring_buffer_ioctl(filp
, cmd
, arg
, buf
);
324 long lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
325 unsigned long arg
, struct lib_ring_buffer
*buf
)
327 struct channel
*chan
= buf
->backend
.chan
;
328 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
330 if (lib_ring_buffer_channel_is_disabled(chan
))
334 case RING_BUFFER_COMPAT_SNAPSHOT
:
336 * First, ensure we perform a "final" flush onto the
337 * stream. This will ensure we create a packet of
338 * padding if we encounter an empty packet. This ensures
339 * the time-stamps right before the snapshot is used as
340 * end of packet timestamp.
343 lib_ring_buffer_switch_remote_empty(buf
);
344 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
345 &buf
->prod_snapshot
);
346 case RING_BUFFER_COMPAT_SNAPSHOT_SAMPLE_POSITIONS
:
347 return lib_ring_buffer_snapshot_sample_positions(buf
,
348 &buf
->cons_snapshot
, &buf
->prod_snapshot
);
349 case RING_BUFFER_COMPAT_SNAPSHOT_GET_CONSUMED
:
350 return compat_put_ulong(buf
->cons_snapshot
, arg
);
351 case RING_BUFFER_COMPAT_SNAPSHOT_GET_PRODUCED
:
352 return compat_put_ulong(buf
->prod_snapshot
, arg
);
353 case RING_BUFFER_COMPAT_GET_SUBBUF
:
356 unsigned long consume
;
359 ret
= get_user(uconsume
, (__u32 __user
*) arg
);
361 return ret
; /* will return -EFAULT */
362 consume
= buf
->cons_snapshot
;
363 consume
&= ~0xFFFFFFFFL
;
365 ret
= lib_ring_buffer_get_subbuf(buf
, consume
);
367 /* Set file position to zero at each successful "get" */
372 case RING_BUFFER_COMPAT_PUT_SUBBUF
:
373 lib_ring_buffer_put_subbuf(buf
);
376 case RING_BUFFER_COMPAT_GET_NEXT_SUBBUF
:
380 ret
= lib_ring_buffer_get_next_subbuf(buf
);
382 /* Set file position to zero at each successful "get" */
387 case RING_BUFFER_COMPAT_PUT_NEXT_SUBBUF
:
388 lib_ring_buffer_put_next_subbuf(buf
);
390 case RING_BUFFER_COMPAT_GET_SUBBUF_SIZE
:
392 unsigned long data_size
;
394 data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
395 if (data_size
> UINT_MAX
)
397 return compat_put_ulong(data_size
, arg
);
399 case RING_BUFFER_COMPAT_GET_PADDED_SUBBUF_SIZE
:
403 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
404 size
= PAGE_ALIGN(size
);
407 return compat_put_ulong(size
, arg
);
409 case RING_BUFFER_COMPAT_GET_MAX_SUBBUF_SIZE
:
410 if (chan
->backend
.subbuf_size
> UINT_MAX
)
412 return compat_put_ulong(chan
->backend
.subbuf_size
, arg
);
413 case RING_BUFFER_COMPAT_GET_MMAP_LEN
:
415 unsigned long mmap_buf_len
;
417 if (config
->output
!= RING_BUFFER_MMAP
)
419 mmap_buf_len
= chan
->backend
.buf_size
;
420 if (chan
->backend
.extra_reader_sb
)
421 mmap_buf_len
+= chan
->backend
.subbuf_size
;
422 if (mmap_buf_len
> UINT_MAX
)
424 return compat_put_ulong(mmap_buf_len
, arg
);
426 case RING_BUFFER_COMPAT_GET_MMAP_READ_OFFSET
:
428 unsigned long sb_bindex
, read_offset
;
430 if (config
->output
!= RING_BUFFER_MMAP
)
432 sb_bindex
= subbuffer_id_get_index(config
,
433 buf
->backend
.buf_rsb
.id
);
434 read_offset
= buf
->backend
.array
[sb_bindex
]->mmap_offset
;
435 if (read_offset
> UINT_MAX
)
437 return compat_put_ulong(read_offset
, arg
);
439 case RING_BUFFER_COMPAT_FLUSH
:
440 lib_ring_buffer_switch_remote(buf
);
446 EXPORT_SYMBOL_GPL(lib_ring_buffer_compat_ioctl
);
449 long vfs_lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
452 struct lib_ring_buffer
*buf
= filp
->private_data
;
454 return lib_ring_buffer_compat_ioctl(filp
, cmd
, arg
, buf
);
458 const struct file_operations lib_ring_buffer_file_operations
= {
459 .owner
= THIS_MODULE
,
460 .open
= vfs_lib_ring_buffer_open
,
461 .release
= vfs_lib_ring_buffer_release
,
462 .poll
= vfs_lib_ring_buffer_poll
,
463 .splice_read
= vfs_lib_ring_buffer_splice_read
,
464 .mmap
= vfs_lib_ring_buffer_mmap
,
465 .unlocked_ioctl
= vfs_lib_ring_buffer_ioctl
,
466 .llseek
= vfs_lib_ring_buffer_no_llseek
,
468 .compat_ioctl
= vfs_lib_ring_buffer_compat_ioctl
,
471 EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations
);
473 MODULE_LICENSE("GPL and additional rights");
474 MODULE_AUTHOR("Mathieu Desnoyers");
475 MODULE_DESCRIPTION("Ring Buffer Library VFS");
476 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
477 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
478 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
479 LTTNG_MODULES_EXTRAVERSION
);