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