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