xref: /netbsd-src/sys/miscfs/procfs/procfs_vfsops.c (revision 717e1785a5b64a5de249cb35e8c3b6fe608e6ea1)
1 /*	$NetBSD: procfs_vfsops.c,v 1.77 2008/01/28 14:31:19 dholland 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.77 2008/01/28 14:31:19 dholland 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/genfs/genfs.h>
101 
102 #include <miscfs/procfs/procfs.h>
103 
104 #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
105 
106 VFS_PROTOS(procfs);
107 
108 /*
109  * VFS Operations.
110  *
111  * mount system call
112  */
113 /* ARGSUSED */
114 int
115 procfs_mount(
116     struct mount *mp,
117     const char *path,
118     void *data,
119     size_t *data_len)
120 {
121 	struct lwp *l = curlwp;
122 	struct procfsmount *pmnt;
123 	struct procfs_args *args = data;
124 	int error;
125 
126 	if (UIO_MX & (UIO_MX-1)) {
127 		log(LOG_ERR, "procfs: invalid directory entry size");
128 		return (EINVAL);
129 	}
130 
131 	if (mp->mnt_flag & MNT_GETARGS) {
132 		if (*data_len < sizeof *args)
133 			return EINVAL;
134 
135 		pmnt = VFSTOPROC(mp);
136 		if (pmnt == NULL)
137 			return EIO;
138 		args->version = PROCFS_ARGSVERSION;
139 		args->flags = pmnt->pmnt_flags;
140 		*data_len = sizeof *args;
141 		return 0;
142 	}
143 
144 	if (mp->mnt_flag & MNT_UPDATE)
145 		return (EOPNOTSUPP);
146 
147 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
148 		return EINVAL;
149 
150 	pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
151 	    M_UFSMNT, M_WAITOK);   /* XXX need new malloc type */
152 
153 	mp->mnt_stat.f_namemax = MAXNAMLEN;
154 	mp->mnt_flag |= MNT_LOCAL;
155 	mp->mnt_data = pmnt;
156 	vfs_getnewfsid(mp);
157 
158 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
159 	    mp->mnt_op->vfs_name, mp, l);
160 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
161 	if (*data_len >= sizeof *args)
162 		pmnt->pmnt_flags = args->flags;
163 	else
164 		pmnt->pmnt_flags = 0;
165 
166 	mp->mnt_iflag |= IMNT_MPSAFE;
167 	return error;
168 }
169 
170 /*
171  * unmount system call
172  */
173 int
174 procfs_unmount(struct mount *mp, int mntflags)
175 {
176 	int error;
177 	int flags = 0;
178 
179 	if (mntflags & MNT_FORCE)
180 		flags |= FORCECLOSE;
181 
182 	if ((error = vflush(mp, 0, flags)) != 0)
183 		return (error);
184 
185 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
186 
187 	free(mp->mnt_data, M_UFSMNT);
188 	mp->mnt_data = 0;
189 
190 	return (0);
191 }
192 
193 int
194 procfs_root(mp, vpp)
195 	struct mount *mp;
196 	struct vnode **vpp;
197 {
198 
199 	return (procfs_allocvp(mp, vpp, 0, PFSroot, -1, NULL));
200 }
201 
202 /* ARGSUSED */
203 int
204 procfs_start(struct mount *mp, int flags)
205 {
206 
207 	return (0);
208 }
209 
210 /*
211  * Get file system statistics.
212  */
213 int
214 procfs_statvfs(struct mount *mp, struct statvfs *sbp)
215 {
216 
217 	sbp->f_bsize = PAGE_SIZE;
218 	sbp->f_frsize = PAGE_SIZE;
219 	sbp->f_iosize = PAGE_SIZE;
220 	sbp->f_blocks = 1;	/* avoid divide by zero in some df's */
221 	sbp->f_bfree = 0;
222 	sbp->f_bavail = 0;
223 	sbp->f_bresvd = 0;
224 	sbp->f_files = maxproc;			/* approx */
225 	sbp->f_ffree = maxproc - nprocs;	/* approx */
226 	sbp->f_favail = maxproc - nprocs;	/* approx */
227 	sbp->f_fresvd = 0;
228 	copy_statvfs_info(sbp, mp);
229 	return (0);
230 }
231 
232 /*ARGSUSED*/
233 int
234 procfs_sync(
235     struct mount *mp,
236     int waitfor,
237     kauth_cred_t uc)
238 {
239 
240 	return (0);
241 }
242 
243 /*ARGSUSED*/
244 int
245 procfs_vget(struct mount *mp, ino_t ino,
246     struct vnode **vpp)
247 {
248 	return (EOPNOTSUPP);
249 }
250 
251 void
252 procfs_init()
253 {
254 	procfs_hashinit();
255 }
256 
257 void
258 procfs_reinit()
259 {
260 	procfs_hashreinit();
261 }
262 
263 void
264 procfs_done()
265 {
266 	procfs_hashdone();
267 }
268 
269 SYSCTL_SETUP(sysctl_vfs_procfs_setup, "sysctl vfs.procfs subtree setup")
270 {
271 
272 	sysctl_createv(clog, 0, NULL, NULL,
273 		       CTLFLAG_PERMANENT,
274 		       CTLTYPE_NODE, "vfs", NULL,
275 		       NULL, 0, NULL, 0,
276 		       CTL_VFS, CTL_EOL);
277 	sysctl_createv(clog, 0, NULL, NULL,
278 		       CTLFLAG_PERMANENT,
279 		       CTLTYPE_NODE, "procfs",
280 		       SYSCTL_DESCR("Process file system"),
281 		       NULL, 0, NULL, 0,
282 		       CTL_VFS, 12, CTL_EOL);
283 	/*
284 	 * XXX the "12" above could be dynamic, thereby eliminating
285 	 * one more instance of the "number to vfs" mapping problem,
286 	 * but "12" is the order as taken from sys/mount.h
287 	 */
288 }
289 
290 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
291 
292 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
293 	&procfs_vnodeop_opv_desc,
294 	NULL,
295 };
296 
297 struct vfsops procfs_vfsops = {
298 	MOUNT_PROCFS,
299 	sizeof (struct procfs_args),
300 	procfs_mount,
301 	procfs_start,
302 	procfs_unmount,
303 	procfs_root,
304 	(void *)eopnotsupp,		/* vfs_quotactl */
305 	procfs_statvfs,
306 	procfs_sync,
307 	procfs_vget,
308 	(void *)eopnotsupp,		/* vfs_fhtovp */
309 	(void *)eopnotsupp,		/* vfs_vptofh */
310 	procfs_init,
311 	procfs_reinit,
312 	procfs_done,
313 	NULL,				/* vfs_mountroot */
314 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
315 	vfs_stdextattrctl,
316 	(void *)eopnotsupp,		/* vfs_suspendctl */
317 	genfs_renamelock_enter,
318 	genfs_renamelock_exit,
319 	procfs_vnodeopv_descs,
320 	0,
321 	{ NULL, NULL },
322 };
323 VFS_ATTACH(procfs_vfsops);
324