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