xref: /netbsd-src/sys/fs/ptyfs/ptyfs_vfsops.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: ptyfs_vfsops.c,v 1.12 2005/12/11 12:24:29 christos 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  */
35 
36 /*
37  * Pseudo-tty Filesystem
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vfsops.c,v 1.12 2005/12/11 12:24:29 christos Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <sys/conf.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/stat.h>
52 #include <sys/dirent.h>
53 #include <sys/malloc.h>
54 #include <sys/syslog.h>
55 #include <sys/select.h>
56 #include <sys/tty.h>
57 #include <sys/pty.h>
58 
59 #include <fs/ptyfs/ptyfs.h>
60 #include <miscfs/specfs/specdev.h>
61 
62 MALLOC_DEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
63 
64 void	ptyfs_init(void);
65 void	ptyfs_reinit(void);
66 void	ptyfs_done(void);
67 int	ptyfs_mount(struct mount *, const char *, void *, struct nameidata *,
68     struct lwp *);
69 int	ptyfs_start(struct mount *, int, struct lwp *);
70 int	ptyfs_unmount(struct mount *, int, struct lwp *);
71 int	ptyfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
72 int	ptyfs_quotactl(struct mount *, int, uid_t, void *, struct lwp *);
73 int	ptyfs_sync(struct mount *, int, struct ucred *, struct lwp *);
74 int	ptyfs_vget(struct mount *, ino_t, struct vnode **);
75 
76 static int ptyfs__allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
77     dev_t, char);
78 static int ptyfs__makename(struct ptm_pty *, char *, size_t, dev_t, char);
79 static void ptyfs__getvattr(struct ptm_pty *, struct proc *, struct vattr *);
80 
81 /*
82  * ptm glue: When we mount, we make ptm point to us.
83  */
84 struct ptm_pty *ptyfs_save_ptm;
85 static int ptyfs_count;
86 
87 struct ptm_pty ptm_ptyfspty = {
88 	ptyfs__allocvp,
89 	ptyfs__makename,
90 	ptyfs__getvattr,
91 	NULL
92 };
93 
94 static int
95 ptyfs__makename(struct ptm_pty *pt, char *tbuf, size_t bufsiz, dev_t dev,
96     char ms)
97 {
98 	struct mount *mp = pt->arg;
99 	size_t len;
100 
101 	switch (ms) {
102 	case 'p':
103 		/* We don't provide access to the master, should we? */
104 		len = snprintf(tbuf, bufsiz, "/dev/null");
105 		break;
106 	case 't':
107 		len = snprintf(tbuf, bufsiz, "%s/%d", mp->mnt_stat.f_mntonname,
108 		    minor(dev));
109 		break;
110 	default:
111 		return EINVAL;
112 	}
113 
114 	return len >= bufsiz ? ENOSPC : 0;
115 }
116 
117 
118 static int
119 /*ARGSUSED*/
120 ptyfs__allocvp(struct ptm_pty *pt, struct lwp *l, struct vnode **vpp,
121     dev_t dev, char ms)
122 {
123 	struct mount *mp = pt->arg;
124 	ptyfstype type;
125 
126 	switch (ms) {
127 	case 'p':
128 		type = PTYFSptc;
129 		break;
130 	case 't':
131 		type = PTYFSpts;
132 		break;
133 	default:
134 		return EINVAL;
135 	}
136 
137 	return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
138 }
139 
140 
141 static void
142 ptyfs__getvattr(struct ptm_pty *pt, struct proc *p, struct vattr *vattr)
143 {
144 	struct mount *mp = pt->arg;
145 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
146 	VATTR_NULL(vattr);
147 	/* get real uid */
148 	vattr->va_uid = p->p_cred->p_ruid;
149 	vattr->va_gid = pmnt->pmnt_gid;
150 	vattr->va_mode = pmnt->pmnt_mode;
151 }
152 
153 
154 void
155 ptyfs_init(void)
156 {
157 #ifdef _LKM
158 	malloc_type_attach(M_PTYFSMNT);
159 #endif
160 	ptyfs_hashinit();
161 }
162 
163 void
164 ptyfs_reinit(void)
165 {
166 	ptyfs_hashreinit();
167 }
168 
169 void
170 ptyfs_done(void)
171 {
172 #ifdef _LKM
173 	malloc_type_detach(M_PTYFSMNT);
174 #endif
175 	ptyfs_hashdone();
176 }
177 
178 /*
179  * Mount the Pseudo tty params filesystem
180  */
181 int
182 ptyfs_mount(struct mount *mp, const char *path, void *data,
183     struct nameidata *ndp, struct lwp *l)
184 {
185 	int error = 0;
186 	struct ptyfsmount *pmnt;
187 	struct ptyfs_args args;
188 
189 	/* Don't allow more than one mount */
190 	if (ptyfs_count)
191 		return EBUSY;
192 
193 	if (UIO_MX & (UIO_MX - 1)) {
194 		log(LOG_ERR, "ptyfs: invalid directory entry size");
195 		return EINVAL;
196 	}
197 
198 	if (mp->mnt_flag & MNT_GETARGS) {
199 		pmnt = VFSTOPTY(mp);
200 		if (pmnt == NULL)
201 			return EIO;
202 		args.version = PTYFS_ARGSVERSION;
203 		args.mode = pmnt->pmnt_mode;
204 		args.gid = pmnt->pmnt_gid;
205 		return copyout(&args, data, sizeof(args));
206 	}
207 
208 	if (mp->mnt_flag & MNT_UPDATE)
209 		return EOPNOTSUPP;
210 
211 	if (data != NULL) {
212 		error = copyin(data, &args, sizeof args);
213 		if (error != 0)
214 			return error;
215 
216 		if (args.version != PTYFS_ARGSVERSION)
217 			return EINVAL;
218 	} else {
219 		/*
220 		 * Arguments are mandatory.
221 		 */
222 		return EINVAL;
223 	}
224 
225 	pmnt = malloc(sizeof(struct ptyfsmount), M_UFSMNT, M_WAITOK);
226 
227 	mp->mnt_data = pmnt;
228 	pmnt->pmnt_gid = args.gid;
229 	pmnt->pmnt_mode = args.mode;
230 	mp->mnt_flag |= MNT_LOCAL;
231 	vfs_getnewfsid(mp);
232 
233 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
234 	    UIO_SYSSPACE, mp, l)) != 0) {
235 		free(pmnt, M_UFSMNT);
236 		return error;
237 	}
238 
239 	/* Point pty access to us */
240 
241 	ptm_ptyfspty.arg = mp;
242 	ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
243 	ptyfs_count++;
244 	return 0;
245 }
246 
247 /*ARGSUSED*/
248 int
249 ptyfs_start(struct mount *mp, int flags, struct lwp *p)
250 {
251 	return 0;
252 }
253 
254 /*ARGSUSED*/
255 int
256 ptyfs_unmount(struct mount *mp, int mntflags, struct lwp *p)
257 {
258 	int error;
259 	int flags = 0;
260 
261 	if (mntflags & MNT_FORCE)
262 		flags |= FORCECLOSE;
263 
264 	if ((error = vflush(mp, 0, flags)) != 0)
265 		return (error);
266 
267 	/* Restore where pty access was pointing */
268 	(void)pty_sethandler(ptyfs_save_ptm);
269 	ptyfs_save_ptm = NULL;
270 	ptm_ptyfspty.arg = NULL;
271 
272 	/*
273 	 * Finally, throw away the ptyfsmount structure
274 	 */
275 	free(mp->mnt_data, M_UFSMNT);
276 	mp->mnt_data = 0;
277 	ptyfs_count--;
278 
279 	return 0;
280 }
281 
282 int
283 ptyfs_root(struct mount *mp, struct vnode **vpp)
284 {
285 	/* setup "." */
286 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
287 }
288 
289 /*ARGSUSED*/
290 int
291 ptyfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *p)
292 {
293 	return EOPNOTSUPP;
294 }
295 
296 /*ARGSUSED*/
297 int
298 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *p)
299 {
300 	sbp->f_bsize = DEV_BSIZE;
301 	sbp->f_frsize = DEV_BSIZE;
302 	sbp->f_iosize = DEV_BSIZE;
303 	sbp->f_blocks = 2;		/* 1K to keep df happy */
304 	sbp->f_bfree = 0;
305 	sbp->f_bavail = 0;
306 	sbp->f_bresvd = 0;
307 	sbp->f_files = 1024;	/* XXX lie */
308 	sbp->f_ffree = 128;	/* XXX lie */
309 	sbp->f_favail = 128;	/* XXX lie */
310 	sbp->f_fresvd = 0;
311 	sbp->f_namemax = MAXNAMLEN;
312 	copy_statvfs_info(sbp, mp);
313 	return 0;
314 }
315 
316 /*ARGSUSED*/
317 int
318 ptyfs_sync(struct mount *mp, int waitfor, struct ucred *uc, struct lwp *p)
319 {
320 	return 0;
321 }
322 
323 /*
324  * Kernfs flat namespace lookup.
325  * Currently unsupported.
326  */
327 /*ARGSUSED*/
328 int
329 ptyfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
330 {
331 	return EOPNOTSUPP;
332 }
333 
334 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup")
335 {
336 
337 	sysctl_createv(clog, 0, NULL, NULL,
338 		       CTLFLAG_PERMANENT,
339 		       CTLTYPE_NODE, "vfs", NULL,
340 		       NULL, 0, NULL, 0,
341 		       CTL_VFS, CTL_EOL);
342 	sysctl_createv(clog, 0, NULL, NULL,
343 		       CTLFLAG_PERMANENT,
344 		       CTLTYPE_NODE, "ptyfs",
345 		       SYSCTL_DESCR("Pty file system"),
346 		       NULL, 0, NULL, 0,
347 		       CTL_VFS, 23, CTL_EOL);
348 	/*
349 	 * XXX the "23" above could be dynamic, thereby eliminating
350 	 * one more instance of the "number to vfs" mapping problem,
351 	 * but "23" is the order as taken from sys/mount.h
352 	 */
353 }
354 
355 
356 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
357 
358 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
359 	&ptyfs_vnodeop_opv_desc,
360 	NULL,
361 };
362 
363 struct vfsops ptyfs_vfsops = {
364 	MOUNT_PTYFS,
365 	ptyfs_mount,
366 	ptyfs_start,
367 	ptyfs_unmount,
368 	ptyfs_root,
369 	ptyfs_quotactl,
370 	ptyfs_statvfs,
371 	ptyfs_sync,
372 	ptyfs_vget,
373 	NULL,				/* vfs_fhtovp */
374 	NULL,				/* vfs_vptofp */
375 	ptyfs_init,
376 	ptyfs_reinit,
377 	ptyfs_done,
378 	NULL,				/* vfs_mountroot */
379 	(int (*)(struct mount *, struct vnode *, struct timespec *))eopnotsupp,
380 	(int (*)(struct mount *, int, struct vnode *, int, const char *,
381 	    struct lwp *))eopnotsupp,
382 	ptyfs_vnodeopv_descs,
383 };
384 VFS_ATTACH(ptyfs_vfsops);
385