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