procfs_vfsops.c revision 1.74.12.2 1 1.74.12.2 pooka /* $NetBSD: procfs_vfsops.c,v 1.74.12.2 2007/07/31 21:14:17 pooka Exp $ */
2 1.74.12.2 pooka
3 1.74.12.2 pooka /*
4 1.74.12.2 pooka * Copyright (c) 1993
5 1.74.12.2 pooka * The Regents of the University of California. All rights reserved.
6 1.74.12.2 pooka *
7 1.74.12.2 pooka * This code is derived from software contributed to Berkeley by
8 1.74.12.2 pooka * Jan-Simon Pendry.
9 1.74.12.2 pooka *
10 1.74.12.2 pooka * Redistribution and use in source and binary forms, with or without
11 1.74.12.2 pooka * modification, are permitted provided that the following conditions
12 1.74.12.2 pooka * are met:
13 1.74.12.2 pooka * 1. Redistributions of source code must retain the above copyright
14 1.74.12.2 pooka * notice, this list of conditions and the following disclaimer.
15 1.74.12.2 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.74.12.2 pooka * notice, this list of conditions and the following disclaimer in the
17 1.74.12.2 pooka * documentation and/or other materials provided with the distribution.
18 1.74.12.2 pooka * 3. Neither the name of the University nor the names of its contributors
19 1.74.12.2 pooka * may be used to endorse or promote products derived from this software
20 1.74.12.2 pooka * without specific prior written permission.
21 1.74.12.2 pooka *
22 1.74.12.2 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.74.12.2 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.74.12.2 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.74.12.2 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.74.12.2 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.74.12.2 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.74.12.2 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.74.12.2 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.74.12.2 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.74.12.2 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.74.12.2 pooka * SUCH DAMAGE.
33 1.74.12.2 pooka *
34 1.74.12.2 pooka * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
35 1.74.12.2 pooka */
36 1.74.12.2 pooka
37 1.74.12.2 pooka /*
38 1.74.12.2 pooka * Copyright (c) 1993 Jan-Simon Pendry
39 1.74.12.2 pooka *
40 1.74.12.2 pooka * This code is derived from software contributed to Berkeley by
41 1.74.12.2 pooka * Jan-Simon Pendry.
42 1.74.12.2 pooka *
43 1.74.12.2 pooka * Redistribution and use in source and binary forms, with or without
44 1.74.12.2 pooka * modification, are permitted provided that the following conditions
45 1.74.12.2 pooka * are met:
46 1.74.12.2 pooka * 1. Redistributions of source code must retain the above copyright
47 1.74.12.2 pooka * notice, this list of conditions and the following disclaimer.
48 1.74.12.2 pooka * 2. Redistributions in binary form must reproduce the above copyright
49 1.74.12.2 pooka * notice, this list of conditions and the following disclaimer in the
50 1.74.12.2 pooka * documentation and/or other materials provided with the distribution.
51 1.74.12.2 pooka * 3. All advertising materials mentioning features or use of this software
52 1.74.12.2 pooka * must display the following acknowledgement:
53 1.74.12.2 pooka * This product includes software developed by the University of
54 1.74.12.2 pooka * California, Berkeley and its contributors.
55 1.74.12.2 pooka * 4. Neither the name of the University nor the names of its contributors
56 1.74.12.2 pooka * may be used to endorse or promote products derived from this software
57 1.74.12.2 pooka * without specific prior written permission.
58 1.74.12.2 pooka *
59 1.74.12.2 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.74.12.2 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.74.12.2 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.74.12.2 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.74.12.2 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.74.12.2 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.74.12.2 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.74.12.2 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.74.12.2 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.74.12.2 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.74.12.2 pooka * SUCH DAMAGE.
70 1.74.12.2 pooka *
71 1.74.12.2 pooka * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
72 1.74.12.2 pooka */
73 1.74.12.2 pooka
74 1.74.12.2 pooka /*
75 1.74.12.2 pooka * procfs VFS interface
76 1.74.12.2 pooka */
77 1.74.12.2 pooka
78 1.74.12.2 pooka #include <sys/cdefs.h>
79 1.74.12.2 pooka __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.74.12.2 2007/07/31 21:14:17 pooka Exp $");
80 1.74.12.2 pooka
81 1.74.12.2 pooka #if defined(_KERNEL_OPT)
82 1.74.12.2 pooka #include "opt_compat_netbsd.h"
83 1.74.12.2 pooka #endif
84 1.74.12.2 pooka
85 1.74.12.2 pooka #include <sys/param.h>
86 1.74.12.2 pooka #include <sys/time.h>
87 1.74.12.2 pooka #include <sys/kernel.h>
88 1.74.12.2 pooka #include <sys/systm.h>
89 1.74.12.2 pooka #include <sys/sysctl.h>
90 1.74.12.2 pooka #include <sys/proc.h>
91 1.74.12.2 pooka #include <sys/buf.h>
92 1.74.12.2 pooka #include <sys/syslog.h>
93 1.74.12.2 pooka #include <sys/mount.h>
94 1.74.12.2 pooka #include <sys/dirent.h>
95 1.74.12.2 pooka #include <sys/signalvar.h>
96 1.74.12.2 pooka #include <sys/vnode.h>
97 1.74.12.2 pooka #include <sys/malloc.h>
98 1.74.12.2 pooka #include <sys/kauth.h>
99 1.74.12.2 pooka
100 1.74.12.2 pooka #include <miscfs/procfs/procfs.h>
101 1.74.12.2 pooka
102 1.74.12.2 pooka #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
103 1.74.12.2 pooka
104 1.74.12.2 pooka VFS_PROTOS(procfs);
105 1.74.12.2 pooka
106 1.74.12.2 pooka /*
107 1.74.12.2 pooka * VFS Operations.
108 1.74.12.2 pooka *
109 1.74.12.2 pooka * mount system call
110 1.74.12.2 pooka */
111 1.74.12.2 pooka /* ARGSUSED */
112 1.74.12.2 pooka int
113 1.74.12.2 pooka procfs_mount(
114 1.74.12.2 pooka struct mount *mp,
115 1.74.12.2 pooka const char *path,
116 1.74.12.2 pooka void *data,
117 1.74.12.2 pooka size_t *data_len,
118 1.74.12.2 pooka struct lwp *l
119 1.74.12.2 pooka )
120 1.74.12.2 pooka {
121 1.74.12.2 pooka struct procfsmount *pmnt;
122 1.74.12.2 pooka struct procfs_args *args = data;
123 1.74.12.2 pooka int error;
124 1.74.12.2 pooka
125 1.74.12.2 pooka if (UIO_MX & (UIO_MX-1)) {
126 1.74.12.2 pooka log(LOG_ERR, "procfs: invalid directory entry size");
127 1.74.12.2 pooka return (EINVAL);
128 1.74.12.2 pooka }
129 1.74.12.2 pooka
130 1.74.12.2 pooka if (mp->mnt_flag & MNT_GETARGS) {
131 1.74.12.2 pooka if (*data_len < sizeof *args)
132 1.74.12.2 pooka return EINVAL;
133 1.74.12.2 pooka
134 1.74.12.2 pooka pmnt = VFSTOPROC(mp);
135 1.74.12.2 pooka if (pmnt == NULL)
136 1.74.12.2 pooka return EIO;
137 1.74.12.2 pooka args->version = PROCFS_ARGSVERSION;
138 1.74.12.2 pooka args->flags = pmnt->pmnt_flags;
139 1.74.12.2 pooka *data_len = sizeof *args;
140 1.74.12.2 pooka return 0;
141 1.74.12.2 pooka }
142 1.74.12.2 pooka
143 1.74.12.2 pooka if (mp->mnt_flag & MNT_UPDATE)
144 1.74.12.2 pooka return (EOPNOTSUPP);
145 1.74.12.2 pooka
146 1.74.12.2 pooka if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
147 1.74.12.2 pooka return EINVAL;
148 1.74.12.2 pooka
149 1.74.12.2 pooka pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
150 1.74.12.2 pooka M_UFSMNT, M_WAITOK); /* XXX need new malloc type */
151 1.74.12.2 pooka
152 1.74.12.2 pooka mp->mnt_stat.f_namemax = MAXNAMLEN;
153 1.74.12.2 pooka mp->mnt_flag |= MNT_LOCAL;
154 1.74.12.2 pooka mp->mnt_data = pmnt;
155 1.74.12.2 pooka vfs_getnewfsid(mp);
156 1.74.12.2 pooka
157 1.74.12.2 pooka error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
158 1.74.12.2 pooka mp->mnt_op->vfs_name, mp, l);
159 1.74.12.2 pooka pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
160 1.74.12.2 pooka if (*data_len >= sizeof *args)
161 1.74.12.2 pooka pmnt->pmnt_flags = args->flags;
162 1.74.12.2 pooka else
163 1.74.12.2 pooka pmnt->pmnt_flags = 0;
164 1.74.12.2 pooka
165 1.74.12.2 pooka return error;
166 1.74.12.2 pooka }
167 1.74.12.2 pooka
168 1.74.12.2 pooka /*
169 1.74.12.2 pooka * unmount system call
170 1.74.12.2 pooka */
171 1.74.12.2 pooka int
172 1.74.12.2 pooka procfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
173 1.74.12.2 pooka {
174 1.74.12.2 pooka int error;
175 1.74.12.2 pooka int flags = 0;
176 1.74.12.2 pooka
177 1.74.12.2 pooka if (mntflags & MNT_FORCE)
178 1.74.12.2 pooka flags |= FORCECLOSE;
179 1.74.12.2 pooka
180 1.74.12.2 pooka if ((error = vflush(mp, 0, flags)) != 0)
181 1.74.12.2 pooka return (error);
182 1.74.12.2 pooka
183 1.74.12.2 pooka exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
184 1.74.12.2 pooka
185 1.74.12.2 pooka free(mp->mnt_data, M_UFSMNT);
186 1.74.12.2 pooka mp->mnt_data = 0;
187 1.74.12.2 pooka
188 1.74.12.2 pooka return (0);
189 1.74.12.2 pooka }
190 1.74.12.2 pooka
191 1.74.12.2 pooka int
192 1.74.12.2 pooka procfs_root(mp, vpp)
193 1.74.12.2 pooka struct mount *mp;
194 1.74.12.2 pooka struct vnode **vpp;
195 1.74.12.2 pooka {
196 1.74.12.2 pooka
197 1.74.12.2 pooka return (procfs_allocvp(mp, vpp, 0, PFSroot, -1, NULL));
198 1.74.12.2 pooka }
199 1.74.12.2 pooka
200 1.74.12.2 pooka /* ARGSUSED */
201 1.74.12.2 pooka int
202 1.74.12.2 pooka procfs_start(struct mount *mp, int flags,
203 1.74.12.2 pooka struct lwp *l)
204 1.74.12.2 pooka {
205 1.74.12.2 pooka
206 1.74.12.2 pooka return (0);
207 1.74.12.2 pooka }
208 1.74.12.2 pooka
209 1.74.12.2 pooka /*
210 1.74.12.2 pooka * Get file system statistics.
211 1.74.12.2 pooka */
212 1.74.12.2 pooka int
213 1.74.12.2 pooka procfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
214 1.74.12.2 pooka {
215 1.74.12.2 pooka
216 1.74.12.2 pooka sbp->f_bsize = PAGE_SIZE;
217 1.74.12.2 pooka sbp->f_frsize = PAGE_SIZE;
218 1.74.12.2 pooka sbp->f_iosize = PAGE_SIZE;
219 1.74.12.2 pooka sbp->f_blocks = 1; /* avoid divide by zero in some df's */
220 1.74.12.2 pooka sbp->f_bfree = 0;
221 1.74.12.2 pooka sbp->f_bavail = 0;
222 1.74.12.2 pooka sbp->f_bresvd = 0;
223 1.74.12.2 pooka sbp->f_files = maxproc; /* approx */
224 1.74.12.2 pooka sbp->f_ffree = maxproc - nprocs; /* approx */
225 1.74.12.2 pooka sbp->f_favail = maxproc - nprocs; /* approx */
226 1.74.12.2 pooka sbp->f_fresvd = 0;
227 1.74.12.2 pooka copy_statvfs_info(sbp, mp);
228 1.74.12.2 pooka return (0);
229 1.74.12.2 pooka }
230 1.74.12.2 pooka
231 1.74.12.2 pooka /*ARGSUSED*/
232 1.74.12.2 pooka int
233 1.74.12.2 pooka procfs_quotactl(
234 1.74.12.2 pooka struct mount *mp,
235 1.74.12.2 pooka int cmds,
236 1.74.12.2 pooka uid_t uid,
237 1.74.12.2 pooka void *arg,
238 1.74.12.2 pooka struct lwp *l
239 1.74.12.2 pooka )
240 1.74.12.2 pooka {
241 1.74.12.2 pooka
242 1.74.12.2 pooka return (EOPNOTSUPP);
243 1.74.12.2 pooka }
244 1.74.12.2 pooka
245 1.74.12.2 pooka /*ARGSUSED*/
246 1.74.12.2 pooka int
247 1.74.12.2 pooka procfs_sync(
248 1.74.12.2 pooka struct mount *mp,
249 1.74.12.2 pooka int waitfor,
250 1.74.12.2 pooka kauth_cred_t uc,
251 1.74.12.2 pooka struct lwp *l
252 1.74.12.2 pooka )
253 1.74.12.2 pooka {
254 1.74.12.2 pooka
255 1.74.12.2 pooka return (0);
256 1.74.12.2 pooka }
257 1.74.12.2 pooka
258 1.74.12.2 pooka /*ARGSUSED*/
259 1.74.12.2 pooka int
260 1.74.12.2 pooka procfs_vget(struct mount *mp, ino_t ino,
261 1.74.12.2 pooka struct vnode **vpp)
262 1.74.12.2 pooka {
263 1.74.12.2 pooka return (EOPNOTSUPP);
264 1.74.12.2 pooka }
265 1.74.12.2 pooka
266 1.74.12.2 pooka void
267 1.74.12.2 pooka procfs_init()
268 1.74.12.2 pooka {
269 1.74.12.2 pooka procfs_hashinit();
270 1.74.12.2 pooka }
271 1.74.12.2 pooka
272 1.74.12.2 pooka void
273 1.74.12.2 pooka procfs_reinit()
274 1.74.12.2 pooka {
275 1.74.12.2 pooka procfs_hashreinit();
276 1.74.12.2 pooka }
277 1.74.12.2 pooka
278 1.74.12.2 pooka void
279 1.74.12.2 pooka procfs_done()
280 1.74.12.2 pooka {
281 1.74.12.2 pooka procfs_hashdone();
282 1.74.12.2 pooka }
283 1.74.12.2 pooka
284 1.74.12.2 pooka SYSCTL_SETUP(sysctl_vfs_procfs_setup, "sysctl vfs.procfs subtree setup")
285 1.74.12.2 pooka {
286 1.74.12.2 pooka
287 1.74.12.2 pooka sysctl_createv(clog, 0, NULL, NULL,
288 1.74.12.2 pooka CTLFLAG_PERMANENT,
289 1.74.12.2 pooka CTLTYPE_NODE, "vfs", NULL,
290 1.74.12.2 pooka NULL, 0, NULL, 0,
291 1.74.12.2 pooka CTL_VFS, CTL_EOL);
292 1.74.12.2 pooka sysctl_createv(clog, 0, NULL, NULL,
293 1.74.12.2 pooka CTLFLAG_PERMANENT,
294 1.74.12.2 pooka CTLTYPE_NODE, "procfs",
295 1.74.12.2 pooka SYSCTL_DESCR("Process file system"),
296 1.74.12.2 pooka NULL, 0, NULL, 0,
297 1.74.12.2 pooka CTL_VFS, 12, CTL_EOL);
298 1.74.12.2 pooka /*
299 1.74.12.2 pooka * XXX the "12" above could be dynamic, thereby eliminating
300 1.74.12.2 pooka * one more instance of the "number to vfs" mapping problem,
301 1.74.12.2 pooka * but "12" is the order as taken from sys/mount.h
302 1.74.12.2 pooka */
303 1.74.12.2 pooka }
304 1.74.12.2 pooka
305 1.74.12.2 pooka extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
306 1.74.12.2 pooka
307 1.74.12.2 pooka const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
308 1.74.12.2 pooka &procfs_vnodeop_opv_desc,
309 1.74.12.2 pooka NULL,
310 1.74.12.2 pooka };
311 1.74.12.2 pooka
312 1.74.12.2 pooka struct vfsops procfs_vfsops = {
313 1.74.12.2 pooka MOUNT_PROCFS,
314 1.74.12.2 pooka sizeof (struct procfs_args),
315 1.74.12.2 pooka procfs_mount,
316 1.74.12.2 pooka procfs_start,
317 1.74.12.2 pooka procfs_unmount,
318 1.74.12.2 pooka procfs_root,
319 1.74.12.2 pooka procfs_quotactl,
320 1.74.12.2 pooka procfs_statvfs,
321 1.74.12.2 pooka procfs_sync,
322 1.74.12.2 pooka procfs_vget,
323 1.74.12.2 pooka (void *)eopnotsupp, /* vfs_fhtovp */
324 1.74.12.2 pooka (void *)eopnotsupp, /* vfs_vptofh */
325 1.74.12.2 pooka procfs_init,
326 1.74.12.2 pooka procfs_reinit,
327 1.74.12.2 pooka procfs_done,
328 1.74.12.2 pooka NULL, /* vfs_mountroot */
329 1.74.12.2 pooka (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
330 1.74.12.2 pooka vfs_stdextattrctl,
331 1.74.12.2 pooka (void *)eopnotsupp, /* vfs_suspendctl */
332 1.74.12.2 pooka procfs_vnodeopv_descs,
333 1.74.12.2 pooka 0,
334 1.74.12.2 pooka { NULL, NULL },
335 1.74.12.2 pooka };
336 1.74.12.2 pooka VFS_ATTACH(procfs_vfsops);
337