]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/relayfs/relay.c
a9cd5585c45ca33133dda7a31b64e5681d4175eb
[linux-2.6-omap-h63xx.git] / fs / relayfs / relay.c
1 /*
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  */
27 int 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  */
38 int 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  */
56 static 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  */
70 static 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  */
78 static void buf_unmapped_default_callback(struct rchan_buf *buf,
79                                           struct file *filp)
80 {
81 }
82
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,
88 };
89
90 /**
91  *      wakeup_readers - wake up readers waiting on a channel
92  *      @private: the channel buffer
93  *
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.
97  */
98 static void wakeup_readers(void *private)
99 {
100         struct rchan_buf *buf = private;
101         wake_up_interruptible(&buf->read_wait);
102 }
103
104 /**
105  *      __relay_reset - reset a channel buffer
106  *      @buf: the channel buffer
107  *      @init: 1 if this is a first-time initialization
108  *
109  *      See relay_reset for description of effect.
110  */
111 static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
112 {
113         size_t i;
114
115         if (init) {
116                 init_waitqueue_head(&buf->read_wait);
117                 kref_init(&buf->kref);
118                 INIT_WORK(&buf->wake_readers, NULL, NULL);
119         } else {
120                 cancel_delayed_work(&buf->wake_readers);
121                 flush_scheduled_work();
122         }
123
124         buf->subbufs_produced = 0;
125         buf->subbufs_consumed = 0;
126         buf->bytes_consumed = 0;
127         buf->finalized = 0;
128         buf->data = buf->start;
129         buf->offset = 0;
130
131         for (i = 0; i < buf->chan->n_subbufs; i++)
132                 buf->padding[i] = 0;
133
134         buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
135 }
136
137 /**
138  *      relay_reset - reset the channel
139  *      @chan: the channel
140  *
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.
144  *
145  *      NOTE: Care should be taken that the channel isn't actually
146  *      being used by anything when this call is made.
147  */
148 void relay_reset(struct rchan *chan)
149 {
150         unsigned int i;
151
152         if (!chan)
153                 return;
154
155         for (i = 0; i < NR_CPUS; i++) {
156                 if (!chan->buf[i])
157                         continue;
158                 __relay_reset(chan->buf[i], 0);
159         }
160 }
161
162 /**
163  *      relay_open_buf - create a new channel buffer in relayfs
164  *
165  *      Internal - used by relay_open().
166  */
167 static struct rchan_buf *relay_open_buf(struct rchan *chan,
168                                         const char *filename,
169                                         struct dentry *parent)
170 {
171         struct rchan_buf *buf;
172         struct dentry *dentry;
173
174         buf = relay_create_buf(chan);
175         if (!buf)
176                 return NULL;
177
178         /* Create file in fs */
179         dentry = relayfs_create_file(filename, parent, S_IRUSR,
180                                      &relayfs_file_operations, buf);
181         if (!dentry) {
182                 relay_destroy_buf(buf);
183                 return NULL;
184         }
185
186         buf->dentry = dentry;
187         __relay_reset(buf, 1);
188
189         return buf;
190 }
191
192 /**
193  *      relay_close_buf - close a channel buffer
194  *      @buf: channel buffer
195  *
196  *      Marks the buffer finalized and restores the default callbacks.
197  *      The channel buffer and channel buffer data structure are then freed
198  *      automatically when the last reference is given up.
199  */
200 static inline void relay_close_buf(struct rchan_buf *buf)
201 {
202         buf->finalized = 1;
203         buf->chan->cb = &default_channel_callbacks;
204         cancel_delayed_work(&buf->wake_readers);
205         flush_scheduled_work();
206         kref_put(&buf->kref, relay_remove_buf);
207 }
208
209 static inline void setup_callbacks(struct rchan *chan,
210                                    struct rchan_callbacks *cb)
211 {
212         if (!cb) {
213                 chan->cb = &default_channel_callbacks;
214                 return;
215         }
216
217         if (!cb->subbuf_start)
218                 cb->subbuf_start = subbuf_start_default_callback;
219         if (!cb->buf_mapped)
220                 cb->buf_mapped = buf_mapped_default_callback;
221         if (!cb->buf_unmapped)
222                 cb->buf_unmapped = buf_unmapped_default_callback;
223         chan->cb = cb;
224 }
225
226 /**
227  *      relay_open - create a new relayfs channel
228  *      @base_filename: base name of files to create
229  *      @parent: dentry of parent directory, NULL for root directory
230  *      @subbuf_size: size of sub-buffers
231  *      @n_subbufs: number of sub-buffers
232  *      @cb: client callback functions
233  *
234  *      Returns channel pointer if successful, NULL otherwise.
235  *
236  *      Creates a channel buffer for each cpu using the sizes and
237  *      attributes specified.  The created channel buffer files
238  *      will be named base_filename0...base_filenameN-1.  File
239  *      permissions will be S_IRUSR.
240  */
241 struct rchan *relay_open(const char *base_filename,
242                          struct dentry *parent,
243                          size_t subbuf_size,
244                          size_t n_subbufs,
245                          struct rchan_callbacks *cb)
246 {
247         unsigned int i;
248         struct rchan *chan;
249         char *tmpname;
250
251         if (!base_filename)
252                 return NULL;
253
254         if (!(subbuf_size && n_subbufs))
255                 return NULL;
256
257         chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
258         if (!chan)
259                 return NULL;
260
261         chan->version = RELAYFS_CHANNEL_VERSION;
262         chan->n_subbufs = n_subbufs;
263         chan->subbuf_size = subbuf_size;
264         chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
265         setup_callbacks(chan, cb);
266         kref_init(&chan->kref);
267
268         tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
269         if (!tmpname)
270                 goto free_chan;
271
272         for_each_online_cpu(i) {
273                 sprintf(tmpname, "%s%d", base_filename, i);
274                 chan->buf[i] = relay_open_buf(chan, tmpname, parent);
275                 chan->buf[i]->cpu = i;
276                 if (!chan->buf[i])
277                         goto free_bufs;
278         }
279
280         kfree(tmpname);
281         return chan;
282
283 free_bufs:
284         for (i = 0; i < NR_CPUS; i++) {
285                 if (!chan->buf[i])
286                         break;
287                 relay_close_buf(chan->buf[i]);
288         }
289         kfree(tmpname);
290
291 free_chan:
292         kref_put(&chan->kref, relay_destroy_channel);
293         return NULL;
294 }
295
296 /**
297  *      relay_switch_subbuf - switch to a new sub-buffer
298  *      @buf: channel buffer
299  *      @length: size of current event
300  *
301  *      Returns either the length passed in or 0 if full.
302
303  *      Performs sub-buffer-switch tasks such as invoking callbacks,
304  *      updating padding counts, waking up readers, etc.
305  */
306 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
307 {
308         void *old, *new;
309         size_t old_subbuf, new_subbuf;
310
311         if (unlikely(length > buf->chan->subbuf_size))
312                 goto toobig;
313
314         if (buf->offset != buf->chan->subbuf_size + 1) {
315                 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
316                 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
317                 buf->padding[old_subbuf] = buf->prev_padding;
318                 buf->subbufs_produced++;
319                 if (waitqueue_active(&buf->read_wait)) {
320                         PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
321                         schedule_delayed_work(&buf->wake_readers, 1);
322                 }
323         }
324
325         old = buf->data;
326         new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
327         new = buf->start + new_subbuf * buf->chan->subbuf_size;
328         buf->offset = 0;
329         if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
330                 buf->offset = buf->chan->subbuf_size + 1;
331                 return 0;
332         }
333         buf->data = new;
334         buf->padding[new_subbuf] = 0;
335
336         if (unlikely(length + buf->offset > buf->chan->subbuf_size))
337                 goto toobig;
338
339         return length;
340
341 toobig:
342         buf->chan->last_toobig = length;
343         return 0;
344 }
345
346 /**
347  *      relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
348  *      @chan: the channel
349  *      @cpu: the cpu associated with the channel buffer to update
350  *      @subbufs_consumed: number of sub-buffers to add to current buf's count
351  *
352  *      Adds to the channel buffer's consumed sub-buffer count.
353  *      subbufs_consumed should be the number of sub-buffers newly consumed,
354  *      not the total consumed.
355  *
356  *      NOTE: kernel clients don't need to call this function if the channel
357  *      mode is 'overwrite'.
358  */
359 void relay_subbufs_consumed(struct rchan *chan,
360                             unsigned int cpu,
361                             size_t subbufs_consumed)
362 {
363         struct rchan_buf *buf;
364
365         if (!chan)
366                 return;
367
368         if (cpu >= NR_CPUS || !chan->buf[cpu])
369                 return;
370
371         buf = chan->buf[cpu];
372         buf->subbufs_consumed += subbufs_consumed;
373         if (buf->subbufs_consumed > buf->subbufs_produced)
374                 buf->subbufs_consumed = buf->subbufs_produced;
375 }
376
377 /**
378  *      relay_destroy_channel - free the channel struct
379  *
380  *      Should only be called from kref_put().
381  */
382 void relay_destroy_channel(struct kref *kref)
383 {
384         struct rchan *chan = container_of(kref, struct rchan, kref);
385         kfree(chan);
386 }
387
388 /**
389  *      relay_close - close the channel
390  *      @chan: the channel
391  *
392  *      Closes all channel buffers and frees the channel.
393  */
394 void relay_close(struct rchan *chan)
395 {
396         unsigned int i;
397
398         if (!chan)
399                 return;
400
401         for (i = 0; i < NR_CPUS; i++) {
402                 if (!chan->buf[i])
403                         continue;
404                 relay_close_buf(chan->buf[i]);
405         }
406
407         if (chan->last_toobig)
408                 printk(KERN_WARNING "relayfs: one or more items not logged "
409                        "[item size (%Zd) > sub-buffer size (%Zd)]\n",
410                        chan->last_toobig, chan->subbuf_size);
411
412         kref_put(&chan->kref, relay_destroy_channel);
413 }
414
415 /**
416  *      relay_flush - close the channel
417  *      @chan: the channel
418  *
419  *      Flushes all channel buffers i.e. forces buffer switch.
420  */
421 void relay_flush(struct rchan *chan)
422 {
423         unsigned int i;
424
425         if (!chan)
426                 return;
427
428         for (i = 0; i < NR_CPUS; i++) {
429                 if (!chan->buf[i])
430                         continue;
431                 relay_switch_subbuf(chan->buf[i], 0);
432         }
433 }
434
435 EXPORT_SYMBOL_GPL(relay_open);
436 EXPORT_SYMBOL_GPL(relay_close);
437 EXPORT_SYMBOL_GPL(relay_flush);
438 EXPORT_SYMBOL_GPL(relay_reset);
439 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
440 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
441 EXPORT_SYMBOL_GPL(relay_buf_full);