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