kernfs_vfsops.c revision 1.25 1 1.25 christos /* $NetBSD: kernfs_vfsops.c,v 1.25 1996/02/09 22:40:22 christos Exp $ */
2 1.18 cgd
3 1.1 cgd /*
4 1.15 mycroft * Copyright (c) 1992, 1993
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.11 cgd * This code is derived from software donated to Berkeley by
8 1.1 cgd * Jan-Simon Pendry.
9 1.1 cgd *
10 1.2 cgd * Redistribution and use in source and binary forms, with or without
11 1.2 cgd * modification, are permitted provided that the following conditions
12 1.2 cgd * are met:
13 1.2 cgd * 1. Redistributions of source code must retain the above copyright
14 1.2 cgd * notice, this list of conditions and the following disclaimer.
15 1.2 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 cgd * notice, this list of conditions and the following disclaimer in the
17 1.2 cgd * documentation and/or other materials provided with the distribution.
18 1.2 cgd * 3. All advertising materials mentioning features or use of this software
19 1.2 cgd * must display the following acknowledgement:
20 1.11 cgd * This product includes software developed by the University of
21 1.11 cgd * California, Berkeley and its contributors.
22 1.2 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.2 cgd * may be used to endorse or promote products derived from this software
24 1.2 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.2 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.2 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.2 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.2 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.2 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.2 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.2 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.2 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.2 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.2 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.2 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.18 cgd * @(#)kernfs_vfsops.c 8.5 (Berkeley) 6/15/94
39 1.1 cgd */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Kernel params Filesystem
43 1.1 cgd */
44 1.1 cgd
45 1.9 mycroft #include <sys/param.h>
46 1.9 mycroft #include <sys/systm.h>
47 1.15 mycroft #include <sys/conf.h>
48 1.9 mycroft #include <sys/types.h>
49 1.9 mycroft #include <sys/proc.h>
50 1.9 mycroft #include <sys/vnode.h>
51 1.9 mycroft #include <sys/mount.h>
52 1.9 mycroft #include <sys/namei.h>
53 1.9 mycroft #include <sys/malloc.h>
54 1.25 christos #include <sys/cpu.h>
55 1.15 mycroft
56 1.15 mycroft #include <miscfs/specfs/specdev.h>
57 1.9 mycroft #include <miscfs/kernfs/kernfs.h>
58 1.1 cgd
59 1.15 mycroft dev_t rrootdev = NODEV;
60 1.15 mycroft
61 1.25 christos void kernfs_init __P((void));
62 1.25 christos void kernfs_get_rrootdev __P((void));
63 1.25 christos int kernfs_mount __P((struct mount *, char *, caddr_t, struct nameidata *,
64 1.25 christos struct proc *));
65 1.25 christos int kernfs_start __P((struct mount *, int, struct proc *));
66 1.25 christos int kernfs_unmount __P((struct mount *, int, struct proc *));
67 1.25 christos int kernfs_root __P((struct mount *, struct vnode **));
68 1.25 christos int kernfs_statfs __P((struct mount *, struct statfs *, struct proc *));
69 1.25 christos int kernfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
70 1.25 christos struct proc *));
71 1.25 christos int kernfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
72 1.25 christos int kernfs_vget __P((struct mount *, ino_t, struct vnode **));
73 1.25 christos int kernfs_fhtovp __P((struct mount *, struct fid *, struct mbuf *,
74 1.25 christos struct vnode **, int *, struct ucred **));
75 1.25 christos int kernfs_vptofh __P((struct vnode *, struct fid *));
76 1.25 christos
77 1.25 christos /*ARGSUSED*/
78 1.25 christos void
79 1.1 cgd kernfs_init()
80 1.1 cgd {
81 1.10 cgd }
82 1.10 cgd
83 1.11 cgd void
84 1.15 mycroft kernfs_get_rrootdev()
85 1.10 cgd {
86 1.16 mycroft static int tried = 0;
87 1.16 mycroft int cmaj;
88 1.10 cgd
89 1.16 mycroft if (tried) {
90 1.15 mycroft /* Already did it once. */
91 1.15 mycroft return;
92 1.15 mycroft }
93 1.16 mycroft tried = 1;
94 1.16 mycroft
95 1.16 mycroft if (rootdev == NODEV)
96 1.16 mycroft return;
97 1.10 cgd for (cmaj = 0; cmaj < nchrdev; cmaj++) {
98 1.16 mycroft rrootdev = makedev(cmaj, minor(rootdev));
99 1.16 mycroft if (chrtoblk(rrootdev) == rootdev)
100 1.15 mycroft return;
101 1.10 cgd }
102 1.16 mycroft rrootdev = NODEV;
103 1.15 mycroft printf("kernfs_get_rrootdev: no raw root device\n");
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd /*
107 1.15 mycroft * Mount the Kernel params filesystem
108 1.1 cgd */
109 1.25 christos int
110 1.1 cgd kernfs_mount(mp, path, data, ndp, p)
111 1.1 cgd struct mount *mp;
112 1.1 cgd char *path;
113 1.1 cgd caddr_t data;
114 1.1 cgd struct nameidata *ndp;
115 1.1 cgd struct proc *p;
116 1.1 cgd {
117 1.1 cgd int error = 0;
118 1.23 mycroft size_t size;
119 1.1 cgd struct kernfs_mount *fmp;
120 1.1 cgd struct vnode *rvp;
121 1.1 cgd
122 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
123 1.1 cgd printf("kernfs_mount(mp = %x)\n", mp);
124 1.1 cgd #endif
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * Update is a no-op
128 1.1 cgd */
129 1.1 cgd if (mp->mnt_flag & MNT_UPDATE)
130 1.1 cgd return (EOPNOTSUPP);
131 1.1 cgd
132 1.25 christos error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);
133 1.25 christos if (error)
134 1.1 cgd return (error);
135 1.1 cgd
136 1.15 mycroft MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
137 1.15 mycroft M_MISCFSMNT, M_WAITOK);
138 1.1 cgd rvp->v_type = VDIR;
139 1.1 cgd rvp->v_flag |= VROOT;
140 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
141 1.1 cgd printf("kernfs_mount: root vp = %x\n", rvp);
142 1.1 cgd #endif
143 1.1 cgd fmp->kf_root = rvp;
144 1.1 cgd mp->mnt_flag |= MNT_LOCAL;
145 1.15 mycroft mp->mnt_data = (qaddr_t)fmp;
146 1.14 cgd getnewfsid(mp, makefstype(MOUNT_KERNFS));
147 1.1 cgd
148 1.1 cgd (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
149 1.1 cgd bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
150 1.1 cgd bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
151 1.1 cgd bcopy("kernfs", mp->mnt_stat.f_mntfromname, sizeof("kernfs"));
152 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
153 1.1 cgd printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
154 1.1 cgd #endif
155 1.10 cgd
156 1.15 mycroft kernfs_get_rrootdev();
157 1.1 cgd return (0);
158 1.1 cgd }
159 1.1 cgd
160 1.25 christos int
161 1.1 cgd kernfs_start(mp, flags, p)
162 1.1 cgd struct mount *mp;
163 1.1 cgd int flags;
164 1.1 cgd struct proc *p;
165 1.1 cgd {
166 1.15 mycroft
167 1.1 cgd return (0);
168 1.1 cgd }
169 1.1 cgd
170 1.25 christos int
171 1.1 cgd kernfs_unmount(mp, mntflags, p)
172 1.1 cgd struct mount *mp;
173 1.1 cgd int mntflags;
174 1.1 cgd struct proc *p;
175 1.1 cgd {
176 1.1 cgd int error;
177 1.1 cgd int flags = 0;
178 1.1 cgd extern int doforce;
179 1.1 cgd struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
180 1.1 cgd
181 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
182 1.1 cgd printf("kernfs_unmount(mp = %x)\n", mp);
183 1.1 cgd #endif
184 1.1 cgd
185 1.1 cgd if (mntflags & MNT_FORCE) {
186 1.1 cgd /* kernfs can never be rootfs so don't check for it */
187 1.1 cgd if (!doforce)
188 1.1 cgd return (EINVAL);
189 1.1 cgd flags |= FORCECLOSE;
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd /*
193 1.1 cgd * Clear out buffer cache. I don't think we
194 1.1 cgd * ever get anything cached at this level at the
195 1.1 cgd * moment, but who knows...
196 1.1 cgd */
197 1.1 cgd if (rootvp->v_usecount > 1)
198 1.1 cgd return (EBUSY);
199 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
200 1.1 cgd printf("kernfs_unmount: calling vflush\n");
201 1.1 cgd #endif
202 1.25 christos if ((error = vflush(mp, rootvp, flags)) != 0)
203 1.1 cgd return (error);
204 1.1 cgd
205 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
206 1.1 cgd vprint("kernfs root", rootvp);
207 1.15 mycroft #endif
208 1.1 cgd /*
209 1.15 mycroft * Clean out the old root vnode for reuse.
210 1.1 cgd */
211 1.1 cgd vrele(rootvp);
212 1.1 cgd vgone(rootvp);
213 1.1 cgd /*
214 1.1 cgd * Finally, throw away the kernfs_mount structure
215 1.1 cgd */
216 1.6 cgd free(mp->mnt_data, M_MISCFSMNT);
217 1.1 cgd mp->mnt_data = 0;
218 1.15 mycroft return (0);
219 1.1 cgd }
220 1.1 cgd
221 1.25 christos int
222 1.1 cgd kernfs_root(mp, vpp)
223 1.1 cgd struct mount *mp;
224 1.1 cgd struct vnode **vpp;
225 1.1 cgd {
226 1.1 cgd struct vnode *vp;
227 1.1 cgd
228 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
229 1.1 cgd printf("kernfs_root(mp = %x)\n", mp);
230 1.1 cgd #endif
231 1.1 cgd
232 1.1 cgd /*
233 1.1 cgd * Return locked reference to root.
234 1.1 cgd */
235 1.1 cgd vp = VFSTOKERNFS(mp)->kf_root;
236 1.1 cgd VREF(vp);
237 1.1 cgd VOP_LOCK(vp);
238 1.1 cgd *vpp = vp;
239 1.1 cgd return (0);
240 1.1 cgd }
241 1.1 cgd
242 1.25 christos int
243 1.1 cgd kernfs_quotactl(mp, cmd, uid, arg, p)
244 1.1 cgd struct mount *mp;
245 1.1 cgd int cmd;
246 1.1 cgd uid_t uid;
247 1.1 cgd caddr_t arg;
248 1.1 cgd struct proc *p;
249 1.1 cgd {
250 1.15 mycroft
251 1.1 cgd return (EOPNOTSUPP);
252 1.1 cgd }
253 1.1 cgd
254 1.25 christos int
255 1.1 cgd kernfs_statfs(mp, sbp, p)
256 1.1 cgd struct mount *mp;
257 1.1 cgd struct statfs *sbp;
258 1.1 cgd struct proc *p;
259 1.1 cgd {
260 1.1 cgd
261 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
262 1.1 cgd printf("kernfs_statfs(mp = %x)\n", mp);
263 1.1 cgd #endif
264 1.1 cgd
265 1.12 cgd #ifdef COMPAT_09
266 1.12 cgd sbp->f_type = 7;
267 1.12 cgd #else
268 1.12 cgd sbp->f_type = 0;
269 1.12 cgd #endif
270 1.1 cgd sbp->f_bsize = DEV_BSIZE;
271 1.13 cgd sbp->f_iosize = DEV_BSIZE;
272 1.1 cgd sbp->f_blocks = 2; /* 1K to keep df happy */
273 1.1 cgd sbp->f_bfree = 0;
274 1.1 cgd sbp->f_bavail = 0;
275 1.15 mycroft sbp->f_files = 0;
276 1.15 mycroft sbp->f_ffree = 0;
277 1.1 cgd if (sbp != &mp->mnt_stat) {
278 1.1 cgd bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
279 1.1 cgd bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
280 1.1 cgd bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
281 1.1 cgd }
282 1.21 mycroft strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
283 1.1 cgd return (0);
284 1.1 cgd }
285 1.1 cgd
286 1.25 christos /*ARGSUSED*/
287 1.25 christos int
288 1.25 christos kernfs_sync(mp, waitfor, uc, p)
289 1.1 cgd struct mount *mp;
290 1.1 cgd int waitfor;
291 1.25 christos struct ucred *uc;
292 1.25 christos struct proc *p;
293 1.1 cgd {
294 1.15 mycroft
295 1.1 cgd return (0);
296 1.1 cgd }
297 1.1 cgd
298 1.15 mycroft /*
299 1.15 mycroft * Kernfs flat namespace lookup.
300 1.15 mycroft * Currently unsupported.
301 1.15 mycroft */
302 1.25 christos int
303 1.15 mycroft kernfs_vget(mp, ino, vpp)
304 1.15 mycroft struct mount *mp;
305 1.15 mycroft ino_t ino;
306 1.15 mycroft struct vnode **vpp;
307 1.15 mycroft {
308 1.15 mycroft
309 1.15 mycroft return (EOPNOTSUPP);
310 1.15 mycroft }
311 1.15 mycroft
312 1.25 christos /*ARGSUSED*/
313 1.25 christos int
314 1.25 christos kernfs_fhtovp(mp, fhp, mb, vpp, what, anon)
315 1.1 cgd struct mount *mp;
316 1.1 cgd struct fid *fhp;
317 1.25 christos struct mbuf *mb;
318 1.1 cgd struct vnode **vpp;
319 1.25 christos int *what;
320 1.25 christos struct ucred **anon;
321 1.1 cgd {
322 1.15 mycroft
323 1.1 cgd return (EOPNOTSUPP);
324 1.1 cgd }
325 1.1 cgd
326 1.25 christos /*ARGSUSED*/
327 1.25 christos int
328 1.1 cgd kernfs_vptofh(vp, fhp)
329 1.1 cgd struct vnode *vp;
330 1.1 cgd struct fid *fhp;
331 1.1 cgd {
332 1.15 mycroft
333 1.1 cgd return (EOPNOTSUPP);
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd struct vfsops kernfs_vfsops = {
337 1.12 cgd MOUNT_KERNFS,
338 1.1 cgd kernfs_mount,
339 1.1 cgd kernfs_start,
340 1.1 cgd kernfs_unmount,
341 1.1 cgd kernfs_root,
342 1.1 cgd kernfs_quotactl,
343 1.1 cgd kernfs_statfs,
344 1.1 cgd kernfs_sync,
345 1.15 mycroft kernfs_vget,
346 1.1 cgd kernfs_fhtovp,
347 1.1 cgd kernfs_vptofh,
348 1.1 cgd kernfs_init,
349 1.1 cgd };
350