xref: /netbsd-src/sys/miscfs/kernfs/kernfs_vfsops.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: kernfs_vfsops.c,v 1.23 1995/03/09 12:05:52 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
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.5 (Berkeley) 6/15/94
39  */
40 
41 /*
42  * Kernel params Filesystem
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/types.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
54 
55 #include <miscfs/specfs/specdev.h>
56 #include <miscfs/kernfs/kernfs.h>
57 
58 dev_t rrootdev = NODEV;
59 
60 kernfs_init()
61 {
62 
63 }
64 
65 void
66 kernfs_get_rrootdev()
67 {
68 	static int tried = 0;
69 	int cmaj;
70 
71 	if (tried) {
72 		/* Already did it once. */
73 		return;
74 	}
75 	tried = 1;
76 
77 	if (rootdev == NODEV)
78 		return;
79 	for (cmaj = 0; cmaj < nchrdev; cmaj++) {
80 		rrootdev = makedev(cmaj, minor(rootdev));
81 		if (chrtoblk(rrootdev) == rootdev)
82 			return;
83 	}
84 	rrootdev = NODEV;
85 	printf("kernfs_get_rrootdev: no raw root device\n");
86 }
87 
88 /*
89  * Mount the Kernel params filesystem
90  */
91 kernfs_mount(mp, path, data, ndp, p)
92 	struct mount *mp;
93 	char *path;
94 	caddr_t data;
95 	struct nameidata *ndp;
96 	struct proc *p;
97 {
98 	int error = 0;
99 	size_t size;
100 	struct kernfs_mount *fmp;
101 	struct vnode *rvp;
102 
103 #ifdef KERNFS_DIAGNOSTIC
104 	printf("kernfs_mount(mp = %x)\n", mp);
105 #endif
106 
107 	/*
108 	 * Update is a no-op
109 	 */
110 	if (mp->mnt_flag & MNT_UPDATE)
111 		return (EOPNOTSUPP);
112 
113 	if (error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp))
114 		return (error);
115 
116 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
117 	    M_MISCFSMNT, M_WAITOK);
118 	rvp->v_type = VDIR;
119 	rvp->v_flag |= VROOT;
120 #ifdef KERNFS_DIAGNOSTIC
121 	printf("kernfs_mount: root vp = %x\n", rvp);
122 #endif
123 	fmp->kf_root = rvp;
124 	mp->mnt_flag |= MNT_LOCAL;
125 	mp->mnt_data = (qaddr_t)fmp;
126 	getnewfsid(mp, makefstype(MOUNT_KERNFS));
127 
128 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
129 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
130 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
131 	bcopy("kernfs", mp->mnt_stat.f_mntfromname, sizeof("kernfs"));
132 #ifdef KERNFS_DIAGNOSTIC
133 	printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
134 #endif
135 
136 	kernfs_get_rrootdev();
137 	return (0);
138 }
139 
140 kernfs_start(mp, flags, p)
141 	struct mount *mp;
142 	int flags;
143 	struct proc *p;
144 {
145 
146 	return (0);
147 }
148 
149 kernfs_unmount(mp, mntflags, p)
150 	struct mount *mp;
151 	int mntflags;
152 	struct proc *p;
153 {
154 	int error;
155 	int flags = 0;
156 	extern int doforce;
157 	struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
158 
159 #ifdef KERNFS_DIAGNOSTIC
160 	printf("kernfs_unmount(mp = %x)\n", mp);
161 #endif
162 
163 	if (mntflags & MNT_FORCE) {
164 		/* kernfs can never be rootfs so don't check for it */
165 		if (!doforce)
166 			return (EINVAL);
167 		flags |= FORCECLOSE;
168 	}
169 
170 	/*
171 	 * Clear out buffer cache.  I don't think we
172 	 * ever get anything cached at this level at the
173 	 * moment, but who knows...
174 	 */
175 	if (rootvp->v_usecount > 1)
176 		return (EBUSY);
177 #ifdef KERNFS_DIAGNOSTIC
178 	printf("kernfs_unmount: calling vflush\n");
179 #endif
180 	if (error = vflush(mp, rootvp, flags))
181 		return (error);
182 
183 #ifdef KERNFS_DIAGNOSTIC
184 	vprint("kernfs root", rootvp);
185 #endif
186 	/*
187 	 * Clean out the old root vnode for reuse.
188 	 */
189 	vrele(rootvp);
190 	vgone(rootvp);
191 	/*
192 	 * Finally, throw away the kernfs_mount structure
193 	 */
194 	free(mp->mnt_data, M_MISCFSMNT);
195 	mp->mnt_data = 0;
196 	return (0);
197 }
198 
199 kernfs_root(mp, vpp)
200 	struct mount *mp;
201 	struct vnode **vpp;
202 {
203 	struct vnode *vp;
204 
205 #ifdef KERNFS_DIAGNOSTIC
206 	printf("kernfs_root(mp = %x)\n", mp);
207 #endif
208 
209 	/*
210 	 * Return locked reference to root.
211 	 */
212 	vp = VFSTOKERNFS(mp)->kf_root;
213 	VREF(vp);
214 	VOP_LOCK(vp);
215 	*vpp = vp;
216 	return (0);
217 }
218 
219 kernfs_quotactl(mp, cmd, uid, arg, p)
220 	struct mount *mp;
221 	int cmd;
222 	uid_t uid;
223 	caddr_t arg;
224 	struct proc *p;
225 {
226 
227 	return (EOPNOTSUPP);
228 }
229 
230 kernfs_statfs(mp, sbp, p)
231 	struct mount *mp;
232 	struct statfs *sbp;
233 	struct proc *p;
234 {
235 
236 #ifdef KERNFS_DIAGNOSTIC
237 	printf("kernfs_statfs(mp = %x)\n", mp);
238 #endif
239 
240 #ifdef COMPAT_09
241 	sbp->f_type = 7;
242 #else
243 	sbp->f_type = 0;
244 #endif
245 	sbp->f_bsize = DEV_BSIZE;
246 	sbp->f_iosize = DEV_BSIZE;
247 	sbp->f_blocks = 2;		/* 1K to keep df happy */
248 	sbp->f_bfree = 0;
249 	sbp->f_bavail = 0;
250 	sbp->f_files = 0;
251 	sbp->f_ffree = 0;
252 	if (sbp != &mp->mnt_stat) {
253 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
254 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
255 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
256 	}
257 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
258 	sbp->f_fstypename[MFSNAMELEN] = '\0';
259 	return (0);
260 }
261 
262 kernfs_sync(mp, waitfor)
263 	struct mount *mp;
264 	int waitfor;
265 {
266 
267 	return (0);
268 }
269 
270 /*
271  * Kernfs flat namespace lookup.
272  * Currently unsupported.
273  */
274 kernfs_vget(mp, ino, vpp)
275 	struct mount *mp;
276 	ino_t ino;
277 	struct vnode **vpp;
278 {
279 
280 	return (EOPNOTSUPP);
281 }
282 
283 
284 kernfs_fhtovp(mp, fhp, setgen, vpp)
285 	struct mount *mp;
286 	struct fid *fhp;
287 	int setgen;
288 	struct vnode **vpp;
289 {
290 
291 	return (EOPNOTSUPP);
292 }
293 
294 kernfs_vptofh(vp, fhp)
295 	struct vnode *vp;
296 	struct fid *fhp;
297 {
298 
299 	return (EOPNOTSUPP);
300 }
301 
302 struct vfsops kernfs_vfsops = {
303 	MOUNT_KERNFS,
304 	kernfs_mount,
305 	kernfs_start,
306 	kernfs_unmount,
307 	kernfs_root,
308 	kernfs_quotactl,
309 	kernfs_statfs,
310 	kernfs_sync,
311 	kernfs_vget,
312 	kernfs_fhtovp,
313 	kernfs_vptofh,
314 	kernfs_init,
315 };
316