1b86ff981SJens Axboe /* 2b86ff981SJens Axboe * Public API and common code for kernel->userspace relay file support. 3b86ff981SJens Axboe * 4c9b3febcSJesper Juhl * See Documentation/filesystems/relay.txt for an overview. 5b86ff981SJens Axboe * 6b86ff981SJens Axboe * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp 7b86ff981SJens Axboe * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com) 8b86ff981SJens Axboe * 9b86ff981SJens Axboe * Moved to kernel/relay.c by Paul Mundt, 2006. 1023c88752SMathieu Desnoyers * November 2006 - CPU hotplug support by Mathieu Desnoyers 1123c88752SMathieu Desnoyers * (mathieu.desnoyers@polymtl.ca) 12b86ff981SJens Axboe * 13b86ff981SJens Axboe * This file is released under the GPL. 14b86ff981SJens Axboe */ 15b86ff981SJens Axboe #include <linux/errno.h> 16b86ff981SJens Axboe #include <linux/stddef.h> 17b86ff981SJens Axboe #include <linux/slab.h> 18b86ff981SJens Axboe #include <linux/module.h> 19b86ff981SJens Axboe #include <linux/string.h> 20b86ff981SJens Axboe #include <linux/relay.h> 21b86ff981SJens Axboe #include <linux/vmalloc.h> 22b86ff981SJens Axboe #include <linux/mm.h> 2323c88752SMathieu Desnoyers #include <linux/cpu.h> 24d6b29d7cSJens Axboe #include <linux/splice.h> 2523c88752SMathieu Desnoyers 2623c88752SMathieu Desnoyers /* list of open channels, for cpu hotplug */ 2723c88752SMathieu Desnoyers static DEFINE_MUTEX(relay_channels_mutex); 2823c88752SMathieu Desnoyers static LIST_HEAD(relay_channels); 29b86ff981SJens Axboe 30b86ff981SJens Axboe /* 31b86ff981SJens Axboe * close() vm_op implementation for relay file mapping. 32b86ff981SJens Axboe */ 33b86ff981SJens Axboe static void relay_file_mmap_close(struct vm_area_struct *vma) 34b86ff981SJens Axboe { 35b86ff981SJens Axboe struct rchan_buf *buf = vma->vm_private_data; 36b86ff981SJens Axboe buf->chan->cb->buf_unmapped(buf, vma->vm_file); 37b86ff981SJens Axboe } 38b86ff981SJens Axboe 39b86ff981SJens Axboe /* 40a1e09612SNick Piggin * fault() vm_op implementation for relay file mapping. 41b86ff981SJens Axboe */ 42a1e09612SNick Piggin static int relay_buf_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 43b86ff981SJens Axboe { 44b86ff981SJens Axboe struct page *page; 45b86ff981SJens Axboe struct rchan_buf *buf = vma->vm_private_data; 46a1e09612SNick Piggin pgoff_t pgoff = vmf->pgoff; 47b86ff981SJens Axboe 48b86ff981SJens Axboe if (!buf) 49a1e09612SNick Piggin return VM_FAULT_OOM; 50b86ff981SJens Axboe 51a1e09612SNick Piggin page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT)); 52b86ff981SJens Axboe if (!page) 53a1e09612SNick Piggin return VM_FAULT_SIGBUS; 54b86ff981SJens Axboe get_page(page); 55a1e09612SNick Piggin vmf->page = page; 56b86ff981SJens Axboe 57a1e09612SNick Piggin return 0; 58b86ff981SJens Axboe } 59b86ff981SJens Axboe 60b86ff981SJens Axboe /* 61b86ff981SJens Axboe * vm_ops for relay file mappings. 62b86ff981SJens Axboe */ 63b86ff981SJens Axboe static struct vm_operations_struct relay_file_mmap_ops = { 64a1e09612SNick Piggin .fault = relay_buf_fault, 65b86ff981SJens Axboe .close = relay_file_mmap_close, 66b86ff981SJens Axboe }; 67b86ff981SJens Axboe 6868ab3d88SMasami Hiramatsu /* 6968ab3d88SMasami Hiramatsu * allocate an array of pointers of struct page 7068ab3d88SMasami Hiramatsu */ 7168ab3d88SMasami Hiramatsu static struct page **relay_alloc_page_array(unsigned int n_pages) 7268ab3d88SMasami Hiramatsu { 7368ab3d88SMasami Hiramatsu struct page **array; 7468ab3d88SMasami Hiramatsu size_t pa_size = n_pages * sizeof(struct page *); 7568ab3d88SMasami Hiramatsu 7668ab3d88SMasami Hiramatsu if (pa_size > PAGE_SIZE) { 7768ab3d88SMasami Hiramatsu array = vmalloc(pa_size); 7868ab3d88SMasami Hiramatsu if (array) 7968ab3d88SMasami Hiramatsu memset(array, 0, pa_size); 8068ab3d88SMasami Hiramatsu } else { 8168ab3d88SMasami Hiramatsu array = kzalloc(pa_size, GFP_KERNEL); 8268ab3d88SMasami Hiramatsu } 8368ab3d88SMasami Hiramatsu return array; 8468ab3d88SMasami Hiramatsu } 8568ab3d88SMasami Hiramatsu 8668ab3d88SMasami Hiramatsu /* 8768ab3d88SMasami Hiramatsu * free an array of pointers of struct page 8868ab3d88SMasami Hiramatsu */ 8968ab3d88SMasami Hiramatsu static void relay_free_page_array(struct page **array) 9068ab3d88SMasami Hiramatsu { 9168ab3d88SMasami Hiramatsu if (is_vmalloc_addr(array)) 9268ab3d88SMasami Hiramatsu vfree(array); 9368ab3d88SMasami Hiramatsu else 9468ab3d88SMasami Hiramatsu kfree(array); 9568ab3d88SMasami Hiramatsu } 9668ab3d88SMasami Hiramatsu 97b86ff981SJens Axboe /** 98b86ff981SJens Axboe * relay_mmap_buf: - mmap channel buffer to process address space 99b86ff981SJens Axboe * @buf: relay channel buffer 100b86ff981SJens Axboe * @vma: vm_area_struct describing memory to be mapped 101b86ff981SJens Axboe * 102b86ff981SJens Axboe * Returns 0 if ok, negative on error 103b86ff981SJens Axboe * 104b86ff981SJens Axboe * Caller should already have grabbed mmap_sem. 105b86ff981SJens Axboe */ 10601c55ed3SAdrian Bunk static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) 107b86ff981SJens Axboe { 108b86ff981SJens Axboe unsigned long length = vma->vm_end - vma->vm_start; 109b86ff981SJens Axboe struct file *filp = vma->vm_file; 110b86ff981SJens Axboe 111b86ff981SJens Axboe if (!buf) 112b86ff981SJens Axboe return -EBADF; 113b86ff981SJens Axboe 114b86ff981SJens Axboe if (length != (unsigned long)buf->chan->alloc_size) 115b86ff981SJens Axboe return -EINVAL; 116b86ff981SJens Axboe 117b86ff981SJens Axboe vma->vm_ops = &relay_file_mmap_ops; 1182f98735cSNick Piggin vma->vm_flags |= VM_DONTEXPAND; 119b86ff981SJens Axboe vma->vm_private_data = buf; 120b86ff981SJens Axboe buf->chan->cb->buf_mapped(buf, filp); 121b86ff981SJens Axboe 122b86ff981SJens Axboe return 0; 123b86ff981SJens Axboe } 124b86ff981SJens Axboe 125b86ff981SJens Axboe /** 126b86ff981SJens Axboe * relay_alloc_buf - allocate a channel buffer 127b86ff981SJens Axboe * @buf: the buffer struct 128b86ff981SJens Axboe * @size: total size of the buffer 129b86ff981SJens Axboe * 1304c78a663SRandy Dunlap * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The 131221415d7SJens Axboe * passed in size will get page aligned, if it isn't already. 132b86ff981SJens Axboe */ 133221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) 134b86ff981SJens Axboe { 135b86ff981SJens Axboe void *mem; 136b86ff981SJens Axboe unsigned int i, j, n_pages; 137b86ff981SJens Axboe 138221415d7SJens Axboe *size = PAGE_ALIGN(*size); 139221415d7SJens Axboe n_pages = *size >> PAGE_SHIFT; 140b86ff981SJens Axboe 14168ab3d88SMasami Hiramatsu buf->page_array = relay_alloc_page_array(n_pages); 142b86ff981SJens Axboe if (!buf->page_array) 143b86ff981SJens Axboe return NULL; 144b86ff981SJens Axboe 145b86ff981SJens Axboe for (i = 0; i < n_pages; i++) { 146b86ff981SJens Axboe buf->page_array[i] = alloc_page(GFP_KERNEL); 147b86ff981SJens Axboe if (unlikely(!buf->page_array[i])) 148b86ff981SJens Axboe goto depopulate; 149ebf99093STom Zanussi set_page_private(buf->page_array[i], (unsigned long)buf); 150b86ff981SJens Axboe } 151b86ff981SJens Axboe mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); 152b86ff981SJens Axboe if (!mem) 153b86ff981SJens Axboe goto depopulate; 154b86ff981SJens Axboe 155221415d7SJens Axboe memset(mem, 0, *size); 156b86ff981SJens Axboe buf->page_count = n_pages; 157b86ff981SJens Axboe return mem; 158b86ff981SJens Axboe 159b86ff981SJens Axboe depopulate: 160b86ff981SJens Axboe for (j = 0; j < i; j++) 161b86ff981SJens Axboe __free_page(buf->page_array[j]); 16268ab3d88SMasami Hiramatsu relay_free_page_array(buf->page_array); 163b86ff981SJens Axboe return NULL; 164b86ff981SJens Axboe } 165b86ff981SJens Axboe 166b86ff981SJens Axboe /** 167b86ff981SJens Axboe * relay_create_buf - allocate and initialize a channel buffer 1684c78a663SRandy Dunlap * @chan: the relay channel 169b86ff981SJens Axboe * 1704c78a663SRandy Dunlap * Returns channel buffer if successful, %NULL otherwise. 171b86ff981SJens Axboe */ 17201c55ed3SAdrian Bunk static struct rchan_buf *relay_create_buf(struct rchan *chan) 173b86ff981SJens Axboe { 174cd861280SRobert P. J. Day struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); 175b86ff981SJens Axboe if (!buf) 176b86ff981SJens Axboe return NULL; 177b86ff981SJens Axboe 178b86ff981SJens Axboe buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL); 179b86ff981SJens Axboe if (!buf->padding) 180b86ff981SJens Axboe goto free_buf; 181b86ff981SJens Axboe 182221415d7SJens Axboe buf->start = relay_alloc_buf(buf, &chan->alloc_size); 183b86ff981SJens Axboe if (!buf->start) 184b86ff981SJens Axboe goto free_buf; 185b86ff981SJens Axboe 186b86ff981SJens Axboe buf->chan = chan; 187b86ff981SJens Axboe kref_get(&buf->chan->kref); 188b86ff981SJens Axboe return buf; 189b86ff981SJens Axboe 190b86ff981SJens Axboe free_buf: 191b86ff981SJens Axboe kfree(buf->padding); 192b86ff981SJens Axboe kfree(buf); 193b86ff981SJens Axboe return NULL; 194b86ff981SJens Axboe } 195b86ff981SJens Axboe 196b86ff981SJens Axboe /** 197b86ff981SJens Axboe * relay_destroy_channel - free the channel struct 1984c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay channel 199b86ff981SJens Axboe * 200b86ff981SJens Axboe * Should only be called from kref_put(). 201b86ff981SJens Axboe */ 20201c55ed3SAdrian Bunk static void relay_destroy_channel(struct kref *kref) 203b86ff981SJens Axboe { 204b86ff981SJens Axboe struct rchan *chan = container_of(kref, struct rchan, kref); 205b86ff981SJens Axboe kfree(chan); 206b86ff981SJens Axboe } 207b86ff981SJens Axboe 208b86ff981SJens Axboe /** 209b86ff981SJens Axboe * relay_destroy_buf - destroy an rchan_buf struct and associated buffer 210b86ff981SJens Axboe * @buf: the buffer struct 211b86ff981SJens Axboe */ 21201c55ed3SAdrian Bunk static void relay_destroy_buf(struct rchan_buf *buf) 213b86ff981SJens Axboe { 214b86ff981SJens Axboe struct rchan *chan = buf->chan; 215b86ff981SJens Axboe unsigned int i; 216b86ff981SJens Axboe 217b86ff981SJens Axboe if (likely(buf->start)) { 218b86ff981SJens Axboe vunmap(buf->start); 219b86ff981SJens Axboe for (i = 0; i < buf->page_count; i++) 220b86ff981SJens Axboe __free_page(buf->page_array[i]); 22168ab3d88SMasami Hiramatsu relay_free_page_array(buf->page_array); 222b86ff981SJens Axboe } 22323c88752SMathieu Desnoyers chan->buf[buf->cpu] = NULL; 224b86ff981SJens Axboe kfree(buf->padding); 225b86ff981SJens Axboe kfree(buf); 226b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 227b86ff981SJens Axboe } 228b86ff981SJens Axboe 229b86ff981SJens Axboe /** 230b86ff981SJens Axboe * relay_remove_buf - remove a channel buffer 2314c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay buffer 232b86ff981SJens Axboe * 233b86ff981SJens Axboe * Removes the file from the fileystem, which also frees the 234b86ff981SJens Axboe * rchan_buf_struct and the channel buffer. Should only be called from 235b86ff981SJens Axboe * kref_put(). 236b86ff981SJens Axboe */ 23701c55ed3SAdrian Bunk static void relay_remove_buf(struct kref *kref) 238b86ff981SJens Axboe { 239b86ff981SJens Axboe struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); 240b86ff981SJens Axboe buf->chan->cb->remove_buf_file(buf->dentry); 241b86ff981SJens Axboe relay_destroy_buf(buf); 242b86ff981SJens Axboe } 243b86ff981SJens Axboe 244b86ff981SJens Axboe /** 245b86ff981SJens Axboe * relay_buf_empty - boolean, is the channel buffer empty? 246b86ff981SJens Axboe * @buf: channel buffer 247b86ff981SJens Axboe * 248b86ff981SJens Axboe * Returns 1 if the buffer is empty, 0 otherwise. 249b86ff981SJens Axboe */ 25001c55ed3SAdrian Bunk static int relay_buf_empty(struct rchan_buf *buf) 251b86ff981SJens Axboe { 252b86ff981SJens Axboe return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; 253b86ff981SJens Axboe } 254b86ff981SJens Axboe 255b86ff981SJens Axboe /** 256b86ff981SJens Axboe * relay_buf_full - boolean, is the channel buffer full? 257b86ff981SJens Axboe * @buf: channel buffer 258b86ff981SJens Axboe * 259b86ff981SJens Axboe * Returns 1 if the buffer is full, 0 otherwise. 260b86ff981SJens Axboe */ 261b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf) 262b86ff981SJens Axboe { 263b86ff981SJens Axboe size_t ready = buf->subbufs_produced - buf->subbufs_consumed; 264b86ff981SJens Axboe return (ready >= buf->chan->n_subbufs) ? 1 : 0; 265b86ff981SJens Axboe } 266b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full); 267b86ff981SJens Axboe 268b86ff981SJens Axboe /* 269b86ff981SJens Axboe * High-level relay kernel API and associated functions. 270b86ff981SJens Axboe */ 271b86ff981SJens Axboe 272b86ff981SJens Axboe /* 273b86ff981SJens Axboe * rchan_callback implementations defining default channel behavior. Used 274b86ff981SJens Axboe * in place of corresponding NULL values in client callback struct. 275b86ff981SJens Axboe */ 276b86ff981SJens Axboe 277b86ff981SJens Axboe /* 278b86ff981SJens Axboe * subbuf_start() default callback. Does nothing. 279b86ff981SJens Axboe */ 280b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf, 281b86ff981SJens Axboe void *subbuf, 282b86ff981SJens Axboe void *prev_subbuf, 283b86ff981SJens Axboe size_t prev_padding) 284b86ff981SJens Axboe { 285b86ff981SJens Axboe if (relay_buf_full(buf)) 286b86ff981SJens Axboe return 0; 287b86ff981SJens Axboe 288b86ff981SJens Axboe return 1; 289b86ff981SJens Axboe } 290b86ff981SJens Axboe 291b86ff981SJens Axboe /* 292b86ff981SJens Axboe * buf_mapped() default callback. Does nothing. 293b86ff981SJens Axboe */ 294b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf, 295b86ff981SJens Axboe struct file *filp) 296b86ff981SJens Axboe { 297b86ff981SJens Axboe } 298b86ff981SJens Axboe 299b86ff981SJens Axboe /* 300b86ff981SJens Axboe * buf_unmapped() default callback. Does nothing. 301b86ff981SJens Axboe */ 302b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf, 303b86ff981SJens Axboe struct file *filp) 304b86ff981SJens Axboe { 305b86ff981SJens Axboe } 306b86ff981SJens Axboe 307b86ff981SJens Axboe /* 308b86ff981SJens Axboe * create_buf_file_create() default callback. Does nothing. 309b86ff981SJens Axboe */ 310b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename, 311b86ff981SJens Axboe struct dentry *parent, 312b86ff981SJens Axboe int mode, 313b86ff981SJens Axboe struct rchan_buf *buf, 314b86ff981SJens Axboe int *is_global) 315b86ff981SJens Axboe { 316b86ff981SJens Axboe return NULL; 317b86ff981SJens Axboe } 318b86ff981SJens Axboe 319b86ff981SJens Axboe /* 320b86ff981SJens Axboe * remove_buf_file() default callback. Does nothing. 321b86ff981SJens Axboe */ 322b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry) 323b86ff981SJens Axboe { 324b86ff981SJens Axboe return -EINVAL; 325b86ff981SJens Axboe } 326b86ff981SJens Axboe 327b86ff981SJens Axboe /* relay channel default callbacks */ 328b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = { 329b86ff981SJens Axboe .subbuf_start = subbuf_start_default_callback, 330b86ff981SJens Axboe .buf_mapped = buf_mapped_default_callback, 331b86ff981SJens Axboe .buf_unmapped = buf_unmapped_default_callback, 332b86ff981SJens Axboe .create_buf_file = create_buf_file_default_callback, 333b86ff981SJens Axboe .remove_buf_file = remove_buf_file_default_callback, 334b86ff981SJens Axboe }; 335b86ff981SJens Axboe 336b86ff981SJens Axboe /** 337b86ff981SJens Axboe * wakeup_readers - wake up readers waiting on a channel 3389a9136e2SLinus Torvalds * @data: contains the channel buffer 339b86ff981SJens Axboe * 3407c9cb383STom Zanussi * This is the timer function used to defer reader waking. 341b86ff981SJens Axboe */ 3427c9cb383STom Zanussi static void wakeup_readers(unsigned long data) 343b86ff981SJens Axboe { 3447c9cb383STom Zanussi struct rchan_buf *buf = (struct rchan_buf *)data; 345b86ff981SJens Axboe wake_up_interruptible(&buf->read_wait); 346b86ff981SJens Axboe } 347b86ff981SJens Axboe 348b86ff981SJens Axboe /** 349b86ff981SJens Axboe * __relay_reset - reset a channel buffer 350b86ff981SJens Axboe * @buf: the channel buffer 351b86ff981SJens Axboe * @init: 1 if this is a first-time initialization 352b86ff981SJens Axboe * 35372fd4a35SRobert P. J. Day * See relay_reset() for description of effect. 354b86ff981SJens Axboe */ 355192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init) 356b86ff981SJens Axboe { 357b86ff981SJens Axboe size_t i; 358b86ff981SJens Axboe 359b86ff981SJens Axboe if (init) { 360b86ff981SJens Axboe init_waitqueue_head(&buf->read_wait); 361b86ff981SJens Axboe kref_init(&buf->kref); 3627c9cb383STom Zanussi setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf); 3637c9cb383STom Zanussi } else 3647c9cb383STom Zanussi del_timer_sync(&buf->timer); 365b86ff981SJens Axboe 366b86ff981SJens Axboe buf->subbufs_produced = 0; 367b86ff981SJens Axboe buf->subbufs_consumed = 0; 368b86ff981SJens Axboe buf->bytes_consumed = 0; 369b86ff981SJens Axboe buf->finalized = 0; 370b86ff981SJens Axboe buf->data = buf->start; 371b86ff981SJens Axboe buf->offset = 0; 372b86ff981SJens Axboe 373b86ff981SJens Axboe for (i = 0; i < buf->chan->n_subbufs; i++) 374b86ff981SJens Axboe buf->padding[i] = 0; 375b86ff981SJens Axboe 376b86ff981SJens Axboe buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); 377b86ff981SJens Axboe } 378b86ff981SJens Axboe 379b86ff981SJens Axboe /** 380b86ff981SJens Axboe * relay_reset - reset the channel 381b86ff981SJens Axboe * @chan: the channel 382b86ff981SJens Axboe * 383b86ff981SJens Axboe * This has the effect of erasing all data from all channel buffers 384b86ff981SJens Axboe * and restarting the channel in its initial state. The buffers 385b86ff981SJens Axboe * are not freed, so any mappings are still in effect. 386b86ff981SJens Axboe * 38772fd4a35SRobert P. J. Day * NOTE. Care should be taken that the channel isn't actually 388b86ff981SJens Axboe * being used by anything when this call is made. 389b86ff981SJens Axboe */ 390b86ff981SJens Axboe void relay_reset(struct rchan *chan) 391b86ff981SJens Axboe { 392b86ff981SJens Axboe unsigned int i; 393b86ff981SJens Axboe 394b86ff981SJens Axboe if (!chan) 395b86ff981SJens Axboe return; 396b86ff981SJens Axboe 39723c88752SMathieu Desnoyers if (chan->is_global && chan->buf[0]) { 39823c88752SMathieu Desnoyers __relay_reset(chan->buf[0], 0); 39923c88752SMathieu Desnoyers return; 400b86ff981SJens Axboe } 40123c88752SMathieu Desnoyers 40223c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 40323c88752SMathieu Desnoyers for_each_online_cpu(i) 40423c88752SMathieu Desnoyers if (chan->buf[i]) 40523c88752SMathieu Desnoyers __relay_reset(chan->buf[i], 0); 40623c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 407b86ff981SJens Axboe } 408b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset); 409b86ff981SJens Axboe 410*20d8b67cSEduard - Gabriel Munteanu static inline void relay_set_buf_dentry(struct rchan_buf *buf, 411*20d8b67cSEduard - Gabriel Munteanu struct dentry *dentry) 412*20d8b67cSEduard - Gabriel Munteanu { 413*20d8b67cSEduard - Gabriel Munteanu buf->dentry = dentry; 414*20d8b67cSEduard - Gabriel Munteanu buf->dentry->d_inode->i_size = buf->early_bytes; 415*20d8b67cSEduard - Gabriel Munteanu } 416*20d8b67cSEduard - Gabriel Munteanu 417*20d8b67cSEduard - Gabriel Munteanu static struct dentry *relay_create_buf_file(struct rchan *chan, 418*20d8b67cSEduard - Gabriel Munteanu struct rchan_buf *buf, 419*20d8b67cSEduard - Gabriel Munteanu unsigned int cpu) 420*20d8b67cSEduard - Gabriel Munteanu { 421*20d8b67cSEduard - Gabriel Munteanu struct dentry *dentry; 422*20d8b67cSEduard - Gabriel Munteanu char *tmpname; 423*20d8b67cSEduard - Gabriel Munteanu 424*20d8b67cSEduard - Gabriel Munteanu tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); 425*20d8b67cSEduard - Gabriel Munteanu if (!tmpname) 426*20d8b67cSEduard - Gabriel Munteanu return NULL; 427*20d8b67cSEduard - Gabriel Munteanu snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu); 428*20d8b67cSEduard - Gabriel Munteanu 429*20d8b67cSEduard - Gabriel Munteanu /* Create file in fs */ 430*20d8b67cSEduard - Gabriel Munteanu dentry = chan->cb->create_buf_file(tmpname, chan->parent, 431*20d8b67cSEduard - Gabriel Munteanu S_IRUSR, buf, 432*20d8b67cSEduard - Gabriel Munteanu &chan->is_global); 433*20d8b67cSEduard - Gabriel Munteanu 434*20d8b67cSEduard - Gabriel Munteanu kfree(tmpname); 435*20d8b67cSEduard - Gabriel Munteanu 436*20d8b67cSEduard - Gabriel Munteanu return dentry; 437*20d8b67cSEduard - Gabriel Munteanu } 438*20d8b67cSEduard - Gabriel Munteanu 4394c78a663SRandy Dunlap /* 440b86ff981SJens Axboe * relay_open_buf - create a new relay channel buffer 441b86ff981SJens Axboe * 44223c88752SMathieu Desnoyers * used by relay_open() and CPU hotplug. 443b86ff981SJens Axboe */ 44423c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) 445b86ff981SJens Axboe { 44623c88752SMathieu Desnoyers struct rchan_buf *buf = NULL; 447b86ff981SJens Axboe struct dentry *dentry; 448b86ff981SJens Axboe 44923c88752SMathieu Desnoyers if (chan->is_global) 450b86ff981SJens Axboe return chan->buf[0]; 451b86ff981SJens Axboe 452b86ff981SJens Axboe buf = relay_create_buf(chan); 453b86ff981SJens Axboe if (!buf) 454*20d8b67cSEduard - Gabriel Munteanu return NULL; 455*20d8b67cSEduard - Gabriel Munteanu 456*20d8b67cSEduard - Gabriel Munteanu if (chan->has_base_filename) { 457*20d8b67cSEduard - Gabriel Munteanu dentry = relay_create_buf_file(chan, buf, cpu); 458*20d8b67cSEduard - Gabriel Munteanu if (!dentry) 459*20d8b67cSEduard - Gabriel Munteanu goto free_buf; 460*20d8b67cSEduard - Gabriel Munteanu relay_set_buf_dentry(buf, dentry); 461*20d8b67cSEduard - Gabriel Munteanu } 462b86ff981SJens Axboe 46323c88752SMathieu Desnoyers buf->cpu = cpu; 464b86ff981SJens Axboe __relay_reset(buf, 1); 465b86ff981SJens Axboe 46623c88752SMathieu Desnoyers if(chan->is_global) { 46723c88752SMathieu Desnoyers chan->buf[0] = buf; 46823c88752SMathieu Desnoyers buf->cpu = 0; 46923c88752SMathieu Desnoyers } 47023c88752SMathieu Desnoyers 471*20d8b67cSEduard - Gabriel Munteanu return buf; 47223c88752SMathieu Desnoyers 47323c88752SMathieu Desnoyers free_buf: 47423c88752SMathieu Desnoyers relay_destroy_buf(buf); 475*20d8b67cSEduard - Gabriel Munteanu return NULL; 476b86ff981SJens Axboe } 477b86ff981SJens Axboe 478b86ff981SJens Axboe /** 479b86ff981SJens Axboe * relay_close_buf - close a channel buffer 480b86ff981SJens Axboe * @buf: channel buffer 481b86ff981SJens Axboe * 482b86ff981SJens Axboe * Marks the buffer finalized and restores the default callbacks. 483b86ff981SJens Axboe * The channel buffer and channel buffer data structure are then freed 484b86ff981SJens Axboe * automatically when the last reference is given up. 485b86ff981SJens Axboe */ 486192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf) 487b86ff981SJens Axboe { 488b86ff981SJens Axboe buf->finalized = 1; 4897c9cb383STom Zanussi del_timer_sync(&buf->timer); 490b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 491b86ff981SJens Axboe } 492b86ff981SJens Axboe 493192636adSAndrew Morton static void setup_callbacks(struct rchan *chan, 494b86ff981SJens Axboe struct rchan_callbacks *cb) 495b86ff981SJens Axboe { 496b86ff981SJens Axboe if (!cb) { 497b86ff981SJens Axboe chan->cb = &default_channel_callbacks; 498b86ff981SJens Axboe return; 499b86ff981SJens Axboe } 500b86ff981SJens Axboe 501b86ff981SJens Axboe if (!cb->subbuf_start) 502b86ff981SJens Axboe cb->subbuf_start = subbuf_start_default_callback; 503b86ff981SJens Axboe if (!cb->buf_mapped) 504b86ff981SJens Axboe cb->buf_mapped = buf_mapped_default_callback; 505b86ff981SJens Axboe if (!cb->buf_unmapped) 506b86ff981SJens Axboe cb->buf_unmapped = buf_unmapped_default_callback; 507b86ff981SJens Axboe if (!cb->create_buf_file) 508b86ff981SJens Axboe cb->create_buf_file = create_buf_file_default_callback; 509b86ff981SJens Axboe if (!cb->remove_buf_file) 510b86ff981SJens Axboe cb->remove_buf_file = remove_buf_file_default_callback; 511b86ff981SJens Axboe chan->cb = cb; 512b86ff981SJens Axboe } 513b86ff981SJens Axboe 514b86ff981SJens Axboe /** 51523c88752SMathieu Desnoyers * relay_hotcpu_callback - CPU hotplug callback 51623c88752SMathieu Desnoyers * @nb: notifier block 51723c88752SMathieu Desnoyers * @action: hotplug action to take 51823c88752SMathieu Desnoyers * @hcpu: CPU number 51923c88752SMathieu Desnoyers * 52005fb6bf0SRandy Dunlap * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) 52123c88752SMathieu Desnoyers */ 52223c88752SMathieu Desnoyers static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb, 52323c88752SMathieu Desnoyers unsigned long action, 52423c88752SMathieu Desnoyers void *hcpu) 52523c88752SMathieu Desnoyers { 52623c88752SMathieu Desnoyers unsigned int hotcpu = (unsigned long)hcpu; 52723c88752SMathieu Desnoyers struct rchan *chan; 52823c88752SMathieu Desnoyers 52923c88752SMathieu Desnoyers switch(action) { 53023c88752SMathieu Desnoyers case CPU_UP_PREPARE: 5318bb78442SRafael J. Wysocki case CPU_UP_PREPARE_FROZEN: 53223c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 53323c88752SMathieu Desnoyers list_for_each_entry(chan, &relay_channels, list) { 53423c88752SMathieu Desnoyers if (chan->buf[hotcpu]) 53523c88752SMathieu Desnoyers continue; 53623c88752SMathieu Desnoyers chan->buf[hotcpu] = relay_open_buf(chan, hotcpu); 53723c88752SMathieu Desnoyers if(!chan->buf[hotcpu]) { 53823c88752SMathieu Desnoyers printk(KERN_ERR 53923c88752SMathieu Desnoyers "relay_hotcpu_callback: cpu %d buffer " 54023c88752SMathieu Desnoyers "creation failed\n", hotcpu); 54123c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 54223c88752SMathieu Desnoyers return NOTIFY_BAD; 54323c88752SMathieu Desnoyers } 54423c88752SMathieu Desnoyers } 54523c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 54623c88752SMathieu Desnoyers break; 54723c88752SMathieu Desnoyers case CPU_DEAD: 5488bb78442SRafael J. Wysocki case CPU_DEAD_FROZEN: 54923c88752SMathieu Desnoyers /* No need to flush the cpu : will be flushed upon 55023c88752SMathieu Desnoyers * final relay_flush() call. */ 55123c88752SMathieu Desnoyers break; 55223c88752SMathieu Desnoyers } 55323c88752SMathieu Desnoyers return NOTIFY_OK; 55423c88752SMathieu Desnoyers } 55523c88752SMathieu Desnoyers 55623c88752SMathieu Desnoyers /** 557b86ff981SJens Axboe * relay_open - create a new relay channel 558*20d8b67cSEduard - Gabriel Munteanu * @base_filename: base name of files to create, %NULL for buffering only 559*20d8b67cSEduard - Gabriel Munteanu * @parent: dentry of parent directory, %NULL for root directory or buffer 560b86ff981SJens Axboe * @subbuf_size: size of sub-buffers 561b86ff981SJens Axboe * @n_subbufs: number of sub-buffers 562b86ff981SJens Axboe * @cb: client callback functions 56323c88752SMathieu Desnoyers * @private_data: user-defined data 564b86ff981SJens Axboe * 5654c78a663SRandy Dunlap * Returns channel pointer if successful, %NULL otherwise. 566b86ff981SJens Axboe * 567b86ff981SJens Axboe * Creates a channel buffer for each cpu using the sizes and 568b86ff981SJens Axboe * attributes specified. The created channel buffer files 569b86ff981SJens Axboe * will be named base_filename0...base_filenameN-1. File 57072fd4a35SRobert P. J. Day * permissions will be %S_IRUSR. 571b86ff981SJens Axboe */ 572b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename, 573b86ff981SJens Axboe struct dentry *parent, 574b86ff981SJens Axboe size_t subbuf_size, 575b86ff981SJens Axboe size_t n_subbufs, 57623c88752SMathieu Desnoyers struct rchan_callbacks *cb, 57723c88752SMathieu Desnoyers void *private_data) 578b86ff981SJens Axboe { 579b86ff981SJens Axboe unsigned int i; 580b86ff981SJens Axboe struct rchan *chan; 581b86ff981SJens Axboe 582b86ff981SJens Axboe if (!(subbuf_size && n_subbufs)) 583b86ff981SJens Axboe return NULL; 584b86ff981SJens Axboe 585cd861280SRobert P. J. Day chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); 586b86ff981SJens Axboe if (!chan) 587b86ff981SJens Axboe return NULL; 588b86ff981SJens Axboe 589b86ff981SJens Axboe chan->version = RELAYFS_CHANNEL_VERSION; 590b86ff981SJens Axboe chan->n_subbufs = n_subbufs; 591b86ff981SJens Axboe chan->subbuf_size = subbuf_size; 592b86ff981SJens Axboe chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs); 59323c88752SMathieu Desnoyers chan->parent = parent; 59423c88752SMathieu Desnoyers chan->private_data = private_data; 595*20d8b67cSEduard - Gabriel Munteanu if (base_filename) { 596*20d8b67cSEduard - Gabriel Munteanu chan->has_base_filename = 1; 59723c88752SMathieu Desnoyers strlcpy(chan->base_filename, base_filename, NAME_MAX); 598*20d8b67cSEduard - Gabriel Munteanu } 599b86ff981SJens Axboe setup_callbacks(chan, cb); 600b86ff981SJens Axboe kref_init(&chan->kref); 601b86ff981SJens Axboe 60223c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 603b86ff981SJens Axboe for_each_online_cpu(i) { 60423c88752SMathieu Desnoyers chan->buf[i] = relay_open_buf(chan, i); 605b86ff981SJens Axboe if (!chan->buf[i]) 606b86ff981SJens Axboe goto free_bufs; 607b86ff981SJens Axboe } 60823c88752SMathieu Desnoyers list_add(&chan->list, &relay_channels); 60923c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 610b86ff981SJens Axboe 611b86ff981SJens Axboe return chan; 612b86ff981SJens Axboe 613b86ff981SJens Axboe free_bufs: 61423c88752SMathieu Desnoyers for_each_online_cpu(i) { 615b86ff981SJens Axboe if (!chan->buf[i]) 616b86ff981SJens Axboe break; 617b86ff981SJens Axboe relay_close_buf(chan->buf[i]); 618b86ff981SJens Axboe } 619b86ff981SJens Axboe 620b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 62123c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 622b86ff981SJens Axboe return NULL; 623b86ff981SJens Axboe } 624b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open); 625b86ff981SJens Axboe 626*20d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher { 627*20d8b67cSEduard - Gabriel Munteanu struct rchan_buf *buf; 628*20d8b67cSEduard - Gabriel Munteanu struct dentry *dentry; 629*20d8b67cSEduard - Gabriel Munteanu }; 630*20d8b67cSEduard - Gabriel Munteanu 631*20d8b67cSEduard - Gabriel Munteanu /* Called in atomic context. */ 632*20d8b67cSEduard - Gabriel Munteanu static void __relay_set_buf_dentry(void *info) 633*20d8b67cSEduard - Gabriel Munteanu { 634*20d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher *p = info; 635*20d8b67cSEduard - Gabriel Munteanu 636*20d8b67cSEduard - Gabriel Munteanu relay_set_buf_dentry(p->buf, p->dentry); 637*20d8b67cSEduard - Gabriel Munteanu } 638*20d8b67cSEduard - Gabriel Munteanu 639*20d8b67cSEduard - Gabriel Munteanu /** 640*20d8b67cSEduard - Gabriel Munteanu * relay_late_setup_files - triggers file creation 641*20d8b67cSEduard - Gabriel Munteanu * @chan: channel to operate on 642*20d8b67cSEduard - Gabriel Munteanu * @base_filename: base name of files to create 643*20d8b67cSEduard - Gabriel Munteanu * @parent: dentry of parent directory, %NULL for root directory 644*20d8b67cSEduard - Gabriel Munteanu * 645*20d8b67cSEduard - Gabriel Munteanu * Returns 0 if successful, non-zero otherwise. 646*20d8b67cSEduard - Gabriel Munteanu * 647*20d8b67cSEduard - Gabriel Munteanu * Use to setup files for a previously buffer-only channel. 648*20d8b67cSEduard - Gabriel Munteanu * Useful to do early tracing in kernel, before VFS is up, for example. 649*20d8b67cSEduard - Gabriel Munteanu */ 650*20d8b67cSEduard - Gabriel Munteanu int relay_late_setup_files(struct rchan *chan, 651*20d8b67cSEduard - Gabriel Munteanu const char *base_filename, 652*20d8b67cSEduard - Gabriel Munteanu struct dentry *parent) 653*20d8b67cSEduard - Gabriel Munteanu { 654*20d8b67cSEduard - Gabriel Munteanu int err = 0; 655*20d8b67cSEduard - Gabriel Munteanu unsigned int i, curr_cpu; 656*20d8b67cSEduard - Gabriel Munteanu unsigned long flags; 657*20d8b67cSEduard - Gabriel Munteanu struct dentry *dentry; 658*20d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher disp; 659*20d8b67cSEduard - Gabriel Munteanu 660*20d8b67cSEduard - Gabriel Munteanu if (!chan || !base_filename) 661*20d8b67cSEduard - Gabriel Munteanu return -EINVAL; 662*20d8b67cSEduard - Gabriel Munteanu 663*20d8b67cSEduard - Gabriel Munteanu strlcpy(chan->base_filename, base_filename, NAME_MAX); 664*20d8b67cSEduard - Gabriel Munteanu 665*20d8b67cSEduard - Gabriel Munteanu mutex_lock(&relay_channels_mutex); 666*20d8b67cSEduard - Gabriel Munteanu /* Is chan already set up? */ 667*20d8b67cSEduard - Gabriel Munteanu if (unlikely(chan->has_base_filename)) 668*20d8b67cSEduard - Gabriel Munteanu return -EEXIST; 669*20d8b67cSEduard - Gabriel Munteanu chan->has_base_filename = 1; 670*20d8b67cSEduard - Gabriel Munteanu chan->parent = parent; 671*20d8b67cSEduard - Gabriel Munteanu curr_cpu = get_cpu(); 672*20d8b67cSEduard - Gabriel Munteanu /* 673*20d8b67cSEduard - Gabriel Munteanu * The CPU hotplug notifier ran before us and created buffers with 674*20d8b67cSEduard - Gabriel Munteanu * no files associated. So it's safe to call relay_setup_buf_file() 675*20d8b67cSEduard - Gabriel Munteanu * on all currently online CPUs. 676*20d8b67cSEduard - Gabriel Munteanu */ 677*20d8b67cSEduard - Gabriel Munteanu for_each_online_cpu(i) { 678*20d8b67cSEduard - Gabriel Munteanu if (unlikely(!chan->buf[i])) { 679*20d8b67cSEduard - Gabriel Munteanu printk(KERN_ERR "relay_late_setup_files: CPU %u " 680*20d8b67cSEduard - Gabriel Munteanu "has no buffer, it must have!\n", i); 681*20d8b67cSEduard - Gabriel Munteanu BUG(); 682*20d8b67cSEduard - Gabriel Munteanu err = -EINVAL; 683*20d8b67cSEduard - Gabriel Munteanu break; 684*20d8b67cSEduard - Gabriel Munteanu } 685*20d8b67cSEduard - Gabriel Munteanu 686*20d8b67cSEduard - Gabriel Munteanu dentry = relay_create_buf_file(chan, chan->buf[i], i); 687*20d8b67cSEduard - Gabriel Munteanu if (unlikely(!dentry)) { 688*20d8b67cSEduard - Gabriel Munteanu err = -EINVAL; 689*20d8b67cSEduard - Gabriel Munteanu break; 690*20d8b67cSEduard - Gabriel Munteanu } 691*20d8b67cSEduard - Gabriel Munteanu 692*20d8b67cSEduard - Gabriel Munteanu if (curr_cpu == i) { 693*20d8b67cSEduard - Gabriel Munteanu local_irq_save(flags); 694*20d8b67cSEduard - Gabriel Munteanu relay_set_buf_dentry(chan->buf[i], dentry); 695*20d8b67cSEduard - Gabriel Munteanu local_irq_restore(flags); 696*20d8b67cSEduard - Gabriel Munteanu } else { 697*20d8b67cSEduard - Gabriel Munteanu disp.buf = chan->buf[i]; 698*20d8b67cSEduard - Gabriel Munteanu disp.dentry = dentry; 699*20d8b67cSEduard - Gabriel Munteanu smp_mb(); 700*20d8b67cSEduard - Gabriel Munteanu /* relay_channels_mutex must be held, so wait. */ 701*20d8b67cSEduard - Gabriel Munteanu err = smp_call_function_single(i, 702*20d8b67cSEduard - Gabriel Munteanu __relay_set_buf_dentry, 703*20d8b67cSEduard - Gabriel Munteanu &disp, 1); 704*20d8b67cSEduard - Gabriel Munteanu } 705*20d8b67cSEduard - Gabriel Munteanu if (unlikely(err)) 706*20d8b67cSEduard - Gabriel Munteanu break; 707*20d8b67cSEduard - Gabriel Munteanu } 708*20d8b67cSEduard - Gabriel Munteanu put_cpu(); 709*20d8b67cSEduard - Gabriel Munteanu mutex_unlock(&relay_channels_mutex); 710*20d8b67cSEduard - Gabriel Munteanu 711*20d8b67cSEduard - Gabriel Munteanu return err; 712*20d8b67cSEduard - Gabriel Munteanu } 713*20d8b67cSEduard - Gabriel Munteanu 714b86ff981SJens Axboe /** 715b86ff981SJens Axboe * relay_switch_subbuf - switch to a new sub-buffer 716b86ff981SJens Axboe * @buf: channel buffer 717b86ff981SJens Axboe * @length: size of current event 718b86ff981SJens Axboe * 719b86ff981SJens Axboe * Returns either the length passed in or 0 if full. 720b86ff981SJens Axboe * 721b86ff981SJens Axboe * Performs sub-buffer-switch tasks such as invoking callbacks, 722b86ff981SJens Axboe * updating padding counts, waking up readers, etc. 723b86ff981SJens Axboe */ 724b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) 725b86ff981SJens Axboe { 726b86ff981SJens Axboe void *old, *new; 727b86ff981SJens Axboe size_t old_subbuf, new_subbuf; 728b86ff981SJens Axboe 729b86ff981SJens Axboe if (unlikely(length > buf->chan->subbuf_size)) 730b86ff981SJens Axboe goto toobig; 731b86ff981SJens Axboe 732b86ff981SJens Axboe if (buf->offset != buf->chan->subbuf_size + 1) { 733b86ff981SJens Axboe buf->prev_padding = buf->chan->subbuf_size - buf->offset; 734b86ff981SJens Axboe old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 735b86ff981SJens Axboe buf->padding[old_subbuf] = buf->prev_padding; 736b86ff981SJens Axboe buf->subbufs_produced++; 737*20d8b67cSEduard - Gabriel Munteanu if (buf->dentry) 738*20d8b67cSEduard - Gabriel Munteanu buf->dentry->d_inode->i_size += 739*20d8b67cSEduard - Gabriel Munteanu buf->chan->subbuf_size - 740*20d8b67cSEduard - Gabriel Munteanu buf->padding[old_subbuf]; 741*20d8b67cSEduard - Gabriel Munteanu else 742*20d8b67cSEduard - Gabriel Munteanu buf->early_bytes += buf->chan->subbuf_size - 743221415d7SJens Axboe buf->padding[old_subbuf]; 744221415d7SJens Axboe smp_mb(); 7457c9cb383STom Zanussi if (waitqueue_active(&buf->read_wait)) 7467c9cb383STom Zanussi /* 7477c9cb383STom Zanussi * Calling wake_up_interruptible() from here 7487c9cb383STom Zanussi * will deadlock if we happen to be logging 7497c9cb383STom Zanussi * from the scheduler (trying to re-grab 7507c9cb383STom Zanussi * rq->lock), so defer it. 7517c9cb383STom Zanussi */ 7527c9cb383STom Zanussi __mod_timer(&buf->timer, jiffies + 1); 753b86ff981SJens Axboe } 754b86ff981SJens Axboe 755b86ff981SJens Axboe old = buf->data; 756b86ff981SJens Axboe new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 757b86ff981SJens Axboe new = buf->start + new_subbuf * buf->chan->subbuf_size; 758b86ff981SJens Axboe buf->offset = 0; 759b86ff981SJens Axboe if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { 760b86ff981SJens Axboe buf->offset = buf->chan->subbuf_size + 1; 761b86ff981SJens Axboe return 0; 762b86ff981SJens Axboe } 763b86ff981SJens Axboe buf->data = new; 764b86ff981SJens Axboe buf->padding[new_subbuf] = 0; 765b86ff981SJens Axboe 766b86ff981SJens Axboe if (unlikely(length + buf->offset > buf->chan->subbuf_size)) 767b86ff981SJens Axboe goto toobig; 768b86ff981SJens Axboe 769b86ff981SJens Axboe return length; 770b86ff981SJens Axboe 771b86ff981SJens Axboe toobig: 772b86ff981SJens Axboe buf->chan->last_toobig = length; 773b86ff981SJens Axboe return 0; 774b86ff981SJens Axboe } 775b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf); 776b86ff981SJens Axboe 777b86ff981SJens Axboe /** 778b86ff981SJens Axboe * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count 779b86ff981SJens Axboe * @chan: the channel 780b86ff981SJens Axboe * @cpu: the cpu associated with the channel buffer to update 781b86ff981SJens Axboe * @subbufs_consumed: number of sub-buffers to add to current buf's count 782b86ff981SJens Axboe * 783b86ff981SJens Axboe * Adds to the channel buffer's consumed sub-buffer count. 784b86ff981SJens Axboe * subbufs_consumed should be the number of sub-buffers newly consumed, 785b86ff981SJens Axboe * not the total consumed. 786b86ff981SJens Axboe * 78772fd4a35SRobert P. J. Day * NOTE. Kernel clients don't need to call this function if the channel 788b86ff981SJens Axboe * mode is 'overwrite'. 789b86ff981SJens Axboe */ 790b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan, 791b86ff981SJens Axboe unsigned int cpu, 792b86ff981SJens Axboe size_t subbufs_consumed) 793b86ff981SJens Axboe { 794b86ff981SJens Axboe struct rchan_buf *buf; 795b86ff981SJens Axboe 796b86ff981SJens Axboe if (!chan) 797b86ff981SJens Axboe return; 798b86ff981SJens Axboe 799b86ff981SJens Axboe if (cpu >= NR_CPUS || !chan->buf[cpu]) 800b86ff981SJens Axboe return; 801b86ff981SJens Axboe 802b86ff981SJens Axboe buf = chan->buf[cpu]; 803b86ff981SJens Axboe buf->subbufs_consumed += subbufs_consumed; 804b86ff981SJens Axboe if (buf->subbufs_consumed > buf->subbufs_produced) 805b86ff981SJens Axboe buf->subbufs_consumed = buf->subbufs_produced; 806b86ff981SJens Axboe } 807b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed); 808b86ff981SJens Axboe 809b86ff981SJens Axboe /** 810b86ff981SJens Axboe * relay_close - close the channel 811b86ff981SJens Axboe * @chan: the channel 812b86ff981SJens Axboe * 813b86ff981SJens Axboe * Closes all channel buffers and frees the channel. 814b86ff981SJens Axboe */ 815b86ff981SJens Axboe void relay_close(struct rchan *chan) 816b86ff981SJens Axboe { 817b86ff981SJens Axboe unsigned int i; 818b86ff981SJens Axboe 819b86ff981SJens Axboe if (!chan) 820b86ff981SJens Axboe return; 821b86ff981SJens Axboe 82223c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 82323c88752SMathieu Desnoyers if (chan->is_global && chan->buf[0]) 82423c88752SMathieu Desnoyers relay_close_buf(chan->buf[0]); 82523c88752SMathieu Desnoyers else 82623c88752SMathieu Desnoyers for_each_possible_cpu(i) 82723c88752SMathieu Desnoyers if (chan->buf[i]) 828b86ff981SJens Axboe relay_close_buf(chan->buf[i]); 829b86ff981SJens Axboe 830b86ff981SJens Axboe if (chan->last_toobig) 831b86ff981SJens Axboe printk(KERN_WARNING "relay: one or more items not logged " 832b86ff981SJens Axboe "[item size (%Zd) > sub-buffer size (%Zd)]\n", 833b86ff981SJens Axboe chan->last_toobig, chan->subbuf_size); 834b86ff981SJens Axboe 83523c88752SMathieu Desnoyers list_del(&chan->list); 836b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 83723c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 838b86ff981SJens Axboe } 839b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close); 840b86ff981SJens Axboe 841b86ff981SJens Axboe /** 842b86ff981SJens Axboe * relay_flush - close the channel 843b86ff981SJens Axboe * @chan: the channel 844b86ff981SJens Axboe * 8454c78a663SRandy Dunlap * Flushes all channel buffers, i.e. forces buffer switch. 846b86ff981SJens Axboe */ 847b86ff981SJens Axboe void relay_flush(struct rchan *chan) 848b86ff981SJens Axboe { 849b86ff981SJens Axboe unsigned int i; 850b86ff981SJens Axboe 851b86ff981SJens Axboe if (!chan) 852b86ff981SJens Axboe return; 853b86ff981SJens Axboe 85423c88752SMathieu Desnoyers if (chan->is_global && chan->buf[0]) { 85523c88752SMathieu Desnoyers relay_switch_subbuf(chan->buf[0], 0); 85623c88752SMathieu Desnoyers return; 857b86ff981SJens Axboe } 85823c88752SMathieu Desnoyers 85923c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 86023c88752SMathieu Desnoyers for_each_possible_cpu(i) 86123c88752SMathieu Desnoyers if (chan->buf[i]) 86223c88752SMathieu Desnoyers relay_switch_subbuf(chan->buf[i], 0); 86323c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 864b86ff981SJens Axboe } 865b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush); 866b86ff981SJens Axboe 867b86ff981SJens Axboe /** 868b86ff981SJens Axboe * relay_file_open - open file op for relay files 869b86ff981SJens Axboe * @inode: the inode 870b86ff981SJens Axboe * @filp: the file 871b86ff981SJens Axboe * 872b86ff981SJens Axboe * Increments the channel buffer refcount. 873b86ff981SJens Axboe */ 874b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp) 875b86ff981SJens Axboe { 8768e18e294STheodore Ts'o struct rchan_buf *buf = inode->i_private; 877b86ff981SJens Axboe kref_get(&buf->kref); 878b86ff981SJens Axboe filp->private_data = buf; 879b86ff981SJens Axboe 88037529fe9SLai Jiangshan return nonseekable_open(inode, filp); 881b86ff981SJens Axboe } 882b86ff981SJens Axboe 883b86ff981SJens Axboe /** 884b86ff981SJens Axboe * relay_file_mmap - mmap file op for relay files 885b86ff981SJens Axboe * @filp: the file 886b86ff981SJens Axboe * @vma: the vma describing what to map 887b86ff981SJens Axboe * 88872fd4a35SRobert P. J. Day * Calls upon relay_mmap_buf() to map the file into user space. 889b86ff981SJens Axboe */ 890b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) 891b86ff981SJens Axboe { 892b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 893b86ff981SJens Axboe return relay_mmap_buf(buf, vma); 894b86ff981SJens Axboe } 895b86ff981SJens Axboe 896b86ff981SJens Axboe /** 897b86ff981SJens Axboe * relay_file_poll - poll file op for relay files 898b86ff981SJens Axboe * @filp: the file 899b86ff981SJens Axboe * @wait: poll table 900b86ff981SJens Axboe * 901b86ff981SJens Axboe * Poll implemention. 902b86ff981SJens Axboe */ 903b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait) 904b86ff981SJens Axboe { 905b86ff981SJens Axboe unsigned int mask = 0; 906b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 907b86ff981SJens Axboe 908b86ff981SJens Axboe if (buf->finalized) 909b86ff981SJens Axboe return POLLERR; 910b86ff981SJens Axboe 911b86ff981SJens Axboe if (filp->f_mode & FMODE_READ) { 912b86ff981SJens Axboe poll_wait(filp, &buf->read_wait, wait); 913b86ff981SJens Axboe if (!relay_buf_empty(buf)) 914b86ff981SJens Axboe mask |= POLLIN | POLLRDNORM; 915b86ff981SJens Axboe } 916b86ff981SJens Axboe 917b86ff981SJens Axboe return mask; 918b86ff981SJens Axboe } 919b86ff981SJens Axboe 920b86ff981SJens Axboe /** 921b86ff981SJens Axboe * relay_file_release - release file op for relay files 922b86ff981SJens Axboe * @inode: the inode 923b86ff981SJens Axboe * @filp: the file 924b86ff981SJens Axboe * 925b86ff981SJens Axboe * Decrements the channel refcount, as the filesystem is 926b86ff981SJens Axboe * no longer using it. 927b86ff981SJens Axboe */ 928b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp) 929b86ff981SJens Axboe { 930b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 931b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 932b86ff981SJens Axboe 933b86ff981SJens Axboe return 0; 934b86ff981SJens Axboe } 935b86ff981SJens Axboe 9364c78a663SRandy Dunlap /* 937b86ff981SJens Axboe * relay_file_read_consume - update the consumed count for the buffer 938b86ff981SJens Axboe */ 939b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf, 940b86ff981SJens Axboe size_t read_pos, 941b86ff981SJens Axboe size_t bytes_consumed) 942b86ff981SJens Axboe { 943b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 944b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 945b86ff981SJens Axboe size_t read_subbuf; 946b86ff981SJens Axboe 947b86ff981SJens Axboe if (buf->bytes_consumed + bytes_consumed > subbuf_size) { 948b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 949b86ff981SJens Axboe buf->bytes_consumed = 0; 950b86ff981SJens Axboe } 951b86ff981SJens Axboe 952b86ff981SJens Axboe buf->bytes_consumed += bytes_consumed; 953a66e356cSMasami Hiramatsu if (!read_pos) 954a66e356cSMasami Hiramatsu read_subbuf = buf->subbufs_consumed % n_subbufs; 955a66e356cSMasami Hiramatsu else 956b86ff981SJens Axboe read_subbuf = read_pos / buf->chan->subbuf_size; 957b86ff981SJens Axboe if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { 958b86ff981SJens Axboe if ((read_subbuf == buf->subbufs_produced % n_subbufs) && 959b86ff981SJens Axboe (buf->offset == subbuf_size)) 960b86ff981SJens Axboe return; 961b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 962b86ff981SJens Axboe buf->bytes_consumed = 0; 963b86ff981SJens Axboe } 964b86ff981SJens Axboe } 965b86ff981SJens Axboe 9664c78a663SRandy Dunlap /* 967b86ff981SJens Axboe * relay_file_read_avail - boolean, are there unconsumed bytes available? 968b86ff981SJens Axboe */ 969b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) 970b86ff981SJens Axboe { 971b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 972b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 973221415d7SJens Axboe size_t produced = buf->subbufs_produced; 974221415d7SJens Axboe size_t consumed = buf->subbufs_consumed; 975b86ff981SJens Axboe 976b86ff981SJens Axboe relay_file_read_consume(buf, read_pos, 0); 977b86ff981SJens Axboe 978221415d7SJens Axboe if (unlikely(buf->offset > subbuf_size)) { 979221415d7SJens Axboe if (produced == consumed) 980221415d7SJens Axboe return 0; 981221415d7SJens Axboe return 1; 982221415d7SJens Axboe } 983221415d7SJens Axboe 984221415d7SJens Axboe if (unlikely(produced - consumed >= n_subbufs)) { 985a66e356cSMasami Hiramatsu consumed = produced - n_subbufs + 1; 986221415d7SJens Axboe buf->subbufs_consumed = consumed; 987a66e356cSMasami Hiramatsu buf->bytes_consumed = 0; 988221415d7SJens Axboe } 989221415d7SJens Axboe 990221415d7SJens Axboe produced = (produced % n_subbufs) * subbuf_size + buf->offset; 991221415d7SJens Axboe consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; 992221415d7SJens Axboe 993221415d7SJens Axboe if (consumed > produced) 994221415d7SJens Axboe produced += n_subbufs * subbuf_size; 995221415d7SJens Axboe 996221415d7SJens Axboe if (consumed == produced) 997221415d7SJens Axboe return 0; 998221415d7SJens Axboe 999b86ff981SJens Axboe return 1; 1000b86ff981SJens Axboe } 1001b86ff981SJens Axboe 1002b86ff981SJens Axboe /** 1003b86ff981SJens Axboe * relay_file_read_subbuf_avail - return bytes available in sub-buffer 10044c78a663SRandy Dunlap * @read_pos: file read position 10054c78a663SRandy Dunlap * @buf: relay channel buffer 1006b86ff981SJens Axboe */ 1007b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos, 1008b86ff981SJens Axboe struct rchan_buf *buf) 1009b86ff981SJens Axboe { 1010b86ff981SJens Axboe size_t padding, avail = 0; 1011b86ff981SJens Axboe size_t read_subbuf, read_offset, write_subbuf, write_offset; 1012b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 1013b86ff981SJens Axboe 1014b86ff981SJens Axboe write_subbuf = (buf->data - buf->start) / subbuf_size; 1015b86ff981SJens Axboe write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; 1016b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 1017b86ff981SJens Axboe read_offset = read_pos % subbuf_size; 1018b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 1019b86ff981SJens Axboe 1020b86ff981SJens Axboe if (read_subbuf == write_subbuf) { 1021b86ff981SJens Axboe if (read_offset + padding < write_offset) 1022b86ff981SJens Axboe avail = write_offset - (read_offset + padding); 1023b86ff981SJens Axboe } else 1024b86ff981SJens Axboe avail = (subbuf_size - padding) - read_offset; 1025b86ff981SJens Axboe 1026b86ff981SJens Axboe return avail; 1027b86ff981SJens Axboe } 1028b86ff981SJens Axboe 1029b86ff981SJens Axboe /** 1030b86ff981SJens Axboe * relay_file_read_start_pos - find the first available byte to read 10314c78a663SRandy Dunlap * @read_pos: file read position 10324c78a663SRandy Dunlap * @buf: relay channel buffer 1033b86ff981SJens Axboe * 103472fd4a35SRobert P. J. Day * If the @read_pos is in the middle of padding, return the 1035b86ff981SJens Axboe * position of the first actually available byte, otherwise 1036b86ff981SJens Axboe * return the original value. 1037b86ff981SJens Axboe */ 1038b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos, 1039b86ff981SJens Axboe struct rchan_buf *buf) 1040b86ff981SJens Axboe { 1041b86ff981SJens Axboe size_t read_subbuf, padding, padding_start, padding_end; 1042b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 1043b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 10448d62fdebSDavid Wilder size_t consumed = buf->subbufs_consumed % n_subbufs; 1045b86ff981SJens Axboe 10468d62fdebSDavid Wilder if (!read_pos) 10478d62fdebSDavid Wilder read_pos = consumed * subbuf_size + buf->bytes_consumed; 1048b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 1049b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 1050b86ff981SJens Axboe padding_start = (read_subbuf + 1) * subbuf_size - padding; 1051b86ff981SJens Axboe padding_end = (read_subbuf + 1) * subbuf_size; 1052b86ff981SJens Axboe if (read_pos >= padding_start && read_pos < padding_end) { 1053b86ff981SJens Axboe read_subbuf = (read_subbuf + 1) % n_subbufs; 1054b86ff981SJens Axboe read_pos = read_subbuf * subbuf_size; 1055b86ff981SJens Axboe } 1056b86ff981SJens Axboe 1057b86ff981SJens Axboe return read_pos; 1058b86ff981SJens Axboe } 1059b86ff981SJens Axboe 1060b86ff981SJens Axboe /** 1061b86ff981SJens Axboe * relay_file_read_end_pos - return the new read position 10624c78a663SRandy Dunlap * @read_pos: file read position 10634c78a663SRandy Dunlap * @buf: relay channel buffer 10644c78a663SRandy Dunlap * @count: number of bytes to be read 1065b86ff981SJens Axboe */ 1066b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf, 1067b86ff981SJens Axboe size_t read_pos, 1068b86ff981SJens Axboe size_t count) 1069b86ff981SJens Axboe { 1070b86ff981SJens Axboe size_t read_subbuf, padding, end_pos; 1071b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 1072b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 1073b86ff981SJens Axboe 1074b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 1075b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 1076b86ff981SJens Axboe if (read_pos % subbuf_size + count + padding == subbuf_size) 1077b86ff981SJens Axboe end_pos = (read_subbuf + 1) * subbuf_size; 1078b86ff981SJens Axboe else 1079b86ff981SJens Axboe end_pos = read_pos + count; 1080b86ff981SJens Axboe if (end_pos >= subbuf_size * n_subbufs) 1081b86ff981SJens Axboe end_pos = 0; 1082b86ff981SJens Axboe 1083b86ff981SJens Axboe return end_pos; 1084b86ff981SJens Axboe } 1085b86ff981SJens Axboe 10864c78a663SRandy Dunlap /* 10876dac40a7STom Zanussi * subbuf_read_actor - read up to one subbuf's worth of data 1088b86ff981SJens Axboe */ 10896dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start, 10906dac40a7STom Zanussi struct rchan_buf *buf, 10916dac40a7STom Zanussi size_t avail, 10926dac40a7STom Zanussi read_descriptor_t *desc, 10936dac40a7STom Zanussi read_actor_t actor) 1094b86ff981SJens Axboe { 1095b86ff981SJens Axboe void *from; 10966dac40a7STom Zanussi int ret = 0; 1097b86ff981SJens Axboe 1098b86ff981SJens Axboe from = buf->start + read_start; 10996dac40a7STom Zanussi ret = avail; 1100ba2397efSAl Viro if (copy_to_user(desc->arg.buf, from, avail)) { 11016dac40a7STom Zanussi desc->error = -EFAULT; 11026dac40a7STom Zanussi ret = 0; 1103b86ff981SJens Axboe } 11046dac40a7STom Zanussi desc->arg.data += ret; 11056dac40a7STom Zanussi desc->written += ret; 11066dac40a7STom Zanussi desc->count -= ret; 11076dac40a7STom Zanussi 1108b86ff981SJens Axboe return ret; 1109b86ff981SJens Axboe } 1110b86ff981SJens Axboe 11116dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start, 11126dac40a7STom Zanussi struct rchan_buf *buf, 11136dac40a7STom Zanussi size_t avail, 11146dac40a7STom Zanussi read_descriptor_t *desc, 11156dac40a7STom Zanussi read_actor_t actor); 11166dac40a7STom Zanussi 11174c78a663SRandy Dunlap /* 11186dac40a7STom Zanussi * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries 11196dac40a7STom Zanussi */ 1120192636adSAndrew Morton static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos, 11216dac40a7STom Zanussi subbuf_actor_t subbuf_actor, 11226dac40a7STom Zanussi read_actor_t actor, 1123ba2397efSAl Viro read_descriptor_t *desc) 1124221415d7SJens Axboe { 1125221415d7SJens Axboe struct rchan_buf *buf = filp->private_data; 1126221415d7SJens Axboe size_t read_start, avail; 11276dac40a7STom Zanussi int ret; 1128221415d7SJens Axboe 1129ba2397efSAl Viro if (!desc->count) 1130221415d7SJens Axboe return 0; 1131221415d7SJens Axboe 1132f3a43f3fSJosef "Jeff" Sipek mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); 11336dac40a7STom Zanussi do { 11346dac40a7STom Zanussi if (!relay_file_read_avail(buf, *ppos)) 1135221415d7SJens Axboe break; 1136221415d7SJens Axboe 11376dac40a7STom Zanussi read_start = relay_file_read_start_pos(*ppos, buf); 11386dac40a7STom Zanussi avail = relay_file_read_subbuf_avail(read_start, buf); 11396dac40a7STom Zanussi if (!avail) 11406dac40a7STom Zanussi break; 1141221415d7SJens Axboe 1142ba2397efSAl Viro avail = min(desc->count, avail); 1143ba2397efSAl Viro ret = subbuf_actor(read_start, buf, avail, desc, actor); 1144ba2397efSAl Viro if (desc->error < 0) 11456dac40a7STom Zanussi break; 11466dac40a7STom Zanussi 11476dac40a7STom Zanussi if (ret) { 1148221415d7SJens Axboe relay_file_read_consume(buf, read_start, ret); 1149221415d7SJens Axboe *ppos = relay_file_read_end_pos(buf, read_start, ret); 1150221415d7SJens Axboe } 1151ba2397efSAl Viro } while (desc->count && ret); 1152f3a43f3fSJosef "Jeff" Sipek mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); 1153221415d7SJens Axboe 1154ba2397efSAl Viro return desc->written; 1155221415d7SJens Axboe } 1156221415d7SJens Axboe 11576dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp, 11586dac40a7STom Zanussi char __user *buffer, 11596dac40a7STom Zanussi size_t count, 11606dac40a7STom Zanussi loff_t *ppos) 11616dac40a7STom Zanussi { 1162ba2397efSAl Viro read_descriptor_t desc; 1163ba2397efSAl Viro desc.written = 0; 1164ba2397efSAl Viro desc.count = count; 1165ba2397efSAl Viro desc.arg.buf = buffer; 1166ba2397efSAl Viro desc.error = 0; 1167ba2397efSAl Viro return relay_file_read_subbufs(filp, ppos, subbuf_read_actor, 1168ba2397efSAl Viro NULL, &desc); 11696dac40a7STom Zanussi } 11706dac40a7STom Zanussi 11711db60cf2SJens Axboe static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed) 11721db60cf2SJens Axboe { 11731db60cf2SJens Axboe rbuf->bytes_consumed += bytes_consumed; 11741db60cf2SJens Axboe 11751db60cf2SJens Axboe if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) { 11761db60cf2SJens Axboe relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1); 11771db60cf2SJens Axboe rbuf->bytes_consumed %= rbuf->chan->subbuf_size; 11781db60cf2SJens Axboe } 11791db60cf2SJens Axboe } 11801db60cf2SJens Axboe 1181ebf99093STom Zanussi static void relay_pipe_buf_release(struct pipe_inode_info *pipe, 1182ebf99093STom Zanussi struct pipe_buffer *buf) 1183221415d7SJens Axboe { 1184ebf99093STom Zanussi struct rchan_buf *rbuf; 1185ebf99093STom Zanussi 1186ebf99093STom Zanussi rbuf = (struct rchan_buf *)page_private(buf->page); 11871db60cf2SJens Axboe relay_consume_bytes(rbuf, buf->private); 1188ebf99093STom Zanussi } 1189ebf99093STom Zanussi 1190ebf99093STom Zanussi static struct pipe_buf_operations relay_pipe_buf_ops = { 1191ebf99093STom Zanussi .can_merge = 0, 1192ebf99093STom Zanussi .map = generic_pipe_buf_map, 1193ebf99093STom Zanussi .unmap = generic_pipe_buf_unmap, 1194cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 1195ebf99093STom Zanussi .release = relay_pipe_buf_release, 1196ebf99093STom Zanussi .steal = generic_pipe_buf_steal, 1197ebf99093STom Zanussi .get = generic_pipe_buf_get, 1198ebf99093STom Zanussi }; 1199ebf99093STom Zanussi 12005eb7f9faSJens Axboe static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i) 12015eb7f9faSJens Axboe { 12025eb7f9faSJens Axboe } 12035eb7f9faSJens Axboe 1204d3f35d98STom Zanussi /* 1205ebf99093STom Zanussi * subbuf_splice_actor - splice up to one subbuf's worth of data 1206ebf99093STom Zanussi */ 1207ebf99093STom Zanussi static int subbuf_splice_actor(struct file *in, 1208ebf99093STom Zanussi loff_t *ppos, 1209ebf99093STom Zanussi struct pipe_inode_info *pipe, 1210ebf99093STom Zanussi size_t len, 1211ebf99093STom Zanussi unsigned int flags, 1212ebf99093STom Zanussi int *nonpad_ret) 1213ebf99093STom Zanussi { 121416d54669SJens Axboe unsigned int pidx, poff, total_len, subbuf_pages, nr_pages, ret; 1215ebf99093STom Zanussi struct rchan_buf *rbuf = in->private_data; 1216ebf99093STom Zanussi unsigned int subbuf_size = rbuf->chan->subbuf_size; 121724da24deSTom Zanussi uint64_t pos = (uint64_t) *ppos; 121824da24deSTom Zanussi uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size; 121924da24deSTom Zanussi size_t read_start = (size_t) do_div(pos, alloc_size); 1220ebf99093STom Zanussi size_t read_subbuf = read_start / subbuf_size; 1221ebf99093STom Zanussi size_t padding = rbuf->padding[read_subbuf]; 1222ebf99093STom Zanussi size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding; 12231db60cf2SJens Axboe struct page *pages[PIPE_BUFFERS]; 12241db60cf2SJens Axboe struct partial_page partial[PIPE_BUFFERS]; 12251db60cf2SJens Axboe struct splice_pipe_desc spd = { 12261db60cf2SJens Axboe .pages = pages, 12271db60cf2SJens Axboe .nr_pages = 0, 12281db60cf2SJens Axboe .partial = partial, 12291db60cf2SJens Axboe .flags = flags, 12301db60cf2SJens Axboe .ops = &relay_pipe_buf_ops, 12315eb7f9faSJens Axboe .spd_release = relay_page_release, 12321db60cf2SJens Axboe }; 1233ebf99093STom Zanussi 1234ebf99093STom Zanussi if (rbuf->subbufs_produced == rbuf->subbufs_consumed) 1235ebf99093STom Zanussi return 0; 1236ebf99093STom Zanussi 12371db60cf2SJens Axboe /* 12381db60cf2SJens Axboe * Adjust read len, if longer than what is available 12391db60cf2SJens Axboe */ 12401db60cf2SJens Axboe if (len > (subbuf_size - read_start % subbuf_size)) 12411db60cf2SJens Axboe len = subbuf_size - read_start % subbuf_size; 1242ebf99093STom Zanussi 1243ebf99093STom Zanussi subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; 1244ebf99093STom Zanussi pidx = (read_start / PAGE_SIZE) % subbuf_pages; 1245ebf99093STom Zanussi poff = read_start & ~PAGE_MASK; 124616d54669SJens Axboe nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS); 1247ebf99093STom Zanussi 124816d54669SJens Axboe for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) { 12491db60cf2SJens Axboe unsigned int this_len, this_end, private; 12501db60cf2SJens Axboe unsigned int cur_pos = read_start + total_len; 1251ebf99093STom Zanussi 12521db60cf2SJens Axboe if (!len) 1253ebf99093STom Zanussi break; 12541db60cf2SJens Axboe 12551db60cf2SJens Axboe this_len = min_t(unsigned long, len, PAGE_SIZE - poff); 12561db60cf2SJens Axboe private = this_len; 12571db60cf2SJens Axboe 12581db60cf2SJens Axboe spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; 12591db60cf2SJens Axboe spd.partial[spd.nr_pages].offset = poff; 12601db60cf2SJens Axboe 12611db60cf2SJens Axboe this_end = cur_pos + this_len; 12621db60cf2SJens Axboe if (this_end >= nonpad_end) { 12631db60cf2SJens Axboe this_len = nonpad_end - cur_pos; 12641db60cf2SJens Axboe private = this_len + padding; 1265ebf99093STom Zanussi } 12661db60cf2SJens Axboe spd.partial[spd.nr_pages].len = this_len; 12671db60cf2SJens Axboe spd.partial[spd.nr_pages].private = private; 1268ebf99093STom Zanussi 12691db60cf2SJens Axboe len -= this_len; 12701db60cf2SJens Axboe total_len += this_len; 1271ebf99093STom Zanussi poff = 0; 1272ebf99093STom Zanussi pidx = (pidx + 1) % subbuf_pages; 1273ebf99093STom Zanussi 12741db60cf2SJens Axboe if (this_end >= nonpad_end) { 12751db60cf2SJens Axboe spd.nr_pages++; 1276ebf99093STom Zanussi break; 1277ebf99093STom Zanussi } 1278ebf99093STom Zanussi } 1279ebf99093STom Zanussi 12801db60cf2SJens Axboe if (!spd.nr_pages) 12811db60cf2SJens Axboe return 0; 1282ebf99093STom Zanussi 12831db60cf2SJens Axboe ret = *nonpad_ret = splice_to_pipe(pipe, &spd); 12841db60cf2SJens Axboe if (ret < 0 || ret < total_len) 12851db60cf2SJens Axboe return ret; 1286ebf99093STom Zanussi 12871db60cf2SJens Axboe if (read_start + ret == nonpad_end) 12881db60cf2SJens Axboe ret += padding; 1289ebf99093STom Zanussi 1290ebf99093STom Zanussi return ret; 1291ebf99093STom Zanussi } 1292ebf99093STom Zanussi 1293ebf99093STom Zanussi static ssize_t relay_file_splice_read(struct file *in, 1294ebf99093STom Zanussi loff_t *ppos, 1295ebf99093STom Zanussi struct pipe_inode_info *pipe, 1296ebf99093STom Zanussi size_t len, 1297ebf99093STom Zanussi unsigned int flags) 1298ebf99093STom Zanussi { 1299ebf99093STom Zanussi ssize_t spliced; 1300ebf99093STom Zanussi int ret; 1301ebf99093STom Zanussi int nonpad_ret = 0; 1302ebf99093STom Zanussi 1303ebf99093STom Zanussi ret = 0; 1304ebf99093STom Zanussi spliced = 0; 1305ebf99093STom Zanussi 1306a82c53a0STom Zanussi while (len && !spliced) { 1307ebf99093STom Zanussi ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret); 1308ebf99093STom Zanussi if (ret < 0) 1309ebf99093STom Zanussi break; 1310ebf99093STom Zanussi else if (!ret) { 1311ebf99093STom Zanussi if (spliced) 1312ebf99093STom Zanussi break; 1313ebf99093STom Zanussi if (flags & SPLICE_F_NONBLOCK) { 1314ebf99093STom Zanussi ret = -EAGAIN; 1315ebf99093STom Zanussi break; 1316ebf99093STom Zanussi } 1317ebf99093STom Zanussi } 1318ebf99093STom Zanussi 1319ebf99093STom Zanussi *ppos += ret; 1320ebf99093STom Zanussi if (ret > len) 1321ebf99093STom Zanussi len = 0; 1322ebf99093STom Zanussi else 1323ebf99093STom Zanussi len -= ret; 1324ebf99093STom Zanussi spliced += nonpad_ret; 1325ebf99093STom Zanussi nonpad_ret = 0; 1326ebf99093STom Zanussi } 1327ebf99093STom Zanussi 1328ebf99093STom Zanussi if (spliced) 1329ebf99093STom Zanussi return spliced; 1330ebf99093STom Zanussi 1331ebf99093STom Zanussi return ret; 1332221415d7SJens Axboe } 1333221415d7SJens Axboe 133415ad7cdcSHelge Deller const struct file_operations relay_file_operations = { 1335b86ff981SJens Axboe .open = relay_file_open, 1336b86ff981SJens Axboe .poll = relay_file_poll, 1337b86ff981SJens Axboe .mmap = relay_file_mmap, 1338b86ff981SJens Axboe .read = relay_file_read, 1339b86ff981SJens Axboe .llseek = no_llseek, 1340b86ff981SJens Axboe .release = relay_file_release, 1341ebf99093STom Zanussi .splice_read = relay_file_splice_read, 1342b86ff981SJens Axboe }; 1343b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations); 134423c88752SMathieu Desnoyers 134523c88752SMathieu Desnoyers static __init int relay_init(void) 134623c88752SMathieu Desnoyers { 134723c88752SMathieu Desnoyers 134823c88752SMathieu Desnoyers hotcpu_notifier(relay_hotcpu_callback, 0); 134923c88752SMathieu Desnoyers return 0; 135023c88752SMathieu Desnoyers } 135123c88752SMathieu Desnoyers 1352*20d8b67cSEduard - Gabriel Munteanu early_initcall(relay_init); 1353