xref: /csrg-svn/sys/miscfs/fdesc/fdesc_vnops.c (revision 59561)
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*59561Spendry  *	@(#)fdesc_vnops.c	7.7 (Berkeley) 04/30/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 
5859457Spendry loop:
5959255Spendry 	/* get stashed copy of the vnode */
6059255Spendry 	if (ix >= 0 && ix < FD_MAX) {
6159255Spendry 		nvpp = &fdescvp[ix];
6259457Spendry 		if (*nvpp) {
6359457Spendry 			if (vget(*nvpp))
6459457Spendry 				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 	 */
7559457Spendry 	if (fdescvplock & FDL_LOCKED) {
7659255Spendry 		fdescvplock |= FDL_WANT;
7759255Spendry 		sleep((caddr_t) &fdescvplock, PINOD);
7859457Spendry 		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);
335*59561Spendry 		if (error == 0 && vap->va_type == VDIR) {
336*59561Spendry 			/*
337*59561Spendry 			 * don't allow directories to show up because
338*59561Spendry 			 * that causes loops in the namespace.
339*59561Spendry 			 */
340*59561Spendry 			vap->va_type = VFIFO;
341*59561Spendry 		}
34253838Spendry 		break;
34353838Spendry 
34453838Spendry 	case DTYPE_SOCKET:
34553838Spendry 		error = EOPNOTSUPP;
34653838Spendry 		break;
34753838Spendry 
34853838Spendry 	default:
34953838Spendry 		panic("fdesc attr");
35053838Spendry 		break;
35153838Spendry 	}
35253838Spendry 
35353838Spendry #ifdef FDESC_DIAGNOSTIC
35453838Spendry 	printf("fdesc_attr: returns error %d\n", error);
35553838Spendry #endif
35653838Spendry 	return (error);
35753838Spendry }
35853838Spendry 
35955017Smckusick fdesc_getattr(ap)
36055017Smckusick 	struct vop_getattr_args /* {
36155017Smckusick 		struct vnode *a_vp;
36255017Smckusick 		struct vattr *a_vap;
36355017Smckusick 		struct ucred *a_cred;
36455017Smckusick 		struct proc *a_p;
36555017Smckusick 	} */ *ap;
36653838Spendry {
36754049Spendry 	struct vnode *vp = ap->a_vp;
36854049Spendry 	struct vattr *vap = ap->a_vap;
36953838Spendry 	unsigned fd;
37053838Spendry 	int error;
37153838Spendry 
37253838Spendry #ifdef FDESC_DIAGNOSTIC
37359255Spendry 	printf("fdesc_getattr: stat type = %d\n", VTOFDESC(vp)->fd_type);
37453838Spendry #endif
37559255Spendry 
37659255Spendry 	switch (VTOFDESC(vp)->fd_type) {
37759255Spendry 	case Froot:
37859255Spendry 	case Fdevfd:
37959255Spendry 	case Flink:
38059255Spendry 	case Fctty:
38154049Spendry 		bzero((caddr_t) vap, sizeof(*vap));
38254049Spendry 		vattr_null(vap);
38359255Spendry 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
38459255Spendry 
38559255Spendry 		switch (VTOFDESC(vp)->fd_type) {
38659255Spendry 		case Flink:
38759255Spendry 			vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
38859255Spendry 			vap->va_type = VLNK;
38959255Spendry 			vap->va_nlink = 1;
39059255Spendry 			/* vap->va_qsize = strlen(VTOFDESC(vp)->fd_link); */
39159255Spendry 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
39259255Spendry 			break;
39359255Spendry 
39459255Spendry 		case Fctty:
39559255Spendry 			vap->va_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
39659255Spendry 			vap->va_type = VFIFO;
39759255Spendry 			vap->va_nlink = 1;
39859255Spendry 			/* vap->va_qsize = 0; */
39959255Spendry 			vap->va_size = 0;
40059255Spendry 			break;
40159255Spendry 
40259255Spendry 		default:
40359255Spendry 			vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
40459255Spendry 			vap->va_type = VDIR;
40559255Spendry 			vap->va_nlink = 2;
40659255Spendry 			/* vap->va_qsize = 0; */
40759255Spendry 			vap->va_size = DEV_BSIZE;
40859255Spendry 			break;
40959255Spendry 		}
41054049Spendry 		vap->va_uid = 0;
41154049Spendry 		vap->va_gid = 0;
41254049Spendry 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
41354049Spendry 		vap->va_blocksize = DEV_BSIZE;
41459255Spendry 		vap->va_atime.ts_sec = boottime.tv_sec;
41559255Spendry 		vap->va_atime.ts_nsec = 0;
41654049Spendry 		vap->va_mtime = vap->va_atime;
41754049Spendry 		vap->va_ctime = vap->va_ctime;
41854049Spendry 		vap->va_gen = 0;
41954049Spendry 		vap->va_flags = 0;
42054049Spendry 		vap->va_rdev = 0;
42154049Spendry 		/* vap->va_qbytes = 0; */
42254049Spendry 		vap->va_bytes = 0;
42359255Spendry 		break;
42459255Spendry 
42559255Spendry 	case Fdesc:
42659255Spendry #ifdef FDESC_DIAGNOSTIC
42759255Spendry 		printf("fdesc_getattr: stat desc #%d\n", VTOFDESC(vp)->fd_fd);
42859255Spendry #endif
42959255Spendry 		fd = VTOFDESC(vp)->fd_fd;
43059255Spendry 		error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
43159255Spendry 		break;
43259255Spendry 
43359255Spendry 	default:
43459255Spendry 		panic("fdesc_getattr");
43559255Spendry 		break;
43653838Spendry 	}
43753838Spendry 
43853838Spendry 	if (error == 0)
43954049Spendry 		vp->v_type = vap->va_type;
44059255Spendry 
44159255Spendry #ifdef FDESC_DIAGNOSTIC
44259255Spendry 	printf("fdesc_getattr: stat returns 0\n");
44359255Spendry #endif
44453838Spendry 	return (error);
44553838Spendry }
44653838Spendry 
44755017Smckusick fdesc_setattr(ap)
44855017Smckusick 	struct vop_setattr_args /* {
44955017Smckusick 		struct vnode *a_vp;
45055017Smckusick 		struct vattr *a_vap;
45155017Smckusick 		struct ucred *a_cred;
45255017Smckusick 		struct proc *a_p;
45355017Smckusick 	} */ *ap;
45453838Spendry {
45553838Spendry 	struct filedesc *fdp = ap->a_p->p_fd;
45653838Spendry 	struct file *fp;
45753838Spendry 	unsigned fd;
45853838Spendry 	int error;
45953838Spendry 
46053838Spendry 	/*
46153838Spendry 	 * Can't mess with the root vnode
46253838Spendry 	 */
46359255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
46459255Spendry 	case Fdesc:
46559255Spendry 		break;
46659255Spendry 
46759255Spendry 	case Fctty:
46859255Spendry 		return (0);
46959255Spendry 
47059255Spendry 	default:
47153838Spendry 		return (EACCES);
47259255Spendry 	}
47353838Spendry 
47459255Spendry 	fd = VTOFDESC(ap->a_vp)->fd_fd;
47553838Spendry #ifdef FDESC_DIAGNOSTIC
47653838Spendry 	printf("fdesc_setattr: fd = %d, nfiles = %d\n", fd, fdp->fd_nfiles);
47753838Spendry #endif
47853838Spendry 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
47953838Spendry #ifdef FDESC_DIAGNOSTIC
48053838Spendry 		printf("fdesc_setattr: fp = %x (EBADF)\n", fp);
48153838Spendry #endif
48253838Spendry 		return (EBADF);
48353838Spendry 	}
48453838Spendry 
48553838Spendry 	/*
48653838Spendry 	 * Can setattr the underlying vnode, but not sockets!
48753838Spendry 	 */
48853838Spendry 	switch (fp->f_type) {
48953838Spendry 	case DTYPE_VNODE:
49053838Spendry 		error = VOP_SETATTR((struct vnode *) fp->f_data, ap->a_vap, ap->a_cred, ap->a_p);
49153838Spendry 		break;
49253838Spendry 
49353838Spendry 	case DTYPE_SOCKET:
49458646Spendry 		error = 0;
49553838Spendry 		break;
49653838Spendry 
49753838Spendry 	default:
49853838Spendry 		panic("fdesc setattr");
49953838Spendry 		break;
50053838Spendry 	}
50153838Spendry 
50253838Spendry #ifdef FDESC_DIAGNOSTIC
50353838Spendry 	printf("fdesc_setattr: returns error %d\n", error);
50453838Spendry #endif
50553838Spendry 	return (error);
50653838Spendry }
50753838Spendry 
50859255Spendry #define UIO_MX 16
50959255Spendry 
51059255Spendry static struct dirtmp {
51159255Spendry 	u_long d_fileno;
51259255Spendry 	u_short d_reclen;
51359255Spendry 	u_short d_namlen;
51459255Spendry 	char d_name[8];
51559255Spendry } rootent[] = {
51659255Spendry 	{ FD_DEVFD, UIO_MX, 2, "fd" },
51759255Spendry 	{ FD_STDIN, UIO_MX, 5, "stdin" },
51859255Spendry 	{ FD_STDOUT, UIO_MX, 6, "stdout" },
51959255Spendry 	{ FD_STDERR, UIO_MX, 6, "stderr" },
52059255Spendry 	{ FD_CTTY, UIO_MX, 3, "tty" },
52159255Spendry 	{ 0 }
52259255Spendry };
52359255Spendry 
52455017Smckusick fdesc_readdir(ap)
52555017Smckusick 	struct vop_readdir_args /* {
52655017Smckusick 		struct vnode *a_vp;
52755017Smckusick 		struct uio *a_uio;
52855017Smckusick 		struct ucred *a_cred;
52955017Smckusick 	} */ *ap;
53053838Spendry {
53154049Spendry 	struct uio *uio = ap->a_uio;
53253838Spendry 	struct filedesc *fdp;
53353838Spendry 	int i;
53453838Spendry 	int error;
53553838Spendry 
53659255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
53759255Spendry 	case Fctty:
53859255Spendry 		return (0);
53953838Spendry 
54059255Spendry 	case Fdesc:
54159255Spendry 		return (ENOTDIR);
54259255Spendry 
54359255Spendry 	default:
54459255Spendry 		break;
54559255Spendry 	}
54659255Spendry 
54754049Spendry 	fdp = uio->uio_procp->p_fd;
54859255Spendry 
54959255Spendry 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
55059255Spendry 		struct dirent d;
55159255Spendry 		struct dirent *dp = &d;
55259255Spendry 		struct dirtmp *dt;
55359255Spendry 
55459255Spendry 		i = uio->uio_offset / UIO_MX;
55559255Spendry 		error = 0;
55659255Spendry 
55759255Spendry 		while (uio->uio_resid > 0) {
55859255Spendry 			dt = &rootent[i];
55959255Spendry 			if (dt->d_fileno == 0) {
56059255Spendry 				/**eofflagp = 1;*/
56159255Spendry 				break;
56259255Spendry 			}
56359255Spendry 			i++;
56459255Spendry 
56559255Spendry 			switch (dt->d_fileno) {
56659255Spendry 			case FD_CTTY:
56759255Spendry 				if (cttyvp(uio->uio_procp) == NULL)
56859255Spendry 					continue;
56959255Spendry 				break;
57059255Spendry 
57159255Spendry 			case FD_STDIN:
57259255Spendry 			case FD_STDOUT:
57359255Spendry 			case FD_STDERR:
57459255Spendry 				if ((i-FD_STDIN) >= fdp->fd_nfiles)
57559255Spendry 					continue;
57659255Spendry 				if (fdp->fd_ofiles[i-FD_STDIN] == NULL)
57759255Spendry 					continue;
57859255Spendry 				break;
57959255Spendry 			}
58059255Spendry 			bzero(dp, UIO_MX);
58159255Spendry 			dp->d_fileno = dt->d_fileno;
58259255Spendry 			dp->d_namlen = dt->d_namlen;
58359255Spendry 			dp->d_type = DT_UNKNOWN;
58459255Spendry 			dp->d_reclen = dt->d_reclen;
58559255Spendry 			bcopy(dt->d_name, dp->d_name, dp->d_namlen+1);
58659255Spendry 			error = uiomove((caddr_t) dp, UIO_MX, uio);
58759255Spendry 			if (error)
58859255Spendry 				break;
58959255Spendry 		}
59059255Spendry 		uio->uio_offset = i * UIO_MX;
59159255Spendry 		return (error);
59259255Spendry 	}
59359255Spendry 
59454049Spendry 	i = uio->uio_offset / UIO_MX;
59553838Spendry 	error = 0;
59654049Spendry 	while (uio->uio_resid > 0) {
59753838Spendry 		if (i >= fdp->fd_nfiles) {
59854980Spendry 			/* *ap->a_eofflagp = 1; */
59953838Spendry 			break;
60053838Spendry 		}
60153838Spendry 		if (fdp->fd_ofiles[i] != NULL) {
60254981Spendry 			struct dirent d;
60354981Spendry 			struct dirent *dp = &d;
60453838Spendry 			char *cp = dp->d_name;
60553838Spendry 			bzero((caddr_t) dp, UIO_MX);
60653838Spendry 
60753838Spendry 			dp->d_namlen = sprintf(dp->d_name, "%d", i);
60853838Spendry 			/*
60953838Spendry 			 * Fill in the remaining fields
61053838Spendry 			 */
61153838Spendry 			dp->d_reclen = UIO_MX;
61254981Spendry 			dp->d_type = DT_UNKNOWN;
61359255Spendry 			dp->d_fileno = i + FD_STDIN;
61453838Spendry 			/*
61553838Spendry 			 * And ship to userland
61653838Spendry 			 */
61754049Spendry 			error = uiomove((caddr_t) dp, UIO_MX, uio);
61853838Spendry 			if (error)
61953838Spendry 				break;
62053838Spendry 		}
62153838Spendry 		i++;
62253838Spendry 	}
62353838Spendry 
62454049Spendry 	uio->uio_offset = i * UIO_MX;
62553838Spendry 	return (error);
62653838Spendry }
62753838Spendry 
62859255Spendry int
62959255Spendry fdesc_readlink(ap)
63059255Spendry 	struct vop_readlink_args /* {
63159255Spendry 		struct vnode *a_vp;
63259255Spendry 		struct uio *a_uio;
63359255Spendry 		struct ucred *a_cred;
63459255Spendry 	} */ *ap;
63559255Spendry {
63659255Spendry 	register struct vnode *vp = ap->a_vp;
63759255Spendry 	int error;
63859255Spendry 
63959255Spendry 	if (vp->v_type != VLNK)
64059255Spendry 		return (EPERM);
64159255Spendry 
64259255Spendry 	if (VTOFDESC(vp)->fd_type == Flink) {
64359255Spendry 		char *ln = VTOFDESC(vp)->fd_link;
64459255Spendry 		error = uiomove(ln, strlen(ln), ap->a_uio);
64559255Spendry 	} else {
64659255Spendry 		error = EOPNOTSUPP;
64759255Spendry 	}
64859255Spendry 
64959255Spendry 	return (error);
65059255Spendry }
65159255Spendry 
65259255Spendry fdesc_read(ap)
65359255Spendry 	struct vop_read_args /* {
65459255Spendry 		struct vnode *a_vp;
65559255Spendry 		struct uio *a_uio;
65659255Spendry 		int  a_ioflag;
65759255Spendry 		struct ucred *a_cred;
65859255Spendry 	} */ *ap;
65959255Spendry {
66059255Spendry 	int error = EOPNOTSUPP;
66159255Spendry 
66259255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
66359255Spendry 	case Fctty:
66459255Spendry 		error = cttyread(devctty, ap->a_uio, ap->a_ioflag);
66559255Spendry 		break;
66659255Spendry 
66759255Spendry 	default:
66859255Spendry 		error = EOPNOTSUPP;
66959255Spendry 		break;
67059255Spendry 	}
67159255Spendry 
67259255Spendry 	return (error);
67359255Spendry }
67459255Spendry 
67559255Spendry fdesc_write(ap)
67659255Spendry 	struct vop_write_args /* {
67759255Spendry 		struct vnode *a_vp;
67859255Spendry 		struct uio *a_uio;
67959255Spendry 		int  a_ioflag;
68059255Spendry 		struct ucred *a_cred;
68159255Spendry 	} */ *ap;
68259255Spendry {
68359255Spendry 	int error = EOPNOTSUPP;
68459255Spendry 
68559255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
68659255Spendry 	case Fctty:
68759255Spendry 		error = cttywrite(devctty, ap->a_uio, ap->a_ioflag);
68859255Spendry 		break;
68959255Spendry 
69059255Spendry 	default:
69159255Spendry 		error = EOPNOTSUPP;
69259255Spendry 		break;
69359255Spendry 	}
69459255Spendry 
69559255Spendry 	return (error);
69659255Spendry }
69759255Spendry 
69859255Spendry fdesc_ioctl(ap)
69959255Spendry 	struct vop_ioctl_args /* {
70059255Spendry 		struct vnode *a_vp;
70159255Spendry 		int  a_command;
70259255Spendry 		caddr_t  a_data;
70359255Spendry 		int  a_fflag;
70459255Spendry 		struct ucred *a_cred;
70559255Spendry 		struct proc *a_p;
70659255Spendry 	} */ *ap;
70759255Spendry {
70859255Spendry 	int error = EOPNOTSUPP;
70959255Spendry 
71059255Spendry #ifdef FDESC_DIAGNOSTIC
71159255Spendry 	printf("fdesc_ioctl: type = %d, command = %x\n",
71259255Spendry 			VTOFDESC(ap->a_vp)->fd_type, ap->a_command);
71359255Spendry #endif
71459255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
71559255Spendry 	case Fctty:
71659255Spendry 		error = cttyioctl(devctty, ap->a_command, ap->a_data,
71759255Spendry 					ap->a_fflag, ap->a_p);
71859255Spendry 		break;
71959255Spendry 
72059255Spendry 	default:
72159255Spendry 		error = EOPNOTSUPP;
72259255Spendry 		break;
72359255Spendry 	}
72459255Spendry 
72559255Spendry 	return (error);
72659255Spendry }
72759255Spendry 
72859255Spendry fdesc_select(ap)
72959255Spendry 	struct vop_select_args /* {
73059255Spendry 		struct vnode *a_vp;
73159255Spendry 		int  a_which;
73259255Spendry 		int  a_fflags;
73359255Spendry 		struct ucred *a_cred;
73459255Spendry 		struct proc *a_p;
73559255Spendry 	} */ *ap;
73659255Spendry {
73759255Spendry 	int error = EOPNOTSUPP;
73859255Spendry 
73959255Spendry 	switch (VTOFDESC(ap->a_vp)->fd_type) {
74059255Spendry 	case Fctty:
74159255Spendry 		error = cttyselect(devctty, ap->a_fflags, ap->a_p);
74259255Spendry 		break;
74359255Spendry 
74459255Spendry 	default:
74559255Spendry 		error = EOPNOTSUPP;
74659255Spendry 		break;
74759255Spendry 	}
74859255Spendry 
74959255Spendry 	return (error);
75059255Spendry }
75159255Spendry 
75255017Smckusick fdesc_inactive(ap)
75355017Smckusick 	struct vop_inactive_args /* {
75455017Smckusick 		struct vnode *a_vp;
75555017Smckusick 	} */ *ap;
75653838Spendry {
75754049Spendry 	struct vnode *vp = ap->a_vp;
75854049Spendry 
75953838Spendry 	/*
76053838Spendry 	 * Clear out the v_type field to avoid
76153838Spendry 	 * nasty things happening in vgone().
76253838Spendry 	 */
76354049Spendry 	vp->v_type = VNON;
76453838Spendry #ifdef FDESC_DIAGNOSTIC
76554049Spendry 	printf("fdesc_inactive(%x)\n", vp);
76653838Spendry #endif
76753838Spendry 	return (0);
76853838Spendry }
76953838Spendry 
77055017Smckusick fdesc_reclaim(ap)
77155017Smckusick 	struct vop_reclaim_args /* {
77255017Smckusick 		struct vnode *a_vp;
77355017Smckusick 	} */ *ap;
77453838Spendry {
77553838Spendry 	struct vnode *vp = ap->a_vp;
77659255Spendry 	int ix;
77759255Spendry 
77859255Spendry #ifdef FDESC_DIAGNOSTIC
77953838Spendry 	printf("fdesc_reclaim(%x)\n", vp);
78059255Spendry #endif
78159255Spendry 	ix = VTOFDESC(vp)->fd_ix;
78259255Spendry 	if (ix >= 0 && ix < FD_MAX) {
78359255Spendry 		if (fdescvp[ix] != vp)
78459255Spendry 			panic("fdesc_reclaim");
78559255Spendry 		fdescvp[ix] = 0;
78659255Spendry 	}
78753838Spendry 	if (vp->v_data) {
78853838Spendry 		FREE(vp->v_data, M_TEMP);
78953838Spendry 		vp->v_data = 0;
79053838Spendry 	}
79153838Spendry 	return (0);
79253838Spendry }
79353838Spendry 
79453838Spendry /*
79553838Spendry  * Print out the contents of a /dev/fd vnode.
79653838Spendry  */
79753838Spendry /* ARGSUSED */
79855017Smckusick fdesc_print(ap)
79955017Smckusick 	struct vop_print_args /* {
80055017Smckusick 		struct vnode *a_vp;
80155017Smckusick 	} */ *ap;
80253838Spendry {
80355017Smckusick 
80453838Spendry 	printf("tag VT_NON, fdesc vnode\n");
80555017Smckusick 	return (0);
80653838Spendry }
80753838Spendry 
80853838Spendry /*void*/
80955017Smckusick fdesc_vfree(ap)
81055017Smckusick 	struct vop_vfree_args /* {
81155017Smckusick 		struct vnode *a_pvp;
81255017Smckusick 		ino_t a_ino;
81355017Smckusick 		int a_mode;
81455017Smckusick 	} */ *ap;
81553838Spendry {
81653838Spendry 
81755017Smckusick 	return (0);
81853838Spendry }
81953838Spendry 
82053838Spendry /*
82153838Spendry  * /dev/fd vnode unsupported operation
82253838Spendry  */
82353838Spendry fdesc_enotsupp()
82453838Spendry {
82555017Smckusick 
82653838Spendry 	return (EOPNOTSUPP);
82753838Spendry }
82853838Spendry 
82953838Spendry /*
83053838Spendry  * /dev/fd "should never get here" operation
83153838Spendry  */
83253838Spendry fdesc_badop()
83353838Spendry {
83455017Smckusick 
83553838Spendry 	panic("fdesc: bad op");
83653838Spendry 	/* NOTREACHED */
83753838Spendry }
83853838Spendry 
83953838Spendry /*
84053838Spendry  * /dev/fd vnode null operation
84153838Spendry  */
84253838Spendry fdesc_nullop()
84353838Spendry {
84455017Smckusick 
84553838Spendry 	return (0);
84653838Spendry }
84753838Spendry 
84853838Spendry #define fdesc_create ((int (*) __P((struct  vop_create_args *)))fdesc_enotsupp)
84953838Spendry #define fdesc_mknod ((int (*) __P((struct  vop_mknod_args *)))fdesc_enotsupp)
85053838Spendry #define fdesc_close ((int (*) __P((struct  vop_close_args *)))nullop)
85153838Spendry #define fdesc_access ((int (*) __P((struct  vop_access_args *)))nullop)
85253838Spendry #define fdesc_mmap ((int (*) __P((struct  vop_mmap_args *)))fdesc_enotsupp)
85353838Spendry #define fdesc_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)
85453838Spendry #define fdesc_seek ((int (*) __P((struct  vop_seek_args *)))nullop)
85553838Spendry #define fdesc_remove ((int (*) __P((struct  vop_remove_args *)))fdesc_enotsupp)
85653838Spendry #define fdesc_link ((int (*) __P((struct  vop_link_args *)))fdesc_enotsupp)
85753838Spendry #define fdesc_rename ((int (*) __P((struct  vop_rename_args *)))fdesc_enotsupp)
85853838Spendry #define fdesc_mkdir ((int (*) __P((struct  vop_mkdir_args *)))fdesc_enotsupp)
85953838Spendry #define fdesc_rmdir ((int (*) __P((struct  vop_rmdir_args *)))fdesc_enotsupp)
86055017Smckusick #define fdesc_symlink ((int (*) __P((struct vop_symlink_args *)))fdesc_enotsupp)
86153838Spendry #define fdesc_abortop ((int (*) __P((struct  vop_abortop_args *)))nullop)
86253838Spendry #define fdesc_lock ((int (*) __P((struct  vop_lock_args *)))nullop)
86353838Spendry #define fdesc_unlock ((int (*) __P((struct  vop_unlock_args *)))nullop)
86453838Spendry #define fdesc_bmap ((int (*) __P((struct  vop_bmap_args *)))fdesc_badop)
86553838Spendry #define fdesc_strategy ((int (*) __P((struct  vop_strategy_args *)))fdesc_badop)
86653838Spendry #define fdesc_islocked ((int (*) __P((struct  vop_islocked_args *)))nullop)
86755017Smckusick #define fdesc_advlock ((int (*) __P((struct vop_advlock_args *)))fdesc_enotsupp)
86855170Spendry #define fdesc_blkatoff \
86955017Smckusick 	((int (*) __P((struct  vop_blkatoff_args *)))fdesc_enotsupp)
87053838Spendry #define fdesc_vget ((int (*) __P((struct  vop_vget_args *)))fdesc_enotsupp)
87153838Spendry #define fdesc_valloc ((int(*) __P(( \
87253838Spendry 		struct vnode *pvp, \
87353838Spendry 		int mode, \
87453838Spendry 		struct ucred *cred, \
87553838Spendry 		struct vnode **vpp))) fdesc_enotsupp)
87655170Spendry #define fdesc_truncate \
87755017Smckusick 	((int (*) __P((struct  vop_truncate_args *)))fdesc_enotsupp)
87853838Spendry #define fdesc_update ((int (*) __P((struct  vop_update_args *)))fdesc_enotsupp)
87953838Spendry #define fdesc_bwrite ((int (*) __P((struct  vop_bwrite_args *)))fdesc_enotsupp)
88053838Spendry 
88153838Spendry int (**fdesc_vnodeop_p)();
88253838Spendry struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
88353838Spendry 	{ &vop_default_desc, vn_default_error },
88453838Spendry 	{ &vop_lookup_desc, fdesc_lookup },	/* lookup */
88553838Spendry 	{ &vop_create_desc, fdesc_create },	/* create */
88653838Spendry 	{ &vop_mknod_desc, fdesc_mknod },	/* mknod */
88755017Smckusick 	{ &vop_open_desc, fdesc_open },		/* open */
88853838Spendry 	{ &vop_close_desc, fdesc_close },	/* close */
88953838Spendry 	{ &vop_access_desc, fdesc_access },	/* access */
89053838Spendry 	{ &vop_getattr_desc, fdesc_getattr },	/* getattr */
89153838Spendry 	{ &vop_setattr_desc, fdesc_setattr },	/* setattr */
89255017Smckusick 	{ &vop_read_desc, fdesc_read },		/* read */
89353838Spendry 	{ &vop_write_desc, fdesc_write },	/* write */
89453838Spendry 	{ &vop_ioctl_desc, fdesc_ioctl },	/* ioctl */
89553838Spendry 	{ &vop_select_desc, fdesc_select },	/* select */
89655017Smckusick 	{ &vop_mmap_desc, fdesc_mmap },		/* mmap */
89753838Spendry 	{ &vop_fsync_desc, fdesc_fsync },	/* fsync */
89855017Smckusick 	{ &vop_seek_desc, fdesc_seek },		/* seek */
89953838Spendry 	{ &vop_remove_desc, fdesc_remove },	/* remove */
90055017Smckusick 	{ &vop_link_desc, fdesc_link },		/* link */
90153838Spendry 	{ &vop_rename_desc, fdesc_rename },	/* rename */
90253838Spendry 	{ &vop_mkdir_desc, fdesc_mkdir },	/* mkdir */
90353838Spendry 	{ &vop_rmdir_desc, fdesc_rmdir },	/* rmdir */
90453838Spendry 	{ &vop_symlink_desc, fdesc_symlink },	/* symlink */
90553838Spendry 	{ &vop_readdir_desc, fdesc_readdir },	/* readdir */
90653838Spendry 	{ &vop_readlink_desc, fdesc_readlink },	/* readlink */
90753838Spendry 	{ &vop_abortop_desc, fdesc_abortop },	/* abortop */
90853838Spendry 	{ &vop_inactive_desc, fdesc_inactive },	/* inactive */
90953838Spendry 	{ &vop_reclaim_desc, fdesc_reclaim },	/* reclaim */
91055017Smckusick 	{ &vop_lock_desc, fdesc_lock },		/* lock */
91153838Spendry 	{ &vop_unlock_desc, fdesc_unlock },	/* unlock */
91255017Smckusick 	{ &vop_bmap_desc, fdesc_bmap },		/* bmap */
91353838Spendry 	{ &vop_strategy_desc, fdesc_strategy },	/* strategy */
91453838Spendry 	{ &vop_print_desc, fdesc_print },	/* print */
91553838Spendry 	{ &vop_islocked_desc, fdesc_islocked },	/* islocked */
91653838Spendry 	{ &vop_advlock_desc, fdesc_advlock },	/* advlock */
91753838Spendry 	{ &vop_blkatoff_desc, fdesc_blkatoff },	/* blkatoff */
91853838Spendry 	{ &vop_valloc_desc, fdesc_valloc },	/* valloc */
91953838Spendry 	{ &vop_vfree_desc, fdesc_vfree },	/* vfree */
92053838Spendry 	{ &vop_truncate_desc, fdesc_truncate },	/* truncate */
92153838Spendry 	{ &vop_update_desc, fdesc_update },	/* update */
92253838Spendry 	{ &vop_bwrite_desc, fdesc_bwrite },	/* bwrite */
92353838Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
92453838Spendry };
92553838Spendry struct vnodeopv_desc fdesc_vnodeop_opv_desc =
92653838Spendry 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
927