xref: /netbsd-src/sys/miscfs/fdesc/fdesc_vfsops.c (revision f14316bcbc544b96a93e884bc5c2b15fd60e22ae)
1 /*	$NetBSD: fdesc_vfsops.c,v 1.89 2014/07/13 11:23:01 hannken Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated 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  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
35  *
36  * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
37  */
38 
39 /*
40  * /dev/fd Filesystem
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.89 2014/07/13 11:23:01 hannken Exp $");
45 
46 #if defined(_KERNEL_OPT)
47 #include "opt_compat_netbsd.h"
48 #endif
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 #include <sys/time.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/filedesc.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/dirent.h>
60 #include <sys/namei.h>
61 #include <sys/malloc.h>
62 #include <sys/kauth.h>
63 #include <sys/module.h>
64 
65 #include <miscfs/genfs/genfs.h>
66 #include <miscfs/fdesc/fdesc.h>
67 
68 MODULE(MODULE_CLASS_VFS, fdesc, NULL);
69 
70 VFS_PROTOS(fdesc);
71 
72 static struct sysctllog *fdesc_sysctl_log;
73 
74 /*
75  * Mount the per-process file descriptors (/dev/fd)
76  */
77 int
78 fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
79 {
80 	struct lwp *l = curlwp;
81 	int error = 0, ix;
82 	struct vnode *rvp;
83 
84 	if (mp->mnt_flag & MNT_GETARGS) {
85 		*data_len = 0;
86 		return 0;
87 	}
88 	/*
89 	 * Update is a no-op
90 	 */
91 	if (mp->mnt_flag & MNT_UPDATE)
92 		return (EOPNOTSUPP);
93 
94 	ix = FD_ROOT;
95 	error = vcache_get(mp, &ix, sizeof(ix), &rvp);
96 	if (error)
97 		return error;
98 
99 	mp->mnt_stat.f_namemax = FDESC_MAXNAMLEN;
100 	mp->mnt_flag |= MNT_LOCAL;
101 	mp->mnt_data = rvp;
102 	vfs_getnewfsid(mp);
103 
104 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
105 	    mp->mnt_op->vfs_name, mp, l);
106 	return error;
107 }
108 
109 int
110 fdesc_start(struct mount *mp, int flags)
111 {
112 	return (0);
113 }
114 
115 int
116 fdesc_unmount(struct mount *mp, int mntflags)
117 {
118 	int error;
119 	int flags = 0;
120 	struct vnode *rtvp = mp->mnt_data;
121 
122 	if (mntflags & MNT_FORCE)
123 		flags |= FORCECLOSE;
124 
125 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
126 		return (EBUSY);
127 	if ((error = vflush(mp, rtvp, flags)) != 0)
128 		return (error);
129 
130 	/*
131 	 * Blow it away for future re-use
132 	 */
133 	vgone(rtvp);
134 	mp->mnt_data = NULL;
135 
136 	return (0);
137 }
138 
139 int
140 fdesc_root(struct mount *mp, struct vnode **vpp)
141 {
142 	struct vnode *vp;
143 
144 	/*
145 	 * Return locked reference to root.
146 	 */
147 	vp = mp->mnt_data;
148 	vref(vp);
149 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
150 	*vpp = vp;
151 	return (0);
152 }
153 
154 /*ARGSUSED*/
155 int
156 fdesc_sync(struct mount *mp, int waitfor,
157     kauth_cred_t uc)
158 {
159 
160 	return (0);
161 }
162 
163 /*
164  * Fdesc flat namespace lookup.
165  * Currently unsupported.
166  */
167 int
168 fdesc_vget(struct mount *mp, ino_t ino,
169     struct vnode **vpp)
170 {
171 
172 	return (EOPNOTSUPP);
173 }
174 
175 int
176 fdesc_loadvnode(struct mount *mp, struct vnode *vp,
177     const void *key, size_t key_len, const void **new_key)
178 {
179 	int ix;
180 	struct fdescnode *fd;
181 
182 	KASSERT(key_len == sizeof(ix));
183 	memcpy(&ix, key, key_len);
184 
185 	fd = kmem_alloc(sizeof(struct fdescnode), KM_SLEEP);
186 	fd->fd_fd = -1;
187 	fd->fd_link = NULL;
188 	fd->fd_ix = ix;
189 	fd->fd_vnode = vp;
190 	vp->v_tag = VT_FDESC;
191 	vp->v_op = fdesc_vnodeop_p;
192 	vp->v_data = fd;
193 	switch (ix) {
194 	case FD_ROOT:
195 		fd->fd_type = Froot;
196 		vp->v_type = VDIR;
197 		vp->v_vflag |= VV_ROOT;
198 		break;
199 	case FD_DEVFD:
200 		fd->fd_type = Fdevfd;
201 		vp->v_type = VDIR;
202 		break;
203 	case FD_CTTY:
204 		fd->fd_type = Fctty;
205 		vp->v_type = VNON;
206 		break;
207 	case FD_STDIN:
208 		fd->fd_type = Flink;
209 		fd->fd_link = "fd/0";
210 		vp->v_type = VLNK;
211 		break;
212 	case FD_STDOUT:
213 		fd->fd_type = Flink;
214 		fd->fd_link = "fd/1";
215 		vp->v_type = VLNK;
216 		break;
217 	case FD_STDERR:
218 		fd->fd_type = Flink;
219 		fd->fd_link = "fd/2";
220 		vp->v_type = VLNK;
221 		break;
222 	default:
223 		KASSERT(ix >= FD_DESC);
224 		fd->fd_type = Fdesc;
225 		fd->fd_fd = ix - FD_DESC;
226 		vp->v_type = VNON;
227 		break;
228 	}
229 	uvm_vnp_setsize(vp, 0);
230 	*new_key = &fd->fd_ix;
231 
232 	return 0;
233 }
234 
235 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
236 
237 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
238 	&fdesc_vnodeop_opv_desc,
239 	NULL,
240 };
241 
242 struct vfsops fdesc_vfsops = {
243 	.vfs_name = MOUNT_FDESC,
244 	.vfs_min_mount_data = 0,
245 	.vfs_mount = fdesc_mount,
246 	.vfs_start = fdesc_start,
247 	.vfs_unmount = fdesc_unmount,
248 	.vfs_root = fdesc_root,
249 	.vfs_quotactl = (void *)eopnotsupp,
250 	.vfs_statvfs = genfs_statvfs,
251 	.vfs_sync = fdesc_sync,
252 	.vfs_vget = fdesc_vget,
253 	.vfs_loadvnode = fdesc_loadvnode,
254 	.vfs_fhtovp = (void *)eopnotsupp,
255 	.vfs_vptofh = (void *)eopnotsupp,
256 	.vfs_init = fdesc_init,
257 	.vfs_done = fdesc_done,
258 	.vfs_snapshot = (void *)eopnotsupp,
259 	.vfs_extattrctl = vfs_stdextattrctl,
260 	.vfs_suspendctl = (void *)eopnotsupp,
261 	.vfs_renamelock_enter = genfs_renamelock_enter,
262 	.vfs_renamelock_exit = genfs_renamelock_exit,
263 	.vfs_fsync = (void *)eopnotsupp,
264 	.vfs_opv_descs = fdesc_vnodeopv_descs
265 };
266 
267 static int
268 fdesc_modcmd(modcmd_t cmd, void *arg)
269 {
270 	int error;
271 
272 	switch (cmd) {
273 	case MODULE_CMD_INIT:
274 		error = vfs_attach(&fdesc_vfsops);
275 		if (error != 0)
276 			break;
277 		sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL,
278 			       CTLFLAG_PERMANENT,
279 			       CTLTYPE_NODE, "fdesc",
280 			       SYSCTL_DESCR("File-descriptor file system"),
281 			       NULL, 0, NULL, 0,
282 			       CTL_VFS, 7, CTL_EOL);
283 		/*
284 		 * XXX the "7" above could be dynamic, thereby eliminating one
285 		 * more instance of the "number to vfs" mapping problem, but
286 		 * "7" is the order as taken from sys/mount.h
287 		 */
288 		break;
289 	case MODULE_CMD_FINI:
290 		error = vfs_detach(&fdesc_vfsops);
291 		if (error != 0)
292 			break;
293 		sysctl_teardown(&fdesc_sysctl_log);
294 		break;
295 	default:
296 		error = ENOTTY;
297 		break;
298 	}
299 
300 	return (error);
301 }
302