procfs_vfsops.c revision 1.51 1 /* $NetBSD: procfs_vfsops.c,v 1.51 2003/12/04 19:38:24 atatat 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.51 2003/12/04 19:38:24 atatat 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/signalvar.h>
95 #include <sys/vnode.h>
96 #include <sys/malloc.h>
97
98 #include <miscfs/procfs/procfs.h>
99
100 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
101
102 void procfs_init __P((void));
103 void procfs_reinit __P((void));
104 void procfs_done __P((void));
105 int procfs_mount __P((struct mount *, const char *, void *,
106 struct nameidata *, struct proc *));
107 int procfs_start __P((struct mount *, int, struct proc *));
108 int procfs_unmount __P((struct mount *, int, struct proc *));
109 int procfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
110 struct proc *));
111 int procfs_statfs __P((struct mount *, struct statfs *, struct proc *));
112 int procfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
113 int procfs_vget __P((struct mount *, ino_t, struct vnode **));
114 int procfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
115 int procfs_checkexp __P((struct mount *, struct mbuf *, int *,
116 struct ucred **));
117 int procfs_vptofh __P((struct vnode *, struct fid *));
118 /*
119 * VFS Operations.
120 *
121 * mount system call
122 */
123 /* ARGSUSED */
124 int
125 procfs_mount(mp, path, data, ndp, p)
126 struct mount *mp;
127 const char *path;
128 void *data;
129 struct nameidata *ndp;
130 struct proc *p;
131 {
132 struct procfsmount *pmnt;
133 struct procfs_args args;
134 int error;
135
136 if (UIO_MX & (UIO_MX-1)) {
137 log(LOG_ERR, "procfs: invalid directory entry size");
138 return (EINVAL);
139 }
140
141 if (mp->mnt_flag & MNT_GETARGS) {
142 pmnt = VFSTOPROC(mp);
143 if (pmnt == NULL)
144 return EIO;
145 args.version = PROCFS_ARGSVERSION;
146 args.flags = pmnt->pmnt_flags;
147 return copyout(&args, data, sizeof(args));
148 }
149
150 if (mp->mnt_flag & MNT_UPDATE)
151 return (EOPNOTSUPP);
152
153 if (data != NULL) {
154 error = copyin(data, &args, sizeof args);
155 if (error != 0)
156 return error;
157
158 if (args.version != PROCFS_ARGSVERSION)
159 return EINVAL;
160 } else
161 args.flags = 0;
162
163 mp->mnt_flag |= MNT_LOCAL;
164 pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
165 M_UFSMNT, M_WAITOK); /* XXX need new malloc type */
166
167 mp->mnt_data = pmnt;
168 vfs_getnewfsid(mp);
169
170 error = set_statfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
171 mp, p);
172 pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
173 pmnt->pmnt_flags = args.flags;
174
175 return error;
176 }
177
178 /*
179 * unmount system call
180 */
181 int
182 procfs_unmount(mp, mntflags, p)
183 struct mount *mp;
184 int mntflags;
185 struct proc *p;
186 {
187 int error;
188 int flags = 0;
189
190 if (mntflags & MNT_FORCE)
191 flags |= FORCECLOSE;
192
193 if ((error = vflush(mp, 0, flags)) != 0)
194 return (error);
195
196 exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
197
198 free(mp->mnt_data, M_UFSMNT);
199 mp->mnt_data = 0;
200
201 return (0);
202 }
203
204 int
205 procfs_root(mp, vpp)
206 struct mount *mp;
207 struct vnode **vpp;
208 {
209
210 return (procfs_allocvp(mp, vpp, 0, PFSroot, -1));
211 }
212
213 /* ARGSUSED */
214 int
215 procfs_start(mp, flags, p)
216 struct mount *mp;
217 int flags;
218 struct proc *p;
219 {
220
221 return (0);
222 }
223
224 /*
225 * Get file system statistics.
226 */
227 int
228 procfs_statfs(mp, sbp, p)
229 struct mount *mp;
230 struct statfs *sbp;
231 struct proc *p;
232 {
233
234 sbp->f_bsize = 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_files = maxproc; /* approx */
240 sbp->f_ffree = maxproc - nprocs; /* approx */
241 #ifdef COMPAT_09
242 sbp->f_type = 10;
243 #else
244 sbp->f_type = 0;
245 #endif
246 copy_statfs_info(sbp, mp);
247 return (0);
248 }
249
250 /*ARGSUSED*/
251 int
252 procfs_quotactl(mp, cmds, uid, arg, p)
253 struct mount *mp;
254 int cmds;
255 uid_t uid;
256 caddr_t arg;
257 struct proc *p;
258 {
259
260 return (EOPNOTSUPP);
261 }
262
263 /*ARGSUSED*/
264 int
265 procfs_sync(mp, waitfor, uc, p)
266 struct mount *mp;
267 int waitfor;
268 struct ucred *uc;
269 struct proc *p;
270 {
271
272 return (0);
273 }
274
275 /*ARGSUSED*/
276 int
277 procfs_vget(mp, ino, vpp)
278 struct mount *mp;
279 ino_t ino;
280 struct vnode **vpp;
281 {
282 return (EOPNOTSUPP);
283 }
284
285 /*ARGSUSED*/
286 int
287 procfs_fhtovp(mp, fhp, vpp)
288 struct mount *mp;
289 struct fid *fhp;
290 struct vnode **vpp;
291 {
292
293 return (EINVAL);
294 }
295
296 /*ARGSUSED*/
297 int
298 procfs_checkexp(mp, mb, what, anon)
299 struct mount *mp;
300 struct mbuf *mb;
301 int *what;
302 struct ucred **anon;
303 {
304
305 return (EINVAL);
306 }
307
308 /*ARGSUSED*/
309 int
310 procfs_vptofh(vp, fhp)
311 struct vnode *vp;
312 struct fid *fhp;
313 {
314
315 return (EINVAL);
316 }
317
318 void
319 procfs_init()
320 {
321 procfs_hashinit();
322 }
323
324 void
325 procfs_reinit()
326 {
327 procfs_hashreinit();
328 }
329
330 void
331 procfs_done()
332 {
333 procfs_hashdone();
334 }
335
336 SYSCTL_SETUP(sysctl_vfs_procfs_setup, "sysctl vfs.procfs subtree setup")
337 {
338
339 sysctl_createv(SYSCTL_PERMANENT,
340 CTLTYPE_NODE, "vfs", NULL,
341 NULL, 0, NULL, 0,
342 CTL_VFS, CTL_EOL);
343 sysctl_createv(SYSCTL_PERMANENT,
344 CTLTYPE_NODE, "procfs", NULL,
345 NULL, 0, NULL, 0,
346 CTL_VFS, 12, CTL_EOL);
347 /*
348 * XXX the "12" above could be dynamic, thereby eliminating
349 * one more instance of the "number to vfs" mapping problem,
350 * but "12" is the order as taken from sys/mount.h
351 */
352 }
353
354 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
355
356 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
357 &procfs_vnodeop_opv_desc,
358 NULL,
359 };
360
361 struct vfsops procfs_vfsops = {
362 MOUNT_PROCFS,
363 procfs_mount,
364 procfs_start,
365 procfs_unmount,
366 procfs_root,
367 procfs_quotactl,
368 procfs_statfs,
369 procfs_sync,
370 procfs_vget,
371 procfs_fhtovp,
372 procfs_vptofh,
373 procfs_init,
374 procfs_reinit,
375 procfs_done,
376 NULL,
377 NULL, /* vfs_mountroot */
378 procfs_checkexp,
379 procfs_vnodeopv_descs,
380 };
381