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