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 | /* | |
40 | * nopage() vm_op implementation for relay file mapping. | |
41 | */ | |
42 | static 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 | */ | |
69 | static 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 | */ | |
01c55ed3 | 83 | static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) |
b86ff981 JA |
84 | { |
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; | |
2f98735c | 95 | vma->vm_flags |= VM_DONTEXPAND; |
b86ff981 JA |
96 | 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 | * | |
4c78a663 | 107 | * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The |
221415d7 | 108 | * passed in size will get page aligned, if it isn't already. |
b86ff981 | 109 | */ |
221415d7 | 110 | static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) |
b86ff981 JA |
111 | { |
112 | void *mem; | |
113 | unsigned int i, j, n_pages; | |
114 | ||
221415d7 JA |
115 | *size = PAGE_ALIGN(*size); |
116 | n_pages = *size >> PAGE_SHIFT; | |
b86ff981 JA |
117 | |
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; | |
ebf99093 | 126 | set_page_private(buf->page_array[i], (unsigned long)buf); |
b86ff981 JA |
127 | } |
128 | mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); | |
129 | if (!mem) | |
130 | goto depopulate; | |
131 | ||
221415d7 | 132 | memset(mem, 0, *size); |
b86ff981 JA |
133 | buf->page_count = n_pages; |
134 | return mem; | |
135 | ||
136 | depopulate: | |
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 | |
4c78a663 | 145 | * @chan: the relay channel |
b86ff981 | 146 | * |
4c78a663 | 147 | * Returns channel buffer if successful, %NULL otherwise. |
b86ff981 | 148 | */ |
01c55ed3 | 149 | static struct rchan_buf *relay_create_buf(struct rchan *chan) |
b86ff981 | 150 | { |
cd861280 | 151 | struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); |
b86ff981 JA |
152 | 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 | ||
221415d7 | 159 | buf->start = relay_alloc_buf(buf, &chan->alloc_size); |
b86ff981 JA |
160 | if (!buf->start) |
161 | goto free_buf; | |
162 | ||
163 | buf->chan = chan; | |
164 | kref_get(&buf->chan->kref); | |
165 | return buf; | |
166 | ||
167 | free_buf: | |
168 | kfree(buf->padding); | |
169 | kfree(buf); | |
170 | return NULL; | |
171 | } | |
172 | ||
173 | /** | |
174 | * relay_destroy_channel - free the channel struct | |
4c78a663 | 175 | * @kref: target kernel reference that contains the relay channel |
b86ff981 JA |
176 | * |
177 | * Should only be called from kref_put(). | |
178 | */ | |
01c55ed3 | 179 | static void relay_destroy_channel(struct kref *kref) |
b86ff981 JA |
180 | { |
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 | */ | |
01c55ed3 | 189 | static void relay_destroy_buf(struct rchan_buf *buf) |
b86ff981 JA |
190 | { |
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 | } | |
23c88752 | 200 | chan->buf[buf->cpu] = NULL; |
b86ff981 JA |
201 | 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 | |
4c78a663 | 208 | * @kref: target kernel reference that contains the relay buffer |
b86ff981 JA |
209 | * |
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 | */ | |
01c55ed3 | 214 | static void relay_remove_buf(struct kref *kref) |
b86ff981 JA |
215 | { |
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 | */ | |
01c55ed3 | 227 | static int relay_buf_empty(struct rchan_buf *buf) |
b86ff981 JA |
228 | { |
229 | return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; | |
230 | } | |
b86ff981 JA |
231 | |
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 | */ | |
238 | int 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 | } | |
243 | EXPORT_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 | */ | |
257 | static 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 | */ | |
271 | static 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 | */ | |
279 | static 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 | */ | |
287 | static 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 | */ | |
299 | static int remove_buf_file_default_callback(struct dentry *dentry) | |
300 | { | |
301 | return -EINVAL; | |
302 | } | |
303 | ||
304 | /* relay channel default callbacks */ | |
305 | static 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 | |
9a9136e2 | 315 | * @data: contains the channel buffer |
b86ff981 | 316 | * |
7c9cb383 | 317 | * This is the timer function used to defer reader waking. |
b86ff981 | 318 | */ |
7c9cb383 | 319 | static void wakeup_readers(unsigned long data) |
b86ff981 | 320 | { |
7c9cb383 | 321 | struct rchan_buf *buf = (struct rchan_buf *)data; |
b86ff981 JA |
322 | 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 | * | |
72fd4a35 | 330 | * See relay_reset() for description of effect. |
b86ff981 | 331 | */ |
192636ad | 332 | static void __relay_reset(struct rchan_buf *buf, unsigned int init) |
b86ff981 JA |
333 | { |
334 | size_t i; | |
335 | ||
336 | if (init) { | |
337 | init_waitqueue_head(&buf->read_wait); | |
338 | kref_init(&buf->kref); | |
7c9cb383 TZ |
339 | setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf); |
340 | } else | |
341 | del_timer_sync(&buf->timer); | |
b86ff981 JA |
342 | |
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 | * | |
72fd4a35 | 364 | * NOTE. Care should be taken that the channel isn't actually |
b86ff981 JA |
365 | * being used by anything when this call is made. |
366 | */ | |
367 | void relay_reset(struct rchan *chan) | |
368 | { | |
369 | unsigned int i; | |
b86ff981 JA |
370 | |
371 | if (!chan) | |
372 | return; | |
373 | ||
1bfbc608 | 374 | if (chan->is_global && chan->buf[0]) { |
23c88752 MD |
375 | __relay_reset(chan->buf[0], 0); |
376 | return; | |
b86ff981 | 377 | } |
23c88752 MD |
378 | |
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); | |
b86ff981 JA |
384 | } |
385 | EXPORT_SYMBOL_GPL(relay_reset); | |
386 | ||
4c78a663 | 387 | /* |
b86ff981 JA |
388 | * relay_open_buf - create a new relay channel buffer |
389 | * | |
23c88752 | 390 | * used by relay_open() and CPU hotplug. |
b86ff981 | 391 | */ |
23c88752 | 392 | static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) |
b86ff981 | 393 | { |
23c88752 | 394 | struct rchan_buf *buf = NULL; |
b86ff981 | 395 | struct dentry *dentry; |
23c88752 | 396 | char *tmpname; |
b86ff981 | 397 | |
23c88752 | 398 | if (chan->is_global) |
b86ff981 JA |
399 | return chan->buf[0]; |
400 | ||
23c88752 MD |
401 | 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 | ||
b86ff981 JA |
406 | buf = relay_create_buf(chan); |
407 | if (!buf) | |
23c88752 MD |
408 | goto free_name; |
409 | ||
410 | buf->cpu = cpu; | |
411 | __relay_reset(buf, 1); | |
b86ff981 JA |
412 | |
413 | /* Create file in fs */ | |
23c88752 MD |
414 | dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR, |
415 | buf, &chan->is_global); | |
416 | if (!dentry) | |
417 | goto free_buf; | |
b86ff981 JA |
418 | |
419 | buf->dentry = dentry; | |
b86ff981 | 420 | |
23c88752 MD |
421 | if(chan->is_global) { |
422 | chan->buf[0] = buf; | |
423 | buf->cpu = 0; | |
424 | } | |
425 | ||
426 | goto free_name; | |
427 | ||
428 | free_buf: | |
429 | relay_destroy_buf(buf); | |
c9b3febc | 430 | buf = NULL; |
23c88752 MD |
431 | free_name: |
432 | kfree(tmpname); | |
433 | end: | |
b86ff981 JA |
434 | 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 | */ | |
192636ad | 445 | static void relay_close_buf(struct rchan_buf *buf) |
b86ff981 JA |
446 | { |
447 | buf->finalized = 1; | |
7c9cb383 | 448 | del_timer_sync(&buf->timer); |
b86ff981 JA |
449 | kref_put(&buf->kref, relay_remove_buf); |
450 | } | |
451 | ||
192636ad | 452 | static void setup_callbacks(struct rchan *chan, |
b86ff981 JA |
453 | 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 | ||
23c88752 | 473 | /** |
23c88752 MD |
474 | * relay_hotcpu_callback - CPU hotplug callback |
475 | * @nb: notifier block | |
476 | * @action: hotplug action to take | |
477 | * @hcpu: CPU number | |
478 | * | |
05fb6bf0 | 479 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) |
23c88752 MD |
480 | */ |
481 | static 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: | |
8bb78442 | 490 | case CPU_UP_PREPARE_FROZEN: |
23c88752 MD |
491 | 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: | |
8bb78442 | 507 | case CPU_DEAD_FROZEN: |
23c88752 MD |
508 | /* No need to flush the cpu : will be flushed upon |
509 | * final relay_flush() call. */ | |
510 | break; | |
511 | } | |
512 | return NOTIFY_OK; | |
513 | } | |
514 | ||
b86ff981 JA |
515 | /** |
516 | * relay_open - create a new relay channel | |
517 | * @base_filename: base name of files to create | |
4c78a663 | 518 | * @parent: dentry of parent directory, %NULL for root directory |
b86ff981 JA |
519 | * @subbuf_size: size of sub-buffers |
520 | * @n_subbufs: number of sub-buffers | |
521 | * @cb: client callback functions | |
23c88752 | 522 | * @private_data: user-defined data |
b86ff981 | 523 | * |
4c78a663 | 524 | * Returns channel pointer if successful, %NULL otherwise. |
b86ff981 JA |
525 | * |
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 | |
72fd4a35 | 529 | * permissions will be %S_IRUSR. |
b86ff981 JA |
530 | */ |
531 | struct rchan *relay_open(const char *base_filename, | |
532 | struct dentry *parent, | |
533 | size_t subbuf_size, | |
534 | size_t n_subbufs, | |
23c88752 MD |
535 | struct rchan_callbacks *cb, |
536 | void *private_data) | |
b86ff981 JA |
537 | { |
538 | unsigned int i; | |
539 | struct rchan *chan; | |
b86ff981 JA |
540 | if (!base_filename) |
541 | return NULL; | |
542 | ||
543 | if (!(subbuf_size && n_subbufs)) | |
544 | return NULL; | |
545 | ||
cd861280 | 546 | chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); |
b86ff981 JA |
547 | 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); | |
23c88752 MD |
554 | chan->parent = parent; |
555 | chan->private_data = private_data; | |
556 | strlcpy(chan->base_filename, base_filename, NAME_MAX); | |
b86ff981 JA |
557 | setup_callbacks(chan, cb); |
558 | kref_init(&chan->kref); | |
559 | ||
23c88752 | 560 | mutex_lock(&relay_channels_mutex); |
b86ff981 | 561 | for_each_online_cpu(i) { |
23c88752 | 562 | chan->buf[i] = relay_open_buf(chan, i); |
b86ff981 JA |
563 | if (!chan->buf[i]) |
564 | goto free_bufs; | |
b86ff981 | 565 | } |
23c88752 MD |
566 | list_add(&chan->list, &relay_channels); |
567 | mutex_unlock(&relay_channels_mutex); | |
b86ff981 | 568 | |
b86ff981 JA |
569 | return chan; |
570 | ||
571 | free_bufs: | |
23c88752 | 572 | for_each_online_cpu(i) { |
b86ff981 JA |
573 | if (!chan->buf[i]) |
574 | break; | |
575 | relay_close_buf(chan->buf[i]); | |
b86ff981 | 576 | } |
b86ff981 | 577 | |
b86ff981 | 578 | kref_put(&chan->kref, relay_destroy_channel); |
23c88752 | 579 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
580 | return NULL; |
581 | } | |
582 | EXPORT_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 | */ | |
594 | size_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++; | |
221415d7 JA |
607 | buf->dentry->d_inode->i_size += buf->chan->subbuf_size - |
608 | buf->padding[old_subbuf]; | |
609 | smp_mb(); | |
7c9cb383 TZ |
610 | 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); | |
b86ff981 JA |
618 | } |
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 | ||
636 | toobig: | |
637 | buf->chan->last_toobig = length; | |
638 | return 0; | |
639 | } | |
640 | EXPORT_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 | * | |
72fd4a35 | 652 | * NOTE. Kernel clients don't need to call this function if the channel |
b86ff981 JA |
653 | * mode is 'overwrite'. |
654 | */ | |
655 | void 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 | } | |
672 | EXPORT_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 | */ | |
680 | void relay_close(struct rchan *chan) | |
681 | { | |
682 | unsigned int i; | |
b86ff981 JA |
683 | |
684 | if (!chan) | |
685 | return; | |
686 | ||
23c88752 MD |
687 | 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]); | |
b86ff981 JA |
694 | |
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 | ||
23c88752 | 700 | list_del(&chan->list); |
b86ff981 | 701 | kref_put(&chan->kref, relay_destroy_channel); |
23c88752 | 702 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
703 | } |
704 | EXPORT_SYMBOL_GPL(relay_close); | |
705 | ||
706 | /** | |
707 | * relay_flush - close the channel | |
708 | * @chan: the channel | |
709 | * | |
4c78a663 | 710 | * Flushes all channel buffers, i.e. forces buffer switch. |
b86ff981 JA |
711 | */ |
712 | void relay_flush(struct rchan *chan) | |
713 | { | |
714 | unsigned int i; | |
b86ff981 JA |
715 | |
716 | if (!chan) | |
717 | return; | |
718 | ||
23c88752 MD |
719 | if (chan->is_global && chan->buf[0]) { |
720 | relay_switch_subbuf(chan->buf[0], 0); | |
721 | return; | |
b86ff981 | 722 | } |
23c88752 MD |
723 | |
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); | |
b86ff981 JA |
729 | } |
730 | EXPORT_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 | */ | |
739 | static int relay_file_open(struct inode *inode, struct file *filp) | |
740 | { | |
8e18e294 | 741 | struct rchan_buf *buf = inode->i_private; |
b86ff981 JA |
742 | 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 | * | |
72fd4a35 | 753 | * Calls upon relay_mmap_buf() to map the file into user space. |
b86ff981 JA |
754 | */ |
755 | static 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 | */ | |
768 | static 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 | */ | |
793 | static 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 | ||
4c78a663 | 801 | /* |
b86ff981 JA |
802 | * relay_file_read_consume - update the consumed count for the buffer |
803 | */ | |
804 | static 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; | |
a66e356c MH |
818 | if (!read_pos) |
819 | read_subbuf = buf->subbufs_consumed % n_subbufs; | |
820 | else | |
821 | read_subbuf = read_pos / buf->chan->subbuf_size; | |
b86ff981 JA |
822 | 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 | ||
4c78a663 | 831 | /* |
b86ff981 JA |
832 | * relay_file_read_avail - boolean, are there unconsumed bytes available? |
833 | */ | |
834 | static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) | |
835 | { | |
b86ff981 JA |
836 | size_t subbuf_size = buf->chan->subbuf_size; |
837 | size_t n_subbufs = buf->chan->n_subbufs; | |
221415d7 JA |
838 | size_t produced = buf->subbufs_produced; |
839 | size_t consumed = buf->subbufs_consumed; | |
b86ff981 | 840 | |
221415d7 | 841 | relay_file_read_consume(buf, read_pos, 0); |
b86ff981 | 842 | |
221415d7 JA |
843 | if (unlikely(buf->offset > subbuf_size)) { |
844 | if (produced == consumed) | |
845 | return 0; | |
846 | return 1; | |
b86ff981 JA |
847 | } |
848 | ||
221415d7 | 849 | if (unlikely(produced - consumed >= n_subbufs)) { |
a66e356c | 850 | consumed = produced - n_subbufs + 1; |
221415d7 | 851 | buf->subbufs_consumed = consumed; |
a66e356c | 852 | buf->bytes_consumed = 0; |
221415d7 | 853 | } |
1bfbc608 | 854 | |
221415d7 JA |
855 | 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; | |
1bfbc608 | 860 | |
221415d7 | 861 | if (consumed == produced) |
b86ff981 JA |
862 | return 0; |
863 | ||
b86ff981 JA |
864 | return 1; |
865 | } | |
866 | ||
867 | /** | |
868 | * relay_file_read_subbuf_avail - return bytes available in sub-buffer | |
4c78a663 RD |
869 | * @read_pos: file read position |
870 | * @buf: relay channel buffer | |
b86ff981 JA |
871 | */ |
872 | static 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 | |
4c78a663 RD |
896 | * @read_pos: file read position |
897 | * @buf: relay channel buffer | |
b86ff981 | 898 | * |
72fd4a35 | 899 | * If the @read_pos is in the middle of padding, return the |
b86ff981 JA |
900 | * position of the first actually available byte, otherwise |
901 | * return the original value. | |
902 | */ | |
903 | static 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; | |
8d62fdeb | 909 | size_t consumed = buf->subbufs_consumed % n_subbufs; |
b86ff981 | 910 | |
8d62fdeb DW |
911 | if (!read_pos) |
912 | read_pos = consumed * subbuf_size + buf->bytes_consumed; | |
b86ff981 JA |
913 | 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 | |
4c78a663 RD |
927 | * @read_pos: file read position |
928 | * @buf: relay channel buffer | |
929 | * @count: number of bytes to be read | |
b86ff981 JA |
930 | */ |
931 | static 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 | ||
4c78a663 | 951 | /* |
6dac40a7 | 952 | * subbuf_read_actor - read up to one subbuf's worth of data |
b86ff981 | 953 | */ |
6dac40a7 TZ |
954 | static 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) | |
b86ff981 | 959 | { |
b86ff981 | 960 | void *from; |
6dac40a7 | 961 | int ret = 0; |
b86ff981 JA |
962 | |
963 | from = buf->start + read_start; | |
6dac40a7 | 964 | ret = avail; |
ba2397ef | 965 | if (copy_to_user(desc->arg.buf, from, avail)) { |
6dac40a7 TZ |
966 | desc->error = -EFAULT; |
967 | ret = 0; | |
b86ff981 | 968 | } |
6dac40a7 TZ |
969 | desc->arg.data += ret; |
970 | desc->written += ret; | |
971 | desc->count -= ret; | |
972 | ||
b86ff981 JA |
973 | return ret; |
974 | } | |
975 | ||
6dac40a7 TZ |
976 | typedef 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 | ||
4c78a663 | 982 | /* |
6dac40a7 TZ |
983 | * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries |
984 | */ | |
192636ad AM |
985 | static 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) | |
221415d7 | 989 | { |
6dac40a7 TZ |
990 | struct rchan_buf *buf = filp->private_data; |
991 | size_t read_start, avail; | |
6dac40a7 | 992 | int ret; |
221415d7 | 993 | |
ba2397ef | 994 | if (!desc->count) |
221415d7 JA |
995 | return 0; |
996 | ||
f3a43f3f | 997 | mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); |
221415d7 | 998 | do { |
6dac40a7 TZ |
999 | if (!relay_file_read_avail(buf, *ppos)) |
1000 | break; | |
1001 | ||
1002 | read_start = relay_file_read_start_pos(*ppos, buf); | |
1003 | avail = relay_file_read_subbuf_avail(read_start, buf); | |
1004 | if (!avail) | |
221415d7 | 1005 | break; |
221415d7 | 1006 | |
ba2397ef AV |
1007 | avail = min(desc->count, avail); |
1008 | ret = subbuf_actor(read_start, buf, avail, desc, actor); | |
1009 | if (desc->error < 0) | |
6dac40a7 TZ |
1010 | 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); | |
1015 | } | |
ba2397ef | 1016 | } while (desc->count && ret); |
f3a43f3f | 1017 | mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); |
6dac40a7 | 1018 | |
ba2397ef | 1019 | return desc->written; |
6dac40a7 TZ |
1020 | } |
1021 | ||
1022 | static ssize_t relay_file_read(struct file *filp, | |
1023 | char __user *buffer, | |
1024 | size_t count, | |
1025 | loff_t *ppos) | |
1026 | { | |
ba2397ef AV |
1027 | 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); | |
6dac40a7 TZ |
1034 | } |
1035 | ||
1db60cf2 JA |
1036 | static 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 | ||
ebf99093 TZ |
1046 | static void relay_pipe_buf_release(struct pipe_inode_info *pipe, |
1047 | struct pipe_buffer *buf) | |
6dac40a7 | 1048 | { |
ebf99093 TZ |
1049 | struct rchan_buf *rbuf; |
1050 | ||
1051 | rbuf = (struct rchan_buf *)page_private(buf->page); | |
1db60cf2 | 1052 | relay_consume_bytes(rbuf, buf->private); |
ebf99093 TZ |
1053 | } |
1054 | ||
1055 | static struct pipe_buf_operations relay_pipe_buf_ops = { | |
1056 | .can_merge = 0, | |
1057 | .map = generic_pipe_buf_map, | |
1058 | .unmap = generic_pipe_buf_unmap, | |
cac36bb0 | 1059 | .confirm = generic_pipe_buf_confirm, |
ebf99093 TZ |
1060 | .release = relay_pipe_buf_release, |
1061 | .steal = generic_pipe_buf_steal, | |
1062 | .get = generic_pipe_buf_get, | |
1063 | }; | |
1064 | ||
d3f35d98 | 1065 | /* |
ebf99093 TZ |
1066 | * subbuf_splice_actor - splice up to one subbuf's worth of data |
1067 | */ | |
1068 | static 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 | { | |
1db60cf2 | 1075 | unsigned int pidx, poff, total_len, subbuf_pages, ret; |
ebf99093 TZ |
1076 | struct rchan_buf *rbuf = in->private_data; |
1077 | unsigned int subbuf_size = rbuf->chan->subbuf_size; | |
24da24de TZ |
1078 | 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); | |
ebf99093 TZ |
1081 | 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; | |
1db60cf2 JA |
1084 | 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 | }; | |
ebf99093 TZ |
1093 | |
1094 | if (rbuf->subbufs_produced == rbuf->subbufs_consumed) | |
1095 | return 0; | |
1096 | ||
1db60cf2 JA |
1097 | /* |
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; | |
ebf99093 TZ |
1102 | |
1103 | subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; | |
1104 | pidx = (read_start / PAGE_SIZE) % subbuf_pages; | |
1105 | poff = read_start & ~PAGE_MASK; | |
1106 | ||
1db60cf2 JA |
1107 | 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; | |
ebf99093 | 1110 | |
1db60cf2 | 1111 | if (!len) |
ebf99093 | 1112 | break; |
ebf99093 | 1113 | |
1db60cf2 JA |
1114 | this_len = min_t(unsigned long, len, PAGE_SIZE - poff); |
1115 | private = this_len; | |
ebf99093 | 1116 | |
1db60cf2 JA |
1117 | spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; |
1118 | spd.partial[spd.nr_pages].offset = poff; | |
ebf99093 | 1119 | |
1db60cf2 JA |
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; | |
ebf99093 | 1124 | } |
1db60cf2 JA |
1125 | spd.partial[spd.nr_pages].len = this_len; |
1126 | spd.partial[spd.nr_pages].private = private; | |
ebf99093 | 1127 | |
1db60cf2 JA |
1128 | len -= this_len; |
1129 | total_len += this_len; | |
1130 | poff = 0; | |
1131 | pidx = (pidx + 1) % subbuf_pages; | |
ebf99093 | 1132 | |
1db60cf2 JA |
1133 | if (this_end >= nonpad_end) { |
1134 | spd.nr_pages++; | |
ebf99093 TZ |
1135 | break; |
1136 | } | |
ebf99093 TZ |
1137 | } |
1138 | ||
1db60cf2 JA |
1139 | if (!spd.nr_pages) |
1140 | return 0; | |
ebf99093 | 1141 | |
1db60cf2 JA |
1142 | ret = *nonpad_ret = splice_to_pipe(pipe, &spd); |
1143 | if (ret < 0 || ret < total_len) | |
1144 | return ret; | |
ebf99093 | 1145 | |
1db60cf2 JA |
1146 | if (read_start + ret == nonpad_end) |
1147 | ret += padding; | |
1148 | ||
1149 | return ret; | |
ebf99093 TZ |
1150 | } |
1151 | ||
1152 | static 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) { | |
ebf99093 TZ |
1170 | 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; | |
221415d7 JA |
1191 | } |
1192 | ||
15ad7cdc | 1193 | const struct file_operations relay_file_operations = { |
b86ff981 JA |
1194 | .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, | |
ebf99093 | 1200 | .splice_read = relay_file_splice_read, |
b86ff981 JA |
1201 | }; |
1202 | EXPORT_SYMBOL_GPL(relay_file_operations); | |
23c88752 MD |
1203 | |
1204 | static __init int relay_init(void) | |
1205 | { | |
1206 | ||
1207 | hotcpu_notifier(relay_hotcpu_callback, 0); | |
1208 | return 0; | |
1209 | } | |
1210 | ||
1211 | module_init(relay_init); |