Move headers under include/
[lttng-modules.git] / lib / ringbuffer / ring_buffer_iterator.c
1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
2 *
3 * ring_buffer_iterator.c
4 *
5 * Ring buffer and channel iterators. Get each event of a channel in order. Uses
6 * a prio heap for per-cpu buffers, giving a O(log(NR_CPUS)) algorithmic
7 * complexity for the "get next event" operation.
8 *
9 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 */
11
12 #include <ringbuffer/iterator.h>
13 #include <linux/file.h>
14 #include <linux/uaccess.h>
15 #include <linux/jiffies.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18
19 /*
20 * Safety factor taking into account internal kernel interrupt latency.
21 * Assuming 250ms worse-case latency.
22 */
23 #define MAX_SYSTEM_LATENCY 250
24
25 /*
26 * Maximum delta expected between trace clocks. At most 1 jiffy delta.
27 */
28 #define MAX_CLOCK_DELTA (jiffies_to_usecs(1) * 1000)
29
30 /**
31 * lib_ring_buffer_get_next_record - Get the next record in a buffer.
32 * @chan: channel
33 * @buf: buffer
34 *
35 * Returns the size of the event read, -EAGAIN if buffer is empty, -ENODATA if
36 * buffer is empty and finalized. The buffer must already be opened for reading.
37 */
38 ssize_t lib_ring_buffer_get_next_record(struct channel *chan,
39 struct lib_ring_buffer *buf)
40 {
41 const struct lib_ring_buffer_config *config = &chan->backend.config;
42 struct lib_ring_buffer_iter *iter = &buf->iter;
43 int ret;
44
45 restart:
46 switch (iter->state) {
47 case ITER_GET_SUBBUF:
48 ret = lib_ring_buffer_get_next_subbuf(buf);
49 if (ret && !READ_ONCE(buf->finalized)
50 && config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
51 /*
52 * Use "pull" scheme for global buffers. The reader
53 * itself flushes the buffer to "pull" data not visible
54 * to readers yet. Flush current subbuffer and re-try.
55 *
56 * Per-CPU buffers rather use a "push" scheme because
57 * the IPI needed to flush all CPU's buffers is too
58 * costly. In the "push" scheme, the reader waits for
59 * the writer periodic timer to flush the
60 * buffers (keeping track of a quiescent state
61 * timestamp). Therefore, the writer "pushes" data out
62 * of the buffers rather than letting the reader "pull"
63 * data from the buffer.
64 */
65 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
66 ret = lib_ring_buffer_get_next_subbuf(buf);
67 }
68 if (ret)
69 return ret;
70 iter->consumed = buf->cons_snapshot;
71 iter->data_size = lib_ring_buffer_get_read_data_size(config, buf);
72 iter->read_offset = iter->consumed;
73 /* skip header */
74 iter->read_offset += config->cb.subbuffer_header_size();
75 iter->state = ITER_TEST_RECORD;
76 goto restart;
77 case ITER_TEST_RECORD:
78 if (iter->read_offset - iter->consumed >= iter->data_size) {
79 iter->state = ITER_PUT_SUBBUF;
80 } else {
81 CHAN_WARN_ON(chan, !config->cb.record_get);
82 config->cb.record_get(config, chan, buf,
83 iter->read_offset,
84 &iter->header_len,
85 &iter->payload_len,
86 &iter->timestamp);
87 iter->read_offset += iter->header_len;
88 subbuffer_consume_record(config, &buf->backend);
89 iter->state = ITER_NEXT_RECORD;
90 return iter->payload_len;
91 }
92 goto restart;
93 case ITER_NEXT_RECORD:
94 iter->read_offset += iter->payload_len;
95 iter->state = ITER_TEST_RECORD;
96 goto restart;
97 case ITER_PUT_SUBBUF:
98 lib_ring_buffer_put_next_subbuf(buf);
99 iter->state = ITER_GET_SUBBUF;
100 goto restart;
101 default:
102 CHAN_WARN_ON(chan, 1); /* Should not happen */
103 return -EPERM;
104 }
105 }
106 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_next_record);
107
108 static int buf_is_higher(void *a, void *b)
109 {
110 struct lib_ring_buffer *bufa = a;
111 struct lib_ring_buffer *bufb = b;
112
113 /* Consider lowest timestamps to be at the top of the heap */
114 return (bufa->iter.timestamp < bufb->iter.timestamp);
115 }
116
117 static
118 void lib_ring_buffer_get_empty_buf_records(const struct lib_ring_buffer_config *config,
119 struct channel *chan)
120 {
121 struct lttng_ptr_heap *heap = &chan->iter.heap;
122 struct lib_ring_buffer *buf, *tmp;
123 ssize_t len;
124
125 list_for_each_entry_safe(buf, tmp, &chan->iter.empty_head,
126 iter.empty_node) {
127 len = lib_ring_buffer_get_next_record(chan, buf);
128
129 /*
130 * Deal with -EAGAIN and -ENODATA.
131 * len >= 0 means record contains data.
132 * -EBUSY should never happen, because we support only one
133 * reader.
134 */
135 switch (len) {
136 case -EAGAIN:
137 /* Keep node in empty list */
138 break;
139 case -ENODATA:
140 /*
141 * Buffer is finalized. Don't add to list of empty
142 * buffer, because it has no more data to provide, ever.
143 */
144 list_del(&buf->iter.empty_node);
145 break;
146 case -EBUSY:
147 CHAN_WARN_ON(chan, 1);
148 break;
149 default:
150 /*
151 * Insert buffer into the heap, remove from empty buffer
152 * list.
153 */
154 CHAN_WARN_ON(chan, len < 0);
155 list_del(&buf->iter.empty_node);
156 CHAN_WARN_ON(chan, lttng_heap_insert(heap, buf));
157 }
158 }
159 }
160
161 static
162 void lib_ring_buffer_wait_for_qs(const struct lib_ring_buffer_config *config,
163 struct channel *chan)
164 {
165 u64 timestamp_qs;
166 unsigned long wait_msecs;
167
168 /*
169 * No need to wait if no empty buffers are present.
170 */
171 if (list_empty(&chan->iter.empty_head))
172 return;
173
174 timestamp_qs = config->cb.ring_buffer_clock_read(chan);
175 /*
176 * We need to consider previously empty buffers.
177 * Do a get next buf record on each of them. Add them to
178 * the heap if they have data. If at least one of them
179 * don't have data, we need to wait for
180 * switch_timer_interval + MAX_SYSTEM_LATENCY (so we are sure the
181 * buffers have been switched either by the timer or idle entry) and
182 * check them again, adding them if they have data.
183 */
184 lib_ring_buffer_get_empty_buf_records(config, chan);
185
186 /*
187 * No need to wait if no empty buffers are present.
188 */
189 if (list_empty(&chan->iter.empty_head))
190 return;
191
192 /*
193 * We need to wait for the buffer switch timer to run. If the
194 * CPU is idle, idle entry performed the switch.
195 * TODO: we could optimize further by skipping the sleep if all
196 * empty buffers belong to idle or offline cpus.
197 */
198 wait_msecs = jiffies_to_msecs(chan->switch_timer_interval);
199 wait_msecs += MAX_SYSTEM_LATENCY;
200 msleep(wait_msecs);
201 lib_ring_buffer_get_empty_buf_records(config, chan);
202 /*
203 * Any buffer still in the empty list here cannot possibly
204 * contain an event with a timestamp prior to "timestamp_qs".
205 * The new quiescent state timestamp is the one we grabbed
206 * before waiting for buffer data. It is therefore safe to
207 * ignore empty buffers up to last_qs timestamp for fusion
208 * merge.
209 */
210 chan->iter.last_qs = timestamp_qs;
211 }
212
213 /**
214 * channel_get_next_record - Get the next record in a channel.
215 * @chan: channel
216 * @ret_buf: the buffer in which the event is located (output)
217 *
218 * Returns the size of new current event, -EAGAIN if all buffers are empty,
219 * -ENODATA if all buffers are empty and finalized. The channel must already be
220 * opened for reading.
221 */
222
223 ssize_t channel_get_next_record(struct channel *chan,
224 struct lib_ring_buffer **ret_buf)
225 {
226 const struct lib_ring_buffer_config *config = &chan->backend.config;
227 struct lib_ring_buffer *buf;
228 struct lttng_ptr_heap *heap;
229 ssize_t len;
230
231 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
232 *ret_buf = channel_get_ring_buffer(config, chan, 0);
233 return lib_ring_buffer_get_next_record(chan, *ret_buf);
234 }
235
236 heap = &chan->iter.heap;
237
238 /*
239 * get next record for topmost buffer.
240 */
241 buf = lttng_heap_maximum(heap);
242 if (buf) {
243 len = lib_ring_buffer_get_next_record(chan, buf);
244 /*
245 * Deal with -EAGAIN and -ENODATA.
246 * len >= 0 means record contains data.
247 */
248 switch (len) {
249 case -EAGAIN:
250 buf->iter.timestamp = 0;
251 list_add(&buf->iter.empty_node, &chan->iter.empty_head);
252 /* Remove topmost buffer from the heap */
253 CHAN_WARN_ON(chan, lttng_heap_remove(heap) != buf);
254 break;
255 case -ENODATA:
256 /*
257 * Buffer is finalized. Remove buffer from heap and
258 * don't add to list of empty buffer, because it has no
259 * more data to provide, ever.
260 */
261 CHAN_WARN_ON(chan, lttng_heap_remove(heap) != buf);
262 break;
263 case -EBUSY:
264 CHAN_WARN_ON(chan, 1);
265 break;
266 default:
267 /*
268 * Reinsert buffer into the heap. Note that heap can be
269 * partially empty, so we need to use
270 * lttng_heap_replace_max().
271 */
272 CHAN_WARN_ON(chan, len < 0);
273 CHAN_WARN_ON(chan, lttng_heap_replace_max(heap, buf) != buf);
274 break;
275 }
276 }
277
278 buf = lttng_heap_maximum(heap);
279 if (!buf || buf->iter.timestamp > chan->iter.last_qs) {
280 /*
281 * Deal with buffers previously showing no data.
282 * Add buffers containing data to the heap, update
283 * last_qs.
284 */
285 lib_ring_buffer_wait_for_qs(config, chan);
286 }
287
288 *ret_buf = buf = lttng_heap_maximum(heap);
289 if (buf) {
290 /*
291 * If this warning triggers, you probably need to check your
292 * system interrupt latency. Typical causes: too many printk()
293 * output going to a serial console with interrupts off.
294 * Allow for MAX_CLOCK_DELTA ns timestamp delta going backward.
295 * Observed on SMP KVM setups with trace_clock().
296 */
297 if (chan->iter.last_timestamp
298 > (buf->iter.timestamp + MAX_CLOCK_DELTA)) {
299 printk(KERN_WARNING "ring_buffer: timestamps going "
300 "backward. Last time %llu ns, cpu %d, "
301 "current time %llu ns, cpu %d, "
302 "delta %llu ns.\n",
303 chan->iter.last_timestamp, chan->iter.last_cpu,
304 buf->iter.timestamp, buf->backend.cpu,
305 chan->iter.last_timestamp - buf->iter.timestamp);
306 CHAN_WARN_ON(chan, 1);
307 }
308 chan->iter.last_timestamp = buf->iter.timestamp;
309 chan->iter.last_cpu = buf->backend.cpu;
310 return buf->iter.payload_len;
311 } else {
312 /* Heap is empty */
313 if (list_empty(&chan->iter.empty_head))
314 return -ENODATA; /* All buffers finalized */
315 else
316 return -EAGAIN; /* Temporarily empty */
317 }
318 }
319 EXPORT_SYMBOL_GPL(channel_get_next_record);
320
321 static
322 void lib_ring_buffer_iterator_init(struct channel *chan, struct lib_ring_buffer *buf)
323 {
324 if (buf->iter.allocated)
325 return;
326
327 buf->iter.allocated = 1;
328 if (chan->iter.read_open && !buf->iter.read_open) {
329 CHAN_WARN_ON(chan, lib_ring_buffer_open_read(buf) != 0);
330 buf->iter.read_open = 1;
331 }
332
333 /* Add to list of buffers without any current record */
334 if (chan->backend.config.alloc == RING_BUFFER_ALLOC_PER_CPU)
335 list_add(&buf->iter.empty_node, &chan->iter.empty_head);
336 }
337
338 int lttng_cpuhp_rb_iter_online(unsigned int cpu,
339 struct lttng_cpuhp_node *node)
340 {
341 struct channel *chan = container_of(node, struct channel,
342 cpuhp_iter_online);
343 struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
344 const struct lib_ring_buffer_config *config = &chan->backend.config;
345
346 CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
347
348 lib_ring_buffer_iterator_init(chan, buf);
349 return 0;
350 }
351 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_iter_online);
352
353 int channel_iterator_init(struct channel *chan)
354 {
355 const struct lib_ring_buffer_config *config = &chan->backend.config;
356 struct lib_ring_buffer *buf;
357
358 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
359 int ret;
360
361 INIT_LIST_HEAD(&chan->iter.empty_head);
362 ret = lttng_heap_init(&chan->iter.heap,
363 num_possible_cpus(),
364 GFP_KERNEL, buf_is_higher);
365 if (ret)
366 return ret;
367
368 chan->cpuhp_iter_online.component = LTTNG_RING_BUFFER_ITER;
369 ret = cpuhp_state_add_instance(lttng_rb_hp_online,
370 &chan->cpuhp_iter_online.node);
371 if (ret)
372 return ret;
373 } else {
374 buf = channel_get_ring_buffer(config, chan, 0);
375 lib_ring_buffer_iterator_init(chan, buf);
376 }
377 return 0;
378 }
379
380 void channel_iterator_unregister_notifiers(struct channel *chan)
381 {
382 const struct lib_ring_buffer_config *config = &chan->backend.config;
383
384 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
385 {
386 int ret;
387
388 ret = cpuhp_state_remove_instance(lttng_rb_hp_online,
389 &chan->cpuhp_iter_online.node);
390 WARN_ON(ret);
391 }
392 }
393 }
394
395 void channel_iterator_free(struct channel *chan)
396 {
397 const struct lib_ring_buffer_config *config = &chan->backend.config;
398
399 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
400 lttng_heap_free(&chan->iter.heap);
401 }
402
403 int lib_ring_buffer_iterator_open(struct lib_ring_buffer *buf)
404 {
405 struct channel *chan = buf->backend.chan;
406 const struct lib_ring_buffer_config *config = &chan->backend.config;
407 CHAN_WARN_ON(chan, config->output != RING_BUFFER_ITERATOR);
408 return lib_ring_buffer_open_read(buf);
409 }
410 EXPORT_SYMBOL_GPL(lib_ring_buffer_iterator_open);
411
412 /*
413 * Note: Iterators must not be mixed with other types of outputs, because an
414 * iterator can leave the buffer in "GET" state, which is not consistent with
415 * other types of output (mmap, splice, raw data read).
416 */
417 void lib_ring_buffer_iterator_release(struct lib_ring_buffer *buf)
418 {
419 lib_ring_buffer_release_read(buf);
420 }
421 EXPORT_SYMBOL_GPL(lib_ring_buffer_iterator_release);
422
423 int channel_iterator_open(struct channel *chan)
424 {
425 const struct lib_ring_buffer_config *config = &chan->backend.config;
426 struct lib_ring_buffer *buf;
427 int ret = 0, cpu;
428
429 CHAN_WARN_ON(chan, config->output != RING_BUFFER_ITERATOR);
430
431 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
432 get_online_cpus();
433 /* Allow CPU hotplug to keep track of opened reader */
434 chan->iter.read_open = 1;
435 for_each_channel_cpu(cpu, chan) {
436 buf = channel_get_ring_buffer(config, chan, cpu);
437 ret = lib_ring_buffer_iterator_open(buf);
438 if (ret)
439 goto error;
440 buf->iter.read_open = 1;
441 }
442 put_online_cpus();
443 } else {
444 buf = channel_get_ring_buffer(config, chan, 0);
445 ret = lib_ring_buffer_iterator_open(buf);
446 }
447 return ret;
448 error:
449 /* Error should always happen on CPU 0, hence no close is required. */
450 CHAN_WARN_ON(chan, cpu != 0);
451 put_online_cpus();
452 return ret;
453 }
454 EXPORT_SYMBOL_GPL(channel_iterator_open);
455
456 void channel_iterator_release(struct channel *chan)
457 {
458 const struct lib_ring_buffer_config *config = &chan->backend.config;
459 struct lib_ring_buffer *buf;
460 int cpu;
461
462 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
463 get_online_cpus();
464 for_each_channel_cpu(cpu, chan) {
465 buf = channel_get_ring_buffer(config, chan, cpu);
466 if (buf->iter.read_open) {
467 lib_ring_buffer_iterator_release(buf);
468 buf->iter.read_open = 0;
469 }
470 }
471 chan->iter.read_open = 0;
472 put_online_cpus();
473 } else {
474 buf = channel_get_ring_buffer(config, chan, 0);
475 lib_ring_buffer_iterator_release(buf);
476 }
477 }
478 EXPORT_SYMBOL_GPL(channel_iterator_release);
479
480 void lib_ring_buffer_iterator_reset(struct lib_ring_buffer *buf)
481 {
482 struct channel *chan = buf->backend.chan;
483
484 if (buf->iter.state != ITER_GET_SUBBUF)
485 lib_ring_buffer_put_next_subbuf(buf);
486 buf->iter.state = ITER_GET_SUBBUF;
487 /* Remove from heap (if present). */
488 if (lttng_heap_cherrypick(&chan->iter.heap, buf))
489 list_add(&buf->iter.empty_node, &chan->iter.empty_head);
490 buf->iter.timestamp = 0;
491 buf->iter.header_len = 0;
492 buf->iter.payload_len = 0;
493 buf->iter.consumed = 0;
494 buf->iter.read_offset = 0;
495 buf->iter.data_size = 0;
496 /* Don't reset allocated and read_open */
497 }
498
499 void channel_iterator_reset(struct channel *chan)
500 {
501 const struct lib_ring_buffer_config *config = &chan->backend.config;
502 struct lib_ring_buffer *buf;
503 int cpu;
504
505 /* Empty heap, put into empty_head */
506 while ((buf = lttng_heap_remove(&chan->iter.heap)) != NULL)
507 list_add(&buf->iter.empty_node, &chan->iter.empty_head);
508
509 for_each_channel_cpu(cpu, chan) {
510 buf = channel_get_ring_buffer(config, chan, cpu);
511 lib_ring_buffer_iterator_reset(buf);
512 }
513 /* Don't reset read_open */
514 chan->iter.last_qs = 0;
515 chan->iter.last_timestamp = 0;
516 chan->iter.last_cpu = 0;
517 chan->iter.len_left = 0;
518 }
519
520 /*
521 * Ring buffer payload extraction read() implementation.
522 */
523 static
524 ssize_t channel_ring_buffer_file_read(struct file *filp,
525 char __user *user_buf,
526 size_t count,
527 loff_t *ppos,
528 struct channel *chan,
529 struct lib_ring_buffer *buf,
530 int fusionmerge)
531 {
532 const struct lib_ring_buffer_config *config = &chan->backend.config;
533 size_t read_count = 0, read_offset;
534 ssize_t len;
535
536 might_sleep();
537 if (!access_ok(user_buf, count))
538 return -EFAULT;
539
540 /* Finish copy of previous record */
541 if (*ppos != 0) {
542 if (read_count < count) {
543 len = chan->iter.len_left;
544 read_offset = *ppos;
545 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU
546 && fusionmerge)
547 buf = lttng_heap_maximum(&chan->iter.heap);
548 CHAN_WARN_ON(chan, !buf);
549 goto skip_get_next;
550 }
551 }
552
553 while (read_count < count) {
554 size_t copy_len, space_left;
555
556 if (fusionmerge)
557 len = channel_get_next_record(chan, &buf);
558 else
559 len = lib_ring_buffer_get_next_record(chan, buf);
560 len_test:
561 if (len < 0) {
562 /*
563 * Check if buffer is finalized (end of file).
564 */
565 if (len == -ENODATA) {
566 /* A 0 read_count will tell about end of file */
567 goto nodata;
568 }
569 if (filp->f_flags & O_NONBLOCK) {
570 if (!read_count)
571 read_count = -EAGAIN;
572 goto nodata;
573 } else {
574 int error;
575
576 /*
577 * No data available at the moment, return what
578 * we got.
579 */
580 if (read_count)
581 goto nodata;
582
583 /*
584 * Wait for returned len to be >= 0 or -ENODATA.
585 */
586 if (fusionmerge)
587 error = wait_event_interruptible(
588 chan->read_wait,
589 ((len = channel_get_next_record(chan,
590 &buf)), len != -EAGAIN));
591 else
592 error = wait_event_interruptible(
593 buf->read_wait,
594 ((len = lib_ring_buffer_get_next_record(
595 chan, buf)), len != -EAGAIN));
596 CHAN_WARN_ON(chan, len == -EBUSY);
597 if (error) {
598 read_count = error;
599 goto nodata;
600 }
601 CHAN_WARN_ON(chan, len < 0 && len != -ENODATA);
602 goto len_test;
603 }
604 }
605 read_offset = buf->iter.read_offset;
606 skip_get_next:
607 space_left = count - read_count;
608 if (len <= space_left) {
609 copy_len = len;
610 chan->iter.len_left = 0;
611 *ppos = 0;
612 } else {
613 copy_len = space_left;
614 chan->iter.len_left = len - copy_len;
615 *ppos = read_offset + copy_len;
616 }
617 if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset,
618 &user_buf[read_count],
619 copy_len)) {
620 /*
621 * Leave the len_left and ppos values at their current
622 * state, as we currently have a valid event to read.
623 */
624 return -EFAULT;
625 }
626 read_count += copy_len;
627 };
628 return read_count;
629
630 nodata:
631 *ppos = 0;
632 chan->iter.len_left = 0;
633 return read_count;
634 }
635
636 /**
637 * lib_ring_buffer_file_read - Read buffer record payload.
638 * @filp: file structure pointer.
639 * @buffer: user buffer to read data into.
640 * @count: number of bytes to read.
641 * @ppos: file read position.
642 *
643 * Returns a negative value on error, or the number of bytes read on success.
644 * ppos is used to save the position _within the current record_ between calls
645 * to read().
646 */
647 static
648 ssize_t lib_ring_buffer_file_read(struct file *filp,
649 char __user *user_buf,
650 size_t count,
651 loff_t *ppos)
652 {
653 struct inode *inode = filp->f_path.dentry->d_inode;
654 struct lib_ring_buffer *buf = inode->i_private;
655 struct channel *chan = buf->backend.chan;
656
657 return channel_ring_buffer_file_read(filp, user_buf, count, ppos,
658 chan, buf, 0);
659 }
660
661 /**
662 * channel_file_read - Read channel record payload.
663 * @filp: file structure pointer.
664 * @buffer: user buffer to read data into.
665 * @count: number of bytes to read.
666 * @ppos: file read position.
667 *
668 * Returns a negative value on error, or the number of bytes read on success.
669 * ppos is used to save the position _within the current record_ between calls
670 * to read().
671 */
672 static
673 ssize_t channel_file_read(struct file *filp,
674 char __user *user_buf,
675 size_t count,
676 loff_t *ppos)
677 {
678 struct inode *inode = filp->f_path.dentry->d_inode;
679 struct channel *chan = inode->i_private;
680 const struct lib_ring_buffer_config *config = &chan->backend.config;
681
682 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
683 return channel_ring_buffer_file_read(filp, user_buf, count,
684 ppos, chan, NULL, 1);
685 else {
686 struct lib_ring_buffer *buf =
687 channel_get_ring_buffer(config, chan, 0);
688 return channel_ring_buffer_file_read(filp, user_buf, count,
689 ppos, chan, buf, 0);
690 }
691 }
692
693 static
694 int lib_ring_buffer_file_open(struct inode *inode, struct file *file)
695 {
696 struct lib_ring_buffer *buf = inode->i_private;
697 int ret;
698
699 ret = lib_ring_buffer_iterator_open(buf);
700 if (ret)
701 return ret;
702
703 file->private_data = buf;
704 ret = nonseekable_open(inode, file);
705 if (ret)
706 goto release_iter;
707 return 0;
708
709 release_iter:
710 lib_ring_buffer_iterator_release(buf);
711 return ret;
712 }
713
714 static
715 int lib_ring_buffer_file_release(struct inode *inode, struct file *file)
716 {
717 struct lib_ring_buffer *buf = inode->i_private;
718
719 lib_ring_buffer_iterator_release(buf);
720 return 0;
721 }
722
723 static
724 int channel_file_open(struct inode *inode, struct file *file)
725 {
726 struct channel *chan = inode->i_private;
727 int ret;
728
729 ret = channel_iterator_open(chan);
730 if (ret)
731 return ret;
732
733 file->private_data = chan;
734 ret = nonseekable_open(inode, file);
735 if (ret)
736 goto release_iter;
737 return 0;
738
739 release_iter:
740 channel_iterator_release(chan);
741 return ret;
742 }
743
744 static
745 int channel_file_release(struct inode *inode, struct file *file)
746 {
747 struct channel *chan = inode->i_private;
748
749 channel_iterator_release(chan);
750 return 0;
751 }
752
753 const struct file_operations channel_payload_file_operations = {
754 .owner = THIS_MODULE,
755 .open = channel_file_open,
756 .release = channel_file_release,
757 .read = channel_file_read,
758 .llseek = vfs_lib_ring_buffer_no_llseek,
759 };
760 EXPORT_SYMBOL_GPL(channel_payload_file_operations);
761
762 const struct file_operations lib_ring_buffer_payload_file_operations = {
763 .owner = THIS_MODULE,
764 .open = lib_ring_buffer_file_open,
765 .release = lib_ring_buffer_file_release,
766 .read = lib_ring_buffer_file_read,
767 .llseek = vfs_lib_ring_buffer_no_llseek,
768 };
769 EXPORT_SYMBOL_GPL(lib_ring_buffer_payload_file_operations);
This page took 0.057662 seconds and 4 git commands to generate.