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,
83 /* relay channel default callbacks */
84 static struct rchan_callbacks default_channel_callbacks = {
85 .subbuf_start = subbuf_start_default_callback,
86 .buf_mapped = buf_mapped_default_callback,
87 .buf_unmapped = buf_unmapped_default_callback,
91 * wakeup_readers - wake up readers waiting on a channel
92 * @private: the channel buffer
94 * This is the work function used to defer reader waking. The
95 * reason waking is deferred is that calling directly from write
96 * causes problems if you're writing from say the scheduler.
98 static void wakeup_readers(void *private)
100 struct rchan_buf *buf = private;
101 wake_up_interruptible(&buf->read_wait);
105 * __relay_reset - reset a channel buffer
106 * @buf: the channel buffer
107 * @init: 1 if this is a first-time initialization
109 * See relay_reset for description of effect.
111 static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
116 init_waitqueue_head(&buf->read_wait);
117 kref_init(&buf->kref);
118 INIT_WORK(&buf->wake_readers, NULL, NULL);
120 cancel_delayed_work(&buf->wake_readers);
121 flush_scheduled_work();
124 buf->subbufs_produced = 0;
125 buf->subbufs_consumed = 0;
126 buf->bytes_consumed = 0;
128 buf->data = buf->start;
131 for (i = 0; i < buf->chan->n_subbufs; i++)
134 buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
138 * relay_reset - reset the channel
141 * This has the effect of erasing all data from all channel buffers
142 * and restarting the channel in its initial state. The buffers
143 * are not freed, so any mappings are still in effect.
145 * NOTE: Care should be taken that the channel isn't actually
146 * being used by anything when this call is made.
148 void relay_reset(struct rchan *chan)
155 for (i = 0; i < NR_CPUS; i++) {
158 __relay_reset(chan->buf[i], 0);
163 * relay_open_buf - create a new channel buffer in relayfs
165 * Internal - used by relay_open().
167 static struct rchan_buf *relay_open_buf(struct rchan *chan,
168 const char *filename,
169 struct dentry *parent)
171 struct rchan_buf *buf;
172 struct dentry *dentry;
174 buf = relay_create_buf(chan);
178 /* Create file in fs */
179 dentry = relayfs_create_file(filename, parent, S_IRUSR, buf);
181 relay_destroy_buf(buf);
185 buf->dentry = dentry;
186 __relay_reset(buf, 1);
192 * relay_close_buf - close a channel buffer
193 * @buf: channel buffer
195 * Marks the buffer finalized and restores the default callbacks.
196 * The channel buffer and channel buffer data structure are then freed
197 * automatically when the last reference is given up.
199 static inline void relay_close_buf(struct rchan_buf *buf)
202 buf->chan->cb = &default_channel_callbacks;
203 cancel_delayed_work(&buf->wake_readers);
204 flush_scheduled_work();
205 kref_put(&buf->kref, relay_remove_buf);
208 static inline void setup_callbacks(struct rchan *chan,
209 struct rchan_callbacks *cb)
212 chan->cb = &default_channel_callbacks;
216 if (!cb->subbuf_start)
217 cb->subbuf_start = subbuf_start_default_callback;
219 cb->buf_mapped = buf_mapped_default_callback;
220 if (!cb->buf_unmapped)
221 cb->buf_unmapped = buf_unmapped_default_callback;
226 * relay_open - create a new relayfs channel
227 * @base_filename: base name of files to create
228 * @parent: dentry of parent directory, NULL for root directory
229 * @subbuf_size: size of sub-buffers
230 * @n_subbufs: number of sub-buffers
231 * @cb: client callback functions
233 * Returns channel pointer if successful, NULL otherwise.
235 * Creates a channel buffer for each cpu using the sizes and
236 * attributes specified. The created channel buffer files
237 * will be named base_filename0...base_filenameN-1. File
238 * permissions will be S_IRUSR.
240 struct rchan *relay_open(const char *base_filename,
241 struct dentry *parent,
244 struct rchan_callbacks *cb)
253 if (!(subbuf_size && n_subbufs))
256 chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
260 chan->version = RELAYFS_CHANNEL_VERSION;
261 chan->n_subbufs = n_subbufs;
262 chan->subbuf_size = subbuf_size;
263 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
264 setup_callbacks(chan, cb);
265 kref_init(&chan->kref);
267 tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
271 for_each_online_cpu(i) {
272 sprintf(tmpname, "%s%d", base_filename, i);
273 chan->buf[i] = relay_open_buf(chan, tmpname, parent);
274 chan->buf[i]->cpu = i;
283 for (i = 0; i < NR_CPUS; i++) {
286 relay_close_buf(chan->buf[i]);
291 kref_put(&chan->kref, relay_destroy_channel);
296 * relay_switch_subbuf - switch to a new sub-buffer
297 * @buf: channel buffer
298 * @length: size of current event
300 * Returns either the length passed in or 0 if full.
302 * Performs sub-buffer-switch tasks such as invoking callbacks,
303 * updating padding counts, waking up readers, etc.
305 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
308 size_t old_subbuf, new_subbuf;
310 if (unlikely(length > buf->chan->subbuf_size))
313 if (buf->offset != buf->chan->subbuf_size + 1) {
314 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
315 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
316 buf->padding[old_subbuf] = buf->prev_padding;
317 buf->subbufs_produced++;
318 if (waitqueue_active(&buf->read_wait)) {
319 PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
320 schedule_delayed_work(&buf->wake_readers, 1);
325 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
326 new = buf->start + new_subbuf * buf->chan->subbuf_size;
328 if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
329 buf->offset = buf->chan->subbuf_size + 1;
333 buf->padding[new_subbuf] = 0;
335 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
341 buf->chan->last_toobig = length;
346 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
348 * @cpu: the cpu associated with the channel buffer to update
349 * @subbufs_consumed: number of sub-buffers to add to current buf's count
351 * Adds to the channel buffer's consumed sub-buffer count.
352 * subbufs_consumed should be the number of sub-buffers newly consumed,
353 * not the total consumed.
355 * NOTE: kernel clients don't need to call this function if the channel
356 * mode is 'overwrite'.
358 void relay_subbufs_consumed(struct rchan *chan,
360 size_t subbufs_consumed)
362 struct rchan_buf *buf;
367 if (cpu >= NR_CPUS || !chan->buf[cpu])
370 buf = chan->buf[cpu];
371 buf->subbufs_consumed += subbufs_consumed;
372 if (buf->subbufs_consumed > buf->subbufs_produced)
373 buf->subbufs_consumed = buf->subbufs_produced;
377 * relay_destroy_channel - free the channel struct
379 * Should only be called from kref_put().
381 void relay_destroy_channel(struct kref *kref)
383 struct rchan *chan = container_of(kref, struct rchan, kref);
388 * relay_close - close the channel
391 * Closes all channel buffers and frees the channel.
393 void relay_close(struct rchan *chan)
400 for (i = 0; i < NR_CPUS; i++) {
403 relay_close_buf(chan->buf[i]);
406 if (chan->last_toobig)
407 printk(KERN_WARNING "relayfs: one or more items not logged "
408 "[item size (%Zd) > sub-buffer size (%Zd)]\n",
409 chan->last_toobig, chan->subbuf_size);
411 kref_put(&chan->kref, relay_destroy_channel);
415 * relay_flush - close the channel
418 * Flushes all channel buffers i.e. forces buffer switch.
420 void relay_flush(struct rchan *chan)
427 for (i = 0; i < NR_CPUS; i++) {
430 relay_switch_subbuf(chan->buf[i], 0);
434 EXPORT_SYMBOL_GPL(relay_open);
435 EXPORT_SYMBOL_GPL(relay_close);
436 EXPORT_SYMBOL_GPL(relay_flush);
437 EXPORT_SYMBOL_GPL(relay_reset);
438 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
439 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
440 EXPORT_SYMBOL_GPL(relay_buf_full);