2 * Public API and common code for RelayFS.
4 * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
6 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
9 * This file is released under the GPL.
12 #include <linux/errno.h>
13 #include <linux/stddef.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/relayfs_fs.h>
22 * relay_buf_empty - boolean, is the channel buffer empty?
23 * @buf: channel buffer
25 * Returns 1 if the buffer is empty, 0 otherwise.
27 int relay_buf_empty(struct rchan_buf *buf)
29 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
33 * relay_buf_full - boolean, is the channel buffer full?
34 * @buf: channel buffer
36 * Returns 1 if the buffer is full, 0 otherwise.
38 int relay_buf_full(struct rchan_buf *buf)
40 size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
41 return (ready >= buf->chan->n_subbufs) ? 1 : 0;
45 * High-level relayfs kernel API and associated functions.
49 * rchan_callback implementations defining default channel behavior. Used
50 * in place of corresponding NULL values in client callback struct.
54 * subbuf_start() default callback. Does nothing.
56 static int subbuf_start_default_callback (struct rchan_buf *buf,
61 if (relay_buf_full(buf))
68 * buf_mapped() default callback. Does nothing.
70 static void buf_mapped_default_callback(struct rchan_buf *buf,
76 * buf_unmapped() default callback. Does nothing.
78 static void buf_unmapped_default_callback(struct rchan_buf *buf,
84 * create_buf_file_create() default callback. Creates file to represent buf.
86 static struct dentry *create_buf_file_default_callback(const char *filename,
87 struct dentry *parent,
89 struct rchan_buf *buf)
91 return relayfs_create_file(filename, parent, mode,
92 &relayfs_file_operations, buf);
96 * remove_buf_file() default callback. Removes file representing relay buffer.
98 static int remove_buf_file_default_callback(struct dentry *dentry)
100 return relayfs_remove(dentry);
103 /* relay channel default callbacks */
104 static struct rchan_callbacks default_channel_callbacks = {
105 .subbuf_start = subbuf_start_default_callback,
106 .buf_mapped = buf_mapped_default_callback,
107 .buf_unmapped = buf_unmapped_default_callback,
108 .create_buf_file = create_buf_file_default_callback,
109 .remove_buf_file = remove_buf_file_default_callback,
113 * wakeup_readers - wake up readers waiting on a channel
114 * @private: the channel buffer
116 * This is the work function used to defer reader waking. The
117 * reason waking is deferred is that calling directly from write
118 * causes problems if you're writing from say the scheduler.
120 static void wakeup_readers(void *private)
122 struct rchan_buf *buf = private;
123 wake_up_interruptible(&buf->read_wait);
127 * __relay_reset - reset a channel buffer
128 * @buf: the channel buffer
129 * @init: 1 if this is a first-time initialization
131 * See relay_reset for description of effect.
133 static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
138 init_waitqueue_head(&buf->read_wait);
139 kref_init(&buf->kref);
140 INIT_WORK(&buf->wake_readers, NULL, NULL);
142 cancel_delayed_work(&buf->wake_readers);
143 flush_scheduled_work();
146 buf->subbufs_produced = 0;
147 buf->subbufs_consumed = 0;
148 buf->bytes_consumed = 0;
150 buf->data = buf->start;
153 for (i = 0; i < buf->chan->n_subbufs; i++)
156 buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
160 * relay_reset - reset the channel
163 * This has the effect of erasing all data from all channel buffers
164 * and restarting the channel in its initial state. The buffers
165 * are not freed, so any mappings are still in effect.
167 * NOTE: Care should be taken that the channel isn't actually
168 * being used by anything when this call is made.
170 void relay_reset(struct rchan *chan)
177 for (i = 0; i < NR_CPUS; i++) {
180 __relay_reset(chan->buf[i], 0);
185 * relay_open_buf - create a new channel buffer in relayfs
187 * Internal - used by relay_open().
189 static struct rchan_buf *relay_open_buf(struct rchan *chan,
190 const char *filename,
191 struct dentry *parent)
193 struct rchan_buf *buf;
194 struct dentry *dentry;
196 buf = relay_create_buf(chan);
200 /* Create file in fs */
201 dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
204 relay_destroy_buf(buf);
208 buf->dentry = dentry;
209 __relay_reset(buf, 1);
215 * relay_close_buf - close a channel buffer
216 * @buf: channel buffer
218 * Marks the buffer finalized and restores the default callbacks.
219 * The channel buffer and channel buffer data structure are then freed
220 * automatically when the last reference is given up.
222 static inline void relay_close_buf(struct rchan_buf *buf)
225 buf->chan->cb = &default_channel_callbacks;
226 cancel_delayed_work(&buf->wake_readers);
227 flush_scheduled_work();
228 kref_put(&buf->kref, relay_remove_buf);
231 static inline void setup_callbacks(struct rchan *chan,
232 struct rchan_callbacks *cb)
235 chan->cb = &default_channel_callbacks;
239 if (!cb->subbuf_start)
240 cb->subbuf_start = subbuf_start_default_callback;
242 cb->buf_mapped = buf_mapped_default_callback;
243 if (!cb->buf_unmapped)
244 cb->buf_unmapped = buf_unmapped_default_callback;
245 if (!cb->create_buf_file)
246 cb->create_buf_file = create_buf_file_default_callback;
247 if (!cb->remove_buf_file)
248 cb->remove_buf_file = remove_buf_file_default_callback;
253 * relay_open - create a new relayfs channel
254 * @base_filename: base name of files to create
255 * @parent: dentry of parent directory, NULL for root directory
256 * @subbuf_size: size of sub-buffers
257 * @n_subbufs: number of sub-buffers
258 * @cb: client callback functions
260 * Returns channel pointer if successful, NULL otherwise.
262 * Creates a channel buffer for each cpu using the sizes and
263 * attributes specified. The created channel buffer files
264 * will be named base_filename0...base_filenameN-1. File
265 * permissions will be S_IRUSR.
267 struct rchan *relay_open(const char *base_filename,
268 struct dentry *parent,
271 struct rchan_callbacks *cb)
280 if (!(subbuf_size && n_subbufs))
283 chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
287 chan->version = RELAYFS_CHANNEL_VERSION;
288 chan->n_subbufs = n_subbufs;
289 chan->subbuf_size = subbuf_size;
290 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
291 setup_callbacks(chan, cb);
292 kref_init(&chan->kref);
294 tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
298 for_each_online_cpu(i) {
299 sprintf(tmpname, "%s%d", base_filename, i);
300 chan->buf[i] = relay_open_buf(chan, tmpname, parent);
301 chan->buf[i]->cpu = i;
310 for (i = 0; i < NR_CPUS; i++) {
313 relay_close_buf(chan->buf[i]);
318 kref_put(&chan->kref, relay_destroy_channel);
323 * relay_switch_subbuf - switch to a new sub-buffer
324 * @buf: channel buffer
325 * @length: size of current event
327 * Returns either the length passed in or 0 if full.
329 * Performs sub-buffer-switch tasks such as invoking callbacks,
330 * updating padding counts, waking up readers, etc.
332 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
335 size_t old_subbuf, new_subbuf;
337 if (unlikely(length > buf->chan->subbuf_size))
340 if (buf->offset != buf->chan->subbuf_size + 1) {
341 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
342 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
343 buf->padding[old_subbuf] = buf->prev_padding;
344 buf->subbufs_produced++;
345 if (waitqueue_active(&buf->read_wait)) {
346 PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
347 schedule_delayed_work(&buf->wake_readers, 1);
352 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
353 new = buf->start + new_subbuf * buf->chan->subbuf_size;
355 if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
356 buf->offset = buf->chan->subbuf_size + 1;
360 buf->padding[new_subbuf] = 0;
362 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
368 buf->chan->last_toobig = length;
373 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
375 * @cpu: the cpu associated with the channel buffer to update
376 * @subbufs_consumed: number of sub-buffers to add to current buf's count
378 * Adds to the channel buffer's consumed sub-buffer count.
379 * subbufs_consumed should be the number of sub-buffers newly consumed,
380 * not the total consumed.
382 * NOTE: kernel clients don't need to call this function if the channel
383 * mode is 'overwrite'.
385 void relay_subbufs_consumed(struct rchan *chan,
387 size_t subbufs_consumed)
389 struct rchan_buf *buf;
394 if (cpu >= NR_CPUS || !chan->buf[cpu])
397 buf = chan->buf[cpu];
398 buf->subbufs_consumed += subbufs_consumed;
399 if (buf->subbufs_consumed > buf->subbufs_produced)
400 buf->subbufs_consumed = buf->subbufs_produced;
404 * relay_destroy_channel - free the channel struct
406 * Should only be called from kref_put().
408 void relay_destroy_channel(struct kref *kref)
410 struct rchan *chan = container_of(kref, struct rchan, kref);
415 * relay_close - close the channel
418 * Closes all channel buffers and frees the channel.
420 void relay_close(struct rchan *chan)
427 for (i = 0; i < NR_CPUS; i++) {
430 relay_close_buf(chan->buf[i]);
433 if (chan->last_toobig)
434 printk(KERN_WARNING "relayfs: one or more items not logged "
435 "[item size (%Zd) > sub-buffer size (%Zd)]\n",
436 chan->last_toobig, chan->subbuf_size);
438 kref_put(&chan->kref, relay_destroy_channel);
442 * relay_flush - close the channel
445 * Flushes all channel buffers i.e. forces buffer switch.
447 void relay_flush(struct rchan *chan)
454 for (i = 0; i < NR_CPUS; i++) {
457 relay_switch_subbuf(chan->buf[i], 0);
461 EXPORT_SYMBOL_GPL(relay_open);
462 EXPORT_SYMBOL_GPL(relay_close);
463 EXPORT_SYMBOL_GPL(relay_flush);
464 EXPORT_SYMBOL_GPL(relay_reset);
465 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
466 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
467 EXPORT_SYMBOL_GPL(relay_buf_full);