Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vnops.c revision 1.62.4.1
      1 /*	$NetBSD: ptyfs_vnops.c,v 1.62.4.1 2021/08/01 22:42:37 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed 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  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     35  */
     36 
     37 /*
     38  * Copyright (c) 1993 Jan-Simon Pendry
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Jan-Simon Pendry.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     72  */
     73 
     74 /*
     75  * ptyfs vnode interface
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vnops.c,v 1.62.4.1 2021/08/01 22:42:37 thorpej Exp $");
     80 
     81 #include <sys/param.h>
     82 #include <sys/systm.h>
     83 #include <sys/time.h>
     84 #include <sys/kernel.h>
     85 #include <sys/file.h>
     86 #include <sys/filedesc.h>
     87 #include <sys/proc.h>
     88 #include <sys/vnode.h>
     89 #include <sys/namei.h>
     90 #include <sys/malloc.h>
     91 #include <sys/mount.h>
     92 #include <sys/select.h>
     93 #include <sys/dirent.h>
     94 #include <sys/resourcevar.h>
     95 #include <sys/stat.h>
     96 #include <sys/conf.h>
     97 #include <sys/tty.h>
     98 #include <sys/pty.h>
     99 #include <sys/kauth.h>
    100 
    101 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
    102 
    103 #include <machine/reg.h>
    104 
    105 #include <fs/ptyfs/ptyfs.h>
    106 #include <miscfs/genfs/genfs.h>
    107 #include <miscfs/specfs/specdev.h>
    108 
    109 MALLOC_DECLARE(M_PTYFSTMP);
    110 
    111 /*
    112  * Vnode Operations.
    113  *
    114  */
    115 
    116 int	ptyfs_lookup	(void *);
    117 int	ptyfs_open	(void *);
    118 int	ptyfs_close	(void *);
    119 int	ptyfs_access	(void *);
    120 int	ptyfs_getattr	(void *);
    121 int	ptyfs_setattr	(void *);
    122 int	ptyfs_read	(void *);
    123 int	ptyfs_write	(void *);
    124 int	ptyfs_ioctl	(void *);
    125 int	ptyfs_poll	(void *);
    126 int	ptyfs_kqfilter	(void *);
    127 int	ptyfs_readdir	(void *);
    128 int	ptyfs_reclaim	(void *);
    129 int	ptyfs_inactive	(void *);
    130 int	ptyfs_print	(void *);
    131 int	ptyfs_pathconf	(void *);
    132 int	ptyfs_advlock	(void *);
    133 
    134 static int ptyfs_update(struct vnode *, const struct timespec *,
    135     const struct timespec *, int);
    136 static int ptyfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
    137     struct lwp *);
    138 static int ptyfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
    139 static int atoi(const char *, size_t);
    140 
    141 /*
    142  * ptyfs vnode operations.
    143  */
    144 int (**ptyfs_vnodeop_p)(void *);
    145 const struct vnodeopv_entry_desc ptyfs_vnodeop_entries[] = {
    146 	{ &vop_default_desc, vn_default_error },
    147 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
    148 	{ &vop_lookup_desc, ptyfs_lookup },		/* lookup */
    149 	{ &vop_create_desc, genfs_eopnotsupp },		/* create */
    150 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
    151 	{ &vop_open_desc, ptyfs_open },			/* open */
    152 	{ &vop_close_desc, ptyfs_close },		/* close */
    153 	{ &vop_access_desc, ptyfs_access },		/* access */
    154 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
    155 	{ &vop_getattr_desc, ptyfs_getattr },		/* getattr */
    156 	{ &vop_setattr_desc, ptyfs_setattr },		/* setattr */
    157 	{ &vop_read_desc, ptyfs_read },			/* read */
    158 	{ &vop_write_desc, ptyfs_write },		/* write */
    159 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
    160 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
    161 	{ &vop_ioctl_desc, ptyfs_ioctl },		/* ioctl */
    162 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    163 	{ &vop_poll_desc, ptyfs_poll },			/* poll */
    164 	{ &vop_kqfilter_desc, ptyfs_kqfilter },		/* kqfilter */
    165 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
    166 	{ &vop_mmap_desc, genfs_eopnotsupp },		/* mmap */
    167 	{ &vop_fsync_desc, genfs_nullop },		/* fsync */
    168 	{ &vop_seek_desc, genfs_nullop },		/* seek */
    169 	{ &vop_remove_desc, genfs_eopnotsupp },		/* remove */
    170 	{ &vop_link_desc, genfs_eopnotsupp },		/* link */
    171 	{ &vop_rename_desc, genfs_eopnotsupp },		/* rename */
    172 	{ &vop_mkdir_desc, genfs_eopnotsupp },		/* mkdir */
    173 	{ &vop_rmdir_desc, genfs_eopnotsupp },		/* rmdir */
    174 	{ &vop_symlink_desc, genfs_eopnotsupp },	/* symlink */
    175 	{ &vop_readdir_desc, ptyfs_readdir },		/* readdir */
    176 	{ &vop_readlink_desc, genfs_eopnotsupp },	/* readlink */
    177 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
    178 	{ &vop_inactive_desc, ptyfs_inactive },		/* inactive */
    179 	{ &vop_reclaim_desc, ptyfs_reclaim },		/* reclaim */
    180 	{ &vop_lock_desc, genfs_lock },			/* lock */
    181 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    182 	{ &vop_bmap_desc, genfs_eopnotsupp },		/* bmap */
    183 	{ &vop_strategy_desc, genfs_badop },		/* strategy */
    184 	{ &vop_print_desc, ptyfs_print },		/* print */
    185 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    186 	{ &vop_pathconf_desc, ptyfs_pathconf },		/* pathconf */
    187 	{ &vop_advlock_desc, ptyfs_advlock },		/* advlock */
    188 	{ &vop_bwrite_desc, genfs_eopnotsupp },		/* bwrite */
    189 	{ &vop_putpages_desc, genfs_null_putpages },	/* putpages */
    190 	{ NULL, NULL }
    191 };
    192 const struct vnodeopv_desc ptyfs_vnodeop_opv_desc =
    193 	{ &ptyfs_vnodeop_p, ptyfs_vnodeop_entries };
    194 
    195 /*
    196  * free any private data and remove the node
    197  * from any private lists.
    198  */
    199 int
    200 ptyfs_reclaim(void *v)
    201 {
    202 	struct vop_reclaim_v2_args /* {
    203 		struct vnode *a_vp;
    204 	} */ *ap = v;
    205 	struct vnode *vp = ap->a_vp;
    206 
    207 	VOP_UNLOCK(vp);
    208 
    209 	vp->v_data = NULL;
    210 	return 0;
    211 }
    212 
    213 int
    214 ptyfs_inactive(void *v)
    215 {
    216 	struct vop_inactive_v2_args /* {
    217 		struct vnode *a_vp;
    218 		bool *a_recycle;
    219 	} */ *ap = v;
    220 	struct vnode *vp = ap->a_vp;
    221 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    222 
    223 	if (ptyfs->ptyfs_type == PTYFSptc)
    224 		ptyfs_clr_active(vp->v_mount, ptyfs->ptyfs_pty);
    225 
    226 	return 0;
    227 }
    228 
    229 /*
    230  * Return POSIX pathconf information applicable to special devices.
    231  */
    232 int
    233 ptyfs_pathconf(void *v)
    234 {
    235 	struct vop_pathconf_args /* {
    236 		struct vnode *a_vp;
    237 		int a_name;
    238 		register_t *a_retval;
    239 	} */ *ap = v;
    240 
    241 	switch (ap->a_name) {
    242 	case _PC_LINK_MAX:
    243 		*ap->a_retval = LINK_MAX;
    244 		return 0;
    245 	case _PC_MAX_CANON:
    246 		*ap->a_retval = MAX_CANON;
    247 		return 0;
    248 	case _PC_MAX_INPUT:
    249 		*ap->a_retval = MAX_INPUT;
    250 		return 0;
    251 	case _PC_PIPE_BUF:
    252 		*ap->a_retval = PIPE_BUF;
    253 		return 0;
    254 	case _PC_CHOWN_RESTRICTED:
    255 		*ap->a_retval = 1;
    256 		return 0;
    257 	case _PC_VDISABLE:
    258 		*ap->a_retval = _POSIX_VDISABLE;
    259 		return 0;
    260 	case _PC_SYNC_IO:
    261 		*ap->a_retval = 1;
    262 		return 0;
    263 	default:
    264 		return genfs_pathconf(ap);
    265 	}
    266 }
    267 
    268 /*
    269  * _print is used for debugging.
    270  * just print a readable description
    271  * of (vp).
    272  */
    273 int
    274 ptyfs_print(void *v)
    275 {
    276 	struct vop_print_args /* {
    277 		struct vnode *a_vp;
    278 	} */ *ap = v;
    279 	struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
    280 
    281 	printf("tag VT_PTYFS, type %d, pty %d\n",
    282 	    ptyfs->ptyfs_type, ptyfs->ptyfs_pty);
    283 	return 0;
    284 }
    285 
    286 /*
    287  * support advisory locking on pty nodes
    288  */
    289 int
    290 ptyfs_advlock(void *v)
    291 {
    292 	struct vop_print_args /* {
    293 		struct vnode *a_vp;
    294 	} */ *ap = v;
    295 	struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
    296 
    297 	switch (ptyfs->ptyfs_type) {
    298 	case PTYFSpts:
    299 	case PTYFSptc:
    300 		return spec_advlock(v);
    301 	default:
    302 		return EOPNOTSUPP;
    303 	}
    304 }
    305 
    306 /*
    307  * Invent attributes for ptyfsnode (vp) and store
    308  * them in (vap).
    309  * Directories lengths are returned as zero since
    310  * any real length would require the genuine size
    311  * to be computed, and nothing cares anyway.
    312  *
    313  * this is relatively minimal for ptyfs.
    314  */
    315 int
    316 ptyfs_getattr(void *v)
    317 {
    318 	struct vop_getattr_args /* {
    319 		struct vnode *a_vp;
    320 		struct vattr *a_vap;
    321 		kauth_cred_t a_cred;
    322 	} */ *ap = v;
    323 	struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
    324 	struct vattr *vap = ap->a_vap;
    325 
    326 	PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
    327 
    328 	/* start by zeroing out the attributes */
    329 	vattr_null(vap);
    330 
    331 	/* next do all the common fields */
    332 	vap->va_type = ap->a_vp->v_type;
    333 	vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    334 	vap->va_fileid = ptyfs->ptyfs_fileno;
    335 	vap->va_gen = 0;
    336 	vap->va_flags = 0;
    337 	vap->va_blocksize = PAGE_SIZE;
    338 
    339 	vap->va_atime = ptyfs->ptyfs_atime;
    340 	vap->va_mtime = ptyfs->ptyfs_mtime;
    341 	vap->va_ctime = ptyfs->ptyfs_ctime;
    342 	vap->va_birthtime = ptyfs->ptyfs_birthtime;
    343 	vap->va_mode = ptyfs->ptyfs_mode;
    344 	vap->va_flags = ptyfs->ptyfs_flags;
    345 	vap->va_uid = ptyfs->ptyfs_uid;
    346 	vap->va_gid = ptyfs->ptyfs_gid;
    347 
    348 	switch (ptyfs->ptyfs_type) {
    349 	case PTYFSpts:
    350 	case PTYFSptc:
    351 		if (pty_isfree(ptyfs->ptyfs_pty, 1))
    352 			return ENOENT;
    353 		vap->va_bytes = vap->va_size = 0;
    354 		vap->va_rdev = ap->a_vp->v_rdev;
    355 		vap->va_nlink = 1;
    356 		break;
    357 	case PTYFSroot:
    358 		vap->va_rdev = 0;
    359 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    360 		vap->va_nlink = 2;
    361 		break;
    362 	default:
    363 		return EOPNOTSUPP;
    364 	}
    365 
    366 	return 0;
    367 }
    368 
    369 /*ARGSUSED*/
    370 int
    371 ptyfs_setattr(void *v)
    372 {
    373 	struct vop_setattr_args /* {
    374 		struct vnodeop_desc *a_desc;
    375 		struct vnode *a_vp;
    376 		struct vattr *a_vap;
    377 		kauth_cred_t a_cred;
    378 	} */ *ap = v;
    379 	struct vnode *vp = ap->a_vp;
    380 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    381 	struct vattr *vap = ap->a_vap;
    382 	kauth_cred_t cred = ap->a_cred;
    383 	struct lwp *l = curlwp;
    384 	int error;
    385 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    386 	bool changing_sysflags = false;
    387 
    388 	if (vap->va_size != VNOVALSIZE) {
    389  		switch (ptyfs->ptyfs_type) {
    390  		case PTYFSroot:
    391  			return EISDIR;
    392  		case PTYFSpts:
    393  		case PTYFSptc:
    394 			break;
    395 		default:
    396 			return EINVAL;
    397 		}
    398 	}
    399 
    400 	if (vap->va_flags != VNOVALFLAGS) {
    401 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    402 			return EROFS;
    403 
    404 		/* Immutable and append-only flags are not supported on ptyfs. */
    405 		if (vap->va_flags & (IMMUTABLE | APPEND))
    406 			return EINVAL;
    407 
    408 		/* Snapshot flag cannot be set or cleared */
    409 		if ((vap->va_flags & SF_SNAPSHOT) != (ptyfs->ptyfs_flags & SF_SNAPSHOT))
    410 			return EPERM;
    411 
    412 		if ((ptyfs->ptyfs_flags & SF_SETTABLE) != (vap->va_flags & SF_SETTABLE)) {
    413 			changing_sysflags = true;
    414 			action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    415 		}
    416 
    417 		error = kauth_authorize_vnode(cred, action, vp, NULL,
    418 		    genfs_can_chflags(vp, cred, ptyfs->ptyfs_uid,
    419 		    changing_sysflags));
    420 		if (error)
    421 			return error;
    422 
    423 		if (changing_sysflags) {
    424 			ptyfs->ptyfs_flags = vap->va_flags;
    425 		} else {
    426 			ptyfs->ptyfs_flags &= SF_SETTABLE;
    427 			ptyfs->ptyfs_flags |= (vap->va_flags & UF_SETTABLE);
    428 		}
    429 		ptyfs->ptyfs_status |= PTYFS_CHANGE;
    430 	}
    431 
    432 	/*
    433 	 * Go through the fields and update iff not VNOVAL.
    434 	 */
    435 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    436 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    437 			return EROFS;
    438 		error = ptyfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    439 		if (error)
    440 			return error;
    441 	}
    442 
    443 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
    444 	    vap->va_birthtime.tv_sec != VNOVAL) {
    445 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    446 			return EROFS;
    447 		if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0)
    448 			return EPERM;
    449 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
    450 		    NULL, genfs_can_chtimes(vp, cred, ptyfs->ptyfs_uid,
    451 		    vap->va_vaflags));
    452 		if (error)
    453 			return (error);
    454 		if (vap->va_atime.tv_sec != VNOVAL)
    455 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    456 				ptyfs->ptyfs_status |= PTYFS_ACCESS;
    457 		if (vap->va_mtime.tv_sec != VNOVAL) {
    458 			ptyfs->ptyfs_status |= PTYFS_CHANGE | PTYFS_MODIFY;
    459 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
    460 				ptyfs->ptyfs_status |= PTYFS_ACCESS;
    461 		}
    462 		if (vap->va_birthtime.tv_sec != VNOVAL)
    463 			ptyfs->ptyfs_birthtime = vap->va_birthtime;
    464 		ptyfs->ptyfs_status |= PTYFS_CHANGE;
    465 		error = ptyfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
    466 		if (error)
    467 			return error;
    468 	}
    469 	if (vap->va_mode != (mode_t)VNOVAL) {
    470 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    471 			return EROFS;
    472 		if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0 &&
    473 		    (vap->va_mode &
    474 		    (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IXOTH|S_IWOTH)))
    475 			return EPERM;
    476 		error = ptyfs_chmod(vp, vap->va_mode, cred, l);
    477 		if (error)
    478 			return error;
    479 	}
    480 	VN_KNOTE(vp, NOTE_ATTRIB);
    481 	return 0;
    482 }
    483 
    484 /*
    485  * Change the mode on a file.
    486  * Inode must be locked before calling.
    487  */
    488 static int
    489 ptyfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
    490 {
    491 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    492 	int error;
    493 
    494 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    495 	    NULL, genfs_can_chmod(vp, cred, ptyfs->ptyfs_uid, ptyfs->ptyfs_gid,
    496 	    mode));
    497 	if (error)
    498 		return (error);
    499 
    500 	ptyfs->ptyfs_mode &= ~ALLPERMS;
    501 	ptyfs->ptyfs_mode |= (mode & ALLPERMS);
    502 	return 0;
    503 }
    504 
    505 /*
    506  * Perform chown operation on inode ip;
    507  * inode must be locked prior to call.
    508  */
    509 static int
    510 ptyfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
    511     struct lwp *l)
    512 {
    513 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    514 	int error;
    515 
    516 	if (uid == (uid_t)VNOVAL)
    517 		uid = ptyfs->ptyfs_uid;
    518 	if (gid == (gid_t)VNOVAL)
    519 		gid = ptyfs->ptyfs_gid;
    520 
    521 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    522 	    NULL, genfs_can_chown(vp, cred, ptyfs->ptyfs_uid, ptyfs->ptyfs_gid,
    523 	    uid, gid));
    524 	if (error)
    525 		return (error);
    526 
    527 	ptyfs->ptyfs_gid = gid;
    528 	ptyfs->ptyfs_uid = uid;
    529 	return 0;
    530 }
    531 
    532 /*
    533  * implement access checking.
    534  *
    535  * actually, the check for super-user is slightly
    536  * broken since it will allow read access to write-only
    537  * objects.  this doesn't cause any particular trouble
    538  * but does mean that the i/o entry points need to check
    539  * that the operation really does make sense.
    540  */
    541 int
    542 ptyfs_access(void *v)
    543 {
    544 	struct vop_access_args /* {
    545 		struct vnode *a_vp;
    546 		accmode_t a_accmode;
    547 		kauth_cred_t a_cred;
    548 	} */ *ap = v;
    549 	struct vattr va;
    550 	int error;
    551 
    552 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
    553 		return error;
    554 
    555 	return kauth_authorize_vnode(ap->a_cred,
    556 	    KAUTH_ACCESS_ACTION(ap->a_accmode, ap->a_vp->v_type, va.va_mode),
    557 	    ap->a_vp, NULL, genfs_can_access(ap->a_vp, ap->a_cred, va.va_uid,
    558 	    va.va_gid, va.va_mode, NULL, ap->a_accmode));
    559 }
    560 
    561 /*
    562  * lookup.  this is incredibly complicated in the
    563  * general case, however for most pseudo-filesystems
    564  * very little needs to be done.
    565  *
    566  * Locking isn't hard here, just poorly documented.
    567  *
    568  * If we're looking up ".", just vref the parent & return it.
    569  *
    570  * If we're looking up "..", unlock the parent, and lock "..". If everything
    571  * went ok, try to re-lock the parent. We do this to prevent lock races.
    572  *
    573  * For anything else, get the needed node.
    574  *
    575  * We try to exit with the parent locked in error cases.
    576  */
    577 int
    578 ptyfs_lookup(void *v)
    579 {
    580 	struct vop_lookup_v2_args /* {
    581 		struct vnode * a_dvp;
    582 		struct vnode ** a_vpp;
    583 		struct componentname * a_cnp;
    584 	} */ *ap = v;
    585 	struct componentname *cnp = ap->a_cnp;
    586 	struct vnode **vpp = ap->a_vpp;
    587 	struct vnode *dvp = ap->a_dvp;
    588 	const char *pname = cnp->cn_nameptr;
    589 	struct ptyfsnode *ptyfs;
    590 	int pty, error;
    591 
    592 	*vpp = NULL;
    593 
    594 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    595 		return EROFS;
    596 
    597 	if (cnp->cn_namelen == 1 && *pname == '.') {
    598 		*vpp = dvp;
    599 		vref(dvp);
    600 		return 0;
    601 	}
    602 
    603 	ptyfs = VTOPTYFS(dvp);
    604 	switch (ptyfs->ptyfs_type) {
    605 	case PTYFSroot:
    606 		/*
    607 		 * Shouldn't get here with .. in the root node.
    608 		 */
    609 		if (cnp->cn_flags & ISDOTDOT)
    610 			return EIO;
    611 
    612 		pty = atoi(pname, cnp->cn_namelen);
    613 		if (pty < 0 || ptyfs_next_active(dvp->v_mount, pty) != pty)
    614 			break;
    615 		error = ptyfs_allocvp(dvp->v_mount, vpp, PTYFSpts, pty);
    616 		if (error)
    617 			return error;
    618 		if (ptyfs_next_active(dvp->v_mount, pty) != pty) {
    619 			vrele(*vpp);
    620 			*vpp = NULL;
    621 			return ENOENT;
    622 		}
    623 		return 0;
    624 
    625 	default:
    626 		return ENOTDIR;
    627 	}
    628 
    629 	return cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS;
    630 }
    631 
    632 /*
    633  * readdir returns directory entries from ptyfsnode (vp).
    634  *
    635  * the strategy here with ptyfs is to generate a single
    636  * directory entry at a time (struct dirent) and then
    637  * copy that out to userland using uiomove.  a more efficent
    638  * though more complex implementation, would try to minimize
    639  * the number of calls to uiomove().  for ptyfs, this is
    640  * hardly worth the added code complexity.
    641  *
    642  * this should just be done through read()
    643  */
    644 int
    645 ptyfs_readdir(void *v)
    646 {
    647 	struct vop_readdir_args /* {
    648 		struct vnode *a_vp;
    649 		struct uio *a_uio;
    650 		kauth_cred_t a_cred;
    651 		int *a_eofflag;
    652 		off_t **a_cookies;
    653 		int *a_ncookies;
    654 	} */ *ap = v;
    655 	struct uio *uio = ap->a_uio;
    656 	struct dirent *dp;
    657 	struct ptyfsnode *ptyfs;
    658 	off_t i;
    659 	int error;
    660 	off_t *cookies = NULL;
    661 	int ncookies;
    662 	struct vnode *vp;
    663 	int n, nc = 0;
    664 
    665 	vp = ap->a_vp;
    666 	ptyfs = VTOPTYFS(vp);
    667 
    668 	if (uio->uio_resid < UIO_MX)
    669 		return EINVAL;
    670 	if (uio->uio_offset < 0)
    671 		return EINVAL;
    672 
    673 	dp = malloc(sizeof(struct dirent), M_PTYFSTMP, M_WAITOK | M_ZERO);
    674 
    675 	error = 0;
    676 	i = uio->uio_offset;
    677 	dp->d_reclen = UIO_MX;
    678 	ncookies = uio->uio_resid / UIO_MX;
    679 
    680 	if (ptyfs->ptyfs_type != PTYFSroot) {
    681 		error = ENOTDIR;
    682 		goto out;
    683 	}
    684 
    685 	if (i >= npty)
    686 		goto out;
    687 
    688 	if (ap->a_ncookies) {
    689 		ncookies = uimin(ncookies, (npty + 2 - i));
    690 		cookies = malloc(ncookies * sizeof (off_t),
    691 		    M_TEMP, M_WAITOK);
    692 		*ap->a_cookies = cookies;
    693 	}
    694 
    695 	for (; i < 2; i++) {
    696 		/* `.' and/or `..' */
    697 		dp->d_fileno = PTYFS_FILENO(PTYFSroot, 0);
    698 		dp->d_namlen = i + 1;
    699 		(void)memcpy(dp->d_name, "..", dp->d_namlen);
    700 		dp->d_name[i + 1] = '\0';
    701 		dp->d_type = DT_DIR;
    702 		if ((error = uiomove(dp, UIO_MX, uio)) != 0)
    703 			goto out;
    704 		if (cookies)
    705 			*cookies++ = i + 1;
    706 		nc++;
    707 	}
    708 	while (uio->uio_resid >= UIO_MX) {
    709 		/* check for used ptys */
    710 		n = ptyfs_next_active(vp->v_mount, i - 2);
    711 		if (n < 0)
    712 			break;
    713 		dp->d_fileno = PTYFS_FILENO(PTYFSpts, n);
    714 		dp->d_namlen = snprintf(dp->d_name, sizeof(dp->d_name),
    715 		    "%lld", (long long)(n));
    716 		dp->d_type = DT_CHR;
    717 		if ((error = uiomove(dp, UIO_MX, uio)) != 0)
    718 			goto out;
    719 		i = n + 3;
    720 		if (cookies)
    721 			*cookies++ = i;
    722 		nc++;
    723 	}
    724 
    725 out:
    726 	/* not pertinent in error cases */
    727 	ncookies = nc;
    728 
    729 	if (ap->a_ncookies) {
    730 		if (error) {
    731 			if (cookies)
    732 				free(*ap->a_cookies, M_TEMP);
    733 			*ap->a_ncookies = 0;
    734 			*ap->a_cookies = NULL;
    735 		} else
    736 			*ap->a_ncookies = ncookies;
    737 	}
    738 	uio->uio_offset = i;
    739 	free(dp, M_PTYFSTMP);
    740 	return error;
    741 }
    742 
    743 int
    744 ptyfs_open(void *v)
    745 {
    746 	struct vop_open_args /* {
    747 		struct vnode *a_vp;
    748 		int  a_mode;
    749 		kauth_cred_t a_cred;
    750 	} */ *ap = v;
    751 	struct vnode *vp = ap->a_vp;
    752 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    753 
    754 	switch (ptyfs->ptyfs_type) {
    755 	case PTYFSpts:
    756 	case PTYFSptc:
    757 		return spec_open(v);
    758 	case PTYFSroot:
    759 		return 0;
    760 	default:
    761 		return EINVAL;
    762 	}
    763 }
    764 
    765 int
    766 ptyfs_close(void *v)
    767 {
    768 	struct vop_close_args /* {
    769 		struct vnode *a_vp;
    770 		int  a_fflag;
    771 		kauth_cred_t a_cred;
    772 	} */ *ap = v;
    773 	struct vnode *vp = ap->a_vp;
    774 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    775 
    776 	mutex_enter(vp->v_interlock);
    777 	if (vrefcnt(vp) > 1)
    778 		PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
    779 	mutex_exit(vp->v_interlock);
    780 
    781 	switch (ptyfs->ptyfs_type) {
    782 	case PTYFSpts:
    783 	case PTYFSptc:
    784 		return spec_close(v);
    785 	case PTYFSroot:
    786 		return 0;
    787 	default:
    788 		return EINVAL;
    789 	}
    790 }
    791 
    792 int
    793 ptyfs_read(void *v)
    794 {
    795 	struct vop_read_args /* {
    796 		struct vnode *a_vp;
    797 		struct uio *a_uio;
    798 		int  a_ioflag;
    799 		kauth_cred_t a_cred;
    800 	} */ *ap = v;
    801 	struct timespec ts;
    802 	struct vnode *vp = ap->a_vp;
    803 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    804 	int error;
    805 
    806 	if (vp->v_type == VDIR)
    807 		return EISDIR;
    808 
    809 	ptyfs->ptyfs_status |= PTYFS_ACCESS;
    810 	/* hardclock() resolution is good enough for ptyfs */
    811 	getnanotime(&ts);
    812 	(void)ptyfs_update(vp, &ts, &ts, 0);
    813 
    814 	switch (ptyfs->ptyfs_type) {
    815 	case PTYFSpts:
    816 	case PTYFSptc:
    817 		VOP_UNLOCK(vp);
    818 		error = cdev_read(vp->v_rdev, ap->a_uio, ap->a_ioflag);
    819 		vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
    820 		return error;
    821 	default:
    822 		return EOPNOTSUPP;
    823 	}
    824 }
    825 
    826 int
    827 ptyfs_write(void *v)
    828 {
    829 	struct vop_write_args /* {
    830 		struct vnode *a_vp;
    831 		struct uio *a_uio;
    832 		int  a_ioflag;
    833 		kauth_cred_t a_cred;
    834 	} */ *ap = v;
    835 	struct timespec ts;
    836 	struct vnode *vp = ap->a_vp;
    837 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    838 	int error;
    839 
    840 	ptyfs->ptyfs_status |= PTYFS_MODIFY;
    841 	getnanotime(&ts);
    842 	(void)ptyfs_update(vp, &ts, &ts, 0);
    843 
    844 	switch (ptyfs->ptyfs_type) {
    845 	case PTYFSpts:
    846 	case PTYFSptc:
    847 		VOP_UNLOCK(vp);
    848 		error = cdev_write(vp->v_rdev, ap->a_uio, ap->a_ioflag);
    849 		vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
    850 		return error;
    851 	default:
    852 		return EOPNOTSUPP;
    853 	}
    854 }
    855 
    856 int
    857 ptyfs_ioctl(void *v)
    858 {
    859 	struct vop_ioctl_args /* {
    860 		struct vnode *a_vp;
    861 		u_long a_command;
    862 		void *a_data;
    863 		int  a_fflag;
    864 		kauth_cred_t a_cred;
    865 	} */ *ap = v;
    866 	struct vnode *vp = ap->a_vp;
    867 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    868 
    869 	switch (ptyfs->ptyfs_type) {
    870 	case PTYFSpts:
    871 	case PTYFSptc:
    872 		return cdev_ioctl(vp->v_rdev, ap->a_command,
    873 		    ap->a_data, ap->a_fflag, curlwp);
    874 	default:
    875 		return EOPNOTSUPP;
    876 	}
    877 }
    878 
    879 int
    880 ptyfs_poll(void *v)
    881 {
    882 	struct vop_poll_args /* {
    883 		struct vnode *a_vp;
    884 		int a_events;
    885 	} */ *ap = v;
    886 	struct vnode *vp = ap->a_vp;
    887 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    888 
    889 	switch (ptyfs->ptyfs_type) {
    890 	case PTYFSpts:
    891 	case PTYFSptc:
    892 		return cdev_poll(vp->v_rdev, ap->a_events, curlwp);
    893 	default:
    894 		return genfs_poll(v);
    895 	}
    896 }
    897 
    898 int
    899 ptyfs_kqfilter(void *v)
    900 {
    901 	struct vop_kqfilter_args /* {
    902 		struct vnode *a_vp;
    903 		struct knote *a_kn;
    904 	} */ *ap = v;
    905 	struct vnode *vp = ap->a_vp;
    906 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    907 
    908 	switch (ptyfs->ptyfs_type) {
    909 	case PTYFSpts:
    910 	case PTYFSptc:
    911 		return cdev_kqfilter(vp->v_rdev, ap->a_kn);
    912 	default:
    913 		return genfs_kqfilter(v);
    914 	}
    915 }
    916 
    917 static int
    918 ptyfs_update(struct vnode *vp, const struct timespec *acc,
    919     const struct timespec *mod, int flags)
    920 {
    921 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    922 
    923 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    924 		return 0;
    925 
    926 	PTYFS_ITIMES(ptyfs, acc, mod, NULL);
    927 	return 0;
    928 }
    929 
    930 void
    931 ptyfs_itimes(struct ptyfsnode *ptyfs, const struct timespec *acc,
    932     const struct timespec *mod, const struct timespec *cre)
    933 {
    934 	struct timespec now;
    935 
    936 	KASSERT(ptyfs->ptyfs_status & (PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY));
    937 
    938 	getnanotime(&now);
    939 	if (ptyfs->ptyfs_status & PTYFS_ACCESS) {
    940 		if (acc == NULL)
    941 			acc = &now;
    942 		ptyfs->ptyfs_atime = *acc;
    943 	}
    944 	if (ptyfs->ptyfs_status & PTYFS_MODIFY) {
    945 		if (mod == NULL)
    946 			mod = &now;
    947 		ptyfs->ptyfs_mtime = *mod;
    948 	}
    949 	if (ptyfs->ptyfs_status & PTYFS_CHANGE) {
    950 		if (cre == NULL)
    951 			cre = &now;
    952 		ptyfs->ptyfs_ctime = *cre;
    953 	}
    954 	ptyfs->ptyfs_status &= ~(PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY);
    955 }
    956 
    957 /*
    958  * convert decimal ascii to int
    959  */
    960 static int
    961 atoi(const char *b, size_t len)
    962 {
    963 	int p = 0;
    964 
    965 	while (len--) {
    966 		char c = *b++;
    967 		if (c < '0' || c > '9')
    968 			return -1;
    969 		p = 10 * p + (c - '0');
    970 	}
    971 
    972 	return p;
    973 }
    974