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