Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Public API and common code for kernel->userspace relay file support. |
| 3 | * |
Jesper Juhl | c9b3feb | 2007-07-31 00:39:18 -0700 | [diff] [blame] | 4 | * See Documentation/filesystems/relay.txt for an overview. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp |
| 7 | * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com) |
| 8 | * |
| 9 | * Moved to kernel/relay.c by Paul Mundt, 2006. |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 10 | * November 2006 - CPU hotplug support by Mathieu Desnoyers |
| 11 | * (mathieu.desnoyers@polymtl.ca) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 12 | * |
| 13 | * This file is released under the GPL. |
| 14 | */ |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/stddef.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/relay.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/mm.h> |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 23 | #include <linux/cpu.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 24 | #include <linux/splice.h> |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 25 | |
| 26 | /* list of open channels, for cpu hotplug */ |
| 27 | static DEFINE_MUTEX(relay_channels_mutex); |
| 28 | static LIST_HEAD(relay_channels); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * close() vm_op implementation for relay file mapping. |
| 32 | */ |
| 33 | static void relay_file_mmap_close(struct vm_area_struct *vma) |
| 34 | { |
| 35 | struct rchan_buf *buf = vma->vm_private_data; |
| 36 | buf->chan->cb->buf_unmapped(buf, vma->vm_file); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * nopage() vm_op implementation for relay file mapping. |
| 41 | */ |
| 42 | static struct page *relay_buf_nopage(struct vm_area_struct *vma, |
| 43 | unsigned long address, |
| 44 | int *type) |
| 45 | { |
| 46 | struct page *page; |
| 47 | struct rchan_buf *buf = vma->vm_private_data; |
| 48 | unsigned long offset = address - vma->vm_start; |
| 49 | |
| 50 | if (address > vma->vm_end) |
| 51 | return NOPAGE_SIGBUS; /* Disallow mremap */ |
| 52 | if (!buf) |
| 53 | return NOPAGE_OOM; |
| 54 | |
| 55 | page = vmalloc_to_page(buf->start + offset); |
| 56 | if (!page) |
| 57 | return NOPAGE_OOM; |
| 58 | get_page(page); |
| 59 | |
| 60 | if (type) |
| 61 | *type = VM_FAULT_MINOR; |
| 62 | |
| 63 | return page; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * vm_ops for relay file mappings. |
| 68 | */ |
| 69 | static struct vm_operations_struct relay_file_mmap_ops = { |
| 70 | .nopage = relay_buf_nopage, |
| 71 | .close = relay_file_mmap_close, |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * relay_mmap_buf: - mmap channel buffer to process address space |
| 76 | * @buf: relay channel buffer |
| 77 | * @vma: vm_area_struct describing memory to be mapped |
| 78 | * |
| 79 | * Returns 0 if ok, negative on error |
| 80 | * |
| 81 | * Caller should already have grabbed mmap_sem. |
| 82 | */ |
Adrian Bunk | 01c55ed | 2007-07-19 01:48:32 -0700 | [diff] [blame] | 83 | static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 84 | { |
| 85 | unsigned long length = vma->vm_end - vma->vm_start; |
| 86 | struct file *filp = vma->vm_file; |
| 87 | |
| 88 | if (!buf) |
| 89 | return -EBADF; |
| 90 | |
| 91 | if (length != (unsigned long)buf->chan->alloc_size) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | vma->vm_ops = &relay_file_mmap_ops; |
Nick Piggin | 2f98735 | 2008-02-02 03:08:53 +0100 | [diff] [blame^] | 95 | vma->vm_flags |= VM_DONTEXPAND; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 96 | vma->vm_private_data = buf; |
| 97 | buf->chan->cb->buf_mapped(buf, filp); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * relay_alloc_buf - allocate a channel buffer |
| 104 | * @buf: the buffer struct |
| 105 | * @size: total size of the buffer |
| 106 | * |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 107 | * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 108 | * passed in size will get page aligned, if it isn't already. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 109 | */ |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 110 | static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 111 | { |
| 112 | void *mem; |
| 113 | unsigned int i, j, n_pages; |
| 114 | |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 115 | *size = PAGE_ALIGN(*size); |
| 116 | n_pages = *size >> PAGE_SHIFT; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 117 | |
| 118 | buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL); |
| 119 | if (!buf->page_array) |
| 120 | return NULL; |
| 121 | |
| 122 | for (i = 0; i < n_pages; i++) { |
| 123 | buf->page_array[i] = alloc_page(GFP_KERNEL); |
| 124 | if (unlikely(!buf->page_array[i])) |
| 125 | goto depopulate; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 126 | set_page_private(buf->page_array[i], (unsigned long)buf); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 127 | } |
| 128 | mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); |
| 129 | if (!mem) |
| 130 | goto depopulate; |
| 131 | |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 132 | memset(mem, 0, *size); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 133 | buf->page_count = n_pages; |
| 134 | return mem; |
| 135 | |
| 136 | depopulate: |
| 137 | for (j = 0; j < i; j++) |
| 138 | __free_page(buf->page_array[j]); |
| 139 | kfree(buf->page_array); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * relay_create_buf - allocate and initialize a channel buffer |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 145 | * @chan: the relay channel |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 146 | * |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 147 | * Returns channel buffer if successful, %NULL otherwise. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 148 | */ |
Adrian Bunk | 01c55ed | 2007-07-19 01:48:32 -0700 | [diff] [blame] | 149 | static struct rchan_buf *relay_create_buf(struct rchan *chan) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 150 | { |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 151 | struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 152 | if (!buf) |
| 153 | return NULL; |
| 154 | |
| 155 | buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL); |
| 156 | if (!buf->padding) |
| 157 | goto free_buf; |
| 158 | |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 159 | buf->start = relay_alloc_buf(buf, &chan->alloc_size); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 160 | if (!buf->start) |
| 161 | goto free_buf; |
| 162 | |
| 163 | buf->chan = chan; |
| 164 | kref_get(&buf->chan->kref); |
| 165 | return buf; |
| 166 | |
| 167 | free_buf: |
| 168 | kfree(buf->padding); |
| 169 | kfree(buf); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * relay_destroy_channel - free the channel struct |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 175 | * @kref: target kernel reference that contains the relay channel |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 176 | * |
| 177 | * Should only be called from kref_put(). |
| 178 | */ |
Adrian Bunk | 01c55ed | 2007-07-19 01:48:32 -0700 | [diff] [blame] | 179 | static void relay_destroy_channel(struct kref *kref) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 180 | { |
| 181 | struct rchan *chan = container_of(kref, struct rchan, kref); |
| 182 | kfree(chan); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * relay_destroy_buf - destroy an rchan_buf struct and associated buffer |
| 187 | * @buf: the buffer struct |
| 188 | */ |
Adrian Bunk | 01c55ed | 2007-07-19 01:48:32 -0700 | [diff] [blame] | 189 | static void relay_destroy_buf(struct rchan_buf *buf) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 190 | { |
| 191 | struct rchan *chan = buf->chan; |
| 192 | unsigned int i; |
| 193 | |
| 194 | if (likely(buf->start)) { |
| 195 | vunmap(buf->start); |
| 196 | for (i = 0; i < buf->page_count; i++) |
| 197 | __free_page(buf->page_array[i]); |
| 198 | kfree(buf->page_array); |
| 199 | } |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 200 | chan->buf[buf->cpu] = NULL; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 201 | kfree(buf->padding); |
| 202 | kfree(buf); |
| 203 | kref_put(&chan->kref, relay_destroy_channel); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * relay_remove_buf - remove a channel buffer |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 208 | * @kref: target kernel reference that contains the relay buffer |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 209 | * |
| 210 | * Removes the file from the fileystem, which also frees the |
| 211 | * rchan_buf_struct and the channel buffer. Should only be called from |
| 212 | * kref_put(). |
| 213 | */ |
Adrian Bunk | 01c55ed | 2007-07-19 01:48:32 -0700 | [diff] [blame] | 214 | static void relay_remove_buf(struct kref *kref) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 215 | { |
| 216 | struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); |
| 217 | buf->chan->cb->remove_buf_file(buf->dentry); |
| 218 | relay_destroy_buf(buf); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * relay_buf_empty - boolean, is the channel buffer empty? |
| 223 | * @buf: channel buffer |
| 224 | * |
| 225 | * Returns 1 if the buffer is empty, 0 otherwise. |
| 226 | */ |
Adrian Bunk | 01c55ed | 2007-07-19 01:48:32 -0700 | [diff] [blame] | 227 | static int relay_buf_empty(struct rchan_buf *buf) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 228 | { |
| 229 | return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; |
| 230 | } |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * relay_buf_full - boolean, is the channel buffer full? |
| 234 | * @buf: channel buffer |
| 235 | * |
| 236 | * Returns 1 if the buffer is full, 0 otherwise. |
| 237 | */ |
| 238 | int relay_buf_full(struct rchan_buf *buf) |
| 239 | { |
| 240 | size_t ready = buf->subbufs_produced - buf->subbufs_consumed; |
| 241 | return (ready >= buf->chan->n_subbufs) ? 1 : 0; |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(relay_buf_full); |
| 244 | |
| 245 | /* |
| 246 | * High-level relay kernel API and associated functions. |
| 247 | */ |
| 248 | |
| 249 | /* |
| 250 | * rchan_callback implementations defining default channel behavior. Used |
| 251 | * in place of corresponding NULL values in client callback struct. |
| 252 | */ |
| 253 | |
| 254 | /* |
| 255 | * subbuf_start() default callback. Does nothing. |
| 256 | */ |
| 257 | static int subbuf_start_default_callback (struct rchan_buf *buf, |
| 258 | void *subbuf, |
| 259 | void *prev_subbuf, |
| 260 | size_t prev_padding) |
| 261 | { |
| 262 | if (relay_buf_full(buf)) |
| 263 | return 0; |
| 264 | |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * buf_mapped() default callback. Does nothing. |
| 270 | */ |
| 271 | static void buf_mapped_default_callback(struct rchan_buf *buf, |
| 272 | struct file *filp) |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * buf_unmapped() default callback. Does nothing. |
| 278 | */ |
| 279 | static void buf_unmapped_default_callback(struct rchan_buf *buf, |
| 280 | struct file *filp) |
| 281 | { |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * create_buf_file_create() default callback. Does nothing. |
| 286 | */ |
| 287 | static struct dentry *create_buf_file_default_callback(const char *filename, |
| 288 | struct dentry *parent, |
| 289 | int mode, |
| 290 | struct rchan_buf *buf, |
| 291 | int *is_global) |
| 292 | { |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * remove_buf_file() default callback. Does nothing. |
| 298 | */ |
| 299 | static int remove_buf_file_default_callback(struct dentry *dentry) |
| 300 | { |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | /* relay channel default callbacks */ |
| 305 | static struct rchan_callbacks default_channel_callbacks = { |
| 306 | .subbuf_start = subbuf_start_default_callback, |
| 307 | .buf_mapped = buf_mapped_default_callback, |
| 308 | .buf_unmapped = buf_unmapped_default_callback, |
| 309 | .create_buf_file = create_buf_file_default_callback, |
| 310 | .remove_buf_file = remove_buf_file_default_callback, |
| 311 | }; |
| 312 | |
| 313 | /** |
| 314 | * wakeup_readers - wake up readers waiting on a channel |
Linus Torvalds | 9a9136e2 | 2007-05-09 12:54:17 -0700 | [diff] [blame] | 315 | * @data: contains the channel buffer |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 316 | * |
Tom Zanussi | 7c9cb38 | 2007-05-09 02:34:01 -0700 | [diff] [blame] | 317 | * This is the timer function used to defer reader waking. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 318 | */ |
Tom Zanussi | 7c9cb38 | 2007-05-09 02:34:01 -0700 | [diff] [blame] | 319 | static void wakeup_readers(unsigned long data) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 320 | { |
Tom Zanussi | 7c9cb38 | 2007-05-09 02:34:01 -0700 | [diff] [blame] | 321 | struct rchan_buf *buf = (struct rchan_buf *)data; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 322 | wake_up_interruptible(&buf->read_wait); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * __relay_reset - reset a channel buffer |
| 327 | * @buf: the channel buffer |
| 328 | * @init: 1 if this is a first-time initialization |
| 329 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 330 | * See relay_reset() for description of effect. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 331 | */ |
Andrew Morton | 192636a | 2006-12-22 01:11:30 -0800 | [diff] [blame] | 332 | static void __relay_reset(struct rchan_buf *buf, unsigned int init) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 333 | { |
| 334 | size_t i; |
| 335 | |
| 336 | if (init) { |
| 337 | init_waitqueue_head(&buf->read_wait); |
| 338 | kref_init(&buf->kref); |
Tom Zanussi | 7c9cb38 | 2007-05-09 02:34:01 -0700 | [diff] [blame] | 339 | setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf); |
| 340 | } else |
| 341 | del_timer_sync(&buf->timer); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 342 | |
| 343 | buf->subbufs_produced = 0; |
| 344 | buf->subbufs_consumed = 0; |
| 345 | buf->bytes_consumed = 0; |
| 346 | buf->finalized = 0; |
| 347 | buf->data = buf->start; |
| 348 | buf->offset = 0; |
| 349 | |
| 350 | for (i = 0; i < buf->chan->n_subbufs; i++) |
| 351 | buf->padding[i] = 0; |
| 352 | |
| 353 | buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * relay_reset - reset the channel |
| 358 | * @chan: the channel |
| 359 | * |
| 360 | * This has the effect of erasing all data from all channel buffers |
| 361 | * and restarting the channel in its initial state. The buffers |
| 362 | * are not freed, so any mappings are still in effect. |
| 363 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 364 | * NOTE. Care should be taken that the channel isn't actually |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 365 | * being used by anything when this call is made. |
| 366 | */ |
| 367 | void relay_reset(struct rchan *chan) |
| 368 | { |
| 369 | unsigned int i; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 370 | |
| 371 | if (!chan) |
| 372 | return; |
| 373 | |
Daniel Walker | 1bfbc60 | 2007-10-18 03:06:05 -0700 | [diff] [blame] | 374 | if (chan->is_global && chan->buf[0]) { |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 375 | __relay_reset(chan->buf[0], 0); |
| 376 | return; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 377 | } |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 378 | |
| 379 | mutex_lock(&relay_channels_mutex); |
| 380 | for_each_online_cpu(i) |
| 381 | if (chan->buf[i]) |
| 382 | __relay_reset(chan->buf[i], 0); |
| 383 | mutex_unlock(&relay_channels_mutex); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 384 | } |
| 385 | EXPORT_SYMBOL_GPL(relay_reset); |
| 386 | |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 387 | /* |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 388 | * relay_open_buf - create a new relay channel buffer |
| 389 | * |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 390 | * used by relay_open() and CPU hotplug. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 391 | */ |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 392 | static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 393 | { |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 394 | struct rchan_buf *buf = NULL; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 395 | struct dentry *dentry; |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 396 | char *tmpname; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 397 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 398 | if (chan->is_global) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 399 | return chan->buf[0]; |
| 400 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 401 | tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); |
| 402 | if (!tmpname) |
| 403 | goto end; |
| 404 | snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu); |
| 405 | |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 406 | buf = relay_create_buf(chan); |
| 407 | if (!buf) |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 408 | goto free_name; |
| 409 | |
| 410 | buf->cpu = cpu; |
| 411 | __relay_reset(buf, 1); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 412 | |
| 413 | /* Create file in fs */ |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 414 | dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR, |
| 415 | buf, &chan->is_global); |
| 416 | if (!dentry) |
| 417 | goto free_buf; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 418 | |
| 419 | buf->dentry = dentry; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 420 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 421 | if(chan->is_global) { |
| 422 | chan->buf[0] = buf; |
| 423 | buf->cpu = 0; |
| 424 | } |
| 425 | |
| 426 | goto free_name; |
| 427 | |
| 428 | free_buf: |
| 429 | relay_destroy_buf(buf); |
Jesper Juhl | c9b3feb | 2007-07-31 00:39:18 -0700 | [diff] [blame] | 430 | buf = NULL; |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 431 | free_name: |
| 432 | kfree(tmpname); |
| 433 | end: |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 434 | return buf; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * relay_close_buf - close a channel buffer |
| 439 | * @buf: channel buffer |
| 440 | * |
| 441 | * Marks the buffer finalized and restores the default callbacks. |
| 442 | * The channel buffer and channel buffer data structure are then freed |
| 443 | * automatically when the last reference is given up. |
| 444 | */ |
Andrew Morton | 192636a | 2006-12-22 01:11:30 -0800 | [diff] [blame] | 445 | static void relay_close_buf(struct rchan_buf *buf) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 446 | { |
| 447 | buf->finalized = 1; |
Tom Zanussi | 7c9cb38 | 2007-05-09 02:34:01 -0700 | [diff] [blame] | 448 | del_timer_sync(&buf->timer); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 449 | kref_put(&buf->kref, relay_remove_buf); |
| 450 | } |
| 451 | |
Andrew Morton | 192636a | 2006-12-22 01:11:30 -0800 | [diff] [blame] | 452 | static void setup_callbacks(struct rchan *chan, |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 453 | struct rchan_callbacks *cb) |
| 454 | { |
| 455 | if (!cb) { |
| 456 | chan->cb = &default_channel_callbacks; |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (!cb->subbuf_start) |
| 461 | cb->subbuf_start = subbuf_start_default_callback; |
| 462 | if (!cb->buf_mapped) |
| 463 | cb->buf_mapped = buf_mapped_default_callback; |
| 464 | if (!cb->buf_unmapped) |
| 465 | cb->buf_unmapped = buf_unmapped_default_callback; |
| 466 | if (!cb->create_buf_file) |
| 467 | cb->create_buf_file = create_buf_file_default_callback; |
| 468 | if (!cb->remove_buf_file) |
| 469 | cb->remove_buf_file = remove_buf_file_default_callback; |
| 470 | chan->cb = cb; |
| 471 | } |
| 472 | |
| 473 | /** |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 474 | * relay_hotcpu_callback - CPU hotplug callback |
| 475 | * @nb: notifier block |
| 476 | * @action: hotplug action to take |
| 477 | * @hcpu: CPU number |
| 478 | * |
Randy Dunlap | 05fb6bf | 2007-02-28 20:12:13 -0800 | [diff] [blame] | 479 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 480 | */ |
| 481 | static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb, |
| 482 | unsigned long action, |
| 483 | void *hcpu) |
| 484 | { |
| 485 | unsigned int hotcpu = (unsigned long)hcpu; |
| 486 | struct rchan *chan; |
| 487 | |
| 488 | switch(action) { |
| 489 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 490 | case CPU_UP_PREPARE_FROZEN: |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 491 | mutex_lock(&relay_channels_mutex); |
| 492 | list_for_each_entry(chan, &relay_channels, list) { |
| 493 | if (chan->buf[hotcpu]) |
| 494 | continue; |
| 495 | chan->buf[hotcpu] = relay_open_buf(chan, hotcpu); |
| 496 | if(!chan->buf[hotcpu]) { |
| 497 | printk(KERN_ERR |
| 498 | "relay_hotcpu_callback: cpu %d buffer " |
| 499 | "creation failed\n", hotcpu); |
| 500 | mutex_unlock(&relay_channels_mutex); |
| 501 | return NOTIFY_BAD; |
| 502 | } |
| 503 | } |
| 504 | mutex_unlock(&relay_channels_mutex); |
| 505 | break; |
| 506 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 507 | case CPU_DEAD_FROZEN: |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 508 | /* No need to flush the cpu : will be flushed upon |
| 509 | * final relay_flush() call. */ |
| 510 | break; |
| 511 | } |
| 512 | return NOTIFY_OK; |
| 513 | } |
| 514 | |
| 515 | /** |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 516 | * relay_open - create a new relay channel |
| 517 | * @base_filename: base name of files to create |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 518 | * @parent: dentry of parent directory, %NULL for root directory |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 519 | * @subbuf_size: size of sub-buffers |
| 520 | * @n_subbufs: number of sub-buffers |
| 521 | * @cb: client callback functions |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 522 | * @private_data: user-defined data |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 523 | * |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 524 | * Returns channel pointer if successful, %NULL otherwise. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 525 | * |
| 526 | * Creates a channel buffer for each cpu using the sizes and |
| 527 | * attributes specified. The created channel buffer files |
| 528 | * will be named base_filename0...base_filenameN-1. File |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 529 | * permissions will be %S_IRUSR. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 530 | */ |
| 531 | struct rchan *relay_open(const char *base_filename, |
| 532 | struct dentry *parent, |
| 533 | size_t subbuf_size, |
| 534 | size_t n_subbufs, |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 535 | struct rchan_callbacks *cb, |
| 536 | void *private_data) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 537 | { |
| 538 | unsigned int i; |
| 539 | struct rchan *chan; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 540 | if (!base_filename) |
| 541 | return NULL; |
| 542 | |
| 543 | if (!(subbuf_size && n_subbufs)) |
| 544 | return NULL; |
| 545 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 546 | chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 547 | if (!chan) |
| 548 | return NULL; |
| 549 | |
| 550 | chan->version = RELAYFS_CHANNEL_VERSION; |
| 551 | chan->n_subbufs = n_subbufs; |
| 552 | chan->subbuf_size = subbuf_size; |
| 553 | chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs); |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 554 | chan->parent = parent; |
| 555 | chan->private_data = private_data; |
| 556 | strlcpy(chan->base_filename, base_filename, NAME_MAX); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 557 | setup_callbacks(chan, cb); |
| 558 | kref_init(&chan->kref); |
| 559 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 560 | mutex_lock(&relay_channels_mutex); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 561 | for_each_online_cpu(i) { |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 562 | chan->buf[i] = relay_open_buf(chan, i); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 563 | if (!chan->buf[i]) |
| 564 | goto free_bufs; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 565 | } |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 566 | list_add(&chan->list, &relay_channels); |
| 567 | mutex_unlock(&relay_channels_mutex); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 568 | |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 569 | return chan; |
| 570 | |
| 571 | free_bufs: |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 572 | for_each_online_cpu(i) { |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 573 | if (!chan->buf[i]) |
| 574 | break; |
| 575 | relay_close_buf(chan->buf[i]); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 576 | } |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 577 | |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 578 | kref_put(&chan->kref, relay_destroy_channel); |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 579 | mutex_unlock(&relay_channels_mutex); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 580 | return NULL; |
| 581 | } |
| 582 | EXPORT_SYMBOL_GPL(relay_open); |
| 583 | |
| 584 | /** |
| 585 | * relay_switch_subbuf - switch to a new sub-buffer |
| 586 | * @buf: channel buffer |
| 587 | * @length: size of current event |
| 588 | * |
| 589 | * Returns either the length passed in or 0 if full. |
| 590 | * |
| 591 | * Performs sub-buffer-switch tasks such as invoking callbacks, |
| 592 | * updating padding counts, waking up readers, etc. |
| 593 | */ |
| 594 | size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) |
| 595 | { |
| 596 | void *old, *new; |
| 597 | size_t old_subbuf, new_subbuf; |
| 598 | |
| 599 | if (unlikely(length > buf->chan->subbuf_size)) |
| 600 | goto toobig; |
| 601 | |
| 602 | if (buf->offset != buf->chan->subbuf_size + 1) { |
| 603 | buf->prev_padding = buf->chan->subbuf_size - buf->offset; |
| 604 | old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; |
| 605 | buf->padding[old_subbuf] = buf->prev_padding; |
| 606 | buf->subbufs_produced++; |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 607 | buf->dentry->d_inode->i_size += buf->chan->subbuf_size - |
| 608 | buf->padding[old_subbuf]; |
| 609 | smp_mb(); |
Tom Zanussi | 7c9cb38 | 2007-05-09 02:34:01 -0700 | [diff] [blame] | 610 | if (waitqueue_active(&buf->read_wait)) |
| 611 | /* |
| 612 | * Calling wake_up_interruptible() from here |
| 613 | * will deadlock if we happen to be logging |
| 614 | * from the scheduler (trying to re-grab |
| 615 | * rq->lock), so defer it. |
| 616 | */ |
| 617 | __mod_timer(&buf->timer, jiffies + 1); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | old = buf->data; |
| 621 | new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; |
| 622 | new = buf->start + new_subbuf * buf->chan->subbuf_size; |
| 623 | buf->offset = 0; |
| 624 | if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { |
| 625 | buf->offset = buf->chan->subbuf_size + 1; |
| 626 | return 0; |
| 627 | } |
| 628 | buf->data = new; |
| 629 | buf->padding[new_subbuf] = 0; |
| 630 | |
| 631 | if (unlikely(length + buf->offset > buf->chan->subbuf_size)) |
| 632 | goto toobig; |
| 633 | |
| 634 | return length; |
| 635 | |
| 636 | toobig: |
| 637 | buf->chan->last_toobig = length; |
| 638 | return 0; |
| 639 | } |
| 640 | EXPORT_SYMBOL_GPL(relay_switch_subbuf); |
| 641 | |
| 642 | /** |
| 643 | * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count |
| 644 | * @chan: the channel |
| 645 | * @cpu: the cpu associated with the channel buffer to update |
| 646 | * @subbufs_consumed: number of sub-buffers to add to current buf's count |
| 647 | * |
| 648 | * Adds to the channel buffer's consumed sub-buffer count. |
| 649 | * subbufs_consumed should be the number of sub-buffers newly consumed, |
| 650 | * not the total consumed. |
| 651 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 652 | * NOTE. Kernel clients don't need to call this function if the channel |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 653 | * mode is 'overwrite'. |
| 654 | */ |
| 655 | void relay_subbufs_consumed(struct rchan *chan, |
| 656 | unsigned int cpu, |
| 657 | size_t subbufs_consumed) |
| 658 | { |
| 659 | struct rchan_buf *buf; |
| 660 | |
| 661 | if (!chan) |
| 662 | return; |
| 663 | |
| 664 | if (cpu >= NR_CPUS || !chan->buf[cpu]) |
| 665 | return; |
| 666 | |
| 667 | buf = chan->buf[cpu]; |
| 668 | buf->subbufs_consumed += subbufs_consumed; |
| 669 | if (buf->subbufs_consumed > buf->subbufs_produced) |
| 670 | buf->subbufs_consumed = buf->subbufs_produced; |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(relay_subbufs_consumed); |
| 673 | |
| 674 | /** |
| 675 | * relay_close - close the channel |
| 676 | * @chan: the channel |
| 677 | * |
| 678 | * Closes all channel buffers and frees the channel. |
| 679 | */ |
| 680 | void relay_close(struct rchan *chan) |
| 681 | { |
| 682 | unsigned int i; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 683 | |
| 684 | if (!chan) |
| 685 | return; |
| 686 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 687 | mutex_lock(&relay_channels_mutex); |
| 688 | if (chan->is_global && chan->buf[0]) |
| 689 | relay_close_buf(chan->buf[0]); |
| 690 | else |
| 691 | for_each_possible_cpu(i) |
| 692 | if (chan->buf[i]) |
| 693 | relay_close_buf(chan->buf[i]); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 694 | |
| 695 | if (chan->last_toobig) |
| 696 | printk(KERN_WARNING "relay: one or more items not logged " |
| 697 | "[item size (%Zd) > sub-buffer size (%Zd)]\n", |
| 698 | chan->last_toobig, chan->subbuf_size); |
| 699 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 700 | list_del(&chan->list); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 701 | kref_put(&chan->kref, relay_destroy_channel); |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 702 | mutex_unlock(&relay_channels_mutex); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 703 | } |
| 704 | EXPORT_SYMBOL_GPL(relay_close); |
| 705 | |
| 706 | /** |
| 707 | * relay_flush - close the channel |
| 708 | * @chan: the channel |
| 709 | * |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 710 | * Flushes all channel buffers, i.e. forces buffer switch. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 711 | */ |
| 712 | void relay_flush(struct rchan *chan) |
| 713 | { |
| 714 | unsigned int i; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 715 | |
| 716 | if (!chan) |
| 717 | return; |
| 718 | |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 719 | if (chan->is_global && chan->buf[0]) { |
| 720 | relay_switch_subbuf(chan->buf[0], 0); |
| 721 | return; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 722 | } |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 723 | |
| 724 | mutex_lock(&relay_channels_mutex); |
| 725 | for_each_possible_cpu(i) |
| 726 | if (chan->buf[i]) |
| 727 | relay_switch_subbuf(chan->buf[i], 0); |
| 728 | mutex_unlock(&relay_channels_mutex); |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 729 | } |
| 730 | EXPORT_SYMBOL_GPL(relay_flush); |
| 731 | |
| 732 | /** |
| 733 | * relay_file_open - open file op for relay files |
| 734 | * @inode: the inode |
| 735 | * @filp: the file |
| 736 | * |
| 737 | * Increments the channel buffer refcount. |
| 738 | */ |
| 739 | static int relay_file_open(struct inode *inode, struct file *filp) |
| 740 | { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 741 | struct rchan_buf *buf = inode->i_private; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 742 | kref_get(&buf->kref); |
| 743 | filp->private_data = buf; |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * relay_file_mmap - mmap file op for relay files |
| 750 | * @filp: the file |
| 751 | * @vma: the vma describing what to map |
| 752 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 753 | * Calls upon relay_mmap_buf() to map the file into user space. |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 754 | */ |
| 755 | static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 756 | { |
| 757 | struct rchan_buf *buf = filp->private_data; |
| 758 | return relay_mmap_buf(buf, vma); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * relay_file_poll - poll file op for relay files |
| 763 | * @filp: the file |
| 764 | * @wait: poll table |
| 765 | * |
| 766 | * Poll implemention. |
| 767 | */ |
| 768 | static unsigned int relay_file_poll(struct file *filp, poll_table *wait) |
| 769 | { |
| 770 | unsigned int mask = 0; |
| 771 | struct rchan_buf *buf = filp->private_data; |
| 772 | |
| 773 | if (buf->finalized) |
| 774 | return POLLERR; |
| 775 | |
| 776 | if (filp->f_mode & FMODE_READ) { |
| 777 | poll_wait(filp, &buf->read_wait, wait); |
| 778 | if (!relay_buf_empty(buf)) |
| 779 | mask |= POLLIN | POLLRDNORM; |
| 780 | } |
| 781 | |
| 782 | return mask; |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * relay_file_release - release file op for relay files |
| 787 | * @inode: the inode |
| 788 | * @filp: the file |
| 789 | * |
| 790 | * Decrements the channel refcount, as the filesystem is |
| 791 | * no longer using it. |
| 792 | */ |
| 793 | static int relay_file_release(struct inode *inode, struct file *filp) |
| 794 | { |
| 795 | struct rchan_buf *buf = filp->private_data; |
| 796 | kref_put(&buf->kref, relay_remove_buf); |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 801 | /* |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 802 | * relay_file_read_consume - update the consumed count for the buffer |
| 803 | */ |
| 804 | static void relay_file_read_consume(struct rchan_buf *buf, |
| 805 | size_t read_pos, |
| 806 | size_t bytes_consumed) |
| 807 | { |
| 808 | size_t subbuf_size = buf->chan->subbuf_size; |
| 809 | size_t n_subbufs = buf->chan->n_subbufs; |
| 810 | size_t read_subbuf; |
| 811 | |
| 812 | if (buf->bytes_consumed + bytes_consumed > subbuf_size) { |
| 813 | relay_subbufs_consumed(buf->chan, buf->cpu, 1); |
| 814 | buf->bytes_consumed = 0; |
| 815 | } |
| 816 | |
| 817 | buf->bytes_consumed += bytes_consumed; |
Masami Hiramatsu | a66e356 | 2007-06-27 14:10:04 -0700 | [diff] [blame] | 818 | if (!read_pos) |
| 819 | read_subbuf = buf->subbufs_consumed % n_subbufs; |
| 820 | else |
| 821 | read_subbuf = read_pos / buf->chan->subbuf_size; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 822 | if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { |
| 823 | if ((read_subbuf == buf->subbufs_produced % n_subbufs) && |
| 824 | (buf->offset == subbuf_size)) |
| 825 | return; |
| 826 | relay_subbufs_consumed(buf->chan, buf->cpu, 1); |
| 827 | buf->bytes_consumed = 0; |
| 828 | } |
| 829 | } |
| 830 | |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 831 | /* |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 832 | * relay_file_read_avail - boolean, are there unconsumed bytes available? |
| 833 | */ |
| 834 | static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) |
| 835 | { |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 836 | size_t subbuf_size = buf->chan->subbuf_size; |
| 837 | size_t n_subbufs = buf->chan->n_subbufs; |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 838 | size_t produced = buf->subbufs_produced; |
| 839 | size_t consumed = buf->subbufs_consumed; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 840 | |
| 841 | relay_file_read_consume(buf, read_pos, 0); |
| 842 | |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 843 | if (unlikely(buf->offset > subbuf_size)) { |
| 844 | if (produced == consumed) |
| 845 | return 0; |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | if (unlikely(produced - consumed >= n_subbufs)) { |
Masami Hiramatsu | a66e356 | 2007-06-27 14:10:04 -0700 | [diff] [blame] | 850 | consumed = produced - n_subbufs + 1; |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 851 | buf->subbufs_consumed = consumed; |
Masami Hiramatsu | a66e356 | 2007-06-27 14:10:04 -0700 | [diff] [blame] | 852 | buf->bytes_consumed = 0; |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 853 | } |
Daniel Walker | 1bfbc60 | 2007-10-18 03:06:05 -0700 | [diff] [blame] | 854 | |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 855 | produced = (produced % n_subbufs) * subbuf_size + buf->offset; |
| 856 | consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; |
| 857 | |
| 858 | if (consumed > produced) |
| 859 | produced += n_subbufs * subbuf_size; |
Daniel Walker | 1bfbc60 | 2007-10-18 03:06:05 -0700 | [diff] [blame] | 860 | |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 861 | if (consumed == produced) |
| 862 | return 0; |
| 863 | |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 864 | return 1; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * relay_file_read_subbuf_avail - return bytes available in sub-buffer |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 869 | * @read_pos: file read position |
| 870 | * @buf: relay channel buffer |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 871 | */ |
| 872 | static size_t relay_file_read_subbuf_avail(size_t read_pos, |
| 873 | struct rchan_buf *buf) |
| 874 | { |
| 875 | size_t padding, avail = 0; |
| 876 | size_t read_subbuf, read_offset, write_subbuf, write_offset; |
| 877 | size_t subbuf_size = buf->chan->subbuf_size; |
| 878 | |
| 879 | write_subbuf = (buf->data - buf->start) / subbuf_size; |
| 880 | write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; |
| 881 | read_subbuf = read_pos / subbuf_size; |
| 882 | read_offset = read_pos % subbuf_size; |
| 883 | padding = buf->padding[read_subbuf]; |
| 884 | |
| 885 | if (read_subbuf == write_subbuf) { |
| 886 | if (read_offset + padding < write_offset) |
| 887 | avail = write_offset - (read_offset + padding); |
| 888 | } else |
| 889 | avail = (subbuf_size - padding) - read_offset; |
| 890 | |
| 891 | return avail; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * relay_file_read_start_pos - find the first available byte to read |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 896 | * @read_pos: file read position |
| 897 | * @buf: relay channel buffer |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 898 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 899 | * If the @read_pos is in the middle of padding, return the |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 900 | * position of the first actually available byte, otherwise |
| 901 | * return the original value. |
| 902 | */ |
| 903 | static size_t relay_file_read_start_pos(size_t read_pos, |
| 904 | struct rchan_buf *buf) |
| 905 | { |
| 906 | size_t read_subbuf, padding, padding_start, padding_end; |
| 907 | size_t subbuf_size = buf->chan->subbuf_size; |
| 908 | size_t n_subbufs = buf->chan->n_subbufs; |
David Wilder | 8d62fde | 2007-06-27 14:10:03 -0700 | [diff] [blame] | 909 | size_t consumed = buf->subbufs_consumed % n_subbufs; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 910 | |
David Wilder | 8d62fde | 2007-06-27 14:10:03 -0700 | [diff] [blame] | 911 | if (!read_pos) |
| 912 | read_pos = consumed * subbuf_size + buf->bytes_consumed; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 913 | read_subbuf = read_pos / subbuf_size; |
| 914 | padding = buf->padding[read_subbuf]; |
| 915 | padding_start = (read_subbuf + 1) * subbuf_size - padding; |
| 916 | padding_end = (read_subbuf + 1) * subbuf_size; |
| 917 | if (read_pos >= padding_start && read_pos < padding_end) { |
| 918 | read_subbuf = (read_subbuf + 1) % n_subbufs; |
| 919 | read_pos = read_subbuf * subbuf_size; |
| 920 | } |
| 921 | |
| 922 | return read_pos; |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * relay_file_read_end_pos - return the new read position |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 927 | * @read_pos: file read position |
| 928 | * @buf: relay channel buffer |
| 929 | * @count: number of bytes to be read |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 930 | */ |
| 931 | static size_t relay_file_read_end_pos(struct rchan_buf *buf, |
| 932 | size_t read_pos, |
| 933 | size_t count) |
| 934 | { |
| 935 | size_t read_subbuf, padding, end_pos; |
| 936 | size_t subbuf_size = buf->chan->subbuf_size; |
| 937 | size_t n_subbufs = buf->chan->n_subbufs; |
| 938 | |
| 939 | read_subbuf = read_pos / subbuf_size; |
| 940 | padding = buf->padding[read_subbuf]; |
| 941 | if (read_pos % subbuf_size + count + padding == subbuf_size) |
| 942 | end_pos = (read_subbuf + 1) * subbuf_size; |
| 943 | else |
| 944 | end_pos = read_pos + count; |
| 945 | if (end_pos >= subbuf_size * n_subbufs) |
| 946 | end_pos = 0; |
| 947 | |
| 948 | return end_pos; |
| 949 | } |
| 950 | |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 951 | /* |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 952 | * subbuf_read_actor - read up to one subbuf's worth of data |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 953 | */ |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 954 | static int subbuf_read_actor(size_t read_start, |
| 955 | struct rchan_buf *buf, |
| 956 | size_t avail, |
| 957 | read_descriptor_t *desc, |
| 958 | read_actor_t actor) |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 959 | { |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 960 | void *from; |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 961 | int ret = 0; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 962 | |
| 963 | from = buf->start + read_start; |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 964 | ret = avail; |
Al Viro | ba2397e | 2006-10-10 22:46:57 +0100 | [diff] [blame] | 965 | if (copy_to_user(desc->arg.buf, from, avail)) { |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 966 | desc->error = -EFAULT; |
| 967 | ret = 0; |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 968 | } |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 969 | desc->arg.data += ret; |
| 970 | desc->written += ret; |
| 971 | desc->count -= ret; |
| 972 | |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 973 | return ret; |
| 974 | } |
| 975 | |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 976 | typedef int (*subbuf_actor_t) (size_t read_start, |
| 977 | struct rchan_buf *buf, |
| 978 | size_t avail, |
| 979 | read_descriptor_t *desc, |
| 980 | read_actor_t actor); |
| 981 | |
Randy Dunlap | 4c78a66 | 2006-09-29 01:59:10 -0700 | [diff] [blame] | 982 | /* |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 983 | * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries |
| 984 | */ |
Andrew Morton | 192636a | 2006-12-22 01:11:30 -0800 | [diff] [blame] | 985 | static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos, |
| 986 | subbuf_actor_t subbuf_actor, |
| 987 | read_actor_t actor, |
| 988 | read_descriptor_t *desc) |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 989 | { |
| 990 | struct rchan_buf *buf = filp->private_data; |
| 991 | size_t read_start, avail; |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 992 | int ret; |
| 993 | |
Al Viro | ba2397e | 2006-10-10 22:46:57 +0100 | [diff] [blame] | 994 | if (!desc->count) |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 995 | return 0; |
| 996 | |
Josef "Jeff" Sipek | f3a43f3 | 2006-12-08 02:36:43 -0800 | [diff] [blame] | 997 | mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 998 | do { |
| 999 | if (!relay_file_read_avail(buf, *ppos)) |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 1000 | break; |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 1001 | |
| 1002 | read_start = relay_file_read_start_pos(*ppos, buf); |
| 1003 | avail = relay_file_read_subbuf_avail(read_start, buf); |
| 1004 | if (!avail) |
| 1005 | break; |
| 1006 | |
Al Viro | ba2397e | 2006-10-10 22:46:57 +0100 | [diff] [blame] | 1007 | avail = min(desc->count, avail); |
| 1008 | ret = subbuf_actor(read_start, buf, avail, desc, actor); |
| 1009 | if (desc->error < 0) |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 1010 | break; |
| 1011 | |
| 1012 | if (ret) { |
| 1013 | relay_file_read_consume(buf, read_start, ret); |
| 1014 | *ppos = relay_file_read_end_pos(buf, read_start, ret); |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 1015 | } |
Al Viro | ba2397e | 2006-10-10 22:46:57 +0100 | [diff] [blame] | 1016 | } while (desc->count && ret); |
Josef "Jeff" Sipek | f3a43f3 | 2006-12-08 02:36:43 -0800 | [diff] [blame] | 1017 | mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 1018 | |
Al Viro | ba2397e | 2006-10-10 22:46:57 +0100 | [diff] [blame] | 1019 | return desc->written; |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 1022 | static ssize_t relay_file_read(struct file *filp, |
| 1023 | char __user *buffer, |
| 1024 | size_t count, |
| 1025 | loff_t *ppos) |
| 1026 | { |
Al Viro | ba2397e | 2006-10-10 22:46:57 +0100 | [diff] [blame] | 1027 | read_descriptor_t desc; |
| 1028 | desc.written = 0; |
| 1029 | desc.count = count; |
| 1030 | desc.arg.buf = buffer; |
| 1031 | desc.error = 0; |
| 1032 | return relay_file_read_subbufs(filp, ppos, subbuf_read_actor, |
| 1033 | NULL, &desc); |
Tom Zanussi | 6dac40a | 2006-03-23 19:58:45 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1036 | static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed) |
| 1037 | { |
| 1038 | rbuf->bytes_consumed += bytes_consumed; |
| 1039 | |
| 1040 | if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) { |
| 1041 | relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1); |
| 1042 | rbuf->bytes_consumed %= rbuf->chan->subbuf_size; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1046 | static void relay_pipe_buf_release(struct pipe_inode_info *pipe, |
| 1047 | struct pipe_buffer *buf) |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 1048 | { |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1049 | struct rchan_buf *rbuf; |
| 1050 | |
| 1051 | rbuf = (struct rchan_buf *)page_private(buf->page); |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1052 | relay_consume_bytes(rbuf, buf->private); |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static struct pipe_buf_operations relay_pipe_buf_ops = { |
| 1056 | .can_merge = 0, |
| 1057 | .map = generic_pipe_buf_map, |
| 1058 | .unmap = generic_pipe_buf_unmap, |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 1059 | .confirm = generic_pipe_buf_confirm, |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1060 | .release = relay_pipe_buf_release, |
| 1061 | .steal = generic_pipe_buf_steal, |
| 1062 | .get = generic_pipe_buf_get, |
| 1063 | }; |
| 1064 | |
Tom Zanussi | d3f35d98 | 2007-07-12 08:12:05 +0200 | [diff] [blame] | 1065 | /* |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1066 | * subbuf_splice_actor - splice up to one subbuf's worth of data |
| 1067 | */ |
| 1068 | static int subbuf_splice_actor(struct file *in, |
| 1069 | loff_t *ppos, |
| 1070 | struct pipe_inode_info *pipe, |
| 1071 | size_t len, |
| 1072 | unsigned int flags, |
| 1073 | int *nonpad_ret) |
| 1074 | { |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1075 | unsigned int pidx, poff, total_len, subbuf_pages, ret; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1076 | struct rchan_buf *rbuf = in->private_data; |
| 1077 | unsigned int subbuf_size = rbuf->chan->subbuf_size; |
Tom Zanussi | 24da24d | 2007-07-12 08:12:04 +0200 | [diff] [blame] | 1078 | uint64_t pos = (uint64_t) *ppos; |
| 1079 | uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size; |
| 1080 | size_t read_start = (size_t) do_div(pos, alloc_size); |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1081 | size_t read_subbuf = read_start / subbuf_size; |
| 1082 | size_t padding = rbuf->padding[read_subbuf]; |
| 1083 | size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding; |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1084 | struct page *pages[PIPE_BUFFERS]; |
| 1085 | struct partial_page partial[PIPE_BUFFERS]; |
| 1086 | struct splice_pipe_desc spd = { |
| 1087 | .pages = pages, |
| 1088 | .nr_pages = 0, |
| 1089 | .partial = partial, |
| 1090 | .flags = flags, |
| 1091 | .ops = &relay_pipe_buf_ops, |
| 1092 | }; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1093 | |
| 1094 | if (rbuf->subbufs_produced == rbuf->subbufs_consumed) |
| 1095 | return 0; |
| 1096 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1097 | /* |
| 1098 | * Adjust read len, if longer than what is available |
| 1099 | */ |
| 1100 | if (len > (subbuf_size - read_start % subbuf_size)) |
| 1101 | len = subbuf_size - read_start % subbuf_size; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1102 | |
| 1103 | subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; |
| 1104 | pidx = (read_start / PAGE_SIZE) % subbuf_pages; |
| 1105 | poff = read_start & ~PAGE_MASK; |
| 1106 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1107 | for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) { |
| 1108 | unsigned int this_len, this_end, private; |
| 1109 | unsigned int cur_pos = read_start + total_len; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1110 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1111 | if (!len) |
| 1112 | break; |
| 1113 | |
| 1114 | this_len = min_t(unsigned long, len, PAGE_SIZE - poff); |
| 1115 | private = this_len; |
| 1116 | |
| 1117 | spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; |
| 1118 | spd.partial[spd.nr_pages].offset = poff; |
| 1119 | |
| 1120 | this_end = cur_pos + this_len; |
| 1121 | if (this_end >= nonpad_end) { |
| 1122 | this_len = nonpad_end - cur_pos; |
| 1123 | private = this_len + padding; |
| 1124 | } |
| 1125 | spd.partial[spd.nr_pages].len = this_len; |
| 1126 | spd.partial[spd.nr_pages].private = private; |
| 1127 | |
| 1128 | len -= this_len; |
| 1129 | total_len += this_len; |
| 1130 | poff = 0; |
| 1131 | pidx = (pidx + 1) % subbuf_pages; |
| 1132 | |
| 1133 | if (this_end >= nonpad_end) { |
| 1134 | spd.nr_pages++; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1135 | break; |
| 1136 | } |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1139 | if (!spd.nr_pages) |
| 1140 | return 0; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1141 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1142 | ret = *nonpad_ret = splice_to_pipe(pipe, &spd); |
| 1143 | if (ret < 0 || ret < total_len) |
| 1144 | return ret; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1145 | |
Jens Axboe | 1db60cf | 2007-06-12 08:43:46 +0200 | [diff] [blame] | 1146 | if (read_start + ret == nonpad_end) |
| 1147 | ret += padding; |
| 1148 | |
| 1149 | return ret; |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static ssize_t relay_file_splice_read(struct file *in, |
| 1153 | loff_t *ppos, |
| 1154 | struct pipe_inode_info *pipe, |
| 1155 | size_t len, |
| 1156 | unsigned int flags) |
| 1157 | { |
| 1158 | ssize_t spliced; |
| 1159 | int ret; |
| 1160 | int nonpad_ret = 0; |
| 1161 | |
| 1162 | ret = 0; |
| 1163 | spliced = 0; |
| 1164 | |
| 1165 | while (len) { |
| 1166 | ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret); |
| 1167 | if (ret < 0) |
| 1168 | break; |
| 1169 | else if (!ret) { |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1170 | if (spliced) |
| 1171 | break; |
| 1172 | if (flags & SPLICE_F_NONBLOCK) { |
| 1173 | ret = -EAGAIN; |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | *ppos += ret; |
| 1179 | if (ret > len) |
| 1180 | len = 0; |
| 1181 | else |
| 1182 | len -= ret; |
| 1183 | spliced += nonpad_ret; |
| 1184 | nonpad_ret = 0; |
| 1185 | } |
| 1186 | |
| 1187 | if (spliced) |
| 1188 | return spliced; |
| 1189 | |
| 1190 | return ret; |
Jens Axboe | 221415d | 2006-03-23 19:57:55 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 1193 | const struct file_operations relay_file_operations = { |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 1194 | .open = relay_file_open, |
| 1195 | .poll = relay_file_poll, |
| 1196 | .mmap = relay_file_mmap, |
| 1197 | .read = relay_file_read, |
| 1198 | .llseek = no_llseek, |
| 1199 | .release = relay_file_release, |
Tom Zanussi | ebf9909 | 2007-06-04 09:12:05 +0200 | [diff] [blame] | 1200 | .splice_read = relay_file_splice_read, |
Jens Axboe | b86ff981 | 2006-03-23 19:56:55 +0100 | [diff] [blame] | 1201 | }; |
| 1202 | EXPORT_SYMBOL_GPL(relay_file_operations); |
Mathieu Desnoyers | 23c8875 | 2007-02-10 01:45:05 -0800 | [diff] [blame] | 1203 | |
| 1204 | static __init int relay_init(void) |
| 1205 | { |
| 1206 | |
| 1207 | hotcpu_notifier(relay_hotcpu_callback, 0); |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | module_init(relay_init); |