xref: /linux/kernel/relay.c (revision ac05b7a1b48ba9fc79937a08db4c7131dba8fc5f)
1b86ff981SJens Axboe /*
2b86ff981SJens Axboe  * Public API and common code for kernel->userspace relay file support.
3b86ff981SJens Axboe  *
40c1bc6b8SMauro Carvalho Chehab  * See Documentation/filesystems/relay.rst for an overview.
5b86ff981SJens Axboe  *
6b86ff981SJens Axboe  * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7b86ff981SJens Axboe  * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
8b86ff981SJens Axboe  *
9b86ff981SJens Axboe  * Moved to kernel/relay.c by Paul Mundt, 2006.
1023c88752SMathieu Desnoyers  * November 2006 - CPU hotplug support by Mathieu Desnoyers
1123c88752SMathieu Desnoyers  * 	(mathieu.desnoyers@polymtl.ca)
12b86ff981SJens Axboe  *
13b86ff981SJens Axboe  * This file is released under the GPL.
14b86ff981SJens Axboe  */
15b86ff981SJens Axboe #include <linux/errno.h>
16b86ff981SJens Axboe #include <linux/stddef.h>
17b86ff981SJens Axboe #include <linux/slab.h>
189984de1aSPaul Gortmaker #include <linux/export.h>
19b86ff981SJens Axboe #include <linux/string.h>
20b86ff981SJens Axboe #include <linux/relay.h>
21b86ff981SJens Axboe #include <linux/vmalloc.h>
22b86ff981SJens Axboe #include <linux/mm.h>
2323c88752SMathieu Desnoyers #include <linux/cpu.h>
24d6b29d7cSJens Axboe #include <linux/splice.h>
2523c88752SMathieu Desnoyers 
2623c88752SMathieu Desnoyers /* list of open channels, for cpu hotplug */
2723c88752SMathieu Desnoyers static DEFINE_MUTEX(relay_channels_mutex);
2823c88752SMathieu Desnoyers static LIST_HEAD(relay_channels);
29b86ff981SJens Axboe 
30b86ff981SJens Axboe /*
31b86ff981SJens Axboe  * close() vm_op implementation for relay file mapping.
32b86ff981SJens Axboe  */
33b86ff981SJens Axboe static void relay_file_mmap_close(struct vm_area_struct *vma)
34b86ff981SJens Axboe {
35b86ff981SJens Axboe 	struct rchan_buf *buf = vma->vm_private_data;
36b86ff981SJens Axboe 	buf->chan->cb->buf_unmapped(buf, vma->vm_file);
37b86ff981SJens Axboe }
38b86ff981SJens Axboe 
39b86ff981SJens Axboe /*
40a1e09612SNick Piggin  * fault() vm_op implementation for relay file mapping.
41b86ff981SJens Axboe  */
423fb3894bSSouptick Joarder static vm_fault_t relay_buf_fault(struct vm_fault *vmf)
43b86ff981SJens Axboe {
44b86ff981SJens Axboe 	struct page *page;
4511bac800SDave Jiang 	struct rchan_buf *buf = vmf->vma->vm_private_data;
46a1e09612SNick Piggin 	pgoff_t pgoff = vmf->pgoff;
47b86ff981SJens Axboe 
48b86ff981SJens Axboe 	if (!buf)
49a1e09612SNick Piggin 		return VM_FAULT_OOM;
50b86ff981SJens Axboe 
51a1e09612SNick Piggin 	page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
52b86ff981SJens Axboe 	if (!page)
53a1e09612SNick Piggin 		return VM_FAULT_SIGBUS;
54b86ff981SJens Axboe 	get_page(page);
55a1e09612SNick Piggin 	vmf->page = page;
56b86ff981SJens Axboe 
57a1e09612SNick Piggin 	return 0;
58b86ff981SJens Axboe }
59b86ff981SJens Axboe 
60b86ff981SJens Axboe /*
61b86ff981SJens Axboe  * vm_ops for relay file mappings.
62b86ff981SJens Axboe  */
63f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct relay_file_mmap_ops = {
64a1e09612SNick Piggin 	.fault = relay_buf_fault,
65b86ff981SJens Axboe 	.close = relay_file_mmap_close,
66b86ff981SJens Axboe };
67b86ff981SJens Axboe 
6868ab3d88SMasami Hiramatsu /*
6968ab3d88SMasami Hiramatsu  * allocate an array of pointers of struct page
7068ab3d88SMasami Hiramatsu  */
7168ab3d88SMasami Hiramatsu static struct page **relay_alloc_page_array(unsigned int n_pages)
7268ab3d88SMasami Hiramatsu {
73408af87aSJesper Juhl 	const size_t pa_size = n_pages * sizeof(struct page *);
74408af87aSJesper Juhl 	if (pa_size > PAGE_SIZE)
75408af87aSJesper Juhl 		return vzalloc(pa_size);
76408af87aSJesper Juhl 	return kzalloc(pa_size, GFP_KERNEL);
7768ab3d88SMasami Hiramatsu }
7868ab3d88SMasami Hiramatsu 
7968ab3d88SMasami Hiramatsu /*
8068ab3d88SMasami Hiramatsu  * free an array of pointers of struct page
8168ab3d88SMasami Hiramatsu  */
8268ab3d88SMasami Hiramatsu static void relay_free_page_array(struct page **array)
8368ab3d88SMasami Hiramatsu {
84200f1ce3SPekka Enberg 	kvfree(array);
8568ab3d88SMasami Hiramatsu }
8668ab3d88SMasami Hiramatsu 
87b86ff981SJens Axboe /**
88b86ff981SJens Axboe  *	relay_mmap_buf: - mmap channel buffer to process address space
89b86ff981SJens Axboe  *	@buf: relay channel buffer
90b86ff981SJens Axboe  *	@vma: vm_area_struct describing memory to be mapped
91b86ff981SJens Axboe  *
92b86ff981SJens Axboe  *	Returns 0 if ok, negative on error
93b86ff981SJens Axboe  *
94c1e8d7c6SMichel Lespinasse  *	Caller should already have grabbed mmap_lock.
95b86ff981SJens Axboe  */
9601c55ed3SAdrian Bunk static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
97b86ff981SJens Axboe {
98b86ff981SJens Axboe 	unsigned long length = vma->vm_end - vma->vm_start;
99b86ff981SJens Axboe 	struct file *filp = vma->vm_file;
100b86ff981SJens Axboe 
101b86ff981SJens Axboe 	if (!buf)
102b86ff981SJens Axboe 		return -EBADF;
103b86ff981SJens Axboe 
104b86ff981SJens Axboe 	if (length != (unsigned long)buf->chan->alloc_size)
105b86ff981SJens Axboe 		return -EINVAL;
106b86ff981SJens Axboe 
107b86ff981SJens Axboe 	vma->vm_ops = &relay_file_mmap_ops;
1082f98735cSNick Piggin 	vma->vm_flags |= VM_DONTEXPAND;
109b86ff981SJens Axboe 	vma->vm_private_data = buf;
110b86ff981SJens Axboe 	buf->chan->cb->buf_mapped(buf, filp);
111b86ff981SJens Axboe 
112b86ff981SJens Axboe 	return 0;
113b86ff981SJens Axboe }
114b86ff981SJens Axboe 
115b86ff981SJens Axboe /**
116b86ff981SJens Axboe  *	relay_alloc_buf - allocate a channel buffer
117b86ff981SJens Axboe  *	@buf: the buffer struct
118b86ff981SJens Axboe  *	@size: total size of the buffer
119b86ff981SJens Axboe  *
1204c78a663SRandy Dunlap  *	Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
121221415d7SJens Axboe  *	passed in size will get page aligned, if it isn't already.
122b86ff981SJens Axboe  */
123221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
124b86ff981SJens Axboe {
125b86ff981SJens Axboe 	void *mem;
126b86ff981SJens Axboe 	unsigned int i, j, n_pages;
127b86ff981SJens Axboe 
128221415d7SJens Axboe 	*size = PAGE_ALIGN(*size);
129221415d7SJens Axboe 	n_pages = *size >> PAGE_SHIFT;
130b86ff981SJens Axboe 
13168ab3d88SMasami Hiramatsu 	buf->page_array = relay_alloc_page_array(n_pages);
132b86ff981SJens Axboe 	if (!buf->page_array)
133b86ff981SJens Axboe 		return NULL;
134b86ff981SJens Axboe 
135b86ff981SJens Axboe 	for (i = 0; i < n_pages; i++) {
136b86ff981SJens Axboe 		buf->page_array[i] = alloc_page(GFP_KERNEL);
137b86ff981SJens Axboe 		if (unlikely(!buf->page_array[i]))
138b86ff981SJens Axboe 			goto depopulate;
139ebf99093STom Zanussi 		set_page_private(buf->page_array[i], (unsigned long)buf);
140b86ff981SJens Axboe 	}
141b86ff981SJens Axboe 	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
142b86ff981SJens Axboe 	if (!mem)
143b86ff981SJens Axboe 		goto depopulate;
144b86ff981SJens Axboe 
145221415d7SJens Axboe 	memset(mem, 0, *size);
146b86ff981SJens Axboe 	buf->page_count = n_pages;
147b86ff981SJens Axboe 	return mem;
148b86ff981SJens Axboe 
149b86ff981SJens Axboe depopulate:
150b86ff981SJens Axboe 	for (j = 0; j < i; j++)
151b86ff981SJens Axboe 		__free_page(buf->page_array[j]);
15268ab3d88SMasami Hiramatsu 	relay_free_page_array(buf->page_array);
153b86ff981SJens Axboe 	return NULL;
154b86ff981SJens Axboe }
155b86ff981SJens Axboe 
156b86ff981SJens Axboe /**
157b86ff981SJens Axboe  *	relay_create_buf - allocate and initialize a channel buffer
1584c78a663SRandy Dunlap  *	@chan: the relay channel
159b86ff981SJens Axboe  *
1604c78a663SRandy Dunlap  *	Returns channel buffer if successful, %NULL otherwise.
161b86ff981SJens Axboe  */
16201c55ed3SAdrian Bunk static struct rchan_buf *relay_create_buf(struct rchan *chan)
163b86ff981SJens Axboe {
164f6302f1bSDan Carpenter 	struct rchan_buf *buf;
165f6302f1bSDan Carpenter 
16688913bd8SDavid Rientjes 	if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t *))
167b86ff981SJens Axboe 		return NULL;
168b86ff981SJens Axboe 
169f6302f1bSDan Carpenter 	buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
170f6302f1bSDan Carpenter 	if (!buf)
171f6302f1bSDan Carpenter 		return NULL;
1726da2ec56SKees Cook 	buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t *),
1736da2ec56SKees Cook 				     GFP_KERNEL);
174b86ff981SJens Axboe 	if (!buf->padding)
175b86ff981SJens Axboe 		goto free_buf;
176b86ff981SJens Axboe 
177221415d7SJens Axboe 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
178b86ff981SJens Axboe 	if (!buf->start)
179b86ff981SJens Axboe 		goto free_buf;
180b86ff981SJens Axboe 
181b86ff981SJens Axboe 	buf->chan = chan;
182b86ff981SJens Axboe 	kref_get(&buf->chan->kref);
183b86ff981SJens Axboe 	return buf;
184b86ff981SJens Axboe 
185b86ff981SJens Axboe free_buf:
186b86ff981SJens Axboe 	kfree(buf->padding);
187b86ff981SJens Axboe 	kfree(buf);
188b86ff981SJens Axboe 	return NULL;
189b86ff981SJens Axboe }
190b86ff981SJens Axboe 
191b86ff981SJens Axboe /**
192b86ff981SJens Axboe  *	relay_destroy_channel - free the channel struct
1934c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay channel
194b86ff981SJens Axboe  *
195b86ff981SJens Axboe  *	Should only be called from kref_put().
196b86ff981SJens Axboe  */
19701c55ed3SAdrian Bunk static void relay_destroy_channel(struct kref *kref)
198b86ff981SJens Axboe {
199b86ff981SJens Axboe 	struct rchan *chan = container_of(kref, struct rchan, kref);
20071e84329SWei Yongjun 	free_percpu(chan->buf);
201b86ff981SJens Axboe 	kfree(chan);
202b86ff981SJens Axboe }
203b86ff981SJens Axboe 
204b86ff981SJens Axboe /**
205b86ff981SJens Axboe  *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
206b86ff981SJens Axboe  *	@buf: the buffer struct
207b86ff981SJens Axboe  */
20801c55ed3SAdrian Bunk static void relay_destroy_buf(struct rchan_buf *buf)
209b86ff981SJens Axboe {
210b86ff981SJens Axboe 	struct rchan *chan = buf->chan;
211b86ff981SJens Axboe 	unsigned int i;
212b86ff981SJens Axboe 
213b86ff981SJens Axboe 	if (likely(buf->start)) {
214b86ff981SJens Axboe 		vunmap(buf->start);
215b86ff981SJens Axboe 		for (i = 0; i < buf->page_count; i++)
216b86ff981SJens Axboe 			__free_page(buf->page_array[i]);
21768ab3d88SMasami Hiramatsu 		relay_free_page_array(buf->page_array);
218b86ff981SJens Axboe 	}
219017c59c0SAkash Goel 	*per_cpu_ptr(chan->buf, buf->cpu) = NULL;
220b86ff981SJens Axboe 	kfree(buf->padding);
221b86ff981SJens Axboe 	kfree(buf);
222b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
223b86ff981SJens Axboe }
224b86ff981SJens Axboe 
225b86ff981SJens Axboe /**
226b86ff981SJens Axboe  *	relay_remove_buf - remove a channel buffer
2274c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay buffer
228b86ff981SJens Axboe  *
229e227867fSMasanari Iida  *	Removes the file from the filesystem, which also frees the
230b86ff981SJens Axboe  *	rchan_buf_struct and the channel buffer.  Should only be called from
231b86ff981SJens Axboe  *	kref_put().
232b86ff981SJens Axboe  */
23301c55ed3SAdrian Bunk static void relay_remove_buf(struct kref *kref)
234b86ff981SJens Axboe {
235b86ff981SJens Axboe 	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
236b86ff981SJens Axboe 	relay_destroy_buf(buf);
237b86ff981SJens Axboe }
238b86ff981SJens Axboe 
239b86ff981SJens Axboe /**
240b86ff981SJens Axboe  *	relay_buf_empty - boolean, is the channel buffer empty?
241b86ff981SJens Axboe  *	@buf: channel buffer
242b86ff981SJens Axboe  *
243b86ff981SJens Axboe  *	Returns 1 if the buffer is empty, 0 otherwise.
244b86ff981SJens Axboe  */
24501c55ed3SAdrian Bunk static int relay_buf_empty(struct rchan_buf *buf)
246b86ff981SJens Axboe {
247b86ff981SJens Axboe 	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
248b86ff981SJens Axboe }
249b86ff981SJens Axboe 
250b86ff981SJens Axboe /**
251b86ff981SJens Axboe  *	relay_buf_full - boolean, is the channel buffer full?
252b86ff981SJens Axboe  *	@buf: channel buffer
253b86ff981SJens Axboe  *
254b86ff981SJens Axboe  *	Returns 1 if the buffer is full, 0 otherwise.
255b86ff981SJens Axboe  */
256b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf)
257b86ff981SJens Axboe {
258b86ff981SJens Axboe 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
259b86ff981SJens Axboe 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
260b86ff981SJens Axboe }
261b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full);
262b86ff981SJens Axboe 
263b86ff981SJens Axboe /*
264b86ff981SJens Axboe  * High-level relay kernel API and associated functions.
265b86ff981SJens Axboe  */
266b86ff981SJens Axboe 
267b86ff981SJens Axboe /*
268b86ff981SJens Axboe  * rchan_callback implementations defining default channel behavior.  Used
269b86ff981SJens Axboe  * in place of corresponding NULL values in client callback struct.
270b86ff981SJens Axboe  */
271b86ff981SJens Axboe 
272b86ff981SJens Axboe /*
273b86ff981SJens Axboe  * subbuf_start() default callback.  Does nothing.
274b86ff981SJens Axboe  */
275b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf,
276b86ff981SJens Axboe 					  void *subbuf,
277b86ff981SJens Axboe 					  void *prev_subbuf,
278b86ff981SJens Axboe 					  size_t prev_padding)
279b86ff981SJens Axboe {
280b86ff981SJens Axboe 	if (relay_buf_full(buf))
281b86ff981SJens Axboe 		return 0;
282b86ff981SJens Axboe 
283b86ff981SJens Axboe 	return 1;
284b86ff981SJens Axboe }
285b86ff981SJens Axboe 
286b86ff981SJens Axboe /*
287b86ff981SJens Axboe  * buf_mapped() default callback.  Does nothing.
288b86ff981SJens Axboe  */
289b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf,
290b86ff981SJens Axboe 					struct file *filp)
291b86ff981SJens Axboe {
292b86ff981SJens Axboe }
293b86ff981SJens Axboe 
294b86ff981SJens Axboe /*
295b86ff981SJens Axboe  * buf_unmapped() default callback.  Does nothing.
296b86ff981SJens Axboe  */
297b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf,
298b86ff981SJens Axboe 					  struct file *filp)
299b86ff981SJens Axboe {
300b86ff981SJens Axboe }
301b86ff981SJens Axboe 
302b86ff981SJens Axboe /*
303b86ff981SJens Axboe  * create_buf_file_create() default callback.  Does nothing.
304b86ff981SJens Axboe  */
305b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename,
306b86ff981SJens Axboe 						       struct dentry *parent,
307f4ae40a6SAl Viro 						       umode_t mode,
308b86ff981SJens Axboe 						       struct rchan_buf *buf,
309b86ff981SJens Axboe 						       int *is_global)
310b86ff981SJens Axboe {
311b86ff981SJens Axboe 	return NULL;
312b86ff981SJens Axboe }
313b86ff981SJens Axboe 
314b86ff981SJens Axboe /*
315b86ff981SJens Axboe  * remove_buf_file() default callback.  Does nothing.
316b86ff981SJens Axboe  */
317b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry)
318b86ff981SJens Axboe {
319b86ff981SJens Axboe 	return -EINVAL;
320b86ff981SJens Axboe }
321b86ff981SJens Axboe 
322b86ff981SJens Axboe /* relay channel default callbacks */
323b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = {
324b86ff981SJens Axboe 	.subbuf_start = subbuf_start_default_callback,
325b86ff981SJens Axboe 	.buf_mapped = buf_mapped_default_callback,
326b86ff981SJens Axboe 	.buf_unmapped = buf_unmapped_default_callback,
327b86ff981SJens Axboe 	.create_buf_file = create_buf_file_default_callback,
328b86ff981SJens Axboe 	.remove_buf_file = remove_buf_file_default_callback,
329b86ff981SJens Axboe };
330b86ff981SJens Axboe 
331b86ff981SJens Axboe /**
332b86ff981SJens Axboe  *	wakeup_readers - wake up readers waiting on a channel
33326b5679eSPeter Zijlstra  *	@work: contains the channel buffer
334b86ff981SJens Axboe  *
33526b5679eSPeter Zijlstra  *	This is the function used to defer reader waking
336b86ff981SJens Axboe  */
33726b5679eSPeter Zijlstra static void wakeup_readers(struct irq_work *work)
338b86ff981SJens Axboe {
33926b5679eSPeter Zijlstra 	struct rchan_buf *buf;
34026b5679eSPeter Zijlstra 
34126b5679eSPeter Zijlstra 	buf = container_of(work, struct rchan_buf, wakeup_work);
342b86ff981SJens Axboe 	wake_up_interruptible(&buf->read_wait);
343b86ff981SJens Axboe }
344b86ff981SJens Axboe 
345b86ff981SJens Axboe /**
346b86ff981SJens Axboe  *	__relay_reset - reset a channel buffer
347b86ff981SJens Axboe  *	@buf: the channel buffer
348b86ff981SJens Axboe  *	@init: 1 if this is a first-time initialization
349b86ff981SJens Axboe  *
35072fd4a35SRobert P. J. Day  *	See relay_reset() for description of effect.
351b86ff981SJens Axboe  */
352192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init)
353b86ff981SJens Axboe {
354b86ff981SJens Axboe 	size_t i;
355b86ff981SJens Axboe 
356b86ff981SJens Axboe 	if (init) {
357b86ff981SJens Axboe 		init_waitqueue_head(&buf->read_wait);
358b86ff981SJens Axboe 		kref_init(&buf->kref);
35926b5679eSPeter Zijlstra 		init_irq_work(&buf->wakeup_work, wakeup_readers);
36026b5679eSPeter Zijlstra 	} else {
36126b5679eSPeter Zijlstra 		irq_work_sync(&buf->wakeup_work);
36226b5679eSPeter Zijlstra 	}
363b86ff981SJens Axboe 
364b86ff981SJens Axboe 	buf->subbufs_produced = 0;
365b86ff981SJens Axboe 	buf->subbufs_consumed = 0;
366b86ff981SJens Axboe 	buf->bytes_consumed = 0;
367b86ff981SJens Axboe 	buf->finalized = 0;
368b86ff981SJens Axboe 	buf->data = buf->start;
369b86ff981SJens Axboe 	buf->offset = 0;
370b86ff981SJens Axboe 
371b86ff981SJens Axboe 	for (i = 0; i < buf->chan->n_subbufs; i++)
372b86ff981SJens Axboe 		buf->padding[i] = 0;
373b86ff981SJens Axboe 
374b86ff981SJens Axboe 	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
375b86ff981SJens Axboe }
376b86ff981SJens Axboe 
377b86ff981SJens Axboe /**
378b86ff981SJens Axboe  *	relay_reset - reset the channel
379b86ff981SJens Axboe  *	@chan: the channel
380b86ff981SJens Axboe  *
381b86ff981SJens Axboe  *	This has the effect of erasing all data from all channel buffers
382b86ff981SJens Axboe  *	and restarting the channel in its initial state.  The buffers
383b86ff981SJens Axboe  *	are not freed, so any mappings are still in effect.
384b86ff981SJens Axboe  *
38572fd4a35SRobert P. J. Day  *	NOTE. Care should be taken that the channel isn't actually
386b86ff981SJens Axboe  *	being used by anything when this call is made.
387b86ff981SJens Axboe  */
388b86ff981SJens Axboe void relay_reset(struct rchan *chan)
389b86ff981SJens Axboe {
390017c59c0SAkash Goel 	struct rchan_buf *buf;
391b86ff981SJens Axboe 	unsigned int i;
392b86ff981SJens Axboe 
393b86ff981SJens Axboe 	if (!chan)
394b86ff981SJens Axboe 		return;
395b86ff981SJens Axboe 
396017c59c0SAkash Goel 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
397017c59c0SAkash Goel 		__relay_reset(buf, 0);
39823c88752SMathieu Desnoyers 		return;
399b86ff981SJens Axboe 	}
40023c88752SMathieu Desnoyers 
40123c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
40298ba4031SLai Jiangshan 	for_each_possible_cpu(i)
403017c59c0SAkash Goel 		if ((buf = *per_cpu_ptr(chan->buf, i)))
404017c59c0SAkash Goel 			__relay_reset(buf, 0);
40523c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
406b86ff981SJens Axboe }
407b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset);
408b86ff981SJens Axboe 
40920d8b67cSEduard - Gabriel Munteanu static inline void relay_set_buf_dentry(struct rchan_buf *buf,
41020d8b67cSEduard - Gabriel Munteanu 					struct dentry *dentry)
41120d8b67cSEduard - Gabriel Munteanu {
41220d8b67cSEduard - Gabriel Munteanu 	buf->dentry = dentry;
4137682c918SDavid Howells 	d_inode(buf->dentry)->i_size = buf->early_bytes;
41420d8b67cSEduard - Gabriel Munteanu }
41520d8b67cSEduard - Gabriel Munteanu 
41620d8b67cSEduard - Gabriel Munteanu static struct dentry *relay_create_buf_file(struct rchan *chan,
41720d8b67cSEduard - Gabriel Munteanu 					    struct rchan_buf *buf,
41820d8b67cSEduard - Gabriel Munteanu 					    unsigned int cpu)
41920d8b67cSEduard - Gabriel Munteanu {
42020d8b67cSEduard - Gabriel Munteanu 	struct dentry *dentry;
42120d8b67cSEduard - Gabriel Munteanu 	char *tmpname;
42220d8b67cSEduard - Gabriel Munteanu 
42320d8b67cSEduard - Gabriel Munteanu 	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
42420d8b67cSEduard - Gabriel Munteanu 	if (!tmpname)
42520d8b67cSEduard - Gabriel Munteanu 		return NULL;
42620d8b67cSEduard - Gabriel Munteanu 	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
42720d8b67cSEduard - Gabriel Munteanu 
42820d8b67cSEduard - Gabriel Munteanu 	/* Create file in fs */
42920d8b67cSEduard - Gabriel Munteanu 	dentry = chan->cb->create_buf_file(tmpname, chan->parent,
43020d8b67cSEduard - Gabriel Munteanu 					   S_IRUSR, buf,
43120d8b67cSEduard - Gabriel Munteanu 					   &chan->is_global);
4322c1cf00eSGreg Kroah-Hartman 	if (IS_ERR(dentry))
4332c1cf00eSGreg Kroah-Hartman 		dentry = NULL;
43420d8b67cSEduard - Gabriel Munteanu 
43520d8b67cSEduard - Gabriel Munteanu 	kfree(tmpname);
43620d8b67cSEduard - Gabriel Munteanu 
43720d8b67cSEduard - Gabriel Munteanu 	return dentry;
43820d8b67cSEduard - Gabriel Munteanu }
43920d8b67cSEduard - Gabriel Munteanu 
4404c78a663SRandy Dunlap /*
441b86ff981SJens Axboe  *	relay_open_buf - create a new relay channel buffer
442b86ff981SJens Axboe  *
44323c88752SMathieu Desnoyers  *	used by relay_open() and CPU hotplug.
444b86ff981SJens Axboe  */
44523c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
446b86ff981SJens Axboe {
44723c88752SMathieu Desnoyers  	struct rchan_buf *buf = NULL;
448b86ff981SJens Axboe 	struct dentry *dentry;
449b86ff981SJens Axboe 
45023c88752SMathieu Desnoyers  	if (chan->is_global)
451017c59c0SAkash Goel 		return *per_cpu_ptr(chan->buf, 0);
452b86ff981SJens Axboe 
453b86ff981SJens Axboe 	buf = relay_create_buf(chan);
454b86ff981SJens Axboe 	if (!buf)
45520d8b67cSEduard - Gabriel Munteanu 		return NULL;
45620d8b67cSEduard - Gabriel Munteanu 
45720d8b67cSEduard - Gabriel Munteanu 	if (chan->has_base_filename) {
45820d8b67cSEduard - Gabriel Munteanu 		dentry = relay_create_buf_file(chan, buf, cpu);
45920d8b67cSEduard - Gabriel Munteanu 		if (!dentry)
46020d8b67cSEduard - Gabriel Munteanu 			goto free_buf;
46120d8b67cSEduard - Gabriel Munteanu 		relay_set_buf_dentry(buf, dentry);
46259dbb2a0SAkash Goel 	} else {
46359dbb2a0SAkash Goel 		/* Only retrieve global info, nothing more, nothing less */
46459dbb2a0SAkash Goel 		dentry = chan->cb->create_buf_file(NULL, NULL,
46559dbb2a0SAkash Goel 						   S_IRUSR, buf,
46659dbb2a0SAkash Goel 						   &chan->is_global);
4672c1cf00eSGreg Kroah-Hartman 		if (IS_ERR_OR_NULL(dentry))
46859dbb2a0SAkash Goel 			goto free_buf;
46920d8b67cSEduard - Gabriel Munteanu 	}
470b86ff981SJens Axboe 
47123c88752SMathieu Desnoyers  	buf->cpu = cpu;
472b86ff981SJens Axboe  	__relay_reset(buf, 1);
473b86ff981SJens Axboe 
47423c88752SMathieu Desnoyers  	if(chan->is_global) {
475017c59c0SAkash Goel 		*per_cpu_ptr(chan->buf, 0) = buf;
47623c88752SMathieu Desnoyers  		buf->cpu = 0;
47723c88752SMathieu Desnoyers   	}
47823c88752SMathieu Desnoyers 
47920d8b67cSEduard - Gabriel Munteanu 	return buf;
48023c88752SMathieu Desnoyers 
48123c88752SMathieu Desnoyers free_buf:
48223c88752SMathieu Desnoyers  	relay_destroy_buf(buf);
48320d8b67cSEduard - Gabriel Munteanu 	return NULL;
484b86ff981SJens Axboe }
485b86ff981SJens Axboe 
486b86ff981SJens Axboe /**
487b86ff981SJens Axboe  *	relay_close_buf - close a channel buffer
488b86ff981SJens Axboe  *	@buf: channel buffer
489b86ff981SJens Axboe  *
490b86ff981SJens Axboe  *	Marks the buffer finalized and restores the default callbacks.
491b86ff981SJens Axboe  *	The channel buffer and channel buffer data structure are then freed
492b86ff981SJens Axboe  *	automatically when the last reference is given up.
493b86ff981SJens Axboe  */
494192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf)
495b86ff981SJens Axboe {
496b86ff981SJens Axboe 	buf->finalized = 1;
49726b5679eSPeter Zijlstra 	irq_work_sync(&buf->wakeup_work);
498b8d4a5bfSDmitry Monakhov 	buf->chan->cb->remove_buf_file(buf->dentry);
499b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
500b86ff981SJens Axboe }
501b86ff981SJens Axboe 
502192636adSAndrew Morton static void setup_callbacks(struct rchan *chan,
503b86ff981SJens Axboe 				   struct rchan_callbacks *cb)
504b86ff981SJens Axboe {
505b86ff981SJens Axboe 	if (!cb) {
506b86ff981SJens Axboe 		chan->cb = &default_channel_callbacks;
507b86ff981SJens Axboe 		return;
508b86ff981SJens Axboe 	}
509b86ff981SJens Axboe 
510b86ff981SJens Axboe 	if (!cb->subbuf_start)
511b86ff981SJens Axboe 		cb->subbuf_start = subbuf_start_default_callback;
512b86ff981SJens Axboe 	if (!cb->buf_mapped)
513b86ff981SJens Axboe 		cb->buf_mapped = buf_mapped_default_callback;
514b86ff981SJens Axboe 	if (!cb->buf_unmapped)
515b86ff981SJens Axboe 		cb->buf_unmapped = buf_unmapped_default_callback;
516b86ff981SJens Axboe 	if (!cb->create_buf_file)
517b86ff981SJens Axboe 		cb->create_buf_file = create_buf_file_default_callback;
518b86ff981SJens Axboe 	if (!cb->remove_buf_file)
519b86ff981SJens Axboe 		cb->remove_buf_file = remove_buf_file_default_callback;
520b86ff981SJens Axboe 	chan->cb = cb;
521b86ff981SJens Axboe }
522b86ff981SJens Axboe 
523e6d4989aSRichard Weinberger int relay_prepare_cpu(unsigned int cpu)
52423c88752SMathieu Desnoyers {
52523c88752SMathieu Desnoyers 	struct rchan *chan;
526017c59c0SAkash Goel 	struct rchan_buf *buf;
52723c88752SMathieu Desnoyers 
52823c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
52923c88752SMathieu Desnoyers 	list_for_each_entry(chan, &relay_channels, list) {
530e6d4989aSRichard Weinberger 		if ((buf = *per_cpu_ptr(chan->buf, cpu)))
53123c88752SMathieu Desnoyers 			continue;
532e6d4989aSRichard Weinberger 		buf = relay_open_buf(chan, cpu);
533017c59c0SAkash Goel 		if (!buf) {
534e6d4989aSRichard Weinberger 			pr_err("relay: cpu %d buffer creation failed\n", cpu);
53523c88752SMathieu Desnoyers 			mutex_unlock(&relay_channels_mutex);
536e6d4989aSRichard Weinberger 			return -ENOMEM;
53723c88752SMathieu Desnoyers 		}
538e6d4989aSRichard Weinberger 		*per_cpu_ptr(chan->buf, cpu) = buf;
53923c88752SMathieu Desnoyers 	}
54023c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
541e6d4989aSRichard Weinberger 	return 0;
54223c88752SMathieu Desnoyers }
54323c88752SMathieu Desnoyers 
54423c88752SMathieu Desnoyers /**
545b86ff981SJens Axboe  *	relay_open - create a new relay channel
54620d8b67cSEduard - Gabriel Munteanu  *	@base_filename: base name of files to create, %NULL for buffering only
54720d8b67cSEduard - Gabriel Munteanu  *	@parent: dentry of parent directory, %NULL for root directory or buffer
548b86ff981SJens Axboe  *	@subbuf_size: size of sub-buffers
549b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers
550b86ff981SJens Axboe  *	@cb: client callback functions
55123c88752SMathieu Desnoyers  *	@private_data: user-defined data
552b86ff981SJens Axboe  *
5534c78a663SRandy Dunlap  *	Returns channel pointer if successful, %NULL otherwise.
554b86ff981SJens Axboe  *
555b86ff981SJens Axboe  *	Creates a channel buffer for each cpu using the sizes and
556b86ff981SJens Axboe  *	attributes specified.  The created channel buffer files
557b86ff981SJens Axboe  *	will be named base_filename0...base_filenameN-1.  File
55872fd4a35SRobert P. J. Day  *	permissions will be %S_IRUSR.
55959dbb2a0SAkash Goel  *
56059dbb2a0SAkash Goel  *	If opening a buffer (@parent = NULL) that you later wish to register
56159dbb2a0SAkash Goel  *	in a filesystem, call relay_late_setup_files() once the @parent dentry
56259dbb2a0SAkash Goel  *	is available.
563b86ff981SJens Axboe  */
564b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
565b86ff981SJens Axboe 			 struct dentry *parent,
566b86ff981SJens Axboe 			 size_t subbuf_size,
567b86ff981SJens Axboe 			 size_t n_subbufs,
56823c88752SMathieu Desnoyers 			 struct rchan_callbacks *cb,
56923c88752SMathieu Desnoyers 			 void *private_data)
570b86ff981SJens Axboe {
571b86ff981SJens Axboe 	unsigned int i;
572b86ff981SJens Axboe 	struct rchan *chan;
573017c59c0SAkash Goel 	struct rchan_buf *buf;
574b86ff981SJens Axboe 
575b86ff981SJens Axboe 	if (!(subbuf_size && n_subbufs))
576b86ff981SJens Axboe 		return NULL;
577f6302f1bSDan Carpenter 	if (subbuf_size > UINT_MAX / n_subbufs)
578f6302f1bSDan Carpenter 		return NULL;
579b86ff981SJens Axboe 
580cd861280SRobert P. J. Day 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
581b86ff981SJens Axboe 	if (!chan)
582b86ff981SJens Axboe 		return NULL;
583b86ff981SJens Axboe 
584017c59c0SAkash Goel 	chan->buf = alloc_percpu(struct rchan_buf *);
58554e200abSDaniel Axtens 	if (!chan->buf) {
58654e200abSDaniel Axtens 		kfree(chan);
58754e200abSDaniel Axtens 		return NULL;
58854e200abSDaniel Axtens 	}
58954e200abSDaniel Axtens 
590b86ff981SJens Axboe 	chan->version = RELAYFS_CHANNEL_VERSION;
591b86ff981SJens Axboe 	chan->n_subbufs = n_subbufs;
592b86ff981SJens Axboe 	chan->subbuf_size = subbuf_size;
593a05342cbSzhangwei(Jovi) 	chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
59423c88752SMathieu Desnoyers 	chan->parent = parent;
59523c88752SMathieu Desnoyers 	chan->private_data = private_data;
59620d8b67cSEduard - Gabriel Munteanu 	if (base_filename) {
59720d8b67cSEduard - Gabriel Munteanu 		chan->has_base_filename = 1;
59823c88752SMathieu Desnoyers 		strlcpy(chan->base_filename, base_filename, NAME_MAX);
59920d8b67cSEduard - Gabriel Munteanu 	}
600b86ff981SJens Axboe 	setup_callbacks(chan, cb);
601b86ff981SJens Axboe 	kref_init(&chan->kref);
602b86ff981SJens Axboe 
60323c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
604b86ff981SJens Axboe 	for_each_online_cpu(i) {
605017c59c0SAkash Goel 		buf = relay_open_buf(chan, i);
606017c59c0SAkash Goel 		if (!buf)
607b86ff981SJens Axboe 			goto free_bufs;
608017c59c0SAkash Goel 		*per_cpu_ptr(chan->buf, i) = buf;
609b86ff981SJens Axboe 	}
61023c88752SMathieu Desnoyers 	list_add(&chan->list, &relay_channels);
61123c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
612b86ff981SJens Axboe 
613b86ff981SJens Axboe 	return chan;
614b86ff981SJens Axboe 
615b86ff981SJens Axboe free_bufs:
61698ba4031SLai Jiangshan 	for_each_possible_cpu(i) {
617017c59c0SAkash Goel 		if ((buf = *per_cpu_ptr(chan->buf, i)))
618017c59c0SAkash Goel 			relay_close_buf(buf);
619b86ff981SJens Axboe 	}
620b86ff981SJens Axboe 
621b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
62223c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
623b86ff981SJens Axboe 	return NULL;
624b86ff981SJens Axboe }
625b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open);
626b86ff981SJens Axboe 
62720d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher {
62820d8b67cSEduard - Gabriel Munteanu 	struct rchan_buf *buf;
62920d8b67cSEduard - Gabriel Munteanu 	struct dentry *dentry;
63020d8b67cSEduard - Gabriel Munteanu };
63120d8b67cSEduard - Gabriel Munteanu 
63220d8b67cSEduard - Gabriel Munteanu /* Called in atomic context. */
63320d8b67cSEduard - Gabriel Munteanu static void __relay_set_buf_dentry(void *info)
63420d8b67cSEduard - Gabriel Munteanu {
63520d8b67cSEduard - Gabriel Munteanu 	struct rchan_percpu_buf_dispatcher *p = info;
63620d8b67cSEduard - Gabriel Munteanu 
63720d8b67cSEduard - Gabriel Munteanu 	relay_set_buf_dentry(p->buf, p->dentry);
63820d8b67cSEduard - Gabriel Munteanu }
63920d8b67cSEduard - Gabriel Munteanu 
64020d8b67cSEduard - Gabriel Munteanu /**
64120d8b67cSEduard - Gabriel Munteanu  *	relay_late_setup_files - triggers file creation
64220d8b67cSEduard - Gabriel Munteanu  *	@chan: channel to operate on
64320d8b67cSEduard - Gabriel Munteanu  *	@base_filename: base name of files to create
64420d8b67cSEduard - Gabriel Munteanu  *	@parent: dentry of parent directory, %NULL for root directory
64520d8b67cSEduard - Gabriel Munteanu  *
64620d8b67cSEduard - Gabriel Munteanu  *	Returns 0 if successful, non-zero otherwise.
64720d8b67cSEduard - Gabriel Munteanu  *
64859dbb2a0SAkash Goel  *	Use to setup files for a previously buffer-only channel created
64959dbb2a0SAkash Goel  *	by relay_open() with a NULL parent dentry.
65059dbb2a0SAkash Goel  *
65159dbb2a0SAkash Goel  *	For example, this is useful for perfomring early tracing in kernel,
65259dbb2a0SAkash Goel  *	before VFS is up and then exposing the early results once the dentry
65359dbb2a0SAkash Goel  *	is available.
65420d8b67cSEduard - Gabriel Munteanu  */
65520d8b67cSEduard - Gabriel Munteanu int relay_late_setup_files(struct rchan *chan,
65620d8b67cSEduard - Gabriel Munteanu 			   const char *base_filename,
65720d8b67cSEduard - Gabriel Munteanu 			   struct dentry *parent)
65820d8b67cSEduard - Gabriel Munteanu {
65920d8b67cSEduard - Gabriel Munteanu 	int err = 0;
66020d8b67cSEduard - Gabriel Munteanu 	unsigned int i, curr_cpu;
66120d8b67cSEduard - Gabriel Munteanu 	unsigned long flags;
66220d8b67cSEduard - Gabriel Munteanu 	struct dentry *dentry;
663017c59c0SAkash Goel 	struct rchan_buf *buf;
66420d8b67cSEduard - Gabriel Munteanu 	struct rchan_percpu_buf_dispatcher disp;
66520d8b67cSEduard - Gabriel Munteanu 
66620d8b67cSEduard - Gabriel Munteanu 	if (!chan || !base_filename)
66720d8b67cSEduard - Gabriel Munteanu 		return -EINVAL;
66820d8b67cSEduard - Gabriel Munteanu 
66920d8b67cSEduard - Gabriel Munteanu 	strlcpy(chan->base_filename, base_filename, NAME_MAX);
67020d8b67cSEduard - Gabriel Munteanu 
67120d8b67cSEduard - Gabriel Munteanu 	mutex_lock(&relay_channels_mutex);
67220d8b67cSEduard - Gabriel Munteanu 	/* Is chan already set up? */
673b786c6a9SJiri Slaby 	if (unlikely(chan->has_base_filename)) {
674b786c6a9SJiri Slaby 		mutex_unlock(&relay_channels_mutex);
67520d8b67cSEduard - Gabriel Munteanu 		return -EEXIST;
676b786c6a9SJiri Slaby 	}
67720d8b67cSEduard - Gabriel Munteanu 	chan->has_base_filename = 1;
67820d8b67cSEduard - Gabriel Munteanu 	chan->parent = parent;
67959dbb2a0SAkash Goel 
68059dbb2a0SAkash Goel 	if (chan->is_global) {
68159dbb2a0SAkash Goel 		err = -EINVAL;
682017c59c0SAkash Goel 		buf = *per_cpu_ptr(chan->buf, 0);
683017c59c0SAkash Goel 		if (!WARN_ON_ONCE(!buf)) {
684017c59c0SAkash Goel 			dentry = relay_create_buf_file(chan, buf, 0);
68559dbb2a0SAkash Goel 			if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
686017c59c0SAkash Goel 				relay_set_buf_dentry(buf, dentry);
68759dbb2a0SAkash Goel 				err = 0;
68859dbb2a0SAkash Goel 			}
68959dbb2a0SAkash Goel 		}
69059dbb2a0SAkash Goel 		mutex_unlock(&relay_channels_mutex);
69159dbb2a0SAkash Goel 		return err;
69259dbb2a0SAkash Goel 	}
69359dbb2a0SAkash Goel 
69420d8b67cSEduard - Gabriel Munteanu 	curr_cpu = get_cpu();
69520d8b67cSEduard - Gabriel Munteanu 	/*
69620d8b67cSEduard - Gabriel Munteanu 	 * The CPU hotplug notifier ran before us and created buffers with
69720d8b67cSEduard - Gabriel Munteanu 	 * no files associated. So it's safe to call relay_setup_buf_file()
69820d8b67cSEduard - Gabriel Munteanu 	 * on all currently online CPUs.
69920d8b67cSEduard - Gabriel Munteanu 	 */
70020d8b67cSEduard - Gabriel Munteanu 	for_each_online_cpu(i) {
701017c59c0SAkash Goel 		buf = *per_cpu_ptr(chan->buf, i);
702017c59c0SAkash Goel 		if (unlikely(!buf)) {
7037a51cffbSIngo Molnar 			WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
70420d8b67cSEduard - Gabriel Munteanu 			err = -EINVAL;
70520d8b67cSEduard - Gabriel Munteanu 			break;
70620d8b67cSEduard - Gabriel Munteanu 		}
70720d8b67cSEduard - Gabriel Munteanu 
708017c59c0SAkash Goel 		dentry = relay_create_buf_file(chan, buf, i);
70920d8b67cSEduard - Gabriel Munteanu 		if (unlikely(!dentry)) {
71020d8b67cSEduard - Gabriel Munteanu 			err = -EINVAL;
71120d8b67cSEduard - Gabriel Munteanu 			break;
71220d8b67cSEduard - Gabriel Munteanu 		}
71320d8b67cSEduard - Gabriel Munteanu 
71420d8b67cSEduard - Gabriel Munteanu 		if (curr_cpu == i) {
71520d8b67cSEduard - Gabriel Munteanu 			local_irq_save(flags);
716017c59c0SAkash Goel 			relay_set_buf_dentry(buf, dentry);
71720d8b67cSEduard - Gabriel Munteanu 			local_irq_restore(flags);
71820d8b67cSEduard - Gabriel Munteanu 		} else {
719017c59c0SAkash Goel 			disp.buf = buf;
72020d8b67cSEduard - Gabriel Munteanu 			disp.dentry = dentry;
72120d8b67cSEduard - Gabriel Munteanu 			smp_mb();
72220d8b67cSEduard - Gabriel Munteanu 			/* relay_channels_mutex must be held, so wait. */
72320d8b67cSEduard - Gabriel Munteanu 			err = smp_call_function_single(i,
72420d8b67cSEduard - Gabriel Munteanu 						       __relay_set_buf_dentry,
72520d8b67cSEduard - Gabriel Munteanu 						       &disp, 1);
72620d8b67cSEduard - Gabriel Munteanu 		}
72720d8b67cSEduard - Gabriel Munteanu 		if (unlikely(err))
72820d8b67cSEduard - Gabriel Munteanu 			break;
72920d8b67cSEduard - Gabriel Munteanu 	}
73020d8b67cSEduard - Gabriel Munteanu 	put_cpu();
73120d8b67cSEduard - Gabriel Munteanu 	mutex_unlock(&relay_channels_mutex);
73220d8b67cSEduard - Gabriel Munteanu 
73320d8b67cSEduard - Gabriel Munteanu 	return err;
73420d8b67cSEduard - Gabriel Munteanu }
73559dbb2a0SAkash Goel EXPORT_SYMBOL_GPL(relay_late_setup_files);
73620d8b67cSEduard - Gabriel Munteanu 
737b86ff981SJens Axboe /**
738b86ff981SJens Axboe  *	relay_switch_subbuf - switch to a new sub-buffer
739b86ff981SJens Axboe  *	@buf: channel buffer
740b86ff981SJens Axboe  *	@length: size of current event
741b86ff981SJens Axboe  *
742b86ff981SJens Axboe  *	Returns either the length passed in or 0 if full.
743b86ff981SJens Axboe  *
744b86ff981SJens Axboe  *	Performs sub-buffer-switch tasks such as invoking callbacks,
745b86ff981SJens Axboe  *	updating padding counts, waking up readers, etc.
746b86ff981SJens Axboe  */
747b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
748b86ff981SJens Axboe {
749b86ff981SJens Axboe 	void *old, *new;
750b86ff981SJens Axboe 	size_t old_subbuf, new_subbuf;
751b86ff981SJens Axboe 
752b86ff981SJens Axboe 	if (unlikely(length > buf->chan->subbuf_size))
753b86ff981SJens Axboe 		goto toobig;
754b86ff981SJens Axboe 
755b86ff981SJens Axboe 	if (buf->offset != buf->chan->subbuf_size + 1) {
756b86ff981SJens Axboe 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
757b86ff981SJens Axboe 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
758b86ff981SJens Axboe 		buf->padding[old_subbuf] = buf->prev_padding;
759b86ff981SJens Axboe 		buf->subbufs_produced++;
76020d8b67cSEduard - Gabriel Munteanu 		if (buf->dentry)
7617682c918SDavid Howells 			d_inode(buf->dentry)->i_size +=
76220d8b67cSEduard - Gabriel Munteanu 				buf->chan->subbuf_size -
76320d8b67cSEduard - Gabriel Munteanu 				buf->padding[old_subbuf];
76420d8b67cSEduard - Gabriel Munteanu 		else
76520d8b67cSEduard - Gabriel Munteanu 			buf->early_bytes += buf->chan->subbuf_size -
766221415d7SJens Axboe 					    buf->padding[old_subbuf];
767221415d7SJens Axboe 		smp_mb();
76826b5679eSPeter Zijlstra 		if (waitqueue_active(&buf->read_wait)) {
7697c9cb383STom Zanussi 			/*
7707c9cb383STom Zanussi 			 * Calling wake_up_interruptible() from here
7717c9cb383STom Zanussi 			 * will deadlock if we happen to be logging
7727c9cb383STom Zanussi 			 * from the scheduler (trying to re-grab
7737c9cb383STom Zanussi 			 * rq->lock), so defer it.
7747c9cb383STom Zanussi 			 */
77526b5679eSPeter Zijlstra 			irq_work_queue(&buf->wakeup_work);
77626b5679eSPeter Zijlstra 		}
777b86ff981SJens Axboe 	}
778b86ff981SJens Axboe 
779b86ff981SJens Axboe 	old = buf->data;
780b86ff981SJens Axboe 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
781b86ff981SJens Axboe 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
782b86ff981SJens Axboe 	buf->offset = 0;
783b86ff981SJens Axboe 	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
784b86ff981SJens Axboe 		buf->offset = buf->chan->subbuf_size + 1;
785b86ff981SJens Axboe 		return 0;
786b86ff981SJens Axboe 	}
787b86ff981SJens Axboe 	buf->data = new;
788b86ff981SJens Axboe 	buf->padding[new_subbuf] = 0;
789b86ff981SJens Axboe 
790b86ff981SJens Axboe 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
791b86ff981SJens Axboe 		goto toobig;
792b86ff981SJens Axboe 
793b86ff981SJens Axboe 	return length;
794b86ff981SJens Axboe 
795b86ff981SJens Axboe toobig:
796b86ff981SJens Axboe 	buf->chan->last_toobig = length;
797b86ff981SJens Axboe 	return 0;
798b86ff981SJens Axboe }
799b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf);
800b86ff981SJens Axboe 
801b86ff981SJens Axboe /**
802b86ff981SJens Axboe  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
803b86ff981SJens Axboe  *	@chan: the channel
804b86ff981SJens Axboe  *	@cpu: the cpu associated with the channel buffer to update
805b86ff981SJens Axboe  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
806b86ff981SJens Axboe  *
807b86ff981SJens Axboe  *	Adds to the channel buffer's consumed sub-buffer count.
808b86ff981SJens Axboe  *	subbufs_consumed should be the number of sub-buffers newly consumed,
809b86ff981SJens Axboe  *	not the total consumed.
810b86ff981SJens Axboe  *
81172fd4a35SRobert P. J. Day  *	NOTE. Kernel clients don't need to call this function if the channel
812b86ff981SJens Axboe  *	mode is 'overwrite'.
813b86ff981SJens Axboe  */
814b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan,
815b86ff981SJens Axboe 			    unsigned int cpu,
816b86ff981SJens Axboe 			    size_t subbufs_consumed)
817b86ff981SJens Axboe {
818b86ff981SJens Axboe 	struct rchan_buf *buf;
819b86ff981SJens Axboe 
8209a29d0fbSDan Carpenter 	if (!chan || cpu >= NR_CPUS)
821b86ff981SJens Axboe 		return;
822b86ff981SJens Axboe 
823017c59c0SAkash Goel 	buf = *per_cpu_ptr(chan->buf, cpu);
8249a29d0fbSDan Carpenter 	if (!buf || subbufs_consumed > chan->n_subbufs)
825b86ff981SJens Axboe 		return;
826b86ff981SJens Axboe 
8272c53d910SAravind Srinivasan 	if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
828b86ff981SJens Axboe 		buf->subbufs_consumed = buf->subbufs_produced;
8292c53d910SAravind Srinivasan 	else
8302c53d910SAravind Srinivasan 		buf->subbufs_consumed += subbufs_consumed;
831b86ff981SJens Axboe }
832b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
833b86ff981SJens Axboe 
834b86ff981SJens Axboe /**
835b86ff981SJens Axboe  *	relay_close - close the channel
836b86ff981SJens Axboe  *	@chan: the channel
837b86ff981SJens Axboe  *
838b86ff981SJens Axboe  *	Closes all channel buffers and frees the channel.
839b86ff981SJens Axboe  */
840b86ff981SJens Axboe void relay_close(struct rchan *chan)
841b86ff981SJens Axboe {
842017c59c0SAkash Goel 	struct rchan_buf *buf;
843b86ff981SJens Axboe 	unsigned int i;
844b86ff981SJens Axboe 
845b86ff981SJens Axboe 	if (!chan)
846b86ff981SJens Axboe 		return;
847b86ff981SJens Axboe 
84823c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
849017c59c0SAkash Goel 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
850017c59c0SAkash Goel 		relay_close_buf(buf);
85123c88752SMathieu Desnoyers 	else
85223c88752SMathieu Desnoyers 		for_each_possible_cpu(i)
853017c59c0SAkash Goel 			if ((buf = *per_cpu_ptr(chan->buf, i)))
854017c59c0SAkash Goel 				relay_close_buf(buf);
855b86ff981SJens Axboe 
856b86ff981SJens Axboe 	if (chan->last_toobig)
857b86ff981SJens Axboe 		printk(KERN_WARNING "relay: one or more items not logged "
8585b5e0928SAlexey Dobriyan 		       "[item size (%zd) > sub-buffer size (%zd)]\n",
859b86ff981SJens Axboe 		       chan->last_toobig, chan->subbuf_size);
860b86ff981SJens Axboe 
86123c88752SMathieu Desnoyers 	list_del(&chan->list);
862b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
86323c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
864b86ff981SJens Axboe }
865b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close);
866b86ff981SJens Axboe 
867b86ff981SJens Axboe /**
868b86ff981SJens Axboe  *	relay_flush - close the channel
869b86ff981SJens Axboe  *	@chan: the channel
870b86ff981SJens Axboe  *
8714c78a663SRandy Dunlap  *	Flushes all channel buffers, i.e. forces buffer switch.
872b86ff981SJens Axboe  */
873b86ff981SJens Axboe void relay_flush(struct rchan *chan)
874b86ff981SJens Axboe {
875017c59c0SAkash Goel 	struct rchan_buf *buf;
876b86ff981SJens Axboe 	unsigned int i;
877b86ff981SJens Axboe 
878b86ff981SJens Axboe 	if (!chan)
879b86ff981SJens Axboe 		return;
880b86ff981SJens Axboe 
881017c59c0SAkash Goel 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
882017c59c0SAkash Goel 		relay_switch_subbuf(buf, 0);
88323c88752SMathieu Desnoyers 		return;
884b86ff981SJens Axboe 	}
88523c88752SMathieu Desnoyers 
88623c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
88723c88752SMathieu Desnoyers 	for_each_possible_cpu(i)
888017c59c0SAkash Goel 		if ((buf = *per_cpu_ptr(chan->buf, i)))
889017c59c0SAkash Goel 			relay_switch_subbuf(buf, 0);
89023c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
891b86ff981SJens Axboe }
892b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush);
893b86ff981SJens Axboe 
894b86ff981SJens Axboe /**
895b86ff981SJens Axboe  *	relay_file_open - open file op for relay files
896b86ff981SJens Axboe  *	@inode: the inode
897b86ff981SJens Axboe  *	@filp: the file
898b86ff981SJens Axboe  *
899b86ff981SJens Axboe  *	Increments the channel buffer refcount.
900b86ff981SJens Axboe  */
901b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp)
902b86ff981SJens Axboe {
9038e18e294STheodore Ts'o 	struct rchan_buf *buf = inode->i_private;
904b86ff981SJens Axboe 	kref_get(&buf->kref);
905b86ff981SJens Axboe 	filp->private_data = buf;
906b86ff981SJens Axboe 
90737529fe9SLai Jiangshan 	return nonseekable_open(inode, filp);
908b86ff981SJens Axboe }
909b86ff981SJens Axboe 
910b86ff981SJens Axboe /**
911b86ff981SJens Axboe  *	relay_file_mmap - mmap file op for relay files
912b86ff981SJens Axboe  *	@filp: the file
913b86ff981SJens Axboe  *	@vma: the vma describing what to map
914b86ff981SJens Axboe  *
91572fd4a35SRobert P. J. Day  *	Calls upon relay_mmap_buf() to map the file into user space.
916b86ff981SJens Axboe  */
917b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
918b86ff981SJens Axboe {
919b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
920b86ff981SJens Axboe 	return relay_mmap_buf(buf, vma);
921b86ff981SJens Axboe }
922b86ff981SJens Axboe 
923b86ff981SJens Axboe /**
924b86ff981SJens Axboe  *	relay_file_poll - poll file op for relay files
925b86ff981SJens Axboe  *	@filp: the file
926b86ff981SJens Axboe  *	@wait: poll table
927b86ff981SJens Axboe  *
928b86ff981SJens Axboe  *	Poll implemention.
929b86ff981SJens Axboe  */
9309dd95748SAl Viro static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
931b86ff981SJens Axboe {
9329dd95748SAl Viro 	__poll_t mask = 0;
933b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
934b86ff981SJens Axboe 
935b86ff981SJens Axboe 	if (buf->finalized)
936a9a08845SLinus Torvalds 		return EPOLLERR;
937b86ff981SJens Axboe 
938b86ff981SJens Axboe 	if (filp->f_mode & FMODE_READ) {
939b86ff981SJens Axboe 		poll_wait(filp, &buf->read_wait, wait);
940b86ff981SJens Axboe 		if (!relay_buf_empty(buf))
941a9a08845SLinus Torvalds 			mask |= EPOLLIN | EPOLLRDNORM;
942b86ff981SJens Axboe 	}
943b86ff981SJens Axboe 
944b86ff981SJens Axboe 	return mask;
945b86ff981SJens Axboe }
946b86ff981SJens Axboe 
947b86ff981SJens Axboe /**
948b86ff981SJens Axboe  *	relay_file_release - release file op for relay files
949b86ff981SJens Axboe  *	@inode: the inode
950b86ff981SJens Axboe  *	@filp: the file
951b86ff981SJens Axboe  *
952b86ff981SJens Axboe  *	Decrements the channel refcount, as the filesystem is
953b86ff981SJens Axboe  *	no longer using it.
954b86ff981SJens Axboe  */
955b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp)
956b86ff981SJens Axboe {
957b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
958b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
959b86ff981SJens Axboe 
960b86ff981SJens Axboe 	return 0;
961b86ff981SJens Axboe }
962b86ff981SJens Axboe 
9634c78a663SRandy Dunlap /*
964b86ff981SJens Axboe  *	relay_file_read_consume - update the consumed count for the buffer
965b86ff981SJens Axboe  */
966b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf,
967b86ff981SJens Axboe 				    size_t read_pos,
968b86ff981SJens Axboe 				    size_t bytes_consumed)
969b86ff981SJens Axboe {
970b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
971b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
972b86ff981SJens Axboe 	size_t read_subbuf;
973b86ff981SJens Axboe 
97432194450STom Zanussi 	if (buf->subbufs_produced == buf->subbufs_consumed &&
97532194450STom Zanussi 	    buf->offset == buf->bytes_consumed)
97632194450STom Zanussi 		return;
97732194450STom Zanussi 
978b86ff981SJens Axboe 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
979b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
980b86ff981SJens Axboe 		buf->bytes_consumed = 0;
981b86ff981SJens Axboe 	}
982b86ff981SJens Axboe 
983b86ff981SJens Axboe 	buf->bytes_consumed += bytes_consumed;
984a66e356cSMasami Hiramatsu 	if (!read_pos)
985a66e356cSMasami Hiramatsu 		read_subbuf = buf->subbufs_consumed % n_subbufs;
986a66e356cSMasami Hiramatsu 	else
987b86ff981SJens Axboe 		read_subbuf = read_pos / buf->chan->subbuf_size;
988b86ff981SJens Axboe 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
989b86ff981SJens Axboe 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
990b86ff981SJens Axboe 		    (buf->offset == subbuf_size))
991b86ff981SJens Axboe 			return;
992b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
993b86ff981SJens Axboe 		buf->bytes_consumed = 0;
994b86ff981SJens Axboe 	}
995b86ff981SJens Axboe }
996b86ff981SJens Axboe 
9974c78a663SRandy Dunlap /*
998b86ff981SJens Axboe  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
999b86ff981SJens Axboe  */
1000341a7213SPengcheng Yang static int relay_file_read_avail(struct rchan_buf *buf)
1001b86ff981SJens Axboe {
1002b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
1003b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
1004221415d7SJens Axboe 	size_t produced = buf->subbufs_produced;
1005*ac05b7a1SSudip Mukherjee 	size_t consumed;
1006b86ff981SJens Axboe 
1007341a7213SPengcheng Yang 	relay_file_read_consume(buf, 0, 0);
1008b86ff981SJens Axboe 
100932194450STom Zanussi 	consumed = buf->subbufs_consumed;
101032194450STom Zanussi 
1011221415d7SJens Axboe 	if (unlikely(buf->offset > subbuf_size)) {
1012221415d7SJens Axboe 		if (produced == consumed)
1013221415d7SJens Axboe 			return 0;
1014221415d7SJens Axboe 		return 1;
1015221415d7SJens Axboe 	}
1016221415d7SJens Axboe 
1017221415d7SJens Axboe 	if (unlikely(produced - consumed >= n_subbufs)) {
1018a66e356cSMasami Hiramatsu 		consumed = produced - n_subbufs + 1;
1019221415d7SJens Axboe 		buf->subbufs_consumed = consumed;
1020a66e356cSMasami Hiramatsu 		buf->bytes_consumed = 0;
1021221415d7SJens Axboe 	}
1022221415d7SJens Axboe 
1023221415d7SJens Axboe 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
1024221415d7SJens Axboe 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
1025221415d7SJens Axboe 
1026221415d7SJens Axboe 	if (consumed > produced)
1027221415d7SJens Axboe 		produced += n_subbufs * subbuf_size;
1028221415d7SJens Axboe 
102932194450STom Zanussi 	if (consumed == produced) {
103032194450STom Zanussi 		if (buf->offset == subbuf_size &&
103132194450STom Zanussi 		    buf->subbufs_produced > buf->subbufs_consumed)
103232194450STom Zanussi 			return 1;
1033221415d7SJens Axboe 		return 0;
103432194450STom Zanussi 	}
1035221415d7SJens Axboe 
1036b86ff981SJens Axboe 	return 1;
1037b86ff981SJens Axboe }
1038b86ff981SJens Axboe 
1039b86ff981SJens Axboe /**
1040b86ff981SJens Axboe  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
10414c78a663SRandy Dunlap  *	@read_pos: file read position
10424c78a663SRandy Dunlap  *	@buf: relay channel buffer
1043b86ff981SJens Axboe  */
1044b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos,
1045b86ff981SJens Axboe 					   struct rchan_buf *buf)
1046b86ff981SJens Axboe {
1047b86ff981SJens Axboe 	size_t padding, avail = 0;
1048b86ff981SJens Axboe 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
1049b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
1050b86ff981SJens Axboe 
1051b86ff981SJens Axboe 	write_subbuf = (buf->data - buf->start) / subbuf_size;
1052b86ff981SJens Axboe 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
1053b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
1054b86ff981SJens Axboe 	read_offset = read_pos % subbuf_size;
1055b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
1056b86ff981SJens Axboe 
1057b86ff981SJens Axboe 	if (read_subbuf == write_subbuf) {
1058b86ff981SJens Axboe 		if (read_offset + padding < write_offset)
1059b86ff981SJens Axboe 			avail = write_offset - (read_offset + padding);
1060b86ff981SJens Axboe 	} else
1061b86ff981SJens Axboe 		avail = (subbuf_size - padding) - read_offset;
1062b86ff981SJens Axboe 
1063b86ff981SJens Axboe 	return avail;
1064b86ff981SJens Axboe }
1065b86ff981SJens Axboe 
1066b86ff981SJens Axboe /**
1067b86ff981SJens Axboe  *	relay_file_read_start_pos - find the first available byte to read
10684c78a663SRandy Dunlap  *	@buf: relay channel buffer
1069b86ff981SJens Axboe  *
1070341a7213SPengcheng Yang  *	If the read_pos is in the middle of padding, return the
1071b86ff981SJens Axboe  *	position of the first actually available byte, otherwise
1072b86ff981SJens Axboe  *	return the original value.
1073b86ff981SJens Axboe  */
1074341a7213SPengcheng Yang static size_t relay_file_read_start_pos(struct rchan_buf *buf)
1075b86ff981SJens Axboe {
1076b86ff981SJens Axboe 	size_t read_subbuf, padding, padding_start, padding_end;
1077b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
1078b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
10798d62fdebSDavid Wilder 	size_t consumed = buf->subbufs_consumed % n_subbufs;
1080341a7213SPengcheng Yang 	size_t read_pos = consumed * subbuf_size + buf->bytes_consumed;
1081b86ff981SJens Axboe 
1082b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
1083b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
1084b86ff981SJens Axboe 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
1085b86ff981SJens Axboe 	padding_end = (read_subbuf + 1) * subbuf_size;
1086b86ff981SJens Axboe 	if (read_pos >= padding_start && read_pos < padding_end) {
1087b86ff981SJens Axboe 		read_subbuf = (read_subbuf + 1) % n_subbufs;
1088b86ff981SJens Axboe 		read_pos = read_subbuf * subbuf_size;
1089b86ff981SJens Axboe 	}
1090b86ff981SJens Axboe 
1091b86ff981SJens Axboe 	return read_pos;
1092b86ff981SJens Axboe }
1093b86ff981SJens Axboe 
1094b86ff981SJens Axboe /**
1095b86ff981SJens Axboe  *	relay_file_read_end_pos - return the new read position
10964c78a663SRandy Dunlap  *	@read_pos: file read position
10974c78a663SRandy Dunlap  *	@buf: relay channel buffer
10984c78a663SRandy Dunlap  *	@count: number of bytes to be read
1099b86ff981SJens Axboe  */
1100b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf,
1101b86ff981SJens Axboe 				      size_t read_pos,
1102b86ff981SJens Axboe 				      size_t count)
1103b86ff981SJens Axboe {
1104b86ff981SJens Axboe 	size_t read_subbuf, padding, end_pos;
1105b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
1106b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
1107b86ff981SJens Axboe 
1108b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
1109b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
1110b86ff981SJens Axboe 	if (read_pos % subbuf_size + count + padding == subbuf_size)
1111b86ff981SJens Axboe 		end_pos = (read_subbuf + 1) * subbuf_size;
1112b86ff981SJens Axboe 	else
1113b86ff981SJens Axboe 		end_pos = read_pos + count;
1114b86ff981SJens Axboe 	if (end_pos >= subbuf_size * n_subbufs)
1115b86ff981SJens Axboe 		end_pos = 0;
1116b86ff981SJens Axboe 
1117b86ff981SJens Axboe 	return end_pos;
1118b86ff981SJens Axboe }
1119b86ff981SJens Axboe 
1120a7c22421SAl Viro static ssize_t relay_file_read(struct file *filp,
1121a7c22421SAl Viro 			       char __user *buffer,
1122a7c22421SAl Viro 			       size_t count,
1123a7c22421SAl Viro 			       loff_t *ppos)
1124221415d7SJens Axboe {
1125221415d7SJens Axboe 	struct rchan_buf *buf = filp->private_data;
1126221415d7SJens Axboe 	size_t read_start, avail;
1127a7c22421SAl Viro 	size_t written = 0;
11286dac40a7STom Zanussi 	int ret;
1129221415d7SJens Axboe 
1130a7c22421SAl Viro 	if (!count)
1131221415d7SJens Axboe 		return 0;
1132221415d7SJens Axboe 
11335955102cSAl Viro 	inode_lock(file_inode(filp));
11346dac40a7STom Zanussi 	do {
1135a7c22421SAl Viro 		void *from;
1136a7c22421SAl Viro 
1137341a7213SPengcheng Yang 		if (!relay_file_read_avail(buf))
1138221415d7SJens Axboe 			break;
1139221415d7SJens Axboe 
1140341a7213SPengcheng Yang 		read_start = relay_file_read_start_pos(buf);
11416dac40a7STom Zanussi 		avail = relay_file_read_subbuf_avail(read_start, buf);
11426dac40a7STom Zanussi 		if (!avail)
11436dac40a7STom Zanussi 			break;
1144221415d7SJens Axboe 
1145a7c22421SAl Viro 		avail = min(count, avail);
1146a7c22421SAl Viro 		from = buf->start + read_start;
1147a7c22421SAl Viro 		ret = avail;
1148a7c22421SAl Viro 		if (copy_to_user(buffer, from, avail))
11496dac40a7STom Zanussi 			break;
11506dac40a7STom Zanussi 
1151a7c22421SAl Viro 		buffer += ret;
1152a7c22421SAl Viro 		written += ret;
1153a7c22421SAl Viro 		count -= ret;
1154a7c22421SAl Viro 
1155221415d7SJens Axboe 		relay_file_read_consume(buf, read_start, ret);
1156221415d7SJens Axboe 		*ppos = relay_file_read_end_pos(buf, read_start, ret);
1157a7c22421SAl Viro 	} while (count);
11585955102cSAl Viro 	inode_unlock(file_inode(filp));
1159221415d7SJens Axboe 
1160a7c22421SAl Viro 	return written;
11616dac40a7STom Zanussi }
11626dac40a7STom Zanussi 
11631db60cf2SJens Axboe static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
11641db60cf2SJens Axboe {
11651db60cf2SJens Axboe 	rbuf->bytes_consumed += bytes_consumed;
11661db60cf2SJens Axboe 
11671db60cf2SJens Axboe 	if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
11681db60cf2SJens Axboe 		relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
11691db60cf2SJens Axboe 		rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
11701db60cf2SJens Axboe 	}
11711db60cf2SJens Axboe }
11721db60cf2SJens Axboe 
1173ebf99093STom Zanussi static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1174ebf99093STom Zanussi 				   struct pipe_buffer *buf)
1175221415d7SJens Axboe {
1176ebf99093STom Zanussi 	struct rchan_buf *rbuf;
1177ebf99093STom Zanussi 
1178ebf99093STom Zanussi 	rbuf = (struct rchan_buf *)page_private(buf->page);
11791db60cf2SJens Axboe 	relay_consume_bytes(rbuf, buf->private);
1180ebf99093STom Zanussi }
1181ebf99093STom Zanussi 
118228dfef8fSAlexey Dobriyan static const struct pipe_buf_operations relay_pipe_buf_ops = {
1183ebf99093STom Zanussi 	.release	= relay_pipe_buf_release,
1184c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
1185ebf99093STom Zanussi 	.get		= generic_pipe_buf_get,
1186ebf99093STom Zanussi };
1187ebf99093STom Zanussi 
11885eb7f9faSJens Axboe static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
11895eb7f9faSJens Axboe {
11905eb7f9faSJens Axboe }
11915eb7f9faSJens Axboe 
1192d3f35d98STom Zanussi /*
1193ebf99093STom Zanussi  *	subbuf_splice_actor - splice up to one subbuf's worth of data
1194ebf99093STom Zanussi  */
11955f1664f9SDan Carpenter static ssize_t subbuf_splice_actor(struct file *in,
1196ebf99093STom Zanussi 			       loff_t *ppos,
1197ebf99093STom Zanussi 			       struct pipe_inode_info *pipe,
1198ebf99093STom Zanussi 			       size_t len,
1199ebf99093STom Zanussi 			       unsigned int flags,
1200ebf99093STom Zanussi 			       int *nonpad_ret)
1201ebf99093STom Zanussi {
12025f1664f9SDan Carpenter 	unsigned int pidx, poff, total_len, subbuf_pages, nr_pages;
1203ebf99093STom Zanussi 	struct rchan_buf *rbuf = in->private_data;
1204ebf99093STom Zanussi 	unsigned int subbuf_size = rbuf->chan->subbuf_size;
120524da24deSTom Zanussi 	uint64_t pos = (uint64_t) *ppos;
120624da24deSTom Zanussi 	uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
120724da24deSTom Zanussi 	size_t read_start = (size_t) do_div(pos, alloc_size);
1208ebf99093STom Zanussi 	size_t read_subbuf = read_start / subbuf_size;
1209ebf99093STom Zanussi 	size_t padding = rbuf->padding[read_subbuf];
1210ebf99093STom Zanussi 	size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
121135f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
121235f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
12131db60cf2SJens Axboe 	struct splice_pipe_desc spd = {
12141db60cf2SJens Axboe 		.pages = pages,
12151db60cf2SJens Axboe 		.nr_pages = 0,
1216047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
12171db60cf2SJens Axboe 		.partial = partial,
12181db60cf2SJens Axboe 		.ops = &relay_pipe_buf_ops,
12195eb7f9faSJens Axboe 		.spd_release = relay_page_release,
12201db60cf2SJens Axboe 	};
12215f1664f9SDan Carpenter 	ssize_t ret;
1222ebf99093STom Zanussi 
1223ebf99093STom Zanussi 	if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1224ebf99093STom Zanussi 		return 0;
122535f3d14dSJens Axboe 	if (splice_grow_spd(pipe, &spd))
122635f3d14dSJens Axboe 		return -ENOMEM;
1227ebf99093STom Zanussi 
12281db60cf2SJens Axboe 	/*
12291db60cf2SJens Axboe 	 * Adjust read len, if longer than what is available
12301db60cf2SJens Axboe 	 */
12311db60cf2SJens Axboe 	if (len > (subbuf_size - read_start % subbuf_size))
12321db60cf2SJens Axboe 		len = subbuf_size - read_start % subbuf_size;
1233ebf99093STom Zanussi 
1234ebf99093STom Zanussi 	subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1235ebf99093STom Zanussi 	pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1236ebf99093STom Zanussi 	poff = read_start & ~PAGE_MASK;
1237a786c06dSAl Viro 	nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max);
1238ebf99093STom Zanussi 
123916d54669SJens Axboe 	for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) {
12401db60cf2SJens Axboe 		unsigned int this_len, this_end, private;
12411db60cf2SJens Axboe 		unsigned int cur_pos = read_start + total_len;
1242ebf99093STom Zanussi 
12431db60cf2SJens Axboe 		if (!len)
1244ebf99093STom Zanussi 			break;
12451db60cf2SJens Axboe 
12461db60cf2SJens Axboe 		this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
12471db60cf2SJens Axboe 		private = this_len;
12481db60cf2SJens Axboe 
12491db60cf2SJens Axboe 		spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
12501db60cf2SJens Axboe 		spd.partial[spd.nr_pages].offset = poff;
12511db60cf2SJens Axboe 
12521db60cf2SJens Axboe 		this_end = cur_pos + this_len;
12531db60cf2SJens Axboe 		if (this_end >= nonpad_end) {
12541db60cf2SJens Axboe 			this_len = nonpad_end - cur_pos;
12551db60cf2SJens Axboe 			private = this_len + padding;
1256ebf99093STom Zanussi 		}
12571db60cf2SJens Axboe 		spd.partial[spd.nr_pages].len = this_len;
12581db60cf2SJens Axboe 		spd.partial[spd.nr_pages].private = private;
1259ebf99093STom Zanussi 
12601db60cf2SJens Axboe 		len -= this_len;
12611db60cf2SJens Axboe 		total_len += this_len;
1262ebf99093STom Zanussi 		poff = 0;
1263ebf99093STom Zanussi 		pidx = (pidx + 1) % subbuf_pages;
1264ebf99093STom Zanussi 
12651db60cf2SJens Axboe 		if (this_end >= nonpad_end) {
12661db60cf2SJens Axboe 			spd.nr_pages++;
1267ebf99093STom Zanussi 			break;
1268ebf99093STom Zanussi 		}
1269ebf99093STom Zanussi 	}
1270ebf99093STom Zanussi 
127135f3d14dSJens Axboe 	ret = 0;
12721db60cf2SJens Axboe 	if (!spd.nr_pages)
127335f3d14dSJens Axboe 		goto out;
1274ebf99093STom Zanussi 
12751db60cf2SJens Axboe 	ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
12761db60cf2SJens Axboe 	if (ret < 0 || ret < total_len)
127735f3d14dSJens Axboe 		goto out;
1278ebf99093STom Zanussi 
12791db60cf2SJens Axboe         if (read_start + ret == nonpad_end)
12801db60cf2SJens Axboe                 ret += padding;
1281ebf99093STom Zanussi 
128235f3d14dSJens Axboe out:
1283047fe360SEric Dumazet 	splice_shrink_spd(&spd);
1284ebf99093STom Zanussi 	return ret;
1285ebf99093STom Zanussi }
1286ebf99093STom Zanussi 
1287ebf99093STom Zanussi static ssize_t relay_file_splice_read(struct file *in,
1288ebf99093STom Zanussi 				      loff_t *ppos,
1289ebf99093STom Zanussi 				      struct pipe_inode_info *pipe,
1290ebf99093STom Zanussi 				      size_t len,
1291ebf99093STom Zanussi 				      unsigned int flags)
1292ebf99093STom Zanussi {
1293ebf99093STom Zanussi 	ssize_t spliced;
1294ebf99093STom Zanussi 	int ret;
1295ebf99093STom Zanussi 	int nonpad_ret = 0;
1296ebf99093STom Zanussi 
1297ebf99093STom Zanussi 	ret = 0;
1298ebf99093STom Zanussi 	spliced = 0;
1299ebf99093STom Zanussi 
1300a82c53a0STom Zanussi 	while (len && !spliced) {
1301ebf99093STom Zanussi 		ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1302ebf99093STom Zanussi 		if (ret < 0)
1303ebf99093STom Zanussi 			break;
1304ebf99093STom Zanussi 		else if (!ret) {
1305fbb5b7aeSTom Zanussi 			if (flags & SPLICE_F_NONBLOCK)
1306ebf99093STom Zanussi 				ret = -EAGAIN;
1307ebf99093STom Zanussi 			break;
1308ebf99093STom Zanussi 		}
1309ebf99093STom Zanussi 
1310ebf99093STom Zanussi 		*ppos += ret;
1311ebf99093STom Zanussi 		if (ret > len)
1312ebf99093STom Zanussi 			len = 0;
1313ebf99093STom Zanussi 		else
1314ebf99093STom Zanussi 			len -= ret;
1315ebf99093STom Zanussi 		spliced += nonpad_ret;
1316ebf99093STom Zanussi 		nonpad_ret = 0;
1317ebf99093STom Zanussi 	}
1318ebf99093STom Zanussi 
1319ebf99093STom Zanussi 	if (spliced)
1320ebf99093STom Zanussi 		return spliced;
1321ebf99093STom Zanussi 
1322ebf99093STom Zanussi 	return ret;
1323221415d7SJens Axboe }
1324221415d7SJens Axboe 
132515ad7cdcSHelge Deller const struct file_operations relay_file_operations = {
1326b86ff981SJens Axboe 	.open		= relay_file_open,
1327b86ff981SJens Axboe 	.poll		= relay_file_poll,
1328b86ff981SJens Axboe 	.mmap		= relay_file_mmap,
1329b86ff981SJens Axboe 	.read		= relay_file_read,
1330b86ff981SJens Axboe 	.llseek		= no_llseek,
1331b86ff981SJens Axboe 	.release	= relay_file_release,
1332ebf99093STom Zanussi 	.splice_read	= relay_file_splice_read,
1333b86ff981SJens Axboe };
1334b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations);
1335