]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/miscfs/kernfs/kernfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r47471,
[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.26 1999/05/08 06:39:52 phk 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 void     kernfs_get_rrootdev __P((void));
60 static int      kernfs_init __P((struct vfsconf *vfsp));
61 static int      kernfs_mount __P((struct mount *mp, char *path, caddr_t data,
62                                   struct nameidata *ndp, struct proc *p));
63 static int      kernfs_start __P((struct mount *mp, int flags, struct proc *p));
64 static int      kernfs_unmount __P((struct mount *mp, int mntflags,
65                                     struct proc *p));
66 static int      kernfs_root __P((struct mount *mp, struct vnode **vpp));
67 static int      kernfs_statfs __P((struct mount *mp, struct statfs *sbp,
68                                    struct proc *p));
69
70 static int
71 kernfs_init(vfsp)
72         struct vfsconf *vfsp;
73 {
74
75         return (0);
76 }
77
78 static void
79 kernfs_get_rrootdev()
80 {
81         static int tried = 0;
82         int cmaj;
83
84         if (tried) {
85                 /* Already did it once. */
86                 return;
87         }
88         tried = 1;
89
90         if (!bdevsw(rootdev)) {
91                 panic("root dev has no bdevsw");
92         }
93         if (rootdev == NODEV)
94                 return;
95         for (cmaj = 0; cmaj < nchrdev; cmaj++) {
96                 rrootdev = makedev(cmaj, minor(rootdev));
97                 if (chrtoblk(rrootdev) == rootdev)
98                         return;
99         }
100         rrootdev = NODEV;
101         printf("kernfs_get_rrootdev: no raw root device\n");
102 }
103
104 /*
105  * Mount the Kernel params filesystem
106  */
107 static int
108 kernfs_mount(mp, path, data, ndp, p)
109         struct mount *mp;
110         char *path;
111         caddr_t data;
112         struct nameidata *ndp;
113         struct proc *p;
114 {
115         int error = 0;
116         u_int size;
117         struct kernfs_mount *fmp;
118         struct vnode *rvp;
119
120 #ifdef KERNFS_DIAGNOSTIC
121         printf("kernfs_mount(mp = %x)\n", mp);
122 #endif
123
124         /*
125          * Update is a no-op
126          */
127         if (mp->mnt_flag & MNT_UPDATE)
128                 return (EOPNOTSUPP);
129
130         MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
131                                 M_KERNFSMNT, M_WAITOK); /* XXX */
132
133         error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);     /* XXX */
134         if (error) {
135                 FREE(fmp, M_KERNFSMNT);
136                 return (error);
137         }
138
139         rvp->v_type = VDIR;
140         rvp->v_flag |= VROOT;
141 #ifdef KERNFS_DIAGNOSTIC
142         printf("kernfs_mount: root vp = %x\n", rvp);
143 #endif
144         fmp->kf_root = rvp;
145         mp->mnt_flag |= MNT_LOCAL;
146         mp->mnt_data = (qaddr_t) fmp;
147         vfs_getnewfsid(mp);
148
149         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
150         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
151         bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
152         bcopy("kernfs", mp->mnt_stat.f_mntfromname, sizeof("kernfs"));
153         (void)kernfs_statfs(mp, &mp->mnt_stat, p);
154 #ifdef KERNFS_DIAGNOSTIC
155         printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
156 #endif
157
158         kernfs_get_rrootdev();
159         return (0);
160 }
161
162 static int
163 kernfs_start(mp, flags, p)
164         struct mount *mp;
165         int flags;
166         struct proc *p;
167 {
168         return (0);
169 }
170
171 static int
172 kernfs_unmount(mp, mntflags, p)
173         struct mount *mp;
174         int mntflags;
175         struct proc *p;
176 {
177         int error;
178         int flags = 0;
179         struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
180
181 #ifdef KERNFS_DIAGNOSTIC
182         printf("kernfs_unmount(mp = %x)\n", mp);
183 #endif
184
185         if (mntflags & MNT_FORCE)
186                 flags |= FORCECLOSE;
187
188         /*
189          * Clear out buffer cache.  I don't think we
190          * ever get anything cached at this level at the
191          * moment, but who knows...
192          */
193         if (rootvp->v_usecount > 1)
194                 return (EBUSY);
195 #ifdef KERNFS_DIAGNOSTIC
196         printf("kernfs_unmount: calling vflush\n");
197 #endif
198         error = vflush(mp, rootvp, flags);
199         if (error)
200                 return (error);
201
202 #ifdef KERNFS_DIAGNOSTIC
203         vprint("kernfs root", rootvp);
204 #endif
205         /*
206          * Release reference on underlying root vnode
207          */
208         vrele(rootvp);
209         /*
210          * And blow it away for future re-use
211          */
212         vgone(rootvp);
213         /*
214          * Finally, throw away the kernfs_mount structure
215          */
216         free(mp->mnt_data, M_KERNFSMNT);        /* XXX */
217         mp->mnt_data = 0;
218         return 0;
219 }
220
221 static int
222 kernfs_root(mp, vpp)
223         struct mount *mp;
224         struct vnode **vpp;
225 {
226         struct proc *p = curproc;       /* XXX */
227         struct vnode *vp;
228
229 #ifdef KERNFS_DIAGNOSTIC
230         printf("kernfs_root(mp = %x)\n", mp);
231 #endif
232
233         /*
234          * Return locked reference to root.
235          */
236         vp = VFSTOKERNFS(mp)->kf_root;
237         VREF(vp);
238         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
239         *vpp = vp;
240         return (0);
241 }
242
243 static int
244 kernfs_statfs(mp, sbp, p)
245         struct mount *mp;
246         struct statfs *sbp;
247         struct proc *p;
248 {
249 #ifdef KERNFS_DIAGNOSTIC
250         printf("kernfs_statfs(mp = %x)\n", mp);
251 #endif
252
253         sbp->f_flags = 0;
254         sbp->f_bsize = DEV_BSIZE;
255         sbp->f_iosize = DEV_BSIZE;
256         sbp->f_blocks = 2;              /* 1K to keep df happy */
257         sbp->f_bfree = 0;
258         sbp->f_bavail = 0;
259         sbp->f_files = 0;
260         sbp->f_ffree = 0;
261         if (sbp != &mp->mnt_stat) {
262                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
263                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
264                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
265                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
266         }
267         return (0);
268 }
269
270 static struct vfsops kernfs_vfsops = {
271         kernfs_mount,
272         kernfs_start,
273         kernfs_unmount,
274         kernfs_root,
275         kernfs_quotactl,
276         kernfs_statfs,
277         kernfs_sync,
278         kernfs_vget,
279         kernfs_fhtovp,
280         kernfs_vptofh,
281         kernfs_init,
282 };
283
284 VFS_SET(kernfs_vfsops, kernfs, VFCF_SYNTHETIC);