Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vnops.c revision 1.130.4.1
      1 /*	$NetBSD: fdesc_vnops.c,v 1.130.4.1 2021/05/03 09:03:22 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      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  *	@(#)fdesc_vnops.c	8.17 (Berkeley) 5/22/95
     35  *
     36  * #Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp #
     37  */
     38 
     39 /*
     40  * /dev/fd Filesystem
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.130.4.1 2021/05/03 09:03:22 martin Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/time.h>
     49 #include <sys/proc.h>
     50 #include <sys/kernel.h>	/* boottime */
     51 #include <sys/resourcevar.h>
     52 #include <sys/socketvar.h>
     53 #include <sys/filedesc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/malloc.h>
     56 #include <sys/conf.h>
     57 #include <sys/file.h>
     58 #include <sys/stat.h>
     59 #include <sys/mount.h>
     60 #include <sys/namei.h>
     61 #include <sys/buf.h>
     62 #include <sys/dirent.h>
     63 #include <sys/tty.h>
     64 #include <sys/kauth.h>
     65 #include <sys/atomic.h>
     66 
     67 #include <miscfs/fdesc/fdesc.h>
     68 #include <miscfs/genfs/genfs.h>
     69 
     70 #define cttyvp(p) ((p)->p_lflag & PL_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
     71 
     72 dev_t devctty;
     73 
     74 #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
     75 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
     76 #endif
     77 
     78 int	fdesc_lookup(void *);
     79 #define	fdesc_create	genfs_eopnotsupp
     80 #define	fdesc_mknod	genfs_eopnotsupp
     81 int	fdesc_open(void *);
     82 #define	fdesc_close	genfs_nullop
     83 #define	fdesc_access	genfs_nullop
     84 int	fdesc_getattr(void *);
     85 int	fdesc_setattr(void *);
     86 int	fdesc_read(void *);
     87 int	fdesc_write(void *);
     88 int	fdesc_ioctl(void *);
     89 int	fdesc_poll(void *);
     90 int	fdesc_kqfilter(void *);
     91 #define	fdesc_mmap	genfs_eopnotsupp
     92 #define	fdesc_fcntl	genfs_fcntl
     93 #define	fdesc_fsync	genfs_nullop
     94 #define	fdesc_seek	genfs_seek
     95 #define	fdesc_remove	genfs_eopnotsupp
     96 int	fdesc_link(void *);
     97 #define	fdesc_rename	genfs_eopnotsupp
     98 #define	fdesc_mkdir	genfs_eopnotsupp
     99 #define	fdesc_rmdir	genfs_eopnotsupp
    100 int	fdesc_symlink(void *);
    101 int	fdesc_readdir(void *);
    102 int	fdesc_readlink(void *);
    103 #define	fdesc_abortop	genfs_abortop
    104 int	fdesc_inactive(void *);
    105 int	fdesc_reclaim(void *);
    106 #define	fdesc_lock	genfs_lock
    107 #define	fdesc_unlock	genfs_unlock
    108 #define	fdesc_bmap	genfs_badop
    109 #define	fdesc_strategy	genfs_badop
    110 int	fdesc_print(void *);
    111 int	fdesc_pathconf(void *);
    112 #define	fdesc_islocked	genfs_islocked
    113 #define	fdesc_advlock	genfs_einval
    114 #define	fdesc_bwrite	genfs_eopnotsupp
    115 #define fdesc_revoke	genfs_revoke
    116 #define fdesc_putpages	genfs_null_putpages
    117 
    118 static int fdesc_attr(int, struct vattr *, kauth_cred_t);
    119 
    120 int (**fdesc_vnodeop_p)(void *);
    121 const struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
    122 	{ &vop_default_desc, vn_default_error },
    123 	{ &vop_lookup_desc, fdesc_lookup },		/* lookup */
    124 	{ &vop_create_desc, fdesc_create },		/* create */
    125 	{ &vop_mknod_desc, fdesc_mknod },		/* mknod */
    126 	{ &vop_open_desc, fdesc_open },			/* open */
    127 	{ &vop_close_desc, fdesc_close },		/* close */
    128 	{ &vop_access_desc, fdesc_access },		/* access */
    129 	{ &vop_getattr_desc, fdesc_getattr },		/* getattr */
    130 	{ &vop_setattr_desc, fdesc_setattr },		/* setattr */
    131 	{ &vop_read_desc, fdesc_read },			/* read */
    132 	{ &vop_write_desc, fdesc_write },		/* write */
    133 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
    134 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
    135 	{ &vop_ioctl_desc, fdesc_ioctl },		/* ioctl */
    136 	{ &vop_fcntl_desc, fdesc_fcntl },		/* fcntl */
    137 	{ &vop_poll_desc, fdesc_poll },			/* poll */
    138 	{ &vop_kqfilter_desc, fdesc_kqfilter },		/* kqfilter */
    139 	{ &vop_revoke_desc, fdesc_revoke },		/* revoke */
    140 	{ &vop_mmap_desc, fdesc_mmap },			/* mmap */
    141 	{ &vop_fsync_desc, fdesc_fsync },		/* fsync */
    142 	{ &vop_seek_desc, fdesc_seek },			/* seek */
    143 	{ &vop_remove_desc, fdesc_remove },		/* remove */
    144 	{ &vop_link_desc, fdesc_link },			/* link */
    145 	{ &vop_rename_desc, fdesc_rename },		/* rename */
    146 	{ &vop_mkdir_desc, fdesc_mkdir },		/* mkdir */
    147 	{ &vop_rmdir_desc, fdesc_rmdir },		/* rmdir */
    148 	{ &vop_symlink_desc, fdesc_symlink },		/* symlink */
    149 	{ &vop_readdir_desc, fdesc_readdir },		/* readdir */
    150 	{ &vop_readlink_desc, fdesc_readlink },		/* readlink */
    151 	{ &vop_abortop_desc, fdesc_abortop },		/* abortop */
    152 	{ &vop_inactive_desc, fdesc_inactive },		/* inactive */
    153 	{ &vop_reclaim_desc, fdesc_reclaim },		/* reclaim */
    154 	{ &vop_lock_desc, fdesc_lock },			/* lock */
    155 	{ &vop_unlock_desc, fdesc_unlock },		/* unlock */
    156 	{ &vop_bmap_desc, fdesc_bmap },			/* bmap */
    157 	{ &vop_strategy_desc, fdesc_strategy },		/* strategy */
    158 	{ &vop_print_desc, fdesc_print },		/* print */
    159 	{ &vop_islocked_desc, fdesc_islocked },		/* islocked */
    160 	{ &vop_pathconf_desc, fdesc_pathconf },		/* pathconf */
    161 	{ &vop_advlock_desc, fdesc_advlock },		/* advlock */
    162 	{ &vop_bwrite_desc, fdesc_bwrite },		/* bwrite */
    163 	{ &vop_putpages_desc, fdesc_putpages },		/* putpages */
    164 	{ NULL, NULL }
    165 };
    166 
    167 const struct vnodeopv_desc fdesc_vnodeop_opv_desc =
    168 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
    169 
    170 /*
    171  * Initialise cache headers
    172  */
    173 void
    174 fdesc_init(void)
    175 {
    176 	int cttymajor;
    177 
    178 	/* locate the major number */
    179 	cttymajor = devsw_name2chr("ctty", NULL, 0);
    180 	devctty = makedev(cttymajor, 0);
    181 }
    182 
    183 void
    184 fdesc_done(void)
    185 {
    186 }
    187 
    188 /*
    189  * vp is the current namei directory
    190  * ndp is the name to locate in that directory...
    191  */
    192 int
    193 fdesc_lookup(void *v)
    194 {
    195 	struct vop_lookup_v2_args /* {
    196 		struct vnode * a_dvp;
    197 		struct vnode ** a_vpp;
    198 		struct componentname * a_cnp;
    199 	} */ *ap = v;
    200 	struct vnode **vpp = ap->a_vpp;
    201 	struct vnode *dvp = ap->a_dvp;
    202 	struct componentname *cnp = ap->a_cnp;
    203 	struct lwp *l = curlwp;
    204 	const char *pname = cnp->cn_nameptr;
    205 	struct proc *p = l->l_proc;
    206 	unsigned fd = 0;
    207 	int error, ix = -1;
    208 	fdtab_t *dt;
    209 
    210 	dt = curlwp->l_fd->fd_dt;
    211 
    212 	if (cnp->cn_namelen == 1 && *pname == '.') {
    213 		*vpp = dvp;
    214 		vref(dvp);
    215 		return (0);
    216 	}
    217 
    218 	switch (VTOFDESC(dvp)->fd_type) {
    219 	default:
    220 	case Flink:
    221 	case Fdesc:
    222 	case Fctty:
    223 		error = ENOTDIR;
    224 		goto bad;
    225 
    226 	case Froot:
    227 		if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
    228 			ix = FD_DEVFD;
    229 			goto good;
    230 		}
    231 
    232 		if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
    233 			struct vnode *ttyvp = cttyvp(p);
    234 			if (ttyvp == NULL) {
    235 				error = ENXIO;
    236 				goto bad;
    237 			}
    238 			ix = FD_CTTY;
    239 			goto good;
    240 		}
    241 
    242 		switch (cnp->cn_namelen) {
    243 		case 5:
    244 			if (memcmp(pname, "stdin", 5) == 0) {
    245 				ix = FD_STDIN;
    246 				goto good;
    247 			}
    248 			break;
    249 		case 6:
    250 			if (memcmp(pname, "stdout", 6) == 0) {
    251 				ix = FD_STDOUT;
    252 				goto good;
    253 			} else if (memcmp(pname, "stderr", 6) == 0) {
    254 				ix = FD_STDERR;
    255 				goto good;
    256 			}
    257 			break;
    258 		}
    259 
    260 		error = ENOENT;
    261 		goto bad;
    262 
    263 	case Fdevfd:
    264 		if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
    265 			ix = FD_ROOT;
    266 			goto good;
    267 		}
    268 
    269 		fd = 0;
    270 		while (*pname >= '0' && *pname <= '9') {
    271 			fd = 10 * fd + *pname++ - '0';
    272 			if (fd >= dt->dt_nfiles)
    273 				break;
    274 		}
    275 
    276 		if (*pname != '\0') {
    277 			error = ENOENT;
    278 			goto bad;
    279 		}
    280 
    281 		if (fd >= dt->dt_nfiles || dt->dt_ff[fd] == NULL ||
    282 		    dt->dt_ff[fd]->ff_file == NULL) {
    283 			error = EBADF;
    284 			goto bad;
    285 		}
    286 
    287 		ix = FD_DESC + fd;
    288 		goto good;
    289 	}
    290 
    291 bad:
    292 	*vpp = NULL;
    293 	return error;
    294 
    295 good:
    296 	KASSERT(ix != -1);
    297 	error = vcache_get(dvp->v_mount, &ix, sizeof(ix), vpp);
    298 	if (error)
    299 		return error;
    300 
    301 	/*
    302 	 * Prevent returning VNON nodes.
    303 	 * Operation fdesc_inactive() will reset the type to VNON.
    304 	 */
    305 	if (ix == FD_CTTY)
    306 		(*vpp)->v_type = VCHR;
    307 	else if (ix >= FD_DESC)
    308 		(*vpp)->v_type = VREG;
    309 	KASSERT((*vpp)->v_type != VNON);
    310 
    311 	return 0;
    312 }
    313 
    314 int
    315 fdesc_open(void *v)
    316 {
    317 	struct vop_open_args /* {
    318 		struct vnode *a_vp;
    319 		int  a_mode;
    320 		kauth_cred_t a_cred;
    321 	} */ *ap = v;
    322 	struct vnode *vp = ap->a_vp;
    323 
    324 	switch (VTOFDESC(vp)->fd_type) {
    325 	case Fdesc:
    326 		/*
    327 		 * XXX Kludge: set dupfd to contain the value of the
    328 		 * the file descriptor being sought for duplication. The error
    329 		 * return ensures that the vnode for this device will be
    330 		 * released by vn_open. Open will detect this special error and
    331 		 * take the actions in dupfdopen.  Other callers of vn_open or
    332 		 * VOP_OPEN will simply report the error.
    333 		 */
    334 		curlwp->l_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
    335 		return EDUPFD;
    336 
    337 	case Fctty:
    338 		return cdev_open(devctty, ap->a_mode, 0, curlwp);
    339 	case Froot:
    340 	case Fdevfd:
    341 	case Flink:
    342 		break;
    343 	}
    344 
    345 	return (0);
    346 }
    347 
    348 static int
    349 fdesc_attr(int fd, struct vattr *vap, kauth_cred_t cred)
    350 {
    351 	file_t *fp;
    352 	struct stat stb;
    353 	int error;
    354 
    355 	if ((fp = fd_getfile(fd)) == NULL)
    356 		return (EBADF);
    357 
    358 	switch (fp->f_type) {
    359 	case DTYPE_VNODE:
    360 		vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
    361 		error = VOP_GETATTR(fp->f_vnode, vap, cred);
    362 		VOP_UNLOCK(fp->f_vnode);
    363 		if (error == 0 && vap->va_type == VDIR) {
    364 			/*
    365 			 * directories can cause loops in the namespace,
    366 			 * so turn off the 'x' bits to avoid trouble.
    367 			 */
    368 			vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
    369 		}
    370 		break;
    371 
    372 	default:
    373 		memset(&stb, 0, sizeof(stb));
    374 		error = (*fp->f_ops->fo_stat)(fp, &stb);
    375 		if (error)
    376 			break;
    377 
    378 		vattr_null(vap);
    379 		switch(fp->f_type) {
    380 		case DTYPE_SOCKET:
    381 			vap->va_type = VSOCK;
    382 			break;
    383 		case DTYPE_PIPE:
    384 			vap->va_type = VFIFO;
    385 			break;
    386 		default:
    387 			/* use VNON perhaps? */
    388 			vap->va_type = VBAD;
    389 			break;
    390 		}
    391 		vap->va_mode = stb.st_mode;
    392 		vap->va_nlink = stb.st_nlink;
    393 		vap->va_uid = stb.st_uid;
    394 		vap->va_gid = stb.st_gid;
    395 		vap->va_fsid = stb.st_dev;
    396 		vap->va_fileid = stb.st_ino;
    397 		vap->va_size = stb.st_size;
    398 		vap->va_blocksize = stb.st_blksize;
    399 		vap->va_atime = stb.st_atimespec;
    400 		vap->va_mtime = stb.st_mtimespec;
    401 		vap->va_ctime = stb.st_ctimespec;
    402 		vap->va_gen = stb.st_gen;
    403 		vap->va_flags = stb.st_flags;
    404 		vap->va_rdev = stb.st_rdev;
    405 		vap->va_bytes = stb.st_blocks * stb.st_blksize;
    406 		break;
    407 	}
    408 
    409 	fd_putfile(fd);
    410 	return (error);
    411 }
    412 
    413 int
    414 fdesc_getattr(void *v)
    415 {
    416 	struct vop_getattr_args /* {
    417 		struct vnode *a_vp;
    418 		struct vattr *a_vap;
    419 		kauth_cred_t a_cred;
    420 		struct lwp *a_l;
    421 	} */ *ap = v;
    422 	struct vnode *vp = ap->a_vp;
    423 	struct vattr *vap = ap->a_vap;
    424 	unsigned fd;
    425 	int error = 0;
    426 
    427 	switch (VTOFDESC(vp)->fd_type) {
    428 	case Froot:
    429 	case Fdevfd:
    430 	case Flink:
    431 	case Fctty:
    432 		vattr_null(vap);
    433 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
    434 
    435 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
    436 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
    437 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
    438 
    439 		switch (VTOFDESC(vp)->fd_type) {
    440 		case Flink:
    441 			vap->va_mode = R_ALL|X_ALL;
    442 			vap->va_type = VLNK;
    443 			vap->va_rdev = 0;
    444 			vap->va_nlink = 1;
    445 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
    446 			break;
    447 
    448 		case Fctty:
    449 			vap->va_mode = R_ALL|W_ALL;
    450 			vap->va_type = VCHR;
    451 			vap->va_rdev = devctty;
    452 			vap->va_nlink = 1;
    453 			vap->va_size = 0;
    454 			break;
    455 
    456 		default:
    457 			vap->va_mode = R_ALL|X_ALL;
    458 			vap->va_type = VDIR;
    459 			vap->va_rdev = 0;
    460 			vap->va_nlink = 2;
    461 			vap->va_size = DEV_BSIZE;
    462 			break;
    463 		}
    464 		vap->va_uid = 0;
    465 		vap->va_gid = 0;
    466 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    467 		vap->va_blocksize = DEV_BSIZE;
    468 		vap->va_atime.tv_sec = boottime.tv_sec;
    469 		vap->va_atime.tv_nsec = 0;
    470 		vap->va_mtime = vap->va_atime;
    471 		vap->va_ctime = vap->va_mtime;
    472 		vap->va_gen = 0;
    473 		vap->va_flags = 0;
    474 		vap->va_bytes = 0;
    475 		break;
    476 
    477 	case Fdesc:
    478 		fd = VTOFDESC(vp)->fd_fd;
    479 		error = fdesc_attr(fd, vap, ap->a_cred);
    480 		break;
    481 
    482 	default:
    483 		panic("fdesc_getattr");
    484 		break;
    485 	}
    486 
    487 	if (error == 0)
    488 		vp->v_type = vap->va_type;
    489 
    490 	return (error);
    491 }
    492 
    493 int
    494 fdesc_setattr(void *v)
    495 {
    496 	struct vop_setattr_args /* {
    497 		struct vnode *a_vp;
    498 		struct vattr *a_vap;
    499 		kauth_cred_t a_cred;
    500 	} */ *ap = v;
    501 	file_t *fp;
    502 	unsigned fd;
    503 
    504 	/*
    505 	 * Can't mess with the root vnode
    506 	 */
    507 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    508 	case Fdesc:
    509 		break;
    510 
    511 	case Fctty:
    512 		return (0);
    513 
    514 	default:
    515 		return (EACCES);
    516 	}
    517 
    518 	fd = VTOFDESC(ap->a_vp)->fd_fd;
    519 	if ((fp = fd_getfile(fd)) == NULL)
    520 		return (EBADF);
    521 
    522 	/*
    523 	 * XXX: Can't reasonably set the attr's on any types currently.
    524 	 *      On vnode's this will cause truncation and socket/pipes make
    525 	 *      no sense.
    526 	 */
    527 	fd_putfile(fd);
    528 	return (0);
    529 }
    530 
    531 
    532 struct fdesc_target {
    533 	ino_t ft_fileno;
    534 	u_char ft_type;
    535 	u_char ft_namlen;
    536 	const char *ft_name;
    537 } fdesc_targets[] = {
    538 #define N(s) sizeof(s)-1, s
    539 	{ FD_DEVFD,  DT_DIR,     N("fd")     },
    540 	{ FD_STDIN,  DT_LNK,     N("stdin")  },
    541 	{ FD_STDOUT, DT_LNK,     N("stdout") },
    542 	{ FD_STDERR, DT_LNK,     N("stderr") },
    543 	{ FD_CTTY,   DT_UNKNOWN, N("tty")    },
    544 #undef N
    545 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
    546 };
    547 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
    548 
    549 int
    550 fdesc_readdir(void *v)
    551 {
    552 	struct vop_readdir_args /* {
    553 		struct vnode *a_vp;
    554 		struct uio *a_uio;
    555 		kauth_cred_t a_cred;
    556 		int *a_eofflag;
    557 		off_t **a_cookies;
    558 		int *a_ncookies;
    559 	} */ *ap = v;
    560 	struct uio *uio = ap->a_uio;
    561 	struct dirent d;
    562 	off_t i;
    563 	int j;
    564 	int error;
    565 	off_t *cookies = NULL;
    566 	int ncookies;
    567 	fdtab_t *dt;
    568 
    569 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    570 	case Fctty:
    571 		return 0;
    572 
    573 	case Fdesc:
    574 		return ENOTDIR;
    575 
    576 	default:
    577 		break;
    578 	}
    579 
    580 	dt = curlwp->l_fd->fd_dt;
    581 
    582 	if (uio->uio_resid < UIO_MX)
    583 		return EINVAL;
    584 	if (uio->uio_offset < 0)
    585 		return EINVAL;
    586 
    587 	error = 0;
    588 	i = uio->uio_offset;
    589 	(void)memset(&d, 0, UIO_MX);
    590 	d.d_reclen = UIO_MX;
    591 	if (ap->a_ncookies)
    592 		ncookies = uio->uio_resid / UIO_MX;
    593 	else
    594 		ncookies = 0;
    595 
    596 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
    597 		struct fdesc_target *ft;
    598 
    599 		if (i >= nfdesc_targets)
    600 			return 0;
    601 
    602 		if (ap->a_ncookies) {
    603 			ncookies = uimin(ncookies, (nfdesc_targets - i));
    604 			cookies = malloc(ncookies * sizeof(off_t),
    605 			    M_TEMP, M_WAITOK);
    606 			*ap->a_cookies = cookies;
    607 			*ap->a_ncookies = ncookies;
    608 		}
    609 
    610 		for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
    611 		    i < nfdesc_targets; ft++, i++) {
    612 			switch (ft->ft_fileno) {
    613 			case FD_CTTY:
    614 				if (cttyvp(curproc) == NULL)
    615 					continue;
    616 				break;
    617 
    618 			case FD_STDIN:
    619 			case FD_STDOUT:
    620 			case FD_STDERR:
    621 				if ((ft->ft_fileno - FD_STDIN) >=
    622 				    dt->dt_nfiles)
    623 					continue;
    624 				if (dt->dt_ff[ft->ft_fileno - FD_STDIN]
    625 				    == NULL || dt->dt_ff[ft->ft_fileno -
    626 				    FD_STDIN]->ff_file == NULL)
    627 					continue;
    628 				break;
    629 			}
    630 
    631 			d.d_fileno = ft->ft_fileno;
    632 			d.d_namlen = ft->ft_namlen;
    633 			(void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
    634 			d.d_type = ft->ft_type;
    635 
    636 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    637 				break;
    638 			if (cookies)
    639 				*cookies++ = i + 1;
    640 		}
    641 	} else {
    642 		membar_consumer();
    643 		if (ap->a_ncookies) {
    644 			ncookies = uimin(ncookies, dt->dt_nfiles + 2);
    645 			cookies = malloc(ncookies * sizeof(off_t),
    646 			    M_TEMP, M_WAITOK);
    647 			*ap->a_cookies = cookies;
    648 			*ap->a_ncookies = ncookies;
    649 		}
    650 		for (; i - 2 < dt->dt_nfiles && uio->uio_resid >= UIO_MX; i++) {
    651 			switch (i) {
    652 			case 0:
    653 			case 1:
    654 				d.d_fileno = FD_ROOT;		/* XXX */
    655 				d.d_namlen = i + 1;
    656 				(void)memcpy(d.d_name, "..", d.d_namlen);
    657 				d.d_name[i + 1] = '\0';
    658 				d.d_type = DT_DIR;
    659 				break;
    660 
    661 			default:
    662 				j = (int)i - 2;
    663 				if (dt->dt_ff[j] == NULL ||
    664 				    dt->dt_ff[j]->ff_file == NULL)
    665 					continue;
    666 				d.d_fileno = j + FD_STDIN;
    667 				d.d_namlen = snprintf(d.d_name,
    668 				    sizeof(d.d_name), "%d", j);
    669 				d.d_type = DT_UNKNOWN;
    670 				break;
    671 			}
    672 
    673 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    674 				break;
    675 			if (cookies)
    676 				*cookies++ = i + 1;
    677 		}
    678 	}
    679 
    680 	if (ap->a_ncookies && error) {
    681 		free(*ap->a_cookies, M_TEMP);
    682 		*ap->a_ncookies = 0;
    683 		*ap->a_cookies = NULL;
    684 	}
    685 
    686 	uio->uio_offset = i;
    687 	return error;
    688 }
    689 
    690 int
    691 fdesc_readlink(void *v)
    692 {
    693 	struct vop_readlink_args /* {
    694 		struct vnode *a_vp;
    695 		struct uio *a_uio;
    696 		kauth_cred_t a_cred;
    697 	} */ *ap = v;
    698 	struct vnode *vp = ap->a_vp;
    699 	int error;
    700 
    701 	if (vp->v_type != VLNK)
    702 		return (EPERM);
    703 
    704 	if (VTOFDESC(vp)->fd_type == Flink) {
    705 		const char *ln = VTOFDESC(vp)->fd_link;
    706 		error = uiomove(__UNCONST(ln), strlen(ln), ap->a_uio);
    707 	} else {
    708 		error = EOPNOTSUPP;
    709 	}
    710 
    711 	return (error);
    712 }
    713 
    714 int
    715 fdesc_read(void *v)
    716 {
    717 	struct vop_read_args /* {
    718 		struct vnode *a_vp;
    719 		struct uio *a_uio;
    720 		int  a_ioflag;
    721 		kauth_cred_t a_cred;
    722 	} */ *ap = v;
    723 	int error = EOPNOTSUPP;
    724 	struct vnode *vp = ap->a_vp;
    725 
    726 	switch (VTOFDESC(vp)->fd_type) {
    727 	case Fctty:
    728 		VOP_UNLOCK(vp);
    729 		error = cdev_read(devctty, ap->a_uio, ap->a_ioflag);
    730 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    731 		break;
    732 
    733 	default:
    734 		error = EOPNOTSUPP;
    735 		break;
    736 	}
    737 
    738 	return (error);
    739 }
    740 
    741 int
    742 fdesc_write(void *v)
    743 {
    744 	struct vop_write_args /* {
    745 		struct vnode *a_vp;
    746 		struct uio *a_uio;
    747 		int  a_ioflag;
    748 		kauth_cred_t a_cred;
    749 	} */ *ap = v;
    750 	int error = EOPNOTSUPP;
    751 	struct vnode *vp = ap->a_vp;
    752 
    753 	switch (VTOFDESC(vp)->fd_type) {
    754 	case Fctty:
    755 		VOP_UNLOCK(vp);
    756 		error = cdev_write(devctty, ap->a_uio, ap->a_ioflag);
    757 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    758 		break;
    759 
    760 	default:
    761 		error = EOPNOTSUPP;
    762 		break;
    763 	}
    764 
    765 	return (error);
    766 }
    767 
    768 int
    769 fdesc_ioctl(void *v)
    770 {
    771 	struct vop_ioctl_args /* {
    772 		struct vnode *a_vp;
    773 		u_long a_command;
    774 		void *a_data;
    775 		int  a_fflag;
    776 		kauth_cred_t a_cred;
    777 	} */ *ap = v;
    778 	int error = EOPNOTSUPP;
    779 
    780 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    781 	case Fctty:
    782 		error = cdev_ioctl(devctty, ap->a_command, ap->a_data,
    783 		    ap->a_fflag, curlwp);
    784 		break;
    785 
    786 	default:
    787 		error = EOPNOTSUPP;
    788 		break;
    789 	}
    790 
    791 	return (error);
    792 }
    793 
    794 int
    795 fdesc_poll(void *v)
    796 {
    797 	struct vop_poll_args /* {
    798 		struct vnode *a_vp;
    799 		int a_events;
    800 	} */ *ap = v;
    801 	int revents;
    802 
    803 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    804 	case Fctty:
    805 		revents = cdev_poll(devctty, ap->a_events, curlwp);
    806 		break;
    807 
    808 	default:
    809 		revents = genfs_poll(v);
    810 		break;
    811 	}
    812 
    813 	return (revents);
    814 }
    815 
    816 int
    817 fdesc_kqfilter(void *v)
    818 {
    819 	struct vop_kqfilter_args /* {
    820 		struct vnode *a_vp;
    821 		struct knote *a_kn;
    822 	} */ *ap = v;
    823 	int error, fd;
    824 	file_t *fp;
    825 
    826 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    827 	case Fctty:
    828 		error = cdev_kqfilter(devctty, ap->a_kn);
    829 		break;
    830 
    831 	case Fdesc:
    832 		/* just invoke kqfilter for the underlying descriptor */
    833 		fd = VTOFDESC(ap->a_vp)->fd_fd;
    834 		if ((fp = fd_getfile(fd)) == NULL)
    835 			return (1);
    836 		error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
    837 		fd_putfile(fd);
    838 		break;
    839 
    840 	default:
    841 		return (genfs_kqfilter(v));
    842 	}
    843 
    844 	return (error);
    845 }
    846 
    847 int
    848 fdesc_inactive(void *v)
    849 {
    850 	struct vop_inactive_v2_args /* {
    851 		struct vnode *a_vp;
    852 	} */ *ap = v;
    853 	struct vnode *vp = ap->a_vp;
    854 	struct fdescnode *fd = VTOFDESC(vp);
    855 
    856 	/*
    857 	 * Clear out the v_type field to avoid
    858 	 * nasty things happening on reclaim.
    859 	 */
    860 	if (fd->fd_type == Fctty || fd->fd_type == Fdesc)
    861 		vp->v_type = VNON;
    862 
    863 	return (0);
    864 }
    865 
    866 int
    867 fdesc_reclaim(void *v)
    868 {
    869 	struct vop_reclaim_v2_args /* {
    870 		struct vnode *a_vp;
    871 	} */ *ap = v;
    872 	struct vnode *vp = ap->a_vp;
    873 	struct fdescnode *fd = VTOFDESC(vp);
    874 
    875 	VOP_UNLOCK(vp);
    876 
    877 	vp->v_data = NULL;
    878 	kmem_free(fd, sizeof(struct fdescnode));
    879 
    880 	return (0);
    881 }
    882 
    883 /*
    884  * Return POSIX pathconf information applicable to special devices.
    885  */
    886 int
    887 fdesc_pathconf(void *v)
    888 {
    889 	struct vop_pathconf_args /* {
    890 		struct vnode *a_vp;
    891 		int a_name;
    892 		register_t *a_retval;
    893 	} */ *ap = v;
    894 
    895 	switch (ap->a_name) {
    896 	case _PC_LINK_MAX:
    897 		*ap->a_retval = LINK_MAX;
    898 		return (0);
    899 	case _PC_MAX_CANON:
    900 		*ap->a_retval = MAX_CANON;
    901 		return (0);
    902 	case _PC_MAX_INPUT:
    903 		*ap->a_retval = MAX_INPUT;
    904 		return (0);
    905 	case _PC_PIPE_BUF:
    906 		*ap->a_retval = PIPE_BUF;
    907 		return (0);
    908 	case _PC_CHOWN_RESTRICTED:
    909 		*ap->a_retval = 1;
    910 		return (0);
    911 	case _PC_VDISABLE:
    912 		*ap->a_retval = _POSIX_VDISABLE;
    913 		return (0);
    914 	case _PC_SYNC_IO:
    915 		*ap->a_retval = 1;
    916 		return (0);
    917 	default:
    918 		return (EINVAL);
    919 	}
    920 	/* NOTREACHED */
    921 }
    922 
    923 /*
    924  * Print out the contents of a /dev/fd vnode.
    925  */
    926 /* ARGSUSED */
    927 int
    928 fdesc_print(void *v)
    929 {
    930 	printf("tag VT_NON, fdesc vnode\n");
    931 	return (0);
    932 }
    933 
    934 int
    935 fdesc_link(void *v)
    936 {
    937 	struct vop_link_v2_args /* {
    938 		struct vnode *a_dvp;
    939 		struct vnode *a_vp;
    940 		struct componentname *a_cnp;
    941 	} */ *ap = v;
    942 
    943 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    944 	return (EROFS);
    945 }
    946 
    947 int
    948 fdesc_symlink(void *v)
    949 {
    950 	struct vop_symlink_v3_args /* {
    951 		struct vnode *a_dvp;
    952 		struct vnode **a_vpp;
    953 		struct componentname *a_cnp;
    954 		struct vattr *a_vap;
    955 		char *a_target;
    956 	} */ *ap = v;
    957 
    958 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    959 	return (EROFS);
    960 }
    961