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