xref: /netbsd-src/sys/miscfs/procfs/procfs_vfsops.c (revision c0179c282a5968435315a82f4128c61372c68fc3)
1 /*	$NetBSD: procfs_vfsops.c,v 1.67 2006/11/16 01:33:38 christos 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.67 2006/11/16 01:33:38 christos 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 #include <sys/kauth.h>
99 
100 #include <miscfs/procfs/procfs.h>
101 
102 #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
103 
104 void	procfs_init(void);
105 void	procfs_reinit(void);
106 void	procfs_done(void);
107 int	procfs_mount(struct mount *, const char *, void *,
108 			  struct nameidata *, struct lwp *);
109 int	procfs_start(struct mount *, int, struct lwp *);
110 int	procfs_unmount(struct mount *, int, struct lwp *);
111 int	procfs_quotactl(struct mount *, int, uid_t, void *,
112 			     struct lwp *);
113 int	procfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
114 int	procfs_sync(struct mount *, int, kauth_cred_t, struct lwp *);
115 int	procfs_vget(struct mount *, ino_t, struct vnode **);
116 
117 /*
118  * VFS Operations.
119  *
120  * mount system call
121  */
122 /* ARGSUSED */
123 int
124 procfs_mount(
125     struct mount *mp,
126     const char *path,
127     void *data,
128     struct nameidata *ndp,
129     struct lwp *l
130 )
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 	pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
164 	    M_UFSMNT, M_WAITOK);   /* XXX need new malloc type */
165 
166 	mp->mnt_stat.f_namemax = MAXNAMLEN;
167 	mp->mnt_flag |= MNT_LOCAL;
168 	mp->mnt_data = pmnt;
169 	vfs_getnewfsid(mp);
170 
171 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
172 	    mp, l);
173 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
174 	pmnt->pmnt_flags = args.flags;
175 
176 	return error;
177 }
178 
179 /*
180  * unmount system call
181  */
182 int
183 procfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
184 {
185 	int error;
186 	int flags = 0;
187 
188 	if (mntflags & MNT_FORCE)
189 		flags |= FORCECLOSE;
190 
191 	if ((error = vflush(mp, 0, flags)) != 0)
192 		return (error);
193 
194 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
195 
196 	free(mp->mnt_data, M_UFSMNT);
197 	mp->mnt_data = 0;
198 
199 	return (0);
200 }
201 
202 int
203 procfs_root(mp, vpp)
204 	struct mount *mp;
205 	struct vnode **vpp;
206 {
207 
208 	return (procfs_allocvp(mp, vpp, 0, PFSroot, -1));
209 }
210 
211 /* ARGSUSED */
212 int
213 procfs_start(struct mount *mp, int flags,
214     struct lwp *l)
215 {
216 
217 	return (0);
218 }
219 
220 /*
221  * Get file system statistics.
222  */
223 int
224 procfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
225 {
226 
227 	sbp->f_bsize = PAGE_SIZE;
228 	sbp->f_frsize = PAGE_SIZE;
229 	sbp->f_iosize = PAGE_SIZE;
230 	sbp->f_blocks = 1;	/* avoid divide by zero in some df's */
231 	sbp->f_bfree = 0;
232 	sbp->f_bavail = 0;
233 	sbp->f_bresvd = 0;
234 	sbp->f_files = maxproc;			/* approx */
235 	sbp->f_ffree = maxproc - nprocs;	/* approx */
236 	sbp->f_favail = maxproc - nprocs;	/* approx */
237 	sbp->f_fresvd = 0;
238 	copy_statvfs_info(sbp, mp);
239 	return (0);
240 }
241 
242 /*ARGSUSED*/
243 int
244 procfs_quotactl(
245     struct mount *mp,
246     int cmds,
247     uid_t uid,
248     void *arg,
249     struct lwp *l
250 )
251 {
252 
253 	return (EOPNOTSUPP);
254 }
255 
256 /*ARGSUSED*/
257 int
258 procfs_sync(
259     struct mount *mp,
260     int waitfor,
261     kauth_cred_t uc,
262     struct lwp *l
263 )
264 {
265 
266 	return (0);
267 }
268 
269 /*ARGSUSED*/
270 int
271 procfs_vget(struct mount *mp, ino_t ino,
272     struct vnode **vpp)
273 {
274 	return (EOPNOTSUPP);
275 }
276 
277 void
278 procfs_init()
279 {
280 	procfs_hashinit();
281 }
282 
283 void
284 procfs_reinit()
285 {
286 	procfs_hashreinit();
287 }
288 
289 void
290 procfs_done()
291 {
292 	procfs_hashdone();
293 }
294 
295 SYSCTL_SETUP(sysctl_vfs_procfs_setup, "sysctl vfs.procfs subtree setup")
296 {
297 
298 	sysctl_createv(clog, 0, NULL, NULL,
299 		       CTLFLAG_PERMANENT,
300 		       CTLTYPE_NODE, "vfs", NULL,
301 		       NULL, 0, NULL, 0,
302 		       CTL_VFS, CTL_EOL);
303 	sysctl_createv(clog, 0, NULL, NULL,
304 		       CTLFLAG_PERMANENT,
305 		       CTLTYPE_NODE, "procfs",
306 		       SYSCTL_DESCR("Process file system"),
307 		       NULL, 0, NULL, 0,
308 		       CTL_VFS, 12, CTL_EOL);
309 	/*
310 	 * XXX the "12" above could be dynamic, thereby eliminating
311 	 * one more instance of the "number to vfs" mapping problem,
312 	 * but "12" is the order as taken from sys/mount.h
313 	 */
314 }
315 
316 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
317 
318 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
319 	&procfs_vnodeop_opv_desc,
320 	NULL,
321 };
322 
323 struct vfsops procfs_vfsops = {
324 	MOUNT_PROCFS,
325 	procfs_mount,
326 	procfs_start,
327 	procfs_unmount,
328 	procfs_root,
329 	procfs_quotactl,
330 	procfs_statvfs,
331 	procfs_sync,
332 	procfs_vget,
333 	NULL,				/* vfs_fhtovp */
334 	NULL,				/* vfs_vptofh */
335 	procfs_init,
336 	procfs_reinit,
337 	procfs_done,
338 	NULL,				/* vfs_mountroot */
339 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
340 	vfs_stdextattrctl,
341 	procfs_vnodeopv_descs,
342 	0,
343 	{ NULL, NULL },
344 };
345 VFS_ATTACH(procfs_vfsops);
346