xref: /linux/kernel/relay.c (revision d6b29d7cee064f28ca097e906de7453541351095)
1b86ff981SJens Axboe /*
2b86ff981SJens Axboe  * Public API and common code for kernel->userspace relay file support.
3b86ff981SJens Axboe  *
4b86ff981SJens Axboe  * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
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>
18b86ff981SJens Axboe #include <linux/module.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>
24*d6b29d7cSJens 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 /*
40b86ff981SJens Axboe  * nopage() vm_op implementation for relay file mapping.
41b86ff981SJens Axboe  */
42b86ff981SJens Axboe static struct page *relay_buf_nopage(struct vm_area_struct *vma,
43b86ff981SJens Axboe 				     unsigned long address,
44b86ff981SJens Axboe 				     int *type)
45b86ff981SJens Axboe {
46b86ff981SJens Axboe 	struct page *page;
47b86ff981SJens Axboe 	struct rchan_buf *buf = vma->vm_private_data;
48b86ff981SJens Axboe 	unsigned long offset = address - vma->vm_start;
49b86ff981SJens Axboe 
50b86ff981SJens Axboe 	if (address > vma->vm_end)
51b86ff981SJens Axboe 		return NOPAGE_SIGBUS; /* Disallow mremap */
52b86ff981SJens Axboe 	if (!buf)
53b86ff981SJens Axboe 		return NOPAGE_OOM;
54b86ff981SJens Axboe 
55b86ff981SJens Axboe 	page = vmalloc_to_page(buf->start + offset);
56b86ff981SJens Axboe 	if (!page)
57b86ff981SJens Axboe 		return NOPAGE_OOM;
58b86ff981SJens Axboe 	get_page(page);
59b86ff981SJens Axboe 
60b86ff981SJens Axboe 	if (type)
61b86ff981SJens Axboe 		*type = VM_FAULT_MINOR;
62b86ff981SJens Axboe 
63b86ff981SJens Axboe 	return page;
64b86ff981SJens Axboe }
65b86ff981SJens Axboe 
66b86ff981SJens Axboe /*
67b86ff981SJens Axboe  * vm_ops for relay file mappings.
68b86ff981SJens Axboe  */
69b86ff981SJens Axboe static struct vm_operations_struct relay_file_mmap_ops = {
70b86ff981SJens Axboe 	.nopage = relay_buf_nopage,
71b86ff981SJens Axboe 	.close = relay_file_mmap_close,
72b86ff981SJens Axboe };
73b86ff981SJens Axboe 
74b86ff981SJens Axboe /**
75b86ff981SJens Axboe  *	relay_mmap_buf: - mmap channel buffer to process address space
76b86ff981SJens Axboe  *	@buf: relay channel buffer
77b86ff981SJens Axboe  *	@vma: vm_area_struct describing memory to be mapped
78b86ff981SJens Axboe  *
79b86ff981SJens Axboe  *	Returns 0 if ok, negative on error
80b86ff981SJens Axboe  *
81b86ff981SJens Axboe  *	Caller should already have grabbed mmap_sem.
82b86ff981SJens Axboe  */
83b86ff981SJens Axboe int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
84b86ff981SJens Axboe {
85b86ff981SJens Axboe 	unsigned long length = vma->vm_end - vma->vm_start;
86b86ff981SJens Axboe 	struct file *filp = vma->vm_file;
87b86ff981SJens Axboe 
88b86ff981SJens Axboe 	if (!buf)
89b86ff981SJens Axboe 		return -EBADF;
90b86ff981SJens Axboe 
91b86ff981SJens Axboe 	if (length != (unsigned long)buf->chan->alloc_size)
92b86ff981SJens Axboe 		return -EINVAL;
93b86ff981SJens Axboe 
94b86ff981SJens Axboe 	vma->vm_ops = &relay_file_mmap_ops;
95b86ff981SJens Axboe 	vma->vm_private_data = buf;
96b86ff981SJens Axboe 	buf->chan->cb->buf_mapped(buf, filp);
97b86ff981SJens Axboe 
98b86ff981SJens Axboe 	return 0;
99b86ff981SJens Axboe }
100b86ff981SJens Axboe 
101b86ff981SJens Axboe /**
102b86ff981SJens Axboe  *	relay_alloc_buf - allocate a channel buffer
103b86ff981SJens Axboe  *	@buf: the buffer struct
104b86ff981SJens Axboe  *	@size: total size of the buffer
105b86ff981SJens Axboe  *
1064c78a663SRandy Dunlap  *	Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
107221415d7SJens Axboe  *	passed in size will get page aligned, if it isn't already.
108b86ff981SJens Axboe  */
109221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
110b86ff981SJens Axboe {
111b86ff981SJens Axboe 	void *mem;
112b86ff981SJens Axboe 	unsigned int i, j, n_pages;
113b86ff981SJens Axboe 
114221415d7SJens Axboe 	*size = PAGE_ALIGN(*size);
115221415d7SJens Axboe 	n_pages = *size >> PAGE_SHIFT;
116b86ff981SJens Axboe 
117b86ff981SJens Axboe 	buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
118b86ff981SJens Axboe 	if (!buf->page_array)
119b86ff981SJens Axboe 		return NULL;
120b86ff981SJens Axboe 
121b86ff981SJens Axboe 	for (i = 0; i < n_pages; i++) {
122b86ff981SJens Axboe 		buf->page_array[i] = alloc_page(GFP_KERNEL);
123b86ff981SJens Axboe 		if (unlikely(!buf->page_array[i]))
124b86ff981SJens Axboe 			goto depopulate;
125ebf99093STom Zanussi 		set_page_private(buf->page_array[i], (unsigned long)buf);
126b86ff981SJens Axboe 	}
127b86ff981SJens Axboe 	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
128b86ff981SJens Axboe 	if (!mem)
129b86ff981SJens Axboe 		goto depopulate;
130b86ff981SJens Axboe 
131221415d7SJens Axboe 	memset(mem, 0, *size);
132b86ff981SJens Axboe 	buf->page_count = n_pages;
133b86ff981SJens Axboe 	return mem;
134b86ff981SJens Axboe 
135b86ff981SJens Axboe depopulate:
136b86ff981SJens Axboe 	for (j = 0; j < i; j++)
137b86ff981SJens Axboe 		__free_page(buf->page_array[j]);
138b86ff981SJens Axboe 	kfree(buf->page_array);
139b86ff981SJens Axboe 	return NULL;
140b86ff981SJens Axboe }
141b86ff981SJens Axboe 
142b86ff981SJens Axboe /**
143b86ff981SJens Axboe  *	relay_create_buf - allocate and initialize a channel buffer
1444c78a663SRandy Dunlap  *	@chan: the relay channel
145b86ff981SJens Axboe  *
1464c78a663SRandy Dunlap  *	Returns channel buffer if successful, %NULL otherwise.
147b86ff981SJens Axboe  */
148b86ff981SJens Axboe struct rchan_buf *relay_create_buf(struct rchan *chan)
149b86ff981SJens Axboe {
150cd861280SRobert P. J. Day 	struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
151b86ff981SJens Axboe 	if (!buf)
152b86ff981SJens Axboe 		return NULL;
153b86ff981SJens Axboe 
154b86ff981SJens Axboe 	buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
155b86ff981SJens Axboe 	if (!buf->padding)
156b86ff981SJens Axboe 		goto free_buf;
157b86ff981SJens Axboe 
158221415d7SJens Axboe 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
159b86ff981SJens Axboe 	if (!buf->start)
160b86ff981SJens Axboe 		goto free_buf;
161b86ff981SJens Axboe 
162b86ff981SJens Axboe 	buf->chan = chan;
163b86ff981SJens Axboe 	kref_get(&buf->chan->kref);
164b86ff981SJens Axboe 	return buf;
165b86ff981SJens Axboe 
166b86ff981SJens Axboe free_buf:
167b86ff981SJens Axboe 	kfree(buf->padding);
168b86ff981SJens Axboe 	kfree(buf);
169b86ff981SJens Axboe 	return NULL;
170b86ff981SJens Axboe }
171b86ff981SJens Axboe 
172b86ff981SJens Axboe /**
173b86ff981SJens Axboe  *	relay_destroy_channel - free the channel struct
1744c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay channel
175b86ff981SJens Axboe  *
176b86ff981SJens Axboe  *	Should only be called from kref_put().
177b86ff981SJens Axboe  */
178b86ff981SJens Axboe void relay_destroy_channel(struct kref *kref)
179b86ff981SJens Axboe {
180b86ff981SJens Axboe 	struct rchan *chan = container_of(kref, struct rchan, kref);
181b86ff981SJens Axboe 	kfree(chan);
182b86ff981SJens Axboe }
183b86ff981SJens Axboe 
184b86ff981SJens Axboe /**
185b86ff981SJens Axboe  *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
186b86ff981SJens Axboe  *	@buf: the buffer struct
187b86ff981SJens Axboe  */
188b86ff981SJens Axboe void relay_destroy_buf(struct rchan_buf *buf)
189b86ff981SJens Axboe {
190b86ff981SJens Axboe 	struct rchan *chan = buf->chan;
191b86ff981SJens Axboe 	unsigned int i;
192b86ff981SJens Axboe 
193b86ff981SJens Axboe 	if (likely(buf->start)) {
194b86ff981SJens Axboe 		vunmap(buf->start);
195b86ff981SJens Axboe 		for (i = 0; i < buf->page_count; i++)
196b86ff981SJens Axboe 			__free_page(buf->page_array[i]);
197b86ff981SJens Axboe 		kfree(buf->page_array);
198b86ff981SJens Axboe 	}
19923c88752SMathieu Desnoyers 	chan->buf[buf->cpu] = NULL;
200b86ff981SJens Axboe 	kfree(buf->padding);
201b86ff981SJens Axboe 	kfree(buf);
202b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
203b86ff981SJens Axboe }
204b86ff981SJens Axboe 
205b86ff981SJens Axboe /**
206b86ff981SJens Axboe  *	relay_remove_buf - remove a channel buffer
2074c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay buffer
208b86ff981SJens Axboe  *
209b86ff981SJens Axboe  *	Removes the file from the fileystem, which also frees the
210b86ff981SJens Axboe  *	rchan_buf_struct and the channel buffer.  Should only be called from
211b86ff981SJens Axboe  *	kref_put().
212b86ff981SJens Axboe  */
213b86ff981SJens Axboe void relay_remove_buf(struct kref *kref)
214b86ff981SJens Axboe {
215b86ff981SJens Axboe 	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
216b86ff981SJens Axboe 	buf->chan->cb->remove_buf_file(buf->dentry);
217b86ff981SJens Axboe 	relay_destroy_buf(buf);
218b86ff981SJens Axboe }
219b86ff981SJens Axboe 
220b86ff981SJens Axboe /**
221b86ff981SJens Axboe  *	relay_buf_empty - boolean, is the channel buffer empty?
222b86ff981SJens Axboe  *	@buf: channel buffer
223b86ff981SJens Axboe  *
224b86ff981SJens Axboe  *	Returns 1 if the buffer is empty, 0 otherwise.
225b86ff981SJens Axboe  */
226b86ff981SJens Axboe int relay_buf_empty(struct rchan_buf *buf)
227b86ff981SJens Axboe {
228b86ff981SJens Axboe 	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
229b86ff981SJens Axboe }
230b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_empty);
231b86ff981SJens Axboe 
232b86ff981SJens Axboe /**
233b86ff981SJens Axboe  *	relay_buf_full - boolean, is the channel buffer full?
234b86ff981SJens Axboe  *	@buf: channel buffer
235b86ff981SJens Axboe  *
236b86ff981SJens Axboe  *	Returns 1 if the buffer is full, 0 otherwise.
237b86ff981SJens Axboe  */
238b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf)
239b86ff981SJens Axboe {
240b86ff981SJens Axboe 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
241b86ff981SJens Axboe 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
242b86ff981SJens Axboe }
243b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full);
244b86ff981SJens Axboe 
245b86ff981SJens Axboe /*
246b86ff981SJens Axboe  * High-level relay kernel API and associated functions.
247b86ff981SJens Axboe  */
248b86ff981SJens Axboe 
249b86ff981SJens Axboe /*
250b86ff981SJens Axboe  * rchan_callback implementations defining default channel behavior.  Used
251b86ff981SJens Axboe  * in place of corresponding NULL values in client callback struct.
252b86ff981SJens Axboe  */
253b86ff981SJens Axboe 
254b86ff981SJens Axboe /*
255b86ff981SJens Axboe  * subbuf_start() default callback.  Does nothing.
256b86ff981SJens Axboe  */
257b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf,
258b86ff981SJens Axboe 					  void *subbuf,
259b86ff981SJens Axboe 					  void *prev_subbuf,
260b86ff981SJens Axboe 					  size_t prev_padding)
261b86ff981SJens Axboe {
262b86ff981SJens Axboe 	if (relay_buf_full(buf))
263b86ff981SJens Axboe 		return 0;
264b86ff981SJens Axboe 
265b86ff981SJens Axboe 	return 1;
266b86ff981SJens Axboe }
267b86ff981SJens Axboe 
268b86ff981SJens Axboe /*
269b86ff981SJens Axboe  * buf_mapped() default callback.  Does nothing.
270b86ff981SJens Axboe  */
271b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf,
272b86ff981SJens Axboe 					struct file *filp)
273b86ff981SJens Axboe {
274b86ff981SJens Axboe }
275b86ff981SJens Axboe 
276b86ff981SJens Axboe /*
277b86ff981SJens Axboe  * buf_unmapped() default callback.  Does nothing.
278b86ff981SJens Axboe  */
279b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf,
280b86ff981SJens Axboe 					  struct file *filp)
281b86ff981SJens Axboe {
282b86ff981SJens Axboe }
283b86ff981SJens Axboe 
284b86ff981SJens Axboe /*
285b86ff981SJens Axboe  * create_buf_file_create() default callback.  Does nothing.
286b86ff981SJens Axboe  */
287b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename,
288b86ff981SJens Axboe 						       struct dentry *parent,
289b86ff981SJens Axboe 						       int mode,
290b86ff981SJens Axboe 						       struct rchan_buf *buf,
291b86ff981SJens Axboe 						       int *is_global)
292b86ff981SJens Axboe {
293b86ff981SJens Axboe 	return NULL;
294b86ff981SJens Axboe }
295b86ff981SJens Axboe 
296b86ff981SJens Axboe /*
297b86ff981SJens Axboe  * remove_buf_file() default callback.  Does nothing.
298b86ff981SJens Axboe  */
299b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry)
300b86ff981SJens Axboe {
301b86ff981SJens Axboe 	return -EINVAL;
302b86ff981SJens Axboe }
303b86ff981SJens Axboe 
304b86ff981SJens Axboe /* relay channel default callbacks */
305b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = {
306b86ff981SJens Axboe 	.subbuf_start = subbuf_start_default_callback,
307b86ff981SJens Axboe 	.buf_mapped = buf_mapped_default_callback,
308b86ff981SJens Axboe 	.buf_unmapped = buf_unmapped_default_callback,
309b86ff981SJens Axboe 	.create_buf_file = create_buf_file_default_callback,
310b86ff981SJens Axboe 	.remove_buf_file = remove_buf_file_default_callback,
311b86ff981SJens Axboe };
312b86ff981SJens Axboe 
313b86ff981SJens Axboe /**
314b86ff981SJens Axboe  *	wakeup_readers - wake up readers waiting on a channel
3159a9136e2SLinus Torvalds  *	@data: contains the channel buffer
316b86ff981SJens Axboe  *
3177c9cb383STom Zanussi  *	This is the timer function used to defer reader waking.
318b86ff981SJens Axboe  */
3197c9cb383STom Zanussi static void wakeup_readers(unsigned long data)
320b86ff981SJens Axboe {
3217c9cb383STom Zanussi 	struct rchan_buf *buf = (struct rchan_buf *)data;
322b86ff981SJens Axboe 	wake_up_interruptible(&buf->read_wait);
323b86ff981SJens Axboe }
324b86ff981SJens Axboe 
325b86ff981SJens Axboe /**
326b86ff981SJens Axboe  *	__relay_reset - reset a channel buffer
327b86ff981SJens Axboe  *	@buf: the channel buffer
328b86ff981SJens Axboe  *	@init: 1 if this is a first-time initialization
329b86ff981SJens Axboe  *
33072fd4a35SRobert P. J. Day  *	See relay_reset() for description of effect.
331b86ff981SJens Axboe  */
332192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init)
333b86ff981SJens Axboe {
334b86ff981SJens Axboe 	size_t i;
335b86ff981SJens Axboe 
336b86ff981SJens Axboe 	if (init) {
337b86ff981SJens Axboe 		init_waitqueue_head(&buf->read_wait);
338b86ff981SJens Axboe 		kref_init(&buf->kref);
3397c9cb383STom Zanussi 		setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
3407c9cb383STom Zanussi 	} else
3417c9cb383STom Zanussi 		del_timer_sync(&buf->timer);
342b86ff981SJens Axboe 
343b86ff981SJens Axboe 	buf->subbufs_produced = 0;
344b86ff981SJens Axboe 	buf->subbufs_consumed = 0;
345b86ff981SJens Axboe 	buf->bytes_consumed = 0;
346b86ff981SJens Axboe 	buf->finalized = 0;
347b86ff981SJens Axboe 	buf->data = buf->start;
348b86ff981SJens Axboe 	buf->offset = 0;
349b86ff981SJens Axboe 
350b86ff981SJens Axboe 	for (i = 0; i < buf->chan->n_subbufs; i++)
351b86ff981SJens Axboe 		buf->padding[i] = 0;
352b86ff981SJens Axboe 
353b86ff981SJens Axboe 	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
354b86ff981SJens Axboe }
355b86ff981SJens Axboe 
356b86ff981SJens Axboe /**
357b86ff981SJens Axboe  *	relay_reset - reset the channel
358b86ff981SJens Axboe  *	@chan: the channel
359b86ff981SJens Axboe  *
360b86ff981SJens Axboe  *	This has the effect of erasing all data from all channel buffers
361b86ff981SJens Axboe  *	and restarting the channel in its initial state.  The buffers
362b86ff981SJens Axboe  *	are not freed, so any mappings are still in effect.
363b86ff981SJens Axboe  *
36472fd4a35SRobert P. J. Day  *	NOTE. Care should be taken that the channel isn't actually
365b86ff981SJens Axboe  *	being used by anything when this call is made.
366b86ff981SJens Axboe  */
367b86ff981SJens Axboe void relay_reset(struct rchan *chan)
368b86ff981SJens Axboe {
369b86ff981SJens Axboe 	unsigned int i;
370b86ff981SJens Axboe 
371b86ff981SJens Axboe 	if (!chan)
372b86ff981SJens Axboe 		return;
373b86ff981SJens Axboe 
37423c88752SMathieu Desnoyers  	if (chan->is_global && chan->buf[0]) {
37523c88752SMathieu Desnoyers 		__relay_reset(chan->buf[0], 0);
37623c88752SMathieu Desnoyers 		return;
377b86ff981SJens Axboe 	}
37823c88752SMathieu Desnoyers 
37923c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
38023c88752SMathieu Desnoyers 	for_each_online_cpu(i)
38123c88752SMathieu Desnoyers 		if (chan->buf[i])
38223c88752SMathieu Desnoyers 			__relay_reset(chan->buf[i], 0);
38323c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
384b86ff981SJens Axboe }
385b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset);
386b86ff981SJens Axboe 
3874c78a663SRandy Dunlap /*
388b86ff981SJens Axboe  *	relay_open_buf - create a new relay channel buffer
389b86ff981SJens Axboe  *
39023c88752SMathieu Desnoyers  *	used by relay_open() and CPU hotplug.
391b86ff981SJens Axboe  */
39223c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
393b86ff981SJens Axboe {
39423c88752SMathieu Desnoyers  	struct rchan_buf *buf = NULL;
395b86ff981SJens Axboe 	struct dentry *dentry;
39623c88752SMathieu Desnoyers  	char *tmpname;
397b86ff981SJens Axboe 
39823c88752SMathieu Desnoyers  	if (chan->is_global)
399b86ff981SJens Axboe 		return chan->buf[0];
400b86ff981SJens Axboe 
40123c88752SMathieu Desnoyers 	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
40223c88752SMathieu Desnoyers  	if (!tmpname)
40323c88752SMathieu Desnoyers  		goto end;
40423c88752SMathieu Desnoyers  	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
40523c88752SMathieu Desnoyers 
406b86ff981SJens Axboe 	buf = relay_create_buf(chan);
407b86ff981SJens Axboe 	if (!buf)
40823c88752SMathieu Desnoyers  		goto free_name;
409b86ff981SJens Axboe 
41023c88752SMathieu Desnoyers  	buf->cpu = cpu;
411b86ff981SJens Axboe  	__relay_reset(buf, 1);
412b86ff981SJens Axboe 
41323c88752SMathieu Desnoyers 	/* Create file in fs */
41423c88752SMathieu Desnoyers  	dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
41523c88752SMathieu Desnoyers  					   buf, &chan->is_global);
41623c88752SMathieu Desnoyers  	if (!dentry)
41723c88752SMathieu Desnoyers  		goto free_buf;
41823c88752SMathieu Desnoyers 
41923c88752SMathieu Desnoyers 	buf->dentry = dentry;
42023c88752SMathieu Desnoyers 
42123c88752SMathieu Desnoyers  	if(chan->is_global) {
42223c88752SMathieu Desnoyers  		chan->buf[0] = buf;
42323c88752SMathieu Desnoyers  		buf->cpu = 0;
42423c88752SMathieu Desnoyers   	}
42523c88752SMathieu Desnoyers 
42623c88752SMathieu Desnoyers  	goto free_name;
42723c88752SMathieu Desnoyers 
42823c88752SMathieu Desnoyers free_buf:
42923c88752SMathieu Desnoyers  	relay_destroy_buf(buf);
43023c88752SMathieu Desnoyers free_name:
43123c88752SMathieu Desnoyers  	kfree(tmpname);
43223c88752SMathieu Desnoyers end:
433b86ff981SJens Axboe 	return buf;
434b86ff981SJens Axboe }
435b86ff981SJens Axboe 
436b86ff981SJens Axboe /**
437b86ff981SJens Axboe  *	relay_close_buf - close a channel buffer
438b86ff981SJens Axboe  *	@buf: channel buffer
439b86ff981SJens Axboe  *
440b86ff981SJens Axboe  *	Marks the buffer finalized and restores the default callbacks.
441b86ff981SJens Axboe  *	The channel buffer and channel buffer data structure are then freed
442b86ff981SJens Axboe  *	automatically when the last reference is given up.
443b86ff981SJens Axboe  */
444192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf)
445b86ff981SJens Axboe {
446b86ff981SJens Axboe 	buf->finalized = 1;
4477c9cb383STom Zanussi 	del_timer_sync(&buf->timer);
448b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
449b86ff981SJens Axboe }
450b86ff981SJens Axboe 
451192636adSAndrew Morton static void setup_callbacks(struct rchan *chan,
452b86ff981SJens Axboe 				   struct rchan_callbacks *cb)
453b86ff981SJens Axboe {
454b86ff981SJens Axboe 	if (!cb) {
455b86ff981SJens Axboe 		chan->cb = &default_channel_callbacks;
456b86ff981SJens Axboe 		return;
457b86ff981SJens Axboe 	}
458b86ff981SJens Axboe 
459b86ff981SJens Axboe 	if (!cb->subbuf_start)
460b86ff981SJens Axboe 		cb->subbuf_start = subbuf_start_default_callback;
461b86ff981SJens Axboe 	if (!cb->buf_mapped)
462b86ff981SJens Axboe 		cb->buf_mapped = buf_mapped_default_callback;
463b86ff981SJens Axboe 	if (!cb->buf_unmapped)
464b86ff981SJens Axboe 		cb->buf_unmapped = buf_unmapped_default_callback;
465b86ff981SJens Axboe 	if (!cb->create_buf_file)
466b86ff981SJens Axboe 		cb->create_buf_file = create_buf_file_default_callback;
467b86ff981SJens Axboe 	if (!cb->remove_buf_file)
468b86ff981SJens Axboe 		cb->remove_buf_file = remove_buf_file_default_callback;
469b86ff981SJens Axboe 	chan->cb = cb;
470b86ff981SJens Axboe }
471b86ff981SJens Axboe 
472b86ff981SJens Axboe /**
47323c88752SMathieu Desnoyers  * 	relay_hotcpu_callback - CPU hotplug callback
47423c88752SMathieu Desnoyers  * 	@nb: notifier block
47523c88752SMathieu Desnoyers  * 	@action: hotplug action to take
47623c88752SMathieu Desnoyers  * 	@hcpu: CPU number
47723c88752SMathieu Desnoyers  *
47805fb6bf0SRandy Dunlap  * 	Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
47923c88752SMathieu Desnoyers  */
48023c88752SMathieu Desnoyers static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
48123c88752SMathieu Desnoyers 				unsigned long action,
48223c88752SMathieu Desnoyers 				void *hcpu)
48323c88752SMathieu Desnoyers {
48423c88752SMathieu Desnoyers 	unsigned int hotcpu = (unsigned long)hcpu;
48523c88752SMathieu Desnoyers 	struct rchan *chan;
48623c88752SMathieu Desnoyers 
48723c88752SMathieu Desnoyers 	switch(action) {
48823c88752SMathieu Desnoyers 	case CPU_UP_PREPARE:
4898bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
49023c88752SMathieu Desnoyers 		mutex_lock(&relay_channels_mutex);
49123c88752SMathieu Desnoyers 		list_for_each_entry(chan, &relay_channels, list) {
49223c88752SMathieu Desnoyers 			if (chan->buf[hotcpu])
49323c88752SMathieu Desnoyers 				continue;
49423c88752SMathieu Desnoyers 			chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
49523c88752SMathieu Desnoyers 			if(!chan->buf[hotcpu]) {
49623c88752SMathieu Desnoyers 				printk(KERN_ERR
49723c88752SMathieu Desnoyers 					"relay_hotcpu_callback: cpu %d buffer "
49823c88752SMathieu Desnoyers 					"creation failed\n", hotcpu);
49923c88752SMathieu Desnoyers 				mutex_unlock(&relay_channels_mutex);
50023c88752SMathieu Desnoyers 				return NOTIFY_BAD;
50123c88752SMathieu Desnoyers 			}
50223c88752SMathieu Desnoyers 		}
50323c88752SMathieu Desnoyers 		mutex_unlock(&relay_channels_mutex);
50423c88752SMathieu Desnoyers 		break;
50523c88752SMathieu Desnoyers 	case CPU_DEAD:
5068bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
50723c88752SMathieu Desnoyers 		/* No need to flush the cpu : will be flushed upon
50823c88752SMathieu Desnoyers 		 * final relay_flush() call. */
50923c88752SMathieu Desnoyers 		break;
51023c88752SMathieu Desnoyers 	}
51123c88752SMathieu Desnoyers 	return NOTIFY_OK;
51223c88752SMathieu Desnoyers }
51323c88752SMathieu Desnoyers 
51423c88752SMathieu Desnoyers /**
515b86ff981SJens Axboe  *	relay_open - create a new relay channel
516b86ff981SJens Axboe  *	@base_filename: base name of files to create
5174c78a663SRandy Dunlap  *	@parent: dentry of parent directory, %NULL for root directory
518b86ff981SJens Axboe  *	@subbuf_size: size of sub-buffers
519b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers
520b86ff981SJens Axboe  *	@cb: client callback functions
52123c88752SMathieu Desnoyers  *	@private_data: user-defined data
522b86ff981SJens Axboe  *
5234c78a663SRandy Dunlap  *	Returns channel pointer if successful, %NULL otherwise.
524b86ff981SJens Axboe  *
525b86ff981SJens Axboe  *	Creates a channel buffer for each cpu using the sizes and
526b86ff981SJens Axboe  *	attributes specified.  The created channel buffer files
527b86ff981SJens Axboe  *	will be named base_filename0...base_filenameN-1.  File
52872fd4a35SRobert P. J. Day  *	permissions will be %S_IRUSR.
529b86ff981SJens Axboe  */
530b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
531b86ff981SJens Axboe 			 struct dentry *parent,
532b86ff981SJens Axboe 			 size_t subbuf_size,
533b86ff981SJens Axboe 			 size_t n_subbufs,
53423c88752SMathieu Desnoyers 			 struct rchan_callbacks *cb,
53523c88752SMathieu Desnoyers 			 void *private_data)
536b86ff981SJens Axboe {
537b86ff981SJens Axboe 	unsigned int i;
538b86ff981SJens Axboe 	struct rchan *chan;
539b86ff981SJens Axboe 	if (!base_filename)
540b86ff981SJens Axboe 		return NULL;
541b86ff981SJens Axboe 
542b86ff981SJens Axboe 	if (!(subbuf_size && n_subbufs))
543b86ff981SJens Axboe 		return NULL;
544b86ff981SJens Axboe 
545cd861280SRobert P. J. Day 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
546b86ff981SJens Axboe 	if (!chan)
547b86ff981SJens Axboe 		return NULL;
548b86ff981SJens Axboe 
549b86ff981SJens Axboe 	chan->version = RELAYFS_CHANNEL_VERSION;
550b86ff981SJens Axboe 	chan->n_subbufs = n_subbufs;
551b86ff981SJens Axboe 	chan->subbuf_size = subbuf_size;
552b86ff981SJens Axboe 	chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
55323c88752SMathieu Desnoyers 	chan->parent = parent;
55423c88752SMathieu Desnoyers 	chan->private_data = private_data;
55523c88752SMathieu Desnoyers 	strlcpy(chan->base_filename, base_filename, NAME_MAX);
556b86ff981SJens Axboe 	setup_callbacks(chan, cb);
557b86ff981SJens Axboe 	kref_init(&chan->kref);
558b86ff981SJens Axboe 
55923c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
560b86ff981SJens Axboe 	for_each_online_cpu(i) {
56123c88752SMathieu Desnoyers 		chan->buf[i] = relay_open_buf(chan, i);
562b86ff981SJens Axboe 		if (!chan->buf[i])
563b86ff981SJens Axboe 			goto free_bufs;
564b86ff981SJens Axboe 	}
56523c88752SMathieu Desnoyers 	list_add(&chan->list, &relay_channels);
56623c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
567b86ff981SJens Axboe 
568b86ff981SJens Axboe 	return chan;
569b86ff981SJens Axboe 
570b86ff981SJens Axboe free_bufs:
57123c88752SMathieu Desnoyers 	for_each_online_cpu(i) {
572b86ff981SJens Axboe 		if (!chan->buf[i])
573b86ff981SJens Axboe 			break;
574b86ff981SJens Axboe 		relay_close_buf(chan->buf[i]);
575b86ff981SJens Axboe 	}
576b86ff981SJens Axboe 
577b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
57823c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
579b86ff981SJens Axboe 	return NULL;
580b86ff981SJens Axboe }
581b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open);
582b86ff981SJens Axboe 
583b86ff981SJens Axboe /**
584b86ff981SJens Axboe  *	relay_switch_subbuf - switch to a new sub-buffer
585b86ff981SJens Axboe  *	@buf: channel buffer
586b86ff981SJens Axboe  *	@length: size of current event
587b86ff981SJens Axboe  *
588b86ff981SJens Axboe  *	Returns either the length passed in or 0 if full.
589b86ff981SJens Axboe  *
590b86ff981SJens Axboe  *	Performs sub-buffer-switch tasks such as invoking callbacks,
591b86ff981SJens Axboe  *	updating padding counts, waking up readers, etc.
592b86ff981SJens Axboe  */
593b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
594b86ff981SJens Axboe {
595b86ff981SJens Axboe 	void *old, *new;
596b86ff981SJens Axboe 	size_t old_subbuf, new_subbuf;
597b86ff981SJens Axboe 
598b86ff981SJens Axboe 	if (unlikely(length > buf->chan->subbuf_size))
599b86ff981SJens Axboe 		goto toobig;
600b86ff981SJens Axboe 
601b86ff981SJens Axboe 	if (buf->offset != buf->chan->subbuf_size + 1) {
602b86ff981SJens Axboe 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
603b86ff981SJens Axboe 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
604b86ff981SJens Axboe 		buf->padding[old_subbuf] = buf->prev_padding;
605b86ff981SJens Axboe 		buf->subbufs_produced++;
606221415d7SJens Axboe 		buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
607221415d7SJens Axboe 			buf->padding[old_subbuf];
608221415d7SJens Axboe 		smp_mb();
6097c9cb383STom Zanussi 		if (waitqueue_active(&buf->read_wait))
6107c9cb383STom Zanussi 			/*
6117c9cb383STom Zanussi 			 * Calling wake_up_interruptible() from here
6127c9cb383STom Zanussi 			 * will deadlock if we happen to be logging
6137c9cb383STom Zanussi 			 * from the scheduler (trying to re-grab
6147c9cb383STom Zanussi 			 * rq->lock), so defer it.
6157c9cb383STom Zanussi 			 */
6167c9cb383STom Zanussi 			__mod_timer(&buf->timer, jiffies + 1);
617b86ff981SJens Axboe 	}
618b86ff981SJens Axboe 
619b86ff981SJens Axboe 	old = buf->data;
620b86ff981SJens Axboe 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
621b86ff981SJens Axboe 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
622b86ff981SJens Axboe 	buf->offset = 0;
623b86ff981SJens Axboe 	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
624b86ff981SJens Axboe 		buf->offset = buf->chan->subbuf_size + 1;
625b86ff981SJens Axboe 		return 0;
626b86ff981SJens Axboe 	}
627b86ff981SJens Axboe 	buf->data = new;
628b86ff981SJens Axboe 	buf->padding[new_subbuf] = 0;
629b86ff981SJens Axboe 
630b86ff981SJens Axboe 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
631b86ff981SJens Axboe 		goto toobig;
632b86ff981SJens Axboe 
633b86ff981SJens Axboe 	return length;
634b86ff981SJens Axboe 
635b86ff981SJens Axboe toobig:
636b86ff981SJens Axboe 	buf->chan->last_toobig = length;
637b86ff981SJens Axboe 	return 0;
638b86ff981SJens Axboe }
639b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf);
640b86ff981SJens Axboe 
641b86ff981SJens Axboe /**
642b86ff981SJens Axboe  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
643b86ff981SJens Axboe  *	@chan: the channel
644b86ff981SJens Axboe  *	@cpu: the cpu associated with the channel buffer to update
645b86ff981SJens Axboe  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
646b86ff981SJens Axboe  *
647b86ff981SJens Axboe  *	Adds to the channel buffer's consumed sub-buffer count.
648b86ff981SJens Axboe  *	subbufs_consumed should be the number of sub-buffers newly consumed,
649b86ff981SJens Axboe  *	not the total consumed.
650b86ff981SJens Axboe  *
65172fd4a35SRobert P. J. Day  *	NOTE. Kernel clients don't need to call this function if the channel
652b86ff981SJens Axboe  *	mode is 'overwrite'.
653b86ff981SJens Axboe  */
654b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan,
655b86ff981SJens Axboe 			    unsigned int cpu,
656b86ff981SJens Axboe 			    size_t subbufs_consumed)
657b86ff981SJens Axboe {
658b86ff981SJens Axboe 	struct rchan_buf *buf;
659b86ff981SJens Axboe 
660b86ff981SJens Axboe 	if (!chan)
661b86ff981SJens Axboe 		return;
662b86ff981SJens Axboe 
663b86ff981SJens Axboe 	if (cpu >= NR_CPUS || !chan->buf[cpu])
664b86ff981SJens Axboe 		return;
665b86ff981SJens Axboe 
666b86ff981SJens Axboe 	buf = chan->buf[cpu];
667b86ff981SJens Axboe 	buf->subbufs_consumed += subbufs_consumed;
668b86ff981SJens Axboe 	if (buf->subbufs_consumed > buf->subbufs_produced)
669b86ff981SJens Axboe 		buf->subbufs_consumed = buf->subbufs_produced;
670b86ff981SJens Axboe }
671b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
672b86ff981SJens Axboe 
673b86ff981SJens Axboe /**
674b86ff981SJens Axboe  *	relay_close - close the channel
675b86ff981SJens Axboe  *	@chan: the channel
676b86ff981SJens Axboe  *
677b86ff981SJens Axboe  *	Closes all channel buffers and frees the channel.
678b86ff981SJens Axboe  */
679b86ff981SJens Axboe void relay_close(struct rchan *chan)
680b86ff981SJens Axboe {
681b86ff981SJens Axboe 	unsigned int i;
682b86ff981SJens Axboe 
683b86ff981SJens Axboe 	if (!chan)
684b86ff981SJens Axboe 		return;
685b86ff981SJens Axboe 
68623c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
68723c88752SMathieu Desnoyers 	if (chan->is_global && chan->buf[0])
68823c88752SMathieu Desnoyers 		relay_close_buf(chan->buf[0]);
68923c88752SMathieu Desnoyers 	else
69023c88752SMathieu Desnoyers 		for_each_possible_cpu(i)
69123c88752SMathieu Desnoyers 			if (chan->buf[i])
692b86ff981SJens Axboe 				relay_close_buf(chan->buf[i]);
693b86ff981SJens Axboe 
694b86ff981SJens Axboe 	if (chan->last_toobig)
695b86ff981SJens Axboe 		printk(KERN_WARNING "relay: one or more items not logged "
696b86ff981SJens Axboe 		       "[item size (%Zd) > sub-buffer size (%Zd)]\n",
697b86ff981SJens Axboe 		       chan->last_toobig, chan->subbuf_size);
698b86ff981SJens Axboe 
69923c88752SMathieu Desnoyers 	list_del(&chan->list);
700b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
70123c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
702b86ff981SJens Axboe }
703b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close);
704b86ff981SJens Axboe 
705b86ff981SJens Axboe /**
706b86ff981SJens Axboe  *	relay_flush - close the channel
707b86ff981SJens Axboe  *	@chan: the channel
708b86ff981SJens Axboe  *
7094c78a663SRandy Dunlap  *	Flushes all channel buffers, i.e. forces buffer switch.
710b86ff981SJens Axboe  */
711b86ff981SJens Axboe void relay_flush(struct rchan *chan)
712b86ff981SJens Axboe {
713b86ff981SJens Axboe 	unsigned int i;
714b86ff981SJens Axboe 
715b86ff981SJens Axboe 	if (!chan)
716b86ff981SJens Axboe 		return;
717b86ff981SJens Axboe 
71823c88752SMathieu Desnoyers 	if (chan->is_global && chan->buf[0]) {
71923c88752SMathieu Desnoyers 		relay_switch_subbuf(chan->buf[0], 0);
72023c88752SMathieu Desnoyers 		return;
721b86ff981SJens Axboe 	}
72223c88752SMathieu Desnoyers 
72323c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
72423c88752SMathieu Desnoyers 	for_each_possible_cpu(i)
72523c88752SMathieu Desnoyers 		if (chan->buf[i])
72623c88752SMathieu Desnoyers 			relay_switch_subbuf(chan->buf[i], 0);
72723c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
728b86ff981SJens Axboe }
729b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush);
730b86ff981SJens Axboe 
731b86ff981SJens Axboe /**
732b86ff981SJens Axboe  *	relay_file_open - open file op for relay files
733b86ff981SJens Axboe  *	@inode: the inode
734b86ff981SJens Axboe  *	@filp: the file
735b86ff981SJens Axboe  *
736b86ff981SJens Axboe  *	Increments the channel buffer refcount.
737b86ff981SJens Axboe  */
738b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp)
739b86ff981SJens Axboe {
7408e18e294STheodore Ts'o 	struct rchan_buf *buf = inode->i_private;
741b86ff981SJens Axboe 	kref_get(&buf->kref);
742b86ff981SJens Axboe 	filp->private_data = buf;
743b86ff981SJens Axboe 
744b86ff981SJens Axboe 	return 0;
745b86ff981SJens Axboe }
746b86ff981SJens Axboe 
747b86ff981SJens Axboe /**
748b86ff981SJens Axboe  *	relay_file_mmap - mmap file op for relay files
749b86ff981SJens Axboe  *	@filp: the file
750b86ff981SJens Axboe  *	@vma: the vma describing what to map
751b86ff981SJens Axboe  *
75272fd4a35SRobert P. J. Day  *	Calls upon relay_mmap_buf() to map the file into user space.
753b86ff981SJens Axboe  */
754b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
755b86ff981SJens Axboe {
756b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
757b86ff981SJens Axboe 	return relay_mmap_buf(buf, vma);
758b86ff981SJens Axboe }
759b86ff981SJens Axboe 
760b86ff981SJens Axboe /**
761b86ff981SJens Axboe  *	relay_file_poll - poll file op for relay files
762b86ff981SJens Axboe  *	@filp: the file
763b86ff981SJens Axboe  *	@wait: poll table
764b86ff981SJens Axboe  *
765b86ff981SJens Axboe  *	Poll implemention.
766b86ff981SJens Axboe  */
767b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
768b86ff981SJens Axboe {
769b86ff981SJens Axboe 	unsigned int mask = 0;
770b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
771b86ff981SJens Axboe 
772b86ff981SJens Axboe 	if (buf->finalized)
773b86ff981SJens Axboe 		return POLLERR;
774b86ff981SJens Axboe 
775b86ff981SJens Axboe 	if (filp->f_mode & FMODE_READ) {
776b86ff981SJens Axboe 		poll_wait(filp, &buf->read_wait, wait);
777b86ff981SJens Axboe 		if (!relay_buf_empty(buf))
778b86ff981SJens Axboe 			mask |= POLLIN | POLLRDNORM;
779b86ff981SJens Axboe 	}
780b86ff981SJens Axboe 
781b86ff981SJens Axboe 	return mask;
782b86ff981SJens Axboe }
783b86ff981SJens Axboe 
784b86ff981SJens Axboe /**
785b86ff981SJens Axboe  *	relay_file_release - release file op for relay files
786b86ff981SJens Axboe  *	@inode: the inode
787b86ff981SJens Axboe  *	@filp: the file
788b86ff981SJens Axboe  *
789b86ff981SJens Axboe  *	Decrements the channel refcount, as the filesystem is
790b86ff981SJens Axboe  *	no longer using it.
791b86ff981SJens Axboe  */
792b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp)
793b86ff981SJens Axboe {
794b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
795b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
796b86ff981SJens Axboe 
797b86ff981SJens Axboe 	return 0;
798b86ff981SJens Axboe }
799b86ff981SJens Axboe 
8004c78a663SRandy Dunlap /*
801b86ff981SJens Axboe  *	relay_file_read_consume - update the consumed count for the buffer
802b86ff981SJens Axboe  */
803b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf,
804b86ff981SJens Axboe 				    size_t read_pos,
805b86ff981SJens Axboe 				    size_t bytes_consumed)
806b86ff981SJens Axboe {
807b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
808b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
809b86ff981SJens Axboe 	size_t read_subbuf;
810b86ff981SJens Axboe 
811b86ff981SJens Axboe 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
812b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
813b86ff981SJens Axboe 		buf->bytes_consumed = 0;
814b86ff981SJens Axboe 	}
815b86ff981SJens Axboe 
816b86ff981SJens Axboe 	buf->bytes_consumed += bytes_consumed;
817a66e356cSMasami Hiramatsu 	if (!read_pos)
818a66e356cSMasami Hiramatsu 		read_subbuf = buf->subbufs_consumed % n_subbufs;
819a66e356cSMasami Hiramatsu 	else
820b86ff981SJens Axboe 		read_subbuf = read_pos / buf->chan->subbuf_size;
821b86ff981SJens Axboe 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
822b86ff981SJens Axboe 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
823b86ff981SJens Axboe 		    (buf->offset == subbuf_size))
824b86ff981SJens Axboe 			return;
825b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
826b86ff981SJens Axboe 		buf->bytes_consumed = 0;
827b86ff981SJens Axboe 	}
828b86ff981SJens Axboe }
829b86ff981SJens Axboe 
8304c78a663SRandy Dunlap /*
831b86ff981SJens Axboe  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
832b86ff981SJens Axboe  */
833b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
834b86ff981SJens Axboe {
835b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
836b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
837221415d7SJens Axboe 	size_t produced = buf->subbufs_produced;
838221415d7SJens Axboe 	size_t consumed = buf->subbufs_consumed;
839b86ff981SJens Axboe 
840b86ff981SJens Axboe 	relay_file_read_consume(buf, read_pos, 0);
841b86ff981SJens Axboe 
842221415d7SJens Axboe 	if (unlikely(buf->offset > subbuf_size)) {
843221415d7SJens Axboe 		if (produced == consumed)
844221415d7SJens Axboe 			return 0;
845221415d7SJens Axboe 		return 1;
846221415d7SJens Axboe 	}
847221415d7SJens Axboe 
848221415d7SJens Axboe 	if (unlikely(produced - consumed >= n_subbufs)) {
849a66e356cSMasami Hiramatsu 		consumed = produced - n_subbufs + 1;
850221415d7SJens Axboe 		buf->subbufs_consumed = consumed;
851a66e356cSMasami Hiramatsu 		buf->bytes_consumed = 0;
852221415d7SJens Axboe 	}
853221415d7SJens Axboe 
854221415d7SJens Axboe 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
855221415d7SJens Axboe 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
856221415d7SJens Axboe 
857221415d7SJens Axboe 	if (consumed > produced)
858221415d7SJens Axboe 		produced += n_subbufs * subbuf_size;
859221415d7SJens Axboe 
860221415d7SJens Axboe 	if (consumed == produced)
861221415d7SJens Axboe 		return 0;
862221415d7SJens Axboe 
863b86ff981SJens Axboe 	return 1;
864b86ff981SJens Axboe }
865b86ff981SJens Axboe 
866b86ff981SJens Axboe /**
867b86ff981SJens Axboe  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
8684c78a663SRandy Dunlap  *	@read_pos: file read position
8694c78a663SRandy Dunlap  *	@buf: relay channel buffer
870b86ff981SJens Axboe  */
871b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos,
872b86ff981SJens Axboe 					   struct rchan_buf *buf)
873b86ff981SJens Axboe {
874b86ff981SJens Axboe 	size_t padding, avail = 0;
875b86ff981SJens Axboe 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
876b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
877b86ff981SJens Axboe 
878b86ff981SJens Axboe 	write_subbuf = (buf->data - buf->start) / subbuf_size;
879b86ff981SJens Axboe 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
880b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
881b86ff981SJens Axboe 	read_offset = read_pos % subbuf_size;
882b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
883b86ff981SJens Axboe 
884b86ff981SJens Axboe 	if (read_subbuf == write_subbuf) {
885b86ff981SJens Axboe 		if (read_offset + padding < write_offset)
886b86ff981SJens Axboe 			avail = write_offset - (read_offset + padding);
887b86ff981SJens Axboe 	} else
888b86ff981SJens Axboe 		avail = (subbuf_size - padding) - read_offset;
889b86ff981SJens Axboe 
890b86ff981SJens Axboe 	return avail;
891b86ff981SJens Axboe }
892b86ff981SJens Axboe 
893b86ff981SJens Axboe /**
894b86ff981SJens Axboe  *	relay_file_read_start_pos - find the first available byte to read
8954c78a663SRandy Dunlap  *	@read_pos: file read position
8964c78a663SRandy Dunlap  *	@buf: relay channel buffer
897b86ff981SJens Axboe  *
89872fd4a35SRobert P. J. Day  *	If the @read_pos is in the middle of padding, return the
899b86ff981SJens Axboe  *	position of the first actually available byte, otherwise
900b86ff981SJens Axboe  *	return the original value.
901b86ff981SJens Axboe  */
902b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos,
903b86ff981SJens Axboe 					struct rchan_buf *buf)
904b86ff981SJens Axboe {
905b86ff981SJens Axboe 	size_t read_subbuf, padding, padding_start, padding_end;
906b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
907b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
9088d62fdebSDavid Wilder 	size_t consumed = buf->subbufs_consumed % n_subbufs;
909b86ff981SJens Axboe 
9108d62fdebSDavid Wilder 	if (!read_pos)
9118d62fdebSDavid Wilder 		read_pos = consumed * subbuf_size + buf->bytes_consumed;
912b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
913b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
914b86ff981SJens Axboe 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
915b86ff981SJens Axboe 	padding_end = (read_subbuf + 1) * subbuf_size;
916b86ff981SJens Axboe 	if (read_pos >= padding_start && read_pos < padding_end) {
917b86ff981SJens Axboe 		read_subbuf = (read_subbuf + 1) % n_subbufs;
918b86ff981SJens Axboe 		read_pos = read_subbuf * subbuf_size;
919b86ff981SJens Axboe 	}
920b86ff981SJens Axboe 
921b86ff981SJens Axboe 	return read_pos;
922b86ff981SJens Axboe }
923b86ff981SJens Axboe 
924b86ff981SJens Axboe /**
925b86ff981SJens Axboe  *	relay_file_read_end_pos - return the new read position
9264c78a663SRandy Dunlap  *	@read_pos: file read position
9274c78a663SRandy Dunlap  *	@buf: relay channel buffer
9284c78a663SRandy Dunlap  *	@count: number of bytes to be read
929b86ff981SJens Axboe  */
930b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf,
931b86ff981SJens Axboe 				      size_t read_pos,
932b86ff981SJens Axboe 				      size_t count)
933b86ff981SJens Axboe {
934b86ff981SJens Axboe 	size_t read_subbuf, padding, end_pos;
935b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
936b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
937b86ff981SJens Axboe 
938b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
939b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
940b86ff981SJens Axboe 	if (read_pos % subbuf_size + count + padding == subbuf_size)
941b86ff981SJens Axboe 		end_pos = (read_subbuf + 1) * subbuf_size;
942b86ff981SJens Axboe 	else
943b86ff981SJens Axboe 		end_pos = read_pos + count;
944b86ff981SJens Axboe 	if (end_pos >= subbuf_size * n_subbufs)
945b86ff981SJens Axboe 		end_pos = 0;
946b86ff981SJens Axboe 
947b86ff981SJens Axboe 	return end_pos;
948b86ff981SJens Axboe }
949b86ff981SJens Axboe 
9504c78a663SRandy Dunlap /*
9516dac40a7STom Zanussi  *	subbuf_read_actor - read up to one subbuf's worth of data
952b86ff981SJens Axboe  */
9536dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start,
9546dac40a7STom Zanussi 			     struct rchan_buf *buf,
9556dac40a7STom Zanussi 			     size_t avail,
9566dac40a7STom Zanussi 			     read_descriptor_t *desc,
9576dac40a7STom Zanussi 			     read_actor_t actor)
958b86ff981SJens Axboe {
959b86ff981SJens Axboe 	void *from;
9606dac40a7STom Zanussi 	int ret = 0;
961b86ff981SJens Axboe 
962b86ff981SJens Axboe 	from = buf->start + read_start;
9636dac40a7STom Zanussi 	ret = avail;
964ba2397efSAl Viro 	if (copy_to_user(desc->arg.buf, from, avail)) {
9656dac40a7STom Zanussi 		desc->error = -EFAULT;
9666dac40a7STom Zanussi 		ret = 0;
967b86ff981SJens Axboe 	}
9686dac40a7STom Zanussi 	desc->arg.data += ret;
9696dac40a7STom Zanussi 	desc->written += ret;
9706dac40a7STom Zanussi 	desc->count -= ret;
9716dac40a7STom Zanussi 
972b86ff981SJens Axboe 	return ret;
973b86ff981SJens Axboe }
974b86ff981SJens Axboe 
9756dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start,
9766dac40a7STom Zanussi 			       struct rchan_buf *buf,
9776dac40a7STom Zanussi 			       size_t avail,
9786dac40a7STom Zanussi 			       read_descriptor_t *desc,
9796dac40a7STom Zanussi 			       read_actor_t actor);
9806dac40a7STom Zanussi 
9814c78a663SRandy Dunlap /*
9826dac40a7STom Zanussi  *	relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
9836dac40a7STom Zanussi  */
984192636adSAndrew Morton static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
9856dac40a7STom Zanussi 					subbuf_actor_t subbuf_actor,
9866dac40a7STom Zanussi 					read_actor_t actor,
987ba2397efSAl Viro 					read_descriptor_t *desc)
988221415d7SJens Axboe {
989221415d7SJens Axboe 	struct rchan_buf *buf = filp->private_data;
990221415d7SJens Axboe 	size_t read_start, avail;
9916dac40a7STom Zanussi 	int ret;
992221415d7SJens Axboe 
993ba2397efSAl Viro 	if (!desc->count)
994221415d7SJens Axboe 		return 0;
995221415d7SJens Axboe 
996f3a43f3fSJosef "Jeff" Sipek 	mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
9976dac40a7STom Zanussi 	do {
9986dac40a7STom Zanussi 		if (!relay_file_read_avail(buf, *ppos))
999221415d7SJens Axboe 			break;
1000221415d7SJens Axboe 
10016dac40a7STom Zanussi 		read_start = relay_file_read_start_pos(*ppos, buf);
10026dac40a7STom Zanussi 		avail = relay_file_read_subbuf_avail(read_start, buf);
10036dac40a7STom Zanussi 		if (!avail)
10046dac40a7STom Zanussi 			break;
1005221415d7SJens Axboe 
1006ba2397efSAl Viro 		avail = min(desc->count, avail);
1007ba2397efSAl Viro 		ret = subbuf_actor(read_start, buf, avail, desc, actor);
1008ba2397efSAl Viro 		if (desc->error < 0)
10096dac40a7STom Zanussi 			break;
10106dac40a7STom Zanussi 
10116dac40a7STom Zanussi 		if (ret) {
1012221415d7SJens Axboe 			relay_file_read_consume(buf, read_start, ret);
1013221415d7SJens Axboe 			*ppos = relay_file_read_end_pos(buf, read_start, ret);
1014221415d7SJens Axboe 		}
1015ba2397efSAl Viro 	} while (desc->count && ret);
1016f3a43f3fSJosef "Jeff" Sipek 	mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
1017221415d7SJens Axboe 
1018ba2397efSAl Viro 	return desc->written;
1019221415d7SJens Axboe }
1020221415d7SJens Axboe 
10216dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp,
10226dac40a7STom Zanussi 			       char __user *buffer,
10236dac40a7STom Zanussi 			       size_t count,
10246dac40a7STom Zanussi 			       loff_t *ppos)
10256dac40a7STom Zanussi {
1026ba2397efSAl Viro 	read_descriptor_t desc;
1027ba2397efSAl Viro 	desc.written = 0;
1028ba2397efSAl Viro 	desc.count = count;
1029ba2397efSAl Viro 	desc.arg.buf = buffer;
1030ba2397efSAl Viro 	desc.error = 0;
1031ba2397efSAl Viro 	return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
1032ba2397efSAl Viro 				       NULL, &desc);
10336dac40a7STom Zanussi }
10346dac40a7STom Zanussi 
1035ebf99093STom Zanussi static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1036ebf99093STom Zanussi 				   struct pipe_buffer *buf)
1037221415d7SJens Axboe {
1038ebf99093STom Zanussi 	struct rchan_buf *rbuf;
1039ebf99093STom Zanussi 
1040ebf99093STom Zanussi 	rbuf = (struct rchan_buf *)page_private(buf->page);
1041ebf99093STom Zanussi 
1042ebf99093STom Zanussi 	rbuf->bytes_consumed += PAGE_SIZE;
1043ebf99093STom Zanussi 
1044ebf99093STom Zanussi 	if (rbuf->bytes_consumed == rbuf->chan->subbuf_size) {
1045ebf99093STom Zanussi 		relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1046ebf99093STom Zanussi 		rbuf->bytes_consumed = 0;
1047ebf99093STom Zanussi 	}
1048ebf99093STom Zanussi }
1049ebf99093STom Zanussi 
1050ebf99093STom Zanussi static struct pipe_buf_operations relay_pipe_buf_ops = {
1051ebf99093STom Zanussi 	.can_merge = 0,
1052ebf99093STom Zanussi 	.map = generic_pipe_buf_map,
1053ebf99093STom Zanussi 	.unmap = generic_pipe_buf_unmap,
1054ebf99093STom Zanussi 	.pin = generic_pipe_buf_pin,
1055ebf99093STom Zanussi 	.release = relay_pipe_buf_release,
1056ebf99093STom Zanussi 	.steal = generic_pipe_buf_steal,
1057ebf99093STom Zanussi 	.get = generic_pipe_buf_get,
1058ebf99093STom Zanussi };
1059ebf99093STom Zanussi 
1060ebf99093STom Zanussi /**
1061ebf99093STom Zanussi  *	subbuf_splice_actor - splice up to one subbuf's worth of data
1062ebf99093STom Zanussi  */
1063ebf99093STom Zanussi static int subbuf_splice_actor(struct file *in,
1064ebf99093STom Zanussi 			       loff_t *ppos,
1065ebf99093STom Zanussi 			       struct pipe_inode_info *pipe,
1066ebf99093STom Zanussi 			       size_t len,
1067ebf99093STom Zanussi 			       unsigned int flags,
1068ebf99093STom Zanussi 			       int *nonpad_ret)
1069ebf99093STom Zanussi {
1070ebf99093STom Zanussi 	unsigned int pidx, poff;
1071ebf99093STom Zanussi 	unsigned int subbuf_pages;
1072ebf99093STom Zanussi 	int ret = 0;
1073ebf99093STom Zanussi 	int do_wakeup = 0;
1074ebf99093STom Zanussi 	struct rchan_buf *rbuf = in->private_data;
1075ebf99093STom Zanussi 	unsigned int subbuf_size = rbuf->chan->subbuf_size;
1076ebf99093STom Zanussi 	size_t read_start = ((size_t)*ppos) % rbuf->chan->alloc_size;
1077ebf99093STom Zanussi 	size_t avail = subbuf_size - read_start % subbuf_size;
1078ebf99093STom Zanussi 	size_t read_subbuf = read_start / subbuf_size;
1079ebf99093STom Zanussi 	size_t padding = rbuf->padding[read_subbuf];
1080ebf99093STom Zanussi 	size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
1081ebf99093STom Zanussi 
1082ebf99093STom Zanussi 	if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1083ebf99093STom Zanussi 		return 0;
1084ebf99093STom Zanussi 
1085ebf99093STom Zanussi 	if (len > avail)
1086ebf99093STom Zanussi 		len = avail;
1087ebf99093STom Zanussi 
1088ebf99093STom Zanussi 	if (pipe->inode)
1089ebf99093STom Zanussi 		mutex_lock(&pipe->inode->i_mutex);
1090ebf99093STom Zanussi 
1091ebf99093STom Zanussi 	subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1092ebf99093STom Zanussi 	pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1093ebf99093STom Zanussi 	poff = read_start & ~PAGE_MASK;
1094ebf99093STom Zanussi 
1095ebf99093STom Zanussi 	for (;;) {
1096ebf99093STom Zanussi 		unsigned int this_len;
1097ebf99093STom Zanussi 		unsigned int this_end;
1098ebf99093STom Zanussi 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
1099ebf99093STom Zanussi 		struct pipe_buffer *buf = pipe->bufs + newbuf;
1100ebf99093STom Zanussi 
1101ebf99093STom Zanussi 		if (!pipe->readers) {
1102ebf99093STom Zanussi 			send_sig(SIGPIPE, current, 0);
1103ebf99093STom Zanussi 			if (!ret)
1104ebf99093STom Zanussi 				ret = -EPIPE;
1105ebf99093STom Zanussi 			break;
1106ebf99093STom Zanussi 		}
1107ebf99093STom Zanussi 
1108ebf99093STom Zanussi 		if (pipe->nrbufs < PIPE_BUFFERS) {
1109ebf99093STom Zanussi 			this_len = PAGE_SIZE - poff;
1110ebf99093STom Zanussi 			if (this_len > avail)
1111ebf99093STom Zanussi 				this_len = avail;
1112ebf99093STom Zanussi 
1113ebf99093STom Zanussi 			buf->page = rbuf->page_array[pidx];
1114ebf99093STom Zanussi 			buf->offset = poff;
1115ebf99093STom Zanussi 			this_end = read_start + ret + this_len;
1116ebf99093STom Zanussi 			if (this_end > nonpad_end) {
1117ebf99093STom Zanussi 				if (read_start + ret >= nonpad_end)
1118ebf99093STom Zanussi 					buf->len = 0;
1119ebf99093STom Zanussi 				else
1120ebf99093STom Zanussi 					buf->len = nonpad_end - (read_start + ret);
1121ebf99093STom Zanussi 			} else
1122ebf99093STom Zanussi 				buf->len = this_len;
1123ebf99093STom Zanussi 
1124ebf99093STom Zanussi 			*nonpad_ret += buf->len;
1125ebf99093STom Zanussi 
1126ebf99093STom Zanussi 			buf->ops = &relay_pipe_buf_ops;
1127ebf99093STom Zanussi 			pipe->nrbufs++;
1128ebf99093STom Zanussi 
1129ebf99093STom Zanussi 			avail -= this_len;
1130ebf99093STom Zanussi 			ret += this_len;
1131ebf99093STom Zanussi 			poff = 0;
1132ebf99093STom Zanussi 			pidx = (pidx + 1) % subbuf_pages;
1133ebf99093STom Zanussi 
1134ebf99093STom Zanussi 			if (pipe->inode)
1135ebf99093STom Zanussi 				do_wakeup = 1;
1136ebf99093STom Zanussi 
1137ebf99093STom Zanussi 			if (!avail)
1138ebf99093STom Zanussi 				break;
1139ebf99093STom Zanussi 
1140ebf99093STom Zanussi 			if (pipe->nrbufs < PIPE_BUFFERS)
1141ebf99093STom Zanussi 				continue;
1142ebf99093STom Zanussi 
1143ebf99093STom Zanussi 			break;
1144ebf99093STom Zanussi 		}
1145ebf99093STom Zanussi 
1146ebf99093STom Zanussi 		if (flags & SPLICE_F_NONBLOCK) {
1147ebf99093STom Zanussi 			if (!ret)
1148ebf99093STom Zanussi 				ret = -EAGAIN;
1149ebf99093STom Zanussi 			break;
1150ebf99093STom Zanussi 		}
1151ebf99093STom Zanussi 
1152ebf99093STom Zanussi 		if (signal_pending(current)) {
1153ebf99093STom Zanussi 			if (!ret)
1154ebf99093STom Zanussi 				ret = -ERESTARTSYS;
1155ebf99093STom Zanussi 			break;
1156ebf99093STom Zanussi 		}
1157ebf99093STom Zanussi 
1158ebf99093STom Zanussi 		if (do_wakeup) {
1159ebf99093STom Zanussi 			smp_mb();
1160ebf99093STom Zanussi 			if (waitqueue_active(&pipe->wait))
1161ebf99093STom Zanussi 				wake_up_interruptible_sync(&pipe->wait);
1162ebf99093STom Zanussi 			kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1163ebf99093STom Zanussi 			do_wakeup = 0;
1164ebf99093STom Zanussi 		}
1165ebf99093STom Zanussi 
1166ebf99093STom Zanussi 		pipe->waiting_writers++;
1167ebf99093STom Zanussi 		pipe_wait(pipe);
1168ebf99093STom Zanussi 		pipe->waiting_writers--;
1169ebf99093STom Zanussi 	}
1170ebf99093STom Zanussi 
1171ebf99093STom Zanussi 	if (pipe->inode)
1172ebf99093STom Zanussi 		mutex_unlock(&pipe->inode->i_mutex);
1173ebf99093STom Zanussi 
1174ebf99093STom Zanussi 	if (do_wakeup) {
1175ebf99093STom Zanussi 		smp_mb();
1176ebf99093STom Zanussi 		if (waitqueue_active(&pipe->wait))
1177ebf99093STom Zanussi 			wake_up_interruptible(&pipe->wait);
1178ebf99093STom Zanussi 		kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1179ebf99093STom Zanussi 	}
1180ebf99093STom Zanussi 
1181ebf99093STom Zanussi 	return ret;
1182ebf99093STom Zanussi }
1183ebf99093STom Zanussi 
1184ebf99093STom Zanussi static ssize_t relay_file_splice_read(struct file *in,
1185ebf99093STom Zanussi 				      loff_t *ppos,
1186ebf99093STom Zanussi 				      struct pipe_inode_info *pipe,
1187ebf99093STom Zanussi 				      size_t len,
1188ebf99093STom Zanussi 				      unsigned int flags)
1189ebf99093STom Zanussi {
1190ebf99093STom Zanussi 	ssize_t spliced;
1191ebf99093STom Zanussi 	int ret;
1192ebf99093STom Zanussi 	int nonpad_ret = 0;
1193ebf99093STom Zanussi 
1194ebf99093STom Zanussi 	ret = 0;
1195ebf99093STom Zanussi 	spliced = 0;
1196ebf99093STom Zanussi 
1197ebf99093STom Zanussi 	while (len) {
1198ebf99093STom Zanussi 		ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1199ebf99093STom Zanussi 		if (ret < 0)
1200ebf99093STom Zanussi 			break;
1201ebf99093STom Zanussi 		else if (!ret) {
1202ebf99093STom Zanussi 			break;
1203ebf99093STom Zanussi 			if (spliced)
1204ebf99093STom Zanussi 				break;
1205ebf99093STom Zanussi 			if (flags & SPLICE_F_NONBLOCK) {
1206ebf99093STom Zanussi 				ret = -EAGAIN;
1207ebf99093STom Zanussi 				break;
1208ebf99093STom Zanussi 			}
1209ebf99093STom Zanussi 		}
1210ebf99093STom Zanussi 
1211ebf99093STom Zanussi 		*ppos += ret;
1212ebf99093STom Zanussi 		if (ret > len)
1213ebf99093STom Zanussi 			len = 0;
1214ebf99093STom Zanussi 		else
1215ebf99093STom Zanussi 			len -= ret;
1216ebf99093STom Zanussi 		spliced += nonpad_ret;
1217ebf99093STom Zanussi 		nonpad_ret = 0;
1218ebf99093STom Zanussi 	}
1219ebf99093STom Zanussi 
1220ebf99093STom Zanussi 	if (spliced)
1221ebf99093STom Zanussi 		return spliced;
1222ebf99093STom Zanussi 
1223ebf99093STom Zanussi 	return ret;
1224221415d7SJens Axboe }
1225221415d7SJens Axboe 
122615ad7cdcSHelge Deller const struct file_operations relay_file_operations = {
1227b86ff981SJens Axboe 	.open		= relay_file_open,
1228b86ff981SJens Axboe 	.poll		= relay_file_poll,
1229b86ff981SJens Axboe 	.mmap		= relay_file_mmap,
1230b86ff981SJens Axboe 	.read		= relay_file_read,
1231b86ff981SJens Axboe 	.llseek		= no_llseek,
1232b86ff981SJens Axboe 	.release	= relay_file_release,
1233ebf99093STom Zanussi 	.splice_read	= relay_file_splice_read,
1234b86ff981SJens Axboe };
1235b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations);
123623c88752SMathieu Desnoyers 
123723c88752SMathieu Desnoyers static __init int relay_init(void)
123823c88752SMathieu Desnoyers {
123923c88752SMathieu Desnoyers 
124023c88752SMathieu Desnoyers 	hotcpu_notifier(relay_hotcpu_callback, 0);
124123c88752SMathieu Desnoyers 	return 0;
124223c88752SMathieu Desnoyers }
124323c88752SMathieu Desnoyers 
124423c88752SMathieu Desnoyers module_init(relay_init);
1245