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