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