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