xref: /netbsd-src/sys/miscfs/kernfs/kernfs.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: kernfs.h,v 1.23 2004/05/20 06:34:30 atatat Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kernfs.h	8.6 (Berkeley) 3/29/95
35  */
36 
37 #define	_PATH_KERNFS	"/kern"		/* Default mountpoint */
38 
39 #ifdef _KERNEL
40 #include <sys/queue.h>
41 
42 /*
43  * The different types of node in a kernfs filesystem
44  */
45 typedef enum {
46 	KFSkern,		/* the filesystem itself (.) */
47 	KFSroot,		/* the filesystem root (..) */
48 	KFSnull,		/* none aplicable */
49 	KFStime,		/* boottime */
50 	KFSint,			/* integer */
51 	KFSstring,		/* string */
52 	KFShostname,	/* hostname */
53 	KFSavenrun,		/* loadavg */
54 	KFSdevice,		/* device file (rootdev/rrootdev) */
55 	KFSmsgbuf,		/* msgbuf */
56 	KFSipsecsadir,	/* ipsec security association (top dir) */
57 	KFSipsecspdir,	/* ipsec security policy (top dir) */
58 	KFSipsecsa,		/* ipsec security association entry */
59 	KFSipsecsp,		/* ipsec security policy entry */
60 	KFSsubdir,		/* directory */
61 	KFSlasttype,		/* last used type */
62 	KFSmaxtype = (1<<6) - 1	/* last possible type */
63 } kfstype;
64 
65 /*
66  * Control data for the kern file system.
67  */
68 struct kern_target {
69 	u_char		kt_type;
70 	u_char		kt_namlen;
71 	const char	*kt_name;
72 	void		*kt_data;
73 	kfstype		kt_tag;
74 	u_char		kt_vtype;
75 	mode_t		kt_mode;
76 };
77 
78 struct dyn_kern_target {
79 	struct kern_target		dkt_kt;
80 	SIMPLEQ_ENTRY(dyn_kern_target)	dkt_queue;
81 };
82 
83 struct kernfs_subdir {
84 	SIMPLEQ_HEAD(,dyn_kern_target)	ks_entries;
85 	unsigned int			ks_nentries;
86 	unsigned int			ks_dirs;
87 	const struct kern_target	*ks_parent;
88 };
89 
90 struct kernfs_node {
91 	LIST_ENTRY(kernfs_node) kfs_hash; /* hash chain */
92 	TAILQ_ENTRY(kernfs_node) kfs_list; /* flat list */
93 	struct vnode	*kfs_vnode;	/* vnode associated with this pfsnode */
94 	kfstype		kfs_type;	/* type of procfs node */
95 	mode_t		kfs_mode;	/* mode bits for stat() */
96 	long		kfs_fileno;	/* unique file id */
97 	u_int32_t	kfs_value;	/* SA id or SP id (KFSint) */
98 	const struct kern_target *kfs_kt;
99 	void		*kfs_v;		/* pointer to secasvar/secpolicy/mbuf */
100 	long		kfs_cookie;	/* fileno cookie */
101 };
102 
103 struct kernfs_mount {
104 	TAILQ_HEAD(, kernfs_node) nodelist;
105 	long fileno_cookie;
106 };
107 
108 #define UIO_MX	32
109 
110 #define KERNFS_FILENO(kt, typ, cookie) \
111 	((kt >= &kern_targets[0] && kt < &kern_targets[static_nkern_targets]) \
112 	    ? 2 + ((kt) - &kern_targets[0]) \
113 	      : (((cookie + 1) << 6) | (typ)))
114 #define KERNFS_TYPE_FILENO(typ, cookie) \
115 	(((cookie + 1) << 6) | (typ))
116 
117 #define VFSTOKERNFS(mp)	((struct kernfs_mount *)((mp)->mnt_data))
118 #define	VTOKERN(vp)	((struct kernfs_node *)(vp)->v_data)
119 #define KERNFSTOV(kfs)	((kfs)->kfs_vnode)
120 
121 extern const struct kern_target kern_targets[];
122 extern int nkern_targets;
123 extern const int static_nkern_targets;
124 extern int (**kernfs_vnodeop_p) __P((void *));
125 extern struct vfsops kernfs_vfsops;
126 extern dev_t rrootdev;
127 
128 struct secasvar;
129 struct secpolicy;
130 
131 int kernfs_root __P((struct mount *, struct vnode **));
132 
133 void kernfs_hashinit __P((void));
134 void kernfs_hashreinit __P((void));
135 void kernfs_hashdone __P((void));
136 int kernfs_freevp __P((struct vnode *));
137 int kernfs_allocvp __P((struct mount *, struct vnode **, kfstype,
138 	const struct kern_target *, u_int32_t));
139 
140 void kernfs_revoke_sa __P((struct secasvar *));
141 void kernfs_revoke_sp __P((struct secpolicy *));
142 
143 /*
144  * Data types for the kernfs file operations.
145  */
146 typedef enum {
147 	KERNFS_XWRITE,
148 	KERNFS_FILEOP_CLOSE,
149 	KERNFS_FILEOP_GETATTR,
150 	KERNFS_FILEOP_IOCTL,
151 	KERNFS_FILEOP_MMAP,
152 	KERNFS_FILEOP_OPEN,
153 	KERNFS_FILEOP_WRITE,
154 } kfsfileop;
155 
156 struct kernfs_fileop {
157 	kfstype				kf_type;
158 	kfsfileop			kf_fileop;
159 	union {
160 		void			*_kf_genop;
161 		int			(*_kf_vop)(void *);
162 		int			(*_kf_xwrite)
163 			(const struct kernfs_node *, char *, size_t);
164 	} _kf_opfn;
165 	SPLAY_ENTRY(kernfs_fileop)	kf_node;
166 };
167 #define	kf_genop	_kf_opfn
168 #define	kf_vop		_kf_opfn._kf_vop
169 #define	kf_xwrite	_kf_opfn._kf_xwrite
170 
171 typedef struct kern_target kernfs_parentdir_t;
172 typedef struct dyn_kern_target kernfs_entry_t;
173 
174 /*
175  * Functions for adding kernfs datatypes and nodes.
176  */
177 kfstype kernfs_alloctype(int, const struct kernfs_fileop *);
178 #define	KERNFS_ALLOCTYPE(kf) kernfs_alloctype(sizeof((kf)) / \
179 	sizeof((kf)[0]), (kf))
180 #define	KERNFS_ALLOCENTRY(dkt, m_type, m_flags)				\
181 	dkt = (struct dyn_kern_target *)malloc(				\
182 		sizeof(struct dyn_kern_target), (m_type), (m_flags))
183 #define	KERNFS_INITENTRY(dkt, type, name, data, tag, vtype, mode) do {	\
184 	(dkt)->dkt_kt.kt_type = (type);					\
185 	(dkt)->dkt_kt.kt_namlen = strlen((name));			\
186 	(dkt)->dkt_kt.kt_name = (name);					\
187 	(dkt)->dkt_kt.kt_data = (data);					\
188 	(dkt)->dkt_kt.kt_tag = (tag);					\
189 	(dkt)->dkt_kt.kt_vtype = (vtype);				\
190 	(dkt)->dkt_kt.kt_mode = (mode);					\
191 } while (/*CONSTCOND*/0)
192 #define	KERNFS_ENTOPARENTDIR(dkt) &(dkt)->dkt_kt
193 int kernfs_addentry __P((kernfs_parentdir_t *, kernfs_entry_t *));
194 
195 #ifdef SYSCTL_SETUP_PROTO
196 SYSCTL_SETUP_PROTO(sysctl_vfs_kernfs_setup);
197 #endif /* SYSCTL_SETUP_PROTO */
198 
199 #endif /* _KERNEL */
200