xref: /netbsd-src/sys/miscfs/kernfs/kernfs_vfsops.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: kernfs_vfsops.c,v 1.81 2007/07/31 21:14:16 pooka 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.81 2007/07/31 21:14:16 pooka 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 #include <sys/kauth.h>
60 
61 #include <miscfs/specfs/specdev.h>
62 #include <miscfs/kernfs/kernfs.h>
63 
64 MALLOC_JUSTDEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures");
65 
66 dev_t rrootdev = NODEV;
67 
68 VFS_PROTOS(kernfs);
69 
70 void	kernfs_get_rrootdev(void);
71 
72 void
73 kernfs_init()
74 {
75 
76 	malloc_type_attach(M_KERNFSMNT);
77 	kernfs_hashinit();
78 }
79 
80 void
81 kernfs_reinit()
82 {
83 	kernfs_hashreinit();
84 }
85 
86 void
87 kernfs_done()
88 {
89 
90 	kernfs_hashdone();
91 	malloc_type_detach(M_KERNFSMNT);
92 }
93 
94 void
95 kernfs_get_rrootdev()
96 {
97 	static int tried = 0;
98 
99 	if (tried) {
100 		/* Already did it once. */
101 		return;
102 	}
103 	tried = 1;
104 
105 	if (rootdev == NODEV)
106 		return;
107 	rrootdev = devsw_blk2chr(rootdev);
108 	if (rrootdev != NODEV)
109 		return;
110 	rrootdev = NODEV;
111 	printf("kernfs_get_rrootdev: no raw root device\n");
112 }
113 
114 /*
115  * Mount the Kernel params filesystem
116  */
117 int
118 kernfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len,
119     struct lwp *l)
120 {
121 	int error = 0;
122 	struct kernfs_mount *fmp;
123 
124 	if (UIO_MX & (UIO_MX - 1)) {
125 		log(LOG_ERR, "kernfs: invalid directory entry size");
126 		return (EINVAL);
127 	}
128 
129 	if (mp->mnt_flag & MNT_GETARGS) {
130 		*data_len = 0;
131 		return 0;
132 	}
133 	/*
134 	 * Update is a no-op
135 	 */
136 	if (mp->mnt_flag & MNT_UPDATE)
137 		return (EOPNOTSUPP);
138 
139 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
140 	    M_KERNFSMNT, M_WAITOK);
141 	memset(fmp, 0, sizeof(*fmp));
142 	TAILQ_INIT(&fmp->nodelist);
143 
144 	mp->mnt_stat.f_namemax = MAXNAMLEN;
145 	mp->mnt_flag |= MNT_LOCAL;
146 	mp->mnt_data = fmp;
147 	vfs_getnewfsid(mp);
148 
149 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
150 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
151 		free(fmp, M_KERNFSMNT);
152 		return error;
153 	}
154 
155 	kernfs_get_rrootdev();
156 	return 0;
157 }
158 
159 int
160 kernfs_start(struct mount *mp, int flags,
161     struct lwp *l)
162 {
163 
164 	return (0);
165 }
166 
167 int
168 kernfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
169 {
170 	int error;
171 	int flags = 0;
172 
173 	if (mntflags & MNT_FORCE)
174 		flags |= FORCECLOSE;
175 
176 	if ((error = vflush(mp, 0, flags)) != 0)
177 		return (error);
178 
179 	/*
180 	 * Finally, throw away the kernfs_mount structure
181 	 */
182 	free(mp->mnt_data, M_KERNFSMNT);
183 	mp->mnt_data = NULL;
184 	return (0);
185 }
186 
187 int
188 kernfs_root(mp, vpp)
189 	struct mount *mp;
190 	struct vnode **vpp;
191 {
192 
193 	/* setup "." */
194 	return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0));
195 }
196 
197 int
198 kernfs_quotactl(struct mount *mp, int cmd, uid_t uid,
199     void *arg, struct lwp *l)
200 {
201 
202 	return (EOPNOTSUPP);
203 }
204 
205 int
206 kernfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
207 {
208 
209 	sbp->f_bsize = DEV_BSIZE;
210 	sbp->f_frsize = DEV_BSIZE;
211 	sbp->f_iosize = DEV_BSIZE;
212 	sbp->f_blocks = 2;		/* 1K to keep df happy */
213 	sbp->f_bfree = 0;
214 	sbp->f_bavail = 0;
215 	sbp->f_bresvd = 0;
216 	sbp->f_files = 1024;	/* XXX lie */
217 	sbp->f_ffree = 128;	/* XXX lie */
218 	sbp->f_favail = 128;	/* XXX lie */
219 	sbp->f_fresvd = 0;
220 	copy_statvfs_info(sbp, mp);
221 	return (0);
222 }
223 
224 /*ARGSUSED*/
225 int
226 kernfs_sync(struct mount *mp, int waitfor,
227     kauth_cred_t uc, struct lwp *l)
228 {
229 
230 	return (0);
231 }
232 
233 /*
234  * Kernfs flat namespace lookup.
235  * Currently unsupported.
236  */
237 int
238 kernfs_vget(struct mount *mp, ino_t ino,
239     struct vnode **vpp)
240 {
241 
242 	return (EOPNOTSUPP);
243 }
244 
245 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup")
246 {
247 
248 	sysctl_createv(clog, 0, NULL, NULL,
249 		       CTLFLAG_PERMANENT,
250 		       CTLTYPE_NODE, "vfs", NULL,
251 		       NULL, 0, NULL, 0,
252 		       CTL_VFS, CTL_EOL);
253 	sysctl_createv(clog, 0, NULL, NULL,
254 		       CTLFLAG_PERMANENT,
255 		       CTLTYPE_NODE, "kernfs",
256 		       SYSCTL_DESCR("/kern file system"),
257 		       NULL, 0, NULL, 0,
258 		       CTL_VFS, 11, CTL_EOL);
259 	/*
260 	 * XXX the "11" above could be dynamic, thereby eliminating one
261 	 * more instance of the "number to vfs" mapping problem, but
262 	 * "11" is the order as taken from sys/mount.h
263 	 */
264 }
265 
266 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
267 
268 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
269 	&kernfs_vnodeop_opv_desc,
270 	NULL,
271 };
272 
273 struct vfsops kernfs_vfsops = {
274 	MOUNT_KERNFS,
275 	0,
276 	kernfs_mount,
277 	kernfs_start,
278 	kernfs_unmount,
279 	kernfs_root,
280 	kernfs_quotactl,
281 	kernfs_statvfs,
282 	kernfs_sync,
283 	kernfs_vget,
284 	(void *)eopnotsupp,		/* vfs_fhtovp */
285 	(void *)eopnotsupp,		/* vfs_vptofh */
286 	kernfs_init,
287 	kernfs_reinit,
288 	kernfs_done,
289 	NULL,				/* vfs_mountroot */
290 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
291 	vfs_stdextattrctl,
292 	(void *)eopnotsupp,		/* vfs_suspendctl */
293 	kernfs_vnodeopv_descs,
294 	0,
295 	{ NULL, NULL },
296 };
297 VFS_ATTACH(kernfs_vfsops);
298