ia64/linux-2.6.18-xen.hg

annotate kernel/relay.c @ 912:dd42cdb0ab89

[IA64] Build blktap2 driver by default in x86 builds.

add CONFIG_XEN_BLKDEV_TAP2=y to buildconfigs/linux-defconfig_xen_ia64.

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