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