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