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