xref: /csrg-svn/sys/miscfs/fdesc/fdesc_vnops.c (revision 59457)
153838Spendry /*
253838Spendry  * Copyright (c) 1992 The Regents of the University of California
359255Spendry  * Copyright (c) 1990, 1992, 1993 Jan-Simon Pendry
453838Spendry  * All rights reserved.
553838Spendry  *
653838Spendry  * This code is derived from software donated to Berkeley by
753838Spendry  * Jan-Simon Pendry.
853838Spendry  *
953838Spendry  * %sccs.include.redist.c%
1053838Spendry  *
11*59457Spendry  *	@(#)fdesc_vnops.c	7.6 (Berkeley) 04/28/93
1253838Spendry  *
1359255Spendry  * $Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp $
1453838Spendry  */
1553838Spendry 
1653838Spendry /*
1753838Spendry  * /dev/fd Filesystem
1853838Spendry  */
1953838Spendry 
2053838Spendry #include <sys/param.h>
2153838Spendry #include <sys/systm.h>
2253838Spendry #include <sys/types.h>
2353838Spendry #include <sys/time.h>
2453838Spendry #include <sys/proc.h>
2559255Spendry #include <sys/kernel.h>	/* boottime */
2653838Spendry #include <sys/resourcevar.h>
2753838Spendry #include <sys/filedesc.h>
2853838Spendry #include <sys/vnode.h>
2953838Spendry #include <sys/malloc.h>
3053838Spendry #include <sys/file.h>
3153838Spendry #include <sys/stat.h>
3253838Spendry #include <sys/mount.h>
3353838Spendry #include <sys/namei.h>
3453838Spendry #include <sys/buf.h>
3554981Spendry #include <sys/dirent.h>
3655017Smckusick #include <miscfs/fdesc/fdesc.h>
3753838Spendry 
3859255Spendry #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL)
3959255Spendry 
4059255Spendry #define FDL_WANT	0x01
4159255Spendry #define FDL_LOCKED	0x02
4259255Spendry static int fdescvplock;
4359255Spendry static struct vnode *fdescvp[FD_MAX];
4459255Spendry 
4559255Spendry #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
4659255Spendry FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
4759255Spendry #endif
4859255Spendry 
4959255Spendry fdesc_allocvp(ftype, ix, mp, vpp)
5059255Spendry 	fdntype ftype;
5159255Spendry 	int ix;
5259255Spendry 	struct mount *mp;
5359255Spendry 	struct vnode **vpp;
5459255Spendry {
5559255Spendry 	struct vnode **nvpp = 0;
5659255Spendry 	int error = 0;
5759255Spendry 
58*59457Spendry loop:
5959255Spendry 	/* get stashed copy of the vnode */
6059255Spendry 	if (ix >= 0 && ix < FD_MAX) {
6159255Spendry 		nvpp = &fdescvp[ix];
62*59457Spendry 		if (*nvpp) {
63*59457Spendry 			if (vget(*nvpp))
64*59457Spendry 				goto loop;
6559452Smckusick 			VOP_UNLOCK(*nvpp);
6659255Spendry 			*vpp = *nvpp;
6759255Spendry 			return (error);
6859255Spendry 		}
6959255Spendry 	}
7059255Spendry 
7159255Spendry 	/*
7259255Spendry 	 * otherwise lock the array while we call getnewvnode
7359255Spendry 	 * since that can block.
7459255Spendry 	 */
75*59457Spendry 	if (fdescvplock & FDL_LOCKED) {
7659255Spendry 		fdescvplock |= FDL_WANT;
7759255Spendry 		sleep((caddr_t) &fdescvplock, PINOD);
78*59457Spendry 		goto loop;
7959255Spendry 	}
8059255Spendry 	fdescvplock |= FDL_LOCKED;
8159255Spendry 
8259255Spendry 	error = getnewvnode(VT_UFS, mp, fdesc_vnodeop_p, vpp);
8359255Spendry 	if (error)
8459255Spendry 		goto out;
8559255Spendry 	MALLOC((*vpp)->v_data, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
8659255Spendry 	if (nvpp)
8759255Spendry 		*nvpp = *vpp;
8859255Spendry 	VTOFDESC(*vpp)->fd_type = ftype;
8959255Spendry 	VTOFDESC(*vpp)->fd_fd = -1;
9059255Spendry 	VTOFDESC(*vpp)->fd_link = 0;
9159255Spendry 	VTOFDESC(*vpp)->fd_ix = ix;
9259255Spendry 
9359255Spendry out:;
9459255Spendry 	fdescvplock &= ~FDL_LOCKED;
9559255Spendry 
9659255Spendry 	if (fdescvplock & FDL_WANT) {
9759255Spendry 		fdescvplock &= ~FDL_WANT;
9859255Spendry 		wakeup((caddr_t) &fdescvplock);
9959255Spendry 	}
10059255Spendry 
10159255Spendry 	return (error);
10259255Spendry }
10359255Spendry 
10453838Spendry /*
10553838Spendry  * vp is the current namei directory
10653838Spendry  * ndp is the name to locate in that directory...
10753838Spendry  */
10855017Smckusick fdesc_lookup(ap)
10955017Smckusick 	struct vop_lookup_args /* {
11055017Smckusick 		struct vnode * a_dvp;
11155017Smckusick 		struct vnode ** a_vpp;
11255017Smckusick 		struct componentname * a_cnp;
11355017Smckusick 	} */ *ap;
11453838Spendry {
11554049Spendry 	struct vnode **vpp = ap->a_vpp;
11654049Spendry 	struct vnode *dvp = ap->a_dvp;
11753838Spendry 	char *pname;
11853838Spendry 	struct proc *p;
11953838Spendry 	int nfiles;
12053838Spendry 	unsigned fd;
12153838Spendry 	int error;
12253838Spendry 	struct vnode *fvp;
12359255Spendry 	char *ln;
12453838Spendry 
12553838Spendry #ifdef FDESC_DIAGNOSTIC
12653838Spendry 	printf("fdesc_lookup(%x)\n", ap);
12754049Spendry 	printf("fdesc_lookup(dp = %x, vpp = %x, cnp = %x)\n", dvp, vpp, ap->a_cnp);
12853838Spendry #endif
12953838Spendry 	pname = ap->a_cnp->cn_nameptr;
13053838Spendry #ifdef FDESC_DIAGNOSTIC
13153838Spendry 	printf("fdesc_lookup(%s)\n", pname);
13253838Spendry #endif
13353838Spendry 	if (ap->a_cnp->cn_namelen == 1 && *pname == '.') {
13454049Spendry 		*vpp = dvp;
13554049Spendry 		VREF(dvp);
13659255Spendry 		VOP_LOCK(dvp);
13753838Spendry 		return (0);
13853838Spendry 	}
13953838Spendry 
14053838Spendry 	p = ap->a_cnp->cn_proc;
14153838Spendry 	nfiles = p->p_fd->fd_nfiles;
14253838Spendry 
14359255Spendry 	switch (VTOFDESC(dvp)->fd_type) {
14459255Spendry 	default:
14559255Spendry 	case Flink:
14659255Spendry 	case Fdesc:
14759255Spendry 	case Fctty:
14859255Spendry 		error = ENOTDIR;
14959255Spendry 		goto bad;
15059255Spendry 
15159255Spendry 	case Froot:
15259255Spendry 		if (ap->a_cnp->cn_namelen == 2 && bcmp(pname, "fd", 2) == 0) {
15359255Spendry 			error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
15459255Spendry 			if (error)
15559255Spendry 				goto bad;
15659255Spendry 			*vpp = fvp;
15759255Spendry 			fvp->v_type = VDIR;
15859255Spendry 			VOP_LOCK(fvp);
15959255Spendry #ifdef FDESC_DIAGNOSTIC
16059255Spendry 			printf("fdesc_lookup: newvp = %x\n", fvp);
16159255Spendry #endif
16259255Spendry 			return (0);
16359255Spendry 		}
16459255Spendry 
16559255Spendry 		if (ap->a_cnp->cn_namelen == 3 && bcmp(pname, "tty", 3) == 0) {
16659255Spendry 			struct vnode *ttyvp = cttyvp(p);
16759255Spendry 			if (ttyvp == NULL) {
16859255Spendry 				error = ENXIO;
16959255Spendry 				goto bad;
17059255Spendry 			}
17159255Spendry 			error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
17259255Spendry 			if (error)
17359255Spendry 				goto bad;
17459255Spendry 			*vpp = fvp;
17559255Spendry 			fvp->v_type = VFIFO;
17659255Spendry 			VOP_LOCK(fvp);
17759255Spendry #ifdef FDESC_DIAGNOSTIC
17859255Spendry 			printf("fdesc_lookup: ttyvp = %x\n", fvp);
17959255Spendry #endif
18059255Spendry 			return (0);
18159255Spendry 		}
18259255Spendry 
18359255Spendry 		ln = 0;
18459255Spendry 		switch (ap->a_cnp->cn_namelen) {
18559255Spendry 		case 5:
18659255Spendry 			if (bcmp(pname, "stdin", 5) == 0) {
18759255Spendry 				ln = "fd/0";
18859255Spendry 				fd = FD_STDIN;
18959255Spendry 			}
19053838Spendry 			break;
19159255Spendry 		case 6:
19259255Spendry 			if (bcmp(pname, "stdout", 6) == 0) {
19359255Spendry 				ln = "fd/1";
19459255Spendry 				fd = FD_STDOUT;
19559255Spendry 			} else
19659255Spendry 			if (bcmp(pname, "stderr", 6) == 0) {
19759255Spendry 				ln = "fd/2";
19859255Spendry 				fd = FD_STDERR;
19959255Spendry 			}
20059255Spendry 			break;
20159255Spendry 		}
20253838Spendry 
20359255Spendry 		if (ln) {
20453838Spendry #ifdef FDESC_DIAGNOSTIC
20559255Spendry 			printf("fdesc_lookup: link -> %s\n", ln);
20653838Spendry #endif
20759255Spendry 			error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
20859255Spendry 			if (error)
20959255Spendry 				goto bad;
21059255Spendry 			VTOFDESC(fvp)->fd_link = ln;
21159255Spendry 			*vpp = fvp;
21259255Spendry 			fvp->v_type = VLNK;
21359255Spendry 			VOP_LOCK(fvp);
21459255Spendry #ifdef FDESC_DIAGNOSTIC
21559255Spendry 			printf("fdesc_lookup: newvp = %x\n", fvp);
21659255Spendry #endif
21759255Spendry 			return (0);
21859255Spendry 		} else {
21959255Spendry 			error = ENOENT;
22059255Spendry 			goto bad;
22159255Spendry 		}
22253838Spendry 
22359255Spendry 		/* fall through */
22453838Spendry 
22559255Spendry 	case Fdevfd:
22659255Spendry 		if (ap->a_cnp->cn_namelen == 2 && bcmp(pname, "..", 2) == 0) {
22759255Spendry 			error = fdesc_root(dvp->v_mount, vpp);
22859255Spendry 			return (error);
22959255Spendry 		}
23059255Spendry 
23159255Spendry 		fd = 0;
23259255Spendry 		while (*pname >= '0' && *pname <= '9') {
23359255Spendry 			fd = 10 * fd + *pname++ - '0';
23459255Spendry 			if (fd >= nfiles)
23559255Spendry 				break;
23659255Spendry 		}
23759255Spendry 
23853838Spendry #ifdef FDESC_DIAGNOSTIC
23959255Spendry 		printf("fdesc_lookup: fd = %d, *pname = %x\n", fd, *pname);
24053838Spendry #endif
24159255Spendry 		if (*pname != '\0') {
24259255Spendry 			error = ENOENT;
24359255Spendry 			goto bad;
24459255Spendry 		}
24559255Spendry 
24659255Spendry 		if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
24759255Spendry 			error = EBADF;
24859255Spendry 			goto bad;
24959255Spendry 		}
25059255Spendry 
25153838Spendry #ifdef FDESC_DIAGNOSTIC
25259255Spendry 		printf("fdesc_lookup: allocate new vnode\n");
25353838Spendry #endif
25459255Spendry 		error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
25559255Spendry 		if (error)
25659255Spendry 			goto bad;
25759255Spendry 		VTOFDESC(fvp)->fd_fd = fd;
25859255Spendry 		*vpp = fvp;
25959255Spendry #ifdef FDESC_DIAGNOSTIC
26059255Spendry 		printf("fdesc_lookup: newvp = %x\n", fvp);
26159255Spendry #endif
26259255Spendry 		return (0);
26359255Spendry 	}
26453838Spendry 
26553838Spendry bad:;
26654049Spendry 	*vpp = NULL;
26753838Spendry #ifdef FDESC_DIAGNOSTIC
26853838Spendry 	printf("fdesc_lookup: error = %d\n", error);
26953838Spendry #endif
27053838Spendry 	return (error);
27153838Spendry }
27253838Spendry 
27355017Smckusick fdesc_open(ap)
27455017Smckusick 	struct vop_open_args /* {
27555017Smckusick 		struct vnode *a_vp;
27655017Smckusick 		int  a_mode;
27755017Smckusick 		struct ucred *a_cred;
27855017Smckusick 		struct proc *a_p;
27955017Smckusick 	} */ *ap;
28053838Spendry {
28154049Spendry 	struct vnode *vp = ap->a_vp;
28259255Spendry 	int error = 0;
28354049Spendry 
28459255Spendry 	switch (VTOFDESC(vp)->fd_type) {
28559255Spendry 	case Fdesc:
28659255Spendry 		/*
28759255Spendry 		 * XXX Kludge: set p->p_dupfd to contain the value of the
28859255Spendry 		 * the file descriptor being sought for duplication. The error
28959255Spendry 		 * return ensures that the vnode for this device will be
29059255Spendry 		 * released by vn_open. Open will detect this special error and
29159255Spendry 		 * take the actions in dupfdopen.  Other callers of vn_open or
29259255Spendry 		 * VOP_OPEN will simply report the error.
29359255Spendry 		 */
29459255Spendry 		ap->a_p->p_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
29559255Spendry 		error = ENODEV;
29659255Spendry 		break;
29753838Spendry 
29859255Spendry 	case Fctty:
29959255Spendry 		error = cttyopen(devctty, ap->a_mode, 0, ap->a_p);
30059255Spendry 		break;
30159255Spendry 	}
30259255Spendry 
30359255Spendry 	return (error);
30453838Spendry }
30553838Spendry 
30653838Spendry static int
30753838Spendry fdesc_attr(fd, vap, cred, p)
30853838Spendry 	int fd;
30953838Spendry 	struct vattr *vap;
31053838Spendry 	struct ucred *cred;
31153838Spendry 	struct proc *p;
31253838Spendry {
31353838Spendry 	struct filedesc *fdp = p->p_fd;
31453838Spendry 	struct file *fp;
31553838Spendry 	int error;
31653838Spendry 
31753838Spendry #ifdef FDESC_DIAGNOSTIC
31853838Spendry 	printf("fdesc_attr: fd = %d, nfiles = %d\n", fd, fdp->fd_nfiles);
31953838Spendry #endif
32053838Spendry 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
32153838Spendry #ifdef FDESC_DIAGNOSTIC
32253838Spendry 		printf("fdesc_attr: fp = %x (EBADF)\n", fp);
32353838Spendry #endif
32453838Spendry 		return (EBADF);
32553838Spendry 	}
32653838Spendry 
32753838Spendry 	/*
32853838Spendry 	 * Can stat the underlying vnode, but not sockets because
32953838Spendry 	 * they don't use struct vattrs.  Well, we could convert from
33053838Spendry 	 * a struct stat back to a struct vattr, later...
33153838Spendry 	 */
33253838Spendry 	switch (fp->f_type) {
33353838Spendry 	case DTYPE_VNODE:
33453838Spendry 		error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
33553838Spendry 		break;
33653838Spendry 
33753838Spendry 	case DTYPE_SOCKET:
33853838Spendry 		error = EOPNOTSUPP;
33953838Spendry 		break;
34053838Spendry 
34153838Spendry 	default:
34253838Spendry 		panic("fdesc attr");
34353838Spendry 		break;
34453838Spendry 	}
34553838Spendry 
34653838Spendry #ifdef FDESC_DIAGNOSTIC
34753838Spendry 	printf("fdesc_attr: returns error %d\n", error);
34853838Spendry #endif
34953838Spendry 	return (error);
35053838Spendry }
35153838Spendry 
35255017Smckusick fdesc_getattr(ap)
35355017Smckusick 	struct vop_getattr_args /* {
35455017Smckusick 		struct vnode *a_vp;
35555017Smckusick 		struct vattr *a_vap;
35655017Smckusick 		struct ucred *a_cred;
35755017Smckusick 		struct proc *a_p;
35855017Smckusick 	} */ *ap;
35953838Spendry {
36054049Spendry 	struct vnode *vp = ap->a_vp;
36154049Spendry 	struct vattr *vap = ap->a_vap;
36253838Spendry 	unsigned fd;
36353838Spendry 	int error;
36453838Spendry 
36553838Spendry #ifdef FDESC_DIAGNOSTIC
36659255Spendry 	printf("fdesc_getattr: stat type = %d\n", VTOFDESC(vp)->fd_type);
36753838Spendry #endif
36859255Spendry 
36959255Spendry 	switch (VTOFDESC(vp)->fd_type) {
37059255Spendry 	case Froot:
37159255Spendry 	case Fdevfd:
37259255Spendry 	case Flink:
37359255Spendry 	case Fctty:
37454049Spendry 		bzero((caddr_t) vap, sizeof(*vap));
37554049Spendry 		vattr_null(vap);
37659255Spendry 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
37759255Spendry 
37859255Spendry 		switch (VTOFDESC(vp)->fd_type) {
37959255Spendry 		case Flink:
38059255Spendry 			vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
38159255Spendry 			vap->va_type = VLNK;
38259255Spendry 			vap->va_nlink = 1;
38359255Spendry 			/* vap->va_qsize = strlen(VTOFDESC(vp)->fd_link); */
38459255Spendry 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
38559255Spendry 			break;
38659255Spendry 
38759255Spendry 		case Fctty:
38859255Spendry 			vap->va_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
38959255Spendry 			vap->va_type = VFIFO;
39059255Spendry 			vap->va_nlink = 1;
39159255Spendry 			/* vap->va_qsize = 0; */
39259255Spendry 			vap->va_size = 0;
39359255Spendry 			break;
39459255Spendry 
39559255Spendry 		default:
39659255Spendry 			vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
39759255Spendry 			vap->va_type = VDIR;
39859255Spendry 			vap->va_nlink = 2;
39959255Spendry 			/* vap->va_qsize = 0; */
40059255Spendry 			vap->va_size = DEV_BSIZE;
40159255Spendry 			break;
40259255Spendry 		}
40354049Spendry 		vap->va_uid = 0;
40454049Spendry 		vap->va_gid = 0;
40554049Spendry 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
40654049Spendry 		vap->va_blocksize = DEV_BSIZE;
40759255Spendry 		vap->va_atime.ts_sec = boottime.tv_sec;
40859255Spendry 		vap->va_atime.ts_nsec = 0;
40954049Spendry 		vap->va_mtime = vap->va_atime;
41054049Spendry 		vap->va_ctime = vap->va_ctime;
41154049Spendry 		vap->va_gen = 0;
41254049Spendry 		vap->va_flags = 0;
41354049Spendry 		vap->va_rdev = 0;
41454049Spendry 		/* vap->va_qbytes = 0; */
41554049Spendry 		vap->va_bytes = 0;
41659255Spendry 		break;
41759255Spendry 
41859255Spendry 	case Fdesc:
41959255Spendry #ifdef FDESC_DIAGNOSTIC
42059255Spendry 		printf("fdesc_getattr: stat desc #%d\n", VTOFDESC(vp)->fd_fd);
42159255Spendry #endif
42259255Spendry 		fd = VTOFDESC(vp)->fd_fd;
42359255Spendry 		error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
42459255Spendry 		break;
42559255Spendry 
42659255Spendry 	default:
42759255Spendry 		panic("fdesc_getattr");
42859255Spendry 		break;
42953838Spendry 	}
43053838Spendry 
43153838Spendry 	if (error == 0)
43254049Spendry 		vp->v_type = vap->va_type;
43359255Spendry 
43459255Spendry #ifdef FDESC_DIAGNOSTIC
43559255Spendry 	printf("fdesc_getattr: stat returns 0\n");
43659255Spendry #endif
43753838Spendry 	return (error);
43853838Spendry }
43953838Spendry 
44055017Smckusick fdesc_setattr(ap)
44155017Smckusick 	struct vop_setattr_args /* {
44255017Smckusick 		struct vnode *a_vp;
44355017Smckusick 		struct vattr *a_vap;
44455017Smckusick 		struct ucred *a_cred;
44555017Smckusick 		struct proc *a_p;
44655017Smckusick 	} */ *ap;
44753838Spendry {
44853838Spendry 	struct filedesc *fdp = ap->a_p->p_fd;
44953838Spendry 	struct file *fp;
45053838Spendry 	unsigned fd;
45153838Spendry 	int error;
45253838Spendry 
45353838Spendry 	/*
45453838Spendry 	 * Can't mess with the root vnode
45553838Spendry 	 */
45659255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
45759255Spendry 	case Fdesc:
45859255Spendry 		break;
45959255Spendry 
46059255Spendry 	case Fctty:
46159255Spendry 		return (0);
46259255Spendry 
46359255Spendry 	default:
46453838Spendry 		return (EACCES);
46559255Spendry 	}
46653838Spendry 
46759255Spendry 	fd = VTOFDESC(ap->a_vp)->fd_fd;
46853838Spendry #ifdef FDESC_DIAGNOSTIC
46953838Spendry 	printf("fdesc_setattr: fd = %d, nfiles = %d\n", fd, fdp->fd_nfiles);
47053838Spendry #endif
47153838Spendry 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
47253838Spendry #ifdef FDESC_DIAGNOSTIC
47353838Spendry 		printf("fdesc_setattr: fp = %x (EBADF)\n", fp);
47453838Spendry #endif
47553838Spendry 		return (EBADF);
47653838Spendry 	}
47753838Spendry 
47853838Spendry 	/*
47953838Spendry 	 * Can setattr the underlying vnode, but not sockets!
48053838Spendry 	 */
48153838Spendry 	switch (fp->f_type) {
48253838Spendry 	case DTYPE_VNODE:
48353838Spendry 		error = VOP_SETATTR((struct vnode *) fp->f_data, ap->a_vap, ap->a_cred, ap->a_p);
48453838Spendry 		break;
48553838Spendry 
48653838Spendry 	case DTYPE_SOCKET:
48758646Spendry 		error = 0;
48853838Spendry 		break;
48953838Spendry 
49053838Spendry 	default:
49153838Spendry 		panic("fdesc setattr");
49253838Spendry 		break;
49353838Spendry 	}
49453838Spendry 
49553838Spendry #ifdef FDESC_DIAGNOSTIC
49653838Spendry 	printf("fdesc_setattr: returns error %d\n", error);
49753838Spendry #endif
49853838Spendry 	return (error);
49953838Spendry }
50053838Spendry 
50159255Spendry #define UIO_MX 16
50259255Spendry 
50359255Spendry static struct dirtmp {
50459255Spendry 	u_long d_fileno;
50559255Spendry 	u_short d_reclen;
50659255Spendry 	u_short d_namlen;
50759255Spendry 	char d_name[8];
50859255Spendry } rootent[] = {
50959255Spendry 	{ FD_DEVFD, UIO_MX, 2, "fd" },
51059255Spendry 	{ FD_STDIN, UIO_MX, 5, "stdin" },
51159255Spendry 	{ FD_STDOUT, UIO_MX, 6, "stdout" },
51259255Spendry 	{ FD_STDERR, UIO_MX, 6, "stderr" },
51359255Spendry 	{ FD_CTTY, UIO_MX, 3, "tty" },
51459255Spendry 	{ 0 }
51559255Spendry };
51659255Spendry 
51755017Smckusick fdesc_readdir(ap)
51855017Smckusick 	struct vop_readdir_args /* {
51955017Smckusick 		struct vnode *a_vp;
52055017Smckusick 		struct uio *a_uio;
52155017Smckusick 		struct ucred *a_cred;
52255017Smckusick 	} */ *ap;
52353838Spendry {
52454049Spendry 	struct uio *uio = ap->a_uio;
52553838Spendry 	struct filedesc *fdp;
52653838Spendry 	int i;
52753838Spendry 	int error;
52853838Spendry 
52959255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
53059255Spendry 	case Fctty:
53159255Spendry 		return (0);
53253838Spendry 
53359255Spendry 	case Fdesc:
53459255Spendry 		return (ENOTDIR);
53559255Spendry 
53659255Spendry 	default:
53759255Spendry 		break;
53859255Spendry 	}
53959255Spendry 
54054049Spendry 	fdp = uio->uio_procp->p_fd;
54159255Spendry 
54259255Spendry 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
54359255Spendry 		struct dirent d;
54459255Spendry 		struct dirent *dp = &d;
54559255Spendry 		struct dirtmp *dt;
54659255Spendry 
54759255Spendry 		i = uio->uio_offset / UIO_MX;
54859255Spendry 		error = 0;
54959255Spendry 
55059255Spendry 		while (uio->uio_resid > 0) {
55159255Spendry 			dt = &rootent[i];
55259255Spendry 			if (dt->d_fileno == 0) {
55359255Spendry 				/**eofflagp = 1;*/
55459255Spendry 				break;
55559255Spendry 			}
55659255Spendry 			i++;
55759255Spendry 
55859255Spendry 			switch (dt->d_fileno) {
55959255Spendry 			case FD_CTTY:
56059255Spendry 				if (cttyvp(uio->uio_procp) == NULL)
56159255Spendry 					continue;
56259255Spendry 				break;
56359255Spendry 
56459255Spendry 			case FD_STDIN:
56559255Spendry 			case FD_STDOUT:
56659255Spendry 			case FD_STDERR:
56759255Spendry 				if ((i-FD_STDIN) >= fdp->fd_nfiles)
56859255Spendry 					continue;
56959255Spendry 				if (fdp->fd_ofiles[i-FD_STDIN] == NULL)
57059255Spendry 					continue;
57159255Spendry 				break;
57259255Spendry 			}
57359255Spendry 			bzero(dp, UIO_MX);
57459255Spendry 			dp->d_fileno = dt->d_fileno;
57559255Spendry 			dp->d_namlen = dt->d_namlen;
57659255Spendry 			dp->d_type = DT_UNKNOWN;
57759255Spendry 			dp->d_reclen = dt->d_reclen;
57859255Spendry 			bcopy(dt->d_name, dp->d_name, dp->d_namlen+1);
57959255Spendry 			error = uiomove((caddr_t) dp, UIO_MX, uio);
58059255Spendry 			if (error)
58159255Spendry 				break;
58259255Spendry 		}
58359255Spendry 		uio->uio_offset = i * UIO_MX;
58459255Spendry 		return (error);
58559255Spendry 	}
58659255Spendry 
58754049Spendry 	i = uio->uio_offset / UIO_MX;
58853838Spendry 	error = 0;
58954049Spendry 	while (uio->uio_resid > 0) {
59053838Spendry 		if (i >= fdp->fd_nfiles) {
59154980Spendry 			/* *ap->a_eofflagp = 1; */
59253838Spendry 			break;
59353838Spendry 		}
59453838Spendry 		if (fdp->fd_ofiles[i] != NULL) {
59554981Spendry 			struct dirent d;
59654981Spendry 			struct dirent *dp = &d;
59753838Spendry 			char *cp = dp->d_name;
59853838Spendry 			bzero((caddr_t) dp, UIO_MX);
59953838Spendry 
60053838Spendry 			dp->d_namlen = sprintf(dp->d_name, "%d", i);
60153838Spendry 			/*
60253838Spendry 			 * Fill in the remaining fields
60353838Spendry 			 */
60453838Spendry 			dp->d_reclen = UIO_MX;
60554981Spendry 			dp->d_type = DT_UNKNOWN;
60659255Spendry 			dp->d_fileno = i + FD_STDIN;
60753838Spendry 			/*
60853838Spendry 			 * And ship to userland
60953838Spendry 			 */
61054049Spendry 			error = uiomove((caddr_t) dp, UIO_MX, uio);
61153838Spendry 			if (error)
61253838Spendry 				break;
61353838Spendry 		}
61453838Spendry 		i++;
61553838Spendry 	}
61653838Spendry 
61754049Spendry 	uio->uio_offset = i * UIO_MX;
61853838Spendry 	return (error);
61953838Spendry }
62053838Spendry 
62159255Spendry int
62259255Spendry fdesc_readlink(ap)
62359255Spendry 	struct vop_readlink_args /* {
62459255Spendry 		struct vnode *a_vp;
62559255Spendry 		struct uio *a_uio;
62659255Spendry 		struct ucred *a_cred;
62759255Spendry 	} */ *ap;
62859255Spendry {
62959255Spendry 	register struct vnode *vp = ap->a_vp;
63059255Spendry 	int error;
63159255Spendry 
63259255Spendry 	if (vp->v_type != VLNK)
63359255Spendry 		return (EPERM);
63459255Spendry 
63559255Spendry 	if (VTOFDESC(vp)->fd_type == Flink) {
63659255Spendry 		char *ln = VTOFDESC(vp)->fd_link;
63759255Spendry 		error = uiomove(ln, strlen(ln), ap->a_uio);
63859255Spendry 	} else {
63959255Spendry 		error = EOPNOTSUPP;
64059255Spendry 	}
64159255Spendry 
64259255Spendry 	return (error);
64359255Spendry }
64459255Spendry 
64559255Spendry fdesc_read(ap)
64659255Spendry 	struct vop_read_args /* {
64759255Spendry 		struct vnode *a_vp;
64859255Spendry 		struct uio *a_uio;
64959255Spendry 		int  a_ioflag;
65059255Spendry 		struct ucred *a_cred;
65159255Spendry 	} */ *ap;
65259255Spendry {
65359255Spendry 	int error = EOPNOTSUPP;
65459255Spendry 
65559255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
65659255Spendry 	case Fctty:
65759255Spendry 		error = cttyread(devctty, ap->a_uio, ap->a_ioflag);
65859255Spendry 		break;
65959255Spendry 
66059255Spendry 	default:
66159255Spendry 		error = EOPNOTSUPP;
66259255Spendry 		break;
66359255Spendry 	}
66459255Spendry 
66559255Spendry 	return (error);
66659255Spendry }
66759255Spendry 
66859255Spendry fdesc_write(ap)
66959255Spendry 	struct vop_write_args /* {
67059255Spendry 		struct vnode *a_vp;
67159255Spendry 		struct uio *a_uio;
67259255Spendry 		int  a_ioflag;
67359255Spendry 		struct ucred *a_cred;
67459255Spendry 	} */ *ap;
67559255Spendry {
67659255Spendry 	int error = EOPNOTSUPP;
67759255Spendry 
67859255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
67959255Spendry 	case Fctty:
68059255Spendry 		error = cttywrite(devctty, ap->a_uio, ap->a_ioflag);
68159255Spendry 		break;
68259255Spendry 
68359255Spendry 	default:
68459255Spendry 		error = EOPNOTSUPP;
68559255Spendry 		break;
68659255Spendry 	}
68759255Spendry 
68859255Spendry 	return (error);
68959255Spendry }
69059255Spendry 
69159255Spendry fdesc_ioctl(ap)
69259255Spendry 	struct vop_ioctl_args /* {
69359255Spendry 		struct vnode *a_vp;
69459255Spendry 		int  a_command;
69559255Spendry 		caddr_t  a_data;
69659255Spendry 		int  a_fflag;
69759255Spendry 		struct ucred *a_cred;
69859255Spendry 		struct proc *a_p;
69959255Spendry 	} */ *ap;
70059255Spendry {
70159255Spendry 	int error = EOPNOTSUPP;
70259255Spendry 
70359255Spendry #ifdef FDESC_DIAGNOSTIC
70459255Spendry 	printf("fdesc_ioctl: type = %d, command = %x\n",
70559255Spendry 			VTOFDESC(ap->a_vp)->fd_type, ap->a_command);
70659255Spendry #endif
70759255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
70859255Spendry 	case Fctty:
70959255Spendry 		error = cttyioctl(devctty, ap->a_command, ap->a_data,
71059255Spendry 					ap->a_fflag, ap->a_p);
71159255Spendry 		break;
71259255Spendry 
71359255Spendry 	default:
71459255Spendry 		error = EOPNOTSUPP;
71559255Spendry 		break;
71659255Spendry 	}
71759255Spendry 
71859255Spendry 	return (error);
71959255Spendry }
72059255Spendry 
72159255Spendry fdesc_select(ap)
72259255Spendry 	struct vop_select_args /* {
72359255Spendry 		struct vnode *a_vp;
72459255Spendry 		int  a_which;
72559255Spendry 		int  a_fflags;
72659255Spendry 		struct ucred *a_cred;
72759255Spendry 		struct proc *a_p;
72859255Spendry 	} */ *ap;
72959255Spendry {
73059255Spendry 	int error = EOPNOTSUPP;
73159255Spendry 
73259255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
73359255Spendry 	case Fctty:
73459255Spendry 		error = cttyselect(devctty, ap->a_fflags, ap->a_p);
73559255Spendry 		break;
73659255Spendry 
73759255Spendry 	default:
73859255Spendry 		error = EOPNOTSUPP;
73959255Spendry 		break;
74059255Spendry 	}
74159255Spendry 
74259255Spendry 	return (error);
74359255Spendry }
74459255Spendry 
74555017Smckusick fdesc_inactive(ap)
74655017Smckusick 	struct vop_inactive_args /* {
74755017Smckusick 		struct vnode *a_vp;
74855017Smckusick 	} */ *ap;
74953838Spendry {
75054049Spendry 	struct vnode *vp = ap->a_vp;
75154049Spendry 
75253838Spendry 	/*
75353838Spendry 	 * Clear out the v_type field to avoid
75453838Spendry 	 * nasty things happening in vgone().
75553838Spendry 	 */
75654049Spendry 	vp->v_type = VNON;
75753838Spendry #ifdef FDESC_DIAGNOSTIC
75854049Spendry 	printf("fdesc_inactive(%x)\n", vp);
75953838Spendry #endif
76053838Spendry 	return (0);
76153838Spendry }
76253838Spendry 
76355017Smckusick fdesc_reclaim(ap)
76455017Smckusick 	struct vop_reclaim_args /* {
76555017Smckusick 		struct vnode *a_vp;
76655017Smckusick 	} */ *ap;
76753838Spendry {
76853838Spendry 	struct vnode *vp = ap->a_vp;
76959255Spendry 	int ix;
77059255Spendry 
77159255Spendry #ifdef FDESC_DIAGNOSTIC
77253838Spendry 	printf("fdesc_reclaim(%x)\n", vp);
77359255Spendry #endif
77459255Spendry 	ix = VTOFDESC(vp)->fd_ix;
77559255Spendry 	if (ix >= 0 && ix < FD_MAX) {
77659255Spendry 		if (fdescvp[ix] != vp)
77759255Spendry 			panic("fdesc_reclaim");
77859255Spendry 		fdescvp[ix] = 0;
77959255Spendry 	}
78053838Spendry 	if (vp->v_data) {
78153838Spendry 		FREE(vp->v_data, M_TEMP);
78253838Spendry 		vp->v_data = 0;
78353838Spendry 	}
78453838Spendry 	return (0);
78553838Spendry }
78653838Spendry 
78753838Spendry /*
78853838Spendry  * Print out the contents of a /dev/fd vnode.
78953838Spendry  */
79053838Spendry /* ARGSUSED */
79155017Smckusick fdesc_print(ap)
79255017Smckusick 	struct vop_print_args /* {
79355017Smckusick 		struct vnode *a_vp;
79455017Smckusick 	} */ *ap;
79553838Spendry {
79655017Smckusick 
79753838Spendry 	printf("tag VT_NON, fdesc vnode\n");
79855017Smckusick 	return (0);
79953838Spendry }
80053838Spendry 
80153838Spendry /*void*/
80255017Smckusick fdesc_vfree(ap)
80355017Smckusick 	struct vop_vfree_args /* {
80455017Smckusick 		struct vnode *a_pvp;
80555017Smckusick 		ino_t a_ino;
80655017Smckusick 		int a_mode;
80755017Smckusick 	} */ *ap;
80853838Spendry {
80953838Spendry 
81055017Smckusick 	return (0);
81153838Spendry }
81253838Spendry 
81353838Spendry /*
81453838Spendry  * /dev/fd vnode unsupported operation
81553838Spendry  */
81653838Spendry fdesc_enotsupp()
81753838Spendry {
81855017Smckusick 
81953838Spendry 	return (EOPNOTSUPP);
82053838Spendry }
82153838Spendry 
82253838Spendry /*
82353838Spendry  * /dev/fd "should never get here" operation
82453838Spendry  */
82553838Spendry fdesc_badop()
82653838Spendry {
82755017Smckusick 
82853838Spendry 	panic("fdesc: bad op");
82953838Spendry 	/* NOTREACHED */
83053838Spendry }
83153838Spendry 
83253838Spendry /*
83353838Spendry  * /dev/fd vnode null operation
83453838Spendry  */
83553838Spendry fdesc_nullop()
83653838Spendry {
83755017Smckusick 
83853838Spendry 	return (0);
83953838Spendry }
84053838Spendry 
84153838Spendry #define fdesc_create ((int (*) __P((struct  vop_create_args *)))fdesc_enotsupp)
84253838Spendry #define fdesc_mknod ((int (*) __P((struct  vop_mknod_args *)))fdesc_enotsupp)
84353838Spendry #define fdesc_close ((int (*) __P((struct  vop_close_args *)))nullop)
84453838Spendry #define fdesc_access ((int (*) __P((struct  vop_access_args *)))nullop)
84553838Spendry #define fdesc_mmap ((int (*) __P((struct  vop_mmap_args *)))fdesc_enotsupp)
84653838Spendry #define fdesc_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)
84753838Spendry #define fdesc_seek ((int (*) __P((struct  vop_seek_args *)))nullop)
84853838Spendry #define fdesc_remove ((int (*) __P((struct  vop_remove_args *)))fdesc_enotsupp)
84953838Spendry #define fdesc_link ((int (*) __P((struct  vop_link_args *)))fdesc_enotsupp)
85053838Spendry #define fdesc_rename ((int (*) __P((struct  vop_rename_args *)))fdesc_enotsupp)
85153838Spendry #define fdesc_mkdir ((int (*) __P((struct  vop_mkdir_args *)))fdesc_enotsupp)
85253838Spendry #define fdesc_rmdir ((int (*) __P((struct  vop_rmdir_args *)))fdesc_enotsupp)
85355017Smckusick #define fdesc_symlink ((int (*) __P((struct vop_symlink_args *)))fdesc_enotsupp)
85453838Spendry #define fdesc_abortop ((int (*) __P((struct  vop_abortop_args *)))nullop)
85553838Spendry #define fdesc_lock ((int (*) __P((struct  vop_lock_args *)))nullop)
85653838Spendry #define fdesc_unlock ((int (*) __P((struct  vop_unlock_args *)))nullop)
85753838Spendry #define fdesc_bmap ((int (*) __P((struct  vop_bmap_args *)))fdesc_badop)
85853838Spendry #define fdesc_strategy ((int (*) __P((struct  vop_strategy_args *)))fdesc_badop)
85953838Spendry #define fdesc_islocked ((int (*) __P((struct  vop_islocked_args *)))nullop)
86055017Smckusick #define fdesc_advlock ((int (*) __P((struct vop_advlock_args *)))fdesc_enotsupp)
86155170Spendry #define fdesc_blkatoff \
86255017Smckusick 	((int (*) __P((struct  vop_blkatoff_args *)))fdesc_enotsupp)
86353838Spendry #define fdesc_vget ((int (*) __P((struct  vop_vget_args *)))fdesc_enotsupp)
86453838Spendry #define fdesc_valloc ((int(*) __P(( \
86553838Spendry 		struct vnode *pvp, \
86653838Spendry 		int mode, \
86753838Spendry 		struct ucred *cred, \
86853838Spendry 		struct vnode **vpp))) fdesc_enotsupp)
86955170Spendry #define fdesc_truncate \
87055017Smckusick 	((int (*) __P((struct  vop_truncate_args *)))fdesc_enotsupp)
87153838Spendry #define fdesc_update ((int (*) __P((struct  vop_update_args *)))fdesc_enotsupp)
87253838Spendry #define fdesc_bwrite ((int (*) __P((struct  vop_bwrite_args *)))fdesc_enotsupp)
87353838Spendry 
87453838Spendry int (**fdesc_vnodeop_p)();
87553838Spendry struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
87653838Spendry 	{ &vop_default_desc, vn_default_error },
87753838Spendry 	{ &vop_lookup_desc, fdesc_lookup },	/* lookup */
87853838Spendry 	{ &vop_create_desc, fdesc_create },	/* create */
87953838Spendry 	{ &vop_mknod_desc, fdesc_mknod },	/* mknod */
88055017Smckusick 	{ &vop_open_desc, fdesc_open },		/* open */
88153838Spendry 	{ &vop_close_desc, fdesc_close },	/* close */
88253838Spendry 	{ &vop_access_desc, fdesc_access },	/* access */
88353838Spendry 	{ &vop_getattr_desc, fdesc_getattr },	/* getattr */
88453838Spendry 	{ &vop_setattr_desc, fdesc_setattr },	/* setattr */
88555017Smckusick 	{ &vop_read_desc, fdesc_read },		/* read */
88653838Spendry 	{ &vop_write_desc, fdesc_write },	/* write */
88753838Spendry 	{ &vop_ioctl_desc, fdesc_ioctl },	/* ioctl */
88853838Spendry 	{ &vop_select_desc, fdesc_select },	/* select */
88955017Smckusick 	{ &vop_mmap_desc, fdesc_mmap },		/* mmap */
89053838Spendry 	{ &vop_fsync_desc, fdesc_fsync },	/* fsync */
89155017Smckusick 	{ &vop_seek_desc, fdesc_seek },		/* seek */
89253838Spendry 	{ &vop_remove_desc, fdesc_remove },	/* remove */
89355017Smckusick 	{ &vop_link_desc, fdesc_link },		/* link */
89453838Spendry 	{ &vop_rename_desc, fdesc_rename },	/* rename */
89553838Spendry 	{ &vop_mkdir_desc, fdesc_mkdir },	/* mkdir */
89653838Spendry 	{ &vop_rmdir_desc, fdesc_rmdir },	/* rmdir */
89753838Spendry 	{ &vop_symlink_desc, fdesc_symlink },	/* symlink */
89853838Spendry 	{ &vop_readdir_desc, fdesc_readdir },	/* readdir */
89953838Spendry 	{ &vop_readlink_desc, fdesc_readlink },	/* readlink */
90053838Spendry 	{ &vop_abortop_desc, fdesc_abortop },	/* abortop */
90153838Spendry 	{ &vop_inactive_desc, fdesc_inactive },	/* inactive */
90253838Spendry 	{ &vop_reclaim_desc, fdesc_reclaim },	/* reclaim */
90355017Smckusick 	{ &vop_lock_desc, fdesc_lock },		/* lock */
90453838Spendry 	{ &vop_unlock_desc, fdesc_unlock },	/* unlock */
90555017Smckusick 	{ &vop_bmap_desc, fdesc_bmap },		/* bmap */
90653838Spendry 	{ &vop_strategy_desc, fdesc_strategy },	/* strategy */
90753838Spendry 	{ &vop_print_desc, fdesc_print },	/* print */
90853838Spendry 	{ &vop_islocked_desc, fdesc_islocked },	/* islocked */
90953838Spendry 	{ &vop_advlock_desc, fdesc_advlock },	/* advlock */
91053838Spendry 	{ &vop_blkatoff_desc, fdesc_blkatoff },	/* blkatoff */
91153838Spendry 	{ &vop_valloc_desc, fdesc_valloc },	/* valloc */
91253838Spendry 	{ &vop_vfree_desc, fdesc_vfree },	/* vfree */
91353838Spendry 	{ &vop_truncate_desc, fdesc_truncate },	/* truncate */
91453838Spendry 	{ &vop_update_desc, fdesc_update },	/* update */
91553838Spendry 	{ &vop_bwrite_desc, fdesc_bwrite },	/* bwrite */
91653838Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
91753838Spendry };
91853838Spendry struct vnodeopv_desc fdesc_vnodeop_opv_desc =
91953838Spendry 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
920