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