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