kernfs_vfsops.c revision 1.52.2.2 1 /* $NetBSD: kernfs_vfsops.c,v 1.52.2.2 2004/08/03 10:54:05 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1995
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_vfsops.c 8.10 (Berkeley) 5/14/95
35 */
36
37 /*
38 * Kernel params Filesystem
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.52.2.2 2004/08/03 10:54:05 skrll Exp $");
43
44 #ifdef _KERNEL_OPT
45 #include "opt_compat_netbsd.h"
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 #include <sys/conf.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/dirent.h>
57 #include <sys/malloc.h>
58 #include <sys/syslog.h>
59
60 #include <miscfs/specfs/specdev.h>
61 #include <miscfs/kernfs/kernfs.h>
62
63 MALLOC_DEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures");
64
65 dev_t rrootdev = NODEV;
66
67 void kernfs_init __P((void));
68 void kernfs_reinit __P((void));
69 void kernfs_done __P((void));
70 void kernfs_get_rrootdev __P((void));
71 int kernfs_mount __P((struct mount *, const char *, void *,
72 struct nameidata *, struct lwp *));
73 int kernfs_start __P((struct mount *, int, struct lwp *));
74 int kernfs_unmount __P((struct mount *, int, struct lwp *));
75 int kernfs_statvfs __P((struct mount *, struct statvfs *, struct lwp *));
76 int kernfs_quotactl __P((struct mount *, int, uid_t, void *,
77 struct lwp *));
78 int kernfs_sync __P((struct mount *, int, struct ucred *, struct lwp *));
79 int kernfs_vget __P((struct mount *, ino_t, struct vnode **, struct lwp *));
80 int kernfs_fhtovp __P((struct mount *, struct fid *, struct vnode **,
81 struct lwp *));
82 int kernfs_checkexp __P((struct mount *, struct mbuf *, int *,
83 struct ucred **));
84 int kernfs_vptofh __P((struct vnode *, struct fid *));
85
86 void
87 kernfs_init()
88 {
89 #ifdef _LKM
90 malloc_type_attach(M_KERNFSMNT);
91 #endif
92 kernfs_hashinit();
93 }
94
95 void
96 kernfs_reinit()
97 {
98 kernfs_hashreinit();
99 }
100
101 void
102 kernfs_done()
103 {
104 #ifdef _LKM
105 malloc_type_detach(M_KERNFSMNT);
106 #endif
107 kernfs_hashdone();
108 }
109
110 void
111 kernfs_get_rrootdev()
112 {
113 static int tried = 0;
114
115 if (tried) {
116 /* Already did it once. */
117 return;
118 }
119 tried = 1;
120
121 if (rootdev == NODEV)
122 return;
123 rrootdev = devsw_blk2chr(rootdev);
124 if (rrootdev != NODEV)
125 return;
126 rrootdev = NODEV;
127 printf("kernfs_get_rrootdev: no raw root device\n");
128 }
129
130 /*
131 * Mount the Kernel params filesystem
132 */
133 int
134 kernfs_mount(mp, path, data, ndp, l)
135 struct mount *mp;
136 const char *path;
137 void *data;
138 struct nameidata *ndp;
139 struct lwp *l;
140 {
141 int error = 0;
142 struct kernfs_mount *fmp;
143
144 if (UIO_MX & (UIO_MX - 1)) {
145 log(LOG_ERR, "kernfs: invalid directory entry size");
146 return (EINVAL);
147 }
148
149 if (mp->mnt_flag & MNT_GETARGS)
150 return 0;
151 /*
152 * Update is a no-op
153 */
154 if (mp->mnt_flag & MNT_UPDATE)
155 return (EOPNOTSUPP);
156
157 MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
158 M_KERNFSMNT, M_WAITOK);
159 memset(fmp, 0, sizeof(*fmp));
160 TAILQ_INIT(&fmp->nodelist);
161
162 mp->mnt_data = fmp;
163 mp->mnt_flag |= MNT_LOCAL;
164 vfs_getnewfsid(mp);
165
166 if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
167 UIO_SYSSPACE, mp, l)) != 0) {
168 free(fmp, M_KERNFSMNT);
169 return error;
170 }
171
172 kernfs_get_rrootdev();
173 return 0;
174 }
175
176 int
177 kernfs_start(mp, flags, l)
178 struct mount *mp;
179 int flags;
180 struct lwp *l;
181 {
182
183 return (0);
184 }
185
186 int
187 kernfs_unmount(mp, mntflags, l)
188 struct mount *mp;
189 int mntflags;
190 struct lwp *l;
191 {
192 int error;
193 int flags = 0;
194
195 if (mntflags & MNT_FORCE)
196 flags |= FORCECLOSE;
197
198 if ((error = vflush(mp, 0, flags)) != 0)
199 return (error);
200
201 /*
202 * Finally, throw away the kernfs_mount structure
203 */
204 free(mp->mnt_data, M_KERNFSMNT);
205 mp->mnt_data = NULL;
206 return (0);
207 }
208
209 int
210 kernfs_root(mp, vpp, l)
211 struct mount *mp;
212 struct vnode **vpp;
213 struct lwp *l;
214 {
215
216 /* setup "." */
217 return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0, l));
218 }
219
220 int
221 kernfs_quotactl(mp, cmd, uid, arg, l)
222 struct mount *mp;
223 int cmd;
224 uid_t uid;
225 void *arg;
226 struct lwp *l;
227 {
228
229 return (EOPNOTSUPP);
230 }
231
232 int
233 kernfs_statvfs(mp, sbp, l)
234 struct mount *mp;
235 struct statvfs *sbp;
236 struct lwp *l;
237 {
238
239 sbp->f_bsize = DEV_BSIZE;
240 sbp->f_frsize = DEV_BSIZE;
241 sbp->f_iosize = DEV_BSIZE;
242 sbp->f_blocks = 2; /* 1K to keep df happy */
243 sbp->f_bfree = 0;
244 sbp->f_bavail = 0;
245 sbp->f_bresvd = 0;
246 sbp->f_files = 1024; /* XXX lie */
247 sbp->f_ffree = 128; /* XXX lie */
248 sbp->f_favail = 128; /* XXX lie */
249 sbp->f_fresvd = 0;
250 sbp->f_namemax = MAXNAMLEN;
251 copy_statvfs_info(sbp, mp);
252 return (0);
253 }
254
255 /*ARGSUSED*/
256 int
257 kernfs_sync(mp, waitfor, uc, l)
258 struct mount *mp;
259 int waitfor;
260 struct ucred *uc;
261 struct lwp *l;
262 {
263
264 return (0);
265 }
266
267 /*
268 * Kernfs flat namespace lookup.
269 * Currently unsupported.
270 */
271 int
272 kernfs_vget(mp, ino, vpp, l)
273 struct mount *mp;
274 ino_t ino;
275 struct vnode **vpp;
276 struct lwp *l;
277 {
278
279 return (EOPNOTSUPP);
280 }
281
282 /*ARGSUSED*/
283 int
284 kernfs_fhtovp(mp, fhp, vpp, l)
285 struct mount *mp;
286 struct fid *fhp;
287 struct vnode **vpp;
288 struct lwp *l;
289 {
290
291 return (EOPNOTSUPP);
292 }
293
294 /*ARGSUSED*/
295 int
296 kernfs_checkexp(mp, mb, what, anon)
297 struct mount *mp;
298 struct mbuf *mb;
299 int *what;
300 struct ucred **anon;
301 {
302
303 return (EOPNOTSUPP);
304 }
305
306 /*ARGSUSED*/
307 int
308 kernfs_vptofh(vp, fhp)
309 struct vnode *vp;
310 struct fid *fhp;
311 {
312
313 return (EOPNOTSUPP);
314 }
315
316 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup")
317 {
318
319 sysctl_createv(clog, 0, NULL, NULL,
320 CTLFLAG_PERMANENT,
321 CTLTYPE_NODE, "vfs", NULL,
322 NULL, 0, NULL, 0,
323 CTL_VFS, CTL_EOL);
324 sysctl_createv(clog, 0, NULL, NULL,
325 CTLFLAG_PERMANENT,
326 CTLTYPE_NODE, "kernfs",
327 SYSCTL_DESCR("/kern file system"),
328 NULL, 0, NULL, 0,
329 CTL_VFS, 11, CTL_EOL);
330 /*
331 * XXX the "11" above could be dynamic, thereby eliminating one
332 * more instance of the "number to vfs" mapping problem, but
333 * "11" is the order as taken from sys/mount.h
334 */
335 }
336
337 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
338
339 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
340 &kernfs_vnodeop_opv_desc,
341 NULL,
342 };
343
344 struct vfsops kernfs_vfsops = {
345 MOUNT_KERNFS,
346 kernfs_mount,
347 kernfs_start,
348 kernfs_unmount,
349 kernfs_root,
350 kernfs_quotactl,
351 kernfs_statvfs,
352 kernfs_sync,
353 kernfs_vget,
354 kernfs_fhtovp,
355 kernfs_vptofh,
356 kernfs_init,
357 kernfs_reinit,
358 kernfs_done,
359 NULL,
360 NULL, /* vfs_mountroot */
361 kernfs_checkexp,
362 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
363 kernfs_vnodeopv_descs,
364 };
365