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