Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[linux-2.6] / kernel / relay.c
1 /*
2  * Public API and common code for kernel->userspace relay file support.
3  *
4  * See Documentation/filesystems/relay.txt for an overview.
5  *
6  * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7  * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
8  *
9  * Moved to kernel/relay.c by Paul Mundt, 2006.
10  * November 2006 - CPU hotplug support by Mathieu Desnoyers
11  *      (mathieu.desnoyers@polymtl.ca)
12  *
13  * This file is released under the GPL.
14  */
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>
25
26 /* list of open channels, for cpu hotplug */
27 static DEFINE_MUTEX(relay_channels_mutex);
28 static LIST_HEAD(relay_channels);
29
30 /*
31  * close() vm_op implementation for relay file mapping.
32  */
33 static void relay_file_mmap_close(struct vm_area_struct *vma)
34 {
35         struct rchan_buf *buf = vma->vm_private_data;
36         buf->chan->cb->buf_unmapped(buf, vma->vm_file);
37 }
38
39 /*
40  * fault() vm_op implementation for relay file mapping.
41  */
42 static int relay_buf_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
43 {
44         struct page *page;
45         struct rchan_buf *buf = vma->vm_private_data;
46         pgoff_t pgoff = vmf->pgoff;
47
48         if (!buf)
49                 return VM_FAULT_OOM;
50
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;
56
57         return 0;
58 }
59
60 /*
61  * vm_ops for relay file mappings.
62  */
63 static struct vm_operations_struct relay_file_mmap_ops = {
64         .fault = relay_buf_fault,
65         .close = relay_file_mmap_close,
66 };
67
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
72  *
73  *      Returns 0 if ok, negative on error
74  *
75  *      Caller should already have grabbed mmap_sem.
76  */
77 static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
78 {
79         unsigned long length = vma->vm_end - vma->vm_start;
80         struct file *filp = vma->vm_file;
81
82         if (!buf)
83                 return -EBADF;
84
85         if (length != (unsigned long)buf->chan->alloc_size)
86                 return -EINVAL;
87
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);
92
93         return 0;
94 }
95
96 /**
97  *      relay_alloc_buf - allocate a channel buffer
98  *      @buf: the buffer struct
99  *      @size: total size of the buffer
100  *
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.
103  */
104 static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
105 {
106         void *mem;
107         unsigned int i, j, n_pages;
108
109         *size = PAGE_ALIGN(*size);
110         n_pages = *size >> PAGE_SHIFT;
111
112         buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
113         if (!buf->page_array)
114                 return NULL;
115
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);
121         }
122         mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
123         if (!mem)
124                 goto depopulate;
125
126         memset(mem, 0, *size);
127         buf->page_count = n_pages;
128         return mem;
129
130 depopulate:
131         for (j = 0; j < i; j++)
132                 __free_page(buf->page_array[j]);
133         kfree(buf->page_array);
134         return NULL;
135 }
136
137 /**
138  *      relay_create_buf - allocate and initialize a channel buffer
139  *      @chan: the relay channel
140  *
141  *      Returns channel buffer if successful, %NULL otherwise.
142  */
143 static struct rchan_buf *relay_create_buf(struct rchan *chan)
144 {
145         struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
146         if (!buf)
147                 return NULL;
148
149         buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
150         if (!buf->padding)
151                 goto free_buf;
152
153         buf->start = relay_alloc_buf(buf, &chan->alloc_size);
154         if (!buf->start)
155                 goto free_buf;
156
157         buf->chan = chan;
158         kref_get(&buf->chan->kref);
159         return buf;
160
161 free_buf:
162         kfree(buf->padding);
163         kfree(buf);
164         return NULL;
165 }
166
167 /**
168  *      relay_destroy_channel - free the channel struct
169  *      @kref: target kernel reference that contains the relay channel
170  *
171  *      Should only be called from kref_put().
172  */
173 static void relay_destroy_channel(struct kref *kref)
174 {
175         struct rchan *chan = container_of(kref, struct rchan, kref);
176         kfree(chan);
177 }
178
179 /**
180  *      relay_destroy_buf - destroy an rchan_buf struct and associated buffer
181  *      @buf: the buffer struct
182  */
183 static void relay_destroy_buf(struct rchan_buf *buf)
184 {
185         struct rchan *chan = buf->chan;
186         unsigned int i;
187
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);
193         }
194         chan->buf[buf->cpu] = NULL;
195         kfree(buf->padding);
196         kfree(buf);
197         kref_put(&chan->kref, relay_destroy_channel);
198 }
199
200 /**
201  *      relay_remove_buf - remove a channel buffer
202  *      @kref: target kernel reference that contains the relay buffer
203  *
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().
207  */
208 static void relay_remove_buf(struct kref *kref)
209 {
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);
213 }
214
215 /**
216  *      relay_buf_empty - boolean, is the channel buffer empty?
217  *      @buf: channel buffer
218  *
219  *      Returns 1 if the buffer is empty, 0 otherwise.
220  */
221 static int relay_buf_empty(struct rchan_buf *buf)
222 {
223         return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
224 }
225
226 /**
227  *      relay_buf_full - boolean, is the channel buffer full?
228  *      @buf: channel buffer
229  *
230  *      Returns 1 if the buffer is full, 0 otherwise.
231  */
232 int relay_buf_full(struct rchan_buf *buf)
233 {
234         size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
235         return (ready >= buf->chan->n_subbufs) ? 1 : 0;
236 }
237 EXPORT_SYMBOL_GPL(relay_buf_full);
238
239 /*
240  * High-level relay kernel API and associated functions.
241  */
242
243 /*
244  * rchan_callback implementations defining default channel behavior.  Used
245  * in place of corresponding NULL values in client callback struct.
246  */
247
248 /*
249  * subbuf_start() default callback.  Does nothing.
250  */
251 static int subbuf_start_default_callback (struct rchan_buf *buf,
252                                           void *subbuf,
253                                           void *prev_subbuf,
254                                           size_t prev_padding)
255 {
256         if (relay_buf_full(buf))
257                 return 0;
258
259         return 1;
260 }
261
262 /*
263  * buf_mapped() default callback.  Does nothing.
264  */
265 static void buf_mapped_default_callback(struct rchan_buf *buf,
266                                         struct file *filp)
267 {
268 }
269
270 /*
271  * buf_unmapped() default callback.  Does nothing.
272  */
273 static void buf_unmapped_default_callback(struct rchan_buf *buf,
274                                           struct file *filp)
275 {
276 }
277
278 /*
279  * create_buf_file_create() default callback.  Does nothing.
280  */
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)
286 {
287         return NULL;
288 }
289
290 /*
291  * remove_buf_file() default callback.  Does nothing.
292  */
293 static int remove_buf_file_default_callback(struct dentry *dentry)
294 {
295         return -EINVAL;
296 }
297
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,
305 };
306
307 /**
308  *      wakeup_readers - wake up readers waiting on a channel
309  *      @data: contains the channel buffer
310  *
311  *      This is the timer function used to defer reader waking.
312  */
313 static void wakeup_readers(unsigned long data)
314 {
315         struct rchan_buf *buf = (struct rchan_buf *)data;
316         wake_up_interruptible(&buf->read_wait);
317 }
318
319 /**
320  *      __relay_reset - reset a channel buffer
321  *      @buf: the channel buffer
322  *      @init: 1 if this is a first-time initialization
323  *
324  *      See relay_reset() for description of effect.
325  */
326 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
327 {
328         size_t i;
329
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);
336
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;
343
344         for (i = 0; i < buf->chan->n_subbufs; i++)
345                 buf->padding[i] = 0;
346
347         buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
348 }
349
350 /**
351  *      relay_reset - reset the channel
352  *      @chan: the channel
353  *
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.
357  *
358  *      NOTE. Care should be taken that the channel isn't actually
359  *      being used by anything when this call is made.
360  */
361 void relay_reset(struct rchan *chan)
362 {
363         unsigned int i;
364
365         if (!chan)
366                 return;
367
368         if (chan->is_global && chan->buf[0]) {
369                 __relay_reset(chan->buf[0], 0);
370                 return;
371         }
372
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);
378 }
379 EXPORT_SYMBOL_GPL(relay_reset);
380
381 /*
382  *      relay_open_buf - create a new relay channel buffer
383  *
384  *      used by relay_open() and CPU hotplug.
385  */
386 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
387 {
388         struct rchan_buf *buf = NULL;
389         struct dentry *dentry;
390         char *tmpname;
391
392         if (chan->is_global)
393                 return chan->buf[0];
394
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);
399
400         buf = relay_create_buf(chan);
401         if (!buf)
402                 goto free_name;
403
404         buf->cpu = cpu;
405         __relay_reset(buf, 1);
406
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;
412
413         buf->dentry = dentry;
414
415         if(chan->is_global) {
416                 chan->buf[0] = buf;
417                 buf->cpu = 0;
418         }
419
420         goto free_name;
421
422 free_buf:
423         relay_destroy_buf(buf);
424         buf = NULL;
425 free_name:
426         kfree(tmpname);
427 end:
428         return buf;
429 }
430
431 /**
432  *      relay_close_buf - close a channel buffer
433  *      @buf: channel buffer
434  *
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.
438  */
439 static void relay_close_buf(struct rchan_buf *buf)
440 {
441         buf->finalized = 1;
442         del_timer_sync(&buf->timer);
443         kref_put(&buf->kref, relay_remove_buf);
444 }
445
446 static void setup_callbacks(struct rchan *chan,
447                                    struct rchan_callbacks *cb)
448 {
449         if (!cb) {
450                 chan->cb = &default_channel_callbacks;
451                 return;
452         }
453
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;
465 }
466
467 /**
468  *      relay_hotcpu_callback - CPU hotplug callback
469  *      @nb: notifier block
470  *      @action: hotplug action to take
471  *      @hcpu: CPU number
472  *
473  *      Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
474  */
475 static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
476                                 unsigned long action,
477                                 void *hcpu)
478 {
479         unsigned int hotcpu = (unsigned long)hcpu;
480         struct rchan *chan;
481
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;
496                         }
497                 }
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;
505         }
506         return NOTIFY_OK;
507 }
508
509 /**
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
517  *
518  *      Returns channel pointer if successful, %NULL otherwise.
519  *
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.
524  */
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)
531 {
532         unsigned int i;
533         struct rchan *chan;
534         if (!base_filename)
535                 return NULL;
536
537         if (!(subbuf_size && n_subbufs))
538                 return NULL;
539
540         chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
541         if (!chan)
542                 return NULL;
543
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);
553
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;
559         }
560         list_add(&chan->list, &relay_channels);
561         mutex_unlock(&relay_channels_mutex);
562
563         return chan;
564
565 free_bufs:
566         for_each_online_cpu(i) {
567                 if (!chan->buf[i])
568                         break;
569                 relay_close_buf(chan->buf[i]);
570         }
571
572         kref_put(&chan->kref, relay_destroy_channel);
573         mutex_unlock(&relay_channels_mutex);
574         return NULL;
575 }
576 EXPORT_SYMBOL_GPL(relay_open);
577
578 /**
579  *      relay_switch_subbuf - switch to a new sub-buffer
580  *      @buf: channel buffer
581  *      @length: size of current event
582  *
583  *      Returns either the length passed in or 0 if full.
584  *
585  *      Performs sub-buffer-switch tasks such as invoking callbacks,
586  *      updating padding counts, waking up readers, etc.
587  */
588 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
589 {
590         void *old, *new;
591         size_t old_subbuf, new_subbuf;
592
593         if (unlikely(length > buf->chan->subbuf_size))
594                 goto toobig;
595
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))
605                         /*
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.
610                          */
611                         __mod_timer(&buf->timer, jiffies + 1);
612         }
613
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;
621         }
622         buf->data = new;
623         buf->padding[new_subbuf] = 0;
624
625         if (unlikely(length + buf->offset > buf->chan->subbuf_size))
626                 goto toobig;
627
628         return length;
629
630 toobig:
631         buf->chan->last_toobig = length;
632         return 0;
633 }
634 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
635
636 /**
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
641  *
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.
645  *
646  *      NOTE. Kernel clients don't need to call this function if the channel
647  *      mode is 'overwrite'.
648  */
649 void relay_subbufs_consumed(struct rchan *chan,
650                             unsigned int cpu,
651                             size_t subbufs_consumed)
652 {
653         struct rchan_buf *buf;
654
655         if (!chan)
656                 return;
657
658         if (cpu >= NR_CPUS || !chan->buf[cpu])
659                 return;
660
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;
665 }
666 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
667
668 /**
669  *      relay_close - close the channel
670  *      @chan: the channel
671  *
672  *      Closes all channel buffers and frees the channel.
673  */
674 void relay_close(struct rchan *chan)
675 {
676         unsigned int i;
677
678         if (!chan)
679                 return;
680
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]);
688
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);
693
694         list_del(&chan->list);
695         kref_put(&chan->kref, relay_destroy_channel);
696         mutex_unlock(&relay_channels_mutex);
697 }
698 EXPORT_SYMBOL_GPL(relay_close);
699
700 /**
701  *      relay_flush - close the channel
702  *      @chan: the channel
703  *
704  *      Flushes all channel buffers, i.e. forces buffer switch.
705  */
706 void relay_flush(struct rchan *chan)
707 {
708         unsigned int i;
709
710         if (!chan)
711                 return;
712
713         if (chan->is_global && chan->buf[0]) {
714                 relay_switch_subbuf(chan->buf[0], 0);
715                 return;
716         }
717
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);
723 }
724 EXPORT_SYMBOL_GPL(relay_flush);
725
726 /**
727  *      relay_file_open - open file op for relay files
728  *      @inode: the inode
729  *      @filp: the file
730  *
731  *      Increments the channel buffer refcount.
732  */
733 static int relay_file_open(struct inode *inode, struct file *filp)
734 {
735         struct rchan_buf *buf = inode->i_private;
736         kref_get(&buf->kref);
737         filp->private_data = buf;
738
739         return 0;
740 }
741
742 /**
743  *      relay_file_mmap - mmap file op for relay files
744  *      @filp: the file
745  *      @vma: the vma describing what to map
746  *
747  *      Calls upon relay_mmap_buf() to map the file into user space.
748  */
749 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
750 {
751         struct rchan_buf *buf = filp->private_data;
752         return relay_mmap_buf(buf, vma);
753 }
754
755 /**
756  *      relay_file_poll - poll file op for relay files
757  *      @filp: the file
758  *      @wait: poll table
759  *
760  *      Poll implemention.
761  */
762 static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
763 {
764         unsigned int mask = 0;
765         struct rchan_buf *buf = filp->private_data;
766
767         if (buf->finalized)
768                 return POLLERR;
769
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;
774         }
775
776         return mask;
777 }
778
779 /**
780  *      relay_file_release - release file op for relay files
781  *      @inode: the inode
782  *      @filp: the file
783  *
784  *      Decrements the channel refcount, as the filesystem is
785  *      no longer using it.
786  */
787 static int relay_file_release(struct inode *inode, struct file *filp)
788 {
789         struct rchan_buf *buf = filp->private_data;
790         kref_put(&buf->kref, relay_remove_buf);
791
792         return 0;
793 }
794
795 /*
796  *      relay_file_read_consume - update the consumed count for the buffer
797  */
798 static void relay_file_read_consume(struct rchan_buf *buf,
799                                     size_t read_pos,
800                                     size_t bytes_consumed)
801 {
802         size_t subbuf_size = buf->chan->subbuf_size;
803         size_t n_subbufs = buf->chan->n_subbufs;
804         size_t read_subbuf;
805
806         if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
807                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
808                 buf->bytes_consumed = 0;
809         }
810
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;
822         }
823 }
824
825 /*
826  *      relay_file_read_avail - boolean, are there unconsumed bytes available?
827  */
828 static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
829 {
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;
834
835         relay_file_read_consume(buf, read_pos, 0);
836
837         if (unlikely(buf->offset > subbuf_size)) {
838                 if (produced == consumed)
839                         return 0;
840                 return 1;
841         }
842
843         if (unlikely(produced - consumed >= n_subbufs)) {
844                 consumed = produced - n_subbufs + 1;
845                 buf->subbufs_consumed = consumed;
846                 buf->bytes_consumed = 0;
847         }
848
849         produced = (produced % n_subbufs) * subbuf_size + buf->offset;
850         consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
851
852         if (consumed > produced)
853                 produced += n_subbufs * subbuf_size;
854
855         if (consumed == produced)
856                 return 0;
857
858         return 1;
859 }
860
861 /**
862  *      relay_file_read_subbuf_avail - return bytes available in sub-buffer
863  *      @read_pos: file read position
864  *      @buf: relay channel buffer
865  */
866 static size_t relay_file_read_subbuf_avail(size_t read_pos,
867                                            struct rchan_buf *buf)
868 {
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;
872
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];
878
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;
884
885         return avail;
886 }
887
888 /**
889  *      relay_file_read_start_pos - find the first available byte to read
890  *      @read_pos: file read position
891  *      @buf: relay channel buffer
892  *
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.
896  */
897 static size_t relay_file_read_start_pos(size_t read_pos,
898                                         struct rchan_buf *buf)
899 {
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;
904
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;
914         }
915
916         return read_pos;
917 }
918
919 /**
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
924  */
925 static size_t relay_file_read_end_pos(struct rchan_buf *buf,
926                                       size_t read_pos,
927                                       size_t count)
928 {
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;
932
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;
941
942         return end_pos;
943 }
944
945 /*
946  *      subbuf_read_actor - read up to one subbuf's worth of data
947  */
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)
953 {
954         void *from;
955         int ret = 0;
956
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;
962         }
963         desc->arg.data += ret;
964         desc->written += ret;
965         desc->count -= ret;
966
967         return ret;
968 }
969
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);
975
976 /*
977  *      relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
978  */
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)
983 {
984         struct rchan_buf *buf = filp->private_data;
985         size_t read_start, avail;
986         int ret;
987
988         if (!desc->count)
989                 return 0;
990
991         mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
992         do {
993                 if (!relay_file_read_avail(buf, *ppos))
994                         break;
995
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;
1000
1001                 avail = min(desc->count, avail);
1002                 ret = subbuf_actor(read_start, buf, avail, desc, actor);
1003                 if (desc->error < 0)
1004                         break;
1005
1006                 if (ret) {
1007                         relay_file_read_consume(buf, read_start, ret);
1008                         *ppos = relay_file_read_end_pos(buf, read_start, ret);
1009                 }
1010         } while (desc->count && ret);
1011         mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
1012
1013         return desc->written;
1014 }
1015
1016 static ssize_t relay_file_read(struct file *filp,
1017                                char __user *buffer,
1018                                size_t count,
1019                                loff_t *ppos)
1020 {
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);
1028 }
1029
1030 static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
1031 {
1032         rbuf->bytes_consumed += bytes_consumed;
1033
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;
1037         }
1038 }
1039
1040 static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1041                                    struct pipe_buffer *buf)
1042 {
1043         struct rchan_buf *rbuf;
1044
1045         rbuf = (struct rchan_buf *)page_private(buf->page);
1046         relay_consume_bytes(rbuf, buf->private);
1047 }
1048
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,
1057 };
1058
1059 /*
1060  *      subbuf_splice_actor - splice up to one subbuf's worth of data
1061  */
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)
1068 {
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,
1086         };
1087
1088         if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1089                 return 0;
1090
1091         /*
1092          * Adjust read len, if longer than what is available
1093          */
1094         if (len > (subbuf_size - read_start % subbuf_size))
1095                 len = subbuf_size - read_start % subbuf_size;
1096
1097         subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1098         pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1099         poff = read_start & ~PAGE_MASK;
1100
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;
1104
1105                 if (!len)
1106                         break;
1107
1108                 this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
1109                 private = this_len;
1110
1111                 spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
1112                 spd.partial[spd.nr_pages].offset = poff;
1113
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;
1118                 }
1119                 spd.partial[spd.nr_pages].len = this_len;
1120                 spd.partial[spd.nr_pages].private = private;
1121
1122                 len -= this_len;
1123                 total_len += this_len;
1124                 poff = 0;
1125                 pidx = (pidx + 1) % subbuf_pages;
1126
1127                 if (this_end >= nonpad_end) {
1128                         spd.nr_pages++;
1129                         break;
1130                 }
1131         }
1132
1133         if (!spd.nr_pages)
1134                 return 0;
1135
1136         ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
1137         if (ret < 0 || ret < total_len)
1138                 return ret;
1139
1140         if (read_start + ret == nonpad_end)
1141                 ret += padding;
1142
1143         return ret;
1144 }
1145
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)
1151 {
1152         ssize_t spliced;
1153         int ret;
1154         int nonpad_ret = 0;
1155
1156         ret = 0;
1157         spliced = 0;
1158
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;
1169                         }
1170                 }
1171
1172                 *ppos += ret;
1173                 if (ret > len)
1174                         len = 0;
1175                 else
1176                         len -= ret;
1177                 spliced += nonpad_ret;
1178                 nonpad_ret = 0;
1179         }
1180
1181         if (spliced)
1182                 return spliced;
1183
1184         return ret;
1185 }
1186
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,
1195 };
1196 EXPORT_SYMBOL_GPL(relay_file_operations);
1197
1198 static __init int relay_init(void)
1199 {
1200
1201         hotcpu_notifier(relay_hotcpu_callback, 0);
1202         return 0;
1203 }
1204
1205 module_init(relay_init);