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