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