2 * linux/include/linux/relayfs_fs.h
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)
7 * RelayFS definitions and declarations
9 * Please see Documentation/filesystems/relayfs.txt for more info.
12 #ifndef _LINUX_RELAYFS_FS_H
13 #define _LINUX_RELAYFS_FS_H
15 #include <linux/config.h>
16 #include <linux/types.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/list.h>
23 * Tracks changes to rchan struct
25 #define RELAYFS_CHANNEL_VERSION 1
28 * Maximum number of simultaneously open channels
30 #define RELAY_MAX_CHANNELS 256
35 #define RELAY_MIN_BUFS 2
36 #define RELAY_MIN_BUFSIZE 4096
37 #define RELAY_MAX_BUFS 256
38 #define RELAY_MAX_BUF_SIZE 0x1000000
39 #define RELAY_MAX_TOTAL_BUF_SIZE 0x8000000
42 * Lockless scheme utility macros
44 #define RELAY_MAX_BUFNO(bufno_bits) (1UL << (bufno_bits))
45 #define RELAY_BUF_SIZE(offset_bits) (1UL << (offset_bits))
46 #define RELAY_BUF_OFFSET_MASK(offset_bits) (RELAY_BUF_SIZE(offset_bits) - 1)
47 #define RELAY_BUFNO_GET(index, offset_bits) ((index) >> (offset_bits))
48 #define RELAY_BUF_OFFSET_GET(index, mask) ((index) & (mask))
49 #define RELAY_BUF_OFFSET_CLEAR(index, mask) ((index) & ~(mask))
52 * Flags returned by relay_reserve()
54 #define RELAY_BUFFER_SWITCH_NONE 0x0
55 #define RELAY_WRITE_DISCARD_NONE 0x0
56 #define RELAY_BUFFER_SWITCH 0x1
57 #define RELAY_WRITE_DISCARD 0x2
58 #define RELAY_WRITE_TOO_LONG 0x4
61 * Relay attribute flags
63 #define RELAY_DELIVERY_BULK 0x1
64 #define RELAY_DELIVERY_PACKET 0x2
65 #define RELAY_SCHEME_LOCKLESS 0x4
66 #define RELAY_SCHEME_LOCKING 0x8
67 #define RELAY_SCHEME_ANY 0xC
68 #define RELAY_TIMESTAMP_TSC 0x10
69 #define RELAY_TIMESTAMP_GETTIMEOFDAY 0x20
70 #define RELAY_TIMESTAMP_ANY 0x30
71 #define RELAY_USAGE_SMP 0x40
72 #define RELAY_USAGE_GLOBAL 0x80
73 #define RELAY_MODE_CONTINUOUS 0x100
74 #define RELAY_MODE_NO_OVERWRITE 0x200
77 * Flags for needs_resize() callback
79 #define RELAY_RESIZE_NONE 0x0
80 #define RELAY_RESIZE_EXPAND 0x1
81 #define RELAY_RESIZE_SHRINK 0x2
82 #define RELAY_RESIZE_REPLACE 0x4
83 #define RELAY_RESIZE_REPLACED 0x8
86 * Values for fileop_notify() callback
97 * Data structure returned by relay_info()
101 u32 flags; /* relay attribute flags for channel */
102 u32 buf_size; /* channel's sub-buffer size */
103 char *buf_addr; /* address of channel start */
104 u32 alloc_size; /* total buffer size actually allocated */
105 u32 n_bufs; /* number of sub-buffers in channel */
106 u32 cur_idx; /* current write index into channel */
107 u32 bufs_produced; /* current count of sub-buffers produced */
108 u32 bufs_consumed; /* current count of sub-buffers consumed */
109 u32 buf_id; /* buf_id of current sub-buffer */
110 int buffer_complete[RELAY_MAX_BUFS]; /* boolean per sub-buffer */
111 int unused_bytes[RELAY_MAX_BUFS]; /* count per sub-buffer */
115 * Relay channel client callbacks
117 struct rchan_callbacks
120 * buffer_start - called at the beginning of a new sub-buffer
121 * @rchan_id: the channel id
122 * @current_write_pos: position in sub-buffer client should write to
123 * @buffer_id: the id of the new sub-buffer
124 * @start_time: the timestamp associated with the start of sub-buffer
125 * @start_tsc: the TSC associated with the timestamp, if using_tsc
126 * @using_tsc: boolean, indicates whether start_tsc is valid
128 * Return value should be the number of bytes written by the client.
130 * See Documentation/filesystems/relayfs.txt for details.
132 int (*buffer_start) (int rchan_id,
133 char *current_write_pos,
135 struct timeval start_time,
140 * buffer_end - called at the end of a sub-buffer
141 * @rchan_id: the channel id
142 * @current_write_pos: position in sub-buffer of end of data
143 * @end_of_buffer: the position of the end of the sub-buffer
144 * @end_time: the timestamp associated with the end of the sub-buffer
145 * @end_tsc: the TSC associated with the end_time, if using_tsc
146 * @using_tsc: boolean, indicates whether end_tsc is valid
148 * Return value should be the number of bytes written by the client.
150 * See Documentation/filesystems/relayfs.txt for details.
152 int (*buffer_end) (int rchan_id,
153 char *current_write_pos,
155 struct timeval end_time,
160 * deliver - called when data is ready for the client
161 * @rchan_id: the channel id
162 * @from: the start of the delivered data
163 * @len: the length of the delivered data
165 * See Documentation/filesystems/relayfs.txt for details.
167 void (*deliver) (int rchan_id, char *from, u32 len);
170 * user_deliver - called when data has been written from userspace
171 * @rchan_id: the channel id
172 * @from: the start of the delivered data
173 * @len: the length of the delivered data
175 * See Documentation/filesystems/relayfs.txt for details.
177 void (*user_deliver) (int rchan_id, char *from, u32 len);
180 * needs_resize - called when a resizing event occurs
181 * @rchan_id: the channel id
182 * @resize_type: the type of resizing event
183 * @suggested_buf_size: the suggested new sub-buffer size
184 * @suggested_buf_size: the suggested new number of sub-buffers
186 * See Documentation/filesystems/relayfs.txt for details.
188 void (*needs_resize)(int rchan_id,
190 u32 suggested_buf_size,
191 u32 suggested_n_bufs);
194 * fileop_notify - called on open/close/mmap/munmap of a relayfs file
195 * @rchan_id: the channel id
196 * @filp: relayfs file pointer
197 * @fileop: which file operation is in progress
199 * The return value can direct the outcome of the operation.
201 * See Documentation/filesystems/relayfs.txt for details.
203 int (*fileop_notify)(int rchan_id,
205 enum relay_fileop fileop);
208 * ioctl - called in ioctl context from userspace
209 * @rchan_id: the channel id
211 * @arg: ioctl cmd arg
213 * The return value is returned as the value from the ioctl call.
215 * See Documentation/filesystems/relayfs.txt for details.
217 int (*ioctl) (int rchan_id, unsigned int cmd, unsigned long arg);
221 * Lockless scheme-specific data
223 struct lockless_rchan
225 u8 bufno_bits; /* # bits used for sub-buffer id */
226 u8 offset_bits; /* # bits used for offset within sub-buffer */
227 u32 index; /* current index = sub-buffer id and offset */
228 u32 offset_mask; /* used to obtain offset portion of index */
229 u32 index_mask; /* used to mask off unused bits index */
230 atomic_t fill_count[RELAY_MAX_BUFS]; /* fill count per sub-buffer */
234 * Locking scheme-specific data
238 char *write_buf; /* start of write sub-buffer */
239 char *write_buf_end; /* end of write sub-buffer */
240 char *current_write_pos; /* current write pointer */
241 char *write_limit; /* takes reserves into account */
242 char *in_progress_event_pos; /* used for interrupted writes */
243 u16 in_progress_event_size; /* used for interrupted writes */
244 char *interrupted_pos; /* used for interrupted writes */
245 u16 interrupting_size; /* used for interrupted writes */
246 spinlock_t lock; /* channel lock for locking scheme */
252 * Offset resizing data structure
262 * Relay channel data structure
266 u32 version; /* the version of this struct */
267 char *buf; /* the channel buffer */
270 struct lockless_rchan lockless;
271 struct locking_rchan locking;
272 } scheme; /* scheme-specific channel data */
274 int id; /* the channel id */
275 struct rchan_callbacks *callbacks; /* client callbacks */
276 u32 flags; /* relay channel attributes */
277 u32 buf_id; /* current sub-buffer id */
278 u32 buf_idx; /* current sub-buffer index */
280 atomic_t mapped; /* map count */
282 atomic_t suspended; /* channel suspended i.e full? */
283 int half_switch; /* used internally for suspend */
285 struct timeval buf_start_time; /* current sub-buffer start time */
286 u32 buf_start_tsc; /* current sub-buffer start TSC */
288 u32 buf_size; /* sub-buffer size */
289 u32 alloc_size; /* total buffer size allocated */
290 u32 n_bufs; /* number of sub-buffers */
292 u32 bufs_produced; /* count of sub-buffers produced */
293 u32 bufs_consumed; /* count of sub-buffers consumed */
294 u32 bytes_consumed; /* bytes consumed in cur sub-buffer */
296 int initialized; /* first buffer initialized? */
297 int finalized; /* channel finalized? */
299 u32 start_reserve; /* reserve at start of sub-buffers */
300 u32 end_reserve; /* reserve at end of sub-buffers */
301 u32 rchan_start_reserve; /* additional reserve sub-buffer 0 */
303 struct dentry *dentry; /* channel file dentry */
305 wait_queue_head_t read_wait; /* VFS read wait queue */
306 wait_queue_head_t write_wait; /* VFS write wait queue */
307 struct work_struct wake_readers; /* reader wake-up work struct */
308 struct work_struct wake_writers; /* reader wake-up work struct */
309 atomic_t refcount; /* channel refcount */
311 struct relay_ops *relay_ops; /* scheme-specific channel ops */
313 int unused_bytes[RELAY_MAX_BUFS]; /* unused count per sub-buffer */
315 struct semaphore resize_sem; /* serializes alloc/repace */
316 struct work_struct work; /* resize allocation work struct */
318 struct list_head open_readers; /* open readers for this channel */
319 rwlock_t open_readers_lock; /* protection for open_readers list */
321 char *init_buf; /* init channel buffer, if non-NULL */
323 u32 resize_min; /* minimum resized total buffer size */
324 u32 resize_max; /* maximum resized total buffer size */
325 char *resize_buf; /* for autosize alloc/free */
326 u32 resize_buf_size; /* resized sub-buffer size */
327 u32 resize_n_bufs; /* resized number of sub-buffers */
328 u32 resize_alloc_size; /* resized actual total size */
329 int resizing; /* is resizing in progress? */
330 int resize_err; /* resizing err code */
331 int resize_failures; /* number of resize failures */
332 int replace_buffer; /* is the alloced buffer ready? */
333 struct resize_offset resize_offset; /* offset change */
334 struct timer_list shrink_timer; /* timer used for shrinking */
335 int resize_order; /* size of last resize */
336 u32 expand_buf_id; /* subbuf id expand will occur at */
338 struct page **buf_page_array; /* array of current buffer pages */
339 int buf_page_count; /* number of current buffer pages */
340 struct page **expand_page_array;/* new pages to be inserted */
341 int expand_page_count; /* number of new pages */
342 struct page **shrink_page_array;/* old pages to be freed */
343 int shrink_page_count; /* number of old pages */
344 struct page **resize_page_array;/* will become current pages */
345 int resize_page_count; /* number of resize pages */
346 struct page **old_buf_page_array; /* hold for freeing */
347 } ____cacheline_aligned;
350 * Relay channel reader struct
354 struct list_head list; /* for list inclusion */
355 struct rchan *rchan; /* the channel we're reading from */
356 int auto_consume; /* does this reader auto-consume? */
357 u32 bufs_consumed; /* buffers this reader has consumed */
358 u32 bytes_consumed; /* bytes consumed in cur sub-buffer */
359 int offset_changed; /* have channel offsets changed? */
360 int vfs_reader; /* are we a VFS reader? */
361 int map_reader; /* are we an mmap reader? */
367 } pos; /* current read offset */
371 * These help make union member access less tedious
373 #define channel_buffer(rchan) ((rchan)->buf)
374 #define idx(rchan) ((rchan)->scheme.lockless.index)
375 #define bufno_bits(rchan) ((rchan)->scheme.lockless.bufno_bits)
376 #define offset_bits(rchan) ((rchan)->scheme.lockless.offset_bits)
377 #define offset_mask(rchan) ((rchan)->scheme.lockless.offset_mask)
378 #define idx_mask(rchan) ((rchan)->scheme.lockless.index_mask)
379 #define bulk_delivery(rchan) (((rchan)->flags & RELAY_DELIVERY_BULK) ? 1 : 0)
380 #define packet_delivery(rchan) (((rchan)->flags & RELAY_DELIVERY_PACKET) ? 1 : 0)
381 #define using_lockless(rchan) (((rchan)->flags & RELAY_SCHEME_LOCKLESS) ? 1 : 0)
382 #define using_locking(rchan) (((rchan)->flags & RELAY_SCHEME_LOCKING) ? 1 : 0)
383 #define using_tsc(rchan) (((rchan)->flags & RELAY_TIMESTAMP_TSC) ? 1 : 0)
384 #define using_gettimeofday(rchan) (((rchan)->flags & RELAY_TIMESTAMP_GETTIMEOFDAY) ? 1 : 0)
385 #define usage_smp(rchan) (((rchan)->flags & RELAY_USAGE_SMP) ? 1 : 0)
386 #define usage_global(rchan) (((rchan)->flags & RELAY_USAGE_GLOBAL) ? 1 : 0)
387 #define mode_continuous(rchan) (((rchan)->flags & RELAY_MODE_CONTINUOUS) ? 1 : 0)
388 #define fill_count(rchan, i) ((rchan)->scheme.lockless.fill_count[(i)])
389 #define write_buf(rchan) ((rchan)->scheme.locking.write_buf)
390 #define read_buf(rchan) ((rchan)->scheme.locking.read_buf)
391 #define write_buf_end(rchan) ((rchan)->scheme.locking.write_buf_end)
392 #define read_buf_end(rchan) ((rchan)->scheme.locking.read_buf_end)
393 #define cur_write_pos(rchan) ((rchan)->scheme.locking.current_write_pos)
394 #define read_limit(rchan) ((rchan)->scheme.locking.read_limit)
395 #define write_limit(rchan) ((rchan)->scheme.locking.write_limit)
396 #define in_progress_event_pos(rchan) ((rchan)->scheme.locking.in_progress_event_pos)
397 #define in_progress_event_size(rchan) ((rchan)->scheme.locking.in_progress_event_size)
398 #define interrupted_pos(rchan) ((rchan)->scheme.locking.interrupted_pos)
399 #define interrupting_size(rchan) ((rchan)->scheme.locking.interrupting_size)
400 #define channel_lock(rchan) ((rchan)->scheme.locking.lock)
404 * calc_time_delta - utility function for time delta calculation
408 * Returns the time delta produced by subtracting start time from now.
411 calc_time_delta(struct timeval *now,
412 struct timeval *start)
414 return (now->tv_sec - start->tv_sec) * 1000000
415 + (now->tv_usec - start->tv_usec);
419 * recalc_time_delta - utility function for time delta recalculation
421 * @new_delta: the new time delta calculated
422 * @cpu: the associated CPU id
425 recalc_time_delta(struct timeval *now,
429 if (using_tsc(rchan) == 0)
430 *new_delta = calc_time_delta(now, &rchan->buf_start_time);
434 * have_cmpxchg - does this architecture have a cmpxchg?
436 * Returns 1 if this architecture has a cmpxchg useable by
437 * the lockless scheme, 0 otherwise.
442 #if defined(__HAVE_ARCH_CMPXCHG)
450 * relay_write_direct - write data directly into destination buffer
452 #define relay_write_direct(DEST, SRC, SIZE) \
455 memcpy(DEST, SRC, SIZE);\
460 * relay_lock_channel - lock the relay channel if applicable
462 * This macro only affects the locking scheme. If the locking scheme
463 * is in use and the channel usage is SMP, does a local_irq_save. If the
464 * locking sheme is in use and the channel usage is GLOBAL, uses
465 * spin_lock_irqsave. FLAGS is initialized to 0 since we know that
466 * it is being initialized prior to use and we avoid the compiler warning.
468 #define relay_lock_channel(RCHAN, FLAGS) \
472 if (using_locking(RCHAN)) {\
473 if (usage_smp(RCHAN)) {\
474 local_irq_save(FLAGS); \
476 spin_lock_irqsave(&(RCHAN)->scheme.locking.lock, FLAGS); \
482 * relay_unlock_channel - unlock the relay channel if applicable
484 * This macro only affects the locking scheme. See relay_lock_channel.
486 #define relay_unlock_channel(RCHAN, FLAGS) \
489 if (using_locking(RCHAN)) {\
490 if (usage_smp(RCHAN)) {\
491 local_irq_restore(FLAGS); \
493 spin_unlock_irqrestore(&(RCHAN)->scheme.locking.lock, FLAGS); \
499 * Define cmpxchg if we don't have it
501 #ifndef __HAVE_ARCH_CMPXCHG
502 #define cmpxchg(p,o,n) 0
506 * High-level relayfs kernel API, fs/relayfs/relay.c
509 relay_open(const char *chanpath,
513 struct rchan_callbacks *channel_callbacks,
516 u32 rchan_start_reserve,
524 relay_close(int rchan_id);
527 relay_write(int rchan_id,
528 const void *data_ptr,
534 relay_read(struct rchan_reader *reader,
538 u32 *actual_read_offset);
541 relay_discard_init_buf(int rchan_id);
543 extern struct rchan_reader *
544 add_rchan_reader(int rchan_id, int autoconsume);
547 remove_rchan_reader(struct rchan_reader *reader);
549 extern struct rchan_reader *
550 add_map_reader(int rchan_id);
553 remove_map_reader(struct rchan_reader *reader);
556 relay_info(int rchan_id, struct rchan_info *rchan_info);
559 relay_buffers_consumed(struct rchan_reader *reader, u32 buffers_consumed);
562 relay_bytes_consumed(struct rchan_reader *reader, u32 bytes_consumed, u32 read_offset);
565 relay_bytes_avail(struct rchan_reader *reader);
568 relay_realloc_buffer(int rchan_id, u32 new_nbufs, int in_background);
571 relay_replace_buffer(int rchan_id);
574 rchan_empty(struct rchan_reader *reader);
577 rchan_full(struct rchan_reader *reader);
580 update_readers_consumed(struct rchan *rchan, u32 bufs_consumed, u32 bytes_consumed);
583 __relay_mmap_buffer(struct rchan *rchan, struct vm_area_struct *vma);
585 extern struct rchan_reader *
586 __add_rchan_reader(struct rchan *rchan, struct file *filp, int auto_consume, int map_reader);
589 __remove_rchan_reader(struct rchan_reader *reader);
592 * Low-level relayfs kernel API, fs/relayfs/relay.c
594 extern struct rchan *
595 rchan_get(int rchan_id);
598 rchan_put(struct rchan *rchan);
601 relay_reserve(struct rchan *rchan,
603 struct timeval *time_stamp,
609 relay_commit(struct rchan *rchan,
616 relay_get_offset(struct rchan *rchan, u32 *max_offset);
619 relay_reset(int rchan_id);
622 * VFS functions, fs/relayfs/inode.c
625 relayfs_create_dir(const char *name,
626 struct dentry *parent,
627 struct dentry **dentry);
630 relayfs_create_file(const char * name,
631 struct dentry *parent,
632 struct dentry **dentry,
637 relayfs_remove_file(struct dentry *dentry);
640 reset_index(struct rchan *rchan, u32 old_index);
644 * klog functions, fs/relayfs/klog.c
647 create_klog_channel(void);
650 remove_klog_channel(void);
653 * Scheme-specific channel ops
657 char * (*reserve) (struct rchan *rchan,
659 struct timeval *time_stamp,
664 void (*commit) (struct rchan *rchan,
670 u32 (*get_offset) (struct rchan *rchan,
673 void (*resume) (struct rchan *rchan);
674 void (*finalize) (struct rchan *rchan);
675 void (*reset) (struct rchan *rchan,
677 int (*reset_index) (struct rchan *rchan,
681 #endif /* _LINUX_RELAYFS_FS_H */