1b86ff981SJens Axboe /* 2b86ff981SJens Axboe * Public API and common code for kernel->userspace relay file support. 3b86ff981SJens Axboe * 4b86ff981SJens Axboe * See Documentation/filesystems/relayfs.txt for an overview of relayfs. 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. 10b86ff981SJens Axboe * 11b86ff981SJens Axboe * This file is released under the GPL. 12b86ff981SJens Axboe */ 13b86ff981SJens Axboe #include <linux/errno.h> 14b86ff981SJens Axboe #include <linux/stddef.h> 15b86ff981SJens Axboe #include <linux/slab.h> 16b86ff981SJens Axboe #include <linux/module.h> 17b86ff981SJens Axboe #include <linux/string.h> 18b86ff981SJens Axboe #include <linux/relay.h> 19b86ff981SJens Axboe #include <linux/vmalloc.h> 20b86ff981SJens Axboe #include <linux/mm.h> 21b86ff981SJens Axboe 22b86ff981SJens Axboe /* 23b86ff981SJens Axboe * close() vm_op implementation for relay file mapping. 24b86ff981SJens Axboe */ 25b86ff981SJens Axboe static void relay_file_mmap_close(struct vm_area_struct *vma) 26b86ff981SJens Axboe { 27b86ff981SJens Axboe struct rchan_buf *buf = vma->vm_private_data; 28b86ff981SJens Axboe buf->chan->cb->buf_unmapped(buf, vma->vm_file); 29b86ff981SJens Axboe } 30b86ff981SJens Axboe 31b86ff981SJens Axboe /* 32b86ff981SJens Axboe * nopage() vm_op implementation for relay file mapping. 33b86ff981SJens Axboe */ 34b86ff981SJens Axboe static struct page *relay_buf_nopage(struct vm_area_struct *vma, 35b86ff981SJens Axboe unsigned long address, 36b86ff981SJens Axboe int *type) 37b86ff981SJens Axboe { 38b86ff981SJens Axboe struct page *page; 39b86ff981SJens Axboe struct rchan_buf *buf = vma->vm_private_data; 40b86ff981SJens Axboe unsigned long offset = address - vma->vm_start; 41b86ff981SJens Axboe 42b86ff981SJens Axboe if (address > vma->vm_end) 43b86ff981SJens Axboe return NOPAGE_SIGBUS; /* Disallow mremap */ 44b86ff981SJens Axboe if (!buf) 45b86ff981SJens Axboe return NOPAGE_OOM; 46b86ff981SJens Axboe 47b86ff981SJens Axboe page = vmalloc_to_page(buf->start + offset); 48b86ff981SJens Axboe if (!page) 49b86ff981SJens Axboe return NOPAGE_OOM; 50b86ff981SJens Axboe get_page(page); 51b86ff981SJens Axboe 52b86ff981SJens Axboe if (type) 53b86ff981SJens Axboe *type = VM_FAULT_MINOR; 54b86ff981SJens Axboe 55b86ff981SJens Axboe return page; 56b86ff981SJens Axboe } 57b86ff981SJens Axboe 58b86ff981SJens Axboe /* 59b86ff981SJens Axboe * vm_ops for relay file mappings. 60b86ff981SJens Axboe */ 61b86ff981SJens Axboe static struct vm_operations_struct relay_file_mmap_ops = { 62b86ff981SJens Axboe .nopage = relay_buf_nopage, 63b86ff981SJens Axboe .close = relay_file_mmap_close, 64b86ff981SJens Axboe }; 65b86ff981SJens Axboe 66b86ff981SJens Axboe /** 67b86ff981SJens Axboe * relay_mmap_buf: - mmap channel buffer to process address space 68b86ff981SJens Axboe * @buf: relay channel buffer 69b86ff981SJens Axboe * @vma: vm_area_struct describing memory to be mapped 70b86ff981SJens Axboe * 71b86ff981SJens Axboe * Returns 0 if ok, negative on error 72b86ff981SJens Axboe * 73b86ff981SJens Axboe * Caller should already have grabbed mmap_sem. 74b86ff981SJens Axboe */ 75b86ff981SJens Axboe int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) 76b86ff981SJens Axboe { 77b86ff981SJens Axboe unsigned long length = vma->vm_end - vma->vm_start; 78b86ff981SJens Axboe struct file *filp = vma->vm_file; 79b86ff981SJens Axboe 80b86ff981SJens Axboe if (!buf) 81b86ff981SJens Axboe return -EBADF; 82b86ff981SJens Axboe 83b86ff981SJens Axboe if (length != (unsigned long)buf->chan->alloc_size) 84b86ff981SJens Axboe return -EINVAL; 85b86ff981SJens Axboe 86b86ff981SJens Axboe vma->vm_ops = &relay_file_mmap_ops; 87b86ff981SJens Axboe vma->vm_private_data = buf; 88b86ff981SJens Axboe buf->chan->cb->buf_mapped(buf, filp); 89b86ff981SJens Axboe 90b86ff981SJens Axboe return 0; 91b86ff981SJens Axboe } 92b86ff981SJens Axboe 93b86ff981SJens Axboe /** 94b86ff981SJens Axboe * relay_alloc_buf - allocate a channel buffer 95b86ff981SJens Axboe * @buf: the buffer struct 96b86ff981SJens Axboe * @size: total size of the buffer 97b86ff981SJens Axboe * 984c78a663SRandy Dunlap * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The 99221415d7SJens Axboe * passed in size will get page aligned, if it isn't already. 100b86ff981SJens Axboe */ 101221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) 102b86ff981SJens Axboe { 103b86ff981SJens Axboe void *mem; 104b86ff981SJens Axboe unsigned int i, j, n_pages; 105b86ff981SJens Axboe 106221415d7SJens Axboe *size = PAGE_ALIGN(*size); 107221415d7SJens Axboe n_pages = *size >> PAGE_SHIFT; 108b86ff981SJens Axboe 109b86ff981SJens Axboe buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL); 110b86ff981SJens Axboe if (!buf->page_array) 111b86ff981SJens Axboe return NULL; 112b86ff981SJens Axboe 113b86ff981SJens Axboe for (i = 0; i < n_pages; i++) { 114b86ff981SJens Axboe buf->page_array[i] = alloc_page(GFP_KERNEL); 115b86ff981SJens Axboe if (unlikely(!buf->page_array[i])) 116b86ff981SJens Axboe goto depopulate; 117b86ff981SJens Axboe } 118b86ff981SJens Axboe mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); 119b86ff981SJens Axboe if (!mem) 120b86ff981SJens Axboe goto depopulate; 121b86ff981SJens Axboe 122221415d7SJens Axboe memset(mem, 0, *size); 123b86ff981SJens Axboe buf->page_count = n_pages; 124b86ff981SJens Axboe return mem; 125b86ff981SJens Axboe 126b86ff981SJens Axboe depopulate: 127b86ff981SJens Axboe for (j = 0; j < i; j++) 128b86ff981SJens Axboe __free_page(buf->page_array[j]); 129b86ff981SJens Axboe kfree(buf->page_array); 130b86ff981SJens Axboe return NULL; 131b86ff981SJens Axboe } 132b86ff981SJens Axboe 133b86ff981SJens Axboe /** 134b86ff981SJens Axboe * relay_create_buf - allocate and initialize a channel buffer 1354c78a663SRandy Dunlap * @chan: the relay channel 136b86ff981SJens Axboe * 1374c78a663SRandy Dunlap * Returns channel buffer if successful, %NULL otherwise. 138b86ff981SJens Axboe */ 139b86ff981SJens Axboe struct rchan_buf *relay_create_buf(struct rchan *chan) 140b86ff981SJens Axboe { 141*cd861280SRobert P. J. Day struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); 142b86ff981SJens Axboe if (!buf) 143b86ff981SJens Axboe return NULL; 144b86ff981SJens Axboe 145b86ff981SJens Axboe buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL); 146b86ff981SJens Axboe if (!buf->padding) 147b86ff981SJens Axboe goto free_buf; 148b86ff981SJens Axboe 149221415d7SJens Axboe buf->start = relay_alloc_buf(buf, &chan->alloc_size); 150b86ff981SJens Axboe if (!buf->start) 151b86ff981SJens Axboe goto free_buf; 152b86ff981SJens Axboe 153b86ff981SJens Axboe buf->chan = chan; 154b86ff981SJens Axboe kref_get(&buf->chan->kref); 155b86ff981SJens Axboe return buf; 156b86ff981SJens Axboe 157b86ff981SJens Axboe free_buf: 158b86ff981SJens Axboe kfree(buf->padding); 159b86ff981SJens Axboe kfree(buf); 160b86ff981SJens Axboe return NULL; 161b86ff981SJens Axboe } 162b86ff981SJens Axboe 163b86ff981SJens Axboe /** 164b86ff981SJens Axboe * relay_destroy_channel - free the channel struct 1654c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay channel 166b86ff981SJens Axboe * 167b86ff981SJens Axboe * Should only be called from kref_put(). 168b86ff981SJens Axboe */ 169b86ff981SJens Axboe void relay_destroy_channel(struct kref *kref) 170b86ff981SJens Axboe { 171b86ff981SJens Axboe struct rchan *chan = container_of(kref, struct rchan, kref); 172b86ff981SJens Axboe kfree(chan); 173b86ff981SJens Axboe } 174b86ff981SJens Axboe 175b86ff981SJens Axboe /** 176b86ff981SJens Axboe * relay_destroy_buf - destroy an rchan_buf struct and associated buffer 177b86ff981SJens Axboe * @buf: the buffer struct 178b86ff981SJens Axboe */ 179b86ff981SJens Axboe void relay_destroy_buf(struct rchan_buf *buf) 180b86ff981SJens Axboe { 181b86ff981SJens Axboe struct rchan *chan = buf->chan; 182b86ff981SJens Axboe unsigned int i; 183b86ff981SJens Axboe 184b86ff981SJens Axboe if (likely(buf->start)) { 185b86ff981SJens Axboe vunmap(buf->start); 186b86ff981SJens Axboe for (i = 0; i < buf->page_count; i++) 187b86ff981SJens Axboe __free_page(buf->page_array[i]); 188b86ff981SJens Axboe kfree(buf->page_array); 189b86ff981SJens Axboe } 190b86ff981SJens Axboe kfree(buf->padding); 191b86ff981SJens Axboe kfree(buf); 192b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 193b86ff981SJens Axboe } 194b86ff981SJens Axboe 195b86ff981SJens Axboe /** 196b86ff981SJens Axboe * relay_remove_buf - remove a channel buffer 1974c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay buffer 198b86ff981SJens Axboe * 199b86ff981SJens Axboe * Removes the file from the fileystem, which also frees the 200b86ff981SJens Axboe * rchan_buf_struct and the channel buffer. Should only be called from 201b86ff981SJens Axboe * kref_put(). 202b86ff981SJens Axboe */ 203b86ff981SJens Axboe void relay_remove_buf(struct kref *kref) 204b86ff981SJens Axboe { 205b86ff981SJens Axboe struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); 206b86ff981SJens Axboe buf->chan->cb->remove_buf_file(buf->dentry); 207b86ff981SJens Axboe relay_destroy_buf(buf); 208b86ff981SJens Axboe } 209b86ff981SJens Axboe 210b86ff981SJens Axboe /** 211b86ff981SJens Axboe * relay_buf_empty - boolean, is the channel buffer empty? 212b86ff981SJens Axboe * @buf: channel buffer 213b86ff981SJens Axboe * 214b86ff981SJens Axboe * Returns 1 if the buffer is empty, 0 otherwise. 215b86ff981SJens Axboe */ 216b86ff981SJens Axboe int relay_buf_empty(struct rchan_buf *buf) 217b86ff981SJens Axboe { 218b86ff981SJens Axboe return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; 219b86ff981SJens Axboe } 220b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_empty); 221b86ff981SJens Axboe 222b86ff981SJens Axboe /** 223b86ff981SJens Axboe * relay_buf_full - boolean, is the channel buffer full? 224b86ff981SJens Axboe * @buf: channel buffer 225b86ff981SJens Axboe * 226b86ff981SJens Axboe * Returns 1 if the buffer is full, 0 otherwise. 227b86ff981SJens Axboe */ 228b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf) 229b86ff981SJens Axboe { 230b86ff981SJens Axboe size_t ready = buf->subbufs_produced - buf->subbufs_consumed; 231b86ff981SJens Axboe return (ready >= buf->chan->n_subbufs) ? 1 : 0; 232b86ff981SJens Axboe } 233b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full); 234b86ff981SJens Axboe 235b86ff981SJens Axboe /* 236b86ff981SJens Axboe * High-level relay kernel API and associated functions. 237b86ff981SJens Axboe */ 238b86ff981SJens Axboe 239b86ff981SJens Axboe /* 240b86ff981SJens Axboe * rchan_callback implementations defining default channel behavior. Used 241b86ff981SJens Axboe * in place of corresponding NULL values in client callback struct. 242b86ff981SJens Axboe */ 243b86ff981SJens Axboe 244b86ff981SJens Axboe /* 245b86ff981SJens Axboe * subbuf_start() default callback. Does nothing. 246b86ff981SJens Axboe */ 247b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf, 248b86ff981SJens Axboe void *subbuf, 249b86ff981SJens Axboe void *prev_subbuf, 250b86ff981SJens Axboe size_t prev_padding) 251b86ff981SJens Axboe { 252b86ff981SJens Axboe if (relay_buf_full(buf)) 253b86ff981SJens Axboe return 0; 254b86ff981SJens Axboe 255b86ff981SJens Axboe return 1; 256b86ff981SJens Axboe } 257b86ff981SJens Axboe 258b86ff981SJens Axboe /* 259b86ff981SJens Axboe * buf_mapped() default callback. Does nothing. 260b86ff981SJens Axboe */ 261b86ff981SJens Axboe static void buf_mapped_default_callback(struct rchan_buf *buf, 262b86ff981SJens Axboe struct file *filp) 263b86ff981SJens Axboe { 264b86ff981SJens Axboe } 265b86ff981SJens Axboe 266b86ff981SJens Axboe /* 267b86ff981SJens Axboe * buf_unmapped() default callback. Does nothing. 268b86ff981SJens Axboe */ 269b86ff981SJens Axboe static void buf_unmapped_default_callback(struct rchan_buf *buf, 270b86ff981SJens Axboe struct file *filp) 271b86ff981SJens Axboe { 272b86ff981SJens Axboe } 273b86ff981SJens Axboe 274b86ff981SJens Axboe /* 275b86ff981SJens Axboe * create_buf_file_create() default callback. Does nothing. 276b86ff981SJens Axboe */ 277b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename, 278b86ff981SJens Axboe struct dentry *parent, 279b86ff981SJens Axboe int mode, 280b86ff981SJens Axboe struct rchan_buf *buf, 281b86ff981SJens Axboe int *is_global) 282b86ff981SJens Axboe { 283b86ff981SJens Axboe return NULL; 284b86ff981SJens Axboe } 285b86ff981SJens Axboe 286b86ff981SJens Axboe /* 287b86ff981SJens Axboe * remove_buf_file() default callback. Does nothing. 288b86ff981SJens Axboe */ 289b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry) 290b86ff981SJens Axboe { 291b86ff981SJens Axboe return -EINVAL; 292b86ff981SJens Axboe } 293b86ff981SJens Axboe 294b86ff981SJens Axboe /* relay channel default callbacks */ 295b86ff981SJens Axboe static struct rchan_callbacks default_channel_callbacks = { 296b86ff981SJens Axboe .subbuf_start = subbuf_start_default_callback, 297b86ff981SJens Axboe .buf_mapped = buf_mapped_default_callback, 298b86ff981SJens Axboe .buf_unmapped = buf_unmapped_default_callback, 299b86ff981SJens Axboe .create_buf_file = create_buf_file_default_callback, 300b86ff981SJens Axboe .remove_buf_file = remove_buf_file_default_callback, 301b86ff981SJens Axboe }; 302b86ff981SJens Axboe 303b86ff981SJens Axboe /** 304b86ff981SJens Axboe * wakeup_readers - wake up readers waiting on a channel 305b86ff981SJens Axboe * @private: the channel buffer 306b86ff981SJens Axboe * 307b86ff981SJens Axboe * This is the work function used to defer reader waking. The 308b86ff981SJens Axboe * reason waking is deferred is that calling directly from write 309b86ff981SJens Axboe * causes problems if you're writing from say the scheduler. 310b86ff981SJens Axboe */ 311c4028958SDavid Howells static void wakeup_readers(struct work_struct *work) 312b86ff981SJens Axboe { 313c4028958SDavid Howells struct rchan_buf *buf = 314c4028958SDavid Howells container_of(work, struct rchan_buf, wake_readers.work); 315b86ff981SJens Axboe wake_up_interruptible(&buf->read_wait); 316b86ff981SJens Axboe } 317b86ff981SJens Axboe 318b86ff981SJens Axboe /** 319b86ff981SJens Axboe * __relay_reset - reset a channel buffer 320b86ff981SJens Axboe * @buf: the channel buffer 321b86ff981SJens Axboe * @init: 1 if this is a first-time initialization 322b86ff981SJens Axboe * 323b86ff981SJens Axboe * See relay_reset for description of effect. 324b86ff981SJens Axboe */ 325b86ff981SJens Axboe static inline void __relay_reset(struct rchan_buf *buf, unsigned int init) 326b86ff981SJens Axboe { 327b86ff981SJens Axboe size_t i; 328b86ff981SJens Axboe 329b86ff981SJens Axboe if (init) { 330b86ff981SJens Axboe init_waitqueue_head(&buf->read_wait); 331b86ff981SJens Axboe kref_init(&buf->kref); 332c4028958SDavid Howells INIT_DELAYED_WORK(&buf->wake_readers, NULL); 333b86ff981SJens Axboe } else { 334b86ff981SJens Axboe cancel_delayed_work(&buf->wake_readers); 335b86ff981SJens Axboe flush_scheduled_work(); 336b86ff981SJens Axboe } 337b86ff981SJens Axboe 338b86ff981SJens Axboe buf->subbufs_produced = 0; 339b86ff981SJens Axboe buf->subbufs_consumed = 0; 340b86ff981SJens Axboe buf->bytes_consumed = 0; 341b86ff981SJens Axboe buf->finalized = 0; 342b86ff981SJens Axboe buf->data = buf->start; 343b86ff981SJens Axboe buf->offset = 0; 344b86ff981SJens Axboe 345b86ff981SJens Axboe for (i = 0; i < buf->chan->n_subbufs; i++) 346b86ff981SJens Axboe buf->padding[i] = 0; 347b86ff981SJens Axboe 348b86ff981SJens Axboe buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); 349b86ff981SJens Axboe } 350b86ff981SJens Axboe 351b86ff981SJens Axboe /** 352b86ff981SJens Axboe * relay_reset - reset the channel 353b86ff981SJens Axboe * @chan: the channel 354b86ff981SJens Axboe * 355b86ff981SJens Axboe * This has the effect of erasing all data from all channel buffers 356b86ff981SJens Axboe * and restarting the channel in its initial state. The buffers 357b86ff981SJens Axboe * are not freed, so any mappings are still in effect. 358b86ff981SJens Axboe * 359b86ff981SJens Axboe * NOTE: Care should be taken that the channel isn't actually 360b86ff981SJens Axboe * being used by anything when this call is made. 361b86ff981SJens Axboe */ 362b86ff981SJens Axboe void relay_reset(struct rchan *chan) 363b86ff981SJens Axboe { 364b86ff981SJens Axboe unsigned int i; 365b86ff981SJens Axboe struct rchan_buf *prev = NULL; 366b86ff981SJens Axboe 367b86ff981SJens Axboe if (!chan) 368b86ff981SJens Axboe return; 369b86ff981SJens Axboe 370b86ff981SJens Axboe for (i = 0; i < NR_CPUS; i++) { 371b86ff981SJens Axboe if (!chan->buf[i] || chan->buf[i] == prev) 372b86ff981SJens Axboe break; 373b86ff981SJens Axboe __relay_reset(chan->buf[i], 0); 374b86ff981SJens Axboe prev = chan->buf[i]; 375b86ff981SJens Axboe } 376b86ff981SJens Axboe } 377b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset); 378b86ff981SJens Axboe 3794c78a663SRandy Dunlap /* 380b86ff981SJens Axboe * relay_open_buf - create a new relay channel buffer 381b86ff981SJens Axboe * 382b86ff981SJens Axboe * Internal - used by relay_open(). 383b86ff981SJens Axboe */ 384b86ff981SJens Axboe static struct rchan_buf *relay_open_buf(struct rchan *chan, 385b86ff981SJens Axboe const char *filename, 386b86ff981SJens Axboe struct dentry *parent, 387b86ff981SJens Axboe int *is_global) 388b86ff981SJens Axboe { 389b86ff981SJens Axboe struct rchan_buf *buf; 390b86ff981SJens Axboe struct dentry *dentry; 391b86ff981SJens Axboe 392b86ff981SJens Axboe if (*is_global) 393b86ff981SJens Axboe return chan->buf[0]; 394b86ff981SJens Axboe 395b86ff981SJens Axboe buf = relay_create_buf(chan); 396b86ff981SJens Axboe if (!buf) 397b86ff981SJens Axboe return NULL; 398b86ff981SJens Axboe 399b86ff981SJens Axboe /* Create file in fs */ 400b86ff981SJens Axboe dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR, 401b86ff981SJens Axboe buf, is_global); 402b86ff981SJens Axboe if (!dentry) { 403b86ff981SJens Axboe relay_destroy_buf(buf); 404b86ff981SJens Axboe return NULL; 405b86ff981SJens Axboe } 406b86ff981SJens Axboe 407b86ff981SJens Axboe buf->dentry = dentry; 408b86ff981SJens Axboe __relay_reset(buf, 1); 409b86ff981SJens Axboe 410b86ff981SJens Axboe return buf; 411b86ff981SJens Axboe } 412b86ff981SJens Axboe 413b86ff981SJens Axboe /** 414b86ff981SJens Axboe * relay_close_buf - close a channel buffer 415b86ff981SJens Axboe * @buf: channel buffer 416b86ff981SJens Axboe * 417b86ff981SJens Axboe * Marks the buffer finalized and restores the default callbacks. 418b86ff981SJens Axboe * The channel buffer and channel buffer data structure are then freed 419b86ff981SJens Axboe * automatically when the last reference is given up. 420b86ff981SJens Axboe */ 421b86ff981SJens Axboe static inline void relay_close_buf(struct rchan_buf *buf) 422b86ff981SJens Axboe { 423b86ff981SJens Axboe buf->finalized = 1; 424b86ff981SJens Axboe cancel_delayed_work(&buf->wake_readers); 425b86ff981SJens Axboe flush_scheduled_work(); 426b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 427b86ff981SJens Axboe } 428b86ff981SJens Axboe 429b86ff981SJens Axboe static inline void setup_callbacks(struct rchan *chan, 430b86ff981SJens Axboe struct rchan_callbacks *cb) 431b86ff981SJens Axboe { 432b86ff981SJens Axboe if (!cb) { 433b86ff981SJens Axboe chan->cb = &default_channel_callbacks; 434b86ff981SJens Axboe return; 435b86ff981SJens Axboe } 436b86ff981SJens Axboe 437b86ff981SJens Axboe if (!cb->subbuf_start) 438b86ff981SJens Axboe cb->subbuf_start = subbuf_start_default_callback; 439b86ff981SJens Axboe if (!cb->buf_mapped) 440b86ff981SJens Axboe cb->buf_mapped = buf_mapped_default_callback; 441b86ff981SJens Axboe if (!cb->buf_unmapped) 442b86ff981SJens Axboe cb->buf_unmapped = buf_unmapped_default_callback; 443b86ff981SJens Axboe if (!cb->create_buf_file) 444b86ff981SJens Axboe cb->create_buf_file = create_buf_file_default_callback; 445b86ff981SJens Axboe if (!cb->remove_buf_file) 446b86ff981SJens Axboe cb->remove_buf_file = remove_buf_file_default_callback; 447b86ff981SJens Axboe chan->cb = cb; 448b86ff981SJens Axboe } 449b86ff981SJens Axboe 450b86ff981SJens Axboe /** 451b86ff981SJens Axboe * relay_open - create a new relay channel 452b86ff981SJens Axboe * @base_filename: base name of files to create 4534c78a663SRandy Dunlap * @parent: dentry of parent directory, %NULL for root directory 454b86ff981SJens Axboe * @subbuf_size: size of sub-buffers 455b86ff981SJens Axboe * @n_subbufs: number of sub-buffers 456b86ff981SJens Axboe * @cb: client callback functions 457b86ff981SJens Axboe * 4584c78a663SRandy Dunlap * Returns channel pointer if successful, %NULL otherwise. 459b86ff981SJens Axboe * 460b86ff981SJens Axboe * Creates a channel buffer for each cpu using the sizes and 461b86ff981SJens Axboe * attributes specified. The created channel buffer files 462b86ff981SJens Axboe * will be named base_filename0...base_filenameN-1. File 463b86ff981SJens Axboe * permissions will be S_IRUSR. 464b86ff981SJens Axboe */ 465b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename, 466b86ff981SJens Axboe struct dentry *parent, 467b86ff981SJens Axboe size_t subbuf_size, 468b86ff981SJens Axboe size_t n_subbufs, 469b86ff981SJens Axboe struct rchan_callbacks *cb) 470b86ff981SJens Axboe { 471b86ff981SJens Axboe unsigned int i; 472b86ff981SJens Axboe struct rchan *chan; 473b86ff981SJens Axboe char *tmpname; 474b86ff981SJens Axboe int is_global = 0; 475b86ff981SJens Axboe 476b86ff981SJens Axboe if (!base_filename) 477b86ff981SJens Axboe return NULL; 478b86ff981SJens Axboe 479b86ff981SJens Axboe if (!(subbuf_size && n_subbufs)) 480b86ff981SJens Axboe return NULL; 481b86ff981SJens Axboe 482*cd861280SRobert P. J. Day chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); 483b86ff981SJens Axboe if (!chan) 484b86ff981SJens Axboe return NULL; 485b86ff981SJens Axboe 486b86ff981SJens Axboe chan->version = RELAYFS_CHANNEL_VERSION; 487b86ff981SJens Axboe chan->n_subbufs = n_subbufs; 488b86ff981SJens Axboe chan->subbuf_size = subbuf_size; 489b86ff981SJens Axboe chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs); 490b86ff981SJens Axboe setup_callbacks(chan, cb); 491b86ff981SJens Axboe kref_init(&chan->kref); 492b86ff981SJens Axboe 493b86ff981SJens Axboe tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL); 494b86ff981SJens Axboe if (!tmpname) 495b86ff981SJens Axboe goto free_chan; 496b86ff981SJens Axboe 497b86ff981SJens Axboe for_each_online_cpu(i) { 498b86ff981SJens Axboe sprintf(tmpname, "%s%d", base_filename, i); 499b86ff981SJens Axboe chan->buf[i] = relay_open_buf(chan, tmpname, parent, 500b86ff981SJens Axboe &is_global); 501b86ff981SJens Axboe if (!chan->buf[i]) 502b86ff981SJens Axboe goto free_bufs; 503b86ff981SJens Axboe 504b86ff981SJens Axboe chan->buf[i]->cpu = i; 505b86ff981SJens Axboe } 506b86ff981SJens Axboe 507b86ff981SJens Axboe kfree(tmpname); 508b86ff981SJens Axboe return chan; 509b86ff981SJens Axboe 510b86ff981SJens Axboe free_bufs: 511b86ff981SJens Axboe for (i = 0; i < NR_CPUS; i++) { 512b86ff981SJens Axboe if (!chan->buf[i]) 513b86ff981SJens Axboe break; 514b86ff981SJens Axboe relay_close_buf(chan->buf[i]); 515b86ff981SJens Axboe if (is_global) 516b86ff981SJens Axboe break; 517b86ff981SJens Axboe } 518b86ff981SJens Axboe kfree(tmpname); 519b86ff981SJens Axboe 520b86ff981SJens Axboe free_chan: 521b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 522b86ff981SJens Axboe return NULL; 523b86ff981SJens Axboe } 524b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open); 525b86ff981SJens Axboe 526b86ff981SJens Axboe /** 527b86ff981SJens Axboe * relay_switch_subbuf - switch to a new sub-buffer 528b86ff981SJens Axboe * @buf: channel buffer 529b86ff981SJens Axboe * @length: size of current event 530b86ff981SJens Axboe * 531b86ff981SJens Axboe * Returns either the length passed in or 0 if full. 532b86ff981SJens Axboe * 533b86ff981SJens Axboe * Performs sub-buffer-switch tasks such as invoking callbacks, 534b86ff981SJens Axboe * updating padding counts, waking up readers, etc. 535b86ff981SJens Axboe */ 536b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) 537b86ff981SJens Axboe { 538b86ff981SJens Axboe void *old, *new; 539b86ff981SJens Axboe size_t old_subbuf, new_subbuf; 540b86ff981SJens Axboe 541b86ff981SJens Axboe if (unlikely(length > buf->chan->subbuf_size)) 542b86ff981SJens Axboe goto toobig; 543b86ff981SJens Axboe 544b86ff981SJens Axboe if (buf->offset != buf->chan->subbuf_size + 1) { 545b86ff981SJens Axboe buf->prev_padding = buf->chan->subbuf_size - buf->offset; 546b86ff981SJens Axboe old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 547b86ff981SJens Axboe buf->padding[old_subbuf] = buf->prev_padding; 548b86ff981SJens Axboe buf->subbufs_produced++; 549221415d7SJens Axboe buf->dentry->d_inode->i_size += buf->chan->subbuf_size - 550221415d7SJens Axboe buf->padding[old_subbuf]; 551221415d7SJens Axboe smp_mb(); 552b86ff981SJens Axboe if (waitqueue_active(&buf->read_wait)) { 553c4028958SDavid Howells PREPARE_DELAYED_WORK(&buf->wake_readers, 554c4028958SDavid Howells wakeup_readers); 555b86ff981SJens Axboe schedule_delayed_work(&buf->wake_readers, 1); 556b86ff981SJens Axboe } 557b86ff981SJens Axboe } 558b86ff981SJens Axboe 559b86ff981SJens Axboe old = buf->data; 560b86ff981SJens Axboe new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 561b86ff981SJens Axboe new = buf->start + new_subbuf * buf->chan->subbuf_size; 562b86ff981SJens Axboe buf->offset = 0; 563b86ff981SJens Axboe if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { 564b86ff981SJens Axboe buf->offset = buf->chan->subbuf_size + 1; 565b86ff981SJens Axboe return 0; 566b86ff981SJens Axboe } 567b86ff981SJens Axboe buf->data = new; 568b86ff981SJens Axboe buf->padding[new_subbuf] = 0; 569b86ff981SJens Axboe 570b86ff981SJens Axboe if (unlikely(length + buf->offset > buf->chan->subbuf_size)) 571b86ff981SJens Axboe goto toobig; 572b86ff981SJens Axboe 573b86ff981SJens Axboe return length; 574b86ff981SJens Axboe 575b86ff981SJens Axboe toobig: 576b86ff981SJens Axboe buf->chan->last_toobig = length; 577b86ff981SJens Axboe return 0; 578b86ff981SJens Axboe } 579b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf); 580b86ff981SJens Axboe 581b86ff981SJens Axboe /** 582b86ff981SJens Axboe * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count 583b86ff981SJens Axboe * @chan: the channel 584b86ff981SJens Axboe * @cpu: the cpu associated with the channel buffer to update 585b86ff981SJens Axboe * @subbufs_consumed: number of sub-buffers to add to current buf's count 586b86ff981SJens Axboe * 587b86ff981SJens Axboe * Adds to the channel buffer's consumed sub-buffer count. 588b86ff981SJens Axboe * subbufs_consumed should be the number of sub-buffers newly consumed, 589b86ff981SJens Axboe * not the total consumed. 590b86ff981SJens Axboe * 5914c78a663SRandy Dunlap * NOTE: Kernel clients don't need to call this function if the channel 592b86ff981SJens Axboe * mode is 'overwrite'. 593b86ff981SJens Axboe */ 594b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan, 595b86ff981SJens Axboe unsigned int cpu, 596b86ff981SJens Axboe size_t subbufs_consumed) 597b86ff981SJens Axboe { 598b86ff981SJens Axboe struct rchan_buf *buf; 599b86ff981SJens Axboe 600b86ff981SJens Axboe if (!chan) 601b86ff981SJens Axboe return; 602b86ff981SJens Axboe 603b86ff981SJens Axboe if (cpu >= NR_CPUS || !chan->buf[cpu]) 604b86ff981SJens Axboe return; 605b86ff981SJens Axboe 606b86ff981SJens Axboe buf = chan->buf[cpu]; 607b86ff981SJens Axboe buf->subbufs_consumed += subbufs_consumed; 608b86ff981SJens Axboe if (buf->subbufs_consumed > buf->subbufs_produced) 609b86ff981SJens Axboe buf->subbufs_consumed = buf->subbufs_produced; 610b86ff981SJens Axboe } 611b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed); 612b86ff981SJens Axboe 613b86ff981SJens Axboe /** 614b86ff981SJens Axboe * relay_close - close the channel 615b86ff981SJens Axboe * @chan: the channel 616b86ff981SJens Axboe * 617b86ff981SJens Axboe * Closes all channel buffers and frees the channel. 618b86ff981SJens Axboe */ 619b86ff981SJens Axboe void relay_close(struct rchan *chan) 620b86ff981SJens Axboe { 621b86ff981SJens Axboe unsigned int i; 622b86ff981SJens Axboe struct rchan_buf *prev = NULL; 623b86ff981SJens Axboe 624b86ff981SJens Axboe if (!chan) 625b86ff981SJens Axboe return; 626b86ff981SJens Axboe 627b86ff981SJens Axboe for (i = 0; i < NR_CPUS; i++) { 628b86ff981SJens Axboe if (!chan->buf[i] || chan->buf[i] == prev) 629b86ff981SJens Axboe break; 630b86ff981SJens Axboe relay_close_buf(chan->buf[i]); 631b86ff981SJens Axboe prev = chan->buf[i]; 632b86ff981SJens Axboe } 633b86ff981SJens Axboe 634b86ff981SJens Axboe if (chan->last_toobig) 635b86ff981SJens Axboe printk(KERN_WARNING "relay: one or more items not logged " 636b86ff981SJens Axboe "[item size (%Zd) > sub-buffer size (%Zd)]\n", 637b86ff981SJens Axboe chan->last_toobig, chan->subbuf_size); 638b86ff981SJens Axboe 639b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 640b86ff981SJens Axboe } 641b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close); 642b86ff981SJens Axboe 643b86ff981SJens Axboe /** 644b86ff981SJens Axboe * relay_flush - close the channel 645b86ff981SJens Axboe * @chan: the channel 646b86ff981SJens Axboe * 6474c78a663SRandy Dunlap * Flushes all channel buffers, i.e. forces buffer switch. 648b86ff981SJens Axboe */ 649b86ff981SJens Axboe void relay_flush(struct rchan *chan) 650b86ff981SJens Axboe { 651b86ff981SJens Axboe unsigned int i; 652b86ff981SJens Axboe struct rchan_buf *prev = NULL; 653b86ff981SJens Axboe 654b86ff981SJens Axboe if (!chan) 655b86ff981SJens Axboe return; 656b86ff981SJens Axboe 657b86ff981SJens Axboe for (i = 0; i < NR_CPUS; i++) { 658b86ff981SJens Axboe if (!chan->buf[i] || chan->buf[i] == prev) 659b86ff981SJens Axboe break; 660b86ff981SJens Axboe relay_switch_subbuf(chan->buf[i], 0); 661b86ff981SJens Axboe prev = chan->buf[i]; 662b86ff981SJens Axboe } 663b86ff981SJens Axboe } 664b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush); 665b86ff981SJens Axboe 666b86ff981SJens Axboe /** 667b86ff981SJens Axboe * relay_file_open - open file op for relay files 668b86ff981SJens Axboe * @inode: the inode 669b86ff981SJens Axboe * @filp: the file 670b86ff981SJens Axboe * 671b86ff981SJens Axboe * Increments the channel buffer refcount. 672b86ff981SJens Axboe */ 673b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp) 674b86ff981SJens Axboe { 6758e18e294STheodore Ts'o struct rchan_buf *buf = inode->i_private; 676b86ff981SJens Axboe kref_get(&buf->kref); 677b86ff981SJens Axboe filp->private_data = buf; 678b86ff981SJens Axboe 679b86ff981SJens Axboe return 0; 680b86ff981SJens Axboe } 681b86ff981SJens Axboe 682b86ff981SJens Axboe /** 683b86ff981SJens Axboe * relay_file_mmap - mmap file op for relay files 684b86ff981SJens Axboe * @filp: the file 685b86ff981SJens Axboe * @vma: the vma describing what to map 686b86ff981SJens Axboe * 687b86ff981SJens Axboe * Calls upon relay_mmap_buf to map the file into user space. 688b86ff981SJens Axboe */ 689b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) 690b86ff981SJens Axboe { 691b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 692b86ff981SJens Axboe return relay_mmap_buf(buf, vma); 693b86ff981SJens Axboe } 694b86ff981SJens Axboe 695b86ff981SJens Axboe /** 696b86ff981SJens Axboe * relay_file_poll - poll file op for relay files 697b86ff981SJens Axboe * @filp: the file 698b86ff981SJens Axboe * @wait: poll table 699b86ff981SJens Axboe * 700b86ff981SJens Axboe * Poll implemention. 701b86ff981SJens Axboe */ 702b86ff981SJens Axboe static unsigned int relay_file_poll(struct file *filp, poll_table *wait) 703b86ff981SJens Axboe { 704b86ff981SJens Axboe unsigned int mask = 0; 705b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 706b86ff981SJens Axboe 707b86ff981SJens Axboe if (buf->finalized) 708b86ff981SJens Axboe return POLLERR; 709b86ff981SJens Axboe 710b86ff981SJens Axboe if (filp->f_mode & FMODE_READ) { 711b86ff981SJens Axboe poll_wait(filp, &buf->read_wait, wait); 712b86ff981SJens Axboe if (!relay_buf_empty(buf)) 713b86ff981SJens Axboe mask |= POLLIN | POLLRDNORM; 714b86ff981SJens Axboe } 715b86ff981SJens Axboe 716b86ff981SJens Axboe return mask; 717b86ff981SJens Axboe } 718b86ff981SJens Axboe 719b86ff981SJens Axboe /** 720b86ff981SJens Axboe * relay_file_release - release file op for relay files 721b86ff981SJens Axboe * @inode: the inode 722b86ff981SJens Axboe * @filp: the file 723b86ff981SJens Axboe * 724b86ff981SJens Axboe * Decrements the channel refcount, as the filesystem is 725b86ff981SJens Axboe * no longer using it. 726b86ff981SJens Axboe */ 727b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp) 728b86ff981SJens Axboe { 729b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 730b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 731b86ff981SJens Axboe 732b86ff981SJens Axboe return 0; 733b86ff981SJens Axboe } 734b86ff981SJens Axboe 7354c78a663SRandy Dunlap /* 736b86ff981SJens Axboe * relay_file_read_consume - update the consumed count for the buffer 737b86ff981SJens Axboe */ 738b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf, 739b86ff981SJens Axboe size_t read_pos, 740b86ff981SJens Axboe size_t bytes_consumed) 741b86ff981SJens Axboe { 742b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 743b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 744b86ff981SJens Axboe size_t read_subbuf; 745b86ff981SJens Axboe 746b86ff981SJens Axboe if (buf->bytes_consumed + bytes_consumed > subbuf_size) { 747b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 748b86ff981SJens Axboe buf->bytes_consumed = 0; 749b86ff981SJens Axboe } 750b86ff981SJens Axboe 751b86ff981SJens Axboe buf->bytes_consumed += bytes_consumed; 752b86ff981SJens Axboe read_subbuf = read_pos / buf->chan->subbuf_size; 753b86ff981SJens Axboe if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { 754b86ff981SJens Axboe if ((read_subbuf == buf->subbufs_produced % n_subbufs) && 755b86ff981SJens Axboe (buf->offset == subbuf_size)) 756b86ff981SJens Axboe return; 757b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 758b86ff981SJens Axboe buf->bytes_consumed = 0; 759b86ff981SJens Axboe } 760b86ff981SJens Axboe } 761b86ff981SJens Axboe 7624c78a663SRandy Dunlap /* 763b86ff981SJens Axboe * relay_file_read_avail - boolean, are there unconsumed bytes available? 764b86ff981SJens Axboe */ 765b86ff981SJens Axboe static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) 766b86ff981SJens Axboe { 767b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 768b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 769221415d7SJens Axboe size_t produced = buf->subbufs_produced; 770221415d7SJens Axboe size_t consumed = buf->subbufs_consumed; 771b86ff981SJens Axboe 772b86ff981SJens Axboe relay_file_read_consume(buf, read_pos, 0); 773b86ff981SJens Axboe 774221415d7SJens Axboe if (unlikely(buf->offset > subbuf_size)) { 775221415d7SJens Axboe if (produced == consumed) 776221415d7SJens Axboe return 0; 777221415d7SJens Axboe return 1; 778221415d7SJens Axboe } 779221415d7SJens Axboe 780221415d7SJens Axboe if (unlikely(produced - consumed >= n_subbufs)) { 781221415d7SJens Axboe consumed = (produced / n_subbufs) * n_subbufs; 782221415d7SJens Axboe buf->subbufs_consumed = consumed; 783221415d7SJens Axboe } 784221415d7SJens Axboe 785221415d7SJens Axboe produced = (produced % n_subbufs) * subbuf_size + buf->offset; 786221415d7SJens Axboe consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; 787221415d7SJens Axboe 788221415d7SJens Axboe if (consumed > produced) 789221415d7SJens Axboe produced += n_subbufs * subbuf_size; 790221415d7SJens Axboe 791221415d7SJens Axboe if (consumed == produced) 792221415d7SJens Axboe return 0; 793221415d7SJens Axboe 794b86ff981SJens Axboe return 1; 795b86ff981SJens Axboe } 796b86ff981SJens Axboe 797b86ff981SJens Axboe /** 798b86ff981SJens Axboe * relay_file_read_subbuf_avail - return bytes available in sub-buffer 7994c78a663SRandy Dunlap * @read_pos: file read position 8004c78a663SRandy Dunlap * @buf: relay channel buffer 801b86ff981SJens Axboe */ 802b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos, 803b86ff981SJens Axboe struct rchan_buf *buf) 804b86ff981SJens Axboe { 805b86ff981SJens Axboe size_t padding, avail = 0; 806b86ff981SJens Axboe size_t read_subbuf, read_offset, write_subbuf, write_offset; 807b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 808b86ff981SJens Axboe 809b86ff981SJens Axboe write_subbuf = (buf->data - buf->start) / subbuf_size; 810b86ff981SJens Axboe write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; 811b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 812b86ff981SJens Axboe read_offset = read_pos % subbuf_size; 813b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 814b86ff981SJens Axboe 815b86ff981SJens Axboe if (read_subbuf == write_subbuf) { 816b86ff981SJens Axboe if (read_offset + padding < write_offset) 817b86ff981SJens Axboe avail = write_offset - (read_offset + padding); 818b86ff981SJens Axboe } else 819b86ff981SJens Axboe avail = (subbuf_size - padding) - read_offset; 820b86ff981SJens Axboe 821b86ff981SJens Axboe return avail; 822b86ff981SJens Axboe } 823b86ff981SJens Axboe 824b86ff981SJens Axboe /** 825b86ff981SJens Axboe * relay_file_read_start_pos - find the first available byte to read 8264c78a663SRandy Dunlap * @read_pos: file read position 8274c78a663SRandy Dunlap * @buf: relay channel buffer 828b86ff981SJens Axboe * 829b86ff981SJens Axboe * If the read_pos is in the middle of padding, return the 830b86ff981SJens Axboe * position of the first actually available byte, otherwise 831b86ff981SJens Axboe * return the original value. 832b86ff981SJens Axboe */ 833b86ff981SJens Axboe static size_t relay_file_read_start_pos(size_t read_pos, 834b86ff981SJens Axboe struct rchan_buf *buf) 835b86ff981SJens Axboe { 836b86ff981SJens Axboe size_t read_subbuf, padding, padding_start, padding_end; 837b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 838b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 839b86ff981SJens Axboe 840b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 841b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 842b86ff981SJens Axboe padding_start = (read_subbuf + 1) * subbuf_size - padding; 843b86ff981SJens Axboe padding_end = (read_subbuf + 1) * subbuf_size; 844b86ff981SJens Axboe if (read_pos >= padding_start && read_pos < padding_end) { 845b86ff981SJens Axboe read_subbuf = (read_subbuf + 1) % n_subbufs; 846b86ff981SJens Axboe read_pos = read_subbuf * subbuf_size; 847b86ff981SJens Axboe } 848b86ff981SJens Axboe 849b86ff981SJens Axboe return read_pos; 850b86ff981SJens Axboe } 851b86ff981SJens Axboe 852b86ff981SJens Axboe /** 853b86ff981SJens Axboe * relay_file_read_end_pos - return the new read position 8544c78a663SRandy Dunlap * @read_pos: file read position 8554c78a663SRandy Dunlap * @buf: relay channel buffer 8564c78a663SRandy Dunlap * @count: number of bytes to be read 857b86ff981SJens Axboe */ 858b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf, 859b86ff981SJens Axboe size_t read_pos, 860b86ff981SJens Axboe size_t count) 861b86ff981SJens Axboe { 862b86ff981SJens Axboe size_t read_subbuf, padding, end_pos; 863b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 864b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 865b86ff981SJens Axboe 866b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 867b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 868b86ff981SJens Axboe if (read_pos % subbuf_size + count + padding == subbuf_size) 869b86ff981SJens Axboe end_pos = (read_subbuf + 1) * subbuf_size; 870b86ff981SJens Axboe else 871b86ff981SJens Axboe end_pos = read_pos + count; 872b86ff981SJens Axboe if (end_pos >= subbuf_size * n_subbufs) 873b86ff981SJens Axboe end_pos = 0; 874b86ff981SJens Axboe 875b86ff981SJens Axboe return end_pos; 876b86ff981SJens Axboe } 877b86ff981SJens Axboe 8784c78a663SRandy Dunlap /* 8796dac40a7STom Zanussi * subbuf_read_actor - read up to one subbuf's worth of data 880b86ff981SJens Axboe */ 8816dac40a7STom Zanussi static int subbuf_read_actor(size_t read_start, 8826dac40a7STom Zanussi struct rchan_buf *buf, 8836dac40a7STom Zanussi size_t avail, 8846dac40a7STom Zanussi read_descriptor_t *desc, 8856dac40a7STom Zanussi read_actor_t actor) 886b86ff981SJens Axboe { 887b86ff981SJens Axboe void *from; 8886dac40a7STom Zanussi int ret = 0; 889b86ff981SJens Axboe 890b86ff981SJens Axboe from = buf->start + read_start; 8916dac40a7STom Zanussi ret = avail; 892ba2397efSAl Viro if (copy_to_user(desc->arg.buf, from, avail)) { 8936dac40a7STom Zanussi desc->error = -EFAULT; 8946dac40a7STom Zanussi ret = 0; 895b86ff981SJens Axboe } 8966dac40a7STom Zanussi desc->arg.data += ret; 8976dac40a7STom Zanussi desc->written += ret; 8986dac40a7STom Zanussi desc->count -= ret; 8996dac40a7STom Zanussi 900b86ff981SJens Axboe return ret; 901b86ff981SJens Axboe } 902b86ff981SJens Axboe 9034c78a663SRandy Dunlap /* 9046dac40a7STom Zanussi * subbuf_send_actor - send up to one subbuf's worth of data 9056dac40a7STom Zanussi */ 9066dac40a7STom Zanussi static int subbuf_send_actor(size_t read_start, 9076dac40a7STom Zanussi struct rchan_buf *buf, 9086dac40a7STom Zanussi size_t avail, 9096dac40a7STom Zanussi read_descriptor_t *desc, 9106dac40a7STom Zanussi read_actor_t actor) 9116dac40a7STom Zanussi { 9126dac40a7STom Zanussi unsigned long pidx, poff; 9136dac40a7STom Zanussi unsigned int subbuf_pages; 9146dac40a7STom Zanussi int ret = 0; 9156dac40a7STom Zanussi 9166dac40a7STom Zanussi subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT; 9176dac40a7STom Zanussi pidx = (read_start / PAGE_SIZE) % subbuf_pages; 9186dac40a7STom Zanussi poff = read_start & ~PAGE_MASK; 9196dac40a7STom Zanussi while (avail) { 9206dac40a7STom Zanussi struct page *p = buf->page_array[pidx]; 9216dac40a7STom Zanussi unsigned int len; 9226dac40a7STom Zanussi 9236dac40a7STom Zanussi len = PAGE_SIZE - poff; 9246dac40a7STom Zanussi if (len > avail) 9256dac40a7STom Zanussi len = avail; 9266dac40a7STom Zanussi 9276dac40a7STom Zanussi len = actor(desc, p, poff, len); 9286dac40a7STom Zanussi if (desc->error) 9296dac40a7STom Zanussi break; 9306dac40a7STom Zanussi 9316dac40a7STom Zanussi avail -= len; 9326dac40a7STom Zanussi ret += len; 9336dac40a7STom Zanussi poff = 0; 9346dac40a7STom Zanussi pidx = (pidx + 1) % subbuf_pages; 9356dac40a7STom Zanussi } 9366dac40a7STom Zanussi 9376dac40a7STom Zanussi return ret; 9386dac40a7STom Zanussi } 9396dac40a7STom Zanussi 9406dac40a7STom Zanussi typedef int (*subbuf_actor_t) (size_t read_start, 9416dac40a7STom Zanussi struct rchan_buf *buf, 9426dac40a7STom Zanussi size_t avail, 9436dac40a7STom Zanussi read_descriptor_t *desc, 9446dac40a7STom Zanussi read_actor_t actor); 9456dac40a7STom Zanussi 9464c78a663SRandy Dunlap /* 9476dac40a7STom Zanussi * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries 9486dac40a7STom Zanussi */ 9496dac40a7STom Zanussi static inline ssize_t relay_file_read_subbufs(struct file *filp, 9506dac40a7STom Zanussi loff_t *ppos, 9516dac40a7STom Zanussi subbuf_actor_t subbuf_actor, 9526dac40a7STom Zanussi read_actor_t actor, 953ba2397efSAl Viro read_descriptor_t *desc) 954221415d7SJens Axboe { 955221415d7SJens Axboe struct rchan_buf *buf = filp->private_data; 956221415d7SJens Axboe size_t read_start, avail; 9576dac40a7STom Zanussi int ret; 958221415d7SJens Axboe 959ba2397efSAl Viro if (!desc->count) 960221415d7SJens Axboe return 0; 961221415d7SJens Axboe 962f3a43f3fSJosef "Jeff" Sipek mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); 9636dac40a7STom Zanussi do { 9646dac40a7STom Zanussi if (!relay_file_read_avail(buf, *ppos)) 965221415d7SJens Axboe break; 966221415d7SJens Axboe 9676dac40a7STom Zanussi read_start = relay_file_read_start_pos(*ppos, buf); 9686dac40a7STom Zanussi avail = relay_file_read_subbuf_avail(read_start, buf); 9696dac40a7STom Zanussi if (!avail) 9706dac40a7STom Zanussi break; 971221415d7SJens Axboe 972ba2397efSAl Viro avail = min(desc->count, avail); 973ba2397efSAl Viro ret = subbuf_actor(read_start, buf, avail, desc, actor); 974ba2397efSAl Viro if (desc->error < 0) 9756dac40a7STom Zanussi break; 9766dac40a7STom Zanussi 9776dac40a7STom Zanussi if (ret) { 978221415d7SJens Axboe relay_file_read_consume(buf, read_start, ret); 979221415d7SJens Axboe *ppos = relay_file_read_end_pos(buf, read_start, ret); 980221415d7SJens Axboe } 981ba2397efSAl Viro } while (desc->count && ret); 982f3a43f3fSJosef "Jeff" Sipek mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); 983221415d7SJens Axboe 984ba2397efSAl Viro return desc->written; 985221415d7SJens Axboe } 986221415d7SJens Axboe 9876dac40a7STom Zanussi static ssize_t relay_file_read(struct file *filp, 9886dac40a7STom Zanussi char __user *buffer, 9896dac40a7STom Zanussi size_t count, 9906dac40a7STom Zanussi loff_t *ppos) 9916dac40a7STom Zanussi { 992ba2397efSAl Viro read_descriptor_t desc; 993ba2397efSAl Viro desc.written = 0; 994ba2397efSAl Viro desc.count = count; 995ba2397efSAl Viro desc.arg.buf = buffer; 996ba2397efSAl Viro desc.error = 0; 997ba2397efSAl Viro return relay_file_read_subbufs(filp, ppos, subbuf_read_actor, 998ba2397efSAl Viro NULL, &desc); 9996dac40a7STom Zanussi } 10006dac40a7STom Zanussi 10016dac40a7STom Zanussi static ssize_t relay_file_sendfile(struct file *filp, 10026dac40a7STom Zanussi loff_t *ppos, 10036dac40a7STom Zanussi size_t count, 10046dac40a7STom Zanussi read_actor_t actor, 1005221415d7SJens Axboe void *target) 1006221415d7SJens Axboe { 1007ba2397efSAl Viro read_descriptor_t desc; 1008ba2397efSAl Viro desc.written = 0; 1009ba2397efSAl Viro desc.count = count; 1010ba2397efSAl Viro desc.arg.data = target; 1011ba2397efSAl Viro desc.error = 0; 1012ba2397efSAl Viro return relay_file_read_subbufs(filp, ppos, subbuf_send_actor, 1013ba2397efSAl Viro actor, &desc); 1014221415d7SJens Axboe } 1015221415d7SJens Axboe 101615ad7cdcSHelge Deller const struct file_operations relay_file_operations = { 1017b86ff981SJens Axboe .open = relay_file_open, 1018b86ff981SJens Axboe .poll = relay_file_poll, 1019b86ff981SJens Axboe .mmap = relay_file_mmap, 1020b86ff981SJens Axboe .read = relay_file_read, 1021b86ff981SJens Axboe .llseek = no_llseek, 1022b86ff981SJens Axboe .release = relay_file_release, 1023221415d7SJens Axboe .sendfile = relay_file_sendfile, 1024b86ff981SJens Axboe }; 1025b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations); 1026