Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vnops.c revision 1.21
      1 /*	$NetBSD: fdesc_vnops.c,v 1.21 1994/12/04 03:13:06 mycroft 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  *	from: Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp
     39  *	@(#)fdesc_vnops.c	8.9 (Berkeley) 1/21/94
     40  */
     41 
     42 /*
     43  * /dev/fd Filesystem
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/types.h>
     49 #include <sys/time.h>
     50 #include <sys/proc.h>
     51 #include <sys/kernel.h>	/* boottime */
     52 #include <sys/resourcevar.h>
     53 #include <sys/filedesc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/malloc.h>
     56 #include <sys/file.h>
     57 #include <sys/stat.h>
     58 #include <sys/mount.h>
     59 #include <sys/namei.h>
     60 #include <sys/buf.h>
     61 #include <sys/dirent.h>
     62 #include <miscfs/fdesc/fdesc.h>
     63 
     64 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
     65 
     66 #define FDL_WANT	0x01
     67 #define FDL_LOCKED	0x02
     68 static int fdcache_lock;
     69 
     70 dev_t devctty;
     71 
     72 #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
     73 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
     74 #endif
     75 
     76 #define	NFDCACHE 4
     77 
     78 #define FD_NHASH(ix) \
     79 	(&fdhashtbl[(ix) & fdhash])
     80 LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
     81 u_long fdhash;
     82 
     83 /*
     84  * Initialise cache headers
     85  */
     86 fdesc_init()
     87 {
     88 
     89 	devctty = makedev(nchrdev, 0);
     90 	fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
     91 }
     92 
     93 int
     94 fdesc_allocvp(ftype, ix, mp, vpp)
     95 	fdntype ftype;
     96 	int ix;
     97 	struct mount *mp;
     98 	struct vnode **vpp;
     99 {
    100 	struct fdhashhead *fc;
    101 	struct fdescnode *fd;
    102 	int error = 0;
    103 
    104 	fc = FD_NHASH(ix);
    105 loop:
    106 	for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
    107 		if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
    108 			if (vget(fd->fd_vnode, 0))
    109 				goto loop;
    110 			*vpp = fd->fd_vnode;
    111 			return (error);
    112 		}
    113 	}
    114 
    115 	/*
    116 	 * otherwise lock the array while we call getnewvnode
    117 	 * since that can block.
    118 	 */
    119 	if (fdcache_lock & FDL_LOCKED) {
    120 		fdcache_lock |= FDL_WANT;
    121 		sleep((caddr_t) &fdcache_lock, PINOD);
    122 		goto loop;
    123 	}
    124 	fdcache_lock |= FDL_LOCKED;
    125 
    126 	error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
    127 	if (error)
    128 		goto out;
    129 	MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
    130 	(*vpp)->v_data = fd;
    131 	fd->fd_vnode = *vpp;
    132 	fd->fd_type = ftype;
    133 	fd->fd_fd = -1;
    134 	fd->fd_link = 0;
    135 	fd->fd_ix = ix;
    136 	LIST_INSERT_HEAD(fc, fd, fd_hash);
    137 
    138 out:;
    139 	fdcache_lock &= ~FDL_LOCKED;
    140 
    141 	if (fdcache_lock & FDL_WANT) {
    142 		fdcache_lock &= ~FDL_WANT;
    143 		wakeup((caddr_t) &fdcache_lock);
    144 	}
    145 
    146 	return (error);
    147 }
    148 
    149 /*
    150  * vp is the current namei directory
    151  * ndp is the name to locate in that directory...
    152  */
    153 int
    154 fdesc_lookup(ap)
    155 	struct vop_lookup_args /* {
    156 		struct vnode * a_dvp;
    157 		struct vnode ** a_vpp;
    158 		struct componentname * a_cnp;
    159 	} */ *ap;
    160 {
    161 	struct vnode **vpp = ap->a_vpp;
    162 	struct vnode *dvp = ap->a_dvp;
    163 	char *pname;
    164 	struct proc *p;
    165 	int nfiles;
    166 	unsigned fd;
    167 	int error;
    168 	struct vnode *fvp;
    169 	char *ln;
    170 
    171 	pname = ap->a_cnp->cn_nameptr;
    172 	if (ap->a_cnp->cn_namelen == 1 && *pname == '.') {
    173 		*vpp = dvp;
    174 		VREF(dvp);
    175 		VOP_LOCK(dvp);
    176 		return (0);
    177 	}
    178 
    179 	p = ap->a_cnp->cn_proc;
    180 	nfiles = p->p_fd->fd_nfiles;
    181 
    182 	switch (VTOFDESC(dvp)->fd_type) {
    183 	default:
    184 	case Flink:
    185 	case Fdesc:
    186 	case Fctty:
    187 		error = ENOTDIR;
    188 		goto bad;
    189 
    190 	case Froot:
    191 		if (ap->a_cnp->cn_namelen == 2 && bcmp(pname, "fd", 2) == 0) {
    192 			error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
    193 			if (error)
    194 				goto bad;
    195 			*vpp = fvp;
    196 			fvp->v_type = VDIR;
    197 			VOP_LOCK(fvp);
    198 			return (0);
    199 		}
    200 
    201 		if (ap->a_cnp->cn_namelen == 3 && bcmp(pname, "tty", 3) == 0) {
    202 			struct vnode *ttyvp = cttyvp(p);
    203 			if (ttyvp == NULL) {
    204 				error = ENXIO;
    205 				goto bad;
    206 			}
    207 			error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
    208 			if (error)
    209 				goto bad;
    210 			*vpp = fvp;
    211 			fvp->v_type = VFIFO;
    212 			VOP_LOCK(fvp);
    213 			return (0);
    214 		}
    215 
    216 		ln = 0;
    217 		switch (ap->a_cnp->cn_namelen) {
    218 		case 5:
    219 			if (bcmp(pname, "stdin", 5) == 0) {
    220 				ln = "fd/0";
    221 				fd = FD_STDIN;
    222 			}
    223 			break;
    224 		case 6:
    225 			if (bcmp(pname, "stdout", 6) == 0) {
    226 				ln = "fd/1";
    227 				fd = FD_STDOUT;
    228 			} else
    229 			if (bcmp(pname, "stderr", 6) == 0) {
    230 				ln = "fd/2";
    231 				fd = FD_STDERR;
    232 			}
    233 			break;
    234 		}
    235 
    236 		if (ln) {
    237 			error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
    238 			if (error)
    239 				goto bad;
    240 			VTOFDESC(fvp)->fd_link = ln;
    241 			*vpp = fvp;
    242 			fvp->v_type = VLNK;
    243 			VOP_LOCK(fvp);
    244 			return (0);
    245 		} else {
    246 			error = ENOENT;
    247 			goto bad;
    248 		}
    249 
    250 		/* FALL THROUGH */
    251 
    252 	case Fdevfd:
    253 		if (ap->a_cnp->cn_namelen == 2 && bcmp(pname, "..", 2) == 0) {
    254 			error = fdesc_root(dvp->v_mount, vpp);
    255 			return (error);
    256 		}
    257 
    258 		fd = 0;
    259 		while (*pname >= '0' && *pname <= '9') {
    260 			fd = 10 * fd + *pname++ - '0';
    261 			if (fd >= nfiles)
    262 				break;
    263 		}
    264 
    265 		if (*pname != '\0') {
    266 			error = ENOENT;
    267 			goto bad;
    268 		}
    269 
    270 		if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
    271 			error = EBADF;
    272 			goto bad;
    273 		}
    274 
    275 		error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
    276 		if (error)
    277 			goto bad;
    278 		VTOFDESC(fvp)->fd_fd = fd;
    279 		*vpp = fvp;
    280 		return (0);
    281 	}
    282 
    283 bad:;
    284 	*vpp = NULL;
    285 	return (error);
    286 }
    287 
    288 int
    289 fdesc_open(ap)
    290 	struct vop_open_args /* {
    291 		struct vnode *a_vp;
    292 		int  a_mode;
    293 		struct ucred *a_cred;
    294 		struct proc *a_p;
    295 		struct file *a_fp;
    296 	} */ *ap;
    297 {
    298 	struct vnode *vp = ap->a_vp;
    299 
    300 	switch (VTOFDESC(vp)->fd_type) {
    301 	case Fdesc:
    302 		/* Do the same thing as fdopen(). */
    303 		return (fddupopen(VTOFDESC(vp)->fd_fd, ap->a_mode, ap->a_p, ap->a_fp));
    304 
    305 	case Fctty:
    306 		return (cttyopen(devctty, ap->a_mode, 0, ap->a_p));
    307 	}
    308 
    309 	return (0);
    310 }
    311 
    312 static int
    313 fdesc_attr(fd, vap, cred, p)
    314 	int fd;
    315 	struct vattr *vap;
    316 	struct ucred *cred;
    317 	struct proc *p;
    318 {
    319 	struct filedesc *fdp = p->p_fd;
    320 	struct file *fp;
    321 	struct stat stb;
    322 	int error;
    323 
    324 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
    325 		return (EBADF);
    326 
    327 	switch (fp->f_type) {
    328 	case DTYPE_VNODE:
    329 		error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
    330 		if (error == 0 && vap->va_type == VDIR) {
    331 			/*
    332 			 * don't allow directories to show up because
    333 			 * that causes loops in the namespace.
    334 			 */
    335 			vap->va_type = VFIFO;
    336 		}
    337 		break;
    338 
    339 	case DTYPE_SOCKET:
    340 		error = soo_stat((struct socket *)fp->f_data, &stb);
    341 		if (error == 0) {
    342 			vattr_null(vap);
    343 			vap->va_type = VSOCK;
    344 			vap->va_mode = stb.st_mode;
    345 			vap->va_nlink = stb.st_nlink;
    346 			vap->va_uid = stb.st_uid;
    347 			vap->va_gid = stb.st_gid;
    348 			vap->va_fsid = stb.st_dev;
    349 			vap->va_fileid = stb.st_ino;
    350 			vap->va_size = stb.st_size;
    351 			vap->va_blocksize = stb.st_blksize;
    352 			vap->va_atime = stb.st_atimespec;
    353 			vap->va_mtime = stb.st_mtimespec;
    354 			vap->va_ctime = stb.st_ctimespec;
    355 			vap->va_gen = stb.st_gen;
    356 			vap->va_flags = stb.st_flags;
    357 			vap->va_rdev = stb.st_rdev;
    358 			vap->va_bytes = stb.st_blocks * stb.st_blksize;
    359 		}
    360 		break;
    361 
    362 	default:
    363 		panic("fdesc attr");
    364 		break;
    365 	}
    366 
    367 	return (error);
    368 }
    369 
    370 int
    371 fdesc_getattr(ap)
    372 	struct vop_getattr_args /* {
    373 		struct vnode *a_vp;
    374 		struct vattr *a_vap;
    375 		struct ucred *a_cred;
    376 		struct proc *a_p;
    377 	} */ *ap;
    378 {
    379 	struct vnode *vp = ap->a_vp;
    380 	struct vattr *vap = ap->a_vap;
    381 	unsigned fd;
    382 	int error = 0;
    383 
    384 	switch (VTOFDESC(vp)->fd_type) {
    385 	case Froot:
    386 	case Fdevfd:
    387 	case Flink:
    388 	case Fctty:
    389 		bzero((caddr_t) vap, sizeof(*vap));
    390 		vattr_null(vap);
    391 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
    392 
    393 		switch (VTOFDESC(vp)->fd_type) {
    394 		case Flink:
    395 			vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    396 			vap->va_type = VLNK;
    397 			vap->va_nlink = 1;
    398 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
    399 			break;
    400 
    401 		case Fctty:
    402 			vap->va_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
    403 			vap->va_type = VFIFO;
    404 			vap->va_nlink = 1;
    405 			vap->va_size = 0;
    406 			break;
    407 
    408 		default:
    409 			vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    410 			vap->va_type = VDIR;
    411 			vap->va_nlink = 2;
    412 			vap->va_size = DEV_BSIZE;
    413 			break;
    414 		}
    415 		vap->va_uid = 0;
    416 		vap->va_gid = 0;
    417 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
    418 		vap->va_blocksize = DEV_BSIZE;
    419 		vap->va_atime.ts_sec = boottime.tv_sec;
    420 		vap->va_atime.ts_nsec = 0;
    421 		vap->va_mtime = vap->va_atime;
    422 		vap->va_ctime = vap->va_mtime;
    423 		vap->va_gen = 0;
    424 		vap->va_flags = 0;
    425 		vap->va_rdev = 0;
    426 		vap->va_bytes = 0;
    427 		break;
    428 
    429 	case Fdesc:
    430 		fd = VTOFDESC(vp)->fd_fd;
    431 		error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
    432 		break;
    433 
    434 	default:
    435 		panic("fdesc_getattr");
    436 		break;
    437 	}
    438 
    439 	if (error == 0)
    440 		vp->v_type = vap->va_type;
    441 
    442 	return (error);
    443 }
    444 
    445 int
    446 fdesc_setattr(ap)
    447 	struct vop_setattr_args /* {
    448 		struct vnode *a_vp;
    449 		struct vattr *a_vap;
    450 		struct ucred *a_cred;
    451 		struct proc *a_p;
    452 	} */ *ap;
    453 {
    454 	struct filedesc *fdp = ap->a_p->p_fd;
    455 	struct file *fp;
    456 	unsigned fd;
    457 	int error;
    458 
    459 	/*
    460 	 * Can't mess with the root vnode
    461 	 */
    462 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    463 	case Fdesc:
    464 		break;
    465 
    466 	case Fctty:
    467 		return (0);
    468 
    469 	default:
    470 		return (EACCES);
    471 	}
    472 
    473 	fd = VTOFDESC(ap->a_vp)->fd_fd;
    474 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
    475 		return (EBADF);
    476 	}
    477 
    478 	/*
    479 	 * Can setattr the underlying vnode, but not sockets!
    480 	 */
    481 	switch (fp->f_type) {
    482 	case DTYPE_VNODE:
    483 		error = VOP_SETATTR((struct vnode *) fp->f_data, ap->a_vap, ap->a_cred, ap->a_p);
    484 		break;
    485 
    486 	case DTYPE_SOCKET:
    487 		error = 0;
    488 		break;
    489 
    490 	default:
    491 		panic("fdesc setattr");
    492 		break;
    493 	}
    494 
    495 	return (error);
    496 }
    497 
    498 #define UIO_MX 16
    499 
    500 static struct dirtmp {
    501 	u_long d_fileno;
    502 	u_short d_reclen;
    503 	u_short d_namlen;
    504 	char d_name[8];
    505 } rootent[] = {
    506 	{ FD_DEVFD, UIO_MX, 2, "fd" },
    507 	{ FD_STDIN, UIO_MX, 5, "stdin" },
    508 	{ FD_STDOUT, UIO_MX, 6, "stdout" },
    509 	{ FD_STDERR, UIO_MX, 6, "stderr" },
    510 	{ FD_CTTY, UIO_MX, 3, "tty" },
    511 	{ 0 }
    512 };
    513 
    514 int
    515 fdesc_readdir(ap)
    516 	struct vop_readdir_args /* {
    517 		struct vnode *a_vp;
    518 		struct uio *a_uio;
    519 		struct ucred *a_cred;
    520 	} */ *ap;
    521 {
    522 	struct uio *uio = ap->a_uio;
    523 	struct filedesc *fdp;
    524 	int i;
    525 	int error;
    526 
    527 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    528 	case Fctty:
    529 		return (0);
    530 
    531 	case Fdesc:
    532 		return (ENOTDIR);
    533 
    534 	default:
    535 		break;
    536 	}
    537 
    538 	fdp = uio->uio_procp->p_fd;
    539 
    540 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
    541 		struct dirent d;
    542 		struct dirent *dp = &d;
    543 		struct dirtmp *dt;
    544 
    545 		i = uio->uio_offset / UIO_MX;
    546 		error = 0;
    547 
    548 		while (uio->uio_resid > 0) {
    549 			dt = &rootent[i];
    550 			if (dt->d_fileno == 0) {
    551 				/**eofflagp = 1;*/
    552 				break;
    553 			}
    554 			i++;
    555 
    556 			switch (dt->d_fileno) {
    557 			case FD_CTTY:
    558 				if (cttyvp(uio->uio_procp) == NULL)
    559 					continue;
    560 				break;
    561 
    562 			case FD_STDIN:
    563 			case FD_STDOUT:
    564 			case FD_STDERR:
    565 				if ((dt->d_fileno-FD_STDIN) >= fdp->fd_nfiles)
    566 					continue;
    567 				if (fdp->fd_ofiles[dt->d_fileno-FD_STDIN] == NULL)
    568 					continue;
    569 				break;
    570 			}
    571 			bzero((caddr_t) dp, UIO_MX);
    572 			dp->d_fileno = dt->d_fileno;
    573 			dp->d_namlen = dt->d_namlen;
    574 			dp->d_type = DT_UNKNOWN;
    575 			dp->d_reclen = dt->d_reclen;
    576 			bcopy(dt->d_name, dp->d_name, dp->d_namlen+1);
    577 			error = uiomove((caddr_t) dp, UIO_MX, uio);
    578 			if (error)
    579 				break;
    580 		}
    581 		uio->uio_offset = i * UIO_MX;
    582 		return (error);
    583 	}
    584 
    585 	i = uio->uio_offset / UIO_MX;
    586 	error = 0;
    587 	while (uio->uio_resid > 0) {
    588 		if (i >= fdp->fd_nfiles)
    589 			break;
    590 
    591 		if (fdp->fd_ofiles[i] != NULL) {
    592 			struct dirent d;
    593 			struct dirent *dp = &d;
    594 
    595 			bzero((caddr_t) dp, UIO_MX);
    596 
    597 			dp->d_namlen = sprintf(dp->d_name, "%d", i);
    598 			dp->d_reclen = UIO_MX;
    599 			dp->d_type = DT_UNKNOWN;
    600 			dp->d_fileno = i + FD_STDIN;
    601 			/*
    602 			 * And ship to userland
    603 			 */
    604 			error = uiomove((caddr_t) dp, UIO_MX, uio);
    605 			if (error)
    606 				break;
    607 		}
    608 		i++;
    609 	}
    610 
    611 	uio->uio_offset = i * UIO_MX;
    612 	return (error);
    613 }
    614 
    615 int
    616 fdesc_readlink(ap)
    617 	struct vop_readlink_args /* {
    618 		struct vnode *a_vp;
    619 		struct uio *a_uio;
    620 		struct ucred *a_cred;
    621 	} */ *ap;
    622 {
    623 	struct vnode *vp = ap->a_vp;
    624 	int error;
    625 
    626 	if (vp->v_type != VLNK)
    627 		return (EPERM);
    628 
    629 	if (VTOFDESC(vp)->fd_type == Flink) {
    630 		char *ln = VTOFDESC(vp)->fd_link;
    631 		error = uiomove(ln, strlen(ln), ap->a_uio);
    632 	} else {
    633 		error = EOPNOTSUPP;
    634 	}
    635 
    636 	return (error);
    637 }
    638 
    639 int
    640 fdesc_read(ap)
    641 	struct vop_read_args /* {
    642 		struct vnode *a_vp;
    643 		struct uio *a_uio;
    644 		int  a_ioflag;
    645 		struct ucred *a_cred;
    646 	} */ *ap;
    647 {
    648 	int error = EOPNOTSUPP;
    649 
    650 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    651 	case Fctty:
    652 		error = cttyread(devctty, ap->a_uio, ap->a_ioflag);
    653 		break;
    654 
    655 	default:
    656 		error = EOPNOTSUPP;
    657 		break;
    658 	}
    659 
    660 	return (error);
    661 }
    662 
    663 int
    664 fdesc_write(ap)
    665 	struct vop_write_args /* {
    666 		struct vnode *a_vp;
    667 		struct uio *a_uio;
    668 		int  a_ioflag;
    669 		struct ucred *a_cred;
    670 	} */ *ap;
    671 {
    672 	int error = EOPNOTSUPP;
    673 
    674 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    675 	case Fctty:
    676 		error = cttywrite(devctty, ap->a_uio, ap->a_ioflag);
    677 		break;
    678 
    679 	default:
    680 		error = EOPNOTSUPP;
    681 		break;
    682 	}
    683 
    684 	return (error);
    685 }
    686 
    687 int
    688 fdesc_ioctl(ap)
    689 	struct vop_ioctl_args /* {
    690 		struct vnode *a_vp;
    691 		u_long a_command;
    692 		caddr_t  a_data;
    693 		int  a_fflag;
    694 		struct ucred *a_cred;
    695 		struct proc *a_p;
    696 	} */ *ap;
    697 {
    698 	int error = EOPNOTSUPP;
    699 
    700 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    701 	case Fctty:
    702 		error = cttyioctl(devctty, ap->a_command, ap->a_data,
    703 					ap->a_fflag, ap->a_p);
    704 		break;
    705 
    706 	default:
    707 		error = EOPNOTSUPP;
    708 		break;
    709 	}
    710 
    711 	return (error);
    712 }
    713 
    714 int
    715 fdesc_select(ap)
    716 	struct vop_select_args /* {
    717 		struct vnode *a_vp;
    718 		int  a_which;
    719 		int  a_fflags;
    720 		struct ucred *a_cred;
    721 		struct proc *a_p;
    722 	} */ *ap;
    723 {
    724 	int error = EOPNOTSUPP;
    725 
    726 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    727 	case Fctty:
    728 		error = cttyselect(devctty, ap->a_fflags, ap->a_p);
    729 		break;
    730 
    731 	default:
    732 		error = EOPNOTSUPP;
    733 		break;
    734 	}
    735 
    736 	return (error);
    737 }
    738 
    739 int
    740 fdesc_inactive(ap)
    741 	struct vop_inactive_args /* {
    742 		struct vnode *a_vp;
    743 	} */ *ap;
    744 {
    745 	struct vnode *vp = ap->a_vp;
    746 
    747 	/*
    748 	 * Clear out the v_type field to avoid
    749 	 * nasty things happening in vgone().
    750 	 */
    751 	vp->v_type = VNON;
    752 	return (0);
    753 }
    754 
    755 int
    756 fdesc_reclaim(ap)
    757 	struct vop_reclaim_args /* {
    758 		struct vnode *a_vp;
    759 	} */ *ap;
    760 {
    761 	struct vnode *vp = ap->a_vp;
    762 	struct fdescnode *fd = VTOFDESC(vp);
    763 
    764 	LIST_REMOVE(fd, fd_hash);
    765 	FREE(vp->v_data, M_TEMP);
    766 	vp->v_data = 0;
    767 
    768 	return (0);
    769 }
    770 
    771 /*
    772  * Return POSIX pathconf information applicable to special devices.
    773  */
    774 fdesc_pathconf(ap)
    775 	struct vop_pathconf_args /* {
    776 		struct vnode *a_vp;
    777 		int a_name;
    778 		register_t *a_retval;
    779 	} */ *ap;
    780 {
    781 
    782 	switch (ap->a_name) {
    783 	case _PC_LINK_MAX:
    784 		*ap->a_retval = LINK_MAX;
    785 		return (0);
    786 	case _PC_MAX_CANON:
    787 		*ap->a_retval = MAX_CANON;
    788 		return (0);
    789 	case _PC_MAX_INPUT:
    790 		*ap->a_retval = MAX_INPUT;
    791 		return (0);
    792 	case _PC_PIPE_BUF:
    793 		*ap->a_retval = PIPE_BUF;
    794 		return (0);
    795 	case _PC_CHOWN_RESTRICTED:
    796 		*ap->a_retval = 1;
    797 		return (0);
    798 	case _PC_VDISABLE:
    799 		*ap->a_retval = _POSIX_VDISABLE;
    800 		return (0);
    801 	default:
    802 		return (EINVAL);
    803 	}
    804 	/* NOTREACHED */
    805 }
    806 
    807 /*
    808  * Print out the contents of a /dev/fd vnode.
    809  */
    810 /* ARGSUSED */
    811 int
    812 fdesc_print(ap)
    813 	struct vop_print_args /* {
    814 		struct vnode *a_vp;
    815 	} */ *ap;
    816 {
    817 
    818 	printf("tag VT_NON, fdesc vnode\n");
    819 	return (0);
    820 }
    821 
    822 /*void*/
    823 int
    824 fdesc_vfree(ap)
    825 	struct vop_vfree_args /* {
    826 		struct vnode *a_pvp;
    827 		ino_t a_ino;
    828 		int a_mode;
    829 	} */ *ap;
    830 {
    831 
    832 	return (0);
    833 }
    834 
    835 /*
    836  * /dev/fd vnode unsupported operation
    837  */
    838 int
    839 fdesc_enotsupp()
    840 {
    841 
    842 	return (EOPNOTSUPP);
    843 }
    844 
    845 /*
    846  * /dev/fd "should never get here" operation
    847  */
    848 int
    849 fdesc_badop()
    850 {
    851 
    852 	panic("fdesc: bad op");
    853 	/* NOTREACHED */
    854 }
    855 
    856 /*
    857  * /dev/fd vnode null operation
    858  */
    859 int
    860 fdesc_nullop()
    861 {
    862 
    863 	return (0);
    864 }
    865 
    866 #define fdesc_create ((int (*) __P((struct  vop_create_args *)))fdesc_enotsupp)
    867 #define fdesc_mknod ((int (*) __P((struct  vop_mknod_args *)))fdesc_enotsupp)
    868 #define fdesc_close ((int (*) __P((struct  vop_close_args *)))nullop)
    869 #define fdesc_access ((int (*) __P((struct  vop_access_args *)))nullop)
    870 #define fdesc_mmap ((int (*) __P((struct  vop_mmap_args *)))fdesc_enotsupp)
    871 #define fdesc_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)
    872 #define fdesc_seek ((int (*) __P((struct  vop_seek_args *)))nullop)
    873 #define fdesc_remove ((int (*) __P((struct  vop_remove_args *)))fdesc_enotsupp)
    874 #define fdesc_link ((int (*) __P((struct  vop_link_args *)))fdesc_enotsupp)
    875 #define fdesc_rename ((int (*) __P((struct  vop_rename_args *)))fdesc_enotsupp)
    876 #define fdesc_mkdir ((int (*) __P((struct  vop_mkdir_args *)))fdesc_enotsupp)
    877 #define fdesc_rmdir ((int (*) __P((struct  vop_rmdir_args *)))fdesc_enotsupp)
    878 #define fdesc_symlink ((int (*) __P((struct vop_symlink_args *)))fdesc_enotsupp)
    879 #define fdesc_abortop ((int (*) __P((struct  vop_abortop_args *)))nullop)
    880 #define fdesc_lock ((int (*) __P((struct  vop_lock_args *)))nullop)
    881 #define fdesc_unlock ((int (*) __P((struct  vop_unlock_args *)))nullop)
    882 #define fdesc_bmap ((int (*) __P((struct  vop_bmap_args *)))fdesc_badop)
    883 #define fdesc_strategy ((int (*) __P((struct  vop_strategy_args *)))fdesc_badop)
    884 #define fdesc_islocked ((int (*) __P((struct  vop_islocked_args *)))nullop)
    885 #define fdesc_advlock ((int (*) __P((struct vop_advlock_args *)))fdesc_enotsupp)
    886 #define fdesc_blkatoff \
    887 	((int (*) __P((struct  vop_blkatoff_args *)))fdesc_enotsupp)
    888 #define fdesc_vget ((int (*) __P((struct  vop_vget_args *)))fdesc_enotsupp)
    889 #define fdesc_valloc ((int(*) __P(( \
    890 		struct vnode *pvp, \
    891 		int mode, \
    892 		struct ucred *cred, \
    893 		struct vnode **vpp))) fdesc_enotsupp)
    894 #define fdesc_truncate \
    895 	((int (*) __P((struct  vop_truncate_args *)))fdesc_enotsupp)
    896 #define fdesc_update ((int (*) __P((struct  vop_update_args *)))fdesc_enotsupp)
    897 #define fdesc_bwrite ((int (*) __P((struct  vop_bwrite_args *)))fdesc_enotsupp)
    898 
    899 int (**fdesc_vnodeop_p)();
    900 struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
    901 	{ &vop_default_desc, vn_default_error },
    902 	{ &vop_lookup_desc, fdesc_lookup },	/* lookup */
    903 	{ &vop_create_desc, fdesc_create },	/* create */
    904 	{ &vop_mknod_desc, fdesc_mknod },	/* mknod */
    905 	{ &vop_open_desc, fdesc_open },		/* open */
    906 	{ &vop_close_desc, fdesc_close },	/* close */
    907 	{ &vop_access_desc, fdesc_access },	/* access */
    908 	{ &vop_getattr_desc, fdesc_getattr },	/* getattr */
    909 	{ &vop_setattr_desc, fdesc_setattr },	/* setattr */
    910 	{ &vop_read_desc, fdesc_read },		/* read */
    911 	{ &vop_write_desc, fdesc_write },	/* write */
    912 	{ &vop_ioctl_desc, fdesc_ioctl },	/* ioctl */
    913 	{ &vop_select_desc, fdesc_select },	/* select */
    914 	{ &vop_mmap_desc, fdesc_mmap },		/* mmap */
    915 	{ &vop_fsync_desc, fdesc_fsync },	/* fsync */
    916 	{ &vop_seek_desc, fdesc_seek },		/* seek */
    917 	{ &vop_remove_desc, fdesc_remove },	/* remove */
    918 	{ &vop_link_desc, fdesc_link },		/* link */
    919 	{ &vop_rename_desc, fdesc_rename },	/* rename */
    920 	{ &vop_mkdir_desc, fdesc_mkdir },	/* mkdir */
    921 	{ &vop_rmdir_desc, fdesc_rmdir },	/* rmdir */
    922 	{ &vop_symlink_desc, fdesc_symlink },	/* symlink */
    923 	{ &vop_readdir_desc, fdesc_readdir },	/* readdir */
    924 	{ &vop_readlink_desc, fdesc_readlink },	/* readlink */
    925 	{ &vop_abortop_desc, fdesc_abortop },	/* abortop */
    926 	{ &vop_inactive_desc, fdesc_inactive },	/* inactive */
    927 	{ &vop_reclaim_desc, fdesc_reclaim },	/* reclaim */
    928 	{ &vop_lock_desc, fdesc_lock },		/* lock */
    929 	{ &vop_unlock_desc, fdesc_unlock },	/* unlock */
    930 	{ &vop_bmap_desc, fdesc_bmap },		/* bmap */
    931 	{ &vop_strategy_desc, fdesc_strategy },	/* strategy */
    932 	{ &vop_print_desc, fdesc_print },	/* print */
    933 	{ &vop_islocked_desc, fdesc_islocked },	/* islocked */
    934 	{ &vop_pathconf_desc, fdesc_pathconf },	/* pathconf */
    935 	{ &vop_advlock_desc, fdesc_advlock },	/* advlock */
    936 	{ &vop_blkatoff_desc, fdesc_blkatoff },	/* blkatoff */
    937 	{ &vop_valloc_desc, fdesc_valloc },	/* valloc */
    938 	{ &vop_vfree_desc, fdesc_vfree },	/* vfree */
    939 	{ &vop_truncate_desc, fdesc_truncate },	/* truncate */
    940 	{ &vop_update_desc, fdesc_update },	/* update */
    941 	{ &vop_bwrite_desc, fdesc_bwrite },	/* bwrite */
    942 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
    943 };
    944 struct vnodeopv_desc fdesc_vnodeop_opv_desc =
    945 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
    946