]>
Commit | Line | Data |
---|---|---|
b86ff981 JA |
1 | /* |
2 | * Public API and common code for kernel->userspace relay file support. | |
3 | * | |
c9b3febc | 4 | * See Documentation/filesystems/relay.txt for an overview. |
b86ff981 JA |
5 | * |
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. | |
23c88752 MD |
10 | * November 2006 - CPU hotplug support by Mathieu Desnoyers |
11 | * (mathieu.desnoyers@polymtl.ca) | |
b86ff981 JA |
12 | * |
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> | |
23c88752 | 23 | #include <linux/cpu.h> |
d6b29d7c | 24 | #include <linux/splice.h> |
23c88752 MD |
25 | |
26 | /* list of open channels, for cpu hotplug */ | |
27 | static DEFINE_MUTEX(relay_channels_mutex); | |
28 | static LIST_HEAD(relay_channels); | |
b86ff981 JA |
29 | |
30 | /* | |
31 | * close() vm_op implementation for relay file mapping. | |
32 | */ | |
33 | static 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 | /* | |
a1e09612 | 40 | * fault() vm_op implementation for relay file mapping. |
b86ff981 | 41 | */ |
a1e09612 | 42 | static int relay_buf_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b86ff981 JA |
43 | { |
44 | struct page *page; | |
45 | struct rchan_buf *buf = vma->vm_private_data; | |
a1e09612 | 46 | pgoff_t pgoff = vmf->pgoff; |
b86ff981 | 47 | |
b86ff981 | 48 | if (!buf) |
a1e09612 | 49 | return VM_FAULT_OOM; |
b86ff981 | 50 | |
a1e09612 | 51 | page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT)); |
b86ff981 | 52 | if (!page) |
a1e09612 | 53 | return VM_FAULT_SIGBUS; |
b86ff981 | 54 | get_page(page); |
a1e09612 | 55 | vmf->page = page; |
b86ff981 | 56 | |
a1e09612 | 57 | return 0; |
b86ff981 JA |
58 | } |
59 | ||
60 | /* | |
61 | * vm_ops for relay file mappings. | |
62 | */ | |
63 | static struct vm_operations_struct relay_file_mmap_ops = { | |
a1e09612 | 64 | .fault = relay_buf_fault, |
b86ff981 JA |
65 | .close = relay_file_mmap_close, |
66 | }; | |
67 | ||
68 | /** | |
69 | * relay_mmap_buf: - mmap channel buffer to process address space | |
70 | * @buf: relay channel buffer | |
71 | * @vma: vm_area_struct describing memory to be mapped | |
72 | * | |
73 | * Returns 0 if ok, negative on error | |
74 | * | |
75 | * Caller should already have grabbed mmap_sem. | |
76 | */ | |
01c55ed3 | 77 | static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) |
b86ff981 JA |
78 | { |
79 | unsigned long length = vma->vm_end - vma->vm_start; | |
80 | struct file *filp = vma->vm_file; | |
81 | ||
82 | if (!buf) | |
83 | return -EBADF; | |
84 | ||
85 | if (length != (unsigned long)buf->chan->alloc_size) | |
86 | return -EINVAL; | |
87 | ||
88 | vma->vm_ops = &relay_file_mmap_ops; | |
2f98735c | 89 | vma->vm_flags |= VM_DONTEXPAND; |
b86ff981 JA |
90 | vma->vm_private_data = buf; |
91 | buf->chan->cb->buf_mapped(buf, filp); | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | /** | |
97 | * relay_alloc_buf - allocate a channel buffer | |
98 | * @buf: the buffer struct | |
99 | * @size: total size of the buffer | |
100 | * | |
4c78a663 | 101 | * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The |
221415d7 | 102 | * passed in size will get page aligned, if it isn't already. |
b86ff981 | 103 | */ |
221415d7 | 104 | static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) |
b86ff981 JA |
105 | { |
106 | void *mem; | |
107 | unsigned int i, j, n_pages; | |
108 | ||
221415d7 JA |
109 | *size = PAGE_ALIGN(*size); |
110 | n_pages = *size >> PAGE_SHIFT; | |
b86ff981 JA |
111 | |
112 | buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL); | |
113 | if (!buf->page_array) | |
114 | return NULL; | |
115 | ||
116 | for (i = 0; i < n_pages; i++) { | |
117 | buf->page_array[i] = alloc_page(GFP_KERNEL); | |
118 | if (unlikely(!buf->page_array[i])) | |
119 | goto depopulate; | |
ebf99093 | 120 | set_page_private(buf->page_array[i], (unsigned long)buf); |
b86ff981 JA |
121 | } |
122 | mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); | |
123 | if (!mem) | |
124 | goto depopulate; | |
125 | ||
221415d7 | 126 | memset(mem, 0, *size); |
b86ff981 JA |
127 | buf->page_count = n_pages; |
128 | return mem; | |
129 | ||
130 | depopulate: | |
131 | for (j = 0; j < i; j++) | |
132 | __free_page(buf->page_array[j]); | |
133 | kfree(buf->page_array); | |
134 | return NULL; | |
135 | } | |
136 | ||
137 | /** | |
138 | * relay_create_buf - allocate and initialize a channel buffer | |
4c78a663 | 139 | * @chan: the relay channel |
b86ff981 | 140 | * |
4c78a663 | 141 | * Returns channel buffer if successful, %NULL otherwise. |
b86ff981 | 142 | */ |
01c55ed3 | 143 | static struct rchan_buf *relay_create_buf(struct rchan *chan) |
b86ff981 | 144 | { |
cd861280 | 145 | struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); |
b86ff981 JA |
146 | if (!buf) |
147 | return NULL; | |
148 | ||
149 | buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL); | |
150 | if (!buf->padding) | |
151 | goto free_buf; | |
152 | ||
221415d7 | 153 | buf->start = relay_alloc_buf(buf, &chan->alloc_size); |
b86ff981 JA |
154 | if (!buf->start) |
155 | goto free_buf; | |
156 | ||
157 | buf->chan = chan; | |
158 | kref_get(&buf->chan->kref); | |
159 | return buf; | |
160 | ||
161 | free_buf: | |
162 | kfree(buf->padding); | |
163 | kfree(buf); | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | /** | |
168 | * relay_destroy_channel - free the channel struct | |
4c78a663 | 169 | * @kref: target kernel reference that contains the relay channel |
b86ff981 JA |
170 | * |
171 | * Should only be called from kref_put(). | |
172 | */ | |
01c55ed3 | 173 | static void relay_destroy_channel(struct kref *kref) |
b86ff981 JA |
174 | { |
175 | struct rchan *chan = container_of(kref, struct rchan, kref); | |
176 | kfree(chan); | |
177 | } | |
178 | ||
179 | /** | |
180 | * relay_destroy_buf - destroy an rchan_buf struct and associated buffer | |
181 | * @buf: the buffer struct | |
182 | */ | |
01c55ed3 | 183 | static void relay_destroy_buf(struct rchan_buf *buf) |
b86ff981 JA |
184 | { |
185 | struct rchan *chan = buf->chan; | |
186 | unsigned int i; | |
187 | ||
188 | if (likely(buf->start)) { | |
189 | vunmap(buf->start); | |
190 | for (i = 0; i < buf->page_count; i++) | |
191 | __free_page(buf->page_array[i]); | |
192 | kfree(buf->page_array); | |
193 | } | |
23c88752 | 194 | chan->buf[buf->cpu] = NULL; |
b86ff981 JA |
195 | kfree(buf->padding); |
196 | kfree(buf); | |
197 | kref_put(&chan->kref, relay_destroy_channel); | |
198 | } | |
199 | ||
200 | /** | |
201 | * relay_remove_buf - remove a channel buffer | |
4c78a663 | 202 | * @kref: target kernel reference that contains the relay buffer |
b86ff981 JA |
203 | * |
204 | * Removes the file from the fileystem, which also frees the | |
205 | * rchan_buf_struct and the channel buffer. Should only be called from | |
206 | * kref_put(). | |
207 | */ | |
01c55ed3 | 208 | static void relay_remove_buf(struct kref *kref) |
b86ff981 JA |
209 | { |
210 | struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); | |
211 | buf->chan->cb->remove_buf_file(buf->dentry); | |
212 | relay_destroy_buf(buf); | |
213 | } | |
214 | ||
215 | /** | |
216 | * relay_buf_empty - boolean, is the channel buffer empty? | |
217 | * @buf: channel buffer | |
218 | * | |
219 | * Returns 1 if the buffer is empty, 0 otherwise. | |
220 | */ | |
01c55ed3 | 221 | static int relay_buf_empty(struct rchan_buf *buf) |
b86ff981 JA |
222 | { |
223 | return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; | |
224 | } | |
b86ff981 JA |
225 | |
226 | /** | |
227 | * relay_buf_full - boolean, is the channel buffer full? | |
228 | * @buf: channel buffer | |
229 | * | |
230 | * Returns 1 if the buffer is full, 0 otherwise. | |
231 | */ | |
232 | int relay_buf_full(struct rchan_buf *buf) | |
233 | { | |
234 | size_t ready = buf->subbufs_produced - buf->subbufs_consumed; | |
235 | return (ready >= buf->chan->n_subbufs) ? 1 : 0; | |
236 | } | |
237 | EXPORT_SYMBOL_GPL(relay_buf_full); | |
238 | ||
239 | /* | |
240 | * High-level relay kernel API and associated functions. | |
241 | */ | |
242 | ||
243 | /* | |
244 | * rchan_callback implementations defining default channel behavior. Used | |
245 | * in place of corresponding NULL values in client callback struct. | |
246 | */ | |
247 | ||
248 | /* | |
249 | * subbuf_start() default callback. Does nothing. | |
250 | */ | |
251 | static int subbuf_start_default_callback (struct rchan_buf *buf, | |
252 | void *subbuf, | |
253 | void *prev_subbuf, | |
254 | size_t prev_padding) | |
255 | { | |
256 | if (relay_buf_full(buf)) | |
257 | return 0; | |
258 | ||
259 | return 1; | |
260 | } | |
261 | ||
262 | /* | |
263 | * buf_mapped() default callback. Does nothing. | |
264 | */ | |
265 | static void buf_mapped_default_callback(struct rchan_buf *buf, | |
266 | struct file *filp) | |
267 | { | |
268 | } | |
269 | ||
270 | /* | |
271 | * buf_unmapped() default callback. Does nothing. | |
272 | */ | |
273 | static void buf_unmapped_default_callback(struct rchan_buf *buf, | |
274 | struct file *filp) | |
275 | { | |
276 | } | |
277 | ||
278 | /* | |
279 | * create_buf_file_create() default callback. Does nothing. | |
280 | */ | |
281 | static struct dentry *create_buf_file_default_callback(const char *filename, | |
282 | struct dentry *parent, | |
283 | int mode, | |
284 | struct rchan_buf *buf, | |
285 | int *is_global) | |
286 | { | |
287 | return NULL; | |
288 | } | |
289 | ||
290 | /* | |
291 | * remove_buf_file() default callback. Does nothing. | |
292 | */ | |
293 | static int remove_buf_file_default_callback(struct dentry *dentry) | |
294 | { | |
295 | return -EINVAL; | |
296 | } | |
297 | ||
298 | /* relay channel default callbacks */ | |
299 | static struct rchan_callbacks default_channel_callbacks = { | |
300 | .subbuf_start = subbuf_start_default_callback, | |
301 | .buf_mapped = buf_mapped_default_callback, | |
302 | .buf_unmapped = buf_unmapped_default_callback, | |
303 | .create_buf_file = create_buf_file_default_callback, | |
304 | .remove_buf_file = remove_buf_file_default_callback, | |
305 | }; | |
306 | ||
307 | /** | |
308 | * wakeup_readers - wake up readers waiting on a channel | |
9a9136e2 | 309 | * @data: contains the channel buffer |
b86ff981 | 310 | * |
7c9cb383 | 311 | * This is the timer function used to defer reader waking. |
b86ff981 | 312 | */ |
7c9cb383 | 313 | static void wakeup_readers(unsigned long data) |
b86ff981 | 314 | { |
7c9cb383 | 315 | struct rchan_buf *buf = (struct rchan_buf *)data; |
b86ff981 JA |
316 | wake_up_interruptible(&buf->read_wait); |
317 | } | |
318 | ||
319 | /** | |
320 | * __relay_reset - reset a channel buffer | |
321 | * @buf: the channel buffer | |
322 | * @init: 1 if this is a first-time initialization | |
323 | * | |
72fd4a35 | 324 | * See relay_reset() for description of effect. |
b86ff981 | 325 | */ |
192636ad | 326 | static void __relay_reset(struct rchan_buf *buf, unsigned int init) |
b86ff981 JA |
327 | { |
328 | size_t i; | |
329 | ||
330 | if (init) { | |
331 | init_waitqueue_head(&buf->read_wait); | |
332 | kref_init(&buf->kref); | |
7c9cb383 TZ |
333 | setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf); |
334 | } else | |
335 | del_timer_sync(&buf->timer); | |
b86ff981 JA |
336 | |
337 | buf->subbufs_produced = 0; | |
338 | buf->subbufs_consumed = 0; | |
339 | buf->bytes_consumed = 0; | |
340 | buf->finalized = 0; | |
341 | buf->data = buf->start; | |
342 | buf->offset = 0; | |
343 | ||
344 | for (i = 0; i < buf->chan->n_subbufs; i++) | |
345 | buf->padding[i] = 0; | |
346 | ||
347 | buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); | |
348 | } | |
349 | ||
350 | /** | |
351 | * relay_reset - reset the channel | |
352 | * @chan: the channel | |
353 | * | |
354 | * This has the effect of erasing all data from all channel buffers | |
355 | * and restarting the channel in its initial state. The buffers | |
356 | * are not freed, so any mappings are still in effect. | |
357 | * | |
72fd4a35 | 358 | * NOTE. Care should be taken that the channel isn't actually |
b86ff981 JA |
359 | * being used by anything when this call is made. |
360 | */ | |
361 | void relay_reset(struct rchan *chan) | |
362 | { | |
363 | unsigned int i; | |
b86ff981 JA |
364 | |
365 | if (!chan) | |
366 | return; | |
367 | ||
1bfbc608 | 368 | if (chan->is_global && chan->buf[0]) { |
23c88752 MD |
369 | __relay_reset(chan->buf[0], 0); |
370 | return; | |
b86ff981 | 371 | } |
23c88752 MD |
372 | |
373 | mutex_lock(&relay_channels_mutex); | |
374 | for_each_online_cpu(i) | |
375 | if (chan->buf[i]) | |
376 | __relay_reset(chan->buf[i], 0); | |
377 | mutex_unlock(&relay_channels_mutex); | |
b86ff981 JA |
378 | } |
379 | EXPORT_SYMBOL_GPL(relay_reset); | |
380 | ||
4c78a663 | 381 | /* |
b86ff981 JA |
382 | * relay_open_buf - create a new relay channel buffer |
383 | * | |
23c88752 | 384 | * used by relay_open() and CPU hotplug. |
b86ff981 | 385 | */ |
23c88752 | 386 | static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) |
b86ff981 | 387 | { |
23c88752 | 388 | struct rchan_buf *buf = NULL; |
b86ff981 | 389 | struct dentry *dentry; |
23c88752 | 390 | char *tmpname; |
b86ff981 | 391 | |
23c88752 | 392 | if (chan->is_global) |
b86ff981 JA |
393 | return chan->buf[0]; |
394 | ||
23c88752 MD |
395 | tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); |
396 | if (!tmpname) | |
397 | goto end; | |
398 | snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu); | |
399 | ||
b86ff981 JA |
400 | buf = relay_create_buf(chan); |
401 | if (!buf) | |
23c88752 MD |
402 | goto free_name; |
403 | ||
404 | buf->cpu = cpu; | |
405 | __relay_reset(buf, 1); | |
b86ff981 JA |
406 | |
407 | /* Create file in fs */ | |
23c88752 MD |
408 | dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR, |
409 | buf, &chan->is_global); | |
410 | if (!dentry) | |
411 | goto free_buf; | |
b86ff981 JA |
412 | |
413 | buf->dentry = dentry; | |
b86ff981 | 414 | |
23c88752 MD |
415 | if(chan->is_global) { |
416 | chan->buf[0] = buf; | |
417 | buf->cpu = 0; | |
418 | } | |
419 | ||
420 | goto free_name; | |
421 | ||
422 | free_buf: | |
423 | relay_destroy_buf(buf); | |
c9b3febc | 424 | buf = NULL; |
23c88752 MD |
425 | free_name: |
426 | kfree(tmpname); | |
427 | end: | |
b86ff981 JA |
428 | return buf; |
429 | } | |
430 | ||
431 | /** | |
432 | * relay_close_buf - close a channel buffer | |
433 | * @buf: channel buffer | |
434 | * | |
435 | * Marks the buffer finalized and restores the default callbacks. | |
436 | * The channel buffer and channel buffer data structure are then freed | |
437 | * automatically when the last reference is given up. | |
438 | */ | |
192636ad | 439 | static void relay_close_buf(struct rchan_buf *buf) |
b86ff981 JA |
440 | { |
441 | buf->finalized = 1; | |
7c9cb383 | 442 | del_timer_sync(&buf->timer); |
b86ff981 JA |
443 | kref_put(&buf->kref, relay_remove_buf); |
444 | } | |
445 | ||
192636ad | 446 | static void setup_callbacks(struct rchan *chan, |
b86ff981 JA |
447 | struct rchan_callbacks *cb) |
448 | { | |
449 | if (!cb) { | |
450 | chan->cb = &default_channel_callbacks; | |
451 | return; | |
452 | } | |
453 | ||
454 | if (!cb->subbuf_start) | |
455 | cb->subbuf_start = subbuf_start_default_callback; | |
456 | if (!cb->buf_mapped) | |
457 | cb->buf_mapped = buf_mapped_default_callback; | |
458 | if (!cb->buf_unmapped) | |
459 | cb->buf_unmapped = buf_unmapped_default_callback; | |
460 | if (!cb->create_buf_file) | |
461 | cb->create_buf_file = create_buf_file_default_callback; | |
462 | if (!cb->remove_buf_file) | |
463 | cb->remove_buf_file = remove_buf_file_default_callback; | |
464 | chan->cb = cb; | |
465 | } | |
466 | ||
23c88752 | 467 | /** |
23c88752 MD |
468 | * relay_hotcpu_callback - CPU hotplug callback |
469 | * @nb: notifier block | |
470 | * @action: hotplug action to take | |
471 | * @hcpu: CPU number | |
472 | * | |
05fb6bf0 | 473 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) |
23c88752 MD |
474 | */ |
475 | static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb, | |
476 | unsigned long action, | |
477 | void *hcpu) | |
478 | { | |
479 | unsigned int hotcpu = (unsigned long)hcpu; | |
480 | struct rchan *chan; | |
481 | ||
482 | switch(action) { | |
483 | case CPU_UP_PREPARE: | |
8bb78442 | 484 | case CPU_UP_PREPARE_FROZEN: |
23c88752 MD |
485 | mutex_lock(&relay_channels_mutex); |
486 | list_for_each_entry(chan, &relay_channels, list) { | |
487 | if (chan->buf[hotcpu]) | |
488 | continue; | |
489 | chan->buf[hotcpu] = relay_open_buf(chan, hotcpu); | |
490 | if(!chan->buf[hotcpu]) { | |
491 | printk(KERN_ERR | |
492 | "relay_hotcpu_callback: cpu %d buffer " | |
493 | "creation failed\n", hotcpu); | |
494 | mutex_unlock(&relay_channels_mutex); | |
495 | return NOTIFY_BAD; | |
496 | } | |
497 | } | |
498 | mutex_unlock(&relay_channels_mutex); | |
499 | break; | |
500 | case CPU_DEAD: | |
8bb78442 | 501 | case CPU_DEAD_FROZEN: |
23c88752 MD |
502 | /* No need to flush the cpu : will be flushed upon |
503 | * final relay_flush() call. */ | |
504 | break; | |
505 | } | |
506 | return NOTIFY_OK; | |
507 | } | |
508 | ||
b86ff981 JA |
509 | /** |
510 | * relay_open - create a new relay channel | |
511 | * @base_filename: base name of files to create | |
4c78a663 | 512 | * @parent: dentry of parent directory, %NULL for root directory |
b86ff981 JA |
513 | * @subbuf_size: size of sub-buffers |
514 | * @n_subbufs: number of sub-buffers | |
515 | * @cb: client callback functions | |
23c88752 | 516 | * @private_data: user-defined data |
b86ff981 | 517 | * |
4c78a663 | 518 | * Returns channel pointer if successful, %NULL otherwise. |
b86ff981 JA |
519 | * |
520 | * Creates a channel buffer for each cpu using the sizes and | |
521 | * attributes specified. The created channel buffer files | |
522 | * will be named base_filename0...base_filenameN-1. File | |
72fd4a35 | 523 | * permissions will be %S_IRUSR. |
b86ff981 JA |
524 | */ |
525 | struct rchan *relay_open(const char *base_filename, | |
526 | struct dentry *parent, | |
527 | size_t subbuf_size, | |
528 | size_t n_subbufs, | |
23c88752 MD |
529 | struct rchan_callbacks *cb, |
530 | void *private_data) | |
b86ff981 JA |
531 | { |
532 | unsigned int i; | |
533 | struct rchan *chan; | |
b86ff981 JA |
534 | if (!base_filename) |
535 | return NULL; | |
536 | ||
537 | if (!(subbuf_size && n_subbufs)) | |
538 | return NULL; | |
539 | ||
cd861280 | 540 | chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); |
b86ff981 JA |
541 | if (!chan) |
542 | return NULL; | |
543 | ||
544 | chan->version = RELAYFS_CHANNEL_VERSION; | |
545 | chan->n_subbufs = n_subbufs; | |
546 | chan->subbuf_size = subbuf_size; | |
547 | chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs); | |
23c88752 MD |
548 | chan->parent = parent; |
549 | chan->private_data = private_data; | |
550 | strlcpy(chan->base_filename, base_filename, NAME_MAX); | |
b86ff981 JA |
551 | setup_callbacks(chan, cb); |
552 | kref_init(&chan->kref); | |
553 | ||
23c88752 | 554 | mutex_lock(&relay_channels_mutex); |
b86ff981 | 555 | for_each_online_cpu(i) { |
23c88752 | 556 | chan->buf[i] = relay_open_buf(chan, i); |
b86ff981 JA |
557 | if (!chan->buf[i]) |
558 | goto free_bufs; | |
b86ff981 | 559 | } |
23c88752 MD |
560 | list_add(&chan->list, &relay_channels); |
561 | mutex_unlock(&relay_channels_mutex); | |
b86ff981 | 562 | |
b86ff981 JA |
563 | return chan; |
564 | ||
565 | free_bufs: | |
23c88752 | 566 | for_each_online_cpu(i) { |
b86ff981 JA |
567 | if (!chan->buf[i]) |
568 | break; | |
569 | relay_close_buf(chan->buf[i]); | |
b86ff981 | 570 | } |
b86ff981 | 571 | |
b86ff981 | 572 | kref_put(&chan->kref, relay_destroy_channel); |
23c88752 | 573 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
574 | return NULL; |
575 | } | |
576 | EXPORT_SYMBOL_GPL(relay_open); | |
577 | ||
578 | /** | |
579 | * relay_switch_subbuf - switch to a new sub-buffer | |
580 | * @buf: channel buffer | |
581 | * @length: size of current event | |
582 | * | |
583 | * Returns either the length passed in or 0 if full. | |
584 | * | |
585 | * Performs sub-buffer-switch tasks such as invoking callbacks, | |
586 | * updating padding counts, waking up readers, etc. | |
587 | */ | |
588 | size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) | |
589 | { | |
590 | void *old, *new; | |
591 | size_t old_subbuf, new_subbuf; | |
592 | ||
593 | if (unlikely(length > buf->chan->subbuf_size)) | |
594 | goto toobig; | |
595 | ||
596 | if (buf->offset != buf->chan->subbuf_size + 1) { | |
597 | buf->prev_padding = buf->chan->subbuf_size - buf->offset; | |
598 | old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; | |
599 | buf->padding[old_subbuf] = buf->prev_padding; | |
600 | buf->subbufs_produced++; | |
221415d7 JA |
601 | buf->dentry->d_inode->i_size += buf->chan->subbuf_size - |
602 | buf->padding[old_subbuf]; | |
603 | smp_mb(); | |
7c9cb383 TZ |
604 | if (waitqueue_active(&buf->read_wait)) |
605 | /* | |
606 | * Calling wake_up_interruptible() from here | |
607 | * will deadlock if we happen to be logging | |
608 | * from the scheduler (trying to re-grab | |
609 | * rq->lock), so defer it. | |
610 | */ | |
611 | __mod_timer(&buf->timer, jiffies + 1); | |
b86ff981 JA |
612 | } |
613 | ||
614 | old = buf->data; | |
615 | new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; | |
616 | new = buf->start + new_subbuf * buf->chan->subbuf_size; | |
617 | buf->offset = 0; | |
618 | if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { | |
619 | buf->offset = buf->chan->subbuf_size + 1; | |
620 | return 0; | |
621 | } | |
622 | buf->data = new; | |
623 | buf->padding[new_subbuf] = 0; | |
624 | ||
625 | if (unlikely(length + buf->offset > buf->chan->subbuf_size)) | |
626 | goto toobig; | |
627 | ||
628 | return length; | |
629 | ||
630 | toobig: | |
631 | buf->chan->last_toobig = length; | |
632 | return 0; | |
633 | } | |
634 | EXPORT_SYMBOL_GPL(relay_switch_subbuf); | |
635 | ||
636 | /** | |
637 | * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count | |
638 | * @chan: the channel | |
639 | * @cpu: the cpu associated with the channel buffer to update | |
640 | * @subbufs_consumed: number of sub-buffers to add to current buf's count | |
641 | * | |
642 | * Adds to the channel buffer's consumed sub-buffer count. | |
643 | * subbufs_consumed should be the number of sub-buffers newly consumed, | |
644 | * not the total consumed. | |
645 | * | |
72fd4a35 | 646 | * NOTE. Kernel clients don't need to call this function if the channel |
b86ff981 JA |
647 | * mode is 'overwrite'. |
648 | */ | |
649 | void relay_subbufs_consumed(struct rchan *chan, | |
650 | unsigned int cpu, | |
651 | size_t subbufs_consumed) | |
652 | { | |
653 | struct rchan_buf *buf; | |
654 | ||
655 | if (!chan) | |
656 | return; | |
657 | ||
658 | if (cpu >= NR_CPUS || !chan->buf[cpu]) | |
659 | return; | |
660 | ||
661 | buf = chan->buf[cpu]; | |
662 | buf->subbufs_consumed += subbufs_consumed; | |
663 | if (buf->subbufs_consumed > buf->subbufs_produced) | |
664 | buf->subbufs_consumed = buf->subbufs_produced; | |
665 | } | |
666 | EXPORT_SYMBOL_GPL(relay_subbufs_consumed); | |
667 | ||
668 | /** | |
669 | * relay_close - close the channel | |
670 | * @chan: the channel | |
671 | * | |
672 | * Closes all channel buffers and frees the channel. | |
673 | */ | |
674 | void relay_close(struct rchan *chan) | |
675 | { | |
676 | unsigned int i; | |
b86ff981 JA |
677 | |
678 | if (!chan) | |
679 | return; | |
680 | ||
23c88752 MD |
681 | mutex_lock(&relay_channels_mutex); |
682 | if (chan->is_global && chan->buf[0]) | |
683 | relay_close_buf(chan->buf[0]); | |
684 | else | |
685 | for_each_possible_cpu(i) | |
686 | if (chan->buf[i]) | |
687 | relay_close_buf(chan->buf[i]); | |
b86ff981 JA |
688 | |
689 | if (chan->last_toobig) | |
690 | printk(KERN_WARNING "relay: one or more items not logged " | |
691 | "[item size (%Zd) > sub-buffer size (%Zd)]\n", | |
692 | chan->last_toobig, chan->subbuf_size); | |
693 | ||
23c88752 | 694 | list_del(&chan->list); |
b86ff981 | 695 | kref_put(&chan->kref, relay_destroy_channel); |
23c88752 | 696 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
697 | } |
698 | EXPORT_SYMBOL_GPL(relay_close); | |
699 | ||
700 | /** | |
701 | * relay_flush - close the channel | |
702 | * @chan: the channel | |
703 | * | |
4c78a663 | 704 | * Flushes all channel buffers, i.e. forces buffer switch. |
b86ff981 JA |
705 | */ |
706 | void relay_flush(struct rchan *chan) | |
707 | { | |
708 | unsigned int i; | |
b86ff981 JA |
709 | |
710 | if (!chan) | |
711 | return; | |
712 | ||
23c88752 MD |
713 | if (chan->is_global && chan->buf[0]) { |
714 | relay_switch_subbuf(chan->buf[0], 0); | |
715 | return; | |
b86ff981 | 716 | } |
23c88752 MD |
717 | |
718 | mutex_lock(&relay_channels_mutex); | |
719 | for_each_possible_cpu(i) | |
720 | if (chan->buf[i]) | |
721 | relay_switch_subbuf(chan->buf[i], 0); | |
722 | mutex_unlock(&relay_channels_mutex); | |
b86ff981 JA |
723 | } |
724 | EXPORT_SYMBOL_GPL(relay_flush); | |
725 | ||
726 | /** | |
727 | * relay_file_open - open file op for relay files | |
728 | * @inode: the inode | |
729 | * @filp: the file | |
730 | * | |
731 | * Increments the channel buffer refcount. | |
732 | */ | |
733 | static int relay_file_open(struct inode *inode, struct file *filp) | |
734 | { | |
8e18e294 | 735 | struct rchan_buf *buf = inode->i_private; |
b86ff981 JA |
736 | kref_get(&buf->kref); |
737 | filp->private_data = buf; | |
738 | ||
739 | return 0; | |
740 | } | |
741 | ||
742 | /** | |
743 | * relay_file_mmap - mmap file op for relay files | |
744 | * @filp: the file | |
745 | * @vma: the vma describing what to map | |
746 | * | |
72fd4a35 | 747 | * Calls upon relay_mmap_buf() to map the file into user space. |
b86ff981 JA |
748 | */ |
749 | static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) | |
750 | { | |
751 | struct rchan_buf *buf = filp->private_data; | |
752 | return relay_mmap_buf(buf, vma); | |
753 | } | |
754 | ||
755 | /** | |
756 | * relay_file_poll - poll file op for relay files | |
757 | * @filp: the file | |
758 | * @wait: poll table | |
759 | * | |
760 | * Poll implemention. | |
761 | */ | |
762 | static unsigned int relay_file_poll(struct file *filp, poll_table *wait) | |
763 | { | |
764 | unsigned int mask = 0; | |
765 | struct rchan_buf *buf = filp->private_data; | |
766 | ||
767 | if (buf->finalized) | |
768 | return POLLERR; | |
769 | ||
770 | if (filp->f_mode & FMODE_READ) { | |
771 | poll_wait(filp, &buf->read_wait, wait); | |
772 | if (!relay_buf_empty(buf)) | |
773 | mask |= POLLIN | POLLRDNORM; | |
774 | } | |
775 | ||
776 | return mask; | |
777 | } | |
778 | ||
779 | /** | |
780 | * relay_file_release - release file op for relay files | |
781 | * @inode: the inode | |
782 | * @filp: the file | |
783 | * | |
784 | * Decrements the channel refcount, as the filesystem is | |
785 | * no longer using it. | |
786 | */ | |
787 | static int relay_file_release(struct inode *inode, struct file *filp) | |
788 | { | |
789 | struct rchan_buf *buf = filp->private_data; | |
790 | kref_put(&buf->kref, relay_remove_buf); | |
791 | ||
792 | return 0; | |
793 | } | |
794 | ||
4c78a663 | 795 | /* |
b86ff981 JA |
796 | * relay_file_read_consume - update the consumed count for the buffer |
797 | */ | |
798 | static void relay_file_read_consume(struct rchan_buf *buf, | |
799 | size_t read_pos, | |
800 | size_t bytes_consumed) | |
801 | { | |
802 | size_t subbuf_size = buf->chan->subbuf_size; | |
803 | size_t n_subbufs = buf->chan->n_subbufs; | |
804 | size_t read_subbuf; | |
805 | ||
806 | if (buf->bytes_consumed + bytes_consumed > subbuf_size) { | |
807 | relay_subbufs_consumed(buf->chan, buf->cpu, 1); | |
808 | buf->bytes_consumed = 0; | |
809 | } | |
810 | ||
811 | buf->bytes_consumed += bytes_consumed; | |
a66e356c MH |
812 | if (!read_pos) |
813 | read_subbuf = buf->subbufs_consumed % n_subbufs; | |
814 | else | |
815 | read_subbuf = read_pos / buf->chan->subbuf_size; | |
b86ff981 JA |
816 | if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { |
817 | if ((read_subbuf == buf->subbufs_produced % n_subbufs) && | |
818 | (buf->offset == subbuf_size)) | |
819 | return; | |
820 | relay_subbufs_consumed(buf->chan, buf->cpu, 1); | |
821 | buf->bytes_consumed = 0; | |
822 | } | |
823 | } | |
824 | ||
4c78a663 | 825 | /* |
b86ff981 JA |
826 | * relay_file_read_avail - boolean, are there unconsumed bytes available? |
827 | */ | |
828 | static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) | |
829 | { | |
b86ff981 JA |
830 | size_t subbuf_size = buf->chan->subbuf_size; |
831 | size_t n_subbufs = buf->chan->n_subbufs; | |
221415d7 JA |
832 | size_t produced = buf->subbufs_produced; |
833 | size_t consumed = buf->subbufs_consumed; | |
b86ff981 | 834 | |
221415d7 | 835 | relay_file_read_consume(buf, read_pos, 0); |
b86ff981 | 836 | |
221415d7 JA |
837 | if (unlikely(buf->offset > subbuf_size)) { |
838 | if (produced == consumed) | |
839 | return 0; | |
840 | return 1; | |
b86ff981 JA |
841 | } |
842 | ||
221415d7 | 843 | if (unlikely(produced - consumed >= n_subbufs)) { |
a66e356c | 844 | consumed = produced - n_subbufs + 1; |
221415d7 | 845 | buf->subbufs_consumed = consumed; |
a66e356c | 846 | buf->bytes_consumed = 0; |
221415d7 | 847 | } |
1bfbc608 | 848 | |
221415d7 JA |
849 | produced = (produced % n_subbufs) * subbuf_size + buf->offset; |
850 | consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; | |
851 | ||
852 | if (consumed > produced) | |
853 | produced += n_subbufs * subbuf_size; | |
1bfbc608 | 854 | |
221415d7 | 855 | if (consumed == produced) |
b86ff981 JA |
856 | return 0; |
857 | ||
b86ff981 JA |
858 | return 1; |
859 | } | |
860 | ||
861 | /** | |
862 | * relay_file_read_subbuf_avail - return bytes available in sub-buffer | |
4c78a663 RD |
863 | * @read_pos: file read position |
864 | * @buf: relay channel buffer | |
b86ff981 JA |
865 | */ |
866 | static size_t relay_file_read_subbuf_avail(size_t read_pos, | |
867 | struct rchan_buf *buf) | |
868 | { | |
869 | size_t padding, avail = 0; | |
870 | size_t read_subbuf, read_offset, write_subbuf, write_offset; | |
871 | size_t subbuf_size = buf->chan->subbuf_size; | |
872 | ||
873 | write_subbuf = (buf->data - buf->start) / subbuf_size; | |
874 | write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; | |
875 | read_subbuf = read_pos / subbuf_size; | |
876 | read_offset = read_pos % subbuf_size; | |
877 | padding = buf->padding[read_subbuf]; | |
878 | ||
879 | if (read_subbuf == write_subbuf) { | |
880 | if (read_offset + padding < write_offset) | |
881 | avail = write_offset - (read_offset + padding); | |
882 | } else | |
883 | avail = (subbuf_size - padding) - read_offset; | |
884 | ||
885 | return avail; | |
886 | } | |
887 | ||
888 | /** | |
889 | * relay_file_read_start_pos - find the first available byte to read | |
4c78a663 RD |
890 | * @read_pos: file read position |
891 | * @buf: relay channel buffer | |
b86ff981 | 892 | * |
72fd4a35 | 893 | * If the @read_pos is in the middle of padding, return the |
b86ff981 JA |
894 | * position of the first actually available byte, otherwise |
895 | * return the original value. | |
896 | */ | |
897 | static size_t relay_file_read_start_pos(size_t read_pos, | |
898 | struct rchan_buf *buf) | |
899 | { | |
900 | size_t read_subbuf, padding, padding_start, padding_end; | |
901 | size_t subbuf_size = buf->chan->subbuf_size; | |
902 | size_t n_subbufs = buf->chan->n_subbufs; | |
8d62fdeb | 903 | size_t consumed = buf->subbufs_consumed % n_subbufs; |
b86ff981 | 904 | |
8d62fdeb DW |
905 | if (!read_pos) |
906 | read_pos = consumed * subbuf_size + buf->bytes_consumed; | |
b86ff981 JA |
907 | read_subbuf = read_pos / subbuf_size; |
908 | padding = buf->padding[read_subbuf]; | |
909 | padding_start = (read_subbuf + 1) * subbuf_size - padding; | |
910 | padding_end = (read_subbuf + 1) * subbuf_size; | |
911 | if (read_pos >= padding_start && read_pos < padding_end) { | |
912 | read_subbuf = (read_subbuf + 1) % n_subbufs; | |
913 | read_pos = read_subbuf * subbuf_size; | |
914 | } | |
915 | ||
916 | return read_pos; | |
917 | } | |
918 | ||
919 | /** | |
920 | * relay_file_read_end_pos - return the new read position | |
4c78a663 RD |
921 | * @read_pos: file read position |
922 | * @buf: relay channel buffer | |
923 | * @count: number of bytes to be read | |
b86ff981 JA |
924 | */ |
925 | static size_t relay_file_read_end_pos(struct rchan_buf *buf, | |
926 | size_t read_pos, | |
927 | size_t count) | |
928 | { | |
929 | size_t read_subbuf, padding, end_pos; | |
930 | size_t subbuf_size = buf->chan->subbuf_size; | |
931 | size_t n_subbufs = buf->chan->n_subbufs; | |
932 | ||
933 | read_subbuf = read_pos / subbuf_size; | |
934 | padding = buf->padding[read_subbuf]; | |
935 | if (read_pos % subbuf_size + count + padding == subbuf_size) | |
936 | end_pos = (read_subbuf + 1) * subbuf_size; | |
937 | else | |
938 | end_pos = read_pos + count; | |
939 | if (end_pos >= subbuf_size * n_subbufs) | |
940 | end_pos = 0; | |
941 | ||
942 | return end_pos; | |
943 | } | |
944 | ||
4c78a663 | 945 | /* |
6dac40a7 | 946 | * subbuf_read_actor - read up to one subbuf's worth of data |
b86ff981 | 947 | */ |
6dac40a7 TZ |
948 | static int subbuf_read_actor(size_t read_start, |
949 | struct rchan_buf *buf, | |
950 | size_t avail, | |
951 | read_descriptor_t *desc, | |
952 | read_actor_t actor) | |
b86ff981 | 953 | { |
b86ff981 | 954 | void *from; |
6dac40a7 | 955 | int ret = 0; |
b86ff981 JA |
956 | |
957 | from = buf->start + read_start; | |
6dac40a7 | 958 | ret = avail; |
ba2397ef | 959 | if (copy_to_user(desc->arg.buf, from, avail)) { |
6dac40a7 TZ |
960 | desc->error = -EFAULT; |
961 | ret = 0; | |
b86ff981 | 962 | } |
6dac40a7 TZ |
963 | desc->arg.data += ret; |
964 | desc->written += ret; | |
965 | desc->count -= ret; | |
966 | ||
b86ff981 JA |
967 | return ret; |
968 | } | |
969 | ||
6dac40a7 TZ |
970 | typedef int (*subbuf_actor_t) (size_t read_start, |
971 | struct rchan_buf *buf, | |
972 | size_t avail, | |
973 | read_descriptor_t *desc, | |
974 | read_actor_t actor); | |
975 | ||
4c78a663 | 976 | /* |
6dac40a7 TZ |
977 | * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries |
978 | */ | |
192636ad AM |
979 | static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos, |
980 | subbuf_actor_t subbuf_actor, | |
981 | read_actor_t actor, | |
982 | read_descriptor_t *desc) | |
221415d7 | 983 | { |
6dac40a7 TZ |
984 | struct rchan_buf *buf = filp->private_data; |
985 | size_t read_start, avail; | |
6dac40a7 | 986 | int ret; |
221415d7 | 987 | |
ba2397ef | 988 | if (!desc->count) |
221415d7 JA |
989 | return 0; |
990 | ||
f3a43f3f | 991 | mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); |
221415d7 | 992 | do { |
6dac40a7 TZ |
993 | if (!relay_file_read_avail(buf, *ppos)) |
994 | break; | |
995 | ||
996 | read_start = relay_file_read_start_pos(*ppos, buf); | |
997 | avail = relay_file_read_subbuf_avail(read_start, buf); | |
998 | if (!avail) | |
221415d7 | 999 | break; |
221415d7 | 1000 | |
ba2397ef AV |
1001 | avail = min(desc->count, avail); |
1002 | ret = subbuf_actor(read_start, buf, avail, desc, actor); | |
1003 | if (desc->error < 0) | |
6dac40a7 TZ |
1004 | break; |
1005 | ||
1006 | if (ret) { | |
1007 | relay_file_read_consume(buf, read_start, ret); | |
1008 | *ppos = relay_file_read_end_pos(buf, read_start, ret); | |
1009 | } | |
ba2397ef | 1010 | } while (desc->count && ret); |
f3a43f3f | 1011 | mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); |
6dac40a7 | 1012 | |
ba2397ef | 1013 | return desc->written; |
6dac40a7 TZ |
1014 | } |
1015 | ||
1016 | static ssize_t relay_file_read(struct file *filp, | |
1017 | char __user *buffer, | |
1018 | size_t count, | |
1019 | loff_t *ppos) | |
1020 | { | |
ba2397ef AV |
1021 | read_descriptor_t desc; |
1022 | desc.written = 0; | |
1023 | desc.count = count; | |
1024 | desc.arg.buf = buffer; | |
1025 | desc.error = 0; | |
1026 | return relay_file_read_subbufs(filp, ppos, subbuf_read_actor, | |
1027 | NULL, &desc); | |
6dac40a7 TZ |
1028 | } |
1029 | ||
1db60cf2 JA |
1030 | static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed) |
1031 | { | |
1032 | rbuf->bytes_consumed += bytes_consumed; | |
1033 | ||
1034 | if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) { | |
1035 | relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1); | |
1036 | rbuf->bytes_consumed %= rbuf->chan->subbuf_size; | |
1037 | } | |
1038 | } | |
1039 | ||
ebf99093 TZ |
1040 | static void relay_pipe_buf_release(struct pipe_inode_info *pipe, |
1041 | struct pipe_buffer *buf) | |
6dac40a7 | 1042 | { |
ebf99093 TZ |
1043 | struct rchan_buf *rbuf; |
1044 | ||
1045 | rbuf = (struct rchan_buf *)page_private(buf->page); | |
1db60cf2 | 1046 | relay_consume_bytes(rbuf, buf->private); |
ebf99093 TZ |
1047 | } |
1048 | ||
1049 | static struct pipe_buf_operations relay_pipe_buf_ops = { | |
1050 | .can_merge = 0, | |
1051 | .map = generic_pipe_buf_map, | |
1052 | .unmap = generic_pipe_buf_unmap, | |
cac36bb0 | 1053 | .confirm = generic_pipe_buf_confirm, |
ebf99093 TZ |
1054 | .release = relay_pipe_buf_release, |
1055 | .steal = generic_pipe_buf_steal, | |
1056 | .get = generic_pipe_buf_get, | |
1057 | }; | |
1058 | ||
d3f35d98 | 1059 | /* |
ebf99093 TZ |
1060 | * subbuf_splice_actor - splice up to one subbuf's worth of data |
1061 | */ | |
1062 | static int subbuf_splice_actor(struct file *in, | |
1063 | loff_t *ppos, | |
1064 | struct pipe_inode_info *pipe, | |
1065 | size_t len, | |
1066 | unsigned int flags, | |
1067 | int *nonpad_ret) | |
1068 | { | |
1db60cf2 | 1069 | unsigned int pidx, poff, total_len, subbuf_pages, ret; |
ebf99093 TZ |
1070 | struct rchan_buf *rbuf = in->private_data; |
1071 | unsigned int subbuf_size = rbuf->chan->subbuf_size; | |
24da24de TZ |
1072 | uint64_t pos = (uint64_t) *ppos; |
1073 | uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size; | |
1074 | size_t read_start = (size_t) do_div(pos, alloc_size); | |
ebf99093 TZ |
1075 | size_t read_subbuf = read_start / subbuf_size; |
1076 | size_t padding = rbuf->padding[read_subbuf]; | |
1077 | size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding; | |
1db60cf2 JA |
1078 | struct page *pages[PIPE_BUFFERS]; |
1079 | struct partial_page partial[PIPE_BUFFERS]; | |
1080 | struct splice_pipe_desc spd = { | |
1081 | .pages = pages, | |
1082 | .nr_pages = 0, | |
1083 | .partial = partial, | |
1084 | .flags = flags, | |
1085 | .ops = &relay_pipe_buf_ops, | |
1086 | }; | |
ebf99093 TZ |
1087 | |
1088 | if (rbuf->subbufs_produced == rbuf->subbufs_consumed) | |
1089 | return 0; | |
1090 | ||
1db60cf2 JA |
1091 | /* |
1092 | * Adjust read len, if longer than what is available | |
1093 | */ | |
1094 | if (len > (subbuf_size - read_start % subbuf_size)) | |
1095 | len = subbuf_size - read_start % subbuf_size; | |
ebf99093 TZ |
1096 | |
1097 | subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; | |
1098 | pidx = (read_start / PAGE_SIZE) % subbuf_pages; | |
1099 | poff = read_start & ~PAGE_MASK; | |
1100 | ||
1db60cf2 JA |
1101 | for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) { |
1102 | unsigned int this_len, this_end, private; | |
1103 | unsigned int cur_pos = read_start + total_len; | |
ebf99093 | 1104 | |
1db60cf2 | 1105 | if (!len) |
ebf99093 | 1106 | break; |
ebf99093 | 1107 | |
1db60cf2 JA |
1108 | this_len = min_t(unsigned long, len, PAGE_SIZE - poff); |
1109 | private = this_len; | |
ebf99093 | 1110 | |
1db60cf2 JA |
1111 | spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; |
1112 | spd.partial[spd.nr_pages].offset = poff; | |
ebf99093 | 1113 | |
1db60cf2 JA |
1114 | this_end = cur_pos + this_len; |
1115 | if (this_end >= nonpad_end) { | |
1116 | this_len = nonpad_end - cur_pos; | |
1117 | private = this_len + padding; | |
ebf99093 | 1118 | } |
1db60cf2 JA |
1119 | spd.partial[spd.nr_pages].len = this_len; |
1120 | spd.partial[spd.nr_pages].private = private; | |
ebf99093 | 1121 | |
1db60cf2 JA |
1122 | len -= this_len; |
1123 | total_len += this_len; | |
1124 | poff = 0; | |
1125 | pidx = (pidx + 1) % subbuf_pages; | |
ebf99093 | 1126 | |
1db60cf2 JA |
1127 | if (this_end >= nonpad_end) { |
1128 | spd.nr_pages++; | |
ebf99093 TZ |
1129 | break; |
1130 | } | |
ebf99093 TZ |
1131 | } |
1132 | ||
1db60cf2 JA |
1133 | if (!spd.nr_pages) |
1134 | return 0; | |
ebf99093 | 1135 | |
1db60cf2 JA |
1136 | ret = *nonpad_ret = splice_to_pipe(pipe, &spd); |
1137 | if (ret < 0 || ret < total_len) | |
1138 | return ret; | |
ebf99093 | 1139 | |
1db60cf2 JA |
1140 | if (read_start + ret == nonpad_end) |
1141 | ret += padding; | |
1142 | ||
1143 | return ret; | |
ebf99093 TZ |
1144 | } |
1145 | ||
1146 | static ssize_t relay_file_splice_read(struct file *in, | |
1147 | loff_t *ppos, | |
1148 | struct pipe_inode_info *pipe, | |
1149 | size_t len, | |
1150 | unsigned int flags) | |
1151 | { | |
1152 | ssize_t spliced; | |
1153 | int ret; | |
1154 | int nonpad_ret = 0; | |
1155 | ||
1156 | ret = 0; | |
1157 | spliced = 0; | |
1158 | ||
1159 | while (len) { | |
1160 | ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret); | |
1161 | if (ret < 0) | |
1162 | break; | |
1163 | else if (!ret) { | |
ebf99093 TZ |
1164 | if (spliced) |
1165 | break; | |
1166 | if (flags & SPLICE_F_NONBLOCK) { | |
1167 | ret = -EAGAIN; | |
1168 | break; | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | *ppos += ret; | |
1173 | if (ret > len) | |
1174 | len = 0; | |
1175 | else | |
1176 | len -= ret; | |
1177 | spliced += nonpad_ret; | |
1178 | nonpad_ret = 0; | |
1179 | } | |
1180 | ||
1181 | if (spliced) | |
1182 | return spliced; | |
1183 | ||
1184 | return ret; | |
221415d7 JA |
1185 | } |
1186 | ||
15ad7cdc | 1187 | const struct file_operations relay_file_operations = { |
b86ff981 JA |
1188 | .open = relay_file_open, |
1189 | .poll = relay_file_poll, | |
1190 | .mmap = relay_file_mmap, | |
1191 | .read = relay_file_read, | |
1192 | .llseek = no_llseek, | |
1193 | .release = relay_file_release, | |
ebf99093 | 1194 | .splice_read = relay_file_splice_read, |
b86ff981 JA |
1195 | }; |
1196 | EXPORT_SYMBOL_GPL(relay_file_operations); | |
23c88752 MD |
1197 | |
1198 | static __init int relay_init(void) | |
1199 | { | |
1200 | ||
1201 | hotcpu_notifier(relay_hotcpu_callback, 0); | |
1202 | return 0; | |
1203 | } | |
1204 | ||
1205 | module_init(relay_init); |