kernfs_vfsops.c revision 1.32 1 /* $NetBSD: kernfs_vfsops.c,v 1.32 1998/03/01 02:21:04 fvdl 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. 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.10 (Berkeley) 5/14/95
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 void kernfs_init __P((void));
61 void kernfs_get_rrootdev __P((void));
62 int kernfs_mount __P((struct mount *, const char *, void *,
63 struct nameidata *, struct proc *));
64 int kernfs_start __P((struct mount *, int, struct proc *));
65 int kernfs_unmount __P((struct mount *, int, struct proc *));
66 int kernfs_root __P((struct mount *, struct vnode **));
67 int kernfs_statfs __P((struct mount *, struct statfs *, struct proc *));
68 int kernfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
69 struct proc *));
70 int kernfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
71 int kernfs_vget __P((struct mount *, ino_t, struct vnode **));
72 int kernfs_fhtovp __P((struct mount *, struct fid *, struct mbuf *,
73 struct vnode **, int *, struct ucred **));
74 int kernfs_vptofh __P((struct vnode *, struct fid *));
75 int kernfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
76 struct proc *));
77
78 void
79 kernfs_init()
80 {
81 }
82
83 void
84 kernfs_get_rrootdev()
85 {
86 static int tried = 0;
87 int cmaj;
88
89 if (tried) {
90 /* Already did it once. */
91 return;
92 }
93 tried = 1;
94
95 if (rootdev == NODEV)
96 return;
97 for (cmaj = 0; cmaj < nchrdev; cmaj++) {
98 rrootdev = makedev(cmaj, minor(rootdev));
99 if (chrtoblk(rrootdev) == rootdev)
100 return;
101 }
102 rrootdev = NODEV;
103 printf("kernfs_get_rrootdev: no raw root device\n");
104 }
105
106 /*
107 * Mount the Kernel params filesystem
108 */
109 int
110 kernfs_mount(mp, path, data, ndp, p)
111 struct mount *mp;
112 const char *path;
113 void *data;
114 struct nameidata *ndp;
115 struct proc *p;
116 {
117 int error = 0;
118 size_t size;
119 struct kernfs_mount *fmp;
120 struct vnode *rvp;
121
122 #ifdef KERNFS_DIAGNOSTIC
123 printf("kernfs_mount(mp = %p)\n", mp);
124 #endif
125
126 /*
127 * Update is a no-op
128 */
129 if (mp->mnt_flag & MNT_UPDATE)
130 return (EOPNOTSUPP);
131
132 error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);
133 if (error)
134 return (error);
135
136 MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
137 M_MISCFSMNT, M_WAITOK);
138 rvp->v_type = VDIR;
139 rvp->v_flag |= VROOT;
140 #ifdef KERNFS_DIAGNOSTIC
141 printf("kernfs_mount: root vp = %p\n", rvp);
142 #endif
143 fmp->kf_root = rvp;
144 mp->mnt_flag |= MNT_LOCAL;
145 mp->mnt_data = (qaddr_t)fmp;
146 vfs_getnewfsid(mp, MOUNT_KERNFS);
147
148 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
149 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
150 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
151 bcopy("kernfs", mp->mnt_stat.f_mntfromname, sizeof("kernfs"));
152 #ifdef KERNFS_DIAGNOSTIC
153 printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
154 #endif
155
156 kernfs_get_rrootdev();
157 return (0);
158 }
159
160 int
161 kernfs_start(mp, flags, p)
162 struct mount *mp;
163 int flags;
164 struct proc *p;
165 {
166
167 return (0);
168 }
169
170 int
171 kernfs_unmount(mp, mntflags, p)
172 struct mount *mp;
173 int mntflags;
174 struct proc *p;
175 {
176 int error;
177 int flags = 0;
178 struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
179
180 #ifdef KERNFS_DIAGNOSTIC
181 printf("kernfs_unmount(mp = %p)\n", mp);
182 #endif
183
184 if (mntflags & MNT_FORCE)
185 flags |= FORCECLOSE;
186
187 /*
188 * Clear out buffer cache. I don't think we
189 * ever get anything cached at this level at the
190 * moment, but who knows...
191 */
192 if (rootvp->v_usecount > 1)
193 return (EBUSY);
194 #ifdef KERNFS_DIAGNOSTIC
195 printf("kernfs_unmount: calling vflush\n");
196 #endif
197 if ((error = vflush(mp, rootvp, flags)) != 0)
198 return (error);
199
200 #ifdef KERNFS_DIAGNOSTIC
201 vprint("kernfs root", rootvp);
202 #endif
203 /*
204 * Clean out the old root vnode for reuse.
205 */
206 vrele(rootvp);
207 vgone(rootvp);
208 /*
209 * Finally, throw away the kernfs_mount structure
210 */
211 free(mp->mnt_data, M_MISCFSMNT);
212 mp->mnt_data = 0;
213 return (0);
214 }
215
216 int
217 kernfs_root(mp, vpp)
218 struct mount *mp;
219 struct vnode **vpp;
220 {
221 struct vnode *vp;
222
223 #ifdef KERNFS_DIAGNOSTIC
224 printf("kernfs_root(mp = %p)\n", mp);
225 #endif
226
227 /*
228 * Return locked reference to root.
229 */
230 vp = VFSTOKERNFS(mp)->kf_root;
231 VREF(vp);
232 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
233 *vpp = vp;
234 return (0);
235 }
236
237 int
238 kernfs_quotactl(mp, cmd, uid, arg, p)
239 struct mount *mp;
240 int cmd;
241 uid_t uid;
242 caddr_t arg;
243 struct proc *p;
244 {
245
246 return (EOPNOTSUPP);
247 }
248
249 int
250 kernfs_statfs(mp, sbp, p)
251 struct mount *mp;
252 struct statfs *sbp;
253 struct proc *p;
254 {
255
256 #ifdef KERNFS_DIAGNOSTIC
257 printf("kernfs_statfs(mp = %p)\n", mp);
258 #endif
259
260 sbp->f_bsize = DEV_BSIZE;
261 sbp->f_iosize = DEV_BSIZE;
262 sbp->f_blocks = 2; /* 1K to keep df happy */
263 sbp->f_bfree = 0;
264 sbp->f_bavail = 0;
265 sbp->f_files = 0;
266 sbp->f_ffree = 0;
267 #ifdef COMPAT_09
268 sbp->f_type = 7;
269 #else
270 sbp->f_type = 0;
271 #endif
272 if (sbp != &mp->mnt_stat) {
273 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
274 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
275 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
276 }
277 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
278 return (0);
279 }
280
281 /*ARGSUSED*/
282 int
283 kernfs_sync(mp, waitfor, uc, p)
284 struct mount *mp;
285 int waitfor;
286 struct ucred *uc;
287 struct proc *p;
288 {
289
290 return (0);
291 }
292
293 /*
294 * Kernfs flat namespace lookup.
295 * Currently unsupported.
296 */
297 int
298 kernfs_vget(mp, ino, vpp)
299 struct mount *mp;
300 ino_t ino;
301 struct vnode **vpp;
302 {
303
304 return (EOPNOTSUPP);
305 }
306
307 /*ARGSUSED*/
308 int
309 kernfs_fhtovp(mp, fhp, mb, vpp, what, anon)
310 struct mount *mp;
311 struct fid *fhp;
312 struct mbuf *mb;
313 struct vnode **vpp;
314 int *what;
315 struct ucred **anon;
316 {
317
318 return (EOPNOTSUPP);
319 }
320
321 /*ARGSUSED*/
322 int
323 kernfs_vptofh(vp, fhp)
324 struct vnode *vp;
325 struct fid *fhp;
326 {
327
328 return (EOPNOTSUPP);
329 }
330
331 int
332 kernfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
333 int *name;
334 u_int namelen;
335 void *oldp;
336 size_t *oldlenp;
337 void *newp;
338 size_t newlen;
339 struct proc *p;
340 {
341 return (EOPNOTSUPP);
342 }
343
344 extern struct vnodeopv_desc kernfs_vnodeop_opv_desc;
345
346 struct vnodeopv_desc *kernfs_vnodeopv_descs[] = {
347 &kernfs_vnodeop_opv_desc,
348 NULL,
349 };
350
351 struct vfsops kernfs_vfsops = {
352 MOUNT_KERNFS,
353 kernfs_mount,
354 kernfs_start,
355 kernfs_unmount,
356 kernfs_root,
357 kernfs_quotactl,
358 kernfs_statfs,
359 kernfs_sync,
360 kernfs_vget,
361 kernfs_fhtovp,
362 kernfs_vptofh,
363 kernfs_init,
364 kernfs_sysctl,
365 NULL, /* vfs_mountroot */
366 kernfs_vnodeopv_descs,
367 };
368