Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vfsops.c revision 1.10
      1 /*	$NetBSD: ptyfs_vfsops.c,v 1.10 2005/09/29 14:45:56 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.10 2005/09/29 14:45:56 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 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 	/* Don't allow more than one mount */
    189 	if (ptyfs_save_ptm != NULL)
    190 		return EBUSY;
    191 
    192 	if (UIO_MX & (UIO_MX - 1)) {
    193 		log(LOG_ERR, "ptyfs: invalid directory entry size");
    194 		return EINVAL;
    195 	}
    196 
    197 	if (mp->mnt_flag & MNT_GETARGS) {
    198 		pmnt = VFSTOPTY(mp);
    199 		if (pmnt == NULL)
    200 			return EIO;
    201 		args.version = PTYFS_ARGSVERSION;
    202 		args.mode = pmnt->pmnt_mode;
    203 		args.gid = pmnt->pmnt_gid;
    204 		return copyout(&args, data, sizeof(args));
    205 	}
    206 
    207 	if (mp->mnt_flag & MNT_UPDATE)
    208 		return EOPNOTSUPP;
    209 
    210 	if (data != NULL) {
    211 		error = copyin(data, &args, sizeof args);
    212 		if (error != 0)
    213 			return error;
    214 
    215 		if (args.version != PTYFS_ARGSVERSION)
    216 			return EINVAL;
    217 	} else {
    218 		/*
    219 		 * Arguments are mandatory.
    220 		 */
    221 		return EINVAL;
    222 	}
    223 
    224 	pmnt = malloc(sizeof(struct ptyfsmount), M_UFSMNT, M_WAITOK);
    225 
    226 	mp->mnt_data = pmnt;
    227 	pmnt->pmnt_gid = args.gid;
    228 	pmnt->pmnt_mode = args.mode;
    229 	mp->mnt_flag |= MNT_LOCAL;
    230 	vfs_getnewfsid(mp);
    231 
    232 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
    233 	    UIO_SYSSPACE, mp, p)) != 0) {
    234 		free(pmnt, M_UFSMNT);
    235 		return error;
    236 	}
    237 
    238 	/* Point pty access to us */
    239 
    240 	ptm_ptyfspty.arg = mp;
    241 	ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
    242 	return 0;
    243 }
    244 
    245 /*ARGSUSED*/
    246 int
    247 ptyfs_start(struct mount *mp, int flags, struct proc *p)
    248 {
    249 	return 0;
    250 }
    251 
    252 /*ARGSUSED*/
    253 int
    254 ptyfs_unmount(struct mount *mp, int mntflags, struct proc *p)
    255 {
    256 	int error;
    257 	int flags = 0;
    258 
    259 	if (mntflags & MNT_FORCE)
    260 		flags |= FORCECLOSE;
    261 
    262 	if ((error = vflush(mp, 0, flags)) != 0)
    263 		return (error);
    264 
    265 	/* Restore where pty access was pointing */
    266 	KASSERT(ptyfs_save_ptm != NULL);
    267 	(void)pty_sethandler(ptyfs_save_ptm);
    268 	ptyfs_save_ptm = NULL;
    269 	ptm_ptyfspty.arg = NULL;
    270 
    271 	/*
    272 	 * Finally, throw away the ptyfsmount structure
    273 	 */
    274 	free(mp->mnt_data, M_UFSMNT);
    275 	mp->mnt_data = 0;
    276 
    277 	return 0;
    278 }
    279 
    280 int
    281 ptyfs_root(struct mount *mp, struct vnode **vpp)
    282 {
    283 	/* setup "." */
    284 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
    285 }
    286 
    287 /*ARGSUSED*/
    288 int
    289 ptyfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct proc *p)
    290 {
    291 	return EOPNOTSUPP;
    292 }
    293 
    294 /*ARGSUSED*/
    295 int
    296 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
    297 {
    298 	sbp->f_bsize = DEV_BSIZE;
    299 	sbp->f_frsize = DEV_BSIZE;
    300 	sbp->f_iosize = DEV_BSIZE;
    301 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    302 	sbp->f_bfree = 0;
    303 	sbp->f_bavail = 0;
    304 	sbp->f_bresvd = 0;
    305 	sbp->f_files = 1024;	/* XXX lie */
    306 	sbp->f_ffree = 128;	/* XXX lie */
    307 	sbp->f_favail = 128;	/* XXX lie */
    308 	sbp->f_fresvd = 0;
    309 	sbp->f_namemax = MAXNAMLEN;
    310 	copy_statvfs_info(sbp, mp);
    311 	return 0;
    312 }
    313 
    314 /*ARGSUSED*/
    315 int
    316 ptyfs_sync(struct mount *mp, int waitfor, struct ucred *uc, struct proc *p)
    317 {
    318 	return 0;
    319 }
    320 
    321 /*
    322  * Kernfs flat namespace lookup.
    323  * Currently unsupported.
    324  */
    325 /*ARGSUSED*/
    326 int
    327 ptyfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    328 {
    329 	return EOPNOTSUPP;
    330 }
    331 
    332 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup")
    333 {
    334 
    335 	sysctl_createv(clog, 0, NULL, NULL,
    336 		       CTLFLAG_PERMANENT,
    337 		       CTLTYPE_NODE, "vfs", NULL,
    338 		       NULL, 0, NULL, 0,
    339 		       CTL_VFS, CTL_EOL);
    340 	sysctl_createv(clog, 0, NULL, NULL,
    341 		       CTLFLAG_PERMANENT,
    342 		       CTLTYPE_NODE, "ptyfs",
    343 		       SYSCTL_DESCR("Pty file system"),
    344 		       NULL, 0, NULL, 0,
    345 		       CTL_VFS, 23, CTL_EOL);
    346 	/*
    347 	 * XXX the "23" above could be dynamic, thereby eliminating
    348 	 * one more instance of the "number to vfs" mapping problem,
    349 	 * but "23" is the order as taken from sys/mount.h
    350 	 */
    351 }
    352 
    353 
    354 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
    355 
    356 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
    357 	&ptyfs_vnodeop_opv_desc,
    358 	NULL,
    359 };
    360 
    361 struct vfsops ptyfs_vfsops = {
    362 	MOUNT_PTYFS,
    363 	ptyfs_mount,
    364 	ptyfs_start,
    365 	ptyfs_unmount,
    366 	ptyfs_root,
    367 	ptyfs_quotactl,
    368 	ptyfs_statvfs,
    369 	ptyfs_sync,
    370 	ptyfs_vget,
    371 	NULL,				/* vfs_fhtovp */
    372 	NULL,				/* vfs_vptofp */
    373 	ptyfs_init,
    374 	ptyfs_reinit,
    375 	ptyfs_done,
    376 	NULL,				/* vfs_mountroot */
    377 	(int (*)(struct mount *, struct vnode *, struct timespec *))eopnotsupp,
    378 	(int (*)(struct mount *, int, struct vnode *, int, const char *,
    379 	    struct proc *))eopnotsupp,
    380 	ptyfs_vnodeopv_descs,
    381 };
    382 VFS_ATTACH(ptyfs_vfsops);
    383