Initial import from FreeBSD RELENG_4:
[games.git] / sys / vfs / procfs / procfs_vfsops.c
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)procfs_vfsops.c     8.7 (Berkeley) 5/10/95
38  *
39  * $FreeBSD: src/sys/miscfs/procfs/procfs_vfsops.c,v 1.32.2.1 2001/10/15 20:42:01 des Exp $
40  */
41
42 /*
43  * procfs VFS interface
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <miscfs/procfs/procfs.h>
53
54 static int      procfs_mount __P((struct mount *mp, char *path, caddr_t data,
55                                   struct nameidata *ndp, struct proc *p));
56 static int      procfs_statfs __P((struct mount *mp, struct statfs *sbp,
57                                    struct proc *p));
58 static int      procfs_unmount __P((struct mount *mp, int mntflags,
59                                     struct proc *p));
60
61 /*
62  * VFS Operations.
63  *
64  * mount system call
65  */
66 /* ARGSUSED */
67 static int
68 procfs_mount(mp, path, data, ndp, p)
69         struct mount *mp;
70         char *path;
71         caddr_t data;
72         struct nameidata *ndp;
73         struct proc *p;
74 {
75         size_t size;
76         int error;
77
78         if (mp->mnt_flag & MNT_UPDATE)
79                 return (EOPNOTSUPP);
80
81         if (mp->mnt_vfc->vfc_refcount == 1 && (error = at_exit(procfs_exit))) {
82                 printf("procfs:  cannot register procfs_exit with at_exit\n");
83                 return(error);
84         }
85
86         mp->mnt_flag |= MNT_LOCAL;
87         mp->mnt_data = 0;
88         vfs_getnewfsid(mp);
89
90         (void) copyinstr(path, (caddr_t)mp->mnt_stat.f_mntonname, MNAMELEN, &size);
91         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
92
93         size = sizeof("procfs") - 1;
94         bcopy("procfs", mp->mnt_stat.f_mntfromname, size);
95         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
96         (void)procfs_statfs(mp, &mp->mnt_stat, p);
97
98         return (0);
99 }
100
101 /*
102  * unmount system call
103  */
104 static int
105 procfs_unmount(mp, mntflags, p)
106         struct mount *mp;
107         int mntflags;
108         struct proc *p;
109 {
110         int error;
111         int flags = 0;
112
113         if (mntflags & MNT_FORCE)
114                 flags |= FORCECLOSE;
115
116         error = vflush(mp, 0, flags);
117         if (error)
118                 return (error);
119
120         if (mp->mnt_vfc->vfc_refcount == 1)
121                 rm_at_exit(procfs_exit);
122
123         return (0);
124 }
125
126 int
127 procfs_root(mp, vpp)
128         struct mount *mp;
129         struct vnode **vpp;
130 {
131
132         return (procfs_allocvp(mp, vpp, 0, Proot));
133 }
134
135 /*
136  * Get file system statistics.
137  */
138 static int
139 procfs_statfs(mp, sbp, p)
140         struct mount *mp;
141         struct statfs *sbp;
142         struct proc *p;
143 {
144         sbp->f_bsize = PAGE_SIZE;
145         sbp->f_iosize = PAGE_SIZE;
146         sbp->f_blocks = 1;      /* avoid divide by zero in some df's */
147         sbp->f_bfree = 0;
148         sbp->f_bavail = 0;
149         sbp->f_files = maxproc;                 /* approx */
150         sbp->f_ffree = maxproc - nprocs;        /* approx */
151
152         if (sbp != &mp->mnt_stat) {
153                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
154                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
155                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
156                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
157         }
158
159         return (0);
160 }
161
162 static struct vfsops procfs_vfsops = {
163         procfs_mount,
164         vfs_stdstart,
165         procfs_unmount,
166         procfs_root,
167         vfs_stdquotactl,
168         procfs_statfs,
169         vfs_stdsync,
170         vfs_stdvget,
171         vfs_stdfhtovp,
172         vfs_stdcheckexp,
173         vfs_stdvptofh,
174         vfs_stdinit,
175         vfs_stduninit,
176         vfs_stdextattrctl,
177 };
178
179 VFS_SET(procfs_vfsops, procfs, VFCF_SYNTHETIC);
180 MODULE_VERSION(procfs, 1);