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