Commit | Line | Data |
---|---|---|
f3bc08c5 MD |
1 | /* |
2 | * ring_buffer_vfs.c | |
3 | * | |
f3bc08c5 MD |
4 | * Ring Buffer VFS file operations. |
5 | * | |
886d51a3 MD |
6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
f3bc08c5 MD |
21 | */ |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/fs.h> | |
25 | #include <linux/compat.h> | |
26 | ||
27 | #include "../../wrapper/ringbuffer/backend.h" | |
28 | #include "../../wrapper/ringbuffer/frontend.h" | |
29 | #include "../../wrapper/ringbuffer/vfs.h" | |
30 | #include "../../wrapper/poll.h" | |
31 | ||
32 | static int put_ulong(unsigned long val, unsigned long arg) | |
33 | { | |
34 | return put_user(val, (unsigned long __user *)arg); | |
35 | } | |
36 | ||
37 | #ifdef CONFIG_COMPAT | |
38 | static int compat_put_ulong(compat_ulong_t val, unsigned long arg) | |
39 | { | |
40 | return put_user(val, (compat_ulong_t __user *)compat_ptr(arg)); | |
41 | } | |
42 | #endif | |
43 | ||
b3b8072b MD |
44 | /* |
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. | |
47 | */ | |
d83004aa JD |
48 | int lib_ring_buffer_open(struct inode *inode, struct file *file, |
49 | struct lib_ring_buffer *buf) | |
f3bc08c5 | 50 | { |
f3bc08c5 MD |
51 | int ret; |
52 | ||
733ce41d MD |
53 | if (!buf) |
54 | return -EINVAL; | |
55 | ||
f3bc08c5 MD |
56 | ret = lib_ring_buffer_open_read(buf); |
57 | if (ret) | |
58 | return ret; | |
59 | ||
f3bc08c5 MD |
60 | ret = nonseekable_open(inode, file); |
61 | if (ret) | |
62 | goto release_read; | |
63 | return 0; | |
64 | ||
65 | release_read: | |
66 | lib_ring_buffer_release_read(buf); | |
67 | return ret; | |
68 | } | |
d83004aa | 69 | EXPORT_SYMBOL_GPL(lib_ring_buffer_open); |
f3bc08c5 MD |
70 | |
71 | /** | |
d83004aa | 72 | * vfs_lib_ring_buffer_open - ring buffer open file operation |
f3bc08c5 MD |
73 | * @inode: opened inode |
74 | * @file: opened file | |
75 | * | |
d83004aa JD |
76 | * Open implementation. Makes sure only one open instance of a buffer is |
77 | * done at a given moment. | |
f3bc08c5 | 78 | */ |
d83004aa JD |
79 | static |
80 | int vfs_lib_ring_buffer_open(struct inode *inode, struct file *file) | |
f3bc08c5 | 81 | { |
d83004aa JD |
82 | struct lib_ring_buffer *buf = inode->i_private; |
83 | ||
84 | file->private_data = buf; | |
85 | return lib_ring_buffer_open(inode, file, buf); | |
86 | } | |
f3bc08c5 | 87 | |
d83004aa JD |
88 | int lib_ring_buffer_release(struct inode *inode, struct file *file, |
89 | struct lib_ring_buffer *buf) | |
90 | { | |
f3bc08c5 MD |
91 | lib_ring_buffer_release_read(buf); |
92 | ||
93 | return 0; | |
94 | } | |
d83004aa | 95 | EXPORT_SYMBOL_GPL(lib_ring_buffer_release); |
f3bc08c5 MD |
96 | |
97 | /** | |
d83004aa JD |
98 | * vfs_lib_ring_buffer_release - ring buffer release file operation |
99 | * @inode: opened inode | |
100 | * @file: opened file | |
f3bc08c5 | 101 | * |
d83004aa | 102 | * Release implementation. |
f3bc08c5 | 103 | */ |
d83004aa JD |
104 | static |
105 | int vfs_lib_ring_buffer_release(struct inode *inode, struct file *file) | |
106 | { | |
107 | struct lib_ring_buffer *buf = file->private_data; | |
108 | ||
109 | return lib_ring_buffer_release(inode, file, buf); | |
110 | } | |
111 | ||
112 | unsigned int lib_ring_buffer_poll(struct file *filp, poll_table *wait, | |
113 | struct lib_ring_buffer *buf) | |
f3bc08c5 MD |
114 | { |
115 | unsigned int mask = 0; | |
f3bc08c5 | 116 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 117 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
254ec7bc | 118 | int finalized, disabled; |
f3bc08c5 MD |
119 | |
120 | if (filp->f_mode & FMODE_READ) { | |
a33e44a6 | 121 | poll_wait_set_exclusive(wait); |
f3bc08c5 MD |
122 | poll_wait(filp, &buf->read_wait, wait); |
123 | ||
124 | finalized = lib_ring_buffer_is_finalized(config, buf); | |
254ec7bc MD |
125 | disabled = lib_ring_buffer_channel_is_disabled(chan); |
126 | ||
f3bc08c5 MD |
127 | /* |
128 | * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering | |
129 | * finalized load before offsets loads. | |
130 | */ | |
131 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
132 | retry: | |
254ec7bc MD |
133 | if (disabled) |
134 | return POLLERR; | |
135 | ||
f3bc08c5 MD |
136 | if (subbuf_trunc(lib_ring_buffer_get_offset(config, buf), chan) |
137 | - subbuf_trunc(lib_ring_buffer_get_consumed(config, buf), chan) | |
138 | == 0) { | |
139 | if (finalized) | |
140 | return POLLHUP; | |
141 | else { | |
142 | /* | |
143 | * The memory barriers | |
144 | * __wait_event()/wake_up_interruptible() take | |
145 | * care of "raw_spin_is_locked" memory ordering. | |
146 | */ | |
147 | if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) | |
148 | goto retry; | |
149 | else | |
150 | return 0; | |
151 | } | |
152 | } else { | |
153 | if (subbuf_trunc(lib_ring_buffer_get_offset(config, buf), | |
154 | chan) | |
155 | - subbuf_trunc(lib_ring_buffer_get_consumed(config, buf), | |
156 | chan) | |
157 | >= chan->backend.buf_size) | |
158 | return POLLPRI | POLLRDBAND; | |
159 | else | |
160 | return POLLIN | POLLRDNORM; | |
161 | } | |
162 | } | |
163 | return mask; | |
164 | } | |
d83004aa | 165 | EXPORT_SYMBOL_GPL(lib_ring_buffer_poll); |
f3bc08c5 MD |
166 | |
167 | /** | |
d83004aa | 168 | * vfs_lib_ring_buffer_poll - ring buffer poll file operation |
f3bc08c5 | 169 | * @filp: the file |
d83004aa | 170 | * @wait: poll table |
f3bc08c5 | 171 | * |
d83004aa | 172 | * Poll implementation. |
f3bc08c5 | 173 | */ |
d83004aa JD |
174 | static |
175 | unsigned int vfs_lib_ring_buffer_poll(struct file *filp, poll_table *wait) | |
f3bc08c5 MD |
176 | { |
177 | struct lib_ring_buffer *buf = filp->private_data; | |
d83004aa JD |
178 | |
179 | return lib_ring_buffer_poll(filp, wait, buf); | |
180 | } | |
181 | ||
182 | long lib_ring_buffer_ioctl(struct file *filp, unsigned int cmd, | |
183 | unsigned long arg, struct lib_ring_buffer *buf) | |
184 | { | |
f3bc08c5 | 185 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 186 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 | 187 | |
254ec7bc MD |
188 | if (lib_ring_buffer_channel_is_disabled(chan)) |
189 | return -EIO; | |
190 | ||
f3bc08c5 MD |
191 | switch (cmd) { |
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: | |
200 | { | |
201 | unsigned long uconsume; | |
202 | long ret; | |
203 | ||
204 | ret = get_user(uconsume, (unsigned long __user *) arg); | |
205 | if (ret) | |
206 | return ret; /* will return -EFAULT */ | |
207 | ret = lib_ring_buffer_get_subbuf(buf, uconsume); | |
208 | if (!ret) { | |
209 | /* Set file position to zero at each successful "get" */ | |
210 | filp->f_pos = 0; | |
211 | } | |
212 | return ret; | |
213 | } | |
214 | case RING_BUFFER_PUT_SUBBUF: | |
215 | lib_ring_buffer_put_subbuf(buf); | |
216 | return 0; | |
217 | ||
218 | case RING_BUFFER_GET_NEXT_SUBBUF: | |
219 | { | |
220 | long ret; | |
221 | ||
222 | ret = lib_ring_buffer_get_next_subbuf(buf); | |
223 | if (!ret) { | |
224 | /* Set file position to zero at each successful "get" */ | |
225 | filp->f_pos = 0; | |
226 | } | |
227 | return ret; | |
228 | } | |
229 | case RING_BUFFER_PUT_NEXT_SUBBUF: | |
230 | lib_ring_buffer_put_next_subbuf(buf); | |
231 | return 0; | |
232 | case RING_BUFFER_GET_SUBBUF_SIZE: | |
233 | return put_ulong(lib_ring_buffer_get_read_data_size(config, buf), | |
234 | arg); | |
235 | case RING_BUFFER_GET_PADDED_SUBBUF_SIZE: | |
236 | { | |
237 | unsigned long size; | |
238 | ||
239 | size = lib_ring_buffer_get_read_data_size(config, buf); | |
240 | size = PAGE_ALIGN(size); | |
241 | return put_ulong(size, arg); | |
242 | } | |
243 | case RING_BUFFER_GET_MAX_SUBBUF_SIZE: | |
244 | return put_ulong(chan->backend.subbuf_size, arg); | |
245 | case RING_BUFFER_GET_MMAP_LEN: | |
246 | { | |
247 | unsigned long mmap_buf_len; | |
248 | ||
249 | if (config->output != RING_BUFFER_MMAP) | |
250 | return -EINVAL; | |
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) | |
255 | return -EFBIG; | |
256 | return put_ulong(mmap_buf_len, arg); | |
257 | } | |
258 | case RING_BUFFER_GET_MMAP_READ_OFFSET: | |
259 | { | |
260 | unsigned long sb_bindex; | |
261 | ||
262 | if (config->output != RING_BUFFER_MMAP) | |
263 | return -EINVAL; | |
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, | |
267 | arg); | |
268 | } | |
5c055266 | 269 | case RING_BUFFER_FLUSH: |
5e391252 | 270 | lib_ring_buffer_switch_remote(buf); |
5c055266 | 271 | return 0; |
f3bc08c5 MD |
272 | default: |
273 | return -ENOIOCTLCMD; | |
274 | } | |
275 | } | |
d83004aa JD |
276 | EXPORT_SYMBOL_GPL(lib_ring_buffer_ioctl); |
277 | ||
278 | /** | |
279 | * vfs_lib_ring_buffer_ioctl - control ring buffer reader synchronization | |
280 | * | |
281 | * @filp: the file | |
282 | * @cmd: the command | |
283 | * @arg: command arg | |
284 | * | |
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. | |
301 | */ | |
302 | static | |
303 | long vfs_lib_ring_buffer_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
304 | { | |
305 | struct lib_ring_buffer *buf = filp->private_data; | |
306 | ||
307 | return lib_ring_buffer_ioctl(filp, cmd, arg, buf); | |
308 | } | |
f3bc08c5 MD |
309 | |
310 | #ifdef CONFIG_COMPAT | |
311 | long lib_ring_buffer_compat_ioctl(struct file *filp, unsigned int cmd, | |
d83004aa | 312 | unsigned long arg, struct lib_ring_buffer *buf) |
f3bc08c5 | 313 | { |
f3bc08c5 | 314 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 315 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
254ec7bc MD |
316 | |
317 | if (lib_ring_buffer_channel_is_disabled(chan)) | |
318 | return -EIO; | |
f3bc08c5 MD |
319 | |
320 | switch (cmd) { | |
216f7feb | 321 | case RING_BUFFER_COMPAT_SNAPSHOT: |
f3bc08c5 MD |
322 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
323 | &buf->prod_snapshot); | |
216f7feb | 324 | case RING_BUFFER_COMPAT_SNAPSHOT_GET_CONSUMED: |
f3bc08c5 | 325 | return compat_put_ulong(buf->cons_snapshot, arg); |
216f7feb | 326 | case RING_BUFFER_COMPAT_SNAPSHOT_GET_PRODUCED: |
f3bc08c5 | 327 | return compat_put_ulong(buf->prod_snapshot, arg); |
216f7feb | 328 | case RING_BUFFER_COMPAT_GET_SUBBUF: |
f3bc08c5 MD |
329 | { |
330 | __u32 uconsume; | |
331 | unsigned long consume; | |
332 | long ret; | |
333 | ||
334 | ret = get_user(uconsume, (__u32 __user *) arg); | |
335 | if (ret) | |
336 | return ret; /* will return -EFAULT */ | |
337 | consume = buf->cons_snapshot; | |
338 | consume &= ~0xFFFFFFFFL; | |
339 | consume |= uconsume; | |
340 | ret = lib_ring_buffer_get_subbuf(buf, consume); | |
341 | if (!ret) { | |
342 | /* Set file position to zero at each successful "get" */ | |
343 | filp->f_pos = 0; | |
344 | } | |
345 | return ret; | |
346 | } | |
216f7feb | 347 | case RING_BUFFER_COMPAT_PUT_SUBBUF: |
f3bc08c5 MD |
348 | lib_ring_buffer_put_subbuf(buf); |
349 | return 0; | |
350 | ||
216f7feb | 351 | case RING_BUFFER_COMPAT_GET_NEXT_SUBBUF: |
f3bc08c5 MD |
352 | { |
353 | long ret; | |
354 | ||
355 | ret = lib_ring_buffer_get_next_subbuf(buf); | |
356 | if (!ret) { | |
357 | /* Set file position to zero at each successful "get" */ | |
358 | filp->f_pos = 0; | |
359 | } | |
360 | return ret; | |
361 | } | |
216f7feb | 362 | case RING_BUFFER_COMPAT_PUT_NEXT_SUBBUF: |
f3bc08c5 MD |
363 | lib_ring_buffer_put_next_subbuf(buf); |
364 | return 0; | |
216f7feb | 365 | case RING_BUFFER_COMPAT_GET_SUBBUF_SIZE: |
f3bc08c5 MD |
366 | { |
367 | unsigned long data_size; | |
368 | ||
369 | data_size = lib_ring_buffer_get_read_data_size(config, buf); | |
370 | if (data_size > UINT_MAX) | |
371 | return -EFBIG; | |
4343ec8e | 372 | return compat_put_ulong(data_size, arg); |
f3bc08c5 | 373 | } |
216f7feb | 374 | case RING_BUFFER_COMPAT_GET_PADDED_SUBBUF_SIZE: |
f3bc08c5 MD |
375 | { |
376 | unsigned long size; | |
377 | ||
378 | size = lib_ring_buffer_get_read_data_size(config, buf); | |
379 | size = PAGE_ALIGN(size); | |
380 | if (size > UINT_MAX) | |
381 | return -EFBIG; | |
4343ec8e | 382 | return compat_put_ulong(size, arg); |
f3bc08c5 | 383 | } |
216f7feb | 384 | case RING_BUFFER_COMPAT_GET_MAX_SUBBUF_SIZE: |
f3bc08c5 MD |
385 | if (chan->backend.subbuf_size > UINT_MAX) |
386 | return -EFBIG; | |
4343ec8e | 387 | return compat_put_ulong(chan->backend.subbuf_size, arg); |
216f7feb | 388 | case RING_BUFFER_COMPAT_GET_MMAP_LEN: |
f3bc08c5 MD |
389 | { |
390 | unsigned long mmap_buf_len; | |
391 | ||
392 | if (config->output != RING_BUFFER_MMAP) | |
393 | return -EINVAL; | |
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) | |
398 | return -EFBIG; | |
4343ec8e | 399 | return compat_put_ulong(mmap_buf_len, arg); |
f3bc08c5 | 400 | } |
216f7feb | 401 | case RING_BUFFER_COMPAT_GET_MMAP_READ_OFFSET: |
f3bc08c5 MD |
402 | { |
403 | unsigned long sb_bindex, read_offset; | |
404 | ||
405 | if (config->output != RING_BUFFER_MMAP) | |
406 | return -EINVAL; | |
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) | |
411 | return -EINVAL; | |
4343ec8e | 412 | return compat_put_ulong(read_offset, arg); |
f3bc08c5 | 413 | } |
216f7feb | 414 | case RING_BUFFER_COMPAT_FLUSH: |
5e391252 | 415 | lib_ring_buffer_switch_remote(buf); |
5c055266 | 416 | return 0; |
f3bc08c5 MD |
417 | default: |
418 | return -ENOIOCTLCMD; | |
419 | } | |
420 | } | |
d83004aa JD |
421 | EXPORT_SYMBOL_GPL(lib_ring_buffer_compat_ioctl); |
422 | ||
423 | static | |
424 | long vfs_lib_ring_buffer_compat_ioctl(struct file *filp, unsigned int cmd, | |
425 | unsigned long arg) | |
426 | { | |
427 | struct lib_ring_buffer *buf = filp->private_data; | |
428 | ||
429 | return lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); | |
430 | } | |
f3bc08c5 MD |
431 | #endif |
432 | ||
433 | const struct file_operations lib_ring_buffer_file_operations = { | |
a33c9927 | 434 | .owner = THIS_MODULE, |
d83004aa JD |
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, | |
f3bc08c5 | 442 | #ifdef CONFIG_COMPAT |
d83004aa | 443 | .compat_ioctl = vfs_lib_ring_buffer_compat_ioctl, |
f3bc08c5 MD |
444 | #endif |
445 | }; | |
446 | EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations); | |
447 | ||
448 | MODULE_LICENSE("GPL and additional rights"); | |
449 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
450 | MODULE_DESCRIPTION("Ring Buffer Library VFS"); |