blob: 91cacc34c159192ab5a7fa712982ea5a8d268b59 [file] [log] [blame]
Jens Axboeb86ff982006-03-23 19:56:55 +01001/*
2 * linux/include/linux/relay.h
3 *
4 * Copyright (C) 2002, 2003 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
5 * Copyright (C) 1999, 2000, 2001, 2002 - Karim Yaghmour (karim@opersys.com)
6 *
7 * CONFIG_RELAY definitions and declarations
8 */
9
10#ifndef _LINUX_RELAY_H
11#define _LINUX_RELAY_H
12
Jens Axboeb86ff982006-03-23 19:56:55 +010013#include <linux/types.h>
14#include <linux/sched.h>
Tom Zanussi7c9cb382007-05-09 02:34:01 -070015#include <linux/timer.h>
Jens Axboeb86ff982006-03-23 19:56:55 +010016#include <linux/wait.h>
17#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050018#include <linux/bug.h>
Jens Axboeb86ff982006-03-23 19:56:55 +010019#include <linux/fs.h>
20#include <linux/poll.h>
21#include <linux/kref.h>
22
23/* Needs a _much_ better name... */
24#define FIX_SIZE(x) ((((x) - 1) & PAGE_MASK) + PAGE_SIZE)
25
26/*
27 * Tracks changes to rchan/rchan_buf structs
28 */
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080029#define RELAYFS_CHANNEL_VERSION 7
Jens Axboeb86ff982006-03-23 19:56:55 +010030
31/*
32 * Per-cpu relay channel buffer
33 */
34struct rchan_buf
35{
36 void *start; /* start of channel buffer */
37 void *data; /* start of current sub-buffer */
38 size_t offset; /* current offset into sub-buffer */
39 size_t subbufs_produced; /* count of sub-buffers produced */
40 size_t subbufs_consumed; /* count of sub-buffers consumed */
41 struct rchan *chan; /* associated channel */
42 wait_queue_head_t read_wait; /* reader wait queue */
Tom Zanussi7c9cb382007-05-09 02:34:01 -070043 struct timer_list timer; /* reader wake-up timer */
Jens Axboeb86ff982006-03-23 19:56:55 +010044 struct dentry *dentry; /* channel file dentry */
45 struct kref kref; /* channel buffer refcount */
46 struct page **page_array; /* array of current buffer pages */
47 unsigned int page_count; /* number of current buffer pages */
48 unsigned int finalized; /* buffer has been finalized */
49 size_t *padding; /* padding counts per sub-buffer */
50 size_t prev_padding; /* temporary variable */
51 size_t bytes_consumed; /* bytes consumed in cur read subbuf */
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -070052 size_t early_bytes; /* bytes consumed before VFS inited */
Jens Axboeb86ff982006-03-23 19:56:55 +010053 unsigned int cpu; /* this buf's cpu */
54} ____cacheline_aligned;
55
56/*
57 * Relay channel data structure
58 */
59struct rchan
60{
61 u32 version; /* the version of this struct */
62 size_t subbuf_size; /* sub-buffer size */
63 size_t n_subbufs; /* number of sub-buffers per buffer */
64 size_t alloc_size; /* total buffer size allocated */
65 struct rchan_callbacks *cb; /* client callbacks */
66 struct kref kref; /* channel refcount */
67 void *private_data; /* for user-defined data */
68 size_t last_toobig; /* tried to log event > subbuf size */
69 struct rchan_buf *buf[NR_CPUS]; /* per-cpu channel buffers */
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080070 int is_global; /* One global buffer ? */
71 struct list_head list; /* for channel list */
72 struct dentry *parent; /* parent dentry passed to open */
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -070073 int has_base_filename; /* has a filename associated? */
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080074 char base_filename[NAME_MAX]; /* saved base filename */
Jens Axboeb86ff982006-03-23 19:56:55 +010075};
76
77/*
78 * Relay channel client callbacks
79 */
80struct rchan_callbacks
81{
82 /*
83 * subbuf_start - called on buffer-switch to a new sub-buffer
84 * @buf: the channel buffer containing the new sub-buffer
85 * @subbuf: the start of the new sub-buffer
86 * @prev_subbuf: the start of the previous sub-buffer
87 * @prev_padding: unused space at the end of previous sub-buffer
88 *
89 * The client should return 1 to continue logging, 0 to stop
90 * logging.
91 *
92 * NOTE: subbuf_start will also be invoked when the buffer is
93 * created, so that the first sub-buffer can be initialized
94 * if necessary. In this case, prev_subbuf will be NULL.
95 *
96 * NOTE: the client can reserve bytes at the beginning of the new
97 * sub-buffer by calling subbuf_start_reserve() in this callback.
98 */
99 int (*subbuf_start) (struct rchan_buf *buf,
100 void *subbuf,
101 void *prev_subbuf,
102 size_t prev_padding);
103
104 /*
105 * buf_mapped - relay buffer mmap notification
106 * @buf: the channel buffer
107 * @filp: relay file pointer
108 *
109 * Called when a relay file is successfully mmapped
110 */
111 void (*buf_mapped)(struct rchan_buf *buf,
112 struct file *filp);
113
114 /*
115 * buf_unmapped - relay buffer unmap notification
116 * @buf: the channel buffer
117 * @filp: relay file pointer
118 *
119 * Called when a relay file is successfully unmapped
120 */
121 void (*buf_unmapped)(struct rchan_buf *buf,
122 struct file *filp);
123 /*
124 * create_buf_file - create file to represent a relay channel buffer
125 * @filename: the name of the file to create
126 * @parent: the parent of the file to create
127 * @mode: the mode of the file to create
128 * @buf: the channel buffer
129 * @is_global: outparam - set non-zero if the buffer should be global
130 *
131 * Called during relay_open(), once for each per-cpu buffer,
132 * to allow the client to create a file to be used to
133 * represent the corresponding channel buffer. If the file is
134 * created outside of relay, the parent must also exist in
135 * that filesystem.
136 *
137 * The callback should return the dentry of the file created
138 * to represent the relay buffer.
139 *
140 * Setting the is_global outparam to a non-zero value will
141 * cause relay_open() to create a single global buffer rather
142 * than the default set of per-cpu buffers.
143 *
Randy Dunlap55dff492009-09-23 15:56:17 -0700144 * See Documentation/filesystems/relay.txt for more info.
Jens Axboeb86ff982006-03-23 19:56:55 +0100145 */
146 struct dentry *(*create_buf_file)(const char *filename,
147 struct dentry *parent,
Al Virof4ae40a2011-07-24 04:33:43 -0400148 umode_t mode,
Jens Axboeb86ff982006-03-23 19:56:55 +0100149 struct rchan_buf *buf,
150 int *is_global);
151
152 /*
153 * remove_buf_file - remove file representing a relay channel buffer
154 * @dentry: the dentry of the file to remove
155 *
156 * Called during relay_close(), once for each per-cpu buffer,
157 * to allow the client to remove a file used to represent a
158 * channel buffer.
159 *
160 * The callback should return 0 if successful, negative if not.
161 */
162 int (*remove_buf_file)(struct dentry *dentry);
163};
164
165/*
166 * CONFIG_RELAY kernel API, kernel/relay.c
167 */
168
169struct rchan *relay_open(const char *base_filename,
170 struct dentry *parent,
171 size_t subbuf_size,
172 size_t n_subbufs,
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800173 struct rchan_callbacks *cb,
174 void *private_data);
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700175extern int relay_late_setup_files(struct rchan *chan,
176 const char *base_filename,
177 struct dentry *parent);
Jens Axboeb86ff982006-03-23 19:56:55 +0100178extern void relay_close(struct rchan *chan);
179extern void relay_flush(struct rchan *chan);
180extern void relay_subbufs_consumed(struct rchan *chan,
181 unsigned int cpu,
182 size_t consumed);
183extern void relay_reset(struct rchan *chan);
184extern int relay_buf_full(struct rchan_buf *buf);
185
186extern size_t relay_switch_subbuf(struct rchan_buf *buf,
187 size_t length);
188
189/**
190 * relay_write - write data into the channel
191 * @chan: relay channel
192 * @data: data to be written
193 * @length: number of bytes to write
194 *
195 * Writes data into the current cpu's channel buffer.
196 *
197 * Protects the buffer by disabling interrupts. Use this
198 * if you might be logging from interrupt context. Try
199 * __relay_write() if you know you won't be logging from
200 * interrupt context.
201 */
202static inline void relay_write(struct rchan *chan,
203 const void *data,
204 size_t length)
205{
206 unsigned long flags;
207 struct rchan_buf *buf;
208
209 local_irq_save(flags);
210 buf = chan->buf[smp_processor_id()];
211 if (unlikely(buf->offset + length > chan->subbuf_size))
212 length = relay_switch_subbuf(buf, length);
213 memcpy(buf->data + buf->offset, data, length);
214 buf->offset += length;
215 local_irq_restore(flags);
216}
217
218/**
219 * __relay_write - write data into the channel
220 * @chan: relay channel
221 * @data: data to be written
222 * @length: number of bytes to write
223 *
224 * Writes data into the current cpu's channel buffer.
225 *
226 * Protects the buffer by disabling preemption. Use
227 * relay_write() if you might be logging from interrupt
228 * context.
229 */
230static inline void __relay_write(struct rchan *chan,
231 const void *data,
232 size_t length)
233{
234 struct rchan_buf *buf;
235
236 buf = chan->buf[get_cpu()];
237 if (unlikely(buf->offset + length > buf->chan->subbuf_size))
238 length = relay_switch_subbuf(buf, length);
239 memcpy(buf->data + buf->offset, data, length);
240 buf->offset += length;
241 put_cpu();
242}
243
244/**
245 * relay_reserve - reserve slot in channel buffer
246 * @chan: relay channel
247 * @length: number of bytes to reserve
248 *
249 * Returns pointer to reserved slot, NULL if full.
250 *
251 * Reserves a slot in the current cpu's channel buffer.
252 * Does not protect the buffer at all - caller must provide
253 * appropriate synchronization.
254 */
255static inline void *relay_reserve(struct rchan *chan, size_t length)
256{
257 void *reserved;
258 struct rchan_buf *buf = chan->buf[smp_processor_id()];
259
260 if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
261 length = relay_switch_subbuf(buf, length);
262 if (!length)
263 return NULL;
264 }
265 reserved = buf->data + buf->offset;
266 buf->offset += length;
267
268 return reserved;
269}
270
271/**
272 * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
273 * @buf: relay channel buffer
274 * @length: number of bytes to reserve
275 *
276 * Helper function used to reserve bytes at the beginning of
277 * a sub-buffer in the subbuf_start() callback.
278 */
279static inline void subbuf_start_reserve(struct rchan_buf *buf,
280 size_t length)
281{
282 BUG_ON(length >= buf->chan->subbuf_size - 1);
283 buf->offset = length;
284}
285
286/*
287 * exported relay file operations, kernel/relay.c
288 */
Helge Deller15ad7cd2006-12-06 20:40:36 -0800289extern const struct file_operations relay_file_operations;
Jens Axboeb86ff982006-03-23 19:56:55 +0100290
291#endif /* _LINUX_RELAY_H */
292