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