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