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