xref: /linux/kernel/relay.c (revision 01c55ed3260e130f152b7fbab2e18f23980b59a4)
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>
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 /*
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  */
83*01c55ed3SAdrian Bunk static 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  */
148*01c55ed3SAdrian Bunk static 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  */
178*01c55ed3SAdrian Bunk static 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  */
188*01c55ed3SAdrian Bunk static 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  */
213*01c55ed3SAdrian Bunk static 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  */
226*01c55ed3SAdrian Bunk static 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 
231b86ff981SJens Axboe /**
232b86ff981SJens Axboe  *	relay_buf_full - boolean, is the channel buffer full?
233b86ff981SJens Axboe  *	@buf: channel buffer
234b86ff981SJens Axboe  *
235b86ff981SJens Axboe  *	Returns 1 if the buffer is full, 0 otherwise.
236b86ff981SJens Axboe  */
237b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf)
238b86ff981SJens Axboe {
239b86ff981SJens Axboe 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
240b86ff981SJens Axboe 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
241b86ff981SJens Axboe }
242b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full);
243b86ff981SJens Axboe 
244b86ff981SJens Axboe /*
245b86ff981SJens Axboe  * High-level relay kernel API and associated functions.
246b86ff981SJens Axboe  */
247b86ff981SJens Axboe 
248b86ff981SJens Axboe /*
249b86ff981SJens Axboe  * rchan_callback implementations defining default channel behavior.  Used
250b86ff981SJens Axboe  * in place of corresponding NULL values in client callback struct.
251b86ff981SJens Axboe  */
252b86ff981SJens Axboe 
253b86ff981SJens Axboe /*
254b86ff981SJens Axboe  * subbuf_start() default callback.  Does nothing.
255b86ff981SJens Axboe  */
256b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf,
257b86ff981SJens Axboe 					  void *subbuf,
258b86ff981SJens Axboe 					  void *prev_subbuf,
259b86ff981SJens Axboe 					  size_t prev_padding)
260b86ff981SJens Axboe {
261b86ff981SJens Axboe 	if (relay_buf_full(buf))
262b86ff981SJens Axboe 		return 0;
263b86ff981SJens Axboe 
264b86ff981SJens Axboe 	return 1;
265b86ff981SJens Axboe }
266b86ff981SJens Axboe 
267b86ff981SJens Axboe /*
268b86ff981SJens Axboe  * buf_mapped() default callback.  Does nothing.
269b86ff981SJens Axboe  */
270b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf,
271b86ff981SJens Axboe 					struct file *filp)
272b86ff981SJens Axboe {
273b86ff981SJens Axboe }
274b86ff981SJens Axboe 
275b86ff981SJens Axboe /*
276b86ff981SJens Axboe  * buf_unmapped() default callback.  Does nothing.
277b86ff981SJens Axboe  */
278b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf,
279b86ff981SJens Axboe 					  struct file *filp)
280b86ff981SJens Axboe {
281b86ff981SJens Axboe }
282b86ff981SJens Axboe 
283b86ff981SJens Axboe /*
284b86ff981SJens Axboe  * create_buf_file_create() default callback.  Does nothing.
285b86ff981SJens Axboe  */
286b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename,
287b86ff981SJens Axboe 						       struct dentry *parent,
288b86ff981SJens Axboe 						       int mode,
289b86ff981SJens Axboe 						       struct rchan_buf *buf,
290b86ff981SJens Axboe 						       int *is_global)
291b86ff981SJens Axboe {
292b86ff981SJens Axboe 	return NULL;
293b86ff981SJens Axboe }
294b86ff981SJens Axboe 
295b86ff981SJens Axboe /*
296b86ff981SJens Axboe  * remove_buf_file() default callback.  Does nothing.
297b86ff981SJens Axboe  */
298b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry)
299b86ff981SJens Axboe {
300b86ff981SJens Axboe 	return -EINVAL;
301b86ff981SJens Axboe }
302b86ff981SJens Axboe 
303b86ff981SJens Axboe /* relay channel default callbacks */
304b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = {
305b86ff981SJens Axboe 	.subbuf_start = subbuf_start_default_callback,
306b86ff981SJens Axboe 	.buf_mapped = buf_mapped_default_callback,
307b86ff981SJens Axboe 	.buf_unmapped = buf_unmapped_default_callback,
308b86ff981SJens Axboe 	.create_buf_file = create_buf_file_default_callback,
309b86ff981SJens Axboe 	.remove_buf_file = remove_buf_file_default_callback,
310b86ff981SJens Axboe };
311b86ff981SJens Axboe 
312b86ff981SJens Axboe /**
313b86ff981SJens Axboe  *	wakeup_readers - wake up readers waiting on a channel
3149a9136e2SLinus Torvalds  *	@data: contains the channel buffer
315b86ff981SJens Axboe  *
3167c9cb383STom Zanussi  *	This is the timer function used to defer reader waking.
317b86ff981SJens Axboe  */
3187c9cb383STom Zanussi static void wakeup_readers(unsigned long data)
319b86ff981SJens Axboe {
3207c9cb383STom Zanussi 	struct rchan_buf *buf = (struct rchan_buf *)data;
321b86ff981SJens Axboe 	wake_up_interruptible(&buf->read_wait);
322b86ff981SJens Axboe }
323b86ff981SJens Axboe 
324b86ff981SJens Axboe /**
325b86ff981SJens Axboe  *	__relay_reset - reset a channel buffer
326b86ff981SJens Axboe  *	@buf: the channel buffer
327b86ff981SJens Axboe  *	@init: 1 if this is a first-time initialization
328b86ff981SJens Axboe  *
32972fd4a35SRobert P. J. Day  *	See relay_reset() for description of effect.
330b86ff981SJens Axboe  */
331192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init)
332b86ff981SJens Axboe {
333b86ff981SJens Axboe 	size_t i;
334b86ff981SJens Axboe 
335b86ff981SJens Axboe 	if (init) {
336b86ff981SJens Axboe 		init_waitqueue_head(&buf->read_wait);
337b86ff981SJens Axboe 		kref_init(&buf->kref);
3387c9cb383STom Zanussi 		setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
3397c9cb383STom Zanussi 	} else
3407c9cb383STom Zanussi 		del_timer_sync(&buf->timer);
341b86ff981SJens Axboe 
342b86ff981SJens Axboe 	buf->subbufs_produced = 0;
343b86ff981SJens Axboe 	buf->subbufs_consumed = 0;
344b86ff981SJens Axboe 	buf->bytes_consumed = 0;
345b86ff981SJens Axboe 	buf->finalized = 0;
346b86ff981SJens Axboe 	buf->data = buf->start;
347b86ff981SJens Axboe 	buf->offset = 0;
348b86ff981SJens Axboe 
349b86ff981SJens Axboe 	for (i = 0; i < buf->chan->n_subbufs; i++)
350b86ff981SJens Axboe 		buf->padding[i] = 0;
351b86ff981SJens Axboe 
352b86ff981SJens Axboe 	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
353b86ff981SJens Axboe }
354b86ff981SJens Axboe 
355b86ff981SJens Axboe /**
356b86ff981SJens Axboe  *	relay_reset - reset the channel
357b86ff981SJens Axboe  *	@chan: the channel
358b86ff981SJens Axboe  *
359b86ff981SJens Axboe  *	This has the effect of erasing all data from all channel buffers
360b86ff981SJens Axboe  *	and restarting the channel in its initial state.  The buffers
361b86ff981SJens Axboe  *	are not freed, so any mappings are still in effect.
362b86ff981SJens Axboe  *
36372fd4a35SRobert P. J. Day  *	NOTE. Care should be taken that the channel isn't actually
364b86ff981SJens Axboe  *	being used by anything when this call is made.
365b86ff981SJens Axboe  */
366b86ff981SJens Axboe void relay_reset(struct rchan *chan)
367b86ff981SJens Axboe {
368b86ff981SJens Axboe 	unsigned int i;
369b86ff981SJens Axboe 
370b86ff981SJens Axboe 	if (!chan)
371b86ff981SJens Axboe 		return;
372b86ff981SJens Axboe 
37323c88752SMathieu Desnoyers  	if (chan->is_global && chan->buf[0]) {
37423c88752SMathieu Desnoyers 		__relay_reset(chan->buf[0], 0);
37523c88752SMathieu Desnoyers 		return;
376b86ff981SJens Axboe 	}
37723c88752SMathieu Desnoyers 
37823c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
37923c88752SMathieu Desnoyers 	for_each_online_cpu(i)
38023c88752SMathieu Desnoyers 		if (chan->buf[i])
38123c88752SMathieu Desnoyers 			__relay_reset(chan->buf[i], 0);
38223c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
383b86ff981SJens Axboe }
384b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset);
385b86ff981SJens Axboe 
3864c78a663SRandy Dunlap /*
387b86ff981SJens Axboe  *	relay_open_buf - create a new relay channel buffer
388b86ff981SJens Axboe  *
38923c88752SMathieu Desnoyers  *	used by relay_open() and CPU hotplug.
390b86ff981SJens Axboe  */
39123c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
392b86ff981SJens Axboe {
39323c88752SMathieu Desnoyers  	struct rchan_buf *buf = NULL;
394b86ff981SJens Axboe 	struct dentry *dentry;
39523c88752SMathieu Desnoyers  	char *tmpname;
396b86ff981SJens Axboe 
39723c88752SMathieu Desnoyers  	if (chan->is_global)
398b86ff981SJens Axboe 		return chan->buf[0];
399b86ff981SJens Axboe 
40023c88752SMathieu Desnoyers 	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
40123c88752SMathieu Desnoyers  	if (!tmpname)
40223c88752SMathieu Desnoyers  		goto end;
40323c88752SMathieu Desnoyers  	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
40423c88752SMathieu Desnoyers 
405b86ff981SJens Axboe 	buf = relay_create_buf(chan);
406b86ff981SJens Axboe 	if (!buf)
40723c88752SMathieu Desnoyers  		goto free_name;
408b86ff981SJens Axboe 
40923c88752SMathieu Desnoyers  	buf->cpu = cpu;
410b86ff981SJens Axboe  	__relay_reset(buf, 1);
411b86ff981SJens Axboe 
41223c88752SMathieu Desnoyers 	/* Create file in fs */
41323c88752SMathieu Desnoyers  	dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
41423c88752SMathieu Desnoyers  					   buf, &chan->is_global);
41523c88752SMathieu Desnoyers  	if (!dentry)
41623c88752SMathieu Desnoyers  		goto free_buf;
41723c88752SMathieu Desnoyers 
41823c88752SMathieu Desnoyers 	buf->dentry = dentry;
41923c88752SMathieu Desnoyers 
42023c88752SMathieu Desnoyers  	if(chan->is_global) {
42123c88752SMathieu Desnoyers  		chan->buf[0] = buf;
42223c88752SMathieu Desnoyers  		buf->cpu = 0;
42323c88752SMathieu Desnoyers   	}
42423c88752SMathieu Desnoyers 
42523c88752SMathieu Desnoyers  	goto free_name;
42623c88752SMathieu Desnoyers 
42723c88752SMathieu Desnoyers free_buf:
42823c88752SMathieu Desnoyers  	relay_destroy_buf(buf);
42923c88752SMathieu Desnoyers free_name:
43023c88752SMathieu Desnoyers  	kfree(tmpname);
43123c88752SMathieu Desnoyers end:
432b86ff981SJens Axboe 	return buf;
433b86ff981SJens Axboe }
434b86ff981SJens Axboe 
435b86ff981SJens Axboe /**
436b86ff981SJens Axboe  *	relay_close_buf - close a channel buffer
437b86ff981SJens Axboe  *	@buf: channel buffer
438b86ff981SJens Axboe  *
439b86ff981SJens Axboe  *	Marks the buffer finalized and restores the default callbacks.
440b86ff981SJens Axboe  *	The channel buffer and channel buffer data structure are then freed
441b86ff981SJens Axboe  *	automatically when the last reference is given up.
442b86ff981SJens Axboe  */
443192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf)
444b86ff981SJens Axboe {
445b86ff981SJens Axboe 	buf->finalized = 1;
4467c9cb383STom Zanussi 	del_timer_sync(&buf->timer);
447b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
448b86ff981SJens Axboe }
449b86ff981SJens Axboe 
450192636adSAndrew Morton static void setup_callbacks(struct rchan *chan,
451b86ff981SJens Axboe 				   struct rchan_callbacks *cb)
452b86ff981SJens Axboe {
453b86ff981SJens Axboe 	if (!cb) {
454b86ff981SJens Axboe 		chan->cb = &default_channel_callbacks;
455b86ff981SJens Axboe 		return;
456b86ff981SJens Axboe 	}
457b86ff981SJens Axboe 
458b86ff981SJens Axboe 	if (!cb->subbuf_start)
459b86ff981SJens Axboe 		cb->subbuf_start = subbuf_start_default_callback;
460b86ff981SJens Axboe 	if (!cb->buf_mapped)
461b86ff981SJens Axboe 		cb->buf_mapped = buf_mapped_default_callback;
462b86ff981SJens Axboe 	if (!cb->buf_unmapped)
463b86ff981SJens Axboe 		cb->buf_unmapped = buf_unmapped_default_callback;
464b86ff981SJens Axboe 	if (!cb->create_buf_file)
465b86ff981SJens Axboe 		cb->create_buf_file = create_buf_file_default_callback;
466b86ff981SJens Axboe 	if (!cb->remove_buf_file)
467b86ff981SJens Axboe 		cb->remove_buf_file = remove_buf_file_default_callback;
468b86ff981SJens Axboe 	chan->cb = cb;
469b86ff981SJens Axboe }
470b86ff981SJens Axboe 
471b86ff981SJens Axboe /**
47223c88752SMathieu Desnoyers  * 	relay_hotcpu_callback - CPU hotplug callback
47323c88752SMathieu Desnoyers  * 	@nb: notifier block
47423c88752SMathieu Desnoyers  * 	@action: hotplug action to take
47523c88752SMathieu Desnoyers  * 	@hcpu: CPU number
47623c88752SMathieu Desnoyers  *
47705fb6bf0SRandy Dunlap  * 	Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
47823c88752SMathieu Desnoyers  */
47923c88752SMathieu Desnoyers static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
48023c88752SMathieu Desnoyers 				unsigned long action,
48123c88752SMathieu Desnoyers 				void *hcpu)
48223c88752SMathieu Desnoyers {
48323c88752SMathieu Desnoyers 	unsigned int hotcpu = (unsigned long)hcpu;
48423c88752SMathieu Desnoyers 	struct rchan *chan;
48523c88752SMathieu Desnoyers 
48623c88752SMathieu Desnoyers 	switch(action) {
48723c88752SMathieu Desnoyers 	case CPU_UP_PREPARE:
4888bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
48923c88752SMathieu Desnoyers 		mutex_lock(&relay_channels_mutex);
49023c88752SMathieu Desnoyers 		list_for_each_entry(chan, &relay_channels, list) {
49123c88752SMathieu Desnoyers 			if (chan->buf[hotcpu])
49223c88752SMathieu Desnoyers 				continue;
49323c88752SMathieu Desnoyers 			chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
49423c88752SMathieu Desnoyers 			if(!chan->buf[hotcpu]) {
49523c88752SMathieu Desnoyers 				printk(KERN_ERR
49623c88752SMathieu Desnoyers 					"relay_hotcpu_callback: cpu %d buffer "
49723c88752SMathieu Desnoyers 					"creation failed\n", hotcpu);
49823c88752SMathieu Desnoyers 				mutex_unlock(&relay_channels_mutex);
49923c88752SMathieu Desnoyers 				return NOTIFY_BAD;
50023c88752SMathieu Desnoyers 			}
50123c88752SMathieu Desnoyers 		}
50223c88752SMathieu Desnoyers 		mutex_unlock(&relay_channels_mutex);
50323c88752SMathieu Desnoyers 		break;
50423c88752SMathieu Desnoyers 	case CPU_DEAD:
5058bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
50623c88752SMathieu Desnoyers 		/* No need to flush the cpu : will be flushed upon
50723c88752SMathieu Desnoyers 		 * final relay_flush() call. */
50823c88752SMathieu Desnoyers 		break;
50923c88752SMathieu Desnoyers 	}
51023c88752SMathieu Desnoyers 	return NOTIFY_OK;
51123c88752SMathieu Desnoyers }
51223c88752SMathieu Desnoyers 
51323c88752SMathieu Desnoyers /**
514b86ff981SJens Axboe  *	relay_open - create a new relay channel
515b86ff981SJens Axboe  *	@base_filename: base name of files to create
5164c78a663SRandy Dunlap  *	@parent: dentry of parent directory, %NULL for root directory
517b86ff981SJens Axboe  *	@subbuf_size: size of sub-buffers
518b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers
519b86ff981SJens Axboe  *	@cb: client callback functions
52023c88752SMathieu Desnoyers  *	@private_data: user-defined data
521b86ff981SJens Axboe  *
5224c78a663SRandy Dunlap  *	Returns channel pointer if successful, %NULL otherwise.
523b86ff981SJens Axboe  *
524b86ff981SJens Axboe  *	Creates a channel buffer for each cpu using the sizes and
525b86ff981SJens Axboe  *	attributes specified.  The created channel buffer files
526b86ff981SJens Axboe  *	will be named base_filename0...base_filenameN-1.  File
52772fd4a35SRobert P. J. Day  *	permissions will be %S_IRUSR.
528b86ff981SJens Axboe  */
529b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
530b86ff981SJens Axboe 			 struct dentry *parent,
531b86ff981SJens Axboe 			 size_t subbuf_size,
532b86ff981SJens Axboe 			 size_t n_subbufs,
53323c88752SMathieu Desnoyers 			 struct rchan_callbacks *cb,
53423c88752SMathieu Desnoyers 			 void *private_data)
535b86ff981SJens Axboe {
536b86ff981SJens Axboe 	unsigned int i;
537b86ff981SJens Axboe 	struct rchan *chan;
538b86ff981SJens Axboe 	if (!base_filename)
539b86ff981SJens Axboe 		return NULL;
540b86ff981SJens Axboe 
541b86ff981SJens Axboe 	if (!(subbuf_size && n_subbufs))
542b86ff981SJens Axboe 		return NULL;
543b86ff981SJens Axboe 
544cd861280SRobert P. J. Day 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
545b86ff981SJens Axboe 	if (!chan)
546b86ff981SJens Axboe 		return NULL;
547b86ff981SJens Axboe 
548b86ff981SJens Axboe 	chan->version = RELAYFS_CHANNEL_VERSION;
549b86ff981SJens Axboe 	chan->n_subbufs = n_subbufs;
550b86ff981SJens Axboe 	chan->subbuf_size = subbuf_size;
551b86ff981SJens Axboe 	chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
55223c88752SMathieu Desnoyers 	chan->parent = parent;
55323c88752SMathieu Desnoyers 	chan->private_data = private_data;
55423c88752SMathieu Desnoyers 	strlcpy(chan->base_filename, base_filename, NAME_MAX);
555b86ff981SJens Axboe 	setup_callbacks(chan, cb);
556b86ff981SJens Axboe 	kref_init(&chan->kref);
557b86ff981SJens Axboe 
55823c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
559b86ff981SJens Axboe 	for_each_online_cpu(i) {
56023c88752SMathieu Desnoyers 		chan->buf[i] = relay_open_buf(chan, i);
561b86ff981SJens Axboe 		if (!chan->buf[i])
562b86ff981SJens Axboe 			goto free_bufs;
563b86ff981SJens Axboe 	}
56423c88752SMathieu Desnoyers 	list_add(&chan->list, &relay_channels);
56523c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
566b86ff981SJens Axboe 
567b86ff981SJens Axboe 	return chan;
568b86ff981SJens Axboe 
569b86ff981SJens Axboe free_bufs:
57023c88752SMathieu Desnoyers 	for_each_online_cpu(i) {
571b86ff981SJens Axboe 		if (!chan->buf[i])
572b86ff981SJens Axboe 			break;
573b86ff981SJens Axboe 		relay_close_buf(chan->buf[i]);
574b86ff981SJens Axboe 	}
575b86ff981SJens Axboe 
576b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
57723c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
578b86ff981SJens Axboe 	return NULL;
579b86ff981SJens Axboe }
580b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open);
581b86ff981SJens Axboe 
582b86ff981SJens Axboe /**
583b86ff981SJens Axboe  *	relay_switch_subbuf - switch to a new sub-buffer
584b86ff981SJens Axboe  *	@buf: channel buffer
585b86ff981SJens Axboe  *	@length: size of current event
586b86ff981SJens Axboe  *
587b86ff981SJens Axboe  *	Returns either the length passed in or 0 if full.
588b86ff981SJens Axboe  *
589b86ff981SJens Axboe  *	Performs sub-buffer-switch tasks such as invoking callbacks,
590b86ff981SJens Axboe  *	updating padding counts, waking up readers, etc.
591b86ff981SJens Axboe  */
592b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
593b86ff981SJens Axboe {
594b86ff981SJens Axboe 	void *old, *new;
595b86ff981SJens Axboe 	size_t old_subbuf, new_subbuf;
596b86ff981SJens Axboe 
597b86ff981SJens Axboe 	if (unlikely(length > buf->chan->subbuf_size))
598b86ff981SJens Axboe 		goto toobig;
599b86ff981SJens Axboe 
600b86ff981SJens Axboe 	if (buf->offset != buf->chan->subbuf_size + 1) {
601b86ff981SJens Axboe 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
602b86ff981SJens Axboe 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
603b86ff981SJens Axboe 		buf->padding[old_subbuf] = buf->prev_padding;
604b86ff981SJens Axboe 		buf->subbufs_produced++;
605221415d7SJens Axboe 		buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
606221415d7SJens Axboe 			buf->padding[old_subbuf];
607221415d7SJens Axboe 		smp_mb();
6087c9cb383STom Zanussi 		if (waitqueue_active(&buf->read_wait))
6097c9cb383STom Zanussi 			/*
6107c9cb383STom Zanussi 			 * Calling wake_up_interruptible() from here
6117c9cb383STom Zanussi 			 * will deadlock if we happen to be logging
6127c9cb383STom Zanussi 			 * from the scheduler (trying to re-grab
6137c9cb383STom Zanussi 			 * rq->lock), so defer it.
6147c9cb383STom Zanussi 			 */
6157c9cb383STom Zanussi 			__mod_timer(&buf->timer, jiffies + 1);
616b86ff981SJens Axboe 	}
617b86ff981SJens Axboe 
618b86ff981SJens Axboe 	old = buf->data;
619b86ff981SJens Axboe 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
620b86ff981SJens Axboe 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
621b86ff981SJens Axboe 	buf->offset = 0;
622b86ff981SJens Axboe 	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
623b86ff981SJens Axboe 		buf->offset = buf->chan->subbuf_size + 1;
624b86ff981SJens Axboe 		return 0;
625b86ff981SJens Axboe 	}
626b86ff981SJens Axboe 	buf->data = new;
627b86ff981SJens Axboe 	buf->padding[new_subbuf] = 0;
628b86ff981SJens Axboe 
629b86ff981SJens Axboe 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
630b86ff981SJens Axboe 		goto toobig;
631b86ff981SJens Axboe 
632b86ff981SJens Axboe 	return length;
633b86ff981SJens Axboe 
634b86ff981SJens Axboe toobig:
635b86ff981SJens Axboe 	buf->chan->last_toobig = length;
636b86ff981SJens Axboe 	return 0;
637b86ff981SJens Axboe }
638b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf);
639b86ff981SJens Axboe 
640b86ff981SJens Axboe /**
641b86ff981SJens Axboe  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
642b86ff981SJens Axboe  *	@chan: the channel
643b86ff981SJens Axboe  *	@cpu: the cpu associated with the channel buffer to update
644b86ff981SJens Axboe  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
645b86ff981SJens Axboe  *
646b86ff981SJens Axboe  *	Adds to the channel buffer's consumed sub-buffer count.
647b86ff981SJens Axboe  *	subbufs_consumed should be the number of sub-buffers newly consumed,
648b86ff981SJens Axboe  *	not the total consumed.
649b86ff981SJens Axboe  *
65072fd4a35SRobert P. J. Day  *	NOTE. Kernel clients don't need to call this function if the channel
651b86ff981SJens Axboe  *	mode is 'overwrite'.
652b86ff981SJens Axboe  */
653b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan,
654b86ff981SJens Axboe 			    unsigned int cpu,
655b86ff981SJens Axboe 			    size_t subbufs_consumed)
656b86ff981SJens Axboe {
657b86ff981SJens Axboe 	struct rchan_buf *buf;
658b86ff981SJens Axboe 
659b86ff981SJens Axboe 	if (!chan)
660b86ff981SJens Axboe 		return;
661b86ff981SJens Axboe 
662b86ff981SJens Axboe 	if (cpu >= NR_CPUS || !chan->buf[cpu])
663b86ff981SJens Axboe 		return;
664b86ff981SJens Axboe 
665b86ff981SJens Axboe 	buf = chan->buf[cpu];
666b86ff981SJens Axboe 	buf->subbufs_consumed += subbufs_consumed;
667b86ff981SJens Axboe 	if (buf->subbufs_consumed > buf->subbufs_produced)
668b86ff981SJens Axboe 		buf->subbufs_consumed = buf->subbufs_produced;
669b86ff981SJens Axboe }
670b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
671b86ff981SJens Axboe 
672b86ff981SJens Axboe /**
673b86ff981SJens Axboe  *	relay_close - close the channel
674b86ff981SJens Axboe  *	@chan: the channel
675b86ff981SJens Axboe  *
676b86ff981SJens Axboe  *	Closes all channel buffers and frees the channel.
677b86ff981SJens Axboe  */
678b86ff981SJens Axboe void relay_close(struct rchan *chan)
679b86ff981SJens Axboe {
680b86ff981SJens Axboe 	unsigned int i;
681b86ff981SJens Axboe 
682b86ff981SJens Axboe 	if (!chan)
683b86ff981SJens Axboe 		return;
684b86ff981SJens Axboe 
68523c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
68623c88752SMathieu Desnoyers 	if (chan->is_global && chan->buf[0])
68723c88752SMathieu Desnoyers 		relay_close_buf(chan->buf[0]);
68823c88752SMathieu Desnoyers 	else
68923c88752SMathieu Desnoyers 		for_each_possible_cpu(i)
69023c88752SMathieu Desnoyers 			if (chan->buf[i])
691b86ff981SJens Axboe 				relay_close_buf(chan->buf[i]);
692b86ff981SJens Axboe 
693b86ff981SJens Axboe 	if (chan->last_toobig)
694b86ff981SJens Axboe 		printk(KERN_WARNING "relay: one or more items not logged "
695b86ff981SJens Axboe 		       "[item size (%Zd) > sub-buffer size (%Zd)]\n",
696b86ff981SJens Axboe 		       chan->last_toobig, chan->subbuf_size);
697b86ff981SJens Axboe 
69823c88752SMathieu Desnoyers 	list_del(&chan->list);
699b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
70023c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
701b86ff981SJens Axboe }
702b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close);
703b86ff981SJens Axboe 
704b86ff981SJens Axboe /**
705b86ff981SJens Axboe  *	relay_flush - close the channel
706b86ff981SJens Axboe  *	@chan: the channel
707b86ff981SJens Axboe  *
7084c78a663SRandy Dunlap  *	Flushes all channel buffers, i.e. forces buffer switch.
709b86ff981SJens Axboe  */
710b86ff981SJens Axboe void relay_flush(struct rchan *chan)
711b86ff981SJens Axboe {
712b86ff981SJens Axboe 	unsigned int i;
713b86ff981SJens Axboe 
714b86ff981SJens Axboe 	if (!chan)
715b86ff981SJens Axboe 		return;
716b86ff981SJens Axboe 
71723c88752SMathieu Desnoyers 	if (chan->is_global && chan->buf[0]) {
71823c88752SMathieu Desnoyers 		relay_switch_subbuf(chan->buf[0], 0);
71923c88752SMathieu Desnoyers 		return;
720b86ff981SJens Axboe 	}
72123c88752SMathieu Desnoyers 
72223c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
72323c88752SMathieu Desnoyers 	for_each_possible_cpu(i)
72423c88752SMathieu Desnoyers 		if (chan->buf[i])
72523c88752SMathieu Desnoyers 			relay_switch_subbuf(chan->buf[i], 0);
72623c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
727b86ff981SJens Axboe }
728b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush);
729b86ff981SJens Axboe 
730b86ff981SJens Axboe /**
731b86ff981SJens Axboe  *	relay_file_open - open file op for relay files
732b86ff981SJens Axboe  *	@inode: the inode
733b86ff981SJens Axboe  *	@filp: the file
734b86ff981SJens Axboe  *
735b86ff981SJens Axboe  *	Increments the channel buffer refcount.
736b86ff981SJens Axboe  */
737b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp)
738b86ff981SJens Axboe {
7398e18e294STheodore Ts'o 	struct rchan_buf *buf = inode->i_private;
740b86ff981SJens Axboe 	kref_get(&buf->kref);
741b86ff981SJens Axboe 	filp->private_data = buf;
742b86ff981SJens Axboe 
743b86ff981SJens Axboe 	return 0;
744b86ff981SJens Axboe }
745b86ff981SJens Axboe 
746b86ff981SJens Axboe /**
747b86ff981SJens Axboe  *	relay_file_mmap - mmap file op for relay files
748b86ff981SJens Axboe  *	@filp: the file
749b86ff981SJens Axboe  *	@vma: the vma describing what to map
750b86ff981SJens Axboe  *
75172fd4a35SRobert P. J. Day  *	Calls upon relay_mmap_buf() to map the file into user space.
752b86ff981SJens Axboe  */
753b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
754b86ff981SJens Axboe {
755b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
756b86ff981SJens Axboe 	return relay_mmap_buf(buf, vma);
757b86ff981SJens Axboe }
758b86ff981SJens Axboe 
759b86ff981SJens Axboe /**
760b86ff981SJens Axboe  *	relay_file_poll - poll file op for relay files
761b86ff981SJens Axboe  *	@filp: the file
762b86ff981SJens Axboe  *	@wait: poll table
763b86ff981SJens Axboe  *
764b86ff981SJens Axboe  *	Poll implemention.
765b86ff981SJens Axboe  */
766b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
767b86ff981SJens Axboe {
768b86ff981SJens Axboe 	unsigned int mask = 0;
769b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
770b86ff981SJens Axboe 
771b86ff981SJens Axboe 	if (buf->finalized)
772b86ff981SJens Axboe 		return POLLERR;
773b86ff981SJens Axboe 
774b86ff981SJens Axboe 	if (filp->f_mode & FMODE_READ) {
775b86ff981SJens Axboe 		poll_wait(filp, &buf->read_wait, wait);
776b86ff981SJens Axboe 		if (!relay_buf_empty(buf))
777b86ff981SJens Axboe 			mask |= POLLIN | POLLRDNORM;
778b86ff981SJens Axboe 	}
779b86ff981SJens Axboe 
780b86ff981SJens Axboe 	return mask;
781b86ff981SJens Axboe }
782b86ff981SJens Axboe 
783b86ff981SJens Axboe /**
784b86ff981SJens Axboe  *	relay_file_release - release file op for relay files
785b86ff981SJens Axboe  *	@inode: the inode
786b86ff981SJens Axboe  *	@filp: the file
787b86ff981SJens Axboe  *
788b86ff981SJens Axboe  *	Decrements the channel refcount, as the filesystem is
789b86ff981SJens Axboe  *	no longer using it.
790b86ff981SJens Axboe  */
791b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp)
792b86ff981SJens Axboe {
793b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
794b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
795b86ff981SJens Axboe 
796b86ff981SJens Axboe 	return 0;
797b86ff981SJens Axboe }
798b86ff981SJens Axboe 
7994c78a663SRandy Dunlap /*
800b86ff981SJens Axboe  *	relay_file_read_consume - update the consumed count for the buffer
801b86ff981SJens Axboe  */
802b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf,
803b86ff981SJens Axboe 				    size_t read_pos,
804b86ff981SJens Axboe 				    size_t bytes_consumed)
805b86ff981SJens Axboe {
806b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
807b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
808b86ff981SJens Axboe 	size_t read_subbuf;
809b86ff981SJens Axboe 
810b86ff981SJens Axboe 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
811b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
812b86ff981SJens Axboe 		buf->bytes_consumed = 0;
813b86ff981SJens Axboe 	}
814b86ff981SJens Axboe 
815b86ff981SJens Axboe 	buf->bytes_consumed += bytes_consumed;
816a66e356cSMasami Hiramatsu 	if (!read_pos)
817a66e356cSMasami Hiramatsu 		read_subbuf = buf->subbufs_consumed % n_subbufs;
818a66e356cSMasami Hiramatsu 	else
819b86ff981SJens Axboe 		read_subbuf = read_pos / buf->chan->subbuf_size;
820b86ff981SJens Axboe 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
821b86ff981SJens Axboe 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
822b86ff981SJens Axboe 		    (buf->offset == subbuf_size))
823b86ff981SJens Axboe 			return;
824b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
825b86ff981SJens Axboe 		buf->bytes_consumed = 0;
826b86ff981SJens Axboe 	}
827b86ff981SJens Axboe }
828b86ff981SJens Axboe 
8294c78a663SRandy Dunlap /*
830b86ff981SJens Axboe  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
831b86ff981SJens Axboe  */
832b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
833b86ff981SJens Axboe {
834b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
835b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
836221415d7SJens Axboe 	size_t produced = buf->subbufs_produced;
837221415d7SJens Axboe 	size_t consumed = buf->subbufs_consumed;
838b86ff981SJens Axboe 
839b86ff981SJens Axboe 	relay_file_read_consume(buf, read_pos, 0);
840b86ff981SJens Axboe 
841221415d7SJens Axboe 	if (unlikely(buf->offset > subbuf_size)) {
842221415d7SJens Axboe 		if (produced == consumed)
843221415d7SJens Axboe 			return 0;
844221415d7SJens Axboe 		return 1;
845221415d7SJens Axboe 	}
846221415d7SJens Axboe 
847221415d7SJens Axboe 	if (unlikely(produced - consumed >= n_subbufs)) {
848a66e356cSMasami Hiramatsu 		consumed = produced - n_subbufs + 1;
849221415d7SJens Axboe 		buf->subbufs_consumed = consumed;
850a66e356cSMasami Hiramatsu 		buf->bytes_consumed = 0;
851221415d7SJens Axboe 	}
852221415d7SJens Axboe 
853221415d7SJens Axboe 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
854221415d7SJens Axboe 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
855221415d7SJens Axboe 
856221415d7SJens Axboe 	if (consumed > produced)
857221415d7SJens Axboe 		produced += n_subbufs * subbuf_size;
858221415d7SJens Axboe 
859221415d7SJens Axboe 	if (consumed == produced)
860221415d7SJens Axboe 		return 0;
861221415d7SJens Axboe 
862b86ff981SJens Axboe 	return 1;
863b86ff981SJens Axboe }
864b86ff981SJens Axboe 
865b86ff981SJens Axboe /**
866b86ff981SJens Axboe  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
8674c78a663SRandy Dunlap  *	@read_pos: file read position
8684c78a663SRandy Dunlap  *	@buf: relay channel buffer
869b86ff981SJens Axboe  */
870b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos,
871b86ff981SJens Axboe 					   struct rchan_buf *buf)
872b86ff981SJens Axboe {
873b86ff981SJens Axboe 	size_t padding, avail = 0;
874b86ff981SJens Axboe 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
875b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
876b86ff981SJens Axboe 
877b86ff981SJens Axboe 	write_subbuf = (buf->data - buf->start) / subbuf_size;
878b86ff981SJens Axboe 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
879b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
880b86ff981SJens Axboe 	read_offset = read_pos % subbuf_size;
881b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
882b86ff981SJens Axboe 
883b86ff981SJens Axboe 	if (read_subbuf == write_subbuf) {
884b86ff981SJens Axboe 		if (read_offset + padding < write_offset)
885b86ff981SJens Axboe 			avail = write_offset - (read_offset + padding);
886b86ff981SJens Axboe 	} else
887b86ff981SJens Axboe 		avail = (subbuf_size - padding) - read_offset;
888b86ff981SJens Axboe 
889b86ff981SJens Axboe 	return avail;
890b86ff981SJens Axboe }
891b86ff981SJens Axboe 
892b86ff981SJens Axboe /**
893b86ff981SJens Axboe  *	relay_file_read_start_pos - find the first available byte to read
8944c78a663SRandy Dunlap  *	@read_pos: file read position
8954c78a663SRandy Dunlap  *	@buf: relay channel buffer
896b86ff981SJens Axboe  *
89772fd4a35SRobert P. J. Day  *	If the @read_pos is in the middle of padding, return the
898b86ff981SJens Axboe  *	position of the first actually available byte, otherwise
899b86ff981SJens Axboe  *	return the original value.
900b86ff981SJens Axboe  */
901b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos,
902b86ff981SJens Axboe 					struct rchan_buf *buf)
903b86ff981SJens Axboe {
904b86ff981SJens Axboe 	size_t read_subbuf, padding, padding_start, padding_end;
905b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
906b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
9078d62fdebSDavid Wilder 	size_t consumed = buf->subbufs_consumed % n_subbufs;
908b86ff981SJens Axboe 
9098d62fdebSDavid Wilder 	if (!read_pos)
9108d62fdebSDavid Wilder 		read_pos = consumed * subbuf_size + buf->bytes_consumed;
911b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
912b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
913b86ff981SJens Axboe 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
914b86ff981SJens Axboe 	padding_end = (read_subbuf + 1) * subbuf_size;
915b86ff981SJens Axboe 	if (read_pos >= padding_start && read_pos < padding_end) {
916b86ff981SJens Axboe 		read_subbuf = (read_subbuf + 1) % n_subbufs;
917b86ff981SJens Axboe 		read_pos = read_subbuf * subbuf_size;
918b86ff981SJens Axboe 	}
919b86ff981SJens Axboe 
920b86ff981SJens Axboe 	return read_pos;
921b86ff981SJens Axboe }
922b86ff981SJens Axboe 
923b86ff981SJens Axboe /**
924b86ff981SJens Axboe  *	relay_file_read_end_pos - return the new read position
9254c78a663SRandy Dunlap  *	@read_pos: file read position
9264c78a663SRandy Dunlap  *	@buf: relay channel buffer
9274c78a663SRandy Dunlap  *	@count: number of bytes to be read
928b86ff981SJens Axboe  */
929b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf,
930b86ff981SJens Axboe 				      size_t read_pos,
931b86ff981SJens Axboe 				      size_t count)
932b86ff981SJens Axboe {
933b86ff981SJens Axboe 	size_t read_subbuf, padding, end_pos;
934b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
935b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
936b86ff981SJens Axboe 
937b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
938b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
939b86ff981SJens Axboe 	if (read_pos % subbuf_size + count + padding == subbuf_size)
940b86ff981SJens Axboe 		end_pos = (read_subbuf + 1) * subbuf_size;
941b86ff981SJens Axboe 	else
942b86ff981SJens Axboe 		end_pos = read_pos + count;
943b86ff981SJens Axboe 	if (end_pos >= subbuf_size * n_subbufs)
944b86ff981SJens Axboe 		end_pos = 0;
945b86ff981SJens Axboe 
946b86ff981SJens Axboe 	return end_pos;
947b86ff981SJens Axboe }
948b86ff981SJens Axboe 
9494c78a663SRandy Dunlap /*
9506dac40a7STom Zanussi  *	subbuf_read_actor - read up to one subbuf's worth of data
951b86ff981SJens Axboe  */
9526dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start,
9536dac40a7STom Zanussi 			     struct rchan_buf *buf,
9546dac40a7STom Zanussi 			     size_t avail,
9556dac40a7STom Zanussi 			     read_descriptor_t *desc,
9566dac40a7STom Zanussi 			     read_actor_t actor)
957b86ff981SJens Axboe {
958b86ff981SJens Axboe 	void *from;
9596dac40a7STom Zanussi 	int ret = 0;
960b86ff981SJens Axboe 
961b86ff981SJens Axboe 	from = buf->start + read_start;
9626dac40a7STom Zanussi 	ret = avail;
963ba2397efSAl Viro 	if (copy_to_user(desc->arg.buf, from, avail)) {
9646dac40a7STom Zanussi 		desc->error = -EFAULT;
9656dac40a7STom Zanussi 		ret = 0;
966b86ff981SJens Axboe 	}
9676dac40a7STom Zanussi 	desc->arg.data += ret;
9686dac40a7STom Zanussi 	desc->written += ret;
9696dac40a7STom Zanussi 	desc->count -= ret;
9706dac40a7STom Zanussi 
971b86ff981SJens Axboe 	return ret;
972b86ff981SJens Axboe }
973b86ff981SJens Axboe 
9746dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start,
9756dac40a7STom Zanussi 			       struct rchan_buf *buf,
9766dac40a7STom Zanussi 			       size_t avail,
9776dac40a7STom Zanussi 			       read_descriptor_t *desc,
9786dac40a7STom Zanussi 			       read_actor_t actor);
9796dac40a7STom Zanussi 
9804c78a663SRandy Dunlap /*
9816dac40a7STom Zanussi  *	relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
9826dac40a7STom Zanussi  */
983192636adSAndrew Morton static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
9846dac40a7STom Zanussi 					subbuf_actor_t subbuf_actor,
9856dac40a7STom Zanussi 					read_actor_t actor,
986ba2397efSAl Viro 					read_descriptor_t *desc)
987221415d7SJens Axboe {
988221415d7SJens Axboe 	struct rchan_buf *buf = filp->private_data;
989221415d7SJens Axboe 	size_t read_start, avail;
9906dac40a7STom Zanussi 	int ret;
991221415d7SJens Axboe 
992ba2397efSAl Viro 	if (!desc->count)
993221415d7SJens Axboe 		return 0;
994221415d7SJens Axboe 
995f3a43f3fSJosef "Jeff" Sipek 	mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
9966dac40a7STom Zanussi 	do {
9976dac40a7STom Zanussi 		if (!relay_file_read_avail(buf, *ppos))
998221415d7SJens Axboe 			break;
999221415d7SJens Axboe 
10006dac40a7STom Zanussi 		read_start = relay_file_read_start_pos(*ppos, buf);
10016dac40a7STom Zanussi 		avail = relay_file_read_subbuf_avail(read_start, buf);
10026dac40a7STom Zanussi 		if (!avail)
10036dac40a7STom Zanussi 			break;
1004221415d7SJens Axboe 
1005ba2397efSAl Viro 		avail = min(desc->count, avail);
1006ba2397efSAl Viro 		ret = subbuf_actor(read_start, buf, avail, desc, actor);
1007ba2397efSAl Viro 		if (desc->error < 0)
10086dac40a7STom Zanussi 			break;
10096dac40a7STom Zanussi 
10106dac40a7STom Zanussi 		if (ret) {
1011221415d7SJens Axboe 			relay_file_read_consume(buf, read_start, ret);
1012221415d7SJens Axboe 			*ppos = relay_file_read_end_pos(buf, read_start, ret);
1013221415d7SJens Axboe 		}
1014ba2397efSAl Viro 	} while (desc->count && ret);
1015f3a43f3fSJosef "Jeff" Sipek 	mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
1016221415d7SJens Axboe 
1017ba2397efSAl Viro 	return desc->written;
1018221415d7SJens Axboe }
1019221415d7SJens Axboe 
10206dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp,
10216dac40a7STom Zanussi 			       char __user *buffer,
10226dac40a7STom Zanussi 			       size_t count,
10236dac40a7STom Zanussi 			       loff_t *ppos)
10246dac40a7STom Zanussi {
1025ba2397efSAl Viro 	read_descriptor_t desc;
1026ba2397efSAl Viro 	desc.written = 0;
1027ba2397efSAl Viro 	desc.count = count;
1028ba2397efSAl Viro 	desc.arg.buf = buffer;
1029ba2397efSAl Viro 	desc.error = 0;
1030ba2397efSAl Viro 	return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
1031ba2397efSAl Viro 				       NULL, &desc);
10326dac40a7STom Zanussi }
10336dac40a7STom Zanussi 
10341db60cf2SJens Axboe static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
10351db60cf2SJens Axboe {
10361db60cf2SJens Axboe 	rbuf->bytes_consumed += bytes_consumed;
10371db60cf2SJens Axboe 
10381db60cf2SJens Axboe 	if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
10391db60cf2SJens Axboe 		relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
10401db60cf2SJens Axboe 		rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
10411db60cf2SJens Axboe 	}
10421db60cf2SJens Axboe }
10431db60cf2SJens Axboe 
1044ebf99093STom Zanussi static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1045ebf99093STom Zanussi 				   struct pipe_buffer *buf)
1046221415d7SJens Axboe {
1047ebf99093STom Zanussi 	struct rchan_buf *rbuf;
1048ebf99093STom Zanussi 
1049ebf99093STom Zanussi 	rbuf = (struct rchan_buf *)page_private(buf->page);
10501db60cf2SJens Axboe 	relay_consume_bytes(rbuf, buf->private);
1051ebf99093STom Zanussi }
1052ebf99093STom Zanussi 
1053ebf99093STom Zanussi static struct pipe_buf_operations relay_pipe_buf_ops = {
1054ebf99093STom Zanussi 	.can_merge = 0,
1055ebf99093STom Zanussi 	.map = generic_pipe_buf_map,
1056ebf99093STom Zanussi 	.unmap = generic_pipe_buf_unmap,
1057cac36bb0SJens Axboe 	.confirm = generic_pipe_buf_confirm,
1058ebf99093STom Zanussi 	.release = relay_pipe_buf_release,
1059ebf99093STom Zanussi 	.steal = generic_pipe_buf_steal,
1060ebf99093STom Zanussi 	.get = generic_pipe_buf_get,
1061ebf99093STom Zanussi };
1062ebf99093STom Zanussi 
1063d3f35d98STom Zanussi /*
1064ebf99093STom Zanussi  *	subbuf_splice_actor - splice up to one subbuf's worth of data
1065ebf99093STom Zanussi  */
1066ebf99093STom Zanussi static int subbuf_splice_actor(struct file *in,
1067ebf99093STom Zanussi 			       loff_t *ppos,
1068ebf99093STom Zanussi 			       struct pipe_inode_info *pipe,
1069ebf99093STom Zanussi 			       size_t len,
1070ebf99093STom Zanussi 			       unsigned int flags,
1071ebf99093STom Zanussi 			       int *nonpad_ret)
1072ebf99093STom Zanussi {
10731db60cf2SJens Axboe 	unsigned int pidx, poff, total_len, subbuf_pages, ret;
1074ebf99093STom Zanussi 	struct rchan_buf *rbuf = in->private_data;
1075ebf99093STom Zanussi 	unsigned int subbuf_size = rbuf->chan->subbuf_size;
107624da24deSTom Zanussi 	uint64_t pos = (uint64_t) *ppos;
107724da24deSTom Zanussi 	uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
107824da24deSTom Zanussi 	size_t read_start = (size_t) do_div(pos, alloc_size);
1079ebf99093STom Zanussi 	size_t read_subbuf = read_start / subbuf_size;
1080ebf99093STom Zanussi 	size_t padding = rbuf->padding[read_subbuf];
1081ebf99093STom Zanussi 	size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
10821db60cf2SJens Axboe 	struct page *pages[PIPE_BUFFERS];
10831db60cf2SJens Axboe 	struct partial_page partial[PIPE_BUFFERS];
10841db60cf2SJens Axboe 	struct splice_pipe_desc spd = {
10851db60cf2SJens Axboe 		.pages = pages,
10861db60cf2SJens Axboe 		.nr_pages = 0,
10871db60cf2SJens Axboe 		.partial = partial,
10881db60cf2SJens Axboe 		.flags = flags,
10891db60cf2SJens Axboe 		.ops = &relay_pipe_buf_ops,
10901db60cf2SJens Axboe 	};
1091ebf99093STom Zanussi 
1092ebf99093STom Zanussi 	if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1093ebf99093STom Zanussi 		return 0;
1094ebf99093STom Zanussi 
10951db60cf2SJens Axboe 	/*
10961db60cf2SJens Axboe 	 * Adjust read len, if longer than what is available
10971db60cf2SJens Axboe 	 */
10981db60cf2SJens Axboe 	if (len > (subbuf_size - read_start % subbuf_size))
10991db60cf2SJens Axboe 		len = subbuf_size - read_start % subbuf_size;
1100ebf99093STom Zanussi 
1101ebf99093STom Zanussi 	subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1102ebf99093STom Zanussi 	pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1103ebf99093STom Zanussi 	poff = read_start & ~PAGE_MASK;
1104ebf99093STom Zanussi 
11051db60cf2SJens Axboe 	for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) {
11061db60cf2SJens Axboe 		unsigned int this_len, this_end, private;
11071db60cf2SJens Axboe 		unsigned int cur_pos = read_start + total_len;
1108ebf99093STom Zanussi 
11091db60cf2SJens Axboe 		if (!len)
1110ebf99093STom Zanussi 			break;
11111db60cf2SJens Axboe 
11121db60cf2SJens Axboe 		this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
11131db60cf2SJens Axboe 		private = this_len;
11141db60cf2SJens Axboe 
11151db60cf2SJens Axboe 		spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
11161db60cf2SJens Axboe 		spd.partial[spd.nr_pages].offset = poff;
11171db60cf2SJens Axboe 
11181db60cf2SJens Axboe 		this_end = cur_pos + this_len;
11191db60cf2SJens Axboe 		if (this_end >= nonpad_end) {
11201db60cf2SJens Axboe 			this_len = nonpad_end - cur_pos;
11211db60cf2SJens Axboe 			private = this_len + padding;
1122ebf99093STom Zanussi 		}
11231db60cf2SJens Axboe 		spd.partial[spd.nr_pages].len = this_len;
11241db60cf2SJens Axboe 		spd.partial[spd.nr_pages].private = private;
1125ebf99093STom Zanussi 
11261db60cf2SJens Axboe 		len -= this_len;
11271db60cf2SJens Axboe 		total_len += this_len;
1128ebf99093STom Zanussi 		poff = 0;
1129ebf99093STom Zanussi 		pidx = (pidx + 1) % subbuf_pages;
1130ebf99093STom Zanussi 
11311db60cf2SJens Axboe 		if (this_end >= nonpad_end) {
11321db60cf2SJens Axboe 			spd.nr_pages++;
1133ebf99093STom Zanussi 			break;
1134ebf99093STom Zanussi 		}
1135ebf99093STom Zanussi 	}
1136ebf99093STom Zanussi 
11371db60cf2SJens Axboe 	if (!spd.nr_pages)
11381db60cf2SJens Axboe 		return 0;
1139ebf99093STom Zanussi 
11401db60cf2SJens Axboe 	ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
11411db60cf2SJens Axboe 	if (ret < 0 || ret < total_len)
11421db60cf2SJens Axboe 		return ret;
1143ebf99093STom Zanussi 
11441db60cf2SJens Axboe         if (read_start + ret == nonpad_end)
11451db60cf2SJens Axboe                 ret += padding;
1146ebf99093STom Zanussi 
1147ebf99093STom Zanussi         return ret;
1148ebf99093STom Zanussi }
1149ebf99093STom Zanussi 
1150ebf99093STom Zanussi static ssize_t relay_file_splice_read(struct file *in,
1151ebf99093STom Zanussi 				      loff_t *ppos,
1152ebf99093STom Zanussi 				      struct pipe_inode_info *pipe,
1153ebf99093STom Zanussi 				      size_t len,
1154ebf99093STom Zanussi 				      unsigned int flags)
1155ebf99093STom Zanussi {
1156ebf99093STom Zanussi 	ssize_t spliced;
1157ebf99093STom Zanussi 	int ret;
1158ebf99093STom Zanussi 	int nonpad_ret = 0;
1159ebf99093STom Zanussi 
1160ebf99093STom Zanussi 	ret = 0;
1161ebf99093STom Zanussi 	spliced = 0;
1162ebf99093STom Zanussi 
1163ebf99093STom Zanussi 	while (len) {
1164ebf99093STom Zanussi 		ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1165ebf99093STom Zanussi 		if (ret < 0)
1166ebf99093STom Zanussi 			break;
1167ebf99093STom Zanussi 		else if (!ret) {
1168ebf99093STom Zanussi 			if (spliced)
1169ebf99093STom Zanussi 				break;
1170ebf99093STom Zanussi 			if (flags & SPLICE_F_NONBLOCK) {
1171ebf99093STom Zanussi 				ret = -EAGAIN;
1172ebf99093STom Zanussi 				break;
1173ebf99093STom Zanussi 			}
1174ebf99093STom Zanussi 		}
1175ebf99093STom Zanussi 
1176ebf99093STom Zanussi 		*ppos += ret;
1177ebf99093STom Zanussi 		if (ret > len)
1178ebf99093STom Zanussi 			len = 0;
1179ebf99093STom Zanussi 		else
1180ebf99093STom Zanussi 			len -= ret;
1181ebf99093STom Zanussi 		spliced += nonpad_ret;
1182ebf99093STom Zanussi 		nonpad_ret = 0;
1183ebf99093STom Zanussi 	}
1184ebf99093STom Zanussi 
1185ebf99093STom Zanussi 	if (spliced)
1186ebf99093STom Zanussi 		return spliced;
1187ebf99093STom Zanussi 
1188ebf99093STom Zanussi 	return ret;
1189221415d7SJens Axboe }
1190221415d7SJens Axboe 
119115ad7cdcSHelge Deller const struct file_operations relay_file_operations = {
1192b86ff981SJens Axboe 	.open		= relay_file_open,
1193b86ff981SJens Axboe 	.poll		= relay_file_poll,
1194b86ff981SJens Axboe 	.mmap		= relay_file_mmap,
1195b86ff981SJens Axboe 	.read		= relay_file_read,
1196b86ff981SJens Axboe 	.llseek		= no_llseek,
1197b86ff981SJens Axboe 	.release	= relay_file_release,
1198ebf99093STom Zanussi 	.splice_read	= relay_file_splice_read,
1199b86ff981SJens Axboe };
1200b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations);
120123c88752SMathieu Desnoyers 
120223c88752SMathieu Desnoyers static __init int relay_init(void)
120323c88752SMathieu Desnoyers {
120423c88752SMathieu Desnoyers 
120523c88752SMathieu Desnoyers 	hotcpu_notifier(relay_hotcpu_callback, 0);
120623c88752SMathieu Desnoyers 	return 0;
120723c88752SMathieu Desnoyers }
120823c88752SMathieu Desnoyers 
120923c88752SMathieu Desnoyers module_init(relay_init);
1210