xref: /netbsd-src/sys/miscfs/kernfs/kernfs_vfsops.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: kernfs_vfsops.c,v 1.70 2005/12/11 12:24:51 christos 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
35  */
36 
37 /*
38  * Kernel params Filesystem
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.70 2005/12/11 12:24:51 christos Exp $");
43 
44 #ifdef _KERNEL_OPT
45 #include "opt_compat_netbsd.h"
46 #endif
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 #include <sys/conf.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/dirent.h>
57 #include <sys/malloc.h>
58 #include <sys/syslog.h>
59 
60 #include <miscfs/specfs/specdev.h>
61 #include <miscfs/kernfs/kernfs.h>
62 
63 MALLOC_DEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures");
64 
65 dev_t rrootdev = NODEV;
66 
67 void	kernfs_init(void);
68 void	kernfs_reinit(void);
69 void	kernfs_done(void);
70 void	kernfs_get_rrootdev(void);
71 int	kernfs_mount(struct mount *, const char *, void *,
72 	    struct nameidata *, struct lwp *);
73 int	kernfs_start(struct mount *, int, struct lwp *);
74 int	kernfs_unmount(struct mount *, int, struct lwp *);
75 int	kernfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
76 int	kernfs_quotactl(struct mount *, int, uid_t, void *,
77 			     struct lwp *);
78 int	kernfs_sync(struct mount *, int, struct ucred *, struct lwp *);
79 int	kernfs_vget(struct mount *, ino_t, struct vnode **);
80 
81 void
82 kernfs_init()
83 {
84 #ifdef _LKM
85 	malloc_type_attach(M_KERNFSMNT);
86 #endif
87 	kernfs_hashinit();
88 }
89 
90 void
91 kernfs_reinit()
92 {
93 	kernfs_hashreinit();
94 }
95 
96 void
97 kernfs_done()
98 {
99 #ifdef _LKM
100 	malloc_type_detach(M_KERNFSMNT);
101 #endif
102 	kernfs_hashdone();
103 }
104 
105 void
106 kernfs_get_rrootdev()
107 {
108 	static int tried = 0;
109 
110 	if (tried) {
111 		/* Already did it once. */
112 		return;
113 	}
114 	tried = 1;
115 
116 	if (rootdev == NODEV)
117 		return;
118 	rrootdev = devsw_blk2chr(rootdev);
119 	if (rrootdev != NODEV)
120 		return;
121 	rrootdev = NODEV;
122 	printf("kernfs_get_rrootdev: no raw root device\n");
123 }
124 
125 /*
126  * Mount the Kernel params filesystem
127  */
128 int
129 kernfs_mount(mp, path, data, ndp, l)
130 	struct mount *mp;
131 	const char *path;
132 	void *data;
133 	struct nameidata *ndp;
134 	struct lwp *l;
135 {
136 	int error = 0;
137 	struct kernfs_mount *fmp;
138 
139 	if (UIO_MX & (UIO_MX - 1)) {
140 		log(LOG_ERR, "kernfs: invalid directory entry size");
141 		return (EINVAL);
142 	}
143 
144 	if (mp->mnt_flag & MNT_GETARGS)
145 		return 0;
146 	/*
147 	 * Update is a no-op
148 	 */
149 	if (mp->mnt_flag & MNT_UPDATE)
150 		return (EOPNOTSUPP);
151 
152 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
153 	    M_KERNFSMNT, M_WAITOK);
154 	memset(fmp, 0, sizeof(*fmp));
155 	TAILQ_INIT(&fmp->nodelist);
156 
157 	mp->mnt_stat.f_namemax = MAXNAMLEN;
158 	mp->mnt_flag |= MNT_LOCAL;
159 	mp->mnt_data = fmp;
160 	vfs_getnewfsid(mp);
161 
162 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
163 	    UIO_SYSSPACE, mp, l)) != 0) {
164 		free(fmp, M_KERNFSMNT);
165 		return error;
166 	}
167 
168 	kernfs_get_rrootdev();
169 	return 0;
170 }
171 
172 int
173 kernfs_start(mp, flags, l)
174 	struct mount *mp;
175 	int flags;
176 	struct lwp *l;
177 {
178 
179 	return (0);
180 }
181 
182 int
183 kernfs_unmount(mp, mntflags, l)
184 	struct mount *mp;
185 	int mntflags;
186 	struct lwp *l;
187 {
188 	int error;
189 	int flags = 0;
190 
191 	if (mntflags & MNT_FORCE)
192 		flags |= FORCECLOSE;
193 
194 	if ((error = vflush(mp, 0, flags)) != 0)
195 		return (error);
196 
197 	/*
198 	 * Finally, throw away the kernfs_mount structure
199 	 */
200 	free(mp->mnt_data, M_KERNFSMNT);
201 	mp->mnt_data = NULL;
202 	return (0);
203 }
204 
205 int
206 kernfs_root(mp, vpp)
207 	struct mount *mp;
208 	struct vnode **vpp;
209 {
210 
211 	/* setup "." */
212 	return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0));
213 }
214 
215 int
216 kernfs_quotactl(mp, cmd, uid, arg, l)
217 	struct mount *mp;
218 	int cmd;
219 	uid_t uid;
220 	void *arg;
221 	struct lwp *l;
222 {
223 
224 	return (EOPNOTSUPP);
225 }
226 
227 int
228 kernfs_statvfs(mp, sbp, l)
229 	struct mount *mp;
230 	struct statvfs *sbp;
231 	struct lwp *l;
232 {
233 
234 	sbp->f_bsize = DEV_BSIZE;
235 	sbp->f_frsize = DEV_BSIZE;
236 	sbp->f_iosize = DEV_BSIZE;
237 	sbp->f_blocks = 2;		/* 1K to keep df happy */
238 	sbp->f_bfree = 0;
239 	sbp->f_bavail = 0;
240 	sbp->f_bresvd = 0;
241 	sbp->f_files = 1024;	/* XXX lie */
242 	sbp->f_ffree = 128;	/* XXX lie */
243 	sbp->f_favail = 128;	/* XXX lie */
244 	sbp->f_fresvd = 0;
245 	copy_statvfs_info(sbp, mp);
246 	return (0);
247 }
248 
249 /*ARGSUSED*/
250 int
251 kernfs_sync(mp, waitfor, uc, l)
252 	struct mount *mp;
253 	int waitfor;
254 	struct ucred *uc;
255 	struct lwp *l;
256 {
257 
258 	return (0);
259 }
260 
261 /*
262  * Kernfs flat namespace lookup.
263  * Currently unsupported.
264  */
265 int
266 kernfs_vget(mp, ino, vpp)
267 	struct mount *mp;
268 	ino_t ino;
269 	struct vnode **vpp;
270 {
271 
272 	return (EOPNOTSUPP);
273 }
274 
275 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup")
276 {
277 
278 	sysctl_createv(clog, 0, NULL, NULL,
279 		       CTLFLAG_PERMANENT,
280 		       CTLTYPE_NODE, "vfs", NULL,
281 		       NULL, 0, NULL, 0,
282 		       CTL_VFS, CTL_EOL);
283 	sysctl_createv(clog, 0, NULL, NULL,
284 		       CTLFLAG_PERMANENT,
285 		       CTLTYPE_NODE, "kernfs",
286 		       SYSCTL_DESCR("/kern file system"),
287 		       NULL, 0, NULL, 0,
288 		       CTL_VFS, 11, CTL_EOL);
289 	/*
290 	 * XXX the "11" above could be dynamic, thereby eliminating one
291 	 * more instance of the "number to vfs" mapping problem, but
292 	 * "11" is the order as taken from sys/mount.h
293 	 */
294 }
295 
296 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
297 
298 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
299 	&kernfs_vnodeop_opv_desc,
300 	NULL,
301 };
302 
303 struct vfsops kernfs_vfsops = {
304 	MOUNT_KERNFS,
305 	kernfs_mount,
306 	kernfs_start,
307 	kernfs_unmount,
308 	kernfs_root,
309 	kernfs_quotactl,
310 	kernfs_statvfs,
311 	kernfs_sync,
312 	kernfs_vget,
313 	NULL,				/* vfs_fhtovp */
314 	NULL,				/* vfs_vptofh */
315 	kernfs_init,
316 	kernfs_reinit,
317 	kernfs_done,
318 	NULL,				/* vfs_mountroot */
319 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
320 	vfs_stdextattrctl,
321 	kernfs_vnodeopv_descs,
322 };
323 VFS_ATTACH(kernfs_vfsops);
324