xref: /linux/kernel/relay.c (revision 8d62fdebdaf9b866c7e236a8f5cfe90e6dba5773)
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>
2423c88752SMathieu Desnoyers 
2523c88752SMathieu Desnoyers /* list of open channels, for cpu hotplug */
2623c88752SMathieu Desnoyers static DEFINE_MUTEX(relay_channels_mutex);
2723c88752SMathieu Desnoyers static LIST_HEAD(relay_channels);
28b86ff981SJens Axboe 
29b86ff981SJens Axboe /*
30b86ff981SJens Axboe  * close() vm_op implementation for relay file mapping.
31b86ff981SJens Axboe  */
32b86ff981SJens Axboe static void relay_file_mmap_close(struct vm_area_struct *vma)
33b86ff981SJens Axboe {
34b86ff981SJens Axboe 	struct rchan_buf *buf = vma->vm_private_data;
35b86ff981SJens Axboe 	buf->chan->cb->buf_unmapped(buf, vma->vm_file);
36b86ff981SJens Axboe }
37b86ff981SJens Axboe 
38b86ff981SJens Axboe /*
39b86ff981SJens Axboe  * nopage() vm_op implementation for relay file mapping.
40b86ff981SJens Axboe  */
41b86ff981SJens Axboe static struct page *relay_buf_nopage(struct vm_area_struct *vma,
42b86ff981SJens Axboe 				     unsigned long address,
43b86ff981SJens Axboe 				     int *type)
44b86ff981SJens Axboe {
45b86ff981SJens Axboe 	struct page *page;
46b86ff981SJens Axboe 	struct rchan_buf *buf = vma->vm_private_data;
47b86ff981SJens Axboe 	unsigned long offset = address - vma->vm_start;
48b86ff981SJens Axboe 
49b86ff981SJens Axboe 	if (address > vma->vm_end)
50b86ff981SJens Axboe 		return NOPAGE_SIGBUS; /* Disallow mremap */
51b86ff981SJens Axboe 	if (!buf)
52b86ff981SJens Axboe 		return NOPAGE_OOM;
53b86ff981SJens Axboe 
54b86ff981SJens Axboe 	page = vmalloc_to_page(buf->start + offset);
55b86ff981SJens Axboe 	if (!page)
56b86ff981SJens Axboe 		return NOPAGE_OOM;
57b86ff981SJens Axboe 	get_page(page);
58b86ff981SJens Axboe 
59b86ff981SJens Axboe 	if (type)
60b86ff981SJens Axboe 		*type = VM_FAULT_MINOR;
61b86ff981SJens Axboe 
62b86ff981SJens Axboe 	return page;
63b86ff981SJens Axboe }
64b86ff981SJens Axboe 
65b86ff981SJens Axboe /*
66b86ff981SJens Axboe  * vm_ops for relay file mappings.
67b86ff981SJens Axboe  */
68b86ff981SJens Axboe static struct vm_operations_struct relay_file_mmap_ops = {
69b86ff981SJens Axboe 	.nopage = relay_buf_nopage,
70b86ff981SJens Axboe 	.close = relay_file_mmap_close,
71b86ff981SJens Axboe };
72b86ff981SJens Axboe 
73b86ff981SJens Axboe /**
74b86ff981SJens Axboe  *	relay_mmap_buf: - mmap channel buffer to process address space
75b86ff981SJens Axboe  *	@buf: relay channel buffer
76b86ff981SJens Axboe  *	@vma: vm_area_struct describing memory to be mapped
77b86ff981SJens Axboe  *
78b86ff981SJens Axboe  *	Returns 0 if ok, negative on error
79b86ff981SJens Axboe  *
80b86ff981SJens Axboe  *	Caller should already have grabbed mmap_sem.
81b86ff981SJens Axboe  */
82b86ff981SJens Axboe int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
83b86ff981SJens Axboe {
84b86ff981SJens Axboe 	unsigned long length = vma->vm_end - vma->vm_start;
85b86ff981SJens Axboe 	struct file *filp = vma->vm_file;
86b86ff981SJens Axboe 
87b86ff981SJens Axboe 	if (!buf)
88b86ff981SJens Axboe 		return -EBADF;
89b86ff981SJens Axboe 
90b86ff981SJens Axboe 	if (length != (unsigned long)buf->chan->alloc_size)
91b86ff981SJens Axboe 		return -EINVAL;
92b86ff981SJens Axboe 
93b86ff981SJens Axboe 	vma->vm_ops = &relay_file_mmap_ops;
94b86ff981SJens Axboe 	vma->vm_private_data = buf;
95b86ff981SJens Axboe 	buf->chan->cb->buf_mapped(buf, filp);
96b86ff981SJens Axboe 
97b86ff981SJens Axboe 	return 0;
98b86ff981SJens Axboe }
99b86ff981SJens Axboe 
100b86ff981SJens Axboe /**
101b86ff981SJens Axboe  *	relay_alloc_buf - allocate a channel buffer
102b86ff981SJens Axboe  *	@buf: the buffer struct
103b86ff981SJens Axboe  *	@size: total size of the buffer
104b86ff981SJens Axboe  *
1054c78a663SRandy Dunlap  *	Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
106221415d7SJens Axboe  *	passed in size will get page aligned, if it isn't already.
107b86ff981SJens Axboe  */
108221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
109b86ff981SJens Axboe {
110b86ff981SJens Axboe 	void *mem;
111b86ff981SJens Axboe 	unsigned int i, j, n_pages;
112b86ff981SJens Axboe 
113221415d7SJens Axboe 	*size = PAGE_ALIGN(*size);
114221415d7SJens Axboe 	n_pages = *size >> PAGE_SHIFT;
115b86ff981SJens Axboe 
116b86ff981SJens Axboe 	buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
117b86ff981SJens Axboe 	if (!buf->page_array)
118b86ff981SJens Axboe 		return NULL;
119b86ff981SJens Axboe 
120b86ff981SJens Axboe 	for (i = 0; i < n_pages; i++) {
121b86ff981SJens Axboe 		buf->page_array[i] = alloc_page(GFP_KERNEL);
122b86ff981SJens Axboe 		if (unlikely(!buf->page_array[i]))
123b86ff981SJens Axboe 			goto depopulate;
124b86ff981SJens Axboe 	}
125b86ff981SJens Axboe 	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
126b86ff981SJens Axboe 	if (!mem)
127b86ff981SJens Axboe 		goto depopulate;
128b86ff981SJens Axboe 
129221415d7SJens Axboe 	memset(mem, 0, *size);
130b86ff981SJens Axboe 	buf->page_count = n_pages;
131b86ff981SJens Axboe 	return mem;
132b86ff981SJens Axboe 
133b86ff981SJens Axboe depopulate:
134b86ff981SJens Axboe 	for (j = 0; j < i; j++)
135b86ff981SJens Axboe 		__free_page(buf->page_array[j]);
136b86ff981SJens Axboe 	kfree(buf->page_array);
137b86ff981SJens Axboe 	return NULL;
138b86ff981SJens Axboe }
139b86ff981SJens Axboe 
140b86ff981SJens Axboe /**
141b86ff981SJens Axboe  *	relay_create_buf - allocate and initialize a channel buffer
1424c78a663SRandy Dunlap  *	@chan: the relay channel
143b86ff981SJens Axboe  *
1444c78a663SRandy Dunlap  *	Returns channel buffer if successful, %NULL otherwise.
145b86ff981SJens Axboe  */
146b86ff981SJens Axboe struct rchan_buf *relay_create_buf(struct rchan *chan)
147b86ff981SJens Axboe {
148cd861280SRobert P. J. Day 	struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
149b86ff981SJens Axboe 	if (!buf)
150b86ff981SJens Axboe 		return NULL;
151b86ff981SJens Axboe 
152b86ff981SJens Axboe 	buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
153b86ff981SJens Axboe 	if (!buf->padding)
154b86ff981SJens Axboe 		goto free_buf;
155b86ff981SJens Axboe 
156221415d7SJens Axboe 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
157b86ff981SJens Axboe 	if (!buf->start)
158b86ff981SJens Axboe 		goto free_buf;
159b86ff981SJens Axboe 
160b86ff981SJens Axboe 	buf->chan = chan;
161b86ff981SJens Axboe 	kref_get(&buf->chan->kref);
162b86ff981SJens Axboe 	return buf;
163b86ff981SJens Axboe 
164b86ff981SJens Axboe free_buf:
165b86ff981SJens Axboe 	kfree(buf->padding);
166b86ff981SJens Axboe 	kfree(buf);
167b86ff981SJens Axboe 	return NULL;
168b86ff981SJens Axboe }
169b86ff981SJens Axboe 
170b86ff981SJens Axboe /**
171b86ff981SJens Axboe  *	relay_destroy_channel - free the channel struct
1724c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay channel
173b86ff981SJens Axboe  *
174b86ff981SJens Axboe  *	Should only be called from kref_put().
175b86ff981SJens Axboe  */
176b86ff981SJens Axboe void relay_destroy_channel(struct kref *kref)
177b86ff981SJens Axboe {
178b86ff981SJens Axboe 	struct rchan *chan = container_of(kref, struct rchan, kref);
179b86ff981SJens Axboe 	kfree(chan);
180b86ff981SJens Axboe }
181b86ff981SJens Axboe 
182b86ff981SJens Axboe /**
183b86ff981SJens Axboe  *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
184b86ff981SJens Axboe  *	@buf: the buffer struct
185b86ff981SJens Axboe  */
186b86ff981SJens Axboe void relay_destroy_buf(struct rchan_buf *buf)
187b86ff981SJens Axboe {
188b86ff981SJens Axboe 	struct rchan *chan = buf->chan;
189b86ff981SJens Axboe 	unsigned int i;
190b86ff981SJens Axboe 
191b86ff981SJens Axboe 	if (likely(buf->start)) {
192b86ff981SJens Axboe 		vunmap(buf->start);
193b86ff981SJens Axboe 		for (i = 0; i < buf->page_count; i++)
194b86ff981SJens Axboe 			__free_page(buf->page_array[i]);
195b86ff981SJens Axboe 		kfree(buf->page_array);
196b86ff981SJens Axboe 	}
19723c88752SMathieu Desnoyers 	chan->buf[buf->cpu] = NULL;
198b86ff981SJens Axboe 	kfree(buf->padding);
199b86ff981SJens Axboe 	kfree(buf);
200b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
201b86ff981SJens Axboe }
202b86ff981SJens Axboe 
203b86ff981SJens Axboe /**
204b86ff981SJens Axboe  *	relay_remove_buf - remove a channel buffer
2054c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay buffer
206b86ff981SJens Axboe  *
207b86ff981SJens Axboe  *	Removes the file from the fileystem, which also frees the
208b86ff981SJens Axboe  *	rchan_buf_struct and the channel buffer.  Should only be called from
209b86ff981SJens Axboe  *	kref_put().
210b86ff981SJens Axboe  */
211b86ff981SJens Axboe void relay_remove_buf(struct kref *kref)
212b86ff981SJens Axboe {
213b86ff981SJens Axboe 	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
214b86ff981SJens Axboe 	buf->chan->cb->remove_buf_file(buf->dentry);
215b86ff981SJens Axboe 	relay_destroy_buf(buf);
216b86ff981SJens Axboe }
217b86ff981SJens Axboe 
218b86ff981SJens Axboe /**
219b86ff981SJens Axboe  *	relay_buf_empty - boolean, is the channel buffer empty?
220b86ff981SJens Axboe  *	@buf: channel buffer
221b86ff981SJens Axboe  *
222b86ff981SJens Axboe  *	Returns 1 if the buffer is empty, 0 otherwise.
223b86ff981SJens Axboe  */
224b86ff981SJens Axboe int relay_buf_empty(struct rchan_buf *buf)
225b86ff981SJens Axboe {
226b86ff981SJens Axboe 	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
227b86ff981SJens Axboe }
228b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_empty);
229b86ff981SJens Axboe 
230b86ff981SJens Axboe /**
231b86ff981SJens Axboe  *	relay_buf_full - boolean, is the channel buffer full?
232b86ff981SJens Axboe  *	@buf: channel buffer
233b86ff981SJens Axboe  *
234b86ff981SJens Axboe  *	Returns 1 if the buffer is full, 0 otherwise.
235b86ff981SJens Axboe  */
236b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf)
237b86ff981SJens Axboe {
238b86ff981SJens Axboe 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
239b86ff981SJens Axboe 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
240b86ff981SJens Axboe }
241b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full);
242b86ff981SJens Axboe 
243b86ff981SJens Axboe /*
244b86ff981SJens Axboe  * High-level relay kernel API and associated functions.
245b86ff981SJens Axboe  */
246b86ff981SJens Axboe 
247b86ff981SJens Axboe /*
248b86ff981SJens Axboe  * rchan_callback implementations defining default channel behavior.  Used
249b86ff981SJens Axboe  * in place of corresponding NULL values in client callback struct.
250b86ff981SJens Axboe  */
251b86ff981SJens Axboe 
252b86ff981SJens Axboe /*
253b86ff981SJens Axboe  * subbuf_start() default callback.  Does nothing.
254b86ff981SJens Axboe  */
255b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf,
256b86ff981SJens Axboe 					  void *subbuf,
257b86ff981SJens Axboe 					  void *prev_subbuf,
258b86ff981SJens Axboe 					  size_t prev_padding)
259b86ff981SJens Axboe {
260b86ff981SJens Axboe 	if (relay_buf_full(buf))
261b86ff981SJens Axboe 		return 0;
262b86ff981SJens Axboe 
263b86ff981SJens Axboe 	return 1;
264b86ff981SJens Axboe }
265b86ff981SJens Axboe 
266b86ff981SJens Axboe /*
267b86ff981SJens Axboe  * buf_mapped() default callback.  Does nothing.
268b86ff981SJens Axboe  */
269b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf,
270b86ff981SJens Axboe 					struct file *filp)
271b86ff981SJens Axboe {
272b86ff981SJens Axboe }
273b86ff981SJens Axboe 
274b86ff981SJens Axboe /*
275b86ff981SJens Axboe  * buf_unmapped() default callback.  Does nothing.
276b86ff981SJens Axboe  */
277b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf,
278b86ff981SJens Axboe 					  struct file *filp)
279b86ff981SJens Axboe {
280b86ff981SJens Axboe }
281b86ff981SJens Axboe 
282b86ff981SJens Axboe /*
283b86ff981SJens Axboe  * create_buf_file_create() default callback.  Does nothing.
284b86ff981SJens Axboe  */
285b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename,
286b86ff981SJens Axboe 						       struct dentry *parent,
287b86ff981SJens Axboe 						       int mode,
288b86ff981SJens Axboe 						       struct rchan_buf *buf,
289b86ff981SJens Axboe 						       int *is_global)
290b86ff981SJens Axboe {
291b86ff981SJens Axboe 	return NULL;
292b86ff981SJens Axboe }
293b86ff981SJens Axboe 
294b86ff981SJens Axboe /*
295b86ff981SJens Axboe  * remove_buf_file() default callback.  Does nothing.
296b86ff981SJens Axboe  */
297b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry)
298b86ff981SJens Axboe {
299b86ff981SJens Axboe 	return -EINVAL;
300b86ff981SJens Axboe }
301b86ff981SJens Axboe 
302b86ff981SJens Axboe /* relay channel default callbacks */
303b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = {
304b86ff981SJens Axboe 	.subbuf_start = subbuf_start_default_callback,
305b86ff981SJens Axboe 	.buf_mapped = buf_mapped_default_callback,
306b86ff981SJens Axboe 	.buf_unmapped = buf_unmapped_default_callback,
307b86ff981SJens Axboe 	.create_buf_file = create_buf_file_default_callback,
308b86ff981SJens Axboe 	.remove_buf_file = remove_buf_file_default_callback,
309b86ff981SJens Axboe };
310b86ff981SJens Axboe 
311b86ff981SJens Axboe /**
312b86ff981SJens Axboe  *	wakeup_readers - wake up readers waiting on a channel
3139a9136e2SLinus Torvalds  *	@data: contains the channel buffer
314b86ff981SJens Axboe  *
3157c9cb383STom Zanussi  *	This is the timer function used to defer reader waking.
316b86ff981SJens Axboe  */
3177c9cb383STom Zanussi static void wakeup_readers(unsigned long data)
318b86ff981SJens Axboe {
3197c9cb383STom Zanussi 	struct rchan_buf *buf = (struct rchan_buf *)data;
320b86ff981SJens Axboe 	wake_up_interruptible(&buf->read_wait);
321b86ff981SJens Axboe }
322b86ff981SJens Axboe 
323b86ff981SJens Axboe /**
324b86ff981SJens Axboe  *	__relay_reset - reset a channel buffer
325b86ff981SJens Axboe  *	@buf: the channel buffer
326b86ff981SJens Axboe  *	@init: 1 if this is a first-time initialization
327b86ff981SJens Axboe  *
32872fd4a35SRobert P. J. Day  *	See relay_reset() for description of effect.
329b86ff981SJens Axboe  */
330192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init)
331b86ff981SJens Axboe {
332b86ff981SJens Axboe 	size_t i;
333b86ff981SJens Axboe 
334b86ff981SJens Axboe 	if (init) {
335b86ff981SJens Axboe 		init_waitqueue_head(&buf->read_wait);
336b86ff981SJens Axboe 		kref_init(&buf->kref);
3377c9cb383STom Zanussi 		setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
3387c9cb383STom Zanussi 	} else
3397c9cb383STom Zanussi 		del_timer_sync(&buf->timer);
340b86ff981SJens Axboe 
341b86ff981SJens Axboe 	buf->subbufs_produced = 0;
342b86ff981SJens Axboe 	buf->subbufs_consumed = 0;
343b86ff981SJens Axboe 	buf->bytes_consumed = 0;
344b86ff981SJens Axboe 	buf->finalized = 0;
345b86ff981SJens Axboe 	buf->data = buf->start;
346b86ff981SJens Axboe 	buf->offset = 0;
347b86ff981SJens Axboe 
348b86ff981SJens Axboe 	for (i = 0; i < buf->chan->n_subbufs; i++)
349b86ff981SJens Axboe 		buf->padding[i] = 0;
350b86ff981SJens Axboe 
351b86ff981SJens Axboe 	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
352b86ff981SJens Axboe }
353b86ff981SJens Axboe 
354b86ff981SJens Axboe /**
355b86ff981SJens Axboe  *	relay_reset - reset the channel
356b86ff981SJens Axboe  *	@chan: the channel
357b86ff981SJens Axboe  *
358b86ff981SJens Axboe  *	This has the effect of erasing all data from all channel buffers
359b86ff981SJens Axboe  *	and restarting the channel in its initial state.  The buffers
360b86ff981SJens Axboe  *	are not freed, so any mappings are still in effect.
361b86ff981SJens Axboe  *
36272fd4a35SRobert P. J. Day  *	NOTE. Care should be taken that the channel isn't actually
363b86ff981SJens Axboe  *	being used by anything when this call is made.
364b86ff981SJens Axboe  */
365b86ff981SJens Axboe void relay_reset(struct rchan *chan)
366b86ff981SJens Axboe {
367b86ff981SJens Axboe 	unsigned int i;
368b86ff981SJens Axboe 
369b86ff981SJens Axboe 	if (!chan)
370b86ff981SJens Axboe 		return;
371b86ff981SJens Axboe 
37223c88752SMathieu Desnoyers  	if (chan->is_global && chan->buf[0]) {
37323c88752SMathieu Desnoyers 		__relay_reset(chan->buf[0], 0);
37423c88752SMathieu Desnoyers 		return;
375b86ff981SJens Axboe 	}
37623c88752SMathieu Desnoyers 
37723c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
37823c88752SMathieu Desnoyers 	for_each_online_cpu(i)
37923c88752SMathieu Desnoyers 		if (chan->buf[i])
38023c88752SMathieu Desnoyers 			__relay_reset(chan->buf[i], 0);
38123c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
382b86ff981SJens Axboe }
383b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset);
384b86ff981SJens Axboe 
3854c78a663SRandy Dunlap /*
386b86ff981SJens Axboe  *	relay_open_buf - create a new relay channel buffer
387b86ff981SJens Axboe  *
38823c88752SMathieu Desnoyers  *	used by relay_open() and CPU hotplug.
389b86ff981SJens Axboe  */
39023c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
391b86ff981SJens Axboe {
39223c88752SMathieu Desnoyers  	struct rchan_buf *buf = NULL;
393b86ff981SJens Axboe 	struct dentry *dentry;
39423c88752SMathieu Desnoyers  	char *tmpname;
395b86ff981SJens Axboe 
39623c88752SMathieu Desnoyers  	if (chan->is_global)
397b86ff981SJens Axboe 		return chan->buf[0];
398b86ff981SJens Axboe 
39923c88752SMathieu Desnoyers 	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
40023c88752SMathieu Desnoyers  	if (!tmpname)
40123c88752SMathieu Desnoyers  		goto end;
40223c88752SMathieu Desnoyers  	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
40323c88752SMathieu Desnoyers 
404b86ff981SJens Axboe 	buf = relay_create_buf(chan);
405b86ff981SJens Axboe 	if (!buf)
40623c88752SMathieu Desnoyers  		goto free_name;
407b86ff981SJens Axboe 
40823c88752SMathieu Desnoyers  	buf->cpu = cpu;
409b86ff981SJens Axboe  	__relay_reset(buf, 1);
410b86ff981SJens Axboe 
41123c88752SMathieu Desnoyers 	/* Create file in fs */
41223c88752SMathieu Desnoyers  	dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
41323c88752SMathieu Desnoyers  					   buf, &chan->is_global);
41423c88752SMathieu Desnoyers  	if (!dentry)
41523c88752SMathieu Desnoyers  		goto free_buf;
41623c88752SMathieu Desnoyers 
41723c88752SMathieu Desnoyers 	buf->dentry = dentry;
41823c88752SMathieu Desnoyers 
41923c88752SMathieu Desnoyers  	if(chan->is_global) {
42023c88752SMathieu Desnoyers  		chan->buf[0] = buf;
42123c88752SMathieu Desnoyers  		buf->cpu = 0;
42223c88752SMathieu Desnoyers   	}
42323c88752SMathieu Desnoyers 
42423c88752SMathieu Desnoyers  	goto free_name;
42523c88752SMathieu Desnoyers 
42623c88752SMathieu Desnoyers free_buf:
42723c88752SMathieu Desnoyers  	relay_destroy_buf(buf);
42823c88752SMathieu Desnoyers free_name:
42923c88752SMathieu Desnoyers  	kfree(tmpname);
43023c88752SMathieu Desnoyers end:
431b86ff981SJens Axboe 	return buf;
432b86ff981SJens Axboe }
433b86ff981SJens Axboe 
434b86ff981SJens Axboe /**
435b86ff981SJens Axboe  *	relay_close_buf - close a channel buffer
436b86ff981SJens Axboe  *	@buf: channel buffer
437b86ff981SJens Axboe  *
438b86ff981SJens Axboe  *	Marks the buffer finalized and restores the default callbacks.
439b86ff981SJens Axboe  *	The channel buffer and channel buffer data structure are then freed
440b86ff981SJens Axboe  *	automatically when the last reference is given up.
441b86ff981SJens Axboe  */
442192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf)
443b86ff981SJens Axboe {
444b86ff981SJens Axboe 	buf->finalized = 1;
4457c9cb383STom Zanussi 	del_timer_sync(&buf->timer);
446b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
447b86ff981SJens Axboe }
448b86ff981SJens Axboe 
449192636adSAndrew Morton static void setup_callbacks(struct rchan *chan,
450b86ff981SJens Axboe 				   struct rchan_callbacks *cb)
451b86ff981SJens Axboe {
452b86ff981SJens Axboe 	if (!cb) {
453b86ff981SJens Axboe 		chan->cb = &default_channel_callbacks;
454b86ff981SJens Axboe 		return;
455b86ff981SJens Axboe 	}
456b86ff981SJens Axboe 
457b86ff981SJens Axboe 	if (!cb->subbuf_start)
458b86ff981SJens Axboe 		cb->subbuf_start = subbuf_start_default_callback;
459b86ff981SJens Axboe 	if (!cb->buf_mapped)
460b86ff981SJens Axboe 		cb->buf_mapped = buf_mapped_default_callback;
461b86ff981SJens Axboe 	if (!cb->buf_unmapped)
462b86ff981SJens Axboe 		cb->buf_unmapped = buf_unmapped_default_callback;
463b86ff981SJens Axboe 	if (!cb->create_buf_file)
464b86ff981SJens Axboe 		cb->create_buf_file = create_buf_file_default_callback;
465b86ff981SJens Axboe 	if (!cb->remove_buf_file)
466b86ff981SJens Axboe 		cb->remove_buf_file = remove_buf_file_default_callback;
467b86ff981SJens Axboe 	chan->cb = cb;
468b86ff981SJens Axboe }
469b86ff981SJens Axboe 
470b86ff981SJens Axboe /**
47123c88752SMathieu Desnoyers  * 	relay_hotcpu_callback - CPU hotplug callback
47223c88752SMathieu Desnoyers  * 	@nb: notifier block
47323c88752SMathieu Desnoyers  * 	@action: hotplug action to take
47423c88752SMathieu Desnoyers  * 	@hcpu: CPU number
47523c88752SMathieu Desnoyers  *
47605fb6bf0SRandy Dunlap  * 	Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
47723c88752SMathieu Desnoyers  */
47823c88752SMathieu Desnoyers static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
47923c88752SMathieu Desnoyers 				unsigned long action,
48023c88752SMathieu Desnoyers 				void *hcpu)
48123c88752SMathieu Desnoyers {
48223c88752SMathieu Desnoyers 	unsigned int hotcpu = (unsigned long)hcpu;
48323c88752SMathieu Desnoyers 	struct rchan *chan;
48423c88752SMathieu Desnoyers 
48523c88752SMathieu Desnoyers 	switch(action) {
48623c88752SMathieu Desnoyers 	case CPU_UP_PREPARE:
4878bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
48823c88752SMathieu Desnoyers 		mutex_lock(&relay_channels_mutex);
48923c88752SMathieu Desnoyers 		list_for_each_entry(chan, &relay_channels, list) {
49023c88752SMathieu Desnoyers 			if (chan->buf[hotcpu])
49123c88752SMathieu Desnoyers 				continue;
49223c88752SMathieu Desnoyers 			chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
49323c88752SMathieu Desnoyers 			if(!chan->buf[hotcpu]) {
49423c88752SMathieu Desnoyers 				printk(KERN_ERR
49523c88752SMathieu Desnoyers 					"relay_hotcpu_callback: cpu %d buffer "
49623c88752SMathieu Desnoyers 					"creation failed\n", hotcpu);
49723c88752SMathieu Desnoyers 				mutex_unlock(&relay_channels_mutex);
49823c88752SMathieu Desnoyers 				return NOTIFY_BAD;
49923c88752SMathieu Desnoyers 			}
50023c88752SMathieu Desnoyers 		}
50123c88752SMathieu Desnoyers 		mutex_unlock(&relay_channels_mutex);
50223c88752SMathieu Desnoyers 		break;
50323c88752SMathieu Desnoyers 	case CPU_DEAD:
5048bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
50523c88752SMathieu Desnoyers 		/* No need to flush the cpu : will be flushed upon
50623c88752SMathieu Desnoyers 		 * final relay_flush() call. */
50723c88752SMathieu Desnoyers 		break;
50823c88752SMathieu Desnoyers 	}
50923c88752SMathieu Desnoyers 	return NOTIFY_OK;
51023c88752SMathieu Desnoyers }
51123c88752SMathieu Desnoyers 
51223c88752SMathieu Desnoyers /**
513b86ff981SJens Axboe  *	relay_open - create a new relay channel
514b86ff981SJens Axboe  *	@base_filename: base name of files to create
5154c78a663SRandy Dunlap  *	@parent: dentry of parent directory, %NULL for root directory
516b86ff981SJens Axboe  *	@subbuf_size: size of sub-buffers
517b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers
518b86ff981SJens Axboe  *	@cb: client callback functions
51923c88752SMathieu Desnoyers  *	@private_data: user-defined data
520b86ff981SJens Axboe  *
5214c78a663SRandy Dunlap  *	Returns channel pointer if successful, %NULL otherwise.
522b86ff981SJens Axboe  *
523b86ff981SJens Axboe  *	Creates a channel buffer for each cpu using the sizes and
524b86ff981SJens Axboe  *	attributes specified.  The created channel buffer files
525b86ff981SJens Axboe  *	will be named base_filename0...base_filenameN-1.  File
52672fd4a35SRobert P. J. Day  *	permissions will be %S_IRUSR.
527b86ff981SJens Axboe  */
528b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
529b86ff981SJens Axboe 			 struct dentry *parent,
530b86ff981SJens Axboe 			 size_t subbuf_size,
531b86ff981SJens Axboe 			 size_t n_subbufs,
53223c88752SMathieu Desnoyers 			 struct rchan_callbacks *cb,
53323c88752SMathieu Desnoyers 			 void *private_data)
534b86ff981SJens Axboe {
535b86ff981SJens Axboe 	unsigned int i;
536b86ff981SJens Axboe 	struct rchan *chan;
537b86ff981SJens Axboe 	if (!base_filename)
538b86ff981SJens Axboe 		return NULL;
539b86ff981SJens Axboe 
540b86ff981SJens Axboe 	if (!(subbuf_size && n_subbufs))
541b86ff981SJens Axboe 		return NULL;
542b86ff981SJens Axboe 
543cd861280SRobert P. J. Day 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
544b86ff981SJens Axboe 	if (!chan)
545b86ff981SJens Axboe 		return NULL;
546b86ff981SJens Axboe 
547b86ff981SJens Axboe 	chan->version = RELAYFS_CHANNEL_VERSION;
548b86ff981SJens Axboe 	chan->n_subbufs = n_subbufs;
549b86ff981SJens Axboe 	chan->subbuf_size = subbuf_size;
550b86ff981SJens Axboe 	chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
55123c88752SMathieu Desnoyers 	chan->parent = parent;
55223c88752SMathieu Desnoyers 	chan->private_data = private_data;
55323c88752SMathieu Desnoyers 	strlcpy(chan->base_filename, base_filename, NAME_MAX);
554b86ff981SJens Axboe 	setup_callbacks(chan, cb);
555b86ff981SJens Axboe 	kref_init(&chan->kref);
556b86ff981SJens Axboe 
55723c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
558b86ff981SJens Axboe 	for_each_online_cpu(i) {
55923c88752SMathieu Desnoyers 		chan->buf[i] = relay_open_buf(chan, i);
560b86ff981SJens Axboe 		if (!chan->buf[i])
561b86ff981SJens Axboe 			goto free_bufs;
562b86ff981SJens Axboe 	}
56323c88752SMathieu Desnoyers 	list_add(&chan->list, &relay_channels);
56423c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
565b86ff981SJens Axboe 
566b86ff981SJens Axboe 	return chan;
567b86ff981SJens Axboe 
568b86ff981SJens Axboe free_bufs:
56923c88752SMathieu Desnoyers 	for_each_online_cpu(i) {
570b86ff981SJens Axboe 		if (!chan->buf[i])
571b86ff981SJens Axboe 			break;
572b86ff981SJens Axboe 		relay_close_buf(chan->buf[i]);
573b86ff981SJens Axboe 	}
574b86ff981SJens Axboe 
575b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
57623c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
577b86ff981SJens Axboe 	return NULL;
578b86ff981SJens Axboe }
579b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open);
580b86ff981SJens Axboe 
581b86ff981SJens Axboe /**
582b86ff981SJens Axboe  *	relay_switch_subbuf - switch to a new sub-buffer
583b86ff981SJens Axboe  *	@buf: channel buffer
584b86ff981SJens Axboe  *	@length: size of current event
585b86ff981SJens Axboe  *
586b86ff981SJens Axboe  *	Returns either the length passed in or 0 if full.
587b86ff981SJens Axboe  *
588b86ff981SJens Axboe  *	Performs sub-buffer-switch tasks such as invoking callbacks,
589b86ff981SJens Axboe  *	updating padding counts, waking up readers, etc.
590b86ff981SJens Axboe  */
591b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
592b86ff981SJens Axboe {
593b86ff981SJens Axboe 	void *old, *new;
594b86ff981SJens Axboe 	size_t old_subbuf, new_subbuf;
595b86ff981SJens Axboe 
596b86ff981SJens Axboe 	if (unlikely(length > buf->chan->subbuf_size))
597b86ff981SJens Axboe 		goto toobig;
598b86ff981SJens Axboe 
599b86ff981SJens Axboe 	if (buf->offset != buf->chan->subbuf_size + 1) {
600b86ff981SJens Axboe 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
601b86ff981SJens Axboe 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
602b86ff981SJens Axboe 		buf->padding[old_subbuf] = buf->prev_padding;
603b86ff981SJens Axboe 		buf->subbufs_produced++;
604221415d7SJens Axboe 		buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
605221415d7SJens Axboe 			buf->padding[old_subbuf];
606221415d7SJens Axboe 		smp_mb();
6077c9cb383STom Zanussi 		if (waitqueue_active(&buf->read_wait))
6087c9cb383STom Zanussi 			/*
6097c9cb383STom Zanussi 			 * Calling wake_up_interruptible() from here
6107c9cb383STom Zanussi 			 * will deadlock if we happen to be logging
6117c9cb383STom Zanussi 			 * from the scheduler (trying to re-grab
6127c9cb383STom Zanussi 			 * rq->lock), so defer it.
6137c9cb383STom Zanussi 			 */
6147c9cb383STom Zanussi 			__mod_timer(&buf->timer, jiffies + 1);
615b86ff981SJens Axboe 	}
616b86ff981SJens Axboe 
617b86ff981SJens Axboe 	old = buf->data;
618b86ff981SJens Axboe 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
619b86ff981SJens Axboe 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
620b86ff981SJens Axboe 	buf->offset = 0;
621b86ff981SJens Axboe 	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
622b86ff981SJens Axboe 		buf->offset = buf->chan->subbuf_size + 1;
623b86ff981SJens Axboe 		return 0;
624b86ff981SJens Axboe 	}
625b86ff981SJens Axboe 	buf->data = new;
626b86ff981SJens Axboe 	buf->padding[new_subbuf] = 0;
627b86ff981SJens Axboe 
628b86ff981SJens Axboe 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
629b86ff981SJens Axboe 		goto toobig;
630b86ff981SJens Axboe 
631b86ff981SJens Axboe 	return length;
632b86ff981SJens Axboe 
633b86ff981SJens Axboe toobig:
634b86ff981SJens Axboe 	buf->chan->last_toobig = length;
635b86ff981SJens Axboe 	return 0;
636b86ff981SJens Axboe }
637b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf);
638b86ff981SJens Axboe 
639b86ff981SJens Axboe /**
640b86ff981SJens Axboe  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
641b86ff981SJens Axboe  *	@chan: the channel
642b86ff981SJens Axboe  *	@cpu: the cpu associated with the channel buffer to update
643b86ff981SJens Axboe  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
644b86ff981SJens Axboe  *
645b86ff981SJens Axboe  *	Adds to the channel buffer's consumed sub-buffer count.
646b86ff981SJens Axboe  *	subbufs_consumed should be the number of sub-buffers newly consumed,
647b86ff981SJens Axboe  *	not the total consumed.
648b86ff981SJens Axboe  *
64972fd4a35SRobert P. J. Day  *	NOTE. Kernel clients don't need to call this function if the channel
650b86ff981SJens Axboe  *	mode is 'overwrite'.
651b86ff981SJens Axboe  */
652b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan,
653b86ff981SJens Axboe 			    unsigned int cpu,
654b86ff981SJens Axboe 			    size_t subbufs_consumed)
655b86ff981SJens Axboe {
656b86ff981SJens Axboe 	struct rchan_buf *buf;
657b86ff981SJens Axboe 
658b86ff981SJens Axboe 	if (!chan)
659b86ff981SJens Axboe 		return;
660b86ff981SJens Axboe 
661b86ff981SJens Axboe 	if (cpu >= NR_CPUS || !chan->buf[cpu])
662b86ff981SJens Axboe 		return;
663b86ff981SJens Axboe 
664b86ff981SJens Axboe 	buf = chan->buf[cpu];
665b86ff981SJens Axboe 	buf->subbufs_consumed += subbufs_consumed;
666b86ff981SJens Axboe 	if (buf->subbufs_consumed > buf->subbufs_produced)
667b86ff981SJens Axboe 		buf->subbufs_consumed = buf->subbufs_produced;
668b86ff981SJens Axboe }
669b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
670b86ff981SJens Axboe 
671b86ff981SJens Axboe /**
672b86ff981SJens Axboe  *	relay_close - close the channel
673b86ff981SJens Axboe  *	@chan: the channel
674b86ff981SJens Axboe  *
675b86ff981SJens Axboe  *	Closes all channel buffers and frees the channel.
676b86ff981SJens Axboe  */
677b86ff981SJens Axboe void relay_close(struct rchan *chan)
678b86ff981SJens Axboe {
679b86ff981SJens Axboe 	unsigned int i;
680b86ff981SJens Axboe 
681b86ff981SJens Axboe 	if (!chan)
682b86ff981SJens Axboe 		return;
683b86ff981SJens Axboe 
68423c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
68523c88752SMathieu Desnoyers 	if (chan->is_global && chan->buf[0])
68623c88752SMathieu Desnoyers 		relay_close_buf(chan->buf[0]);
68723c88752SMathieu Desnoyers 	else
68823c88752SMathieu Desnoyers 		for_each_possible_cpu(i)
68923c88752SMathieu Desnoyers 			if (chan->buf[i])
690b86ff981SJens Axboe 				relay_close_buf(chan->buf[i]);
691b86ff981SJens Axboe 
692b86ff981SJens Axboe 	if (chan->last_toobig)
693b86ff981SJens Axboe 		printk(KERN_WARNING "relay: one or more items not logged "
694b86ff981SJens Axboe 		       "[item size (%Zd) > sub-buffer size (%Zd)]\n",
695b86ff981SJens Axboe 		       chan->last_toobig, chan->subbuf_size);
696b86ff981SJens Axboe 
69723c88752SMathieu Desnoyers 	list_del(&chan->list);
698b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
69923c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
700b86ff981SJens Axboe }
701b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close);
702b86ff981SJens Axboe 
703b86ff981SJens Axboe /**
704b86ff981SJens Axboe  *	relay_flush - close the channel
705b86ff981SJens Axboe  *	@chan: the channel
706b86ff981SJens Axboe  *
7074c78a663SRandy Dunlap  *	Flushes all channel buffers, i.e. forces buffer switch.
708b86ff981SJens Axboe  */
709b86ff981SJens Axboe void relay_flush(struct rchan *chan)
710b86ff981SJens Axboe {
711b86ff981SJens Axboe 	unsigned int i;
712b86ff981SJens Axboe 
713b86ff981SJens Axboe 	if (!chan)
714b86ff981SJens Axboe 		return;
715b86ff981SJens Axboe 
71623c88752SMathieu Desnoyers 	if (chan->is_global && chan->buf[0]) {
71723c88752SMathieu Desnoyers 		relay_switch_subbuf(chan->buf[0], 0);
71823c88752SMathieu Desnoyers 		return;
719b86ff981SJens Axboe 	}
72023c88752SMathieu Desnoyers 
72123c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
72223c88752SMathieu Desnoyers 	for_each_possible_cpu(i)
72323c88752SMathieu Desnoyers 		if (chan->buf[i])
72423c88752SMathieu Desnoyers 			relay_switch_subbuf(chan->buf[i], 0);
72523c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
726b86ff981SJens Axboe }
727b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush);
728b86ff981SJens Axboe 
729b86ff981SJens Axboe /**
730b86ff981SJens Axboe  *	relay_file_open - open file op for relay files
731b86ff981SJens Axboe  *	@inode: the inode
732b86ff981SJens Axboe  *	@filp: the file
733b86ff981SJens Axboe  *
734b86ff981SJens Axboe  *	Increments the channel buffer refcount.
735b86ff981SJens Axboe  */
736b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp)
737b86ff981SJens Axboe {
7388e18e294STheodore Ts'o 	struct rchan_buf *buf = inode->i_private;
739b86ff981SJens Axboe 	kref_get(&buf->kref);
740b86ff981SJens Axboe 	filp->private_data = buf;
741b86ff981SJens Axboe 
742b86ff981SJens Axboe 	return 0;
743b86ff981SJens Axboe }
744b86ff981SJens Axboe 
745b86ff981SJens Axboe /**
746b86ff981SJens Axboe  *	relay_file_mmap - mmap file op for relay files
747b86ff981SJens Axboe  *	@filp: the file
748b86ff981SJens Axboe  *	@vma: the vma describing what to map
749b86ff981SJens Axboe  *
75072fd4a35SRobert P. J. Day  *	Calls upon relay_mmap_buf() to map the file into user space.
751b86ff981SJens Axboe  */
752b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
753b86ff981SJens Axboe {
754b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
755b86ff981SJens Axboe 	return relay_mmap_buf(buf, vma);
756b86ff981SJens Axboe }
757b86ff981SJens Axboe 
758b86ff981SJens Axboe /**
759b86ff981SJens Axboe  *	relay_file_poll - poll file op for relay files
760b86ff981SJens Axboe  *	@filp: the file
761b86ff981SJens Axboe  *	@wait: poll table
762b86ff981SJens Axboe  *
763b86ff981SJens Axboe  *	Poll implemention.
764b86ff981SJens Axboe  */
765b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
766b86ff981SJens Axboe {
767b86ff981SJens Axboe 	unsigned int mask = 0;
768b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
769b86ff981SJens Axboe 
770b86ff981SJens Axboe 	if (buf->finalized)
771b86ff981SJens Axboe 		return POLLERR;
772b86ff981SJens Axboe 
773b86ff981SJens Axboe 	if (filp->f_mode & FMODE_READ) {
774b86ff981SJens Axboe 		poll_wait(filp, &buf->read_wait, wait);
775b86ff981SJens Axboe 		if (!relay_buf_empty(buf))
776b86ff981SJens Axboe 			mask |= POLLIN | POLLRDNORM;
777b86ff981SJens Axboe 	}
778b86ff981SJens Axboe 
779b86ff981SJens Axboe 	return mask;
780b86ff981SJens Axboe }
781b86ff981SJens Axboe 
782b86ff981SJens Axboe /**
783b86ff981SJens Axboe  *	relay_file_release - release file op for relay files
784b86ff981SJens Axboe  *	@inode: the inode
785b86ff981SJens Axboe  *	@filp: the file
786b86ff981SJens Axboe  *
787b86ff981SJens Axboe  *	Decrements the channel refcount, as the filesystem is
788b86ff981SJens Axboe  *	no longer using it.
789b86ff981SJens Axboe  */
790b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp)
791b86ff981SJens Axboe {
792b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
793b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
794b86ff981SJens Axboe 
795b86ff981SJens Axboe 	return 0;
796b86ff981SJens Axboe }
797b86ff981SJens Axboe 
7984c78a663SRandy Dunlap /*
799b86ff981SJens Axboe  *	relay_file_read_consume - update the consumed count for the buffer
800b86ff981SJens Axboe  */
801b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf,
802b86ff981SJens Axboe 				    size_t read_pos,
803b86ff981SJens Axboe 				    size_t bytes_consumed)
804b86ff981SJens Axboe {
805b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
806b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
807b86ff981SJens Axboe 	size_t read_subbuf;
808b86ff981SJens Axboe 
809b86ff981SJens Axboe 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
810b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
811b86ff981SJens Axboe 		buf->bytes_consumed = 0;
812b86ff981SJens Axboe 	}
813b86ff981SJens Axboe 
814b86ff981SJens Axboe 	buf->bytes_consumed += bytes_consumed;
815b86ff981SJens Axboe 	read_subbuf = read_pos / buf->chan->subbuf_size;
816b86ff981SJens Axboe 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
817b86ff981SJens Axboe 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
818b86ff981SJens Axboe 		    (buf->offset == subbuf_size))
819b86ff981SJens Axboe 			return;
820b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
821b86ff981SJens Axboe 		buf->bytes_consumed = 0;
822b86ff981SJens Axboe 	}
823b86ff981SJens Axboe }
824b86ff981SJens Axboe 
8254c78a663SRandy Dunlap /*
826b86ff981SJens Axboe  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
827b86ff981SJens Axboe  */
828b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
829b86ff981SJens Axboe {
830b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
831b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
832221415d7SJens Axboe 	size_t produced = buf->subbufs_produced;
833221415d7SJens Axboe 	size_t consumed = buf->subbufs_consumed;
834b86ff981SJens Axboe 
835b86ff981SJens Axboe 	relay_file_read_consume(buf, read_pos, 0);
836b86ff981SJens Axboe 
837221415d7SJens Axboe 	if (unlikely(buf->offset > subbuf_size)) {
838221415d7SJens Axboe 		if (produced == consumed)
839221415d7SJens Axboe 			return 0;
840221415d7SJens Axboe 		return 1;
841221415d7SJens Axboe 	}
842221415d7SJens Axboe 
843221415d7SJens Axboe 	if (unlikely(produced - consumed >= n_subbufs)) {
844221415d7SJens Axboe 		consumed = (produced / n_subbufs) * n_subbufs;
845221415d7SJens Axboe 		buf->subbufs_consumed = consumed;
846221415d7SJens Axboe 	}
847221415d7SJens Axboe 
848221415d7SJens Axboe 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
849221415d7SJens Axboe 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
850221415d7SJens Axboe 
851221415d7SJens Axboe 	if (consumed > produced)
852221415d7SJens Axboe 		produced += n_subbufs * subbuf_size;
853221415d7SJens Axboe 
854221415d7SJens Axboe 	if (consumed == produced)
855221415d7SJens Axboe 		return 0;
856221415d7SJens Axboe 
857b86ff981SJens Axboe 	return 1;
858b86ff981SJens Axboe }
859b86ff981SJens Axboe 
860b86ff981SJens Axboe /**
861b86ff981SJens Axboe  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
8624c78a663SRandy Dunlap  *	@read_pos: file read position
8634c78a663SRandy Dunlap  *	@buf: relay channel buffer
864b86ff981SJens Axboe  */
865b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos,
866b86ff981SJens Axboe 					   struct rchan_buf *buf)
867b86ff981SJens Axboe {
868b86ff981SJens Axboe 	size_t padding, avail = 0;
869b86ff981SJens Axboe 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
870b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
871b86ff981SJens Axboe 
872b86ff981SJens Axboe 	write_subbuf = (buf->data - buf->start) / subbuf_size;
873b86ff981SJens Axboe 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
874b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
875b86ff981SJens Axboe 	read_offset = read_pos % subbuf_size;
876b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
877b86ff981SJens Axboe 
878b86ff981SJens Axboe 	if (read_subbuf == write_subbuf) {
879b86ff981SJens Axboe 		if (read_offset + padding < write_offset)
880b86ff981SJens Axboe 			avail = write_offset - (read_offset + padding);
881b86ff981SJens Axboe 	} else
882b86ff981SJens Axboe 		avail = (subbuf_size - padding) - read_offset;
883b86ff981SJens Axboe 
884b86ff981SJens Axboe 	return avail;
885b86ff981SJens Axboe }
886b86ff981SJens Axboe 
887b86ff981SJens Axboe /**
888b86ff981SJens Axboe  *	relay_file_read_start_pos - find the first available byte to read
8894c78a663SRandy Dunlap  *	@read_pos: file read position
8904c78a663SRandy Dunlap  *	@buf: relay channel buffer
891b86ff981SJens Axboe  *
89272fd4a35SRobert P. J. Day  *	If the @read_pos is in the middle of padding, return the
893b86ff981SJens Axboe  *	position of the first actually available byte, otherwise
894b86ff981SJens Axboe  *	return the original value.
895b86ff981SJens Axboe  */
896b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos,
897b86ff981SJens Axboe 					struct rchan_buf *buf)
898b86ff981SJens Axboe {
899b86ff981SJens Axboe 	size_t read_subbuf, padding, padding_start, padding_end;
900b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
901b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
902*8d62fdebSDavid Wilder 	size_t consumed = buf->subbufs_consumed % n_subbufs;
903b86ff981SJens Axboe 
904*8d62fdebSDavid Wilder 	if (!read_pos)
905*8d62fdebSDavid Wilder 		read_pos = consumed * subbuf_size + buf->bytes_consumed;
906b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
907b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
908b86ff981SJens Axboe 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
909b86ff981SJens Axboe 	padding_end = (read_subbuf + 1) * subbuf_size;
910b86ff981SJens Axboe 	if (read_pos >= padding_start && read_pos < padding_end) {
911b86ff981SJens Axboe 		read_subbuf = (read_subbuf + 1) % n_subbufs;
912b86ff981SJens Axboe 		read_pos = read_subbuf * subbuf_size;
913b86ff981SJens Axboe 	}
914b86ff981SJens Axboe 
915b86ff981SJens Axboe 	return read_pos;
916b86ff981SJens Axboe }
917b86ff981SJens Axboe 
918b86ff981SJens Axboe /**
919b86ff981SJens Axboe  *	relay_file_read_end_pos - return the new read position
9204c78a663SRandy Dunlap  *	@read_pos: file read position
9214c78a663SRandy Dunlap  *	@buf: relay channel buffer
9224c78a663SRandy Dunlap  *	@count: number of bytes to be read
923b86ff981SJens Axboe  */
924b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf,
925b86ff981SJens Axboe 				      size_t read_pos,
926b86ff981SJens Axboe 				      size_t count)
927b86ff981SJens Axboe {
928b86ff981SJens Axboe 	size_t read_subbuf, padding, end_pos;
929b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
930b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
931b86ff981SJens Axboe 
932b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
933b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
934b86ff981SJens Axboe 	if (read_pos % subbuf_size + count + padding == subbuf_size)
935b86ff981SJens Axboe 		end_pos = (read_subbuf + 1) * subbuf_size;
936b86ff981SJens Axboe 	else
937b86ff981SJens Axboe 		end_pos = read_pos + count;
938b86ff981SJens Axboe 	if (end_pos >= subbuf_size * n_subbufs)
939b86ff981SJens Axboe 		end_pos = 0;
940b86ff981SJens Axboe 
941b86ff981SJens Axboe 	return end_pos;
942b86ff981SJens Axboe }
943b86ff981SJens Axboe 
9444c78a663SRandy Dunlap /*
9456dac40a7STom Zanussi  *	subbuf_read_actor - read up to one subbuf's worth of data
946b86ff981SJens Axboe  */
9476dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start,
9486dac40a7STom Zanussi 			     struct rchan_buf *buf,
9496dac40a7STom Zanussi 			     size_t avail,
9506dac40a7STom Zanussi 			     read_descriptor_t *desc,
9516dac40a7STom Zanussi 			     read_actor_t actor)
952b86ff981SJens Axboe {
953b86ff981SJens Axboe 	void *from;
9546dac40a7STom Zanussi 	int ret = 0;
955b86ff981SJens Axboe 
956b86ff981SJens Axboe 	from = buf->start + read_start;
9576dac40a7STom Zanussi 	ret = avail;
958ba2397efSAl Viro 	if (copy_to_user(desc->arg.buf, from, avail)) {
9596dac40a7STom Zanussi 		desc->error = -EFAULT;
9606dac40a7STom Zanussi 		ret = 0;
961b86ff981SJens Axboe 	}
9626dac40a7STom Zanussi 	desc->arg.data += ret;
9636dac40a7STom Zanussi 	desc->written += ret;
9646dac40a7STom Zanussi 	desc->count -= ret;
9656dac40a7STom Zanussi 
966b86ff981SJens Axboe 	return ret;
967b86ff981SJens Axboe }
968b86ff981SJens Axboe 
9694c78a663SRandy Dunlap /*
9706dac40a7STom Zanussi  *	subbuf_send_actor - send up to one subbuf's worth of data
9716dac40a7STom Zanussi  */
9726dac40a7STom Zanussi static int subbuf_send_actor(size_t read_start,
9736dac40a7STom Zanussi 			     struct rchan_buf *buf,
9746dac40a7STom Zanussi 			     size_t avail,
9756dac40a7STom Zanussi 			     read_descriptor_t *desc,
9766dac40a7STom Zanussi 			     read_actor_t actor)
9776dac40a7STom Zanussi {
9786dac40a7STom Zanussi 	unsigned long pidx, poff;
9796dac40a7STom Zanussi 	unsigned int subbuf_pages;
9806dac40a7STom Zanussi 	int ret = 0;
9816dac40a7STom Zanussi 
9826dac40a7STom Zanussi 	subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT;
9836dac40a7STom Zanussi 	pidx = (read_start / PAGE_SIZE) % subbuf_pages;
9846dac40a7STom Zanussi 	poff = read_start & ~PAGE_MASK;
9856dac40a7STom Zanussi 	while (avail) {
9866dac40a7STom Zanussi 		struct page *p = buf->page_array[pidx];
9876dac40a7STom Zanussi 		unsigned int len;
9886dac40a7STom Zanussi 
9896dac40a7STom Zanussi 		len = PAGE_SIZE - poff;
9906dac40a7STom Zanussi 		if (len > avail)
9916dac40a7STom Zanussi 			len = avail;
9926dac40a7STom Zanussi 
9936dac40a7STom Zanussi 		len = actor(desc, p, poff, len);
9946dac40a7STom Zanussi 		if (desc->error)
9956dac40a7STom Zanussi 			break;
9966dac40a7STom Zanussi 
9976dac40a7STom Zanussi 		avail -= len;
9986dac40a7STom Zanussi 		ret += len;
9996dac40a7STom Zanussi 		poff = 0;
10006dac40a7STom Zanussi 		pidx = (pidx + 1) % subbuf_pages;
10016dac40a7STom Zanussi 	}
10026dac40a7STom Zanussi 
10036dac40a7STom Zanussi 	return ret;
10046dac40a7STom Zanussi }
10056dac40a7STom Zanussi 
10066dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start,
10076dac40a7STom Zanussi 			       struct rchan_buf *buf,
10086dac40a7STom Zanussi 			       size_t avail,
10096dac40a7STom Zanussi 			       read_descriptor_t *desc,
10106dac40a7STom Zanussi 			       read_actor_t actor);
10116dac40a7STom Zanussi 
10124c78a663SRandy Dunlap /*
10136dac40a7STom Zanussi  *	relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
10146dac40a7STom Zanussi  */
1015192636adSAndrew Morton static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
10166dac40a7STom Zanussi 					subbuf_actor_t subbuf_actor,
10176dac40a7STom Zanussi 					read_actor_t actor,
1018ba2397efSAl Viro 					read_descriptor_t *desc)
1019221415d7SJens Axboe {
1020221415d7SJens Axboe 	struct rchan_buf *buf = filp->private_data;
1021221415d7SJens Axboe 	size_t read_start, avail;
10226dac40a7STom Zanussi 	int ret;
1023221415d7SJens Axboe 
1024ba2397efSAl Viro 	if (!desc->count)
1025221415d7SJens Axboe 		return 0;
1026221415d7SJens Axboe 
1027f3a43f3fSJosef "Jeff" Sipek 	mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
10286dac40a7STom Zanussi 	do {
10296dac40a7STom Zanussi 		if (!relay_file_read_avail(buf, *ppos))
1030221415d7SJens Axboe 			break;
1031221415d7SJens Axboe 
10326dac40a7STom Zanussi 		read_start = relay_file_read_start_pos(*ppos, buf);
10336dac40a7STom Zanussi 		avail = relay_file_read_subbuf_avail(read_start, buf);
10346dac40a7STom Zanussi 		if (!avail)
10356dac40a7STom Zanussi 			break;
1036221415d7SJens Axboe 
1037ba2397efSAl Viro 		avail = min(desc->count, avail);
1038ba2397efSAl Viro 		ret = subbuf_actor(read_start, buf, avail, desc, actor);
1039ba2397efSAl Viro 		if (desc->error < 0)
10406dac40a7STom Zanussi 			break;
10416dac40a7STom Zanussi 
10426dac40a7STom Zanussi 		if (ret) {
1043221415d7SJens Axboe 			relay_file_read_consume(buf, read_start, ret);
1044221415d7SJens Axboe 			*ppos = relay_file_read_end_pos(buf, read_start, ret);
1045221415d7SJens Axboe 		}
1046ba2397efSAl Viro 	} while (desc->count && ret);
1047f3a43f3fSJosef "Jeff" Sipek 	mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
1048221415d7SJens Axboe 
1049ba2397efSAl Viro 	return desc->written;
1050221415d7SJens Axboe }
1051221415d7SJens Axboe 
10526dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp,
10536dac40a7STom Zanussi 			       char __user *buffer,
10546dac40a7STom Zanussi 			       size_t count,
10556dac40a7STom Zanussi 			       loff_t *ppos)
10566dac40a7STom Zanussi {
1057ba2397efSAl Viro 	read_descriptor_t desc;
1058ba2397efSAl Viro 	desc.written = 0;
1059ba2397efSAl Viro 	desc.count = count;
1060ba2397efSAl Viro 	desc.arg.buf = buffer;
1061ba2397efSAl Viro 	desc.error = 0;
1062ba2397efSAl Viro 	return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
1063ba2397efSAl Viro 				       NULL, &desc);
10646dac40a7STom Zanussi }
10656dac40a7STom Zanussi 
10666dac40a7STom Zanussi static ssize_t relay_file_sendfile(struct file *filp,
10676dac40a7STom Zanussi 				   loff_t *ppos,
10686dac40a7STom Zanussi 				   size_t count,
10696dac40a7STom Zanussi 				   read_actor_t actor,
1070221415d7SJens Axboe 				   void *target)
1071221415d7SJens Axboe {
1072ba2397efSAl Viro 	read_descriptor_t desc;
1073ba2397efSAl Viro 	desc.written = 0;
1074ba2397efSAl Viro 	desc.count = count;
1075ba2397efSAl Viro 	desc.arg.data = target;
1076ba2397efSAl Viro 	desc.error = 0;
1077ba2397efSAl Viro 	return relay_file_read_subbufs(filp, ppos, subbuf_send_actor,
1078ba2397efSAl Viro 				       actor, &desc);
1079221415d7SJens Axboe }
1080221415d7SJens Axboe 
108115ad7cdcSHelge Deller const struct file_operations relay_file_operations = {
1082b86ff981SJens Axboe 	.open		= relay_file_open,
1083b86ff981SJens Axboe 	.poll		= relay_file_poll,
1084b86ff981SJens Axboe 	.mmap		= relay_file_mmap,
1085b86ff981SJens Axboe 	.read		= relay_file_read,
1086b86ff981SJens Axboe 	.llseek		= no_llseek,
1087b86ff981SJens Axboe 	.release	= relay_file_release,
1088221415d7SJens Axboe 	.sendfile       = relay_file_sendfile,
1089b86ff981SJens Axboe };
1090b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations);
109123c88752SMathieu Desnoyers 
109223c88752SMathieu Desnoyers static __init int relay_init(void)
109323c88752SMathieu Desnoyers {
109423c88752SMathieu Desnoyers 
109523c88752SMathieu Desnoyers 	hotcpu_notifier(relay_hotcpu_callback, 0);
109623c88752SMathieu Desnoyers 	return 0;
109723c88752SMathieu Desnoyers }
109823c88752SMathieu Desnoyers 
109923c88752SMathieu Desnoyers module_init(relay_init);
1100