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