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 68b86ff981SJens Axboe /** 69b86ff981SJens Axboe * relay_mmap_buf: - mmap channel buffer to process address space 70b86ff981SJens Axboe * @buf: relay channel buffer 71b86ff981SJens Axboe * @vma: vm_area_struct describing memory to be mapped 72b86ff981SJens Axboe * 73b86ff981SJens Axboe * Returns 0 if ok, negative on error 74b86ff981SJens Axboe * 75b86ff981SJens Axboe * Caller should already have grabbed mmap_sem. 76b86ff981SJens Axboe */ 7701c55ed3SAdrian Bunk static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) 78b86ff981SJens Axboe { 79b86ff981SJens Axboe unsigned long length = vma->vm_end - vma->vm_start; 80b86ff981SJens Axboe struct file *filp = vma->vm_file; 81b86ff981SJens Axboe 82b86ff981SJens Axboe if (!buf) 83b86ff981SJens Axboe return -EBADF; 84b86ff981SJens Axboe 85b86ff981SJens Axboe if (length != (unsigned long)buf->chan->alloc_size) 86b86ff981SJens Axboe return -EINVAL; 87b86ff981SJens Axboe 88b86ff981SJens Axboe vma->vm_ops = &relay_file_mmap_ops; 892f98735cSNick Piggin vma->vm_flags |= VM_DONTEXPAND; 90b86ff981SJens Axboe vma->vm_private_data = buf; 91b86ff981SJens Axboe buf->chan->cb->buf_mapped(buf, filp); 92b86ff981SJens Axboe 93b86ff981SJens Axboe return 0; 94b86ff981SJens Axboe } 95b86ff981SJens Axboe 96b86ff981SJens Axboe /** 97b86ff981SJens Axboe * relay_alloc_buf - allocate a channel buffer 98b86ff981SJens Axboe * @buf: the buffer struct 99b86ff981SJens Axboe * @size: total size of the buffer 100b86ff981SJens Axboe * 1014c78a663SRandy Dunlap * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The 102221415d7SJens Axboe * passed in size will get page aligned, if it isn't already. 103b86ff981SJens Axboe */ 104221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) 105b86ff981SJens Axboe { 106b86ff981SJens Axboe void *mem; 107b86ff981SJens Axboe unsigned int i, j, n_pages; 108b86ff981SJens Axboe 109221415d7SJens Axboe *size = PAGE_ALIGN(*size); 110221415d7SJens Axboe n_pages = *size >> PAGE_SHIFT; 111b86ff981SJens Axboe 112b86ff981SJens Axboe buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL); 113b86ff981SJens Axboe if (!buf->page_array) 114b86ff981SJens Axboe return NULL; 115b86ff981SJens Axboe 116b86ff981SJens Axboe for (i = 0; i < n_pages; i++) { 117b86ff981SJens Axboe buf->page_array[i] = alloc_page(GFP_KERNEL); 118b86ff981SJens Axboe if (unlikely(!buf->page_array[i])) 119b86ff981SJens Axboe goto depopulate; 120ebf99093STom Zanussi set_page_private(buf->page_array[i], (unsigned long)buf); 121b86ff981SJens Axboe } 122b86ff981SJens Axboe mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); 123b86ff981SJens Axboe if (!mem) 124b86ff981SJens Axboe goto depopulate; 125b86ff981SJens Axboe 126221415d7SJens Axboe memset(mem, 0, *size); 127b86ff981SJens Axboe buf->page_count = n_pages; 128b86ff981SJens Axboe return mem; 129b86ff981SJens Axboe 130b86ff981SJens Axboe depopulate: 131b86ff981SJens Axboe for (j = 0; j < i; j++) 132b86ff981SJens Axboe __free_page(buf->page_array[j]); 133b86ff981SJens Axboe kfree(buf->page_array); 134b86ff981SJens Axboe return NULL; 135b86ff981SJens Axboe } 136b86ff981SJens Axboe 137b86ff981SJens Axboe /** 138b86ff981SJens Axboe * relay_create_buf - allocate and initialize a channel buffer 1394c78a663SRandy Dunlap * @chan: the relay channel 140b86ff981SJens Axboe * 1414c78a663SRandy Dunlap * Returns channel buffer if successful, %NULL otherwise. 142b86ff981SJens Axboe */ 14301c55ed3SAdrian Bunk static struct rchan_buf *relay_create_buf(struct rchan *chan) 144b86ff981SJens Axboe { 145cd861280SRobert P. J. Day struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); 146b86ff981SJens Axboe if (!buf) 147b86ff981SJens Axboe return NULL; 148b86ff981SJens Axboe 149b86ff981SJens Axboe buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL); 150b86ff981SJens Axboe if (!buf->padding) 151b86ff981SJens Axboe goto free_buf; 152b86ff981SJens Axboe 153221415d7SJens Axboe buf->start = relay_alloc_buf(buf, &chan->alloc_size); 154b86ff981SJens Axboe if (!buf->start) 155b86ff981SJens Axboe goto free_buf; 156b86ff981SJens Axboe 157b86ff981SJens Axboe buf->chan = chan; 158b86ff981SJens Axboe kref_get(&buf->chan->kref); 159b86ff981SJens Axboe return buf; 160b86ff981SJens Axboe 161b86ff981SJens Axboe free_buf: 162b86ff981SJens Axboe kfree(buf->padding); 163b86ff981SJens Axboe kfree(buf); 164b86ff981SJens Axboe return NULL; 165b86ff981SJens Axboe } 166b86ff981SJens Axboe 167b86ff981SJens Axboe /** 168b86ff981SJens Axboe * relay_destroy_channel - free the channel struct 1694c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay channel 170b86ff981SJens Axboe * 171b86ff981SJens Axboe * Should only be called from kref_put(). 172b86ff981SJens Axboe */ 17301c55ed3SAdrian Bunk static void relay_destroy_channel(struct kref *kref) 174b86ff981SJens Axboe { 175b86ff981SJens Axboe struct rchan *chan = container_of(kref, struct rchan, kref); 176b86ff981SJens Axboe kfree(chan); 177b86ff981SJens Axboe } 178b86ff981SJens Axboe 179b86ff981SJens Axboe /** 180b86ff981SJens Axboe * relay_destroy_buf - destroy an rchan_buf struct and associated buffer 181b86ff981SJens Axboe * @buf: the buffer struct 182b86ff981SJens Axboe */ 18301c55ed3SAdrian Bunk static void relay_destroy_buf(struct rchan_buf *buf) 184b86ff981SJens Axboe { 185b86ff981SJens Axboe struct rchan *chan = buf->chan; 186b86ff981SJens Axboe unsigned int i; 187b86ff981SJens Axboe 188b86ff981SJens Axboe if (likely(buf->start)) { 189b86ff981SJens Axboe vunmap(buf->start); 190b86ff981SJens Axboe for (i = 0; i < buf->page_count; i++) 191b86ff981SJens Axboe __free_page(buf->page_array[i]); 192b86ff981SJens Axboe kfree(buf->page_array); 193b86ff981SJens Axboe } 19423c88752SMathieu Desnoyers chan->buf[buf->cpu] = NULL; 195b86ff981SJens Axboe kfree(buf->padding); 196b86ff981SJens Axboe kfree(buf); 197b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 198b86ff981SJens Axboe } 199b86ff981SJens Axboe 200b86ff981SJens Axboe /** 201b86ff981SJens Axboe * relay_remove_buf - remove a channel buffer 2024c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay buffer 203b86ff981SJens Axboe * 204b86ff981SJens Axboe * Removes the file from the fileystem, which also frees the 205b86ff981SJens Axboe * rchan_buf_struct and the channel buffer. Should only be called from 206b86ff981SJens Axboe * kref_put(). 207b86ff981SJens Axboe */ 20801c55ed3SAdrian Bunk static void relay_remove_buf(struct kref *kref) 209b86ff981SJens Axboe { 210b86ff981SJens Axboe struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); 211b86ff981SJens Axboe buf->chan->cb->remove_buf_file(buf->dentry); 212b86ff981SJens Axboe relay_destroy_buf(buf); 213b86ff981SJens Axboe } 214b86ff981SJens Axboe 215b86ff981SJens Axboe /** 216b86ff981SJens Axboe * relay_buf_empty - boolean, is the channel buffer empty? 217b86ff981SJens Axboe * @buf: channel buffer 218b86ff981SJens Axboe * 219b86ff981SJens Axboe * Returns 1 if the buffer is empty, 0 otherwise. 220b86ff981SJens Axboe */ 22101c55ed3SAdrian Bunk static int relay_buf_empty(struct rchan_buf *buf) 222b86ff981SJens Axboe { 223b86ff981SJens Axboe return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; 224b86ff981SJens Axboe } 225b86ff981SJens Axboe 226b86ff981SJens Axboe /** 227b86ff981SJens Axboe * relay_buf_full - boolean, is the channel buffer full? 228b86ff981SJens Axboe * @buf: channel buffer 229b86ff981SJens Axboe * 230b86ff981SJens Axboe * Returns 1 if the buffer is full, 0 otherwise. 231b86ff981SJens Axboe */ 232b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf) 233b86ff981SJens Axboe { 234b86ff981SJens Axboe size_t ready = buf->subbufs_produced - buf->subbufs_consumed; 235b86ff981SJens Axboe return (ready >= buf->chan->n_subbufs) ? 1 : 0; 236b86ff981SJens Axboe } 237b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full); 238b86ff981SJens Axboe 239b86ff981SJens Axboe /* 240b86ff981SJens Axboe * High-level relay kernel API and associated functions. 241b86ff981SJens Axboe */ 242b86ff981SJens Axboe 243b86ff981SJens Axboe /* 244b86ff981SJens Axboe * rchan_callback implementations defining default channel behavior. Used 245b86ff981SJens Axboe * in place of corresponding NULL values in client callback struct. 246b86ff981SJens Axboe */ 247b86ff981SJens Axboe 248b86ff981SJens Axboe /* 249b86ff981SJens Axboe * subbuf_start() default callback. Does nothing. 250b86ff981SJens Axboe */ 251b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf, 252b86ff981SJens Axboe void *subbuf, 253b86ff981SJens Axboe void *prev_subbuf, 254b86ff981SJens Axboe size_t prev_padding) 255b86ff981SJens Axboe { 256b86ff981SJens Axboe if (relay_buf_full(buf)) 257b86ff981SJens Axboe return 0; 258b86ff981SJens Axboe 259b86ff981SJens Axboe return 1; 260b86ff981SJens Axboe } 261b86ff981SJens Axboe 262b86ff981SJens Axboe /* 263b86ff981SJens Axboe * buf_mapped() default callback. Does nothing. 264b86ff981SJens Axboe */ 265b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf, 266b86ff981SJens Axboe struct file *filp) 267b86ff981SJens Axboe { 268b86ff981SJens Axboe } 269b86ff981SJens Axboe 270b86ff981SJens Axboe /* 271b86ff981SJens Axboe * buf_unmapped() default callback. Does nothing. 272b86ff981SJens Axboe */ 273b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf, 274b86ff981SJens Axboe struct file *filp) 275b86ff981SJens Axboe { 276b86ff981SJens Axboe } 277b86ff981SJens Axboe 278b86ff981SJens Axboe /* 279b86ff981SJens Axboe * create_buf_file_create() default callback. Does nothing. 280b86ff981SJens Axboe */ 281b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename, 282b86ff981SJens Axboe struct dentry *parent, 283b86ff981SJens Axboe int mode, 284b86ff981SJens Axboe struct rchan_buf *buf, 285b86ff981SJens Axboe int *is_global) 286b86ff981SJens Axboe { 287b86ff981SJens Axboe return NULL; 288b86ff981SJens Axboe } 289b86ff981SJens Axboe 290b86ff981SJens Axboe /* 291b86ff981SJens Axboe * remove_buf_file() default callback. Does nothing. 292b86ff981SJens Axboe */ 293b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry) 294b86ff981SJens Axboe { 295b86ff981SJens Axboe return -EINVAL; 296b86ff981SJens Axboe } 297b86ff981SJens Axboe 298b86ff981SJens Axboe /* relay channel default callbacks */ 299b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = { 300b86ff981SJens Axboe .subbuf_start = subbuf_start_default_callback, 301b86ff981SJens Axboe .buf_mapped = buf_mapped_default_callback, 302b86ff981SJens Axboe .buf_unmapped = buf_unmapped_default_callback, 303b86ff981SJens Axboe .create_buf_file = create_buf_file_default_callback, 304b86ff981SJens Axboe .remove_buf_file = remove_buf_file_default_callback, 305b86ff981SJens Axboe }; 306b86ff981SJens Axboe 307b86ff981SJens Axboe /** 308b86ff981SJens Axboe * wakeup_readers - wake up readers waiting on a channel 3099a9136e2SLinus Torvalds * @data: contains the channel buffer 310b86ff981SJens Axboe * 3117c9cb383STom Zanussi * This is the timer function used to defer reader waking. 312b86ff981SJens Axboe */ 3137c9cb383STom Zanussi static void wakeup_readers(unsigned long data) 314b86ff981SJens Axboe { 3157c9cb383STom Zanussi struct rchan_buf *buf = (struct rchan_buf *)data; 316b86ff981SJens Axboe wake_up_interruptible(&buf->read_wait); 317b86ff981SJens Axboe } 318b86ff981SJens Axboe 319b86ff981SJens Axboe /** 320b86ff981SJens Axboe * __relay_reset - reset a channel buffer 321b86ff981SJens Axboe * @buf: the channel buffer 322b86ff981SJens Axboe * @init: 1 if this is a first-time initialization 323b86ff981SJens Axboe * 32472fd4a35SRobert P. J. Day * See relay_reset() for description of effect. 325b86ff981SJens Axboe */ 326192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init) 327b86ff981SJens Axboe { 328b86ff981SJens Axboe size_t i; 329b86ff981SJens Axboe 330b86ff981SJens Axboe if (init) { 331b86ff981SJens Axboe init_waitqueue_head(&buf->read_wait); 332b86ff981SJens Axboe kref_init(&buf->kref); 3337c9cb383STom Zanussi setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf); 3347c9cb383STom Zanussi } else 3357c9cb383STom Zanussi del_timer_sync(&buf->timer); 336b86ff981SJens Axboe 337b86ff981SJens Axboe buf->subbufs_produced = 0; 338b86ff981SJens Axboe buf->subbufs_consumed = 0; 339b86ff981SJens Axboe buf->bytes_consumed = 0; 340b86ff981SJens Axboe buf->finalized = 0; 341b86ff981SJens Axboe buf->data = buf->start; 342b86ff981SJens Axboe buf->offset = 0; 343b86ff981SJens Axboe 344b86ff981SJens Axboe for (i = 0; i < buf->chan->n_subbufs; i++) 345b86ff981SJens Axboe buf->padding[i] = 0; 346b86ff981SJens Axboe 347b86ff981SJens Axboe buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); 348b86ff981SJens Axboe } 349b86ff981SJens Axboe 350b86ff981SJens Axboe /** 351b86ff981SJens Axboe * relay_reset - reset the channel 352b86ff981SJens Axboe * @chan: the channel 353b86ff981SJens Axboe * 354b86ff981SJens Axboe * This has the effect of erasing all data from all channel buffers 355b86ff981SJens Axboe * and restarting the channel in its initial state. The buffers 356b86ff981SJens Axboe * are not freed, so any mappings are still in effect. 357b86ff981SJens Axboe * 35872fd4a35SRobert P. J. Day * NOTE. Care should be taken that the channel isn't actually 359b86ff981SJens Axboe * being used by anything when this call is made. 360b86ff981SJens Axboe */ 361b86ff981SJens Axboe void relay_reset(struct rchan *chan) 362b86ff981SJens Axboe { 363b86ff981SJens Axboe unsigned int i; 364b86ff981SJens Axboe 365b86ff981SJens Axboe if (!chan) 366b86ff981SJens Axboe return; 367b86ff981SJens Axboe 36823c88752SMathieu Desnoyers if (chan->is_global && chan->buf[0]) { 36923c88752SMathieu Desnoyers __relay_reset(chan->buf[0], 0); 37023c88752SMathieu Desnoyers return; 371b86ff981SJens Axboe } 37223c88752SMathieu Desnoyers 37323c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 37423c88752SMathieu Desnoyers for_each_online_cpu(i) 37523c88752SMathieu Desnoyers if (chan->buf[i]) 37623c88752SMathieu Desnoyers __relay_reset(chan->buf[i], 0); 37723c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 378b86ff981SJens Axboe } 379b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset); 380b86ff981SJens Axboe 3814c78a663SRandy Dunlap /* 382b86ff981SJens Axboe * relay_open_buf - create a new relay channel buffer 383b86ff981SJens Axboe * 38423c88752SMathieu Desnoyers * used by relay_open() and CPU hotplug. 385b86ff981SJens Axboe */ 38623c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) 387b86ff981SJens Axboe { 38823c88752SMathieu Desnoyers struct rchan_buf *buf = NULL; 389b86ff981SJens Axboe struct dentry *dentry; 39023c88752SMathieu Desnoyers char *tmpname; 391b86ff981SJens Axboe 39223c88752SMathieu Desnoyers if (chan->is_global) 393b86ff981SJens Axboe return chan->buf[0]; 394b86ff981SJens Axboe 39523c88752SMathieu Desnoyers tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); 39623c88752SMathieu Desnoyers if (!tmpname) 39723c88752SMathieu Desnoyers goto end; 39823c88752SMathieu Desnoyers snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu); 39923c88752SMathieu Desnoyers 400b86ff981SJens Axboe buf = relay_create_buf(chan); 401b86ff981SJens Axboe if (!buf) 40223c88752SMathieu Desnoyers goto free_name; 403b86ff981SJens Axboe 40423c88752SMathieu Desnoyers buf->cpu = cpu; 405b86ff981SJens Axboe __relay_reset(buf, 1); 406b86ff981SJens Axboe 40723c88752SMathieu Desnoyers /* Create file in fs */ 40823c88752SMathieu Desnoyers dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR, 40923c88752SMathieu Desnoyers buf, &chan->is_global); 41023c88752SMathieu Desnoyers if (!dentry) 41123c88752SMathieu Desnoyers goto free_buf; 41223c88752SMathieu Desnoyers 41323c88752SMathieu Desnoyers buf->dentry = dentry; 41423c88752SMathieu Desnoyers 41523c88752SMathieu Desnoyers if(chan->is_global) { 41623c88752SMathieu Desnoyers chan->buf[0] = buf; 41723c88752SMathieu Desnoyers buf->cpu = 0; 41823c88752SMathieu Desnoyers } 41923c88752SMathieu Desnoyers 42023c88752SMathieu Desnoyers goto free_name; 42123c88752SMathieu Desnoyers 42223c88752SMathieu Desnoyers free_buf: 42323c88752SMathieu Desnoyers relay_destroy_buf(buf); 424c9b3febcSJesper Juhl buf = NULL; 42523c88752SMathieu Desnoyers free_name: 42623c88752SMathieu Desnoyers kfree(tmpname); 42723c88752SMathieu Desnoyers end: 428b86ff981SJens Axboe return buf; 429b86ff981SJens Axboe } 430b86ff981SJens Axboe 431b86ff981SJens Axboe /** 432b86ff981SJens Axboe * relay_close_buf - close a channel buffer 433b86ff981SJens Axboe * @buf: channel buffer 434b86ff981SJens Axboe * 435b86ff981SJens Axboe * Marks the buffer finalized and restores the default callbacks. 436b86ff981SJens Axboe * The channel buffer and channel buffer data structure are then freed 437b86ff981SJens Axboe * automatically when the last reference is given up. 438b86ff981SJens Axboe */ 439192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf) 440b86ff981SJens Axboe { 441b86ff981SJens Axboe buf->finalized = 1; 4427c9cb383STom Zanussi del_timer_sync(&buf->timer); 443b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 444b86ff981SJens Axboe } 445b86ff981SJens Axboe 446192636adSAndrew Morton static void setup_callbacks(struct rchan *chan, 447b86ff981SJens Axboe struct rchan_callbacks *cb) 448b86ff981SJens Axboe { 449b86ff981SJens Axboe if (!cb) { 450b86ff981SJens Axboe chan->cb = &default_channel_callbacks; 451b86ff981SJens Axboe return; 452b86ff981SJens Axboe } 453b86ff981SJens Axboe 454b86ff981SJens Axboe if (!cb->subbuf_start) 455b86ff981SJens Axboe cb->subbuf_start = subbuf_start_default_callback; 456b86ff981SJens Axboe if (!cb->buf_mapped) 457b86ff981SJens Axboe cb->buf_mapped = buf_mapped_default_callback; 458b86ff981SJens Axboe if (!cb->buf_unmapped) 459b86ff981SJens Axboe cb->buf_unmapped = buf_unmapped_default_callback; 460b86ff981SJens Axboe if (!cb->create_buf_file) 461b86ff981SJens Axboe cb->create_buf_file = create_buf_file_default_callback; 462b86ff981SJens Axboe if (!cb->remove_buf_file) 463b86ff981SJens Axboe cb->remove_buf_file = remove_buf_file_default_callback; 464b86ff981SJens Axboe chan->cb = cb; 465b86ff981SJens Axboe } 466b86ff981SJens Axboe 467b86ff981SJens Axboe /** 46823c88752SMathieu Desnoyers * relay_hotcpu_callback - CPU hotplug callback 46923c88752SMathieu Desnoyers * @nb: notifier block 47023c88752SMathieu Desnoyers * @action: hotplug action to take 47123c88752SMathieu Desnoyers * @hcpu: CPU number 47223c88752SMathieu Desnoyers * 47305fb6bf0SRandy Dunlap * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) 47423c88752SMathieu Desnoyers */ 47523c88752SMathieu Desnoyers static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb, 47623c88752SMathieu Desnoyers unsigned long action, 47723c88752SMathieu Desnoyers void *hcpu) 47823c88752SMathieu Desnoyers { 47923c88752SMathieu Desnoyers unsigned int hotcpu = (unsigned long)hcpu; 48023c88752SMathieu Desnoyers struct rchan *chan; 48123c88752SMathieu Desnoyers 48223c88752SMathieu Desnoyers switch(action) { 48323c88752SMathieu Desnoyers case CPU_UP_PREPARE: 4848bb78442SRafael J. Wysocki case CPU_UP_PREPARE_FROZEN: 48523c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 48623c88752SMathieu Desnoyers list_for_each_entry(chan, &relay_channels, list) { 48723c88752SMathieu Desnoyers if (chan->buf[hotcpu]) 48823c88752SMathieu Desnoyers continue; 48923c88752SMathieu Desnoyers chan->buf[hotcpu] = relay_open_buf(chan, hotcpu); 49023c88752SMathieu Desnoyers if(!chan->buf[hotcpu]) { 49123c88752SMathieu Desnoyers printk(KERN_ERR 49223c88752SMathieu Desnoyers "relay_hotcpu_callback: cpu %d buffer " 49323c88752SMathieu Desnoyers "creation failed\n", hotcpu); 49423c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 49523c88752SMathieu Desnoyers return NOTIFY_BAD; 49623c88752SMathieu Desnoyers } 49723c88752SMathieu Desnoyers } 49823c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 49923c88752SMathieu Desnoyers break; 50023c88752SMathieu Desnoyers case CPU_DEAD: 5018bb78442SRafael J. Wysocki case CPU_DEAD_FROZEN: 50223c88752SMathieu Desnoyers /* No need to flush the cpu : will be flushed upon 50323c88752SMathieu Desnoyers * final relay_flush() call. */ 50423c88752SMathieu Desnoyers break; 50523c88752SMathieu Desnoyers } 50623c88752SMathieu Desnoyers return NOTIFY_OK; 50723c88752SMathieu Desnoyers } 50823c88752SMathieu Desnoyers 50923c88752SMathieu Desnoyers /** 510b86ff981SJens Axboe * relay_open - create a new relay channel 511b86ff981SJens Axboe * @base_filename: base name of files to create 5124c78a663SRandy Dunlap * @parent: dentry of parent directory, %NULL for root directory 513b86ff981SJens Axboe * @subbuf_size: size of sub-buffers 514b86ff981SJens Axboe * @n_subbufs: number of sub-buffers 515b86ff981SJens Axboe * @cb: client callback functions 51623c88752SMathieu Desnoyers * @private_data: user-defined data 517b86ff981SJens Axboe * 5184c78a663SRandy Dunlap * Returns channel pointer if successful, %NULL otherwise. 519b86ff981SJens Axboe * 520b86ff981SJens Axboe * Creates a channel buffer for each cpu using the sizes and 521b86ff981SJens Axboe * attributes specified. The created channel buffer files 522b86ff981SJens Axboe * will be named base_filename0...base_filenameN-1. File 52372fd4a35SRobert P. J. Day * permissions will be %S_IRUSR. 524b86ff981SJens Axboe */ 525b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename, 526b86ff981SJens Axboe struct dentry *parent, 527b86ff981SJens Axboe size_t subbuf_size, 528b86ff981SJens Axboe size_t n_subbufs, 52923c88752SMathieu Desnoyers struct rchan_callbacks *cb, 53023c88752SMathieu Desnoyers void *private_data) 531b86ff981SJens Axboe { 532b86ff981SJens Axboe unsigned int i; 533b86ff981SJens Axboe struct rchan *chan; 534b86ff981SJens Axboe if (!base_filename) 535b86ff981SJens Axboe return NULL; 536b86ff981SJens Axboe 537b86ff981SJens Axboe if (!(subbuf_size && n_subbufs)) 538b86ff981SJens Axboe return NULL; 539b86ff981SJens Axboe 540cd861280SRobert P. J. Day chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); 541b86ff981SJens Axboe if (!chan) 542b86ff981SJens Axboe return NULL; 543b86ff981SJens Axboe 544b86ff981SJens Axboe chan->version = RELAYFS_CHANNEL_VERSION; 545b86ff981SJens Axboe chan->n_subbufs = n_subbufs; 546b86ff981SJens Axboe chan->subbuf_size = subbuf_size; 547b86ff981SJens Axboe chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs); 54823c88752SMathieu Desnoyers chan->parent = parent; 54923c88752SMathieu Desnoyers chan->private_data = private_data; 55023c88752SMathieu Desnoyers strlcpy(chan->base_filename, base_filename, NAME_MAX); 551b86ff981SJens Axboe setup_callbacks(chan, cb); 552b86ff981SJens Axboe kref_init(&chan->kref); 553b86ff981SJens Axboe 55423c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 555b86ff981SJens Axboe for_each_online_cpu(i) { 55623c88752SMathieu Desnoyers chan->buf[i] = relay_open_buf(chan, i); 557b86ff981SJens Axboe if (!chan->buf[i]) 558b86ff981SJens Axboe goto free_bufs; 559b86ff981SJens Axboe } 56023c88752SMathieu Desnoyers list_add(&chan->list, &relay_channels); 56123c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 562b86ff981SJens Axboe 563b86ff981SJens Axboe return chan; 564b86ff981SJens Axboe 565b86ff981SJens Axboe free_bufs: 56623c88752SMathieu Desnoyers for_each_online_cpu(i) { 567b86ff981SJens Axboe if (!chan->buf[i]) 568b86ff981SJens Axboe break; 569b86ff981SJens Axboe relay_close_buf(chan->buf[i]); 570b86ff981SJens Axboe } 571b86ff981SJens Axboe 572b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 57323c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 574b86ff981SJens Axboe return NULL; 575b86ff981SJens Axboe } 576b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open); 577b86ff981SJens Axboe 578b86ff981SJens Axboe /** 579b86ff981SJens Axboe * relay_switch_subbuf - switch to a new sub-buffer 580b86ff981SJens Axboe * @buf: channel buffer 581b86ff981SJens Axboe * @length: size of current event 582b86ff981SJens Axboe * 583b86ff981SJens Axboe * Returns either the length passed in or 0 if full. 584b86ff981SJens Axboe * 585b86ff981SJens Axboe * Performs sub-buffer-switch tasks such as invoking callbacks, 586b86ff981SJens Axboe * updating padding counts, waking up readers, etc. 587b86ff981SJens Axboe */ 588b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) 589b86ff981SJens Axboe { 590b86ff981SJens Axboe void *old, *new; 591b86ff981SJens Axboe size_t old_subbuf, new_subbuf; 592b86ff981SJens Axboe 593b86ff981SJens Axboe if (unlikely(length > buf->chan->subbuf_size)) 594b86ff981SJens Axboe goto toobig; 595b86ff981SJens Axboe 596b86ff981SJens Axboe if (buf->offset != buf->chan->subbuf_size + 1) { 597b86ff981SJens Axboe buf->prev_padding = buf->chan->subbuf_size - buf->offset; 598b86ff981SJens Axboe old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 599b86ff981SJens Axboe buf->padding[old_subbuf] = buf->prev_padding; 600b86ff981SJens Axboe buf->subbufs_produced++; 601221415d7SJens Axboe buf->dentry->d_inode->i_size += buf->chan->subbuf_size - 602221415d7SJens Axboe buf->padding[old_subbuf]; 603221415d7SJens Axboe smp_mb(); 6047c9cb383STom Zanussi if (waitqueue_active(&buf->read_wait)) 6057c9cb383STom Zanussi /* 6067c9cb383STom Zanussi * Calling wake_up_interruptible() from here 6077c9cb383STom Zanussi * will deadlock if we happen to be logging 6087c9cb383STom Zanussi * from the scheduler (trying to re-grab 6097c9cb383STom Zanussi * rq->lock), so defer it. 6107c9cb383STom Zanussi */ 6117c9cb383STom Zanussi __mod_timer(&buf->timer, jiffies + 1); 612b86ff981SJens Axboe } 613b86ff981SJens Axboe 614b86ff981SJens Axboe old = buf->data; 615b86ff981SJens Axboe new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 616b86ff981SJens Axboe new = buf->start + new_subbuf * buf->chan->subbuf_size; 617b86ff981SJens Axboe buf->offset = 0; 618b86ff981SJens Axboe if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { 619b86ff981SJens Axboe buf->offset = buf->chan->subbuf_size + 1; 620b86ff981SJens Axboe return 0; 621b86ff981SJens Axboe } 622b86ff981SJens Axboe buf->data = new; 623b86ff981SJens Axboe buf->padding[new_subbuf] = 0; 624b86ff981SJens Axboe 625b86ff981SJens Axboe if (unlikely(length + buf->offset > buf->chan->subbuf_size)) 626b86ff981SJens Axboe goto toobig; 627b86ff981SJens Axboe 628b86ff981SJens Axboe return length; 629b86ff981SJens Axboe 630b86ff981SJens Axboe toobig: 631b86ff981SJens Axboe buf->chan->last_toobig = length; 632b86ff981SJens Axboe return 0; 633b86ff981SJens Axboe } 634b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf); 635b86ff981SJens Axboe 636b86ff981SJens Axboe /** 637b86ff981SJens Axboe * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count 638b86ff981SJens Axboe * @chan: the channel 639b86ff981SJens Axboe * @cpu: the cpu associated with the channel buffer to update 640b86ff981SJens Axboe * @subbufs_consumed: number of sub-buffers to add to current buf's count 641b86ff981SJens Axboe * 642b86ff981SJens Axboe * Adds to the channel buffer's consumed sub-buffer count. 643b86ff981SJens Axboe * subbufs_consumed should be the number of sub-buffers newly consumed, 644b86ff981SJens Axboe * not the total consumed. 645b86ff981SJens Axboe * 64672fd4a35SRobert P. J. Day * NOTE. Kernel clients don't need to call this function if the channel 647b86ff981SJens Axboe * mode is 'overwrite'. 648b86ff981SJens Axboe */ 649b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan, 650b86ff981SJens Axboe unsigned int cpu, 651b86ff981SJens Axboe size_t subbufs_consumed) 652b86ff981SJens Axboe { 653b86ff981SJens Axboe struct rchan_buf *buf; 654b86ff981SJens Axboe 655b86ff981SJens Axboe if (!chan) 656b86ff981SJens Axboe return; 657b86ff981SJens Axboe 658b86ff981SJens Axboe if (cpu >= NR_CPUS || !chan->buf[cpu]) 659b86ff981SJens Axboe return; 660b86ff981SJens Axboe 661b86ff981SJens Axboe buf = chan->buf[cpu]; 662b86ff981SJens Axboe buf->subbufs_consumed += subbufs_consumed; 663b86ff981SJens Axboe if (buf->subbufs_consumed > buf->subbufs_produced) 664b86ff981SJens Axboe buf->subbufs_consumed = buf->subbufs_produced; 665b86ff981SJens Axboe } 666b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed); 667b86ff981SJens Axboe 668b86ff981SJens Axboe /** 669b86ff981SJens Axboe * relay_close - close the channel 670b86ff981SJens Axboe * @chan: the channel 671b86ff981SJens Axboe * 672b86ff981SJens Axboe * Closes all channel buffers and frees the channel. 673b86ff981SJens Axboe */ 674b86ff981SJens Axboe void relay_close(struct rchan *chan) 675b86ff981SJens Axboe { 676b86ff981SJens Axboe unsigned int i; 677b86ff981SJens Axboe 678b86ff981SJens Axboe if (!chan) 679b86ff981SJens Axboe return; 680b86ff981SJens Axboe 68123c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 68223c88752SMathieu Desnoyers if (chan->is_global && chan->buf[0]) 68323c88752SMathieu Desnoyers relay_close_buf(chan->buf[0]); 68423c88752SMathieu Desnoyers else 68523c88752SMathieu Desnoyers for_each_possible_cpu(i) 68623c88752SMathieu Desnoyers if (chan->buf[i]) 687b86ff981SJens Axboe relay_close_buf(chan->buf[i]); 688b86ff981SJens Axboe 689b86ff981SJens Axboe if (chan->last_toobig) 690b86ff981SJens Axboe printk(KERN_WARNING "relay: one or more items not logged " 691b86ff981SJens Axboe "[item size (%Zd) > sub-buffer size (%Zd)]\n", 692b86ff981SJens Axboe chan->last_toobig, chan->subbuf_size); 693b86ff981SJens Axboe 69423c88752SMathieu Desnoyers list_del(&chan->list); 695b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 69623c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 697b86ff981SJens Axboe } 698b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close); 699b86ff981SJens Axboe 700b86ff981SJens Axboe /** 701b86ff981SJens Axboe * relay_flush - close the channel 702b86ff981SJens Axboe * @chan: the channel 703b86ff981SJens Axboe * 7044c78a663SRandy Dunlap * Flushes all channel buffers, i.e. forces buffer switch. 705b86ff981SJens Axboe */ 706b86ff981SJens Axboe void relay_flush(struct rchan *chan) 707b86ff981SJens Axboe { 708b86ff981SJens Axboe unsigned int i; 709b86ff981SJens Axboe 710b86ff981SJens Axboe if (!chan) 711b86ff981SJens Axboe return; 712b86ff981SJens Axboe 71323c88752SMathieu Desnoyers if (chan->is_global && chan->buf[0]) { 71423c88752SMathieu Desnoyers relay_switch_subbuf(chan->buf[0], 0); 71523c88752SMathieu Desnoyers return; 716b86ff981SJens Axboe } 71723c88752SMathieu Desnoyers 71823c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 71923c88752SMathieu Desnoyers for_each_possible_cpu(i) 72023c88752SMathieu Desnoyers if (chan->buf[i]) 72123c88752SMathieu Desnoyers relay_switch_subbuf(chan->buf[i], 0); 72223c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 723b86ff981SJens Axboe } 724b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush); 725b86ff981SJens Axboe 726b86ff981SJens Axboe /** 727b86ff981SJens Axboe * relay_file_open - open file op for relay files 728b86ff981SJens Axboe * @inode: the inode 729b86ff981SJens Axboe * @filp: the file 730b86ff981SJens Axboe * 731b86ff981SJens Axboe * Increments the channel buffer refcount. 732b86ff981SJens Axboe */ 733b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp) 734b86ff981SJens Axboe { 7358e18e294STheodore Ts'o struct rchan_buf *buf = inode->i_private; 736b86ff981SJens Axboe kref_get(&buf->kref); 737b86ff981SJens Axboe filp->private_data = buf; 738b86ff981SJens Axboe 739*37529fe9SLai Jiangshan return nonseekable_open(inode, filp); 740b86ff981SJens Axboe } 741b86ff981SJens Axboe 742b86ff981SJens Axboe /** 743b86ff981SJens Axboe * relay_file_mmap - mmap file op for relay files 744b86ff981SJens Axboe * @filp: the file 745b86ff981SJens Axboe * @vma: the vma describing what to map 746b86ff981SJens Axboe * 74772fd4a35SRobert P. J. Day * Calls upon relay_mmap_buf() to map the file into user space. 748b86ff981SJens Axboe */ 749b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) 750b86ff981SJens Axboe { 751b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 752b86ff981SJens Axboe return relay_mmap_buf(buf, vma); 753b86ff981SJens Axboe } 754b86ff981SJens Axboe 755b86ff981SJens Axboe /** 756b86ff981SJens Axboe * relay_file_poll - poll file op for relay files 757b86ff981SJens Axboe * @filp: the file 758b86ff981SJens Axboe * @wait: poll table 759b86ff981SJens Axboe * 760b86ff981SJens Axboe * Poll implemention. 761b86ff981SJens Axboe */ 762b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait) 763b86ff981SJens Axboe { 764b86ff981SJens Axboe unsigned int mask = 0; 765b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 766b86ff981SJens Axboe 767b86ff981SJens Axboe if (buf->finalized) 768b86ff981SJens Axboe return POLLERR; 769b86ff981SJens Axboe 770b86ff981SJens Axboe if (filp->f_mode & FMODE_READ) { 771b86ff981SJens Axboe poll_wait(filp, &buf->read_wait, wait); 772b86ff981SJens Axboe if (!relay_buf_empty(buf)) 773b86ff981SJens Axboe mask |= POLLIN | POLLRDNORM; 774b86ff981SJens Axboe } 775b86ff981SJens Axboe 776b86ff981SJens Axboe return mask; 777b86ff981SJens Axboe } 778b86ff981SJens Axboe 779b86ff981SJens Axboe /** 780b86ff981SJens Axboe * relay_file_release - release file op for relay files 781b86ff981SJens Axboe * @inode: the inode 782b86ff981SJens Axboe * @filp: the file 783b86ff981SJens Axboe * 784b86ff981SJens Axboe * Decrements the channel refcount, as the filesystem is 785b86ff981SJens Axboe * no longer using it. 786b86ff981SJens Axboe */ 787b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp) 788b86ff981SJens Axboe { 789b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 790b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 791b86ff981SJens Axboe 792b86ff981SJens Axboe return 0; 793b86ff981SJens Axboe } 794b86ff981SJens Axboe 7954c78a663SRandy Dunlap /* 796b86ff981SJens Axboe * relay_file_read_consume - update the consumed count for the buffer 797b86ff981SJens Axboe */ 798b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf, 799b86ff981SJens Axboe size_t read_pos, 800b86ff981SJens Axboe size_t bytes_consumed) 801b86ff981SJens Axboe { 802b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 803b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 804b86ff981SJens Axboe size_t read_subbuf; 805b86ff981SJens Axboe 806b86ff981SJens Axboe if (buf->bytes_consumed + bytes_consumed > subbuf_size) { 807b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 808b86ff981SJens Axboe buf->bytes_consumed = 0; 809b86ff981SJens Axboe } 810b86ff981SJens Axboe 811b86ff981SJens Axboe buf->bytes_consumed += bytes_consumed; 812a66e356cSMasami Hiramatsu if (!read_pos) 813a66e356cSMasami Hiramatsu read_subbuf = buf->subbufs_consumed % n_subbufs; 814a66e356cSMasami Hiramatsu else 815b86ff981SJens Axboe read_subbuf = read_pos / buf->chan->subbuf_size; 816b86ff981SJens Axboe if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { 817b86ff981SJens Axboe if ((read_subbuf == buf->subbufs_produced % n_subbufs) && 818b86ff981SJens Axboe (buf->offset == subbuf_size)) 819b86ff981SJens Axboe return; 820b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 821b86ff981SJens Axboe buf->bytes_consumed = 0; 822b86ff981SJens Axboe } 823b86ff981SJens Axboe } 824b86ff981SJens Axboe 8254c78a663SRandy Dunlap /* 826b86ff981SJens Axboe * relay_file_read_avail - boolean, are there unconsumed bytes available? 827b86ff981SJens Axboe */ 828b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) 829b86ff981SJens Axboe { 830b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 831b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 832221415d7SJens Axboe size_t produced = buf->subbufs_produced; 833221415d7SJens Axboe size_t consumed = buf->subbufs_consumed; 834b86ff981SJens Axboe 835b86ff981SJens Axboe relay_file_read_consume(buf, read_pos, 0); 836b86ff981SJens Axboe 837221415d7SJens Axboe if (unlikely(buf->offset > subbuf_size)) { 838221415d7SJens Axboe if (produced == consumed) 839221415d7SJens Axboe return 0; 840221415d7SJens Axboe return 1; 841221415d7SJens Axboe } 842221415d7SJens Axboe 843221415d7SJens Axboe if (unlikely(produced - consumed >= n_subbufs)) { 844a66e356cSMasami Hiramatsu consumed = produced - n_subbufs + 1; 845221415d7SJens Axboe buf->subbufs_consumed = consumed; 846a66e356cSMasami Hiramatsu buf->bytes_consumed = 0; 847221415d7SJens Axboe } 848221415d7SJens Axboe 849221415d7SJens Axboe produced = (produced % n_subbufs) * subbuf_size + buf->offset; 850221415d7SJens Axboe consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; 851221415d7SJens Axboe 852221415d7SJens Axboe if (consumed > produced) 853221415d7SJens Axboe produced += n_subbufs * subbuf_size; 854221415d7SJens Axboe 855221415d7SJens Axboe if (consumed == produced) 856221415d7SJens Axboe return 0; 857221415d7SJens Axboe 858b86ff981SJens Axboe return 1; 859b86ff981SJens Axboe } 860b86ff981SJens Axboe 861b86ff981SJens Axboe /** 862b86ff981SJens Axboe * relay_file_read_subbuf_avail - return bytes available in sub-buffer 8634c78a663SRandy Dunlap * @read_pos: file read position 8644c78a663SRandy Dunlap * @buf: relay channel buffer 865b86ff981SJens Axboe */ 866b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos, 867b86ff981SJens Axboe struct rchan_buf *buf) 868b86ff981SJens Axboe { 869b86ff981SJens Axboe size_t padding, avail = 0; 870b86ff981SJens Axboe size_t read_subbuf, read_offset, write_subbuf, write_offset; 871b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 872b86ff981SJens Axboe 873b86ff981SJens Axboe write_subbuf = (buf->data - buf->start) / subbuf_size; 874b86ff981SJens Axboe write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; 875b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 876b86ff981SJens Axboe read_offset = read_pos % subbuf_size; 877b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 878b86ff981SJens Axboe 879b86ff981SJens Axboe if (read_subbuf == write_subbuf) { 880b86ff981SJens Axboe if (read_offset + padding < write_offset) 881b86ff981SJens Axboe avail = write_offset - (read_offset + padding); 882b86ff981SJens Axboe } else 883b86ff981SJens Axboe avail = (subbuf_size - padding) - read_offset; 884b86ff981SJens Axboe 885b86ff981SJens Axboe return avail; 886b86ff981SJens Axboe } 887b86ff981SJens Axboe 888b86ff981SJens Axboe /** 889b86ff981SJens Axboe * relay_file_read_start_pos - find the first available byte to read 8904c78a663SRandy Dunlap * @read_pos: file read position 8914c78a663SRandy Dunlap * @buf: relay channel buffer 892b86ff981SJens Axboe * 89372fd4a35SRobert P. J. Day * If the @read_pos is in the middle of padding, return the 894b86ff981SJens Axboe * position of the first actually available byte, otherwise 895b86ff981SJens Axboe * return the original value. 896b86ff981SJens Axboe */ 897b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos, 898b86ff981SJens Axboe struct rchan_buf *buf) 899b86ff981SJens Axboe { 900b86ff981SJens Axboe size_t read_subbuf, padding, padding_start, padding_end; 901b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 902b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 9038d62fdebSDavid Wilder size_t consumed = buf->subbufs_consumed % n_subbufs; 904b86ff981SJens Axboe 9058d62fdebSDavid Wilder if (!read_pos) 9068d62fdebSDavid Wilder read_pos = consumed * subbuf_size + buf->bytes_consumed; 907b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 908b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 909b86ff981SJens Axboe padding_start = (read_subbuf + 1) * subbuf_size - padding; 910b86ff981SJens Axboe padding_end = (read_subbuf + 1) * subbuf_size; 911b86ff981SJens Axboe if (read_pos >= padding_start && read_pos < padding_end) { 912b86ff981SJens Axboe read_subbuf = (read_subbuf + 1) % n_subbufs; 913b86ff981SJens Axboe read_pos = read_subbuf * subbuf_size; 914b86ff981SJens Axboe } 915b86ff981SJens Axboe 916b86ff981SJens Axboe return read_pos; 917b86ff981SJens Axboe } 918b86ff981SJens Axboe 919b86ff981SJens Axboe /** 920b86ff981SJens Axboe * relay_file_read_end_pos - return the new read position 9214c78a663SRandy Dunlap * @read_pos: file read position 9224c78a663SRandy Dunlap * @buf: relay channel buffer 9234c78a663SRandy Dunlap * @count: number of bytes to be read 924b86ff981SJens Axboe */ 925b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf, 926b86ff981SJens Axboe size_t read_pos, 927b86ff981SJens Axboe size_t count) 928b86ff981SJens Axboe { 929b86ff981SJens Axboe size_t read_subbuf, padding, end_pos; 930b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 931b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 932b86ff981SJens Axboe 933b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 934b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 935b86ff981SJens Axboe if (read_pos % subbuf_size + count + padding == subbuf_size) 936b86ff981SJens Axboe end_pos = (read_subbuf + 1) * subbuf_size; 937b86ff981SJens Axboe else 938b86ff981SJens Axboe end_pos = read_pos + count; 939b86ff981SJens Axboe if (end_pos >= subbuf_size * n_subbufs) 940b86ff981SJens Axboe end_pos = 0; 941b86ff981SJens Axboe 942b86ff981SJens Axboe return end_pos; 943b86ff981SJens Axboe } 944b86ff981SJens Axboe 9454c78a663SRandy Dunlap /* 9466dac40a7STom Zanussi * subbuf_read_actor - read up to one subbuf's worth of data 947b86ff981SJens Axboe */ 9486dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start, 9496dac40a7STom Zanussi struct rchan_buf *buf, 9506dac40a7STom Zanussi size_t avail, 9516dac40a7STom Zanussi read_descriptor_t *desc, 9526dac40a7STom Zanussi read_actor_t actor) 953b86ff981SJens Axboe { 954b86ff981SJens Axboe void *from; 9556dac40a7STom Zanussi int ret = 0; 956b86ff981SJens Axboe 957b86ff981SJens Axboe from = buf->start + read_start; 9586dac40a7STom Zanussi ret = avail; 959ba2397efSAl Viro if (copy_to_user(desc->arg.buf, from, avail)) { 9606dac40a7STom Zanussi desc->error = -EFAULT; 9616dac40a7STom Zanussi ret = 0; 962b86ff981SJens Axboe } 9636dac40a7STom Zanussi desc->arg.data += ret; 9646dac40a7STom Zanussi desc->written += ret; 9656dac40a7STom Zanussi desc->count -= ret; 9666dac40a7STom Zanussi 967b86ff981SJens Axboe return ret; 968b86ff981SJens Axboe } 969b86ff981SJens Axboe 9706dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start, 9716dac40a7STom Zanussi struct rchan_buf *buf, 9726dac40a7STom Zanussi size_t avail, 9736dac40a7STom Zanussi read_descriptor_t *desc, 9746dac40a7STom Zanussi read_actor_t actor); 9756dac40a7STom Zanussi 9764c78a663SRandy Dunlap /* 9776dac40a7STom Zanussi * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries 9786dac40a7STom Zanussi */ 979192636adSAndrew Morton static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos, 9806dac40a7STom Zanussi subbuf_actor_t subbuf_actor, 9816dac40a7STom Zanussi read_actor_t actor, 982ba2397efSAl Viro read_descriptor_t *desc) 983221415d7SJens Axboe { 984221415d7SJens Axboe struct rchan_buf *buf = filp->private_data; 985221415d7SJens Axboe size_t read_start, avail; 9866dac40a7STom Zanussi int ret; 987221415d7SJens Axboe 988ba2397efSAl Viro if (!desc->count) 989221415d7SJens Axboe return 0; 990221415d7SJens Axboe 991f3a43f3fSJosef "Jeff" Sipek mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); 9926dac40a7STom Zanussi do { 9936dac40a7STom Zanussi if (!relay_file_read_avail(buf, *ppos)) 994221415d7SJens Axboe break; 995221415d7SJens Axboe 9966dac40a7STom Zanussi read_start = relay_file_read_start_pos(*ppos, buf); 9976dac40a7STom Zanussi avail = relay_file_read_subbuf_avail(read_start, buf); 9986dac40a7STom Zanussi if (!avail) 9996dac40a7STom Zanussi break; 1000221415d7SJens Axboe 1001ba2397efSAl Viro avail = min(desc->count, avail); 1002ba2397efSAl Viro ret = subbuf_actor(read_start, buf, avail, desc, actor); 1003ba2397efSAl Viro if (desc->error < 0) 10046dac40a7STom Zanussi break; 10056dac40a7STom Zanussi 10066dac40a7STom Zanussi if (ret) { 1007221415d7SJens Axboe relay_file_read_consume(buf, read_start, ret); 1008221415d7SJens Axboe *ppos = relay_file_read_end_pos(buf, read_start, ret); 1009221415d7SJens Axboe } 1010ba2397efSAl Viro } while (desc->count && ret); 1011f3a43f3fSJosef "Jeff" Sipek mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); 1012221415d7SJens Axboe 1013ba2397efSAl Viro return desc->written; 1014221415d7SJens Axboe } 1015221415d7SJens Axboe 10166dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp, 10176dac40a7STom Zanussi char __user *buffer, 10186dac40a7STom Zanussi size_t count, 10196dac40a7STom Zanussi loff_t *ppos) 10206dac40a7STom Zanussi { 1021ba2397efSAl Viro read_descriptor_t desc; 1022ba2397efSAl Viro desc.written = 0; 1023ba2397efSAl Viro desc.count = count; 1024ba2397efSAl Viro desc.arg.buf = buffer; 1025ba2397efSAl Viro desc.error = 0; 1026ba2397efSAl Viro return relay_file_read_subbufs(filp, ppos, subbuf_read_actor, 1027ba2397efSAl Viro NULL, &desc); 10286dac40a7STom Zanussi } 10296dac40a7STom Zanussi 10301db60cf2SJens Axboe static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed) 10311db60cf2SJens Axboe { 10321db60cf2SJens Axboe rbuf->bytes_consumed += bytes_consumed; 10331db60cf2SJens Axboe 10341db60cf2SJens Axboe if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) { 10351db60cf2SJens Axboe relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1); 10361db60cf2SJens Axboe rbuf->bytes_consumed %= rbuf->chan->subbuf_size; 10371db60cf2SJens Axboe } 10381db60cf2SJens Axboe } 10391db60cf2SJens Axboe 1040ebf99093STom Zanussi static void relay_pipe_buf_release(struct pipe_inode_info *pipe, 1041ebf99093STom Zanussi struct pipe_buffer *buf) 1042221415d7SJens Axboe { 1043ebf99093STom Zanussi struct rchan_buf *rbuf; 1044ebf99093STom Zanussi 1045ebf99093STom Zanussi rbuf = (struct rchan_buf *)page_private(buf->page); 10461db60cf2SJens Axboe relay_consume_bytes(rbuf, buf->private); 1047ebf99093STom Zanussi } 1048ebf99093STom Zanussi 1049ebf99093STom Zanussi static struct pipe_buf_operations relay_pipe_buf_ops = { 1050ebf99093STom Zanussi .can_merge = 0, 1051ebf99093STom Zanussi .map = generic_pipe_buf_map, 1052ebf99093STom Zanussi .unmap = generic_pipe_buf_unmap, 1053cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 1054ebf99093STom Zanussi .release = relay_pipe_buf_release, 1055ebf99093STom Zanussi .steal = generic_pipe_buf_steal, 1056ebf99093STom Zanussi .get = generic_pipe_buf_get, 1057ebf99093STom Zanussi }; 1058ebf99093STom Zanussi 1059d3f35d98STom Zanussi /* 1060ebf99093STom Zanussi * subbuf_splice_actor - splice up to one subbuf's worth of data 1061ebf99093STom Zanussi */ 1062ebf99093STom Zanussi static int subbuf_splice_actor(struct file *in, 1063ebf99093STom Zanussi loff_t *ppos, 1064ebf99093STom Zanussi struct pipe_inode_info *pipe, 1065ebf99093STom Zanussi size_t len, 1066ebf99093STom Zanussi unsigned int flags, 1067ebf99093STom Zanussi int *nonpad_ret) 1068ebf99093STom Zanussi { 106916d54669SJens Axboe unsigned int pidx, poff, total_len, subbuf_pages, nr_pages, ret; 1070ebf99093STom Zanussi struct rchan_buf *rbuf = in->private_data; 1071ebf99093STom Zanussi unsigned int subbuf_size = rbuf->chan->subbuf_size; 107224da24deSTom Zanussi uint64_t pos = (uint64_t) *ppos; 107324da24deSTom Zanussi uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size; 107424da24deSTom Zanussi size_t read_start = (size_t) do_div(pos, alloc_size); 1075ebf99093STom Zanussi size_t read_subbuf = read_start / subbuf_size; 1076ebf99093STom Zanussi size_t padding = rbuf->padding[read_subbuf]; 1077ebf99093STom Zanussi size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding; 10781db60cf2SJens Axboe struct page *pages[PIPE_BUFFERS]; 10791db60cf2SJens Axboe struct partial_page partial[PIPE_BUFFERS]; 10801db60cf2SJens Axboe struct splice_pipe_desc spd = { 10811db60cf2SJens Axboe .pages = pages, 10821db60cf2SJens Axboe .nr_pages = 0, 10831db60cf2SJens Axboe .partial = partial, 10841db60cf2SJens Axboe .flags = flags, 10851db60cf2SJens Axboe .ops = &relay_pipe_buf_ops, 10861db60cf2SJens Axboe }; 1087ebf99093STom Zanussi 1088ebf99093STom Zanussi if (rbuf->subbufs_produced == rbuf->subbufs_consumed) 1089ebf99093STom Zanussi return 0; 1090ebf99093STom Zanussi 10911db60cf2SJens Axboe /* 10921db60cf2SJens Axboe * Adjust read len, if longer than what is available 10931db60cf2SJens Axboe */ 10941db60cf2SJens Axboe if (len > (subbuf_size - read_start % subbuf_size)) 10951db60cf2SJens Axboe len = subbuf_size - read_start % subbuf_size; 1096ebf99093STom Zanussi 1097ebf99093STom Zanussi subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; 1098ebf99093STom Zanussi pidx = (read_start / PAGE_SIZE) % subbuf_pages; 1099ebf99093STom Zanussi poff = read_start & ~PAGE_MASK; 110016d54669SJens Axboe nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS); 1101ebf99093STom Zanussi 110216d54669SJens Axboe for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) { 11031db60cf2SJens Axboe unsigned int this_len, this_end, private; 11041db60cf2SJens Axboe unsigned int cur_pos = read_start + total_len; 1105ebf99093STom Zanussi 11061db60cf2SJens Axboe if (!len) 1107ebf99093STom Zanussi break; 11081db60cf2SJens Axboe 11091db60cf2SJens Axboe this_len = min_t(unsigned long, len, PAGE_SIZE - poff); 11101db60cf2SJens Axboe private = this_len; 11111db60cf2SJens Axboe 11121db60cf2SJens Axboe spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; 11131db60cf2SJens Axboe spd.partial[spd.nr_pages].offset = poff; 11141db60cf2SJens Axboe 11151db60cf2SJens Axboe this_end = cur_pos + this_len; 11161db60cf2SJens Axboe if (this_end >= nonpad_end) { 11171db60cf2SJens Axboe this_len = nonpad_end - cur_pos; 11181db60cf2SJens Axboe private = this_len + padding; 1119ebf99093STom Zanussi } 11201db60cf2SJens Axboe spd.partial[spd.nr_pages].len = this_len; 11211db60cf2SJens Axboe spd.partial[spd.nr_pages].private = private; 1122ebf99093STom Zanussi 11231db60cf2SJens Axboe len -= this_len; 11241db60cf2SJens Axboe total_len += this_len; 1125ebf99093STom Zanussi poff = 0; 1126ebf99093STom Zanussi pidx = (pidx + 1) % subbuf_pages; 1127ebf99093STom Zanussi 11281db60cf2SJens Axboe if (this_end >= nonpad_end) { 11291db60cf2SJens Axboe spd.nr_pages++; 1130ebf99093STom Zanussi break; 1131ebf99093STom Zanussi } 1132ebf99093STom Zanussi } 1133ebf99093STom Zanussi 11341db60cf2SJens Axboe if (!spd.nr_pages) 11351db60cf2SJens Axboe return 0; 1136ebf99093STom Zanussi 11371db60cf2SJens Axboe ret = *nonpad_ret = splice_to_pipe(pipe, &spd); 11381db60cf2SJens Axboe if (ret < 0 || ret < total_len) 11391db60cf2SJens Axboe return ret; 1140ebf99093STom Zanussi 11411db60cf2SJens Axboe if (read_start + ret == nonpad_end) 11421db60cf2SJens Axboe ret += padding; 1143ebf99093STom Zanussi 1144ebf99093STom Zanussi return ret; 1145ebf99093STom Zanussi } 1146ebf99093STom Zanussi 1147ebf99093STom Zanussi static ssize_t relay_file_splice_read(struct file *in, 1148ebf99093STom Zanussi loff_t *ppos, 1149ebf99093STom Zanussi struct pipe_inode_info *pipe, 1150ebf99093STom Zanussi size_t len, 1151ebf99093STom Zanussi unsigned int flags) 1152ebf99093STom Zanussi { 1153ebf99093STom Zanussi ssize_t spliced; 1154ebf99093STom Zanussi int ret; 1155ebf99093STom Zanussi int nonpad_ret = 0; 1156ebf99093STom Zanussi 1157ebf99093STom Zanussi ret = 0; 1158ebf99093STom Zanussi spliced = 0; 1159ebf99093STom Zanussi 1160ebf99093STom Zanussi while (len) { 1161ebf99093STom Zanussi ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret); 1162ebf99093STom Zanussi if (ret < 0) 1163ebf99093STom Zanussi break; 1164ebf99093STom Zanussi else if (!ret) { 1165ebf99093STom Zanussi if (spliced) 1166ebf99093STom Zanussi break; 1167ebf99093STom Zanussi if (flags & SPLICE_F_NONBLOCK) { 1168ebf99093STom Zanussi ret = -EAGAIN; 1169ebf99093STom Zanussi break; 1170ebf99093STom Zanussi } 1171ebf99093STom Zanussi } 1172ebf99093STom Zanussi 1173ebf99093STom Zanussi *ppos += ret; 1174ebf99093STom Zanussi if (ret > len) 1175ebf99093STom Zanussi len = 0; 1176ebf99093STom Zanussi else 1177ebf99093STom Zanussi len -= ret; 1178ebf99093STom Zanussi spliced += nonpad_ret; 1179ebf99093STom Zanussi nonpad_ret = 0; 1180ebf99093STom Zanussi } 1181ebf99093STom Zanussi 1182ebf99093STom Zanussi if (spliced) 1183ebf99093STom Zanussi return spliced; 1184ebf99093STom Zanussi 1185ebf99093STom Zanussi return ret; 1186221415d7SJens Axboe } 1187221415d7SJens Axboe 118815ad7cdcSHelge Deller const struct file_operations relay_file_operations = { 1189b86ff981SJens Axboe .open = relay_file_open, 1190b86ff981SJens Axboe .poll = relay_file_poll, 1191b86ff981SJens Axboe .mmap = relay_file_mmap, 1192b86ff981SJens Axboe .read = relay_file_read, 1193b86ff981SJens Axboe .llseek = no_llseek, 1194b86ff981SJens Axboe .release = relay_file_release, 1195ebf99093STom Zanussi .splice_read = relay_file_splice_read, 1196b86ff981SJens Axboe }; 1197b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations); 119823c88752SMathieu Desnoyers 119923c88752SMathieu Desnoyers static __init int relay_init(void) 120023c88752SMathieu Desnoyers { 120123c88752SMathieu Desnoyers 120223c88752SMathieu Desnoyers hotcpu_notifier(relay_hotcpu_callback, 0); 120323c88752SMathieu Desnoyers return 0; 120423c88752SMathieu Desnoyers } 120523c88752SMathieu Desnoyers 120623c88752SMathieu Desnoyers module_init(relay_init); 1207