xref: /netbsd-src/sys/miscfs/kernfs/kernfs_vfsops.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: kernfs_vfsops.c,v 1.39 2001/02/04 10:20:00 mrg 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_done __P((void));
66 void	kernfs_get_rrootdev __P((void));
67 int	kernfs_mount __P((struct mount *, const char *, void *,
68 	    struct nameidata *, struct proc *));
69 int	kernfs_start __P((struct mount *, int, struct proc *));
70 int	kernfs_unmount __P((struct mount *, int, struct proc *));
71 int	kernfs_root __P((struct mount *, struct vnode **));
72 int	kernfs_statfs __P((struct mount *, struct statfs *, struct proc *));
73 int	kernfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
74 			     struct proc *));
75 int	kernfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
76 int	kernfs_vget __P((struct mount *, ino_t, struct vnode **));
77 int	kernfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
78 int	kernfs_checkexp __P((struct mount *, struct mbuf *, int *,
79 			   struct ucred **));
80 int	kernfs_vptofh __P((struct vnode *, struct fid *));
81 int	kernfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
82 			   struct proc *));
83 
84 void
85 kernfs_init()
86 {
87 }
88 
89 void
90 kernfs_done()
91 {
92 }
93 
94 void
95 kernfs_get_rrootdev()
96 {
97 	static int tried = 0;
98 	int cmaj;
99 
100 	if (tried) {
101 		/* Already did it once. */
102 		return;
103 	}
104 	tried = 1;
105 
106 	if (rootdev == NODEV)
107 		return;
108 	for (cmaj = 0; cmaj < nchrdev; cmaj++) {
109 		rrootdev = makedev(cmaj, minor(rootdev));
110 		if (chrtoblk(rrootdev) == rootdev) {
111 #ifdef KERNFS_DIAGNOSTIC
112 	printf("kernfs_mount: rootdev = %u.%u; rrootdev = %u.%u\n",
113 	    major(rootdev), minor(rootdev), major(rrootdev), minor(rrootdev));
114 #endif
115 			return;
116 		}
117 	}
118 	rrootdev = NODEV;
119 	printf("kernfs_get_rrootdev: no raw root device\n");
120 }
121 
122 /*
123  * Mount the Kernel params filesystem
124  */
125 int
126 kernfs_mount(mp, path, data, ndp, p)
127 	struct mount *mp;
128 	const char *path;
129 	void *data;
130 	struct nameidata *ndp;
131 	struct proc *p;
132 {
133 	int error = 0;
134 	size_t size;
135 	struct kernfs_mount *fmp;
136 	struct vnode *rvp;
137 
138 #ifdef KERNFS_DIAGNOSTIC
139 	printf("kernfs_mount(mp = %p)\n", mp);
140 #endif
141 
142 	/*
143 	 * Update is a no-op
144 	 */
145 	if (mp->mnt_flag & MNT_UPDATE)
146 		return (EOPNOTSUPP);
147 
148 	error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);
149 	if (error)
150 		return (error);
151 
152 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
153 	    M_MISCFSMNT, M_WAITOK);
154 	rvp->v_type = VDIR;
155 	rvp->v_flag |= VROOT;
156 #ifdef KERNFS_DIAGNOSTIC
157 	printf("kernfs_mount: root vp = %p\n", rvp);
158 #endif
159 	fmp->kf_root = rvp;
160 	mp->mnt_flag |= MNT_LOCAL;
161 	mp->mnt_data = (qaddr_t)fmp;
162 	vfs_getnewfsid(mp);
163 
164 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
165 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
166 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
167 	memcpy(mp->mnt_stat.f_mntfromname, "kernfs", sizeof("kernfs"));
168 #ifdef KERNFS_DIAGNOSTIC
169 	printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
170 #endif
171 
172 	kernfs_get_rrootdev();
173 	return (0);
174 }
175 
176 int
177 kernfs_start(mp, flags, p)
178 	struct mount *mp;
179 	int flags;
180 	struct proc *p;
181 {
182 
183 	return (0);
184 }
185 
186 int
187 kernfs_unmount(mp, mntflags, p)
188 	struct mount *mp;
189 	int mntflags;
190 	struct proc *p;
191 {
192 	int error;
193 	int flags = 0;
194 	struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
195 
196 #ifdef KERNFS_DIAGNOSTIC
197 	printf("kernfs_unmount(mp = %p)\n", mp);
198 #endif
199 
200 	 if (mntflags & MNT_FORCE)
201 		flags |= FORCECLOSE;
202 
203 	/*
204 	 * Clear out buffer cache.  I don't think we
205 	 * ever get anything cached at this level at the
206 	 * moment, but who knows...
207 	 */
208 	if (rootvp->v_usecount > 1)
209 		return (EBUSY);
210 #ifdef KERNFS_DIAGNOSTIC
211 	printf("kernfs_unmount: calling vflush\n");
212 #endif
213 	if ((error = vflush(mp, rootvp, flags)) != 0)
214 		return (error);
215 
216 #ifdef KERNFS_DIAGNOSTIC
217 	vprint("kernfs root", rootvp);
218 #endif
219 	/*
220 	 * Clean out the old root vnode for reuse.
221 	 */
222 	vrele(rootvp);
223 	vgone(rootvp);
224 	/*
225 	 * Finally, throw away the kernfs_mount structure
226 	 */
227 	free(mp->mnt_data, M_MISCFSMNT);
228 	mp->mnt_data = 0;
229 	return (0);
230 }
231 
232 int
233 kernfs_root(mp, vpp)
234 	struct mount *mp;
235 	struct vnode **vpp;
236 {
237 	struct vnode *vp;
238 
239 #ifdef KERNFS_DIAGNOSTIC
240 	printf("kernfs_root(mp = %p)\n", mp);
241 #endif
242 
243 	/*
244 	 * Return locked reference to root.
245 	 */
246 	vp = VFSTOKERNFS(mp)->kf_root;
247 	VREF(vp);
248 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
249 	*vpp = vp;
250 	return (0);
251 }
252 
253 int
254 kernfs_quotactl(mp, cmd, uid, arg, p)
255 	struct mount *mp;
256 	int cmd;
257 	uid_t uid;
258 	caddr_t arg;
259 	struct proc *p;
260 {
261 
262 	return (EOPNOTSUPP);
263 }
264 
265 int
266 kernfs_statfs(mp, sbp, p)
267 	struct mount *mp;
268 	struct statfs *sbp;
269 	struct proc *p;
270 {
271 
272 #ifdef KERNFS_DIAGNOSTIC
273 	printf("kernfs_statfs(mp = %p)\n", mp);
274 #endif
275 
276 	sbp->f_bsize = DEV_BSIZE;
277 	sbp->f_iosize = DEV_BSIZE;
278 	sbp->f_blocks = 2;		/* 1K to keep df happy */
279 	sbp->f_bfree = 0;
280 	sbp->f_bavail = 0;
281 	sbp->f_files = 0;
282 	sbp->f_ffree = 0;
283 #ifdef COMPAT_09
284 	sbp->f_type = 7;
285 #else
286 	sbp->f_type = 0;
287 #endif
288 	if (sbp != &mp->mnt_stat) {
289 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
290 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
291 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
292 	}
293 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
294 	return (0);
295 }
296 
297 /*ARGSUSED*/
298 int
299 kernfs_sync(mp, waitfor, uc, p)
300 	struct mount *mp;
301 	int waitfor;
302 	struct ucred *uc;
303 	struct proc *p;
304 {
305 
306 	return (0);
307 }
308 
309 /*
310  * Kernfs flat namespace lookup.
311  * Currently unsupported.
312  */
313 int
314 kernfs_vget(mp, ino, vpp)
315 	struct mount *mp;
316 	ino_t ino;
317 	struct vnode **vpp;
318 {
319 
320 	return (EOPNOTSUPP);
321 }
322 
323 /*ARGSUSED*/
324 int
325 kernfs_fhtovp(mp, fhp, vpp)
326 	struct mount *mp;
327 	struct fid *fhp;
328 	struct vnode **vpp;
329 {
330 
331 	return (EOPNOTSUPP);
332 }
333 
334 /*ARGSUSED*/
335 int
336 kernfs_checkexp(mp, mb, what, anon)
337 	struct mount *mp;
338 	struct mbuf *mb;
339 	int *what;
340 	struct ucred **anon;
341 {
342 
343 	return (EOPNOTSUPP);
344 }
345 
346 /*ARGSUSED*/
347 int
348 kernfs_vptofh(vp, fhp)
349 	struct vnode *vp;
350 	struct fid *fhp;
351 {
352 
353 	return (EOPNOTSUPP);
354 }
355 
356 int
357 kernfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
358 	int *name;
359 	u_int namelen;
360 	void *oldp;
361 	size_t *oldlenp;
362 	void *newp;
363 	size_t newlen;
364 	struct proc *p;
365 {
366 	return (EOPNOTSUPP);
367 }
368 
369 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
370 
371 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
372 	&kernfs_vnodeop_opv_desc,
373 	NULL,
374 };
375 
376 struct vfsops kernfs_vfsops = {
377 	MOUNT_KERNFS,
378 	kernfs_mount,
379 	kernfs_start,
380 	kernfs_unmount,
381 	kernfs_root,
382 	kernfs_quotactl,
383 	kernfs_statfs,
384 	kernfs_sync,
385 	kernfs_vget,
386 	kernfs_fhtovp,
387 	kernfs_vptofh,
388 	kernfs_init,
389 	kernfs_done,
390 	kernfs_sysctl,
391 	NULL,				/* vfs_mountroot */
392 	kernfs_checkexp,
393 	kernfs_vnodeopv_descs,
394 };
395