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