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