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