Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vnops.c revision 1.87.2.1
      1 /*	$NetBSD: fdesc_vnops.c,v 1.87.2.1 2005/10/20 05:04:28 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.87.2.1 2005/10/20 05:04:28 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 proc *);
    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 proc *p = cnp->cn_proc;
    279 	const char *pname = cnp->cn_nameptr;
    280 	int numfiles = p->p_fd->fd_nfiles;
    281 	unsigned fd = 0;
    282 	int error;
    283 	struct vnode *fvp;
    284 	const char *ln;
    285 
    286 	if (cnp->cn_namelen == 1 && *pname == '.') {
    287 		*vpp = dvp;
    288 		VREF(dvp);
    289 		return (0);
    290 	}
    291 
    292 	switch (VTOFDESC(dvp)->fd_type) {
    293 	default:
    294 	case Flink:
    295 	case Fdesc:
    296 	case Fctty:
    297 		error = ENOTDIR;
    298 		goto bad;
    299 
    300 	case Froot:
    301 		if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
    302 			error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
    303 			if (error)
    304 				goto bad;
    305 			*vpp = fvp;
    306 			fvp->v_type = VDIR;
    307 			goto good;
    308 		}
    309 
    310 		if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
    311 			struct vnode *ttyvp = cttyvp(p);
    312 			if (ttyvp == NULL) {
    313 				error = ENXIO;
    314 				goto bad;
    315 			}
    316 			error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
    317 			if (error)
    318 				goto bad;
    319 			*vpp = fvp;
    320 			fvp->v_type = VCHR;
    321 			goto good;
    322 		}
    323 
    324 		ln = 0;
    325 		switch (cnp->cn_namelen) {
    326 		case 5:
    327 			if (memcmp(pname, "stdin", 5) == 0) {
    328 				ln = "fd/0";
    329 				fd = FD_STDIN;
    330 			}
    331 			break;
    332 		case 6:
    333 			if (memcmp(pname, "stdout", 6) == 0) {
    334 				ln = "fd/1";
    335 				fd = FD_STDOUT;
    336 			} else
    337 			if (memcmp(pname, "stderr", 6) == 0) {
    338 				ln = "fd/2";
    339 				fd = FD_STDERR;
    340 			}
    341 			break;
    342 		}
    343 
    344 		if (ln) {
    345 			error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
    346 			if (error)
    347 				goto bad;
    348 			/* XXXUNCONST */
    349 			VTOFDESC(fvp)->fd_link = __UNCONST(ln);
    350 			*vpp = fvp;
    351 			fvp->v_type = VLNK;
    352 			goto good;
    353 		} else {
    354 			error = ENOENT;
    355 			goto bad;
    356 		}
    357 
    358 		/* FALL THROUGH */
    359 
    360 	case Fdevfd:
    361 		if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
    362 			VOP_UNLOCK(dvp, 0);
    363 			cnp->cn_flags |= PDIRUNLOCK;
    364 			error = fdesc_root(dvp->v_mount, vpp);
    365 			if (error)
    366 				goto bad;
    367 			/*
    368 			 * If we're at the last component and need the
    369 			 * parent locked, undo the unlock above.
    370 			 */
    371 			if (((~cnp->cn_flags & (ISLASTCN | LOCKPARENT)) == 0) &&
    372 				   ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    373 				cnp->cn_flags &= ~PDIRUNLOCK;
    374 			return (error);
    375 		}
    376 
    377 		fd = 0;
    378 		while (*pname >= '0' && *pname <= '9') {
    379 			fd = 10 * fd + *pname++ - '0';
    380 			if (fd >= numfiles)
    381 				break;
    382 		}
    383 
    384 		if (*pname != '\0') {
    385 			error = ENOENT;
    386 			goto bad;
    387 		}
    388 
    389 		if (fd >= numfiles || p->p_fd->fd_ofiles[fd] == NULL ||
    390 		    FILE_IS_USABLE(p->p_fd->fd_ofiles[fd]) == 0) {
    391 			error = EBADF;
    392 			goto bad;
    393 		}
    394 
    395 		error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
    396 		if (error)
    397 			goto bad;
    398 		VTOFDESC(fvp)->fd_fd = fd;
    399 		*vpp = fvp;
    400 		goto good;
    401 	}
    402 
    403 bad:;
    404 	*vpp = NULL;
    405 	return (error);
    406 
    407 good:;
    408 	/*
    409 	 * As "." was special cased above, we now unlock the parent if we're
    410 	 * suppoed to. We're only supposed to not unlock if this is the
    411 	 * last component, and the caller requested LOCKPARENT. So if either
    412 	 * condition is false, unlock.
    413 	 */
    414 	if (((~cnp->cn_flags) & (ISLASTCN | LOCKPARENT)) != 0) {
    415 		VOP_UNLOCK(dvp, 0);
    416 		cnp->cn_flags |= PDIRUNLOCK;
    417 	}
    418 	return (0);
    419 }
    420 
    421 int
    422 fdesc_open(v)
    423 	void *v;
    424 {
    425 	struct vop_open_args /* {
    426 		struct vnode *a_vp;
    427 		int  a_mode;
    428 		struct ucred *a_cred;
    429 		struct proc *a_p;
    430 	} */ *ap = v;
    431 	struct vnode *vp = ap->a_vp;
    432 
    433 	switch (VTOFDESC(vp)->fd_type) {
    434 	case Fdesc:
    435 		/*
    436 		 * XXX Kludge: set dupfd to contain the value of the
    437 		 * the file descriptor being sought for duplication. The error
    438 		 * return ensures that the vnode for this device will be
    439 		 * released by vn_open. Open will detect this special error and
    440 		 * take the actions in dupfdopen.  Other callers of vn_open or
    441 		 * VOP_OPEN will simply report the error.
    442 		 */
    443 		curlwp->l_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
    444 		return EDUPFD;
    445 
    446 	case Fctty:
    447 		return ((*ctty_cdevsw.d_open)(devctty, ap->a_mode, 0, ap->a_p));
    448 	case Froot:
    449 	case Fdevfd:
    450 	case Flink:
    451 		break;
    452 	}
    453 
    454 	return (0);
    455 }
    456 
    457 static int
    458 fdesc_attr(fd, vap, cred, p)
    459 	int fd;
    460 	struct vattr *vap;
    461 	struct ucred *cred;
    462 	struct proc *p;
    463 {
    464 	struct filedesc *fdp = p->p_fd;
    465 	struct file *fp;
    466 	struct stat stb;
    467 	int error;
    468 
    469 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    470 		return (EBADF);
    471 
    472 	switch (fp->f_type) {
    473 	case DTYPE_VNODE:
    474 		simple_unlock(&fp->f_slock);
    475 		error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
    476 		if (error == 0 && vap->va_type == VDIR) {
    477 			/*
    478 			 * directories can cause loops in the namespace,
    479 			 * so turn off the 'x' bits to avoid trouble.
    480 			 */
    481 			vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
    482 		}
    483 		break;
    484 
    485 	default:
    486 		FILE_USE(fp);
    487 		memset(&stb, 0, sizeof(stb));
    488 		error = (*fp->f_ops->fo_stat)(fp, &stb, p);
    489 		FILE_UNUSE(fp, p);
    490 		if (error)
    491 			break;
    492 
    493 		vattr_null(vap);
    494 		switch(fp->f_type) {
    495 		case DTYPE_SOCKET:
    496 			vap->va_type = VSOCK;
    497 			break;
    498 		case DTYPE_PIPE:
    499 			vap->va_type = VFIFO;
    500 			break;
    501 		default:
    502 			/* use VNON perhaps? */
    503 			vap->va_type = VBAD;
    504 			break;
    505 		}
    506 		vap->va_mode = stb.st_mode;
    507 		vap->va_nlink = stb.st_nlink;
    508 		vap->va_uid = stb.st_uid;
    509 		vap->va_gid = stb.st_gid;
    510 		vap->va_fsid = stb.st_dev;
    511 		vap->va_fileid = stb.st_ino;
    512 		vap->va_size = stb.st_size;
    513 		vap->va_blocksize = stb.st_blksize;
    514 		vap->va_atime = stb.st_atimespec;
    515 		vap->va_mtime = stb.st_mtimespec;
    516 		vap->va_ctime = stb.st_ctimespec;
    517 		vap->va_gen = stb.st_gen;
    518 		vap->va_flags = stb.st_flags;
    519 		vap->va_rdev = stb.st_rdev;
    520 		vap->va_bytes = stb.st_blocks * stb.st_blksize;
    521 		break;
    522 	}
    523 
    524 	return (error);
    525 }
    526 
    527 int
    528 fdesc_getattr(v)
    529 	void *v;
    530 {
    531 	struct vop_getattr_args /* {
    532 		struct vnode *a_vp;
    533 		struct vattr *a_vap;
    534 		struct ucred *a_cred;
    535 		struct proc *a_p;
    536 	} */ *ap = v;
    537 	struct vnode *vp = ap->a_vp;
    538 	struct vattr *vap = ap->a_vap;
    539 	unsigned fd;
    540 	int error = 0;
    541 
    542 	switch (VTOFDESC(vp)->fd_type) {
    543 	case Froot:
    544 	case Fdevfd:
    545 	case Flink:
    546 	case Fctty:
    547 		VATTR_NULL(vap);
    548 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
    549 
    550 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
    551 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
    552 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
    553 
    554 		switch (VTOFDESC(vp)->fd_type) {
    555 		case Flink:
    556 			vap->va_mode = R_ALL|X_ALL;
    557 			vap->va_type = VLNK;
    558 			vap->va_rdev = 0;
    559 			vap->va_nlink = 1;
    560 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
    561 			break;
    562 
    563 		case Fctty:
    564 			vap->va_mode = R_ALL|W_ALL;
    565 			vap->va_type = VCHR;
    566 			vap->va_rdev = devctty;
    567 			vap->va_nlink = 1;
    568 			vap->va_size = 0;
    569 			break;
    570 
    571 		default:
    572 			vap->va_mode = R_ALL|X_ALL;
    573 			vap->va_type = VDIR;
    574 			vap->va_rdev = 0;
    575 			vap->va_nlink = 2;
    576 			vap->va_size = DEV_BSIZE;
    577 			break;
    578 		}
    579 		vap->va_uid = 0;
    580 		vap->va_gid = 0;
    581 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    582 		vap->va_blocksize = DEV_BSIZE;
    583 		vap->va_atime.tv_sec = boottime.tv_sec;
    584 		vap->va_atime.tv_nsec = 0;
    585 		vap->va_mtime = vap->va_atime;
    586 		vap->va_ctime = vap->va_mtime;
    587 		vap->va_gen = 0;
    588 		vap->va_flags = 0;
    589 		vap->va_bytes = 0;
    590 		break;
    591 
    592 	case Fdesc:
    593 		fd = VTOFDESC(vp)->fd_fd;
    594 		error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
    595 		break;
    596 
    597 	default:
    598 		panic("fdesc_getattr");
    599 		break;
    600 	}
    601 
    602 	if (error == 0)
    603 		vp->v_type = vap->va_type;
    604 
    605 	return (error);
    606 }
    607 
    608 int
    609 fdesc_setattr(v)
    610 	void *v;
    611 {
    612 	struct vop_setattr_args /* {
    613 		struct vnode *a_vp;
    614 		struct vattr *a_vap;
    615 		struct ucred *a_cred;
    616 		struct proc *a_p;
    617 	} */ *ap = v;
    618 	struct filedesc *fdp = ap->a_p->p_fd;
    619 	struct file *fp;
    620 	unsigned fd;
    621 
    622 	/*
    623 	 * Can't mess with the root vnode
    624 	 */
    625 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    626 	case Fdesc:
    627 		break;
    628 
    629 	case Fctty:
    630 		return (0);
    631 
    632 	default:
    633 		return (EACCES);
    634 	}
    635 
    636 	fd = VTOFDESC(ap->a_vp)->fd_fd;
    637 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    638 		return (EBADF);
    639 
    640 	/*
    641 	 * XXX: Can't reasonably set the attr's on any types currently.
    642 	 *      On vnode's this will cause truncation and socket/pipes make
    643 	 *      no sense.
    644 	 */
    645 	simple_unlock(&fp->f_slock);
    646 	return (0);
    647 }
    648 
    649 
    650 struct fdesc_target {
    651 	ino_t ft_fileno;
    652 	u_char ft_type;
    653 	u_char ft_namlen;
    654 	const char *ft_name;
    655 } fdesc_targets[] = {
    656 #define N(s) sizeof(s)-1, s
    657 	{ FD_DEVFD,  DT_DIR,     N("fd")     },
    658 	{ FD_STDIN,  DT_LNK,     N("stdin")  },
    659 	{ FD_STDOUT, DT_LNK,     N("stdout") },
    660 	{ FD_STDERR, DT_LNK,     N("stderr") },
    661 	{ FD_CTTY,   DT_UNKNOWN, N("tty")    },
    662 #undef N
    663 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
    664 };
    665 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
    666 
    667 int
    668 fdesc_readdir(v)
    669 	void *v;
    670 {
    671 	struct vop_readdir_args /* {
    672 		struct vnode *a_vp;
    673 		struct uio *a_uio;
    674 		struct ucred *a_cred;
    675 		int *a_eofflag;
    676 		off_t **a_cookies;
    677 		int *a_ncookies;
    678 	} */ *ap = v;
    679 	struct uio *uio = ap->a_uio;
    680 	struct dirent d;
    681 	struct filedesc *fdp;
    682 	off_t i;
    683 	int j;
    684 	int error;
    685 	off_t *cookies = NULL;
    686 	int ncookies;
    687 
    688 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    689 	case Fctty:
    690 		return 0;
    691 
    692 	case Fdesc:
    693 		return ENOTDIR;
    694 
    695 	default:
    696 		break;
    697 	}
    698 
    699 	fdp = uio->uio_procp ? uio->uio_procp->p_fd : NULL;
    700 
    701 	if (uio->uio_resid < UIO_MX)
    702 		return EINVAL;
    703 	if (uio->uio_offset < 0)
    704 		return EINVAL;
    705 
    706 	error = 0;
    707 	i = uio->uio_offset;
    708 	(void)memset(&d, 0, UIO_MX);
    709 	d.d_reclen = UIO_MX;
    710 	if (ap->a_ncookies)
    711 		ncookies = uio->uio_resid / UIO_MX;
    712 	else
    713 		ncookies = 0;
    714 
    715 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
    716 		struct fdesc_target *ft;
    717 
    718 		if (i >= nfdesc_targets)
    719 			return 0;
    720 
    721 		if (ap->a_ncookies) {
    722 			ncookies = min(ncookies, (nfdesc_targets - i));
    723 			cookies = malloc(ncookies * sizeof(off_t),
    724 			    M_TEMP, M_WAITOK);
    725 			*ap->a_cookies = cookies;
    726 			*ap->a_ncookies = ncookies;
    727 		}
    728 
    729 		for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
    730 		    i < nfdesc_targets; ft++, i++) {
    731 			switch (ft->ft_fileno) {
    732 			case FD_CTTY:
    733 				if (uio->uio_procp == NULL ||
    734 				    cttyvp(uio->uio_procp) == NULL)
    735 					continue;
    736 				break;
    737 
    738 			case FD_STDIN:
    739 			case FD_STDOUT:
    740 			case FD_STDERR:
    741 				if (fdp == NULL)
    742 					continue;
    743 				if ((ft->ft_fileno - FD_STDIN) >=
    744 				    fdp->fd_nfiles)
    745 					continue;
    746 				if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN]
    747 				    == NULL
    748 				    || FILE_IS_USABLE(
    749 				    fdp->fd_ofiles[ft->ft_fileno - FD_STDIN])
    750 				    == 0)
    751 					continue;
    752 				break;
    753 			}
    754 
    755 			d.d_fileno = ft->ft_fileno;
    756 			d.d_namlen = ft->ft_namlen;
    757 			(void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
    758 			d.d_type = ft->ft_type;
    759 
    760 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    761 				break;
    762 			if (cookies)
    763 				*cookies++ = i + 1;
    764 		}
    765 	} else {
    766 		int nfdp = fdp ? fdp->fd_nfiles : 0;
    767 		if (ap->a_ncookies) {
    768 			ncookies = min(ncookies, nfdp + 2);
    769 			cookies = malloc(ncookies * sizeof(off_t),
    770 			    M_TEMP, M_WAITOK);
    771 			*ap->a_cookies = cookies;
    772 			*ap->a_ncookies = ncookies;
    773 		}
    774 		for (; i - 2 < nfdp && uio->uio_resid >= UIO_MX; i++) {
    775 			switch (i) {
    776 			case 0:
    777 			case 1:
    778 				d.d_fileno = FD_ROOT;		/* XXX */
    779 				d.d_namlen = i + 1;
    780 				(void)memcpy(d.d_name, "..", d.d_namlen);
    781 				d.d_name[i + 1] = '\0';
    782 				d.d_type = DT_DIR;
    783 				break;
    784 
    785 			default:
    786 				KASSERT(fdp != NULL);
    787 				j = (int)i - 2;
    788 				if (fdp->fd_ofiles[j] == NULL ||
    789 				    FILE_IS_USABLE(fdp->fd_ofiles[j]) == 0)
    790 					continue;
    791 				d.d_fileno = j + FD_STDIN;
    792 				d.d_namlen = sprintf(d.d_name, "%d", j);
    793 				d.d_type = DT_UNKNOWN;
    794 				break;
    795 			}
    796 
    797 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    798 				break;
    799 			if (cookies)
    800 				*cookies++ = i + 1;
    801 		}
    802 	}
    803 
    804 	if (ap->a_ncookies && error) {
    805 		free(*ap->a_cookies, M_TEMP);
    806 		*ap->a_ncookies = 0;
    807 		*ap->a_cookies = NULL;
    808 	}
    809 
    810 	uio->uio_offset = i;
    811 	return error;
    812 }
    813 
    814 int
    815 fdesc_readlink(v)
    816 	void *v;
    817 {
    818 	struct vop_readlink_args /* {
    819 		struct vnode *a_vp;
    820 		struct uio *a_uio;
    821 		struct ucred *a_cred;
    822 	} */ *ap = v;
    823 	struct vnode *vp = ap->a_vp;
    824 	int error;
    825 
    826 	if (vp->v_type != VLNK)
    827 		return (EPERM);
    828 
    829 	if (VTOFDESC(vp)->fd_type == Flink) {
    830 		char *ln = VTOFDESC(vp)->fd_link;
    831 		error = uiomove(ln, strlen(ln), ap->a_uio);
    832 	} else {
    833 		error = EOPNOTSUPP;
    834 	}
    835 
    836 	return (error);
    837 }
    838 
    839 int
    840 fdesc_read(v)
    841 	void *v;
    842 {
    843 	struct vop_read_args /* {
    844 		struct vnode *a_vp;
    845 		struct uio *a_uio;
    846 		int  a_ioflag;
    847 		struct ucred *a_cred;
    848 	} */ *ap = v;
    849 	int error = EOPNOTSUPP;
    850 	struct vnode *vp = ap->a_vp;
    851 
    852 	switch (VTOFDESC(vp)->fd_type) {
    853 	case Fctty:
    854 		VOP_UNLOCK(vp, 0);
    855 		error = (*ctty_cdevsw.d_read)(devctty, ap->a_uio, ap->a_ioflag);
    856 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    857 		break;
    858 
    859 	default:
    860 		error = EOPNOTSUPP;
    861 		break;
    862 	}
    863 
    864 	return (error);
    865 }
    866 
    867 int
    868 fdesc_write(v)
    869 	void *v;
    870 {
    871 	struct vop_write_args /* {
    872 		struct vnode *a_vp;
    873 		struct uio *a_uio;
    874 		int  a_ioflag;
    875 		struct ucred *a_cred;
    876 	} */ *ap = v;
    877 	int error = EOPNOTSUPP;
    878 	struct vnode *vp = ap->a_vp;
    879 
    880 	switch (VTOFDESC(vp)->fd_type) {
    881 	case Fctty:
    882 		VOP_UNLOCK(vp, 0);
    883 		error = (*ctty_cdevsw.d_write)(devctty, ap->a_uio,
    884 					       ap->a_ioflag);
    885 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    886 		break;
    887 
    888 	default:
    889 		error = EOPNOTSUPP;
    890 		break;
    891 	}
    892 
    893 	return (error);
    894 }
    895 
    896 int
    897 fdesc_ioctl(v)
    898 	void *v;
    899 {
    900 	struct vop_ioctl_args /* {
    901 		struct vnode *a_vp;
    902 		u_long a_command;
    903 		void *a_data;
    904 		int  a_fflag;
    905 		struct ucred *a_cred;
    906 		struct proc *a_p;
    907 	} */ *ap = v;
    908 	int error = EOPNOTSUPP;
    909 
    910 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    911 	case Fctty:
    912 		error = (*ctty_cdevsw.d_ioctl)(devctty, ap->a_command,
    913 					       ap->a_data, ap->a_fflag,
    914 					       ap->a_p);
    915 		break;
    916 
    917 	default:
    918 		error = EOPNOTSUPP;
    919 		break;
    920 	}
    921 
    922 	return (error);
    923 }
    924 
    925 int
    926 fdesc_poll(v)
    927 	void *v;
    928 {
    929 	struct vop_poll_args /* {
    930 		struct vnode *a_vp;
    931 		int a_events;
    932 		struct proc *a_p;
    933 	} */ *ap = v;
    934 	int revents;
    935 
    936 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    937 	case Fctty:
    938 		revents = (*ctty_cdevsw.d_poll)(devctty, ap->a_events, ap->a_p);
    939 		break;
    940 
    941 	default:
    942 		revents = genfs_poll(v);
    943 		break;
    944 	}
    945 
    946 	return (revents);
    947 }
    948 
    949 int
    950 fdesc_kqfilter(v)
    951 	void *v;
    952 {
    953 	struct vop_kqfilter_args /* {
    954 		struct vnode *a_vp;
    955 		struct knote *a_kn;
    956 	} */ *ap = v;
    957 	int error;
    958 	struct proc *p;
    959 	struct file *fp;
    960 
    961 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    962 	case Fctty:
    963 		error = (*ctty_cdevsw.d_kqfilter)(devctty, ap->a_kn);
    964 		break;
    965 
    966 	case Fdesc:
    967 		/* just invoke kqfilter for the underlying descriptor */
    968 		p = curproc;	/* XXX hopefully ok to use curproc here */
    969 		if ((fp = fd_getfile(p->p_fd, VTOFDESC(ap->a_vp)->fd_fd)) == NULL)
    970 			return (1);
    971 
    972 		FILE_USE(fp);
    973 		error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
    974 		FILE_UNUSE(fp, p);
    975 		break;
    976 
    977 	default:
    978 		return (genfs_kqfilter(v));
    979 	}
    980 
    981 	return (error);
    982 }
    983 
    984 int
    985 fdesc_inactive(v)
    986 	void *v;
    987 {
    988 	struct vop_inactive_args /* {
    989 		struct vnode *a_vp;
    990 		struct proc *a_p;
    991 	} */ *ap = v;
    992 	struct vnode *vp = ap->a_vp;
    993 
    994 	/*
    995 	 * Clear out the v_type field to avoid
    996 	 * nasty things happening in vgone().
    997 	 */
    998 	VOP_UNLOCK(vp, 0);
    999 	vp->v_type = VNON;
   1000 	return (0);
   1001 }
   1002 
   1003 int
   1004 fdesc_reclaim(v)
   1005 	void *v;
   1006 {
   1007 	struct vop_reclaim_args /* {
   1008 		struct vnode *a_vp;
   1009 	} */ *ap = v;
   1010 	struct vnode *vp = ap->a_vp;
   1011 	struct fdescnode *fd = VTOFDESC(vp);
   1012 
   1013 	LIST_REMOVE(fd, fd_hash);
   1014 	FREE(vp->v_data, M_TEMP);
   1015 	vp->v_data = 0;
   1016 
   1017 	return (0);
   1018 }
   1019 
   1020 /*
   1021  * Return POSIX pathconf information applicable to special devices.
   1022  */
   1023 int
   1024 fdesc_pathconf(v)
   1025 	void *v;
   1026 {
   1027 	struct vop_pathconf_args /* {
   1028 		struct vnode *a_vp;
   1029 		int a_name;
   1030 		register_t *a_retval;
   1031 	} */ *ap = v;
   1032 
   1033 	switch (ap->a_name) {
   1034 	case _PC_LINK_MAX:
   1035 		*ap->a_retval = LINK_MAX;
   1036 		return (0);
   1037 	case _PC_MAX_CANON:
   1038 		*ap->a_retval = MAX_CANON;
   1039 		return (0);
   1040 	case _PC_MAX_INPUT:
   1041 		*ap->a_retval = MAX_INPUT;
   1042 		return (0);
   1043 	case _PC_PIPE_BUF:
   1044 		*ap->a_retval = PIPE_BUF;
   1045 		return (0);
   1046 	case _PC_CHOWN_RESTRICTED:
   1047 		*ap->a_retval = 1;
   1048 		return (0);
   1049 	case _PC_VDISABLE:
   1050 		*ap->a_retval = _POSIX_VDISABLE;
   1051 		return (0);
   1052 	case _PC_SYNC_IO:
   1053 		*ap->a_retval = 1;
   1054 		return (0);
   1055 	default:
   1056 		return (EINVAL);
   1057 	}
   1058 	/* NOTREACHED */
   1059 }
   1060 
   1061 /*
   1062  * Print out the contents of a /dev/fd vnode.
   1063  */
   1064 /* ARGSUSED */
   1065 int
   1066 fdesc_print(v)
   1067 	void *v;
   1068 {
   1069 	printf("tag VT_NON, fdesc vnode\n");
   1070 	return (0);
   1071 }
   1072 
   1073 int
   1074 fdesc_link(v)
   1075 	void *v;
   1076 {
   1077 	struct vop_link_args /* {
   1078 		struct vnode *a_dvp;
   1079 		struct vnode *a_vp;
   1080 		struct componentname *a_cnp;
   1081 	} */ *ap = v;
   1082 
   1083 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1084 	vput(ap->a_dvp);
   1085 	return (EROFS);
   1086 }
   1087 
   1088 int
   1089 fdesc_symlink(v)
   1090 	void *v;
   1091 {
   1092 	struct vop_symlink_args /* {
   1093 		struct vnode *a_dvp;
   1094 		struct vnode **a_vpp;
   1095 		struct componentname *a_cnp;
   1096 		struct vattr *a_vap;
   1097 		char *a_target;
   1098 	} */ *ap = v;
   1099 
   1100 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1101 	vput(ap->a_dvp);
   1102 	return (EROFS);
   1103 }
   1104