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