USB: drivers/usb/storage/sddr55.c: fix uninitialized var warnings
[linux-2.6/linux-2.6-openrd.git] / kernel / relay.c
blobd080b9d161a75eee95bbf6d7aed235354bda9e9c
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 * fault() vm_op implementation for relay file mapping.
42 static int relay_buf_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
44 struct page *page;
45 struct rchan_buf *buf = vma->vm_private_data;
46 pgoff_t pgoff = vmf->pgoff;
48 if (!buf)
49 return VM_FAULT_OOM;
51 page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
52 if (!page)
53 return VM_FAULT_SIGBUS;
54 get_page(page);
55 vmf->page = page;
57 return 0;
61 * vm_ops for relay file mappings.
63 static struct vm_operations_struct relay_file_mmap_ops = {
64 .fault = relay_buf_fault,
65 .close = relay_file_mmap_close,
68 /**
69 * relay_mmap_buf: - mmap channel buffer to process address space
70 * @buf: relay channel buffer
71 * @vma: vm_area_struct describing memory to be mapped
73 * Returns 0 if ok, negative on error
75 * Caller should already have grabbed mmap_sem.
77 static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
79 unsigned long length = vma->vm_end - vma->vm_start;
80 struct file *filp = vma->vm_file;
82 if (!buf)
83 return -EBADF;
85 if (length != (unsigned long)buf->chan->alloc_size)
86 return -EINVAL;
88 vma->vm_ops = &relay_file_mmap_ops;
89 vma->vm_flags |= VM_DONTEXPAND;
90 vma->vm_private_data = buf;
91 buf->chan->cb->buf_mapped(buf, filp);
93 return 0;
96 /**
97 * relay_alloc_buf - allocate a channel buffer
98 * @buf: the buffer struct
99 * @size: total size of the buffer
101 * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
102 * passed in size will get page aligned, if it isn't already.
104 static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
106 void *mem;
107 unsigned int i, j, n_pages;
109 *size = PAGE_ALIGN(*size);
110 n_pages = *size >> PAGE_SHIFT;
112 buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
113 if (!buf->page_array)
114 return NULL;
116 for (i = 0; i < n_pages; i++) {
117 buf->page_array[i] = alloc_page(GFP_KERNEL);
118 if (unlikely(!buf->page_array[i]))
119 goto depopulate;
120 set_page_private(buf->page_array[i], (unsigned long)buf);
122 mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
123 if (!mem)
124 goto depopulate;
126 memset(mem, 0, *size);
127 buf->page_count = n_pages;
128 return mem;
130 depopulate:
131 for (j = 0; j < i; j++)
132 __free_page(buf->page_array[j]);
133 kfree(buf->page_array);
134 return NULL;
138 * relay_create_buf - allocate and initialize a channel buffer
139 * @chan: the relay channel
141 * Returns channel buffer if successful, %NULL otherwise.
143 static struct rchan_buf *relay_create_buf(struct rchan *chan)
145 struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
146 if (!buf)
147 return NULL;
149 buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
150 if (!buf->padding)
151 goto free_buf;
153 buf->start = relay_alloc_buf(buf, &chan->alloc_size);
154 if (!buf->start)
155 goto free_buf;
157 buf->chan = chan;
158 kref_get(&buf->chan->kref);
159 return buf;
161 free_buf:
162 kfree(buf->padding);
163 kfree(buf);
164 return NULL;
168 * relay_destroy_channel - free the channel struct
169 * @kref: target kernel reference that contains the relay channel
171 * Should only be called from kref_put().
173 static void relay_destroy_channel(struct kref *kref)
175 struct rchan *chan = container_of(kref, struct rchan, kref);
176 kfree(chan);
180 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
181 * @buf: the buffer struct
183 static void relay_destroy_buf(struct rchan_buf *buf)
185 struct rchan *chan = buf->chan;
186 unsigned int i;
188 if (likely(buf->start)) {
189 vunmap(buf->start);
190 for (i = 0; i < buf->page_count; i++)
191 __free_page(buf->page_array[i]);
192 kfree(buf->page_array);
194 chan->buf[buf->cpu] = NULL;
195 kfree(buf->padding);
196 kfree(buf);
197 kref_put(&chan->kref, relay_destroy_channel);
201 * relay_remove_buf - remove a channel buffer
202 * @kref: target kernel reference that contains the relay buffer
204 * Removes the file from the fileystem, which also frees the
205 * rchan_buf_struct and the channel buffer. Should only be called from
206 * kref_put().
208 static void relay_remove_buf(struct kref *kref)
210 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
211 buf->chan->cb->remove_buf_file(buf->dentry);
212 relay_destroy_buf(buf);
216 * relay_buf_empty - boolean, is the channel buffer empty?
217 * @buf: channel buffer
219 * Returns 1 if the buffer is empty, 0 otherwise.
221 static int relay_buf_empty(struct rchan_buf *buf)
223 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
227 * relay_buf_full - boolean, is the channel buffer full?
228 * @buf: channel buffer
230 * Returns 1 if the buffer is full, 0 otherwise.
232 int relay_buf_full(struct rchan_buf *buf)
234 size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
235 return (ready >= buf->chan->n_subbufs) ? 1 : 0;
237 EXPORT_SYMBOL_GPL(relay_buf_full);
240 * High-level relay kernel API and associated functions.
244 * rchan_callback implementations defining default channel behavior. Used
245 * in place of corresponding NULL values in client callback struct.
249 * subbuf_start() default callback. Does nothing.
251 static int subbuf_start_default_callback (struct rchan_buf *buf,
252 void *subbuf,
253 void *prev_subbuf,
254 size_t prev_padding)
256 if (relay_buf_full(buf))
257 return 0;
259 return 1;
263 * buf_mapped() default callback. Does nothing.
265 static void buf_mapped_default_callback(struct rchan_buf *buf,
266 struct file *filp)
271 * buf_unmapped() default callback. Does nothing.
273 static void buf_unmapped_default_callback(struct rchan_buf *buf,
274 struct file *filp)
279 * create_buf_file_create() default callback. Does nothing.
281 static struct dentry *create_buf_file_default_callback(const char *filename,
282 struct dentry *parent,
283 int mode,
284 struct rchan_buf *buf,
285 int *is_global)
287 return NULL;
291 * remove_buf_file() default callback. Does nothing.
293 static int remove_buf_file_default_callback(struct dentry *dentry)
295 return -EINVAL;
298 /* relay channel default callbacks */
299 static struct rchan_callbacks default_channel_callbacks = {
300 .subbuf_start = subbuf_start_default_callback,
301 .buf_mapped = buf_mapped_default_callback,
302 .buf_unmapped = buf_unmapped_default_callback,
303 .create_buf_file = create_buf_file_default_callback,
304 .remove_buf_file = remove_buf_file_default_callback,
308 * wakeup_readers - wake up readers waiting on a channel
309 * @data: contains the channel buffer
311 * This is the timer function used to defer reader waking.
313 static void wakeup_readers(unsigned long data)
315 struct rchan_buf *buf = (struct rchan_buf *)data;
316 wake_up_interruptible(&buf->read_wait);
320 * __relay_reset - reset a channel buffer
321 * @buf: the channel buffer
322 * @init: 1 if this is a first-time initialization
324 * See relay_reset() for description of effect.
326 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
328 size_t i;
330 if (init) {
331 init_waitqueue_head(&buf->read_wait);
332 kref_init(&buf->kref);
333 setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
334 } else
335 del_timer_sync(&buf->timer);
337 buf->subbufs_produced = 0;
338 buf->subbufs_consumed = 0;
339 buf->bytes_consumed = 0;
340 buf->finalized = 0;
341 buf->data = buf->start;
342 buf->offset = 0;
344 for (i = 0; i < buf->chan->n_subbufs; i++)
345 buf->padding[i] = 0;
347 buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
351 * relay_reset - reset the channel
352 * @chan: the channel
354 * This has the effect of erasing all data from all channel buffers
355 * and restarting the channel in its initial state. The buffers
356 * are not freed, so any mappings are still in effect.
358 * NOTE. Care should be taken that the channel isn't actually
359 * being used by anything when this call is made.
361 void relay_reset(struct rchan *chan)
363 unsigned int i;
365 if (!chan)
366 return;
368 if (chan->is_global && chan->buf[0]) {
369 __relay_reset(chan->buf[0], 0);
370 return;
373 mutex_lock(&relay_channels_mutex);
374 for_each_online_cpu(i)
375 if (chan->buf[i])
376 __relay_reset(chan->buf[i], 0);
377 mutex_unlock(&relay_channels_mutex);
379 EXPORT_SYMBOL_GPL(relay_reset);
382 * relay_open_buf - create a new relay channel buffer
384 * used by relay_open() and CPU hotplug.
386 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
388 struct rchan_buf *buf = NULL;
389 struct dentry *dentry;
390 char *tmpname;
392 if (chan->is_global)
393 return chan->buf[0];
395 tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
396 if (!tmpname)
397 goto end;
398 snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
400 buf = relay_create_buf(chan);
401 if (!buf)
402 goto free_name;
404 buf->cpu = cpu;
405 __relay_reset(buf, 1);
407 /* Create file in fs */
408 dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
409 buf, &chan->is_global);
410 if (!dentry)
411 goto free_buf;
413 buf->dentry = dentry;
415 if(chan->is_global) {
416 chan->buf[0] = buf;
417 buf->cpu = 0;
420 goto free_name;
422 free_buf:
423 relay_destroy_buf(buf);
424 buf = NULL;
425 free_name:
426 kfree(tmpname);
427 end:
428 return buf;
432 * relay_close_buf - close a channel buffer
433 * @buf: channel buffer
435 * Marks the buffer finalized and restores the default callbacks.
436 * The channel buffer and channel buffer data structure are then freed
437 * automatically when the last reference is given up.
439 static void relay_close_buf(struct rchan_buf *buf)
441 buf->finalized = 1;
442 del_timer_sync(&buf->timer);
443 kref_put(&buf->kref, relay_remove_buf);
446 static void setup_callbacks(struct rchan *chan,
447 struct rchan_callbacks *cb)
449 if (!cb) {
450 chan->cb = &default_channel_callbacks;
451 return;
454 if (!cb->subbuf_start)
455 cb->subbuf_start = subbuf_start_default_callback;
456 if (!cb->buf_mapped)
457 cb->buf_mapped = buf_mapped_default_callback;
458 if (!cb->buf_unmapped)
459 cb->buf_unmapped = buf_unmapped_default_callback;
460 if (!cb->create_buf_file)
461 cb->create_buf_file = create_buf_file_default_callback;
462 if (!cb->remove_buf_file)
463 cb->remove_buf_file = remove_buf_file_default_callback;
464 chan->cb = cb;
468 * relay_hotcpu_callback - CPU hotplug callback
469 * @nb: notifier block
470 * @action: hotplug action to take
471 * @hcpu: CPU number
473 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
475 static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
476 unsigned long action,
477 void *hcpu)
479 unsigned int hotcpu = (unsigned long)hcpu;
480 struct rchan *chan;
482 switch(action) {
483 case CPU_UP_PREPARE:
484 case CPU_UP_PREPARE_FROZEN:
485 mutex_lock(&relay_channels_mutex);
486 list_for_each_entry(chan, &relay_channels, list) {
487 if (chan->buf[hotcpu])
488 continue;
489 chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
490 if(!chan->buf[hotcpu]) {
491 printk(KERN_ERR
492 "relay_hotcpu_callback: cpu %d buffer "
493 "creation failed\n", hotcpu);
494 mutex_unlock(&relay_channels_mutex);
495 return NOTIFY_BAD;
498 mutex_unlock(&relay_channels_mutex);
499 break;
500 case CPU_DEAD:
501 case CPU_DEAD_FROZEN:
502 /* No need to flush the cpu : will be flushed upon
503 * final relay_flush() call. */
504 break;
506 return NOTIFY_OK;
510 * relay_open - create a new relay channel
511 * @base_filename: base name of files to create
512 * @parent: dentry of parent directory, %NULL for root directory
513 * @subbuf_size: size of sub-buffers
514 * @n_subbufs: number of sub-buffers
515 * @cb: client callback functions
516 * @private_data: user-defined data
518 * Returns channel pointer if successful, %NULL otherwise.
520 * Creates a channel buffer for each cpu using the sizes and
521 * attributes specified. The created channel buffer files
522 * will be named base_filename0...base_filenameN-1. File
523 * permissions will be %S_IRUSR.
525 struct rchan *relay_open(const char *base_filename,
526 struct dentry *parent,
527 size_t subbuf_size,
528 size_t n_subbufs,
529 struct rchan_callbacks *cb,
530 void *private_data)
532 unsigned int i;
533 struct rchan *chan;
534 if (!base_filename)
535 return NULL;
537 if (!(subbuf_size && n_subbufs))
538 return NULL;
540 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
541 if (!chan)
542 return NULL;
544 chan->version = RELAYFS_CHANNEL_VERSION;
545 chan->n_subbufs = n_subbufs;
546 chan->subbuf_size = subbuf_size;
547 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
548 chan->parent = parent;
549 chan->private_data = private_data;
550 strlcpy(chan->base_filename, base_filename, NAME_MAX);
551 setup_callbacks(chan, cb);
552 kref_init(&chan->kref);
554 mutex_lock(&relay_channels_mutex);
555 for_each_online_cpu(i) {
556 chan->buf[i] = relay_open_buf(chan, i);
557 if (!chan->buf[i])
558 goto free_bufs;
560 list_add(&chan->list, &relay_channels);
561 mutex_unlock(&relay_channels_mutex);
563 return chan;
565 free_bufs:
566 for_each_online_cpu(i) {
567 if (!chan->buf[i])
568 break;
569 relay_close_buf(chan->buf[i]);
572 kref_put(&chan->kref, relay_destroy_channel);
573 mutex_unlock(&relay_channels_mutex);
574 return NULL;
576 EXPORT_SYMBOL_GPL(relay_open);
579 * relay_switch_subbuf - switch to a new sub-buffer
580 * @buf: channel buffer
581 * @length: size of current event
583 * Returns either the length passed in or 0 if full.
585 * Performs sub-buffer-switch tasks such as invoking callbacks,
586 * updating padding counts, waking up readers, etc.
588 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
590 void *old, *new;
591 size_t old_subbuf, new_subbuf;
593 if (unlikely(length > buf->chan->subbuf_size))
594 goto toobig;
596 if (buf->offset != buf->chan->subbuf_size + 1) {
597 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
598 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
599 buf->padding[old_subbuf] = buf->prev_padding;
600 buf->subbufs_produced++;
601 buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
602 buf->padding[old_subbuf];
603 smp_mb();
604 if (waitqueue_active(&buf->read_wait))
606 * Calling wake_up_interruptible() from here
607 * will deadlock if we happen to be logging
608 * from the scheduler (trying to re-grab
609 * rq->lock), so defer it.
611 __mod_timer(&buf->timer, jiffies + 1);
614 old = buf->data;
615 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
616 new = buf->start + new_subbuf * buf->chan->subbuf_size;
617 buf->offset = 0;
618 if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
619 buf->offset = buf->chan->subbuf_size + 1;
620 return 0;
622 buf->data = new;
623 buf->padding[new_subbuf] = 0;
625 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
626 goto toobig;
628 return length;
630 toobig:
631 buf->chan->last_toobig = length;
632 return 0;
634 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
637 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
638 * @chan: the channel
639 * @cpu: the cpu associated with the channel buffer to update
640 * @subbufs_consumed: number of sub-buffers to add to current buf's count
642 * Adds to the channel buffer's consumed sub-buffer count.
643 * subbufs_consumed should be the number of sub-buffers newly consumed,
644 * not the total consumed.
646 * NOTE. Kernel clients don't need to call this function if the channel
647 * mode is 'overwrite'.
649 void relay_subbufs_consumed(struct rchan *chan,
650 unsigned int cpu,
651 size_t subbufs_consumed)
653 struct rchan_buf *buf;
655 if (!chan)
656 return;
658 if (cpu >= NR_CPUS || !chan->buf[cpu])
659 return;
661 buf = chan->buf[cpu];
662 buf->subbufs_consumed += subbufs_consumed;
663 if (buf->subbufs_consumed > buf->subbufs_produced)
664 buf->subbufs_consumed = buf->subbufs_produced;
666 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
669 * relay_close - close the channel
670 * @chan: the channel
672 * Closes all channel buffers and frees the channel.
674 void relay_close(struct rchan *chan)
676 unsigned int i;
678 if (!chan)
679 return;
681 mutex_lock(&relay_channels_mutex);
682 if (chan->is_global && chan->buf[0])
683 relay_close_buf(chan->buf[0]);
684 else
685 for_each_possible_cpu(i)
686 if (chan->buf[i])
687 relay_close_buf(chan->buf[i]);
689 if (chan->last_toobig)
690 printk(KERN_WARNING "relay: one or more items not logged "
691 "[item size (%Zd) > sub-buffer size (%Zd)]\n",
692 chan->last_toobig, chan->subbuf_size);
694 list_del(&chan->list);
695 kref_put(&chan->kref, relay_destroy_channel);
696 mutex_unlock(&relay_channels_mutex);
698 EXPORT_SYMBOL_GPL(relay_close);
701 * relay_flush - close the channel
702 * @chan: the channel
704 * Flushes all channel buffers, i.e. forces buffer switch.
706 void relay_flush(struct rchan *chan)
708 unsigned int i;
710 if (!chan)
711 return;
713 if (chan->is_global && chan->buf[0]) {
714 relay_switch_subbuf(chan->buf[0], 0);
715 return;
718 mutex_lock(&relay_channels_mutex);
719 for_each_possible_cpu(i)
720 if (chan->buf[i])
721 relay_switch_subbuf(chan->buf[i], 0);
722 mutex_unlock(&relay_channels_mutex);
724 EXPORT_SYMBOL_GPL(relay_flush);
727 * relay_file_open - open file op for relay files
728 * @inode: the inode
729 * @filp: the file
731 * Increments the channel buffer refcount.
733 static int relay_file_open(struct inode *inode, struct file *filp)
735 struct rchan_buf *buf = inode->i_private;
736 kref_get(&buf->kref);
737 filp->private_data = buf;
739 return 0;
743 * relay_file_mmap - mmap file op for relay files
744 * @filp: the file
745 * @vma: the vma describing what to map
747 * Calls upon relay_mmap_buf() to map the file into user space.
749 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
751 struct rchan_buf *buf = filp->private_data;
752 return relay_mmap_buf(buf, vma);
756 * relay_file_poll - poll file op for relay files
757 * @filp: the file
758 * @wait: poll table
760 * Poll implemention.
762 static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
764 unsigned int mask = 0;
765 struct rchan_buf *buf = filp->private_data;
767 if (buf->finalized)
768 return POLLERR;
770 if (filp->f_mode & FMODE_READ) {
771 poll_wait(filp, &buf->read_wait, wait);
772 if (!relay_buf_empty(buf))
773 mask |= POLLIN | POLLRDNORM;
776 return mask;
780 * relay_file_release - release file op for relay files
781 * @inode: the inode
782 * @filp: the file
784 * Decrements the channel refcount, as the filesystem is
785 * no longer using it.
787 static int relay_file_release(struct inode *inode, struct file *filp)
789 struct rchan_buf *buf = filp->private_data;
790 kref_put(&buf->kref, relay_remove_buf);
792 return 0;
796 * relay_file_read_consume - update the consumed count for the buffer
798 static void relay_file_read_consume(struct rchan_buf *buf,
799 size_t read_pos,
800 size_t bytes_consumed)
802 size_t subbuf_size = buf->chan->subbuf_size;
803 size_t n_subbufs = buf->chan->n_subbufs;
804 size_t read_subbuf;
806 if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
807 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
808 buf->bytes_consumed = 0;
811 buf->bytes_consumed += bytes_consumed;
812 if (!read_pos)
813 read_subbuf = buf->subbufs_consumed % n_subbufs;
814 else
815 read_subbuf = read_pos / buf->chan->subbuf_size;
816 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
817 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
818 (buf->offset == subbuf_size))
819 return;
820 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
821 buf->bytes_consumed = 0;
826 * relay_file_read_avail - boolean, are there unconsumed bytes available?
828 static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
830 size_t subbuf_size = buf->chan->subbuf_size;
831 size_t n_subbufs = buf->chan->n_subbufs;
832 size_t produced = buf->subbufs_produced;
833 size_t consumed = buf->subbufs_consumed;
835 relay_file_read_consume(buf, read_pos, 0);
837 if (unlikely(buf->offset > subbuf_size)) {
838 if (produced == consumed)
839 return 0;
840 return 1;
843 if (unlikely(produced - consumed >= n_subbufs)) {
844 consumed = produced - n_subbufs + 1;
845 buf->subbufs_consumed = consumed;
846 buf->bytes_consumed = 0;
849 produced = (produced % n_subbufs) * subbuf_size + buf->offset;
850 consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
852 if (consumed > produced)
853 produced += n_subbufs * subbuf_size;
855 if (consumed == produced)
856 return 0;
858 return 1;
862 * relay_file_read_subbuf_avail - return bytes available in sub-buffer
863 * @read_pos: file read position
864 * @buf: relay channel buffer
866 static size_t relay_file_read_subbuf_avail(size_t read_pos,
867 struct rchan_buf *buf)
869 size_t padding, avail = 0;
870 size_t read_subbuf, read_offset, write_subbuf, write_offset;
871 size_t subbuf_size = buf->chan->subbuf_size;
873 write_subbuf = (buf->data - buf->start) / subbuf_size;
874 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
875 read_subbuf = read_pos / subbuf_size;
876 read_offset = read_pos % subbuf_size;
877 padding = buf->padding[read_subbuf];
879 if (read_subbuf == write_subbuf) {
880 if (read_offset + padding < write_offset)
881 avail = write_offset - (read_offset + padding);
882 } else
883 avail = (subbuf_size - padding) - read_offset;
885 return avail;
889 * relay_file_read_start_pos - find the first available byte to read
890 * @read_pos: file read position
891 * @buf: relay channel buffer
893 * If the @read_pos is in the middle of padding, return the
894 * position of the first actually available byte, otherwise
895 * return the original value.
897 static size_t relay_file_read_start_pos(size_t read_pos,
898 struct rchan_buf *buf)
900 size_t read_subbuf, padding, padding_start, padding_end;
901 size_t subbuf_size = buf->chan->subbuf_size;
902 size_t n_subbufs = buf->chan->n_subbufs;
903 size_t consumed = buf->subbufs_consumed % n_subbufs;
905 if (!read_pos)
906 read_pos = consumed * subbuf_size + buf->bytes_consumed;
907 read_subbuf = read_pos / subbuf_size;
908 padding = buf->padding[read_subbuf];
909 padding_start = (read_subbuf + 1) * subbuf_size - padding;
910 padding_end = (read_subbuf + 1) * subbuf_size;
911 if (read_pos >= padding_start && read_pos < padding_end) {
912 read_subbuf = (read_subbuf + 1) % n_subbufs;
913 read_pos = read_subbuf * subbuf_size;
916 return read_pos;
920 * relay_file_read_end_pos - return the new read position
921 * @read_pos: file read position
922 * @buf: relay channel buffer
923 * @count: number of bytes to be read
925 static size_t relay_file_read_end_pos(struct rchan_buf *buf,
926 size_t read_pos,
927 size_t count)
929 size_t read_subbuf, padding, end_pos;
930 size_t subbuf_size = buf->chan->subbuf_size;
931 size_t n_subbufs = buf->chan->n_subbufs;
933 read_subbuf = read_pos / subbuf_size;
934 padding = buf->padding[read_subbuf];
935 if (read_pos % subbuf_size + count + padding == subbuf_size)
936 end_pos = (read_subbuf + 1) * subbuf_size;
937 else
938 end_pos = read_pos + count;
939 if (end_pos >= subbuf_size * n_subbufs)
940 end_pos = 0;
942 return end_pos;
946 * subbuf_read_actor - read up to one subbuf's worth of data
948 static int subbuf_read_actor(size_t read_start,
949 struct rchan_buf *buf,
950 size_t avail,
951 read_descriptor_t *desc,
952 read_actor_t actor)
954 void *from;
955 int ret = 0;
957 from = buf->start + read_start;
958 ret = avail;
959 if (copy_to_user(desc->arg.buf, from, avail)) {
960 desc->error = -EFAULT;
961 ret = 0;
963 desc->arg.data += ret;
964 desc->written += ret;
965 desc->count -= ret;
967 return ret;
970 typedef int (*subbuf_actor_t) (size_t read_start,
971 struct rchan_buf *buf,
972 size_t avail,
973 read_descriptor_t *desc,
974 read_actor_t actor);
977 * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
979 static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
980 subbuf_actor_t subbuf_actor,
981 read_actor_t actor,
982 read_descriptor_t *desc)
984 struct rchan_buf *buf = filp->private_data;
985 size_t read_start, avail;
986 int ret;
988 if (!desc->count)
989 return 0;
991 mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
992 do {
993 if (!relay_file_read_avail(buf, *ppos))
994 break;
996 read_start = relay_file_read_start_pos(*ppos, buf);
997 avail = relay_file_read_subbuf_avail(read_start, buf);
998 if (!avail)
999 break;
1001 avail = min(desc->count, avail);
1002 ret = subbuf_actor(read_start, buf, avail, desc, actor);
1003 if (desc->error < 0)
1004 break;
1006 if (ret) {
1007 relay_file_read_consume(buf, read_start, ret);
1008 *ppos = relay_file_read_end_pos(buf, read_start, ret);
1010 } while (desc->count && ret);
1011 mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
1013 return desc->written;
1016 static ssize_t relay_file_read(struct file *filp,
1017 char __user *buffer,
1018 size_t count,
1019 loff_t *ppos)
1021 read_descriptor_t desc;
1022 desc.written = 0;
1023 desc.count = count;
1024 desc.arg.buf = buffer;
1025 desc.error = 0;
1026 return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
1027 NULL, &desc);
1030 static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
1032 rbuf->bytes_consumed += bytes_consumed;
1034 if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
1035 relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1036 rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
1040 static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1041 struct pipe_buffer *buf)
1043 struct rchan_buf *rbuf;
1045 rbuf = (struct rchan_buf *)page_private(buf->page);
1046 relay_consume_bytes(rbuf, buf->private);
1049 static struct pipe_buf_operations relay_pipe_buf_ops = {
1050 .can_merge = 0,
1051 .map = generic_pipe_buf_map,
1052 .unmap = generic_pipe_buf_unmap,
1053 .confirm = generic_pipe_buf_confirm,
1054 .release = relay_pipe_buf_release,
1055 .steal = generic_pipe_buf_steal,
1056 .get = generic_pipe_buf_get,
1060 * subbuf_splice_actor - splice up to one subbuf's worth of data
1062 static int subbuf_splice_actor(struct file *in,
1063 loff_t *ppos,
1064 struct pipe_inode_info *pipe,
1065 size_t len,
1066 unsigned int flags,
1067 int *nonpad_ret)
1069 unsigned int pidx, poff, total_len, subbuf_pages, ret;
1070 struct rchan_buf *rbuf = in->private_data;
1071 unsigned int subbuf_size = rbuf->chan->subbuf_size;
1072 uint64_t pos = (uint64_t) *ppos;
1073 uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
1074 size_t read_start = (size_t) do_div(pos, alloc_size);
1075 size_t read_subbuf = read_start / subbuf_size;
1076 size_t padding = rbuf->padding[read_subbuf];
1077 size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
1078 struct page *pages[PIPE_BUFFERS];
1079 struct partial_page partial[PIPE_BUFFERS];
1080 struct splice_pipe_desc spd = {
1081 .pages = pages,
1082 .nr_pages = 0,
1083 .partial = partial,
1084 .flags = flags,
1085 .ops = &relay_pipe_buf_ops,
1088 if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1089 return 0;
1092 * Adjust read len, if longer than what is available
1094 if (len > (subbuf_size - read_start % subbuf_size))
1095 len = subbuf_size - read_start % subbuf_size;
1097 subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1098 pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1099 poff = read_start & ~PAGE_MASK;
1101 for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) {
1102 unsigned int this_len, this_end, private;
1103 unsigned int cur_pos = read_start + total_len;
1105 if (!len)
1106 break;
1108 this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
1109 private = this_len;
1111 spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
1112 spd.partial[spd.nr_pages].offset = poff;
1114 this_end = cur_pos + this_len;
1115 if (this_end >= nonpad_end) {
1116 this_len = nonpad_end - cur_pos;
1117 private = this_len + padding;
1119 spd.partial[spd.nr_pages].len = this_len;
1120 spd.partial[spd.nr_pages].private = private;
1122 len -= this_len;
1123 total_len += this_len;
1124 poff = 0;
1125 pidx = (pidx + 1) % subbuf_pages;
1127 if (this_end >= nonpad_end) {
1128 spd.nr_pages++;
1129 break;
1133 if (!spd.nr_pages)
1134 return 0;
1136 ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
1137 if (ret < 0 || ret < total_len)
1138 return ret;
1140 if (read_start + ret == nonpad_end)
1141 ret += padding;
1143 return ret;
1146 static ssize_t relay_file_splice_read(struct file *in,
1147 loff_t *ppos,
1148 struct pipe_inode_info *pipe,
1149 size_t len,
1150 unsigned int flags)
1152 ssize_t spliced;
1153 int ret;
1154 int nonpad_ret = 0;
1156 ret = 0;
1157 spliced = 0;
1159 while (len) {
1160 ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1161 if (ret < 0)
1162 break;
1163 else if (!ret) {
1164 if (spliced)
1165 break;
1166 if (flags & SPLICE_F_NONBLOCK) {
1167 ret = -EAGAIN;
1168 break;
1172 *ppos += ret;
1173 if (ret > len)
1174 len = 0;
1175 else
1176 len -= ret;
1177 spliced += nonpad_ret;
1178 nonpad_ret = 0;
1181 if (spliced)
1182 return spliced;
1184 return ret;
1187 const struct file_operations relay_file_operations = {
1188 .open = relay_file_open,
1189 .poll = relay_file_poll,
1190 .mmap = relay_file_mmap,
1191 .read = relay_file_read,
1192 .llseek = no_llseek,
1193 .release = relay_file_release,
1194 .splice_read = relay_file_splice_read,
1196 EXPORT_SYMBOL_GPL(relay_file_operations);
1198 static __init int relay_init(void)
1201 hotcpu_notifier(relay_hotcpu_callback, 0);
1202 return 0;
1205 module_init(relay_init);