]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/miscfs/kernfs/kernfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r41227,
[FreeBSD/FreeBSD.git] / sys / miscfs / kernfs / kernfs_vfsops.c
1 /*
2  * Copyright (c) 1992, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)kernfs_vfsops.c     8.10 (Berkeley) 5/14/95
37  * $Id: kernfs_vfsops.c,v 1.22 1998/05/06 05:29:33 msmith Exp $
38  */
39
40 /*
41  * Kernel params Filesystem
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/malloc.h>
52
53 #include <miscfs/kernfs/kernfs.h>
54
55 static MALLOC_DEFINE(M_KERNFSMNT, "KERNFS mount", "KERNFS mount structure");
56
57 dev_t rrootdev = NODEV;
58
59 static int      cdevvp __P((dev_t dev, struct vnode **vpp));
60 static void     kernfs_get_rrootdev __P((void));
61 static int      kernfs_init __P((struct vfsconf *vfsp));
62 static int      kernfs_mount __P((struct mount *mp, char *path, caddr_t data,
63                                   struct nameidata *ndp, struct proc *p));
64 static int      kernfs_start __P((struct mount *mp, int flags, struct proc *p));
65 static int      kernfs_unmount __P((struct mount *mp, int mntflags,
66                                     struct proc *p));
67 static int      kernfs_root __P((struct mount *mp, struct vnode **vpp));
68 static int      kernfs_statfs __P((struct mount *mp, struct statfs *sbp,
69                                    struct proc *p));
70
71 static int
72 kernfs_init(vfsp)
73         struct vfsconf *vfsp;
74 {
75
76         return (0);
77 }
78
79 static void
80 kernfs_get_rrootdev()
81 {
82         static int tried = 0;
83         int bmaj = major(rootdev);
84         int cmaj;
85
86         if (tried) {
87                 /* Already did it once. */
88                 return;
89         }
90         tried = 1;
91
92         if (!bdevsw[bmaj]) {
93                 panic("root dev has no bdevsw");
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 static int
110 kernfs_mount(mp, path, data, ndp, p)
111         struct mount *mp;
112         char *path;
113         caddr_t data;
114         struct nameidata *ndp;
115         struct proc *p;
116 {
117         int error = 0;
118         u_int size;
119         struct kernfs_mount *fmp;
120         struct vnode *rvp;
121
122 #ifdef KERNFS_DIAGNOSTIC
123         printf("kernfs_mount(mp = %x)\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         MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
133                                 M_KERNFSMNT, M_WAITOK); /* XXX */
134
135         error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);     /* XXX */
136         if (error) {
137                 FREE(fmp, M_KERNFSMNT);
138                 return (error);
139         }
140
141         rvp->v_type = VDIR;
142         rvp->v_flag |= VROOT;
143 #ifdef KERNFS_DIAGNOSTIC
144         printf("kernfs_mount: root vp = %x\n", rvp);
145 #endif
146         fmp->kf_root = rvp;
147         mp->mnt_flag |= MNT_LOCAL;
148         mp->mnt_data = (qaddr_t) fmp;
149         vfs_getnewfsid(mp);
150
151         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
152         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
153         bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
154         bcopy("kernfs", mp->mnt_stat.f_mntfromname, sizeof("kernfs"));
155         (void)kernfs_statfs(mp, &mp->mnt_stat, p);
156 #ifdef KERNFS_DIAGNOSTIC
157         printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
158 #endif
159
160         kernfs_get_rrootdev();
161         return (0);
162 }
163
164 static int
165 kernfs_start(mp, flags, p)
166         struct mount *mp;
167         int flags;
168         struct proc *p;
169 {
170         return (0);
171 }
172
173 static int
174 kernfs_unmount(mp, mntflags, p)
175         struct mount *mp;
176         int mntflags;
177         struct proc *p;
178 {
179         int error;
180         int flags = 0;
181         struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
182
183 #ifdef KERNFS_DIAGNOSTIC
184         printf("kernfs_unmount(mp = %x)\n", mp);
185 #endif
186
187         if (mntflags & MNT_FORCE)
188                 flags |= FORCECLOSE;
189
190         /*
191          * Clear out buffer cache.  I don't think we
192          * ever get anything cached at this level at the
193          * moment, but who knows...
194          */
195         if (rootvp->v_usecount > 1)
196                 return (EBUSY);
197 #ifdef KERNFS_DIAGNOSTIC
198         printf("kernfs_unmount: calling vflush\n");
199 #endif
200         error = vflush(mp, rootvp, flags);
201         if (error)
202                 return (error);
203
204 #ifdef KERNFS_DIAGNOSTIC
205         vprint("kernfs root", rootvp);
206 #endif
207         /*
208          * Release reference on underlying root vnode
209          */
210         vrele(rootvp);
211         /*
212          * And blow it away for future re-use
213          */
214         vgone(rootvp);
215         /*
216          * Finally, throw away the kernfs_mount structure
217          */
218         free(mp->mnt_data, M_KERNFSMNT);        /* XXX */
219         mp->mnt_data = 0;
220         return 0;
221 }
222
223 static int
224 kernfs_root(mp, vpp)
225         struct mount *mp;
226         struct vnode **vpp;
227 {
228         struct proc *p = curproc;       /* XXX */
229         struct vnode *vp;
230
231 #ifdef KERNFS_DIAGNOSTIC
232         printf("kernfs_root(mp = %x)\n", mp);
233 #endif
234
235         /*
236          * Return locked reference to root.
237          */
238         vp = VFSTOKERNFS(mp)->kf_root;
239         VREF(vp);
240         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
241         *vpp = vp;
242         return (0);
243 }
244
245 static int
246 kernfs_statfs(mp, sbp, p)
247         struct mount *mp;
248         struct statfs *sbp;
249         struct proc *p;
250 {
251 #ifdef KERNFS_DIAGNOSTIC
252         printf("kernfs_statfs(mp = %x)\n", mp);
253 #endif
254
255         sbp->f_flags = 0;
256         sbp->f_bsize = DEV_BSIZE;
257         sbp->f_iosize = DEV_BSIZE;
258         sbp->f_blocks = 2;              /* 1K to keep df happy */
259         sbp->f_bfree = 0;
260         sbp->f_bavail = 0;
261         sbp->f_files = 0;
262         sbp->f_ffree = 0;
263         if (sbp != &mp->mnt_stat) {
264                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
265                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
266                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
267                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
268         }
269         return (0);
270 }
271
272 static struct vfsops kernfs_vfsops = {
273         kernfs_mount,
274         kernfs_start,
275         kernfs_unmount,
276         kernfs_root,
277         kernfs_quotactl,
278         kernfs_statfs,
279         kernfs_sync,
280         kernfs_vget,
281         kernfs_fhtovp,
282         kernfs_vptofh,
283         kernfs_init,
284 };
285
286 VFS_SET(kernfs_vfsops, kernfs, VFCF_SYNTHETIC);