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