1 /* $OpenBSD: procfs_vfsops.c,v 1.24 2004/05/20 18:32:38 tedu Exp $ */
2 /* $NetBSD: procfs_vfsops.c,v 1.25 1996/02/09 22:40:53 christos Exp $ */
3
4 /*
5 * Copyright (c) 1993 Jan-Simon Pendry
6 * Copyright (c) 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Jan-Simon Pendry.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. 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 * @(#)procfs_vfsops.c 8.5 (Berkeley) 6/15/94
37 */
38
39 /*
40 * procfs VFS interface
41 */
42
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/buf.h>
49 #include <sys/syslog.h>
50 #include <sys/mount.h>
51 #include <sys/signalvar.h>
52 #include <sys/vnode.h>
53 #include <sys/malloc.h>
54
55 #include <miscfs/procfs/procfs.h>
56
57 #include <uvm/uvm_extern.h>
58
59 int procfs_mount(struct mount *, const char *, void *,
60 struct nameidata *, struct proc *);
61 int procfs_start(struct mount *, int, struct proc *);
62 int procfs_unmount(struct mount *, int, struct proc *);
63 int procfs_statfs(struct mount *, struct statfs *, struct proc *);
64 /*
65 * VFS Operations.
66 *
67 * mount system call
68 */
69 /* ARGSUSED */
70 int
procfs_mount(mp,path,data,ndp,p)71 procfs_mount(mp, path, data, ndp, p)
72 struct mount *mp;
73 const char *path;
74 void *data;
75 struct nameidata *ndp;
76 struct proc *p;
77 {
78 size_t size;
79 struct procfsmount *pmnt;
80 struct procfs_args args;
81 int error;
82
83 if (UIO_MX & (UIO_MX-1)) {
84 log(LOG_ERR, "procfs: invalid directory entry size");
85 return (EINVAL);
86 }
87
88 if (mp->mnt_flag & MNT_UPDATE)
89 return (EOPNOTSUPP);
90
91 if (data != NULL) {
92 error = copyin(data, &args, sizeof args);
93 if (error != 0)
94 return (error);
95
96 if (args.version != PROCFS_ARGSVERSION)
97 return (EINVAL);
98 } else
99 args.flags = 0;
100
101 mp->mnt_flag |= MNT_LOCAL;
102 pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
103 M_MISCFSMNT, M_WAITOK);
104
105 mp->mnt_data = pmnt;
106 vfs_getnewfsid(mp);
107
108 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
109 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
110 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
111 bcopy("procfs", mp->mnt_stat.f_mntfromname, sizeof("procfs"));
112 bcopy(&args, &mp->mnt_stat.mount_info.procfs_args, sizeof(args));
113
114 #ifdef notyet
115 pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
116 #endif
117 pmnt->pmnt_flags = args.flags;
118
119 return (0);
120 }
121
122 /*
123 * unmount system call
124 */
125 int
procfs_unmount(mp,mntflags,p)126 procfs_unmount(mp, mntflags, p)
127 struct mount *mp;
128 int mntflags;
129 struct proc *p;
130 {
131 int error;
132 extern int doforce;
133 int flags = 0;
134
135 if (mntflags & MNT_FORCE) {
136 /* procfs can never be rootfs so don't check for it */
137 if (!doforce)
138 return (EINVAL);
139 flags |= FORCECLOSE;
140 }
141
142 if ((error = vflush(mp, 0, flags)) != 0)
143 return (error);
144
145 free(VFSTOPROC(mp), M_MISCFSMNT);
146 mp->mnt_data = 0;
147
148 return (0);
149 }
150
151 int
procfs_root(mp,vpp)152 procfs_root(mp, vpp)
153 struct mount *mp;
154 struct vnode **vpp;
155 {
156 int error;
157
158 error = procfs_allocvp(mp, vpp, 0, Proot);
159 if (error)
160 return (error);
161 vn_lock(*vpp, LK_EXCLUSIVE, curproc);
162
163 return (0);
164 }
165
166 /* ARGSUSED */
167 int
procfs_start(mp,flags,p)168 procfs_start(mp, flags, p)
169 struct mount *mp;
170 int flags;
171 struct proc *p;
172 {
173
174 return (0);
175 }
176
177 /*
178 * Get file system statistics.
179 */
180 int
procfs_statfs(mp,sbp,p)181 procfs_statfs(mp, sbp, p)
182 struct mount *mp;
183 struct statfs *sbp;
184 struct proc *p;
185 {
186 struct vmtotal vmtotals;
187
188 uvm_total(&vmtotals);
189 sbp->f_bsize = PAGE_SIZE;
190 sbp->f_iosize = PAGE_SIZE;
191 sbp->f_blocks = vmtotals.t_vm;
192 sbp->f_bfree = vmtotals.t_vm - vmtotals.t_avm;
193 sbp->f_bavail = 0;
194 sbp->f_files = maxproc; /* approx */
195 sbp->f_ffree = maxproc - nprocs; /* approx */
196 if (sbp != &mp->mnt_stat) {
197 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
198 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
199 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
200 bcopy(&mp->mnt_stat.mount_info.procfs_args,
201 &sbp->mount_info.procfs_args, sizeof(struct procfs_args));
202 }
203 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
204 return (0);
205 }
206
207
208 #define procfs_sync ((int (*)(struct mount *, int, struct ucred *, \
209 struct proc *))nullop)
210
211 #define procfs_fhtovp ((int (*)(struct mount *, struct fid *, \
212 struct vnode **))eopnotsupp)
213 #define procfs_quotactl ((int (*)(struct mount *, int, uid_t, caddr_t, \
214 struct proc *))eopnotsupp)
215 #define procfs_sysctl ((int (*)(int *, u_int, void *, size_t *, void *, \
216 size_t, struct proc *))eopnotsupp)
217 #define procfs_vget ((int (*)(struct mount *, ino_t, struct vnode **)) \
218 eopnotsupp)
219 #define procfs_vptofh ((int (*)(struct vnode *, struct fid *))eopnotsupp)
220 #define procfs_checkexp ((int (*)(struct mount *, struct mbuf *, \
221 int *, struct ucred **))eopnotsupp)
222
223 const struct vfsops procfs_vfsops = {
224 procfs_mount,
225 procfs_start,
226 procfs_unmount,
227 procfs_root,
228 procfs_quotactl,
229 procfs_statfs,
230 procfs_sync,
231 procfs_vget,
232 procfs_fhtovp,
233 procfs_vptofh,
234 procfs_init,
235 procfs_sysctl,
236 procfs_checkexp
237 };
238