xref: /netbsd-src/sys/miscfs/kernfs/kernfs_vfsops.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: kernfs_vfsops.c,v 1.34 1998/08/09 20:51:08 perry 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. 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.10 (Berkeley) 5/14/95
39  */
40 
41 /*
42  * Kernel params Filesystem
43  */
44 
45 #if defined(_KERNEL) && !defined(_LKM)
46 #include "opt_compat_netbsd.h"
47 #endif
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/conf.h>
52 #include <sys/types.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/malloc.h>
58 
59 #include <miscfs/specfs/specdev.h>
60 #include <miscfs/kernfs/kernfs.h>
61 
62 dev_t rrootdev = NODEV;
63 
64 void	kernfs_init __P((void));
65 void	kernfs_get_rrootdev __P((void));
66 int	kernfs_mount __P((struct mount *, const char *, void *,
67 	    struct nameidata *, struct proc *));
68 int	kernfs_start __P((struct mount *, int, struct proc *));
69 int	kernfs_unmount __P((struct mount *, int, struct proc *));
70 int	kernfs_root __P((struct mount *, struct vnode **));
71 int	kernfs_statfs __P((struct mount *, struct statfs *, struct proc *));
72 int	kernfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
73 			     struct proc *));
74 int	kernfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
75 int	kernfs_vget __P((struct mount *, ino_t, struct vnode **));
76 int	kernfs_fhtovp __P((struct mount *, struct fid *, struct mbuf *,
77 			   struct vnode **, int *, struct ucred **));
78 int	kernfs_vptofh __P((struct vnode *, struct fid *));
79 int	kernfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
80 			   struct proc *));
81 
82 void
83 kernfs_init()
84 {
85 }
86 
87 void
88 kernfs_get_rrootdev()
89 {
90 	static int tried = 0;
91 	int cmaj;
92 
93 	if (tried) {
94 		/* Already did it once. */
95 		return;
96 	}
97 	tried = 1;
98 
99 	if (rootdev == NODEV)
100 		return;
101 	for (cmaj = 0; cmaj < nchrdev; cmaj++) {
102 		rrootdev = makedev(cmaj, minor(rootdev));
103 		if (chrtoblk(rrootdev) == rootdev)
104 			return;
105 	}
106 	rrootdev = NODEV;
107 	printf("kernfs_get_rrootdev: no raw root device\n");
108 }
109 
110 /*
111  * Mount the Kernel params filesystem
112  */
113 int
114 kernfs_mount(mp, path, data, ndp, p)
115 	struct mount *mp;
116 	const char *path;
117 	void *data;
118 	struct nameidata *ndp;
119 	struct proc *p;
120 {
121 	int error = 0;
122 	size_t size;
123 	struct kernfs_mount *fmp;
124 	struct vnode *rvp;
125 
126 #ifdef KERNFS_DIAGNOSTIC
127 	printf("kernfs_mount(mp = %p)\n", mp);
128 #endif
129 
130 	/*
131 	 * Update is a no-op
132 	 */
133 	if (mp->mnt_flag & MNT_UPDATE)
134 		return (EOPNOTSUPP);
135 
136 	error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);
137 	if (error)
138 		return (error);
139 
140 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
141 	    M_MISCFSMNT, M_WAITOK);
142 	rvp->v_type = VDIR;
143 	rvp->v_flag |= VROOT;
144 #ifdef KERNFS_DIAGNOSTIC
145 	printf("kernfs_mount: root vp = %p\n", rvp);
146 #endif
147 	fmp->kf_root = rvp;
148 	mp->mnt_flag |= MNT_LOCAL;
149 	mp->mnt_data = (qaddr_t)fmp;
150 	vfs_getnewfsid(mp, MOUNT_KERNFS);
151 
152 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
153 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
154 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
155 	memcpy(mp->mnt_stat.f_mntfromname, "kernfs", sizeof("kernfs"));
156 #ifdef KERNFS_DIAGNOSTIC
157 	printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
158 #endif
159 
160 	kernfs_get_rrootdev();
161 	return (0);
162 }
163 
164 int
165 kernfs_start(mp, flags, p)
166 	struct mount *mp;
167 	int flags;
168 	struct proc *p;
169 {
170 
171 	return (0);
172 }
173 
174 int
175 kernfs_unmount(mp, mntflags, p)
176 	struct mount *mp;
177 	int mntflags;
178 	struct proc *p;
179 {
180 	int error;
181 	int flags = 0;
182 	struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
183 
184 #ifdef KERNFS_DIAGNOSTIC
185 	printf("kernfs_unmount(mp = %p)\n", mp);
186 #endif
187 
188 	 if (mntflags & MNT_FORCE)
189 		flags |= FORCECLOSE;
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 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
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 	sbp->f_bsize = DEV_BSIZE;
265 	sbp->f_iosize = DEV_BSIZE;
266 	sbp->f_blocks = 2;		/* 1K to keep df happy */
267 	sbp->f_bfree = 0;
268 	sbp->f_bavail = 0;
269 	sbp->f_files = 0;
270 	sbp->f_ffree = 0;
271 #ifdef COMPAT_09
272 	sbp->f_type = 7;
273 #else
274 	sbp->f_type = 0;
275 #endif
276 	if (sbp != &mp->mnt_stat) {
277 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
278 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
279 		memcpy(sbp->f_mntfromname, mp->mnt_stat.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 int
336 kernfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
337 	int *name;
338 	u_int namelen;
339 	void *oldp;
340 	size_t *oldlenp;
341 	void *newp;
342 	size_t newlen;
343 	struct proc *p;
344 {
345 	return (EOPNOTSUPP);
346 }
347 
348 extern struct vnodeopv_desc kernfs_vnodeop_opv_desc;
349 
350 struct vnodeopv_desc *kernfs_vnodeopv_descs[] = {
351 	&kernfs_vnodeop_opv_desc,
352 	NULL,
353 };
354 
355 struct vfsops kernfs_vfsops = {
356 	MOUNT_KERNFS,
357 	kernfs_mount,
358 	kernfs_start,
359 	kernfs_unmount,
360 	kernfs_root,
361 	kernfs_quotactl,
362 	kernfs_statfs,
363 	kernfs_sync,
364 	kernfs_vget,
365 	kernfs_fhtovp,
366 	kernfs_vptofh,
367 	kernfs_init,
368 	kernfs_sysctl,
369 	NULL,				/* vfs_mountroot */
370 	kernfs_vnodeopv_descs,
371 };
372