c0c93a04d4ce926937dfbc9098e1d9df9821853e
[linux-block.git] / kernel / relay.c
1 /*
2  * Public API and common code for kernel->userspace relay file support.
3  *
4  * See Documentation/filesystems/relay.rst 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/export.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  * fault() vm_op implementation for relay file mapping.
32  */
33 static vm_fault_t relay_buf_fault(struct vm_fault *vmf)
34 {
35         struct page *page;
36         struct rchan_buf *buf = vmf->vma->vm_private_data;
37         pgoff_t pgoff = vmf->pgoff;
38
39         if (!buf)
40                 return VM_FAULT_OOM;
41
42         page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
43         if (!page)
44                 return VM_FAULT_SIGBUS;
45         get_page(page);
46         vmf->page = page;
47
48         return 0;
49 }
50
51 /*
52  * vm_ops for relay file mappings.
53  */
54 static const struct vm_operations_struct relay_file_mmap_ops = {
55         .fault = relay_buf_fault,
56 };
57
58 /*
59  * allocate an array of pointers of struct page
60  */
61 static struct page **relay_alloc_page_array(unsigned int n_pages)
62 {
63         return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
64 }
65
66 /*
67  * free an array of pointers of struct page
68  */
69 static void relay_free_page_array(struct page **array)
70 {
71         kvfree(array);
72 }
73
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
78  *
79  *      Returns 0 if ok, negative on error
80  *
81  *      Caller should already have grabbed mmap_lock.
82  */
83 static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
84 {
85         unsigned long length = vma->vm_end - vma->vm_start;
86
87         if (!buf)
88                 return -EBADF;
89
90         if (length != (unsigned long)buf->chan->alloc_size)
91                 return -EINVAL;
92
93         vma->vm_ops = &relay_file_mmap_ops;
94         vm_flags_set(vma, VM_DONTEXPAND);
95         vma->vm_private_data = buf;
96
97         return 0;
98 }
99
100 /**
101  *      relay_alloc_buf - allocate a channel buffer
102  *      @buf: the buffer struct
103  *      @size: total size of the buffer
104  *
105  *      Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
106  *      passed in size will get page aligned, if it isn't already.
107  */
108 static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
109 {
110         void *mem;
111         unsigned int i, j, n_pages;
112
113         *size = PAGE_ALIGN(*size);
114         n_pages = *size >> PAGE_SHIFT;
115
116         buf->page_array = relay_alloc_page_array(n_pages);
117         if (!buf->page_array)
118                 return NULL;
119
120         for (i = 0; i < n_pages; i++) {
121                 buf->page_array[i] = alloc_page(GFP_KERNEL);
122                 if (unlikely(!buf->page_array[i]))
123                         goto depopulate;
124                 set_page_private(buf->page_array[i], (unsigned long)buf);
125         }
126         mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
127         if (!mem)
128                 goto depopulate;
129
130         memset(mem, 0, *size);
131         buf->page_count = n_pages;
132         return mem;
133
134 depopulate:
135         for (j = 0; j < i; j++)
136                 __free_page(buf->page_array[j]);
137         relay_free_page_array(buf->page_array);
138         return NULL;
139 }
140
141 /**
142  *      relay_create_buf - allocate and initialize a channel buffer
143  *      @chan: the relay channel
144  *
145  *      Returns channel buffer if successful, %NULL otherwise.
146  */
147 static struct rchan_buf *relay_create_buf(struct rchan *chan)
148 {
149         struct rchan_buf *buf;
150
151         if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t))
152                 return NULL;
153
154         buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
155         if (!buf)
156                 return NULL;
157         buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t),
158                                      GFP_KERNEL);
159         if (!buf->padding)
160                 goto free_buf;
161
162         buf->start = relay_alloc_buf(buf, &chan->alloc_size);
163         if (!buf->start)
164                 goto free_buf;
165
166         buf->chan = chan;
167         kref_get(&buf->chan->kref);
168         return buf;
169
170 free_buf:
171         kfree(buf->padding);
172         kfree(buf);
173         return NULL;
174 }
175
176 /**
177  *      relay_destroy_channel - free the channel struct
178  *      @kref: target kernel reference that contains the relay channel
179  *
180  *      Should only be called from kref_put().
181  */
182 static void relay_destroy_channel(struct kref *kref)
183 {
184         struct rchan *chan = container_of(kref, struct rchan, kref);
185         free_percpu(chan->buf);
186         kfree(chan);
187 }
188
189 /**
190  *      relay_destroy_buf - destroy an rchan_buf struct and associated buffer
191  *      @buf: the buffer struct
192  */
193 static void relay_destroy_buf(struct rchan_buf *buf)
194 {
195         struct rchan *chan = buf->chan;
196         unsigned int i;
197
198         if (likely(buf->start)) {
199                 vunmap(buf->start);
200                 for (i = 0; i < buf->page_count; i++)
201                         __free_page(buf->page_array[i]);
202                 relay_free_page_array(buf->page_array);
203         }
204         *per_cpu_ptr(chan->buf, buf->cpu) = NULL;
205         kfree(buf->padding);
206         kfree(buf);
207         kref_put(&chan->kref, relay_destroy_channel);
208 }
209
210 /**
211  *      relay_remove_buf - remove a channel buffer
212  *      @kref: target kernel reference that contains the relay buffer
213  *
214  *      Removes the file from the filesystem, which also frees the
215  *      rchan_buf_struct and the channel buffer.  Should only be called from
216  *      kref_put().
217  */
218 static void relay_remove_buf(struct kref *kref)
219 {
220         struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
221         relay_destroy_buf(buf);
222 }
223
224 /**
225  *      relay_buf_empty - boolean, is the channel buffer empty?
226  *      @buf: channel buffer
227  *
228  *      Returns 1 if the buffer is empty, 0 otherwise.
229  */
230 static int relay_buf_empty(struct rchan_buf *buf)
231 {
232         return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
233 }
234
235 /**
236  *      relay_buf_full - boolean, is the channel buffer full?
237  *      @buf: channel buffer
238  *
239  *      Returns 1 if the buffer is full, 0 otherwise.
240  */
241 int relay_buf_full(struct rchan_buf *buf)
242 {
243         size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
244         return (ready >= buf->chan->n_subbufs) ? 1 : 0;
245 }
246 EXPORT_SYMBOL_GPL(relay_buf_full);
247
248 /*
249  * High-level relay kernel API and associated functions.
250  */
251
252 static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
253                               void *prev_subbuf, size_t prev_padding)
254 {
255         if (!buf->chan->cb->subbuf_start)
256                 return !relay_buf_full(buf);
257
258         return buf->chan->cb->subbuf_start(buf, subbuf,
259                                            prev_subbuf, prev_padding);
260 }
261
262 /**
263  *      wakeup_readers - wake up readers waiting on a channel
264  *      @work: contains the channel buffer
265  *
266  *      This is the function used to defer reader waking
267  */
268 static void wakeup_readers(struct irq_work *work)
269 {
270         struct rchan_buf *buf;
271
272         buf = container_of(work, struct rchan_buf, wakeup_work);
273         wake_up_interruptible(&buf->read_wait);
274 }
275
276 /**
277  *      __relay_reset - reset a channel buffer
278  *      @buf: the channel buffer
279  *      @init: 1 if this is a first-time initialization
280  *
281  *      See relay_reset() for description of effect.
282  */
283 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
284 {
285         size_t i;
286
287         if (init) {
288                 init_waitqueue_head(&buf->read_wait);
289                 kref_init(&buf->kref);
290                 init_irq_work(&buf->wakeup_work, wakeup_readers);
291         } else {
292                 irq_work_sync(&buf->wakeup_work);
293         }
294
295         buf->subbufs_produced = 0;
296         buf->subbufs_consumed = 0;
297         buf->bytes_consumed = 0;
298         buf->finalized = 0;
299         buf->data = buf->start;
300         buf->offset = 0;
301
302         for (i = 0; i < buf->chan->n_subbufs; i++)
303                 buf->padding[i] = 0;
304
305         relay_subbuf_start(buf, buf->data, NULL, 0);
306 }
307
308 /**
309  *      relay_reset - reset the channel
310  *      @chan: the channel
311  *
312  *      This has the effect of erasing all data from all channel buffers
313  *      and restarting the channel in its initial state.  The buffers
314  *      are not freed, so any mappings are still in effect.
315  *
316  *      NOTE. Care should be taken that the channel isn't actually
317  *      being used by anything when this call is made.
318  */
319 void relay_reset(struct rchan *chan)
320 {
321         struct rchan_buf *buf;
322         unsigned int i;
323
324         if (!chan)
325                 return;
326
327         if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
328                 __relay_reset(buf, 0);
329                 return;
330         }
331
332         mutex_lock(&relay_channels_mutex);
333         for_each_possible_cpu(i)
334                 if ((buf = *per_cpu_ptr(chan->buf, i)))
335                         __relay_reset(buf, 0);
336         mutex_unlock(&relay_channels_mutex);
337 }
338 EXPORT_SYMBOL_GPL(relay_reset);
339
340 static inline void relay_set_buf_dentry(struct rchan_buf *buf,
341                                         struct dentry *dentry)
342 {
343         buf->dentry = dentry;
344         d_inode(buf->dentry)->i_size = buf->early_bytes;
345 }
346
347 static struct dentry *relay_create_buf_file(struct rchan *chan,
348                                             struct rchan_buf *buf,
349                                             unsigned int cpu)
350 {
351         struct dentry *dentry;
352         char *tmpname;
353
354         tmpname = kasprintf(GFP_KERNEL, "%s%d", chan->base_filename, cpu);
355         if (!tmpname)
356                 return NULL;
357
358         /* Create file in fs */
359         dentry = chan->cb->create_buf_file(tmpname, chan->parent,
360                                            S_IRUSR, buf,
361                                            &chan->is_global);
362         if (IS_ERR(dentry))
363                 dentry = NULL;
364
365         kfree(tmpname);
366
367         return dentry;
368 }
369
370 /*
371  *      relay_open_buf - create a new relay channel buffer
372  *
373  *      used by relay_open() and CPU hotplug.
374  */
375 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
376 {
377         struct rchan_buf *buf;
378         struct dentry *dentry;
379
380         if (chan->is_global)
381                 return *per_cpu_ptr(chan->buf, 0);
382
383         buf = relay_create_buf(chan);
384         if (!buf)
385                 return NULL;
386
387         if (chan->has_base_filename) {
388                 dentry = relay_create_buf_file(chan, buf, cpu);
389                 if (!dentry)
390                         goto free_buf;
391                 relay_set_buf_dentry(buf, dentry);
392         } else {
393                 /* Only retrieve global info, nothing more, nothing less */
394                 dentry = chan->cb->create_buf_file(NULL, NULL,
395                                                    S_IRUSR, buf,
396                                                    &chan->is_global);
397                 if (IS_ERR_OR_NULL(dentry))
398                         goto free_buf;
399         }
400
401         buf->cpu = cpu;
402         __relay_reset(buf, 1);
403
404         if(chan->is_global) {
405                 *per_cpu_ptr(chan->buf, 0) = buf;
406                 buf->cpu = 0;
407         }
408
409         return buf;
410
411 free_buf:
412         relay_destroy_buf(buf);
413         return NULL;
414 }
415
416 /**
417  *      relay_close_buf - close a channel buffer
418  *      @buf: channel buffer
419  *
420  *      Marks the buffer finalized and restores the default callbacks.
421  *      The channel buffer and channel buffer data structure are then freed
422  *      automatically when the last reference is given up.
423  */
424 static void relay_close_buf(struct rchan_buf *buf)
425 {
426         buf->finalized = 1;
427         irq_work_sync(&buf->wakeup_work);
428         buf->chan->cb->remove_buf_file(buf->dentry);
429         kref_put(&buf->kref, relay_remove_buf);
430 }
431
432 int relay_prepare_cpu(unsigned int cpu)
433 {
434         struct rchan *chan;
435         struct rchan_buf *buf;
436
437         mutex_lock(&relay_channels_mutex);
438         list_for_each_entry(chan, &relay_channels, list) {
439                 if (*per_cpu_ptr(chan->buf, cpu))
440                         continue;
441                 buf = relay_open_buf(chan, cpu);
442                 if (!buf) {
443                         pr_err("relay: cpu %d buffer creation failed\n", cpu);
444                         mutex_unlock(&relay_channels_mutex);
445                         return -ENOMEM;
446                 }
447                 *per_cpu_ptr(chan->buf, cpu) = buf;
448         }
449         mutex_unlock(&relay_channels_mutex);
450         return 0;
451 }
452
453 /**
454  *      relay_open - create a new relay channel
455  *      @base_filename: base name of files to create
456  *      @parent: dentry of parent directory, %NULL for root directory or buffer
457  *      @subbuf_size: size of sub-buffers
458  *      @n_subbufs: number of sub-buffers
459  *      @cb: client callback functions
460  *      @private_data: user-defined data
461  *
462  *      Returns channel pointer if successful, %NULL otherwise.
463  *
464  *      Creates a channel buffer for each cpu using the sizes and
465  *      attributes specified.  The created channel buffer files
466  *      will be named base_filename0...base_filenameN-1.  File
467  *      permissions will be %S_IRUSR.
468  */
469 struct rchan *relay_open(const char *base_filename,
470                          struct dentry *parent,
471                          size_t subbuf_size,
472                          size_t n_subbufs,
473                          const struct rchan_callbacks *cb,
474                          void *private_data)
475 {
476         unsigned int i;
477         struct rchan *chan;
478         struct rchan_buf *buf;
479
480         if (!(subbuf_size && n_subbufs))
481                 return NULL;
482         if (subbuf_size > UINT_MAX / n_subbufs)
483                 return NULL;
484         if (!cb || !cb->create_buf_file || !cb->remove_buf_file)
485                 return NULL;
486
487         chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
488         if (!chan)
489                 return NULL;
490
491         chan->buf = alloc_percpu(struct rchan_buf *);
492         if (!chan->buf) {
493                 kfree(chan);
494                 return NULL;
495         }
496
497         chan->version = RELAYFS_CHANNEL_VERSION;
498         chan->n_subbufs = n_subbufs;
499         chan->subbuf_size = subbuf_size;
500         chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
501         chan->parent = parent;
502         chan->private_data = private_data;
503         if (base_filename) {
504                 chan->has_base_filename = 1;
505                 strscpy(chan->base_filename, base_filename, NAME_MAX);
506         }
507         chan->cb = cb;
508         kref_init(&chan->kref);
509
510         mutex_lock(&relay_channels_mutex);
511         for_each_online_cpu(i) {
512                 buf = relay_open_buf(chan, i);
513                 if (!buf)
514                         goto free_bufs;
515                 *per_cpu_ptr(chan->buf, i) = buf;
516         }
517         list_add(&chan->list, &relay_channels);
518         mutex_unlock(&relay_channels_mutex);
519
520         return chan;
521
522 free_bufs:
523         for_each_possible_cpu(i) {
524                 if ((buf = *per_cpu_ptr(chan->buf, i)))
525                         relay_close_buf(buf);
526         }
527
528         kref_put(&chan->kref, relay_destroy_channel);
529         mutex_unlock(&relay_channels_mutex);
530         return NULL;
531 }
532 EXPORT_SYMBOL_GPL(relay_open);
533
534 struct rchan_percpu_buf_dispatcher {
535         struct rchan_buf *buf;
536         struct dentry *dentry;
537 };
538
539 /**
540  *      relay_switch_subbuf - switch to a new sub-buffer
541  *      @buf: channel buffer
542  *      @length: size of current event
543  *
544  *      Returns either the length passed in or 0 if full.
545  *
546  *      Performs sub-buffer-switch tasks such as invoking callbacks,
547  *      updating padding counts, waking up readers, etc.
548  */
549 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
550 {
551         void *old, *new;
552         size_t old_subbuf, new_subbuf;
553
554         if (unlikely(length > buf->chan->subbuf_size))
555                 goto toobig;
556
557         if (buf->offset != buf->chan->subbuf_size + 1) {
558                 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
559                 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
560                 buf->padding[old_subbuf] = buf->prev_padding;
561                 buf->subbufs_produced++;
562                 if (buf->dentry)
563                         d_inode(buf->dentry)->i_size +=
564                                 buf->chan->subbuf_size -
565                                 buf->padding[old_subbuf];
566                 else
567                         buf->early_bytes += buf->chan->subbuf_size -
568                                             buf->padding[old_subbuf];
569                 smp_mb();
570                 if (waitqueue_active(&buf->read_wait)) {
571                         /*
572                          * Calling wake_up_interruptible() from here
573                          * will deadlock if we happen to be logging
574                          * from the scheduler (trying to re-grab
575                          * rq->lock), so defer it.
576                          */
577                         irq_work_queue(&buf->wakeup_work);
578                 }
579         }
580
581         old = buf->data;
582         new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
583         new = buf->start + new_subbuf * buf->chan->subbuf_size;
584         buf->offset = 0;
585         if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) {
586                 buf->offset = buf->chan->subbuf_size + 1;
587                 return 0;
588         }
589         buf->data = new;
590         buf->padding[new_subbuf] = 0;
591
592         if (unlikely(length + buf->offset > buf->chan->subbuf_size))
593                 goto toobig;
594
595         return length;
596
597 toobig:
598         buf->chan->last_toobig = length;
599         return 0;
600 }
601 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
602
603 /**
604  *      relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
605  *      @chan: the channel
606  *      @cpu: the cpu associated with the channel buffer to update
607  *      @subbufs_consumed: number of sub-buffers to add to current buf's count
608  *
609  *      Adds to the channel buffer's consumed sub-buffer count.
610  *      subbufs_consumed should be the number of sub-buffers newly consumed,
611  *      not the total consumed.
612  *
613  *      NOTE. Kernel clients don't need to call this function if the channel
614  *      mode is 'overwrite'.
615  */
616 void relay_subbufs_consumed(struct rchan *chan,
617                             unsigned int cpu,
618                             size_t subbufs_consumed)
619 {
620         struct rchan_buf *buf;
621
622         if (!chan || cpu >= NR_CPUS)
623                 return;
624
625         buf = *per_cpu_ptr(chan->buf, cpu);
626         if (!buf || subbufs_consumed > chan->n_subbufs)
627                 return;
628
629         if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
630                 buf->subbufs_consumed = buf->subbufs_produced;
631         else
632                 buf->subbufs_consumed += subbufs_consumed;
633 }
634 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
635
636 /**
637  *      relay_close - close the channel
638  *      @chan: the channel
639  *
640  *      Closes all channel buffers and frees the channel.
641  */
642 void relay_close(struct rchan *chan)
643 {
644         struct rchan_buf *buf;
645         unsigned int i;
646
647         if (!chan)
648                 return;
649
650         mutex_lock(&relay_channels_mutex);
651         if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
652                 relay_close_buf(buf);
653         else
654                 for_each_possible_cpu(i)
655                         if ((buf = *per_cpu_ptr(chan->buf, i)))
656                                 relay_close_buf(buf);
657
658         if (chan->last_toobig)
659                 printk(KERN_WARNING "relay: one or more items not logged "
660                        "[item size (%zd) > sub-buffer size (%zd)]\n",
661                        chan->last_toobig, chan->subbuf_size);
662
663         list_del(&chan->list);
664         kref_put(&chan->kref, relay_destroy_channel);
665         mutex_unlock(&relay_channels_mutex);
666 }
667 EXPORT_SYMBOL_GPL(relay_close);
668
669 /**
670  *      relay_flush - close the channel
671  *      @chan: the channel
672  *
673  *      Flushes all channel buffers, i.e. forces buffer switch.
674  */
675 void relay_flush(struct rchan *chan)
676 {
677         struct rchan_buf *buf;
678         unsigned int i;
679
680         if (!chan)
681                 return;
682
683         if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
684                 relay_switch_subbuf(buf, 0);
685                 return;
686         }
687
688         mutex_lock(&relay_channels_mutex);
689         for_each_possible_cpu(i)
690                 if ((buf = *per_cpu_ptr(chan->buf, i)))
691                         relay_switch_subbuf(buf, 0);
692         mutex_unlock(&relay_channels_mutex);
693 }
694 EXPORT_SYMBOL_GPL(relay_flush);
695
696 /**
697  *      relay_file_open - open file op for relay files
698  *      @inode: the inode
699  *      @filp: the file
700  *
701  *      Increments the channel buffer refcount.
702  */
703 static int relay_file_open(struct inode *inode, struct file *filp)
704 {
705         struct rchan_buf *buf = inode->i_private;
706         kref_get(&buf->kref);
707         filp->private_data = buf;
708
709         return nonseekable_open(inode, filp);
710 }
711
712 /**
713  *      relay_file_mmap - mmap file op for relay files
714  *      @filp: the file
715  *      @vma: the vma describing what to map
716  *
717  *      Calls upon relay_mmap_buf() to map the file into user space.
718  */
719 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
720 {
721         struct rchan_buf *buf = filp->private_data;
722         return relay_mmap_buf(buf, vma);
723 }
724
725 /**
726  *      relay_file_poll - poll file op for relay files
727  *      @filp: the file
728  *      @wait: poll table
729  *
730  *      Poll implemention.
731  */
732 static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
733 {
734         __poll_t mask = 0;
735         struct rchan_buf *buf = filp->private_data;
736
737         if (buf->finalized)
738                 return EPOLLERR;
739
740         if (filp->f_mode & FMODE_READ) {
741                 poll_wait(filp, &buf->read_wait, wait);
742                 if (!relay_buf_empty(buf))
743                         mask |= EPOLLIN | EPOLLRDNORM;
744         }
745
746         return mask;
747 }
748
749 /**
750  *      relay_file_release - release file op for relay files
751  *      @inode: the inode
752  *      @filp: the file
753  *
754  *      Decrements the channel refcount, as the filesystem is
755  *      no longer using it.
756  */
757 static int relay_file_release(struct inode *inode, struct file *filp)
758 {
759         struct rchan_buf *buf = filp->private_data;
760         kref_put(&buf->kref, relay_remove_buf);
761
762         return 0;
763 }
764
765 /*
766  *      relay_file_read_consume - update the consumed count for the buffer
767  */
768 static void relay_file_read_consume(struct rchan_buf *buf,
769                                     size_t read_pos,
770                                     size_t bytes_consumed)
771 {
772         size_t subbuf_size = buf->chan->subbuf_size;
773         size_t n_subbufs = buf->chan->n_subbufs;
774         size_t read_subbuf;
775
776         if (buf->subbufs_produced == buf->subbufs_consumed &&
777             buf->offset == buf->bytes_consumed)
778                 return;
779
780         if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
781                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
782                 buf->bytes_consumed = 0;
783         }
784
785         buf->bytes_consumed += bytes_consumed;
786         if (!read_pos)
787                 read_subbuf = buf->subbufs_consumed % n_subbufs;
788         else
789                 read_subbuf = read_pos / buf->chan->subbuf_size;
790         if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
791                 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
792                     (buf->offset == subbuf_size))
793                         return;
794                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
795                 buf->bytes_consumed = 0;
796         }
797 }
798
799 /*
800  *      relay_file_read_avail - boolean, are there unconsumed bytes available?
801  */
802 static int relay_file_read_avail(struct rchan_buf *buf)
803 {
804         size_t subbuf_size = buf->chan->subbuf_size;
805         size_t n_subbufs = buf->chan->n_subbufs;
806         size_t produced = buf->subbufs_produced;
807         size_t consumed;
808
809         relay_file_read_consume(buf, 0, 0);
810
811         consumed = buf->subbufs_consumed;
812
813         if (unlikely(buf->offset > subbuf_size)) {
814                 if (produced == consumed)
815                         return 0;
816                 return 1;
817         }
818
819         if (unlikely(produced - consumed >= n_subbufs)) {
820                 consumed = produced - n_subbufs + 1;
821                 buf->subbufs_consumed = consumed;
822                 buf->bytes_consumed = 0;
823         }
824
825         produced = (produced % n_subbufs) * subbuf_size + buf->offset;
826         consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
827
828         if (consumed > produced)
829                 produced += n_subbufs * subbuf_size;
830
831         if (consumed == produced) {
832                 if (buf->offset == subbuf_size &&
833                     buf->subbufs_produced > buf->subbufs_consumed)
834                         return 1;
835                 return 0;
836         }
837
838         return 1;
839 }
840
841 /**
842  *      relay_file_read_subbuf_avail - return bytes available in sub-buffer
843  *      @read_pos: file read position
844  *      @buf: relay channel buffer
845  */
846 static size_t relay_file_read_subbuf_avail(size_t read_pos,
847                                            struct rchan_buf *buf)
848 {
849         size_t padding, avail = 0;
850         size_t read_subbuf, read_offset, write_subbuf, write_offset;
851         size_t subbuf_size = buf->chan->subbuf_size;
852
853         write_subbuf = (buf->data - buf->start) / subbuf_size;
854         write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
855         read_subbuf = read_pos / subbuf_size;
856         read_offset = read_pos % subbuf_size;
857         padding = buf->padding[read_subbuf];
858
859         if (read_subbuf == write_subbuf) {
860                 if (read_offset + padding < write_offset)
861                         avail = write_offset - (read_offset + padding);
862         } else
863                 avail = (subbuf_size - padding) - read_offset;
864
865         return avail;
866 }
867
868 /**
869  *      relay_file_read_start_pos - find the first available byte to read
870  *      @buf: relay channel buffer
871  *
872  *      If the read_pos is in the middle of padding, return the
873  *      position of the first actually available byte, otherwise
874  *      return the original value.
875  */
876 static size_t relay_file_read_start_pos(struct rchan_buf *buf)
877 {
878         size_t read_subbuf, padding, padding_start, padding_end;
879         size_t subbuf_size = buf->chan->subbuf_size;
880         size_t n_subbufs = buf->chan->n_subbufs;
881         size_t consumed = buf->subbufs_consumed % n_subbufs;
882         size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed)
883                         % (n_subbufs * subbuf_size);
884
885         read_subbuf = read_pos / subbuf_size;
886         padding = buf->padding[read_subbuf];
887         padding_start = (read_subbuf + 1) * subbuf_size - padding;
888         padding_end = (read_subbuf + 1) * subbuf_size;
889         if (read_pos >= padding_start && read_pos < padding_end) {
890                 read_subbuf = (read_subbuf + 1) % n_subbufs;
891                 read_pos = read_subbuf * subbuf_size;
892         }
893
894         return read_pos;
895 }
896
897 /**
898  *      relay_file_read_end_pos - return the new read position
899  *      @read_pos: file read position
900  *      @buf: relay channel buffer
901  *      @count: number of bytes to be read
902  */
903 static size_t relay_file_read_end_pos(struct rchan_buf *buf,
904                                       size_t read_pos,
905                                       size_t count)
906 {
907         size_t read_subbuf, padding, end_pos;
908         size_t subbuf_size = buf->chan->subbuf_size;
909         size_t n_subbufs = buf->chan->n_subbufs;
910
911         read_subbuf = read_pos / subbuf_size;
912         padding = buf->padding[read_subbuf];
913         if (read_pos % subbuf_size + count + padding == subbuf_size)
914                 end_pos = (read_subbuf + 1) * subbuf_size;
915         else
916                 end_pos = read_pos + count;
917         if (end_pos >= subbuf_size * n_subbufs)
918                 end_pos = 0;
919
920         return end_pos;
921 }
922
923 static ssize_t relay_file_read(struct file *filp,
924                                char __user *buffer,
925                                size_t count,
926                                loff_t *ppos)
927 {
928         struct rchan_buf *buf = filp->private_data;
929         size_t read_start, avail;
930         size_t written = 0;
931         int ret;
932
933         if (!count)
934                 return 0;
935
936         inode_lock(file_inode(filp));
937         do {
938                 void *from;
939
940                 if (!relay_file_read_avail(buf))
941                         break;
942
943                 read_start = relay_file_read_start_pos(buf);
944                 avail = relay_file_read_subbuf_avail(read_start, buf);
945                 if (!avail)
946                         break;
947
948                 avail = min(count, avail);
949                 from = buf->start + read_start;
950                 ret = avail;
951                 if (copy_to_user(buffer, from, avail))
952                         break;
953
954                 buffer += ret;
955                 written += ret;
956                 count -= ret;
957
958                 relay_file_read_consume(buf, read_start, ret);
959                 *ppos = relay_file_read_end_pos(buf, read_start, ret);
960         } while (count);
961         inode_unlock(file_inode(filp));
962
963         return written;
964 }
965
966
967 const struct file_operations relay_file_operations = {
968         .open           = relay_file_open,
969         .poll           = relay_file_poll,
970         .mmap           = relay_file_mmap,
971         .read           = relay_file_read,
972         .release        = relay_file_release,
973 };
974 EXPORT_SYMBOL_GPL(relay_file_operations);