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