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