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