fdesc_vfsops.c revision 1.82 1 1.82 pooka /* $NetBSD: fdesc_vfsops.c,v 1.82 2009/07/31 19:47:47 pooka Exp $ */
2 1.13 cgd
3 1.1 cgd /*
4 1.24 fvdl * Copyright (c) 1992, 1993, 1995
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.6 cgd * This code is derived from software donated to Berkeley by
8 1.1 cgd * Jan-Simon Pendry.
9 1.1 cgd *
10 1.2 cgd * Redistribution and use in source and binary forms, with or without
11 1.2 cgd * modification, are permitted provided that the following conditions
12 1.2 cgd * are met:
13 1.2 cgd * 1. Redistributions of source code must retain the above copyright
14 1.2 cgd * notice, this list of conditions and the following disclaimer.
15 1.2 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 cgd * notice, this list of conditions and the following disclaimer in the
17 1.2 cgd * documentation and/or other materials provided with the distribution.
18 1.44 agc * 3. Neither the name of the University nor the names of its contributors
19 1.2 cgd * may be used to endorse or promote products derived from this software
20 1.2 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.2 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.2 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.2 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.2 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.2 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.2 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.2 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.2 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.2 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.24 fvdl * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95
35 1.15 mycroft *
36 1.15 mycroft * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd /*
40 1.1 cgd * /dev/fd Filesystem
41 1.1 cgd */
42 1.35 lukem
43 1.35 lukem #include <sys/cdefs.h>
44 1.82 pooka __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.82 2009/07/31 19:47:47 pooka Exp $");
45 1.25 jonathan
46 1.33 mrg #if defined(_KERNEL_OPT)
47 1.25 jonathan #include "opt_compat_netbsd.h"
48 1.25 jonathan #endif
49 1.1 cgd
50 1.6 cgd #include <sys/param.h>
51 1.6 cgd #include <sys/systm.h>
52 1.45 atatat #include <sys/sysctl.h>
53 1.6 cgd #include <sys/time.h>
54 1.6 cgd #include <sys/proc.h>
55 1.6 cgd #include <sys/resourcevar.h>
56 1.6 cgd #include <sys/filedesc.h>
57 1.6 cgd #include <sys/vnode.h>
58 1.6 cgd #include <sys/mount.h>
59 1.48 christos #include <sys/dirent.h>
60 1.6 cgd #include <sys/namei.h>
61 1.6 cgd #include <sys/malloc.h>
62 1.59 elad #include <sys/kauth.h>
63 1.75 rumble #include <sys/module.h>
64 1.59 elad
65 1.73 dholland #include <miscfs/genfs/genfs.h>
66 1.6 cgd #include <miscfs/fdesc/fdesc.h>
67 1.1 cgd
68 1.75 rumble MODULE(MODULE_CLASS_VFS, fdesc, NULL);
69 1.75 rumble
70 1.69 pooka VFS_PROTOS(fdesc);
71 1.21 christos
72 1.77 rumble static struct sysctllog *fdesc_sysctl_log;
73 1.77 rumble
74 1.1 cgd /*
75 1.1 cgd * Mount the per-process file descriptors (/dev/fd)
76 1.1 cgd */
77 1.12 mycroft int
78 1.71 pooka fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
79 1.1 cgd {
80 1.71 pooka struct lwp *l = curlwp;
81 1.1 cgd int error = 0;
82 1.1 cgd struct vnode *rvp;
83 1.1 cgd
84 1.66 dsl if (mp->mnt_flag & MNT_GETARGS) {
85 1.66 dsl *data_len = 0;
86 1.38 christos return 0;
87 1.66 dsl }
88 1.1 cgd /*
89 1.1 cgd * Update is a no-op
90 1.1 cgd */
91 1.1 cgd if (mp->mnt_flag & MNT_UPDATE)
92 1.1 cgd return (EOPNOTSUPP);
93 1.1 cgd
94 1.6 cgd error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
95 1.1 cgd if (error)
96 1.1 cgd return (error);
97 1.1 cgd
98 1.1 cgd rvp->v_type = VDIR;
99 1.70 ad rvp->v_vflag |= VV_ROOT;
100 1.52 jdolecek mp->mnt_stat.f_namemax = MAXNAMLEN;
101 1.14 mycroft mp->mnt_flag |= MNT_LOCAL;
102 1.82 pooka mp->mnt_data = rvp;
103 1.31 assar vfs_getnewfsid(mp);
104 1.1 cgd
105 1.47 christos error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
106 1.67 pooka mp->mnt_op->vfs_name, mp, l);
107 1.28 wrstuden VOP_UNLOCK(rvp, 0);
108 1.39 christos return error;
109 1.1 cgd }
110 1.1 cgd
111 1.12 mycroft int
112 1.71 pooka fdesc_start(struct mount *mp, int flags)
113 1.1 cgd {
114 1.1 cgd return (0);
115 1.1 cgd }
116 1.1 cgd
117 1.12 mycroft int
118 1.71 pooka fdesc_unmount(struct mount *mp, int mntflags)
119 1.1 cgd {
120 1.1 cgd int error;
121 1.1 cgd int flags = 0;
122 1.82 pooka struct vnode *rtvp = mp->mnt_data;
123 1.1 cgd
124 1.24 fvdl if (mntflags & MNT_FORCE)
125 1.1 cgd flags |= FORCECLOSE;
126 1.1 cgd
127 1.65 pooka if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
128 1.1 cgd return (EBUSY);
129 1.55 christos if ((error = vflush(mp, rtvp, flags)) != 0)
130 1.1 cgd return (error);
131 1.1 cgd
132 1.1 cgd /*
133 1.72 ad * Blow it away for future re-use
134 1.1 cgd */
135 1.55 christos vgone(rtvp);
136 1.76 simonb mp->mnt_data = NULL;
137 1.12 mycroft
138 1.12 mycroft return (0);
139 1.1 cgd }
140 1.1 cgd
141 1.12 mycroft int
142 1.79 dsl fdesc_root(struct mount *mp, struct vnode **vpp)
143 1.1 cgd {
144 1.1 cgd struct vnode *vp;
145 1.1 cgd
146 1.1 cgd /*
147 1.1 cgd * Return locked reference to root.
148 1.1 cgd */
149 1.82 pooka vp = mp->mnt_data;
150 1.1 cgd VREF(vp);
151 1.24 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
152 1.1 cgd *vpp = vp;
153 1.1 cgd return (0);
154 1.1 cgd }
155 1.1 cgd
156 1.12 mycroft int
157 1.79 dsl fdesc_statvfs(struct mount *mp, struct statvfs *sbp)
158 1.1 cgd {
159 1.1 cgd
160 1.1 cgd sbp->f_bsize = DEV_BSIZE;
161 1.47 christos sbp->f_frsize = DEV_BSIZE;
162 1.10 cgd sbp->f_iosize = DEV_BSIZE;
163 1.1 cgd sbp->f_blocks = 2; /* 1K to keep df happy */
164 1.1 cgd sbp->f_bfree = 0;
165 1.1 cgd sbp->f_bavail = 0;
166 1.47 christos sbp->f_bresvd = 0;
167 1.81 pooka sbp->f_files = 0;
168 1.81 pooka sbp->f_ffree = 0;
169 1.81 pooka sbp->f_favail = 0;
170 1.47 christos sbp->f_fresvd = 0;
171 1.47 christos copy_statvfs_info(sbp, mp);
172 1.1 cgd return (0);
173 1.1 cgd }
174 1.1 cgd
175 1.21 christos /*ARGSUSED*/
176 1.12 mycroft int
177 1.62 christos fdesc_sync(struct mount *mp, int waitfor,
178 1.71 pooka kauth_cred_t uc)
179 1.1 cgd {
180 1.12 mycroft
181 1.1 cgd return (0);
182 1.1 cgd }
183 1.1 cgd
184 1.12 mycroft /*
185 1.12 mycroft * Fdesc flat namespace lookup.
186 1.12 mycroft * Currently unsupported.
187 1.12 mycroft */
188 1.12 mycroft int
189 1.62 christos fdesc_vget(struct mount *mp, ino_t ino,
190 1.62 christos struct vnode **vpp)
191 1.12 mycroft {
192 1.12 mycroft
193 1.12 mycroft return (EOPNOTSUPP);
194 1.12 mycroft }
195 1.12 mycroft
196 1.32 jdolecek extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
197 1.23 thorpej
198 1.32 jdolecek const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
199 1.23 thorpej &fdesc_vnodeop_opv_desc,
200 1.23 thorpej NULL,
201 1.23 thorpej };
202 1.23 thorpej
203 1.1 cgd struct vfsops fdesc_vfsops = {
204 1.9 cgd MOUNT_FDESC,
205 1.66 dsl 0,
206 1.1 cgd fdesc_mount,
207 1.1 cgd fdesc_start,
208 1.1 cgd fdesc_unmount,
209 1.1 cgd fdesc_root,
210 1.71 pooka (void *)eopnotsupp, /* vfs_quotactl */
211 1.47 christos fdesc_statvfs,
212 1.1 cgd fdesc_sync,
213 1.12 mycroft fdesc_vget,
214 1.63 chs (void *)eopnotsupp, /* vfs_fhtovp */
215 1.63 chs (void *)eopnotsupp, /* vfs_vptofh */
216 1.1 cgd fdesc_init,
217 1.34 chs NULL,
218 1.29 jdolecek fdesc_done,
219 1.23 thorpej NULL, /* vfs_mountroot */
220 1.51 hannken (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
221 1.53 thorpej vfs_stdextattrctl,
222 1.68 pooka (void *)eopnotsupp, /* vfs_suspendctl */
223 1.73 dholland genfs_renamelock_enter,
224 1.73 dholland genfs_renamelock_exit,
225 1.74 ad (void *)eopnotsupp,
226 1.23 thorpej fdesc_vnodeopv_descs,
227 1.60 christos 0,
228 1.60 christos { NULL, NULL},
229 1.1 cgd };
230 1.75 rumble
231 1.75 rumble static int
232 1.75 rumble fdesc_modcmd(modcmd_t cmd, void *arg)
233 1.75 rumble {
234 1.77 rumble int error;
235 1.75 rumble
236 1.75 rumble switch (cmd) {
237 1.75 rumble case MODULE_CMD_INIT:
238 1.77 rumble error = vfs_attach(&fdesc_vfsops);
239 1.77 rumble if (error != 0)
240 1.77 rumble break;
241 1.77 rumble sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL,
242 1.77 rumble CTLFLAG_PERMANENT,
243 1.77 rumble CTLTYPE_NODE, "vfs", NULL,
244 1.77 rumble NULL, 0, NULL, 0,
245 1.77 rumble CTL_VFS, CTL_EOL);
246 1.77 rumble sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL,
247 1.77 rumble CTLFLAG_PERMANENT,
248 1.77 rumble CTLTYPE_NODE, "fdesc",
249 1.77 rumble SYSCTL_DESCR("File-descriptor file system"),
250 1.77 rumble NULL, 0, NULL, 0,
251 1.77 rumble CTL_VFS, 7, CTL_EOL);
252 1.77 rumble /*
253 1.77 rumble * XXX the "7" above could be dynamic, thereby eliminating one
254 1.77 rumble * more instance of the "number to vfs" mapping problem, but
255 1.77 rumble * "7" is the order as taken from sys/mount.h
256 1.77 rumble */
257 1.77 rumble break;
258 1.75 rumble case MODULE_CMD_FINI:
259 1.77 rumble error = vfs_detach(&fdesc_vfsops);
260 1.77 rumble if (error != 0)
261 1.77 rumble break;
262 1.77 rumble sysctl_teardown(&fdesc_sysctl_log);
263 1.77 rumble break;
264 1.75 rumble default:
265 1.77 rumble error = ENOTTY;
266 1.77 rumble break;
267 1.75 rumble }
268 1.77 rumble
269 1.77 rumble return (error);
270 1.75 rumble }
271