Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vnops.c revision 1.84
      1 /*	$NetBSD: fdesc_vnops.c,v 1.84 2005/05/29 21:55:33 christos 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.84 2005/05/29 21:55:33 christos 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	__P((void *));
     88 #define	fdesc_create	genfs_eopnotsupp
     89 #define	fdesc_mknod	genfs_eopnotsupp
     90 int	fdesc_open	__P((void *));
     91 #define	fdesc_close	genfs_nullop
     92 #define	fdesc_access	genfs_nullop
     93 int	fdesc_getattr	__P((void *));
     94 int	fdesc_setattr	__P((void *));
     95 int	fdesc_read	__P((void *));
     96 int	fdesc_write	__P((void *));
     97 int	fdesc_ioctl	__P((void *));
     98 int	fdesc_poll	__P((void *));
     99 int	fdesc_kqfilter	__P((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	__P((void *));
    106 #define	fdesc_rename	genfs_eopnotsupp
    107 #define	fdesc_mkdir	genfs_eopnotsupp
    108 #define	fdesc_rmdir	genfs_eopnotsupp
    109 int	fdesc_symlink	__P((void *));
    110 int	fdesc_readdir	__P((void *));
    111 int	fdesc_readlink	__P((void *));
    112 #define	fdesc_abortop	genfs_abortop
    113 int	fdesc_inactive	__P((void *));
    114 int	fdesc_reclaim	__P((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	__P((void *));
    120 int	fdesc_pathconf	__P((void *));
    121 #define	fdesc_islocked	genfs_islocked
    122 #define	fdesc_advlock	genfs_einval
    123 #define	fdesc_blkatoff	genfs_eopnotsupp
    124 #define	fdesc_valloc	genfs_eopnotsupp
    125 #define	fdesc_vfree	genfs_nullop
    126 #define	fdesc_truncate	genfs_eopnotsupp
    127 #define	fdesc_update	genfs_nullop
    128 #define	fdesc_bwrite	genfs_eopnotsupp
    129 #define fdesc_revoke	genfs_revoke
    130 #define fdesc_putpages	genfs_null_putpages
    131 
    132 static int fdesc_attr __P((int, struct vattr *, struct ucred *, struct proc *));
    133 
    134 int (**fdesc_vnodeop_p) __P((void *));
    135 const 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_kqfilter_desc, fdesc_kqfilter },		/* kqfilter */
    151 	{ &vop_revoke_desc, fdesc_revoke },		/* revoke */
    152 	{ &vop_mmap_desc, fdesc_mmap },			/* mmap */
    153 	{ &vop_fsync_desc, fdesc_fsync },		/* fsync */
    154 	{ &vop_seek_desc, fdesc_seek },			/* seek */
    155 	{ &vop_remove_desc, fdesc_remove },		/* remove */
    156 	{ &vop_link_desc, fdesc_link },			/* link */
    157 	{ &vop_rename_desc, fdesc_rename },		/* rename */
    158 	{ &vop_mkdir_desc, fdesc_mkdir },		/* mkdir */
    159 	{ &vop_rmdir_desc, fdesc_rmdir },		/* rmdir */
    160 	{ &vop_symlink_desc, fdesc_symlink },		/* symlink */
    161 	{ &vop_readdir_desc, fdesc_readdir },		/* readdir */
    162 	{ &vop_readlink_desc, fdesc_readlink },		/* readlink */
    163 	{ &vop_abortop_desc, fdesc_abortop },		/* abortop */
    164 	{ &vop_inactive_desc, fdesc_inactive },		/* inactive */
    165 	{ &vop_reclaim_desc, fdesc_reclaim },		/* reclaim */
    166 	{ &vop_lock_desc, fdesc_lock },			/* lock */
    167 	{ &vop_unlock_desc, fdesc_unlock },		/* unlock */
    168 	{ &vop_bmap_desc, fdesc_bmap },			/* bmap */
    169 	{ &vop_strategy_desc, fdesc_strategy },		/* strategy */
    170 	{ &vop_print_desc, fdesc_print },		/* print */
    171 	{ &vop_islocked_desc, fdesc_islocked },		/* islocked */
    172 	{ &vop_pathconf_desc, fdesc_pathconf },		/* pathconf */
    173 	{ &vop_advlock_desc, fdesc_advlock },		/* advlock */
    174 	{ &vop_blkatoff_desc, fdesc_blkatoff },		/* blkatoff */
    175 	{ &vop_valloc_desc, fdesc_valloc },		/* valloc */
    176 	{ &vop_vfree_desc, fdesc_vfree },		/* vfree */
    177 	{ &vop_truncate_desc, fdesc_truncate },		/* truncate */
    178 	{ &vop_update_desc, fdesc_update },		/* update */
    179 	{ &vop_bwrite_desc, fdesc_bwrite },		/* bwrite */
    180 	{ &vop_putpages_desc, fdesc_putpages },		/* putpages */
    181 	{ NULL, NULL }
    182 };
    183 
    184 const struct vnodeopv_desc fdesc_vnodeop_opv_desc =
    185 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
    186 
    187 extern const struct cdevsw ctty_cdevsw;
    188 
    189 /*
    190  * Initialise cache headers
    191  */
    192 void
    193 fdesc_init()
    194 {
    195 	int cttymajor;
    196 
    197 	/* locate the major number */
    198 	cttymajor = cdevsw_lookup_major(&ctty_cdevsw);
    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(&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 numfiles = p->p_fd->fd_nfiles;
    291 	unsigned fd = 0;
    292 	int error;
    293 	struct vnode *fvp;
    294 	const 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 			/* XXXUNCONST */
    359 			VTOFDESC(fvp)->fd_link = __UNCONST(ln);
    360 			*vpp = fvp;
    361 			fvp->v_type = VLNK;
    362 			goto good;
    363 		} else {
    364 			error = ENOENT;
    365 			goto bad;
    366 		}
    367 
    368 		/* FALL THROUGH */
    369 
    370 	case Fdevfd:
    371 		if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
    372 			VOP_UNLOCK(dvp, 0);
    373 			cnp->cn_flags |= PDIRUNLOCK;
    374 			error = fdesc_root(dvp->v_mount, vpp);
    375 			if (error)
    376 				goto bad;
    377 			/*
    378 			 * If we're at the last component and need the
    379 			 * parent locked, undo the unlock above.
    380 			 */
    381 			if (((~cnp->cn_flags & (ISLASTCN | LOCKPARENT)) == 0) &&
    382 				   ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    383 				cnp->cn_flags &= ~PDIRUNLOCK;
    384 			return (error);
    385 		}
    386 
    387 		fd = 0;
    388 		while (*pname >= '0' && *pname <= '9') {
    389 			fd = 10 * fd + *pname++ - '0';
    390 			if (fd >= numfiles)
    391 				break;
    392 		}
    393 
    394 		if (*pname != '\0') {
    395 			error = ENOENT;
    396 			goto bad;
    397 		}
    398 
    399 		if (fd >= numfiles || p->p_fd->fd_ofiles[fd] == NULL ||
    400 		    FILE_IS_USABLE(p->p_fd->fd_ofiles[fd]) == 0) {
    401 			error = EBADF;
    402 			goto bad;
    403 		}
    404 
    405 		error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
    406 		if (error)
    407 			goto bad;
    408 		VTOFDESC(fvp)->fd_fd = fd;
    409 		*vpp = fvp;
    410 		goto good;
    411 	}
    412 
    413 bad:;
    414 	*vpp = NULL;
    415 	return (error);
    416 
    417 good:;
    418 	/*
    419 	 * As "." was special cased above, we now unlock the parent if we're
    420 	 * suppoed to. We're only supposed to not unlock if this is the
    421 	 * last component, and the caller requested LOCKPARENT. So if either
    422 	 * condition is false, unlock.
    423 	 */
    424 	if (((~cnp->cn_flags) & (ISLASTCN | LOCKPARENT)) != 0) {
    425 		VOP_UNLOCK(dvp, 0);
    426 		cnp->cn_flags |= PDIRUNLOCK;
    427 	}
    428 	return (0);
    429 }
    430 
    431 int
    432 fdesc_open(v)
    433 	void *v;
    434 {
    435 	struct vop_open_args /* {
    436 		struct vnode *a_vp;
    437 		int  a_mode;
    438 		struct ucred *a_cred;
    439 		struct proc *a_p;
    440 	} */ *ap = v;
    441 	struct vnode *vp = ap->a_vp;
    442 
    443 	switch (VTOFDESC(vp)->fd_type) {
    444 	case Fdesc:
    445 		/*
    446 		 * XXX Kludge: set dupfd to contain the value of the
    447 		 * the file descriptor being sought for duplication. The error
    448 		 * return ensures that the vnode for this device will be
    449 		 * released by vn_open. Open will detect this special error and
    450 		 * take the actions in dupfdopen.  Other callers of vn_open or
    451 		 * VOP_OPEN will simply report the error.
    452 		 */
    453 		curlwp->l_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
    454 		return EDUPFD;
    455 
    456 	case Fctty:
    457 		return ((*ctty_cdevsw.d_open)(devctty, ap->a_mode, 0, ap->a_p));
    458 	case Froot:
    459 	case Fdevfd:
    460 	case Flink:
    461 		break;
    462 	}
    463 
    464 	return (0);
    465 }
    466 
    467 static int
    468 fdesc_attr(fd, vap, cred, p)
    469 	int fd;
    470 	struct vattr *vap;
    471 	struct ucred *cred;
    472 	struct proc *p;
    473 {
    474 	struct filedesc *fdp = p->p_fd;
    475 	struct file *fp;
    476 	struct stat stb;
    477 	int error;
    478 
    479 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    480 		return (EBADF);
    481 
    482 	switch (fp->f_type) {
    483 	case DTYPE_VNODE:
    484 		simple_unlock(&fp->f_slock);
    485 		error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
    486 		if (error == 0 && vap->va_type == VDIR) {
    487 			/*
    488 			 * directories can cause loops in the namespace,
    489 			 * so turn off the 'x' bits to avoid trouble.
    490 			 */
    491 			vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
    492 		}
    493 		break;
    494 
    495 	default:
    496 		FILE_USE(fp);
    497 		memset(&stb, 0, sizeof(stb));
    498 		error = (*fp->f_ops->fo_stat)(fp, &stb, p);
    499 		FILE_UNUSE(fp, p);
    500 		if (error)
    501 			break;
    502 
    503 		vattr_null(vap);
    504 		switch(fp->f_type) {
    505 		case DTYPE_SOCKET:
    506 			vap->va_type = VSOCK;
    507 			break;
    508 		case DTYPE_PIPE:
    509 			vap->va_type = VFIFO;
    510 			break;
    511 		default:
    512 			/* use VNON perhaps? */
    513 			vap->va_type = VBAD;
    514 			break;
    515 		}
    516 		vap->va_mode = stb.st_mode;
    517 		vap->va_nlink = stb.st_nlink;
    518 		vap->va_uid = stb.st_uid;
    519 		vap->va_gid = stb.st_gid;
    520 		vap->va_fsid = stb.st_dev;
    521 		vap->va_fileid = stb.st_ino;
    522 		vap->va_size = stb.st_size;
    523 		vap->va_blocksize = stb.st_blksize;
    524 		vap->va_atime = stb.st_atimespec;
    525 		vap->va_mtime = stb.st_mtimespec;
    526 		vap->va_ctime = stb.st_ctimespec;
    527 		vap->va_gen = stb.st_gen;
    528 		vap->va_flags = stb.st_flags;
    529 		vap->va_rdev = stb.st_rdev;
    530 		vap->va_bytes = stb.st_blocks * stb.st_blksize;
    531 		break;
    532 	}
    533 
    534 	return (error);
    535 }
    536 
    537 int
    538 fdesc_getattr(v)
    539 	void *v;
    540 {
    541 	struct vop_getattr_args /* {
    542 		struct vnode *a_vp;
    543 		struct vattr *a_vap;
    544 		struct ucred *a_cred;
    545 		struct proc *a_p;
    546 	} */ *ap = v;
    547 	struct vnode *vp = ap->a_vp;
    548 	struct vattr *vap = ap->a_vap;
    549 	unsigned fd;
    550 	int error = 0;
    551 
    552 	switch (VTOFDESC(vp)->fd_type) {
    553 	case Froot:
    554 	case Fdevfd:
    555 	case Flink:
    556 	case Fctty:
    557 		VATTR_NULL(vap);
    558 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
    559 
    560 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
    561 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
    562 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
    563 
    564 		switch (VTOFDESC(vp)->fd_type) {
    565 		case Flink:
    566 			vap->va_mode = R_ALL|X_ALL;
    567 			vap->va_type = VLNK;
    568 			vap->va_rdev = 0;
    569 			vap->va_nlink = 1;
    570 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
    571 			break;
    572 
    573 		case Fctty:
    574 			vap->va_mode = R_ALL|W_ALL;
    575 			vap->va_type = VCHR;
    576 			vap->va_rdev = devctty;
    577 			vap->va_nlink = 1;
    578 			vap->va_size = 0;
    579 			break;
    580 
    581 		default:
    582 			vap->va_mode = R_ALL|X_ALL;
    583 			vap->va_type = VDIR;
    584 			vap->va_rdev = 0;
    585 			vap->va_nlink = 2;
    586 			vap->va_size = DEV_BSIZE;
    587 			break;
    588 		}
    589 		vap->va_uid = 0;
    590 		vap->va_gid = 0;
    591 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    592 		vap->va_blocksize = DEV_BSIZE;
    593 		vap->va_atime.tv_sec = boottime.tv_sec;
    594 		vap->va_atime.tv_nsec = 0;
    595 		vap->va_mtime = vap->va_atime;
    596 		vap->va_ctime = vap->va_mtime;
    597 		vap->va_gen = 0;
    598 		vap->va_flags = 0;
    599 		vap->va_bytes = 0;
    600 		break;
    601 
    602 	case Fdesc:
    603 		fd = VTOFDESC(vp)->fd_fd;
    604 		error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
    605 		break;
    606 
    607 	default:
    608 		panic("fdesc_getattr");
    609 		break;
    610 	}
    611 
    612 	if (error == 0)
    613 		vp->v_type = vap->va_type;
    614 
    615 	return (error);
    616 }
    617 
    618 int
    619 fdesc_setattr(v)
    620 	void *v;
    621 {
    622 	struct vop_setattr_args /* {
    623 		struct vnode *a_vp;
    624 		struct vattr *a_vap;
    625 		struct ucred *a_cred;
    626 		struct proc *a_p;
    627 	} */ *ap = v;
    628 	struct filedesc *fdp = ap->a_p->p_fd;
    629 	struct file *fp;
    630 	unsigned fd;
    631 
    632 	/*
    633 	 * Can't mess with the root vnode
    634 	 */
    635 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    636 	case Fdesc:
    637 		break;
    638 
    639 	case Fctty:
    640 		return (0);
    641 
    642 	default:
    643 		return (EACCES);
    644 	}
    645 
    646 	fd = VTOFDESC(ap->a_vp)->fd_fd;
    647 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    648 		return (EBADF);
    649 
    650 	/*
    651 	 * XXX: Can't reasonably set the attr's on any types currently.
    652 	 *      On vnode's this will cause truncation and socket/pipes make
    653 	 *      no sense.
    654 	 */
    655 	simple_unlock(&fp->f_slock);
    656 	return (0);
    657 }
    658 
    659 #define UIO_MX 32
    660 
    661 struct fdesc_target {
    662 	ino_t ft_fileno;
    663 	u_char ft_type;
    664 	u_char ft_namlen;
    665 	const char *ft_name;
    666 } fdesc_targets[] = {
    667 /* NOTE: The name must be less than UIO_MX-16 chars in length */
    668 #define N(s) sizeof(s)-1, s
    669 	{ FD_DEVFD,  DT_DIR,     N("fd")     },
    670 	{ FD_STDIN,  DT_LNK,     N("stdin")  },
    671 	{ FD_STDOUT, DT_LNK,     N("stdout") },
    672 	{ FD_STDERR, DT_LNK,     N("stderr") },
    673 	{ FD_CTTY,   DT_UNKNOWN, N("tty")    },
    674 #undef N
    675 };
    676 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
    677 
    678 int
    679 fdesc_readdir(v)
    680 	void *v;
    681 {
    682 	struct vop_readdir_args /* {
    683 		struct vnode *a_vp;
    684 		struct uio *a_uio;
    685 		struct ucred *a_cred;
    686 		int *a_eofflag;
    687 		off_t **a_cookies;
    688 		int *a_ncookies;
    689 	} */ *ap = v;
    690 	struct uio *uio = ap->a_uio;
    691 	struct dirent d;
    692 	struct filedesc *fdp;
    693 	off_t i;
    694 	int error;
    695 	off_t *cookies = NULL;
    696 	int ncookies = 0;
    697 
    698 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    699 	case Fctty:
    700 		return (0);
    701 
    702 	case Fdesc:
    703 		return (ENOTDIR);
    704 
    705 	default:
    706 		break;
    707 	}
    708 
    709 	fdp = uio->uio_procp->p_fd;
    710 
    711 	if (uio->uio_resid < UIO_MX)
    712 		return (EINVAL);
    713 	if (uio->uio_offset < 0)
    714 		return (EINVAL);
    715 
    716 	error = 0;
    717 	i = uio->uio_offset;
    718 	memset(&d, 0, UIO_MX);
    719 	d.d_reclen = UIO_MX;
    720 	if (ap->a_ncookies)
    721 		ncookies = (uio->uio_resid / UIO_MX);
    722 
    723 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
    724 		struct fdesc_target *ft;
    725 
    726 		if (i >= nfdesc_targets)
    727 			return 0;
    728 
    729 		if (ap->a_ncookies) {
    730 			ncookies = min(ncookies, (nfdesc_targets - i));
    731 			cookies = malloc(ncookies * sizeof(off_t),
    732 			    M_TEMP, M_WAITOK);
    733 			*ap->a_cookies = cookies;
    734 			*ap->a_ncookies = ncookies;
    735 		}
    736 
    737 		for (ft = &fdesc_targets[i];
    738 		     uio->uio_resid >= UIO_MX && i < nfdesc_targets; ft++, i++) {
    739 			switch (ft->ft_fileno) {
    740 			case FD_CTTY:
    741 				if (cttyvp(uio->uio_procp) == NULL)
    742 					continue;
    743 				break;
    744 
    745 			case FD_STDIN:
    746 			case FD_STDOUT:
    747 			case FD_STDERR:
    748 				if ((ft->ft_fileno - FD_STDIN) >= fdp->fd_nfiles)
    749 					continue;
    750 				if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN] == NULL
    751 				    || FILE_IS_USABLE(fdp->fd_ofiles[ft->ft_fileno - FD_STDIN]) == 0)
    752 					continue;
    753 				break;
    754 			}
    755 
    756 			d.d_fileno = ft->ft_fileno;
    757 			d.d_namlen = ft->ft_namlen;
    758 			memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
    759 			d.d_type = ft->ft_type;
    760 
    761 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    762 				break;
    763 			if (cookies)
    764 				*cookies++ = i + 1;
    765 		}
    766 	} else {
    767 		if (ap->a_ncookies) {
    768 			ncookies = min(ncookies, (fdp->fd_nfiles + 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 < fdp->fd_nfiles && uio->uio_resid >= UIO_MX;
    775 		     i++) {
    776 			switch (i) {
    777 			case 0:
    778 			case 1:
    779 				d.d_fileno = FD_ROOT;		/* XXX */
    780 				d.d_namlen = i + 1;
    781 				memcpy(d.d_name, "..", d.d_namlen);
    782 				d.d_name[i + 1] = '\0';
    783 				d.d_type = DT_DIR;
    784 				break;
    785 
    786 			default:
    787 				if (fdp->fd_ofiles[i - 2] == NULL ||
    788 				    FILE_IS_USABLE(fdp->fd_ofiles[i - 2]) == 0)
    789 					continue;
    790 				d.d_fileno = i - 2 + FD_STDIN;
    791 				d.d_namlen = sprintf(d.d_name, "%d", (int) i - 2);
    792 				d.d_type = DT_UNKNOWN;
    793 				break;
    794 			}
    795 
    796 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    797 				break;
    798 			if (cookies)
    799 				*cookies++ = i + 1;
    800 		}
    801 	}
    802 
    803 	if (ap->a_ncookies && error) {
    804 		free(*ap->a_cookies, M_TEMP);
    805 		*ap->a_ncookies = 0;
    806 		*ap->a_cookies = NULL;
    807 	}
    808 
    809 	uio->uio_offset = i;
    810 	return (error);
    811 }
    812 
    813 int
    814 fdesc_readlink(v)
    815 	void *v;
    816 {
    817 	struct vop_readlink_args /* {
    818 		struct vnode *a_vp;
    819 		struct uio *a_uio;
    820 		struct ucred *a_cred;
    821 	} */ *ap = v;
    822 	struct vnode *vp = ap->a_vp;
    823 	int error;
    824 
    825 	if (vp->v_type != VLNK)
    826 		return (EPERM);
    827 
    828 	if (VTOFDESC(vp)->fd_type == Flink) {
    829 		char *ln = VTOFDESC(vp)->fd_link;
    830 		error = uiomove(ln, strlen(ln), ap->a_uio);
    831 	} else {
    832 		error = EOPNOTSUPP;
    833 	}
    834 
    835 	return (error);
    836 }
    837 
    838 int
    839 fdesc_read(v)
    840 	void *v;
    841 {
    842 	struct vop_read_args /* {
    843 		struct vnode *a_vp;
    844 		struct uio *a_uio;
    845 		int  a_ioflag;
    846 		struct ucred *a_cred;
    847 	} */ *ap = v;
    848 	int error = EOPNOTSUPP;
    849 	struct vnode *vp = ap->a_vp;
    850 
    851 	switch (VTOFDESC(vp)->fd_type) {
    852 	case Fctty:
    853 		VOP_UNLOCK(vp, 0);
    854 		error = (*ctty_cdevsw.d_read)(devctty, ap->a_uio, ap->a_ioflag);
    855 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    856 		break;
    857 
    858 	default:
    859 		error = EOPNOTSUPP;
    860 		break;
    861 	}
    862 
    863 	return (error);
    864 }
    865 
    866 int
    867 fdesc_write(v)
    868 	void *v;
    869 {
    870 	struct vop_write_args /* {
    871 		struct vnode *a_vp;
    872 		struct uio *a_uio;
    873 		int  a_ioflag;
    874 		struct ucred *a_cred;
    875 	} */ *ap = v;
    876 	int error = EOPNOTSUPP;
    877 	struct vnode *vp = ap->a_vp;
    878 
    879 	switch (VTOFDESC(vp)->fd_type) {
    880 	case Fctty:
    881 		VOP_UNLOCK(vp, 0);
    882 		error = (*ctty_cdevsw.d_write)(devctty, ap->a_uio,
    883 					       ap->a_ioflag);
    884 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    885 		break;
    886 
    887 	default:
    888 		error = EOPNOTSUPP;
    889 		break;
    890 	}
    891 
    892 	return (error);
    893 }
    894 
    895 int
    896 fdesc_ioctl(v)
    897 	void *v;
    898 {
    899 	struct vop_ioctl_args /* {
    900 		struct vnode *a_vp;
    901 		u_long a_command;
    902 		void *a_data;
    903 		int  a_fflag;
    904 		struct ucred *a_cred;
    905 		struct proc *a_p;
    906 	} */ *ap = v;
    907 	int error = EOPNOTSUPP;
    908 
    909 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    910 	case Fctty:
    911 		error = (*ctty_cdevsw.d_ioctl)(devctty, ap->a_command,
    912 					       ap->a_data, ap->a_fflag,
    913 					       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 = (*ctty_cdevsw.d_poll)(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_kqfilter(v)
    950 	void *v;
    951 {
    952 	struct vop_kqfilter_args /* {
    953 		struct vnode *a_vp;
    954 		struct knote *a_kn;
    955 	} */ *ap = v;
    956 	int error;
    957 	struct proc *p;
    958 	struct file *fp;
    959 
    960 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    961 	case Fctty:
    962 		error = (*ctty_cdevsw.d_kqfilter)(devctty, ap->a_kn);
    963 		break;
    964 
    965 	case Fdesc:
    966 		/* just invoke kqfilter for the underlying descriptor */
    967 		p = curproc;	/* XXX hopefully ok to use curproc here */
    968 		if ((fp = fd_getfile(p->p_fd, VTOFDESC(ap->a_vp)->fd_fd)) == NULL)
    969 			return (1);
    970 
    971 		FILE_USE(fp);
    972 		error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
    973 		FILE_UNUSE(fp, p);
    974 		break;
    975 
    976 	default:
    977 		return (genfs_kqfilter(v));
    978 	}
    979 
    980 	return (error);
    981 }
    982 
    983 int
    984 fdesc_inactive(v)
    985 	void *v;
    986 {
    987 	struct vop_inactive_args /* {
    988 		struct vnode *a_vp;
    989 		struct proc *a_p;
    990 	} */ *ap = v;
    991 	struct vnode *vp = ap->a_vp;
    992 
    993 	/*
    994 	 * Clear out the v_type field to avoid
    995 	 * nasty things happening in vgone().
    996 	 */
    997 	VOP_UNLOCK(vp, 0);
    998 	vp->v_type = VNON;
    999 	return (0);
   1000 }
   1001 
   1002 int
   1003 fdesc_reclaim(v)
   1004 	void *v;
   1005 {
   1006 	struct vop_reclaim_args /* {
   1007 		struct vnode *a_vp;
   1008 	} */ *ap = v;
   1009 	struct vnode *vp = ap->a_vp;
   1010 	struct fdescnode *fd = VTOFDESC(vp);
   1011 
   1012 	LIST_REMOVE(fd, fd_hash);
   1013 	FREE(vp->v_data, M_TEMP);
   1014 	vp->v_data = 0;
   1015 
   1016 	return (0);
   1017 }
   1018 
   1019 /*
   1020  * Return POSIX pathconf information applicable to special devices.
   1021  */
   1022 int
   1023 fdesc_pathconf(v)
   1024 	void *v;
   1025 {
   1026 	struct vop_pathconf_args /* {
   1027 		struct vnode *a_vp;
   1028 		int a_name;
   1029 		register_t *a_retval;
   1030 	} */ *ap = v;
   1031 
   1032 	switch (ap->a_name) {
   1033 	case _PC_LINK_MAX:
   1034 		*ap->a_retval = LINK_MAX;
   1035 		return (0);
   1036 	case _PC_MAX_CANON:
   1037 		*ap->a_retval = MAX_CANON;
   1038 		return (0);
   1039 	case _PC_MAX_INPUT:
   1040 		*ap->a_retval = MAX_INPUT;
   1041 		return (0);
   1042 	case _PC_PIPE_BUF:
   1043 		*ap->a_retval = PIPE_BUF;
   1044 		return (0);
   1045 	case _PC_CHOWN_RESTRICTED:
   1046 		*ap->a_retval = 1;
   1047 		return (0);
   1048 	case _PC_VDISABLE:
   1049 		*ap->a_retval = _POSIX_VDISABLE;
   1050 		return (0);
   1051 	case _PC_SYNC_IO:
   1052 		*ap->a_retval = 1;
   1053 		return (0);
   1054 	default:
   1055 		return (EINVAL);
   1056 	}
   1057 	/* NOTREACHED */
   1058 }
   1059 
   1060 /*
   1061  * Print out the contents of a /dev/fd vnode.
   1062  */
   1063 /* ARGSUSED */
   1064 int
   1065 fdesc_print(v)
   1066 	void *v;
   1067 {
   1068 	printf("tag VT_NON, fdesc vnode\n");
   1069 	return (0);
   1070 }
   1071 
   1072 int
   1073 fdesc_link(v)
   1074 	void *v;
   1075 {
   1076 	struct vop_link_args /* {
   1077 		struct vnode *a_dvp;
   1078 		struct vnode *a_vp;
   1079 		struct componentname *a_cnp;
   1080 	} */ *ap = v;
   1081 
   1082 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1083 	vput(ap->a_dvp);
   1084 	return (EROFS);
   1085 }
   1086 
   1087 int
   1088 fdesc_symlink(v)
   1089 	void *v;
   1090 {
   1091 	struct vop_symlink_args /* {
   1092 		struct vnode *a_dvp;
   1093 		struct vnode **a_vpp;
   1094 		struct componentname *a_cnp;
   1095 		struct vattr *a_vap;
   1096 		char *a_target;
   1097 	} */ *ap = v;
   1098 
   1099 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1100 	vput(ap->a_dvp);
   1101 	return (EROFS);
   1102 }
   1103