1 /*	$NetBSD: kernfs.h,v 1.40 2014/07/20 13:58:04 hannken 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 #include <sys/tree.h>
42 #include <sys/mutex.h>
43 
44 /*
45  * The different types of node in a kernfs filesystem
46  */
47 typedef enum {
48 	KFSkern,		/* the filesystem itself (.) */
49 	KFSroot,		/* the filesystem root (..) */
50 	KFSnull,		/* none aplicable */
51 	KFStime,		/* boottime */
52 	KFSint,			/* integer */
53 	KFSstring,		/* string */
54 	KFShostname,	/* hostname */
55 	KFSavenrun,		/* loadavg */
56 	KFSdevice,		/* device file (rootdev/rrootdev) */
57 	KFSmsgbuf,		/* msgbuf */
58 	KFSsubdir,		/* directory */
59 	KFSlasttype,		/* last used type */
60 	KFSmaxtype = (1<<6) - 1	/* last possible type */
61 } kfstype;
62 
63 /*
64  * Control data for the kern file system.
65  */
66 struct kern_target {
67 	u_char		kt_type;
68 	u_char		kt_namlen;
69 	const char	*kt_name;
70 	void		*kt_data;
71 	kfstype		kt_tag;
72 	u_char		kt_vtype;
73 	mode_t		kt_mode;
74 };
75 
76 struct dyn_kern_target {
77 	struct kern_target		dkt_kt;
78 	SIMPLEQ_ENTRY(dyn_kern_target)	dkt_queue;
79 };
80 
81 struct kernfs_subdir {
82 	SIMPLEQ_HEAD(,dyn_kern_target)	ks_entries;
83 	unsigned int			ks_nentries;
84 	unsigned int			ks_dirs;
85 	const struct kern_target	*ks_parent;
86 };
87 
88 struct kernfs_node {
89 	LIST_ENTRY(kernfs_node) kfs_hash; /* hash chain */
90 	TAILQ_ENTRY(kernfs_node) kfs_list; /* flat list */
91 	struct vnode	*kfs_vnode;	/* vnode associated with this kernfs_node */
92 	kfstype		kfs_type;	/* type of kernfs node */
93 	mode_t		kfs_mode;	/* mode bits for stat() */
94 	long		kfs_fileno;	/* unique file id */
95 	const struct kern_target *kfs_kt;
96 	void		*kfs_v;		/* dynamic node private data */
97 	long		kfs_cookie;	/* fileno cookie */
98 };
99 
100 struct kernfs_mount {
101 	TAILQ_HEAD(, kernfs_node) nodelist;
102 	long fileno_cookie;
103 };
104 
105 #define UIO_MX	32
106 
107 #define KERNFS_FILENO(kt, typ, cookie) \
108 	((kt >= &kern_targets[0] && kt < &kern_targets[static_nkern_targets]) \
109 	    ? 2 + ((kt) - &kern_targets[0]) \
110 	      : (((cookie + 1) << 6) | (typ)))
111 #define KERNFS_TYPE_FILENO(typ, cookie) \
112 	(((cookie + 1) << 6) | (typ))
113 
114 #define VFSTOKERNFS(mp)	((struct kernfs_mount *)((mp)->mnt_data))
115 #define	VTOKERN(vp)	((struct kernfs_node *)(vp)->v_data)
116 #define KERNFSTOV(kfs)	((kfs)->kfs_vnode)
117 
118 #define KERNFS_MAXNAMLEN	255
119 
120 extern const struct kern_target kern_targets[];
121 extern int nkern_targets;
122 extern const int static_nkern_targets;
123 extern int (**kernfs_vnodeop_p)(void *);
124 extern struct vfsops kernfs_vfsops;
125 extern dev_t rrootdev;
126 extern kmutex_t kfs_lock;
127 
128 int kernfs_root(struct mount *, struct vnode **);
129 
130 /*
131  * Data types for the kernfs file operations.
132  */
133 typedef enum {
134 	KERNFS_XREAD,
135 	KERNFS_XWRITE,
136 	KERNFS_FILEOP_CLOSE,
137 	KERNFS_FILEOP_GETATTR,
138 	KERNFS_FILEOP_IOCTL,
139 	KERNFS_FILEOP_OPEN,
140 	KERNFS_FILEOP_READ,
141 	KERNFS_FILEOP_WRITE,
142 } kfsfileop;
143 
144 struct kernfs_fileop {
145 	kfstype				kf_type;
146 	kfsfileop			kf_fileop;
147 	union {
148 		int			(*_kf_vop)(void *);
149 		int			(*_kf_xread)
150 			(const struct kernfs_node *, char **, size_t);
151 		int			(*_kf_xwrite)
152 			(const struct kernfs_node *, char *, size_t);
153 	} _kf_opfn;
154 	SPLAY_ENTRY(kernfs_fileop)	kf_node;
155 };
156 
157 #define	kf_vop		_kf_opfn._kf_vop
158 #define	kf_xread	_kf_opfn._kf_xread
159 #define	kf_xwrite	_kf_opfn._kf_xwrite
160 
161 typedef struct kern_target kernfs_parentdir_t;
162 typedef struct dyn_kern_target kernfs_entry_t;
163 
164 /*
165  * Functions for adding kernfs datatypes and nodes.
166  */
167 kfstype kernfs_alloctype(int, const struct kernfs_fileop *);
168 #define	KERNFS_ALLOCTYPE(kf) kernfs_alloctype(sizeof((kf)) / \
169 	sizeof((kf)[0]), (kf))
170 #define	KERNFS_ALLOCENTRY(dkt, m_type, m_flags)				\
171 	dkt = (struct dyn_kern_target *)malloc(				\
172 		sizeof(struct dyn_kern_target), (m_type), (m_flags))
173 #define	KERNFS_INITENTRY(dkt, type, name, data, tag, vtype, mode) do {	\
174 	(dkt)->dkt_kt.kt_type = (type);					\
175 	(dkt)->dkt_kt.kt_namlen = strlen((name));			\
176 	(dkt)->dkt_kt.kt_name = (name);					\
177 	(dkt)->dkt_kt.kt_data = (data);					\
178 	(dkt)->dkt_kt.kt_tag = (tag);					\
179 	(dkt)->dkt_kt.kt_vtype = (vtype);				\
180 	(dkt)->dkt_kt.kt_mode = (mode);					\
181 } while (/*CONSTCOND*/0)
182 #define	KERNFS_ENTOPARENTDIR(dkt) &(dkt)->dkt_kt
183 int kernfs_addentry(kernfs_parentdir_t *, kernfs_entry_t *);
184 
185 #ifdef IPSEC
186 __weak_extern(key_freesp)
187 __weak_extern(key_getspbyid)
188 __weak_extern(key_setdumpsa_spi)
189 __weak_extern(key_setdumpsp)
190 __weak_extern(satailq)
191 __weak_extern(sptailq)
192 #endif
193 
194 #endif /* _KERNEL */
195