blob: b9bb569032729ec541124855a169d96589214eff [file] [log] [blame]
Tom Zanussie82894f2005-09-06 15:16:30 -07001/*
2 * Public API and common code for RelayFS.
3 *
4 * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
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 * This file is released under the GPL.
10 */
11
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>
18#include "relay.h"
19#include "buffers.h"
20
21/**
22 * relay_buf_empty - boolean, is the channel buffer empty?
23 * @buf: channel buffer
24 *
25 * Returns 1 if the buffer is empty, 0 otherwise.
26 */
27int relay_buf_empty(struct rchan_buf *buf)
28{
29 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
30}
31
32/**
33 * relay_buf_full - boolean, is the channel buffer full?
34 * @buf: channel buffer
35 *
36 * Returns 1 if the buffer is full, 0 otherwise.
37 */
38int relay_buf_full(struct rchan_buf *buf)
39{
40 size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
41 return (ready >= buf->chan->n_subbufs) ? 1 : 0;
42}
43
44/*
45 * High-level relayfs kernel API and associated functions.
46 */
47
48/*
49 * rchan_callback implementations defining default channel behavior. Used
50 * in place of corresponding NULL values in client callback struct.
51 */
52
53/*
54 * subbuf_start() default callback. Does nothing.
55 */
56static int subbuf_start_default_callback (struct rchan_buf *buf,
57 void *subbuf,
58 void *prev_subbuf,
59 size_t prev_padding)
60{
61 if (relay_buf_full(buf))
62 return 0;
63
64 return 1;
65}
66
67/*
68 * buf_mapped() default callback. Does nothing.
69 */
70static void buf_mapped_default_callback(struct rchan_buf *buf,
71 struct file *filp)
72{
73}
74
75/*
76 * buf_unmapped() default callback. Does nothing.
77 */
78static void buf_unmapped_default_callback(struct rchan_buf *buf,
79 struct file *filp)
80{
81}
82
Tom Zanussi08c541a2006-01-08 01:02:28 -080083/*
84 * create_buf_file_create() default callback. Creates file to represent buf.
85 */
86static struct dentry *create_buf_file_default_callback(const char *filename,
87 struct dentry *parent,
88 int mode,
89 struct rchan_buf *buf)
90{
91 return relayfs_create_file(filename, parent, mode,
92 &relayfs_file_operations, buf);
93}
94
95/*
96 * remove_buf_file() default callback. Removes file representing relay buffer.
97 */
98static int remove_buf_file_default_callback(struct dentry *dentry)
99{
100 return relayfs_remove(dentry);
101}
102
Tom Zanussie82894f2005-09-06 15:16:30 -0700103/* relay channel default callbacks */
104static 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,
Tom Zanussi08c541a2006-01-08 01:02:28 -0800108 .create_buf_file = create_buf_file_default_callback,
109 .remove_buf_file = remove_buf_file_default_callback,
Tom Zanussie82894f2005-09-06 15:16:30 -0700110};
111
112/**
113 * wakeup_readers - wake up readers waiting on a channel
114 * @private: the channel buffer
115 *
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.
119 */
120static void wakeup_readers(void *private)
121{
122 struct rchan_buf *buf = private;
123 wake_up_interruptible(&buf->read_wait);
124}
125
126/**
127 * __relay_reset - reset a channel buffer
128 * @buf: the channel buffer
129 * @init: 1 if this is a first-time initialization
130 *
131 * See relay_reset for description of effect.
132 */
133static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
134{
135 size_t i;
136
137 if (init) {
138 init_waitqueue_head(&buf->read_wait);
139 kref_init(&buf->kref);
140 INIT_WORK(&buf->wake_readers, NULL, NULL);
141 } else {
142 cancel_delayed_work(&buf->wake_readers);
143 flush_scheduled_work();
144 }
145
146 buf->subbufs_produced = 0;
147 buf->subbufs_consumed = 0;
148 buf->bytes_consumed = 0;
149 buf->finalized = 0;
150 buf->data = buf->start;
151 buf->offset = 0;
152
153 for (i = 0; i < buf->chan->n_subbufs; i++)
154 buf->padding[i] = 0;
155
156 buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
157}
158
159/**
160 * relay_reset - reset the channel
161 * @chan: the channel
162 *
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.
166 *
167 * NOTE: Care should be taken that the channel isn't actually
168 * being used by anything when this call is made.
169 */
170void relay_reset(struct rchan *chan)
171{
172 unsigned int i;
173
174 if (!chan)
175 return;
176
177 for (i = 0; i < NR_CPUS; i++) {
178 if (!chan->buf[i])
179 continue;
180 __relay_reset(chan->buf[i], 0);
181 }
182}
183
184/**
185 * relay_open_buf - create a new channel buffer in relayfs
186 *
187 * Internal - used by relay_open().
188 */
189static struct rchan_buf *relay_open_buf(struct rchan *chan,
190 const char *filename,
191 struct dentry *parent)
192{
193 struct rchan_buf *buf;
194 struct dentry *dentry;
195
Tom Zanussi6625b862006-01-08 01:02:23 -0800196 buf = relay_create_buf(chan);
197 if (!buf)
198 return NULL;
Tom Zanussie82894f2005-09-06 15:16:30 -0700199
Tom Zanussi6625b862006-01-08 01:02:23 -0800200 /* Create file in fs */
Tom Zanussi08c541a2006-01-08 01:02:28 -0800201 dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
202 buf);
Tom Zanussi6625b862006-01-08 01:02:23 -0800203 if (!dentry) {
204 relay_destroy_buf(buf);
205 return NULL;
206 }
207
Tom Zanussie82894f2005-09-06 15:16:30 -0700208 buf->dentry = dentry;
209 __relay_reset(buf, 1);
210
211 return buf;
212}
213
214/**
215 * relay_close_buf - close a channel buffer
216 * @buf: channel buffer
217 *
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.
221 */
222static inline void relay_close_buf(struct rchan_buf *buf)
223{
224 buf->finalized = 1;
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);
229}
230
231static inline void setup_callbacks(struct rchan *chan,
232 struct rchan_callbacks *cb)
233{
234 if (!cb) {
235 chan->cb = &default_channel_callbacks;
236 return;
237 }
238
239 if (!cb->subbuf_start)
240 cb->subbuf_start = subbuf_start_default_callback;
241 if (!cb->buf_mapped)
242 cb->buf_mapped = buf_mapped_default_callback;
243 if (!cb->buf_unmapped)
244 cb->buf_unmapped = buf_unmapped_default_callback;
Tom Zanussi08c541a2006-01-08 01:02:28 -0800245 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;
Tom Zanussie82894f2005-09-06 15:16:30 -0700249 chan->cb = cb;
250}
251
252/**
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
259 *
260 * Returns channel pointer if successful, NULL otherwise.
261 *
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.
266 */
267struct rchan *relay_open(const char *base_filename,
268 struct dentry *parent,
269 size_t subbuf_size,
270 size_t n_subbufs,
271 struct rchan_callbacks *cb)
272{
273 unsigned int i;
274 struct rchan *chan;
275 char *tmpname;
276
277 if (!base_filename)
278 return NULL;
279
280 if (!(subbuf_size && n_subbufs))
281 return NULL;
282
283 chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
284 if (!chan)
285 return NULL;
286
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);
293
294 tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
295 if (!tmpname)
296 goto free_chan;
297
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;
302 if (!chan->buf[i])
303 goto free_bufs;
304 }
305
306 kfree(tmpname);
307 return chan;
308
309free_bufs:
310 for (i = 0; i < NR_CPUS; i++) {
311 if (!chan->buf[i])
312 break;
313 relay_close_buf(chan->buf[i]);
314 }
315 kfree(tmpname);
316
317free_chan:
318 kref_put(&chan->kref, relay_destroy_channel);
319 return NULL;
320}
321
322/**
323 * relay_switch_subbuf - switch to a new sub-buffer
324 * @buf: channel buffer
325 * @length: size of current event
326 *
327 * Returns either the length passed in or 0 if full.
328
329 * Performs sub-buffer-switch tasks such as invoking callbacks,
330 * updating padding counts, waking up readers, etc.
331 */
332size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
333{
334 void *old, *new;
335 size_t old_subbuf, new_subbuf;
336
337 if (unlikely(length > buf->chan->subbuf_size))
338 goto toobig;
339
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);
348 }
349 }
350
351 old = buf->data;
352 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
353 new = buf->start + new_subbuf * buf->chan->subbuf_size;
354 buf->offset = 0;
355 if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
356 buf->offset = buf->chan->subbuf_size + 1;
357 return 0;
358 }
359 buf->data = new;
360 buf->padding[new_subbuf] = 0;
361
362 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
363 goto toobig;
364
365 return length;
366
367toobig:
Tom Zanussifd30fc32005-12-20 13:10:22 -0600368 buf->chan->last_toobig = length;
Tom Zanussie82894f2005-09-06 15:16:30 -0700369 return 0;
370}
371
372/**
373 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
374 * @chan: the channel
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
377 *
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.
381 *
382 * NOTE: kernel clients don't need to call this function if the channel
383 * mode is 'overwrite'.
384 */
385void relay_subbufs_consumed(struct rchan *chan,
386 unsigned int cpu,
387 size_t subbufs_consumed)
388{
389 struct rchan_buf *buf;
390
391 if (!chan)
392 return;
393
394 if (cpu >= NR_CPUS || !chan->buf[cpu])
395 return;
396
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;
401}
402
403/**
404 * relay_destroy_channel - free the channel struct
405 *
406 * Should only be called from kref_put().
407 */
408void relay_destroy_channel(struct kref *kref)
409{
410 struct rchan *chan = container_of(kref, struct rchan, kref);
411 kfree(chan);
412}
413
414/**
415 * relay_close - close the channel
416 * @chan: the channel
417 *
418 * Closes all channel buffers and frees the channel.
419 */
420void relay_close(struct rchan *chan)
421{
422 unsigned int i;
423
424 if (!chan)
425 return;
426
427 for (i = 0; i < NR_CPUS; i++) {
428 if (!chan->buf[i])
429 continue;
430 relay_close_buf(chan->buf[i]);
431 }
432
Tom Zanussifd30fc32005-12-20 13:10:22 -0600433 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);
437
Tom Zanussie82894f2005-09-06 15:16:30 -0700438 kref_put(&chan->kref, relay_destroy_channel);
439}
440
441/**
442 * relay_flush - close the channel
443 * @chan: the channel
444 *
445 * Flushes all channel buffers i.e. forces buffer switch.
446 */
447void relay_flush(struct rchan *chan)
448{
449 unsigned int i;
450
451 if (!chan)
452 return;
453
454 for (i = 0; i < NR_CPUS; i++) {
455 if (!chan->buf[i])
456 continue;
457 relay_switch_subbuf(chan->buf[i], 0);
458 }
459}
460
461EXPORT_SYMBOL_GPL(relay_open);
462EXPORT_SYMBOL_GPL(relay_close);
463EXPORT_SYMBOL_GPL(relay_flush);
464EXPORT_SYMBOL_GPL(relay_reset);
465EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
466EXPORT_SYMBOL_GPL(relay_switch_subbuf);
467EXPORT_SYMBOL_GPL(relay_buf_full);