fdesc_vfsops.c revision 1.95 1 1.95 pgoyette /* $NetBSD: fdesc_vfsops.c,v 1.95 2020/03/21 16:30:39 pgoyette 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.95 pgoyette __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.95 2020/03/21 16:30:39 pgoyette 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.59 elad #include <sys/kauth.h>
62 1.75 rumble #include <sys/module.h>
63 1.59 elad
64 1.73 dholland #include <miscfs/genfs/genfs.h>
65 1.6 cgd #include <miscfs/fdesc/fdesc.h>
66 1.1 cgd
67 1.75 rumble MODULE(MODULE_CLASS_VFS, fdesc, NULL);
68 1.75 rumble
69 1.69 pooka VFS_PROTOS(fdesc);
70 1.21 christos
71 1.1 cgd /*
72 1.1 cgd * Mount the per-process file descriptors (/dev/fd)
73 1.1 cgd */
74 1.12 mycroft int
75 1.71 pooka fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
76 1.1 cgd {
77 1.71 pooka struct lwp *l = curlwp;
78 1.89 hannken int error = 0, ix;
79 1.1 cgd struct vnode *rvp;
80 1.1 cgd
81 1.66 dsl if (mp->mnt_flag & MNT_GETARGS) {
82 1.66 dsl *data_len = 0;
83 1.38 christos return 0;
84 1.66 dsl }
85 1.1 cgd /*
86 1.1 cgd * Update is a no-op
87 1.1 cgd */
88 1.1 cgd if (mp->mnt_flag & MNT_UPDATE)
89 1.1 cgd return (EOPNOTSUPP);
90 1.1 cgd
91 1.89 hannken ix = FD_ROOT;
92 1.89 hannken error = vcache_get(mp, &ix, sizeof(ix), &rvp);
93 1.1 cgd if (error)
94 1.89 hannken return error;
95 1.1 cgd
96 1.86 christos mp->mnt_stat.f_namemax = FDESC_MAXNAMLEN;
97 1.14 mycroft mp->mnt_flag |= MNT_LOCAL;
98 1.82 pooka mp->mnt_data = rvp;
99 1.31 assar vfs_getnewfsid(mp);
100 1.1 cgd
101 1.47 christos error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
102 1.67 pooka mp->mnt_op->vfs_name, mp, l);
103 1.39 christos return error;
104 1.1 cgd }
105 1.1 cgd
106 1.12 mycroft int
107 1.71 pooka fdesc_start(struct mount *mp, int flags)
108 1.1 cgd {
109 1.1 cgd return (0);
110 1.1 cgd }
111 1.1 cgd
112 1.12 mycroft int
113 1.71 pooka fdesc_unmount(struct mount *mp, int mntflags)
114 1.1 cgd {
115 1.1 cgd int error;
116 1.1 cgd int flags = 0;
117 1.82 pooka struct vnode *rtvp = mp->mnt_data;
118 1.1 cgd
119 1.24 fvdl if (mntflags & MNT_FORCE)
120 1.1 cgd flags |= FORCECLOSE;
121 1.1 cgd
122 1.65 pooka if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
123 1.1 cgd return (EBUSY);
124 1.55 christos if ((error = vflush(mp, rtvp, flags)) != 0)
125 1.1 cgd return (error);
126 1.1 cgd
127 1.1 cgd /*
128 1.72 ad * Blow it away for future re-use
129 1.1 cgd */
130 1.55 christos vgone(rtvp);
131 1.76 simonb mp->mnt_data = NULL;
132 1.12 mycroft
133 1.12 mycroft return (0);
134 1.1 cgd }
135 1.1 cgd
136 1.12 mycroft int
137 1.93 ad fdesc_root(struct mount *mp, int lktype, struct vnode **vpp)
138 1.1 cgd {
139 1.1 cgd struct vnode *vp;
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * Return locked reference to root.
143 1.1 cgd */
144 1.82 pooka vp = mp->mnt_data;
145 1.84 pooka vref(vp);
146 1.93 ad vn_lock(vp, lktype | LK_RETRY);
147 1.1 cgd *vpp = vp;
148 1.1 cgd return (0);
149 1.1 cgd }
150 1.1 cgd
151 1.21 christos /*ARGSUSED*/
152 1.12 mycroft int
153 1.62 christos fdesc_sync(struct mount *mp, int waitfor,
154 1.71 pooka kauth_cred_t uc)
155 1.1 cgd {
156 1.12 mycroft
157 1.1 cgd return (0);
158 1.1 cgd }
159 1.1 cgd
160 1.12 mycroft /*
161 1.12 mycroft * Fdesc flat namespace lookup.
162 1.12 mycroft * Currently unsupported.
163 1.12 mycroft */
164 1.12 mycroft int
165 1.93 ad fdesc_vget(struct mount *mp, ino_t ino, int lktype,
166 1.62 christos struct vnode **vpp)
167 1.12 mycroft {
168 1.12 mycroft
169 1.12 mycroft return (EOPNOTSUPP);
170 1.12 mycroft }
171 1.12 mycroft
172 1.89 hannken int
173 1.89 hannken fdesc_loadvnode(struct mount *mp, struct vnode *vp,
174 1.89 hannken const void *key, size_t key_len, const void **new_key)
175 1.89 hannken {
176 1.89 hannken int ix;
177 1.89 hannken struct fdescnode *fd;
178 1.89 hannken
179 1.89 hannken KASSERT(key_len == sizeof(ix));
180 1.89 hannken memcpy(&ix, key, key_len);
181 1.89 hannken
182 1.89 hannken fd = kmem_alloc(sizeof(struct fdescnode), KM_SLEEP);
183 1.89 hannken fd->fd_fd = -1;
184 1.89 hannken fd->fd_link = NULL;
185 1.89 hannken fd->fd_ix = ix;
186 1.89 hannken fd->fd_vnode = vp;
187 1.89 hannken vp->v_tag = VT_FDESC;
188 1.89 hannken vp->v_op = fdesc_vnodeop_p;
189 1.89 hannken vp->v_data = fd;
190 1.89 hannken switch (ix) {
191 1.89 hannken case FD_ROOT:
192 1.89 hannken fd->fd_type = Froot;
193 1.89 hannken vp->v_type = VDIR;
194 1.89 hannken vp->v_vflag |= VV_ROOT;
195 1.89 hannken break;
196 1.89 hannken case FD_DEVFD:
197 1.89 hannken fd->fd_type = Fdevfd;
198 1.89 hannken vp->v_type = VDIR;
199 1.89 hannken break;
200 1.89 hannken case FD_CTTY:
201 1.89 hannken fd->fd_type = Fctty;
202 1.90 christos vp->v_type = VCHR;
203 1.89 hannken break;
204 1.89 hannken case FD_STDIN:
205 1.89 hannken fd->fd_type = Flink;
206 1.89 hannken fd->fd_link = "fd/0";
207 1.89 hannken vp->v_type = VLNK;
208 1.89 hannken break;
209 1.89 hannken case FD_STDOUT:
210 1.89 hannken fd->fd_type = Flink;
211 1.89 hannken fd->fd_link = "fd/1";
212 1.89 hannken vp->v_type = VLNK;
213 1.89 hannken break;
214 1.89 hannken case FD_STDERR:
215 1.89 hannken fd->fd_type = Flink;
216 1.89 hannken fd->fd_link = "fd/2";
217 1.89 hannken vp->v_type = VLNK;
218 1.89 hannken break;
219 1.89 hannken default:
220 1.89 hannken KASSERT(ix >= FD_DESC);
221 1.89 hannken fd->fd_type = Fdesc;
222 1.89 hannken fd->fd_fd = ix - FD_DESC;
223 1.89 hannken vp->v_type = VNON;
224 1.89 hannken break;
225 1.89 hannken }
226 1.89 hannken uvm_vnp_setsize(vp, 0);
227 1.89 hannken *new_key = &fd->fd_ix;
228 1.89 hannken
229 1.89 hannken return 0;
230 1.89 hannken }
231 1.89 hannken
232 1.32 jdolecek extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
233 1.23 thorpej
234 1.32 jdolecek const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
235 1.23 thorpej &fdesc_vnodeop_opv_desc,
236 1.23 thorpej NULL,
237 1.23 thorpej };
238 1.23 thorpej
239 1.1 cgd struct vfsops fdesc_vfsops = {
240 1.88 hannken .vfs_name = MOUNT_FDESC,
241 1.88 hannken .vfs_min_mount_data = 0,
242 1.88 hannken .vfs_mount = fdesc_mount,
243 1.88 hannken .vfs_start = fdesc_start,
244 1.88 hannken .vfs_unmount = fdesc_unmount,
245 1.88 hannken .vfs_root = fdesc_root,
246 1.88 hannken .vfs_quotactl = (void *)eopnotsupp,
247 1.88 hannken .vfs_statvfs = genfs_statvfs,
248 1.88 hannken .vfs_sync = fdesc_sync,
249 1.88 hannken .vfs_vget = fdesc_vget,
250 1.89 hannken .vfs_loadvnode = fdesc_loadvnode,
251 1.88 hannken .vfs_fhtovp = (void *)eopnotsupp,
252 1.88 hannken .vfs_vptofh = (void *)eopnotsupp,
253 1.88 hannken .vfs_init = fdesc_init,
254 1.88 hannken .vfs_done = fdesc_done,
255 1.88 hannken .vfs_snapshot = (void *)eopnotsupp,
256 1.88 hannken .vfs_extattrctl = vfs_stdextattrctl,
257 1.92 hannken .vfs_suspendctl = genfs_suspendctl,
258 1.88 hannken .vfs_renamelock_enter = genfs_renamelock_enter,
259 1.88 hannken .vfs_renamelock_exit = genfs_renamelock_exit,
260 1.88 hannken .vfs_fsync = (void *)eopnotsupp,
261 1.88 hannken .vfs_opv_descs = fdesc_vnodeopv_descs
262 1.1 cgd };
263 1.75 rumble
264 1.94 pgoyette SYSCTL_SETUP(fdesc_sysctl_setup, "fdesc sysctl")
265 1.75 rumble {
266 1.75 rumble
267 1.95 pgoyette sysctl_createv(clog, 0, NULL, NULL,
268 1.77 rumble CTLFLAG_PERMANENT,
269 1.77 rumble CTLTYPE_NODE, "fdesc",
270 1.77 rumble SYSCTL_DESCR("File-descriptor file system"),
271 1.77 rumble NULL, 0, NULL, 0,
272 1.77 rumble CTL_VFS, 7, CTL_EOL);
273 1.77 rumble /*
274 1.77 rumble * XXX the "7" above could be dynamic, thereby eliminating one
275 1.77 rumble * more instance of the "number to vfs" mapping problem, but
276 1.77 rumble * "7" is the order as taken from sys/mount.h
277 1.77 rumble */
278 1.94 pgoyette }
279 1.94 pgoyette
280 1.94 pgoyette static int
281 1.94 pgoyette fdesc_modcmd(modcmd_t cmd, void *arg)
282 1.94 pgoyette {
283 1.94 pgoyette int error;
284 1.94 pgoyette
285 1.94 pgoyette switch (cmd) {
286 1.94 pgoyette case MODULE_CMD_INIT:
287 1.94 pgoyette error = vfs_attach(&fdesc_vfsops);
288 1.94 pgoyette if (error != 0)
289 1.94 pgoyette break;
290 1.77 rumble break;
291 1.75 rumble case MODULE_CMD_FINI:
292 1.77 rumble error = vfs_detach(&fdesc_vfsops);
293 1.77 rumble if (error != 0)
294 1.77 rumble break;
295 1.77 rumble break;
296 1.75 rumble default:
297 1.77 rumble error = ENOTTY;
298 1.77 rumble break;
299 1.75 rumble }
300 1.77 rumble
301 1.77 rumble return (error);
302 1.75 rumble }
303