| 1 | /* |
| 2 | * ring_buffer_vfs.c |
| 3 | * |
| 4 | * Ring Buffer VFS file operations. |
| 5 | * |
| 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 |
| 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 | #include <lttng-tracer.h> |
| 32 | |
| 33 | static int put_ulong(unsigned long val, unsigned long arg) |
| 34 | { |
| 35 | return put_user(val, (unsigned long __user *)arg); |
| 36 | } |
| 37 | |
| 38 | #ifdef CONFIG_COMPAT |
| 39 | static int compat_put_ulong(compat_ulong_t val, unsigned long arg) |
| 40 | { |
| 41 | return put_user(val, (compat_ulong_t __user *)compat_ptr(arg)); |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | /* |
| 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. |
| 48 | */ |
| 49 | int lib_ring_buffer_open(struct inode *inode, struct file *file, |
| 50 | struct lib_ring_buffer *buf) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | if (!buf) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | ret = lib_ring_buffer_open_read(buf); |
| 58 | if (ret) |
| 59 | return ret; |
| 60 | |
| 61 | ret = nonseekable_open(inode, file); |
| 62 | if (ret) |
| 63 | goto release_read; |
| 64 | return 0; |
| 65 | |
| 66 | release_read: |
| 67 | lib_ring_buffer_release_read(buf); |
| 68 | return ret; |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(lib_ring_buffer_open); |
| 71 | |
| 72 | /** |
| 73 | * vfs_lib_ring_buffer_open - ring buffer open file operation |
| 74 | * @inode: opened inode |
| 75 | * @file: opened file |
| 76 | * |
| 77 | * Open implementation. Makes sure only one open instance of a buffer is |
| 78 | * done at a given moment. |
| 79 | */ |
| 80 | static |
| 81 | int vfs_lib_ring_buffer_open(struct inode *inode, struct file *file) |
| 82 | { |
| 83 | struct lib_ring_buffer *buf = inode->i_private; |
| 84 | |
| 85 | file->private_data = buf; |
| 86 | return lib_ring_buffer_open(inode, file, buf); |
| 87 | } |
| 88 | |
| 89 | int lib_ring_buffer_release(struct inode *inode, struct file *file, |
| 90 | struct lib_ring_buffer *buf) |
| 91 | { |
| 92 | lib_ring_buffer_release_read(buf); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(lib_ring_buffer_release); |
| 97 | |
| 98 | /** |
| 99 | * vfs_lib_ring_buffer_release - ring buffer release file operation |
| 100 | * @inode: opened inode |
| 101 | * @file: opened file |
| 102 | * |
| 103 | * Release implementation. |
| 104 | */ |
| 105 | static |
| 106 | int vfs_lib_ring_buffer_release(struct inode *inode, struct file *file) |
| 107 | { |
| 108 | struct lib_ring_buffer *buf = file->private_data; |
| 109 | |
| 110 | return lib_ring_buffer_release(inode, file, buf); |
| 111 | } |
| 112 | |
| 113 | unsigned int lib_ring_buffer_poll(struct file *filp, poll_table *wait, |
| 114 | struct lib_ring_buffer *buf) |
| 115 | { |
| 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; |
| 120 | |
| 121 | if (filp->f_mode & FMODE_READ) { |
| 122 | poll_wait_set_exclusive(wait); |
| 123 | poll_wait(filp, &buf->read_wait, wait); |
| 124 | |
| 125 | finalized = lib_ring_buffer_is_finalized(config, buf); |
| 126 | disabled = lib_ring_buffer_channel_is_disabled(chan); |
| 127 | |
| 128 | /* |
| 129 | * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering |
| 130 | * finalized load before offsets loads. |
| 131 | */ |
| 132 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); |
| 133 | retry: |
| 134 | if (disabled) |
| 135 | return POLLERR; |
| 136 | |
| 137 | if (subbuf_trunc(lib_ring_buffer_get_offset(config, buf), chan) |
| 138 | - subbuf_trunc(lib_ring_buffer_get_consumed(config, buf), chan) |
| 139 | == 0) { |
| 140 | if (finalized) |
| 141 | return POLLHUP; |
| 142 | else { |
| 143 | /* |
| 144 | * The memory barriers |
| 145 | * __wait_event()/wake_up_interruptible() take |
| 146 | * care of "raw_spin_is_locked" memory ordering. |
| 147 | */ |
| 148 | if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) |
| 149 | goto retry; |
| 150 | else |
| 151 | return 0; |
| 152 | } |
| 153 | } else { |
| 154 | if (subbuf_trunc(lib_ring_buffer_get_offset(config, buf), |
| 155 | chan) |
| 156 | - subbuf_trunc(lib_ring_buffer_get_consumed(config, buf), |
| 157 | chan) |
| 158 | >= chan->backend.buf_size) |
| 159 | return POLLPRI | POLLRDBAND; |
| 160 | else |
| 161 | return POLLIN | POLLRDNORM; |
| 162 | } |
| 163 | } |
| 164 | return mask; |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(lib_ring_buffer_poll); |
| 167 | |
| 168 | /** |
| 169 | * vfs_lib_ring_buffer_poll - ring buffer poll file operation |
| 170 | * @filp: the file |
| 171 | * @wait: poll table |
| 172 | * |
| 173 | * Poll implementation. |
| 174 | */ |
| 175 | static |
| 176 | unsigned int vfs_lib_ring_buffer_poll(struct file *filp, poll_table *wait) |
| 177 | { |
| 178 | struct lib_ring_buffer *buf = filp->private_data; |
| 179 | |
| 180 | return lib_ring_buffer_poll(filp, wait, buf); |
| 181 | } |
| 182 | |
| 183 | long lib_ring_buffer_ioctl(struct file *filp, unsigned int cmd, |
| 184 | unsigned long arg, struct lib_ring_buffer *buf) |
| 185 | { |
| 186 | struct channel *chan = buf->backend.chan; |
| 187 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 188 | |
| 189 | if (lib_ring_buffer_channel_is_disabled(chan)) |
| 190 | return -EIO; |
| 191 | |
| 192 | switch (cmd) { |
| 193 | case RING_BUFFER_SNAPSHOT: |
| 194 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 195 | &buf->prod_snapshot); |
| 196 | case RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS: |
| 197 | return lib_ring_buffer_snapshot_sample_positions(buf, |
| 198 | &buf->cons_snapshot, &buf->prod_snapshot); |
| 199 | case RING_BUFFER_SNAPSHOT_GET_CONSUMED: |
| 200 | return put_ulong(buf->cons_snapshot, arg); |
| 201 | case RING_BUFFER_SNAPSHOT_GET_PRODUCED: |
| 202 | return put_ulong(buf->prod_snapshot, arg); |
| 203 | case RING_BUFFER_GET_SUBBUF: |
| 204 | { |
| 205 | unsigned long uconsume; |
| 206 | long ret; |
| 207 | |
| 208 | ret = get_user(uconsume, (unsigned long __user *) arg); |
| 209 | if (ret) |
| 210 | return ret; /* will return -EFAULT */ |
| 211 | ret = lib_ring_buffer_get_subbuf(buf, uconsume); |
| 212 | if (!ret) { |
| 213 | /* Set file position to zero at each successful "get" */ |
| 214 | filp->f_pos = 0; |
| 215 | } |
| 216 | return ret; |
| 217 | } |
| 218 | case RING_BUFFER_PUT_SUBBUF: |
| 219 | lib_ring_buffer_put_subbuf(buf); |
| 220 | return 0; |
| 221 | |
| 222 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 223 | { |
| 224 | long ret; |
| 225 | |
| 226 | ret = lib_ring_buffer_get_next_subbuf(buf); |
| 227 | if (!ret) { |
| 228 | /* Set file position to zero at each successful "get" */ |
| 229 | filp->f_pos = 0; |
| 230 | } |
| 231 | return ret; |
| 232 | } |
| 233 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 234 | lib_ring_buffer_put_next_subbuf(buf); |
| 235 | return 0; |
| 236 | case RING_BUFFER_GET_SUBBUF_SIZE: |
| 237 | return put_ulong(lib_ring_buffer_get_read_data_size(config, buf), |
| 238 | arg); |
| 239 | case RING_BUFFER_GET_PADDED_SUBBUF_SIZE: |
| 240 | { |
| 241 | unsigned long size; |
| 242 | |
| 243 | size = lib_ring_buffer_get_read_data_size(config, buf); |
| 244 | size = PAGE_ALIGN(size); |
| 245 | return put_ulong(size, arg); |
| 246 | } |
| 247 | case RING_BUFFER_GET_MAX_SUBBUF_SIZE: |
| 248 | return put_ulong(chan->backend.subbuf_size, arg); |
| 249 | case RING_BUFFER_GET_MMAP_LEN: |
| 250 | { |
| 251 | unsigned long mmap_buf_len; |
| 252 | |
| 253 | if (config->output != RING_BUFFER_MMAP) |
| 254 | return -EINVAL; |
| 255 | mmap_buf_len = chan->backend.buf_size; |
| 256 | if (chan->backend.extra_reader_sb) |
| 257 | mmap_buf_len += chan->backend.subbuf_size; |
| 258 | if (mmap_buf_len > INT_MAX) |
| 259 | return -EFBIG; |
| 260 | return put_ulong(mmap_buf_len, arg); |
| 261 | } |
| 262 | case RING_BUFFER_GET_MMAP_READ_OFFSET: |
| 263 | { |
| 264 | unsigned long sb_bindex; |
| 265 | |
| 266 | if (config->output != RING_BUFFER_MMAP) |
| 267 | return -EINVAL; |
| 268 | sb_bindex = subbuffer_id_get_index(config, |
| 269 | buf->backend.buf_rsb.id); |
| 270 | return put_ulong(buf->backend.array[sb_bindex]->mmap_offset, |
| 271 | arg); |
| 272 | } |
| 273 | case RING_BUFFER_FLUSH: |
| 274 | lib_ring_buffer_switch_remote(buf); |
| 275 | return 0; |
| 276 | default: |
| 277 | return -ENOIOCTLCMD; |
| 278 | } |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(lib_ring_buffer_ioctl); |
| 281 | |
| 282 | /** |
| 283 | * vfs_lib_ring_buffer_ioctl - control ring buffer reader synchronization |
| 284 | * |
| 285 | * @filp: the file |
| 286 | * @cmd: the command |
| 287 | * @arg: command arg |
| 288 | * |
| 289 | * This ioctl implements commands necessary for producer/consumer |
| 290 | * and flight recorder reader interaction : |
| 291 | * RING_BUFFER_GET_NEXT_SUBBUF |
| 292 | * Get the next sub-buffer that can be read. It never blocks. |
| 293 | * RING_BUFFER_PUT_NEXT_SUBBUF |
| 294 | * Release the currently read sub-buffer. |
| 295 | * RING_BUFFER_GET_SUBBUF_SIZE |
| 296 | * returns the size of the current sub-buffer. |
| 297 | * RING_BUFFER_GET_MAX_SUBBUF_SIZE |
| 298 | * returns the maximum size for sub-buffers. |
| 299 | * RING_BUFFER_GET_NUM_SUBBUF |
| 300 | * returns the number of reader-visible sub-buffers in the per cpu |
| 301 | * channel (for mmap). |
| 302 | * RING_BUFFER_GET_MMAP_READ_OFFSET |
| 303 | * returns the offset of the subbuffer belonging to the reader. |
| 304 | * Should only be used for mmap clients. |
| 305 | */ |
| 306 | static |
| 307 | long vfs_lib_ring_buffer_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 308 | { |
| 309 | struct lib_ring_buffer *buf = filp->private_data; |
| 310 | |
| 311 | return lib_ring_buffer_ioctl(filp, cmd, arg, buf); |
| 312 | } |
| 313 | |
| 314 | #ifdef CONFIG_COMPAT |
| 315 | long lib_ring_buffer_compat_ioctl(struct file *filp, unsigned int cmd, |
| 316 | unsigned long arg, struct lib_ring_buffer *buf) |
| 317 | { |
| 318 | struct channel *chan = buf->backend.chan; |
| 319 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 320 | |
| 321 | if (lib_ring_buffer_channel_is_disabled(chan)) |
| 322 | return -EIO; |
| 323 | |
| 324 | switch (cmd) { |
| 325 | case RING_BUFFER_COMPAT_SNAPSHOT: |
| 326 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 327 | &buf->prod_snapshot); |
| 328 | case RING_BUFFER_COMPAT_SNAPSHOT_SAMPLE_POSITIONS: |
| 329 | return lib_ring_buffer_snapshot_sample_positions(buf, |
| 330 | &buf->cons_snapshot, &buf->prod_snapshot); |
| 331 | case RING_BUFFER_COMPAT_SNAPSHOT_GET_CONSUMED: |
| 332 | return compat_put_ulong(buf->cons_snapshot, arg); |
| 333 | case RING_BUFFER_COMPAT_SNAPSHOT_GET_PRODUCED: |
| 334 | return compat_put_ulong(buf->prod_snapshot, arg); |
| 335 | case RING_BUFFER_COMPAT_GET_SUBBUF: |
| 336 | { |
| 337 | __u32 uconsume; |
| 338 | unsigned long consume; |
| 339 | long ret; |
| 340 | |
| 341 | ret = get_user(uconsume, (__u32 __user *) arg); |
| 342 | if (ret) |
| 343 | return ret; /* will return -EFAULT */ |
| 344 | consume = buf->cons_snapshot; |
| 345 | consume &= ~0xFFFFFFFFL; |
| 346 | consume |= uconsume; |
| 347 | ret = lib_ring_buffer_get_subbuf(buf, consume); |
| 348 | if (!ret) { |
| 349 | /* Set file position to zero at each successful "get" */ |
| 350 | filp->f_pos = 0; |
| 351 | } |
| 352 | return ret; |
| 353 | } |
| 354 | case RING_BUFFER_COMPAT_PUT_SUBBUF: |
| 355 | lib_ring_buffer_put_subbuf(buf); |
| 356 | return 0; |
| 357 | |
| 358 | case RING_BUFFER_COMPAT_GET_NEXT_SUBBUF: |
| 359 | { |
| 360 | long ret; |
| 361 | |
| 362 | ret = lib_ring_buffer_get_next_subbuf(buf); |
| 363 | if (!ret) { |
| 364 | /* Set file position to zero at each successful "get" */ |
| 365 | filp->f_pos = 0; |
| 366 | } |
| 367 | return ret; |
| 368 | } |
| 369 | case RING_BUFFER_COMPAT_PUT_NEXT_SUBBUF: |
| 370 | lib_ring_buffer_put_next_subbuf(buf); |
| 371 | return 0; |
| 372 | case RING_BUFFER_COMPAT_GET_SUBBUF_SIZE: |
| 373 | { |
| 374 | unsigned long data_size; |
| 375 | |
| 376 | data_size = lib_ring_buffer_get_read_data_size(config, buf); |
| 377 | if (data_size > UINT_MAX) |
| 378 | return -EFBIG; |
| 379 | return compat_put_ulong(data_size, arg); |
| 380 | } |
| 381 | case RING_BUFFER_COMPAT_GET_PADDED_SUBBUF_SIZE: |
| 382 | { |
| 383 | unsigned long size; |
| 384 | |
| 385 | size = lib_ring_buffer_get_read_data_size(config, buf); |
| 386 | size = PAGE_ALIGN(size); |
| 387 | if (size > UINT_MAX) |
| 388 | return -EFBIG; |
| 389 | return compat_put_ulong(size, arg); |
| 390 | } |
| 391 | case RING_BUFFER_COMPAT_GET_MAX_SUBBUF_SIZE: |
| 392 | if (chan->backend.subbuf_size > UINT_MAX) |
| 393 | return -EFBIG; |
| 394 | return compat_put_ulong(chan->backend.subbuf_size, arg); |
| 395 | case RING_BUFFER_COMPAT_GET_MMAP_LEN: |
| 396 | { |
| 397 | unsigned long mmap_buf_len; |
| 398 | |
| 399 | if (config->output != RING_BUFFER_MMAP) |
| 400 | return -EINVAL; |
| 401 | mmap_buf_len = chan->backend.buf_size; |
| 402 | if (chan->backend.extra_reader_sb) |
| 403 | mmap_buf_len += chan->backend.subbuf_size; |
| 404 | if (mmap_buf_len > UINT_MAX) |
| 405 | return -EFBIG; |
| 406 | return compat_put_ulong(mmap_buf_len, arg); |
| 407 | } |
| 408 | case RING_BUFFER_COMPAT_GET_MMAP_READ_OFFSET: |
| 409 | { |
| 410 | unsigned long sb_bindex, read_offset; |
| 411 | |
| 412 | if (config->output != RING_BUFFER_MMAP) |
| 413 | return -EINVAL; |
| 414 | sb_bindex = subbuffer_id_get_index(config, |
| 415 | buf->backend.buf_rsb.id); |
| 416 | read_offset = buf->backend.array[sb_bindex]->mmap_offset; |
| 417 | if (read_offset > UINT_MAX) |
| 418 | return -EINVAL; |
| 419 | return compat_put_ulong(read_offset, arg); |
| 420 | } |
| 421 | case RING_BUFFER_COMPAT_FLUSH: |
| 422 | lib_ring_buffer_switch_remote(buf); |
| 423 | return 0; |
| 424 | default: |
| 425 | return -ENOIOCTLCMD; |
| 426 | } |
| 427 | } |
| 428 | EXPORT_SYMBOL_GPL(lib_ring_buffer_compat_ioctl); |
| 429 | |
| 430 | static |
| 431 | long vfs_lib_ring_buffer_compat_ioctl(struct file *filp, unsigned int cmd, |
| 432 | unsigned long arg) |
| 433 | { |
| 434 | struct lib_ring_buffer *buf = filp->private_data; |
| 435 | |
| 436 | return lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); |
| 437 | } |
| 438 | #endif |
| 439 | |
| 440 | const struct file_operations lib_ring_buffer_file_operations = { |
| 441 | .owner = THIS_MODULE, |
| 442 | .open = vfs_lib_ring_buffer_open, |
| 443 | .release = vfs_lib_ring_buffer_release, |
| 444 | .poll = vfs_lib_ring_buffer_poll, |
| 445 | .splice_read = vfs_lib_ring_buffer_splice_read, |
| 446 | .mmap = vfs_lib_ring_buffer_mmap, |
| 447 | .unlocked_ioctl = vfs_lib_ring_buffer_ioctl, |
| 448 | .llseek = vfs_lib_ring_buffer_no_llseek, |
| 449 | #ifdef CONFIG_COMPAT |
| 450 | .compat_ioctl = vfs_lib_ring_buffer_compat_ioctl, |
| 451 | #endif |
| 452 | }; |
| 453 | EXPORT_SYMBOL_GPL(lib_ring_buffer_file_operations); |
| 454 | |
| 455 | MODULE_LICENSE("GPL and additional rights"); |
| 456 | MODULE_AUTHOR("Mathieu Desnoyers"); |
| 457 | MODULE_DESCRIPTION("Ring Buffer Library VFS"); |
| 458 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 459 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 460 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 461 | LTTNG_MODULES_EXTRAVERSION); |