xref: /linux/kernel/relay.c (revision 6dac40a7ce2483a47b54af07afebeb84131c7228)
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.
10b86ff981SJens Axboe  *
11b86ff981SJens Axboe  * This file is released under the GPL.
12b86ff981SJens Axboe  */
13b86ff981SJens Axboe #include <linux/errno.h>
14b86ff981SJens Axboe #include <linux/stddef.h>
15b86ff981SJens Axboe #include <linux/slab.h>
16b86ff981SJens Axboe #include <linux/module.h>
17b86ff981SJens Axboe #include <linux/string.h>
18b86ff981SJens Axboe #include <linux/relay.h>
19b86ff981SJens Axboe #include <linux/vmalloc.h>
20b86ff981SJens Axboe #include <linux/mm.h>
21b86ff981SJens Axboe 
22b86ff981SJens Axboe /*
23b86ff981SJens Axboe  * close() vm_op implementation for relay file mapping.
24b86ff981SJens Axboe  */
25b86ff981SJens Axboe static void relay_file_mmap_close(struct vm_area_struct *vma)
26b86ff981SJens Axboe {
27b86ff981SJens Axboe 	struct rchan_buf *buf = vma->vm_private_data;
28b86ff981SJens Axboe 	buf->chan->cb->buf_unmapped(buf, vma->vm_file);
29b86ff981SJens Axboe }
30b86ff981SJens Axboe 
31b86ff981SJens Axboe /*
32b86ff981SJens Axboe  * nopage() vm_op implementation for relay file mapping.
33b86ff981SJens Axboe  */
34b86ff981SJens Axboe static struct page *relay_buf_nopage(struct vm_area_struct *vma,
35b86ff981SJens Axboe 				     unsigned long address,
36b86ff981SJens Axboe 				     int *type)
37b86ff981SJens Axboe {
38b86ff981SJens Axboe 	struct page *page;
39b86ff981SJens Axboe 	struct rchan_buf *buf = vma->vm_private_data;
40b86ff981SJens Axboe 	unsigned long offset = address - vma->vm_start;
41b86ff981SJens Axboe 
42b86ff981SJens Axboe 	if (address > vma->vm_end)
43b86ff981SJens Axboe 		return NOPAGE_SIGBUS; /* Disallow mremap */
44b86ff981SJens Axboe 	if (!buf)
45b86ff981SJens Axboe 		return NOPAGE_OOM;
46b86ff981SJens Axboe 
47b86ff981SJens Axboe 	page = vmalloc_to_page(buf->start + offset);
48b86ff981SJens Axboe 	if (!page)
49b86ff981SJens Axboe 		return NOPAGE_OOM;
50b86ff981SJens Axboe 	get_page(page);
51b86ff981SJens Axboe 
52b86ff981SJens Axboe 	if (type)
53b86ff981SJens Axboe 		*type = VM_FAULT_MINOR;
54b86ff981SJens Axboe 
55b86ff981SJens Axboe 	return page;
56b86ff981SJens Axboe }
57b86ff981SJens Axboe 
58b86ff981SJens Axboe /*
59b86ff981SJens Axboe  * vm_ops for relay file mappings.
60b86ff981SJens Axboe  */
61b86ff981SJens Axboe static struct vm_operations_struct relay_file_mmap_ops = {
62b86ff981SJens Axboe 	.nopage = relay_buf_nopage,
63b86ff981SJens Axboe 	.close = relay_file_mmap_close,
64b86ff981SJens Axboe };
65b86ff981SJens Axboe 
66b86ff981SJens Axboe /**
67b86ff981SJens Axboe  *	relay_mmap_buf: - mmap channel buffer to process address space
68b86ff981SJens Axboe  *	@buf: relay channel buffer
69b86ff981SJens Axboe  *	@vma: vm_area_struct describing memory to be mapped
70b86ff981SJens Axboe  *
71b86ff981SJens Axboe  *	Returns 0 if ok, negative on error
72b86ff981SJens Axboe  *
73b86ff981SJens Axboe  *	Caller should already have grabbed mmap_sem.
74b86ff981SJens Axboe  */
75b86ff981SJens Axboe int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
76b86ff981SJens Axboe {
77b86ff981SJens Axboe 	unsigned long length = vma->vm_end - vma->vm_start;
78b86ff981SJens Axboe 	struct file *filp = vma->vm_file;
79b86ff981SJens Axboe 
80b86ff981SJens Axboe 	if (!buf)
81b86ff981SJens Axboe 		return -EBADF;
82b86ff981SJens Axboe 
83b86ff981SJens Axboe 	if (length != (unsigned long)buf->chan->alloc_size)
84b86ff981SJens Axboe 		return -EINVAL;
85b86ff981SJens Axboe 
86b86ff981SJens Axboe 	vma->vm_ops = &relay_file_mmap_ops;
87b86ff981SJens Axboe 	vma->vm_private_data = buf;
88b86ff981SJens Axboe 	buf->chan->cb->buf_mapped(buf, filp);
89b86ff981SJens Axboe 
90b86ff981SJens Axboe 	return 0;
91b86ff981SJens Axboe }
92b86ff981SJens Axboe 
93b86ff981SJens Axboe /**
94b86ff981SJens Axboe  *	relay_alloc_buf - allocate a channel buffer
95b86ff981SJens Axboe  *	@buf: the buffer struct
96b86ff981SJens Axboe  *	@size: total size of the buffer
97b86ff981SJens Axboe  *
98221415d7SJens Axboe  *	Returns a pointer to the resulting buffer, NULL if unsuccessful. The
99221415d7SJens Axboe  *	passed in size will get page aligned, if it isn't already.
100b86ff981SJens Axboe  */
101221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
102b86ff981SJens Axboe {
103b86ff981SJens Axboe 	void *mem;
104b86ff981SJens Axboe 	unsigned int i, j, n_pages;
105b86ff981SJens Axboe 
106221415d7SJens Axboe 	*size = PAGE_ALIGN(*size);
107221415d7SJens Axboe 	n_pages = *size >> PAGE_SHIFT;
108b86ff981SJens Axboe 
109b86ff981SJens Axboe 	buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
110b86ff981SJens Axboe 	if (!buf->page_array)
111b86ff981SJens Axboe 		return NULL;
112b86ff981SJens Axboe 
113b86ff981SJens Axboe 	for (i = 0; i < n_pages; i++) {
114b86ff981SJens Axboe 		buf->page_array[i] = alloc_page(GFP_KERNEL);
115b86ff981SJens Axboe 		if (unlikely(!buf->page_array[i]))
116b86ff981SJens Axboe 			goto depopulate;
117b86ff981SJens Axboe 	}
118b86ff981SJens Axboe 	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
119b86ff981SJens Axboe 	if (!mem)
120b86ff981SJens Axboe 		goto depopulate;
121b86ff981SJens Axboe 
122221415d7SJens Axboe 	memset(mem, 0, *size);
123b86ff981SJens Axboe 	buf->page_count = n_pages;
124b86ff981SJens Axboe 	return mem;
125b86ff981SJens Axboe 
126b86ff981SJens Axboe depopulate:
127b86ff981SJens Axboe 	for (j = 0; j < i; j++)
128b86ff981SJens Axboe 		__free_page(buf->page_array[j]);
129b86ff981SJens Axboe 	kfree(buf->page_array);
130b86ff981SJens Axboe 	return NULL;
131b86ff981SJens Axboe }
132b86ff981SJens Axboe 
133b86ff981SJens Axboe /**
134b86ff981SJens Axboe  *	relay_create_buf - allocate and initialize a channel buffer
135b86ff981SJens Axboe  *	@alloc_size: size of the buffer to allocate
136b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers in the channel
137b86ff981SJens Axboe  *
138b86ff981SJens Axboe  *	Returns channel buffer if successful, NULL otherwise
139b86ff981SJens Axboe  */
140b86ff981SJens Axboe struct rchan_buf *relay_create_buf(struct rchan *chan)
141b86ff981SJens Axboe {
142b86ff981SJens Axboe 	struct rchan_buf *buf = kcalloc(1, sizeof(struct rchan_buf), GFP_KERNEL);
143b86ff981SJens Axboe 	if (!buf)
144b86ff981SJens Axboe 		return NULL;
145b86ff981SJens Axboe 
146b86ff981SJens Axboe 	buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
147b86ff981SJens Axboe 	if (!buf->padding)
148b86ff981SJens Axboe 		goto free_buf;
149b86ff981SJens Axboe 
150221415d7SJens Axboe 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
151b86ff981SJens Axboe 	if (!buf->start)
152b86ff981SJens Axboe 		goto free_buf;
153b86ff981SJens Axboe 
154b86ff981SJens Axboe 	buf->chan = chan;
155b86ff981SJens Axboe 	kref_get(&buf->chan->kref);
156b86ff981SJens Axboe 	return buf;
157b86ff981SJens Axboe 
158b86ff981SJens Axboe free_buf:
159b86ff981SJens Axboe 	kfree(buf->padding);
160b86ff981SJens Axboe 	kfree(buf);
161b86ff981SJens Axboe 	return NULL;
162b86ff981SJens Axboe }
163b86ff981SJens Axboe 
164b86ff981SJens Axboe /**
165b86ff981SJens Axboe  *	relay_destroy_channel - free the channel struct
166b86ff981SJens Axboe  *
167b86ff981SJens Axboe  *	Should only be called from kref_put().
168b86ff981SJens Axboe  */
169b86ff981SJens Axboe void relay_destroy_channel(struct kref *kref)
170b86ff981SJens Axboe {
171b86ff981SJens Axboe 	struct rchan *chan = container_of(kref, struct rchan, kref);
172b86ff981SJens Axboe 	kfree(chan);
173b86ff981SJens Axboe }
174b86ff981SJens Axboe 
175b86ff981SJens Axboe /**
176b86ff981SJens Axboe  *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
177b86ff981SJens Axboe  *	@buf: the buffer struct
178b86ff981SJens Axboe  */
179b86ff981SJens Axboe void relay_destroy_buf(struct rchan_buf *buf)
180b86ff981SJens Axboe {
181b86ff981SJens Axboe 	struct rchan *chan = buf->chan;
182b86ff981SJens Axboe 	unsigned int i;
183b86ff981SJens Axboe 
184b86ff981SJens Axboe 	if (likely(buf->start)) {
185b86ff981SJens Axboe 		vunmap(buf->start);
186b86ff981SJens Axboe 		for (i = 0; i < buf->page_count; i++)
187b86ff981SJens Axboe 			__free_page(buf->page_array[i]);
188b86ff981SJens Axboe 		kfree(buf->page_array);
189b86ff981SJens Axboe 	}
190b86ff981SJens Axboe 	kfree(buf->padding);
191b86ff981SJens Axboe 	kfree(buf);
192b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
193b86ff981SJens Axboe }
194b86ff981SJens Axboe 
195b86ff981SJens Axboe /**
196b86ff981SJens Axboe  *	relay_remove_buf - remove a channel buffer
197b86ff981SJens Axboe  *
198b86ff981SJens Axboe  *	Removes the file from the fileystem, which also frees the
199b86ff981SJens Axboe  *	rchan_buf_struct and the channel buffer.  Should only be called from
200b86ff981SJens Axboe  *	kref_put().
201b86ff981SJens Axboe  */
202b86ff981SJens Axboe void relay_remove_buf(struct kref *kref)
203b86ff981SJens Axboe {
204b86ff981SJens Axboe 	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
205b86ff981SJens Axboe 	buf->chan->cb->remove_buf_file(buf->dentry);
206b86ff981SJens Axboe 	relay_destroy_buf(buf);
207b86ff981SJens Axboe }
208b86ff981SJens Axboe 
209b86ff981SJens Axboe /**
210b86ff981SJens Axboe  *	relay_buf_empty - boolean, is the channel buffer empty?
211b86ff981SJens Axboe  *	@buf: channel buffer
212b86ff981SJens Axboe  *
213b86ff981SJens Axboe  *	Returns 1 if the buffer is empty, 0 otherwise.
214b86ff981SJens Axboe  */
215b86ff981SJens Axboe int relay_buf_empty(struct rchan_buf *buf)
216b86ff981SJens Axboe {
217b86ff981SJens Axboe 	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
218b86ff981SJens Axboe }
219b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_empty);
220b86ff981SJens Axboe 
221b86ff981SJens Axboe /**
222b86ff981SJens Axboe  *	relay_buf_full - boolean, is the channel buffer full?
223b86ff981SJens Axboe  *	@buf: channel buffer
224b86ff981SJens Axboe  *
225b86ff981SJens Axboe  *	Returns 1 if the buffer is full, 0 otherwise.
226b86ff981SJens Axboe  */
227b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf)
228b86ff981SJens Axboe {
229b86ff981SJens Axboe 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
230b86ff981SJens Axboe 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
231b86ff981SJens Axboe }
232b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full);
233b86ff981SJens Axboe 
234b86ff981SJens Axboe /*
235b86ff981SJens Axboe  * High-level relay kernel API and associated functions.
236b86ff981SJens Axboe  */
237b86ff981SJens Axboe 
238b86ff981SJens Axboe /*
239b86ff981SJens Axboe  * rchan_callback implementations defining default channel behavior.  Used
240b86ff981SJens Axboe  * in place of corresponding NULL values in client callback struct.
241b86ff981SJens Axboe  */
242b86ff981SJens Axboe 
243b86ff981SJens Axboe /*
244b86ff981SJens Axboe  * subbuf_start() default callback.  Does nothing.
245b86ff981SJens Axboe  */
246b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf,
247b86ff981SJens Axboe 					  void *subbuf,
248b86ff981SJens Axboe 					  void *prev_subbuf,
249b86ff981SJens Axboe 					  size_t prev_padding)
250b86ff981SJens Axboe {
251b86ff981SJens Axboe 	if (relay_buf_full(buf))
252b86ff981SJens Axboe 		return 0;
253b86ff981SJens Axboe 
254b86ff981SJens Axboe 	return 1;
255b86ff981SJens Axboe }
256b86ff981SJens Axboe 
257b86ff981SJens Axboe /*
258b86ff981SJens Axboe  * buf_mapped() default callback.  Does nothing.
259b86ff981SJens Axboe  */
260b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf,
261b86ff981SJens Axboe 					struct file *filp)
262b86ff981SJens Axboe {
263b86ff981SJens Axboe }
264b86ff981SJens Axboe 
265b86ff981SJens Axboe /*
266b86ff981SJens Axboe  * buf_unmapped() default callback.  Does nothing.
267b86ff981SJens Axboe  */
268b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf,
269b86ff981SJens Axboe 					  struct file *filp)
270b86ff981SJens Axboe {
271b86ff981SJens Axboe }
272b86ff981SJens Axboe 
273b86ff981SJens Axboe /*
274b86ff981SJens Axboe  * create_buf_file_create() default callback.  Does nothing.
275b86ff981SJens Axboe  */
276b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename,
277b86ff981SJens Axboe 						       struct dentry *parent,
278b86ff981SJens Axboe 						       int mode,
279b86ff981SJens Axboe 						       struct rchan_buf *buf,
280b86ff981SJens Axboe 						       int *is_global)
281b86ff981SJens Axboe {
282b86ff981SJens Axboe 	return NULL;
283b86ff981SJens Axboe }
284b86ff981SJens Axboe 
285b86ff981SJens Axboe /*
286b86ff981SJens Axboe  * remove_buf_file() default callback.  Does nothing.
287b86ff981SJens Axboe  */
288b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry)
289b86ff981SJens Axboe {
290b86ff981SJens Axboe 	return -EINVAL;
291b86ff981SJens Axboe }
292b86ff981SJens Axboe 
293b86ff981SJens Axboe /* relay channel default callbacks */
294b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = {
295b86ff981SJens Axboe 	.subbuf_start = subbuf_start_default_callback,
296b86ff981SJens Axboe 	.buf_mapped = buf_mapped_default_callback,
297b86ff981SJens Axboe 	.buf_unmapped = buf_unmapped_default_callback,
298b86ff981SJens Axboe 	.create_buf_file = create_buf_file_default_callback,
299b86ff981SJens Axboe 	.remove_buf_file = remove_buf_file_default_callback,
300b86ff981SJens Axboe };
301b86ff981SJens Axboe 
302b86ff981SJens Axboe /**
303b86ff981SJens Axboe  *	wakeup_readers - wake up readers waiting on a channel
304b86ff981SJens Axboe  *	@private: the channel buffer
305b86ff981SJens Axboe  *
306b86ff981SJens Axboe  *	This is the work function used to defer reader waking.  The
307b86ff981SJens Axboe  *	reason waking is deferred is that calling directly from write
308b86ff981SJens Axboe  *	causes problems if you're writing from say the scheduler.
309b86ff981SJens Axboe  */
310b86ff981SJens Axboe static void wakeup_readers(void *private)
311b86ff981SJens Axboe {
312b86ff981SJens Axboe 	struct rchan_buf *buf = private;
313b86ff981SJens Axboe 	wake_up_interruptible(&buf->read_wait);
314b86ff981SJens Axboe }
315b86ff981SJens Axboe 
316b86ff981SJens Axboe /**
317b86ff981SJens Axboe  *	__relay_reset - reset a channel buffer
318b86ff981SJens Axboe  *	@buf: the channel buffer
319b86ff981SJens Axboe  *	@init: 1 if this is a first-time initialization
320b86ff981SJens Axboe  *
321b86ff981SJens Axboe  *	See relay_reset for description of effect.
322b86ff981SJens Axboe  */
323b86ff981SJens Axboe static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
324b86ff981SJens Axboe {
325b86ff981SJens Axboe 	size_t i;
326b86ff981SJens Axboe 
327b86ff981SJens Axboe 	if (init) {
328b86ff981SJens Axboe 		init_waitqueue_head(&buf->read_wait);
329b86ff981SJens Axboe 		kref_init(&buf->kref);
330b86ff981SJens Axboe 		INIT_WORK(&buf->wake_readers, NULL, NULL);
331b86ff981SJens Axboe 	} else {
332b86ff981SJens Axboe 		cancel_delayed_work(&buf->wake_readers);
333b86ff981SJens Axboe 		flush_scheduled_work();
334b86ff981SJens Axboe 	}
335b86ff981SJens Axboe 
336b86ff981SJens Axboe 	buf->subbufs_produced = 0;
337b86ff981SJens Axboe 	buf->subbufs_consumed = 0;
338b86ff981SJens Axboe 	buf->bytes_consumed = 0;
339b86ff981SJens Axboe 	buf->finalized = 0;
340b86ff981SJens Axboe 	buf->data = buf->start;
341b86ff981SJens Axboe 	buf->offset = 0;
342b86ff981SJens Axboe 
343b86ff981SJens Axboe 	for (i = 0; i < buf->chan->n_subbufs; i++)
344b86ff981SJens Axboe 		buf->padding[i] = 0;
345b86ff981SJens Axboe 
346b86ff981SJens Axboe 	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
347b86ff981SJens Axboe }
348b86ff981SJens Axboe 
349b86ff981SJens Axboe /**
350b86ff981SJens Axboe  *	relay_reset - reset the channel
351b86ff981SJens Axboe  *	@chan: the channel
352b86ff981SJens Axboe  *
353b86ff981SJens Axboe  *	This has the effect of erasing all data from all channel buffers
354b86ff981SJens Axboe  *	and restarting the channel in its initial state.  The buffers
355b86ff981SJens Axboe  *	are not freed, so any mappings are still in effect.
356b86ff981SJens Axboe  *
357b86ff981SJens Axboe  *	NOTE: Care should be taken that the channel isn't actually
358b86ff981SJens Axboe  *	being used by anything when this call is made.
359b86ff981SJens Axboe  */
360b86ff981SJens Axboe void relay_reset(struct rchan *chan)
361b86ff981SJens Axboe {
362b86ff981SJens Axboe 	unsigned int i;
363b86ff981SJens Axboe 	struct rchan_buf *prev = NULL;
364b86ff981SJens Axboe 
365b86ff981SJens Axboe 	if (!chan)
366b86ff981SJens Axboe 		return;
367b86ff981SJens Axboe 
368b86ff981SJens Axboe 	for (i = 0; i < NR_CPUS; i++) {
369b86ff981SJens Axboe 		if (!chan->buf[i] || chan->buf[i] == prev)
370b86ff981SJens Axboe 			break;
371b86ff981SJens Axboe 		__relay_reset(chan->buf[i], 0);
372b86ff981SJens Axboe 		prev = chan->buf[i];
373b86ff981SJens Axboe 	}
374b86ff981SJens Axboe }
375b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset);
376b86ff981SJens Axboe 
377b86ff981SJens Axboe /**
378b86ff981SJens Axboe  *	relay_open_buf - create a new relay channel buffer
379b86ff981SJens Axboe  *
380b86ff981SJens Axboe  *	Internal - used by relay_open().
381b86ff981SJens Axboe  */
382b86ff981SJens Axboe static struct rchan_buf *relay_open_buf(struct rchan *chan,
383b86ff981SJens Axboe 					const char *filename,
384b86ff981SJens Axboe 					struct dentry *parent,
385b86ff981SJens Axboe 					int *is_global)
386b86ff981SJens Axboe {
387b86ff981SJens Axboe 	struct rchan_buf *buf;
388b86ff981SJens Axboe 	struct dentry *dentry;
389b86ff981SJens Axboe 
390b86ff981SJens Axboe 	if (*is_global)
391b86ff981SJens Axboe 		return chan->buf[0];
392b86ff981SJens Axboe 
393b86ff981SJens Axboe 	buf = relay_create_buf(chan);
394b86ff981SJens Axboe 	if (!buf)
395b86ff981SJens Axboe 		return NULL;
396b86ff981SJens Axboe 
397b86ff981SJens Axboe 	/* Create file in fs */
398b86ff981SJens Axboe 	dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
399b86ff981SJens Axboe 					   buf, is_global);
400b86ff981SJens Axboe 	if (!dentry) {
401b86ff981SJens Axboe 		relay_destroy_buf(buf);
402b86ff981SJens Axboe 		return NULL;
403b86ff981SJens Axboe 	}
404b86ff981SJens Axboe 
405b86ff981SJens Axboe 	buf->dentry = dentry;
406b86ff981SJens Axboe 	__relay_reset(buf, 1);
407b86ff981SJens Axboe 
408b86ff981SJens Axboe 	return buf;
409b86ff981SJens Axboe }
410b86ff981SJens Axboe 
411b86ff981SJens Axboe /**
412b86ff981SJens Axboe  *	relay_close_buf - close a channel buffer
413b86ff981SJens Axboe  *	@buf: channel buffer
414b86ff981SJens Axboe  *
415b86ff981SJens Axboe  *	Marks the buffer finalized and restores the default callbacks.
416b86ff981SJens Axboe  *	The channel buffer and channel buffer data structure are then freed
417b86ff981SJens Axboe  *	automatically when the last reference is given up.
418b86ff981SJens Axboe  */
419b86ff981SJens Axboe static inline void relay_close_buf(struct rchan_buf *buf)
420b86ff981SJens Axboe {
421b86ff981SJens Axboe 	buf->finalized = 1;
422b86ff981SJens Axboe 	cancel_delayed_work(&buf->wake_readers);
423b86ff981SJens Axboe 	flush_scheduled_work();
424b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
425b86ff981SJens Axboe }
426b86ff981SJens Axboe 
427b86ff981SJens Axboe static inline void setup_callbacks(struct rchan *chan,
428b86ff981SJens Axboe 				   struct rchan_callbacks *cb)
429b86ff981SJens Axboe {
430b86ff981SJens Axboe 	if (!cb) {
431b86ff981SJens Axboe 		chan->cb = &default_channel_callbacks;
432b86ff981SJens Axboe 		return;
433b86ff981SJens Axboe 	}
434b86ff981SJens Axboe 
435b86ff981SJens Axboe 	if (!cb->subbuf_start)
436b86ff981SJens Axboe 		cb->subbuf_start = subbuf_start_default_callback;
437b86ff981SJens Axboe 	if (!cb->buf_mapped)
438b86ff981SJens Axboe 		cb->buf_mapped = buf_mapped_default_callback;
439b86ff981SJens Axboe 	if (!cb->buf_unmapped)
440b86ff981SJens Axboe 		cb->buf_unmapped = buf_unmapped_default_callback;
441b86ff981SJens Axboe 	if (!cb->create_buf_file)
442b86ff981SJens Axboe 		cb->create_buf_file = create_buf_file_default_callback;
443b86ff981SJens Axboe 	if (!cb->remove_buf_file)
444b86ff981SJens Axboe 		cb->remove_buf_file = remove_buf_file_default_callback;
445b86ff981SJens Axboe 	chan->cb = cb;
446b86ff981SJens Axboe }
447b86ff981SJens Axboe 
448b86ff981SJens Axboe /**
449b86ff981SJens Axboe  *	relay_open - create a new relay channel
450b86ff981SJens Axboe  *	@base_filename: base name of files to create
451b86ff981SJens Axboe  *	@parent: dentry of parent directory, NULL for root directory
452b86ff981SJens Axboe  *	@subbuf_size: size of sub-buffers
453b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers
454b86ff981SJens Axboe  *	@cb: client callback functions
455b86ff981SJens Axboe  *
456b86ff981SJens Axboe  *	Returns channel pointer if successful, NULL otherwise.
457b86ff981SJens Axboe  *
458b86ff981SJens Axboe  *	Creates a channel buffer for each cpu using the sizes and
459b86ff981SJens Axboe  *	attributes specified.  The created channel buffer files
460b86ff981SJens Axboe  *	will be named base_filename0...base_filenameN-1.  File
461b86ff981SJens Axboe  *	permissions will be S_IRUSR.
462b86ff981SJens Axboe  */
463b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
464b86ff981SJens Axboe 			 struct dentry *parent,
465b86ff981SJens Axboe 			 size_t subbuf_size,
466b86ff981SJens Axboe 			 size_t n_subbufs,
467b86ff981SJens Axboe 			 struct rchan_callbacks *cb)
468b86ff981SJens Axboe {
469b86ff981SJens Axboe 	unsigned int i;
470b86ff981SJens Axboe 	struct rchan *chan;
471b86ff981SJens Axboe 	char *tmpname;
472b86ff981SJens Axboe 	int is_global = 0;
473b86ff981SJens Axboe 
474b86ff981SJens Axboe 	if (!base_filename)
475b86ff981SJens Axboe 		return NULL;
476b86ff981SJens Axboe 
477b86ff981SJens Axboe 	if (!(subbuf_size && n_subbufs))
478b86ff981SJens Axboe 		return NULL;
479b86ff981SJens Axboe 
480b86ff981SJens Axboe 	chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
481b86ff981SJens Axboe 	if (!chan)
482b86ff981SJens Axboe 		return NULL;
483b86ff981SJens Axboe 
484b86ff981SJens Axboe 	chan->version = RELAYFS_CHANNEL_VERSION;
485b86ff981SJens Axboe 	chan->n_subbufs = n_subbufs;
486b86ff981SJens Axboe 	chan->subbuf_size = subbuf_size;
487b86ff981SJens Axboe 	chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
488b86ff981SJens Axboe 	setup_callbacks(chan, cb);
489b86ff981SJens Axboe 	kref_init(&chan->kref);
490b86ff981SJens Axboe 
491b86ff981SJens Axboe 	tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
492b86ff981SJens Axboe 	if (!tmpname)
493b86ff981SJens Axboe 		goto free_chan;
494b86ff981SJens Axboe 
495b86ff981SJens Axboe 	for_each_online_cpu(i) {
496b86ff981SJens Axboe 		sprintf(tmpname, "%s%d", base_filename, i);
497b86ff981SJens Axboe 		chan->buf[i] = relay_open_buf(chan, tmpname, parent,
498b86ff981SJens Axboe 					      &is_global);
499b86ff981SJens Axboe 		if (!chan->buf[i])
500b86ff981SJens Axboe 			goto free_bufs;
501b86ff981SJens Axboe 
502b86ff981SJens Axboe 		chan->buf[i]->cpu = i;
503b86ff981SJens Axboe 	}
504b86ff981SJens Axboe 
505b86ff981SJens Axboe 	kfree(tmpname);
506b86ff981SJens Axboe 	return chan;
507b86ff981SJens Axboe 
508b86ff981SJens Axboe free_bufs:
509b86ff981SJens Axboe 	for (i = 0; i < NR_CPUS; i++) {
510b86ff981SJens Axboe 		if (!chan->buf[i])
511b86ff981SJens Axboe 			break;
512b86ff981SJens Axboe 		relay_close_buf(chan->buf[i]);
513b86ff981SJens Axboe 		if (is_global)
514b86ff981SJens Axboe 			break;
515b86ff981SJens Axboe 	}
516b86ff981SJens Axboe 	kfree(tmpname);
517b86ff981SJens Axboe 
518b86ff981SJens Axboe free_chan:
519b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
520b86ff981SJens Axboe 	return NULL;
521b86ff981SJens Axboe }
522b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open);
523b86ff981SJens Axboe 
524b86ff981SJens Axboe /**
525b86ff981SJens Axboe  *	relay_switch_subbuf - switch to a new sub-buffer
526b86ff981SJens Axboe  *	@buf: channel buffer
527b86ff981SJens Axboe  *	@length: size of current event
528b86ff981SJens Axboe  *
529b86ff981SJens Axboe  *	Returns either the length passed in or 0 if full.
530b86ff981SJens Axboe  *
531b86ff981SJens Axboe  *	Performs sub-buffer-switch tasks such as invoking callbacks,
532b86ff981SJens Axboe  *	updating padding counts, waking up readers, etc.
533b86ff981SJens Axboe  */
534b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
535b86ff981SJens Axboe {
536b86ff981SJens Axboe 	void *old, *new;
537b86ff981SJens Axboe 	size_t old_subbuf, new_subbuf;
538b86ff981SJens Axboe 
539b86ff981SJens Axboe 	if (unlikely(length > buf->chan->subbuf_size))
540b86ff981SJens Axboe 		goto toobig;
541b86ff981SJens Axboe 
542b86ff981SJens Axboe 	if (buf->offset != buf->chan->subbuf_size + 1) {
543b86ff981SJens Axboe 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
544b86ff981SJens Axboe 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
545b86ff981SJens Axboe 		buf->padding[old_subbuf] = buf->prev_padding;
546b86ff981SJens Axboe 		buf->subbufs_produced++;
547221415d7SJens Axboe 		buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
548221415d7SJens Axboe 			buf->padding[old_subbuf];
549221415d7SJens Axboe 		smp_mb();
550b86ff981SJens Axboe 		if (waitqueue_active(&buf->read_wait)) {
551b86ff981SJens Axboe 			PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
552b86ff981SJens Axboe 			schedule_delayed_work(&buf->wake_readers, 1);
553b86ff981SJens Axboe 		}
554b86ff981SJens Axboe 	}
555b86ff981SJens Axboe 
556b86ff981SJens Axboe 	old = buf->data;
557b86ff981SJens Axboe 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
558b86ff981SJens Axboe 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
559b86ff981SJens Axboe 	buf->offset = 0;
560b86ff981SJens Axboe 	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
561b86ff981SJens Axboe 		buf->offset = buf->chan->subbuf_size + 1;
562b86ff981SJens Axboe 		return 0;
563b86ff981SJens Axboe 	}
564b86ff981SJens Axboe 	buf->data = new;
565b86ff981SJens Axboe 	buf->padding[new_subbuf] = 0;
566b86ff981SJens Axboe 
567b86ff981SJens Axboe 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
568b86ff981SJens Axboe 		goto toobig;
569b86ff981SJens Axboe 
570b86ff981SJens Axboe 	return length;
571b86ff981SJens Axboe 
572b86ff981SJens Axboe toobig:
573b86ff981SJens Axboe 	buf->chan->last_toobig = length;
574b86ff981SJens Axboe 	return 0;
575b86ff981SJens Axboe }
576b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf);
577b86ff981SJens Axboe 
578b86ff981SJens Axboe /**
579b86ff981SJens Axboe  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
580b86ff981SJens Axboe  *	@chan: the channel
581b86ff981SJens Axboe  *	@cpu: the cpu associated with the channel buffer to update
582b86ff981SJens Axboe  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
583b86ff981SJens Axboe  *
584b86ff981SJens Axboe  *	Adds to the channel buffer's consumed sub-buffer count.
585b86ff981SJens Axboe  *	subbufs_consumed should be the number of sub-buffers newly consumed,
586b86ff981SJens Axboe  *	not the total consumed.
587b86ff981SJens Axboe  *
588b86ff981SJens Axboe  *	NOTE: kernel clients don't need to call this function if the channel
589b86ff981SJens Axboe  *	mode is 'overwrite'.
590b86ff981SJens Axboe  */
591b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan,
592b86ff981SJens Axboe 			    unsigned int cpu,
593b86ff981SJens Axboe 			    size_t subbufs_consumed)
594b86ff981SJens Axboe {
595b86ff981SJens Axboe 	struct rchan_buf *buf;
596b86ff981SJens Axboe 
597b86ff981SJens Axboe 	if (!chan)
598b86ff981SJens Axboe 		return;
599b86ff981SJens Axboe 
600b86ff981SJens Axboe 	if (cpu >= NR_CPUS || !chan->buf[cpu])
601b86ff981SJens Axboe 		return;
602b86ff981SJens Axboe 
603b86ff981SJens Axboe 	buf = chan->buf[cpu];
604b86ff981SJens Axboe 	buf->subbufs_consumed += subbufs_consumed;
605b86ff981SJens Axboe 	if (buf->subbufs_consumed > buf->subbufs_produced)
606b86ff981SJens Axboe 		buf->subbufs_consumed = buf->subbufs_produced;
607b86ff981SJens Axboe }
608b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
609b86ff981SJens Axboe 
610b86ff981SJens Axboe /**
611b86ff981SJens Axboe  *	relay_close - close the channel
612b86ff981SJens Axboe  *	@chan: the channel
613b86ff981SJens Axboe  *
614b86ff981SJens Axboe  *	Closes all channel buffers and frees the channel.
615b86ff981SJens Axboe  */
616b86ff981SJens Axboe void relay_close(struct rchan *chan)
617b86ff981SJens Axboe {
618b86ff981SJens Axboe 	unsigned int i;
619b86ff981SJens Axboe 	struct rchan_buf *prev = NULL;
620b86ff981SJens Axboe 
621b86ff981SJens Axboe 	if (!chan)
622b86ff981SJens Axboe 		return;
623b86ff981SJens Axboe 
624b86ff981SJens Axboe 	for (i = 0; i < NR_CPUS; i++) {
625b86ff981SJens Axboe 		if (!chan->buf[i] || chan->buf[i] == prev)
626b86ff981SJens Axboe 			break;
627b86ff981SJens Axboe 		relay_close_buf(chan->buf[i]);
628b86ff981SJens Axboe 		prev = chan->buf[i];
629b86ff981SJens Axboe 	}
630b86ff981SJens Axboe 
631b86ff981SJens Axboe 	if (chan->last_toobig)
632b86ff981SJens Axboe 		printk(KERN_WARNING "relay: one or more items not logged "
633b86ff981SJens Axboe 		       "[item size (%Zd) > sub-buffer size (%Zd)]\n",
634b86ff981SJens Axboe 		       chan->last_toobig, chan->subbuf_size);
635b86ff981SJens Axboe 
636b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
637b86ff981SJens Axboe }
638b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close);
639b86ff981SJens Axboe 
640b86ff981SJens Axboe /**
641b86ff981SJens Axboe  *	relay_flush - close the channel
642b86ff981SJens Axboe  *	@chan: the channel
643b86ff981SJens Axboe  *
644b86ff981SJens Axboe  *	Flushes all channel buffers i.e. forces buffer switch.
645b86ff981SJens Axboe  */
646b86ff981SJens Axboe void relay_flush(struct rchan *chan)
647b86ff981SJens Axboe {
648b86ff981SJens Axboe 	unsigned int i;
649b86ff981SJens Axboe 	struct rchan_buf *prev = NULL;
650b86ff981SJens Axboe 
651b86ff981SJens Axboe 	if (!chan)
652b86ff981SJens Axboe 		return;
653b86ff981SJens Axboe 
654b86ff981SJens Axboe 	for (i = 0; i < NR_CPUS; i++) {
655b86ff981SJens Axboe 		if (!chan->buf[i] || chan->buf[i] == prev)
656b86ff981SJens Axboe 			break;
657b86ff981SJens Axboe 		relay_switch_subbuf(chan->buf[i], 0);
658b86ff981SJens Axboe 		prev = chan->buf[i];
659b86ff981SJens Axboe 	}
660b86ff981SJens Axboe }
661b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush);
662b86ff981SJens Axboe 
663b86ff981SJens Axboe /**
664b86ff981SJens Axboe  *	relay_file_open - open file op for relay files
665b86ff981SJens Axboe  *	@inode: the inode
666b86ff981SJens Axboe  *	@filp: the file
667b86ff981SJens Axboe  *
668b86ff981SJens Axboe  *	Increments the channel buffer refcount.
669b86ff981SJens Axboe  */
670b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp)
671b86ff981SJens Axboe {
672b86ff981SJens Axboe 	struct rchan_buf *buf = inode->u.generic_ip;
673b86ff981SJens Axboe 	kref_get(&buf->kref);
674b86ff981SJens Axboe 	filp->private_data = buf;
675b86ff981SJens Axboe 
676b86ff981SJens Axboe 	return 0;
677b86ff981SJens Axboe }
678b86ff981SJens Axboe 
679b86ff981SJens Axboe /**
680b86ff981SJens Axboe  *	relay_file_mmap - mmap file op for relay files
681b86ff981SJens Axboe  *	@filp: the file
682b86ff981SJens Axboe  *	@vma: the vma describing what to map
683b86ff981SJens Axboe  *
684b86ff981SJens Axboe  *	Calls upon relay_mmap_buf to map the file into user space.
685b86ff981SJens Axboe  */
686b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
687b86ff981SJens Axboe {
688b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
689b86ff981SJens Axboe 	return relay_mmap_buf(buf, vma);
690b86ff981SJens Axboe }
691b86ff981SJens Axboe 
692b86ff981SJens Axboe /**
693b86ff981SJens Axboe  *	relay_file_poll - poll file op for relay files
694b86ff981SJens Axboe  *	@filp: the file
695b86ff981SJens Axboe  *	@wait: poll table
696b86ff981SJens Axboe  *
697b86ff981SJens Axboe  *	Poll implemention.
698b86ff981SJens Axboe  */
699b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
700b86ff981SJens Axboe {
701b86ff981SJens Axboe 	unsigned int mask = 0;
702b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
703b86ff981SJens Axboe 
704b86ff981SJens Axboe 	if (buf->finalized)
705b86ff981SJens Axboe 		return POLLERR;
706b86ff981SJens Axboe 
707b86ff981SJens Axboe 	if (filp->f_mode & FMODE_READ) {
708b86ff981SJens Axboe 		poll_wait(filp, &buf->read_wait, wait);
709b86ff981SJens Axboe 		if (!relay_buf_empty(buf))
710b86ff981SJens Axboe 			mask |= POLLIN | POLLRDNORM;
711b86ff981SJens Axboe 	}
712b86ff981SJens Axboe 
713b86ff981SJens Axboe 	return mask;
714b86ff981SJens Axboe }
715b86ff981SJens Axboe 
716b86ff981SJens Axboe /**
717b86ff981SJens Axboe  *	relay_file_release - release file op for relay files
718b86ff981SJens Axboe  *	@inode: the inode
719b86ff981SJens Axboe  *	@filp: the file
720b86ff981SJens Axboe  *
721b86ff981SJens Axboe  *	Decrements the channel refcount, as the filesystem is
722b86ff981SJens Axboe  *	no longer using it.
723b86ff981SJens Axboe  */
724b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp)
725b86ff981SJens Axboe {
726b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
727b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
728b86ff981SJens Axboe 
729b86ff981SJens Axboe 	return 0;
730b86ff981SJens Axboe }
731b86ff981SJens Axboe 
732b86ff981SJens Axboe /**
733b86ff981SJens Axboe  *	relay_file_read_consume - update the consumed count for the buffer
734b86ff981SJens Axboe  */
735b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf,
736b86ff981SJens Axboe 				    size_t read_pos,
737b86ff981SJens Axboe 				    size_t bytes_consumed)
738b86ff981SJens Axboe {
739b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
740b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
741b86ff981SJens Axboe 	size_t read_subbuf;
742b86ff981SJens Axboe 
743b86ff981SJens Axboe 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
744b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
745b86ff981SJens Axboe 		buf->bytes_consumed = 0;
746b86ff981SJens Axboe 	}
747b86ff981SJens Axboe 
748b86ff981SJens Axboe 	buf->bytes_consumed += bytes_consumed;
749b86ff981SJens Axboe 	read_subbuf = read_pos / buf->chan->subbuf_size;
750b86ff981SJens Axboe 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
751b86ff981SJens Axboe 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
752b86ff981SJens Axboe 		    (buf->offset == subbuf_size))
753b86ff981SJens Axboe 			return;
754b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
755b86ff981SJens Axboe 		buf->bytes_consumed = 0;
756b86ff981SJens Axboe 	}
757b86ff981SJens Axboe }
758b86ff981SJens Axboe 
759b86ff981SJens Axboe /**
760b86ff981SJens Axboe  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
761b86ff981SJens Axboe  */
762b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
763b86ff981SJens Axboe {
764b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
765b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
766221415d7SJens Axboe 	size_t produced = buf->subbufs_produced;
767221415d7SJens Axboe 	size_t consumed = buf->subbufs_consumed;
768b86ff981SJens Axboe 
769b86ff981SJens Axboe 	relay_file_read_consume(buf, read_pos, 0);
770b86ff981SJens Axboe 
771221415d7SJens Axboe 	if (unlikely(buf->offset > subbuf_size)) {
772221415d7SJens Axboe 		if (produced == consumed)
773221415d7SJens Axboe 			return 0;
774221415d7SJens Axboe 		return 1;
775221415d7SJens Axboe 	}
776221415d7SJens Axboe 
777221415d7SJens Axboe 	if (unlikely(produced - consumed >= n_subbufs)) {
778221415d7SJens Axboe 		consumed = (produced / n_subbufs) * n_subbufs;
779221415d7SJens Axboe 		buf->subbufs_consumed = consumed;
780221415d7SJens Axboe 	}
781221415d7SJens Axboe 
782221415d7SJens Axboe 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
783221415d7SJens Axboe 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
784221415d7SJens Axboe 
785221415d7SJens Axboe 	if (consumed > produced)
786221415d7SJens Axboe 		produced += n_subbufs * subbuf_size;
787221415d7SJens Axboe 
788221415d7SJens Axboe 	if (consumed == produced)
789221415d7SJens Axboe 		return 0;
790221415d7SJens Axboe 
791b86ff981SJens Axboe 	return 1;
792b86ff981SJens Axboe }
793b86ff981SJens Axboe 
794b86ff981SJens Axboe /**
795b86ff981SJens Axboe  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
796b86ff981SJens Axboe  */
797b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos,
798b86ff981SJens Axboe 					   struct rchan_buf *buf)
799b86ff981SJens Axboe {
800b86ff981SJens Axboe 	size_t padding, avail = 0;
801b86ff981SJens Axboe 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
802b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
803b86ff981SJens Axboe 
804b86ff981SJens Axboe 	write_subbuf = (buf->data - buf->start) / subbuf_size;
805b86ff981SJens Axboe 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
806b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
807b86ff981SJens Axboe 	read_offset = read_pos % subbuf_size;
808b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
809b86ff981SJens Axboe 
810b86ff981SJens Axboe 	if (read_subbuf == write_subbuf) {
811b86ff981SJens Axboe 		if (read_offset + padding < write_offset)
812b86ff981SJens Axboe 			avail = write_offset - (read_offset + padding);
813b86ff981SJens Axboe 	} else
814b86ff981SJens Axboe 		avail = (subbuf_size - padding) - read_offset;
815b86ff981SJens Axboe 
816b86ff981SJens Axboe 	return avail;
817b86ff981SJens Axboe }
818b86ff981SJens Axboe 
819b86ff981SJens Axboe /**
820b86ff981SJens Axboe  *	relay_file_read_start_pos - find the first available byte to read
821b86ff981SJens Axboe  *
822b86ff981SJens Axboe  *	If the read_pos is in the middle of padding, return the
823b86ff981SJens Axboe  *	position of the first actually available byte, otherwise
824b86ff981SJens Axboe  *	return the original value.
825b86ff981SJens Axboe  */
826b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos,
827b86ff981SJens Axboe 					struct rchan_buf *buf)
828b86ff981SJens Axboe {
829b86ff981SJens Axboe 	size_t read_subbuf, padding, padding_start, padding_end;
830b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
831b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
832b86ff981SJens Axboe 
833b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
834b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
835b86ff981SJens Axboe 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
836b86ff981SJens Axboe 	padding_end = (read_subbuf + 1) * subbuf_size;
837b86ff981SJens Axboe 	if (read_pos >= padding_start && read_pos < padding_end) {
838b86ff981SJens Axboe 		read_subbuf = (read_subbuf + 1) % n_subbufs;
839b86ff981SJens Axboe 		read_pos = read_subbuf * subbuf_size;
840b86ff981SJens Axboe 	}
841b86ff981SJens Axboe 
842b86ff981SJens Axboe 	return read_pos;
843b86ff981SJens Axboe }
844b86ff981SJens Axboe 
845b86ff981SJens Axboe /**
846b86ff981SJens Axboe  *	relay_file_read_end_pos - return the new read position
847b86ff981SJens Axboe  */
848b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf,
849b86ff981SJens Axboe 				      size_t read_pos,
850b86ff981SJens Axboe 				      size_t count)
851b86ff981SJens Axboe {
852b86ff981SJens Axboe 	size_t read_subbuf, padding, end_pos;
853b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
854b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
855b86ff981SJens Axboe 
856b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
857b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
858b86ff981SJens Axboe 	if (read_pos % subbuf_size + count + padding == subbuf_size)
859b86ff981SJens Axboe 		end_pos = (read_subbuf + 1) * subbuf_size;
860b86ff981SJens Axboe 	else
861b86ff981SJens Axboe 		end_pos = read_pos + count;
862b86ff981SJens Axboe 	if (end_pos >= subbuf_size * n_subbufs)
863b86ff981SJens Axboe 		end_pos = 0;
864b86ff981SJens Axboe 
865b86ff981SJens Axboe 	return end_pos;
866b86ff981SJens Axboe }
867b86ff981SJens Axboe 
868b86ff981SJens Axboe /**
869*6dac40a7STom Zanussi  *	subbuf_read_actor - read up to one subbuf's worth of data
870b86ff981SJens Axboe  */
871*6dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start,
872*6dac40a7STom Zanussi 			     struct rchan_buf *buf,
873*6dac40a7STom Zanussi 			     size_t avail,
874*6dac40a7STom Zanussi 			     read_descriptor_t *desc,
875*6dac40a7STom Zanussi 			     read_actor_t actor)
876b86ff981SJens Axboe {
877b86ff981SJens Axboe 	void *from;
878*6dac40a7STom Zanussi 	int ret = 0;
879b86ff981SJens Axboe 
880b86ff981SJens Axboe 	from = buf->start + read_start;
881*6dac40a7STom Zanussi 	ret = avail;
882*6dac40a7STom Zanussi 	if (copy_to_user(desc->arg.data, from, avail)) {
883*6dac40a7STom Zanussi 		desc->error = -EFAULT;
884*6dac40a7STom Zanussi 		ret = 0;
885b86ff981SJens Axboe 	}
886*6dac40a7STom Zanussi 	desc->arg.data += ret;
887*6dac40a7STom Zanussi 	desc->written += ret;
888*6dac40a7STom Zanussi 	desc->count -= ret;
889*6dac40a7STom Zanussi 
890b86ff981SJens Axboe 	return ret;
891b86ff981SJens Axboe }
892b86ff981SJens Axboe 
893*6dac40a7STom Zanussi /**
894*6dac40a7STom Zanussi  *	subbuf_send_actor - send up to one subbuf's worth of data
895*6dac40a7STom Zanussi  */
896*6dac40a7STom Zanussi static int subbuf_send_actor(size_t read_start,
897*6dac40a7STom Zanussi 			     struct rchan_buf *buf,
898*6dac40a7STom Zanussi 			     size_t avail,
899*6dac40a7STom Zanussi 			     read_descriptor_t *desc,
900*6dac40a7STom Zanussi 			     read_actor_t actor)
901*6dac40a7STom Zanussi {
902*6dac40a7STom Zanussi 	unsigned long pidx, poff;
903*6dac40a7STom Zanussi 	unsigned int subbuf_pages;
904*6dac40a7STom Zanussi 	int ret = 0;
905*6dac40a7STom Zanussi 
906*6dac40a7STom Zanussi 	subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT;
907*6dac40a7STom Zanussi 	pidx = (read_start / PAGE_SIZE) % subbuf_pages;
908*6dac40a7STom Zanussi 	poff = read_start & ~PAGE_MASK;
909*6dac40a7STom Zanussi 	while (avail) {
910*6dac40a7STom Zanussi 		struct page *p = buf->page_array[pidx];
911*6dac40a7STom Zanussi 		unsigned int len;
912*6dac40a7STom Zanussi 
913*6dac40a7STom Zanussi 		len = PAGE_SIZE - poff;
914*6dac40a7STom Zanussi 		if (len > avail)
915*6dac40a7STom Zanussi 			len = avail;
916*6dac40a7STom Zanussi 
917*6dac40a7STom Zanussi 		len = actor(desc, p, poff, len);
918*6dac40a7STom Zanussi 		if (desc->error)
919*6dac40a7STom Zanussi 			break;
920*6dac40a7STom Zanussi 
921*6dac40a7STom Zanussi 		avail -= len;
922*6dac40a7STom Zanussi 		ret += len;
923*6dac40a7STom Zanussi 		poff = 0;
924*6dac40a7STom Zanussi 		pidx = (pidx + 1) % subbuf_pages;
925*6dac40a7STom Zanussi 	}
926*6dac40a7STom Zanussi 
927*6dac40a7STom Zanussi 	return ret;
928*6dac40a7STom Zanussi }
929*6dac40a7STom Zanussi 
930*6dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start,
931*6dac40a7STom Zanussi 			       struct rchan_buf *buf,
932*6dac40a7STom Zanussi 			       size_t avail,
933*6dac40a7STom Zanussi 			       read_descriptor_t *desc,
934*6dac40a7STom Zanussi 			       read_actor_t actor);
935*6dac40a7STom Zanussi 
936*6dac40a7STom Zanussi /**
937*6dac40a7STom Zanussi  *	relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
938*6dac40a7STom Zanussi  */
939*6dac40a7STom Zanussi static inline ssize_t relay_file_read_subbufs(struct file *filp,
940*6dac40a7STom Zanussi 					      loff_t *ppos,
941*6dac40a7STom Zanussi 					      size_t count,
942*6dac40a7STom Zanussi 					      subbuf_actor_t subbuf_actor,
943*6dac40a7STom Zanussi 					      read_actor_t actor,
944221415d7SJens Axboe 					      void *target)
945221415d7SJens Axboe {
946221415d7SJens Axboe 	struct rchan_buf *buf = filp->private_data;
947221415d7SJens Axboe 	size_t read_start, avail;
948*6dac40a7STom Zanussi 	read_descriptor_t desc;
949*6dac40a7STom Zanussi 	int ret;
950221415d7SJens Axboe 
951*6dac40a7STom Zanussi 	if (!count)
952221415d7SJens Axboe 		return 0;
953221415d7SJens Axboe 
954221415d7SJens Axboe 	desc.written = 0;
955221415d7SJens Axboe 	desc.count = count;
956221415d7SJens Axboe 	desc.arg.data = target;
957221415d7SJens Axboe 	desc.error = 0;
958221415d7SJens Axboe 
959*6dac40a7STom Zanussi 	mutex_lock(&filp->f_dentry->d_inode->i_mutex);
960*6dac40a7STom Zanussi 	do {
961*6dac40a7STom Zanussi 		if (!relay_file_read_avail(buf, *ppos))
962221415d7SJens Axboe 			break;
963221415d7SJens Axboe 
964*6dac40a7STom Zanussi 		read_start = relay_file_read_start_pos(*ppos, buf);
965*6dac40a7STom Zanussi 		avail = relay_file_read_subbuf_avail(read_start, buf);
966*6dac40a7STom Zanussi 		if (!avail)
967*6dac40a7STom Zanussi 			break;
968221415d7SJens Axboe 
969*6dac40a7STom Zanussi 		avail = min(desc.count, avail);
970*6dac40a7STom Zanussi 		ret = subbuf_actor(read_start, buf, avail, &desc, actor);
971*6dac40a7STom Zanussi 		if (desc.error < 0)
972*6dac40a7STom Zanussi 			break;
973*6dac40a7STom Zanussi 
974*6dac40a7STom Zanussi 		if (ret) {
975221415d7SJens Axboe 			relay_file_read_consume(buf, read_start, ret);
976221415d7SJens Axboe 			*ppos = relay_file_read_end_pos(buf, read_start, ret);
977221415d7SJens Axboe 		}
978*6dac40a7STom Zanussi 	} while (desc.count && ret);
979*6dac40a7STom Zanussi 	mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
980221415d7SJens Axboe 
981*6dac40a7STom Zanussi 	return desc.written;
982221415d7SJens Axboe }
983221415d7SJens Axboe 
984*6dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp,
985*6dac40a7STom Zanussi 			       char __user *buffer,
986*6dac40a7STom Zanussi 			       size_t count,
987*6dac40a7STom Zanussi 			       loff_t *ppos)
988*6dac40a7STom Zanussi {
989*6dac40a7STom Zanussi 	return relay_file_read_subbufs(filp, ppos, count, subbuf_read_actor,
990*6dac40a7STom Zanussi 				       NULL, buffer);
991*6dac40a7STom Zanussi }
992*6dac40a7STom Zanussi 
993*6dac40a7STom Zanussi static ssize_t relay_file_sendfile(struct file *filp,
994*6dac40a7STom Zanussi 				   loff_t *ppos,
995*6dac40a7STom Zanussi 				   size_t count,
996*6dac40a7STom Zanussi 				   read_actor_t actor,
997221415d7SJens Axboe 				   void *target)
998221415d7SJens Axboe {
999*6dac40a7STom Zanussi 	return relay_file_read_subbufs(filp, ppos, count, subbuf_send_actor,
1000*6dac40a7STom Zanussi 				       actor, target);
1001221415d7SJens Axboe }
1002221415d7SJens Axboe 
1003b86ff981SJens Axboe struct file_operations relay_file_operations = {
1004b86ff981SJens Axboe 	.open		= relay_file_open,
1005b86ff981SJens Axboe 	.poll		= relay_file_poll,
1006b86ff981SJens Axboe 	.mmap		= relay_file_mmap,
1007b86ff981SJens Axboe 	.read		= relay_file_read,
1008b86ff981SJens Axboe 	.llseek		= no_llseek,
1009b86ff981SJens Axboe 	.release	= relay_file_release,
1010221415d7SJens Axboe 	.sendfile       = relay_file_sendfile,
1011b86ff981SJens Axboe };
1012b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations);
1013