procfs_vfsops.c revision 1.62 1 /* $NetBSD: procfs_vfsops.c,v 1.62 2005/09/23 12:10:33 jmmv Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
35 */
36
37 /*
38 * Copyright (c) 1993 Jan-Simon Pendry
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
72 */
73
74 /*
75 * procfs VFS interface
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.62 2005/09/23 12:10:33 jmmv Exp $");
80
81 #if defined(_KERNEL_OPT)
82 #include "opt_compat_netbsd.h"
83 #endif
84
85 #include <sys/param.h>
86 #include <sys/time.h>
87 #include <sys/kernel.h>
88 #include <sys/systm.h>
89 #include <sys/sysctl.h>
90 #include <sys/proc.h>
91 #include <sys/buf.h>
92 #include <sys/syslog.h>
93 #include <sys/mount.h>
94 #include <sys/dirent.h>
95 #include <sys/signalvar.h>
96 #include <sys/vnode.h>
97 #include <sys/malloc.h>
98
99 #include <miscfs/procfs/procfs.h>
100
101 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
102
103 void procfs_init(void);
104 void procfs_reinit(void);
105 void procfs_done(void);
106 int procfs_mount(struct mount *, const char *, void *,
107 struct nameidata *, struct proc *);
108 int procfs_start(struct mount *, int, struct proc *);
109 int procfs_unmount(struct mount *, int, struct proc *);
110 int procfs_quotactl(struct mount *, int, uid_t, void *,
111 struct proc *);
112 int procfs_statvfs(struct mount *, struct statvfs *, struct proc *);
113 int procfs_sync(struct mount *, int, struct ucred *, struct proc *);
114 int procfs_vget(struct mount *, ino_t, struct vnode **);
115
116 /*
117 * VFS Operations.
118 *
119 * mount system call
120 */
121 /* ARGSUSED */
122 int
123 procfs_mount(mp, path, data, ndp, p)
124 struct mount *mp;
125 const char *path;
126 void *data;
127 struct nameidata *ndp;
128 struct proc *p;
129 {
130 struct procfsmount *pmnt;
131 struct procfs_args args;
132 int error;
133
134 if (UIO_MX & (UIO_MX-1)) {
135 log(LOG_ERR, "procfs: invalid directory entry size");
136 return (EINVAL);
137 }
138
139 if (mp->mnt_flag & MNT_GETARGS) {
140 pmnt = VFSTOPROC(mp);
141 if (pmnt == NULL)
142 return EIO;
143 args.version = PROCFS_ARGSVERSION;
144 args.flags = pmnt->pmnt_flags;
145 return copyout(&args, data, sizeof(args));
146 }
147
148 if (mp->mnt_flag & MNT_UPDATE)
149 return (EOPNOTSUPP);
150
151 if (data != NULL) {
152 error = copyin(data, &args, sizeof args);
153 if (error != 0)
154 return error;
155
156 if (args.version != PROCFS_ARGSVERSION)
157 return EINVAL;
158 } else
159 args.flags = 0;
160
161 pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
162 M_UFSMNT, M_WAITOK); /* XXX need new malloc type */
163
164 mp->mnt_stat.f_namemax = MAXNAMLEN;
165 mp->mnt_flag |= MNT_LOCAL;
166 mp->mnt_data = pmnt;
167 vfs_getnewfsid(mp);
168
169 error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
170 mp, p);
171 pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
172 pmnt->pmnt_flags = args.flags;
173
174 return error;
175 }
176
177 /*
178 * unmount system call
179 */
180 int
181 procfs_unmount(mp, mntflags, p)
182 struct mount *mp;
183 int mntflags;
184 struct proc *p;
185 {
186 int error;
187 int flags = 0;
188
189 if (mntflags & MNT_FORCE)
190 flags |= FORCECLOSE;
191
192 if ((error = vflush(mp, 0, flags)) != 0)
193 return (error);
194
195 exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
196
197 free(mp->mnt_data, M_UFSMNT);
198 mp->mnt_data = 0;
199
200 return (0);
201 }
202
203 int
204 procfs_root(mp, vpp)
205 struct mount *mp;
206 struct vnode **vpp;
207 {
208
209 return (procfs_allocvp(mp, vpp, 0, PFSroot, -1));
210 }
211
212 /* ARGSUSED */
213 int
214 procfs_start(mp, flags, p)
215 struct mount *mp;
216 int flags;
217 struct proc *p;
218 {
219
220 return (0);
221 }
222
223 /*
224 * Get file system statistics.
225 */
226 int
227 procfs_statvfs(mp, sbp, p)
228 struct mount *mp;
229 struct statvfs *sbp;
230 struct proc *p;
231 {
232
233 sbp->f_bsize = PAGE_SIZE;
234 sbp->f_frsize = PAGE_SIZE;
235 sbp->f_iosize = PAGE_SIZE;
236 sbp->f_blocks = 1; /* avoid divide by zero in some df's */
237 sbp->f_bfree = 0;
238 sbp->f_bavail = 0;
239 sbp->f_bresvd = 0;
240 sbp->f_files = maxproc; /* approx */
241 sbp->f_ffree = maxproc - nprocs; /* approx */
242 sbp->f_favail = maxproc - nprocs; /* approx */
243 sbp->f_fresvd = 0;
244 copy_statvfs_info(sbp, mp);
245 return (0);
246 }
247
248 /*ARGSUSED*/
249 int
250 procfs_quotactl(mp, cmds, uid, arg, p)
251 struct mount *mp;
252 int cmds;
253 uid_t uid;
254 void *arg;
255 struct proc *p;
256 {
257
258 return (EOPNOTSUPP);
259 }
260
261 /*ARGSUSED*/
262 int
263 procfs_sync(mp, waitfor, uc, p)
264 struct mount *mp;
265 int waitfor;
266 struct ucred *uc;
267 struct proc *p;
268 {
269
270 return (0);
271 }
272
273 /*ARGSUSED*/
274 int
275 procfs_vget(mp, ino, vpp)
276 struct mount *mp;
277 ino_t ino;
278 struct vnode **vpp;
279 {
280 return (EOPNOTSUPP);
281 }
282
283 void
284 procfs_init()
285 {
286 procfs_hashinit();
287 }
288
289 void
290 procfs_reinit()
291 {
292 procfs_hashreinit();
293 }
294
295 void
296 procfs_done()
297 {
298 procfs_hashdone();
299 }
300
301 SYSCTL_SETUP(sysctl_vfs_procfs_setup, "sysctl vfs.procfs subtree setup")
302 {
303
304 sysctl_createv(clog, 0, NULL, NULL,
305 CTLFLAG_PERMANENT,
306 CTLTYPE_NODE, "vfs", NULL,
307 NULL, 0, NULL, 0,
308 CTL_VFS, CTL_EOL);
309 sysctl_createv(clog, 0, NULL, NULL,
310 CTLFLAG_PERMANENT,
311 CTLTYPE_NODE, "procfs",
312 SYSCTL_DESCR("Process file system"),
313 NULL, 0, NULL, 0,
314 CTL_VFS, 12, CTL_EOL);
315 /*
316 * XXX the "12" above could be dynamic, thereby eliminating
317 * one more instance of the "number to vfs" mapping problem,
318 * but "12" is the order as taken from sys/mount.h
319 */
320 }
321
322 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
323
324 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
325 &procfs_vnodeop_opv_desc,
326 NULL,
327 };
328
329 struct vfsops procfs_vfsops = {
330 MOUNT_PROCFS,
331 procfs_mount,
332 procfs_start,
333 procfs_unmount,
334 procfs_root,
335 procfs_quotactl,
336 procfs_statvfs,
337 procfs_sync,
338 procfs_vget,
339 NULL, /* vfs_fhtovp */
340 NULL, /* vfs_vptofh */
341 procfs_init,
342 procfs_reinit,
343 procfs_done,
344 NULL, /* vfs_mountroot */
345 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
346 vfs_stdextattrctl,
347 procfs_vnodeopv_descs,
348 };
349 VFS_ATTACH(procfs_vfsops);
350