blob: 7c0373322f18892eea75e3773714554f59f46c1e [file] [log] [blame]
Jens Axboeb86ff9812006-03-23 19:56:55 +01001/*
2 * Public API and common code for kernel->userspace relay file support.
3 *
Jesper Juhlc9b3feb2007-07-31 00:39:18 -07004 * See Documentation/filesystems/relay.txt for an overview.
Jens Axboeb86ff9812006-03-23 19:56:55 +01005 *
6 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
8 *
9 * Moved to kernel/relay.c by Paul Mundt, 2006.
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080010 * November 2006 - CPU hotplug support by Mathieu Desnoyers
11 * (mathieu.desnoyers@polymtl.ca)
Jens Axboeb86ff9812006-03-23 19:56:55 +010012 *
13 * This file is released under the GPL.
14 */
15#include <linux/errno.h>
16#include <linux/stddef.h>
17#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/string.h>
20#include <linux/relay.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080023#include <linux/cpu.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020024#include <linux/splice.h>
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080025
26/* list of open channels, for cpu hotplug */
27static DEFINE_MUTEX(relay_channels_mutex);
28static LIST_HEAD(relay_channels);
Jens Axboeb86ff9812006-03-23 19:56:55 +010029
30/*
31 * close() vm_op implementation for relay file mapping.
32 */
33static void relay_file_mmap_close(struct vm_area_struct *vma)
34{
35 struct rchan_buf *buf = vma->vm_private_data;
36 buf->chan->cb->buf_unmapped(buf, vma->vm_file);
37}
38
39/*
40 * nopage() vm_op implementation for relay file mapping.
41 */
42static struct page *relay_buf_nopage(struct vm_area_struct *vma,
43 unsigned long address,
44 int *type)
45{
46 struct page *page;
47 struct rchan_buf *buf = vma->vm_private_data;
48 unsigned long offset = address - vma->vm_start;
49
50 if (address > vma->vm_end)
51 return NOPAGE_SIGBUS; /* Disallow mremap */
52 if (!buf)
53 return NOPAGE_OOM;
54
55 page = vmalloc_to_page(buf->start + offset);
56 if (!page)
57 return NOPAGE_OOM;
58 get_page(page);
59
60 if (type)
61 *type = VM_FAULT_MINOR;
62
63 return page;
64}
65
66/*
67 * vm_ops for relay file mappings.
68 */
69static struct vm_operations_struct relay_file_mmap_ops = {
70 .nopage = relay_buf_nopage,
71 .close = relay_file_mmap_close,
72};
73
74/**
75 * relay_mmap_buf: - mmap channel buffer to process address space
76 * @buf: relay channel buffer
77 * @vma: vm_area_struct describing memory to be mapped
78 *
79 * Returns 0 if ok, negative on error
80 *
81 * Caller should already have grabbed mmap_sem.
82 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -070083static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
Jens Axboeb86ff9812006-03-23 19:56:55 +010084{
85 unsigned long length = vma->vm_end - vma->vm_start;
86 struct file *filp = vma->vm_file;
87
88 if (!buf)
89 return -EBADF;
90
91 if (length != (unsigned long)buf->chan->alloc_size)
92 return -EINVAL;
93
94 vma->vm_ops = &relay_file_mmap_ops;
Nick Piggin2f987352008-02-02 03:08:53 +010095 vma->vm_flags |= VM_DONTEXPAND;
Jens Axboeb86ff9812006-03-23 19:56:55 +010096 vma->vm_private_data = buf;
97 buf->chan->cb->buf_mapped(buf, filp);
98
99 return 0;
100}
101
102/**
103 * relay_alloc_buf - allocate a channel buffer
104 * @buf: the buffer struct
105 * @size: total size of the buffer
106 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700107 * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
Jens Axboe221415d2006-03-23 19:57:55 +0100108 * passed in size will get page aligned, if it isn't already.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100109 */
Jens Axboe221415d2006-03-23 19:57:55 +0100110static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100111{
112 void *mem;
113 unsigned int i, j, n_pages;
114
Jens Axboe221415d2006-03-23 19:57:55 +0100115 *size = PAGE_ALIGN(*size);
116 n_pages = *size >> PAGE_SHIFT;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100117
118 buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
119 if (!buf->page_array)
120 return NULL;
121
122 for (i = 0; i < n_pages; i++) {
123 buf->page_array[i] = alloc_page(GFP_KERNEL);
124 if (unlikely(!buf->page_array[i]))
125 goto depopulate;
Tom Zanussiebf99092007-06-04 09:12:05 +0200126 set_page_private(buf->page_array[i], (unsigned long)buf);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100127 }
128 mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
129 if (!mem)
130 goto depopulate;
131
Jens Axboe221415d2006-03-23 19:57:55 +0100132 memset(mem, 0, *size);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100133 buf->page_count = n_pages;
134 return mem;
135
136depopulate:
137 for (j = 0; j < i; j++)
138 __free_page(buf->page_array[j]);
139 kfree(buf->page_array);
140 return NULL;
141}
142
143/**
144 * relay_create_buf - allocate and initialize a channel buffer
Randy Dunlap4c78a662006-09-29 01:59:10 -0700145 * @chan: the relay channel
Jens Axboeb86ff9812006-03-23 19:56:55 +0100146 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700147 * Returns channel buffer if successful, %NULL otherwise.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100148 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700149static struct rchan_buf *relay_create_buf(struct rchan *chan)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100150{
Robert P. J. Daycd861282006-12-13 00:34:52 -0800151 struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100152 if (!buf)
153 return NULL;
154
155 buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
156 if (!buf->padding)
157 goto free_buf;
158
Jens Axboe221415d2006-03-23 19:57:55 +0100159 buf->start = relay_alloc_buf(buf, &chan->alloc_size);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100160 if (!buf->start)
161 goto free_buf;
162
163 buf->chan = chan;
164 kref_get(&buf->chan->kref);
165 return buf;
166
167free_buf:
168 kfree(buf->padding);
169 kfree(buf);
170 return NULL;
171}
172
173/**
174 * relay_destroy_channel - free the channel struct
Randy Dunlap4c78a662006-09-29 01:59:10 -0700175 * @kref: target kernel reference that contains the relay channel
Jens Axboeb86ff9812006-03-23 19:56:55 +0100176 *
177 * Should only be called from kref_put().
178 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700179static void relay_destroy_channel(struct kref *kref)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100180{
181 struct rchan *chan = container_of(kref, struct rchan, kref);
182 kfree(chan);
183}
184
185/**
186 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
187 * @buf: the buffer struct
188 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700189static void relay_destroy_buf(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100190{
191 struct rchan *chan = buf->chan;
192 unsigned int i;
193
194 if (likely(buf->start)) {
195 vunmap(buf->start);
196 for (i = 0; i < buf->page_count; i++)
197 __free_page(buf->page_array[i]);
198 kfree(buf->page_array);
199 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800200 chan->buf[buf->cpu] = NULL;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100201 kfree(buf->padding);
202 kfree(buf);
203 kref_put(&chan->kref, relay_destroy_channel);
204}
205
206/**
207 * relay_remove_buf - remove a channel buffer
Randy Dunlap4c78a662006-09-29 01:59:10 -0700208 * @kref: target kernel reference that contains the relay buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100209 *
210 * Removes the file from the fileystem, which also frees the
211 * rchan_buf_struct and the channel buffer. Should only be called from
212 * kref_put().
213 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700214static void relay_remove_buf(struct kref *kref)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100215{
216 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
217 buf->chan->cb->remove_buf_file(buf->dentry);
218 relay_destroy_buf(buf);
219}
220
221/**
222 * relay_buf_empty - boolean, is the channel buffer empty?
223 * @buf: channel buffer
224 *
225 * Returns 1 if the buffer is empty, 0 otherwise.
226 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700227static int relay_buf_empty(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100228{
229 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
230}
Jens Axboeb86ff9812006-03-23 19:56:55 +0100231
232/**
233 * relay_buf_full - boolean, is the channel buffer full?
234 * @buf: channel buffer
235 *
236 * Returns 1 if the buffer is full, 0 otherwise.
237 */
238int relay_buf_full(struct rchan_buf *buf)
239{
240 size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
241 return (ready >= buf->chan->n_subbufs) ? 1 : 0;
242}
243EXPORT_SYMBOL_GPL(relay_buf_full);
244
245/*
246 * High-level relay kernel API and associated functions.
247 */
248
249/*
250 * rchan_callback implementations defining default channel behavior. Used
251 * in place of corresponding NULL values in client callback struct.
252 */
253
254/*
255 * subbuf_start() default callback. Does nothing.
256 */
257static int subbuf_start_default_callback (struct rchan_buf *buf,
258 void *subbuf,
259 void *prev_subbuf,
260 size_t prev_padding)
261{
262 if (relay_buf_full(buf))
263 return 0;
264
265 return 1;
266}
267
268/*
269 * buf_mapped() default callback. Does nothing.
270 */
271static void buf_mapped_default_callback(struct rchan_buf *buf,
272 struct file *filp)
273{
274}
275
276/*
277 * buf_unmapped() default callback. Does nothing.
278 */
279static void buf_unmapped_default_callback(struct rchan_buf *buf,
280 struct file *filp)
281{
282}
283
284/*
285 * create_buf_file_create() default callback. Does nothing.
286 */
287static struct dentry *create_buf_file_default_callback(const char *filename,
288 struct dentry *parent,
289 int mode,
290 struct rchan_buf *buf,
291 int *is_global)
292{
293 return NULL;
294}
295
296/*
297 * remove_buf_file() default callback. Does nothing.
298 */
299static int remove_buf_file_default_callback(struct dentry *dentry)
300{
301 return -EINVAL;
302}
303
304/* relay channel default callbacks */
305static struct rchan_callbacks default_channel_callbacks = {
306 .subbuf_start = subbuf_start_default_callback,
307 .buf_mapped = buf_mapped_default_callback,
308 .buf_unmapped = buf_unmapped_default_callback,
309 .create_buf_file = create_buf_file_default_callback,
310 .remove_buf_file = remove_buf_file_default_callback,
311};
312
313/**
314 * wakeup_readers - wake up readers waiting on a channel
Linus Torvalds9a9136e22007-05-09 12:54:17 -0700315 * @data: contains the channel buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100316 *
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700317 * This is the timer function used to defer reader waking.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100318 */
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700319static void wakeup_readers(unsigned long data)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100320{
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700321 struct rchan_buf *buf = (struct rchan_buf *)data;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100322 wake_up_interruptible(&buf->read_wait);
323}
324
325/**
326 * __relay_reset - reset a channel buffer
327 * @buf: the channel buffer
328 * @init: 1 if this is a first-time initialization
329 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800330 * See relay_reset() for description of effect.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100331 */
Andrew Morton192636a2006-12-22 01:11:30 -0800332static void __relay_reset(struct rchan_buf *buf, unsigned int init)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100333{
334 size_t i;
335
336 if (init) {
337 init_waitqueue_head(&buf->read_wait);
338 kref_init(&buf->kref);
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700339 setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
340 } else
341 del_timer_sync(&buf->timer);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100342
343 buf->subbufs_produced = 0;
344 buf->subbufs_consumed = 0;
345 buf->bytes_consumed = 0;
346 buf->finalized = 0;
347 buf->data = buf->start;
348 buf->offset = 0;
349
350 for (i = 0; i < buf->chan->n_subbufs; i++)
351 buf->padding[i] = 0;
352
353 buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
354}
355
356/**
357 * relay_reset - reset the channel
358 * @chan: the channel
359 *
360 * This has the effect of erasing all data from all channel buffers
361 * and restarting the channel in its initial state. The buffers
362 * are not freed, so any mappings are still in effect.
363 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800364 * NOTE. Care should be taken that the channel isn't actually
Jens Axboeb86ff9812006-03-23 19:56:55 +0100365 * being used by anything when this call is made.
366 */
367void relay_reset(struct rchan *chan)
368{
369 unsigned int i;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100370
371 if (!chan)
372 return;
373
Daniel Walker1bfbc602007-10-18 03:06:05 -0700374 if (chan->is_global && chan->buf[0]) {
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800375 __relay_reset(chan->buf[0], 0);
376 return;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100377 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800378
379 mutex_lock(&relay_channels_mutex);
380 for_each_online_cpu(i)
381 if (chan->buf[i])
382 __relay_reset(chan->buf[i], 0);
383 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100384}
385EXPORT_SYMBOL_GPL(relay_reset);
386
Randy Dunlap4c78a662006-09-29 01:59:10 -0700387/*
Jens Axboeb86ff9812006-03-23 19:56:55 +0100388 * relay_open_buf - create a new relay channel buffer
389 *
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800390 * used by relay_open() and CPU hotplug.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100391 */
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800392static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100393{
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800394 struct rchan_buf *buf = NULL;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100395 struct dentry *dentry;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800396 char *tmpname;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100397
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800398 if (chan->is_global)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100399 return chan->buf[0];
400
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800401 tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
402 if (!tmpname)
403 goto end;
404 snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
405
Jens Axboeb86ff9812006-03-23 19:56:55 +0100406 buf = relay_create_buf(chan);
407 if (!buf)
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800408 goto free_name;
409
410 buf->cpu = cpu;
411 __relay_reset(buf, 1);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100412
413 /* Create file in fs */
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800414 dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
415 buf, &chan->is_global);
416 if (!dentry)
417 goto free_buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100418
419 buf->dentry = dentry;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100420
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800421 if(chan->is_global) {
422 chan->buf[0] = buf;
423 buf->cpu = 0;
424 }
425
426 goto free_name;
427
428free_buf:
429 relay_destroy_buf(buf);
Jesper Juhlc9b3feb2007-07-31 00:39:18 -0700430 buf = NULL;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800431free_name:
432 kfree(tmpname);
433end:
Jens Axboeb86ff9812006-03-23 19:56:55 +0100434 return buf;
435}
436
437/**
438 * relay_close_buf - close a channel buffer
439 * @buf: channel buffer
440 *
441 * Marks the buffer finalized and restores the default callbacks.
442 * The channel buffer and channel buffer data structure are then freed
443 * automatically when the last reference is given up.
444 */
Andrew Morton192636a2006-12-22 01:11:30 -0800445static void relay_close_buf(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100446{
447 buf->finalized = 1;
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700448 del_timer_sync(&buf->timer);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100449 kref_put(&buf->kref, relay_remove_buf);
450}
451
Andrew Morton192636a2006-12-22 01:11:30 -0800452static void setup_callbacks(struct rchan *chan,
Jens Axboeb86ff9812006-03-23 19:56:55 +0100453 struct rchan_callbacks *cb)
454{
455 if (!cb) {
456 chan->cb = &default_channel_callbacks;
457 return;
458 }
459
460 if (!cb->subbuf_start)
461 cb->subbuf_start = subbuf_start_default_callback;
462 if (!cb->buf_mapped)
463 cb->buf_mapped = buf_mapped_default_callback;
464 if (!cb->buf_unmapped)
465 cb->buf_unmapped = buf_unmapped_default_callback;
466 if (!cb->create_buf_file)
467 cb->create_buf_file = create_buf_file_default_callback;
468 if (!cb->remove_buf_file)
469 cb->remove_buf_file = remove_buf_file_default_callback;
470 chan->cb = cb;
471}
472
473/**
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800474 * relay_hotcpu_callback - CPU hotplug callback
475 * @nb: notifier block
476 * @action: hotplug action to take
477 * @hcpu: CPU number
478 *
Randy Dunlap05fb6bf2007-02-28 20:12:13 -0800479 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800480 */
481static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
482 unsigned long action,
483 void *hcpu)
484{
485 unsigned int hotcpu = (unsigned long)hcpu;
486 struct rchan *chan;
487
488 switch(action) {
489 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700490 case CPU_UP_PREPARE_FROZEN:
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800491 mutex_lock(&relay_channels_mutex);
492 list_for_each_entry(chan, &relay_channels, list) {
493 if (chan->buf[hotcpu])
494 continue;
495 chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
496 if(!chan->buf[hotcpu]) {
497 printk(KERN_ERR
498 "relay_hotcpu_callback: cpu %d buffer "
499 "creation failed\n", hotcpu);
500 mutex_unlock(&relay_channels_mutex);
501 return NOTIFY_BAD;
502 }
503 }
504 mutex_unlock(&relay_channels_mutex);
505 break;
506 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700507 case CPU_DEAD_FROZEN:
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800508 /* No need to flush the cpu : will be flushed upon
509 * final relay_flush() call. */
510 break;
511 }
512 return NOTIFY_OK;
513}
514
515/**
Jens Axboeb86ff9812006-03-23 19:56:55 +0100516 * relay_open - create a new relay channel
517 * @base_filename: base name of files to create
Randy Dunlap4c78a662006-09-29 01:59:10 -0700518 * @parent: dentry of parent directory, %NULL for root directory
Jens Axboeb86ff9812006-03-23 19:56:55 +0100519 * @subbuf_size: size of sub-buffers
520 * @n_subbufs: number of sub-buffers
521 * @cb: client callback functions
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800522 * @private_data: user-defined data
Jens Axboeb86ff9812006-03-23 19:56:55 +0100523 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700524 * Returns channel pointer if successful, %NULL otherwise.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100525 *
526 * Creates a channel buffer for each cpu using the sizes and
527 * attributes specified. The created channel buffer files
528 * will be named base_filename0...base_filenameN-1. File
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800529 * permissions will be %S_IRUSR.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100530 */
531struct rchan *relay_open(const char *base_filename,
532 struct dentry *parent,
533 size_t subbuf_size,
534 size_t n_subbufs,
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800535 struct rchan_callbacks *cb,
536 void *private_data)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100537{
538 unsigned int i;
539 struct rchan *chan;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100540 if (!base_filename)
541 return NULL;
542
543 if (!(subbuf_size && n_subbufs))
544 return NULL;
545
Robert P. J. Daycd861282006-12-13 00:34:52 -0800546 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100547 if (!chan)
548 return NULL;
549
550 chan->version = RELAYFS_CHANNEL_VERSION;
551 chan->n_subbufs = n_subbufs;
552 chan->subbuf_size = subbuf_size;
553 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800554 chan->parent = parent;
555 chan->private_data = private_data;
556 strlcpy(chan->base_filename, base_filename, NAME_MAX);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100557 setup_callbacks(chan, cb);
558 kref_init(&chan->kref);
559
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800560 mutex_lock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100561 for_each_online_cpu(i) {
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800562 chan->buf[i] = relay_open_buf(chan, i);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100563 if (!chan->buf[i])
564 goto free_bufs;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100565 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800566 list_add(&chan->list, &relay_channels);
567 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100568
Jens Axboeb86ff9812006-03-23 19:56:55 +0100569 return chan;
570
571free_bufs:
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800572 for_each_online_cpu(i) {
Jens Axboeb86ff9812006-03-23 19:56:55 +0100573 if (!chan->buf[i])
574 break;
575 relay_close_buf(chan->buf[i]);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100576 }
Jens Axboeb86ff9812006-03-23 19:56:55 +0100577
Jens Axboeb86ff9812006-03-23 19:56:55 +0100578 kref_put(&chan->kref, relay_destroy_channel);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800579 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100580 return NULL;
581}
582EXPORT_SYMBOL_GPL(relay_open);
583
584/**
585 * relay_switch_subbuf - switch to a new sub-buffer
586 * @buf: channel buffer
587 * @length: size of current event
588 *
589 * Returns either the length passed in or 0 if full.
590 *
591 * Performs sub-buffer-switch tasks such as invoking callbacks,
592 * updating padding counts, waking up readers, etc.
593 */
594size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
595{
596 void *old, *new;
597 size_t old_subbuf, new_subbuf;
598
599 if (unlikely(length > buf->chan->subbuf_size))
600 goto toobig;
601
602 if (buf->offset != buf->chan->subbuf_size + 1) {
603 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
604 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
605 buf->padding[old_subbuf] = buf->prev_padding;
606 buf->subbufs_produced++;
Jens Axboe221415d2006-03-23 19:57:55 +0100607 buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
608 buf->padding[old_subbuf];
609 smp_mb();
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700610 if (waitqueue_active(&buf->read_wait))
611 /*
612 * Calling wake_up_interruptible() from here
613 * will deadlock if we happen to be logging
614 * from the scheduler (trying to re-grab
615 * rq->lock), so defer it.
616 */
617 __mod_timer(&buf->timer, jiffies + 1);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100618 }
619
620 old = buf->data;
621 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
622 new = buf->start + new_subbuf * buf->chan->subbuf_size;
623 buf->offset = 0;
624 if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
625 buf->offset = buf->chan->subbuf_size + 1;
626 return 0;
627 }
628 buf->data = new;
629 buf->padding[new_subbuf] = 0;
630
631 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
632 goto toobig;
633
634 return length;
635
636toobig:
637 buf->chan->last_toobig = length;
638 return 0;
639}
640EXPORT_SYMBOL_GPL(relay_switch_subbuf);
641
642/**
643 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
644 * @chan: the channel
645 * @cpu: the cpu associated with the channel buffer to update
646 * @subbufs_consumed: number of sub-buffers to add to current buf's count
647 *
648 * Adds to the channel buffer's consumed sub-buffer count.
649 * subbufs_consumed should be the number of sub-buffers newly consumed,
650 * not the total consumed.
651 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800652 * NOTE. Kernel clients don't need to call this function if the channel
Jens Axboeb86ff9812006-03-23 19:56:55 +0100653 * mode is 'overwrite'.
654 */
655void relay_subbufs_consumed(struct rchan *chan,
656 unsigned int cpu,
657 size_t subbufs_consumed)
658{
659 struct rchan_buf *buf;
660
661 if (!chan)
662 return;
663
664 if (cpu >= NR_CPUS || !chan->buf[cpu])
665 return;
666
667 buf = chan->buf[cpu];
668 buf->subbufs_consumed += subbufs_consumed;
669 if (buf->subbufs_consumed > buf->subbufs_produced)
670 buf->subbufs_consumed = buf->subbufs_produced;
671}
672EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
673
674/**
675 * relay_close - close the channel
676 * @chan: the channel
677 *
678 * Closes all channel buffers and frees the channel.
679 */
680void relay_close(struct rchan *chan)
681{
682 unsigned int i;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100683
684 if (!chan)
685 return;
686
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800687 mutex_lock(&relay_channels_mutex);
688 if (chan->is_global && chan->buf[0])
689 relay_close_buf(chan->buf[0]);
690 else
691 for_each_possible_cpu(i)
692 if (chan->buf[i])
693 relay_close_buf(chan->buf[i]);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100694
695 if (chan->last_toobig)
696 printk(KERN_WARNING "relay: one or more items not logged "
697 "[item size (%Zd) > sub-buffer size (%Zd)]\n",
698 chan->last_toobig, chan->subbuf_size);
699
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800700 list_del(&chan->list);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100701 kref_put(&chan->kref, relay_destroy_channel);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800702 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100703}
704EXPORT_SYMBOL_GPL(relay_close);
705
706/**
707 * relay_flush - close the channel
708 * @chan: the channel
709 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700710 * Flushes all channel buffers, i.e. forces buffer switch.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100711 */
712void relay_flush(struct rchan *chan)
713{
714 unsigned int i;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100715
716 if (!chan)
717 return;
718
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800719 if (chan->is_global && chan->buf[0]) {
720 relay_switch_subbuf(chan->buf[0], 0);
721 return;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100722 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800723
724 mutex_lock(&relay_channels_mutex);
725 for_each_possible_cpu(i)
726 if (chan->buf[i])
727 relay_switch_subbuf(chan->buf[i], 0);
728 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100729}
730EXPORT_SYMBOL_GPL(relay_flush);
731
732/**
733 * relay_file_open - open file op for relay files
734 * @inode: the inode
735 * @filp: the file
736 *
737 * Increments the channel buffer refcount.
738 */
739static int relay_file_open(struct inode *inode, struct file *filp)
740{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700741 struct rchan_buf *buf = inode->i_private;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100742 kref_get(&buf->kref);
743 filp->private_data = buf;
744
745 return 0;
746}
747
748/**
749 * relay_file_mmap - mmap file op for relay files
750 * @filp: the file
751 * @vma: the vma describing what to map
752 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800753 * Calls upon relay_mmap_buf() to map the file into user space.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100754 */
755static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
756{
757 struct rchan_buf *buf = filp->private_data;
758 return relay_mmap_buf(buf, vma);
759}
760
761/**
762 * relay_file_poll - poll file op for relay files
763 * @filp: the file
764 * @wait: poll table
765 *
766 * Poll implemention.
767 */
768static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
769{
770 unsigned int mask = 0;
771 struct rchan_buf *buf = filp->private_data;
772
773 if (buf->finalized)
774 return POLLERR;
775
776 if (filp->f_mode & FMODE_READ) {
777 poll_wait(filp, &buf->read_wait, wait);
778 if (!relay_buf_empty(buf))
779 mask |= POLLIN | POLLRDNORM;
780 }
781
782 return mask;
783}
784
785/**
786 * relay_file_release - release file op for relay files
787 * @inode: the inode
788 * @filp: the file
789 *
790 * Decrements the channel refcount, as the filesystem is
791 * no longer using it.
792 */
793static int relay_file_release(struct inode *inode, struct file *filp)
794{
795 struct rchan_buf *buf = filp->private_data;
796 kref_put(&buf->kref, relay_remove_buf);
797
798 return 0;
799}
800
Randy Dunlap4c78a662006-09-29 01:59:10 -0700801/*
Jens Axboeb86ff9812006-03-23 19:56:55 +0100802 * relay_file_read_consume - update the consumed count for the buffer
803 */
804static void relay_file_read_consume(struct rchan_buf *buf,
805 size_t read_pos,
806 size_t bytes_consumed)
807{
808 size_t subbuf_size = buf->chan->subbuf_size;
809 size_t n_subbufs = buf->chan->n_subbufs;
810 size_t read_subbuf;
811
812 if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
813 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
814 buf->bytes_consumed = 0;
815 }
816
817 buf->bytes_consumed += bytes_consumed;
Masami Hiramatsua66e3562007-06-27 14:10:04 -0700818 if (!read_pos)
819 read_subbuf = buf->subbufs_consumed % n_subbufs;
820 else
821 read_subbuf = read_pos / buf->chan->subbuf_size;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100822 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
823 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
824 (buf->offset == subbuf_size))
825 return;
826 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
827 buf->bytes_consumed = 0;
828 }
829}
830
Randy Dunlap4c78a662006-09-29 01:59:10 -0700831/*
Jens Axboeb86ff9812006-03-23 19:56:55 +0100832 * relay_file_read_avail - boolean, are there unconsumed bytes available?
833 */
834static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
835{
Jens Axboeb86ff9812006-03-23 19:56:55 +0100836 size_t subbuf_size = buf->chan->subbuf_size;
837 size_t n_subbufs = buf->chan->n_subbufs;
Jens Axboe221415d2006-03-23 19:57:55 +0100838 size_t produced = buf->subbufs_produced;
839 size_t consumed = buf->subbufs_consumed;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100840
841 relay_file_read_consume(buf, read_pos, 0);
842
Jens Axboe221415d2006-03-23 19:57:55 +0100843 if (unlikely(buf->offset > subbuf_size)) {
844 if (produced == consumed)
845 return 0;
846 return 1;
847 }
848
849 if (unlikely(produced - consumed >= n_subbufs)) {
Masami Hiramatsua66e3562007-06-27 14:10:04 -0700850 consumed = produced - n_subbufs + 1;
Jens Axboe221415d2006-03-23 19:57:55 +0100851 buf->subbufs_consumed = consumed;
Masami Hiramatsua66e3562007-06-27 14:10:04 -0700852 buf->bytes_consumed = 0;
Jens Axboe221415d2006-03-23 19:57:55 +0100853 }
Daniel Walker1bfbc602007-10-18 03:06:05 -0700854
Jens Axboe221415d2006-03-23 19:57:55 +0100855 produced = (produced % n_subbufs) * subbuf_size + buf->offset;
856 consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
857
858 if (consumed > produced)
859 produced += n_subbufs * subbuf_size;
Daniel Walker1bfbc602007-10-18 03:06:05 -0700860
Jens Axboe221415d2006-03-23 19:57:55 +0100861 if (consumed == produced)
862 return 0;
863
Jens Axboeb86ff9812006-03-23 19:56:55 +0100864 return 1;
865}
866
867/**
868 * relay_file_read_subbuf_avail - return bytes available in sub-buffer
Randy Dunlap4c78a662006-09-29 01:59:10 -0700869 * @read_pos: file read position
870 * @buf: relay channel buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100871 */
872static size_t relay_file_read_subbuf_avail(size_t read_pos,
873 struct rchan_buf *buf)
874{
875 size_t padding, avail = 0;
876 size_t read_subbuf, read_offset, write_subbuf, write_offset;
877 size_t subbuf_size = buf->chan->subbuf_size;
878
879 write_subbuf = (buf->data - buf->start) / subbuf_size;
880 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
881 read_subbuf = read_pos / subbuf_size;
882 read_offset = read_pos % subbuf_size;
883 padding = buf->padding[read_subbuf];
884
885 if (read_subbuf == write_subbuf) {
886 if (read_offset + padding < write_offset)
887 avail = write_offset - (read_offset + padding);
888 } else
889 avail = (subbuf_size - padding) - read_offset;
890
891 return avail;
892}
893
894/**
895 * relay_file_read_start_pos - find the first available byte to read
Randy Dunlap4c78a662006-09-29 01:59:10 -0700896 * @read_pos: file read position
897 * @buf: relay channel buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100898 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800899 * If the @read_pos is in the middle of padding, return the
Jens Axboeb86ff9812006-03-23 19:56:55 +0100900 * position of the first actually available byte, otherwise
901 * return the original value.
902 */
903static size_t relay_file_read_start_pos(size_t read_pos,
904 struct rchan_buf *buf)
905{
906 size_t read_subbuf, padding, padding_start, padding_end;
907 size_t subbuf_size = buf->chan->subbuf_size;
908 size_t n_subbufs = buf->chan->n_subbufs;
David Wilder8d62fde2007-06-27 14:10:03 -0700909 size_t consumed = buf->subbufs_consumed % n_subbufs;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100910
David Wilder8d62fde2007-06-27 14:10:03 -0700911 if (!read_pos)
912 read_pos = consumed * subbuf_size + buf->bytes_consumed;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100913 read_subbuf = read_pos / subbuf_size;
914 padding = buf->padding[read_subbuf];
915 padding_start = (read_subbuf + 1) * subbuf_size - padding;
916 padding_end = (read_subbuf + 1) * subbuf_size;
917 if (read_pos >= padding_start && read_pos < padding_end) {
918 read_subbuf = (read_subbuf + 1) % n_subbufs;
919 read_pos = read_subbuf * subbuf_size;
920 }
921
922 return read_pos;
923}
924
925/**
926 * relay_file_read_end_pos - return the new read position
Randy Dunlap4c78a662006-09-29 01:59:10 -0700927 * @read_pos: file read position
928 * @buf: relay channel buffer
929 * @count: number of bytes to be read
Jens Axboeb86ff9812006-03-23 19:56:55 +0100930 */
931static size_t relay_file_read_end_pos(struct rchan_buf *buf,
932 size_t read_pos,
933 size_t count)
934{
935 size_t read_subbuf, padding, end_pos;
936 size_t subbuf_size = buf->chan->subbuf_size;
937 size_t n_subbufs = buf->chan->n_subbufs;
938
939 read_subbuf = read_pos / subbuf_size;
940 padding = buf->padding[read_subbuf];
941 if (read_pos % subbuf_size + count + padding == subbuf_size)
942 end_pos = (read_subbuf + 1) * subbuf_size;
943 else
944 end_pos = read_pos + count;
945 if (end_pos >= subbuf_size * n_subbufs)
946 end_pos = 0;
947
948 return end_pos;
949}
950
Randy Dunlap4c78a662006-09-29 01:59:10 -0700951/*
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100952 * subbuf_read_actor - read up to one subbuf's worth of data
Jens Axboeb86ff9812006-03-23 19:56:55 +0100953 */
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100954static int subbuf_read_actor(size_t read_start,
955 struct rchan_buf *buf,
956 size_t avail,
957 read_descriptor_t *desc,
958 read_actor_t actor)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100959{
Jens Axboeb86ff9812006-03-23 19:56:55 +0100960 void *from;
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100961 int ret = 0;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100962
963 from = buf->start + read_start;
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100964 ret = avail;
Al Viroba2397e2006-10-10 22:46:57 +0100965 if (copy_to_user(desc->arg.buf, from, avail)) {
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100966 desc->error = -EFAULT;
967 ret = 0;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100968 }
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100969 desc->arg.data += ret;
970 desc->written += ret;
971 desc->count -= ret;
972
Jens Axboeb86ff9812006-03-23 19:56:55 +0100973 return ret;
974}
975
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100976typedef int (*subbuf_actor_t) (size_t read_start,
977 struct rchan_buf *buf,
978 size_t avail,
979 read_descriptor_t *desc,
980 read_actor_t actor);
981
Randy Dunlap4c78a662006-09-29 01:59:10 -0700982/*
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100983 * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
984 */
Andrew Morton192636a2006-12-22 01:11:30 -0800985static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
986 subbuf_actor_t subbuf_actor,
987 read_actor_t actor,
988 read_descriptor_t *desc)
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100989{
990 struct rchan_buf *buf = filp->private_data;
991 size_t read_start, avail;
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100992 int ret;
993
Al Viroba2397e2006-10-10 22:46:57 +0100994 if (!desc->count)
Jens Axboe221415d2006-03-23 19:57:55 +0100995 return 0;
996
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800997 mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
Tom Zanussi6dac40a2006-03-23 19:58:45 +0100998 do {
999 if (!relay_file_read_avail(buf, *ppos))
Jens Axboe221415d2006-03-23 19:57:55 +01001000 break;
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001001
1002 read_start = relay_file_read_start_pos(*ppos, buf);
1003 avail = relay_file_read_subbuf_avail(read_start, buf);
1004 if (!avail)
1005 break;
1006
Al Viroba2397e2006-10-10 22:46:57 +01001007 avail = min(desc->count, avail);
1008 ret = subbuf_actor(read_start, buf, avail, desc, actor);
1009 if (desc->error < 0)
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001010 break;
1011
1012 if (ret) {
1013 relay_file_read_consume(buf, read_start, ret);
1014 *ppos = relay_file_read_end_pos(buf, read_start, ret);
Jens Axboe221415d2006-03-23 19:57:55 +01001015 }
Al Viroba2397e2006-10-10 22:46:57 +01001016 } while (desc->count && ret);
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08001017 mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
Jens Axboe221415d2006-03-23 19:57:55 +01001018
Al Viroba2397e2006-10-10 22:46:57 +01001019 return desc->written;
Jens Axboe221415d2006-03-23 19:57:55 +01001020}
1021
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001022static ssize_t relay_file_read(struct file *filp,
1023 char __user *buffer,
1024 size_t count,
1025 loff_t *ppos)
1026{
Al Viroba2397e2006-10-10 22:46:57 +01001027 read_descriptor_t desc;
1028 desc.written = 0;
1029 desc.count = count;
1030 desc.arg.buf = buffer;
1031 desc.error = 0;
1032 return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
1033 NULL, &desc);
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001034}
1035
Jens Axboe1db60cf2007-06-12 08:43:46 +02001036static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
1037{
1038 rbuf->bytes_consumed += bytes_consumed;
1039
1040 if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
1041 relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1042 rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
1043 }
1044}
1045
Tom Zanussiebf99092007-06-04 09:12:05 +02001046static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1047 struct pipe_buffer *buf)
Jens Axboe221415d2006-03-23 19:57:55 +01001048{
Tom Zanussiebf99092007-06-04 09:12:05 +02001049 struct rchan_buf *rbuf;
1050
1051 rbuf = (struct rchan_buf *)page_private(buf->page);
Jens Axboe1db60cf2007-06-12 08:43:46 +02001052 relay_consume_bytes(rbuf, buf->private);
Tom Zanussiebf99092007-06-04 09:12:05 +02001053}
1054
1055static struct pipe_buf_operations relay_pipe_buf_ops = {
1056 .can_merge = 0,
1057 .map = generic_pipe_buf_map,
1058 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb02007-06-14 13:10:48 +02001059 .confirm = generic_pipe_buf_confirm,
Tom Zanussiebf99092007-06-04 09:12:05 +02001060 .release = relay_pipe_buf_release,
1061 .steal = generic_pipe_buf_steal,
1062 .get = generic_pipe_buf_get,
1063};
1064
Tom Zanussid3f35d982007-07-12 08:12:05 +02001065/*
Tom Zanussiebf99092007-06-04 09:12:05 +02001066 * subbuf_splice_actor - splice up to one subbuf's worth of data
1067 */
1068static int subbuf_splice_actor(struct file *in,
1069 loff_t *ppos,
1070 struct pipe_inode_info *pipe,
1071 size_t len,
1072 unsigned int flags,
1073 int *nonpad_ret)
1074{
Jens Axboe1db60cf2007-06-12 08:43:46 +02001075 unsigned int pidx, poff, total_len, subbuf_pages, ret;
Tom Zanussiebf99092007-06-04 09:12:05 +02001076 struct rchan_buf *rbuf = in->private_data;
1077 unsigned int subbuf_size = rbuf->chan->subbuf_size;
Tom Zanussi24da24d2007-07-12 08:12:04 +02001078 uint64_t pos = (uint64_t) *ppos;
1079 uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
1080 size_t read_start = (size_t) do_div(pos, alloc_size);
Tom Zanussiebf99092007-06-04 09:12:05 +02001081 size_t read_subbuf = read_start / subbuf_size;
1082 size_t padding = rbuf->padding[read_subbuf];
1083 size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
Jens Axboe1db60cf2007-06-12 08:43:46 +02001084 struct page *pages[PIPE_BUFFERS];
1085 struct partial_page partial[PIPE_BUFFERS];
1086 struct splice_pipe_desc spd = {
1087 .pages = pages,
1088 .nr_pages = 0,
1089 .partial = partial,
1090 .flags = flags,
1091 .ops = &relay_pipe_buf_ops,
1092 };
Tom Zanussiebf99092007-06-04 09:12:05 +02001093
1094 if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1095 return 0;
1096
Jens Axboe1db60cf2007-06-12 08:43:46 +02001097 /*
1098 * Adjust read len, if longer than what is available
1099 */
1100 if (len > (subbuf_size - read_start % subbuf_size))
1101 len = subbuf_size - read_start % subbuf_size;
Tom Zanussiebf99092007-06-04 09:12:05 +02001102
1103 subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1104 pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1105 poff = read_start & ~PAGE_MASK;
1106
Jens Axboe1db60cf2007-06-12 08:43:46 +02001107 for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) {
1108 unsigned int this_len, this_end, private;
1109 unsigned int cur_pos = read_start + total_len;
Tom Zanussiebf99092007-06-04 09:12:05 +02001110
Jens Axboe1db60cf2007-06-12 08:43:46 +02001111 if (!len)
1112 break;
1113
1114 this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
1115 private = this_len;
1116
1117 spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
1118 spd.partial[spd.nr_pages].offset = poff;
1119
1120 this_end = cur_pos + this_len;
1121 if (this_end >= nonpad_end) {
1122 this_len = nonpad_end - cur_pos;
1123 private = this_len + padding;
1124 }
1125 spd.partial[spd.nr_pages].len = this_len;
1126 spd.partial[spd.nr_pages].private = private;
1127
1128 len -= this_len;
1129 total_len += this_len;
1130 poff = 0;
1131 pidx = (pidx + 1) % subbuf_pages;
1132
1133 if (this_end >= nonpad_end) {
1134 spd.nr_pages++;
Tom Zanussiebf99092007-06-04 09:12:05 +02001135 break;
1136 }
Tom Zanussiebf99092007-06-04 09:12:05 +02001137 }
1138
Jens Axboe1db60cf2007-06-12 08:43:46 +02001139 if (!spd.nr_pages)
1140 return 0;
Tom Zanussiebf99092007-06-04 09:12:05 +02001141
Jens Axboe1db60cf2007-06-12 08:43:46 +02001142 ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
1143 if (ret < 0 || ret < total_len)
1144 return ret;
Tom Zanussiebf99092007-06-04 09:12:05 +02001145
Jens Axboe1db60cf2007-06-12 08:43:46 +02001146 if (read_start + ret == nonpad_end)
1147 ret += padding;
1148
1149 return ret;
Tom Zanussiebf99092007-06-04 09:12:05 +02001150}
1151
1152static ssize_t relay_file_splice_read(struct file *in,
1153 loff_t *ppos,
1154 struct pipe_inode_info *pipe,
1155 size_t len,
1156 unsigned int flags)
1157{
1158 ssize_t spliced;
1159 int ret;
1160 int nonpad_ret = 0;
1161
1162 ret = 0;
1163 spliced = 0;
1164
1165 while (len) {
1166 ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1167 if (ret < 0)
1168 break;
1169 else if (!ret) {
Tom Zanussiebf99092007-06-04 09:12:05 +02001170 if (spliced)
1171 break;
1172 if (flags & SPLICE_F_NONBLOCK) {
1173 ret = -EAGAIN;
1174 break;
1175 }
1176 }
1177
1178 *ppos += ret;
1179 if (ret > len)
1180 len = 0;
1181 else
1182 len -= ret;
1183 spliced += nonpad_ret;
1184 nonpad_ret = 0;
1185 }
1186
1187 if (spliced)
1188 return spliced;
1189
1190 return ret;
Jens Axboe221415d2006-03-23 19:57:55 +01001191}
1192
Helge Deller15ad7cd2006-12-06 20:40:36 -08001193const struct file_operations relay_file_operations = {
Jens Axboeb86ff9812006-03-23 19:56:55 +01001194 .open = relay_file_open,
1195 .poll = relay_file_poll,
1196 .mmap = relay_file_mmap,
1197 .read = relay_file_read,
1198 .llseek = no_llseek,
1199 .release = relay_file_release,
Tom Zanussiebf99092007-06-04 09:12:05 +02001200 .splice_read = relay_file_splice_read,
Jens Axboeb86ff9812006-03-23 19:56:55 +01001201};
1202EXPORT_SYMBOL_GPL(relay_file_operations);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -08001203
1204static __init int relay_init(void)
1205{
1206
1207 hotcpu_notifier(relay_hotcpu_callback, 0);
1208 return 0;
1209}
1210
1211module_init(relay_init);