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