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