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