uml: syle fixes in arch/um/os-Linux
[wrt350n-kernel.git] / kernel / relay.c
blob7c0373322f18892eea75e3773714554f59f46c1e
1 /*
2 * Public API and common code for kernel->userspace relay file support.
4 * See Documentation/filesystems/relay.txt for an overview.
6 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
9 * Moved to kernel/relay.c by Paul Mundt, 2006.
10 * November 2006 - CPU hotplug support by Mathieu Desnoyers
11 * (mathieu.desnoyers@polymtl.ca)
13 * This file is released under the GPL.
15 #include <linux/errno.h>
16 #include <linux/stddef.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/string.h>
20 #include <linux/relay.h>
21 #include <linux/vmalloc.h>
22 #include <linux/mm.h>
23 #include <linux/cpu.h>
24 #include <linux/splice.h>
26 /* list of open channels, for cpu hotplug */
27 static DEFINE_MUTEX(relay_channels_mutex);
28 static LIST_HEAD(relay_channels);
31 * close() vm_op implementation for relay file mapping.
33 static void relay_file_mmap_close(struct vm_area_struct *vma)
35 struct rchan_buf *buf = vma->vm_private_data;
36 buf->chan->cb->buf_unmapped(buf, vma->vm_file);
40 * nopage() vm_op implementation for relay file mapping.
42 static struct page *relay_buf_nopage(struct vm_area_struct *vma,
43 unsigned long address,
44 int *type)
46 struct page *page;
47 struct rchan_buf *buf = vma->vm_private_data;
48 unsigned long offset = address - vma->vm_start;
50 if (address > vma->vm_end)
51 return NOPAGE_SIGBUS; /* Disallow mremap */
52 if (!buf)
53 return NOPAGE_OOM;
55 page = vmalloc_to_page(buf->start + offset);
56 if (!page)
57 return NOPAGE_OOM;
58 get_page(page);
60 if (type)
61 *type = VM_FAULT_MINOR;
63 return page;
67 * vm_ops for relay file mappings.
69 static struct vm_operations_struct relay_file_mmap_ops = {
70 .nopage = relay_buf_nopage,
71 .close = relay_file_mmap_close,
74 /**
75 * relay_mmap_buf: - mmap channel buffer to process address space
76 * @buf: relay channel buffer
77 * @vma: vm_area_struct describing memory to be mapped
79 * Returns 0 if ok, negative on error
81 * Caller should already have grabbed mmap_sem.
83 static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
85 unsigned long length = vma->vm_end - vma->vm_start;
86 struct file *filp = vma->vm_file;
88 if (!buf)
89 return -EBADF;
91 if (length != (unsigned long)buf->chan->alloc_size)
92 return -EINVAL;
94 vma->vm_ops = &relay_file_mmap_ops;
95 vma->vm_flags |= VM_DONTEXPAND;
96 vma->vm_private_data = buf;
97 buf->chan->cb->buf_mapped(buf, filp);
99 return 0;
103 * relay_alloc_buf - allocate a channel buffer
104 * @buf: the buffer struct
105 * @size: total size of the buffer
107 * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
108 * passed in size will get page aligned, if it isn't already.
110 static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
112 void *mem;
113 unsigned int i, j, n_pages;
115 *size = PAGE_ALIGN(*size);
116 n_pages = *size >> PAGE_SHIFT;
118 buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
119 if (!buf->page_array)
120 return NULL;
122 for (i = 0; i < n_pages; i++) {
123 buf->page_array[i] = alloc_page(GFP_KERNEL);
124 if (unlikely(!buf->page_array[i]))
125 goto depopulate;
126 set_page_private(buf->page_array[i], (unsigned long)buf);
128 mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
129 if (!mem)
130 goto depopulate;
132 memset(mem, 0, *size);
133 buf->page_count = n_pages;
134 return mem;
136 depopulate:
137 for (j = 0; j < i; j++)
138 __free_page(buf->page_array[j]);
139 kfree(buf->page_array);
140 return NULL;
144 * relay_create_buf - allocate and initialize a channel buffer
145 * @chan: the relay channel
147 * Returns channel buffer if successful, %NULL otherwise.
149 static struct rchan_buf *relay_create_buf(struct rchan *chan)
151 struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
152 if (!buf)
153 return NULL;
155 buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
156 if (!buf->padding)
157 goto free_buf;
159 buf->start = relay_alloc_buf(buf, &chan->alloc_size);
160 if (!buf->start)
161 goto free_buf;
163 buf->chan = chan;
164 kref_get(&buf->chan->kref);
165 return buf;
167 free_buf:
168 kfree(buf->padding);
169 kfree(buf);
170 return NULL;
174 * relay_destroy_channel - free the channel struct
175 * @kref: target kernel reference that contains the relay channel
177 * Should only be called from kref_put().
179 static void relay_destroy_channel(struct kref *kref)
181 struct rchan *chan = container_of(kref, struct rchan, kref);
182 kfree(chan);
186 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
187 * @buf: the buffer struct
189 static void relay_destroy_buf(struct rchan_buf *buf)
191 struct rchan *chan = buf->chan;
192 unsigned int i;
194 if (likely(buf->start)) {
195 vunmap(buf->start);
196 for (i = 0; i < buf->page_count; i++)
197 __free_page(buf->page_array[i]);
198 kfree(buf->page_array);
200 chan->buf[buf->cpu] = NULL;
201 kfree(buf->padding);
202 kfree(buf);
203 kref_put(&chan->kref, relay_destroy_channel);
207 * relay_remove_buf - remove a channel buffer
208 * @kref: target kernel reference that contains the relay buffer
210 * Removes the file from the fileystem, which also frees the
211 * rchan_buf_struct and the channel buffer. Should only be called from
212 * kref_put().
214 static void relay_remove_buf(struct kref *kref)
216 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
217 buf->chan->cb->remove_buf_file(buf->dentry);
218 relay_destroy_buf(buf);
222 * relay_buf_empty - boolean, is the channel buffer empty?
223 * @buf: channel buffer
225 * Returns 1 if the buffer is empty, 0 otherwise.
227 static int relay_buf_empty(struct rchan_buf *buf)
229 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
233 * relay_buf_full - boolean, is the channel buffer full?
234 * @buf: channel buffer
236 * Returns 1 if the buffer is full, 0 otherwise.
238 int relay_buf_full(struct rchan_buf *buf)
240 size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
241 return (ready >= buf->chan->n_subbufs) ? 1 : 0;
243 EXPORT_SYMBOL_GPL(relay_buf_full);
246 * High-level relay kernel API and associated functions.
250 * rchan_callback implementations defining default channel behavior. Used
251 * in place of corresponding NULL values in client callback struct.
255 * subbuf_start() default callback. Does nothing.
257 static int subbuf_start_default_callback (struct rchan_buf *buf,
258 void *subbuf,
259 void *prev_subbuf,
260 size_t prev_padding)
262 if (relay_buf_full(buf))
263 return 0;
265 return 1;
269 * buf_mapped() default callback. Does nothing.
271 static void buf_mapped_default_callback(struct rchan_buf *buf,
272 struct file *filp)
277 * buf_unmapped() default callback. Does nothing.
279 static void buf_unmapped_default_callback(struct rchan_buf *buf,
280 struct file *filp)
285 * create_buf_file_create() default callback. Does nothing.
287 static struct dentry *create_buf_file_default_callback(const char *filename,
288 struct dentry *parent,
289 int mode,
290 struct rchan_buf *buf,
291 int *is_global)
293 return NULL;
297 * remove_buf_file() default callback. Does nothing.
299 static int remove_buf_file_default_callback(struct dentry *dentry)
301 return -EINVAL;
304 /* relay channel default callbacks */
305 static struct rchan_callbacks default_channel_callbacks = {
306 .subbuf_start = subbuf_start_default_callback,
307 .buf_mapped = buf_mapped_default_callback,
308 .buf_unmapped = buf_unmapped_default_callback,
309 .create_buf_file = create_buf_file_default_callback,
310 .remove_buf_file = remove_buf_file_default_callback,
314 * wakeup_readers - wake up readers waiting on a channel
315 * @data: contains the channel buffer
317 * This is the timer function used to defer reader waking.
319 static void wakeup_readers(unsigned long data)
321 struct rchan_buf *buf = (struct rchan_buf *)data;
322 wake_up_interruptible(&buf->read_wait);
326 * __relay_reset - reset a channel buffer
327 * @buf: the channel buffer
328 * @init: 1 if this is a first-time initialization
330 * See relay_reset() for description of effect.
332 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
334 size_t i;
336 if (init) {
337 init_waitqueue_head(&buf->read_wait);
338 kref_init(&buf->kref);
339 setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
340 } else
341 del_timer_sync(&buf->timer);
343 buf->subbufs_produced = 0;
344 buf->subbufs_consumed = 0;
345 buf->bytes_consumed = 0;
346 buf->finalized = 0;
347 buf->data = buf->start;
348 buf->offset = 0;
350 for (i = 0; i < buf->chan->n_subbufs; i++)
351 buf->padding[i] = 0;
353 buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
357 * relay_reset - reset the channel
358 * @chan: the channel
360 * This has the effect of erasing all data from all channel buffers
361 * and restarting the channel in its initial state. The buffers
362 * are not freed, so any mappings are still in effect.
364 * NOTE. Care should be taken that the channel isn't actually
365 * being used by anything when this call is made.
367 void relay_reset(struct rchan *chan)
369 unsigned int i;
371 if (!chan)
372 return;
374 if (chan->is_global && chan->buf[0]) {
375 __relay_reset(chan->buf[0], 0);
376 return;
379 mutex_lock(&relay_channels_mutex);
380 for_each_online_cpu(i)
381 if (chan->buf[i])
382 __relay_reset(chan->buf[i], 0);
383 mutex_unlock(&relay_channels_mutex);
385 EXPORT_SYMBOL_GPL(relay_reset);
388 * relay_open_buf - create a new relay channel buffer
390 * used by relay_open() and CPU hotplug.
392 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
394 struct rchan_buf *buf = NULL;
395 struct dentry *dentry;
396 char *tmpname;
398 if (chan->is_global)
399 return chan->buf[0];
401 tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
402 if (!tmpname)
403 goto end;
404 snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
406 buf = relay_create_buf(chan);
407 if (!buf)
408 goto free_name;
410 buf->cpu = cpu;
411 __relay_reset(buf, 1);
413 /* Create file in fs */
414 dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
415 buf, &chan->is_global);
416 if (!dentry)
417 goto free_buf;
419 buf->dentry = dentry;
421 if(chan->is_global) {
422 chan->buf[0] = buf;
423 buf->cpu = 0;
426 goto free_name;
428 free_buf:
429 relay_destroy_buf(buf);
430 buf = NULL;
431 free_name:
432 kfree(tmpname);
433 end:
434 return buf;
438 * relay_close_buf - close a channel buffer
439 * @buf: channel buffer
441 * Marks the buffer finalized and restores the default callbacks.
442 * The channel buffer and channel buffer data structure are then freed
443 * automatically when the last reference is given up.
445 static void relay_close_buf(struct rchan_buf *buf)
447 buf->finalized = 1;
448 del_timer_sync(&buf->timer);
449 kref_put(&buf->kref, relay_remove_buf);
452 static void setup_callbacks(struct rchan *chan,
453 struct rchan_callbacks *cb)
455 if (!cb) {
456 chan->cb = &default_channel_callbacks;
457 return;
460 if (!cb->subbuf_start)
461 cb->subbuf_start = subbuf_start_default_callback;
462 if (!cb->buf_mapped)
463 cb->buf_mapped = buf_mapped_default_callback;
464 if (!cb->buf_unmapped)
465 cb->buf_unmapped = buf_unmapped_default_callback;
466 if (!cb->create_buf_file)
467 cb->create_buf_file = create_buf_file_default_callback;
468 if (!cb->remove_buf_file)
469 cb->remove_buf_file = remove_buf_file_default_callback;
470 chan->cb = cb;
474 * relay_hotcpu_callback - CPU hotplug callback
475 * @nb: notifier block
476 * @action: hotplug action to take
477 * @hcpu: CPU number
479 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
481 static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
482 unsigned long action,
483 void *hcpu)
485 unsigned int hotcpu = (unsigned long)hcpu;
486 struct rchan *chan;
488 switch(action) {
489 case CPU_UP_PREPARE:
490 case CPU_UP_PREPARE_FROZEN:
491 mutex_lock(&relay_channels_mutex);
492 list_for_each_entry(chan, &relay_channels, list) {
493 if (chan->buf[hotcpu])
494 continue;
495 chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
496 if(!chan->buf[hotcpu]) {
497 printk(KERN_ERR
498 "relay_hotcpu_callback: cpu %d buffer "
499 "creation failed\n", hotcpu);
500 mutex_unlock(&relay_channels_mutex);
501 return NOTIFY_BAD;
504 mutex_unlock(&relay_channels_mutex);
505 break;
506 case CPU_DEAD:
507 case CPU_DEAD_FROZEN:
508 /* No need to flush the cpu : will be flushed upon
509 * final relay_flush() call. */
510 break;
512 return NOTIFY_OK;
516 * relay_open - create a new relay channel
517 * @base_filename: base name of files to create
518 * @parent: dentry of parent directory, %NULL for root directory
519 * @subbuf_size: size of sub-buffers
520 * @n_subbufs: number of sub-buffers
521 * @cb: client callback functions
522 * @private_data: user-defined data
524 * Returns channel pointer if successful, %NULL otherwise.
526 * Creates a channel buffer for each cpu using the sizes and
527 * attributes specified. The created channel buffer files
528 * will be named base_filename0...base_filenameN-1. File
529 * permissions will be %S_IRUSR.
531 struct rchan *relay_open(const char *base_filename,
532 struct dentry *parent,
533 size_t subbuf_size,
534 size_t n_subbufs,
535 struct rchan_callbacks *cb,
536 void *private_data)
538 unsigned int i;
539 struct rchan *chan;
540 if (!base_filename)
541 return NULL;
543 if (!(subbuf_size && n_subbufs))
544 return NULL;
546 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
547 if (!chan)
548 return NULL;
550 chan->version = RELAYFS_CHANNEL_VERSION;
551 chan->n_subbufs = n_subbufs;
552 chan->subbuf_size = subbuf_size;
553 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
554 chan->parent = parent;
555 chan->private_data = private_data;
556 strlcpy(chan->base_filename, base_filename, NAME_MAX);
557 setup_callbacks(chan, cb);
558 kref_init(&chan->kref);
560 mutex_lock(&relay_channels_mutex);
561 for_each_online_cpu(i) {
562 chan->buf[i] = relay_open_buf(chan, i);
563 if (!chan->buf[i])
564 goto free_bufs;
566 list_add(&chan->list, &relay_channels);
567 mutex_unlock(&relay_channels_mutex);
569 return chan;
571 free_bufs:
572 for_each_online_cpu(i) {
573 if (!chan->buf[i])
574 break;
575 relay_close_buf(chan->buf[i]);
578 kref_put(&chan->kref, relay_destroy_channel);
579 mutex_unlock(&relay_channels_mutex);
580 return NULL;
582 EXPORT_SYMBOL_GPL(relay_open);
585 * relay_switch_subbuf - switch to a new sub-buffer
586 * @buf: channel buffer
587 * @length: size of current event
589 * Returns either the length passed in or 0 if full.
591 * Performs sub-buffer-switch tasks such as invoking callbacks,
592 * updating padding counts, waking up readers, etc.
594 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
596 void *old, *new;
597 size_t old_subbuf, new_subbuf;
599 if (unlikely(length > buf->chan->subbuf_size))
600 goto toobig;
602 if (buf->offset != buf->chan->subbuf_size + 1) {
603 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
604 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
605 buf->padding[old_subbuf] = buf->prev_padding;
606 buf->subbufs_produced++;
607 buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
608 buf->padding[old_subbuf];
609 smp_mb();
610 if (waitqueue_active(&buf->read_wait))
612 * Calling wake_up_interruptible() from here
613 * will deadlock if we happen to be logging
614 * from the scheduler (trying to re-grab
615 * rq->lock), so defer it.
617 __mod_timer(&buf->timer, jiffies + 1);
620 old = buf->data;
621 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
622 new = buf->start + new_subbuf * buf->chan->subbuf_size;
623 buf->offset = 0;
624 if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
625 buf->offset = buf->chan->subbuf_size + 1;
626 return 0;
628 buf->data = new;
629 buf->padding[new_subbuf] = 0;
631 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
632 goto toobig;
634 return length;
636 toobig:
637 buf->chan->last_toobig = length;
638 return 0;
640 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
643 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
644 * @chan: the channel
645 * @cpu: the cpu associated with the channel buffer to update
646 * @subbufs_consumed: number of sub-buffers to add to current buf's count
648 * Adds to the channel buffer's consumed sub-buffer count.
649 * subbufs_consumed should be the number of sub-buffers newly consumed,
650 * not the total consumed.
652 * NOTE. Kernel clients don't need to call this function if the channel
653 * mode is 'overwrite'.
655 void relay_subbufs_consumed(struct rchan *chan,
656 unsigned int cpu,
657 size_t subbufs_consumed)
659 struct rchan_buf *buf;
661 if (!chan)
662 return;
664 if (cpu >= NR_CPUS || !chan->buf[cpu])
665 return;
667 buf = chan->buf[cpu];
668 buf->subbufs_consumed += subbufs_consumed;
669 if (buf->subbufs_consumed > buf->subbufs_produced)
670 buf->subbufs_consumed = buf->subbufs_produced;
672 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
675 * relay_close - close the channel
676 * @chan: the channel
678 * Closes all channel buffers and frees the channel.
680 void relay_close(struct rchan *chan)
682 unsigned int i;
684 if (!chan)
685 return;
687 mutex_lock(&relay_channels_mutex);
688 if (chan->is_global && chan->buf[0])
689 relay_close_buf(chan->buf[0]);
690 else
691 for_each_possible_cpu(i)
692 if (chan->buf[i])
693 relay_close_buf(chan->buf[i]);
695 if (chan->last_toobig)
696 printk(KERN_WARNING "relay: one or more items not logged "
697 "[item size (%Zd) > sub-buffer size (%Zd)]\n",
698 chan->last_toobig, chan->subbuf_size);
700 list_del(&chan->list);
701 kref_put(&chan->kref, relay_destroy_channel);
702 mutex_unlock(&relay_channels_mutex);
704 EXPORT_SYMBOL_GPL(relay_close);
707 * relay_flush - close the channel
708 * @chan: the channel
710 * Flushes all channel buffers, i.e. forces buffer switch.
712 void relay_flush(struct rchan *chan)
714 unsigned int i;
716 if (!chan)
717 return;
719 if (chan->is_global && chan->buf[0]) {
720 relay_switch_subbuf(chan->buf[0], 0);
721 return;
724 mutex_lock(&relay_channels_mutex);
725 for_each_possible_cpu(i)
726 if (chan->buf[i])
727 relay_switch_subbuf(chan->buf[i], 0);
728 mutex_unlock(&relay_channels_mutex);
730 EXPORT_SYMBOL_GPL(relay_flush);
733 * relay_file_open - open file op for relay files
734 * @inode: the inode
735 * @filp: the file
737 * Increments the channel buffer refcount.
739 static int relay_file_open(struct inode *inode, struct file *filp)
741 struct rchan_buf *buf = inode->i_private;
742 kref_get(&buf->kref);
743 filp->private_data = buf;
745 return 0;
749 * relay_file_mmap - mmap file op for relay files
750 * @filp: the file
751 * @vma: the vma describing what to map
753 * Calls upon relay_mmap_buf() to map the file into user space.
755 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
757 struct rchan_buf *buf = filp->private_data;
758 return relay_mmap_buf(buf, vma);
762 * relay_file_poll - poll file op for relay files
763 * @filp: the file
764 * @wait: poll table
766 * Poll implemention.
768 static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
770 unsigned int mask = 0;
771 struct rchan_buf *buf = filp->private_data;
773 if (buf->finalized)
774 return POLLERR;
776 if (filp->f_mode & FMODE_READ) {
777 poll_wait(filp, &buf->read_wait, wait);
778 if (!relay_buf_empty(buf))
779 mask |= POLLIN | POLLRDNORM;
782 return mask;
786 * relay_file_release - release file op for relay files
787 * @inode: the inode
788 * @filp: the file
790 * Decrements the channel refcount, as the filesystem is
791 * no longer using it.
793 static int relay_file_release(struct inode *inode, struct file *filp)
795 struct rchan_buf *buf = filp->private_data;
796 kref_put(&buf->kref, relay_remove_buf);
798 return 0;
802 * relay_file_read_consume - update the consumed count for the buffer
804 static void relay_file_read_consume(struct rchan_buf *buf,
805 size_t read_pos,
806 size_t bytes_consumed)
808 size_t subbuf_size = buf->chan->subbuf_size;
809 size_t n_subbufs = buf->chan->n_subbufs;
810 size_t read_subbuf;
812 if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
813 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
814 buf->bytes_consumed = 0;
817 buf->bytes_consumed += bytes_consumed;
818 if (!read_pos)
819 read_subbuf = buf->subbufs_consumed % n_subbufs;
820 else
821 read_subbuf = read_pos / buf->chan->subbuf_size;
822 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
823 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
824 (buf->offset == subbuf_size))
825 return;
826 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
827 buf->bytes_consumed = 0;
832 * relay_file_read_avail - boolean, are there unconsumed bytes available?
834 static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
836 size_t subbuf_size = buf->chan->subbuf_size;
837 size_t n_subbufs = buf->chan->n_subbufs;
838 size_t produced = buf->subbufs_produced;
839 size_t consumed = buf->subbufs_consumed;
841 relay_file_read_consume(buf, read_pos, 0);
843 if (unlikely(buf->offset > subbuf_size)) {
844 if (produced == consumed)
845 return 0;
846 return 1;
849 if (unlikely(produced - consumed >= n_subbufs)) {
850 consumed = produced - n_subbufs + 1;
851 buf->subbufs_consumed = consumed;
852 buf->bytes_consumed = 0;
855 produced = (produced % n_subbufs) * subbuf_size + buf->offset;
856 consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
858 if (consumed > produced)
859 produced += n_subbufs * subbuf_size;
861 if (consumed == produced)
862 return 0;
864 return 1;
868 * relay_file_read_subbuf_avail - return bytes available in sub-buffer
869 * @read_pos: file read position
870 * @buf: relay channel buffer
872 static size_t relay_file_read_subbuf_avail(size_t read_pos,
873 struct rchan_buf *buf)
875 size_t padding, avail = 0;
876 size_t read_subbuf, read_offset, write_subbuf, write_offset;
877 size_t subbuf_size = buf->chan->subbuf_size;
879 write_subbuf = (buf->data - buf->start) / subbuf_size;
880 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
881 read_subbuf = read_pos / subbuf_size;
882 read_offset = read_pos % subbuf_size;
883 padding = buf->padding[read_subbuf];
885 if (read_subbuf == write_subbuf) {
886 if (read_offset + padding < write_offset)
887 avail = write_offset - (read_offset + padding);
888 } else
889 avail = (subbuf_size - padding) - read_offset;
891 return avail;
895 * relay_file_read_start_pos - find the first available byte to read
896 * @read_pos: file read position
897 * @buf: relay channel buffer
899 * If the @read_pos is in the middle of padding, return the
900 * position of the first actually available byte, otherwise
901 * return the original value.
903 static size_t relay_file_read_start_pos(size_t read_pos,
904 struct rchan_buf *buf)
906 size_t read_subbuf, padding, padding_start, padding_end;
907 size_t subbuf_size = buf->chan->subbuf_size;
908 size_t n_subbufs = buf->chan->n_subbufs;
909 size_t consumed = buf->subbufs_consumed % n_subbufs;
911 if (!read_pos)
912 read_pos = consumed * subbuf_size + buf->bytes_consumed;
913 read_subbuf = read_pos / subbuf_size;
914 padding = buf->padding[read_subbuf];
915 padding_start = (read_subbuf + 1) * subbuf_size - padding;
916 padding_end = (read_subbuf + 1) * subbuf_size;
917 if (read_pos >= padding_start && read_pos < padding_end) {
918 read_subbuf = (read_subbuf + 1) % n_subbufs;
919 read_pos = read_subbuf * subbuf_size;
922 return read_pos;
926 * relay_file_read_end_pos - return the new read position
927 * @read_pos: file read position
928 * @buf: relay channel buffer
929 * @count: number of bytes to be read
931 static size_t relay_file_read_end_pos(struct rchan_buf *buf,
932 size_t read_pos,
933 size_t count)
935 size_t read_subbuf, padding, end_pos;
936 size_t subbuf_size = buf->chan->subbuf_size;
937 size_t n_subbufs = buf->chan->n_subbufs;
939 read_subbuf = read_pos / subbuf_size;
940 padding = buf->padding[read_subbuf];
941 if (read_pos % subbuf_size + count + padding == subbuf_size)
942 end_pos = (read_subbuf + 1) * subbuf_size;
943 else
944 end_pos = read_pos + count;
945 if (end_pos >= subbuf_size * n_subbufs)
946 end_pos = 0;
948 return end_pos;
952 * subbuf_read_actor - read up to one subbuf's worth of data
954 static int subbuf_read_actor(size_t read_start,
955 struct rchan_buf *buf,
956 size_t avail,
957 read_descriptor_t *desc,
958 read_actor_t actor)
960 void *from;
961 int ret = 0;
963 from = buf->start + read_start;
964 ret = avail;
965 if (copy_to_user(desc->arg.buf, from, avail)) {
966 desc->error = -EFAULT;
967 ret = 0;
969 desc->arg.data += ret;
970 desc->written += ret;
971 desc->count -= ret;
973 return ret;
976 typedef int (*subbuf_actor_t) (size_t read_start,
977 struct rchan_buf *buf,
978 size_t avail,
979 read_descriptor_t *desc,
980 read_actor_t actor);
983 * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
985 static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
986 subbuf_actor_t subbuf_actor,
987 read_actor_t actor,
988 read_descriptor_t *desc)
990 struct rchan_buf *buf = filp->private_data;
991 size_t read_start, avail;
992 int ret;
994 if (!desc->count)
995 return 0;
997 mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
998 do {
999 if (!relay_file_read_avail(buf, *ppos))
1000 break;
1002 read_start = relay_file_read_start_pos(*ppos, buf);
1003 avail = relay_file_read_subbuf_avail(read_start, buf);
1004 if (!avail)
1005 break;
1007 avail = min(desc->count, avail);
1008 ret = subbuf_actor(read_start, buf, avail, desc, actor);
1009 if (desc->error < 0)
1010 break;
1012 if (ret) {
1013 relay_file_read_consume(buf, read_start, ret);
1014 *ppos = relay_file_read_end_pos(buf, read_start, ret);
1016 } while (desc->count && ret);
1017 mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
1019 return desc->written;
1022 static ssize_t relay_file_read(struct file *filp,
1023 char __user *buffer,
1024 size_t count,
1025 loff_t *ppos)
1027 read_descriptor_t desc;
1028 desc.written = 0;
1029 desc.count = count;
1030 desc.arg.buf = buffer;
1031 desc.error = 0;
1032 return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
1033 NULL, &desc);
1036 static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
1038 rbuf->bytes_consumed += bytes_consumed;
1040 if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
1041 relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1042 rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
1046 static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1047 struct pipe_buffer *buf)
1049 struct rchan_buf *rbuf;
1051 rbuf = (struct rchan_buf *)page_private(buf->page);
1052 relay_consume_bytes(rbuf, buf->private);
1055 static struct pipe_buf_operations relay_pipe_buf_ops = {
1056 .can_merge = 0,
1057 .map = generic_pipe_buf_map,
1058 .unmap = generic_pipe_buf_unmap,
1059 .confirm = generic_pipe_buf_confirm,
1060 .release = relay_pipe_buf_release,
1061 .steal = generic_pipe_buf_steal,
1062 .get = generic_pipe_buf_get,
1066 * subbuf_splice_actor - splice up to one subbuf's worth of data
1068 static int subbuf_splice_actor(struct file *in,
1069 loff_t *ppos,
1070 struct pipe_inode_info *pipe,
1071 size_t len,
1072 unsigned int flags,
1073 int *nonpad_ret)
1075 unsigned int pidx, poff, total_len, subbuf_pages, ret;
1076 struct rchan_buf *rbuf = in->private_data;
1077 unsigned int subbuf_size = rbuf->chan->subbuf_size;
1078 uint64_t pos = (uint64_t) *ppos;
1079 uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
1080 size_t read_start = (size_t) do_div(pos, alloc_size);
1081 size_t read_subbuf = read_start / subbuf_size;
1082 size_t padding = rbuf->padding[read_subbuf];
1083 size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
1084 struct page *pages[PIPE_BUFFERS];
1085 struct partial_page partial[PIPE_BUFFERS];
1086 struct splice_pipe_desc spd = {
1087 .pages = pages,
1088 .nr_pages = 0,
1089 .partial = partial,
1090 .flags = flags,
1091 .ops = &relay_pipe_buf_ops,
1094 if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1095 return 0;
1098 * Adjust read len, if longer than what is available
1100 if (len > (subbuf_size - read_start % subbuf_size))
1101 len = subbuf_size - read_start % subbuf_size;
1103 subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1104 pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1105 poff = read_start & ~PAGE_MASK;
1107 for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) {
1108 unsigned int this_len, this_end, private;
1109 unsigned int cur_pos = read_start + total_len;
1111 if (!len)
1112 break;
1114 this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
1115 private = this_len;
1117 spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
1118 spd.partial[spd.nr_pages].offset = poff;
1120 this_end = cur_pos + this_len;
1121 if (this_end >= nonpad_end) {
1122 this_len = nonpad_end - cur_pos;
1123 private = this_len + padding;
1125 spd.partial[spd.nr_pages].len = this_len;
1126 spd.partial[spd.nr_pages].private = private;
1128 len -= this_len;
1129 total_len += this_len;
1130 poff = 0;
1131 pidx = (pidx + 1) % subbuf_pages;
1133 if (this_end >= nonpad_end) {
1134 spd.nr_pages++;
1135 break;
1139 if (!spd.nr_pages)
1140 return 0;
1142 ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
1143 if (ret < 0 || ret < total_len)
1144 return ret;
1146 if (read_start + ret == nonpad_end)
1147 ret += padding;
1149 return ret;
1152 static ssize_t relay_file_splice_read(struct file *in,
1153 loff_t *ppos,
1154 struct pipe_inode_info *pipe,
1155 size_t len,
1156 unsigned int flags)
1158 ssize_t spliced;
1159 int ret;
1160 int nonpad_ret = 0;
1162 ret = 0;
1163 spliced = 0;
1165 while (len) {
1166 ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1167 if (ret < 0)
1168 break;
1169 else if (!ret) {
1170 if (spliced)
1171 break;
1172 if (flags & SPLICE_F_NONBLOCK) {
1173 ret = -EAGAIN;
1174 break;
1178 *ppos += ret;
1179 if (ret > len)
1180 len = 0;
1181 else
1182 len -= ret;
1183 spliced += nonpad_ret;
1184 nonpad_ret = 0;
1187 if (spliced)
1188 return spliced;
1190 return ret;
1193 const struct file_operations relay_file_operations = {
1194 .open = relay_file_open,
1195 .poll = relay_file_poll,
1196 .mmap = relay_file_mmap,
1197 .read = relay_file_read,
1198 .llseek = no_llseek,
1199 .release = relay_file_release,
1200 .splice_read = relay_file_splice_read,
1202 EXPORT_SYMBOL_GPL(relay_file_operations);
1204 static __init int relay_init(void)
1207 hotcpu_notifier(relay_hotcpu_callback, 0);
1208 return 0;
1211 module_init(relay_init);