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