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